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

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 26 16:16:16 PDT 2025


================
@@ -103,13 +109,42 @@ 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)) {
+    StringRef secName = s->name;
+    if (trimSectionPrefix(v, secName)) {
+      if (!ctx.arg.zKeepDataSectionPrefix)
+        return v;
+      if (isSectionPrefix(".hot", secName))
+        return s->name.substr(0, v.size() + 4);
+      if (isSectionPrefix(".unlikely", secName))
+        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) {
+        // The reason to specialize this path is to spell out .rodata.hot and
----------------
MaskRay wrote:

The comment is too verbose. I would specify:

// Place input `.rodata.cst<N>.hot.` into `.rodata.hot`.

and delete the previous comment

>  // For .rodata,  a section could be`.rodata.cst<N>.hot.` for constant
> // pool or  `rodata.str<N>.hot.` for string literals.

The code block has fewer than 20 lines, making it easy for users to understand its semantics without adding comments that redundantly explain the code.

---

Now I am unsure why we want to support `.rodata.hot` while we don't support `.rodata.cst4.hot`.

(The `keep-text-data-sections` was added long ago when we supported both `.text.hot` and `.text.hot.` In clang, we now always emit `.text.hot.`)

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


More information about the llvm-commits mailing list