2015-05-29 03:09:30 +08:00
|
|
|
//===- Chunks.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Chunks.h"
|
2015-08-06 22:58:50 +08:00
|
|
|
#include "Error.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "InputFiles.h"
|
2015-08-06 03:51:28 +08:00
|
|
|
#include "Symbols.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-06-20 15:25:45 +08:00
|
|
|
#include <algorithm>
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-06-07 06:46:15 +08:00
|
|
|
using namespace llvm;
|
2015-05-29 03:09:30 +08:00
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
|
|
|
using namespace llvm::COFF;
|
2015-07-25 07:51:14 +08:00
|
|
|
using llvm::support::ulittle32_t;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-06-24 08:12:36 +08:00
|
|
|
SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H)
|
2015-09-26 00:20:24 +08:00
|
|
|
: Chunk(SectionKind), Repl(this), File(F), Header(H),
|
2015-06-26 01:43:37 +08:00
|
|
|
Relocs(File->getCOFFObj()->getRelocations(Header)),
|
|
|
|
NumRelocs(std::distance(Relocs.begin(), Relocs.end())) {
|
2015-05-29 03:09:30 +08:00
|
|
|
// Initialize SectionName.
|
|
|
|
File->getCOFFObj()->getSectionName(Header, SectionName);
|
2015-06-10 12:21:47 +08:00
|
|
|
|
2016-03-18 00:58:08 +08:00
|
|
|
Align = Header->getAlignment();
|
2015-09-17 05:40:47 +08:00
|
|
|
|
|
|
|
// Only COMDAT sections are subject of dead-stripping.
|
|
|
|
Live = !isCOMDAT();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 08:33:38 +08:00
|
|
|
static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
|
|
|
|
static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
|
|
|
|
static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
|
2015-07-25 11:03:46 +08:00
|
|
|
static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); }
|
2015-06-25 08:33:38 +08:00
|
|
|
|
2015-07-30 00:30:45 +08:00
|
|
|
void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, Defined *Sym,
|
2015-09-20 07:28:57 +08:00
|
|
|
uint64_t P) const {
|
2015-07-30 00:30:45 +08:00
|
|
|
uint64_t S = Sym->getRVA();
|
2015-07-08 06:49:21 +08:00
|
|
|
switch (Type) {
|
|
|
|
case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
|
|
|
|
case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
|
|
|
|
case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
|
|
|
|
case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
|
|
|
|
case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
|
|
|
|
case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
|
|
|
|
case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
|
|
|
|
case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
|
|
|
|
case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
|
2015-07-30 00:30:45 +08:00
|
|
|
case IMAGE_REL_AMD64_SECTION: add16(Off, Sym->getSectionIndex()); break;
|
|
|
|
case IMAGE_REL_AMD64_SECREL: add32(Off, Sym->getSecrel()); break;
|
2015-07-08 06:49:21 +08:00
|
|
|
default:
|
2015-08-06 22:58:50 +08:00
|
|
|
error("Unsupported relocation type");
|
2015-07-08 06:49:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 00:30:45 +08:00
|
|
|
void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, Defined *Sym,
|
2015-09-20 07:28:57 +08:00
|
|
|
uint64_t P) const {
|
2015-07-30 00:30:45 +08:00
|
|
|
uint64_t S = Sym->getRVA();
|
2015-07-08 09:45:29 +08:00
|
|
|
switch (Type) {
|
|
|
|
case IMAGE_REL_I386_ABSOLUTE: break;
|
|
|
|
case IMAGE_REL_I386_DIR32: add32(Off, S + Config->ImageBase); break;
|
|
|
|
case IMAGE_REL_I386_DIR32NB: add32(Off, S); break;
|
|
|
|
case IMAGE_REL_I386_REL32: add32(Off, S - P - 4); break;
|
2015-07-30 00:30:45 +08:00
|
|
|
case IMAGE_REL_I386_SECTION: add16(Off, Sym->getSectionIndex()); break;
|
|
|
|
case IMAGE_REL_I386_SECREL: add32(Off, Sym->getSecrel()); break;
|
2015-07-08 09:45:29 +08:00
|
|
|
default:
|
2015-08-06 22:58:50 +08:00
|
|
|
error("Unsupported relocation type");
|
2015-07-08 09:45:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 03:40:07 +08:00
|
|
|
static void applyMOV(uint8_t *Off, uint16_t V) {
|
|
|
|
or16(Off, ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
|
|
|
|
or16(Off + 2, ((V & 0x700) << 4) | (V & 0xff));
|
|
|
|
}
|
|
|
|
|
2015-07-25 11:03:46 +08:00
|
|
|
static void applyMOV32T(uint8_t *Off, uint32_t V) {
|
2015-08-06 03:40:07 +08:00
|
|
|
applyMOV(Off, V); // set MOVW operand
|
|
|
|
applyMOV(Off + 4, V >> 16); // set MOVT operand
|
|
|
|
}
|
|
|
|
|
|
|
|
static void applyBranch20T(uint8_t *Off, int32_t V) {
|
|
|
|
uint32_t S = V < 0 ? 1 : 0;
|
|
|
|
uint32_t J1 = (V >> 19) & 1;
|
|
|
|
uint32_t J2 = (V >> 18) & 1;
|
|
|
|
or16(Off, (S << 10) | ((V >> 12) & 0x3f));
|
|
|
|
or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
|
2015-07-25 11:03:46 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 03:40:07 +08:00
|
|
|
static void applyBranch24T(uint8_t *Off, int32_t V) {
|
2015-07-25 11:19:34 +08:00
|
|
|
uint32_t S = V < 0 ? 1 : 0;
|
|
|
|
uint32_t J1 = ((~V >> 23) & 1) ^ S;
|
|
|
|
uint32_t J2 = ((~V >> 22) & 1) ^ S;
|
2015-08-06 03:40:07 +08:00
|
|
|
or16(Off, (S << 10) | ((V >> 12) & 0x3ff));
|
|
|
|
or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
|
2015-07-25 11:19:34 +08:00
|
|
|
}
|
|
|
|
|
2015-07-30 00:30:45 +08:00
|
|
|
void SectionChunk::applyRelARM(uint8_t *Off, uint16_t Type, Defined *Sym,
|
2015-09-20 07:28:57 +08:00
|
|
|
uint64_t P) const {
|
2015-07-30 00:30:45 +08:00
|
|
|
uint64_t S = Sym->getRVA();
|
2015-07-30 03:25:00 +08:00
|
|
|
// Pointer to thumb code must have the LSB set.
|
|
|
|
if (Sym->isExecutable())
|
|
|
|
S |= 1;
|
2015-07-25 11:03:46 +08:00
|
|
|
switch (Type) {
|
2015-07-25 11:19:34 +08:00
|
|
|
case IMAGE_REL_ARM_ADDR32: add32(Off, S + Config->ImageBase); break;
|
|
|
|
case IMAGE_REL_ARM_ADDR32NB: add32(Off, S); break;
|
|
|
|
case IMAGE_REL_ARM_MOV32T: applyMOV32T(Off, S + Config->ImageBase); break;
|
2015-08-06 03:40:07 +08:00
|
|
|
case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(Off, S - P - 4); break;
|
|
|
|
case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(Off, S - P - 4); break;
|
|
|
|
case IMAGE_REL_ARM_BLX23T: applyBranch24T(Off, S - P - 4); break;
|
2015-07-25 11:03:46 +08:00
|
|
|
default:
|
2015-08-06 22:58:50 +08:00
|
|
|
error("Unsupported relocation type");
|
2015-07-25 11:03:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void SectionChunk::writeTo(uint8_t *Buf) const {
|
2015-05-29 04:04:51 +08:00
|
|
|
if (!hasData())
|
|
|
|
return;
|
2015-06-06 12:07:39 +08:00
|
|
|
// Copy section contents from source object file to output file.
|
2015-06-26 01:56:36 +08:00
|
|
|
ArrayRef<uint8_t> A = getContents();
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, A.data(), A.size());
|
2015-06-06 12:07:39 +08:00
|
|
|
|
|
|
|
// Apply relocations.
|
2015-06-25 08:33:38 +08:00
|
|
|
for (const coff_relocation &Rel : Relocs) {
|
2015-08-14 11:30:59 +08:00
|
|
|
uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress;
|
2015-07-02 08:21:11 +08:00
|
|
|
SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
|
2015-07-30 00:30:45 +08:00
|
|
|
Defined *Sym = cast<Defined>(Body);
|
2015-06-25 08:33:38 +08:00
|
|
|
uint64_t P = RVA + Rel.VirtualAddress;
|
2015-07-26 05:54:50 +08:00
|
|
|
switch (Config->Machine) {
|
|
|
|
case AMD64:
|
2015-07-30 00:30:45 +08:00
|
|
|
applyRelX64(Off, Rel.Type, Sym, P);
|
2015-07-08 09:45:29 +08:00
|
|
|
break;
|
2015-07-26 05:54:50 +08:00
|
|
|
case I386:
|
2015-07-30 00:30:45 +08:00
|
|
|
applyRelX86(Off, Rel.Type, Sym, P);
|
2015-07-08 09:45:29 +08:00
|
|
|
break;
|
2015-07-26 05:54:50 +08:00
|
|
|
case ARMNT:
|
2015-07-30 00:30:45 +08:00
|
|
|
applyRelARM(Off, Rel.Type, Sym, P);
|
2015-07-25 11:03:46 +08:00
|
|
|
break;
|
2015-07-08 09:45:29 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown machine type");
|
|
|
|
}
|
2015-06-25 08:33:38 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SectionChunk::addAssociative(SectionChunk *Child) {
|
|
|
|
AssocChildren.push_back(Child);
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
static uint8_t getBaserelType(const coff_relocation &Rel) {
|
2015-07-26 05:54:50 +08:00
|
|
|
switch (Config->Machine) {
|
|
|
|
case AMD64:
|
2015-07-25 09:44:32 +08:00
|
|
|
if (Rel.Type == IMAGE_REL_AMD64_ADDR64)
|
|
|
|
return IMAGE_REL_BASED_DIR64;
|
|
|
|
return IMAGE_REL_BASED_ABSOLUTE;
|
2015-07-26 05:54:50 +08:00
|
|
|
case I386:
|
2015-07-25 09:44:32 +08:00
|
|
|
if (Rel.Type == IMAGE_REL_I386_DIR32)
|
|
|
|
return IMAGE_REL_BASED_HIGHLOW;
|
|
|
|
return IMAGE_REL_BASED_ABSOLUTE;
|
2015-07-26 05:54:50 +08:00
|
|
|
case ARMNT:
|
2015-07-25 11:03:46 +08:00
|
|
|
if (Rel.Type == IMAGE_REL_ARM_ADDR32)
|
|
|
|
return IMAGE_REL_BASED_HIGHLOW;
|
|
|
|
if (Rel.Type == IMAGE_REL_ARM_MOV32T)
|
|
|
|
return IMAGE_REL_BASED_ARM_MOV32T;
|
|
|
|
return IMAGE_REL_BASED_ABSOLUTE;
|
2015-07-10 04:36:59 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown machine type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 09:23:58 +08:00
|
|
|
// Windows-specific.
|
2015-07-10 04:36:59 +08:00
|
|
|
// Collect all locations that contain absolute addresses, which need to be
|
|
|
|
// fixed by the loader if load-time relocation is needed.
|
2015-06-15 09:23:58 +08:00
|
|
|
// Only called when base relocation is enabled.
|
2015-07-25 09:44:32 +08:00
|
|
|
void SectionChunk::getBaserels(std::vector<Baserel> *Res) {
|
2015-06-25 08:33:38 +08:00
|
|
|
for (const coff_relocation &Rel : Relocs) {
|
2015-07-25 09:44:32 +08:00
|
|
|
uint8_t Ty = getBaserelType(Rel);
|
|
|
|
if (Ty == IMAGE_REL_BASED_ABSOLUTE)
|
2015-06-15 09:23:58 +08:00
|
|
|
continue;
|
2015-07-02 08:21:11 +08:00
|
|
|
SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
|
2015-07-25 06:58:44 +08:00
|
|
|
if (isa<DefinedAbsolute>(Body))
|
2015-06-15 09:23:58 +08:00
|
|
|
continue;
|
2015-07-25 09:44:32 +08:00
|
|
|
Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
|
2015-06-15 09:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
bool SectionChunk::hasData() const {
|
|
|
|
return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SectionChunk::getPermissions() const {
|
|
|
|
return Header->Characteristics & PermMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SectionChunk::isCOMDAT() const {
|
|
|
|
return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
|
|
|
|
}
|
|
|
|
|
2015-06-26 03:10:58 +08:00
|
|
|
void SectionChunk::printDiscardedMessage() const {
|
2015-09-17 05:30:40 +08:00
|
|
|
// Removed by dead-stripping. If it's removed by ICF, ICF already
|
|
|
|
// printed out the name, so don't repeat that here.
|
2015-09-26 00:20:24 +08:00
|
|
|
if (Sym && this == Repl)
|
2015-09-17 05:30:40 +08:00
|
|
|
llvm::outs() << "Discarded " << Sym->getName() << "\n";
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 08:00:52 +08:00
|
|
|
StringRef SectionChunk::getDebugName() {
|
2015-08-21 15:01:10 +08:00
|
|
|
if (Sym)
|
|
|
|
return Sym->getName();
|
|
|
|
return "";
|
2015-06-24 08:00:52 +08:00
|
|
|
}
|
|
|
|
|
2015-06-26 01:56:36 +08:00
|
|
|
ArrayRef<uint8_t> SectionChunk::getContents() const {
|
|
|
|
ArrayRef<uint8_t> A;
|
|
|
|
File->getCOFFObj()->getSectionContents(Header, A);
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2015-09-22 03:36:51 +08:00
|
|
|
void SectionChunk::replace(SectionChunk *Other) {
|
2015-09-26 00:20:24 +08:00
|
|
|
Other->Repl = Repl;
|
2015-09-22 03:36:51 +08:00
|
|
|
Other->Live = false;
|
2015-06-24 12:36:52 +08:00
|
|
|
}
|
|
|
|
|
2015-06-08 11:17:07 +08:00
|
|
|
CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
|
2015-06-20 15:25:45 +08:00
|
|
|
// Common symbols are aligned on natural boundaries up to 32 bytes.
|
|
|
|
// This is what MSVC link.exe does.
|
|
|
|
Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
|
2015-06-08 11:17:07 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
uint32_t CommonChunk::getPermissions() const {
|
|
|
|
return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
|
|
|
|
IMAGE_SCN_MEM_WRITE;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void StringChunk::writeTo(uint8_t *Buf) const {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, Str.data(), Str.size());
|
2015-05-29 03:45:43 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:16:06 +08:00
|
|
|
ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
|
2015-06-27 02:28:56 +08:00
|
|
|
// Intel Optimization Manual says that all branch targets
|
|
|
|
// should be 16-byte aligned. MSVC linker does this too.
|
2015-07-26 05:54:50 +08:00
|
|
|
Align = 16;
|
2015-06-27 02:28:56 +08:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void ImportThunkChunkX64::writeTo(uint8_t *Buf) const {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
|
2015-07-25 09:16:06 +08:00
|
|
|
// The first two bytes is a JMP instruction. Fill its operand.
|
2015-08-14 11:30:59 +08:00
|
|
|
write32le(Buf + OutputSectionOff + 2, ImpSymbol->getRVA() - RVA - getSize());
|
2015-07-25 09:16:06 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
|
|
|
|
Res->emplace_back(getRVA() + 2);
|
2015-07-15 08:25:38 +08:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
|
2015-06-06 12:07:39 +08:00
|
|
|
// The first two bytes is a JMP instruction. Fill its operand.
|
2015-08-14 11:30:59 +08:00
|
|
|
write32le(Buf + OutputSectionOff + 2,
|
|
|
|
ImpSymbol->getRVA() + Config->ImageBase);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 11:39:29 +08:00
|
|
|
void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
|
|
|
|
Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM));
|
2015-07-25 11:39:29 +08:00
|
|
|
// Fix mov.w and mov.t operands.
|
2015-08-14 11:30:59 +08:00
|
|
|
applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase);
|
2015-07-25 11:39:29 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
|
|
|
|
Res->emplace_back(getRVA());
|
2015-07-03 04:33:50 +08:00
|
|
|
}
|
|
|
|
|
2015-07-10 05:15:58 +08:00
|
|
|
size_t LocalImportChunk::getSize() const {
|
|
|
|
return Config->is64() ? 8 : 4;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void LocalImportChunk::writeTo(uint8_t *Buf) const {
|
2015-07-10 05:15:58 +08:00
|
|
|
if (Config->is64()) {
|
2015-08-14 11:30:59 +08:00
|
|
|
write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
|
2015-07-10 05:15:58 +08:00
|
|
|
} else {
|
2015-08-14 11:30:59 +08:00
|
|
|
write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
|
2015-07-10 05:15:58 +08:00
|
|
|
}
|
2015-07-03 04:33:50 +08:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void SEHTableChunk::writeTo(uint8_t *Buf) const {
|
2015-08-14 11:30:59 +08:00
|
|
|
ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff);
|
2015-07-25 07:51:14 +08:00
|
|
|
size_t Cnt = 0;
|
|
|
|
for (Defined *D : Syms)
|
|
|
|
Begin[Cnt++] = D->getRVA();
|
|
|
|
std::sort(Begin, Begin + Cnt);
|
|
|
|
}
|
|
|
|
|
2015-06-15 09:23:58 +08:00
|
|
|
// Windows-specific.
|
|
|
|
// This class represents a block in .reloc section.
|
2015-07-25 09:44:32 +08:00
|
|
|
BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
|
2015-06-15 09:23:58 +08:00
|
|
|
// Block header consists of 4 byte page RVA and 4 byte block size.
|
|
|
|
// Each entry is 2 byte. Last entry may be padding.
|
2016-01-15 04:53:50 +08:00
|
|
|
Data.resize(alignTo((End - Begin) * 2 + 8, 4));
|
2015-06-15 09:23:58 +08:00
|
|
|
uint8_t *P = Data.data();
|
|
|
|
write32le(P, Page);
|
|
|
|
write32le(P + 4, Data.size());
|
|
|
|
P += 8;
|
2015-07-25 09:44:32 +08:00
|
|
|
for (Baserel *I = Begin; I != End; ++I) {
|
|
|
|
write16le(P, (I->Type << 12) | (I->RVA - Page));
|
2015-06-15 09:23:58 +08:00
|
|
|
P += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void BaserelChunk::writeTo(uint8_t *Buf) const {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, Data.data(), Data.size());
|
2015-06-15 09:23:58 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
uint8_t Baserel::getDefaultType() {
|
2015-07-26 05:54:50 +08:00
|
|
|
switch (Config->Machine) {
|
|
|
|
case AMD64:
|
2015-07-25 09:44:32 +08:00
|
|
|
return IMAGE_REL_BASED_DIR64;
|
2015-07-26 05:54:50 +08:00
|
|
|
case I386:
|
2015-07-25 09:44:32 +08:00
|
|
|
return IMAGE_REL_BASED_HIGHLOW;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown machine type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|