[llvm-branch-commits] [lld] 440b16a - [lld-macho] Fill out release notes for 12.x

2021-02-17 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-02-17T18:58:16-05:00
New Revision: 440b16a4fc04a4bb614a60c26f42ab2ec27049a4

URL: 
https://github.com/llvm/llvm-project/commit/440b16a4fc04a4bb614a60c26f42ab2ec27049a4
DIFF: 
https://github.com/llvm/llvm-project/commit/440b16a4fc04a4bb614a60c26f42ab2ec27049a4.diff

LOG: [lld-macho] Fill out release notes for 12.x

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

Added: 


Modified: 
lld/docs/ReleaseNotes.rst

Removed: 




diff  --git a/lld/docs/ReleaseNotes.rst b/lld/docs/ReleaseNotes.rst
index ea1403888eba..7c1cbc4a4c4b 100644
--- a/lld/docs/ReleaseNotes.rst
+++ b/lld/docs/ReleaseNotes.rst
@@ -58,10 +58,26 @@ MinGW Improvements
   (`D93950 `_)
 
 
-MachO Improvements
+Mach-O Improvements
 --
 
-* Item 1.
+We've gotten the new implementation of LLD for Mach-O to the point where it is
+able to link large x86_64 programs, and we'd love to get some alpha testing on
+it. The new Darwin back-end can be invoked as follows:
+
+.. code-block::
+   clang -fuse-ld=lld.darwinnew /path/to/file.c
+
+To reach this point, we implemented numerous features, and it's easier to list
+the major features we *haven't* yet completed:
+
+* LTO support
+* Stack unwinding for exceptions
+* Support for arm64, arm, and i386 architectures
+
+If you stumble upon an issue and it doesn't fall into one of these categories,
+please file a bug report!
+
 
 WebAssembly Improvements
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 1bbe8ef - [lld-macho] Fill out release notes for 13.x

2021-08-16 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-08-16T13:19:00-04:00
New Revision: 1bbe8ef81549b37d2c74cfb607cffe0e01ab4dd3

URL: 
https://github.com/llvm/llvm-project/commit/1bbe8ef81549b37d2c74cfb607cffe0e01ab4dd3
DIFF: 
https://github.com/llvm/llvm-project/commit/1bbe8ef81549b37d2c74cfb607cffe0e01ab4dd3.diff

LOG: [lld-macho] Fill out release notes for 13.x

I probably missed out some things, given how much work was done in the
last few months...

Reviewed By: #lld-macho, oontvoo

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

Added: 


Modified: 
lld/docs/ReleaseNotes.rst

Removed: 




diff  --git a/lld/docs/ReleaseNotes.rst b/lld/docs/ReleaseNotes.rst
index bf1c36f38f14..9ae375523518 100644
--- a/lld/docs/ReleaseNotes.rst
+++ b/lld/docs/ReleaseNotes.rst
@@ -66,10 +66,51 @@ MinGW Improvements
   (`D107237 `_ and
   `D107253 `_)
 
-MachO Improvements
---
-
-* Item 1.
+Mach-O Improvements
+---
+
+The Mach-O backend is now able to link several large, real-world programs,
+though we are still working out the kinks.
+
+* arm64 is now supported as a target. (`D88629 
`_)
+* arm64_32 is now supported as a target. (`D99822 
`_)
+* Branch-range-extension thunks are now supported. (`D100818 
`_)
+* ``-dead_strip`` is now supported. (`D103324 
`_)
+* Support for identical code folding (``--icf=all``) has been added.
+  (`D103292 `_)
+* Support for special ``$start`` and ``$end`` symbols for segment & sections 
has been
+  added. (`D106767 `_, `D106629 
`_)
+* ``$ld$previous`` symbols are now supported. (`D103505 
`_)
+* ``$ld$install_name`` symbols are now supported. (`D103746 
`_)
+* ``__mh_*_header`` symbols are now supported. (`D97007 
`_)
+* LC_CODE_SIGNATURE is now supported. (`D96164 
`_)
+* LC_FUNCTION_STARTS is now supported. (`D97260 
`_)
+* LC_DATA_IN_CODE is now supported. (`D103006 
`_)
+* Bind opcodes are more compactly encoded. (`D106128 
`_,
+  `D105075 `_)
+* LTO cache support has been added. (`D105922 
`_)
+* ``-application_extension`` is now supported. (`D105818 
`_)
+* ``-export_dynamic`` is now partially supported. (`D105482 
`_)
+* ``-arch_multiple`` is now supported. (`D105450 
`_)
+* ``-final_output`` is now supported. (`D105449 
`_)
+* ``-umbrella`` is now supported. (`D105448 
`_)
+* ``--print-dylib-search`` is now supported. (`D103985 
`_)
+* ``-force_load_swift_libs`` is now supported. (`D103709 
`_)
+* ``-reexport_framework``, ``-reexport_library``, ``-reexport-l`` are now 
supported.
+  (`D103497 `_)
+* ``.weak_def_can_be_hidden`` is now supported. (`D101080 
`_)
+* ``-add_ast_path`` is now supported. (`D100076 
`_)
+* ``-segprot`` is now supported.  (`D99389 `_)
+* ``-dependency_info`` is now partially supported. (`D98559 
`_)
+* ``--time-trace`` is now supported. (`D98419 
`_)
+* ``-mark_dead_strippable_dylib`` is now supported. (`D98262 
`_)
+* ``-[un]exported_symbol[s_list]`` is now supported. (`D98223 
`_)
+* ``-flat_namespace`` is now supported. (`D97641 
`_)
+* ``-rename_section`` and ``-rename_segment`` are now supported. (`D97600 
`_)
+* ``-bundle_loader`` is now supported. (`D95913 
`_)
+* ``-map`` is now partially supported. (`D98323 
`_)
+
+There were numerous other bug-fixes as well.
 
 WebAssembly Improvements
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] b3e73dc - [lld-macho][easy] Create group for LLD-specific CLI flags

2021-01-20 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-01-20T14:21:31-05:00
New Revision: b3e73dc5af6b4d1438ea401a7ab60bfe298a53c6

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

LOG: [lld-macho][easy] Create group for LLD-specific CLI flags

Reviewed By: #lld-macho, compnerd

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

Added: 


Modified: 
lld/MachO/Options.td

Removed: 




diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 2ea4c53e0166..af497884eb9e 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -3,23 +3,32 @@ include "llvm/Option/OptParser.td"
 // Flags that lld/MachO understands but ld64 doesn't. These take
 // '--' instead of '-' and use dashes instead of underscores, so
 // they don't collide with the ld64 compat options.
+def grp_lld : OptionGroup<"kind">, HelpText<"LLD-SPECIFIC">;
 
-def help : Flag<["-", "--"], "help">;
+def help : Flag<["-", "--"], "help">,
+Group;
 def help_hidden : Flag<["--"], "help-hidden">,
-  HelpText<"Display help for hidden options">;
+HelpText<"Display help for hidden options">,
+Group;
 def color_diagnostics: Flag<["--"], "color-diagnostics">,
-  HelpText<"Alias for --color-diagnostics=always">;
+HelpText<"Alias for --color-diagnostics=always">,
+Group;
 def no_color_diagnostics: Flag<["--"], "no-color-diagnostics">,
-  HelpText<"Alias for --color-diagnostics=never">;
+HelpText<"Alias for --color-diagnostics=never">,
+Group;
 def color_diagnostics_eq: Joined<["--"], "color-diagnostics=">,
-  HelpText<"Use colors in diagnostics (default: auto)">,
-  MetaVarName<"[auto,always,never]">;
-def reproduce: Separate<["--"], "reproduce">;
+HelpText<"Use colors in diagnostics (default: auto)">,
+MetaVarName<"[auto,always,never]">,
+Group;
+def reproduce: Separate<["--"], "reproduce">,
+Group;
 def reproduce_eq: Joined<["--"], "reproduce=">,
 Alias(reproduce)>,
-HelpText<"Write tar file containing inputs and command to reproduce link">;
+HelpText<"Write tar file containing inputs and command to reproduce link">,
+Group;
 def version: Flag<["--"], "version">,
-HelpText<"Display the version number and exit">;
+HelpText<"Display the version number and exit">,
+Group;
 
 
 // This is a complete Options.td compiled from Apple's ld(1) manpage



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 697f4e4 - [lld-macho] Run ObjCContractPass during LTO

2021-01-20 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-01-20T14:21:32-05:00
New Revision: 697f4e429b900d2d3d8a03713c7d6cd562a5bd35

URL: 
https://github.com/llvm/llvm-project/commit/697f4e429b900d2d3d8a03713c7d6cd562a5bd35
DIFF: 
https://github.com/llvm/llvm-project/commit/697f4e429b900d2d3d8a03713c7d6cd562a5bd35.diff

LOG: [lld-macho] Run ObjCContractPass during LTO

Run the ObjCARCContractPass during LTO. The legacy LTO backend (under
LTO/ThinLTOCodeGenerator.cpp) already does this; this diff just adds that
behavior to the new LTO backend. Without that pass, the objc.clang.arc.use
intrinsic will get passed to the instruction selector, which doesn't know how to
handle it.

In order to test both the new and old pass managers, I've also added support for
the `--[no-]lto-legacy-pass-manager` flags.

P.S. Not sure if the ordering of the pass within the pipeline matters...

Reviewed By: fhahn

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

Added: 
lld/test/MachO/objc-arc-contract.ll

Modified: 
lld/MachO/Config.h
lld/MachO/Driver.cpp
lld/MachO/LTO.cpp
lld/MachO/Options.td
llvm/include/llvm/LTO/Config.h
llvm/lib/LTO/LTOBackend.cpp

Removed: 




diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index 4f27ec2db45f..f6e1f134d974 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -47,6 +47,7 @@ struct Configuration {
   bool implicitDylibs = false;
   bool isPic = false;
   bool headerPadMaxInstallNames = false;
+  bool ltoNewPassManager = LLVM_ENABLE_NEW_PASS_MANAGER;
   bool printEachFile = false;
   bool printWhyLoad = false;
   bool searchDylibsFirst = false;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 857c9991a8e6..1b337f38f7ba 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -733,6 +733,9 @@ bool macho::link(ArrayRef argsArr, bool 
canExitEarly,
   config->printWhyLoad = args.hasArg(OPT_why_load);
   config->outputType = getOutputType(args);
   config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
+  config->ltoNewPassManager =
+  args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager,
+   LLVM_ENABLE_NEW_PASS_MANAGER);
   config->runtimePaths = args::getStrings(args, OPT_rpath);
   config->allLoad = args.hasArg(OPT_all_load);
   config->forceLoadObjC = args.hasArg(OPT_ObjC);

diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index 7554693f15e4..f48bc24df3d7 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -18,6 +18,7 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/ObjCARC.h"
 
 using namespace lld;
 using namespace lld::macho;
@@ -30,6 +31,10 @@ static lto::Config createConfig() {
   c.CodeModel = getCodeModelFromCMModel();
   c.CPU = getCPUStr();
   c.MAttrs = getMAttrs();
+  c.UseNewPM = config->ltoNewPassManager;
+  c.PreCodeGenPassesHook = [](legacy::PassManager &pm) {
+pm.add(createObjCARCContractPass());
+  };
   return c;
 }
 

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index af497884eb9e..89473acebdb2 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -29,6 +29,12 @@ def reproduce_eq: Joined<["--"], "reproduce=">,
 def version: Flag<["--"], "version">,
 HelpText<"Display the version number and exit">,
 Group;
+def lto_legacy_pass_manager: Flag<["--"], "lto-legacy-pass-manager">,
+HelpText<"Use the legacy pass manager in LLVM">,
+Group;
+def no_lto_legacy_pass_manager : Flag<["--"], "no-lto-legacy-pass-manager">,
+HelpText<"Use the new pass manager in LLVM">,
+Group;
 
 
 // This is a complete Options.td compiled from Apple's ld(1) manpage

diff  --git a/lld/test/MachO/objc-arc-contract.ll 
b/lld/test/MachO/objc-arc-contract.ll
new file mode 100644
index ..66f30dc60c49
--- /dev/null
+++ b/lld/test/MachO/objc-arc-contract.ll
@@ -0,0 +1,30 @@
+; REQUIRES: x86
+
+;; Verify that we run the ObjCARCContractPass during LTO. Without that, the
+;; objc.clang.arc.use intrinsic will get passed to the instruction selector,
+;; which doesn't know how to handle it.
+
+; RUN: llvm-as %s -o %t.o
+; RUN: %lld -dylib -lSystem %t.o -o %t --lto-legacy-pass-manager
+; RUN: llvm-objdump -d %t | FileCheck %s
+; RUN: %lld -dylib -lSystem %t.o -o %t --no-lto-legacy-pass-manager
+; RUN: llvm-objdump -d %t | FileCheck %s
+
+; RUN: opt -module-summary %s -o %t.o
+; RUN: %lld -dylib -lSystem %t.o -o %t --lto-legacy-pass-manager
+; RUN: llvm-objdump -d %t | FileCheck %s
+; RUN: %lld -dylib -lSystem %t.o -o %t --no-lto-legacy-pass-manager
+; RUN: llvm-objdump -d %t | FileCheck %s
+
+; CHECK:  <_foo>:
+; CHECK-NEXT: retq
+
+target triple = "x86_64-apple-darwin"
+target datalayout = 
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @foo(i8* %a, i8* %b) {
+  call void (...) @llvm.objc.clang.arc.use(i8* %a, i8* %b) nounwind
+  ret void
+}

[llvm-branch-commits] [lld] 34e8fcf - [lld-macho] Add dependency on ObjCARC to fix shared build

2021-01-20 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-01-20T20:41:51-05:00
New Revision: 34e8fcf63f823ebc5a36166c12c01c3a49deea0b

URL: 
https://github.com/llvm/llvm-project/commit/34e8fcf63f823ebc5a36166c12c01c3a49deea0b
DIFF: 
https://github.com/llvm/llvm-project/commit/34e8fcf63f823ebc5a36166c12c01c3a49deea0b.diff

LOG: [lld-macho] Add dependency on ObjCARC to fix shared build

Added: 


Modified: 
lld/MachO/CMakeLists.txt

Removed: 




diff  --git a/lld/MachO/CMakeLists.txt b/lld/MachO/CMakeLists.txt
index b76c120802a1..0279b2edcc3a 100644
--- a/lld/MachO/CMakeLists.txt
+++ b/lld/MachO/CMakeLists.txt
@@ -31,6 +31,7 @@ add_lld_library(lldMachO2
   DebugInfoDWARF
   LTO
   MC
+  ObjCARC
   Object
   Option
   Passes



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 041f3ee - [lld-macho] Ignore -lto_library

2021-01-22 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-01-22T16:48:50-05:00
New Revision: 041f3ee664c925148d1cfe48976ae671a660c949

URL: 
https://github.com/llvm/llvm-project/commit/041f3ee664c925148d1cfe48976ae671a660c949
DIFF: 
https://github.com/llvm/llvm-project/commit/041f3ee664c925148d1cfe48976ae671a660c949.diff

LOG: [lld-macho] Ignore -lto_library

Just getting rid of some logspew as I test LLD under existing build
systems.

Reviewed By: #lld-macho, smeenai

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

Added: 


Modified: 
lld/MachO/Options.td
lld/test/MachO/silent-ignore.test

Removed: 




diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 89473acebdb2..3410f50fa317 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -862,8 +862,7 @@ def object_path_lto : Separate<["-"], "object_path_lto">,
  Group;
 def lto_library : Separate<["-"], "lto_library">,
  MetaVarName<"">,
- HelpText<"Override the default ../lib/libLTO.dylib as ">,
- Flags<[HelpHidden]>,
+ HelpText<"Deprecated & ignored. LLD supports LTO directly, without using 
an external dylib.">,
  Group;
 def cache_path_lto : Separate<["-"], "cache_path_lto">,
  MetaVarName<"">,

diff  --git a/lld/test/MachO/silent-ignore.test 
b/lld/test/MachO/silent-ignore.test
index 2d1174835728..e6b4703057f8 100644
--- a/lld/test/MachO/silent-ignore.test
+++ b/lld/test/MachO/silent-ignore.test
@@ -4,6 +4,7 @@ RUN:   -no_deduplicate \
 RUN:   -lto_library /lib/foo \
 RUN:   -macosx_version_min 0 \
 RUN:   -dependency_info /path/to/dependency_info.dat \
+RUN:   -lto_library ../lib/libLTO.dylib \
 RUN:   -mllvm -time-passes \
 RUN:   -objc_abi_version 2 \
 RUN:   -ios_simulator_version_min 9.0.0 \



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] daaaed6 - [lld-macho] Fix TLV data initialization

2021-01-08 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-01-08T18:48:12-05:00
New Revision: daaaed6bb89044ac58a23f1bb1ccdd12342a5a58

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

LOG: [lld-macho] Fix TLV data initialization

We were mishandling the case where both `__tbss` and `__thread_data` sections 
were
present.

TLVP relocations should be encoded as offsets from the start of `__thread_data`,
even if the symbol is actually located in `__thread_bss`. Previously, we were
writing the offset from the start of the containing section, which doesn't
really make sense since there's no way `tlv_get_addr()` can know which section a
given `tlv$init` symbol is in at runtime.

In addition, this patch ensures that we place `__thread_data` immediately before
`__thread_bss`. This is what ld64 does, likely for performance reasons. Zerofill
sections must also be at the end of their segments; we were already doing this,
but now we ensure that `__thread_bss` occurs before `__bss`, so that it's always
possible to have it contiguous with `__thread_data`.

Fixes llvm.org/PR48657.

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/InputSection.cpp
lld/MachO/InputSection.h
lld/MachO/Writer.cpp
lld/MachO/Writer.h
lld/test/MachO/bss.s
lld/test/MachO/tlv.s

Removed: 




diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 8477d78d2f3f..4cb322a058f9 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -11,6 +11,7 @@
 #include "OutputSegment.h"
 #include "Symbols.h"
 #include "Target.h"
+#include "Writer.h"
 #include "lld/Common/Memory.h"
 #include "llvm/Support/Endian.h"
 
@@ -45,11 +46,12 @@ void InputSection::writeTo(uint8_t *buf) {
   target->resolveSymbolVA(buf + r.offset, *referentSym, r.type);
 
   if (isThreadLocalVariables(flags)) {
-// References from thread-local variable sections are treated
-// as offsets relative to the start of the referent section,
-// instead of as absolute addresses.
+// References from thread-local variable sections are treated as 
offsets
+// relative to the start of the thread-local data memory area, which
+// is initialized via copying all the TLV data sections (which are all
+// contiguous).
 if (auto *defined = dyn_cast(referentSym))
-  referentVA -= defined->isec->parent->addr;
+  referentVA -= firstTLVDataSection->addr;
   }
 } else if (auto *referentIsec = r.referent.dyn_cast()) {
   referentVA = referentIsec->getVA();

diff  --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index 5829936ece29..00b523fb8d46 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -60,13 +60,22 @@ class InputSection {
   std::vector relocs;
 };
 
+inline uint8_t sectionType(uint32_t flags) {
+  return flags & llvm::MachO::SECTION_TYPE;
+}
+
 inline bool isZeroFill(uint32_t flags) {
-  return llvm::MachO::isVirtualSection(flags & llvm::MachO::SECTION_TYPE);
+  return llvm::MachO::isVirtualSection(sectionType(flags));
 }
 
 inline bool isThreadLocalVariables(uint32_t flags) {
-  return (flags & llvm::MachO::SECTION_TYPE) ==
- llvm::MachO::S_THREAD_LOCAL_VARIABLES;
+  return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_VARIABLES;
+}
+
+// These sections contain the data for initializing thread-local variables.
+inline bool isThreadLocalData(uint32_t flags) {
+  return sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_REGULAR ||
+ sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_ZEROFILL;
 }
 
 inline bool isDebugSection(uint32_t flags) {

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index c9d700658371..53267c072c59 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -557,6 +557,24 @@ static int sectionOrder(OutputSection *osec) {
 .Case(section_names::unwindInfo, std::numeric_limits::max() - 1)
 .Case(section_names::ehFrame, std::numeric_limits::max())
 .Default(0);
+  } else if (segname == segment_names::data) {
+// For each thread spawned, dyld will initialize its TLVs by copying the
+// address range from the start of the first thread-local data section to
+// the end of the last one. We therefore arrange these sections 
contiguously
+// to minimize the amount of memory used. Additionally, since zerofill
+// sections must be at the end of their segments, and since TLV data
+// sections can be zerofills, we end up putting all TLV data sections at 
the
+// end of the segment.
+switch (sectionType(osec->flags)) {
+case S_THREAD_LOCAL_REGULAR:
+  return std::numeric_limits::max() - 2;
+case S_THREAD_LOCAL_ZEROFILL:
+  return std::

[llvm-branch-commits] [lld] e98b441 - [lld-macho] Remove unnecessary llvm:: namespace prefixes

2021-01-09 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2021-01-09T12:44:35-05:00
New Revision: e98b441a09fac0a8419fee15afc725758390ca2a

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

LOG: [lld-macho] Remove unnecessary llvm:: namespace prefixes

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/MachO/InputFiles.cpp
lld/MachO/LTO.cpp
lld/MachO/OutputSegment.cpp
lld/MachO/SymbolTable.cpp

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 9780443fb85a..2e52ef9a07fe 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -75,7 +75,7 @@ static HeaderFileType getOutputType(const opt::InputArgList 
&args) {
 
 static Optional
 findAlongPathsWithExtensions(StringRef name, ArrayRef extensions) {
-  llvm::SmallString<261> base;
+  SmallString<261> base;
   for (StringRef dir : config->librarySearchPaths) {
 base = dir;
 path::append(base, Twine("lib") + name);
@@ -99,7 +99,7 @@ static Optional findLibrary(StringRef name) {
 }
 
 static Optional findFramework(StringRef name) {
-  llvm::SmallString<260> symlink;
+  SmallString<260> symlink;
   StringRef suffix;
   std::tie(name, suffix) = name.split(",");
   for (StringRef dir : config->frameworkSearchPaths) {
@@ -109,7 +109,7 @@ static Optional findFramework(StringRef name) {
 if (!suffix.empty()) {
   // NOTE: we must resolve the symlink before trying the suffixes, because
   // there are no symlinks for the suffixed paths.
-  llvm::SmallString<260> location;
+  SmallString<260> location;
   if (!fs::real_path(symlink, location)) {
 // only append suffix if realpath() succeeds
 Twine suffixed = location + suffix;
@@ -127,11 +127,11 @@ static Optional findFramework(StringRef 
name) {
 
 static TargetInfo *createTargetInfo(opt::InputArgList &args) {
   StringRef arch = args.getLastArgValue(OPT_arch, "x86_64");
-  config->arch = llvm::MachO::getArchitectureFromName(
+  config->arch = MachO::getArchitectureFromName(
   args.getLastArgValue(OPT_arch, arch));
   switch (config->arch) {
-  case llvm::MachO::AK_x86_64:
-  case llvm::MachO::AK_x86_64h:
+  case MachO::AK_x86_64:
+  case MachO::AK_x86_64h:
 return createX86_64TargetInfo();
   default:
 fatal("missing or unsupported -arch " + arch);
@@ -575,7 +575,7 @@ static void handlePlatformVersion(const opt::Arg *arg) {
 static void handleUndefined(const opt::Arg *arg) {
   StringRef treatmentStr = arg->getValue(0);
   config->undefinedSymbolTreatment =
-  llvm::StringSwitch(treatmentStr)
+  StringSwitch(treatmentStr)
   .Case("error", UndefinedSymbolTreatment::error)
   .Case("warning", UndefinedSymbolTreatment::warning)
   .Case("suppress", UndefinedSymbolTreatment::suppress)
@@ -677,7 +677,7 @@ static uint32_t parseDylibVersion(const opt::ArgList& args, 
unsigned id) {
   return version.rawValue();
 }
 
-bool macho::link(llvm::ArrayRef argsArr, bool canExitEarly,
+bool macho::link(ArrayRef argsArr, bool canExitEarly,
  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
   lld::stdoutOS = &stdoutOS;
   lld::stderrOS = &stderrOS;
@@ -762,11 +762,11 @@ bool macho::link(llvm::ArrayRef argsArr, 
bool canExitEarly,
 message(getLLDVersion());
 message(StringRef("Library search paths:") +
 (config->librarySearchPaths.size()
- ? "\n\t" + llvm::join(config->librarySearchPaths, "\n\t")
+ ? "\n\t" + join(config->librarySearchPaths, "\n\t")
  : ""));
 message(StringRef("Framework search paths:") +
 (config->frameworkSearchPaths.size()
- ? "\n\t" + llvm::join(config->frameworkSearchPaths, "\n\t")
+ ? "\n\t" + join(config->frameworkSearchPaths, "\n\t")
  : ""));
 freeArena();
 return !errorCount();

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index e2282f1fb2bb..3d4d98b51606 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -664,7 +664,7 @@ DylibFile::DylibFile(const InterfaceFile &interface, 
DylibFile *umbrella)
 currentTopLevelTapi = nullptr;
 }
 
-ArchiveFile::ArchiveFile(std::unique_ptr &&f)
+ArchiveFile::ArchiveFile(std::unique_ptr &&f)
 : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)) {
   for (const object::Archive::Symbol &sym : file->symbols())
 symtab->addLazy(sym.getName(), this, sym);

diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index 9e74b69ff1ec..7554693f15e4 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -35,7 +35,7 @@ static lto::Config createConfig() {
 
 BitcodeCompiler::BitcodeCompiler() {
   auto backend =
-  
lto::createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
+  lto::createInProcessThinBackend(heavywei

[llvm-branch-commits] [lld] e493f08 - [lld-macho] Fill out release notes for 14.x

2022-02-15 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2022-02-15T14:21:01-05:00
New Revision: e493f08f8222b3a36186331a0d5486b4407ed830

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

LOG: [lld-macho] Fill out release notes for 14.x

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

Added: 


Modified: 
lld/docs/ReleaseNotes.rst

Removed: 




diff  --git a/lld/docs/ReleaseNotes.rst b/lld/docs/ReleaseNotes.rst
index ed4a374d579a3..3601a968bd77b 100644
--- a/lld/docs/ReleaseNotes.rst
+++ b/lld/docs/ReleaseNotes.rst
@@ -118,10 +118,85 @@ MinGW Improvements
 * ``--heap`` is now handled.
   (`D118405 `_)
 
-MachO Improvements
---
-
-* Item 1.
+Mach-O Improvements
+---
+
+* The ``ld64.lld.darwinnew`` symlink has been removed. Use ``ld64.lld`` to
+  invoke the Mach-O backend from now on. Moreover, the symlink
+  ``ld64.lld.darwinold`` -- and the old Mach-O LLD code that it pointed to --
+  have been removed. (`D114842 `_)
+* The "cannot export hidden symbol" error has been downgraded to a warning.
+  (`D107011 `_)
+* Common bitcode symbols are now supported.
+  (`D107027 `_)
+* Weak references in bitcode are now supported.
+  (`D115949 `_)
+* Thunk insertion now works more reliably.
+  (`D108897 `_,
+   `D108924 `_,
+   `D109079  `_,
+   `D116705 `_)
+* ``-ObjC`` now loads archive members before the symbol resolution phase.
+  (`D108781 `_)
+* ``-oso_prefix`` is now supported.
+  (`D112291 `_)
+* Properly encode binaries with zero exported symbols. Tools like
+  ``codesign_allocate`` no longer choke on those binaries.
+  (`D112589 `_)
+* We no longer treat architecture mismatches as a fatal error. Use
+  ``-arch_errors_fatal`` if that behavior is still desired.
+  (`D113082 `_)
+* Several performance improvements were done to speed LLD up on projects with
+  a lot of framework flags and library lookups. Large Swift-based projects
+  will benefit significantly.
+  (`D113073 `_,
+   `D113063 `_,
+   `D113153 `_,
+   `D113235 `_)
+* Several memory-reduction optimizations were done to reduce LLD's RSS
+  footprint.
+  (`D113813 `_,
+   `D113818 `_)
+* Symbol patterns from ``-[un]exported_symbols_list`` are now processed in
+  parallel. (`D113820 `_)
+* ``.weak_def_can_be_hidden`` symbols can now be exported.
+  (`D113167 `_)
+* ``-S`` -- to omit debug info -- is now handled.
+  (`D112594 `_)
+* ``-v`` now writes to stderr instead of stdout.
+  (`D113020 `_)
+* Private externs with GOT relocations are now marked as LOCAL in the indirect
+  symbol table. This allows ``strip -x`` to remove more symbols.
+  (`D111852 `_)
+* We no longer generate bogus addresses when ``__TEXT,__gcc_except_tab`` is
+  renamed. (`D113582 `_)
+* Unreferenced weak dylib symbols no longer trigger fetches from an archive.
+  (`D115092 `_)
+* ``$ld$hide`` symbols are now supported.
+  (`D115775 `_)
+* Symbols imported via `-weak_framework` are now properly marked as weak refs.
+  (`D114397 `_)
+* ``--warn-dylib-install-name`` and ``--no-warn-dylib-install-name`` were added
+  to toggle LLD-specific warnings around the use of ``-install_name``.
+  (`D113534 `_)
+* Passing both ``--icf=all`` and ``-no_deduplicate`` no longer results in a
+  warning. (`D110672 `_)
+* ICF now deduplicates functions with (identical) unwind info too.
+  (`D109946 `_)
+* We now support ordering sections based on call graph profile data.
+  (`D112164 `_)
+* Map file output now proceeds in parallel with output of the binary.
+  (`D117069 `_)
+* The map file now contains dead-stripped symbols too.
+  (`D114737  `_)
+* Multiple TLV sections with 
diff erent alignments are now handled properly.
+  (`D1

[llvm-branch-commits] [lld] b128f6f - [lld-macho] Release notes for 15.x

2022-07-31 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2022-07-31T13:24:58-04:00
New Revision: b128f6feaa467e9f1f53c995848b7b750d00dd23

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

LOG: [lld-macho] Release notes for 15.x

>From inspection of

  git log origin/release/14.x..origin/release/15.x -- lld/MachO

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/docs/ReleaseNotes.rst

Removed: 




diff  --git a/lld/docs/ReleaseNotes.rst b/lld/docs/ReleaseNotes.rst
index 936d800cabc37..6cc4cb465ad71 100644
--- a/lld/docs/ReleaseNotes.rst
+++ b/lld/docs/ReleaseNotes.rst
@@ -71,6 +71,117 @@ MachO Improvements
   (`D129540 `_,
   `D122258 `_)
 
+New flags
+#
+
+* ``-load_hidden`` and ``-hidden-l`` are now supported.
+  (`D130473 `_,
+  `D130529 `_)
+* ``-alias`` is now supported. (`D129938 `_)
+* ``-no_exported_symbols`` and  ``-exported_symbols_list `` are now
+  supported. (`D127562 `_)
+* ``-w`` -- to suppress warnings -- is now supported.
+  (`D127564 `_)
+* ``-non_global_symbols_strip_list``, ``-non_global_symbols_no_strip_list``, 
and
+  ``-x`` are now supported. (`D126046 `_)
+* ``--icf=safe`` is now supported.
+  (`D128938 `_,
+  `D123752 `_)
+* ``-why_live`` is now supported.
+  (`D120377 `_)
+* ``-pagezero_size`` is now supported.
+  (`D118724 `_)
+
+Improvements
+
+
+* Linker optimization hints are now supported.
+  (`D129427 `_,
+  `D129059 `_,
+  `D128942 `_,
+  `D128093 `_)
+* Rebase opcodes are now encoded more compactly.
+  (`D130180 `_,
+  `D128798 `_)
+* C-strings are now aligned more compactly.
+  (`D121342 `_)
+* ``--deduplicate-literals`` (and ``--icf={safe,all}``) now fold the
+  ``__cfstring`` section.
+  (`D130134  `_,
+  `D120137 `_)
+* ICF now folds the ``__objc_classrefs`` section.
+  (`D121053 `_)
+* ICF now folds functions with identical LSDAs.
+  (`D129830 `_)
+* STABS entries for folded functions are now omitted.
+  (`D123252 `_)
+* ``__objc_imageinfo`` sections are now folded.
+  (`D130125 `_)
+* Dylibs with ``LC_DYLD_EXPORTS_TRIE`` can now be read.
+  (`D129430 `_)
+* Writing zippered dylibs is now supported.
+  (`D124887 `_)
+* C-string literals are now included in the mapfile.
+  (`D118077 `_)
+* Symbol names in several more diagnostics are now demangled.
+  (`D130490 `_,
+  `D127110 `_,
+  `D125732 `_)
+* Source information is now included in symbol error messages.
+  (`D128425 `_,
+  `D128184 `_)
+* Numerous other improvements were made to diagnostic messages.
+  (`D127753 `_,
+  `D127696 `_,
+  `D127670 `_,
+  `D118903 `_,
+  `D118798 `_)
+* Many performance and memory improvements were made.
+  (`D13 `_,
+  `D128298 `_,
+  `D128290 `_,
+  `D126800 `_,
+  `D126785 `_,
+  `D121052 `_)
+* Order files and call graph sorting can now be used together.
+  (`D117354 `_)
+* Give LTO more precise symbol resolutions, which allows optimizations to be
+  more effective.
+  (`D119506 `_,
+  `D119372 `_,
+  `D119767 `_)
+* Added partial support for linking object files built with DTrace probes.
+  (`D129062 `_)
+
+Fixes
+#
+
+* Programs using Swift linked with the 14.

[llvm-branch-commits] [lld] 3184519 - [lld-macho] Don't emit rebase opcodes for relocs in TLV sections

2020-12-15 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-15T15:58:26-05:00
New Revision: 31845199094418173a3beadb786767b860bfda03

URL: 
https://github.com/llvm/llvm-project/commit/31845199094418173a3beadb786767b860bfda03
DIFF: 
https://github.com/llvm/llvm-project/commit/31845199094418173a3beadb786767b860bfda03.diff

LOG: [lld-macho] Don't emit rebase opcodes for relocs in TLV sections

Their addresses are already encoded as section-relative offsets, so
there's no need to rebase them at runtime. {D85080} has some context
on the weirdness of TLV sections.

Fixes llvm.org/PR48491.

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/Arch/X86_64.cpp
lld/test/MachO/tlv-dylib.s
lld/test/MachO/tlv.s

Removed: 




diff  --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp
index 7f4024cc998dd..c776e21d6f5f5 100644
--- a/lld/MachO/Arch/X86_64.cpp
+++ b/lld/MachO/Arch/X86_64.cpp
@@ -248,7 +248,11 @@ void X86_64::prepareSymbolRelocation(lld::macho::Symbol 
*sym,
 return;
   }
 }
-addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
+// References from thread-local variable sections are treated as offsets
+// relative to the start of the referent section, and therefore have no
+// need of rebase opcodes.
+if (!(isThreadLocalVariables(isec->flags) && isa(sym)))
+  addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
 break;
   }
   case X86_64_RELOC_SIGNED:

diff  --git a/lld/test/MachO/tlv-dylib.s b/lld/test/MachO/tlv-dylib.s
index c6f9b9add3a0a..74e27e9e24d1b 100644
--- a/lld/test/MachO/tlv-dylib.s
+++ b/lld/test/MachO/tlv-dylib.s
@@ -4,9 +4,14 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libtlv.s -o 
%t/libtlv.o
 # RUN: %lld -dylib -install_name @executable_path/libtlv.dylib \
 # RUN:   -lSystem -o %t/libtlv.dylib %t/libtlv.o
-# RUN: llvm-objdump --exports-trie -d --no-show-raw-insn %t/libtlv.dylib | 
FileCheck %s --check-prefix=DYLIB
+# RUN: llvm-objdump --macho --exports-trie --rebase %t/libtlv.dylib | \
+# RUN:   FileCheck %s --check-prefix=DYLIB
 # DYLIB-DAG: _foo [per-thread]
 # DYLIB-DAG: _bar [per-thread]
+## Make sure we don't emit rebase opcodes for relocations in __thread_vars.
+# DYLIB:   Rebase table:
+# DYLIB-NEXT:  segment  sectionaddress type
+# DYLIB-EMPTY:
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
 # RUN: %lld -lSystem -L%t -ltlv %t/test.o -o %t/test

diff  --git a/lld/test/MachO/tlv.s b/lld/test/MachO/tlv.s
index bf0dfcd02b565..d8d0a950d794c 100644
--- a/lld/test/MachO/tlv.s
+++ b/lld/test/MachO/tlv.s
@@ -1,9 +1,14 @@
 # REQUIRES: x86
 # RUN: mkdir -p %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
+
 # RUN: %lld -lSystem -o %t/test %t/test.o
 # RUN: llvm-readobj --file-headers %t/test | FileCheck %s --check-prefix=HEADER
-# RUN: llvm-objdump -D %t/test | FileCheck %s
+# RUN: llvm-objdump -D --bind --rebase %t/test | FileCheck %s
+
+# RUN: %lld -lSystem -pie -o %t/test %t/test.o
+# RUN: llvm-readobj --file-headers %t/test | FileCheck %s --check-prefix=HEADER
+# RUN: llvm-objdump -D --bind --rebase %t/test | FileCheck %s
 
 # HEADER: MH_HAS_TLV_DESCRIPTORS
 
@@ -36,6 +41,13 @@
 # CHECK-NEXT:  00 00
 # CHECK-NEXT:  00 00
 
+## Make sure we don't emit rebase opcodes for relocations in __thread_vars.
+# CHECK:   Rebase table:
+# CHECK-NEXT:  segment  sectionaddress type
+# CHECK-NEXT:  Bind table:
+# CHECK:   __DATA  __thread_vars   0x{{[0-9a-f]*}}  pointer 0 libSystem 
__tlv_bootstrap
+# CHECK:   __DATA  __thread_vars   0x{{[0-9a-f]*}}  pointer 0 libSystem 
__tlv_bootstrap
+
 .globl _main
 _main:
   mov _foo@TLVP(%rip), %rax



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 544148a - [lld-macho] -weak_{library, framework} should always take priority

2020-12-15 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-15T15:58:26-05:00
New Revision: 544148ae702ac3e94aa41b656e9f6d11b382f9f6

URL: 
https://github.com/llvm/llvm-project/commit/544148ae702ac3e94aa41b656e9f6d11b382f9f6
DIFF: 
https://github.com/llvm/llvm-project/commit/544148ae702ac3e94aa41b656e9f6d11b382f9f6.diff

LOG: [lld-macho] -weak_{library,framework} should always take priority

We were not setting forceWeakImport for file paths given by
`-weak_library` if we had already loaded the file. This diff fixes that
by having `loadDylib` return a cached DylibFile instance even if we have
already loaded that file.

We still avoid emitting multiple LC_LOAD_DYLIBs, but we achieve this by
making inputFiles a SetVector instead of relying on the `loadedDylibs`
cache.

Reviewed By: #lld-macho, smeenai

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/MachO/DriverUtils.cpp
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/test/MachO/invalid/duplicate-symbol.s
lld/test/MachO/weak-import.s

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index c456d2fb1fd5..96972cb17e6f 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -274,8 +274,7 @@ static InputFile *addFile(StringRef path, bool 
forceLoadArchive) {
 if (config->allLoad || forceLoadArchive) {
   if (Optional buffer = readFile(path)) {
 for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
-  inputFiles.push_back(
-  make(member.mbref, member.modTime, path));
+  inputFiles.insert(make(member.mbref, member.modTime, path));
   printArchiveMemberLoad(
   (forceLoadArchive ? "-force_load" : "-all_load"),
   inputFiles.back());
@@ -293,7 +292,7 @@ static InputFile *addFile(StringRef path, bool 
forceLoadArchive) {
   if (Optional buffer = readFile(path)) {
 for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
   if (hasObjCSection(member.mbref)) {
-inputFiles.push_back(
+inputFiles.insert(
 make(member.mbref, member.modTime, path));
 printArchiveMemberLoad("-ObjC", inputFiles.back());
   }
@@ -325,7 +324,7 @@ static InputFile *addFile(StringRef path, bool 
forceLoadArchive) {
 // print the .a name here.
 if (config->printEachFile && magic != file_magic::archive)
   lld::outs() << toString(newFile) << '\n';
-inputFiles.push_back(newFile);
+inputFiles.insert(newFile);
   }
   return newFile;
 }
@@ -521,7 +520,7 @@ static void compileBitcodeFiles() {
   lto->add(*bitcodeFile);
 
   for (ObjFile *file : lto->compile())
-inputFiles.push_back(file);
+inputFiles.insert(file);
 }
 
 // Replaces common symbols with defined symbols residing in __common sections.
@@ -873,7 +872,7 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
 StringRef fileName = arg->getValue(2);
 Optional buffer = readFile(fileName);
 if (buffer)
-  inputFiles.push_back(make(*buffer, segName, sectName));
+  inputFiles.insert(make(*buffer, segName, sectName));
   }
 
   // Initialize InputSections.

diff  --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index 05677a9df78d..5040f634e181 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -15,7 +15,7 @@
 #include "lld/Common/Memory.h"
 #include "lld/Common/Reproduce.h"
 #include "llvm/ADT/CachedHashString.h"
-#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Option/Option.h"
@@ -168,33 +168,32 @@ Optional macho::resolveDylibPath(StringRef 
path) {
   return {};
 }
 
-static Optional makeDylibFromTapi(MemoryBufferRef mbref,
-   DylibFile *umbrella) {
-  Expected> result = TextAPIReader::get(mbref);
-  if (!result) {
-error("could not load TAPI file at " + mbref.getBufferIdentifier() + ": " +
-  toString(result.takeError()));
-return {};
-  }
-  return make(**result, umbrella);
-}
-
-static DenseSet loadedDylibs;
+// It's not uncommon to have multiple attempts to load a single dylib,
+// especially if it's a commonly re-exported core library.
+static DenseMap loadedDylibs;
 
 Optional macho::loadDylib(MemoryBufferRef mbref,
DylibFile *umbrella) {
   StringRef path = mbref.getBufferIdentifier();
-  if (loadedDylibs.contains(CachedHashStringRef(path)))
-return {};
-  loadedDylibs.insert(CachedHashStringRef(path));
+  DylibFile *&file = loadedDylibs[CachedHashStringRef(path)];
+  if (file)
+return file;
 
   file_magic magic = identify_magic(mbref.getBuffer());
-  if (magic == file_magic::tapi_file)
-return makeDylibFromTapi(mbref, umbrella);
-
-  assert(magic == file_magic::macho_dynamically_linked_shared_lib ||

[llvm-branch-commits] [lld] 8a5e068 - [lld-macho] Support -sub_umbrella

2020-12-15 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-15T15:58:26-05:00
New Revision: 8a5e0688233636ac6f9a27fa57006cba9f67a945

URL: 
https://github.com/llvm/llvm-project/commit/8a5e0688233636ac6f9a27fa57006cba9f67a945
DIFF: 
https://github.com/llvm/llvm-project/commit/8a5e0688233636ac6f9a27fa57006cba9f67a945.diff

LOG: [lld-macho] Support -sub_umbrella

>From what I can tell, it's essentially identical to
`-sub_library`, but it doesn't match files ending in ".dylib".

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/MachO/Options.td
lld/test/MachO/sub-library.s

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 96972cb17e6f..8bac9b7b877f 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -488,12 +488,13 @@ static void parseOrderFile(StringRef path) {
 // with a path of .*/libfoo.{dylib, tbd}.
 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
 // I'm not sure what the use case for that is.
-static bool markSubLibrary(StringRef searchName) {
+static bool markReexport(StringRef searchName, ArrayRef extensions) 
{
   for (InputFile *file : inputFiles) {
 if (auto *dylibFile = dyn_cast(file)) {
   StringRef filename = path::filename(dylibFile->getName());
   if (filename.consume_front(searchName) &&
-  (filename == ".dylib" || filename == ".tbd")) {
+  (filename.empty() ||
+   find(extensions, filename) != extensions.end())) {
 dylibFile->reexport = true;
 return true;
   }
@@ -836,11 +837,17 @@ bool macho::link(llvm::ArrayRef argsArr, 
bool canExitEarly,
 
   // Now that all dylibs have been loaded, search for those that should be
   // re-exported.
-  for (opt::Arg *arg : args.filtered(OPT_sub_library)) {
+  for (opt::Arg *arg : args.filtered(OPT_sub_library, OPT_sub_umbrella)) {
 config->hasReexports = true;
 StringRef searchName = arg->getValue();
-if (!markSubLibrary(searchName))
-  error("-sub_library " + searchName + " does not match a supplied dylib");
+std::vector extensions;
+if (arg->getOption().getID() == OPT_sub_library)
+  extensions = {".dylib", ".tbd"};
+else
+  extensions = {".tbd"};
+if (!markReexport(searchName, extensions))
+  error(arg->getSpelling() + " " + searchName +
+" does not match a supplied dylib");
   }
 
   // Parse LTO options.

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index ca13e3b9..e3ee14a74328 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -672,7 +672,6 @@ def sub_library : Separate<["-"], "sub_library">,
 def sub_umbrella : Separate<["-"], "sub_umbrella">,
  MetaVarName<"">,
  HelpText<"Re-export the framework as ">,
- Flags<[HelpHidden]>,
  Group;
 def allowable_client : Separate<["-"], "allowable_client">,
  MetaVarName<"">,

diff  --git a/lld/test/MachO/sub-library.s b/lld/test/MachO/sub-library.s
index 0e5b596dc04a..7f5e2904e962 100644
--- a/lld/test/MachO/sub-library.s
+++ b/lld/test/MachO/sub-library.s
@@ -22,19 +22,16 @@
 # RUN:   --check-prefix=HELLO-HEADERS
 # HELLO-HEADERS: NO_REEXPORTED_DYLIBS
 
-# RUN: llvm-objdump --macho --all-headers %t/libgoodbye.dylib | FileCheck %s 
-DDIR=%t \
-# RUN:   --check-prefix=GOODBYE-HEADERS
-# GOODBYE-HEADERS-NOT: NO_REEXPORTED_DYLIBS
-# GOODBYE-HEADERS: cmd LC_REEXPORT_DYLIB
-# GOODBYE-HEADERS-NOT: Load command
-# GOODBYE-HEADERS: name[[DIR]]/libhello.dylib
-
-# RUN: llvm-objdump --macho --all-headers %t/libsuper.dylib | FileCheck %s 
-DDIR=%t \
-# RUN:   --check-prefix=SUPER-HEADERS
-# SUPER-HEADERS-NOT: NO_REEXPORTED_DYLIBS
-# SUPER-HEADERS: cmd LC_REEXPORT_DYLIB
-# SUPER-HEADERS-NOT: Load command
-# SUPER-HEADERS: name[[DIR]]/libgoodbye.dylib
+# RUN: llvm-objdump --macho --all-headers %t/libgoodbye.dylib | FileCheck %s \
+# RUN:   --check-prefix=REEXPORT-HEADERS -DPATH=%t/libhello.dylib
+
+# RUN: llvm-objdump --macho --all-headers %t/libsuper.dylib | FileCheck %s \
+# RUN:   --check-prefix=REEXPORT-HEADERS -DPATH=%t/libgoodbye.dylib
+
+# REEXPORT-HEADERS-NOT: NO_REEXPORTED_DYLIBS
+# REEXPORT-HEADERS: cmd LC_REEXPORT_DYLIB
+# REEXPORT-HEADERS-NOT: Load command
+# REEXPORT-HEADERS: name[[PATH]]
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/sub-library.o
 # RUN: %lld -o %t/sub-library -L%t -lsuper %t/sub-library.o
@@ -54,6 +51,24 @@
 # RUN:  | FileCheck %s --check-prefix=MISSING-REEXPORT -DDIR=%t
 # MISSING-REEXPORT: error: unable to locate re-export with install name 
[[DIR]]/libgoodbye.dylib
 
+
+## We can match dylibs without extensions too.
+# RUN: mkdir -p %t/Hello.framework/Versions
+# RUN: %lld -dylib %t/libhello.o -o %t/Hello.framework/Versions/Hello
+# RUN: %lld -dylib -o %t/libgoodbye2.dylib -sub_library Hello 
%t/Hello.framework/Versions/Hello %t/libg

[llvm-branch-commits] [lld] 3aa8e07 - [lld-macho] Add implicit dylib support for frameworks

2020-12-15 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-15T15:58:26-05:00
New Revision: 3aa8e071dd1ae7e9fb9e28fa9fcdb9f9fdb7aa06

URL: 
https://github.com/llvm/llvm-project/commit/3aa8e071dd1ae7e9fb9e28fa9fcdb9f9fdb7aa06
DIFF: 
https://github.com/llvm/llvm-project/commit/3aa8e071dd1ae7e9fb9e28fa9fcdb9f9fdb7aa06.diff

LOG: [lld-macho] Add implicit dylib support for frameworks

{D93000} applied to frameworks. Partial fix for PR48511.

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/InputFiles.cpp
lld/test/MachO/implicit-dylibs.s

Removed: 




diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 4bf568284c54..78c3dc9b7a52 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -508,9 +508,16 @@ static bool isImplicitlyLinked(StringRef path) {
   if (!config->implicitDylibs)
 return false;
 
-  return path::parent_path(path) == "/usr/lib";
-  // TODO: check for public frameworks too. We'll need to implement
-  // -sub_umbrella first to write a test case.
+  if (path::parent_path(path) == "/usr/lib")
+return true;
+
+  // Match /System/Library/Frameworks/$FOO.framework/**/$FOO
+  if (path.consume_front("/System/Library/Frameworks/")) {
+StringRef frameworkName = path.take_until([](char c) { return c == '.'; });
+return path::filename(path) == frameworkName;
+  }
+
+  return false;
 }
 
 void loadReexport(StringRef path, DylibFile *umbrella) {

diff  --git a/lld/test/MachO/implicit-dylibs.s 
b/lld/test/MachO/implicit-dylibs.s
index 21084b50006b..bd2d6e8d104b 100644
--- a/lld/test/MachO/implicit-dylibs.s
+++ b/lld/test/MachO/implicit-dylibs.s
@@ -3,10 +3,16 @@
 # UNSUPPORTED: system-windows
 # RUN: rm -rf %t; split-file %s %t
 # RUN: mkdir -p %t/usr/lib/system
+# RUN: mkdir -p %t/System/Library/Frameworks/Foo.framework/Versions/A
+# RUN: mkdir -p 
%t/System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Versions/A
+# RUN: mkdir -p %t/Baz.framework/Versions/A
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o 
%t/libfoo.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libtoplevel.s -o 
%t/libtoplevel.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libsublevel.s -o 
%t/libsublevel.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/framework-foo.s -o 
%t/framework-foo.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/framework-bar.s -o 
%t/framework-bar.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/framework-baz.s -o 
%t/framework-baz.o
 ## libunused will be used to verify that we load implicit dylibs even if we
 ## don't use any symbols they contain.
 # RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o 
%t/libunused.o
@@ -16,10 +22,28 @@
 # RUN: %lld -dylib -lSystem %t/libtoplevel.o -o %t/usr/lib/libtoplevel.dylib 
-install_name /usr/lib/libtoplevel.dylib
 # RUN: %lld -dylib -lSystem %t/libsublevel.o -o 
%t/usr/lib/system/libsublevel.dylib -install_name 
/usr/lib/system/libsublevel.dylib
 # RUN: %lld -dylib -lSystem %t/libunused.o -o %t/usr/lib/libunused.dylib 
-install_name /usr/lib/libunused.dylib
-# RUN: %lld -dylib -syslibroot %t \
+
+## Bar.framework is nested within Foo.framework.
+# RUN: %lld -dylib -lSystem %t/framework-baz.o -o 
%t/System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Versions/A/Bar
 \
+# RUN:   -install_name 
/System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Versions/A/Bar
+# RUN: ln -sf 
%t/System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Versions/A/Bar
 \
+# RUN:   
%t/System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Bar
+
+## Have Foo re-export Bar.
+# RUN: %lld -dylib -F %t/System/Library/Frameworks/Foo.framework/Frameworks \
+# RUN:   -framework Bar -sub_umbrella Bar -lSystem %t/framework-foo.o -o 
%t/System/Library/Frameworks/Foo.framework/Versions/A/Foo \
+# RUN:   -install_name /System/Library/Frameworks/Foo.framework/Versions/A/Foo
+# RUN: ln -sf %t/System/Library/Frameworks/Foo.framework/Versions/A/Foo 
%t/System/Library/Frameworks/Foo.framework/Foo
+
+# RUN: %lld -dylib -lSystem %t/framework-bar.o -o 
%t/Baz.framework/Versions/A/Baz \
+# RUN:   -install_name %t/Baz.framework/Versions/A/Baz
+# RUN: ln -sf %t/Baz.framework/Versions/A/Baz %t/Baz.framework/Baz
+
+# RUN: %lld -dylib -syslibroot %t -framework Foo -F %t -framework Baz \
 # RUN:   -lc++ -ltoplevel -lunused %t/usr/lib/system/libsublevel.dylib 
%t/libfoo.dylib \
 # RUN:   -sub_library libc++ -sub_library libfoo -sub_library libtoplevel \
 # RUN:   -sub_library libsublevel -sub_library libunused \
+# RUN:   -sub_umbrella Baz -sub_umbrella Foo \
 # RUN:   %t/reexporter.o -o %t/libreexporter.dylib
 
 # RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %t/test.s -o %t/test.o
@@ -29,6 +53,9 @@
 # CHECK-DAG: __DATA __data {{.*}} pointer 0 libreexporter

[llvm-branch-commits] [lld] 811444d - [lld-macho] Add support for weak references

2020-12-17 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-17T08:49:16-05:00
New Revision: 811444d7a173e696f975f8d41626f6809439f726

URL: 
https://github.com/llvm/llvm-project/commit/811444d7a173e696f975f8d41626f6809439f726
DIFF: 
https://github.com/llvm/llvm-project/commit/811444d7a173e696f975f8d41626f6809439f726.diff

LOG: [lld-macho] Add support for weak references

Weak references need not necessarily be satisfied at runtime (but they must
still be satisfied at link time). So symbol resolution still works as per usual,
but we now pass around a flag -- ultimately emitting it in the bind table -- to
indicate if a given dylib symbol is a weak reference.

ld64's behavior for symbols that have both weak and strong references is
a bit bizarre. For non-function symbols, it will emit a weak import. For
function symbols (those referenced by BRANCH relocs), it will emit a
regular import. I'm not sure what value there is in that behavior, and
since emulating it will make our implementation more complex, I've
decided to treat regular weakrefs like function symbol ones for now.

Fixes PR48511.

Reviewed By: #lld-macho, thakis

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

Added: 
lld/test/MachO/weak-reference.s

Modified: 
lld/MachO/Driver.cpp
lld/MachO/InputFiles.cpp
lld/MachO/SymbolTable.cpp
lld/MachO/SymbolTable.h
lld/MachO/Symbols.h
lld/MachO/SyntheticSections.cpp
lld/test/MachO/symtab.s

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index c522a082a306..db89dd60a20b 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -285,7 +285,7 @@ static InputFile *addFile(StringRef path, bool 
forceLoadArchive) {
 } else if (config->forceLoadObjC) {
   for (const object::Archive::Symbol &sym : file->symbols())
 if (sym.getName().startswith(objc::klass))
-  symtab->addUndefined(sym.getName());
+  symtab->addUndefined(sym.getName(), /*isWeakRef=*/false);
 
   // TODO: no need to look for ObjC sections for a given archive member if
   // we already found that it contains an ObjC symbol. We should also
@@ -723,7 +723,8 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
   symtab = make();
   target = createTargetInfo(args);
 
-  config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"));
+  config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
+   /*isWeakRef=*/false);
   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
   config->installName =
   args.getLastArgValue(OPT_install_name, config->outputFile);

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 3ae3b976afa3..a32e8caf3d29 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -283,7 +283,7 @@ macho::Symbol *ObjFile::parseNonSectionSymbol(const 
structs::nlist_64 &sym,
   switch (type) {
   case N_UNDF:
 return sym.n_value == 0
-   ? symtab->addUndefined(name)
+   ? symtab->addUndefined(name, sym.n_desc & N_WEAK_REF)
: symtab->addCommon(name, this, sym.n_value,
1 << GET_COMM_ALIGN(sym.n_desc));
   case N_ABS:

diff  --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp
index ecf8f94239e1..93a2508951a5 100644
--- a/lld/MachO/SymbolTable.cpp
+++ b/lld/MachO/SymbolTable.cpp
@@ -62,15 +62,21 @@ Symbol *SymbolTable::addDefined(StringRef name, 
InputSection *isec,
   return s;
 }
 
-Symbol *SymbolTable::addUndefined(StringRef name) {
+Symbol *SymbolTable::addUndefined(StringRef name, bool isWeakRef) {
   Symbol *s;
   bool wasInserted;
   std::tie(s, wasInserted) = insert(name);
 
+  auto refState = isWeakRef ? RefState::Weak : RefState::Strong;
+
   if (wasInserted)
-replaceSymbol(s, name);
-  else if (LazySymbol *lazy = dyn_cast(s))
+replaceSymbol(s, name, refState);
+  else if (auto *lazy = dyn_cast(s))
 lazy->fetchArchiveMember();
+  else if (auto *dynsym = dyn_cast(s))
+dynsym->refState = std::max(dynsym->refState, refState);
+  else if (auto *undefined = dyn_cast(s))
+undefined->refState = std::max(undefined->refState, refState);
   return s;
 }
 
@@ -101,14 +107,21 @@ Symbol *SymbolTable::addDylib(StringRef name, DylibFile 
*file, bool isWeakDef,
   bool wasInserted;
   std::tie(s, wasInserted) = insert(name);
 
-  if (!wasInserted && isWeakDef)
-if (auto *defined = dyn_cast(s))
-  if (!defined->isWeakDef())
+  auto refState = RefState::Unreferenced;
+  if (!wasInserted) {
+if (auto *defined = dyn_cast(s)) {
+  if (isWeakDef && !defined->isWeakDef())
 defined->overridesWeakDef = true;
+} else if (auto *undefined = dyn_cast(s)) {
+  refState = undefined->refState;
+} else if (auto *dysym = dyn_cast(s)) {
+  refState = dysym->refState;
+}
+  }
 
   if (wasInserted || isa(s) ||
   (isa(s) && !isWeakDef && s->

[llvm-branch-commits] [lld] 4c8276c - [lld-macho] Use LC_LOAD_WEAK_DYLIB for dylibs with only weakrefs

2020-12-17 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-17T08:49:17-05:00
New Revision: 4c8276cdc120c24410dcd62a9986f04e7327fc2f

URL: 
https://github.com/llvm/llvm-project/commit/4c8276cdc120c24410dcd62a9986f04e7327fc2f
DIFF: 
https://github.com/llvm/llvm-project/commit/4c8276cdc120c24410dcd62a9986f04e7327fc2f.diff

LOG: [lld-macho] Use LC_LOAD_WEAK_DYLIB for dylibs with only weakrefs

Note that dylibs without *any* refs will still be loaded in the usual
(strong) fashion.

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/Symbols.h
lld/MachO/Writer.cpp
lld/test/MachO/weak-import.s

Removed: 




diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index a32e8caf3d29..ce66c9650446 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -527,7 +527,7 @@ void loadReexport(StringRef path, DylibFile *umbrella) {
 }
 
 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella)
-: InputFile(DylibKind, mb) {
+: InputFile(DylibKind, mb), refState(RefState::Unreferenced) {
   if (umbrella == nullptr)
 umbrella = this;
 
@@ -580,7 +580,7 @@ DylibFile::DylibFile(MemoryBufferRef mb, DylibFile 
*umbrella)
 }
 
 DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella)
-: InputFile(DylibKind, interface) {
+: InputFile(DylibKind, interface), refState(RefState::Unreferenced) {
   if (umbrella == nullptr)
 umbrella = this;
 

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index f48fc1f8c232..ef573145f594 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -38,6 +38,7 @@ namespace macho {
 class InputSection;
 class Symbol;
 struct Reloc;
+enum class RefState : uint8_t;
 
 // If --reproduce option is given, all input files are written
 // to this tar archive.
@@ -135,6 +136,7 @@ class DylibFile : public InputFile {
   uint32_t compatibilityVersion = 0;
   uint32_t currentVersion = 0;
   uint64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel
+  RefState refState;
   bool reexport = false;
   bool forceWeakImport = false;
 };

diff  --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h
index 80898e085818..80be27dd1c1f 100644
--- a/lld/MachO/Symbols.h
+++ b/lld/MachO/Symbols.h
@@ -116,7 +116,11 @@ class Defined : public Symbol {
   const bool external : 1;
 };
 
-// Indicates whether & how a dylib symbol is referenced.
+// This enum does double-duty: as a symbol property, it indicates whether & how
+// a dylib symbol is referenced. As a DylibFile property, it indicates the kind
+// of referenced symbols contained within the file. If there are both weak
+// and strong references to the same file, we will count the file as
+// strongly-referenced.
 enum class RefState : uint8_t { Unreferenced = 0, Weak = 1, Strong = 2 };
 
 class Undefined : public Symbol {

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 29c8fd6ed1fa..a4c677a4b288 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -43,6 +43,7 @@ class Writer {
   Writer() : buffer(errorHandler().outputBuffer) {}
 
   void scanRelocations();
+  void scanSymbols();
   void createOutputSections();
   void createLoadCommands();
   void assignAddresses(OutputSegment *);
@@ -424,6 +425,17 @@ void Writer::scanRelocations() {
   }
 }
 
+void Writer::scanSymbols() {
+  for (const macho::Symbol *sym : symtab->getSymbols()) {
+if (const auto *defined = dyn_cast(sym)) {
+  if (defined->overridesWeakDef)
+in.weakBinding->addNonWeakDefinition(defined);
+} else if (const auto *dysym = dyn_cast(sym)) {
+  dysym->file->refState = std::max(dysym->file->refState, dysym->refState);
+}
+  }
+}
+
 void Writer::createLoadCommands() {
   in.header->addLoadCommand(make(
   in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports));
@@ -463,10 +475,10 @@ void Writer::createLoadCommands() {
   uint64_t dylibOrdinal = 1;
   for (InputFile *file : inputFiles) {
 if (auto *dylibFile = dyn_cast(file)) {
-  // TODO: dylibs that are only referenced by weak refs should also be
-  // loaded via LC_LOAD_WEAK_DYLIB.
   LoadCommandType lcType =
-  dylibFile->forceWeakImport ? LC_LOAD_WEAK_DYLIB : LC_LOAD_DYLIB;
+  dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
+  ? LC_LOAD_WEAK_DYLIB
+  : LC_LOAD_DYLIB;
   in.header->addLoadCommand(make(lcType, dylibFile->dylibName,
   dylibFile->compatibilityVersion,
   dylibFile->currentVersion));
@@ -699,11 +711,7 @@ void Writer::run() {
   scanRelocations();
   if (in.stubHelper->isNeeded())
 in.stubHelper->setup();
-
-  for (const macho::Symbol *sym : symtab->getSymbols())
-if (const auto *defined = dyn_cast(sym))
-

[llvm-branch-commits] [lld] 5f9896d - [lld-macho] Support Obj-C symbols in order files

2020-12-20 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-20T13:49:18-05:00
New Revision: 5f9896d3b23c31beecf225d90194e1bf4e097677

URL: 
https://github.com/llvm/llvm-project/commit/5f9896d3b23c31beecf225d90194e1bf4e097677
DIFF: 
https://github.com/llvm/llvm-project/commit/5f9896d3b23c31beecf225d90194e1bf4e097677.diff

LOG: [lld-macho] Support Obj-C symbols in order files

Obj-C symbols may have spaces and colons, which our previous order file
parser would be confused by. The order file format has made the very unfortunate
choice of using colons for its delimiters, which means that we have to use
heuristics to determine if a given colon is part of a symbol or not...

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/test/MachO/order-file.s

Removed: 
lld/test/MachO/invalid/order-file-bad-arch.test
lld/test/MachO/invalid/order-file-bad-objfile.test



diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 63d101270cf5..9838e67cd4a2 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -393,19 +393,15 @@ static void addFileList(StringRef path) {
 addFile(path, false);
 }
 
-static std::array archNames{"arm","arm64", "i386",
-  "x86_64", "ppc",   "ppc64"};
-static bool isArchString(StringRef s) {
-  static DenseSet archNamesSet(archNames.begin(), archNames.end());
-  return archNamesSet.find(s) != archNamesSet.end();
-}
-
 // An order file has one entry per line, in the following format:
 //
-//   ::
+//   ::
 //
-//  and  are optional. If not specified, then that entry
-// matches any symbol of that name.
+//  and  are optional. If not specified, then that entry
+// matches any symbol of that name. Parsing this format is not quite
+// straightforward because the symbol name itself can contain colons, so when
+// encountering a colon, we consider the preceding characters to decide if it
+// can be a valid CPU type or file path.
 //
 // If a symbol is matched by multiple entries, then it takes the lowest-ordered
 // entry (the one nearest to the front of the list.)
@@ -420,59 +416,37 @@ static void parseOrderFile(StringRef path) {
 
   MemoryBufferRef mbref = *buffer;
   size_t priority = std::numeric_limits::max();
-  for (StringRef rest : args::getLines(mbref)) {
-StringRef arch, objectFile, symbol;
-
-std::array fields;
-uint8_t fieldCount = 0;
-while (rest != "" && fieldCount < 3) {
-  std::pair p = getToken(rest, ": \t\n\v\f\r");
-  StringRef tok = p.first;
-  rest = p.second;
-
-  // Check if we have a comment
-  if (tok == "" || tok[0] == '#')
-break;
-
-  fields[fieldCount++] = tok;
-}
-
-switch (fieldCount) {
-case 3:
-  arch = fields[0];
-  objectFile = fields[1];
-  symbol = fields[2];
-  break;
-case 2:
-  (isArchString(fields[0]) ? arch : objectFile) = fields[0];
-  symbol = fields[1];
-  break;
-case 1:
-  symbol = fields[0];
-  break;
-case 0:
-  break;
-default:
-  llvm_unreachable("too many fields in order file");
-}
+  for (StringRef line : args::getLines(mbref)) {
+StringRef objectFile, symbol;
+line = line.take_until([](char c) { return c == '#'; }); // ignore comments
+line = line.ltrim();
+
+CPUType cpuType = StringSwitch(line)
+  .StartsWith("i386:", CPU_TYPE_I386)
+  .StartsWith("x86_64:", CPU_TYPE_X86_64)
+  .StartsWith("arm:", CPU_TYPE_ARM)
+  .StartsWith("arm64:", CPU_TYPE_ARM64)
+  .StartsWith("ppc:", CPU_TYPE_POWERPC)
+  .StartsWith("ppc64:", CPU_TYPE_POWERPC64)
+  .Default(CPU_TYPE_ANY);
+// Drop the CPU type as well as the colon
+if (cpuType != CPU_TYPE_ANY)
+  line = line.drop_until([](char c) { return c == ':'; }).drop_front();
+// TODO: Update when we extend support for other CPUs
+if (cpuType != CPU_TYPE_ANY && cpuType != CPU_TYPE_X86_64)
+  continue;
 
-if (!arch.empty()) {
-  if (!isArchString(arch)) {
-error("invalid arch \"" + arch + "\" in order file: expected one of " +
-  llvm::join(archNames, ", "));
-continue;
+constexpr std::array fileEnds = {".o:", ".o):"};
+for (StringRef fileEnd : fileEnds) {
+  size_t pos = line.find(fileEnd);
+  if (pos != StringRef::npos) {
+// Split the string around the colon
+objectFile = line.take_front(pos + fileEnd.size() - 1);
+line = line.drop_front(pos + fileEnd.size());
+break;
   }
-
-  // TODO: Update when we extend support for other archs
-  if (arch != "x86_64")
-continue;
-}
-
-if (!objectFile.empty() && !objectFile.endswith(".o")) {
-  error("invalid object file name \

[llvm-branch-commits] [lld] e286335 - [lld-macho][nfc] Use split-file in order file test

2020-12-20 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-20T13:49:18-05:00
New Revision: e2863357de77d6980cbb5a17605b8928d96ddd20

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

LOG: [lld-macho][nfc] Use split-file in order file test

Makes it much easier to edit / maintain.

Reviewed By: #lld-macho, smeenai

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

Added: 


Modified: 
lld/test/MachO/order-file.s

Removed: 




diff  --git a/lld/test/MachO/order-file.s b/lld/test/MachO/order-file.s
index 24b7ce4c4ab6..673343935d3f 100644
--- a/lld/test/MachO/order-file.s
+++ b/lld/test/MachO/order-file.s
@@ -1,8 +1,7 @@
 # REQUIRES: x86
-# RUN: mkdir -p %t
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
-# RUN: echo ".globl _foo; .text; _foo: _bar: ret" | \
-# RUN:   llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/foo.o
+# RUN: rm -rf %t; split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
 # RUN: rm -f %t/foo.a
 # RUN: llvm-ar rcs %t/foo.a %t/foo.o
 
@@ -12,118 +11,149 @@
 # FOO-SECOND: <_main>:
 # FOO-SECOND: <_foo>:
 
-# RUN: echo "_foo # just a comment" > %t/ord-1
-# RUN: echo "_main # another comment" >> %t/ord-1
-# RUN: %lld -o %t/test-1 %t/test.o %t/foo.o -order_file %t/ord-1
+# RUN: %lld -lSystem -o %t/test-1 %t/test.o %t/foo.o -order_file %t/ord-1
 # RUN: llvm-objdump -d %t/test-1 | FileCheck %s --check-prefix=FOO-FIRST
 ## Output should be the same regardless of the command-line order of object 
files
-# RUN: %lld -o %t/test-1 %t/foo.o %t/test.o -order_file %t/ord-1
+# RUN: %lld -lSystem -o %t/test-1 %t/foo.o %t/test.o -order_file %t/ord-1
 # RUN: llvm-objdump -d %t/test-1 | FileCheck %s --check-prefix=FOO-FIRST
 
-# RUN: echo "_main # just a comment" > %t/ord-2
-# RUN: echo "_foo # another comment" >> %t/ord-2
-# RUN: %lld -o %t/test-2 %t/test.o %t/foo.o -order_file %t/ord-2
+# RUN: %lld -lSystem -o %t/test-2 %t/test.o %t/foo.o -order_file %t/ord-2
 # RUN: llvm-objdump -d %t/test-2 | FileCheck %s --check-prefix=FOO-SECOND
-# RUN: %lld -o %t/test-2 %t/foo.o %t/test.o -order_file %t/ord-2
+# RUN: %lld -lSystem -o %t/test-2 %t/foo.o %t/test.o -order_file %t/ord-2
 # RUN: llvm-objdump -d %t/test-2 | FileCheck %s --check-prefix=FOO-SECOND
 
-# RUN: echo "foo.o:_foo" > %t/ord-file-match
-# RUN: echo "_main" >> %t/ord-file-match
-# RUN: %lld -o %t/test-file-match %t/test.o %t/foo.o -order_file 
%t/ord-file-match
+# RUN: %lld -lSystem -o %t/test-file-match %t/test.o %t/foo.o -order_file 
%t/ord-file-match
 # RUN: llvm-objdump -d %t/test-file-match | FileCheck %s 
--check-prefix=FOO-FIRST
 ## Output should be the same regardless of the command-line order of object 
files
-# RUN: %lld -o %t/test-file-match %t/foo.o %t/test.o -order_file 
%t/ord-file-match
+# RUN: %lld -lSystem -o %t/test-file-match %t/foo.o %t/test.o -order_file 
%t/ord-file-match
 # RUN: llvm-objdump -d %t/test-file-match | FileCheck %s 
--check-prefix=FOO-FIRST
 
-# RUN: echo "bar.o:_foo" > %t/ord-file-nomatch
-# RUN: echo "_main" >> %t/ord-file-nomatch
-# RUN: echo "_foo" >> %t/ord-file-nomatch
-# RUN: %lld -o %t/test-file-nomatch %t/test.o %t/foo.o -order_file 
%t/ord-file-nomatch
+# RUN: %lld -lSystem -o %t/test-file-nomatch %t/test.o %t/foo.o -order_file 
%t/ord-file-nomatch
 # RUN: llvm-objdump -d %t/test-file-nomatch | FileCheck %s 
--check-prefix=FOO-SECOND
-# RUN: %lld -o %t/test-file-nomatch %t/foo.o %t/test.o -order_file 
%t/ord-file-nomatch
+# RUN: %lld -lSystem -o %t/test-file-nomatch %t/foo.o %t/test.o -order_file 
%t/ord-file-nomatch
 # RUN: llvm-objdump -d %t/test-file-nomatch | FileCheck %s 
--check-prefix=FOO-SECOND
 
-# RUN: echo "x86_64:_foo" > %t/ord-arch-match
-# RUN: echo "_main" >> %t/ord-arch-match
-# RUN: %lld -o %t/test-arch-match %t/test.o %t/foo.o -order_file 
%t/ord-arch-match
+# RUN: %lld -lSystem -o %t/test-arch-match %t/test.o %t/foo.o -order_file 
%t/ord-arch-match
 # RUN: llvm-objdump -d %t/test-arch-match | FileCheck %s 
--check-prefix=FOO-FIRST
-# RUN: %lld -o %t/test-arch-match %t/foo.o %t/test.o -order_file 
%t/ord-arch-match
+# RUN: %lld -lSystem -o %t/test-arch-match %t/foo.o %t/test.o -order_file 
%t/ord-arch-match
 # RUN: llvm-objdump -d %t/test-arch-match | FileCheck %s 
--check-prefix=FOO-FIRST
 
-# RUN: echo "ppc:_foo" > %t/ord-arch-nomatch
-# RUN: echo "_main" >> %t/ord-arch-nomatch
-# RUN: echo "_foo" >> %t/ord-arch-nomatch
-# RUN: %lld -o %t/test-arch-nomatch %t/test.o %t/foo.o -order_file 
%t/ord-arch-nomatch
+# RUN: %lld -lSystem -o %t/test-arch-nomatch %t/test.o %t/foo.o -order_file 
%t/ord-arch-nomatch
 # RUN: llvm-objdump -d %t/test-arch-nomatch | FileCheck %s 
--check-prefix=FOO-SECOND
-# RUN: %lld -o %t/test-arch-nomatch %t/

[llvm-branch-commits] [lld] 64e4757 - [lld-macho] Have order files support filtering by archive member paths

2020-12-20 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-20T13:49:18-05:00
New Revision: 64e47572002023d1ddd6bd12299ac12969af8665

URL: 
https://github.com/llvm/llvm-project/commit/64e47572002023d1ddd6bd12299ac12969af8665
DIFF: 
https://github.com/llvm/llvm-project/commit/64e47572002023d1ddd6bd12299ac12969af8665.diff

LOG: [lld-macho] Have order files support filtering by archive member paths

Also remove iteration over ArchiveFile symbols in buildInputSectionPriorities --
that was rendered unnecessary after D92539, which included ObjFiles from
ArchiveFiles inside the `inputFiles` vector.

Reviewed By: #lld-macho, smeenai

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

Added: 


Modified: 
lld/MachO/Writer.cpp
lld/test/MachO/order-file.s

Removed: 




diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index fbd03f80157d..c9d700658371 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -32,6 +32,7 @@
 
 using namespace llvm;
 using namespace llvm::MachO;
+using namespace llvm::sys;
 using namespace lld;
 using namespace lld::macho;
 
@@ -497,9 +498,16 @@ void Writer::createLoadCommands() {
 }
 
 static size_t getSymbolPriority(const SymbolPriorityEntry &entry,
-const InputFile &file) {
-  return 
std::max(entry.objectFiles.lookup(sys::path::filename(file.getName())),
-  entry.anyObjectFile);
+const InputFile *f) {
+  // We don't use toString(InputFile *) here because it returns the full path
+  // for object files, and we only want the basename.
+  StringRef filename;
+  if (f->archiveName.empty())
+filename = path::filename(f->getName());
+  else
+filename = saver.save(path::filename(f->archiveName) + "(" +
+  path::filename(f->getName()) + ")");
+  return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile);
 }
 
 // Each section gets assigned the priority of the highest-priority symbol it
@@ -517,12 +525,12 @@ static DenseMap 
buildInputSectionPriorities() {
 
 SymbolPriorityEntry &entry = it->second;
 size_t &priority = sectionPriorities[sym.isec];
-priority = std::max(priority, getSymbolPriority(entry, *sym.isec->file));
+priority = std::max(priority, getSymbolPriority(entry, sym.isec->file));
   };
 
   // TODO: Make sure this handles weak symbols correctly.
   for (InputFile *file : inputFiles)
-if (isa(file) || isa(file))
+if (isa(file))
   for (lld::macho::Symbol *sym : file->symbols)
 if (auto *d = dyn_cast(sym))
   addSym(*d);

diff  --git a/lld/test/MachO/order-file.s b/lld/test/MachO/order-file.s
index a13abe73efb9..83e6840c9786 100644
--- a/lld/test/MachO/order-file.s
+++ b/lld/test/MachO/order-file.s
@@ -55,6 +55,16 @@
 # RUN: %lld -lSystem -o %t/test-archive-1 %t/foo.a %t/test.o -order_file 
%t/ord-1
 # RUN: llvm-objdump -d %t/test-archive-1 | FileCheck %s 
--check-prefix=FOO-FIRST
 
+# RUN: %lld -lSystem -o %t/test-archive-file-no-match %t/test.o %t/foo.a 
-order_file %t/ord-file-match
+# RUN: llvm-objdump -d %t/test-archive-file-no-match | FileCheck %s 
--check-prefix=FOO-SECOND
+# RUN: %lld -lSystem -o %t/test-archive %t/foo.a %t/test.o -order_file 
%t/ord-file-match
+# RUN: llvm-objdump -d %t/test-archive-file-no-match | FileCheck %s 
--check-prefix=FOO-SECOND
+
+# RUN: %lld -lSystem -o %t/test-archive-1 %t/test.o %t/foo.a -order_file 
%t/ord-archive-match
+# RUN: llvm-objdump -d %t/test-archive-1 | FileCheck %s 
--check-prefix=FOO-FIRST
+# RUN: %lld -lSystem -o %t/test-archive-1 %t/foo.a %t/test.o -order_file 
%t/ord-archive-match
+# RUN: llvm-objdump -d %t/test-archive-1 | FileCheck %s 
--check-prefix=FOO-FIRST
+
 # RUN: %lld -lSystem -o %t/test-archive-file-no-match %t/test.o %t/foo.a 
-order_file %t/ord-file-nomatch
 # RUN: llvm-objdump -d %t/test-archive-file-no-match | FileCheck %s 
--check-prefix=FOO-SECOND
 # RUN: %lld -lSystem -o %t/test-archive %t/foo.a %t/test.o -order_file 
%t/ord-file-nomatch
@@ -104,6 +114,10 @@ _main # just a comment
 foo.o:-[Foo doFoo:andBar:]
 _main
 
+#--- ord-archive-match
+foo.a(foo.o):-[Foo doFoo:andBar:]
+_main
+
 #--- ord-file-nomatch
 bar.o:-[Foo doFoo:andBar:]
 _main



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] a817594 - [lld-macho][nfc] Clean up tests

2020-12-21 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-21T14:44:08-05:00
New Revision: a817594de9269b9ac8055a2ff2a22ab824cf143d

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

LOG: [lld-macho][nfc] Clean up tests

* Migrate most of our tests to use `split-file` instead of `echo`
* Remove individual `rm -f %t/libfoo.a` commands in favor of a top-level `rm 
-rf %t`
* Remove unused `Inputs/libfunction.s`

Reviewed By: #lld-macho, compnerd

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

Added: 


Modified: 
lld/test/MachO/archive.s
lld/test/MachO/common-symbol-resolution.s
lld/test/MachO/filelist.s
lld/test/MachO/force-load.s
lld/test/MachO/framework.s
lld/test/MachO/invalid/archive-no-index.s
lld/test/MachO/invalid/bad-archive-member.s
lld/test/MachO/lto-archive.ll
lld/test/MachO/objc.s
lld/test/MachO/order-file.s
lld/test/MachO/resolution.s
lld/test/MachO/section-merge.s
lld/test/MachO/stabs.s
lld/test/MachO/subsections-section-relocs.s
lld/test/MachO/subsections-symbol-relocs.s
lld/test/MachO/symbol-order.s
lld/test/MachO/weak-definition-direct-fetch.s
lld/test/MachO/weak-definition-indirect-fetch.s
lld/test/MachO/weak-definition-order.s
lld/test/MachO/weak-definition-over-dysym.s

Removed: 
lld/test/MachO/Inputs/libfunction.s



diff  --git a/lld/test/MachO/Inputs/libfunction.s 
b/lld/test/MachO/Inputs/libfunction.s
deleted file mode 100644
index fe0b3879a41a..
--- a/lld/test/MachO/Inputs/libfunction.s
+++ /dev/null
@@ -1,6 +0,0 @@
-.section __TEXT,__text
-.globl _some_function
-
-_some_function:
-  mov $1, %rax
-  ret

diff  --git a/lld/test/MachO/archive.s b/lld/test/MachO/archive.s
index abde623b07f4..2ac2d302b88d 100644
--- a/lld/test/MachO/archive.s
+++ b/lld/test/MachO/archive.s
@@ -1,11 +1,10 @@
 # REQUIRES: x86
-# RUN: mkdir -p %t
-# RUN: echo ".global _boo; _boo: ret"   | llvm-mc 
-filetype=obj -triple=x86_64-apple-darwin -o %t/2.o
-# RUN: echo ".global _bar; _bar: ret"   | llvm-mc 
-filetype=obj -triple=x86_64-apple-darwin -o %t/3.o
-# RUN: echo ".global _undefined; .global _unused; _unused: ret" | llvm-mc 
-filetype=obj -triple=x86_64-apple-darwin -o %t/4.o
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/main.o
+# RUN: rm -rf %t; split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/2.s -o %t/2.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/3.s -o %t/3.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/4.s -o %t/4.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o
 
-# RUN: rm -f %t/test.a
 # RUN: llvm-ar rcs %t/test.a %t/2.o %t/3.o %t/4.o
 # RUN: %lld %t/main.o %t/test.a -o %t/test.out
 
@@ -33,9 +32,24 @@
 # ALL-LOAD: T _main
 # ALL-LOAD: T _unused
 
-.global _main
+#--- 2.s
+.globl _boo
+_boo:
+  ret
+
+#--- 3.s
+.globl _bar
+_bar:
+  ret
+
+#--- 4.s
+.globl _undefined, _unused
+_unused:
+  ret
+
+#--- main.s
+.globl _main
 _main:
   callq _boo
   callq _bar
-  mov $0, %rax
   ret

diff  --git a/lld/test/MachO/common-symbol-resolution.s 
b/lld/test/MachO/common-symbol-resolution.s
index 2a88ef51e460..1dc015816da4 100644
--- a/lld/test/MachO/common-symbol-resolution.s
+++ b/lld/test/MachO/common-symbol-resolution.s
@@ -1,5 +1,5 @@
 # REQUIRES: x86
-# RUN: split-file %s %t
+# RUN: rm -rf %t; split-file %s %t
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/common.s -o 
%t/common.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak-common.s -o 
%t/weak-common.o
@@ -11,7 +11,6 @@
 
 # RUN: %lld -lSystem -order_file %t/order -dylib %t/libfoo.o -o %t/libfoo.dylib
 
-# RUN: rm -f %t/defined.a %t/weak-defined-and-common.a
 # RUN: llvm-ar rcs %t/defined.a %t/defined.o
 # RUN: llvm-ar rcs %t/weak-defined-and-common.a %t/weak-defined.o %t/common.o
 

diff  --git a/lld/test/MachO/filelist.s b/lld/test/MachO/filelist.s
index 32be332aa507..3d4846c4505a 100644
--- a/lld/test/MachO/filelist.s
+++ b/lld/test/MachO/filelist.s
@@ -3,10 +3,10 @@
 ## This test verifies that the paths in -filelist get processed in command-line
 ## order.
 
-# RUN: mkdir -p %t
-# RUN: echo ".globl _foo; .weak_definition _foo; .section __TEXT,first; _foo:" 
| llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/first.o
-# RUN: echo ".globl _foo; .weak_definition _foo; .section __TEXT,second; 
_foo:" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/second.o
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
+# RUN: rm -rf %t; split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/first.s -o 
%t/first.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/second.s -o 
%t/second.o
+# RUN: llvm-

[llvm-branch-commits] [lld] 0f8224c - [lld-macho][nfc] Remove %T from headerpad.s

2020-12-21 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-21T14:44:08-05:00
New Revision: 0f8224c2104b7246b36b9f92ffe87aad4d8dd3ac

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

LOG: [lld-macho][nfc] Remove %T from headerpad.s

The llvm-lit docs indicate that it is deprecated.

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/test/MachO/headerpad.s

Removed: 




diff  --git a/lld/test/MachO/headerpad.s b/lld/test/MachO/headerpad.s
index 0f4f19ce9d62..750919438cb0 100644
--- a/lld/test/MachO/headerpad.s
+++ b/lld/test/MachO/headerpad.s
@@ -8,10 +8,12 @@
 ## just enforces a lower bound. We should consider implementing the same
 ## alignment behavior.
 
+# RUN: rm -rf %t; mkdir -p %t
+
  Check default behavior
-# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
-# RUN: %lld -o %t %t.o
-# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PADx
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/test.o
+# RUN: %lld -o %t/test %t/test.o
+# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s 
--check-prefix=PADx
 #
 # PADx:  magic{{.+}}  ncmds  sizeofcmds flags
 # PADx-NEXT: MH_MAGIC_64  {{.+}}  [[#]]  [[#%u, CMDSIZE:]]  {{.*}}
@@ -22,10 +24,10 @@
 # PADx-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x20]]
 
  Zero pad, no LCDylibs
-# RUN: %lld -o %t %t.o -headerpad 0
-# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD0
-# RUN: %lld -o %t %t.o -headerpad 0 -headerpad_max_install_names
-# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s --check-prefix=PAD0
+# RUN: %lld -o %t/test %t/test.o -headerpad 0
+# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s 
--check-prefix=PAD0
+# RUN: %lld -o %t/test %t/test.o -headerpad 0 -headerpad_max_install_names
+# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s 
--check-prefix=PAD0
 #
 # PAD0:  magic{{.+}}  ncmds  sizeofcmds flags
 # PAD0-NEXT: MH_MAGIC_64  {{.+}}  [[#]]  [[#%u, CMDSIZE:]]  {{.*}}
@@ -36,12 +38,12 @@
 # PAD0-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0]]
 
  Each lexical form of a hex number, no LCDylibs
-# RUN: %lld -o %t %t.o -headerpad 11
-# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s 
--check-prefix=PAD11
-# RUN: %lld -o %t %t.o -headerpad 0x11
-# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s 
--check-prefix=PAD11
-# RUN: %lld -o %t %t.o -headerpad 0X11 -headerpad_max_install_names
-# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s 
--check-prefix=PAD11
+# RUN: %lld -o %t/test %t/test.o -headerpad 11
+# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s 
--check-prefix=PAD11
+# RUN: %lld -o %t/test %t/test.o -headerpad 0x11
+# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s 
--check-prefix=PAD11
+# RUN: %lld -o %t/test %t/test.o -headerpad 0X11 -headerpad_max_install_names
+# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s 
--check-prefix=PAD11
 #
 # PAD11:  magic{{.+}}  ncmds  sizeofcmds flags
 # PAD11-NEXT: MH_MAGIC_64  {{.+}}  [[#]]  [[#%u, CMDSIZE:]]  {{.*}}
@@ -52,17 +54,17 @@
 # PAD11-NEXT: offset [[#%u, CMDSIZE + 0x20 + 0x11]]
 
  Each & all 3 kinds of LCDylib
-# RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %T/null.o
-# RUN: %lld -o %T/libnull.dylib %T/null.o -dylib \
+# RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/null.o
+# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
 # RUN: -headerpad_max_install_names
-# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s 
--check-prefix=PADMAX
-# RUN: %lld -o %T/libnull.dylib %T/null.o -dylib \
+# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s 
--check-prefix=PADMAX
+# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
 # RUN: -headerpad_max_install_names -lSystem
-# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s 
--check-prefix=PADMAX
-# RUN: %lld -o %T/libnull.dylib %T/null.o -dylib \
+# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s 
--check-prefix=PADMAX
+# RUN: %lld -o %t/libnull.dylib %t/null.o -dylib \
 # RUN: -headerpad_max_install_names \
 # RUN: -lSystem -sub_library libSystem
-# RUN: llvm-objdump --macho --all-headers %T/libnull.dylib | FileCheck %s 
--check-prefix=PADMAX
+# RUN: llvm-objdump --macho --all-headers %t/libnull.dylib | FileCheck %s 
--check-prefix=PADMAX
 #
 # PADMAX:  magic{{.+}}  ncmdssizeofcmds flags
 # PADMAX-NEXT: MH_MAGIC_64  {{.+}}  [[#%u, N:]]  [[#%u, CMDSIZE:]]  {{.*}}
@@ -73,10 +75,10 @@
 # PADMAX-NEXT: offset

[llvm-branch-commits] [lld] 9d1140e - [lld-macho] Simulator & DriverKit executables should always be PIE

2020-12-23 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-23T11:24:12-05:00
New Revision: 9d1140e18e6f662636ac715b7dca202a969e9845

URL: 
https://github.com/llvm/llvm-project/commit/9d1140e18e6f662636ac715b7dca202a969e9845
DIFF: 
https://github.com/llvm/llvm-project/commit/9d1140e18e6f662636ac715b7dca202a969e9845.diff

LOG: [lld-macho] Simulator & DriverKit executables should always be PIE

We didn't have support for parsing DriverKit in our `-platform`
flag, so add that too. Also remove a bunch of unnecessary namespace
prefixes.

Reviewed By: #lld-macho, thakis

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/test/MachO/platform-version.s
lld/test/MachO/x86-64-reloc-unsigned.s

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index edc9fe001ab5..9780443fb85a 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -548,20 +548,19 @@ static void handlePlatformVersion(const opt::Arg *arg) {
 
   // TODO(compnerd) see if we can generate this case list via XMACROS
   config->platform.kind =
-  llvm::StringSwitch(lowerDash(platformStr))
-  .Cases("macos", "1", llvm::MachO::PlatformKind::macOS)
-  .Cases("ios", "2", llvm::MachO::PlatformKind::iOS)
-  .Cases("tvos", "3", llvm::MachO::PlatformKind::tvOS)
-  .Cases("watchos", "4", llvm::MachO::PlatformKind::watchOS)
-  .Cases("bridgeos", "5", llvm::MachO::PlatformKind::bridgeOS)
-  .Cases("mac-catalyst", "6", llvm::MachO::PlatformKind::macCatalyst)
-  .Cases("ios-simulator", "7", llvm::MachO::PlatformKind::iOSSimulator)
-  .Cases("tvos-simulator", "8",
- llvm::MachO::PlatformKind::tvOSSimulator)
-  .Cases("watchos-simulator", "9",
- llvm::MachO::PlatformKind::watchOSSimulator)
-  .Default(llvm::MachO::PlatformKind::unknown);
-  if (config->platform.kind == llvm::MachO::PlatformKind::unknown)
+  StringSwitch(lowerDash(platformStr))
+  .Cases("macos", "1", PlatformKind::macOS)
+  .Cases("ios", "2", PlatformKind::iOS)
+  .Cases("tvos", "3", PlatformKind::tvOS)
+  .Cases("watchos", "4", PlatformKind::watchOS)
+  .Cases("bridgeos", "5", PlatformKind::bridgeOS)
+  .Cases("mac-catalyst", "6", PlatformKind::macCatalyst)
+  .Cases("ios-simulator", "7", PlatformKind::iOSSimulator)
+  .Cases("tvos-simulator", "8", PlatformKind::tvOSSimulator)
+  .Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator)
+  .Cases("driverkit", "10", PlatformKind::driverKit)
+  .Default(PlatformKind::unknown);
+  if (config->platform.kind == PlatformKind::unknown)
 error(Twine("malformed platform: ") + platformStr);
   // TODO: check validity of version strings, which varies by platform
   // NOTE: ld64 accepts version strings with 5 components
@@ -637,10 +636,14 @@ static bool isPie(opt::InputArgList &args) {
   // to PIE from 10.7, arm64 should always be PIE, etc
   assert(config->arch == AK_x86_64 || config->arch == AK_x86_64h);
 
-  if (config->platform.kind == MachO::PlatformKind::macOS &&
+  PlatformKind kind = config->platform.kind;
+  if (kind == PlatformKind::macOS &&
   config->platform.minimum >= VersionTuple(10, 6))
 return true;
 
+  if (kind == PlatformKind::iOSSimulator || kind == PlatformKind::driverKit)
+return true;
+
   return args.hasArg(OPT_pie);
 }
 

diff  --git a/lld/test/MachO/platform-version.s 
b/lld/test/MachO/platform-version.s
index 326a74428cf4..0bfc46930c5b 100644
--- a/lld/test/MachO/platform-version.s
+++ b/lld/test/MachO/platform-version.s
@@ -55,7 +55,7 @@
 # RUN:-platform_version 0 1 5 \
 # RUN: | FileCheck --check-prefix=FAIL-PLATFORM %s
 # RUN: not %lld -o %t %t.o 2>&1 \
-# RUN:-platform_version 10 1 5 \
+# RUN:-platform_version 11 1 5 \
 # RUN: | FileCheck --check-prefix=FAIL-PLATFORM %s
 # FAIL-PLATFORM: malformed platform: {{.*}}
 # FAIL-PLATFORM-NOT: malformed {{minimum|sdk}} version: {{.*}}

diff  --git a/lld/test/MachO/x86-64-reloc-unsigned.s 
b/lld/test/MachO/x86-64-reloc-unsigned.s
index f1afc0cb7043..5fdbdf1f0627 100644
--- a/lld/test/MachO/x86-64-reloc-unsigned.s
+++ b/lld/test/MachO/x86-64-reloc-unsigned.s
@@ -14,6 +14,10 @@
 # RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
 # RUN: %lld -platform_version macos 10.5.0 11.0 -o %t-no-pie %t.o
 # RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s 
--check-prefix=NO-PIE
+# RUN: %lld -platform_version ios-simulator 11.0.0 14.2 -o %t-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
+# RUN: %lld -platform_version driverkit 19.0 20.0 -o %t-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
 
 # CHECK:   Contents of section __DATA,foo:
 # CHECK-NEXT:  11000 0810 0100

[llvm-branch-commits] [lld] 6b3eecd - [lld-macho] Extend PIE option handling

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T14:35:51-08:00
New Revision: 6b3eecd22ab2afd16be412d19eed0e01f10e6cc8

URL: 
https://github.com/llvm/llvm-project/commit/6b3eecd22ab2afd16be412d19eed0e01f10e6cc8
DIFF: 
https://github.com/llvm/llvm-project/commit/6b3eecd22ab2afd16be412d19eed0e01f10e6cc8.diff

LOG: [lld-macho] Extend PIE option handling

* Enable PIE by default if targeting 10.6 or above on x86-64. (The
  manpage says 10.7, but that actually applies only to i386, and in
  general varies based on the target platform. I didn't update the
  manpage because listing all the different behaviors would make for a
  pretty long description.)
* Add support for `-no_pie`
* Remove `HelpHidden` from `-pie`

Reviewed By: thakis

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/MachO/Options.td
lld/test/MachO/x86-64-reloc-unsigned.s

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 58a41c2b3af1..141082985b4e 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -555,6 +555,21 @@ static const char *getReproduceOption(opt::InputArgList 
&args) {
   return getenv("LLD_REPRODUCE");
 }
 
+static bool isPie(opt::InputArgList &args) {
+  if (config->outputType != MH_EXECUTE || args.hasArg(OPT_no_pie))
+return false;
+
+  // TODO: add logic here as we support more archs. E.g. i386 should default
+  // to PIE from 10.7, arm64 should always be PIE, etc
+  assert(config->arch == AK_x86_64 || config->arch == AK_x86_64h);
+
+  if (config->platform.kind == MachO::PlatformKind::macOS &&
+  config->platform.minimum >= VersionTuple(10, 6))
+return true;
+
+  return args.hasArg(OPT_pie);
+}
+
 bool macho::link(llvm::ArrayRef argsArr, bool canExitEarly,
  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
   lld::stdoutOS = &stdoutOS;
@@ -690,8 +705,7 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
   }
 
   config->isPic = config->outputType == MH_DYLIB ||
-  config->outputType == MH_BUNDLE ||
-  (config->outputType == MH_EXECUTE && args.hasArg(OPT_pie));
+  config->outputType == MH_BUNDLE || isPie(args);
 
   // Now that all dylibs have been loaded, search for those that should be
   // re-exported.

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index de7cbf7faf3e..399928e8d9ae 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -315,11 +315,9 @@ def grp_main : OptionGroup<"main">, HelpText<"MAIN 
EXECUTABLE">;
 
 def pie : Flag<["-"], "pie">,
  HelpText<"Build a position independent executable (default for macOS 10.7 
and later)">,
- Flags<[HelpHidden]>,
  Group;
 def no_pie : Flag<["-"], "no_pie">,
  HelpText<"Do not build a position independent executable (default for 
macOS 10.6 and earlier)">,
- Flags<[HelpHidden]>,
  Group;
 def pagezero_size : Separate<["-"], "pagezero_size">,
  MetaVarName<"">,

diff  --git a/lld/test/MachO/x86-64-reloc-unsigned.s 
b/lld/test/MachO/x86-64-reloc-unsigned.s
index e3608f318d22..c6e5eb665739 100644
--- a/lld/test/MachO/x86-64-reloc-unsigned.s
+++ b/lld/test/MachO/x86-64-reloc-unsigned.s
@@ -3,8 +3,17 @@
 # RUN: %lld -o %t %t.o
 # RUN: llvm-objdump --macho --rebase --full-contents %t | FileCheck %s
 
-# RUN: %lld -pie -o %t-pie %t.o
+# RUN: %lld -fatal_warnings -pie -o %t-pie %t.o
 # RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
+# RUN: %lld -fatal_warnings -pie -no_pie -o %t-no-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s 
--check-prefix=NO-PIE
+# RUN: %lld -fatal_warnings -no_pie -pie -o %t-no-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s 
--check-prefix=NO-PIE
+
+# RUN: %lld -platform_version macos 10.6.0 11.0 -o %t-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
+# RUN: %lld -platform_version macos 10.5.0 11.0 -o %t-no-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s 
--check-prefix=NO-PIE
 
 # CHECK:   Contents of section __DATA,foo:
 # CHECK-NEXT:  11000 0810 0100
@@ -21,6 +30,10 @@
 # PIE-DAG:  __DATA   bar0x[[#ADDR + 12]]  pointer
 # PIE-DAG:  __DATA   baz0x[[#ADDR + 20]]  pointer
 
+# NO-PIE:  Rebase table:
+# NO-PIE-NEXT: segment  sectionaddress   type
+# NO-PIE-EMPTY:
+
 .globl _main, _foo, _bar
 
 .section __DATA,foo



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 3fcb0ee - [lld-macho] Emit STABS symbols for debugging, and drop debug sections

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T15:05:20-08:00
New Revision: 3fcb0eeb152beb4320c7632bcfa2b1e7c2e5ca00

URL: 
https://github.com/llvm/llvm-project/commit/3fcb0eeb152beb4320c7632bcfa2b1e7c2e5ca00
DIFF: 
https://github.com/llvm/llvm-project/commit/3fcb0eeb152beb4320c7632bcfa2b1e7c2e5ca00.diff

LOG: [lld-macho] Emit STABS symbols for debugging, and drop debug sections

Debug sections contain a large amount of data. In order not to bloat the size
of the final binary, we remove them and instead emit STABS symbols for
`dsymutil` and the debugger to locate their contents in the object files.

With this diff, `dsymutil` is able to locate the debug info. However, we need
a few more features before `lldb` is able to work well with our binaries --
e.g. having `LC_DYSYMTAB` accurately reflect the number of local symbols,
emitting `LC_UUID`, and more. Those will be handled in follow-up diffs.

Note also that the STABS we emit differ slightly from what ld64 does. First, we
emit the path to the source file as one `N_SO` symbol instead of two. (`ld64`
emits one `N_SO` for the dirname and one of the basename.) Second, we do not
emit `N_BNSYM` and `N_ENSYM` STABS to mark the start and end of functions,
because the `N_FUN` STABS already serve that purpose. @clayborg recommended
these changes based on his knowledge of what the debugging tools look for.

Additionally, this current implementation doesn't accurately reflect the size
of function symbols. It uses the size of their containing sectioins as a proxy,
but that is only accurate if `.subsections_with_symbols` is set, and if there
isn't an `N_ALT_ENTRY` in that particular subsection. I think we have two
options to solve this:

1. We can split up subsections by symbol even if `.subsections_with_symbols`
   is not set, but include constraints to ensure those subsections retain
   their order in the final output. This is `ld64`'s approach.
2. We could just add a `size` field to our `Symbol` class. This seems simpler,
   and I'm more inclined toward it, but I'm not sure if there are use cases
   that it doesn't handle well. As such I'm punting on the decision for now.

Reviewed By: clayborg

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

Added: 
lld/MachO/Dwarf.cpp
lld/MachO/Dwarf.h
lld/test/MachO/stabs.s

Modified: 
lld/MachO/CMakeLists.txt
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/InputSection.h
lld/MachO/OutputSegment.h
lld/MachO/SyntheticSections.cpp
lld/MachO/SyntheticSections.h
lld/MachO/Writer.cpp

Removed: 




diff  --git a/lld/MachO/CMakeLists.txt b/lld/MachO/CMakeLists.txt
index 6ddc88fb8618..6a8b5d336583 100644
--- a/lld/MachO/CMakeLists.txt
+++ b/lld/MachO/CMakeLists.txt
@@ -9,6 +9,7 @@ add_lld_library(lldMachO2
   UnwindInfoSection.cpp
   Driver.cpp
   DriverUtils.cpp
+  Dwarf.cpp
   ExportTrie.cpp
   InputFiles.cpp
   InputSection.cpp

diff  --git a/lld/MachO/Dwarf.cpp b/lld/MachO/Dwarf.cpp
new file mode 100644
index ..121f54fb1f79
--- /dev/null
+++ b/lld/MachO/Dwarf.cpp
@@ -0,0 +1,49 @@
+//===- DWARF.cpp 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Dwarf.h"
+#include "InputFiles.h"
+#include "InputSection.h"
+#include "OutputSegment.h"
+
+#include 
+
+using namespace lld;
+using namespace lld::macho;
+using namespace llvm;
+
+std::unique_ptr DwarfObject::create(ObjFile *obj) {
+  auto dObj = std::make_unique();
+  bool hasDwarfInfo = false;
+  for (SubsectionMap subsecMap : obj->subsections) {
+for (auto it : subsecMap) {
+  InputSection *isec = it.second;
+  if (!(isDebugSection(isec->flags) &&
+isec->segname == segment_names::dwarf))
+continue;
+
+  if (isec->name == "__debug_info") {
+dObj->infoSection.Data = toStringRef(isec->data);
+hasDwarfInfo = true;
+continue;
+  }
+
+  if (StringRef *s = StringSwitch(isec->name)
+ .Case("__debug_abbrev", &dObj->abbrevSection)
+ .Case("__debug_str", &dObj->strSection)
+ .Default(nullptr)) {
+*s = toStringRef(isec->data);
+hasDwarfInfo = true;
+  }
+}
+  }
+
+  if (hasDwarfInfo)
+return dObj;
+  return nullptr;
+}

diff  --git a/lld/MachO/Dwarf.h b/lld/MachO/Dwarf.h
new file mode 100644
index ..119f2778fc6b
--- /dev/null
+++ b/lld/MachO/Dwarf.h
@@ -0,0 +1,53 @@
+//===- DWARF.h ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.or

[llvm-branch-commits] [lld] 51629ab - [lld-macho] Emit local symbols in symtab; record metadata in LC_DYSYMTAB

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T15:05:20-08:00
New Revision: 51629abce0e2f9d1376eb0b5070532a2bbec6766

URL: 
https://github.com/llvm/llvm-project/commit/51629abce0e2f9d1376eb0b5070532a2bbec6766
DIFF: 
https://github.com/llvm/llvm-project/commit/51629abce0e2f9d1376eb0b5070532a2bbec6766.diff

LOG: [lld-macho] Emit local symbols in symtab; record metadata in LC_DYSYMTAB

Symbols of the same type must be laid out contiguously: following ld64's
lead, we choose to emit all local symbols first, then external symbols,
and finally undefined symbols. For each symbol type, the LC_DYSYMTAB
load command will record the range (start index and total number) of
those symbols in the symbol table.

This work was motivated by the fact that LLDB won't search for debug
info if LC_DYSYMTAB says there are no local symbols (since STABS symbols
are all local symbols). With this change, LLDB is now able to display
the source lines at a given breakpoint when debugging our binaries.

Some tests had to be updated due to local symbol names now appearing in
`llvm-objdump`'s output.

Reviewed By: #lld-macho, smeenai, clayborg

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

Added: 


Modified: 
lld/MachO/SyntheticSections.cpp
lld/MachO/SyntheticSections.h
lld/MachO/Writer.cpp
lld/test/MachO/stabs.s
lld/test/MachO/subsections-symbol-relocs.s
lld/test/MachO/symtab.s
lld/test/MachO/tlv.s

Removed: 




diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index feaa4b5d3e22..56a095c4a7e4 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -19,6 +19,7 @@
 
 #include "lld/Common/ErrorHandler.h"
 #include "lld/Common/Memory.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/EndianStream.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/LEB128.h"
@@ -630,14 +631,35 @@ void SymtabSection::emitFunStabs(Defined *defined) {
 }
 
 void SymtabSection::finalizeContents() {
-  InputFile *lastFile = nullptr;
+  // Local symbols aren't in the SymbolTable, so we walk the list of object
+  // files to gather them.
+  for (InputFile *file : inputFiles) {
+if (auto *objFile = dyn_cast(file)) {
+  for (Symbol *sym : objFile->symbols) {
+// TODO: when we implement -dead_strip, we should filter out symbols
+// that belong to dead sections.
+if (auto *defined = dyn_cast(sym)) {
+  if (!defined->isExternal()) {
+uint32_t strx = stringTableSection.addString(sym->getName());
+localSymbols.push_back({sym, strx});
+  }
+}
+  }
+}
+  }
+
   for (Symbol *sym : symtab->getSymbols()) {
-// TODO support other symbol types
-if (isa(sym) || sym->isInGot() || sym->isInStubs()) {
-  sym->symtabIndex = symbols.size();
-  symbols.push_back({sym, stringTableSection.addString(sym->getName())});
+uint32_t strx = stringTableSection.addString(sym->getName());
+if (auto *defined = dyn_cast(sym)) {
+  assert(defined->isExternal());
+  externalSymbols.push_back({sym, strx});
+} else if (sym->isInGot() || sym->isInStubs()) {
+  undefinedSymbols.push_back({sym, strx});
 }
+  }
 
+  InputFile *lastFile = nullptr;
+  for (Symbol *sym : symtab->getSymbols()) {
 // Emit STABS symbols so that dsymutil and/or the debugger can map address
 // regions in the final binary to the source and object files from which
 // they originated.
@@ -670,13 +692,35 @@ void SymtabSection::finalizeContents() {
 
   if (!stabs.empty())
 emitEndSourceStab();
+
+  uint32_t symtabIndex = stabs.size();
+  for (const SymtabEntry &entry :
+   concat(localSymbols, externalSymbols, undefinedSymbols)) {
+entry.sym->symtabIndex = symtabIndex++;
+  }
+}
+
+uint32_t SymtabSection::getNumSymbols() const {
+  return stabs.size() + localSymbols.size() + externalSymbols.size() +
+ undefinedSymbols.size();
 }
 
 void SymtabSection::writeTo(uint8_t *buf) const {
   auto *nList = reinterpret_cast(buf);
-  for (const SymtabEntry &entry : symbols) {
+  // Emit the stabs entries before the "real" symbols. We cannot emit them
+  // after as that would render Symbol::symtabIndex inaccurate.
+  for (const StabsEntry &entry : stabs) {
+nList->n_strx = entry.strx;
+nList->n_type = entry.type;
+nList->n_sect = entry.sect;
+nList->n_desc = entry.desc;
+nList->n_value = entry.value;
+++nList;
+  }
+
+  for (const SymtabEntry &entry : concat(
+   localSymbols, externalSymbols, undefinedSymbols)) {
 nList->n_strx = entry.strx;
-// TODO support other symbol types
 // TODO populate n_desc with more flags
 if (auto *defined = dyn_cast(entry.sym)) {
   if (defined->isAbsolute()) {
@@ -684,7 +728,8 @@ void SymtabSection::writeTo(uint8_t *buf) const {
 nList->n_sect = MachO::NO_SECT;
 nList->n_value = defined->value;

[llvm-branch-commits] [lld] d0c4be4 - [lld-macho] Emit empty string as first entry of string table

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T15:05:20-08:00
New Revision: d0c4be42e35d8cff069f91a45b76ea24187c233d

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

LOG: [lld-macho] Emit empty string as first entry of string table

ld64 emits string tables which start with a space and a zero byte. We
match its behavior here since some tools depend on it.

Similar rationale as {D89561}.

Reviewed By: #lld-macho, smeenai

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

Added: 


Modified: 
lld/MachO/SyntheticSections.h
lld/test/MachO/symtab.s

Removed: 




diff  --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index e52bc480e9aa..56f4bf66ce23 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -398,11 +398,10 @@ class StringTableSection : public LinkEditSection {
   void writeTo(uint8_t *buf) const override;
 
 private:
-  // An n_strx value of 0 always indicates the empty string, so we must locate
-  // our non-empty string values at positive offsets in the string table.
-  // Therefore we insert a dummy value at position zero.
-  std::vector strings{"\0"};
-  size_t size = 1;
+  // ld64 emits string tables which start with a space and a zero byte. We
+  // match its behavior here since some tools depend on it.
+  std::vector strings{" "};
+  size_t size = 2;
 };
 
 struct SymtabEntry {

diff  --git a/lld/test/MachO/symtab.s b/lld/test/MachO/symtab.s
index 22dc80ad62f3..a4dce665acb4 100644
--- a/lld/test/MachO/symtab.s
+++ b/lld/test/MachO/symtab.s
@@ -4,11 +4,11 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o 
%t/libfoo.o
 # RUN: %lld -dylib %t/libfoo.o -o %t/libfoo.dylib
 # RUN: %lld -lSystem %t/test.o %t/libfoo.dylib -o %t/test
-# RUN: llvm-readobj --syms --macho-dysymtab %t/test | FileCheck %s
 
+# RUN: llvm-readobj --syms --macho-dysymtab %t/test | FileCheck %s
 # CHECK:  Symbols [
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: _local (1)
+# CHECK-NEXT: Name: _local (2)
 # CHECK-NEXT: Type: Section (0xE)
 # CHECK-NEXT: Section: __data (0x4)
 # CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
@@ -17,7 +17,7 @@
 # CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: _main (8)
+# CHECK-NEXT: Name: _main (9)
 # CHECK-NEXT: Extern
 # CHECK-NEXT: Type: Section (0xE)
 # CHECK-NEXT: Section: __text (0x1)
@@ -27,7 +27,7 @@
 # CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: _external (54)
+# CHECK-NEXT: Name: _external (55)
 # CHECK-NEXT: Extern
 # CHECK-NEXT: Type: Section (0xE)
 # CHECK-NEXT: Section: __data (0x4)
@@ -37,7 +37,7 @@
 # CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: _external_weak (64)
+# CHECK-NEXT: Name: _external_weak (65)
 # CHECK-NEXT: Extern
 # CHECK-NEXT: Type: Section (0xE)
 # CHECK-NEXT: Section: __text (0x1)
@@ -48,7 +48,7 @@
 # CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: __dyld_private (102)
+# CHECK-NEXT: Name: __dyld_private (103)
 # CHECK-NEXT: Extern
 # CHECK-NEXT: Type: Section (0xE)
 # CHECK-NEXT: Section: __data (0x4)
@@ -58,7 +58,7 @@
 # CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: dyld_stub_binder (14)
+# CHECK-NEXT: Name: dyld_stub_binder (15)
 # CHECK-NEXT: Type: Undef (0x0)
 # CHECK-NEXT: Section:  (0x0)
 # CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
@@ -67,7 +67,7 @@
 # CHECK-NEXT: Value: 0x0
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
-# CHECK-NEXT: Name: _dynamic (79)
+# CHECK-NEXT: Name: _dynamic (80)
 # CHECK-NEXT: Type: Undef (0x0)
 # CHECK-NEXT: Section:  (0x0)
 # CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
@@ -84,6 +84,11 @@
 # CHECK-NEXT:   iundefsym: 5
 # CHECK-NEXT:   nundefsym: 2
 
+## Verify that the first entry in the StringTable is a space.
+# RUN: obj2yaml %t/test | FileCheck %s --check-prefix=YAML
+# YAML:  StringTable:
+# YAML-NEXT: ' '
+
 #--- libfoo.s
 .globl _dynamic
 _dynamic:



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] c7dbaec - [lld-macho] Add isCodeSection()

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T15:05:21-08:00
New Revision: c7dbaec396ef98b8bc6acb7631d2919449986add

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

LOG: [lld-macho] Add isCodeSection()

This is the same logic that ld64 uses to determine which sections
contain functions. This was added so that we could determine which
STABS entries should be N_FUN.

Reviewed By: clayborg

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

Added: 


Modified: 
lld/MachO/InputSection.cpp
lld/MachO/InputSection.h
lld/MachO/SyntheticSections.cpp
lld/test/MachO/stabs.s

Removed: 




diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index e164630159e2..80c263301f41 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -58,6 +58,23 @@ void InputSection::writeTo(uint8_t *buf) {
   }
 }
 
+bool macho::isCodeSection(InputSection *isec) {
+  uint32_t type = isec->flags & MachO::SECTION_TYPE;
+  if (type != S_REGULAR && type != S_COALESCED)
+return false;
+
+  uint32_t attr = isec->flags & MachO::SECTION_ATTRIBUTES_USR;
+  if (attr == S_ATTR_PURE_INSTRUCTIONS)
+return true;
+
+  if (isec->segname == segment_names::text)
+return StringSwitch(isec->name)
+.Cases("__textcoal_nt", "__StaticInit", true)
+.Default(false);
+
+  return false;
+}
+
 std::string lld::toString(const InputSection *isec) {
   return (toString(isec->file) + ":(" + isec->name + ")").str();
 }

diff  --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index 4ef8d84bc8a0..f405fd6cf6d3 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -76,6 +76,8 @@ class InputSection {
   std::vector relocs;
 };
 
+bool isCodeSection(InputSection *);
+
 extern std::vector inputSections;
 
 } // namespace macho

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 6ba067bbc9e5..808d3594bfad 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -668,9 +668,7 @@ void SymtabSection::emitStabs() {
 symStab.strx = stringTableSection.addString(defined->getName());
 symStab.value = defined->getVA();
 
-// XXX is it right to assume that all symbols in __text are function
-// symbols?
-if (isec->name == "__text") {
+if (isCodeSection(isec)) {
   symStab.type = MachO::N_FUN;
   stabs.emplace_back(std::move(symStab));
   emitEndFunStab(defined);

diff  --git a/lld/test/MachO/stabs.s b/lld/test/MachO/stabs.s
index a3f1e2906d2b..445783269d11 100644
--- a/lld/test/MachO/stabs.s
+++ b/lld/test/MachO/stabs.s
@@ -36,12 +36,15 @@
 # CHECK-NEXT:  [[#DATA_ID:]]  __data
 # CHECK-NEXT:  [[#MORE_DATA_ID:]] more_data
 # CHECK-NEXT:  [[#COMM_ID:]]  __common
+# CHECK-NEXT:  [[#MORE_TEXT_ID:]] more_text
 
 # CHECK:    - 00 SO 
/tmp/test.cpp
 # CHECK-NEXT:  0010 - 03 0001   OSO 
[[DIR]]/test.o
 # CHECK-NEXT:  [[#%x, STATIC:]] - 0[[#MORE_DATA_ID + 1]]  STSYM _static_var
 # CHECK-NEXT:  [[#%x, MAIN:]]   - 0[[#TEXT_ID + 1]]     FUN _main
 # CHECK-NEXT:  0006 - 00    FUN
+# CHECK-NEXT:  [[#%x, FUN:]]- 0[[#MORE_TEXT_ID + 1]]    FUN _fun
+# CHECK-NEXT:  0001 - 00    FUN
 # CHECK-NEXT:  [[#%x, GLOB:]]   - 0[[#DATA_ID + 1]]    GSYM _global_var
 # CHECK-NEXT:  [[#%x, ZERO:]]   - 0[[#COMM_ID + 1]]    GSYM _zero
 # CHECK-NEXT:   - 01 SO
@@ -53,6 +56,7 @@
 # CHECK-NEXT:  [[#STATIC]]  s _static_var
 # CHECK-NEXT:  [[#MAIN]]T _main
 # CHECK-NEXT:  {{[0-9af]+}} A _abs
+# CHECK-NEXT:  [[#FUN]] S _fun
 # CHECK-NEXT:  [[#GLOB]]D _global_var
 # CHECK-NEXT:  [[#ZERO]]S _zero
 # CHECK-NEXT:  [[#FOO]] T _foo
@@ -121,6 +125,11 @@ Ldebug_info_end0:
 .subsections_via_symbols
 .section  __DWARF,__debug_line,regular,debug
 
+.section OTHER,more_text,regular,pure_instructions
+.globl _fun
+_fun:
+  ret
+
 #--- foo.s
 .text
 .globl  _foo



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] b768d57 - [lld-macho] Add archive name and file modtime to STABS output

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T15:05:21-08:00
New Revision: b768d57b368781e6737c403e425bd835850f3a0a

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

LOG: [lld-macho] Add archive name and file modtime to STABS output

We should also set the modtime when running LTO. That will be done in a
future diff, together with support for the `-object_path_lto` flag.

Reviewed By: clayborg

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/MachO/Driver.h
lld/MachO/DriverUtils.cpp
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/LTO.cpp
lld/MachO/SyntheticSections.cpp
lld/test/MachO/stabs.s

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 141082985b4e..295f2c412a7f 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -212,21 +212,32 @@ getFrameworkSearchPaths(opt::InputArgList &args,
 {"/Library/Frameworks", "/System/Library/Frameworks"});
 }
 
+namespace {
+struct ArchiveMember {
+  MemoryBufferRef mbref;
+  uint32_t modTime;
+};
+} // namespace
+
 // Returns slices of MB by parsing MB as an archive file.
 // Each slice consists of a member file in the archive.
-static std::vector getArchiveMembers(MemoryBufferRef mb) {
+static std::vector getArchiveMembers(MemoryBufferRef mb) {
   std::unique_ptr file =
   CHECK(Archive::create(mb),
 mb.getBufferIdentifier() + ": failed to parse archive");
 
-  std::vector v;
+  std::vector v;
   Error err = Error::success();
   for (const Archive::Child &c : file->children(err)) {
 MemoryBufferRef mbref =
 CHECK(c.getMemoryBufferRef(),
   mb.getBufferIdentifier() +
   ": could not get the buffer for a child of the archive");
-v.push_back(mbref);
+uint32_t modTime = toTimeT(
+CHECK(c.getLastModified(), mb.getBufferIdentifier() +
+   ": could not get the modification "
+   "time for a child of the archive"));
+v.push_back({mbref, modTime});
   }
   if (err)
 fatal(mb.getBufferIdentifier() +
@@ -235,6 +246,16 @@ static std::vector 
getArchiveMembers(MemoryBufferRef mb) {
   return v;
 }
 
+static void forceLoadArchive(StringRef path) {
+  if (Optional buffer = readFile(path)) {
+for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
+  auto file = make(member.mbref, member.modTime);
+  file->archiveName = buffer->getBufferIdentifier();
+  inputFiles.push_back(file);
+}
+  }
+}
+
 static InputFile *addFile(StringRef path) {
   Optional buffer = readFile(path);
   if (!buffer)
@@ -251,9 +272,7 @@ static InputFile *addFile(StringRef path) {
   error(path + ": archive has no index; run ranlib to add one");
 
 if (config->allLoad) {
-  if (Optional buffer = readFile(path))
-for (MemoryBufferRef member : getArchiveMembers(*buffer))
-  inputFiles.push_back(make(member));
+  forceLoadArchive(path);
 } else if (config->forceLoadObjC) {
   for (const object::Archive::Symbol &sym : file->symbols())
 if (sym.getName().startswith(objc::klass))
@@ -264,16 +283,16 @@ static InputFile *addFile(StringRef path) {
   // consider creating a LazyObjFile class in order to avoid double-loading
   // these files here and below (as part of the ArchiveFile).
   if (Optional buffer = readFile(path))
-for (MemoryBufferRef member : getArchiveMembers(*buffer))
-  if (hasObjCSection(member))
-inputFiles.push_back(make(member));
+for (const ArchiveMember &member : getArchiveMembers(*buffer))
+  if (hasObjCSection(member.mbref))
+inputFiles.push_back(make(member.mbref, member.modTime));
 }
 
 newFile = make(std::move(file));
 break;
   }
   case file_magic::macho_object:
-newFile = make(mbref);
+newFile = make(mbref, getModTime(path));
 break;
   case file_magic::macho_dynamically_linked_shared_lib:
   case file_magic::macho_dynamically_linked_shared_lib_stub:
@@ -304,12 +323,6 @@ static void addFileList(StringRef path) {
 addFile(path);
 }
 
-static void forceLoadArchive(StringRef path) {
-  if (Optional buffer = readFile(path))
-for (MemoryBufferRef member : getArchiveMembers(*buffer))
-  inputFiles.push_back(make(member));
-}
-
 static std::array archNames{"arm","arm64", "i386",
   "x86_64", "ppc",   "ppc64"};
 static bool isArchString(StringRef s) {

diff  --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index c06d2f9d1b3d..d5625fd3873e 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -43,6 +43,8 @@ llvm::Optional resolveDylibPath(llvm::StringRef 
path

[llvm-branch-commits] [lld] 78f6498 - [lld-macho] Flesh out STABS implementation

2020-12-01 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-01T15:05:21-08:00
New Revision: 78f6498cdcdb5a7644b1c32615cfe2fdfd9c2545

URL: 
https://github.com/llvm/llvm-project/commit/78f6498cdcdb5a7644b1c32615cfe2fdfd9c2545
DIFF: 
https://github.com/llvm/llvm-project/commit/78f6498cdcdb5a7644b1c32615cfe2fdfd9c2545.diff

LOG: [lld-macho] Flesh out STABS implementation

This addresses a lot of the comments in {D89257}. Ideally it'd have been
done in the same diff, but the commits in between make that difficult.

This diff implements:
* N_GSYM and N_STSYM, the STABS for global and static symbols
* Has the STABS reflect the section IDs of their referent symbols
* Ensures we don't fail when encountering absolute symbols or files with
  no debug info
* Sorts STABS symbols by file to minimize the number of N_OSO entries

Reviewed By: clayborg

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

Added: 


Modified: 
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/SyntheticSections.cpp
lld/MachO/SyntheticSections.h
lld/test/MachO/stabs.s

Removed: 




diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 19d531cc5028..8a451d0dc217 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -76,6 +76,7 @@ using namespace lld::macho;
 
 std::vector macho::inputFiles;
 std::unique_ptr macho::tar;
+int InputFile::idCount = 0;
 
 // Open a given file path and return it as a memory-mapped file.
 Optional macho::readFile(StringRef path) {

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index f7fd77dacff5..6ca9a40a49b0 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -65,13 +65,15 @@ class InputFile {
   std::vector symbols;
   ArrayRef sectionHeaders;
   std::vector subsections;
+  // Provides an easy way to sort InputFiles deterministically.
+  const int id;
 
 protected:
   InputFile(Kind kind, MemoryBufferRef mb)
-  : mb(mb), fileKind(kind), name(mb.getBufferIdentifier()) {}
+  : mb(mb), id(idCount++), fileKind(kind), name(mb.getBufferIdentifier()) 
{}
 
   InputFile(Kind kind, const llvm::MachO::InterfaceFile &interface)
-  : fileKind(kind), name(saver.save(interface.getPath())) {}
+  : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
 
   void parseSections(ArrayRef);
 
@@ -85,6 +87,8 @@ class InputFile {
 private:
   const Kind fileKind;
   const StringRef name;
+
+  static int idCount;
 };
 
 // .o file

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index ae66961310c5..6ba067bbc9e5 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -617,22 +617,71 @@ void SymtabSection::emitObjectFileStab(ObjFile *file) {
   stabs.emplace_back(std::move(stab));
 }
 
-void SymtabSection::emitFunStabs(Defined *defined) {
-  {
-StabsEntry stab(MachO::N_FUN);
-stab.sect = 1;
-stab.strx = stringTableSection.addString(defined->getName());
-stab.value = defined->getVA();
-stabs.emplace_back(std::move(stab));
+void SymtabSection::emitEndFunStab(Defined *defined) {
+  StabsEntry stab(MachO::N_FUN);
+  // FIXME this should be the size of the symbol. Using the section size in
+  // lieu is only correct if .subsections_via_symbols is set.
+  stab.value = defined->isec->getSize();
+  stabs.emplace_back(std::move(stab));
+}
+
+void SymtabSection::emitStabs() {
+  std::vector symbolsNeedingStabs;
+  for (const SymtabEntry &entry :
+   concat(localSymbols, externalSymbols)) {
+Symbol *sym = entry.sym;
+if (auto *defined = dyn_cast(sym)) {
+  if (defined->isAbsolute())
+continue;
+  InputSection *isec = defined->isec;
+  ObjFile *file = dyn_cast_or_null(isec->file);
+  if (!file || !file->compileUnit)
+continue;
+  symbolsNeedingStabs.push_back(defined);
+}
   }
 
-  {
-StabsEntry stab(MachO::N_FUN);
-// FIXME this should be the size of the symbol. Using the section size in
-// lieu is only correct if .subsections_via_symbols is set.
-stab.value = defined->isec->getSize();
-stabs.emplace_back(std::move(stab));
+  llvm::stable_sort(symbolsNeedingStabs, [&](Defined *a, Defined *b) {
+return a->isec->file->id < b->isec->file->id;
+  });
+
+  // Emit STABS symbols so that dsymutil and/or the debugger can map address
+  // regions in the final binary to the source and object files from which they
+  // originated.
+  InputFile *lastFile = nullptr;
+  for (Defined *defined : symbolsNeedingStabs) {
+InputSection *isec = defined->isec;
+ObjFile *file = dyn_cast(isec->file);
+assert(file);
+
+if (lastFile == nullptr || lastFile != file) {
+  if (lastFile != nullptr)
+emitEndSourceStab();
+  lastFile = file;
+
+  emitBeginSourceStab(file->compileUnit);
+  emitObjectFileStab(file);
+}
+
+StabsEntry symStab;
+symStab.sect = defined->isec->parent->index;

[llvm-branch-commits] [lld] 7b007ac - [lld-macho][nfc] Move some methods from InputFile to ObjFile

2020-12-08 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-08T10:34:32-08:00
New Revision: 7b007ac0800babe750c58b1aead15e1c3e7f3b0f

URL: 
https://github.com/llvm/llvm-project/commit/7b007ac0800babe750c58b1aead15e1c3e7f3b0f
DIFF: 
https://github.com/llvm/llvm-project/commit/7b007ac0800babe750c58b1aead15e1c3e7f3b0f.diff

LOG: [lld-macho][nfc] Move some methods from InputFile to ObjFile

Additionally:
1. Move the helper functions in InputSection.h below the definition of
   `InputSection`, so the important stuff is on top
2. Remove unnecessary `explicit`

Reviewed By: #lld-macho, compnerd

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

Added: 


Modified: 
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/InputSection.cpp
lld/MachO/InputSection.h

Removed: 




diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 1339152901ce..24600090c491 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -155,7 +155,7 @@ const load_command *macho::findCommand(const mach_header_64 
*hdr,
   return nullptr;
 }
 
-void InputFile::parseSections(ArrayRef sections) {
+void ObjFile::parseSections(ArrayRef sections) {
   subsections.reserve(sections.size());
   auto *buf = reinterpret_cast(mb.getBufferStart());
 
@@ -192,8 +192,8 @@ static InputSection *findContainingSubsection(SubsectionMap 
&map,
   return it->second;
 }
 
-void InputFile::parseRelocations(const section_64 &sec,
- SubsectionMap &subsecMap) {
+void ObjFile::parseRelocations(const section_64 &sec,
+   SubsectionMap &subsecMap) {
   auto *buf = reinterpret_cast(mb.getBufferStart());
   ArrayRef anyRelInfos(
   reinterpret_cast(buf + sec.reloff),
@@ -266,8 +266,8 @@ static macho::Symbol *createAbsolute(const 
structs::nlist_64 &sym,
/*isExternal=*/false);
 }
 
-macho::Symbol *InputFile::parseNonSectionSymbol(const structs::nlist_64 &sym,
-StringRef name) {
+macho::Symbol *ObjFile::parseNonSectionSymbol(const structs::nlist_64 &sym,
+  StringRef name) {
   uint8_t type = sym.n_type & N_TYPE;
   switch (type) {
   case N_UNDF:
@@ -289,8 +289,8 @@ macho::Symbol *InputFile::parseNonSectionSymbol(const 
structs::nlist_64 &sym,
   }
 }
 
-void InputFile::parseSymbols(ArrayRef nList,
- const char *strtab, bool subsectionsViaSymbols) {
+void ObjFile::parseSymbols(ArrayRef nList,
+   const char *strtab, bool subsectionsViaSymbols) {
   // resize(), not reserve(), because we are going to create N_ALT_ENTRY 
symbols
   // out-of-sequence.
   symbols.resize(nList.size());

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index 59638ac197cf..877ccaef2fef 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -64,7 +64,6 @@ class InputFile {
   MemoryBufferRef mb;
 
   std::vector symbols;
-  ArrayRef sectionHeaders;
   std::vector subsections;
   // Provides an easy way to sort InputFiles deterministically.
   const int id;
@@ -80,15 +79,6 @@ class InputFile {
   InputFile(Kind kind, const llvm::MachO::InterfaceFile &interface)
   : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
 
-  void parseSections(ArrayRef);
-
-  void parseSymbols(ArrayRef nList, const char *strtab,
-bool subsectionsViaSymbols);
-
-  Symbol *parseNonSectionSymbol(const structs::nlist_64 &sym, StringRef name);
-
-  void parseRelocations(const llvm::MachO::section_64 &, SubsectionMap &);
-
 private:
   const Kind fileKind;
   const StringRef name;
@@ -99,21 +89,26 @@ class InputFile {
 // .o file
 class ObjFile : public InputFile {
 public:
-  explicit ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef 
archiveName);
+  ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName);
   static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
 
   llvm::DWARFUnit *compileUnit = nullptr;
   const uint32_t modTime;
+  ArrayRef sectionHeaders;
 
 private:
+  void parseSections(ArrayRef);
+  void parseSymbols(ArrayRef nList, const char *strtab,
+bool subsectionsViaSymbols);
+  Symbol *parseNonSectionSymbol(const structs::nlist_64 &sym, StringRef name);
+  void parseRelocations(const llvm::MachO::section_64 &, SubsectionMap &);
   void parseDebugInfo();
 };
 
 // command-line -sectcreate file
 class OpaqueFile : public InputFile {
 public:
-  explicit OpaqueFile(MemoryBufferRef mb, StringRef segName,
-  StringRef sectName);
+  OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName);
   static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; }
 };
 

diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 80c263301f41..8477d78d2f3f 100644
--- a/lld/MachO/InputSection.c

[llvm-branch-commits] [lld] 78976bf - [lld-macho] Support parsing of bitcode within archives

2020-12-08 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-08T10:34:32-08:00
New Revision: 78976bf3dae2a4fac3b7fb2ab1a8d8e986ea36ff

URL: 
https://github.com/llvm/llvm-project/commit/78976bf3dae2a4fac3b7fb2ab1a8d8e986ea36ff
DIFF: 
https://github.com/llvm/llvm-project/commit/78976bf3dae2a4fac3b7fb2ab1a8d8e986ea36ff.diff

LOG: [lld-macho] Support parsing of bitcode within archives

Also error out if we find anything other than an object or bitcode file
in the archive.

Note that we were previously inserting the symbols and sections of the
unpacked ObjFile into the containing ArchiveFile. This was actually
unnecessary -- we can just insert the ObjectFile (or BitcodeFile) into
the `inputFiles` vector. This is the approach taken by LLD-ELF.

Reviewed By: thakis

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

Added: 
lld/test/MachO/bitcode-nodatalayout.ll
lld/test/MachO/invalid/bad-archive-member.s
lld/test/MachO/lto-archive.ll

Modified: 
lld/MachO/InputFiles.cpp

Removed: 




diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 24600090c491..c452648a1b11 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -631,15 +631,26 @@ void ArchiveFile::fetch(const object::Archive::Symbol 
&sym) {
   // to it later.
   const object::Archive::Symbol sym_copy = sym;
 
-  auto file = make(mb, modTime, getName());
+  InputFile *file;
+  switch (identify_magic(mb.getBuffer())) {
+  case file_magic::macho_object:
+file = make(mb, modTime, getName());
+break;
+  case file_magic::bitcode:
+file = make(mb);
+break;
+  default:
+StringRef bufname =
+CHECK(c.getName(), toString(this) + ": could not get buffer name");
+error(toString(this) + ": archive member " + bufname +
+  " has unhandled file type");
+return;
+  }
+  inputFiles.push_back(file);
 
   // ld64 doesn't demangle sym here even with -demangle. Match that, so
   // intentionally no call to toMachOString() here.
   printArchiveMemberLoad(sym_copy.getName(), file);
-
-  symbols.insert(symbols.end(), file->symbols.begin(), file->symbols.end());
-  subsections.insert(subsections.end(), file->subsections.begin(),
- file->subsections.end());
 }
 
 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)

diff  --git a/lld/test/MachO/bitcode-nodatalayout.ll 
b/lld/test/MachO/bitcode-nodatalayout.ll
new file mode 100644
index ..5e1c43c928d9
--- /dev/null
+++ b/lld/test/MachO/bitcode-nodatalayout.ll
@@ -0,0 +1,13 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+; RUN: not %lld %t.o -o /dev/null 2>&1 | FileCheck %s
+
+; CHECK: error: input module has no datalayout
+
+; This bitcode file has no datalayout.
+; Check that we error out producing a reasonable diagnostic.
+target triple = "x86_64-apple-macosx10.15.0"
+
+define void @_start() {
+  ret void
+}

diff  --git a/lld/test/MachO/invalid/bad-archive-member.s 
b/lld/test/MachO/invalid/bad-archive-member.s
new file mode 100644
index ..15ab524476d3
--- /dev/null
+++ b/lld/test/MachO/invalid/bad-archive-member.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+# RUN: %lld -dylib -lSystem %t/foo.o -o %t/foo.dylib
+# RUN: rm -f %t/foo.a
+# RUN: llvm-ar rcs %t/foo.a %t/foo.dylib
+# RUN: not %lld %t/test.o %t/foo.a -o /dev/null 2>&1 | FileCheck %s 
-DFILE=%t/foo.a
+# CHECK: error: [[FILE]]: archive member foo.dylib has unhandled file type
+
+#--- foo.s
+.globl _foo
+_foo:
+  ret
+
+#--- test.s
+.globl _main
+_main:
+  callq _foo
+  ret

diff  --git a/lld/test/MachO/lto-archive.ll b/lld/test/MachO/lto-archive.ll
new file mode 100644
index ..42b2c0fb5759
--- /dev/null
+++ b/lld/test/MachO/lto-archive.ll
@@ -0,0 +1,28 @@
+; REQUIRES: x86
+; RUN: split-file %s %t
+; RUN: llvm-as %t/foo.ll -o %t/foo.o
+; RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+; RUN: rm -f %t/foo.a
+; RUN: llvm-ar rcs %t/foo.a %t/foo.o
+; RUN: %lld -save-temps -lSystem %t/test.o %t/foo.a -o %t/test
+; RUN: llvm-objdump -d --macho --no-show-raw-insn %t/test | FileCheck %s
+
+; CHECK:  _main:
+; CHECK-NEXT: callq _foo
+; CHECK-NEXT: retq
+
+;--- foo.ll
+
+target triple = "x86_64-apple-macosx10.15.0"
+target datalayout = 
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @foo() {
+  ret void
+}
+
+;--- test.s
+
+.globl _main
+_main:
+  callq _foo
+  ret



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 863f7a7 - [lld-macho] Don't attempt to emit rebase opcodes for debug sections

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T15:57:51-08:00
New Revision: 863f7a745e6ba5b9aebca82eeba1a2fb1db53e20

URL: 
https://github.com/llvm/llvm-project/commit/863f7a745e6ba5b9aebca82eeba1a2fb1db53e20
DIFF: 
https://github.com/llvm/llvm-project/commit/863f7a745e6ba5b9aebca82eeba1a2fb1db53e20.diff

LOG: [lld-macho] Don't attempt to emit rebase opcodes for debug sections

This was causing a crash as we were attempting to look up the
nonexistent parent OutputSection of the debug sections. We didn't detect
it earlier because there was no test for PIEs with debug info (PIEs
require us to emit rebases for X86_64_RELOC_UNSIGNED).

This diff filters out the debug sections while loading the ObjFiles. In
addition to fixing the above problem, it also lets us avoid doing
redundant work -- we no longer parse / apply relocations / attempt to
emit dyld opcodes for these sections that we don't emit.

Fixes llvm.org/PR48392.

Reviewed By: thakis

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

Added: 


Modified: 
lld/MachO/Dwarf.cpp
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/MachO/Writer.cpp
lld/test/MachO/stabs.s

Removed: 




diff  --git a/lld/MachO/Dwarf.cpp b/lld/MachO/Dwarf.cpp
index 121f54fb1f79..3e794922ad1d 100644
--- a/lld/MachO/Dwarf.cpp
+++ b/lld/MachO/Dwarf.cpp
@@ -20,26 +20,19 @@ using namespace llvm;
 std::unique_ptr DwarfObject::create(ObjFile *obj) {
   auto dObj = std::make_unique();
   bool hasDwarfInfo = false;
-  for (SubsectionMap subsecMap : obj->subsections) {
-for (auto it : subsecMap) {
-  InputSection *isec = it.second;
-  if (!(isDebugSection(isec->flags) &&
-isec->segname == segment_names::dwarf))
-continue;
-
-  if (isec->name == "__debug_info") {
-dObj->infoSection.Data = toStringRef(isec->data);
-hasDwarfInfo = true;
-continue;
-  }
-
-  if (StringRef *s = StringSwitch(isec->name)
- .Case("__debug_abbrev", &dObj->abbrevSection)
- .Case("__debug_str", &dObj->strSection)
- .Default(nullptr)) {
-*s = toStringRef(isec->data);
-hasDwarfInfo = true;
-  }
+  // LLD only needs to extract the source file path from the debug info, so we
+  // initialize DwarfObject with just the sections necessary to get that path.
+  // The debugger will locate the debug info via the object file paths that we
+  // emit in our STABS symbols, so we don't need to process & emit them
+  // ourselves.
+  for (InputSection *isec : obj->debugSections) {
+if (StringRef *s = StringSwitch(isec->name)
+   .Case("__debug_info", &dObj->infoSection.Data)
+   .Case("__debug_abbrev", &dObj->abbrevSection)
+   .Case("__debug_str", &dObj->strSection)
+   .Default(nullptr)) {
+  *s = toStringRef(isec->data);
+  hasDwarfInfo = true;
 }
   }
 

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index c452648a1b11..3e0ad220bff1 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -174,7 +174,18 @@ void ObjFile::parseSections(ArrayRef sections) 
{
 else
   isec->align = 1 << sec.align;
 isec->flags = sec.flags;
-subsections.push_back({{0, isec}});
+
+if (!(isDebugSection(isec->flags) &&
+  isec->segname == segment_names::dwarf)) {
+  subsections.push_back({{0, isec}});
+} else {
+  // Instead of emitting DWARF sections, we emit STABS symbols to the
+  // object files that contain them. We filter them out early to avoid
+  // parsing their relocations unnecessarily. But we must still push an
+  // empty map to ensure the indices line up for the remaining sections.
+  subsections.push_back({});
+  debugSections.push_back(isec);
+}
   }
 }
 
@@ -307,6 +318,7 @@ void ObjFile::parseSymbols(ArrayRef 
nList,
 
 const section_64 &sec = sectionHeaders[sym.n_sect - 1];
 SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
+assert(!subsecMap.empty());
 uint64_t offset = sym.n_value - sec.addr;
 
 // If the input file does not use subsections-via-symbols, all symbols can
@@ -410,7 +422,8 @@ ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, 
StringRef archiveName)
   // The relocations may refer to the symbols, so we parse them after we have
   // parsed all the symbols.
   for (size_t i = 0, n = subsections.size(); i < n; ++i)
-parseRelocations(sectionHeaders[i], subsections[i]);
+if (!subsections[i].empty())
+  parseRelocations(sectionHeaders[i], subsections[i]);
 
   parseDebugInfo();
 }

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index 877ccaef2fef..2b492280c6f5 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -95,6 +95,7 @@ class ObjFile : public InputFile {
   llvm::DWARFUnit 

[llvm-branch-commits] [lld] 95831a5 - [lld-macho] Implement -object_path_lto

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T15:57:51-08:00
New Revision: 95831a56d092c563b03167ece592be301a2a0081

URL: 
https://github.com/llvm/llvm-project/commit/95831a56d092c563b03167ece592be301a2a0081
DIFF: 
https://github.com/llvm/llvm-project/commit/95831a56d092c563b03167ece592be301a2a0081.diff

LOG: [lld-macho] Implement -object_path_lto

This makes it possible for STABS entries to reference the debug info
contained in the LTO-compiled output.

I'm not sure how to test the file mtime within llvm-lit -- GNU and BSD
`stat` take different command-line arguments. I've omitted the check for
now.

Reviewed By: clayborg

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

Added: 
lld/test/MachO/lto-object-path.ll

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

Removed: 




diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index c13010717bfd..46ce02fb25e0 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -45,6 +45,7 @@ struct Configuration {
   uint32_t headerPad;
   llvm::StringRef installName;
   llvm::StringRef outputFile;
+  llvm::StringRef ltoObjPath;
   bool demangle = false;
   llvm::MachO::Architecture arch;
   PlatformInfo platform;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 72ef1b28ed58..48e7e7e8dc0d 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -703,6 +703,7 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
   config->printEachFile = args.hasArg(OPT_t);
   config->printWhyLoad = args.hasArg(OPT_why_load);
   config->outputType = getOutputType(args);
+  config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
   config->runtimePaths = args::getStrings(args, OPT_rpath);
   config->allLoad = args.hasArg(OPT_all_load);
   config->forceLoadObjC = args.hasArg(OPT_ObjC);

diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index 1932cf46661d..f2a0c683052f 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -8,17 +8,21 @@
 
 #include "LTO.h"
 #include "Config.h"
+#include "Driver.h"
 #include "InputFiles.h"
 
 #include "lld/Common/ErrorHandler.h"
 #include "lld/Common/Strings.h"
 #include "lld/Common/TargetOptionsCommandFlags.h"
 #include "llvm/LTO/LTO.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace lld;
 using namespace lld::macho;
 using namespace llvm;
+using namespace llvm::sys;
 
 static lto::Config createConfig() {
   lto::Config c;
@@ -73,12 +77,26 @@ std::vector BitcodeCompiler::compile() {
   saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
   }
 
-  // TODO: set modTime properly
+  if (!config->ltoObjPath.empty())
+fs::create_directories(config->ltoObjPath);
+
   std::vector ret;
-  for (unsigned i = 0; i != maxTasks; ++i)
-if (!buf[i].empty())
-  ret.push_back(
-  make(MemoryBufferRef(buf[i], "lto.tmp"), /*modTime=*/0, 
""));
+  for (unsigned i = 0; i != maxTasks; ++i) {
+if (buf[i].empty()) {
+  continue;
+}
+SmallString<261> filePath("/tmp/lto.tmp");
+uint32_t modTime = 0;
+if (!config->ltoObjPath.empty()) {
+  filePath = config->ltoObjPath;
+  path::append(filePath, Twine(i) + "." +
+ getArchitectureName(config->arch) + ".lto.o");
+  saveBuffer(buf[i], filePath);
+  modTime = getModTime(filePath);
+}
+ret.push_back(make(
+MemoryBufferRef(buf[i], saver.save(filePath.str())), modTime, ""));
+  }
 
   return ret;
 }

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index b333d2b714f6..b05bc9fc8073 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -840,7 +840,6 @@ def no_objc_category_merging : Flag<["-"], 
"no_objc_category_merging">,
 def object_path_lto : Separate<["-"], "object_path_lto">,
  MetaVarName<"">,
  HelpText<"Retain any temporary mach-o file in  that would otherwise 
be deleted during LTO">,
- Flags<[HelpHidden]>,
  Group;
 def lto_library : Separate<["-"], "lto_library">,
  MetaVarName<"">,

diff  --git a/lld/test/MachO/lto-object-path.ll 
b/lld/test/MachO/lto-object-path.ll
new file mode 100644
index ..b362bf45b133
--- /dev/null
+++ b/lld/test/MachO/lto-object-path.ll
@@ -0,0 +1,37 @@
+; REQUIRES: x86
+
+; RUN: rm -rf %t; mkdir %t
+; RUN: llvm-as %s -o %t/test.o
+
+; RUN: %lld %t/test.o -o %t/test
+; RUN: llvm-nm -pa %t/test | FileCheck %s --check-prefixes CHECK,NOOBJPATH
+
+; RUN: %lld %t/test.o -o %t/test -object_path_lto %t/lto-temps
+; RUN: llvm-nm -pa %t/test | FileCheck %s --check-prefixes CHECK,OBJPATH 
-DDIR=%t/lto-temps
+
+; CHECK:  - 00 SO /tmp/test.cpp
+; NOOBJPATH-NEXT: - 03 0001   OSO /tmp/lto.tmp
+;; check that modTime is nonzero when `-object_path_lto` is provided
+; OBJPATH-NEXT:   {{[0-9a-f]*[1-9a-f]+[0-9

[llvm-branch-commits] [lld] 29d3b0e - [lld-macho] Add support for -mcpu, -mattr, -code-model in LTO

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T15:57:51-08:00
New Revision: 29d3b0e47113e011c6fc541fc81c868ef3df21a3

URL: 
https://github.com/llvm/llvm-project/commit/29d3b0e47113e011c6fc541fc81c868ef3df21a3
DIFF: 
https://github.com/llvm/llvm-project/commit/29d3b0e47113e011c6fc541fc81c868ef3df21a3.diff

LOG: [lld-macho] Add support for -mcpu, -mattr, -code-model in LTO

`-mcpu` and `-code-model` tests were copied from similar ones in
LLD-ELF.

There doesn't seem to be an equivalent test for `-mattr` in LLD-ELF, so
I've verified our behavior by cribbing a test from
CodeGen/X86/recip-fastmath.ll.

Reviewed By: #lld-macho, compnerd, MaskRay

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

Added: 
lld/test/MachO/codemodel.ll
lld/test/MachO/cpu-string.ll
lld/test/MachO/mattrs.ll

Modified: 
lld/MachO/Driver.cpp
lld/MachO/LTO.cpp
lld/MachO/Options.td

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 48e7e7e8dc0d..432bb81925da 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -654,6 +654,17 @@ static bool isPie(opt::InputArgList &args) {
   return args.hasArg(OPT_pie);
 }
 
+static void parseClangOption(StringRef opt, const Twine &msg) {
+  std::string err;
+  raw_string_ostream os(err);
+
+  const char *argv[] = {"lld", opt.data()};
+  if (cl::ParseCommandLineOptions(2, argv, "", &os))
+return;
+  os.flush();
+  error(msg + ": " + StringRef(err).trim());
+}
+
 bool macho::link(llvm::ArrayRef argsArr, bool canExitEarly,
  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
   lld::stdoutOS = &stdoutOS;
@@ -788,6 +799,14 @@ bool macho::link(llvm::ArrayRef argsArr, 
bool canExitEarly,
   error("-sub_library " + searchName + " does not match a supplied dylib");
   }
 
+  // Parse LTO options.
+  if (auto *arg = args.getLastArg(OPT_mcpu))
+parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
+ arg->getSpelling());
+
+  for (auto *arg : args.filtered(OPT_mllvm))
+parseClangOption(arg->getValue(), arg->getSpelling());
+
   initLLVM();
   compileBitcodeFiles();
   replaceCommonSymbols();

diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index f2a0c683052f..9e74b69ff1ec 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -27,6 +27,9 @@ using namespace llvm::sys;
 static lto::Config createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
+  c.CodeModel = getCodeModelFromCMModel();
+  c.CPU = getCPUStr();
+  c.MAttrs = getMAttrs();
   return c;
 }
 

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index b05bc9fc8073..f00e23bfd791 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -876,7 +876,9 @@ def not_for_dyld_shared_cache : Flag<["-"], 
"not_for_dyld_shared_cache">,
  Group;
 def mllvm : Separate<["-"], "mllvm">,
  HelpText<"Options to pass to LLVM">,
- Flags<[HelpHidden]>,
+ Group;
+def mcpu : Separate<["-"], "mcpu">,
+ HelpText<"Processor family target for LTO code generation">,
  Group;
 
 def grp_deprecated : OptionGroup<"deprecated">, HelpText<"DEPRECATED">;
@@ -1210,10 +1212,6 @@ def uikitformac_version_min : Flag<["-"], 
"uikitformac_version_min">,
  HelpText<"Alias for -maccatalyst_version_min">,
  Flags<[HelpHidden]>,
  Group;
-def mcpu : Flag<["-"], "mcpu">,
- HelpText<"This option is undocumented in ld64">,
- Flags<[HelpHidden]>,
- Group;
 def no_compact_unwind : Flag<["-"], "no_compact_unwind">,
  HelpText<"This option is undocumented in ld64">,
  Flags<[HelpHidden]>,

diff  --git a/lld/test/MachO/codemodel.ll b/lld/test/MachO/codemodel.ll
new file mode 100644
index ..2c5fec775b25
--- /dev/null
+++ b/lld/test/MachO/codemodel.ll
@@ -0,0 +1,20 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+; RUN: %lld -lSystem %t.o -o %ts -mllvm -code-model=small
+; RUN: %lld -lSystem %t.o -o %tl -mllvm -code-model=large
+; RUN: llvm-objdump -d %ts | FileCheck %s --check-prefix=CHECK-SMALL
+; RUN: llvm-objdump -d %tl | FileCheck %s --check-prefix=CHECK-LARGE
+
+target triple = "x86_64-apple-darwin"
+target datalayout = 
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+@data = internal constant [0 x i32] []
+
+define i32* @main() nounwind readonly {
+entry:
+; CHECK-SMALL-LABEL: <_main>:
+; CHECK-SMALL:   leaq[[#]](%rip), %rax
+; CHECK-LARGE-LABEL: <_main>:
+; CHECK-LARGE:   movabsq $[[#]], %rax
+  ret i32* getelementptr ([0 x i32], [0 x i32]* @data, i64 0, i64 0)
+}

diff  --git a/lld/test/MachO/cpu-string.ll b/lld/test/MachO/cpu-string.ll
new file mode 100644
index ..dcd51bef6f44
--- /dev/null
+++ b/lld/test/MachO/cpu-string.ll
@@ -0,0 +1,23 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+
+; RUN: %lld %t.o -o %t.dylib -dylib
+; RUN: llvm-objdump -d --section="__text" --no-leading-addr --no-show-raw-insn 
%t.dylib | FileCheck %s
+; CHECK: 

[llvm-branch-commits] [lld] 74d7999 - [lld-macho] Initialize AsmParsers earlier

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T15:57:52-08:00
New Revision: 74d799926e557928352dcd980788cae6487e421c

URL: 
https://github.com/llvm/llvm-project/commit/74d799926e557928352dcd980788cae6487e421c
DIFF: 
https://github.com/llvm/llvm-project/commit/74d799926e557928352dcd980788cae6487e421c.diff

LOG: [lld-macho] Initialize AsmParsers earlier

We need to initialize AsmParsers before any calls to `addFile`, as
bitcode files may require them. Otherwise we trigger `Assertion T &&
T->hasMCAsmParser()' failed`.

Reviewed By: #lld-macho, compnerd

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

Added: 
lld/test/MachO/module-asm.ll

Modified: 
lld/MachO/Driver.cpp

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 432bb81925da..b8a926279891 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -749,6 +749,8 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
 return !errorCount();
   }
 
+  initLLVM(); // must be run before any call to addFile()
+
   for (const auto &arg : args) {
 const auto &opt = arg->getOption();
 warnIfDeprecatedOption(opt);
@@ -807,7 +809,6 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
   for (auto *arg : args.filtered(OPT_mllvm))
 parseClangOption(arg->getValue(), arg->getSpelling());
 
-  initLLVM();
   compileBitcodeFiles();
   replaceCommonSymbols();
 

diff  --git a/lld/test/MachO/module-asm.ll b/lld/test/MachO/module-asm.ll
new file mode 100644
index ..01adcc438d2a
--- /dev/null
+++ b/lld/test/MachO/module-asm.ll
@@ -0,0 +1,24 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+; RUN: %lld %t.o -o %t
+; RUN: llvm-objdump -d %t | FileCheck %s
+
+; CHECK:  <_foo>:
+; CHECK-NEXT: retq
+
+; CHECK:  <_main>:
+; CHECK-NEXT: jmp {{.*}} <_foo>
+
+target triple = "x86_64-apple-macosx10.15.0"
+target datalayout = 
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+module asm ".text"
+module asm ".globl _foo"
+module asm "_foo: ret"
+
+declare void @foo()
+
+define void @main() {
+  call void @foo()
+  ret void
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 6a348f6 - [lld-macho] Implement `-no_implicit_dylibs`

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T15:57:52-08:00
New Revision: 6a348f6158ecdb7a4bcac3f4cd1d3c5b6e80a550

URL: 
https://github.com/llvm/llvm-project/commit/6a348f6158ecdb7a4bcac3f4cd1d3c5b6e80a550
DIFF: 
https://github.com/llvm/llvm-project/commit/6a348f6158ecdb7a4bcac3f4cd1d3c5b6e80a550.diff

LOG: [lld-macho] Implement `-no_implicit_dylibs`

Dylibs that are "public" -- i.e. top-level system libraries -- are considered
implicitly linked when another library re-exports them. That is, we should load
them & bind directly to their symbols instead of via their re-exporting
umbrella library. This diff implements that behavior by default, as well as an
opt-out flag.

In theory, this is just a performance optimization, but in practice it seems
that it's needed for correctness.

Fixes llvm.org/PR48395.

Reviewed By: thakis

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

Added: 
lld/test/MachO/implicit-dylibs.s

Modified: 
lld/MachO/Config.h
lld/MachO/Driver.cpp
lld/MachO/InputFiles.cpp
lld/MachO/Options.td
lld/test/MachO/reexport-stub.s
lld/test/MachO/stub-link.s

Removed: 




diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index 46ce02fb25e0..2835b5c1546e 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -36,6 +36,7 @@ struct Configuration {
   bool allLoad = false;
   bool forceLoadObjC = false;
   bool staticLink = false;
+  bool implicitDylibs = false;
   bool isPic = false;
   bool headerPadMaxInstallNames = false;
   bool printEachFile = false;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index b8a926279891..469ab801bfe8 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -719,6 +719,7 @@ bool macho::link(llvm::ArrayRef argsArr, bool 
canExitEarly,
   config->allLoad = args.hasArg(OPT_all_load);
   config->forceLoadObjC = args.hasArg(OPT_ObjC);
   config->demangle = args.hasArg(OPT_demangle);
+  config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
 
   if (const opt::Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
 config->staticLink = (arg->getOption().getID() == OPT_static);

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 3e0ad220bff1..e9e71e0c6a40 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -479,7 +479,8 @@ const InterfaceFile *currentTopLevelTapi = nullptr;
 
 // Re-exports can either refer to on-disk files, or to documents within .tbd
 // files.
-static Optional loadReexport(StringRef path, DylibFile *umbrella) 
{
+static Optional loadReexportHelper(StringRef path,
+DylibFile *umbrella) {
   if (path::is_absolute(path, path::Style::posix))
 for (StringRef root : config->systemLibraryRoots)
   if (Optional dylibPath =
@@ -504,6 +505,26 @@ static Optional loadReexport(StringRef path, 
DylibFile *umbrella) {
   return {};
 }
 
+// If a re-exported dylib is public (lives in /usr/lib or
+// /System/Library/Frameworks), then it is considered implicitly linked: we
+// should bind to its symbols directly instead of via the re-exporting umbrella
+// library.
+static bool isImplicitlyLinked(StringRef path) {
+  if (!config->implicitDylibs)
+return false;
+
+  return path::parent_path(path) == "/usr/lib";
+  // TODO: check for public frameworks too. We'll need to implement
+  // -sub_umbrella first to write a test case.
+}
+
+static Optional loadReexport(StringRef path, DylibFile *umbrella) 
{
+  Optional reexport = loadReexportHelper(path, umbrella);
+  if (reexport && isImplicitlyLinked(path))
+inputFiles.push_back(*reexport);
+  return reexport;
+}
+
 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella)
 : InputFile(DylibKind, mb) {
   if (umbrella == nullptr)
@@ -522,17 +543,15 @@ DylibFile::DylibFile(MemoryBufferRef mb, DylibFile 
*umbrella)
   }
 
   // Initialize symbols.
-  // TODO: if a re-exported dylib is public (lives in /usr/lib or
-  // /System/Library/Frameworks), we should bind to its symbols directly
-  // instead of the re-exporting umbrella library.
+  DylibFile *exportingFile = isImplicitlyLinked(dylibName) ? this : umbrella;
   if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) {
 auto *c = reinterpret_cast(cmd);
 parseTrie(buf + c->export_off, c->export_size,
   [&](const Twine &name, uint64_t flags) {
 bool isWeakDef = flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
 bool isTlv = flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
-symbols.push_back(symtab->addDylib(saver.save(name), umbrella,
-   isWeakDef, isTlv));
+symbols.push_back(symtab->addDylib(
+saver.save(name), exportingFile, isWeakDef, isTlv));
   });
   } else {
 error("LC_DYLD_INFO_ONLY not found in " + toString(this));
@@ -564,8 +583,9 @@ DylibFile::

[llvm-branch-commits] [lld] 76c36c1 - [lld-macho] Don't load dylibs more than once

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T15:57:52-08:00
New Revision: 76c36c11a9c620a5eeced5750b844a1097ab7586

URL: 
https://github.com/llvm/llvm-project/commit/76c36c11a9c620a5eeced5750b844a1097ab7586
DIFF: 
https://github.com/llvm/llvm-project/commit/76c36c11a9c620a5eeced5750b844a1097ab7586.diff

LOG: [lld-macho] Don't load dylibs more than once

Also remove `DylibFile::reexported` since it's unused.

Fixes llvm.org/PR48393.

Reviewed By: thakis

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

Added: 


Modified: 
lld/MachO/Driver.cpp
lld/MachO/Driver.h
lld/MachO/DriverUtils.cpp
lld/MachO/InputFiles.cpp
lld/MachO/InputFiles.h
lld/test/MachO/dylink.s
lld/test/MachO/implicit-dylibs.s
lld/test/MachO/lc-linker-option.ll

Removed: 




diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 469ab801bfe8..73846d420096 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -309,10 +309,8 @@ static InputFile *addFile(StringRef path, bool 
forceLoadArchive) {
 break;
   case file_magic::macho_dynamically_linked_shared_lib:
   case file_magic::macho_dynamically_linked_shared_lib_stub:
-newFile = make(mbref);
-break;
   case file_magic::tapi_file: {
-if (Optional dylibFile = makeDylibFromTAPI(mbref))
+if (Optional dylibFile = loadDylib(mbref))
   newFile = *dylibFile;
 break;
   }

diff  --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index 9a6a05af0e0d..97b897f0271b 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -43,8 +43,8 @@ std::string createResponseFile(const llvm::opt::InputArgList 
&args);
 // Check for both libfoo.dylib and libfoo.tbd (in that order).
 llvm::Optional resolveDylibPath(llvm::StringRef path);
 
-llvm::Optional makeDylibFromTAPI(llvm::MemoryBufferRef mbref,
-  DylibFile *umbrella = nullptr);
+llvm::Optional loadDylib(llvm::MemoryBufferRef mbref,
+  DylibFile *umbrella = nullptr);
 
 uint32_t getModTime(llvm::StringRef path);
 

diff  --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index 9f06901d3847..05677a9df78d 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -14,6 +14,8 @@
 #include "lld/Common/ErrorHandler.h"
 #include "lld/Common/Memory.h"
 #include "lld/Common/Reproduce.h"
+#include "llvm/ADT/CachedHashString.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Option/Option.h"
@@ -166,7 +168,7 @@ Optional macho::resolveDylibPath(StringRef 
path) {
   return {};
 }
 
-Optional macho::makeDylibFromTAPI(MemoryBufferRef mbref,
+static Optional makeDylibFromTapi(MemoryBufferRef mbref,
DylibFile *umbrella) {
   Expected> result = TextAPIReader::get(mbref);
   if (!result) {
@@ -177,6 +179,24 @@ Optional 
macho::makeDylibFromTAPI(MemoryBufferRef mbref,
   return make(**result, umbrella);
 }
 
+static DenseSet loadedDylibs;
+
+Optional macho::loadDylib(MemoryBufferRef mbref,
+   DylibFile *umbrella) {
+  StringRef path = mbref.getBufferIdentifier();
+  if (loadedDylibs.contains(CachedHashStringRef(path)))
+return {};
+  loadedDylibs.insert(CachedHashStringRef(path));
+
+  file_magic magic = identify_magic(mbref.getBuffer());
+  if (magic == file_magic::tapi_file)
+return makeDylibFromTapi(mbref, umbrella);
+
+  assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
+ magic == file_magic::macho_dynamically_linked_shared_lib_stub);
+  return make(mbref, umbrella);
+}
+
 uint32_t macho::getModTime(StringRef path) {
   fs::file_status stat;
   if (!fs::status(path, stat))

diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index e9e71e0c6a40..92fc7d10ebb0 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -457,12 +457,7 @@ static Optional loadDylib(StringRef path, 
DylibFile *umbrella) {
 error("could not read dylib file at " + path);
 return {};
   }
-
-  file_magic magic = identify_magic(mbref->getBuffer());
-  if (magic == file_magic::tapi_file)
-return makeDylibFromTAPI(*mbref, umbrella);
-  assert(magic == file_magic::macho_dynamically_linked_shared_lib);
-  return make(*mbref, umbrella);
+  return loadDylib(*mbref, umbrella);
 }
 
 // TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
@@ -518,11 +513,10 @@ static bool isImplicitlyLinked(StringRef path) {
   // -sub_umbrella first to write a test case.
 }
 
-static Optional loadReexport(StringRef path, DylibFile *umbrella) 
{
+void loadReexport(StringRef path, DylibFile *umbrella) {
   Optional reexport = loadReexportHelper(path, umbrella);
   if (reexport && isImplicitlyLinked(path))
 inputFiles.push_back(*reexport);
-  return reexport;
 }
 
 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella)
@

[llvm-branch-commits] [lld] 553284b - [lld-macho] Don't include absolute address value in expected test output

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T17:54:23-08:00
New Revision: 553284be2dded5b1d1106b380525e0b1e3b609cd

URL: 
https://github.com/llvm/llvm-project/commit/553284be2dded5b1d1106b380525e0b1e3b609cd
DIFF: 
https://github.com/llvm/llvm-project/commit/553284be2dded5b1d1106b380525e0b1e3b609cd.diff

LOG: [lld-macho] Don't include absolute address value in expected test output

Should fix the mattrs.ll failure introduced by rG29d3b0e47113.

Added: 


Modified: 
lld/test/MachO/mattrs.ll

Removed: 




diff  --git a/lld/test/MachO/mattrs.ll b/lld/test/MachO/mattrs.ll
index 66415013093b..8771f02e0c50 100644
--- a/lld/test/MachO/mattrs.ll
+++ b/lld/test/MachO/mattrs.ll
@@ -11,14 +11,14 @@
 
 ; FMA:  <_foo>:
 ; FMA-NEXT:vrcpss  %xmm0, %xmm0, %xmm1
-; FMA-NEXT:vfmsub213ss 7(%rip), %xmm1, %xmm0  # 2d4
+; FMA-NEXT:vfmsub213ss 7(%rip), %xmm1, %xmm0
 ; FMA-NEXT:vfnmadd132ss%xmm1, %xmm1, %xmm0
 ; FMA-NEXT:retq
 
 ; NO-FMA:  <_foo>:
 ; NO-FMA-NEXT: vrcpss  %xmm0, %xmm0, %xmm1
 ; NO-FMA-NEXT: vmulss  %xmm1, %xmm0, %xmm0
-; NO-FMA-NEXT: vmovss  16(%rip), %xmm2  # 2e0
+; NO-FMA-NEXT: vmovss  16(%rip), %xmm2
 ; NO-FMA-NEXT: vsubss  %xmm0, %xmm2, %xmm0
 ; NO-FMA-NEXT: vmulss  %xmm0, %xmm1, %xmm0
 ; NO-FMA-NEXT: vaddss  %xmm0, %xmm1, %xmm0



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] 349d5c9 - [lld-macho] Disable some tests that are failing on Windows

2020-12-10 Thread Jez Ng via llvm-branch-commits

Author: Jez Ng
Date: 2020-12-10T17:54:23-08:00
New Revision: 349d5c9cf79803187a2e1e2a080a8e460d03cf76

URL: 
https://github.com/llvm/llvm-project/commit/349d5c9cf79803187a2e1e2a080a8e460d03cf76
DIFF: 
https://github.com/llvm/llvm-project/commit/349d5c9cf79803187a2e1e2a080a8e460d03cf76.diff

LOG: [lld-macho] Disable some tests that are failing on Windows

lto-object-path.ll, like stabs.s, is disabled on Windows as the path
separators make it difficult to write a test that works across
platforms.

This diff also disables implicit-dylibs.s on Windows as we seem to emit
LC_LOAD_DYLIBs in a different order on that platform. This seems like a
bug in LLD that needs to be addressed (in a future diff).

Added: 


Modified: 
lld/test/MachO/implicit-dylibs.s
lld/test/MachO/lto-object-path.ll

Removed: 




diff  --git a/lld/test/MachO/implicit-dylibs.s 
b/lld/test/MachO/implicit-dylibs.s
index 2030da423020..21084b50006b 100644
--- a/lld/test/MachO/implicit-dylibs.s
+++ b/lld/test/MachO/implicit-dylibs.s
@@ -1,4 +1,6 @@
 # REQUIRES: x86
+## FIXME: We appear to emit LC_LOAD_DYLIBs in a 
diff erent order on Windows.
+# UNSUPPORTED: system-windows
 # RUN: rm -rf %t; split-file %s %t
 # RUN: mkdir -p %t/usr/lib/system
 

diff  --git a/lld/test/MachO/lto-object-path.ll 
b/lld/test/MachO/lto-object-path.ll
index b362bf45b133..b21b38f23527 100644
--- a/lld/test/MachO/lto-object-path.ll
+++ b/lld/test/MachO/lto-object-path.ll
@@ -1,4 +1,5 @@
 ; REQUIRES: x86
+; UNSUPPORTED: system-windows
 
 ; RUN: rm -rf %t; mkdir %t
 ; RUN: llvm-as %s -o %t/test.o



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits