[lld] 0bc1009 - [lld-macho] Add support for -alias

Keith Smiley via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 19 13:57:08 PDT 2022


Author: Keith Smiley
Date: 2022-07-19T13:55:56-07:00
New Revision: 0bc100986c2502eee8839242fd45894dac67f441

URL: https://github.com/llvm/llvm-project/commit/0bc100986c2502eee8839242fd45894dac67f441
DIFF: https://github.com/llvm/llvm-project/commit/0bc100986c2502eee8839242fd45894dac67f441.diff

LOG: [lld-macho] Add support for -alias

This creates a symbol alias similar to --defsym in the elf linker. This
is used by swiftpm for all executables, so it's useful to support. This
doesn't implement -alias_list but that could be done pretty easily as
needed.

Differential Revision: https://reviews.llvm.org/D129938

Added: 
    lld/test/MachO/aliases.s

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Options.td
    lld/MachO/SymbolTable.cpp
    lld/MachO/SymbolTable.h

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index ccf71b6535ea..90fae41e42ae 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -188,6 +188,8 @@ struct Configuration {
   SymbolPatterns unexportedSymbols;
   SymbolPatterns whyLive;
 
+  std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols;
+
   SymtabPresence localSymbolsPresence = SymtabPresence::All;
   SymbolPatterns localSymbolPatterns;
 

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index abfe381f41e0..30b159725c49 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -1306,6 +1306,11 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
       OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
   config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order);
 
+  for (const Arg *arg : args.filtered(OPT_alias)) {
+    config->aliasedSymbols.push_back(
+        std::make_pair(arg->getValue(0), arg->getValue(1)));
+  }
+
   // FIXME: Add a commandline flag for this too.
   config->zeroModTime = getenv("ZERO_AR_DATE");
 
@@ -1558,6 +1563,18 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
     createSyntheticSections();
     createSyntheticSymbols();
 
+    for (const auto &pair : config->aliasedSymbols) {
+      if (const auto &sym = symtab->find(pair.first)) {
+        if (const auto &defined = dyn_cast<Defined>(sym)) {
+          symtab->aliasDefined(defined, pair.second);
+          continue;
+        }
+      }
+
+      warn("undefined base symbol '" + pair.first + "' for alias '" +
+           pair.second + "'\n");
+    }
+
     if (config->hasExplicitExports) {
       parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
         if (auto *defined = dyn_cast<Defined>(sym)) {

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 9b57f8a0bd49..b3d74a83f582 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -505,7 +505,6 @@ def reexported_symbols_list : Separate<["-"], "reexported_symbols_list">,
 def alias : MultiArg<["-"], "alias", 2>,
     MetaVarName<"<symbol_name> <alternate_name>">,
     HelpText<"Create a symbol alias with default global visibility">,
-    Flags<[HelpHidden]>,
     Group<grp_resolve>;
 def alias_list : Separate<["-"], "alias_list">,
     MetaVarName<"<file>">,

diff  --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp
index 7bda1d13069f..3667a7137291 100644
--- a/lld/MachO/SymbolTable.cpp
+++ b/lld/MachO/SymbolTable.cpp
@@ -117,6 +117,13 @@ Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
   return defined;
 }
 
+Defined *SymbolTable::aliasDefined(Defined *src, StringRef target) {
+  return addDefined(target, src->getFile(), src->isec, src->value, src->size,
+                    src->isWeakDef(), src->privateExtern, src->thumb,
+                    src->referencedDynamically, src->noDeadStrip,
+                    src->weakDefCanBeHidden);
+}
+
 Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
                                   bool isWeakRef) {
   Symbol *s;

diff  --git a/lld/MachO/SymbolTable.h b/lld/MachO/SymbolTable.h
index 0ecfa6dcd093..1b090105e0ca 100644
--- a/lld/MachO/SymbolTable.h
+++ b/lld/MachO/SymbolTable.h
@@ -43,6 +43,8 @@ class SymbolTable {
                       bool isReferencedDynamically, bool noDeadStrip,
                       bool isWeakDefCanBeHidden);
 
+  Defined *aliasDefined(Defined *src, StringRef target);
+
   Symbol *addUndefined(StringRef name, InputFile *, bool isWeakRef);
 
   Symbol *addCommon(StringRef name, InputFile *, uint64_t size, uint32_t align,

diff  --git a/lld/test/MachO/aliases.s b/lld/test/MachO/aliases.s
new file mode 100644
index 000000000000..dd366e78bc5f
--- /dev/null
+++ b/lld/test/MachO/aliases.s
@@ -0,0 +1,40 @@
+# REQUIRES: x86
+# RUN: rm -rf %t; split-file %s %t
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/foo.o %t/foo.s
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/main.o %t/main.s
+
+# RUN: %lld -alias _foo _bar -u _bar -exported_symbol _bar %t/main.o %t/foo.o -o %t/bar.o
+# RUN: llvm-nm %t/bar.o | FileCheck %s --check-prefix=BAR
+
+# BAR: [[#%x,FOO_ADDR:]] T _bar
+# BAR: [[#FOO_ADDR]]     t _foo
+
+# RUN: not %lld -alias _missing _bar -alias _missing2 _baz %t/main.o -o %t/missing.o %s 2>&1 | FileCheck %s --check-prefix=MISSING
+
+# MISSING-DAG: undefined base symbol '_missing' for alias '_bar'
+# MISSING-DAG: undefined base symbol '_missing2' for alias '_baz'
+
+# RUN: %lld -alias _foo _main %t/foo.o -o %t/main_rename.o
+# RUN: llvm-nm %t/main_rename.o | FileCheck %s --check-prefix=MAIN
+
+# MAIN: [[#%x,FOO_ADDR:]] T _foo
+# MAIN: [[#FOO_ADDR]]     T _main
+
+# RUN: %lld -alias _foo _bar -alias _main _fake_main %t/main.o %t/foo.o -o %t/multiple.o
+# RUN: llvm-nm %t/multiple.o | FileCheck %s --check-prefix=MULTIPLE
+
+# MULTIPLE: [[#%x,FOO_ADDR:]]  T _bar
+# MULTIPLE: [[#%x,MAIN_ADDR:]] T _fake_main
+# MULTIPLE: [[#FOO_ADDR]]      T _foo
+# MULTIPLE: [[#MAIN_ADDR]]     T _main
+
+#--- foo.s
+.globl _foo
+_foo:
+  ret
+
+#--- main.s
+.globl _main
+_main:
+  ret


        


More information about the llvm-commits mailing list