Skip to content

Commit 3050061

Browse files
authored
[AsmPrinter] Link .section_sizes to the correct section (llvm#135583)
AsmPrinter may switch the current section when e.g., emitting a jump table for a switch. `.stack_sizes` should still be linked to the function section. If the section is wrong, readelf emits a warning "relocation symbol is not in the expected section".
1 parent e7aed23 commit 3050061

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,7 @@ void AsmPrinter::emitStackSizeSection(const MachineFunction &MF) {
15861586
return;
15871587

15881588
MCSection *StackSizeSection =
1589-
getObjFileLowering().getStackSizesSection(*getCurrentSection());
1589+
getObjFileLowering().getStackSizesSection(*MF.getSection());
15901590
if (!StackSizeSection)
15911591
return;
15921592

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
; RUN: llc -mtriple=aarch64 -aarch64-min-jump-table-entries=4 -stack-size-section %s -o - | FileCheck %s
2+
3+
; CHECK-LABEL: .section .stack_sizes,"o",@progbits,.text{{$}}
4+
; CHECK-NEXT: .xword .Lfunc_begin0
5+
; CHECK-NEXT: .byte 0
6+
define void @empty() {
7+
ret void
8+
}
9+
10+
; CHECK-LABEL: .section .stack_sizes,"o",@progbits,.text{{$}}
11+
; CHECK-NEXT: .xword .Lfunc_begin1
12+
; CHECK-NEXT: .ascii "\200\001"
13+
define void @non_empty() #0 {
14+
alloca [32 x i32]
15+
ret void
16+
}
17+
18+
; CHECK-LABEL: dynalloc:
19+
; CHECK-NOT: .section .stack_sizes
20+
define void @dynalloc(i32 %n) #0 {
21+
alloca i32, i32 %n
22+
ret void
23+
}
24+
25+
; Check that .stack_sizes section is linked to the function's section (.text),
26+
; and not to the section containing the jump table (.rodata).
27+
; CHECK-LABEL: linked_section:
28+
; CHECK: .section .rodata,"a",@progbits
29+
; CHECK: .section .stack_sizes,"o",@progbits,.text
30+
; CHECK-NEXT: .xword .Lfunc_begin3
31+
; CHECK-NEXT: .ascii "\220\001"
32+
declare void @case0()
33+
declare void @case1()
34+
declare void @case2()
35+
declare void @case3()
36+
define void @linked_section(i32 %x) {
37+
%arr = alloca [32 x i32]
38+
switch i32 %x, label %sw.epilog [
39+
i32 0, label %sw.bb0
40+
i32 1, label %sw.bb1
41+
i32 2, label %sw.bb2
42+
i32 3, label %sw.bb3
43+
]
44+
45+
sw.bb0:
46+
call void @case0()
47+
ret void
48+
49+
sw.bb1:
50+
call void @case1()
51+
ret void
52+
53+
sw.bb2:
54+
call void @case2()
55+
ret void
56+
57+
sw.bb3:
58+
call void @case3()
59+
ret void
60+
61+
sw.epilog:
62+
ret void
63+
}

llvm/test/CodeGen/SystemZ/stack-size-section.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,34 @@ define void @dynalloc(i32 %N) #0 {
3838
ret void
3939
}
4040

41+
; Check that .stack_sizes section is linked to the function's section (.text),
42+
; and not to the section containing the jump table (.rodata).
43+
; CHECK-LABEL: .section .stack_sizes,"o",@progbits,.text{{$}}
44+
; CHECK-NEXT: .quad .Lfunc_begin4
45+
; CHECK-NEXT: .ascii "\260!"
46+
define i32 @linked_section(i32 %x) {
47+
%arr = alloca [1024 x i32]
48+
switch i32 %x, label %sw.epilog [
49+
i32 0, label %sw.bb0
50+
i32 1, label %sw.bb1
51+
i32 2, label %sw.bb2
52+
i32 3, label %sw.bb3
53+
]
54+
55+
sw.bb0:
56+
ret i32 0
57+
58+
sw.bb1:
59+
ret i32 1
60+
61+
sw.bb2:
62+
ret i32 2
63+
64+
sw.bb3:
65+
ret i32 3
66+
67+
sw.epilog:
68+
ret i32 -1
69+
}
70+
4171
attributes #0 = { "frame-pointer"="all" }

0 commit comments

Comments
 (0)