[llvm-branch-commits] [ELF] Place .lbss/.lrodata/.ldata after .bss (PR #81224)

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Feb 9 13:58:43 PST 2024


================
@@ -1103,31 +1106,36 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
   }
 
   PhdrEntry *last = nullptr;
-  PhdrEntry *lastRO = nullptr;
-
+  OutputSection *lastRO = nullptr;
+  auto isLarge = [](OutputSection *osec) {
+    return config->emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
+  };
   for (Partition &part : partitions) {
     for (PhdrEntry *p : part.phdrs) {
       if (p->p_type != PT_LOAD)
         continue;
       last = p;
-      if (!(p->p_flags & PF_W))
-        lastRO = p;
+      if (!(p->p_flags & PF_W) && p->lastSec && !isLarge(p->lastSec))
+        lastRO = p->lastSec;
     }
   }
 
   if (lastRO) {
-    // _etext is the first location after the last read-only loadable segment.
+    // _etext is the first location after the last read-only loadable segment
+    // that does not contain large sections.
     if (ElfSym::etext1)
-      ElfSym::etext1->section = lastRO->lastSec;
+      ElfSym::etext1->section = lastRO;
     if (ElfSym::etext2)
-      ElfSym::etext2->section = lastRO->lastSec;
+      ElfSym::etext2->section = lastRO;
   }
 
   if (last) {
-    // _edata points to the end of the last mapped initialized section.
+    // _edata points to the end of the last mapped initialized section before
+    // the first regular NOBITS section (except .tbss and .relro_padding). In
----------------
MaskRay wrote:

Thanks for the catch. After the update the comment no longer makes sense. I should change it to
`// _edata points to the end of the last non-large mapped initialized section.`

I think the primary uses of `_edata` are for linker scripts where our logic here does not matter.
Some code uses something like `p < _edata` to check whether a pointer is guaranteed to be in the read-only segment. It's possible that such code may need updates in case `p` now appears in `.lrodata`. From my experience migrating gold to lld I've only run into one or two instances like this.

https://github.com/llvm/llvm-project/pull/81224


More information about the llvm-branch-commits mailing list