[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:05 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())))
----------------
MaskRay wrote:

If `StringRef name = s->name` at the loop body beginning and use `consume_front`, we can remove `substr(v.size())` here and below

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


More information about the llvm-commits mailing list