[lld] r210612 - [mach-o] add support for parsing resolver functions

Nick Kledzik kledzik at apple.com
Tue Jun 10 17:24:16 PDT 2014


Author: kledzik
Date: Tue Jun 10 19:24:16 2014
New Revision: 210612

URL: http://llvm.org/viewvc/llvm-project?rev=210612&view=rev
Log:
[mach-o] add support for parsing resolver functions

Modified:
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
    lld/trunk/test/mach-o/parse-function.yaml

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=210612&r1=210611&r2=210612&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Tue Jun 10 19:24:16 2014
@@ -121,6 +121,7 @@ private:
   void         layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr);
   void         copySectionContent(SectionInfo *si, ContentBytes &content);
   uint8_t      scopeBits(const DefinedAtom* atom);
+  uint16_t     descBits(const DefinedAtom* atom);
   int          dylibOrdinal(const SharedLibraryAtom *sa);
   void         segIndexForSection(const SectionInfo *sect,
                              uint8_t &segmentIndex, uint64_t &segmentStartAddr);
@@ -589,6 +590,28 @@ uint8_t Util::scopeBits(const DefinedAto
   llvm_unreachable("Unknown scope");
 }
 
+uint16_t Util::descBits(const DefinedAtom* atom) {
+  uint16_t desc = 0;
+  switch (atom->merge()) {
+  case lld::DefinedAtom::mergeNo:
+  case lld::DefinedAtom::mergeAsTentative:
+    break;
+  case lld::DefinedAtom::mergeAsWeak:
+  case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
+    desc |= N_WEAK_DEF;
+    break;
+  case lld::DefinedAtom::mergeSameNameAndSize:
+  case lld::DefinedAtom::mergeByLargestSection:
+  case lld::DefinedAtom::mergeByContent:
+    llvm_unreachable("Unsupported DefinedAtom::merge()");
+    break;
+  }
+  if (atom->contentType() == lld::DefinedAtom::typeResolver)
+    desc |= N_SYMBOL_RESOLVER;
+  return desc;
+}
+
+
 bool Util::AtomSorter::operator()(const AtomAndIndex &left,
                                   const AtomAndIndex &right) {
   return (left.atom->name().compare(right.atom->name()) < 0);
@@ -634,7 +657,7 @@ void Util::addSymbols(const lld::File &a
     sym.type  = N_SECT;
     sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
     sym.sect  = ai.index;
-    sym.desc  = 0;
+    sym.desc  = descBits(static_cast<const DefinedAtom*>(ai.atom));
     sym.value = _atomToAddress[ai.atom];
     file.globalSymbols.push_back(sym);
   }

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp?rev=210612&r1=210611&r2=210612&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp Tue Jun 10 19:24:16 2014
@@ -52,6 +52,7 @@ struct MachORelocatableSectionToAtomType
 
 const MachORelocatableSectionToAtomType sectsToAtomType[] = {
   ENTRY("__TEXT", "__text",           S_REGULAR,          typeCode),
+  ENTRY("__TEXT", "__text",           S_REGULAR,          typeResolver),
   ENTRY("__TEXT", "__cstring",        S_CSTRING_LITERALS, typeCString),
   ENTRY("",       "",                 S_CSTRING_LITERALS, typeCString),
   ENTRY("__TEXT", "__ustring",        S_REGULAR,          typeUTF16String),
@@ -212,7 +213,7 @@ void appendSymbolsInSection(const std::v
 
 void atomFromSymbol(DefinedAtom::ContentType atomType, const Section &section,
                     MachOFile &file, uint64_t symbolAddr, StringRef symbolName,
-                    bool symbolWeakDef, Atom::Scope symbolScope,
+                    uint16_t symbolDescFlags, Atom::Scope symbolScope,
                     uint64_t nextSymbolAddr, bool copyRefs) {
   // Mach-O symbol table does have size in it. Instead the size is the
   // difference between this and the next symbol.
@@ -222,7 +223,7 @@ void atomFromSymbol(DefinedAtom::Content
   } else {
     uint64_t offset = symbolAddr - section.address;
     ArrayRef<uint8_t> atomContent = section.content.slice(offset, size);
-    DefinedAtom::Merge merge = symbolWeakDef 
+    DefinedAtom::Merge merge = (symbolDescFlags & N_WEAK_DEF) 
                               ? DefinedAtom::mergeAsWeak : DefinedAtom::mergeNo;
     if (atomType == DefinedAtom::typeUnknown) {
       // Mach-O needs a segment and section name.  Concatentate those two
@@ -233,6 +234,10 @@ void atomFromSymbol(DefinedAtom::Content
       file.addDefinedAtomInCustomSection(symbolName, symbolScope, atomType,
                                          merge, atomContent, segSectName, true);
     } else {
+      if ((atomType == lld::DefinedAtom::typeCode) && 
+          (symbolDescFlags & N_SYMBOL_RESOLVER)) {
+       atomType = lld::DefinedAtom::typeResolver;
+      }
       file.addDefinedAtom(symbolName, symbolScope, atomType, merge,
                           atomContent, copyRefs);
     }
@@ -283,22 +288,20 @@ error_code processSymboledSection(Define
   if (firstSymbolAddr != section.address) {
     // Section has anonymous content before first symbol.
     atomFromSymbol(atomType, section, file, section.address, StringRef(),
-                  false, Atom::scopeTranslationUnit, firstSymbolAddr, copyRefs);
+                  0, Atom::scopeTranslationUnit, firstSymbolAddr, copyRefs);
   }
 
   const Symbol *lastSym = nullptr;
-  bool lastSymIsWeakDef;
   for (const Symbol *sym : symbols) {
     if (lastSym != nullptr) {
       atomFromSymbol(atomType, section, file, lastSym->value, lastSym->name,
-                     lastSymIsWeakDef, atomScope(lastSym->scope), sym->value, copyRefs);
+                     lastSym->desc, atomScope(lastSym->scope), sym->value, copyRefs);
     }
     lastSym = sym;
-    lastSymIsWeakDef = (lastSym->desc & N_WEAK_DEF);
   }
   if (lastSym != nullptr) {
     atomFromSymbol(atomType, section, file, lastSym->value, lastSym->name,
-                   lastSymIsWeakDef, atomScope(lastSym->scope),
+                   lastSym->desc, atomScope(lastSym->scope),
                    section.address + section.content.size(), copyRefs);
   }
   return error_code();

Modified: lld/trunk/test/mach-o/parse-function.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/parse-function.yaml?rev=210612&r1=210611&r2=210612&view=diff
==============================================================================
--- lld/trunk/test/mach-o/parse-function.yaml (original)
+++ lld/trunk/test/mach-o/parse-function.yaml Tue Jun 10 19:24:16 2014
@@ -17,7 +17,8 @@ sections:
     alignment:       4
     address:         0x0000000000000000
     content:         [ 0xCC, 0xC3, 0x90, 0xC3, 0x90, 0x90, 0xC3, 0x90, 
-                       0x90, 0x90, 0xC3, 0x90, 0x90, 0x90, 0x90, 0xC3 ]
+                       0x90, 0x90, 0xC3, 0x90, 0x90, 0x90, 0x90, 0xC3,
+                       0x31, 0xC0, 0xC3 ]
 local-symbols:
   - name:            _myStatic
     type:            N_SECT
@@ -46,6 +47,12 @@ global-symbols:
     sect:            1
     desc:            [ N_WEAK_DEF ]
     value:           0x0000000000000007
+  - name:            _myResolver
+    type:            N_SECT
+    scope:           [ N_EXT ]
+    sect:            1
+    desc:            [ N_SYMBOL_RESOLVER ]
+    value:           0x0000000000000010
 ...
 
 # CHECK-NOT:  name:  
@@ -73,3 +80,9 @@ global-symbols:
 # CHECK-NOT:	scope:  global
 # CHECK-NOT:	scope:  hidden
 # CHECK:      content:         [ 90, 90, 90, 90, C3 ]
+
+# CHECK:      name:    _myResolver
+# CHECK:      scope:   global
+# CHECK:      type:    resolver
+# CHECK:      content:         [ 31, C0, C3 ]
+





More information about the llvm-commits mailing list