Skip to content

Commit 2865067

Browse files
MaskRayjustinfargnoli
authored andcommitted
[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 6d26606 commit 2865067

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

0 commit comments

Comments
 (0)