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

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 14:25:23 PDT 2025


================
@@ -103,13 +103,48 @@ 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 (!secName.consume_front(v))
----------------
mingmingl-llvm wrote:

The two ifs before `if (!ctx.arg.zKeepDataSectionPrefix)` applies on the general cases, and there may not be a trailing dot to data sections. For instance, https://gcc.godbolt.org/z/G77P8n6qf compiles a simple source code with `-O2 -ffunction-sections -fdata-sections`, and var section name is `.section        .bss.var` (without trailing dot). 

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


More information about the llvm-commits mailing list