File tree Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -1034,7 +1034,7 @@ void LinkerScript::assignOffsets(OutputSection *sec) {
1034
1034
}
1035
1035
}
1036
1036
1037
- static bool isDiscardable (OutputSection &sec) {
1037
+ static bool isDiscardable (const OutputSection &sec) {
1038
1038
if (sec.name == " /DISCARD/" )
1039
1039
return true ;
1040
1040
@@ -1063,6 +1063,11 @@ static bool isDiscardable(OutputSection &sec) {
1063
1063
return true ;
1064
1064
}
1065
1065
1066
+ bool LinkerScript::isDiscarded (const OutputSection *sec) const {
1067
+ return hasSectionsCommand && (getFirstInputSection (sec) == nullptr ) &&
1068
+ isDiscardable (*sec);
1069
+ }
1070
+
1066
1071
static void maybePropagatePhdrs (OutputSection &sec,
1067
1072
std::vector<StringRef> &phdrs) {
1068
1073
if (sec.phdrs .empty ()) {
Original file line number Diff line number Diff line change @@ -318,6 +318,8 @@ class LinkerScript final {
318
318
void processSymbolAssignments ();
319
319
void declareSymbols ();
320
320
321
+ bool isDiscarded (const OutputSection *sec) const ;
322
+
321
323
// Used to handle INSERT AFTER statements.
322
324
void processInsertCommands ();
323
325
Original file line number Diff line number Diff line change @@ -2276,7 +2276,7 @@ template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
2276
2276
Default = Out::elfHeader;
2277
2277
2278
2278
auto define = [=](StringRef start, StringRef end, OutputSection *os) {
2279
- if (os) {
2279
+ if (os && !script-> isDiscarded (os) ) {
2280
2280
addOptionalRegular (start, os, 0 );
2281
2281
addOptionalRegular (end, os, -1 );
2282
2282
} else {
Original file line number Diff line number Diff line change
1
+ # REQUIRES: x86
2
+ # RUN: split-file %s %t
3
+ # RUN: llvm-mc -filetype=obj -triple=x86_64 %t/t.s -o %t.o
4
+
5
+ ## PR52534: https://bugs.llvm.org/show_bug.cgi?id=52534
6
+ ## Check case where .preinit_array is discarded.
7
+ ## Link should succeed without causing an out of range relocation error.
8
+ # RUN: ld.lld -T %t/discarded.script %t.o -o %t1 --image-base=0x80000000
9
+ # RUN: llvm-readelf -s %t1 | FileCheck --check-prefixes=CHECK,DISCARDED %s
10
+
11
+ ## Check case where .preinit_array is emitted but empty.
12
+ # RUN: ld.lld -T %t/empty.script %t.o -o %t2
13
+ # RUN: llvm-readelf -s %t2 | FileCheck --check-prefixes=CHECK,EMPTY %s
14
+
15
+ # CHECK: [[#%x,ADDR:]] 0 NOTYPE LOCAL HIDDEN [[#]] __preinit_array_start
16
+ # CHECK-NEXT: [[#ADDR]] 0 NOTYPE LOCAL HIDDEN [[#]] __preinit_array_end
17
+
18
+ # DISCARDED-NEXT: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] _start
19
+
20
+ # EMPTY-NOT: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] _start
21
+ # EMPTY: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] ADDR
22
+
23
+ #--- t.s
24
+ .global _start
25
+ _start:
26
+ movq __preinit_array_start@GOTPCREL(%rip),%rax
27
+ movq __preinit_array_end@GOTPCREL(%rip),%rax
28
+
29
+ #--- discarded.script
30
+ SECTIONS {
31
+ .text : { *(.text); }
32
+ .preinit_array : { *(.preinit_array); }
33
+ }
34
+
35
+ #--- empty.script
36
+ SECTIONS {
37
+ .text : { *(.text); }
38
+ .preinit_array : { ADDR = .; *(.preinit_array); }
39
+ }
You can’t perform that action at this time.
0 commit comments