Skip to content

Commit f95273f

Browse files
committed
Keep symbols passed by -init and -fini
Previously, symbols passed by -init and -fini look as if they are not referenced by anyone, and the LTO might eliminate them. This patch fixes the issue. Fixes a bug reported in https://bugs.llvm.org/show_bug.cgi?id=43927 Differential Revision: https://reviews.llvm.org/D69985
1 parent 41449c5 commit f95273f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lld/ELF/Driver.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,12 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
17821782
for (StringRef pat : args::getStrings(args, OPT_undefined_glob))
17831783
handleUndefinedGlob(pat);
17841784

1785+
// Mark -init and -fini symbols so that the LTO doesn't eliminate them.
1786+
if (Symbol *sym = symtab->find(config->init))
1787+
sym->isUsedInRegularObj = true;
1788+
if (Symbol *sym = symtab->find(config->fini))
1789+
sym->isUsedInRegularObj = true;
1790+
17851791
// If any of our inputs are bitcode files, the LTO code generator may create
17861792
// references to certain library functions that might not be explicit in the
17871793
// bitcode file's symbol table. If any of those library functions are defined

lld/test/ELF/lto/init-fini.ll

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; REQUIRES: x86
2+
; RUN: llvm-as %s -o %t.o
3+
4+
;;
5+
;; Verify that symbols given by -init and -fini are preserved and
6+
;; DT_INIT/DT_FINI are created.
7+
;;
8+
9+
; RUN: ld.lld -o %t.exe -pie %t.o
10+
; RUN: llvm-nm %t.exe | FileCheck -check-prefix=TEST1 --allow-empty %s
11+
; RUN: llvm-readelf -d %t.exe | FileCheck -check-prefix=TEST2 %s
12+
13+
; TEST1-NOT: foo
14+
; TEST1-NOT: bar
15+
16+
; TEST2-NOT: INIT
17+
; TEST2-NOT: FINI
18+
19+
; RUN: ld.lld -o %t.exe -pie -init=foo -fini=bar %t.o
20+
; RUN: llvm-nm %t.exe | FileCheck -check-prefix=TEST3 %s
21+
; RUN: llvm-readelf -d %t.exe | FileCheck -check-prefix=TEST4 %s
22+
23+
; TEST3: bar
24+
; TEST3: foo
25+
26+
; TEST4: INIT
27+
; TEST4: FINI
28+
29+
target triple = "x86_64-unknown-linux-gnu"
30+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
31+
32+
define void @foo() {
33+
ret void
34+
}
35+
36+
define void @bar() {
37+
ret void
38+
}

0 commit comments

Comments
 (0)