<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Sep 28, 2015 at 1:25 PM, Lang Hames via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: lhames<br>
Date: Mon Sep 28 15:25:14 2015<br>
New Revision: 248732<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=248732&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=248732&view=rev</a><br>
Log:<br>
[lld][MachO] Initial implementation of -flat_namespace and -undefined.<br>
<br>
This is a basic initial implementation of the -flat_namespace and<br>
-undefined options for LLD-darwin. It ignores several subtlties,<br>
but the result is close enough that we can now link LLVM (but not<br></blockquote><div><br></div><div>That's great. Congratulations!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Added:<br>
    lld/trunk/lib/ReaderWriter/MachO/FlatNamespaceFile.h<br>
    lld/trunk/test/mach-o/flat_namespace_undef_error.yaml<br>
    lld/trunk/test/mach-o/flat_namespace_undef_suppress.yaml<br>
    lld/trunk/test/mach-o/twolevel_namespace_undef_dynamic_lookup.yaml<br>
    lld/trunk/test/mach-o/twolevel_namespace_undef_warning_suppress.yaml<br>
Modified:<br>
    lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h<br>
    lld/trunk/lib/Driver/DarwinLdDriver.cpp<br>
    lld/trunk/lib/Driver/DarwinLdOptions.td<br>
    lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp<br>
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp<br>
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp<br>
<br>
Modified: lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=248732&r1=248731&r2=248732&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=248732&r1=248731&r2=248732&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h (original)<br>
+++ lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h Mon Sep 28 15:25:14 2015<br>
@@ -63,6 +63,13 @@ public:<br>
     noDebugMap      // -S option<br>
   };<br>
<br>
+  enum class UndefinedMode {<br>
+    error,<br>
+    warning,<br>
+    suppress,<br>
+    dynamicLookup<br>
+  };<br>
+<br>
   /// Initializes the context to sane default values given the specified output<br>
   /// file type, arch, os, and minimum os version.  This should be called before<br>
   /// other setXXX() methods.<br>
@@ -204,6 +211,30 @@ public:<br>
   /// when linking a binary that does not use any of its symbols.<br>
   bool deadStrippableDylib() const { return _deadStrippableDylib; }<br>
<br>
+  /// \brief Whether or not to use flat namespace.<br>
+  ///<br>
+  /// MachO usually uses a two-level namespace, where each external symbol<br>
+  /// referenced by the target is associated with the dylib that will provide<br>
+  /// the symbol's definition at runtime. Using flat namespace overrides this<br>
+  /// behavior: the linker searches all dylibs on the command line and all<br>
+  /// dylibs those original dylibs depend on, but does not record which dylib<br>
+  /// an external symbol came from. At runtime dyld again searches all images<br>
+  /// and uses the first definition it finds. In addition, any undefines in<br>
+  /// loaded flat_namespace dylibs must be resolvable at build time.<br>
+  bool useFlatNamespace() const { return _flatNamespace; }<br>
+<br>
+  /// \brief How to handle undefined symbols.<br>
+  ///<br>
+  /// Options are:<br>
+  ///  * error: Report an error and terminate linking.<br>
+  ///  * warning: Report a warning, but continue linking.<br>
+  ///  * suppress: Ignore and continue linking.<br>
+  ///  * dynamic_lookup: For use with -twolevel namespace: Records source dylibs<br>
+  ///    for symbols that are defined in a linked dylib at static link time.<br>
+  ///    Undefined symbols are handled by searching all loaded images at<br>
+  ///    runtime.<br>
+  UndefinedMode undefinedMode() const { return _undefinedMode; }<br>
+<br>
   /// \brief The path to the executable that will load the bundle at runtime.<br>
   ///<br>
   /// When building a Mach-O bundle, this executable will be examined if there<br>
@@ -218,6 +249,14 @@ public:<br>
   void setDeadStrippableDylib(bool deadStrippable) {<br>
     _deadStrippableDylib = deadStrippable;<br>
   }<br>
+  void setUseFlatNamespace(bool flatNamespace) {<br>
+    _flatNamespace = flatNamespace;<br>
+  }<br>
+<br>
+  void setUndefinedMode(UndefinedMode undefinedMode) {<br>
+    _undefinedMode = undefinedMode;<br>
+  }<br>
+<br>
   void setBundleLoader(StringRef loader) { _bundleLoader = loader; }<br>
   void setPrintAtoms(bool value=true) { _printAtoms = value; }<br>
   void setTestingFileUsage(bool value = true) {<br>
@@ -301,6 +340,11 @@ public:<br>
   bool customAtomOrderer(const DefinedAtom *left, const DefinedAtom *right,<br>
                          bool &leftBeforeRight) const;<br>
<br>
+  /// Return the 'flat namespace' file. This is the file that supplies<br>
+  /// atoms for otherwise undefined symbols when the -flat_namespace or<br>
+  /// -undefined dynamic_lookup options are used.<br>
+  File* flatNamespaceFile() const { return _flatNamespaceFile; }<br>
+<br>
 private:<br>
   Writer &writer() const override;<br>
   mach_o::MachODylibFile* loadIndirectDylib(StringRef path);<br>
@@ -349,6 +393,8 @@ private:<br>
   uint32_t _currentVersion;<br>
   StringRef _installName;<br>
   StringRefVector _rpaths;<br>
+  bool _flatNamespace;<br>
+  UndefinedMode _undefinedMode;<br>
   bool _deadStrippableDylib;<br>
   bool _printAtoms;<br>
   bool _testingFileUsage;<br>
@@ -369,6 +415,7 @@ private:<br>
   std::unique_ptr<llvm::raw_fd_ostream> _dependencyInfo;<br>
   llvm::StringMap<std::vector<OrderFileNode>> _orderFiles;<br>
   unsigned _orderFileEntries;<br>
+  File *_flatNamespaceFile;<br>
 };<br>
<br>
 } // end namespace lld<br>
<br>
Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=248732&r1=248731&r2=248732&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=248732&r1=248731&r2=248732&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)<br>
+++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Mon Sep 28 15:25:14 2015<br>
@@ -751,6 +751,49 @@ bool DarwinLdDriver::parse(llvm::ArrayRe<br>
     }<br>
   }<br>
<br>
+  // Handle -flat_namespace.<br>
+  if (llvm::opt::Arg *ns =<br>
+          parsedArgs.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace)) {<br>
+    if (ns->getOption().getID() == OPT_flat_namespace)<br>
+      ctx.setUseFlatNamespace(true);<br>
+  }<br>
+<br>
+  // Handle -undefined<br>
+  if (llvm::opt::Arg *undef = parsedArgs.getLastArg(OPT_undefined)) {<br>
+    MachOLinkingContext::UndefinedMode UndefMode;<br>
+    if (StringRef(undef->getValue()).equals("error"))<br>
+      UndefMode = MachOLinkingContext::UndefinedMode::error;<br>
+    else if (StringRef(undef->getValue()).equals("warning"))<br>
+      UndefMode = MachOLinkingContext::UndefinedMode::warning;<br>
+    else if (StringRef(undef->getValue()).equals("suppress"))<br>
+      UndefMode = MachOLinkingContext::UndefinedMode::suppress;<br>
+    else if (StringRef(undef->getValue()).equals("dynamic_lookup"))<br>
+      UndefMode = MachOLinkingContext::UndefinedMode::dynamicLookup;<br>
+    else {<br>
+      diagnostics << "error: invalid option to -undefined "<br>
+                     "[ warning | error | suppress | dynamic_lookup ]\n";<br>
+      return false;<br>
+    }<br>
+<br>
+    if (ctx.useFlatNamespace()) {<br>
+      // If we're using -flat_namespace then 'warning', 'suppress' and<br>
+      // 'dynamic_lookup' are all equivalent, so map them to 'suppress'.<br>
+      if (UndefMode != MachOLinkingContext::UndefinedMode::error)<br>
+        UndefMode = MachOLinkingContext::UndefinedMode::suppress;<br>
+    } else {<br>
+      // If we're using -twolevel_namespace then 'warning' and 'suppress' are<br>
+      // illegal. Emit a diagnostic if they've been (mis)used.<br>
+      if (UndefMode == MachOLinkingContext::UndefinedMode::warning ||<br>
+          UndefMode == MachOLinkingContext::UndefinedMode::suppress) {<br>
+        diagnostics << "error: can't use -undefined warning or suppress with "<br>
+                       "-twolevel_namespace\n";<br>
+        return false;<br>
+      }<br>
+    }<br>
+<br>
+    ctx.setUndefinedMode(UndefMode);<br>
+  }<br>
+<br>
   // Handle -rpath <path><br>
   if (parsedArgs.hasArg(OPT_rpath)) {<br>
     switch (ctx.outputMachOType()) {<br>
<br>
Modified: lld/trunk/lib/Driver/DarwinLdOptions.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdOptions.td?rev=248732&r1=248731&r2=248732&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdOptions.td?rev=248732&r1=248731&r2=248732&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/Driver/DarwinLdOptions.td (original)<br>
+++ lld/trunk/lib/Driver/DarwinLdOptions.td Mon Sep 28 15:25:14 2015<br>
@@ -55,6 +55,19 @@ def order_file : Separate<["-"], "order_<br>
      MetaVarName<"<file-path>">,<br>
      HelpText<"re-order and move specified symbols to start of their section">,<br>
      Group<grp_opts>;<br>
+def flat_namespace : Flag<["-"], "flat_namespace">,<br>
+     HelpText<"Resolves symbols in any (transitively) linked dynamic libraries. "<br>
+              "Source libraries are not recorded: dyld will re-search all "<br>
+              "images at runtime and use the first definition found.">,<br>
+     Group<grp_opts>;<br>
+def twolevel_namespace : Flag<["-"], "twolevel_namespace">,<br>
+     HelpText<"Resolves symbols in listed libraries only. Source libraries are "<br>
+              "recorded in the symbol table.">,<br>
+     Group<grp_opts>;<br>
+def undefined : Separate<["-"], "undefined">,<br>
+                MetaVarName<"<undefined>">,<br>
+                HelpText<"Determines how undefined symbols are handled.">,<br>
+                Group<grp_opts>;<br>
<br>
 // main executable options<br>
 def grp_main : OptionGroup<"opts">, HelpText<"MAIN EXECUTABLE OPTIONS">;<br>
<br>
Added: lld/trunk/lib/ReaderWriter/MachO/FlatNamespaceFile.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/FlatNamespaceFile.h?rev=248732&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/FlatNamespaceFile.h?rev=248732&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/MachO/FlatNamespaceFile.h (added)<br>
+++ lld/trunk/lib/ReaderWriter/MachO/FlatNamespaceFile.h Mon Sep 28 15:25:14 2015<br>
@@ -0,0 +1,61 @@<br>
+//===- lib/ReaderWriter/MachO/FlatNamespaceFile.h -------------------------===//<br>
+//<br>
+//                             The LLVM Linker<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLD_READER_WRITER_MACHO_FLAT_NAMESPACE_FILE_H<br>
+#define LLD_READER_WRITER_MACHO_FLAT_NAMESPACE_FILE_H<br>
+<br>
+#include "lld/Core/SharedLibraryFile.h"<br>
+#include "llvm/Support/Debug.h"<br>
+<br>
+namespace lld {<br>
+namespace mach_o {<br>
+<br>
+//<br>
+// A FlateNamespaceFile instance may be added as a resolution source of last<br>
+// resort, depending on how -flat_namespace and -undefined are set.<br>
+//<br>
+class FlatNamespaceFile : public SharedLibraryFile {<br>
+public:<br>
+    FlatNamespaceFile(const MachOLinkingContext &context, bool warnOnUndef)<br>
+      : SharedLibraryFile("flat namespace") { }<br>
+<br>
+  const SharedLibraryAtom *exports(StringRef name,<br>
+                                   bool dataSymbolOnly) const override {<br>
+    _sharedLibraryAtoms.push_back(<br>
+      new (allocator()) MachOSharedLibraryAtom(*this, name, getDSOName(),<br>
+                                               false));<br>
+<br>
+    return _sharedLibraryAtoms.back();<br>
+  }<br>
+<br>
+  StringRef getDSOName() const override { return "flat-namespace"; }<br>
+<br>
+  const AtomVector<DefinedAtom> &defined() const override {<br>
+    return _noDefinedAtoms;<br>
+  }<br>
+  const AtomVector<UndefinedAtom> &undefined() const override {<br>
+    return _noUndefinedAtoms;<br>
+  }<br>
+<br>
+  const AtomVector<SharedLibraryAtom> &sharedLibrary() const override {<br>
+    return _sharedLibraryAtoms;<br>
+  }<br>
+<br>
+  const AtomVector<AbsoluteAtom> &absolute() const override {<br>
+    return _noAbsoluteAtoms;<br>
+  }<br>
+<br>
+private:<br>
+  mutable AtomVector<SharedLibraryAtom> _sharedLibraryAtoms;<br>
+};<br>
+<br>
+} // namespace mach_o<br>
+} // namespace lld<br>
+<br>
+#endif // LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp?rev=248732&r1=248731&r2=248732&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp?rev=248732&r1=248731&r2=248732&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp Mon Sep 28 15:25:14 2015<br>
@@ -10,6 +10,7 @@<br>
 #include "lld/ReaderWriter/MachOLinkingContext.h"<br>
 #include "ArchHandler.h"<br>
 #include "File.h"<br>
+#include "FlatNamespaceFile.h"<br>
 #include "MachONormalizedFile.h"<br>
 #include "MachOPasses.h"<br>
 #include "lld/Core/ArchiveLibraryFile.h"<br>
@@ -143,10 +144,12 @@ MachOLinkingContext::MachOLinkingContext<br>
       _doNothing(false), _pie(false), _arch(arch_unknown), _os(OS::macOSX),<br>
       _osMinVersion(0), _pageZeroSize(0), _pageSize(4096), _baseAddress(0),<br>
       _stackSize(0), _compatibilityVersion(0), _currentVersion(0),<br>
+      _flatNamespace(false), _undefinedMode(UndefinedMode::error),<br>
       _deadStrippableDylib(false), _printAtoms(false), _testingFileUsage(false),<br>
       _keepPrivateExterns(false), _demangle(false), _archHandler(nullptr),<br>
       _exportMode(ExportMode::globals),<br>
-      _debugInfoMode(DebugInfoMode::addDebugMap), _orderFileEntries(0) {}<br>
+      _debugInfoMode(DebugInfoMode::addDebugMap), _orderFileEntries(0),<br>
+      _flatNamespaceFile(nullptr) {}<br>
<br>
 MachOLinkingContext::~MachOLinkingContext() {}<br>
<br>
@@ -716,6 +719,15 @@ void MachOLinkingContext::createImplicit<br>
<br>
   // Let writer add output type specific extras.<br>
   writer().createImplicitFiles(result);<br>
+<br>
+  // If we're using flat namespace or undefinedMode is != error, add a<br>
+  // FlatNamespaceFile instance. This will provide a SharedLibraryAtom for<br>
+  // symbols that aren't defined elsewhere.<br>
+  if (useFlatNamespace() && undefinedMode() != UndefinedMode::error) {<br>
+    bool warnOnUndef = undefinedMode() == UndefinedMode::warning;<br>
+    result.emplace_back(new mach_o::FlatNamespaceFile(*this, warnOnUndef));<br>
+    _flatNamespaceFile = result.back().get();<br>
+  }<br>
 }<br>
<br>
<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=248732&r1=248731&r2=248732&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=248732&r1=248731&r2=248732&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp Mon Sep 28 15:25:14 2015<br>
@@ -1042,7 +1042,12 @@ void MachOFileLayout::buildBindInfo() {<br>
     _bindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB<br>
                             | entry.segIndex);<br>
     _bindingInfo.append_uleb128(entry.segOffset);<br>
-    _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | entry.ordinal);<br>
+    if (entry.ordinal > 0)<br>
+      _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |<br>
+                               (entry.ordinal & 0xF));<br>
+    else<br>
+      _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |<br>
+                               (entry.ordinal & 0xF));<br>
     _bindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);<br>
     _bindingInfo.append_string(entry.symbolName);<br>
     if (entry.addend != lastAddend) {<br>
@@ -1062,7 +1067,12 @@ void MachOFileLayout::buildLazyBindInfo(<br>
     _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB<br>
                             | entry.segIndex);<br>
     _lazyBindingInfo.append_uleb128Fixed(entry.segOffset, 5);<br>
-    _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | entry.ordinal);<br>
+    if (entry.ordinal > 0)<br>
+      _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |<br>
+                                   (entry.ordinal & 0xF));<br>
+    else<br>
+      _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |<br>
+                                   (entry.ordinal & 0xF));<br>
     _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);<br>
     _lazyBindingInfo.append_string(entry.symbolName);<br>
     _lazyBindingInfo.append_byte(BIND_OPCODE_DO_BIND);<br>
<br>
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=248732&r1=248731&r2=248732&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=248732&r1=248731&r2=248732&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)<br>
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Mon Sep 28 15:25:14 2015<br>
@@ -855,7 +855,9 @@ std::error_code Util::addSymbols(const l<br>
     Symbol sym;<br>
     uint16_t desc = 0;<br>
     if (!rMode) {<br>
-      uint8_t ordinal = dylibOrdinal(dyn_cast<SharedLibraryAtom>(ai.atom));<br>
+      uint8_t ordinal = 0;<br>
+      if (!_ctx.useFlatNamespace())<br>
+        ordinal = dylibOrdinal(dyn_cast<SharedLibraryAtom>(ai.atom));<br>
       llvm::MachO::SET_LIBRARY_ORDINAL(desc, ordinal);<br>
     }<br>
     <a href="http://sym.name" rel="noreferrer" target="_blank">sym.name</a>  = ai.atom->name();<br>
@@ -953,16 +955,28 @@ void Util::addDependentDylibs(const lld:<br>
     DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);<br>
     if (pos == _dylibInfo.end()) {<br>
       DylibInfo info;<br>
-      info.ordinal = ordinal++;<br>
+      bool flatNamespaceAtom = &slAtom->file() == _ctx.flatNamespaceFile();<br>
+<br>
+      // If we're in -flat_namespace mode (or this atom came from the flat<br>
+      // namespace file under -undefined dynamic_lookup) then use the flat<br>
+      // lookup ordinal.<br>
+      if (flatNamespaceAtom || _ctx.useFlatNamespace())<br>
+        info.ordinal = BIND_SPECIAL_DYLIB_FLAT_LOOKUP;<br>
+      else<br>
+        info.ordinal = ordinal++;<br>
       info.hasWeak = slAtom->canBeNullAtRuntime();<br>
       info.hasNonWeak = !info.hasWeak;<br>
       _dylibInfo[loadPath] = info;<br>
-      DependentDylib depInfo;<br>
-      depInfo.path = loadPath;<br>
-      depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;<br>
-      depInfo.currentVersion = _ctx.dylibCurrentVersion(loadPath);<br>
-      depInfo.compatVersion = _ctx.dylibCompatVersion(loadPath);<br>
-      nFile.dependentDylibs.push_back(depInfo);<br>
+<br>
+      // Unless this was a flat_namespace atom, record the source dylib.<br>
+      if (!flatNamespaceAtom) {<br>
+        DependentDylib depInfo;<br>
+        depInfo.path = loadPath;<br>
+        depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;<br>
+        depInfo.currentVersion = _ctx.dylibCurrentVersion(loadPath);<br>
+        depInfo.compatVersion = _ctx.dylibCompatVersion(loadPath);<br>
+        nFile.dependentDylibs.push_back(depInfo);<br>
+      }<br>
     } else {<br>
       if ( slAtom->canBeNullAtRuntime() )<br>
         pos->second.hasWeak = true;<br>
@@ -1180,7 +1194,9 @@ uint32_t Util::fileFlags() {<br>
   if (_ctx.outputMachOType() == MH_OBJECT) {<br>
     return MH_SUBSECTIONS_VIA_SYMBOLS;<br>
   } else {<br>
-    uint32_t flags = MH_DYLDLINK | MH_NOUNDEFS | MH_TWOLEVEL;<br>
+    uint32_t flags = MH_DYLDLINK;<br>
+    if (!_ctx.useFlatNamespace())<br>
+        flags |= MH_TWOLEVEL | MH_NOUNDEFS;<br>
     if ((_ctx.outputMachOType() == MH_EXECUTE) && _ctx.PIE())<br>
       flags |= MH_PIE;<br>
     if (_hasTLVDescriptors)<br>
<br>
Added: lld/trunk/test/mach-o/flat_namespace_undef_error.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/flat_namespace_undef_error.yaml?rev=248732&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/flat_namespace_undef_error.yaml?rev=248732&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/mach-o/flat_namespace_undef_error.yaml (added)<br>
+++ lld/trunk/test/mach-o/flat_namespace_undef_error.yaml Mon Sep 28 15:25:14 2015<br>
@@ -0,0 +1,17 @@<br>
+# RUN: not lld -flavor darwin -arch x86_64 -macosx_version_min 10.9 -flat_namespace -undefined error %s -o %t %p/Inputs/libSystem.yaml 2>&1 | FileCheck %s<br>
+<br>
+--- !native<br>
+defined-atoms:<br>
+  - name:            _main<br>
+    scope:           global<br>
+    content:         [ E9, 00, 00, 00, 00 ]<br>
+    alignment:       16<br>
+    references:<br>
+      - kind:            branch32<br>
+        offset:          1<br>
+        target:          _bar<br>
+undefined-atoms:<br>
+  - name:            _bar<br>
+<br>
+# Make sure we error out for -flat_namespace -undefined error.<br>
+# CHECK: Undefined symbol: : _bar<br>
<br>
Added: lld/trunk/test/mach-o/flat_namespace_undef_suppress.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/flat_namespace_undef_suppress.yaml?rev=248732&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/flat_namespace_undef_suppress.yaml?rev=248732&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/mach-o/flat_namespace_undef_suppress.yaml (added)<br>
+++ lld/trunk/test/mach-o/flat_namespace_undef_suppress.yaml Mon Sep 28 15:25:14 2015<br>
@@ -0,0 +1,17 @@<br>
+# RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 10.9 -flat_namespace -undefined suppress %s -o %t %p/Inputs/libSystem.yaml<br>
+#<br>
+# Sanity check '-flat_namespace -undefined suppress'.<br>
+# This should pass without error, even though '_bar' is undefined.<br>
+<br>
+--- !native<br>
+defined-atoms:<br>
+  - name:            _main<br>
+    scope:           global<br>
+    content:         [ E9, 00, 00, 00, 00 ]<br>
+    alignment:       16<br>
+    references:<br>
+      - kind:            branch32<br>
+        offset:          1<br>
+        target:          _bar<br>
+undefined-atoms:<br>
+  - name:            _bar<br>
<br>
Added: lld/trunk/test/mach-o/twolevel_namespace_undef_dynamic_lookup.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/twolevel_namespace_undef_dynamic_lookup.yaml?rev=248732&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/twolevel_namespace_undef_dynamic_lookup.yaml?rev=248732&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/mach-o/twolevel_namespace_undef_dynamic_lookup.yaml (added)<br>
+++ lld/trunk/test/mach-o/twolevel_namespace_undef_dynamic_lookup.yaml Mon Sep 28 15:25:14 2015<br>
@@ -0,0 +1,17 @@<br>
+# RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 19.9 -twolevel_namespace -undefined dynamic_lookup %s -o %t %p/Inputs/libSystem.yaml<br>
+#<br>
+# Sanity check '-twolevel_namespace -undefined dynamic_lookup'.<br>
+# This should pass without error, even though '_bar' is undefined.<br>
+<br>
+--- !native<br>
+defined-atoms:<br>
+  - name:            _main<br>
+    scope:           global<br>
+    content:         [ E9, 00, 00, 00, 00 ]<br>
+    alignment:       16<br>
+    references:<br>
+      - kind:            branch32<br>
+        offset:          1<br>
+        target:          _bar<br>
+undefined-atoms:<br>
+  - name:            _bar<br>
<br>
Added: lld/trunk/test/mach-o/twolevel_namespace_undef_warning_suppress.yaml<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/twolevel_namespace_undef_warning_suppress.yaml?rev=248732&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/twolevel_namespace_undef_warning_suppress.yaml?rev=248732&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/mach-o/twolevel_namespace_undef_warning_suppress.yaml (added)<br>
+++ lld/trunk/test/mach-o/twolevel_namespace_undef_warning_suppress.yaml Mon Sep 28 15:25:14 2015<br>
@@ -0,0 +1,23 @@<br>
+# RUN: not lld -flavor darwin -arch x86_64 -macosx_version_min 10.9 -twolevel_namespace -undefined warning %s -o %t %p/Inputs/libSystem.yaml 2>&1 | \<br>
+# RUN:   FileCheck --check-prefix=CHECK-WARNING %s<br>
+# RUN: not lld -flavor darwin -arch x86_64 -macosx_version_min 10.9 -twolevel_namespace -undefined suppress %s -o %t %p/Inputs/libSystem.yaml 2>&1 | \<br>
+# RUN:   FileCheck --check-prefix=CHECK-SUPPRESS %s<br>
+<br>
+--- !native<br>
+defined-atoms:<br>
+  - name:            _main<br>
+    scope:           global<br>
+    content:         [ E9, 00, 00, 00, 00 ]<br>
+    alignment:       16<br>
+    references:<br>
+      - kind:            branch32<br>
+        offset:          1<br>
+        target:          _bar<br>
+undefined-atoms:<br>
+  - name:            _bar<br>
+<br>
+# Make sure that the driver issues an error diagnostic about this combination<br>
+# being invalid.<br>
+#<br>
+# CHECK-WARNING:  can't use -undefined warning or suppress with -twolevel_namespace<br>
+# CHECK-SUPPRESS: can't use -undefined warning or suppress with -twolevel_namespace<br>
\ No newline at end of file<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>