[PATCH] D148356: [lld/ELF] Sort sections by SHF_X86_64_LARGE

Arthur Eubanks via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 10:47:43 PDT 2023


aeubanks created this revision.
Herald added subscribers: hiraditya, arichardson, emaste.
Herald added a reviewer: MaskRay.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

So that binaries with both large and non-large sections will have the
non-large sections together, lowering the chance of relocation overflows
between the non-large sections.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148356

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/section-layout.s
  llvm/lib/MC/MCParser/ELFAsmParser.cpp


Index: llvm/lib/MC/MCParser/ELFAsmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/ELFAsmParser.cpp
+++ llvm/lib/MC/MCParser/ELFAsmParser.cpp
@@ -331,6 +331,9 @@
     case 'G':
       flags |= ELF::SHF_GROUP;
       break;
+    case 'l':
+      flags |= ELF::SHF_X86_64_LARGE;
+      break;
     case 'R':
       if (TT.isOSSolaris())
         flags |= ELF::SHF_SUNW_NODISCARD;
Index: lld/test/ELF/section-layout.s
===================================================================
--- lld/test/ELF/section-layout.s
+++ lld/test/ELF/section-layout.s
@@ -18,6 +18,14 @@
 .section n,"", at nobits
 .section m,""
 
+.section u,"axl", at nobits
+.section v,"axl"
+.section w,"awl", at nobits
+.section x,"awl"
+.section y1,"awxl", at nobits
+.section y2,"awxl"
+.section z1,"al", at nobits
+.section z2,"al"
 .section l,"awx", at nobits
 .section k,"awx"
 .section j,"aw", at nobits
@@ -50,6 +58,16 @@
 
 // CHECK: Name: j
 
+// Large sections come at the end
+// CHECK: Name: z1
+// CHECK: Name: z2
+// CHECK: Name: v
+// CHECK: Name: u
+// CHECK: Name: y2
+// CHECK: Name: y1
+// CHECK: Name: x
+// CHECK: Name: w
+
 // Non allocated sections are in input order.
 // CHECK: Name: t
 // CHECK: Name: s
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -826,13 +826,14 @@
 // * It is easy to check if a give branch was taken.
 // * It is easy two see how similar two ranks are (see getRankProximity).
 enum RankFlags {
-  RF_NOT_ADDR_SET = 1 << 27,
-  RF_NOT_ALLOC = 1 << 26,
-  RF_PARTITION = 1 << 18, // Partition number (8 bits)
-  RF_NOT_PART_EHDR = 1 << 17,
-  RF_NOT_PART_PHDR = 1 << 16,
-  RF_NOT_INTERP = 1 << 15,
-  RF_NOT_NOTE = 1 << 14,
+  RF_NOT_ADDR_SET = 1 << 28,
+  RF_NOT_ALLOC = 1 << 27,
+  RF_PARTITION = 1 << 19, // Partition number (8 bits)
+  RF_NOT_PART_EHDR = 1 << 18,
+  RF_NOT_PART_PHDR = 1 << 17,
+  RF_NOT_INTERP = 1 << 16,
+  RF_NOT_NOTE = 1 << 15,
+  RF_LARGE = 1 << 14,
   RF_WRITE = 1 << 13,
   RF_EXEC_WRITE = 1 << 12,
   RF_EXEC = 1 << 11,
@@ -939,6 +940,15 @@
 
   // Some architectures have additional ordering restrictions for sections
   // within the same PT_LOAD.
+
+  if (config->emachine == EM_X86_64) {
+    // Large sections should always come after non-large sections so that
+    // smaller relocations between non-large sections are less likely to
+    // overflow.
+    if (osec.flags & SHF_X86_64_LARGE)
+      rank |= RF_LARGE;
+  }
+
   if (config->emachine == EM_PPC64) {
     // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
     // that we would like to make sure appear is a specific order to maximize


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148356.513677.patch
Type: text/x-patch
Size: 2706 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230414/5ed71bee/attachment.bin>


More information about the llvm-commits mailing list