[lld] r292878 - Do not allocate space for common symbols with -r

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 19:41:21 PST 2017


Author: ruiu
Date: Mon Jan 23 21:41:20 2017
New Revision: 292878

URL: http://llvm.org/viewvc/llvm-project?rev=292878&view=rev
Log:
Do not allocate space for common symbols with -r

Currently ld.lld -r allocates space for common symbols, whereas ld.bfd
-r doesn't.  As a result the OpenBSD makefile bits for creating libraries
fail as they use ld -X -r to strip local symbols, which results in
duplicate symbol errors because space for the common symbols has been
allocated.

The diff also implements the --define-commons option such that allocation
of commons can be forced even if -r is used.

Patch by Mark Kettenis.

Added:
    lld/trunk/test/ELF/relocatable-common.s
Modified:
    lld/trunk/ELF/Config.h
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Options.td
    lld/trunk/ELF/Symbols.cpp
    lld/trunk/ELF/SyntheticSections.cpp

Modified: lld/trunk/ELF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=292878&r1=292877&r2=292878&view=diff
==============================================================================
--- lld/trunk/ELF/Config.h (original)
+++ lld/trunk/ELF/Config.h Mon Jan 23 21:41:20 2017
@@ -99,6 +99,7 @@ struct Configuration {
   bool Bsymbolic;
   bool BsymbolicFunctions;
   bool ColorDiagnostics = false;
+  bool DefineCommon;
   bool Demangle = true;
   bool DisableVerify;
   bool EhFrameHdr;

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=292878&r1=292877&r2=292878&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Mon Jan 23 21:41:20 2017
@@ -498,6 +498,8 @@ void LinkerDriver::readConfigs(opt::Inpu
   Config->Pie = getArg(Args, OPT_pie, OPT_nopie, false);
   Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
   Config->Relocatable = Args.hasArg(OPT_relocatable);
+  Config->DefineCommon = getArg(Args, OPT_define_common, OPT_no_define_common,
+                                !Config->Relocatable);
   Config->Discard = getDiscardOption(Args);
   Config->SaveTemps = Args.hasArg(OPT_save_temps);
   Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);

Modified: lld/trunk/ELF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=292878&r1=292877&r2=292878&view=diff
==============================================================================
--- lld/trunk/ELF/Options.td (original)
+++ lld/trunk/ELF/Options.td Mon Jan 23 21:41:20 2017
@@ -45,6 +45,9 @@ def color_diagnostics: F<"color-diagnost
 def color_diagnostics_eq: J<"color-diagnostics=">,
   HelpText<"Use colors in diagnostics">;
 
+def define_common: F<"define-common">,
+  HelpText<"Assign space to common symbols">;
+
 def demangle: F<"demangle">, HelpText<"Demangle symbol names">;
 
 def disable_new_dtags: F<"disable-new-dtags">,
@@ -134,6 +137,9 @@ def no_as_needed: F<"no-as-needed">,
 def no_color_diagnostics: F<"no-color-diagnostics">,
   HelpText<"Do not use colors in diagnostics">;
 
+def no_define_common: F<"no-define-common">,
+  HelpText<"Do not assign space to common symbols">;
+
 def no_demangle: F<"no-demangle">,
   HelpText<"Do not demangle symbol names">;
 
@@ -265,6 +271,9 @@ def alias_Bstatic_dn: F<"dn">, Alias<Bst
 def alias_Bstatic_non_shared: F<"non_shared">, Alias<Bstatic>;
 def alias_Bstatic_static: F<"static">, Alias<Bstatic>;
 def alias_L__library_path: J<"library-path=">, Alias<L>;
+def alias_define_common_d: Flag<["-"], "d">, Alias<define_common>;
+def alias_define_common_dc: F<"dc">, Alias<define_common>;
+def alias_define_common_dp: F<"dp">, Alias<define_common>;
 def alias_discard_all_x: Flag<["-"], "x">, Alias<discard_all>;
 def alias_discard_locals_X: Flag<["-"], "X">, Alias<discard_locals>;
 def alias_dynamic_list: J<"dynamic-list=">, Alias<dynamic_list>;
@@ -332,7 +341,6 @@ def plugin_opt_eq: J<"plugin-opt=">;
 // Options listed below are silently ignored for now for compatibility.
 def allow_shlib_undefined: F<"allow-shlib-undefined">;
 def cref: Flag<["--"], "cref">;
-def define_common: F<"define-common">;
 def detect_odr_violations: F<"detect-odr-violations">;
 def g: Flag<["-"], "g">;
 def no_add_needed: F<"no-add-needed">;
@@ -355,9 +363,6 @@ def G: JoinedOrSeparate<["-"], "G">;
 def Qy : F<"Qy">;
 
 // Aliases for ignored options
-def alias_define_common_d: Flag<["-"], "d">, Alias<define_common>;
-def alias_define_common_dc: F<"dc">, Alias<define_common>;
-def alias_define_common_dp: F<"dp">, Alias<define_common>;
 def alias_version_script_version_script: J<"version-script=">,
   Alias<version_script>;
 

Modified: lld/trunk/ELF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=292878&r1=292877&r2=292878&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.cpp (original)
+++ lld/trunk/ELF/Symbols.cpp Mon Jan 23 21:41:20 2017
@@ -73,6 +73,8 @@ static typename ELFT::uint getSymVA(cons
     return VA;
   }
   case SymbolBody::DefinedCommonKind:
+    if (!Config->DefineCommon)
+      return 0;
     return In<ELFT>::Common->OutSec->Addr + In<ELFT>::Common->OutSecOff +
            cast<DefinedCommon>(Body).Offset;
   case SymbolBody::SharedKind: {

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=292878&r1=292877&r2=292878&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Mon Jan 23 21:41:20 2017
@@ -59,6 +59,9 @@ template <class ELFT> InputSection<ELFT>
                                        ArrayRef<uint8_t>(), "COMMON");
   Ret->Live = true;
 
+  if (!Config->DefineCommon)
+    return Ret;
+
   // Sort the common symbols by alignment as an heuristic to pack them better.
   std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
   std::stable_sort(Syms.begin(), Syms.end(),
@@ -1164,10 +1167,14 @@ void SymbolTableSection<ELFT>::writeGlob
     ESym->setVisibility(Body->symbol()->Visibility);
     ESym->st_value = Body->getVA<ELFT>();
 
-    if (const OutputSectionBase *OutSec = getOutputSection(Body))
+    if (const OutputSectionBase *OutSec = getOutputSection(Body)) {
       ESym->st_shndx = OutSec->SectionIndex;
-    else if (isa<DefinedRegular<ELFT>>(Body))
+    } else if (isa<DefinedRegular<ELFT>>(Body)) {
       ESym->st_shndx = SHN_ABS;
+    } else if (isa<DefinedCommon>(Body)) {
+      ESym->st_shndx = SHN_COMMON;
+      ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
+    }
 
     if (Config->EMachine == EM_MIPS) {
       // On MIPS we need to mark symbol which has a PLT entry and requires
@@ -1199,6 +1206,8 @@ SymbolTableSection<ELFT>::getOutputSecti
     break;
   }
   case SymbolBody::DefinedCommonKind:
+    if (!Config->DefineCommon)
+      return nullptr;
     return In<ELFT>::Common->OutSec;
   case SymbolBody::SharedKind: {
     auto &SS = cast<SharedSymbol<ELFT>>(*Sym);

Added: lld/trunk/test/ELF/relocatable-common.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/relocatable-common.s?rev=292878&view=auto
==============================================================================
--- lld/trunk/test/ELF/relocatable-common.s (added)
+++ lld/trunk/test/ELF/relocatable-common.s Mon Jan 23 21:41:20 2017
@@ -0,0 +1,36 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: ld.lld -r %t1.o -o %t
+# RUN: llvm-readobj -symbols -r %t | FileCheck %s
+# RUN: ld.lld -r --no-define-common %t1.o -o %t
+# RUN: llvm-readobj -symbols -r %t | FileCheck %s
+# RUN: ld.lld -r --define-common %t1.o -o %t
+# RUN: llvm-readobj -symbols -r %t | FileCheck -check-prefix=DEFCOMM %s
+# RUN: ld.lld -r -d %t1.o -o %t
+# RUN: llvm-readobj -symbols -r %t | FileCheck -check-prefix=DEFCOMM %s
+# RUN: ld.lld -r -dc %t1.o -o %t
+# RUN: llvm-readobj -symbols -r %t | FileCheck -check-prefix=DEFCOMM %s
+# RUN: ld.lld -r -dp %t1.o -o %t
+# RUN: llvm-readobj -symbols -r %t | FileCheck -check-prefix=DEFCOMM %s
+
+# CHECK:        Symbol {
+# CHECK:          Name: common (1)
+# CHECK-NEXT:     Value: 0x4
+# CHECK-NEXT:     Size: 4
+# CHECK-NEXT:     Binding: Global (0x1)
+# CHECK-NEXT:     Type: Object (0x1)
+# CHECK-NEXT:     Other: 0
+# CHECK-NEXT:     Section: Common (0xFFF2)
+# CHECK-NEXT:   }
+
+# DEFCOMM:      Symbol {
+# DEFCOMM:        Name: common (1)
+# DEFCOMM-NEXT:   Value: 0x0
+# DEFCOMM-NEXT:   Size: 4
+# DEFCOMM-NEXT:   Binding: Global (0x1)
+# DEFCOMM-NEXT:   Type: Object (0x1)
+# DEFCOMM-NEXT:   Other: 0
+# DEFCOMM-NEXT:   Section: COMMON (0x2)
+# DEFCOMM-NEXT: }
+
+.comm common,4,4




More information about the llvm-commits mailing list