Skip to content

Commit

Permalink
Revert "[ELF] Refine isExported/isPreemptible condition"
Browse files Browse the repository at this point in the history
This reverts commit 994cea3.

Try to fix the bolt test failures in pre-merge checks.
  • Loading branch information
nikic committed Feb 3, 2025
1 parent df2de13 commit b84f7d1
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 46 deletions.
2 changes: 2 additions & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ struct Config {
bool gdbIndex;
bool gnuHash = false;
bool gnuUnique;
bool hasDynSymTab;
bool ignoreDataAddressEquality;
bool ignoreFunctionAddressEquality;
bool ltoCSProfileGenerate;
Expand All @@ -311,6 +312,7 @@ struct Config {
bool mipsN32Abi = false;
bool mmapOutputFile;
bool nmagic;
bool noDynamicLinker = false;
bool noinhibitExec;
bool nostdlib;
bool oFormatBinary;
Expand Down
11 changes: 9 additions & 2 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,11 @@ static StringRef getDynamicLinker(Ctx &ctx, opt::InputArgList &args) {
auto *arg = args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
if (!arg)
return "";
if (arg->getOption().getID() == OPT_no_dynamic_linker)
if (arg->getOption().getID() == OPT_no_dynamic_linker) {
// --no-dynamic-linker suppresses undefined weak symbols in .dynsym
ctx.arg.noDynamicLinker = true;
return "";
}
return arg->getValue();
}

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

parseFiles(ctx, files);

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

// If an entry symbol is in a static archive, pull out that file now.
if (Symbol *sym = ctx.symtab->find(ctx.arg.entry))
Expand Down
4 changes: 1 addition & 3 deletions lld/ELF/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ void elf::parseVersionAndComputeIsPreemptible(Ctx &ctx) {
// Symbol themselves might know their versions because symbols
// can contain versions in the form of <name>@<version>.
// Let them parse and update their names to exclude version suffix.
// In addition, compute isExported and isPreemptible.
bool hasDynsym = ctx.hasDynsym;
bool maybePreemptible = ctx.sharedFiles.size() || ctx.arg.shared;
for (Symbol *sym : ctx.symtab->getSymbols()) {
if (sym->hasVersionSuffix)
sym->parseSymbolVersion(ctx);
Expand All @@ -373,7 +371,7 @@ void elf::parseVersionAndComputeIsPreemptible(Ctx &ctx) {
continue;
}
if (!sym->isDefined() && !sym->isCommon()) {
sym->isPreemptible = maybePreemptible && computeIsPreemptible(ctx, *sym);
sym->isPreemptible = computeIsPreemptible(ctx, *sym);
} else if (ctx.arg.exportDynamic &&
(sym->isUsedInRegularObj || !sym->ltoCanOmit)) {
sym->isExported = true;
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4740,7 +4740,7 @@ template <class ELFT> void elf::createSyntheticSections(Ctx &ctx) {

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

if (ctx.hasDynsym) {
if (ctx.arg.hasDynSymTab) {
add(*part.dynSymTab);

part.verSym = std::make_unique<VersionTableSection>(ctx);
Expand Down
7 changes: 3 additions & 4 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
llvm::TimeTraceScope timeScope("Demote symbols");
DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
bool hasDynsym = ctx.hasDynsym;
bool maybePreemptible = ctx.sharedFiles.size() || ctx.arg.shared;
for (Symbol *sym : ctx.symtab->getSymbols()) {
if (auto *d = dyn_cast<Defined>(sym)) {
if (d->section && !d->section->isLive())
Expand All @@ -302,8 +301,7 @@ static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
}

if (hasDynsym)
sym->isPreemptible = maybePreemptible &&
(sym->isUndefined() || sym->isExported) &&
sym->isPreemptible = (sym->isUndefined() || sym->isExported) &&
computeIsPreemptible(ctx, *sym);
}
}
Expand Down Expand Up @@ -1944,7 +1942,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {

// computeBinding might localize a linker-synthesized hidden symbol
// (e.g. __global_pointer$) that was considered exported.
if ((sym->isExported || sym->isPreemptible) && !sym->isLocal()) {
if (ctx.hasDynsym && (sym->isUndefined() || sym->isExported) &&
!sym->isLocal()) {
ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
if (auto *file = dyn_cast<SharedFile>(sym->file))
if (file->isNeeded && !sym->isUndefined())
Expand Down
6 changes: 2 additions & 4 deletions lld/test/ELF/executable-undefined-ignoreall.s
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# REQUIRES: x86

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

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

# CHECK: Relocations [
Expand Down
6 changes: 3 additions & 3 deletions lld/test/ELF/ppc32-weak-undef-call.s
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# REQUIRES: ppc
# RUN: llvm-mc -filetype=obj -triple=powerpc %s -o %t.o
# RUN: ld.lld %t.o -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=STATIC %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=PDE %s
# RUN: ld.lld -pie %t.o -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=STATIC %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=PIC %s
# RUN: ld.lld -shared %t.o -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=PIC %s

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

## With -pie or -shared, create a call stub. ld.bfd produces bl .+0
# PIC: bl 0x[[PLT:[0-9a-f]+]]
Expand Down
16 changes: 8 additions & 8 deletions lld/test/ELF/ppc64-undefined-weak.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t.o
# RUN: ld.lld %t.o -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=STATIC
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=PDE
# RUN: ld.lld -pie %t.o -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=STATIC
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s --check-prefix=PIC
# RUN: ld.lld -shared %t.o -o %t.so
# RUN: llvm-objdump -d --no-show-raw-insn %t.so | FileCheck %s --check-prefix=PIC

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

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

# STATIC-LABEL: <_start>:
# STATIC-NEXT: bl {{.*}} <_start>
# STATIC-NEXT: nop
# STATIC-NEXT: bl {{.*}} <_start+0x8>
# STATIC-NEXT: nop
# PDE-LABEL: <_start>:
# PDE-NEXT: bl {{.*}} <_start>
# PDE-NEXT: nop
# PDE-NEXT: bl {{.*}} <_start+0x8>
# PDE-NEXT: nop

# PIC-LABEL: <_start>:
# PIC-NEXT: bl {{.*}} <__plt_undefweak1>
Expand Down
13 changes: 5 additions & 8 deletions lld/test/ELF/riscv-gp.s
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@
# SEC32: {{0*}}000039c0 0 NOTYPE GLOBAL DEFAULT [[#SDATA]] __global_pointer$

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

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

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

# STATICE: '.dynsym'
# STATICE: __global_pointer$
# STATICE: '.symtab'
# STATICE: __global_pointer$
# STATICPIE: '.dynsym'
# STATICPIE-NOT: __global_pointer$
# STATICPIE: '.symtab'
# STATICPIE: __global_pointer$

## -r mode does not define __global_pointer$.
# RUN: ld.lld -r %t.64.o -o %t.64.ro
Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/weak-undef-lib.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# RUN: llvm-readobj --dyn-syms %t.so | FileCheck %s

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

# CHECK: Name: foo
# CHECK-NEXT: Value: 0x0
Expand Down
7 changes: 4 additions & 3 deletions lld/test/ELF/weak-undef-no-dynamic-linker.s
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
# RUN: ld.lld -pie %t.o -o %t
# RUN: llvm-readobj --dyn-syms %t | FileCheck --check-prefix=NO %s
# RUN: llvm-readobj --dyn-syms %t | FileCheck %s
# RUN: ld.lld -pie --no-dynamic-linker %t.o -o %t
# RUN: llvm-readobj --dyn-syms %t | FileCheck --check-prefix=NO %s

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

.weak foo
Expand Down
16 changes: 8 additions & 8 deletions lld/test/ELF/weak-undef-rw.s
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
# RUN: ld.lld %t.o -o %t --export-dynamic
# RUN: llvm-readelf -r --hex-dump=.data %t | FileCheck %s --check-prefix=STATIC
# RUN: llvm-readelf -r --hex-dump=.data %t | FileCheck %s --check-prefix=NOPIC
# RUN: ld.lld %t.o -o %t.pie -pie
# RUN: llvm-readelf -r --hex-dump=.data %t.pie | FileCheck %s --check-prefix=STATIC
# RUN: llvm-readobj -r %t.pie | FileCheck %s --check-prefix=PIC
# RUN: ld.lld %t.o -o %t.so -shared
# RUN: llvm-readobj -r %t.so | FileCheck %s --check-prefix=PIC

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

# STATIC: no relocations
# STATIC: Hex dump of section '.data':
# STATIC-NEXT: {{.*}} 00000000 00000000 .
# STATIC-EMPTY:
# NOPIC: no relocations
# NOPIC: Hex dump of section '.data':
# NOPIC-NEXT: {{.*}} 00000000 00000000
# NOPIC-EMPTY:

# PIC: .rela.dyn {
# PIC-NEXT: R_X86_64_64 foobar 0x0
Expand Down

0 comments on commit b84f7d1

Please sign in to comment.