[COFF] Simplify Chunk::writeTo and remove OutputSectionOff, NFC

Summary:
Prior to this change, every implementation of writeTo would add
OutputSectionOff to the output section buffer start before writing data.
Instead, do this math in the caller, so that it can be written once
instead of many times.

The output section offset is always equivalent to the difference between
the chunk RVA and the output section RVA, so we can replace the one
remaining usage of OutputSectionOff with that subtraction.

This doesn't change the size of SectionChunk because of alignment
requirements, but I will rearrange the fields in a follow-up change to
accomplish that.

Reviewers: ruiu, aganea

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D61696

llvm-svn: 360376
This commit is contained in:
Reid Kleckner 2019-05-09 21:21:22 +00:00
parent df225764b7
commit 4c64256b51
5 changed files with 78 additions and 83 deletions

View File

@ -344,7 +344,7 @@ void SectionChunk::writeTo(uint8_t *Buf) const {
// Copy section contents from source object file to output file. // Copy section contents from source object file to output file.
ArrayRef<uint8_t> A = getContents(); ArrayRef<uint8_t> A = getContents();
if (!A.empty()) if (!A.empty())
memcpy(Buf + OutputSectionOff, A.data(), A.size()); memcpy(Buf, A.data(), A.size());
// Apply relocations. // Apply relocations.
size_t InputSize = getSize(); size_t InputSize = getSize();
@ -360,7 +360,7 @@ void SectionChunk::writeTo(uint8_t *Buf) const {
continue; continue;
} }
uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress; uint8_t *Off = Buf + Rel.VirtualAddress;
auto *Sym = auto *Sym =
dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex)); dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex));
@ -649,8 +649,8 @@ uint32_t CommonChunk::getOutputCharacteristics() const {
} }
void StringChunk::writeTo(uint8_t *Buf) const { void StringChunk::writeTo(uint8_t *Buf) const {
memcpy(Buf + OutputSectionOff, Str.data(), Str.size()); memcpy(Buf, Str.data(), Str.size());
Buf[OutputSectionOff + Str.size()] = '\0'; Buf[Str.size()] = '\0';
} }
ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) { ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
@ -660,9 +660,9 @@ ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
} }
void ImportThunkChunkX64::writeTo(uint8_t *Buf) const { void ImportThunkChunkX64::writeTo(uint8_t *Buf) const {
memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86)); memcpy(Buf, ImportThunkX86, sizeof(ImportThunkX86));
// The first two bytes is a JMP instruction. Fill its operand. // The first two bytes is a JMP instruction. Fill its operand.
write32le(Buf + OutputSectionOff + 2, ImpSymbol->getRVA() - RVA - getSize()); write32le(Buf + 2, ImpSymbol->getRVA() - RVA - getSize());
} }
void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) { void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
@ -670,9 +670,9 @@ void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
} }
void ImportThunkChunkX86::writeTo(uint8_t *Buf) const { void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86)); memcpy(Buf, ImportThunkX86, sizeof(ImportThunkX86));
// The first two bytes is a JMP instruction. Fill its operand. // The first two bytes is a JMP instruction. Fill its operand.
write32le(Buf + OutputSectionOff + 2, write32le(Buf + 2,
ImpSymbol->getRVA() + Config->ImageBase); ImpSymbol->getRVA() + Config->ImageBase);
} }
@ -681,16 +681,16 @@ void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
} }
void ImportThunkChunkARM::writeTo(uint8_t *Buf) const { void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM)); memcpy(Buf, ImportThunkARM, sizeof(ImportThunkARM));
// Fix mov.w and mov.t operands. // Fix mov.w and mov.t operands.
applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase); applyMOV32T(Buf, ImpSymbol->getRVA() + Config->ImageBase);
} }
void ImportThunkChunkARM64::writeTo(uint8_t *Buf) const { void ImportThunkChunkARM64::writeTo(uint8_t *Buf) const {
int64_t Off = ImpSymbol->getRVA() & 0xfff; int64_t Off = ImpSymbol->getRVA() & 0xfff;
memcpy(Buf + OutputSectionOff, ImportThunkARM64, sizeof(ImportThunkARM64)); memcpy(Buf, ImportThunkARM64, sizeof(ImportThunkARM64));
applyArm64Addr(Buf + OutputSectionOff, ImpSymbol->getRVA(), RVA, 12); applyArm64Addr(Buf, ImpSymbol->getRVA(), RVA, 12);
applyArm64Ldr(Buf + OutputSectionOff + 4, Off); applyArm64Ldr(Buf + 4, Off);
} }
// A Thumb2, PIC, non-interworking range extension thunk. // A Thumb2, PIC, non-interworking range extension thunk.
@ -708,8 +708,8 @@ size_t RangeExtensionThunkARM::getSize() const {
void RangeExtensionThunkARM::writeTo(uint8_t *Buf) const { void RangeExtensionThunkARM::writeTo(uint8_t *Buf) const {
assert(Config->Machine == ARMNT); assert(Config->Machine == ARMNT);
uint64_t Offset = Target->getRVA() - RVA - 12; uint64_t Offset = Target->getRVA() - RVA - 12;
memcpy(Buf + OutputSectionOff, ArmThunk, sizeof(ArmThunk)); memcpy(Buf, ArmThunk, sizeof(ArmThunk));
applyMOV32T(Buf + OutputSectionOff, uint32_t(Offset)); applyMOV32T(Buf, uint32_t(Offset));
} }
// A position independent ARM64 adrp+add thunk, with a maximum range of // A position independent ARM64 adrp+add thunk, with a maximum range of
@ -727,9 +727,9 @@ size_t RangeExtensionThunkARM64::getSize() const {
void RangeExtensionThunkARM64::writeTo(uint8_t *Buf) const { void RangeExtensionThunkARM64::writeTo(uint8_t *Buf) const {
assert(Config->Machine == ARM64); assert(Config->Machine == ARM64);
memcpy(Buf + OutputSectionOff, Arm64Thunk, sizeof(Arm64Thunk)); memcpy(Buf, Arm64Thunk, sizeof(Arm64Thunk));
applyArm64Addr(Buf + OutputSectionOff + 0, Target->getRVA(), RVA, 12); applyArm64Addr(Buf + 0, Target->getRVA(), RVA, 12);
applyArm64Imm(Buf + OutputSectionOff + 4, Target->getRVA() & 0xfff, 0); applyArm64Imm(Buf + 4, Target->getRVA() & 0xfff, 0);
} }
void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) { void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
@ -740,14 +740,14 @@ size_t LocalImportChunk::getSize() const { return Config->Wordsize; }
void LocalImportChunk::writeTo(uint8_t *Buf) const { void LocalImportChunk::writeTo(uint8_t *Buf) const {
if (Config->is64()) { if (Config->is64()) {
write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase); write64le(Buf, Sym->getRVA() + Config->ImageBase);
} else { } else {
write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase); write32le(Buf, Sym->getRVA() + Config->ImageBase);
} }
} }
void RVATableChunk::writeTo(uint8_t *Buf) const { void RVATableChunk::writeTo(uint8_t *Buf) const {
ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff); ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf);
size_t Cnt = 0; size_t Cnt = 0;
for (const ChunkAndOffset &CO : Syms) for (const ChunkAndOffset &CO : Syms)
Begin[Cnt++] = CO.InputChunk->getRVA() + CO.Offset; Begin[Cnt++] = CO.InputChunk->getRVA() + CO.Offset;
@ -768,7 +768,7 @@ void PseudoRelocTableChunk::writeTo(uint8_t *Buf) const {
if (Relocs.empty()) if (Relocs.empty())
return; return;
ulittle32_t *Table = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff); ulittle32_t *Table = reinterpret_cast<ulittle32_t *>(Buf);
// This is the list header, to signal the runtime pseudo relocation v2 // This is the list header, to signal the runtime pseudo relocation v2
// format. // format.
Table[0] = 0; Table[0] = 0;
@ -838,7 +838,7 @@ BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
} }
void BaserelChunk::writeTo(uint8_t *Buf) const { void BaserelChunk::writeTo(uint8_t *Buf) const {
memcpy(Buf + OutputSectionOff, Data.data(), Data.size()); memcpy(Buf, Data.data(), Data.size());
} }
uint8_t Baserel::getDefaultType() { uint8_t Baserel::getDefaultType() {
@ -883,7 +883,6 @@ void MergeChunk::finalizeContents() {
size_t Off = Builder.getOffset(toStringRef(C->getContents())); size_t Off = Builder.getOffset(toStringRef(C->getContents()));
C->setOutputSection(Out); C->setOutputSection(Out);
C->setRVA(RVA + Off); C->setRVA(RVA + Off);
C->OutputSectionOff = OutputSectionOff + Off;
} }
} }
@ -896,7 +895,7 @@ size_t MergeChunk::getSize() const {
} }
void MergeChunk::writeTo(uint8_t *Buf) const { void MergeChunk::writeTo(uint8_t *Buf) const {
Builder.write(Buf + OutputSectionOff); Builder.write(Buf);
} }
// MinGW specific. // MinGW specific.
@ -904,9 +903,9 @@ size_t AbsolutePointerChunk::getSize() const { return Config->Wordsize; }
void AbsolutePointerChunk::writeTo(uint8_t *Buf) const { void AbsolutePointerChunk::writeTo(uint8_t *Buf) const {
if (Config->is64()) { if (Config->is64()) {
write64le(Buf + OutputSectionOff, Value); write64le(Buf, Value);
} else { } else {
write32le(Buf + OutputSectionOff, Value); write32le(Buf, Value);
} }
} }

View File

@ -121,10 +121,6 @@ protected:
// The RVA of this chunk in the output. The writer sets a value. // The RVA of this chunk in the output. The writer sets a value.
uint32_t RVA = 0; uint32_t RVA = 0;
public:
// The offset from beginning of the output section. The writer sets a value.
uint32_t OutputSectionOff = 0;
protected: protected:
// The output section for this chunk. // The output section for this chunk.
OutputSection *Out = nullptr; OutputSection *Out = nullptr;

View File

@ -46,9 +46,9 @@ public:
} }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize()); memset(Buf, 0, getSize());
write16le(Buf + OutputSectionOff, Hint); write16le(Buf, Hint);
memcpy(Buf + OutputSectionOff + 2, Name.data(), Name.size()); memcpy(Buf + 2, Name.data(), Name.size());
} }
private: private:
@ -64,9 +64,9 @@ public:
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
if (Config->is64()) if (Config->is64())
write64le(Buf + OutputSectionOff, HintName->getRVA()); write64le(Buf, HintName->getRVA());
else else
write32le(Buf + OutputSectionOff, HintName->getRVA()); write32le(Buf, HintName->getRVA());
} }
Chunk *HintName; Chunk *HintName;
@ -86,9 +86,9 @@ public:
// An import-by-ordinal slot has MSB 1 to indicate that // An import-by-ordinal slot has MSB 1 to indicate that
// this is import-by-ordinal (and not import-by-name). // this is import-by-ordinal (and not import-by-name).
if (Config->is64()) { if (Config->is64()) {
write64le(Buf + OutputSectionOff, (1ULL << 63) | Ordinal); write64le(Buf, (1ULL << 63) | Ordinal);
} else { } else {
write32le(Buf + OutputSectionOff, (1ULL << 31) | Ordinal); write32le(Buf, (1ULL << 31) | Ordinal);
} }
} }
@ -102,9 +102,9 @@ public:
size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); } size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize()); memset(Buf, 0, getSize());
auto *E = (coff_import_directory_table_entry *)(Buf + OutputSectionOff); auto *E = (coff_import_directory_table_entry *)(Buf);
E->ImportLookupTableRVA = LookupTab->getRVA(); E->ImportLookupTableRVA = LookupTab->getRVA();
E->NameRVA = DLLName->getRVA(); E->NameRVA = DLLName->getRVA();
E->ImportAddressTableRVA = AddressTab->getRVA(); E->ImportAddressTableRVA = AddressTab->getRVA();
@ -124,7 +124,7 @@ public:
size_t getSize() const override { return Size; } size_t getSize() const override { return Size; }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, Size); memset(Buf, 0, Size);
} }
private: private:
@ -169,9 +169,9 @@ public:
} }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize()); memset(Buf, 0, getSize());
auto *E = (delay_import_directory_table_entry *)(Buf + OutputSectionOff); auto *E = (delay_import_directory_table_entry *)(Buf);
E->Attributes = 1; E->Attributes = 1;
E->Name = DLLName->getRVA(); E->Name = DLLName->getRVA();
E->ModuleHandle = ModuleHandle->getRVA(); E->ModuleHandle = ModuleHandle->getRVA();
@ -280,10 +280,10 @@ public:
size_t getSize() const override { return sizeof(ThunkX64); } size_t getSize() const override { return sizeof(ThunkX64); }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memcpy(Buf + OutputSectionOff, ThunkX64, sizeof(ThunkX64)); memcpy(Buf, ThunkX64, sizeof(ThunkX64));
write32le(Buf + OutputSectionOff + 36, Imp->getRVA() - RVA - 40); write32le(Buf + 36, Imp->getRVA() - RVA - 40);
write32le(Buf + OutputSectionOff + 43, Desc->getRVA() - RVA - 47); write32le(Buf + 43, Desc->getRVA() - RVA - 47);
write32le(Buf + OutputSectionOff + 48, Helper->getRVA() - RVA - 52); write32le(Buf + 48, Helper->getRVA() - RVA - 52);
} }
Defined *Imp = nullptr; Defined *Imp = nullptr;
@ -299,10 +299,10 @@ public:
size_t getSize() const override { return sizeof(ThunkX86); } size_t getSize() const override { return sizeof(ThunkX86); }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memcpy(Buf + OutputSectionOff, ThunkX86, sizeof(ThunkX86)); memcpy(Buf, ThunkX86, sizeof(ThunkX86));
write32le(Buf + OutputSectionOff + 3, Imp->getRVA() + Config->ImageBase); write32le(Buf + 3, Imp->getRVA() + Config->ImageBase);
write32le(Buf + OutputSectionOff + 8, Desc->getRVA() + Config->ImageBase); write32le(Buf + 8, Desc->getRVA() + Config->ImageBase);
write32le(Buf + OutputSectionOff + 13, Helper->getRVA() - RVA - 17); write32le(Buf + 13, Helper->getRVA() - RVA - 17);
} }
void getBaserels(std::vector<Baserel> *Res) override { void getBaserels(std::vector<Baserel> *Res) override {
@ -323,10 +323,10 @@ public:
size_t getSize() const override { return sizeof(ThunkARM); } size_t getSize() const override { return sizeof(ThunkARM); }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memcpy(Buf + OutputSectionOff, ThunkARM, sizeof(ThunkARM)); memcpy(Buf, ThunkARM, sizeof(ThunkARM));
applyMOV32T(Buf + OutputSectionOff + 0, Imp->getRVA() + Config->ImageBase); applyMOV32T(Buf + 0, Imp->getRVA() + Config->ImageBase);
applyMOV32T(Buf + OutputSectionOff + 22, Desc->getRVA() + Config->ImageBase); applyMOV32T(Buf + 22, Desc->getRVA() + Config->ImageBase);
applyBranch24T(Buf + OutputSectionOff + 30, Helper->getRVA() - RVA - 34); applyBranch24T(Buf + 30, Helper->getRVA() - RVA - 34);
} }
void getBaserels(std::vector<Baserel> *Res) override { void getBaserels(std::vector<Baserel> *Res) override {
@ -347,13 +347,12 @@ public:
size_t getSize() const override { return sizeof(ThunkARM64); } size_t getSize() const override { return sizeof(ThunkARM64); }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memcpy(Buf + OutputSectionOff, ThunkARM64, sizeof(ThunkARM64)); memcpy(Buf, ThunkARM64, sizeof(ThunkARM64));
applyArm64Addr(Buf + OutputSectionOff + 0, Imp->getRVA(), RVA + 0, 12); applyArm64Addr(Buf + 0, Imp->getRVA(), RVA + 0, 12);
applyArm64Imm(Buf + OutputSectionOff + 4, Imp->getRVA() & 0xfff, 0); applyArm64Imm(Buf + 4, Imp->getRVA() & 0xfff, 0);
applyArm64Addr(Buf + OutputSectionOff + 52, Desc->getRVA(), RVA + 52, 12); applyArm64Addr(Buf + 52, Desc->getRVA(), RVA + 52, 12);
applyArm64Imm(Buf + OutputSectionOff + 56, Desc->getRVA() & 0xfff, 0); applyArm64Imm(Buf + 56, Desc->getRVA() & 0xfff, 0);
applyArm64Branch26(Buf + OutputSectionOff + 60, applyArm64Branch26(Buf + 60, Helper->getRVA() - RVA - 60);
Helper->getRVA() - RVA - 60);
} }
Defined *Imp = nullptr; Defined *Imp = nullptr;
@ -371,13 +370,13 @@ public:
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
if (Config->is64()) { if (Config->is64()) {
write64le(Buf + OutputSectionOff, Thunk->getRVA() + Config->ImageBase); write64le(Buf, Thunk->getRVA() + Config->ImageBase);
} else { } else {
uint32_t Bit = 0; uint32_t Bit = 0;
// Pointer to thumb code must have the LSB set, so adjust it. // Pointer to thumb code must have the LSB set, so adjust it.
if (Config->Machine == ARMNT) if (Config->Machine == ARMNT)
Bit = 1; Bit = 1;
write32le(Buf + OutputSectionOff, (Thunk->getRVA() + Config->ImageBase) | Bit); write32le(Buf, (Thunk->getRVA() + Config->ImageBase) | Bit);
} }
} }
@ -403,9 +402,9 @@ public:
} }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize()); memset(Buf, 0, getSize());
auto *E = (export_directory_table_entry *)(Buf + OutputSectionOff); auto *E = (export_directory_table_entry *)(Buf);
E->NameRVA = DLLName->getRVA(); E->NameRVA = DLLName->getRVA();
E->OrdinalBase = 0; E->OrdinalBase = 0;
E->AddressTableEntries = MaxOrdinal + 1; E->AddressTableEntries = MaxOrdinal + 1;
@ -429,10 +428,10 @@ public:
size_t getSize() const override { return Size * 4; } size_t getSize() const override { return Size * 4; }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize()); memset(Buf, 0, getSize());
for (const Export &E : Config->Exports) { for (const Export &E : Config->Exports) {
uint8_t *P = Buf + OutputSectionOff + E.Ordinal * 4; uint8_t *P = Buf + E.Ordinal * 4;
uint32_t Bit = 0; uint32_t Bit = 0;
// Pointer to thumb code must have the LSB set, so adjust it. // Pointer to thumb code must have the LSB set, so adjust it.
if (Config->Machine == ARMNT && !E.Data) if (Config->Machine == ARMNT && !E.Data)
@ -455,10 +454,9 @@ public:
size_t getSize() const override { return Chunks.size() * 4; } size_t getSize() const override { return Chunks.size() * 4; }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
uint8_t *P = Buf + OutputSectionOff;
for (Chunk *C : Chunks) { for (Chunk *C : Chunks) {
write32le(P, C->getRVA()); write32le(Buf, C->getRVA());
P += 4; Buf += 4;
} }
} }
@ -472,12 +470,11 @@ public:
size_t getSize() const override { return Size * 2; } size_t getSize() const override { return Size * 2; }
void writeTo(uint8_t *Buf) const override { void writeTo(uint8_t *Buf) const override {
uint8_t *P = Buf + OutputSectionOff;
for (Export &E : Config->Exports) { for (Export &E : Config->Exports) {
if (E.Noname) if (E.Noname)
continue; continue;
write16le(P, E.Ordinal); write16le(Buf, E.Ordinal);
P += 2; Buf += 2;
} }
} }

View File

@ -1030,7 +1030,7 @@ void PDBLinker::mergeSymbolRecords(ObjFile *File, const CVIndexMap &IndexMap,
static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
SectionChunk &DebugChunk) { SectionChunk &DebugChunk) {
uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk.getSize()); uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk.getSize());
assert(DebugChunk.OutputSectionOff == 0 && assert(DebugChunk.getOutputSection() == nullptr &&
"debug sections should not be in output sections"); "debug sections should not be in output sections");
DebugChunk.writeTo(Buffer); DebugChunk.writeTo(Buffer);
return makeArrayRef(Buffer, DebugChunk.getSize()); return makeArrayRef(Buffer, DebugChunk.getSize());
@ -1557,6 +1557,8 @@ void PDBLinker::addImportFilesToPDB(ArrayRef<OutputSection *> OutputSections) {
} }
DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym); DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym);
Chunk *ThunkChunk = Thunk->getChunk();
OutputSection *ThunkOS = ThunkChunk->getOutputSection();
ObjNameSym ONS(SymbolRecordKind::ObjNameSym); ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
Compile3Sym CS(SymbolRecordKind::Compile3Sym); Compile3Sym CS(SymbolRecordKind::Compile3Sym);
@ -1573,9 +1575,9 @@ void PDBLinker::addImportFilesToPDB(ArrayRef<OutputSection *> OutputSections) {
TS.End = 0; TS.End = 0;
TS.Next = 0; TS.Next = 0;
TS.Thunk = ThunkOrdinal::Standard; TS.Thunk = ThunkOrdinal::Standard;
TS.Length = Thunk->getChunk()->getSize(); TS.Length = ThunkChunk->getSize();
TS.Segment = Thunk->getChunk()->getOutputSection()->SectionIndex; TS.Segment = ThunkOS->SectionIndex;
TS.Offset = Thunk->getChunk()->OutputSectionOff; TS.Offset = ThunkChunk->getRVA() - ThunkOS->getRVA();
Mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol( Mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol(
ONS, Alloc, CodeViewContainer::Pdb)); ONS, Alloc, CodeViewContainer::Pdb));

View File

@ -91,7 +91,7 @@ public:
} }
void writeTo(uint8_t *B) const override { void writeTo(uint8_t *B) const override {
auto *D = reinterpret_cast<debug_directory *>(B + OutputSectionOff); auto *D = reinterpret_cast<debug_directory *>(B);
for (const Chunk *Record : Records) { for (const Chunk *Record : Records) {
OutputSection *OS = Record->getOutputSection(); OutputSection *OS = Record->getOutputSection();
@ -145,10 +145,10 @@ public:
void writeTo(uint8_t *B) const override { void writeTo(uint8_t *B) const override {
// Save off the DebugInfo entry to backfill the file signature (build id) // Save off the DebugInfo entry to backfill the file signature (build id)
// in Writer::writeBuildId // in Writer::writeBuildId
BuildId = reinterpret_cast<codeview::DebugInfo *>(B + OutputSectionOff); BuildId = reinterpret_cast<codeview::DebugInfo *>(B);
// variable sized field (PDB Path) // variable sized field (PDB Path)
char *P = reinterpret_cast<char *>(B + OutputSectionOff + sizeof(*BuildId)); char *P = reinterpret_cast<char *>(B + sizeof(*BuildId));
if (!Config->PDBAltPath.empty()) if (!Config->PDBAltPath.empty())
memcpy(P, Config->PDBAltPath.data(), Config->PDBAltPath.size()); memcpy(P, Config->PDBAltPath.data(), Config->PDBAltPath.size());
P[Config->PDBAltPath.size()] = '\0'; P[Config->PDBAltPath.size()] = '\0';
@ -1161,7 +1161,6 @@ void Writer::assignAddresses() {
VirtualSize += Padding; VirtualSize += Padding;
VirtualSize = alignTo(VirtualSize, C->Alignment); VirtualSize = alignTo(VirtualSize, C->Alignment);
C->setRVA(RVA + VirtualSize); C->setRVA(RVA + VirtualSize);
C->OutputSectionOff = VirtualSize;
C->finalizeContents(); C->finalizeContents();
VirtualSize += C->getSize(); VirtualSize += C->getSize();
if (C->hasData()) if (C->hasData())
@ -1675,7 +1674,9 @@ void Writer::writeSections() {
// ADD instructions). // ADD instructions).
if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE) if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE)
memset(SecBuf, 0xCC, Sec->getRawSize()); memset(SecBuf, 0xCC, Sec->getRawSize());
parallelForEach(Sec->Chunks, [&](Chunk *C) { C->writeTo(SecBuf); }); parallelForEach(Sec->Chunks, [&](Chunk *C) {
C->writeTo(SecBuf + C->getRVA() - Sec->getRVA());
});
} }
} }