[lld] [lld][ELF] Introduce an option to keep data section prefix. (PR #148985)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 21:59:06 PDT 2025


================
@@ -105,13 +110,41 @@ StringRef LinkerScript::getOutputSectionName(const InputSectionBase *s) const {
     return ".text";
   }
 
-  for (StringRef v : {".data.rel.ro", ".data",       ".rodata",
-                      ".bss.rel.ro",  ".bss",        ".ldata",
-                      ".lrodata",     ".lbss",       ".gcc_except_table",
-                      ".init_array",  ".fini_array", ".tbss",
-                      ".tdata",       ".ARM.exidx",  ".ARM.extab",
-                      ".ctors",       ".dtors",      ".sbss",
-                      ".sdata",       ".srodata"})
+  // When zKeepDataSectionPrefix is true, keep .hot and .unlikely suffixes
+  // in data sections.
+  static constexpr StringRef dataSectionPrefixes[] = {
+      ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
+  };
+
+  for (auto [index, v] : llvm::enumerate(dataSectionPrefixes)) {
+    if (isSectionPrefix(v, s->name)) {
+      // The .bss.rel.ro section is considered rarely accessed. So this section
+      // is not partitioned and zKeepDataSectionPrefix is not applied to
+      // make the executable simpler with fewer elf sections.
+      if (ctx.arg.zKeepDataSectionPrefix && index != 3) {
+        if (isSectionPrefix(".hot", s->name.substr(v.size())))
+          return s->name.substr(0, v.size() + 4);
+        if (isSectionPrefix(".unlikely", s->name.substr(v.size())))
+          return s->name.substr(0, v.size() + 9);
+        // For .rodata,  a section could be`.rodata.cst<N>.hot.` for constant
+        // pool or  `rodata.str<N>.hot.` for string literals.
+        if (index == 2) {
+          if (isSectionSuffix(".hot", s->name)) {
----------------
MaskRay wrote:

`isSectionSuffix` supports both `.rodata.hot` and `.rodata.hot.`. I don't think we want to support `.rodata.hot` (which can be used by a function named `hot`). Supporting just `.rodata.hot.`

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


More information about the llvm-commits mailing list