[lld] r259574 - Fix handling of mach header and DSO handle symbols.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 2 13:37:16 PST 2016


Author: pete
Date: Tue Feb  2 15:37:15 2016
New Revision: 259574

URL: http://llvm.org/viewvc/llvm-project?rev=259574&view=rev
Log:
Fix handling of mach header and DSO handle symbols.

The magic file which contained these symbols inherited from archive
which meant that the resolver didn't add the required atoms as archive
members only get added when referenced.  Instead we now inherit from
SimpleFile which always links in the atoms needed.

The second issue was in the handling of these symbols when we emit
the MachO.  The mach header symbol needs to be in the atom list as
it gets an offset (0), and being in the atom list makes sure it is
emitted to the symbol table.  DSO handles are not emitted to the
symbol table.

rdar://problem/24450654

Modified:
    lld/trunk/include/lld/Core/DefinedAtom.h
    lld/trunk/include/lld/Core/File.h
    lld/trunk/lib/Core/DefinedAtom.cpp
    lld/trunk/lib/Core/Resolver.cpp
    lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.h
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
    lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
    lld/trunk/test/mach-o/arm-interworking-movw.yaml

Modified: lld/trunk/include/lld/Core/DefinedAtom.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/DefinedAtom.h?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/DefinedAtom.h (original)
+++ lld/trunk/include/lld/Core/DefinedAtom.h Tue Feb  2 15:37:15 2016
@@ -147,6 +147,7 @@ public:
     typeTLVInitialZeroFill, // TLV initial zero fill data [Darwin]
     typeTLVInitializerPtr,  // pointer to thread local initializer [Darwin]
     typeMachHeader,         // atom representing mach_header [Darwin]
+    typeDSOHandle,          // atom representing DSO handle [Darwin]
     typeThreadZeroFill,     // Uninitialized thread local data(TBSS) [ELF]
     typeThreadData,         // Initialized thread local data(TDATA) [ELF]
     typeRONote,             // Identifies readonly note sections [ELF]

Modified: lld/trunk/include/lld/Core/File.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/File.h?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/File.h (original)
+++ lld/trunk/include/lld/Core/File.h Tue Feb  2 15:37:15 2016
@@ -50,6 +50,7 @@ public:
     kindMachObject,           ///< a MachO object file (.o)
     kindELFObject,            ///< a ELF object file (.o)
     kindCEntryObject,         ///< a file for CEntries
+    kindHeaderObject,         ///< a file for file headers
     kindEntryObject,          ///< a file for the entry
     kindUndefinedSymsObject,  ///< a file for undefined symbols
     kindAliasSymsObject,      ///< a file for alias symbols

Modified: lld/trunk/lib/Core/DefinedAtom.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/DefinedAtom.cpp?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/lib/Core/DefinedAtom.cpp (original)
+++ lld/trunk/lib/Core/DefinedAtom.cpp Tue Feb  2 15:37:15 2016
@@ -79,6 +79,7 @@ DefinedAtom::ContentPermissions DefinedA
   case typeUnknown:
   case typeTempLTO:
   case typeSectCreate:
+  case typeDSOHandle:
     return permUnknown;
   }
   llvm_unreachable("unknown content type");

Modified: lld/trunk/lib/Core/Resolver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/Resolver.cpp?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/lib/Core/Resolver.cpp (original)
+++ lld/trunk/lib/Core/Resolver.cpp Tue Feb  2 15:37:15 2016
@@ -343,6 +343,7 @@ bool Resolver::resolveUndefines() {
     case File::kindMachObject:
     case File::kindELFObject:
     case File::kindCEntryObject:
+    case File::kindHeaderObject:
     case File::kindEntryObject:
     case File::kindUndefinedSymsObject:
     case File::kindAliasSymsObject:

Modified: lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.h?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.h (original)
+++ lld/trunk/lib/ReaderWriter/MachO/ExecutableAtoms.h Tue Feb  2 15:37:15 2016
@@ -11,10 +11,10 @@
 #define LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H
 
 #include "Atoms.h"
+#include "File.h"
 
 #include "llvm/Support/MachO.h"
 
-#include "lld/Core/ArchiveLibraryFile.h"
 #include "lld/Core/DefinedAtom.h"
 #include "lld/Core/File.h"
 #include "lld/Core/LinkingContext.h"
@@ -65,45 +65,57 @@ private:
 // MachHeaderAliasFile lazily instantiates the magic symbols that mark the start
 // of the mach_header for final linked images.
 //
-class MachHeaderAliasFile : public ArchiveLibraryFile {
+class MachHeaderAliasFile : public SimpleFile {
 public:
   MachHeaderAliasFile(const MachOLinkingContext &context)
-      : ArchiveLibraryFile("mach_header symbols") {
-      switch (context.outputMachOType()) {
-      case llvm::MachO::MH_EXECUTE:
-        _machHeaderSymbolName = "__mh_execute_header";
-        break;
-      case llvm::MachO::MH_DYLIB:
-        _machHeaderSymbolName = "__mh_dylib_header";
-        break;
-      case llvm::MachO::MH_BUNDLE:
-        _machHeaderSymbolName = "__mh_bundle_header";
-        break;
-      case llvm::MachO::MH_DYLINKER:
-        _machHeaderSymbolName = "__mh_dylinker_header";
-        break;
-      case llvm::MachO::MH_PRELOAD:
-        _machHeaderSymbolName = "__mh_preload_header";
-        break;
-      default:
-        llvm_unreachable("no mach_header symbol for file type");
-      }
-  }
-
-  std::error_code
-  parseAllMembers(std::vector<std::unique_ptr<File>> &result) override {
-    return std::error_code();
-  }
-
-  File *find(StringRef sym, bool dataSymbolOnly) override {
-    if (sym.equals("___dso_handle") || sym.equals(_machHeaderSymbolName)) {
+    : SimpleFile("mach_header symbols", kindHeaderObject) {
+    StringRef machHeaderSymbolName;
+    StringRef dsoHandleName;
+    switch (context.outputMachOType()) {
+    case llvm::MachO::MH_OBJECT:
+      machHeaderSymbolName = "__mh_object_header";
+      break;
+    case llvm::MachO::MH_EXECUTE:
+      machHeaderSymbolName = "__mh_execute_header";
+      dsoHandleName = "___dso_handle";
+      break;
+    case llvm::MachO::MH_FVMLIB:
+      llvm_unreachable("no mach_header symbol for file type");
+    case llvm::MachO::MH_CORE:
+      llvm_unreachable("no mach_header symbol for file type");
+    case llvm::MachO::MH_PRELOAD:
+      llvm_unreachable("no mach_header symbol for file type");
+    case llvm::MachO::MH_DYLIB:
+      machHeaderSymbolName = "__mh_dylib_header";
+      dsoHandleName = "___dso_handle";
+      break;
+    case llvm::MachO::MH_DYLINKER:
+      machHeaderSymbolName = "__mh_dylinker_header";
+      dsoHandleName = "___dso_handle";
+      break;
+    case llvm::MachO::MH_BUNDLE:
+      machHeaderSymbolName = "__mh_bundle_header";
+      dsoHandleName = "___dso_handle";
+      break;
+    case llvm::MachO::MH_DYLIB_STUB:
+      llvm_unreachable("no mach_header symbol for file type");
+    case llvm::MachO::MH_DSYM:
+      llvm_unreachable("no mach_header symbol for file type");
+    case llvm::MachO::MH_KEXT_BUNDLE:
+      dsoHandleName = "___dso_handle";
+      break;
+    }
+    if (!machHeaderSymbolName.empty())
       _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
-          *this, sym, DefinedAtom::scopeLinkageUnit,
+          *this, machHeaderSymbolName, DefinedAtom::scopeLinkageUnit,
           DefinedAtom::typeMachHeader, DefinedAtom::mergeNo, false, false,
           ArrayRef<uint8_t>(), DefinedAtom::Alignment(4096)));
-      return this;
-    }
-    return nullptr;
+
+    if (!dsoHandleName.empty())
+      _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
+          *this, dsoHandleName, DefinedAtom::scopeLinkageUnit,
+          DefinedAtom::typeDSOHandle, DefinedAtom::mergeNo, false, false,
+          ArrayRef<uint8_t>(), DefinedAtom::Alignment(1)));
   }
 
   const AtomVector<DefinedAtom> &defined() const override {
@@ -124,7 +136,6 @@ public:
 
 private:
   mutable AtomVector<DefinedAtom> _definedAtoms;
-  StringRef _machHeaderSymbolName;
 };
 
 } // namespace mach_o

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Tue Feb  2 15:37:15 2016
@@ -242,6 +242,7 @@ struct MachOFinalSectionFromAtomType {
 
 const MachOFinalSectionFromAtomType sectsToAtomType[] = {
   ENTRY("__TEXT", "__text",           S_REGULAR,          typeCode),
+  ENTRY("__TEXT", "__text",           S_REGULAR,          typeMachHeader),
   ENTRY("__TEXT", "__cstring",        S_CSTRING_LITERALS, typeCString),
   ENTRY("__TEXT", "__ustring",        S_REGULAR,          typeUTF16String),
   ENTRY("__TEXT", "__const",          S_REGULAR,          typeConstant),
@@ -385,7 +386,12 @@ void Util::processAtomAttributes(const D
 }
 
 void Util::assignAtomToSection(const DefinedAtom *atom) {
-  if (atom->contentType() == DefinedAtom::typeMachHeader)
+  if (atom->contentType() == DefinedAtom::typeMachHeader) {
+    _machHeaderAliasAtoms.push_back(atom);
+    // Assign atom to this section with this offset.
+    AtomInfo ai = {atom, 0};
+    sectionForAtom(atom)->atomsAndOffsets.push_back(ai);
+  } else if (atom->contentType() == DefinedAtom::typeDSOHandle)
     _machHeaderAliasAtoms.push_back(atom);
   else
     appendAtom(sectionForAtom(atom), atom);

Modified: lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp Tue Feb  2 15:37:15 2016
@@ -435,6 +435,7 @@ template <> struct ScalarEnumerationTrai
     io.enumCase(value, "tlv-initializer-ptr",
                                           DefinedAtom::typeTLVInitializerPtr);
     io.enumCase(value, "mach_header",     DefinedAtom::typeMachHeader);
+    io.enumCase(value, "dso_handle",      DefinedAtom::typeDSOHandle);
     io.enumCase(value, "thread-data",     DefinedAtom::typeThreadData);
     io.enumCase(value, "thread-zero-fill",DefinedAtom::typeThreadZeroFill);
     io.enumCase(value, "ro-note",         DefinedAtom::typeRONote);

Modified: lld/trunk/test/mach-o/arm-interworking-movw.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/arm-interworking-movw.yaml?rev=259574&r1=259573&r2=259574&view=diff
==============================================================================
--- lld/trunk/test/mach-o/arm-interworking-movw.yaml (original)
+++ lld/trunk/test/mach-o/arm-interworking-movw.yaml Tue Feb  2 15:37:15 2016
@@ -2,7 +2,7 @@
 # RUN: lld -flavor darwin -arch armv7 -r -print_atoms %s -o %t  | FileCheck %s
 # RUN: lld -flavor darwin -arch armv7 -dylib -print_atoms %t -o %t2 \
 # RUN:     %p/Inputs/libSystem.yaml -sectalign __TEXT __text 0x1000  | FileCheck %s
-# RUN: llvm-objdump -d -macho %t2 | FileCheck -check-prefix=CODE %s
+# RUN: llvm-objdump -d -macho -no-symbolic-operands %t2 | FileCheck -check-prefix=CODE %s
 #
 # Test thumb and arm branches round trip through -r.
 # Test movw/movt pairs have low bit set properly for thumb vs arm.
@@ -330,7 +330,7 @@ local-symbols:
 # CODE-NEXT:                   movt	r0, #0
 # CODE-NEXT:                   movw	r1, #1
 # CODE-NEXT:                   movt	r1, #0
-# CODE-NEXT:                   movw	r2, _a2
+# CODE-NEXT:                   movw	r2, #4174
 # CODE-NEXT:                   movt	r2, #0
 # CODE-NEXT:                   movw	r3, #42
 # CODE-NEXT:                   movt	r3, #0
@@ -341,7 +341,7 @@ local-symbols:
 # CODE-NEXT:                   movt	r0, #0
 # CODE-NEXT:                   movw	r1, #65495
 # CODE-NEXT:                   movt	r1, #65535
-# CODE-NEXT:                   movw	r2, _a2
+# CODE-NEXT:                   movw	r2, #4174
 # CODE-NEXT:                   movt	r2, #0
 # CODE-NEXT:                   movw	r3, #0
 # CODE-NEXT:                   movt	r3, #0




More information about the llvm-commits mailing list