Skip to content

Commit 7620f03

Browse files
authored
[MC] Parse SHF_LINK_ORDER argument before section group name (llvm#77407)
When both SHF_LINK_ORDER | SHF_GROUP flags are set, GNU assembler from 2.35 onwards (https://sourceware.org/PR25381 https://sourceware.org/binutils/docs/as/Section.html) parses the SHF_LINK_ORDER argument before section group name, different from us. This is unfortunate, but does not matter because the `.section` flag `o` is a niche feature only used by compiler instrumentations, not adopted by hand-written assembly, and using both flags is extremely rare. Let's just match GNU assembler. There is another benefit: we now support zero-flag section group with the SHF_LINK_ORDER flag, while previously there isn't a syntax. While here, print 'G' after 'o' to be clear that the 'G' argument is parsed after the 'o' argument. To make the diff smaller, we don't print 'G' after 'w' in the absence of 'o' for now.
1 parent 1b8e39a commit 7620f03

14 files changed

+48
-28
lines changed

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,12 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
616616
if (Mergeable)
617617
if (parseMergeSize(Size))
618618
return true;
619-
if (Group)
620-
if (parseGroup(GroupName, IsComdat))
621-
return true;
622619
if (Flags & ELF::SHF_LINK_ORDER)
623620
if (parseLinkedToSym(LinkedToSym))
624621
return true;
622+
if (Group)
623+
if (parseGroup(GroupName, IsComdat))
624+
return true;
625625
if (maybeParseUniqueID(UniqueID))
626626
return true;
627627
}

llvm/lib/MC/MCSectionELF.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
9090
OS << 'e';
9191
if (Flags & ELF::SHF_EXECINSTR)
9292
OS << 'x';
93-
if (Flags & ELF::SHF_GROUP)
93+
// TODO: Always print G after o to be clear that the 'G' argument is parsed
94+
// after the 'o' argument.
95+
if ((Flags & ELF::SHF_GROUP) && !(Flags & ELF::SHF_LINK_ORDER))
9496
OS << 'G';
9597
if (Flags & ELF::SHF_WRITE)
9698
OS << 'w';
@@ -102,6 +104,8 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
102104
OS << 'T';
103105
if (Flags & ELF::SHF_LINK_ORDER)
104106
OS << 'o';
107+
if ((Flags & ELF::SHF_GROUP) && (Flags & ELF::SHF_LINK_ORDER))
108+
OS << 'G';
105109
if (Flags & ELF::SHF_GNU_RETAIN)
106110
OS << 'R';
107111

@@ -183,13 +187,6 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
183187
OS << "," << EntrySize;
184188
}
185189

186-
if (Flags & ELF::SHF_GROUP) {
187-
OS << ",";
188-
printName(OS, Group.getPointer()->getName());
189-
if (isComdat())
190-
OS << ",comdat";
191-
}
192-
193190
if (Flags & ELF::SHF_LINK_ORDER) {
194191
OS << ",";
195192
if (LinkedToSym)
@@ -198,6 +195,13 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
198195
OS << '0';
199196
}
200197

198+
if (Flags & ELF::SHF_GROUP) {
199+
OS << ",";
200+
printName(OS, Group.getPointer()->getName());
201+
if (isComdat())
202+
OS << ",comdat";
203+
}
204+
201205
if (isUnique())
202206
OS << ",unique," << UniqueID;
203207

llvm/test/CodeGen/AArch64/patchable-function-entry.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ define void @f3() "patchable-function-entry"="3" comdat {
4848
; CHECK-NEXT: .Lfunc_begin3:
4949
; CHECK-COUNT-3: nop
5050
; CHECK-NEXT: ret
51-
; CHECK: .section __patchable_function_entries,"aGwo",@progbits,f3,comdat,f3{{$}}
51+
; CHECK: .section __patchable_function_entries,"awoG",@progbits,f3,f3,comdat{{$}}
5252
; CHECK-NEXT: .p2align 3
5353
; CHECK-NEXT: .xword .Lfunc_begin3
5454
ret void
@@ -60,7 +60,7 @@ define void @f5() "patchable-function-entry"="5" comdat {
6060
; CHECK-NEXT: .Lfunc_begin4:
6161
; CHECK-COUNT-5: nop
6262
; CHECK-NEXT: sub sp, sp, #16
63-
; CHECK: .section __patchable_function_entries,"aGwo",@progbits,f5,comdat,f5{{$}}
63+
; CHECK: .section __patchable_function_entries,"awoG",@progbits,f5,f5,comdat{{$}}
6464
; CHECK: .p2align 3
6565
; CHECK-NEXT: .xword .Lfunc_begin4
6666
%frame = alloca i8, i32 16

llvm/test/CodeGen/LoongArch/patchable-function-entry.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ define void @f5() "patchable-function-entry"="5" comdat {
3131
; CHECK-NEXT: .Lfunc_begin2:
3232
; CHECK-COUNT-5: nop
3333
; CHECK-NEXT: ret
34-
; CHECK: .section __patchable_function_entries,"aGwo",@progbits,f5,comdat,f5{{$}}
34+
; CHECK: .section __patchable_function_entries,"awoG",@progbits,f5,f5,comdat{{$}}
3535
; LA32: .p2align 2
3636
; LA32-NEXT: .word .Lfunc_begin2
3737
; LA64: .p2align 3

llvm/test/CodeGen/Mips/xray-section-group.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $bar = comdat any
2424
define i32 @bar() nounwind noinline uwtable "function-instrument"="xray-always" comdat($bar) {
2525
; CHECK: .section .text.bar,"axG",@progbits,bar,comdat
2626
ret i32 1
27-
; CHECK: .section xray_instr_map,"aGo",@progbits,bar,comdat,bar{{$}}
27+
; CHECK: .section xray_instr_map,"aoG",@progbits,bar,bar,comdat{{$}}
2828
}
2929

3030
; CHECK-OBJ: Section {

llvm/test/CodeGen/RISCV/patchable-function-entry.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ define void @f5() "patchable-function-entry"="5" comdat {
3737
; NORVC-NEXT: jalr zero, 0(ra)
3838
; RVC-COUNT-5: c.nop
3939
; RVC-NEXT: c.jr ra
40-
; CHECK: .section __patchable_function_entries,"aGwo",@progbits,f5,comdat,f5{{$}}
40+
; CHECK: .section __patchable_function_entries,"awoG",@progbits,f5,f5,comdat{{$}}
4141
; RV32: .p2align 2
4242
; RV32-NEXT: .word .Lfunc_begin2
4343
; RV64: .p2align 3

llvm/test/CodeGen/X86/basic-block-sections-labels-functions-sections.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ define linkonce_odr dso_local i32 @_Z4fooTIiET_v() comdat {
3535
; CHECK: .section .text._Z4fooTIiET_v,"axG",@progbits,_Z4fooTIiET_v,comdat
3636
; CHECK-LABEL: _Z4fooTIiET_v:
3737
; CHECK-NEXT: [[FOOCOMDAT_BEGIN:.Lfunc_begin[0-9]+]]:
38-
; CHECK: .section .llvm_bb_addr_map,"Go",@llvm_bb_addr_map,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v{{$}}
38+
; CHECK: .section .llvm_bb_addr_map,"oG",@llvm_bb_addr_map,.text._Z4fooTIiET_v,_Z4fooTIiET_v,comdat{{$}}
3939
; CHECK-NEXT: .byte 2 # version
4040
; CHECK-NEXT: .byte 0 # feature
4141
; CHECK-NEXT: .quad [[FOOCOMDAT_BEGIN]] # function address

llvm/test/CodeGen/X86/gcc_except_table-multi.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ define i32 @group() uwtable comdat personality ptr @__gxx_personality_v0 {
1717
; CHECK: .cfi_endproc
1818
; NORMAL-NEXT: .section .gcc_except_table.group,"aG",@progbits,group,comdat{{$}}
1919
; SEP_BFD-NEXT: .section .gcc_except_table.group,"aG",@progbits,group,comdat{{$}}
20-
; SEP-NEXT: .section .gcc_except_table.group,"aGo",@progbits,group,comdat,group{{$}}
20+
; SEP-NEXT: .section .gcc_except_table.group,"aoG",@progbits,group,group,comdat{{$}}
2121
; SEP_NOUNIQUE-NEXT: .section .gcc_except_table,"aG",@progbits,group,comdat{{$}}
2222
; NOUNIQUE-NEXT: .section .gcc_except_table,"aG",@progbits,group,comdat{{$}}
2323
entry:
@@ -61,7 +61,7 @@ define i32 @zero() uwtable comdat personality ptr @__gxx_personality_v0 {
6161
; CHECK: .cfi_endproc
6262
; NORMAL-NEXT: .section .gcc_except_table.zero,"aG",@progbits,zero{{$}}
6363
; SEP_BFD-NEXT: .section .gcc_except_table.zero,"aG",@progbits,zero{{$}}
64-
; SEP-NEXT: .section .gcc_except_table.zero,"aGo",@progbits,zero,zero{{$}}
64+
; SEP-NEXT: .section .gcc_except_table.zero,"aoG",@progbits,zero,zero{{$}}
6565
; SEP_NOUNIQUE-NEXT: .section .gcc_except_table,"aG",@progbits,zero{{$}}
6666
; NOUNIQUE-NEXT: .section .gcc_except_table,"aG",@progbits,zero{{$}}
6767
entry:

llvm/test/CodeGen/X86/patchable-function-entry.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ define void @f3() "patchable-function-entry"="3" comdat {
5050
; 32-NEXT: nop
5151
; 64: nopl (%rax)
5252
; CHECK: ret
53-
; CHECK: .section __patchable_function_entries,"aGwo",@progbits,f3,comdat,f3{{$}}
53+
; CHECK: .section __patchable_function_entries,"awoG",@progbits,f3,f3,comdat{{$}}
5454
; 32: .p2align 2
5555
; 32-NEXT: .long .Lfunc_begin3
5656
; 64: .p2align 3
@@ -66,7 +66,7 @@ define void @f5() "patchable-function-entry"="5" comdat {
6666
; 32-NEXT: nop
6767
; 64: nopl 8(%rax,%rax)
6868
; CHECK-NEXT: ret
69-
; CHECK: .section __patchable_function_entries,"aGwo",@progbits,f5,comdat,f5{{$}}
69+
; CHECK: .section __patchable_function_entries,"awoG",@progbits,f5,f5,comdat{{$}}
7070
; 32: .p2align 2
7171
; 32-NEXT: .long .Lfunc_begin4
7272
; 64: .p2align 3

llvm/test/CodeGen/X86/stack-size-section-function-sections.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
; Check we add .stack_size section to a COMDAT group with the corresponding .text section if such a COMDAT exists.
1717
; UNIQ: .section .text._Z4fooTIiET_v,"axG",@progbits,_Z4fooTIiET_v,comdat{{$}}
18-
; UNIQ: .section .stack_sizes,"Go",@progbits,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v{{$}}
18+
; UNIQ: .section .stack_sizes,"oG",@progbits,.text._Z4fooTIiET_v,_Z4fooTIiET_v,comdat{{$}}
1919
; NOUNIQ: .section .text,"axG",@progbits,_Z4fooTIiET_v,comdat,unique,3
20-
; NOUNIQ: .section .stack_sizes,"Go",@progbits,_Z4fooTIiET_v,comdat,.text,unique,3
20+
; NOUNIQ: .section .stack_sizes,"oG",@progbits,.text,_Z4fooTIiET_v,comdat,unique,3
2121

2222
$_Z4fooTIiET_v = comdat any
2323

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ define void @func2() #0 {
2929

3030
; Check that we still put .stack_sizes into the corresponding COMDAT group if any.
3131
; CHECK: .section .text._Z4fooTIiET_v,"axG",@progbits,_Z4fooTIiET_v,comdat
32-
; GROUPS: .section .stack_sizes,"Go",@progbits,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v{{$}}
32+
; GROUPS: .section .stack_sizes,"oG",@progbits,.text._Z4fooTIiET_v,_Z4fooTIiET_v,comdat{{$}}
3333
; NOGROUPS: .section .stack_sizes,"",@progbits
3434
$_Z4fooTIiET_v = comdat any
3535
define linkonce_odr dso_local i32 @_Z4fooTIiET_v() comdat {

llvm/test/CodeGen/X86/xray-section-group.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $bar = comdat any
1212
define i32 @bar() nounwind noinline uwtable "function-instrument"="xray-always" comdat($bar) {
1313
; CHECK: .section .text.bar,"axG",@progbits,bar,comdat
1414
ret i32 1
15-
; CHECK: .section xray_instr_map,"aGo",@progbits,bar,comdat,bar{{$}}
15+
; CHECK: .section xray_instr_map,"aoG",@progbits,bar,bar,comdat{{$}}
1616
}
1717

1818
; CHECK-OBJ: section xray_instr_map:

llvm/test/MC/ELF/section-combine.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ bar:
3939
.section .foo,"o",@progbits,bar,unique,1
4040
.byte 5
4141

42-
.section .foo,"Go",@progbits,comdat0,comdat,bar,unique,1
42+
.section .foo,"Go",@progbits,bar,comdat0,comdat,unique,1
4343
.byte 6
4444

45-
.section .foo,"Go",@progbits,comdat1,comdat,bar,unique,1
45+
.section .foo,"Go",@progbits,bar,comdat1,comdat,unique,1
4646
.byte 7
47-
.section .foo,"Go",@progbits,comdat1,comdat,bar,unique,1
47+
.section .foo,"oG",@progbits,bar,comdat1,comdat,unique,1
4848
.byte 8

llvm/test/MC/ELF/section.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ bar:
166166
.section .shf_metadata1,"ao",@progbits,.Lshf_metadata_target2_1
167167
.section .shf_metadata2,"ao",@progbits,.Lshf_metadata_target2_2
168168
.section .shf_metadata3,"ao",@progbits,.shf_metadata_target1
169+
.section .linkorder_group_zero,"aoG",@progbits,.shf_metadata_target1,foo
169170
// ASM: .section .shf_metadata1,"ao",@progbits,.Lshf_metadata_target2_1
170171
// ASM: .section .shf_metadata2,"ao",@progbits,.Lshf_metadata_target2_2
171172
// ASM: .section .shf_metadata3,"ao",@progbits,.shf_metadata_target1
173+
// ASM: .section .linkorder_group_zero,"aoG",@progbits,.shf_metadata_target1,foo{{$}}
172174

173175
// CHECK: Section {
174176
// CHECK: Index: 22
@@ -221,6 +223,20 @@ bar:
221223
// CHECK-NEXT: Link: 22
222224
// CHECK-NEXT: Info: 0
223225

226+
// CHECK: Section {
227+
// CHECK: Name: .linkorder_group_zero
228+
// CHECK-NEXT: Type: SHT_PROGBITS
229+
// CHECK-NEXT: Flags [
230+
// CHECK-NEXT: SHF_ALLOC
231+
// CHECK-NEXT: SHF_GROUP
232+
// CHECK-NEXT: SHF_LINK_ORDER
233+
// CHECK-NEXT: ]
234+
// CHECK-NEXT: Address:
235+
// CHECK-NEXT: Offset:
236+
// CHECK-NEXT: Size:
237+
// CHECK-NEXT: Link: 22
238+
// CHECK-NEXT: Info: 0
239+
224240
.section .text.foo
225241
// CHECK: Section {
226242
// CHECK: Name: .text.foo

0 commit comments

Comments
 (0)