Skip to content

Commit 14043d4

Browse files
authored
[lld] Implement getOutputCharacteristics for non-section code thunks. (#70721)
This will be useful for ARM64EC, but it also fixes MinGW export handling when synthetic function symbols are exported.
1 parent aca9c89 commit 14043d4

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

lld/COFF/Chunks.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ class NonSectionChunk : public Chunk {
180180
NonSectionChunk(Kind k = OtherKind) : Chunk(k) {}
181181
};
182182

183+
class NonSectionCodeChunk : public NonSectionChunk {
184+
public:
185+
virtual uint32_t getOutputCharacteristics() const override {
186+
return llvm::COFF::IMAGE_SCN_MEM_READ | llvm::COFF::IMAGE_SCN_MEM_EXECUTE;
187+
}
188+
189+
protected:
190+
NonSectionCodeChunk(Kind k = OtherKind) : NonSectionChunk(k) {}
191+
};
192+
183193
// MinGW specific; information about one individual location in the image
184194
// that needs to be fixed up at runtime after loading. This represents
185195
// one individual element in the PseudoRelocTableChunk table.
@@ -508,10 +518,10 @@ static const uint8_t importThunkARM64[] = {
508518
// Windows-specific.
509519
// A chunk for DLL import jump table entry. In a final output, its
510520
// contents will be a JMP instruction to some __imp_ symbol.
511-
class ImportThunkChunk : public NonSectionChunk {
521+
class ImportThunkChunk : public NonSectionCodeChunk {
512522
public:
513523
ImportThunkChunk(COFFLinkerContext &ctx, Defined *s)
514-
: NonSectionChunk(ImportThunkKind), impSymbol(s), ctx(ctx) {}
524+
: NonSectionCodeChunk(ImportThunkKind), impSymbol(s), ctx(ctx) {}
515525
static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; }
516526

517527
protected:
@@ -560,7 +570,7 @@ class ImportThunkChunkARM64 : public ImportThunkChunk {
560570
MachineTypes getMachine() const override { return ARM64; }
561571
};
562572

563-
class RangeExtensionThunkARM : public NonSectionChunk {
573+
class RangeExtensionThunkARM : public NonSectionCodeChunk {
564574
public:
565575
explicit RangeExtensionThunkARM(COFFLinkerContext &ctx, Defined *t)
566576
: target(t), ctx(ctx) {
@@ -576,7 +586,7 @@ class RangeExtensionThunkARM : public NonSectionChunk {
576586
COFFLinkerContext &ctx;
577587
};
578588

579-
class RangeExtensionThunkARM64 : public NonSectionChunk {
589+
class RangeExtensionThunkARM64 : public NonSectionCodeChunk {
580590
public:
581591
explicit RangeExtensionThunkARM64(COFFLinkerContext &ctx, Defined *t)
582592
: target(t), ctx(ctx) {

lld/COFF/DLL.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static const uint8_t tailMergeARM64[] = {
313313
};
314314

315315
// A chunk for the delay import thunk.
316-
class ThunkChunkX64 : public NonSectionChunk {
316+
class ThunkChunkX64 : public NonSectionCodeChunk {
317317
public:
318318
ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
319319

@@ -330,7 +330,7 @@ class ThunkChunkX64 : public NonSectionChunk {
330330
Chunk *tailMerge = nullptr;
331331
};
332332

333-
class TailMergeChunkX64 : public NonSectionChunk {
333+
class TailMergeChunkX64 : public NonSectionCodeChunk {
334334
public:
335335
TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {}
336336

@@ -382,7 +382,7 @@ class TailMergeUnwindInfoX64 : public NonSectionChunk {
382382
}
383383
};
384384

385-
class ThunkChunkX86 : public NonSectionChunk {
385+
class ThunkChunkX86 : public NonSectionCodeChunk {
386386
public:
387387
ThunkChunkX86(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
388388
: imp(i), tailMerge(tm), ctx(ctx) {}
@@ -407,7 +407,7 @@ class ThunkChunkX86 : public NonSectionChunk {
407407
const COFFLinkerContext &ctx;
408408
};
409409

410-
class TailMergeChunkX86 : public NonSectionChunk {
410+
class TailMergeChunkX86 : public NonSectionCodeChunk {
411411
public:
412412
TailMergeChunkX86(COFFLinkerContext &ctx, Chunk *d, Defined *h)
413413
: desc(d), helper(h), ctx(ctx) {}
@@ -432,7 +432,7 @@ class TailMergeChunkX86 : public NonSectionChunk {
432432
const COFFLinkerContext &ctx;
433433
};
434434

435-
class ThunkChunkARM : public NonSectionChunk {
435+
class ThunkChunkARM : public NonSectionCodeChunk {
436436
public:
437437
ThunkChunkARM(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
438438
: imp(i), tailMerge(tm), ctx(ctx) {
@@ -459,7 +459,7 @@ class ThunkChunkARM : public NonSectionChunk {
459459
const COFFLinkerContext &ctx;
460460
};
461461

462-
class TailMergeChunkARM : public NonSectionChunk {
462+
class TailMergeChunkARM : public NonSectionCodeChunk {
463463
public:
464464
TailMergeChunkARM(COFFLinkerContext &ctx, Chunk *d, Defined *h)
465465
: desc(d), helper(h), ctx(ctx) {
@@ -486,7 +486,7 @@ class TailMergeChunkARM : public NonSectionChunk {
486486
const COFFLinkerContext &ctx;
487487
};
488488

489-
class ThunkChunkARM64 : public NonSectionChunk {
489+
class ThunkChunkARM64 : public NonSectionCodeChunk {
490490
public:
491491
ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {
492492
setAlignment(4);
@@ -506,7 +506,7 @@ class ThunkChunkARM64 : public NonSectionChunk {
506506
Chunk *tailMerge = nullptr;
507507
};
508508

509-
class TailMergeChunkARM64 : public NonSectionChunk {
509+
class TailMergeChunkARM64 : public NonSectionCodeChunk {
510510
public:
511511
TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) {
512512
setAlignment(4);

lld/test/COFF/export-thunk.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
REQUIRES: x86
2+
3+
RUN: echo -e 'LIBRARY test.dll\nEXPORTS\nimpfunc\n' > %t.imp.def
4+
RUN: llvm-dlltool -m i386:x86-64 -d %t.imp.def -l %t.imp.lib
5+
RUN: lld-link -machine:amd64 -out:%t.dll -dll -noentry -lldmingw %t.imp.lib -export:impfunc -output-def:%t.def
6+
7+
Check that the synthetic import thunk is exported as a function, not data.
8+
9+
RUN: cat %t.def | FileCheck %s
10+
CHECK: EXPORTS
11+
CHECK-NEXT: impfunc @1
12+
13+
RUN: cat %t.def | FileCheck -check-prefix=CHECK-NO-DATA %s
14+
CHECK-NO-DATA-NOT: DATA

0 commit comments

Comments
 (0)