Skip to content

Commit 2f43992

Browse files
committed
Address review comments
1 parent 8c6d4b2 commit 2f43992

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

lld/COFF/Chunks.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,19 +1214,15 @@ void DynamicRelocsChunk::writeTo(uint8_t *buf) const {
12141214

12151215
auto pageHeader = reinterpret_cast<coff_base_reloc_block_header *>(buf);
12161216
pageHeader->BlockSize = sizeof(*pageHeader);
1217-
size_t relocSize = sizeof(*pageHeader);
12181217
for (const Arm64XDynamicRelocEntry &entry : arm64xRelocs) {
1219-
entry.writeTo(buf + relocSize);
1220-
size_t entrySize = entry.getSize();
1221-
pageHeader->BlockSize += entrySize;
1222-
relocSize += entrySize;
1218+
entry.writeTo(buf + pageHeader->BlockSize);
1219+
pageHeader->BlockSize += entry.getSize();
12231220
}
12241221
pageHeader->BlockSize = alignTo(pageHeader->BlockSize, sizeof(uint32_t));
1225-
relocSize = alignTo(relocSize, sizeof(uint32_t));
12261222

1227-
header->BaseRelocSize = relocSize;
1228-
table->Size += relocSize;
1229-
assert(size == sizeof(*table) + sizeof(*header) + relocSize);
1223+
header->BaseRelocSize = pageHeader->BlockSize;
1224+
table->Size += header->BaseRelocSize;
1225+
assert(size == sizeof(*table) + sizeof(*header) + header->BaseRelocSize);
12301226
}
12311227

12321228
} // namespace lld::coff

lld/COFF/Chunks.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,9 @@ class DynamicRelocsChunk : public NonSectionChunk {
861861
void writeTo(uint8_t *buf) const override;
862862
void finalize();
863863

864-
uint32_t add(llvm::COFF::Arm64XFixupType type, uint8_t size, uint32_t offset,
865-
uint64_t value) {
864+
void add(llvm::COFF::Arm64XFixupType type, uint8_t size, uint32_t offset,
865+
uint64_t value) {
866866
arm64xRelocs.emplace_back(type, size, offset, value);
867-
return arm64xRelocs.size() - 1;
868867
}
869868

870869
private:

lld/COFF/Writer.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ static_assert(sizeof(dosProgram) % 8 == 0,
7979

8080
static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
8181
static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
82+
static const uint32_t coffHeaderOffset = dosStubSize + sizeof(PEMagic);
83+
static const uint32_t peHeaderOffset =
84+
coffHeaderOffset + sizeof(coff_file_header);
85+
static const uint32_t dataDirOffset64 =
86+
peHeaderOffset + sizeof(pe32plus_header);
8287

8388
static const int numberOfDataDirectory = 16;
8489

@@ -1600,6 +1605,7 @@ void Writer::assignAddresses() {
16001605
for (OutputSection *sec : ctx.outputSections) {
16011606
llvm::TimeTraceScope timeScope("Section: ", sec->name);
16021607
if (sec == relocSec) {
1608+
sec->chunks.clear();
16031609
addBaserels();
16041610
if (ctx.dynamicRelocs) {
16051611
ctx.dynamicRelocs->finalize();
@@ -1680,6 +1686,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
16801686
buf += sizeof(PEMagic);
16811687

16821688
// Write COFF header
1689+
assert(coffHeaderOffset == buf - buffer->getBufferStart());
16831690
auto *coff = reinterpret_cast<coff_file_header *>(buf);
16841691
buf += sizeof(*coff);
16851692
switch (config->machine) {
@@ -1712,6 +1719,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
17121719
sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
17131720

17141721
// Write PE header
1722+
assert(peHeaderOffset == buf - buffer->getBufferStart());
17151723
auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
17161724
buf += sizeof(*pe);
17171725
pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
@@ -1777,6 +1785,8 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
17771785
pe->SizeOfInitializedData = getSizeOfInitializedData();
17781786

17791787
// Write data directory
1788+
assert(!ctx.config.is64() ||
1789+
dataDirOffset64 == buf - buffer->getBufferStart());
17801790
auto *dir = reinterpret_cast<data_directory *>(buf);
17811791
buf += sizeof(*dir) * numberOfDataDirectory;
17821792
if (edataStart) {
@@ -2532,7 +2542,6 @@ uint32_t Writer::getSizeOfInitializedData() {
25322542
void Writer::addBaserels() {
25332543
if (!ctx.config.relocatable)
25342544
return;
2535-
relocSec->chunks.clear();
25362545
std::vector<Baserel> v;
25372546
for (OutputSection *sec : ctx.outputSections) {
25382547
if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
@@ -2570,24 +2579,20 @@ void Writer::createDynamicRelocs() {
25702579
if (!ctx.dynamicRelocs)
25712580
return;
25722581

2573-
const uint32_t coffHeaderOffset = dosStubSize + sizeof(PEMagic);
2574-
const uint32_t peHeaderOffset = coffHeaderOffset + sizeof(coff_file_header);
2575-
const uint32_t dataDirOffset = peHeaderOffset + sizeof(pe32plus_header);
2576-
25772582
// Adjust the Machine field in the COFF header to AMD64.
25782583
ctx.dynamicRelocs->add(IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof(uint16_t),
25792584
coffHeaderOffset + offsetof(coff_file_header, Machine),
25802585
AMD64);
25812586

2582-
// Adjust the load config directory.
2587+
// Clear the load config directory.
25832588
// FIXME: Use the hybrid load config value instead.
25842589
ctx.dynamicRelocs->add(IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof(uint32_t),
2585-
dataDirOffset +
2590+
dataDirOffset64 +
25862591
LOAD_CONFIG_TABLE * sizeof(data_directory) +
25872592
offsetof(data_directory, RelativeVirtualAddress),
25882593
0);
25892594
ctx.dynamicRelocs->add(IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof(uint32_t),
2590-
dataDirOffset +
2595+
dataDirOffset64 +
25912596
LOAD_CONFIG_TABLE * sizeof(data_directory) +
25922597
offsetof(data_directory, Size),
25932598
0);

0 commit comments

Comments
 (0)