Skip to content

Commit c22d84f

Browse files
committed
[ELF] Refine ctx.arg.exportDynamic condition
--export-dynamic should be a no-op when ctx.hasDynsym is false. * Drop unneeded ctx.hasDynsym checks. * Static linking with --export-dynamic does not prevent devirtualization.
1 parent f5d63cc commit c22d84f

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

lld/ELF/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,8 +2617,7 @@ void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
26172617
for (Symbol *sym : obj->getGlobalSymbols()) {
26182618
if (!sym->isDefined())
26192619
continue;
2620-
if (ctx.hasDynsym && ctx.arg.exportDynamic &&
2621-
sym->computeBinding(ctx) != STB_LOCAL)
2620+
if (ctx.arg.exportDynamic && sym->computeBinding(ctx) != STB_LOCAL)
26222621
sym->isExported = true;
26232622
if (sym->hasVersionSuffix)
26242623
sym->parseSymbolVersion(ctx);
@@ -2965,6 +2964,7 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
29652964

29662965
// Create dynamic sections for dynamic linking and static PIE.
29672966
ctx.hasDynsym = !ctx.sharedFiles.empty() || ctx.arg.isPic;
2967+
ctx.arg.exportDynamic &= ctx.hasDynsym;
29682968

29692969
// If an entry symbol is in a static archive, pull out that file now.
29702970
if (Symbol *sym = ctx.symtab->find(ctx.arg.entry))

lld/ELF/Symbols.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,10 @@ void elf::parseVersionAndComputeIsPreemptible(Ctx &ctx) {
361361
// can contain versions in the form of <name>@<version>.
362362
// Let them parse and update their names to exclude version suffix.
363363
// In addition, compute isExported and isPreemptible.
364-
bool hasDynsym = ctx.hasDynsym;
365364
bool maybePreemptible = ctx.sharedFiles.size() || ctx.arg.shared;
366365
for (Symbol *sym : ctx.symtab->getSymbols()) {
367366
if (sym->hasVersionSuffix)
368367
sym->parseSymbolVersion(ctx);
369-
if (!hasDynsym)
370-
continue;
371368
if (sym->computeBinding(ctx) == STB_LOCAL) {
372369
sym->isExported = false;
373370
continue;
@@ -377,7 +374,7 @@ void elf::parseVersionAndComputeIsPreemptible(Ctx &ctx) {
377374
} else if (ctx.arg.exportDynamic &&
378375
(sym->isUsedInRegularObj || !sym->ltoCanOmit)) {
379376
sym->isExported = true;
380-
sym->isPreemptible = computeIsPreemptible(ctx, *sym);
377+
sym->isPreemptible = maybePreemptible && computeIsPreemptible(ctx, *sym);
381378
}
382379
}
383380
}

lld/ELF/Writer.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
284284
static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
285285
llvm::TimeTraceScope timeScope("Demote symbols");
286286
DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
287-
bool hasDynsym = ctx.hasDynsym;
288287
bool maybePreemptible = ctx.sharedFiles.size() || ctx.arg.shared;
289288
for (Symbol *sym : ctx.symtab->getSymbols()) {
290289
if (auto *d = dyn_cast<Defined>(sym)) {
@@ -301,9 +300,8 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
301300
}
302301
}
303302

304-
if (hasDynsym)
305-
sym->isPreemptible = maybePreemptible &&
306-
(sym->isUndefined() || sym->isExported) &&
303+
if (maybePreemptible)
304+
sym->isPreemptible = (sym->isUndefined() || sym->isExported) &&
307305
computeIsPreemptible(ctx, *sym);
308306
}
309307
}
@@ -1853,7 +1851,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18531851

18541852
// If the previous code block defines any non-hidden symbols (e.g.
18551853
// __global_pointer$), they may be exported.
1856-
if (ctx.hasDynsym && ctx.arg.exportDynamic)
1854+
if (ctx.arg.exportDynamic)
18571855
for (Symbol *sym : ctx.synthesizedSymbols)
18581856
if (sym->computeBinding(ctx) != STB_LOCAL)
18591857
sym->isExported = true;

lld/test/ELF/lto/devirt_vcall_vis_export_dynamic.ll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
1010
; RUN: -mllvm -pass-remarks=. 2>&1 | FileCheck %s --check-prefix=REMARK
1111
; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
12+
;; --export-dynamic without .dynsym does not prevent devirtualization.
13+
; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
14+
; RUN: -mllvm -pass-remarks=. \
15+
; RUN: --export-dynamic 2>&1 | FileCheck %s --check-prefix=REMARK
1216

1317
;; Hybrid WPD
1418
;; Generate split module with summary for hybrid Thin/Regular LTO WPD.
@@ -33,19 +37,20 @@
3337
; RUN: ld.lld -shared -soname=ta %ta.o -o %ta.so
3438

3539
;; Index based WPD
36-
; RUN: ld.lld %t2.o -o %t3 -save-temps --lto-whole-program-visibility \
40+
41+
; RUN: ld.lld %t2.o %ta.so -o %t3 -save-temps --lto-whole-program-visibility \
3742
; RUN: -mllvm -pass-remarks=. \
3843
; RUN: --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
3944
; RUN: llvm-dis %t2.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
4045

4146
;; Hybrid WPD
42-
; RUN: ld.lld %t.o -o %t3 -save-temps --lto-whole-program-visibility \
47+
; RUN: ld.lld %t.o %ta.so -o %t3 -save-temps --lto-whole-program-visibility \
4348
; RUN: -mllvm -pass-remarks=. \
4449
; RUN: --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
4550
; RUN: llvm-dis %t.o.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR
4651

4752
;; Regular LTO WPD
48-
; RUN: ld.lld %t4.o -o %t3 -save-temps --lto-whole-program-visibility \
53+
; RUN: ld.lld %t4.o %ta.so -o %t3 -save-temps --lto-whole-program-visibility \
4954
; RUN: -mllvm -pass-remarks=. \
5055
; RUN: --export-dynamic 2>&1 | FileCheck /dev/null --implicit-check-not single-impl --allow-empty
5156
; RUN: llvm-dis %t3.0.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-NODEVIRT-IR

0 commit comments

Comments
 (0)