COFF: Split writeTo in preparation for supporting 32-bit x86.

llvm-svn: 241638
This commit is contained in:
Rui Ueyama 2015-07-07 22:49:21 +00:00
parent 489abd7046
commit 661a4e7ab6
2 changed files with 21 additions and 15 deletions

View File

@ -48,20 +48,9 @@ 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); }
void SectionChunk::writeTo(uint8_t *Buf) {
if (!hasData())
return;
// Copy section contents from source object file to output file.
ArrayRef<uint8_t> A = getContents();
memcpy(Buf + FileOff, A.data(), A.size());
// Apply relocations.
for (const coff_relocation &Rel : Relocs) {
uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
uint64_t S = cast<Defined>(Body)->getRVA();
uint64_t P = RVA + Rel.VirtualAddress;
switch (Rel.Type) {
void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, uint64_t S,
uint64_t P) {
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;
@ -77,6 +66,22 @@ void SectionChunk::writeTo(uint8_t *Buf) {
llvm::report_fatal_error("Unsupported relocation type");
}
}
void SectionChunk::writeTo(uint8_t *Buf) {
if (!hasData())
return;
// Copy section contents from source object file to output file.
ArrayRef<uint8_t> A = getContents();
memcpy(Buf + FileOff, A.data(), A.size());
// Apply relocations.
for (const coff_relocation &Rel : Relocs) {
uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
uint64_t S = cast<Defined>(Body)->getRVA();
uint64_t P = RVA + Rel.VirtualAddress;
applyRelX64(Off, Rel.Type, S, P);
}
}
void SectionChunk::addAssociative(SectionChunk *Child) {

View File

@ -136,6 +136,7 @@ public:
StringRef getSectionName() const override { return SectionName; }
void getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) override;
bool isCOMDAT() const;
void applyRelX64(uint8_t *Off, uint16_t Type, uint64_t S, uint64_t P);
// Called if the garbage collector decides to not include this chunk
// in a final output. It's supposed to print out a log message to stdout.