[PATCH] D35910: Make __start_sec __end_sec handling more precise

Rafael Ávila de Espíndola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 26 13:14:41 PDT 2017


rafael created this revision.
Herald added a subscriber: emaste.

With this we only ask LTO to keep a C named section if there is a __start_ or __end symbol.

This is not as strict as lld's --gc-sections, but is as good as we can get without having a far more detailed ir summary.


https://reviews.llvm.org/D35910

Files:
  ELF/LTO.cpp
  test/ELF/lto/section-name.ll


Index: test/ELF/lto/section-name.ll
===================================================================
--- test/ELF/lto/section-name.ll
+++ test/ELF/lto/section-name.ll
@@ -7,5 +7,14 @@
 target triple = "x86_64-unknown-linux-gnu"
 
 @foo = hidden global i32 42, section "foo_section"
+ at bar = hidden global i32 42, section "bar_section"
 
-; CHECK: foo_section PROGBITS
+ at __start_foo_section = external global i32
+
+define i32* @use() {
+  ret i32* @__start_foo_section
+}
+
+; CHECK-NOT: bar_section
+; CHECK:     foo_section PROGBITS
+; CHECK-NOT: bar_section
Index: ELF/LTO.cpp
===================================================================
--- ELF/LTO.cpp
+++ ELF/LTO.cpp
@@ -11,6 +11,7 @@
 #include "Config.h"
 #include "Error.h"
 #include "InputFiles.h"
+#include "SymbolTable.h"
 #include "Symbols.h"
 #include "lld/Core/TargetOptionsCommandFlags.h"
 #include "llvm/ADT/STLExtras.h"
@@ -122,6 +123,14 @@
   std::vector<Symbol *> Syms = F.getSymbols();
   std::vector<lto::SymbolResolution> Resols(Syms.size());
 
+  DenseSet<StringRef> UsedStartStop;
+  for (Symbol *Sym : Symtab->getSymbols()) {
+    StringRef Name = Sym->body()->getName();
+    for (StringRef Prefix : {"__start_", "__end_"})
+      if (Name.startswith(Prefix))
+        UsedStartStop.insert(Name.substr(Prefix.size()));
+  }
+
   // Provide a resolution to the LTO API for each symbol.
   for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
     Symbol *Sym = Syms[SymNum];
@@ -138,7 +147,7 @@
 
     R.VisibleToRegularObj = Sym->IsUsedInRegularObj ||
                             (R.Prevailing && Sym->includeInDynsym()) ||
-                            isValidCIdentifier(ObjSym.getSectionName());
+                            UsedStartStop.count(ObjSym.getSectionName());
     if (R.Prevailing)
       undefine(Sym);
     R.LinkerRedefined = Config->RenamedSymbols.count(Sym);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35910.108348.patch
Type: text/x-patch
Size: 1872 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170726/9cbb1f24/attachment.bin>


More information about the llvm-commits mailing list