Skip to content

Commit 6ba76dd

Browse files
author
Sean Fertile
committedJun 6, 2019
Revert "Revert "Reland D61583 [ELF] Error on relocations to STT_SECTION symbols if the sections were discarded""
This reverts commit 729111cf1824159bb4dd331cab8a829eab30313f. Reverting the previous commit breaks other LLD buildbots. llvm-svn: 362743
1 parent 38c5ee1 commit 6ba76dd

10 files changed

+100
-12
lines changed
 

‎lld/ELF/DWARF.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ LLDDwarfObj<ELFT>::findAux(const InputSectionBase &Sec, uint64_t Pos,
9393
uint32_t SecIndex = File->getSectionIndex(Sym);
9494

9595
// Broken debug info can point to a non-Defined symbol.
96-
auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
96+
Symbol &S = File->getRelocTargetSym(Rel);
97+
auto *DR = dyn_cast<Defined>(&S);
9798
if (!DR) {
99+
if (S.isSection())
100+
return None;
98101
RelType Type = Rel.getType(Config->IsMips64EL);
99102
if (Type != Target->NoneRel)
100103
error(toString(File) + ": relocation " + lld::toString(Type) + " at 0x" +

‎lld/ELF/InputFiles.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,9 @@ template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
10751075

10761076
if (ESym.st_shndx == SHN_UNDEF)
10771077
this->Symbols[I] = make<Undefined>(this, Name, Binding, StOther, Type);
1078+
else if (Sec == &InputSection::Discarded)
1079+
this->Symbols[I] = make<Undefined>(this, Name, Binding, StOther, Type,
1080+
/*DiscardedSecIdx=*/SecIdx);
10781081
else
10791082
this->Symbols[I] =
10801083
make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);

‎lld/ELF/InputSection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
438438
// hopefully creates a frame that is ignored at runtime.
439439
auto *D = dyn_cast<Defined>(&Sym);
440440
if (!D) {
441-
error("STT_SECTION symbol should be defined");
441+
warn("STT_SECTION symbol should be defined");
442+
P->setSymbolAndType(0, 0, false);
442443
continue;
443444
}
444445
SectionBase *Section = D->Section->Repl;

‎lld/ELF/Relocations.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,17 @@ static std::string maybeReportDiscarded(Undefined &Sym, InputSectionBase &Sec,
679679
return "";
680680
ArrayRef<Elf_Shdr_Impl<ELFT>> ObjSections =
681681
CHECK(File->getObj().sections(), File);
682-
std::string Msg =
683-
"relocation refers to a symbol in a discarded section: " + toString(Sym) +
684-
"\n>>> defined in " + toString(File);
682+
683+
std::string Msg;
684+
if (Sym.Type == ELF::STT_SECTION) {
685+
Msg = "relocation refers to a discarded section: ";
686+
Msg += CHECK(
687+
File->getObj().getSectionName(&ObjSections[Sym.DiscardedSecIdx]), File);
688+
} else {
689+
Msg = "relocation refers to a symbol in a discarded section: " +
690+
toString(Sym);
691+
}
692+
Msg += "\n>>> defined in " + toString(File);
685693

686694
Elf_Shdr_Impl<ELFT> ELFSec = ObjSections[Sym.DiscardedSecIdx - 1];
687695
if (ELFSec.sh_type != SHT_GROUP)

‎lld/test/ELF/comdat-discarded-error.s

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@
55
# RUN: echo '.section .text.foo,"axG",@progbits,foo,comdat; .globl bar; bar:' | \
66
# RUN: llvm-mc -filetype=obj -triple=x86_64 - -o %t3.o
77

8-
# RUN: not ld.lld %t1.o %t2.o %t3.o -o /dev/null 2>&1 | FileCheck %s
8+
# RUN: not ld.lld %t2.o %t3.o %t1.o -o /dev/null 2>&1 | FileCheck %s
99

1010
# CHECK: error: relocation refers to a symbol in a discarded section: bar
1111
# CHECK-NEXT: >>> defined in {{.*}}3.o
1212
# CHECK-NEXT: >>> section group signature: foo
1313
# CHECK-NEXT: >>> prevailing definition is in {{.*}}2.o
1414
# CHECK-NEXT: >>> referenced by {{.*}}1.o:(.text+0x1)
1515

16+
# CHECK: error: relocation refers to a discarded section: .text.foo
17+
# CHECK-NEXT: >>> defined in {{.*}}1.o
18+
# CHECK-NEXT: >>> section group signature: foo
19+
# CHECK-NEXT: >>> prevailing definition is in {{.*}}2.o
20+
# CHECK-NEXT: >>> referenced by {{.*}}1.o:(.data+0x0)
21+
1622
.globl _start
1723
_start:
1824
jmp bar
25+
26+
.section .text.foo,"axG",@progbits,foo,comdat
27+
.data
28+
.quad .text.foo
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
3+
# RUN: ld.lld --gdb-index %t.o %t.o -o %t
4+
5+
## .debug_info has a relocation to .text.foo . The second %t.o is discarded.
6+
## Check we don't error on the relocation.
7+
# CHECK: .rela.debug_info {
8+
# CHECK-NEXT: 0xC R_X86_64_64 .text.foo 0x0
9+
10+
.section .text.foo,"axG",@progbits,foo,comdat
11+
.globl foo
12+
.Lfunc_begin0:
13+
foo:
14+
ret
15+
.Lfunc_end0:
16+
17+
.section .debug_abbrev,"",@progbits
18+
.byte 1 # Abbreviation Code
19+
.byte 17 # DW_TAG_compile_unit
20+
.byte 1 # DW_CHILDREN_yes
21+
.byte 17 # DW_AT_low_pc
22+
.byte 1 # DW_FORM_addr
23+
.byte 18 # DW_AT_high_pc
24+
.byte 6 # DW_FORM_data4
25+
.ascii "\264B" # DW_AT_GNU_pubnames
26+
.byte 25 # DW_FORM_flag_present
27+
.byte 0 # EOM(1)
28+
.byte 0 # EOM(2)
29+
.byte 2 # Abbreviation Code
30+
.byte 46 # DW_TAG_subprogram
31+
.byte 0 # DW_CHILDREN_no
32+
.byte 3 # DW_AT_name
33+
.byte 8 # DW_FORM_string
34+
.byte 0 # EOM(1)
35+
.byte 0 # EOM(2)
36+
.byte 0
37+
38+
.section .debug_info,"",@progbits
39+
.Lcu_begin0:
40+
.long .Lcu_end0 - .Lcu_begin0 - 4
41+
.short 4 # DWARF version number
42+
.long 0 # Offset Into Abbrev. Section
43+
.byte 4 # Address Size
44+
.Ldie0:
45+
.byte 1 # Abbrev [1] DW_TAG_compile_unit
46+
.quad .Lfunc_begin0 # DW_AT_low_pc
47+
.long .Lfunc_end0 - .Lfunc_begin0 # DW_AT_high_pc
48+
.byte 2 # Abbrev [2] DW_TAG_subprogram
49+
.asciz "foo" # DW_AT_name
50+
.byte 0
51+
.Lcu_end0:
52+
53+
.section .debug_gnu_pubnames,"",@progbits
54+
.long .LpubNames_end0 - .LpubNames_begin0
55+
.LpubNames_begin0:
56+
.short 2 # Version
57+
.long .Lcu_begin0 # CU Offset
58+
.long .Lcu_end0 - .Lcu_begin0
59+
.long .Ldie0 - .Lcu_begin0
60+
.byte 48 # Attributes: FUNCTION, EXTERNAL
61+
.asciz "foo" # External Name
62+
.long 0
63+
.LpubNames_end0:

‎lld/test/ELF/comdat-discarded-reloc.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
33
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/comdat-discarded-reloc.s -o %t2.o
4-
# RUN: ld.lld -gc-sections %t.o %t2.o -o %t
4+
# RUN: ld.lld -gc-sections --noinhibit-exec %t.o %t2.o -o /dev/null
55

66
## ELF spec doesn't allow a relocation to point to a deduplicated
77
## COMDAT section. Unfortunately this happens in practice (e.g. .eh_frame)

‎lld/test/ELF/comdat.s

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// REQUIRES: x86
22
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
33
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/comdat.s -o %t2.o
4-
// RUN: ld.lld -shared %t.o %t.o %t2.o -o %t
4+
// RUN: ld.lld -shared %t.o %t2.o -o %t
55
// RUN: llvm-objdump -d %t | FileCheck %s
66
// RUN: llvm-readobj -S --symbols %t | FileCheck --check-prefix=READ %s
77

@@ -31,9 +31,7 @@ foo:
3131
// CHECK-EMPTY:
3232
// CHECK-NEXT: bar:
3333
// 0x1000 - 0x1001 - 5 = -6
34-
// 0 - 0x1006 - 5 = -4107
3534
// CHECK-NEXT: 1001: {{.*}} callq -6
36-
// CHECK-NEXT: 1006: {{.*}} callq -4107
3735

3836
.section .text3,"axG",@progbits,zed,comdat,unique,0
3937

‎lld/test/ELF/invalid-undef-section-symbol.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# RUN: yaml2obj %s -o %t.o
2-
# RUN: not ld.lld -r %t.o -o /dev/null 2>&1 | FileCheck %s
2+
# RUN: not ld.lld -r --fatal-warnings %t.o -o /dev/null 2>&1 | FileCheck %s
33

44
# We used to crash at this.
55
# CHECK: STT_SECTION symbol should be defined

‎lld/test/ELF/relocatable-eh-frame.s

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
3-
# RUN: ld.lld -r %t.o %t.o -o %t
3+
# RUN: ld.lld -r %t.o %t.o -o %t 2>&1 | FileCheck --check-prefix=WARN %s
44
# RUN: llvm-readobj -r %t | FileCheck %s
55
# RUN: ld.lld %t -o %t.so -shared
66
# RUN: llvm-objdump -h %t.so | FileCheck --check-prefix=DSO %s
77

8+
# WARN: STT_SECTION symbol should be defined
9+
810
# DSO: .eh_frame 00000034
911

1012
# CHECK: Relocations [

0 commit comments

Comments
 (0)
Please sign in to comment.