Skip to content

Commit 4728ac7

Browse files
authored
[LLD][COFF][NFC] Always align null chunks (#116677)
Currently, null chunks always follow other aligned chunks, so this patch is NFC. However, it will become observable once support for ARM64X imports is added. The import tables are shared between the native and EC views. They are usually very similar, but in cases where they differ, ARM64X relocations handle the discrepancies. If a DLL is only imported by EC code, the native view will see it as importing zero functions from this DLL (with ARM64X relocations replacing those null chunks with actual imports). In this scenario, the null chunks may appear as the very first chunks, meaning there is nothing else forcing their alignment.
1 parent 681939e commit 4728ac7

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lld/COFF/DLL.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,14 @@ class ImportDirectoryChunk : public NonSectionChunk {
131131
// Contents of this chunk is always null bytes.
132132
class NullChunk : public NonSectionChunk {
133133
public:
134-
explicit NullChunk(size_t n) : size(n) { hasData = false; }
134+
explicit NullChunk(size_t n, uint32_t align) : size(n) {
135+
hasData = false;
136+
setAlignment(align);
137+
}
138+
explicit NullChunk(COFFLinkerContext &ctx)
139+
: NullChunk(ctx.config.wordsize, ctx.config.wordsize) {}
140+
explicit NullChunk(COFFLinkerContext &ctx, size_t n)
141+
: NullChunk(n, ctx.config.wordsize) {}
135142
size_t getSize() const override { return size; }
136143

137144
void writeTo(uint8_t *buf) const override {
@@ -737,11 +744,11 @@ void IdataContents::create(COFFLinkerContext &ctx) {
737744
}
738745
}
739746
// Terminate with null values.
740-
lookups.push_back(make<NullChunk>(ctx.config.wordsize));
741-
addresses.push_back(make<NullChunk>(ctx.config.wordsize));
747+
lookups.push_back(make<NullChunk>(ctx));
748+
addresses.push_back(make<NullChunk>(ctx));
742749
if (ctx.config.machine == ARM64EC) {
743-
auxIat.push_back(make<NullChunk>(ctx.config.wordsize));
744-
auxIatCopy.push_back(make<NullChunk>(ctx.config.wordsize));
750+
auxIat.push_back(make<NullChunk>(ctx));
751+
auxIatCopy.push_back(make<NullChunk>(ctx));
745752
}
746753

747754
for (int i = 0, e = syms.size(); i < e; ++i)
@@ -755,7 +762,7 @@ void IdataContents::create(COFFLinkerContext &ctx) {
755762
dirs.push_back(dir);
756763
}
757764
// Add null terminator.
758-
dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
765+
dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry), 4));
759766
}
760767

761768
std::vector<Chunk *> DelayLoadContents::getChunks() {
@@ -830,17 +837,16 @@ void DelayLoadContents::create(Defined *h) {
830837
saver().save("__tailMerge_" + syms[0]->getDLLName().lower());
831838
ctx.symtab.addSynthetic(tmName, tm);
832839
// Terminate with null values.
833-
addresses.push_back(make<NullChunk>(8));
834-
names.push_back(make<NullChunk>(8));
840+
addresses.push_back(make<NullChunk>(ctx, 8));
841+
names.push_back(make<NullChunk>(ctx, 8));
835842
if (ctx.config.machine == ARM64EC) {
836-
auxIat.push_back(make<NullChunk>(8));
837-
auxIatCopy.push_back(make<NullChunk>(8));
843+
auxIat.push_back(make<NullChunk>(ctx, 8));
844+
auxIatCopy.push_back(make<NullChunk>(ctx, 8));
838845
}
839846

840847
for (int i = 0, e = syms.size(); i < e; ++i)
841848
syms[i]->setLocation(addresses[base + i]);
842-
auto *mh = make<NullChunk>(8);
843-
mh->setAlignment(8);
849+
auto *mh = make<NullChunk>(8, 8);
844850
moduleHandles.push_back(mh);
845851

846852
// Fill the delay import table header fields.
@@ -853,7 +859,8 @@ void DelayLoadContents::create(Defined *h) {
853859
if (unwind)
854860
unwindinfo.push_back(unwind);
855861
// Add null terminator.
856-
dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
862+
dirs.push_back(
863+
make<NullChunk>(sizeof(delay_import_directory_table_entry), 4));
857864
}
858865

859866
Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {

0 commit comments

Comments
 (0)