Skip to content

Commit 96c45a7

Browse files
authored
[MC] Rename temporary symbols of empty name to ".L0 " (#89693)
Temporary symbols generated for .eh_frame and .debug_line have an empty name, which appear in .symtab in the presence of RISC-V style linker relaxation and will not be discarded by ld/objcopy --discard-locals (-X). In contrast, GNU assembler's riscv port assigns a fake name ".L0 " (with a trailing space) to these symbols so that will be discarded by ld/objcopy --discard-locals. This patch matches the GNU behavior. Since Clang's RISC-V targets pass -X to ld, and GNU ld defaults to -X for RISC-V targets, these ".L0 " symbols will be discarded after linking by default, as expected by users. The llvm-symbolizer special case for RISC-V `SF_FormatSpecific` symbols https://reviews.llvm.org/D98669 needs to be adjusted. Note: `"":` in assembly currently crashes.
1 parent 13188bc commit 96c45a7

File tree

13 files changed

+78
-52
lines changed

13 files changed

+78
-52
lines changed

lld/test/ELF/mips-eh_frame-pic.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
# RELOCS: .rel{{a?}}.eh_frame {
3737
# ABS32-RELOCS-NEXT: 0x1C R_MIPS_32 .text
3838
# ABS64-RELOCS-NEXT: 0x1C R_MIPS_64/R_MIPS_NONE/R_MIPS_NONE .text
39-
# PIC64-RELOCS-NEXT: 0x1C R_MIPS_PC32/R_MIPS_NONE/R_MIPS_NONE <null>
40-
# PIC32-RELOCS-NEXT: 0x1C R_MIPS_PC32 <null>
39+
# PIC64-RELOCS-NEXT: 0x1C R_MIPS_PC32/R_MIPS_NONE/R_MIPS_NONE .L0
40+
# PIC32-RELOCS-NEXT: 0x1C R_MIPS_PC32 .L0
4141
# RELOCS-NEXT: }
4242

4343
# ABS64-EH-FRAME: Augmentation data: 0C

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,8 @@ Expected<uint32_t> ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
801801
} else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
802802
if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
803803
StringRef Name = *NameOrErr;
804-
// Mark empty name symbols (used for label differences) and mapping
805-
// symbols.
806-
if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$x"))
804+
// Mark fake labels (used for label differences) and mapping symbols.
805+
if (Name == ".L0 " || Name.starts_with("$d") || Name.starts_with("$x"))
807806
Result |= SymbolRef::SF_FormatSpecific;
808807
} else {
809808
// TODO: Actually report errors helpfully.

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,13 @@ void ELFWriter::computeSymbolTable(
725725
HasLargeSectionIndex = true;
726726
}
727727

728+
// Temporary symbols generated for certain assembler features (.eh_frame,
729+
// .debug_line) of an empty name may be referenced by relocations due to
730+
// linker relaxation. Rename them to ".L0 " to match the gas fake label name
731+
// and allow ld/objcopy --discard-locals to discard such symbols.
728732
StringRef Name = Symbol.getName();
733+
if (Name.empty())
734+
Name = ".L0 ";
729735

730736
// Sections have their own string table
731737
if (Symbol.getType() != ELF::STT_SECTION) {

llvm/test/CodeGen/RISCV/fixups-diff.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ entry:
2727
; CHECK: }
2828

2929
; CHECK: Section {{.*}} .rela.eh_frame {
30-
; CHECK-NEXT: 0x1C R_RISCV_32_PCREL <null> 0x0
30+
; CHECK-NEXT: 0x1C R_RISCV_32_PCREL .L0 0x0
3131
; CHECK-NEXT: }
3232

3333
!llvm.dbg.cu = !{!0}

llvm/test/DebugInfo/LoongArch/dwarf-loongarch-relocs.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
; RELOCS-BOTH: Section ({{.*}}) .rela.debug_frame {
1919
; RELOCS-NORL-NEXT: 0x1C R_LARCH_32 .debug_frame 0x0
2020
; RELOCS-NORL-NEXT: 0x20 R_LARCH_64 .text 0x0
21-
; RELOCS-ENRL-NEXT: 0x1C R_LARCH_32 <null> 0x0
22-
; RELOCS-ENRL-NEXT: 0x20 R_LARCH_64 <null> 0x0
23-
; RELOCS-ENRL-NEXT: 0x28 R_LARCH_ADD64 <null> 0x0
24-
; RELOCS-ENRL-NEXT: 0x28 R_LARCH_SUB64 <null> 0x0
25-
; RELOCS-ENRL-NEXT: 0x3F R_LARCH_ADD6 <null> 0x0
26-
; RELOCS-ENRL-NEXT: 0x3F R_LARCH_SUB6 <null> 0x0
21+
; RELOCS-ENRL-NEXT: 0x1C R_LARCH_32 .L0 0x0
22+
; RELOCS-ENRL-NEXT: 0x20 R_LARCH_64 .L0 0x0
23+
; RELOCS-ENRL-NEXT: 0x28 R_LARCH_ADD64 .L0 0x0
24+
; RELOCS-ENRL-NEXT: 0x28 R_LARCH_SUB64 .L0 0x0
25+
; RELOCS-ENRL-NEXT: 0x3F R_LARCH_ADD6 .L0 0x0
26+
; RELOCS-ENRL-NEXT: 0x3F R_LARCH_SUB6 .L0 0x0
2727
; RELOCS-BOTH-NEXT: }
2828
; RELOCS-BOTH: Section ({{.*}}) .rela.debug_line {
2929
; RELOCS-BOTH-NEXT: 0x22 R_LARCH_32 .debug_line_str 0x0
3030
; RELOCS-BOTH-NEXT: 0x31 R_LARCH_32 .debug_line_str 0x2
3131
; RELOCS-BOTH-NEXT: 0x46 R_LARCH_32 .debug_line_str 0x1B
3232
; RELOCS-NORL-NEXT: 0x4F R_LARCH_64 .text 0x0
33-
; RELOCS-ENRL-NEXT: 0x4F R_LARCH_64 <null> 0x0
34-
; RELOCS-ENRL-NEXT: 0x5F R_LARCH_ADD16 <null> 0x0
35-
; RELOCS-ENRL-NEXT: 0x5F R_LARCH_SUB16 <null> 0x0
33+
; RELOCS-ENRL-NEXT: 0x4F R_LARCH_64 .L0 0x0
34+
; RELOCS-ENRL-NEXT: 0x5F R_LARCH_ADD16 .L0 0x0
35+
; RELOCS-ENRL-NEXT: 0x5F R_LARCH_SUB16 .L0 0x0
3636
; RELOCS-BOTH-NEXT: }
3737
; RELOCS-BOTH-NEXT: ]
3838

llvm/test/DebugInfo/RISCV/dwarf-riscv-relocs.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
; Check that we actually have relocations, otherwise this is kind of pointless.
88
; READOBJ-RELOCS: Section ({{.*}}) .rela.debug_info {
9-
; READOBJ-RELOCS: 0x1B R_RISCV_ADD32 <null> 0x0
10-
; READOBJ-RELOCS-NEXT: 0x1B R_RISCV_SUB32 <null> 0x0
9+
; READOBJ-RELOCS: 0x1B R_RISCV_ADD32 .L0 0x0
10+
; READOBJ-RELOCS-NEXT: 0x1B R_RISCV_SUB32 .L0 0x0
1111
; READOBJ-RELOCS: Section ({{.*}}) .rela.debug_frame {
12-
; READOBJ-RELOCS: 0x20 R_RISCV_ADD32 <null> 0x0
13-
; READOBJ-RELOCS-NEXT: 0x20 R_RISCV_SUB32 <null> 0x0
12+
; READOBJ-RELOCS: 0x20 R_RISCV_ADD32 .L0 0x0
13+
; READOBJ-RELOCS-NEXT: 0x20 R_RISCV_SUB32 .L0 0x0
1414
; READOBJ-RELOCS: Section ({{.*}}) .rela.debug_line {
15-
; READOBJ-RELOCS: 0x5A R_RISCV_ADD16 <null> 0x0
16-
; READOBJ-RELOCS-NEXT: 0x5A R_RISCV_SUB16 <null> 0x0
15+
; READOBJ-RELOCS: 0x5A R_RISCV_ADD16 .L0 0x0
16+
; READOBJ-RELOCS-NEXT: 0x5A R_RISCV_SUB16 .L0 0x0
1717

1818
; Check that we can print the source, even with relocations.
1919
; OBJDUMP-SOURCE: Disassembly of section .text:

llvm/test/DebugInfo/RISCV/relax-debug-frame.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
; RUN: | FileCheck -check-prefix=RELAX-DWARFDUMP %s
55
;
66
; RELAX: Section ({{.*}}) .rela.eh_frame {
7-
; RELAX-NEXT: 0x1C R_RISCV_32_PCREL <null> 0x0
8-
; RELAX-NEXT: 0x30 R_RISCV_32_PCREL <null> 0x0
9-
; RELAX-NEXT: 0x44 R_RISCV_32_PCREL <null> 0x0
10-
; RELAX-NEXT: 0x48 R_RISCV_ADD32 <null> 0x0
11-
; RELAX-NEXT: 0x48 R_RISCV_SUB32 <null> 0x0
7+
; RELAX-NEXT: 0x1C R_RISCV_32_PCREL .L0 0x0
8+
; RELAX-NEXT: 0x30 R_RISCV_32_PCREL .L0 0x0
9+
; RELAX-NEXT: 0x44 R_RISCV_32_PCREL .L0 0x0
10+
; RELAX-NEXT: 0x48 R_RISCV_ADD32 .L0 0x0
11+
; RELAX-NEXT: 0x48 R_RISCV_SUB32 .L0 0x0
1212
; RELAX-NEXT: }
1313

1414
; RELAX-DWARFDUMP-NOT: error: failed to compute relocation

llvm/test/DebugInfo/Symbolize/ELF/riscv-empty-name-symbol.s renamed to llvm/test/DebugInfo/Symbolize/ELF/riscv-temporary-symbol.s

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# REQUIRES: riscv-registered-target
2-
## Ignore empty name symbols.
2+
## Ignore .L0 symbols that are generated by LLVM integrated assembler and GNU
3+
## assembler for .debug_line/.eh_frame related assembler directives.
34

45
# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t
56
# RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=SYM
67

7-
# SYM: 0000000000000004 0 NOTYPE LOCAL DEFAULT [[#]] {{$}}
8+
# SYM: 0000000000000004 0 NOTYPE LOCAL DEFAULT [[#]] .L0 {{$}}
89
# SYM: 0000000000000000 0 NOTYPE GLOBAL DEFAULT [[#]] foo
910

1011
## Make sure we test at an address larger than or equal to an empty name symbol.

llvm/test/ExecutionEngine/JITLink/RISCV/anonymous_symbol.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# the section start and section end. So that by relocating these symbol, the section length
88
# can be calculated.
99
#
10-
# CHECK: Creating defined graph symbol for ELF symbol ""
10+
# CHECK: Creating defined graph symbol for ELF symbol ".L0 "
1111
# CHECK: Creating defined graph symbol for ELF symbol "main"
1212
.text
1313
.globl main

llvm/test/MC/ELF/RISCV/gen-dwarf.s

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,28 @@
4040
# CHECK-NEXT: 0x00000020: [DW_RLE_end_of_list ]
4141

4242
# RELOC: Section ([[#]]) .rela.eh_frame {
43-
# RELOC-NEXT: 0x1C R_RISCV_32_PCREL <null> 0x0
44-
# RELOC-NEXT: 0x20 R_RISCV_ADD32 <null> 0x0
45-
# RELOC-NEXT: 0x20 R_RISCV_SUB32 <null> 0x0
46-
# RELOC-NEXT: 0x25 R_RISCV_SET6 <null> 0x0
47-
# RELOC-NEXT: 0x25 R_RISCV_SUB6 <null> 0x0
48-
# RELOC-NEXT: 0x34 R_RISCV_32_PCREL <null> 0x0
43+
# RELOC-NEXT: 0x1C R_RISCV_32_PCREL .L0 0x0
44+
# RELOC-NEXT: 0x20 R_RISCV_ADD32 .L0 0x0
45+
# RELOC-NEXT: 0x20 R_RISCV_SUB32 .L0 0x0
46+
# RELOC-NEXT: 0x25 R_RISCV_SET6 .L0 0x0
47+
# RELOC-NEXT: 0x25 R_RISCV_SUB6 .L0 0x0
48+
# RELOC-NEXT: 0x34 R_RISCV_32_PCREL .L0 0x0
4949
# RELOC-NEXT: }
5050

5151
# RELOC: Section ([[#]]) .rela.debug_rnglists {
5252
# RELOC-NEXT: 0xD R_RISCV_64 .text.foo 0x0
53-
# RELOC-NEXT: 0x15 R_RISCV_SET_ULEB128 <null> 0x0
53+
# RELOC-NEXT: 0x15 R_RISCV_SET_ULEB128 .L0 0x0
5454
# RELOC-NEXT: 0x15 R_RISCV_SUB_ULEB128 .text.foo 0x0
5555
# RELOC-NEXT: 0x17 R_RISCV_64 .text.bar 0x0
5656
# RELOC-NEXT: }
5757

5858
# RELOC: Section ([[#]]) .rela.debug_line {
59-
# RELOC: R_RISCV_ADD16 <null> 0x0
60-
# RELOC-NEXT: R_RISCV_SUB16 <null> 0x0
61-
# RELOC-NEXT: R_RISCV_ADD16 <null> 0x0
62-
# RELOC-NEXT: R_RISCV_SUB16 <null> 0x0
63-
# RELOC-NEXT: R_RISCV_ADD16 <null> 0x0
64-
# RELOC-NEXT: R_RISCV_SUB16 <null> 0x0
59+
# RELOC: R_RISCV_ADD16 .L0 0x0
60+
# RELOC-NEXT: R_RISCV_SUB16 .L0 0x0
61+
# RELOC-NEXT: R_RISCV_ADD16 .L0 0x0
62+
# RELOC-NEXT: R_RISCV_SUB16 .L0 0x0
63+
# RELOC-NEXT: R_RISCV_ADD16 .L0 0x0
64+
# RELOC-NEXT: R_RISCV_SUB16 .L0 0x0
6565
# RELOC: }
6666

6767
# RELOC: Hex dump of section '.eh_frame':

0 commit comments

Comments
 (0)