[PATCH] D43008: [ELF] Process linker scripts deeper when declaring symbols.

Igor Kudrin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 02:24:33 PST 2018


ikudrin created this revision.
ikudrin added reviewers: grimar, ruiu, rafael.
ikudrin added a project: lld.
Herald added a subscriber: emaste.

This is a follow-up to https://reviews.llvm.org/D41987.

We should process symbols inside output section declarations the same way as top-level ones.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D43008

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/symbols-synthetic.s
  test/ELF/linkerscript/version-script.s


Index: test/ELF/linkerscript/version-script.s
===================================================================
--- test/ELF/linkerscript/version-script.s
+++ test/ELF/linkerscript/version-script.s
@@ -5,6 +5,10 @@
 # RUN: ld.lld -T %t.script -shared --no-undefined-version %t.o -o %t.so
 # RUN: llvm-readobj -V %t.so | FileCheck %s
 
+# RUN: echo "SECTIONS { .text : { bar = foo; *(.text) } } VERSION { V { global: foo; bar; local: *; }; }" > %t.script
+# RUN: ld.lld -T %t.script -shared --no-undefined-version %t.o -o %t.so
+# RUN: llvm-readobj -V %t.so | FileCheck %s
+
 ## Check that we are able to version symbols defined in script.
 # CHECK:      Symbols [
 # CHECK-NEXT:   Symbol {
Index: test/ELF/linkerscript/symbols-synthetic.s
===================================================================
--- test/ELF/linkerscript/symbols-synthetic.s
+++ test/ELF/linkerscript/symbols-synthetic.s
@@ -61,14 +61,14 @@
 # SIMPLE-NEXT: 0000000000000120         .foo    00000000 _begin_sec
 # SIMPLE-NEXT: 0000000000000128         *ABS*   00000000 _end_sec_abs
 # SIMPLE-NEXT: 0000000000001048         .text   00000000 _start
-# SIMPLE-NEXT: 0000000000000ee4         *ABS*   00000000 size_foo_3
 # SIMPLE-NEXT: 0000000000000120         .foo    00000000 begin_foo
 # SIMPLE-NEXT: 0000000000000128         .foo    00000000 end_foo
 # SIMPLE-NEXT: 0000000000000008         *ABS*   00000000 size_foo_1
 # SIMPLE-NEXT: 0000000000000008         *ABS*   00000000 size_foo_1_abs
 # SIMPLE-NEXT: 0000000000001000         .foo    00000000 begin_bar
 # SIMPLE-NEXT: 0000000000001004         .foo    00000000 end_bar
 # SIMPLE-NEXT: 0000000000000ee4         *ABS*   00000000 size_foo_2
+# SIMPLE-NEXT: 0000000000000ee4         *ABS*   00000000 size_foo_3
 # SIMPLE-NEXT: 0000000000001004         .eh_frame_hdr     00000000 __eh_frame_hdr_start
 # SIMPLE-NEXT: 0000000000001010         *ABS*             00000000 __eh_frame_hdr_start2
 # SIMPLE-NEXT: 0000000000001018         .eh_frame_hdr     00000000 __eh_frame_hdr_end
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -170,10 +170,10 @@
 // over symbol assignment commands and create placeholder symbols if needed.
 void LinkerScript::declareSymbols() {
   assert(!Ctx);
-  for (BaseCommand *Base : SectionCommands) {
+  auto Fn = [](BaseCommand *Base) {
     auto *Cmd = dyn_cast<SymbolAssignment>(Base);
     if (!Cmd || !shouldDefineSym(Cmd))
-      continue;
+      return;
 
     // We can't calculate final value right now.
     Symbol *Sym;
@@ -186,6 +186,22 @@
                            STT_NOTYPE, 0, 0, nullptr);
     Cmd->Sym = cast<Defined>(Sym);
     Cmd->Provide = false;
+  };
+
+  for (BaseCommand *Base : SectionCommands) {
+    auto *Sec = dyn_cast<OutputSection>(Base);
+    if (!Sec) {
+      Fn(Base);
+      continue;
+    }
+    // If the output section directive has constraints,
+    // we can't say for sure whether it will be included or not.
+    // Skip such sections for now. Improve the checks if we ever
+    // need symbols from that sections to be declared early.
+    if (Sec->Constraint != ConstraintKind::NoConstraint)
+      continue;
+    for (BaseCommand *Base2 : Sec->SectionCommands)
+      Fn(Base2);
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43008.133176.patch
Type: text/x-patch
Size: 3296 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180207/188b37e6/attachment.bin>


More information about the llvm-commits mailing list