Skip to content

Commit b9f2c16

Browse files
committed
[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 4c8ec8f commit b9f2c16

File tree

14 files changed

+79
-53
lines changed

14 files changed

+79
-53
lines changed

bolt/test/RISCV/unnamed-sym-no-entry.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/// Verify that the binary indeed contains an unnamed symbol at _start
99
// RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-ELF
1010
// CHECK-ELF-DAG: [[#%x,START:]] {{.*}} FUNC GLOBAL DEFAULT [[#%d,SECTION:]] _start{{$}}
11-
// CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] {{$}}
11+
// CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] .L0 {{$}}
1212

1313
/// Verify that BOLT did not create an extra entry point for the unnamed symbol
1414
// RUN: llvm-bolt -o %t.bolt %t --print-cfg | FileCheck %s

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

+2-2
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

+2-3
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

+6
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

+1-1
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

+9-9
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

+6-6
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

+5-5
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

+3-2
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

+1-1
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

+13-13
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':

llvm/test/MC/RISCV/cfi-advance.s

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
# RUN: llvm-mc -filetype=obj -triple riscv32 %s -o %t.o
2-
# RUN: llvm-readobj -r %t.o | FileCheck -check-prefix=CHECK %s
2+
# RUN: llvm-readelf -sr %t.o | FileCheck %s
33
# RUN: llvm-dwarfdump --debug-frame %t.o 2>&1 \
44
# RUN: | FileCheck -check-prefix=CHECK-DWARFDUMP %s
55

6-
# CHECK: .rela.eh_frame {
7-
# CHECK-NEXT: 0x1C R_RISCV_32_PCREL <null> 0x0
8-
# CHECK-NEXT: 0x35 R_RISCV_SET6 <null> 0x0
9-
# CHECK-NEXT: 0x35 R_RISCV_SUB6 <null> 0x0
10-
# CHECK-NEXT: }
6+
7+
# CHECK: Relocation section '.rela.text1' at offset {{.*}} contains 1 entries:
8+
# CHECK-NEXT: Offset Info Type Sym. Value Symbol's Name + Addend
9+
# CHECK-NEXT: 00000000 00000313 R_RISCV_CALL_PLT 00000004 .L0 + 0
10+
# CHECK-EMPTY:
11+
# CHECK-NEXT: Relocation section '.rela.eh_frame' at offset {{.*}} contains 3 entries:
12+
# CHECK: Offset Info Type Sym. Value Symbol's Name + Addend
13+
# CHECK-NEXT: 0000001c 00000139 R_RISCV_32_PCREL 00000000 .L0 + 0
14+
# CHECK-NEXT: 00000035 00000b35 R_RISCV_SET6 00010178 .L0 + 0
15+
# CHECK-NEXT: 00000035 00000934 R_RISCV_SUB6 0001016e .L0 + 0
16+
# CHECK-EMPTY:
17+
# CHECK: Symbol table '.symtab' contains 15 entries:
18+
# CHECK-NEXT: Num: Value Size Type Bind Vis Ndx Name
19+
# CHECK-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
20+
# CHECK-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
21+
# CHECK: 3: 00000004 0 NOTYPE LOCAL DEFAULT 2 .L0{{$}}
22+
# CHECK: 9: 0001016e 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
23+
# CHECK: 11: 00010178 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
24+
1125
# CHECK-DWARFDUMP: DW_CFA_advance_loc1: 104
1226
# CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +8
1327
# CHECK-DWARFDUMP-NEXT: DW_CFA_advance_loc2: 259
@@ -23,6 +37,9 @@
2337
test:
2438
.cfi_startproc
2539
nop
40+
## This looks similar to fake label names ".L0 ". Even if this is ".L0 ",
41+
## the assembler will not conflate it with fake labels.
42+
.L0:
2643
.zero 100, 0x90
2744
.cfi_def_cfa_offset 8
2845
nop
@@ -36,3 +53,6 @@ test:
3653
.cfi_def_cfa_offset 8
3754
nop
3855
.cfi_endproc
56+
57+
.section .text1,"ax"
58+
call .L0

llvm/test/MC/RISCV/fde-reloc.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func:
1212
.cfi_endproc
1313

1414
# CHECK: Section (4) .rela.eh_frame {
15-
# CHECK-NEXT: 0x1C R_RISCV_32_PCREL <null> 0x0
15+
# CHECK-NEXT: 0x1C R_RISCV_32_PCREL .L0 0x0
1616
# CHECK-NEXT: }
1717
# CHECK: Hex dump of section '.eh_frame':
1818
# CHECK-NEXT: 0x00000000 10000000 00000000 017a5200 017c0101

llvm/test/MC/RISCV/scoped-relaxation.s

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
.dword function - .
1010

1111
# CHECK: 0x0 R_RISCV_ADD64 function 0x0
12-
# CHECK-NEXT: 0x0 R_RISCV_SUB64 <null> 0x0
12+
# CHECK-NEXT: 0x0 R_RISCV_SUB64 .L0 0x0
1313

1414
# Relaxed reference, this will resolve to a pair of `RISCV_ADD64` and
1515
# `RISCV_SUB64` relocation.
@@ -19,7 +19,7 @@
1919
.option pop
2020

2121
# CHECK: 0x8 R_RISCV_ADD64 function 0x0
22-
# CHECK-NEXT: 0x8 R_RISCV_SUB64 <null> 0x0
22+
# CHECK-NEXT: 0x8 R_RISCV_SUB64 .L0 0x0
2323

2424
# Unrelaxed reference, this will resolve to a pair of `RISCV_ADD64` and
2525
# `RISCV_SUB64` relocation due to relaxation being sticky to the file.
@@ -29,6 +29,6 @@
2929
.option pop
3030

3131
# CHECK: 0x10 R_RISCV_ADD64 function 0x0
32-
# CHECK-NEXT: 0x10 R_RISCV_SUB64 <null> 0x0
32+
# CHECK-NEXT: 0x10 R_RISCV_SUB64 .L0 0x0
3333

3434
# CHECK: }

0 commit comments

Comments
 (0)