[PATCH] D116528: [ELF] Handle .init_array prefix consistently

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 3 02:26:56 PST 2022


nikic created this revision.
nikic added a reviewer: MaskRay.
Herald added subscribers: pengfei, JDevlieghere, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently, the code in TargetLoweringObjectFile only assign `@init_array` section type to plain `.init_array` sections, but not prioritized sections like `.init_array.00001`.

This is inconsistent with the interpretation in the AsmParser (see https://github.com/llvm/llvm-project/blob/791523bae6153b13bb41ba05c9fc89e502cc4a1a/llvm/lib/MC/MCParser/ELFAsmParser.cpp#L621-L632) and upcoming expectations in LLD (see https://github.com/rust-lang/rust/issues/92181 for context).

This patch assigns `@init_array` section to all sections with an `.init_array` prefix. The same is done for `.fini_array` and `.preinit_array` as well. With that, the logic matches the AsmParser.


https://reviews.llvm.org/D116528

Files:
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/test/CodeGen/X86/attribute-sections.ll


Index: llvm/test/CodeGen/X86/attribute-sections.ll
===================================================================
--- llvm/test/CodeGen/X86/attribute-sections.ll
+++ llvm/test/CodeGen/X86/attribute-sections.ll
@@ -1,18 +1,36 @@
-; RUN: llc < %s -mtriple=i386-unknown-linux-gnu | FileCheck %s -check-prefix=LINUX
+; RUN: llc < %s -mtriple=i386-unknown-linux-gnu | FileCheck %s
 
 declare i32 @foo()
- at G0 = global i32 ()* @foo, section ".init_array"
 
-; LINUX:  .section  .init_array,"aw"
-; LINUX:  .globl G0
+ at init_array1 = global i32 ()* @foo, section ".init_array"
+ at init_array2 = global i32 ()* @foo, section ".init_array.00001"
+ at init_array3 = global i32 ()* @foo, section ".init_arrayfoo"
 
- at G1 = global i32 ()* @foo, section ".fini_array"
+; CHECK:  .section  .init_array,"aw", at init_array
+; CHECK:  .globl init_array1
+; CHECK:  .section  .init_array.00001,"aw", at init_array
+; CHECK:  .globl init_array2
+; CHECK:  .section  .init_arrayfoo,"aw", at progbits
+; CHECK:  .globl init_array3
 
-; LINUX:  .section  .fini_array,"aw"
-; LINUX:  .globl G1
+ at fini_array1 = global i32 ()* @foo, section ".fini_array"
+ at fini_array2 = global i32 ()* @foo, section ".fini_array.00001"
+ at fini_array3 = global i32 ()* @foo, section ".fini_arrayfoo"
 
- at G2 = global i32 ()* @foo, section ".preinit_array"
+; CHECK:  .section  .fini_array,"aw", at fini_array
+; CHECK:  .globl fini_array1
+; CHECK:  .section  .fini_array.00001,"aw", at fini_array
+; CHECK:  .globl fini_array2
+; CHECK:  .section  .fini_arrayfoo,"aw", at progbits
+; CHECK:  .globl fini_array3
 
-; LINUX:  .section .preinit_array,"aw"
-; LINUX:  .globl G2
+ at preinit_array1 = global i32 ()* @foo, section ".preinit_array"
+ at preinit_array2 = global i32 ()* @foo, section ".preinit_array.00001"
+ at preinit_array3 = global i32 ()* @foo, section ".preinit_arrayfoo"
 
+; CHECK:  .section  .preinit_array,"aw", at preinit_array
+; CHECK:  .globl preinit_array1
+; CHECK:  .section  .preinit_array.00001,"aw", at preinit_array
+; CHECK:  .globl preinit_array2
+; CHECK:  .section  .preinit_arrayfoo,"aw", at progbits
+; CHECK:  .globl preinit_array3
Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -477,6 +477,11 @@
   return K;
 }
 
+static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
+  return SectionName.consume_front(Prefix) &&
+         (SectionName.empty() || SectionName[0] == '.');
+}
+
 static unsigned getELFSectionType(StringRef Name, SectionKind K) {
   // Use SHT_NOTE for section whose name starts with ".note" to allow
   // emitting ELF notes from C variable declaration.
@@ -484,13 +489,13 @@
   if (Name.startswith(".note"))
     return ELF::SHT_NOTE;
 
-  if (Name == ".init_array")
+  if (hasPrefix(Name, ".init_array"))
     return ELF::SHT_INIT_ARRAY;
 
-  if (Name == ".fini_array")
+  if (hasPrefix(Name, ".fini_array"))
     return ELF::SHT_FINI_ARRAY;
 
-  if (Name == ".preinit_array")
+  if (hasPrefix(Name, ".preinit_array"))
     return ELF::SHT_PREINIT_ARRAY;
 
   if (K.isBSS() || K.isThreadBSS())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116528.397023.patch
Type: text/x-patch
Size: 3171 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220103/64a29234/attachment.bin>


More information about the llvm-commits mailing list