Skip to content

Commit b84f7d1

Browse files
committed
Revert "[ELF] Refine isExported/isPreemptible condition"
This reverts commit 994cea3. Try to fix the bolt test failures in pre-merge checks.
1 parent df2de13 commit b84f7d1

12 files changed

+48
-46
lines changed

lld/ELF/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ struct Config {
298298
bool gdbIndex;
299299
bool gnuHash = false;
300300
bool gnuUnique;
301+
bool hasDynSymTab;
301302
bool ignoreDataAddressEquality;
302303
bool ignoreFunctionAddressEquality;
303304
bool ltoCSProfileGenerate;
@@ -311,6 +312,7 @@ struct Config {
311312
bool mipsN32Abi = false;
312313
bool mmapOutputFile;
313314
bool nmagic;
315+
bool noDynamicLinker = false;
314316
bool noinhibitExec;
315317
bool nostdlib;
316318
bool oFormatBinary;

lld/ELF/Driver.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,11 @@ static StringRef getDynamicLinker(Ctx &ctx, opt::InputArgList &args) {
781781
auto *arg = args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
782782
if (!arg)
783783
return "";
784-
if (arg->getOption().getID() == OPT_no_dynamic_linker)
784+
if (arg->getOption().getID() == OPT_no_dynamic_linker) {
785+
// --no-dynamic-linker suppresses undefined weak symbols in .dynsym
786+
ctx.arg.noDynamicLinker = true;
785787
return "";
788+
}
786789
return arg->getValue();
787790
}
788791

@@ -2945,8 +2948,12 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
29452948

29462949
parseFiles(ctx, files);
29472950

2951+
// Dynamic linking is used if there is an input DSO,
2952+
// or -shared or non-static pie is specified.
2953+
ctx.hasDynsym = !ctx.sharedFiles.empty() || ctx.arg.shared ||
2954+
(ctx.arg.pie && !ctx.arg.noDynamicLinker);
29482955
// Create dynamic sections for dynamic linking and static PIE.
2949-
ctx.hasDynsym = !ctx.sharedFiles.empty() || ctx.arg.isPic;
2956+
ctx.arg.hasDynSymTab = ctx.hasDynsym || ctx.arg.isPic;
29502957

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

lld/ELF/Symbols.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,7 @@ void elf::parseVersionAndComputeIsPreemptible(Ctx &ctx) {
360360
// Symbol themselves might know their versions because symbols
361361
// can contain versions in the form of <name>@<version>.
362362
// Let them parse and update their names to exclude version suffix.
363-
// In addition, compute isExported and isPreemptible.
364363
bool hasDynsym = ctx.hasDynsym;
365-
bool maybePreemptible = ctx.sharedFiles.size() || ctx.arg.shared;
366364
for (Symbol *sym : ctx.symtab->getSymbols()) {
367365
if (sym->hasVersionSuffix)
368366
sym->parseSymbolVersion(ctx);
@@ -373,7 +371,7 @@ void elf::parseVersionAndComputeIsPreemptible(Ctx &ctx) {
373371
continue;
374372
}
375373
if (!sym->isDefined() && !sym->isCommon()) {
376-
sym->isPreemptible = maybePreemptible && computeIsPreemptible(ctx, *sym);
374+
sym->isPreemptible = computeIsPreemptible(ctx, *sym);
377375
} else if (ctx.arg.exportDynamic &&
378376
(sym->isUsedInRegularObj || !sym->ltoCanOmit)) {
379377
sym->isExported = true;

lld/ELF/SyntheticSections.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,7 +4740,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
47404740

47414741
// Add MIPS-specific sections.
47424742
if (ctx.arg.emachine == EM_MIPS) {
4743-
if (!ctx.arg.shared && ctx.hasDynsym) {
4743+
if (!ctx.arg.shared && ctx.arg.hasDynSymTab) {
47444744
ctx.in.mipsRldMap = std::make_unique<MipsRldMapSection>(ctx);
47454745
add(*ctx.in.mipsRldMap);
47464746
}
@@ -4803,7 +4803,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {
48034803
part.relaDyn = std::make_unique<RelocationSection<ELFT>>(
48044804
ctx, relaDynName, ctx.arg.zCombreloc, threadCount);
48054805

4806-
if (ctx.hasDynsym) {
4806+
if (ctx.arg.hasDynSymTab) {
48074807
add(*part.dynSymTab);
48084808

48094809
part.verSym = std::make_unique<VersionTableSection>(ctx);

lld/ELF/Writer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
285285
llvm::TimeTraceScope timeScope("Demote symbols");
286286
DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
287287
bool hasDynsym = ctx.hasDynsym;
288-
bool maybePreemptible = ctx.sharedFiles.size() || ctx.arg.shared;
289288
for (Symbol *sym : ctx.symtab->getSymbols()) {
290289
if (auto *d = dyn_cast<Defined>(sym)) {
291290
if (d->section && !d->section->isLive())
@@ -302,8 +301,7 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
302301
}
303302

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

19451943
// computeBinding might localize a linker-synthesized hidden symbol
19461944
// (e.g. __global_pointer$) that was considered exported.
1947-
if ((sym->isExported || sym->isPreemptible) && !sym->isLocal()) {
1945+
if (ctx.hasDynsym && (sym->isUndefined() || sym->isExported) &&
1946+
!sym->isLocal()) {
19481947
ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
19491948
if (auto *file = dyn_cast<SharedFile>(sym->file))
19501949
if (file->isNeeded && !sym->isUndefined())

lld/test/ELF/executable-undefined-ignoreall.s

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# REQUIRES: x86
22

3-
## In dynamic linking, --unresolved-symbols=ignore-all behaves similar to -shared:
3+
## --unresolved-symbols=ignore-all behaves similar to -shared:
44
## for PLT relocations to undefined symbols, produce dynamic relocations if we
55
## emit .dynsym.
66

7-
# RUN: llvm-mc -filetype=obj -triple=x86_64 %S/Inputs/shared.s -o %ta.o
8-
# RUN: ld.lld -shared -soname=ta %ta.o -o %ta.so
97
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
10-
# RUN: ld.lld %t.o %ta.so -o %t --unresolved-symbols=ignore-all -pie
8+
# RUN: ld.lld %t.o -o %t --unresolved-symbols=ignore-all -pie
119
# RUN: llvm-readobj -r %t | FileCheck %s
1210

1311
# CHECK: Relocations [

lld/test/ELF/ppc32-weak-undef-call.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# REQUIRES: ppc
22
# RUN: llvm-mc -filetype=obj -triple=powerpc %s -o %t.o
33
# RUN: ld.lld %t.o -o %t
4-
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=STATIC %s
4+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=PDE %s
55
# RUN: ld.lld -pie %t.o -o %t
6-
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=STATIC %s
6+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=PIC %s
77
# RUN: ld.lld -shared %t.o -o %t
88
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=PIC %s
99

1010
## It does not really matter how we fixup it, but we cannot overflow and
1111
## should not generate a call stub (this would waste space).
12-
# STATIC: bl {{.*}} <.text>
12+
# PDE: bl 0x100100b4
1313

1414
## With -pie or -shared, create a call stub. ld.bfd produces bl .+0
1515
# PIC: bl 0x[[PLT:[0-9a-f]+]]

lld/test/ELF/ppc64-undefined-weak.s

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t.o
44
# RUN: ld.lld %t.o -o %t
5-
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=STATIC
5+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=PDE
66
# RUN: ld.lld -pie %t.o -o %t
7-
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=STATIC
7+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=PIC
88
# RUN: ld.lld -shared %t.o -o %t.so
99
# RUN: llvm-objdump -d --no-show-raw-insn %t.so | FileCheck %s --check-prefix=PIC
1010

1111
# RUN: llvm-mc -filetype=obj -triple=powerpc64 %s -o %t.o
1212
# RUN: ld.lld %t.o -o %t
13-
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=STATIC
13+
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=PDE
1414

1515
## Branches to an undefined weak symbol need a thunk iff a dynamic relocation is
1616
## produced. undefweak2 is hidden and does not need a dynamic relocation, so we
1717
## suppress the thunk. undefweak1 needs a thunk iff -pie or -shared.
1818

19-
# STATIC-LABEL: <_start>:
20-
# STATIC-NEXT: bl {{.*}} <_start>
21-
# STATIC-NEXT: nop
22-
# STATIC-NEXT: bl {{.*}} <_start+0x8>
23-
# STATIC-NEXT: nop
19+
# PDE-LABEL: <_start>:
20+
# PDE-NEXT: bl {{.*}} <_start>
21+
# PDE-NEXT: nop
22+
# PDE-NEXT: bl {{.*}} <_start+0x8>
23+
# PDE-NEXT: nop
2424

2525
# PIC-LABEL: <_start>:
2626
# PIC-NEXT: bl {{.*}} <__plt_undefweak1>

lld/test/ELF/riscv-gp.s

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@
1414
# SEC32: {{0*}}000039c0 0 NOTYPE GLOBAL DEFAULT [[#SDATA]] __global_pointer$
1515

1616
# SEC64: [ [[#SDATA:]]] .sdata PROGBITS {{0*}}000032e0
17-
# SEC64: '.dynsym'
18-
# SEC64-NOT: __global_pointer$
19-
# SEC64: '.symtab'
2017
# SEC64: {{0*}}00003ae0 0 NOTYPE GLOBAL DEFAULT [[#SDATA]] __global_pointer$
2118

2219
# ERR: error: relocation R_RISCV_PCREL_HI20 cannot be used against symbol '__global_pointer$'; recompile with -fPIC
2320

2421
# RUN: ld.lld -pie --no-dynamic-linker --export-dynamic %t.64.o -o %t.64e
25-
# RUN: llvm-readelf -s %t.64e | FileCheck %s --check-prefix=STATICE
22+
# RUN: llvm-readelf -s %t.64e | FileCheck %s --check-prefix=STATICPIE
2623

27-
# STATICE: '.dynsym'
28-
# STATICE: __global_pointer$
29-
# STATICE: '.symtab'
30-
# STATICE: __global_pointer$
24+
# STATICPIE: '.dynsym'
25+
# STATICPIE-NOT: __global_pointer$
26+
# STATICPIE: '.symtab'
27+
# STATICPIE: __global_pointer$
3128

3229
## -r mode does not define __global_pointer$.
3330
# RUN: ld.lld -r %t.64.o -o %t.64.ro

lld/test/ELF/weak-undef-lib.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# RUN: llvm-readobj --dyn-syms %t.so | FileCheck %s
88

99
# RUN: ld.lld -pie -o %t %t1.o --start-lib %t2.o
10-
# RUN: llvm-readelf --dyn-syms %t | FileCheck %s --check-prefix=STATICPIE
10+
# RUN: llvm-readobj --dyn-syms %t | FileCheck %s
1111

1212
# CHECK: Name: foo
1313
# CHECK-NEXT: Value: 0x0

lld/test/ELF/weak-undef-no-dynamic-linker.s

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
33
# RUN: ld.lld -pie %t.o -o %t
4-
# RUN: llvm-readobj --dyn-syms %t | FileCheck --check-prefix=NO %s
4+
# RUN: llvm-readobj --dyn-syms %t | FileCheck %s
55
# RUN: ld.lld -pie --no-dynamic-linker %t.o -o %t
66
# RUN: llvm-readobj --dyn-syms %t | FileCheck --check-prefix=NO %s
77

8-
## With static PIE (whether or not --no-dynamic-linker is specified), don't
9-
## emit undefined weak symbols to .dynsym . This suppresses relocations.
8+
## With --no-dynamic-linker, don't emit undefined weak symbols to .dynsym .
9+
## This will suppress a relocation.
10+
# CHECK: Name: foo
1011
# NO-NOT: Name: foo
1112

1213
.weak foo

lld/test/ELF/weak-undef-rw.s

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
33
# RUN: ld.lld %t.o -o %t --export-dynamic
4-
# RUN: llvm-readelf -r --hex-dump=.data %t | FileCheck %s --check-prefix=STATIC
4+
# RUN: llvm-readelf -r --hex-dump=.data %t | FileCheck %s --check-prefix=NOPIC
55
# RUN: ld.lld %t.o -o %t.pie -pie
6-
# RUN: llvm-readelf -r --hex-dump=.data %t.pie | FileCheck %s --check-prefix=STATIC
6+
# RUN: llvm-readobj -r %t.pie | FileCheck %s --check-prefix=PIC
77
# RUN: ld.lld %t.o -o %t.so -shared
88
# RUN: llvm-readobj -r %t.so | FileCheck %s --check-prefix=PIC
99

1010
## gABI leaves the behavior of weak undefined references implementation defined.
11-
## We choose to resolve them statically for static linking and produce dynamic relocations
12-
## for dynamic linking (-shared or at least one input DSO).
11+
## We choose to resolve them statically for -no-pie and produce dynamic relocations
12+
## for -pie and -shared.
1313
##
1414
## Note: Some ports of GNU ld support -z nodynamic-undefined-weak that we don't
1515
## implement.
1616

17-
# STATIC: no relocations
18-
# STATIC: Hex dump of section '.data':
19-
# STATIC-NEXT: {{.*}} 00000000 00000000 .
20-
# STATIC-EMPTY:
17+
# NOPIC: no relocations
18+
# NOPIC: Hex dump of section '.data':
19+
# NOPIC-NEXT: {{.*}} 00000000 00000000
20+
# NOPIC-EMPTY:
2121

2222
# PIC: .rela.dyn {
2323
# PIC-NEXT: R_X86_64_64 foobar 0x0

0 commit comments

Comments
 (0)