]> git.saurik.com Git - apple/ld64.git/blobdiff - src/ld/parsers/macho_relocatable_file.cpp
ld64-274.1.tar.gz
[apple/ld64.git] / src / ld / parsers / macho_relocatable_file.cpp
index 50f53273d2e79829b94aad9e2bf6b9003e85d257..776e695a3846fa229a9efd02ea30efb2691e7ff6 100644 (file)
 #include <set>
 #include <map>
 #include <algorithm>
+#include <type_traits>
 
 #include "dwarf2.h"
 #include "debugline.h"
 
 #include "Architectures.hpp"
+#include "Bitcode.hpp"
 #include "ld.hpp"
 #include "macho_relocatable_file.h"
 
@@ -73,28 +75,41 @@ public:
                                                                                        File(const char* p, time_t mTime, const uint8_t* content, ld::File::Ordinal ord) :
                                                                                                ld::relocatable::File(p,mTime,ord), _fileContent(content),
                                                                                                _sectionsArray(NULL), _atomsArray(NULL),
-                                                                                               _sectionsArrayCount(0), _atomsArrayCount(0),
+                                                                                               _sectionsArrayCount(0), _atomsArrayCount(0), _aliasAtomsArrayCount(0),
                                                                                                _debugInfoKind(ld::relocatable::File::kDebugInfoNone),
-                                                                                               _dwarfTranslationUnitDir(NULL), _dwarfTranslationUnitFile(NULL),
+                                                                                               _dwarfTranslationUnitPath(NULL), 
                                                                                                _dwarfDebugInfoSect(NULL), _dwarfDebugAbbrevSect(NULL), 
                                                                                                _dwarfDebugLineSect(NULL), _dwarfDebugStringSect(NULL), 
                                                                                                _objConstraint(ld::File::objcConstraintNone),
+                                                                                               _swiftVersion(0),
                                                                                                _cpuSubType(0),
-                                                                                               _canScatterAtoms(false) {}
+                                                                                               _minOSVersion(0),
+                                                                                               _platform(0),
+                                                                                               _canScatterAtoms(false),
+                                                                                               _objcHasCategoryClassPropertiesField(false),
+                                                                                               _srcKind(kSourceUnknown) { }
        virtual                                                                 ~File();
 
        // overrides of ld::File
        virtual bool                                                                            forEachAtom(ld::File::AtomHandler&) const;
        virtual bool                                                                            justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const
                                                                                                                                                                        { return false; }
-       
+       virtual uint32_t                                                                        minOSVersion() const            { return _minOSVersion; }
+       virtual uint32_t                                                                        platformLoadCommand() const     { return _platform; }
+
        // overrides of ld::relocatable::File 
        virtual ObjcConstraint                                                          objCConstraint() const                  { return _objConstraint; }
+       virtual bool                                                                            objcHasCategoryClassPropertiesField() const 
+                                                                                                                                                                       { return _objcHasCategoryClassPropertiesField; }
        virtual uint32_t                                                                        cpuSubType() const                              { return _cpuSubType; }
        virtual DebugInfoKind                                                           debugInfo() const                               { return _debugInfoKind; }
-       virtual const std::vector<ld::relocatable::File::Stab>* stabs() const                                   { return &_stabs; }
+       virtual const std::vector<ld::relocatable::File::Stab>* stabs() const                           { return &_stabs; }
        virtual bool                                                                            canScatterAtoms() const                 { return _canScatterAtoms; }
-       bool                                                                                            translationUnitSource(const char** dir, const char** name) const;
+       virtual const char*                                                                     translationUnitSource() const;
+       virtual LinkerOptionsList*                                                      linkerOptions() const                   { return &_linkerOptions; }
+       virtual uint8_t                                                                         swiftVersion() const                    { return _swiftVersion; }
+       virtual ld::Bitcode*                                                            getBitcode() const                              { return _bitcode.get(); }
+       virtual SourceKind                                                                      sourceKind() const                              { return _srcKind; }
        
        const uint8_t*                                                                          fileContent()                                   { return _fileContent; }
 private:
@@ -108,22 +123,30 @@ private:
        const uint8_t*                                                  _fileContent;
        Section<A>**                                                    _sectionsArray;
        uint8_t*                                                                _atomsArray;
+       uint8_t*                                                                _aliasAtomsArray;
        uint32_t                                                                _sectionsArrayCount;
        uint32_t                                                                _atomsArrayCount;
+       uint32_t                                                                _aliasAtomsArrayCount;
        std::vector<ld::Fixup>                                  _fixups;
        std::vector<ld::Atom::UnwindInfo>               _unwindInfos;
        std::vector<ld::Atom::LineInfo>                 _lineInfos;
        std::vector<ld::relocatable::File::Stab>_stabs;
        ld::relocatable::File::DebugInfoKind    _debugInfoKind;
-       const char*                                                             _dwarfTranslationUnitDir;
-       const char*                                                             _dwarfTranslationUnitFile;
+       const char*                                                             _dwarfTranslationUnitPath;
        const macho_section<P>*                                 _dwarfDebugInfoSect;
        const macho_section<P>*                                 _dwarfDebugAbbrevSect;
        const macho_section<P>*                                 _dwarfDebugLineSect;
        const macho_section<P>*                                 _dwarfDebugStringSect;
        ld::File::ObjcConstraint                                _objConstraint;
+       uint8_t                                                                 _swiftVersion;
        uint32_t                                                                _cpuSubType;
+       uint32_t                                                                _minOSVersion;
+       uint32_t                                                                _platform;
        bool                                                                    _canScatterAtoms;
+       bool                                                                    _objcHasCategoryClassPropertiesField;
+       std::vector<std::vector<const char*> >  _linkerOptions;
+       std::unique_ptr<ld::Bitcode>                    _bitcode;
+       SourceKind                                                              _srcKind;
 };
 
 
@@ -142,6 +165,7 @@ public:
        virtual ld::Atom::Alignment             alignmentForAddress(pint_t addr);
        virtual ld::Atom::ContentType   contentType()                           { return ld::Atom::typeUnclassified; }
        virtual bool                                    dontDeadStrip()                         { return (this->_machOSection->flags() & S_ATTR_NO_DEAD_STRIP); }
+       virtual bool                                    dontDeadStripIfReferencesLive() { return ( (this->_machOSection != NULL) && (this->_machOSection->flags() & S_ATTR_LIVE_SUPPORT) );  }
        virtual Atom<A>*                                findAtomByAddress(pint_t addr) { return this->findContentAtomByAddress(addr, this->_beginAtoms, this->_endAtoms); }
        virtual bool                                    addFollowOnFixups() const       { return ! _file.canScatterAtoms(); }
        virtual uint32_t                                appendAtoms(class Parser<A>& parser, uint8_t* buffer, 
@@ -155,6 +179,7 @@ public:
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const { return 0; }
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const { return false; }
+       virtual bool                                    ignoreLabel(const char* label) const { return false; }
        static const char*                              makeSectionName(const macho_section<typename A::P>* s);
 
 protected:     
@@ -168,6 +193,7 @@ protected:
 
        Atom<A>*                                                findContentAtomByAddress(pint_t addr, class Atom<A>* start, class Atom<A>* end);
        uint32_t                                                x86_64PcRelOffset(uint8_t r_type);
+       void                                                    addLOH(class Parser<A>& parser, int kind, int count, const uint64_t addrs[]);
        static const char*                              makeSegmentName(const macho_section<typename A::P>* s);
        static bool                                             readable(const macho_section<typename A::P>* s);
        static bool                                             writable(const macho_section<typename A::P>* s);
@@ -179,6 +205,7 @@ protected:
        class Atom<A>*                                  _beginAtoms;
        class Atom<A>*                                  _endAtoms;
        bool                                                    _hasAliases;
+       std::set<const class Atom<A>*>  _altEntries;
 };
 
 
@@ -188,7 +215,7 @@ class CFISection : public Section<A>
 public:
                                                CFISection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
                                                        : Section<A>(f, s) { }
-       uint32_t                        cfiCount();
+       uint32_t                        cfiCount(Parser<A>& parser);
 
        virtual ld::Atom::ContentType   contentType()           { return ld::Atom::typeCFI; }
        virtual uint32_t        computeAtomCount(class Parser<A>& parser, struct Parser<A>::LabelAndCFIBreakIterator& it, const struct Parser<A>::CFI_CU_InfoArrays&);
@@ -236,7 +263,7 @@ public:
        typedef typename A::P::uint_t                   pint_t;
        typedef libunwind::CFI_Atom_Info<OAS>   CFI_Atom_Info;
        
-       void                            cfiParse(class Parser<A>& parser, uint8_t* buffer, CFI_Atom_Info cfiArray[], uint32_t cfiCount);
+       void                            cfiParse(class Parser<A>& parser, uint8_t* buffer, CFI_Atom_Info cfiArray[], uint32_t& cfiCount, const pint_t cuStarts[], uint32_t cuCount);
        bool                            needsRelocating();
 
        static bool                     bigEndian();
@@ -275,6 +302,7 @@ public:
 
        uint32_t                                count();
        void                                    parse(class Parser<A>& parser, uint32_t cnt, Info array[]);
+       static bool                             encodingMeansUseDwarf(compact_unwind_encoding_t enc);
        
        
 private:
@@ -361,6 +389,10 @@ public:
                                                TLVDefsSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s) :
                                                        SymboledSection<A>(parser, f, s) { }
 
+       typedef typename A::P::uint_t   pint_t;
+
+       virtual ld::Atom::Alignment             alignmentForAddress(pint_t addr)                { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
+
 private:
 
 };
@@ -380,16 +412,17 @@ protected:
        
        virtual bool                                            addFollowOnFixups() const               { return false; }
        virtual const char*                                     unlabeledAtomName(Parser<A>& parser, pint_t addr) = 0;
-       virtual ld::Atom::SymbolTableInclusion  symbolTableInclusion()          { return ld::Atom::symbolTableNotIn; }
+       virtual ld::Atom::SymbolTableInclusion  symbolTableInclusion();
        virtual pint_t                                          elementSizeAtAddress(pint_t addr) = 0;
        virtual ld::Atom::Scope                         scopeAtAddress(Parser<A>& parser, pint_t addr) { return ld::Atom::scopeLinkageUnit; }
        virtual bool                                            useElementAt(Parser<A>& parser, 
                                                                                                struct Parser<A>::LabelAndCFIBreakIterator& it, pint_t addr) = 0;
        virtual ld::Atom::Definition            definition()                                    { return ld::Atom::definitionRegular; }
        virtual ld::Atom::Combine                       combine(Parser<A>& parser, pint_t addr) = 0;
-       virtual bool                                            ignoreLabel(const char* label)  { return (label[0] == 'L'); }
+       virtual bool                                            ignoreLabel(const char* label) const { return (label[0] == 'L'); }
 };
 
+
 template <typename A>
 class FixedSizeSection : public ImplicitSizeSection<A>
 {
@@ -424,6 +457,7 @@ protected:
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
+       virtual bool                                    ignoreLabel(const char* label) const;
 };
 
 template <typename A>
@@ -443,6 +477,7 @@ protected:
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
+       virtual bool                                    ignoreLabel(const char* label) const;
 };
 
 template <typename A>
@@ -462,6 +497,7 @@ protected:
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
+       virtual bool                                    ignoreLabel(const char* label) const;
 };
 
 
@@ -482,7 +518,7 @@ protected:
        virtual pint_t                                  elementSizeAtAddress(pint_t addr)               { return sizeof(pint_t); }
        virtual ld::Atom::Scope                 scopeAtAddress(Parser<A>& parser, pint_t addr);
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t);
-       virtual bool                                    ignoreLabel(const char* label)                  { return true; }
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
@@ -492,6 +528,31 @@ private:
        static ld::Fixup::Kind                  fixupKind();
 };
 
+template <typename A>
+class TLVPointerSection : public FixedSizeSection<A>
+{
+public:
+                                               TLVPointerSection(Parser<A>& parser, File<A>& f, const macho_section<typename A::P>* s)
+                                                       : FixedSizeSection<A>(parser, f, s) {}
+protected:
+       typedef typename A::P::uint_t   pint_t;
+       typedef typename A::P                   P;
+
+       virtual void                                    makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&);
+       virtual ld::Atom::ContentType   contentType()                                                   { return ld::Atom::typeTLVPointer; }
+       virtual ld::Atom::Alignment             alignmentForAddress(pint_t addr)                { return ld::Atom::Alignment(log2(sizeof(pint_t))); }
+       virtual const char*                             unlabeledAtomName(Parser<A>&, pint_t)   { return "tlv_lazy_ptr"; }
+       virtual pint_t                                  elementSizeAtAddress(pint_t addr)               { return sizeof(pint_t); }
+       virtual ld::Atom::Combine               combine(Parser<A>&, pint_t);
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
+       virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
+       virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
+                                                                                                       const ld::IndirectBindingTable& ind) const;
+
+private:
+       static const char*                              targetName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind, bool* isStatic);
+};
+
 
 template <typename A>
 class CFStringSection : public FixedSizeSection<A>
@@ -506,7 +567,7 @@ protected:
        virtual const char*                             unlabeledAtomName(Parser<A>&, pint_t)   { return "CFString"; }
        virtual pint_t                                  elementSizeAtAddress(pint_t addr)               { return 4*sizeof(pint_t); }
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t)                             { return ld::Atom::combineByNameAndReferences; }
-       virtual bool                                    ignoreLabel(const char* label)                  { return true; }
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
@@ -534,7 +595,7 @@ protected:
        virtual ld::Atom::SymbolTableInclusion  symbolTableInclusion()                  { return ld::Atom::symbolTableIn; }
        virtual pint_t                                  elementSizeAtAddress(pint_t addr);
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t)                             { return ld::Atom::combineNever; }
-       virtual bool                                    ignoreLabel(const char* label)                  { return true; }
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
                                                                                                                                                        { return 0; }
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
@@ -556,7 +617,7 @@ protected:
        virtual const char*                             unlabeledAtomName(Parser<A>&, pint_t)   { return "objc-class-ref"; }
        virtual pint_t                                  elementSizeAtAddress(pint_t addr)               { return sizeof(pint_t); }
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t)                             { return ld::Atom::combineByNameAndReferences; }
-       virtual bool                                    ignoreLabel(const char* label)                  { return true; }
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
@@ -579,7 +640,7 @@ protected:
        virtual const char*                             unlabeledAtomName(Parser<A>&, pint_t)   { return "objc-cat-list"; }
        virtual pint_t                                  elementSizeAtAddress(pint_t addr)               { return sizeof(pint_t); }
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t)                             { return ld::Atom::combineNever; }
-       virtual bool                                    ignoreLabel(const char* label)                  { return true; }
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
 private:
        const char*                                             targetClassName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
 };
@@ -598,7 +659,7 @@ protected:
        virtual const char*                             unlabeledAtomName(Parser<A>&, pint_t)   { return "pointer-to-literal-cstring"; }
        virtual pint_t                                  elementSizeAtAddress(pint_t addr)               { return sizeof(pint_t); }
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t)                             { return ld::Atom::combineByNameAndReferences; }
-       virtual bool                                    ignoreLabel(const char* label)                  { return true; }
+       virtual bool                                    ignoreLabel(const char* label) const    { return true; }
        virtual unsigned long                   contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const;
        virtual bool                                    canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs, 
                                                                                                        const ld::IndirectBindingTable& ind) const;
@@ -636,7 +697,7 @@ protected:
        virtual Atom<A>*                                findAtomByAddress(pint_t addr);
        virtual const char*                             unlabeledAtomName(Parser<A>&, pint_t)   { return "cstring"; }
        virtual pint_t                                  elementSizeAtAddress(pint_t addr);
-       virtual bool                                    ignoreLabel(const char* label);
+       virtual bool                                    ignoreLabel(const char* label) const;
        virtual bool                                    useElementAt(Parser<A>& parser, 
                                                                                                struct Parser<A>::LabelAndCFIBreakIterator& it, pint_t addr);
        virtual ld::Atom::Combine               combine(Parser<A>&, pint_t)                             { return ld::Atom::combineByNameAndContent; }
@@ -672,9 +733,9 @@ class Atom : public ld::Atom
 {
 public:
        // overrides of ld::Atom
-       virtual ld::File*                                                       file() const            { return &sect().file(); }
-       virtual bool                                                            translationUnitSource(const char** dir, const char** nm) const
-                                                                                                                                       { return sect().file().translationUnitSource(dir, nm); }
+       virtual const ld::File*                                         file() const;
+       virtual const char*                                                     translationUnitSource() const
+                                                                                                                                       { return sect().file().translationUnitSource(); }
        virtual const char*                                                     name() const            { return _name; }
        virtual uint64_t                                                        size() const            { return _size; }
        virtual uint64_t                                                        objectAddress() const { return _objAddress; }
@@ -690,6 +751,7 @@ public:
        virtual ld::Atom::UnwindInfo::iterator          endUnwind()     const   { return &machofile()._unwindInfos[_unwindInfoStartIndex+_unwindInfoCount];  }
        virtual ld::Atom::LineInfo::iterator            beginLineInfo() const{ return &machofile()._lineInfos[_lineInfoStartIndex]; }
        virtual ld::Atom::LineInfo::iterator            endLineInfo() const { return &machofile()._lineInfos[_lineInfoStartIndex+_lineInfoCount];  }
+       virtual void                                                            setFile(const ld::File* f);
 
 private:
 
@@ -715,7 +777,7 @@ public:
                                                                                                                                                        throwf("too may fixups in %s", name()); ++_fixupsCount; }
                        const uint8_t*                                          contentPointer() const;
                        uint32_t                                                        fixupCount() const { return _fixupsCount; }
-                       void                                                            verifyAlignment() const;
+                       void                                                            verifyAlignment(const macho_section<typename A::P>&) const;
        
        typedef typename A::P                                           P;
        typedef typename A::P::E                                        E;
@@ -737,7 +799,7 @@ public:
                                                                                                                                parser.combineFromSymbol(sym), parser.scopeFromSymbol(sym),
                                                                                                                                parser.resolverFromSymbol(sym) ? ld::Atom::typeResolver : sct.contentType(), 
                                                                                                                                parser.inclusionFromSymbol(sym), 
-                                                                                                                               parser.dontDeadStripFromSymbol(sym) || sct.dontDeadStrip(),
+                                                                                                                               (parser.dontDeadStripFromSymbol(sym) && !sct.dontDeadStripIfReferencesLive()) || sct.dontDeadStrip(),
                                                                                                                                parser.isThumbFromSymbol(sym), alias, 
                                                                                                                                sct.alignmentForAddress(sym.n_value())),
                                                                                                                        _size(sz), _objAddress(sym.n_value()), 
@@ -749,7 +811,9 @@ public:
                                                                                                                                if ( _scope == ld::Atom::scopeGlobal && 
                                                                                                                                                (sym.n_desc() & (N_WEAK_DEF|N_WEAK_REF)) == (N_WEAK_DEF|N_WEAK_REF) )
                                                                                                                                        this->setAutoHide();
-                                                                                                                                       this->verifyAlignment();
+                                                                                                                               this->verifyAlignment(*sct.machoSection());
+                                                                                                                               if ( sct.dontDeadStripIfReferencesLive() )
+                                                                                                                                       this->setDontDeadStripIfReferencesLive();
                                                                                                                        }
 
 private:
@@ -769,10 +833,27 @@ private:
                                                                                                _fixupsCount                    : kFixupCountBits,
                                                                                                _lineInfoCount                  : kLineInfoCountBits,
                                                                                                _unwindInfoCount                : kUnwindInfoCountBits;
-
+                                                                                               
+       static std::map<const ld::Atom*, const ld::File*> _s_fileOverride;
 };
 
+template <typename A>
+std::map<const ld::Atom*, const ld::File*> Atom<A>::_s_fileOverride;
+
+template <typename A>
+void Atom<A>::setFile(const ld::File* f) {
+       _s_fileOverride[this] = f;
+}
 
+template <typename A>
+const ld::File* Atom<A>::file() const
+{
+       std::map<const ld::Atom*, const ld::File*>::iterator pos = _s_fileOverride.find(this);
+       if ( pos != _s_fileOverride.end() )
+               return pos->second;
+               
+       return &sect().file();
+}
 
 template <typename A>
 void Atom<A>::setFixupsRange(uint32_t startIndex, uint32_t count)
@@ -819,6 +900,8 @@ template <typename A>
 const uint8_t* Atom<A>::contentPointer() const
 {
        const macho_section<P>* sct = this->sect().machoSection();
+       if ( this->_objAddress > sct->addr() + sct->size() )
+               throwf("malformed .o file, symbol has address 0x%0llX which is outside range of its section", (uint64_t)this->_objAddress);
        uint32_t fileOffset = sct->offset() - sct->addr() + this->_objAddress;
        return this->sect().file().fileContent()+fileOffset;
 }
@@ -837,7 +920,7 @@ void Atom<A>::copyRawContent(uint8_t buffer[]) const
 }
 
 template <>
-void Atom<arm>::verifyAlignment() const
+void Atom<arm>::verifyAlignment(const macho_section<P>&) const
 {
        if ( (this->section().type() == ld::Section::typeCode) && ! isThumb() ) {
                if ( ((_objAddress % 4) != 0) || (this->alignment().powerOf2 < 2) )
@@ -845,12 +928,57 @@ void Atom<arm>::verifyAlignment() const
        }
 }
 
+#if SUPPORT_ARCH_arm64
+template <>
+void Atom<arm64>::verifyAlignment(const macho_section<P>& sect) const
+{
+       if ( (this->section().type() == ld::Section::typeCode) && (sect.size() != 0) ) {
+               if ( ((_objAddress % 4) != 0) || (this->alignment().powerOf2 < 2) )
+                       warning("arm64 function not 4-byte aligned: %s from %s", this->name(), this->file()->path());
+       }
+}
+#endif
+
+
 template <typename A>
-void Atom<A>::verifyAlignment() const
+void Atom<A>::verifyAlignment(const macho_section<P>&) const
 {
 }
 
 
+class AliasAtom : public ld::Atom
+{
+public:
+                                                                               AliasAtom(const char* name, bool hidden, const ld::File* file, const char* aliasOfName) : 
+                                                                                       ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
+                                                                                                       (hidden ? ld::Atom::scopeLinkageUnit : ld::Atom::scopeGlobal), 
+                                                                                                       ld::Atom::typeUnclassified, ld::Atom::symbolTableIn, 
+                                                                                                       false, false, true, 0),
+                                                                                       _file(file),
+                                                                                       _name(name), 
+                                                                                       _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, ld::Fixup::bindingByNameUnbound, aliasOfName) { }
+
+       virtual const ld::File*                         file() const            { return _file; }
+       virtual const char*                                     translationUnitSource() const
+                                                                                                                       { return NULL; }
+       virtual const char*                                     name() const            { return _name; }
+       virtual uint64_t                                        size() const            { return 0; }
+       virtual uint64_t                                        objectAddress() const { return 0; }
+       virtual void                                            copyRawContent(uint8_t buffer[]) const { }
+       virtual ld::Fixup::iterator                     fixupsBegin() const     { return &((ld::Fixup*)&_fixup)[0]; }
+       virtual ld::Fixup::iterator                     fixupsEnd()     const   { return &((ld::Fixup*)&_fixup)[1]; }
+
+private:
+       static ld::Section                                      _s_section;
+
+       const ld::File*                                         _file;
+       const char*                                                     _name;
+       ld::Fixup                                                       _fixup;
+};
+
+ld::Section AliasAtom::_s_section("__LD", "__aliases", ld::Section::typeTempAlias, true);
+
+
 template <typename A>
 class Parser 
 {
@@ -858,13 +986,18 @@ public:
        static bool                                                                             validFile(const uint8_t* fileContent, bool subtypeMustMatch=false, 
                                                                                                                                cpu_subtype_t subtype=0);
        static const char*                                                              fileKind(const uint8_t* fileContent);
+       static Options::Platform                                                findPlatform(const macho_header<typename A::P>* header);
        static bool                                                                             hasObjC2Categories(const uint8_t* fileContent);
        static bool                                                                             hasObjC1Categories(const uint8_t* fileContent);
+       static bool                                                                             getNonLocalSymbols(const uint8_t* fileContnet, std::vector<const char*> &syms);
        static ld::relocatable::File*                                   parse(const uint8_t* fileContent, uint64_t fileLength, 
                                                                                                                        const char* path, time_t modTime, ld::File::Ordinal ordinal,
                                                                                                                         const ParserOptions& opts) {
                                                                                                                                Parser p(fileContent, fileLength, path, modTime, 
-                                                                                                                                               ordinal, opts.convertUnwindInfo);
+                                                                                                                                               ordinal, opts.warnUnwindConversionProblems,
+                                                                                                                                               opts.keepDwarfUnwind, opts.forceDwarfConversion,
+                                                                                                                                               opts.neverConvertDwarf, opts.verboseOptimizationHints,
+                                                                                                                                               opts.ignoreMismatchPlatform);
                                                                                                                                return p.parse(opts);
                                                                                                                }
 
@@ -933,7 +1066,7 @@ public:
                _allFixups.push_back(FixupInAtom(src, c, k)); 
        }
 
-
+       const char*                                                                             path() { return _path; }
        uint32_t                                                                                symbolCount() { return _symbolCount; }
        uint32_t                                                                                indirectSymbol(uint32_t indirectIndex);
        const macho_nlist<P>&                                                   symbolFromIndex(uint32_t index);
@@ -946,6 +1079,7 @@ public:
        static bool                                                                             isThumbFromSymbol(const macho_nlist<P>& sym);
        static bool                                                                             weakImportFromSymbol(const macho_nlist<P>& sym);
        static bool                                                                             resolverFromSymbol(const macho_nlist<P>& sym);
+       static bool                                                                             altEntryFromSymbol(const macho_nlist<P>& sym);
        uint32_t                                                                                symbolIndexFromIndirectSectionAddress(pint_t,const macho_section<P>*);
        const macho_section<P>*                                                 firstMachOSection() { return _sectionsStart; }
        const macho_section<P>*                                                 machOSectionFromSectionIndex(uint32_t index);
@@ -965,13 +1099,27 @@ public:
                                                                                                                                                                                TargetDesc& target);
        uint32_t                                                                                tentativeDefinitionCount() { return _tentativeDefinitionCount; }
        uint32_t                                                                                absoluteSymbolCount() { return _absoluteSymbolCount; }
-       
+
+       uint32_t                                                                                fileLength() const { return _fileLength; }
        bool                                                                                    hasStubsSection() { return (_stubsSectionNum != 0); }
        unsigned int                                                                    stubsSectionNum() { return _stubsSectionNum; }
        void                                                                                    addDtraceExtraInfos(const SourceLocation& src, const char* provider);
        const char*                                                                             scanSymbolTableForAddress(uint64_t addr);
-       bool                                                                                    convertUnwindInfo() { return _convertUnwindInfo; }
+       bool                                                                                    warnUnwindConversionProblems() { return _warnUnwindConversionProblems; }
        bool                                                                                    hasDataInCodeLabels() { return _hasDataInCodeLabels; }
+       bool                                                                                    keepDwarfUnwind() { return _keepDwarfUnwind; }
+       bool                                                                                    forceDwarfConversion() { return _forceDwarfConversion; }
+       bool                                                                                    verboseOptimizationHints() { return _verboseOptimizationHints; }
+       bool                                                                                    neverConvertDwarf() { return _neverConvertDwarf; }
+       bool                                                                                    armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions; }
+       uint8_t                                                                                 maxDefaultCommonAlignment() { return _maxDefaultCommonAlignment; }
+
+
+       macho_data_in_code_entry<P>*                                    dataInCodeStart() { return _dataInCodeStart; }
+       macho_data_in_code_entry<P>*                                    dataInCodeEnd()   { return _dataInCodeEnd; }
+       const uint8_t*                                                                  optimizationHintsStart() { return _lohStart; }
+       const uint8_t*                                                                  optimizationHintsEnd() { return _lohEnd; }
+       bool                                                                                    hasOptimizationHints() { return _lohStart != _lohEnd; }
 
        
        void                                                    addFixups(const SourceLocation& src, ld::Fixup::Kind kind, const TargetDesc& target);
@@ -986,7 +1134,7 @@ public:
                                                                        : sortedSymbolIndexes(ssa), sortedSymbolCount(ssc), cfiStartsArray(cfisa), 
                                                                                cfiStartsCount(cfisc), fileHasOverlappingSymbols(ols),
                                                                                newSection(false), cfiIndex(0), symIndex(0) {}
-               bool                                    next(Parser<A>& parser, uint32_t sectNum, pint_t startAddr, pint_t endAddr, 
+               bool                                    next(Parser<A>& parser, const Section<A>& sect, uint32_t sectNum, pint_t startAddr, pint_t endAddr, 
                                                                                pint_t* addr, pint_t* size, const macho_nlist<P>** sym);
                pint_t                                  peek(Parser<A>& parser, pint_t startAddr, pint_t endAddr);
                void                                    beginSection() { newSection = true; symIndex = 0; }
@@ -1022,7 +1170,7 @@ private:
                                                sectionTypeUTF16Strings, sectionTypeCFString, sectionTypeObjC2ClassRefs, typeObjC2CategoryList,
                                                sectionTypeObjC1Classes, sectionTypeSymboled, sectionTypeObjC1ClassRefs,
                                                sectionTypeTentativeDefinitions, sectionTypeAbsoluteSymbols, sectionTypeTLVDefs,
-                                               sectionTypeCompactUnwind };
+                                               sectionTypeCompactUnwind, sectionTypeTLVPointers};
 
        template <typename P>
        struct MachOSectionAndSectionClass
@@ -1047,11 +1195,13 @@ private:
        
 
                                                                                                        Parser(const uint8_t* fileContent, uint64_t fileLength, 
-                                                                                                                       const char* path, time_t modTime, 
-                                                                                                                       ld::File::Ordinal ordinal, bool convertUnwindInfo);
+                                                                                                                       const char* path, time_t modTime, ld::File::Ordinal ordinal, 
+                                                                                                                       bool warnUnwindConversionProblems, bool keepDwarfUnwind,
+                                                                                                                       bool forceDwarfConversion, bool neverConvertDwarf,
+                                                                                                                       bool verboseOptimizationHints, bool ignoreMismatchPlatform);
        ld::relocatable::File*                                                  parse(const ParserOptions& opts);
-       uint8_t                                                                                 loadCommandSizeMask();
-       bool                                                                                    parseLoadCommands();
+       static uint8_t                                                                  loadCommandSizeMask();
+       bool                                                                                    parseLoadCommands(Options::Platform platform, uint32_t minOSVersion, bool simulator, bool ignoreMismatchPlatform);
        void                                                                                    makeSections();
        void                                                                                    prescanSymbolTable();
        void                                                                                    makeSortedSymbolsArray(uint32_t symArray[], const uint32_t sectionArray[]);
@@ -1062,10 +1212,13 @@ private:
 
        void                                                                                    parseDebugInfo();
        void                                                                                    parseStabs();
+       void                                                                                    appendAliasAtoms(uint8_t* atomBuffer);
        static bool                                                                             isConstFunStabs(const char *stabStr);
        bool                                                                                    read_comp_unit(const char ** name, const char ** comp_dir,
                                                                                                                                                                                                uint64_t *stmt_list);
-       const char*                                                                             getDwarfString(uint64_t form, const uint8_t* p);
+       pint_t                                                                                  realAddr(pint_t addr);
+       const char*                                                                             getDwarfString(uint64_t form, const uint8_t*& p);
+       uint64_t                                                                                getDwarfOffset(uint64_t form, const uint8_t*& di, bool dwarf64);
        bool                                                                                    skip_form(const uint8_t ** offset, const uint8_t * end, 
                                                                                                                                uint64_t form, uint8_t addr_size, bool dwarf64);
        
@@ -1081,6 +1234,7 @@ private:
        File<A>*                                                                        _file;
        const macho_nlist<P>*                                           _symbols;
        uint32_t                                                                        _symbolCount;
+       uint32_t                                                                        _indirectSymbolCount;
        const char*                                                                     _strings;
        uint32_t                                                                        _stringsSize;
        const uint32_t*                                                         _indirectTable;
@@ -1090,7 +1244,11 @@ private:
        const macho_section<P>*                                         _sectionsStart;
        uint32_t                                                                        _machOSectionsCount;
        bool                                                                            _hasUUID;
-       
+       macho_data_in_code_entry<P>*                            _dataInCodeStart;
+       macho_data_in_code_entry<P>*                            _dataInCodeEnd;
+       const uint8_t*                                                          _lohStart;
+       const uint8_t*                                                          _lohEnd;
+               
        // filled in by parse()
        CFISection<A>*                                                          _EHFrameSection;
        CUSection<A>*                                                           _compactUnwindSection;
@@ -1101,8 +1259,17 @@ private:
        bool                                                                            _hasLongBranchStubs;
        bool                                                                            _AppleObjc; // FSF has objc that uses different data layout
        bool                                                                            _overlappingSymbols;
-       bool                                                                            _convertUnwindInfo;
+       bool                                                                            _warnUnwindConversionProblems;
        bool                                                                            _hasDataInCodeLabels;
+       bool                                                                            _keepDwarfUnwind;
+       bool                                                                            _forceDwarfConversion;
+       bool                                                                            _neverConvertDwarf;
+       bool                                                                            _verboseOptimizationHints;
+       bool                                                                            _armUsesZeroCostExceptions;
+       bool                                                                            _ignoreMismatchPlatform;
+       bool                                                                            _treateBitcodeAsData;
+       bool                                                                            _usingBitcode;
+       uint8_t                                                                         _maxDefaultCommonAlignment;
        unsigned int                                                            _stubsSectionNum;
        const macho_section<P>*                                         _stubsMachOSection;
        std::vector<const char*>                                        _dtraceProviderInfo;
@@ -1113,17 +1280,24 @@ private:
 
 template <typename A>
 Parser<A>::Parser(const uint8_t* fileContent, uint64_t fileLength, const char* path, time_t modTime, 
-                                       ld::File::Ordinal ordinal, bool convertDUI)
+                                       ld::File::Ordinal ordinal, bool convertDUI, bool keepDwarfUnwind, bool forceDwarfConversion, 
+                                       bool neverConvertDwarf, bool verboseOptimizationHints, bool ignoreMismatchPlatform)
                : _fileContent(fileContent), _fileLength(fileLength), _path(path), _modTime(modTime),
                        _ordinal(ordinal), _file(NULL),
-                       _symbols(NULL), _symbolCount(0), _strings(NULL), _stringsSize(0),
+                       _symbols(NULL), _symbolCount(0), _indirectSymbolCount(0), _strings(NULL), _stringsSize(0),
                        _indirectTable(NULL), _indirectTableCount(0), 
                        _undefinedStartIndex(0), _undefinedEndIndex(0), 
                        _sectionsStart(NULL), _machOSectionsCount(0), _hasUUID(false), 
+                       _dataInCodeStart(NULL), _dataInCodeEnd(NULL),
+                       _lohStart(NULL), _lohEnd(NULL),
                        _EHFrameSection(NULL), _compactUnwindSection(NULL), _absoluteSection(NULL),
                        _tentativeDefinitionCount(0), _absoluteSymbolCount(0),
                        _symbolsInSections(0), _hasLongBranchStubs(false),  _AppleObjc(false),
-                       _overlappingSymbols(false), _convertUnwindInfo(convertDUI), _hasDataInCodeLabels(false), 
+                       _overlappingSymbols(false), _warnUnwindConversionProblems(convertDUI), _hasDataInCodeLabels(false),
+                       _keepDwarfUnwind(keepDwarfUnwind), _forceDwarfConversion(forceDwarfConversion),
+                       _neverConvertDwarf(neverConvertDwarf),
+                       _verboseOptimizationHints(verboseOptimizationHints),
+                       _ignoreMismatchPlatform(ignoreMismatchPlatform),
                        _stubsSectionNum(0), _stubsMachOSection(NULL)
 {
 }
@@ -1177,6 +1351,19 @@ bool Parser<arm>::validFile(const uint8_t* fileContent, bool subtypeMustMatch, c
 }
 
 
+template <>
+bool Parser<arm64>::validFile(const uint8_t* fileContent, bool subtypeMustMatch, cpu_subtype_t subtype)
+{
+       const macho_header<P>* header = (const macho_header<P>*)fileContent;
+       if ( header->magic() != MH_MAGIC_64 )
+               return false;
+       if ( header->cputype() != CPU_TYPE_ARM64 )
+               return false;
+       if ( header->filetype() != MH_OBJECT )
+               return false;
+       return true;
+}
+
 
 template <>
 const char* Parser<x86>::fileKind(const uint8_t* fileContent)
@@ -1193,7 +1380,7 @@ template <>
 const char* Parser<x86_64>::fileKind(const uint8_t* fileContent)
 {
        const macho_header<P>* header = (const macho_header<P>*)fileContent;
-       if ( header->magic() != MH_MAGIC )
+       if ( header->magic() != MH_MAGIC_64 )
                return NULL;
        if ( header->cputype() != CPU_TYPE_X86_64 )
                return NULL;
@@ -1216,6 +1403,19 @@ const char* Parser<arm>::fileKind(const uint8_t* fileContent)
        return "arm???";
 }
 
+#if SUPPORT_ARCH_arm64
+template <>
+const char* Parser<arm64>::fileKind(const uint8_t* fileContent)
+{
+       const macho_header<P>* header = (const macho_header<P>*)fileContent;
+       if ( header->magic() != MH_MAGIC_64 )
+               return NULL;
+       if ( header->cputype() != CPU_TYPE_ARM64 )
+               return NULL;
+       return "arm64";
+}
+#endif
+
 
 template <typename A>
 bool Parser<A>::hasObjC2Categories(const uint8_t* fileContent)
@@ -1274,6 +1474,39 @@ bool Parser<A>::hasObjC1Categories(const uint8_t* fileContent)
        return false;
 }
 
+
+template <typename A>
+bool Parser<A>::getNonLocalSymbols(const uint8_t* fileContent, std::vector<const char*> &syms)
+{
+       const macho_header<P>* header = (const macho_header<P>*)fileContent;
+       const uint32_t cmd_count = header->ncmds();
+       const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
+       const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
+       const macho_load_command<P>* cmd = cmds;
+       for (uint32_t i = 0; i < cmd_count; ++i) {
+               if ( cmd->cmd() == LC_SYMTAB ) {
+                       const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
+                       uint32_t symbolCount = symtab->nsyms();
+                       const macho_nlist<P>* symbols = (const macho_nlist<P>*)(fileContent + symtab->symoff());
+                       const char* strings = (char*)fileContent + symtab->stroff();
+                       for (uint32_t j = 0; j < symbolCount; ++j) {
+                               // ignore stabs and count only ext symbols
+                               if ( (symbols[j].n_type() & N_STAB) == 0 &&
+                                        (symbols[j].n_type() & N_EXT) != 0 ) {
+                                       const char* symName = &strings[symbols[j].n_strx()];
+                                       syms.push_back(symName);
+                               }
+                       }
+                       return true;
+               }
+               cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
+               if ( cmd > cmdsEnd )
+                       throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
+       }
+       return false;
+}
+
+
 template <typename A>
 int Parser<A>::pointerSorter(const void* l, const void* r)
 {
@@ -1316,7 +1549,7 @@ typename A::P::uint_t Parser<A>::LabelAndCFIBreakIterator::peek(Parser<A>& parse
 // was becuase of a label, the symbol). Returns false when no more chunks.
 //
 template <typename A>
-bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, uint32_t sectNum, pint_t startAddr, pint_t endAddr, 
+bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, const Section<A>& sect, uint32_t sectNum, pint_t startAddr, pint_t endAddr, 
                                                                                                pint_t* addr, pint_t* size, const macho_nlist<P>** symbol)
 {
        // may not be a label on start of section, but need atom demarcation there
@@ -1325,10 +1558,12 @@ bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, uint32_t sectN
                // advance symIndex until we get to the first label at or past the start of this section
                while ( symIndex < sortedSymbolCount ) {
                        const macho_nlist<P>& sym = parser.symbolFromIndex(sortedSymbolIndexes[symIndex]);
-                       pint_t nextSymbolAddr = sym.n_value();
-                       //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
-                       if ( (nextSymbolAddr > startAddr) || ((nextSymbolAddr == startAddr) && (sym.n_sect() == sectNum)) )
-                               break;
+                       if ( ! sect.ignoreLabel(parser.nameFromSymbol(sym)) ) {
+                               pint_t nextSymbolAddr = sym.n_value();
+                               //fprintf(stderr, "sectNum=%d, nextSymbolAddr=0x%08llX, name=%s\n", sectNum, (uint64_t)nextSymbolAddr, parser.nameFromSymbol(sym));
+                               if ( (nextSymbolAddr > startAddr) || ((nextSymbolAddr == startAddr) && (sym.n_sect() == sectNum)) )
+                                       break;
+                       }
                        ++symIndex;
                }
                if ( symIndex < sortedSymbolCount ) {
@@ -1375,7 +1610,19 @@ bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, uint32_t sectN
                        *symbol = NULL;
                        return true;
                }
-               // no symbols left in whole file, so entire section is one chunk
+               // no symbols in section, check CFI
+               if ( cfiIndex < cfiStartsCount ) {
+                       pint_t nextCfiAddr = cfiStartsArray[cfiIndex];
+                       if ( nextCfiAddr < endAddr ) {
+                               // use cfi
+                               ++cfiIndex;
+                               *addr = nextCfiAddr;
+                               *size = peek(parser, startAddr, endAddr) - nextCfiAddr;
+                               *symbol = NULL;
+                               return true;
+                       }
+               }
+               // no cfi, so whole section is one chunk
                *addr = startAddr;
                *size = endAddr - startAddr;
                *symbol = NULL;
@@ -1453,6 +1700,28 @@ bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, uint32_t sectN
        return false;
 }
 
+template <>
+typename arm::P::uint_t Parser<arm>::realAddr(typename arm::P::uint_t addr)
+{
+       return addr & (-2);
+}
+
+template <typename A>
+typename A::P::uint_t Parser<A>::realAddr(typename A::P::uint_t addr)
+{
+       return addr;
+}
+
+#define STACK_ALLOC_IF_SMALL(_type, _name, _actual_count, _maxCount) \
+       _type*  _name = NULL;   \
+       uint32_t _name##_count = 1; \
+       if ( _actual_count > _maxCount ) \
+               _name = (_type*)malloc(sizeof(_type) * _actual_count); \
+       else \
+               _name##_count = _actual_count; \
+       _type  _name##_buffer[_name##_count]; \
+       if ( _name == NULL ) \
+               _name = _name##_buffer;
 
 
 template <typename A>
@@ -1461,15 +1730,24 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
        // create file object
        _file = new File<A>(_path, _modTime, _fileContent, _ordinal);
 
+       // set sourceKind
+       _file->_srcKind = opts.srcKind;
+       // set treatBitcodeAsData
+       _treateBitcodeAsData = opts.treateBitcodeAsData;
+       _usingBitcode = opts.usingBitcode;
+
        // respond to -t option
        if ( opts.logAllFiles )
                printf("%s\n", _path);
+               
+       _armUsesZeroCostExceptions = opts.armUsesZeroCostExceptions;
+       _maxDefaultCommonAlignment = opts.maxDefaultCommonAlignment;
 
        // parse start of mach-o file
-       if ( ! parseLoadCommands() )
+       if ( ! parseLoadCommands(opts.platform, opts.minOSVersion, opts.simulator, opts.ignoreMismatchPlatform) )
                return _file;
        
-       // make array of 
+       // make array of
        uint32_t sortedSectionIndexes[_machOSectionsCount];
        this->makeSortedSectionsArray(sortedSectionIndexes);
        
@@ -1485,40 +1763,46 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
        uint32_t countOfCUs = 0;
        if ( _compactUnwindSection != NULL )
                countOfCUs = _compactUnwindSection->count();
-       uint8_t cuInfoBuffer[sizeof(typename CUSection<A>::Info) * countOfCUs];
-       typename CUSection<A>::Info*  cuInfoArray = (typename CUSection<A>::Info*)cuInfoBuffer;
+       // stack allocate (if not too large) cuInfoBuffer
+       STACK_ALLOC_IF_SMALL(typename CUSection<A>::Info, cuInfoArray, countOfCUs, 1024);
        if ( countOfCUs != 0 )
                _compactUnwindSection->parse(*this, countOfCUs, cuInfoArray);
+
+       // create lists of address that already have compact unwind and thus don't need the dwarf parsed
+       unsigned cuLsdaCount = 0;
+       pint_t cuStarts[countOfCUs];
+       for (uint32_t i=0; i < countOfCUs; ++i) {
+               if ( CUSection<A>::encodingMeansUseDwarf(cuInfoArray[i].compactUnwindInfo) )
+                       cuStarts[i] = -1;
+               else
+                       cuStarts[i] = cuInfoArray[i].functionStartAddress;
+               if ( cuInfoArray[i].lsdaAddress != 0 )
+                       ++cuLsdaCount;
+       }
+       
        
        // if it exists, do special early parsing of __eh_frame section 
-       // stack allocate array of CFI_Atom_Info
+       // stack allocate (if not too large) array of CFI_Atom_Info
        uint32_t countOfCFIs = 0;
        if ( _EHFrameSection != NULL )
-               countOfCFIs = _EHFrameSection->cfiCount();
-       typename CFISection<A>::CFI_Atom_Info  cfiArray[countOfCFIs];
+               countOfCFIs = _EHFrameSection->cfiCount(*this);
+       STACK_ALLOC_IF_SMALL(typename CFISection<A>::CFI_Atom_Info, cfiArray, countOfCFIs, 1024);
+       
        // stack allocate (if not too large) a copy of __eh_frame to apply relocations to
-       uint8_t* ehBuffer = NULL;
-       uint32_t stackAllocSize = 0;
-       if ( (countOfCFIs != 0) && _EHFrameSection->needsRelocating() ) {
-               uint32_t sectSize = _EHFrameSection->machoSection()->size();
-               if ( sectSize > 50*1024 )
-                       ehBuffer = (uint8_t*)malloc(sectSize);
-               else
-                       stackAllocSize = sectSize;
-       }
-       uint32_t ehStackBuffer[1+stackAllocSize/4]; // make 4-byte aligned stack bufffer
-       if ( ehBuffer == NULL )
-               ehBuffer = (uint8_t*)&ehStackBuffer;
+       uint32_t sectSize = 4;
+       if ( (countOfCFIs != 0) && _EHFrameSection->needsRelocating() ) 
+               sectSize = _EHFrameSection->machoSection()->size()+4;
+       STACK_ALLOC_IF_SMALL(uint8_t, ehBuffer, sectSize, 50*1024);
        uint32_t cfiStartsCount = 0;
        if ( countOfCFIs != 0 ) {
-               _EHFrameSection->cfiParse(*this, ehBuffer, cfiArray, countOfCFIs);
+               _EHFrameSection->cfiParse(*this, ehBuffer, cfiArray, countOfCFIs, cuStarts, countOfCUs);
                // count functions and lsdas
                for(uint32_t i=0; i < countOfCFIs; ++i) {
                        if ( cfiArray[i].isCIE )
                                continue;
-                       //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n", 
-                       //                      (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress, 
-                       //                      (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress, 
+                       //fprintf(stderr, "cfiArray[i].func = 0x%08llX, cfiArray[i].lsda = 0x%08llX, encoding=0x%08X\n",
+                       //                      (uint64_t)cfiArray[i].u.fdeInfo.function.targetAddress,
+                       //                      (uint64_t)cfiArray[i].u.fdeInfo.lsda.targetAddress,
                        //                      cfiArray[i].u.fdeInfo.compactUnwindInfo);
                        if ( cfiArray[i].u.fdeInfo.function.targetAddress != CFI_INVALID_ADDRESS )
                                ++cfiStartsCount;
@@ -1529,23 +1813,41 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
        CFI_CU_InfoArrays cfis(cfiArray, countOfCFIs, cuInfoArray, countOfCUs);
        
        // create sorted array of function starts and lsda starts
-       pint_t cfiStartsArray[cfiStartsCount];
+       pint_t cfiStartsArray[cfiStartsCount+cuLsdaCount];
        uint32_t countOfFDEs = 0;
+       uint32_t cfiStartsArrayCount = 0;
        if ( countOfCFIs != 0 ) {
-               int index = 0;
                for(uint32_t i=0; i < countOfCFIs; ++i) {
                        if ( cfiArray[i].isCIE )
                                continue;
                        if ( cfiArray[i].u.fdeInfo.function.targetAddress != CFI_INVALID_ADDRESS )
-                               cfiStartsArray[index++] = cfiArray[i].u.fdeInfo.function.targetAddress;
+                               cfiStartsArray[cfiStartsArrayCount++] = realAddr(cfiArray[i].u.fdeInfo.function.targetAddress);
                        if ( cfiArray[i].u.fdeInfo.lsda.targetAddress != CFI_INVALID_ADDRESS )
-                               cfiStartsArray[index++] = cfiArray[i].u.fdeInfo.lsda.targetAddress;
+                               cfiStartsArray[cfiStartsArrayCount++] = cfiArray[i].u.fdeInfo.lsda.targetAddress;
                        ++countOfFDEs;
                }
-               ::qsort(cfiStartsArray, cfiStartsCount, sizeof(pint_t), pointerSorter);
+       }
+       if ( cuLsdaCount != 0 ) {
+               // merge in an lsda info from compact unwind
+               for (uint32_t i=0; i < countOfCUs; ++i) {
+                       if ( cuInfoArray[i].lsdaAddress == 0 )
+                               continue;
+                       // append to cfiStartsArray if not already in that list
+                       bool found = false;
+                       for(uint32_t j=0; j < cfiStartsArrayCount; ++j) {
+                               if ( cfiStartsArray[j] == cuInfoArray[i].lsdaAddress )
+                                       found = true;
+                       }
+                       if ( ! found ) {
+                               cfiStartsArray[cfiStartsArrayCount++] = cuInfoArray[i].lsdaAddress;
+                       }
+               }
+       }
+       if ( cfiStartsArrayCount != 0 ) {
+               ::qsort(cfiStartsArray, cfiStartsArrayCount, sizeof(pint_t), pointerSorter);
        #ifndef NDEBUG
                // scan for FDEs claming the same function
-               for(int i=1; i < index; ++i) {
+               for(uint32_t i=1; i < cfiStartsArrayCount; ++i) {
                        assert( cfiStartsArray[i] != cfiStartsArray[i-1] );
                }
        #endif  
@@ -1556,7 +1858,7 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
 
        // figure out how many atoms will be allocated and allocate
        LabelAndCFIBreakIterator breakIterator(sortedSymbolIndexes, _symbolsInSections, cfiStartsArray, 
-                                                                                       cfiStartsCount, _overlappingSymbols);
+                                                                                       cfiStartsArrayCount, _overlappingSymbols);
        uint32_t computedAtomCount = 0;
        for (uint32_t i=0; i < sectionsCount; ++i ) {
                breakIterator.beginSection();
@@ -1571,7 +1873,7 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
        
        // have each section append atoms to _atomsArray
        LabelAndCFIBreakIterator breakIterator2(sortedSymbolIndexes, _symbolsInSections, cfiStartsArray, 
-                                                                                               cfiStartsCount, _overlappingSymbols);
+                                                                                               cfiStartsArrayCount, _overlappingSymbols);
        for (uint32_t i=0; i < sectionsCount; ++i ) {
                uint8_t* atoms = _file->_atomsArray + _file->_atomsArrayCount*sizeof(Atom<A>);
                breakIterator2.beginSection();
@@ -1598,7 +1900,7 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
                p += sizeof(Atom<A>);
        }
        assert(fixupOffset == _allFixups.size());
-       _file->_fixups.reserve(fixupOffset);
+       _file->_fixups.resize(fixupOffset);
        
        // copy each fixup for each atom 
        for(typename std::vector<FixupInAtom>::iterator it=_allFixups.begin(); it != _allFixups.end(); ++it) {
@@ -1622,6 +1924,7 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
                        _file->_unwindInfos.push_back(info);
                        Atom<A>* func = findAtomByAddress(cfiArray[i].u.fdeInfo.function.targetAddress);
                        func->setUnwindInfoRange(_file->_unwindInfos.size()-1, 1);
+                       //fprintf(stderr, "cu from dwarf =0x%08X, atom=%s\n", info.unwindInfo, func->name());
                }
        }
        // apply compact infos in __LD,__compact_unwind section to each function
@@ -1633,38 +1936,59 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
                assert(info->function != NULL);
                ld::Atom::UnwindInfo ui;
                ui.startOffset = info->functionStartAddress - info->function->objectAddress();
-               ui.unwindInfo = info->compactUnwindInfo;                
+               ui.unwindInfo = info->compactUnwindInfo;
                _file->_unwindInfos.push_back(ui);
-               // if previous is for same function, extend range
-               if ( info->function == lastFunc ) {
-                       if ( lastEnd != ui.startOffset ) {
-                               if ( lastEnd < ui.startOffset )
-                                       warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info->function->name(), lastEnd);
-                               else
-                                       warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info->function->name(), lastEnd);
+               // don't override with converted cu with "use dwarf" cu, if forcing dwarf conversion
+               if ( !_forceDwarfConversion || !CUSection<A>::encodingMeansUseDwarf(info->compactUnwindInfo) ) {
+                       //fprintf(stderr, "cu=0x%08X, atom=%s\n", ui.unwindInfo, info->function->name());
+                       // if previous is for same function, extend range
+                       if ( info->function == lastFunc ) {
+                               if ( lastEnd != ui.startOffset ) {
+                                       if ( lastEnd < ui.startOffset )
+                                               warning("__LD,__compact_unwind entries for %s have a gap at offset 0x%0X", info->function->name(), lastEnd);
+                                       else
+                                               warning("__LD,__compact_unwind entries for %s overlap at offset 0x%0X", info->function->name(), lastEnd);
+                               }
+                               lastFunc->extendUnwindInfoRange();
                        }
-                       lastFunc->extendUnwindInfoRange();
+                       else 
+                               info->function->setUnwindInfoRange(_file->_unwindInfos.size()-1, 1);
+                       lastFunc = info->function;
+                       lastEnd = ui.startOffset + info->rangeLength;
                }
-               else 
-                       info->function->setUnwindInfoRange(_file->_unwindInfos.size()-1, 1);
-               lastFunc = info->function;
-               lastEnd = ui.startOffset + info->rangeLength;
        }
        
+       // process indirect symbols which become AliasAtoms
+       _file->_aliasAtomsArray = NULL;
+       _file->_aliasAtomsArrayCount = 0;
+       if ( _indirectSymbolCount != 0 ) {
+               _file->_aliasAtomsArrayCount = _indirectSymbolCount;
+               _file->_aliasAtomsArray = new uint8_t[_file->_aliasAtomsArrayCount*sizeof(AliasAtom)];
+               this->appendAliasAtoms(_file->_aliasAtomsArray);
+       }
+       
+       
        // parse dwarf debug info to get line info
        this->parseDebugInfo();
 
        return _file;
 }
 
-
+static void versionToString(uint32_t value, char buffer[32])
+{
+       if ( value & 0xFF )
+               sprintf(buffer, "%d.%d.%d", value >> 16, (value >> 8) & 0xFF, value & 0xFF);
+       else
+               sprintf(buffer, "%d.%d", value >> 16, (value >> 8) & 0xFF);
+}
 
 template <> uint8_t Parser<x86>::loadCommandSizeMask()         { return 0x03; }
 template <> uint8_t Parser<x86_64>::loadCommandSizeMask()      { return 0x07; }
 template <> uint8_t Parser<arm>::loadCommandSizeMask()         { return 0x03; }
+template <> uint8_t Parser<arm64>::loadCommandSizeMask()       { return 0x07; }
 
 template <typename A>
-bool Parser<A>::parseLoadCommands()
+bool Parser<A>::parseLoadCommands(Options::Platform platform, uint32_t linkMinOSVersion, bool simulator, bool ignoreMismatchPlatform)
 {
        const macho_header<P>* header = (const macho_header<P>*)_fileContent;
 
@@ -1678,6 +2002,7 @@ bool Parser<A>::parseLoadCommands()
        // <rdar://problem/5394172> an empty .o file with zero load commands will crash linker
        if ( cmd_count == 0 )
                return false;
+       Options::Platform lcPlatform = Options::kPlatformUnknown;
        const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
        const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
        const macho_load_command<P>* cmd = cmds;
@@ -1724,29 +2049,159 @@ bool Parser<A>::parseLoadCommands()
                    case LC_UUID:
                                _hasUUID = true;
                                break;
-
-                       default:
-                               if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
-                                       if ( segment != NULL )
-                                               throw "more than one LC_SEGMENT found in object file";
-                                       segment = (macho_segment_command<P>*)cmd;
+                       case LC_DATA_IN_CODE:
+                               {
+                                       const macho_linkedit_data_command<P>* dc = (macho_linkedit_data_command<P>*)cmd;
+                                       _dataInCodeStart = (macho_data_in_code_entry<P>*)(_fileContent + dc->dataoff());
+                                       _dataInCodeEnd = (macho_data_in_code_entry<P>*)(_fileContent + dc->dataoff() + dc->datasize());
+                                       if ( _dataInCodeEnd > (macho_data_in_code_entry<P>*)endOfFile )
+                                               throw "LC_DATA_IN_CODE table extends beyond end of file";
+                               }
+                               break;
+                       case LC_LINKER_OPTION:
+                               {
+                                       const macho_linker_option_command<P>* loc = (macho_linker_option_command<P>*)cmd;
+                                       const char* buffer = loc->buffer();
+                                       _file->_linkerOptions.resize(_file->_linkerOptions.size() + 1);
+                                       std::vector<const char*>& vec = _file->_linkerOptions.back();
+                                       for (uint32_t j=0; j < loc->count(); ++j) {
+                                               vec.push_back(buffer);
+                                               buffer += strlen(buffer) + 1;
+                                       }
+                                       if ( buffer > ((char*)cmd + loc->cmdsize()) )
+                                               throw "malformed LC_LINKER_OPTION";
+                               }
+                               break;
+                       case LC_LINKER_OPTIMIZATION_HINTS:
+                               {
+                                       const macho_linkedit_data_command<P>* loh = (macho_linkedit_data_command<P>*)cmd;
+                                       _lohStart = _fileContent + loh->dataoff();
+                                       _lohEnd = _fileContent + loh->dataoff() + loh->datasize();
+                                       if ( _lohEnd > endOfFile )
+                                               throw "LC_LINKER_OPTIMIZATION_HINTS table extends beyond end of file";
                                }
                                break;
+                       case LC_VERSION_MIN_MACOSX:
+                       case LC_VERSION_MIN_IPHONEOS:
+                       case LC_VERSION_MIN_WATCHOS:
+       #if SUPPORT_APPLE_TV
+                       case LC_VERSION_MIN_TVOS:
+       #endif
+                               if ( ignoreMismatchPlatform )
+                                       break;
+                               _file->_platform = cmd->cmd();
+                               lcPlatform = Options::platformForLoadCommand(cmd->cmd());
+                               _file->_minOSVersion = ((macho_version_min_command<P>*)cmd)->version();
+                               break;
+                       case macho_segment_command<P>::CMD:
+                               if ( segment != NULL )
+                                       throw "more than one LC_SEGMENT found in object file";
+                               segment = (macho_segment_command<P>*)cmd;
+                               break;
+                       default:
+                               // ignore unknown load commands
+                               break;
                }
                cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
                if ( cmd > cmdsEnd )
                        throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
        }
+       // arm/arm64 objects are default to ios platform if not set.
+       // rdar://problem/21746314
+       if (lcPlatform == Options::kPlatformUnknown &&
+               (std::is_same<A, arm>::value || std::is_same<A, arm64>::value))
+               lcPlatform = Options::kPlatformiOS;
+
+       // Check platform cross-linking.
+       if ( !ignoreMismatchPlatform ) {
+               if ( lcPlatform != platform ) {
+                       switch (platform) {
+                               case Options::kPlatformOSX:
+                               case Options::kPlatformiOS:
+                                       if ( lcPlatform == Options::kPlatformUnknown )
+                                               break;
+                                       // fall through if the Platform is not Unknown
+                               case Options::kPlatformWatchOS:
+                                       // Error when using bitcocde, warning otherwise.
+                                       if (_usingBitcode)
+                                               throwf("building for %s%s, but linking in object file built for %s,",
+                                                  Options::platformName(platform), (simulator ? " simulator" : ""),
+                                                  Options::platformName(lcPlatform));
+                                       else
+                                               warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
+                                                               "Note: This will be an error in the future.",
+                                                               Options::platformName(platform), (simulator ? " simulator" : ""), path(),
+                                                               Options::platformName(lcPlatform));
+                                       break;
+       #if SUPPORT_APPLE_TV
+                               case Options::kPlatform_tvOS:
+                                       // Error when using bitcocde, warning otherwise.
+                                       if (_usingBitcode)
+                                               throwf("building for %s%s, but linking in object file built for %s,",
+                                                          Options::platformName(platform), (simulator ? " simulator" : ""),
+                                                          Options::platformName(lcPlatform));
+                                       else
+                                               warning("URGENT: building for %s%s, but linking in object file (%s) built for %s. "
+                                                               "Note: This will be an error in the future.",
+                                                               Options::platformName(platform), (simulator ? " simulator" : ""), path(),
+                                                               Options::platformName(lcPlatform));
+                                       break;
+       #endif
+                               case Options::kPlatformUnknown:
+                                       // skip if the target platform is unknown
+                                       break;
+                       }
+               }
+               if ( linkMinOSVersion && (_file->_minOSVersion > linkMinOSVersion) ) {
+                       char t1[32];
+                       char t2[32];
+                       versionToString(_file->_minOSVersion, t1);
+                       versionToString(linkMinOSVersion, t2);
+                       warning("object file (%s) was built for newer %s version (%s) than being linked (%s)",
+                                       _path, Options::platformName(lcPlatform), t1, t2);
+               }
+       }
+
 
        // record range of sections
        if ( segment == NULL ) 
                throw "missing LC_SEGMENT";
        _sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
        _machOSectionsCount = segment->nsects();
-       
+       if ( (sizeof(macho_segment_command<P>) + _machOSectionsCount * sizeof(macho_section<P>)) > segment->cmdsize() )
+               throw "too many sections for size of LC_SEGMENT command";
        return true;
 }
 
+template <typename A>
+Options::Platform Parser<A>::findPlatform(const macho_header<P>* header)
+{
+       const uint32_t cmd_count = header->ncmds();
+       if ( cmd_count == 0 )
+               return Options::kPlatformUnknown;
+       const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
+       const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
+       const macho_load_command<P>* cmd = cmds;
+       for (uint32_t i = 0; i < cmd_count; ++i) {
+               uint32_t size = cmd->cmdsize();
+               if ( (size & loadCommandSizeMask()) != 0 )
+                       throwf("load command #%d has a unaligned size", i);
+               const uint8_t* endOfCmd = ((uint8_t*)cmd)+cmd->cmdsize();
+               if ( endOfCmd > (uint8_t*)cmdsEnd )
+                       throwf("load command #%d extends beyond the end of the load commands", i);
+               switch (cmd->cmd()) {
+                       case LC_VERSION_MIN_MACOSX:
+                               return Options::kPlatformOSX;
+                       case LC_VERSION_MIN_IPHONEOS:
+                               return Options::kPlatformiOS;
+               }
+               cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
+               if ( cmd > cmdsEnd )
+                       throwf("malformed mach-o file, load command #%d is outside size of load commands", i);
+       }
+       return Options::kPlatformUnknown;
+}
+
 
 template <typename A>
 void Parser<A>::prescanSymbolTable()
@@ -1777,7 +2232,11 @@ void Parser<A>::prescanSymbolTable()
                        }
                        continue;
                }
-                                               
+               else if ( ((sym.n_type() & N_TYPE) == N_INDR) && ((sym.n_type() & N_EXT) != 0) ) {
+                       _indirectSymbolCount++;
+                       continue;
+               }
+
                // count absolute symbols
                if ( (sym.n_type() & N_TYPE) == N_ABS ) {
                        const char* absName = this->nameFromSymbol(sym);
@@ -1814,6 +2273,34 @@ void Parser<A>::prescanSymbolTable()
        }
 }
 
+template <typename A>
+void Parser<A>::appendAliasAtoms(uint8_t* p)
+{
+       for (uint32_t i=0; i < this->_symbolCount; ++i) {
+               const macho_nlist<P>& sym =     symbolFromIndex(i);
+               // ignore stabs
+               if ( (sym.n_type() & N_STAB) != 0 )
+                       continue;
+
+               // only look at N_INDR symbols
+               if ( (sym.n_type() & N_TYPE) != N_INDR ) 
+                       continue;
+
+               // skip non-external aliases
+               if ( (sym.n_type() & N_EXT) == 0 ) 
+                       continue;
+
+               const char* symbolName = this->nameFromSymbol(sym);
+               const char* aliasOfName = &_strings[sym.n_value()];
+               bool isHiddenVisibility = (sym.n_type() & N_PEXT);
+               AliasAtom* allocatedSpace = (AliasAtom*)p;
+               new (allocatedSpace) AliasAtom(symbolName, isHiddenVisibility, _file, aliasOfName);
+               p += sizeof(AliasAtom);
+       }
+}
+
+
+
 template <typename A>
 int Parser<A>::sectionIndexSorter(void* extra, const void* l, const void* r)
 {
@@ -1893,6 +2380,15 @@ int Parser<A>::symbolIndexSorter(void* extra, const void* l, const void* r)
                        }
                }
                // two symbols in same section, means one is an alias
+               // if one is ltmp*, make it an alias (sort first)
+               const char* leftName  = parser->nameFromSymbol(leftSym);
+               const char* rightName = parser->nameFromSymbol(rightSym);
+               bool leftIsTmp  = strncmp(leftName,  "ltmp", 4);
+               bool rightIsTmp = strncmp(rightName, "ltmp", 4);
+               if ( leftIsTmp != rightIsTmp ) {
+                       return (rightIsTmp ? -1 : 1);
+               }
+               
                // if only one is global, make the other an alias (sort first)
                if ( (leftSym.n_type() & N_EXT) != (rightSym.n_type() & N_EXT) ) {
                        if ( (rightSym.n_type() & N_EXT) != 0 )
@@ -1900,8 +2396,8 @@ int Parser<A>::symbolIndexSorter(void* extra, const void* l, const void* r)
                        else
                                return 1;
                }
-               // if both are global, make alphabetically last one be the alias
-               return ( strcmp(parser->nameFromSymbol(rightSym), parser->nameFromSymbol(leftSym)) );
+               // if both are global, sort alphabetically. earlier one will be the alias
+               return ( strcmp(rightName, leftName) );
        }
        else if ( result < 0 )
                return -1;
@@ -1943,6 +2439,7 @@ void Parser<A>::makeSortedSymbolsArray(uint32_t array[], const uint32_t sectionA
        // sort by symbol table address
        ParserAndSectionsArray extra = { this, sectionArray };
        ::qsort_r(array, _symbolsInSections, sizeof(uint32_t), &extra, &symbolIndexSorter);
+
        
        // look for two symbols at same address
        _overlappingSymbols = false;
@@ -1950,6 +2447,7 @@ void Parser<A>::makeSortedSymbolsArray(uint32_t array[], const uint32_t sectionA
                if ( symbolFromIndex(array[i-1]).n_value() == symbolFromIndex(array[i]).n_value() ) {
                        //fprintf(stderr, "overlapping symbols at 0x%08llX\n", symbolFromIndex(array[i-1]).n_value());
                        _overlappingSymbols = true;
+                       break;
                }
        }
 
@@ -1960,7 +2458,6 @@ void Parser<A>::makeSortedSymbolsArray(uint32_t array[], const uint32_t sectionA
        }
 }
 
-
 template <typename A>
 void Parser<A>::makeSections()
 {
@@ -1971,8 +2468,19 @@ void Parser<A>::makeSections()
        // allocate raw storage for all section objects on stack
        MachOSectionAndSectionClass<P>* machOSects = (MachOSectionAndSectionClass<P>*)machOSectsStorage;
        unsigned int count = 0;
+       // local variable for bitcode parsing
+       const macho_section<P>* bitcodeSect = NULL;
+       const macho_section<P>* cmdlineSect = NULL;
+       const macho_section<P>* swiftCmdlineSect = NULL;
+       const macho_section<P>* bundleSect = NULL;
+       bool bitcodeAsm = false;
+
        for (uint32_t i=0; i < _machOSectionsCount; ++i) {
                const macho_section<P>* sect = &_sectionsStart[i];
+               uint8_t sectionType = (sect->flags() & SECTION_TYPE);
+               if ( (sect->offset() + sect->size() > _fileLength) && (sectionType != S_ZEROFILL) && (sectionType != S_THREAD_LOCAL_ZEROFILL) )
+                       throwf("section %s/%s extends beyond end of file,", sect->segname(), sect->sectname());
+
                if ( (sect->flags() & S_ATTR_DEBUG) != 0 ) {
                        if ( strcmp(sect->segname(), "__DWARF") == 0 ) {
                                // note that .o file has dwarf
@@ -1998,6 +2506,23 @@ void Parser<A>::makeSections()
                                }
                        }
                }
+               if ( strcmp(sect->segname(), "__LLVM") == 0 ) {
+                       // Process bitcode segement
+                       if ( strncmp(sect->sectname(), "__bitcode", 9) == 0 ) {
+                               bitcodeSect = sect;
+                       } else if ( strncmp(sect->sectname(), "__cmdline", 9) == 0 ) {
+                               cmdlineSect = sect;
+                       } else if ( strncmp(sect->sectname(), "__swift_cmdline", 15) == 0 ) {
+                               swiftCmdlineSect = sect;
+                       } else if ( strncmp(sect->sectname(), "__bundle", 8) == 0 ) {
+                               bundleSect = sect;
+                       } else if ( strncmp(sect->sectname(), "__asm", 5) == 0 ) {
+                               bitcodeAsm = true;
+                       }
+                       // If treat the bitcode as data, continue to parse as a normal section.
+                       if ( !_treateBitcodeAsData )
+                               continue;
+               }
                // ignore empty __OBJC sections
                if ( (sect->size() == 0) && (strcmp(sect->segname(), "__OBJC") == 0) )
                        continue;
@@ -2010,6 +2535,8 @@ void Parser<A>::makeSections()
                        //      };
                        // #define OBJC_IMAGE_SUPPORTS_GC   2
                        // #define OBJC_IMAGE_GC_ONLY       4
+                       // #define OBJC_IMAGE_IS_SIMULATED  32
+                       // #define OBJC_IMAGE_HAS_CATEGORY_CLASS_PROPERTIES  64
                        //
                        const uint32_t* contents = (uint32_t*)(_file->fileContent()+sect->offset());
                        if ( (sect->size() >= 8) && (contents[0] == 0) ) {
@@ -2018,8 +2545,12 @@ void Parser<A>::makeSections()
                                        _file->_objConstraint = ld::File::objcConstraintGC;
                                else if ( (flags & 2) == 2 )
                                        _file->_objConstraint = ld::File::objcConstraintRetainReleaseOrGC;
+                               else if ( (flags & 32) == 32 )
+                                       _file->_objConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
                                else
                                        _file->_objConstraint = ld::File::objcConstraintRetainRelease;
+                               _file->_swiftVersion = ((flags >> 8) & 0xFF);
+                _file->_objcHasCategoryClassPropertiesField = (flags & 64);
                                if ( sect->size() > 8 ) {
                                        warning("section %s/%s has unexpectedly large size %llu in %s", 
                                                        sect->segname(), Section<A>::makeSectionName(sect), sect->size(), _file->path());
@@ -2057,6 +2588,10 @@ void Parser<A>::makeSections()
                                totalSectionsSize += sizeof(NonLazyPointerSection<A>);
                                machOSects[count++].type = sectionTypeNonLazy;
                                break;
+                       case S_THREAD_LOCAL_VARIABLE_POINTERS:
+                               totalSectionsSize += sizeof(TLVPointerSection<A>);
+                               machOSects[count++].type = sectionTypeTLVPointers;
+                               break;
                        case S_LITERAL_POINTERS:
                                if ( (strcmp(sect->segname(), "__OBJC") == 0) && (strcmp(sect->sectname(), "__cls_refs") == 0) ) {
                                        totalSectionsSize += sizeof(Objc1ClassReferences<A>);
@@ -2113,11 +2648,26 @@ void Parser<A>::makeSections()
                                totalSectionsSize += sizeof(TLVDefsSection<A>);
                                machOSects[count++].type = sectionTypeTLVDefs;
                                break;
-                       case S_THREAD_LOCAL_VARIABLE_POINTERS:
                        default:
                                throwf("unknown section type %d", sect->flags() & SECTION_TYPE);
                }
        }
+
+       // Create bitcode
+       if ( bitcodeSect != NULL ) {
+               if ( cmdlineSect != NULL )
+                       _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::ClangBitcode(&_fileContent[bitcodeSect->offset()], bitcodeSect->size(),
+                                                                                                                                                               &_fileContent[cmdlineSect->offset()], cmdlineSect->size()));
+               else if ( swiftCmdlineSect != NULL )
+                       _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::SwiftBitcode(&_fileContent[bitcodeSect->offset()], bitcodeSect->size(),
+                                                                                                                                                               &_fileContent[swiftCmdlineSect->offset()], swiftCmdlineSect->size()));
+               else
+                       throwf("Object file with bitcode missing cmdline options: %s", _file->path());
+       }
+       else if ( bundleSect != NULL )
+               _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::BundleBitcode(&_fileContent[bundleSect->offset()], bundleSect->size()));
+       else if ( bitcodeAsm )
+               _file->_bitcode = std::unique_ptr<ld::Bitcode>(new ld::AsmBitcode(_fileContent, _fileLength));
        
        // sort by address (mach-o object files don't aways have sections sorted)
        ::qsort(machOSects, count, sizeof(MachOSectionAndSectionClass<P>), MachOSectionAndSectionClass<P>::sorter);
@@ -2160,6 +2710,10 @@ void Parser<A>::makeSections()
                                *objects++ = new (space) NonLazyPointerSection<A>(*this, *_file, machOSects[i].sect);
                                space += sizeof(NonLazyPointerSection<A>);
                                break;
+                       case sectionTypeTLVPointers:
+                               *objects++ = new (space) TLVPointerSection<A>(*this, *_file, machOSects[i].sect);
+                               space += sizeof(TLVPointerSection<A>);
+                               break;
                        case sectionTypeCFI:
                                _EHFrameSection = new (space) CFISection<A>(*this, *_file, machOSects[i].sect);
                                *objects++ = _EHFrameSection;
@@ -2439,8 +2993,14 @@ const char* Parser<A>::scanSymbolTableForAddress(uint64_t addr)
                        continue;
 
                // return with exact match
-               if ( sym.n_value() == addr )
-                       return nameFromSymbol(sym);
+               if ( sym.n_value() == addr ) {
+                       const char* name = nameFromSymbol(sym);
+                       if ( strncmp(name, "ltmp", 4) != 0 ) 
+                               return name;
+                       // treat 'ltmp*' labels as close match
+                       closestSymAddr = sym.n_value();
+                       closestSymName = name;
+               }
                
                // record closest seen so far
                if ( (sym.n_value() < addr) && ((sym.n_value() > closestSymAddr) || (closestSymName == NULL)) )
@@ -2495,6 +3055,29 @@ void Parser<A>::addFixups(const SourceLocation& src, ld::Fixup::Kind setKind, co
                        case ld::Fixup::kindStoreThumbBranch22:
                                firstKind = ld::Fixup::kindStoreTargetAddressThumbBranch22;
                                break;
+#if SUPPORT_ARCH_arm64
+                       case ld::Fixup::kindStoreARM64Branch26:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64Branch26;
+                               break;
+                       case ld::Fixup::kindStoreARM64Page21:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64Page21;
+                               break;
+                       case ld::Fixup::kindStoreARM64PageOff12:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64PageOff12;
+                               break;
+                       case ld::Fixup::kindStoreARM64GOTLoadPage21:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21;
+                               break;
+                       case ld::Fixup::kindStoreARM64GOTLoadPageOff12:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64GOTLoadPageOff12;
+                               break;
+                       case ld::Fixup::kindStoreARM64TLVPLoadPage21:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21;
+                               break;
+                       case ld::Fixup::kindStoreARM64TLVPLoadPageOff12:
+                               firstKind = ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPageOff12;
+                               break;
+#endif
                        default:
                                combined = false;
                                cl = ld::Fixup::k1of2;
@@ -2513,6 +3096,10 @@ void Parser<A>::addFixups(const SourceLocation& src, ld::Fixup::Kind setKind, co
                        // backing string in CFStrings should always be direct
                        addFixup(src, cl, firstKind, target.atom);
                }
+               else if ( (src.atom == target.atom) && (target.atom->combine() == ld::Atom::combineByName) ) {
+                       // reference to self should always be direct
+                       addFixup(src, cl, firstKind, target.atom);
+               }
                else {
                        // change direct fixup to by-name fixup
                        addFixup(src, cl, firstKind, false, target.atom->name());
@@ -2593,10 +3180,10 @@ uint32_t TentativeDefinitionSection<A>::appendAtoms(class Parser<A>& parser, uin
                                alignP2 = 63 - (uint8_t)__builtin_clzll(size);
                                if ( size != (1ULL << alignP2) )
                                        ++alignP2;
+                               // <rdar://problem/24871389> limit default alignment of large commons
+                               if ( alignP2 > parser.maxDefaultCommonAlignment() )
+                                       alignP2 = parser.maxDefaultCommonAlignment();
                        }
-                       // limit alignment of extremely large commons to 2^15 bytes (8-page)
-                       if ( alignP2 > 15 )
-                               alignP2 = 15;
                        Atom<A>* allocatedSpace = (Atom<A>*)p;
                        new (allocatedSpace) Atom<A>(*this, parser.nameFromSymbol(sym), (pint_t)ULLONG_MAX, size,
                                                                                ld::Atom::definitionTentative,  ld::Atom::combineByName, 
@@ -2696,10 +3283,11 @@ uint32_t Parser<A>::symbolIndexFromIndirectSectionAddress(pint_t addr, const mac
                        break;
                case S_LAZY_SYMBOL_POINTERS:
                case S_NON_LAZY_SYMBOL_POINTERS:
+               case S_THREAD_LOCAL_VARIABLE_POINTERS:
                        elementSize = sizeof(pint_t);
                        break;
                default:
-                       throw "section does not use inirect symbol table";
+                       throw "section does not use indirect symbol table";
        }       
        uint32_t indexInSection = (addr - sect->addr()) / elementSize;
        uint32_t indexIntoIndirectTable = sect->reserved1() + indexInSection;
@@ -2792,6 +3380,12 @@ bool Parser<A>::resolverFromSymbol(const macho_nlist<P>& sym)
        return ( sym.n_desc() & N_SYMBOL_RESOLVER );
 }
 
+template <typename A>
+bool Parser<A>::altEntryFromSymbol(const macho_nlist<P>& sym)
+{
+       return ( sym.n_desc() & N_ALT_ENTRY );
+}
+
 
 /* Skip over a LEB128 value (signed or unsigned).  */
 static void
@@ -2898,6 +3492,22 @@ bool Parser<A>::skip_form(const uint8_t ** offset, const uint8_t * end, uint64_t
       sz = 4;
       break;
 
+       case DW_FORM_sec_offset:
+         sz = sizeof(typename A::P::uint_t);
+      break;
+       
+       case DW_FORM_exprloc:
+      sz = read_uleb128 (offset, end);
+      break;
+
+    case DW_FORM_flag_present:
+         sz = 0;
+         break;
+         
+    case DW_FORM_ref_sig8:
+         sz = 8;
+         break;
+
     default:
       return false;
     }
@@ -2909,23 +3519,53 @@ bool Parser<A>::skip_form(const uint8_t ** offset, const uint8_t * end, uint64_t
 
 
 template <typename A>
-const char* Parser<A>::getDwarfString(uint64_t form, const uint8_t* p)
+const char* Parser<A>::getDwarfString(uint64_t form, const uint8_t*& di)
 {
-       if ( form == DW_FORM_string )
-               return (const char*)p;
-       else if ( form == DW_FORM_strp ) {
-               uint32_t offset = E::get32(*((uint32_t*)p));
-               const char* dwarfStrings = (char*)_file->fileContent() + _file->_dwarfDebugStringSect->offset();
-               if ( offset > _file->_dwarfDebugStringSect->size() ) {
-                       warning("unknown dwarf DW_FORM_strp (offset=0x%08X) is too big in %s\n", offset, this->_path);
-                       return NULL;
-               }
-               return &dwarfStrings[offset];
-       }
-       warning("unknown dwarf string encoding (form=%lld) in %s\n", form, this->_path);
-       return NULL;
-}
-
+       uint32_t offset;
+       const char* dwarfStrings;
+       const char* result = NULL;
+       switch (form) {
+               case DW_FORM_string:
+                       result = (const char*)di;
+                       di += strlen(result) + 1;
+                       break;
+               case DW_FORM_strp:
+                       offset = E::get32(*((uint32_t*)di));
+                       dwarfStrings = (char*)_file->fileContent() + _file->_dwarfDebugStringSect->offset();
+                       if ( offset < _file->_dwarfDebugStringSect->size() )
+                               result = &dwarfStrings[offset];
+                       else
+                               warning("dwarf DW_FORM_strp (offset=0x%08X) is too big in %s", offset, this->_path);
+                       di += 4;
+                       break;
+               default:
+                       warning("unknown dwarf string encoding (form=%lld) in %s", form, this->_path);
+                       break;
+       }
+       return result;
+}
+
+template <typename A>
+uint64_t Parser<A>::getDwarfOffset(uint64_t form, const uint8_t*& di, bool dwarf64)
+{
+       if ( form == DW_FORM_sec_offset ) 
+               form = (dwarf64 ? DW_FORM_data8 : DW_FORM_data4);
+       uint64_t result = -1;
+       switch (form) {
+               case DW_FORM_data4:
+                       result = A::P::E::get32(*(uint32_t*)di);
+                       di += 4;
+                       break;
+               case DW_FORM_data8:
+                       result = A::P::E::get64(*(uint64_t*)di);
+                       di += 8;
+                       break;
+               default:
+                       warning("unknown dwarf DW_FORM_ for DW_AT_stmt_list in %s", this->_path);
+       }
+       return result;
+}
+
 
 template <typename A>
 struct AtomAndLineInfo {
@@ -2965,14 +3605,27 @@ void Parser<A>::parseDebugInfo()
                return;
                
        uint64_t stmtList;
-       if ( !read_comp_unit(&_file->_dwarfTranslationUnitFile, &_file->_dwarfTranslationUnitDir, &stmtList) ) {
+       const char* tuDir;
+       const char* tuName;
+       if ( !read_comp_unit(&tuName, &tuDir, &stmtList) ) {
                // if can't parse dwarf, warn and give up
-               _file->_dwarfTranslationUnitFile = NULL;
-               _file->_dwarfTranslationUnitDir = NULL;
+               _file->_dwarfTranslationUnitPath = NULL;
                warning("can't parse dwarf compilation unit info in %s", _path);
                _file->_debugInfoKind = ld::relocatable::File::kDebugInfoNone;
                return;
        }
+       if ( (tuName != NULL) && (tuName[0] == '/') ) {
+               _file->_dwarfTranslationUnitPath = tuName;
+       }
+       else if ( (tuDir != NULL) && (tuName != NULL) ) {
+               asprintf((char**)&(_file->_dwarfTranslationUnitPath), "%s/%s", tuDir, tuName);
+       }
+       else if ( tuDir == NULL ) {
+               _file->_dwarfTranslationUnitPath = tuName;
+       }
+       else {
+               _file->_dwarfTranslationUnitPath = NULL;
+       }
        
        // add line number info to atoms from dwarf
        std::vector<AtomAndLineInfo<A> > entries;
@@ -3092,7 +3745,7 @@ void Parser<A>::parseDebugInfo()
                p += sizeof(Atom<A>);
        }
        assert(liOffset == entries.size());
-       _file->_lineInfos.reserve(liOffset);
+       _file->_lineInfos.resize(liOffset);
 
        // copy each line info for each atom 
        for (typename std::vector<AtomAndLineInfo<A> >::iterator it = entries.begin(); it != entries.end(); ++it) {
@@ -3152,6 +3805,7 @@ void Parser<A>::parseStabs()
                                                case N_LSYM:
                                                case N_RSYM:
                                                case N_PSYM:
+                                               case N_AST:
                                                        // not associated with an atom, just copy
                                                        stab.string = symString;
                                                        break;
@@ -3327,6 +3981,7 @@ bool Parser<A>::read_comp_unit(const char ** name, const char ** comp_dir,
        const uint8_t * debug_info;
        const uint8_t * debug_abbrev;
        const uint8_t * di;
+       const uint8_t * next_cu;
        const uint8_t * da;
        const uint8_t * end;
        const uint8_t * enda;
@@ -3344,105 +3999,122 @@ bool Parser<A>::read_comp_unit(const char ** name, const char ** comp_dir,
        if ( (_file->_dwarfDebugInfoSect == NULL) || (_file->_dwarfDebugAbbrevSect == NULL) )
                return false;
 
-       debug_info = (uint8_t*)_file->fileContent() + _file->_dwarfDebugInfoSect->offset();
-       debug_abbrev = (uint8_t*)_file->fileContent() + _file->_dwarfDebugAbbrevSect->offset();
-       di = debug_info;
-
        if (_file->_dwarfDebugInfoSect->size() < 12)
-               /* Too small to be a real debug_info section.  */
-               return false;
-       sz = A::P::E::get32(*(uint32_t*)di);
-       di += 4;
-       dwarf64 = sz == 0xffffffff;
-       if (dwarf64)
-               sz = A::P::E::get64(*(uint64_t*)di), di += 8;
-       else if (sz > 0xffffff00)
-               /* Unknown dwarf format.  */
-               return false;
-
-       /* Verify claimed size.  */
-       if (sz + (di - debug_info) > _file->_dwarfDebugInfoSect->size() || sz <= (dwarf64 ? 23 : 11))
-               return false;
-
-       vers = A::P::E::get16(*(uint16_t*)di);
-       if (vers < 2 || vers > 3)
-       /* DWARF version wrong for this code.
-          Chances are we could continue anyway, but we don't know for sure.  */
-               return false;
-       di += 2;
-
-       /* Find the debug_abbrev section.  */
-       abbrev_base = dwarf64 ? A::P::E::get64(*(uint64_t*)di) : A::P::E::get32(*(uint32_t*)di);
-       di += dwarf64 ? 8 : 4;
-
-       if (abbrev_base > _file->_dwarfDebugAbbrevSect->size())
-               return false;
-       da = debug_abbrev + abbrev_base;
-       enda = debug_abbrev + _file->_dwarfDebugAbbrevSect->size();
-
-       address_size = *di++;
-
-       /* Find the abbrev number we're looking for.  */
-       end = di + sz;
-       abbrev = read_uleb128 (&di, end);
-       if (abbrev == (uint64_t) -1)
+       /* Too small to be a real debug_info section.  */
                return false;
 
-       /* Skip through the debug_abbrev section looking for that abbrev.  */
-       for (;;)
-       {
-               uint64_t this_abbrev = read_uleb128 (&da, enda);
-               uint64_t attr;
-
-               if (this_abbrev == abbrev)
-                       /* This is almost always taken.  */
-                       break;
-               skip_leb128 (&da, enda); /* Skip the tag.  */
-               if (da == enda)
-                       return false;
-               da++;  /* Skip the DW_CHILDREN_* value.  */
-
-               do {
-                       attr = read_uleb128 (&da, enda);
-                       skip_leb128 (&da, enda);
-               } while (attr != 0 && attr != (uint64_t) -1);
-               if (attr != 0)
-                       return false;
-       }
-
-       /* Check that the abbrev is one for a DW_TAG_compile_unit.  */
-       if (read_uleb128 (&da, enda) != DW_TAG_compile_unit)
-       return false;
-       if (da == enda)
-       return false;
-       da++;  /* Skip the DW_CHILDREN_* value.  */
-
-       /* Now, go through the DIE looking for DW_AT_name,
-        DW_AT_comp_dir, and DW_AT_stmt_list.  */
-       for (;;)
-       {
-               uint64_t attr = read_uleb128 (&da, enda);
-               uint64_t form = read_uleb128 (&da, enda);
-
-               if (attr == (uint64_t) -1)
-                       return false;
-               else if (attr == 0)
-                       return true;
-
-               if (form == DW_FORM_indirect)
-                       form = read_uleb128 (&di, end);
-
-               if (attr == DW_AT_name)
-                       *name = getDwarfString(form, di);
-               else if (attr == DW_AT_comp_dir)
-                       *comp_dir = getDwarfString(form, di);
-               else if (attr == DW_AT_stmt_list && form == DW_FORM_data4)
-                       *stmt_list = A::P::E::get32(*(uint32_t*)di);
-               else if (attr == DW_AT_stmt_list && form == DW_FORM_data8)
-                       *stmt_list = A::P::E::get64(*(uint64_t*)di);
-               if (! skip_form (&di, end, form, address_size, dwarf64))
-                       return false;
-       }
+    debug_info = (uint8_t*)_file->fileContent() + _file->_dwarfDebugInfoSect->offset();
+    debug_abbrev = (uint8_t*)_file->fileContent() + _file->_dwarfDebugAbbrevSect->offset();
+    next_cu = debug_info;
+
+    while ((uint64_t)(next_cu - debug_info) < _file->_dwarfDebugInfoSect->size()) {
+               di = next_cu;
+        sz = A::P::E::get32(*(uint32_t*)di);
+        di += 4;
+        dwarf64 = sz == 0xffffffff;
+        if (dwarf64)
+            sz = A::P::E::get64(*(uint64_t*)di), di += 8;
+        else if (sz > 0xffffff00)
+            /* Unknown dwarf format.  */
+            return false;
+
+        /* Verify claimed size.  */
+        if (sz + (di - debug_info) > _file->_dwarfDebugInfoSect->size() || sz <= (dwarf64 ? 23 : 11))
+            return false;
+
+        next_cu = di + sz;
+
+        vers = A::P::E::get16(*(uint16_t*)di);
+        if (vers < 2 || vers > 4)
+        /* DWARF version wrong for this code.
+           Chances are we could continue anyway, but we don't know for sure.  */
+            return false;
+        di += 2;
+
+        /* Find the debug_abbrev section.  */
+        abbrev_base = dwarf64 ? A::P::E::get64(*(uint64_t*)di) : A::P::E::get32(*(uint32_t*)di);
+        di += dwarf64 ? 8 : 4;
+
+        if (abbrev_base > _file->_dwarfDebugAbbrevSect->size())
+            return false;
+        da = debug_abbrev + abbrev_base;
+        enda = debug_abbrev + _file->_dwarfDebugAbbrevSect->size();
+
+        address_size = *di++;
+
+        /* Find the abbrev number we're looking for.  */
+        end = di + sz;
+        abbrev = read_uleb128 (&di, end);
+        if (abbrev == (uint64_t) -1)
+            return false;
+
+        /* Skip through the debug_abbrev section looking for that abbrev.  */
+        for (;;)
+        {
+            uint64_t this_abbrev = read_uleb128 (&da, enda);
+            uint64_t attr;
+
+            if (this_abbrev == abbrev)
+                /* This is almost always taken.  */
+                break;
+            skip_leb128 (&da, enda); /* Skip the tag.  */
+            if (da == enda)
+                return false;
+            da++;  /* Skip the DW_CHILDREN_* value.  */
+
+            do {
+                attr = read_uleb128 (&da, enda);
+                skip_leb128 (&da, enda);
+            } while (attr != 0 && attr != (uint64_t) -1);
+            if (attr != 0)
+                return false;
+        }
+
+        /* Check that the abbrev is one for a DW_TAG_compile_unit.  */
+        if (read_uleb128 (&da, enda) != DW_TAG_compile_unit)
+        return false;
+        if (da == enda)
+        return false;
+        da++;  /* Skip the DW_CHILDREN_* value.  */
+
+        /* Now, go through the DIE looking for DW_AT_name,
+         DW_AT_comp_dir, and DW_AT_stmt_list.  */
+        bool skip_to_next_cu = false;
+        while (!skip_to_next_cu) {
+
+            uint64_t attr = read_uleb128 (&da, enda);
+            uint64_t form = read_uleb128 (&da, enda);
+
+            if (attr == (uint64_t) -1)
+                return false;
+            else if (attr == 0)
+                return true;
+            if (form == DW_FORM_indirect)
+                form = read_uleb128 (&di, end);
+
+            switch (attr) {
+                case DW_AT_name:
+                    *name = getDwarfString(form, di);
+                    /* Swift object files may contain two CUs: One
+                       describes the Swift code, one is created by the
+                       clang importer. Skip over the CU created by the
+                       clang importer as it may be empty. */
+                    if (std::string(*name) == "<swift-imported-modules>")
+                        skip_to_next_cu = true;
+                    break;
+                case DW_AT_comp_dir:
+                    *comp_dir = getDwarfString(form, di);
+                    break;
+                case DW_AT_stmt_list:
+                    *stmt_list = getDwarfOffset(form, di, dwarf64);
+                    break;
+                default:
+                    if (! skip_form (&di, end, form, address_size, dwarf64))
+                        return false;
+            }
+        }
+    }
+    return false;
 }
 
 
@@ -3455,18 +4127,11 @@ File<A>::~File()
 }
 
 template <typename A>
-bool File<A>::translationUnitSource(const char** dir, const char** name) const
+const char* File<A>::translationUnitSource() const
 {
-       if ( _debugInfoKind == ld::relocatable::File::kDebugInfoDwarf ) {
-               *dir = _dwarfTranslationUnitDir;
-               *name = _dwarfTranslationUnitFile;
-               return (_dwarfTranslationUnitFile != NULL);
-       }
-       return false;
+       return _dwarfTranslationUnitPath;
 }
 
-       
-
 template <typename A>
 bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
 {
@@ -3476,7 +4141,13 @@ bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
                handler.doAtom(*((Atom<A>*)p));
                p += sizeof(Atom<A>);
        }
-       return (_atomsArrayCount != 0);
+       p = _aliasAtomsArray;
+       for(int i=_aliasAtomsArrayCount; i > 0; --i) {
+               handler.doAtom(*((AliasAtom*)p));
+               p += sizeof(AliasAtom);
+       }
+       
+       return (_atomsArrayCount != 0) || (_aliasAtomsArrayCount != 0);
 }
 
 template <typename A>
@@ -3620,6 +4291,8 @@ ld::Section::Type Section<A>::sectionType(const macho_section<typename A::P>* se
                        return ld::Section::typeTLVZeroFill;
                case S_THREAD_LOCAL_VARIABLES:
                        return ld::Section::typeTLVDefs;
+               case S_THREAD_LOCAL_VARIABLE_POINTERS:
+                       return ld::Section::typeTLVPointers;
                case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
                        return ld::Section::typeTLVInitializerPointers;
        }
@@ -3663,7 +4336,10 @@ template <typename A>
 ld::Atom::Alignment Section<A>::alignmentForAddress(pint_t addr) 
 { 
        const uint32_t sectionAlignment = this->_machOSection->align();
-       return ld::Atom::Alignment(sectionAlignment, (addr % (1 << sectionAlignment)));
+       uint32_t modulus = (addr % (1 << sectionAlignment));
+       if ( modulus > 0xFFFF )
+               warning("alignment for symbol at address 0x%08llX in %s exceeds 2^16", (uint64_t)addr, this->file().path());
+       return ld::Atom::Alignment(sectionAlignment, modulus);
 }
 
 template <typename A>
@@ -3676,10 +4352,20 @@ uint32_t Section<A>::sectionNum(class Parser<A>& parser) const
 }
 
 // arm does not have zero cost exceptions
-template <> uint32_t CFISection<arm>::cfiCount() { return 0; }
+template <> 
+uint32_t CFISection<arm>::cfiCount(Parser<arm>& parser) 
+{
+       if ( parser.armUsesZeroCostExceptions() ) {
+               // create ObjectAddressSpace object for use by libunwind
+               OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
+               return libunwind::CFI_Parser<OAS>::getCFICount(oas, 
+                                                                                       this->_machOSection->addr(), this->_machOSection->size());
+       }
+       return 0; 
+}
 
 template <typename A>
-uint32_t CFISection<A>::cfiCount()
+uint32_t CFISection<A>::cfiCount(Parser<A>& parser)
 {
        // create ObjectAddressSpace object for use by libunwind
        OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
@@ -3691,7 +4377,7 @@ template <typename A>
 void CFISection<A>::warnFunc(void* ref, uint64_t funcAddr, const char* msg)
 {
        Parser<A>* parser = (Parser<A>*)ref;
-       if ( ! parser->convertUnwindInfo() ) 
+       if ( ! parser->warnUnwindConversionProblems() ) 
                return;
        if ( funcAddr != CFI_INVALID_ADDRESS ) {
                // atoms are not constructed yet, so scan symbol table for labels
@@ -3709,6 +4395,13 @@ bool CFISection<x86_64>::needsRelocating()
        return true;
 }
 
+template <>
+bool CFISection<arm64>::needsRelocating()
+{
+       return true;
+}
+
+
 template <typename A>
 bool CFISection<A>::needsRelocating()
 {
@@ -3716,12 +4409,13 @@ bool CFISection<A>::needsRelocating()
 }
 
 template <>
-void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer, 
+void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer,
                                                                        libunwind::CFI_Atom_Info<CFISection<x86_64>::OAS>::CFI_Atom_Info cfiArray[], 
-                                                                       uint32_t count)
+                                                                       uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
 {
+       const uint32_t sectionSize = this->_machOSection->size();
        // copy __eh_frame data to buffer
-       memcpy(buffer, file().fileContent() + this->_machOSection->offset(), this->_machOSection->size());
+       memcpy(buffer, file().fileContent() + this->_machOSection->offset(), sectionSize);
 
        // and apply relocations
        const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(file().fileContent() + this->_machOSection->reloff());
@@ -3747,6 +4441,8 @@ void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer,
                                fprintf(stderr, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc->r_address());
                                break;
                }
+               if ( reloc->r_address() > sectionSize )
+                       throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc->r_address());
                uint64_t*       p64;
                uint32_t*       p32;
                switch ( reloc->r_length() ) {
@@ -3764,7 +4460,6 @@ void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer,
                }
        }
        
-       
        // create ObjectAddressSpace object for use by libunwind
        OAS oas(*this, buffer);
        
@@ -3772,6 +4467,7 @@ void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer,
        const char* msg;
        msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_x86_64>::parseCFIs(
                                                        oas, this->_machOSection->addr(), this->_machOSection->size(), 
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(), 
                                                        cfiArray, count, (void*)&parser, warnFunc);
        if ( msg != NULL ) 
                throwf("malformed __eh_frame section: %s", msg);
@@ -3780,7 +4476,7 @@ void CFISection<x86_64>::cfiParse(class Parser<x86_64>& parser, uint8_t* buffer,
 template <>
 void CFISection<x86>::cfiParse(class Parser<x86>& parser, uint8_t* buffer, 
                                                                        libunwind::CFI_Atom_Info<CFISection<x86>::OAS>::CFI_Atom_Info cfiArray[], 
-                                                                       uint32_t count)
+                                                                       uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
 {
        // create ObjectAddressSpace object for use by libunwind
        OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
@@ -3789,6 +4485,7 @@ void CFISection<x86>::cfiParse(class Parser<x86>& parser, uint8_t* buffer,
        const char* msg;
        msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_x86>::parseCFIs(
                                                        oas, this->_machOSection->addr(), this->_machOSection->size(), 
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
                                                        cfiArray, count, (void*)&parser, warnFunc);
        if ( msg != NULL ) 
                throwf("malformed __eh_frame section: %s", msg);
@@ -3800,14 +4497,98 @@ void CFISection<x86>::cfiParse(class Parser<x86>& parser, uint8_t* buffer,
 template <>
 void CFISection<arm>::cfiParse(class Parser<arm>& parser, uint8_t* buffer, 
                                                                        libunwind::CFI_Atom_Info<CFISection<arm>::OAS>::CFI_Atom_Info cfiArray[], 
-                                                                       uint32_t count)
+                                                                       uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
 {
-       // arm does not use zero cost exceptions
-       assert(count == 0);
+       if ( !parser.armUsesZeroCostExceptions() ) {
+               // most arm do not use zero cost exceptions
+               assert(count == 0);
+               return;
+       }
+       // create ObjectAddressSpace object for use by libunwind
+       OAS oas(*this, (uint8_t*)this->file().fileContent()+this->_machOSection->offset());
+       
+       // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
+       const char* msg;
+       msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_arm>::parseCFIs(
+                                                       oas, this->_machOSection->addr(), this->_machOSection->size(), 
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
+                                                       cfiArray, count, (void*)&parser, warnFunc);
+       if ( msg != NULL ) 
+               throwf("malformed __eh_frame section: %s", msg);
 }
 
 
 
+
+template <>
+void CFISection<arm64>::cfiParse(class Parser<arm64>& parser, uint8_t* buffer, 
+                                                                       libunwind::CFI_Atom_Info<CFISection<arm64>::OAS>::CFI_Atom_Info cfiArray[], 
+                                                                       uint32_t& count, const pint_t cuStarts[], uint32_t cuCount)
+{
+       // copy __eh_frame data to buffer
+       const uint32_t sectionSize = this->_machOSection->size();
+       memcpy(buffer, file().fileContent() + this->_machOSection->offset(), sectionSize);
+
+       // and apply relocations
+       const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(file().fileContent() + this->_machOSection->reloff());
+       const macho_relocation_info<P>* relocsEnd = &relocs[this->_machOSection->nreloc()];
+       for (const macho_relocation_info<P>* reloc = relocs; reloc < relocsEnd; ++reloc) {
+               uint64_t* p64 = (uint64_t*)&buffer[reloc->r_address()];
+               uint32_t* p32 = (uint32_t*)&buffer[reloc->r_address()];
+               uint32_t addend32 = E::get32(*p32); 
+               uint64_t addend64 = E::get64(*p64); 
+               uint64_t value = 0;
+               switch ( reloc->r_type() ) {
+                       case ARM64_RELOC_SUBTRACTOR:    
+                               value =  0 - parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
+                               ++reloc;
+                               if ( reloc->r_extern() )
+                                       value += parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
+                               break;
+                       case ARM64_RELOC_UNSIGNED:
+                               value = parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
+                               break;
+                       case ARM64_RELOC_POINTER_TO_GOT:
+                               // this is used for the reference to the personality function in CIEs
+                               // store the symbol number of the personality function for later use as a Fixup
+                               value = reloc->r_symbolnum();
+                               addend32 = 0;
+                               addend64 = 0;
+                               break;
+                       default:
+                               fprintf(stderr, "CFISection::cfiParse() unexpected relocation type at r_address=0x%08X\n", reloc->r_address());
+                               break;
+               }
+               if ( reloc->r_address() > sectionSize )
+                       throwf("malformed __eh_frame relocation, offset (0x%08X) is beyond end of section,", reloc->r_address());
+               switch ( reloc->r_length() ) {
+                       case 3:
+                               E::set64(*p64, value + addend64);
+                               break;
+                       case 2:
+                               E::set32(*p32, value + addend32);
+                               break;
+                       default:
+                               fprintf(stderr, "CFISection::cfiParse() unexpected relocation size at r_address=0x%08X\n", reloc->r_address());
+                               break;
+               }
+       }
+       
+       
+       // create ObjectAddressSpace object for use by libunwind
+       OAS oas(*this, buffer);
+       
+       // use libuwind to parse __eh_frame data into array of CFI_Atom_Info
+       const char* msg;
+       msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_arm64>::parseCFIs(
+                                                       oas, this->_machOSection->addr(), this->_machOSection->size(), 
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
+                                                       cfiArray, count, (void*)&parser, warnFunc);
+       if ( msg != NULL ) 
+               throwf("malformed __eh_frame section: %s", msg);
+}
+
+
 template <typename A>
 uint32_t CFISection<A>::computeAtomCount(class Parser<A>& parser, 
                                                                                        struct Parser<A>::LabelAndCFIBreakIterator& it, 
@@ -3843,7 +4624,7 @@ uint32_t CFISection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
 template <> bool CFISection<x86_64>::bigEndian() { return false; }
 template <> bool CFISection<x86>::bigEndian() { return false; }
 template <> bool CFISection<arm>::bigEndian() { return false; }
-
+template <> bool CFISection<arm64>::bigEndian() { return false; }
 
 template <>
 void CFISection<x86_64>::addCiePersonalityFixups(class Parser<x86_64>& parser, const CFI_Atom_Info* cieInfo)
@@ -3891,12 +4672,60 @@ void CFISection<x86>::addCiePersonalityFixups(class Parser<x86>& parser, const C
        }
 }
 
+#if SUPPORT_ARCH_arm64
+template <>
+void CFISection<arm64>::addCiePersonalityFixups(class Parser<arm64>& parser, const CFI_Atom_Info* cieInfo)
+{
+       uint8_t personalityEncoding = cieInfo->u.cieInfo.personality.encodingOfTargetAddress;
+       if ( personalityEncoding == 0x9B ) {
+               // compiler always produces ARM64_RELOC_GOT r_pcrel=1 to personality function
+               // CFISection<arm64>::cfiParse() set targetAddress to be symbolIndex + addressInCIE
+               uint32_t symbolIndex = cieInfo->u.cieInfo.personality.targetAddress 
+                                                                       - cieInfo->address - cieInfo->u.cieInfo.personality.offsetInCFI;
+               const macho_nlist<P>& sym = parser.symbolFromIndex(symbolIndex);
+               const char* personalityName = parser.nameFromSymbol(sym);
+
+               Atom<arm64>* cieAtom = this->findAtomByAddress(cieInfo->address);
+               Parser<arm64>::SourceLocation src(cieAtom, cieInfo->u.cieInfo.personality.offsetInCFI);
+               parser.addFixup(src, ld::Fixup::k1of2, ld::Fixup::kindSetTargetAddress, false, personalityName);
+               parser.addFixup(src, ld::Fixup::k2of2, ld::Fixup::kindStoreARM64PCRelToGOT);
+       }
+       else if ( personalityEncoding != 0 ) {
+               throwf("unsupported address encoding (%02X) of personality function in CIE", 
+                               personalityEncoding);
+       }
+}
+#endif
+
+
+template <>
+void CFISection<arm>::addCiePersonalityFixups(class Parser<arm>& parser, const CFI_Atom_Info* cieInfo)
+{
+       uint8_t personalityEncoding = cieInfo->u.cieInfo.personality.encodingOfTargetAddress;
+       if ( (personalityEncoding == 0x9B) || (personalityEncoding == 0x90) ) {
+               uint32_t offsetInCFI = cieInfo->u.cieInfo.personality.offsetInCFI;
+               uint32_t nlpAddr = cieInfo->u.cieInfo.personality.targetAddress;
+               Atom<arm>* cieAtom = this->findAtomByAddress(cieInfo->address);
+               Atom<arm>* nlpAtom = parser.findAtomByAddress(nlpAddr);
+               assert(nlpAtom->contentType() == ld::Atom::typeNonLazyPointer);
+               Parser<arm>::SourceLocation src(cieAtom, cieInfo->u.cieInfo.personality.offsetInCFI);
+
+               parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, nlpAtom);
+               parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, cieAtom);
+               parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, offsetInCFI);
+               parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32);
+       }
+       else if ( personalityEncoding != 0 ) {
+               throwf("unsupported address encoding (%02X) of personality function in CIE", personalityEncoding);
+       }
+}
+
+
 
 template <typename A>
 void CFISection<A>::addCiePersonalityFixups(class Parser<A>& parser, const CFI_Atom_Info* cieInfo)
 {
-       // FIX ME
-       assert(0);
+       assert(0 && "addCiePersonalityFixups() not implemented for arch");
 }
 
 template <typename A>
@@ -4130,20 +4959,100 @@ typename A::P::uint_t CFISection<A>::OAS::getEncodedP(pint_t& addr, pint_t end,
 template <>
 const char* CUSection<x86_64>::personalityName(class Parser<x86_64>& parser, const macho_relocation_info<x86_64::P>* reloc)
 {
-       assert(reloc->r_extern() && "reloc not extern on personality column in __compact_unwind section");
-       assert((reloc->r_type() == X86_64_RELOC_UNSIGNED) && "wrong reloc type on personality column in __compact_unwind section");
-       const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
-       return parser.nameFromSymbol(sym);
+       if ( reloc->r_extern() ) {
+               assert((reloc->r_type() == X86_64_RELOC_UNSIGNED) && "wrong reloc type on personality column in __compact_unwind section");
+               const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
+               return parser.nameFromSymbol(sym);
+       }
+       else {
+               const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
+               pint_t personalityAddr = *content;
+               assert((parser.sectionForAddress(personalityAddr)->type() == ld::Section::typeCode) && "personality column in __compact_unwind section is not pointer to function");
+               // atoms may not be constructed yet, so scan symbol table for labels
+               const char* name = parser.scanSymbolTableForAddress(personalityAddr);
+               return name;
+       }
 }
 
 template <>
 const char* CUSection<x86>::personalityName(class Parser<x86>& parser, const macho_relocation_info<x86::P>* reloc)
 {
-       assert(reloc->r_extern() && "reloc not extern on personality column in __compact_unwind section");
-       assert((reloc->r_type() == GENERIC_RELOC_VANILLA) && "wrong reloc type on personality column in __compact_unwind section");
-       const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
-       return parser.nameFromSymbol(sym);
+       if ( reloc->r_extern() ) {
+               assert((reloc->r_type() == GENERIC_RELOC_VANILLA) && "wrong reloc type on personality column in __compact_unwind section");
+               const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
+               return parser.nameFromSymbol(sym);
+       }
+       else {
+               // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
+               const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
+               pint_t nlPointerAddr = *content;
+               Section<x86>* nlSection = parser.sectionForAddress(nlPointerAddr);
+               if ( nlSection->type() == ld::Section::typeCode ) {
+                       // personality function is defined in this .o file, so this is a direct reference to it
+                       // atoms may not be constructed yet, so scan symbol table for labels
+                       const char* name = parser.scanSymbolTableForAddress(nlPointerAddr);
+                       return name;
+               }
+               else {
+                       uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(nlPointerAddr, nlSection->machoSection());
+                       const macho_nlist<P>& nlSymbol = parser.symbolFromIndex(symIndex);
+                       return parser.nameFromSymbol(nlSymbol);
+               }
+       }
+}
+
+#if SUPPORT_ARCH_arm64
+template <>
+const char* CUSection<arm64>::personalityName(class Parser<arm64>& parser, const macho_relocation_info<arm64::P>* reloc)
+{
+       if ( reloc->r_extern() ) {
+               assert((reloc->r_type() == ARM64_RELOC_UNSIGNED) && "wrong reloc type on personality column in __compact_unwind section");
+               const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
+               return parser.nameFromSymbol(sym);
+       }
+       else {
+               const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
+               pint_t personalityAddr = *content;
+               Section<arm64>* personalitySection = parser.sectionForAddress(personalityAddr);
+               (void)personalitySection;
+               assert((personalitySection->type() == ld::Section::typeCode) && "personality column in __compact_unwind section is not pointer to function");
+               // atoms may not be constructed yet, so scan symbol table for labels
+               const char* name = parser.scanSymbolTableForAddress(personalityAddr);
+               return name;
+       }
+}
+#endif
+
+
+#if SUPPORT_ARCH_arm_any
+template <>
+const char* CUSection<arm>::personalityName(class Parser<arm>& parser, const macho_relocation_info<arm::P>* reloc)
+{
+       if ( reloc->r_extern() ) {
+               assert((reloc->r_type() == ARM_RELOC_VANILLA) && "wrong reloc type on personality column in __compact_unwind section");
+               const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
+               return parser.nameFromSymbol(sym);
+       }
+       else {
+               // support __LD, __compact_unwind personality entries which are pointer to personality non-lazy pointer
+               const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
+               pint_t nlPointerAddr = *content;
+               Section<arm>* nlSection = parser.sectionForAddress(nlPointerAddr);
+               if ( nlSection->type() == ld::Section::typeCode ) {
+                       // personality function is defined in this .o file, so this is a direct reference to it
+                       // atoms may not be constructed yet, so scan symbol table for labels
+                       const char* name = parser.scanSymbolTableForAddress(nlPointerAddr);
+                       return name;
+               }
+               else {
+                       uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(nlPointerAddr, nlSection->machoSection());
+                       const macho_nlist<P>& nlSymbol = parser.symbolFromIndex(symIndex);
+                       return parser.nameFromSymbol(nlSymbol);
+               }
+       }
 }
+#endif
+
 
 template <typename A>
 const char* CUSection<A>::personalityName(class Parser<A>& parser, const macho_relocation_info<P>* reloc)
@@ -4151,6 +5060,34 @@ const char* CUSection<A>::personalityName(class Parser<A>& parser, const macho_r
        return NULL;
 }
 
+template <>
+bool CUSection<x86>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
+{
+       return ((enc & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_DWARF);
+}
+
+template <>
+bool CUSection<x86_64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
+{
+       return ((enc & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_DWARF);
+}
+
+#if SUPPORT_ARCH_arm_any
+template <>
+bool CUSection<arm>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
+{
+       return ((enc & UNWIND_ARM_MODE_MASK) == UNWIND_ARM_MODE_DWARF);
+}
+#endif
+
+#if SUPPORT_ARCH_arm64
+template <>
+bool CUSection<arm64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
+{
+       return ((enc & UNWIND_ARM64_MODE_MASK) == UNWIND_ARM64_MODE_DWARF);
+}
+#endif
+
 
 template <typename A>
 int CUSection<A>::infoSorter(const void* l, const void* r)
@@ -4187,11 +5124,15 @@ void CUSection<A>::parse(class Parser<A>& parser, uint32_t cnt, Info array[])
                }
        }
        
-       // scan relocs, local relocs are useless - ignore them
-       // extern relocs are needed for personality references (possibly for function/lsda refs??)
+       // scan relocs, extern relocs are needed for personality references (possibly for function/lsda refs??)
+       const uint32_t sectionSize = this->_machOSection->size();
        const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(this->file().fileContent() + this->_machOSection->reloff());
        const macho_relocation_info<P>* relocsEnd = &relocs[this->_machOSection->nreloc()];
        for (const macho_relocation_info<P>* reloc = relocs; reloc < relocsEnd; ++reloc) {
+               if ( reloc->r_address() & R_SCATTERED )
+                       continue;
+               if ( reloc->r_address() > sectionSize )
+                       throwf("malformed __compact_unwind relocation, offset (0x%08X) is beyond end of section,", reloc->r_address());
                if ( reloc->r_extern() ) {
                        // only expect external relocs on some colummns
                        if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::personalityFieldOffset() ) {
@@ -4209,11 +5150,18 @@ void CUSection<A>::parse(class Parser<A>& parser, uint32_t cnt, Info array[])
                        else if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::codeStartFieldOffset() ) {
                                uint32_t entryIndex = reloc->r_address() / sizeof(macho_compact_unwind_entry<P>);
                                array[entryIndex].functionSymbolIndex = reloc->r_symbolnum();
+                               array[entryIndex].functionStartAddress += parser.symbolFromIndex(reloc->r_symbolnum()).n_value();
                        }
                        else {
                                warning("unexpected extern relocation in __compact_unwind section");
                        }
                }
+               else {
+                       if ( (reloc->r_address() % sizeof(macho_compact_unwind_entry<P>)) == macho_compact_unwind_entry<P>::personalityFieldOffset() ) {
+                               uint32_t entryIndex = reloc->r_address() / sizeof(macho_compact_unwind_entry<P>);
+                               array[entryIndex].personality = this->personalityName(parser, reloc);
+                       }
+               }
        }
        
        // sort array by function start address so unwind infos will be contiguous for a given function
@@ -4236,9 +5184,6 @@ void CUSection<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::C
        Info* const arrayStart = cus.cuArray;
        Info* const arrayEnd = &cus.cuArray[cus.cuCount];
        for (Info* info=arrayStart; info < arrayEnd; ++info) {
-               // if external reloc was used, real address is symbol n_value + addend
-               if ( info->functionSymbolIndex != 0xFFFFFFFF )
-                       info->functionStartAddress += parser.symbolFromIndex(info->functionSymbolIndex).n_value();
                // find function atom from address
                info->function = parser.findAtomByAddress(info->functionStartAddress);  
                // find lsda atom from address
@@ -4324,7 +5269,7 @@ uint32_t SymboledSection<A>::computeAtomCount(class Parser<A>& parser,
        pint_t  addr;
        pint_t  size;
        const macho_nlist<P>* sym;
-       while ( it.next(parser, sectNum, startAddr, endAddr, &addr, &size, &sym) ) {
+       while ( it.next(parser, *this, sectNum, startAddr, endAddr, &addr, &size, &sym) ) {
                ++count;
        }
        //fprintf(stderr, "computeAtomCount(%s,%s) => %d\n", this->segmentName(), this->sectionName(), count);
@@ -4333,7 +5278,7 @@ uint32_t SymboledSection<A>::computeAtomCount(class Parser<A>& parser,
 
 template <typename A>
 uint32_t SymboledSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p, 
-                                                                                       struct Parser<A>::LabelAndCFIBreakIterator& it, 
+                                                                                       struct Parser<A>::LabelAndCFIBreakIterator& it,
                                                                                        const struct Parser<A>::CFI_CU_InfoArrays&)
 {
        this->_beginAtoms = (Atom<A>*)p;
@@ -4347,7 +5292,7 @@ uint32_t SymboledSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
        pint_t  addr;
        pint_t  size;
        const macho_nlist<P>* label;
-       while ( it.next(parser, sectNum, startAddr, endAddr, &addr, &size, &label) ) {
+       while ( it.next(parser, *this, sectNum, startAddr, endAddr, &addr, &size, &label) ) {
                Atom<A>* allocatedSpace = (Atom<A>*)p;
                // is break because of label or CFI?
                if ( label != NULL ) {
@@ -4358,13 +5303,15 @@ uint32_t SymboledSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
                        new (allocatedSpace) Atom<A>(*this, parser, *label, size, isAlias);
                        if ( isAlias )
                                this->_hasAliases = true;
+                       if ( parser.altEntryFromSymbol(*label) )
+                               this->_altEntries.insert(allocatedSpace);
                }
                else {
                        ld::Atom::SymbolTableInclusion inclusion = ld::Atom::symbolTableNotIn;
                        ld::Atom::ContentType ctype = this->contentType();
                        if ( ctype == ld::Atom::typeLSDA )
                                inclusion = ld::Atom::symbolTableInWithRandomAutoStripLabel;
-                       new (allocatedSpace) Atom<A>(*this, "anon", addr, size, ld::Atom::definitionRegular, ld::Atom::combineNever, 
+                       new (allocatedSpace) Atom<A>(*this, "anon", addr, size, ld::Atom::definitionRegular, ld::Atom::combineNever,
                                                                                ld::Atom::scopeTranslationUnit, ctype, inclusion, 
                                                                                this->dontDeadStrip(), false, false, this->alignmentForAddress(addr));
                }
@@ -4377,6 +5324,20 @@ uint32_t SymboledSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p,
 }
 
 
+template <>
+ld::Atom::SymbolTableInclusion ImplicitSizeSection<arm64>::symbolTableInclusion()
+{
+       return ld::Atom::symbolTableInWithRandomAutoStripLabel;
+}
+
+
+template <typename A>
+ld::Atom::SymbolTableInclusion ImplicitSizeSection<A>::symbolTableInclusion()
+{
+       return ld::Atom::symbolTableNotIn;
+}
+
+
 template <typename A>
 uint32_t ImplicitSizeSection<A>::computeAtomCount(class Parser<A>& parser, 
                                                                                                        struct Parser<A>::LabelAndCFIBreakIterator& it, 
@@ -4394,15 +5355,18 @@ uint32_t ImplicitSizeSection<A>::computeAtomCount(class Parser<A>& parser,
                // if there are multiple labels in this section for the same address, then clone them into multi atoms
                pint_t  prevSymbolAddr = (pint_t)(-1);
                uint8_t prevSymbolSectNum = 0;
+               bool prevIgnore = false;
                for(uint32_t i=0; i < it.sortedSymbolCount; ++i) {
                        const macho_nlist<P>& sym = parser.symbolFromIndex(it.sortedSymbolIndexes[i]);
                        const pint_t symbolAddr = sym.n_value();
-                       const pint_t symbolSectNum = sym.n_sect();
-                       if ( (symbolAddr == prevSymbolAddr) && (prevSymbolSectNum == symbolSectNum) && (symbolSectNum == this->sectionNum(parser)) ) { 
+                       const uint8_t symbolSectNum = sym.n_sect();
+                       const bool ignore = this->ignoreLabel(parser.nameFromSymbol(sym));
+                       if ( !ignore && !prevIgnore && (symbolAddr == prevSymbolAddr) && (prevSymbolSectNum == symbolSectNum) && (symbolSectNum == this->sectionNum(parser)) ) { 
                                ++count;
                        }
                        prevSymbolAddr = symbolAddr;
                        prevSymbolSectNum = symbolSectNum;
+                       prevIgnore = ignore;
                }
        }
        return count;
@@ -4425,33 +5389,44 @@ uint32_t ImplicitSizeSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p
        pint_t  size;
        const macho_nlist<P>* foundLabel;
        Atom<A>* allocatedSpace;
-       while ( it.next(parser, sectNum, startAddr, endAddr, &foundAddr, &size, &foundLabel) ) {
+       while ( it.next(parser, *this, sectNum, startAddr, endAddr, &foundAddr, &size, &foundLabel) ) {
                if ( foundLabel != NULL ) {
+                       bool skip = false;
                        pint_t labeledAtomSize = this->elementSizeAtAddress(foundAddr);
                        allocatedSpace = (Atom<A>*)p;
                        if ( this->ignoreLabel(parser.nameFromSymbol(*foundLabel)) ) {
-                               //fprintf(stderr, "  0x%08llX make annon\n", (uint64_t)foundAddr);
-                               new (allocatedSpace) Atom<A>(*this, this->unlabeledAtomName(parser, foundAddr), foundAddr, 
+                               if ( size == 0 ) {
+                                       // <rdar://problem/10018737> 
+                                       // a size of zero means there is another label at same location 
+                                       // and we are supposed to ignore this label
+                                       skip = true;
+                               }
+                               else {
+                                       //fprintf(stderr, "  0x%08llX make annon, size=%lld\n", (uint64_t)foundAddr, (uint64_t)size);
+                                       new (allocatedSpace) Atom<A>(*this, this->unlabeledAtomName(parser, foundAddr), foundAddr, 
                                                                                        this->elementSizeAtAddress(foundAddr), this->definition(), 
                                                                                        this->combine(parser, foundAddr), this->scopeAtAddress(parser, foundAddr), 
                                                                                        this->contentType(), this->symbolTableInclusion(), 
                                                                                        this->dontDeadStrip(), false, false, this->alignmentForAddress(foundAddr));
+                               }
                        }
                        else {
                                // make named atom for label
                                //fprintf(stderr, "  0x%08llX make labeled\n", (uint64_t)foundAddr);
                                new (allocatedSpace) Atom<A>(*this, parser, *foundLabel, labeledAtomSize);
                        }
-                       ++count;
-                       p += sizeof(Atom<A>);
-                       foundAddr += labeledAtomSize;
-                       size -= labeledAtomSize;
+                       if ( !skip ) {
+                               ++count;
+                               p += sizeof(Atom<A>);
+                               foundAddr += labeledAtomSize;
+                               size -= labeledAtomSize;
+                       }
                }
                // some number of anonymous atoms
                for (pint_t addr = foundAddr; addr < (foundAddr+size); addr += elementSizeAtAddress(addr) ) {
                        // make anon atoms for area before label
                        if ( this->useElementAt(parser, it, addr) ) {
-                               //fprintf(stderr, "  0x%08llX make annon\n", (uint64_t)addr);
+                               //fprintf(stderr, "  0x%08llX make annon, size=%lld\n", (uint64_t)addr, (uint64_t)elementSizeAtAddress(addr));
                                allocatedSpace = (Atom<A>*)p;
                                new (allocatedSpace) Atom<A>(*this, this->unlabeledAtomName(parser, addr), addr, this->elementSizeAtAddress(addr), 
                                                                                        this->definition(), this->combine(parser, addr), this->scopeAtAddress(parser, addr), 
@@ -4468,6 +5443,11 @@ uint32_t ImplicitSizeSection<A>::appendAtoms(class Parser<A>& parser, uint8_t* p
        return count;
 }
 
+template <typename A>
+bool Literal4Section<A>::ignoreLabel(const char* label) const
+{
+       return (label[0] == 'L') || (label[0] == 'l');
+}
 
 template <typename A>
 unsigned long Literal4Section<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
@@ -4493,6 +5473,12 @@ bool Literal4Section<A>::canCoalesceWith(const class Atom<A>* atom, const ld::At
 }
 
 
+template <typename A>
+bool Literal8Section<A>::ignoreLabel(const char* label) const
+{
+       return (label[0] == 'L') || (label[0] == 'l');
+}
+
 template <typename A>
 unsigned long Literal8Section<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
 {
@@ -4527,6 +5513,11 @@ bool Literal8Section<A>::canCoalesceWith(const class Atom<A>* atom, const ld::At
        return false;
 }
 
+template <typename A>
+bool Literal16Section<A>::ignoreLabel(const char* label) const
+{
+       return (label[0] == 'L') || (label[0] == 'l');
+}
 
 template <typename A>
 unsigned long Literal16Section<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
@@ -4574,11 +5565,12 @@ bool CStringSection<A>::useElementAt(Parser<A>& parser, struct Parser<A>::LabelA
 }
 
 template <typename A>
-bool CStringSection<A>::ignoreLabel(const char* label)  
+bool CStringSection<A>::ignoreLabel(const char* label) const
 { 
        return (label[0] == 'L') || (label[0] == 'l'); 
 }
 
+
 template <typename A>
 Atom<A>* CStringSection<A>::findAtomByAddress(pint_t addr)
 {
@@ -4633,6 +5625,12 @@ ld::Fixup::Kind NonLazyPointerSection<arm>::fixupKind()
        return ld::Fixup::kindStoreLittleEndian32;
 }
 
+template <>
+ld::Fixup::Kind NonLazyPointerSection<arm64>::fixupKind()
+{
+       return ld::Fixup::kindStoreLittleEndian64;
+}
+
 
 template <>
 void NonLazyPointerSection<x86_64>::makeFixups(class Parser<x86_64>& parser, const struct Parser<x86_64>::CFI_CU_InfoArrays&)
@@ -4766,6 +5764,113 @@ ld::Atom::Scope NonLazyPointerSection<A>::scopeAtAddress(Parser<A>& parser, pint
 }
 
 
+
+template <typename A>
+ld::Atom::Combine TLVPointerSection<A>::combine(Parser<A>& parser, pint_t addr)
+{
+       return ld::Atom::combineByNameAndReferences;
+}
+
+template <>
+void TLVPointerSection<arm>::makeFixups(class Parser<arm>& parser, const struct Parser<arm>::CFI_CU_InfoArrays&)
+{
+       // add references for each thread local pointer atom based on indirect symbol table
+       const macho_section<P>* sect = this->machoSection();
+       const pint_t endAddr = sect->addr() + sect->size();
+       for (pint_t addr = sect->addr(); addr < endAddr; addr += sizeof(pint_t)) {
+               typename Parser<arm>::SourceLocation    src;
+               typename Parser<arm>::TargetDesc                target;
+               src.atom = this->findAtomByAddress(addr);
+               src.offsetInAtom = 0;
+               uint32_t symIndex = parser.symbolIndexFromIndirectSectionAddress(addr, sect);
+               target.atom = NULL;
+               target.name = NULL;
+               target.weakImport = false;
+               target.addend = 0;
+               if ( symIndex == INDIRECT_SYMBOL_LOCAL ) {
+                       throwf("unexpected INDIRECT_SYMBOL_LOCAL in section %s", this->sectionName());
+               }
+               else {
+                       const macho_nlist<P>& sym = parser.symbolFromIndex(symIndex);
+                       // use direct reference for local symbols
+                       if ( ((sym.n_type() & N_TYPE) == N_SECT) && ((sym.n_type() & N_EXT) == 0) ) {
+                               throwf("unexpected pointer to local symbol in section %s", this->sectionName());
+                       }
+                       else {
+                               target.name = parser.nameFromSymbol(sym);
+                               target.weakImport = parser.weakImportFromSymbol(sym);
+                               assert(src.atom->combine() == ld::Atom::combineByNameAndReferences);
+                       }
+               }
+               parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
+       }
+}
+
+template <typename A>
+void TLVPointerSection<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&)
+{
+       assert(0 && "should not have thread-local-pointer sections in .o files");
+}
+
+
+template <typename A>
+const char* TLVPointerSection<A>::targetName(const class Atom<A>* atom, const ld::IndirectBindingTable& ind, bool* isStatic)
+{
+       assert(atom->combine() == ld::Atom::combineByNameAndReferences);
+       assert(atom->fixupCount() == 1);
+       *isStatic = false;
+       ld::Fixup::iterator fit = atom->fixupsBegin();
+       const char* name = NULL;
+       switch ( fit->binding ) {
+               case ld::Fixup::bindingByNameUnbound:
+                       name = fit->u.name;
+                       break;
+               case ld::Fixup::bindingByContentBound:
+                       name = fit->u.target->name();
+                       break;
+               case ld::Fixup::bindingsIndirectlyBound:
+                       name = ind.indirectName(fit->u.bindingIndex);
+                       break;
+               case ld::Fixup::bindingDirectlyBound:
+                       name = fit->u.target->name();
+                       *isStatic = (fit->u.target->scope() == ld::Atom::scopeTranslationUnit);
+                       break;
+               default:
+                       assert(0);
+       }
+       assert(name != NULL);
+       return name;
+}
+
+template <typename A>
+unsigned long TLVPointerSection<A>::contentHash(const class Atom<A>* atom, const ld::IndirectBindingTable& ind) const
+{
+       assert(atom->combine() == ld::Atom::combineByNameAndReferences);
+       unsigned long hash = 9508;
+       bool isStatic;
+       for (const char* s = this->targetName(atom, ind, &isStatic); *s != '\0'; ++s) {
+               hash = hash * 33 + *s;
+       }
+       return hash;
+}
+
+template <typename A>
+bool TLVPointerSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::Atom& rhs,
+                                                                                                       const ld::IndirectBindingTable& indirectBindingTable) const
+{
+       if ( rhs.section().type() != ld::Section::typeTLVPointers )
+               return false;
+       assert(this->type() == rhs.section().type());
+       const Atom<A>* rhsAtom = dynamic_cast<const Atom<A>*>(&rhs);
+       assert(rhsAtom !=  NULL);
+       bool thisIsStatic;
+       bool rhsIsStatic;
+       const char* thisName = this->targetName(atom, indirectBindingTable, &thisIsStatic);
+       const char* rhsName = this->targetName(rhsAtom, indirectBindingTable, &rhsIsStatic);
+       return !thisIsStatic && !rhsIsStatic && (strcmp(thisName, rhsName) == 0);
+}
+
+
 template <typename A>
 const uint8_t* CFStringSection<A>::targetContent(const class Atom<A>* atom, const ld::IndirectBindingTable& ind,
                                                                                                        ContentType* ct, unsigned int* count)
@@ -4800,7 +5905,11 @@ const uint8_t* CFStringSection<A>::targetContent(const class Atom<A>* atom, cons
                        *ct = contentUTF16;
                        *count = (targetAtom->size()+1)/2; // round up incase of buggy compiler that has only one trailing zero byte
                }
-               assert(target !=  NULL);
+               else {
+                       *ct = contentUnknown;
+                       *count = 0;
+                       return NULL;
+               }
                return target->contentPointer();
        }
        assert(0);
@@ -4830,7 +5939,8 @@ unsigned long CFStringSection<A>::contentHash(const class Atom<A>* atom, const l
                        }
                        return hash;
                case contentUnknown:
-                       return 0;
+                       // <rdar://problem/14134211> For malformed CFStrings, hash to address of atom so they have unique hashes
+                       return ULONG_MAX - (unsigned long)(atom);
        }
        return 0;
 }
@@ -4859,6 +5969,12 @@ bool CFStringSection<A>::canCoalesceWith(const class Atom<A>* atom, const ld::At
        if ( thisType != rhsType )
                return false;
 
+       if ( thisType == contentUnknown )
+               return false;
+
+       if ( rhsType == contentUnknown )
+               return false;
+
        // no need to compare content of pointers are already the same
        if ( cstringContent == rhsStringContent ) 
                return true;
@@ -5024,12 +6140,16 @@ const char* PointerToCStringSection<A>::targetCString(const class Atom<A>* atom,
                case ld::Fixup::bindingsIndirectlyBound:
                        targetAtom = ind.indirectAtom(fit->u.bindingIndex);
                        break;
+               case ld::Fixup::bindingDirectlyBound:
+                       targetAtom = fit->u.target;
+                       break;
                default:
-                       assert(0);
+                       assert(0 && "unsupported reference to selector");
        }
        assert(targetAtom != NULL);
        const Atom<A>* target = dynamic_cast<const Atom<A>*>(targetAtom);
-       assert(target !=  NULL);
+       assert(target != NULL);
+       assert(target->contentType() == ld::Atom::typeCString);
        return (char*)target->contentPointer();
 }
 
@@ -5291,10 +6411,14 @@ bool Section<x86_64>::addRelocFixup(class Parser<x86_64>& parser, const macho_re
                        }
                        else {
                                parser.findTargetFromAddressAndSectionNum(contentValue, nextReloc->r_symbolnum(), toTarget);
-                               useDirectBinding = (toTarget.atom->scope() == ld::Atom::scopeTranslationUnit);
+                               useDirectBinding = (toTarget.atom->scope() == ld::Atom::scopeTranslationUnit) || ((toTarget.atom->combine() == ld::Atom::combineByNameAndContent) || (toTarget.atom->combine() == ld::Atom::combineByNameAndReferences));
+                       }
+                       if ( useDirectBinding ) {
+                               if ( (toTarget.atom->combine() == ld::Atom::combineByNameAndContent) || (toTarget.atom->combine() == ld::Atom::combineByNameAndReferences) )
+                                       parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, ld::Fixup::bindingByContentBound, toTarget.atom);
+                               else
+                                       parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.atom);
                        }
-                       if ( useDirectBinding )
-                               parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.atom);
                        else
                                parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.weakImport, toTarget.name);
                        parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindAddAddend, toTarget.addend);
@@ -5614,7 +6738,12 @@ bool Section<arm>::addRelocFixup(class Parser<arm>& parser, const macho_relocati
                                if ((instruction & 0xFE000000) == 0xFA000000)
                                        displacement += ((instruction & 0x01000000) >> 23);
                                if ( reloc->r_extern() ) {
-                                       target.addend = srcAddr + displacement;
+                                       dstAddr = srcAddr + displacement;
+                                       // <rdar://problem/16652542> support large .o files
+                                       if ( srcAddr > 0x2000000 ) {
+                                               dstAddr -= ((srcAddr + 0x1FFFFFF) & 0xFC000000);
+                                       }
+                                       target.addend = dstAddr;
                                        if ( externSymbolIsThumbDef )
                                                target.addend &= -2; // remove thumb bit
                                }
@@ -5657,11 +6786,15 @@ bool Section<arm>::addRelocFixup(class Parser<arm>& parser, const macho_relocati
                                displacement += 4;
                                // If the instruction was blx, force the low 2 bits to be clear
                                dstAddr = srcAddr + displacement;
-                               if ((instruction & 0xF8000000) == 0xE8000000)
+                               if ((instruction & 0xD0000000) == 0xC0000000)
                                        dstAddr &= 0xFFFFFFFC;
 
                                if ( reloc->r_extern() ) {
-                                       target.addend = dstAddr;
+                                       // <rdar://problem/16652542> support large .o files
+                                       if ( srcAddr > 0x1000000 ) {
+                                               dstAddr -= ((srcAddr + 0xFFFFFF) & 0xFE000000);
+                                       }
+                                       target.addend = (int64_t)(int32_t)dstAddr;
                                }
                                else {
                                        parser.findTargetFromAddressAndSectionNum(dstAddr, reloc->r_symbolnum(), target);
@@ -5805,6 +6938,8 @@ bool Section<arm>::addRelocFixup(class Parser<arm>& parser, const macho_relocati
                                if ( sreloc->r_length() != 2 )
                                        throw "bad length for ARM_RELOC_VANILLA";
                                target.atom = parser.findAtomByAddress(sreloc->r_value());
+                               if ( target.atom == NULL )
+                                       throwf("bad r_value (0x%08X) for ARM_RELOC_VANILLA\n", sreloc->r_value());
                                contentValue = LittleEndian::get32(*fixUpPtr);
                                target.addend = contentValue - target.atom->_objAddress;
                                if ( target.atom->isThumb() )
@@ -6020,7 +7155,315 @@ bool Section<arm>::addRelocFixup(class Parser<arm>& parser, const macho_relocati
 #endif
 
 
-
+#if SUPPORT_ARCH_arm64
+template <>
+bool Section<arm64>::addRelocFixup(class Parser<arm64>& parser, const macho_relocation_info<P>* reloc)
+{
+       bool result = false;
+       Parser<arm64>::SourceLocation   src;
+       Parser<arm64>::TargetDesc               target = { NULL, NULL, false, 0 };
+       Parser<arm64>::TargetDesc               toTarget;
+       int32_t prefixRelocAddend = 0;
+       if ( reloc->r_type() == ARM64_RELOC_ADDEND ) {
+               uint32_t rawAddend = reloc->r_symbolnum(); 
+               prefixRelocAddend = rawAddend;
+               if ( rawAddend & 0x00800000 )
+                       prefixRelocAddend |= 0xFF000000; // sign extend 24-bit signed int to 32-bits
+               uint32_t addendAddress = reloc->r_address();
+               ++reloc;  //advance to next reloc record
+               result = true;
+               if ( reloc->r_address() != addendAddress )
+                       throw "ARM64_RELOC_ADDEND r_address does not match next reloc's r_address";
+       }
+       const macho_section<P>* sect = this->machoSection();
+       uint64_t srcAddr = sect->addr() + reloc->r_address();
+       src.atom = this->findAtomByAddress(srcAddr);
+       src.offsetInAtom = srcAddr - src.atom->_objAddress;
+       const uint8_t* fixUpPtr = file().fileContent() + sect->offset() + reloc->r_address();
+       uint64_t contentValue = 0;
+       const macho_relocation_info<arm64::P>* nextReloc = &reloc[1];
+       bool useDirectBinding;
+       uint32_t instruction;
+       uint32_t encodedAddend;
+       switch ( reloc->r_length() ) {
+               case 0:
+                       contentValue = *fixUpPtr;
+                       break;
+               case 1:
+                       contentValue = (int64_t)(int16_t)E::get16(*((uint16_t*)fixUpPtr));
+                       break;
+               case 2:
+                       contentValue = (int64_t)(int32_t)E::get32(*((uint32_t*)fixUpPtr));
+                       break;
+               case 3:
+                       contentValue = E::get64(*((uint64_t*)fixUpPtr));
+                       break;
+       }
+       if ( reloc->r_extern() ) {
+               const macho_nlist<P>& sym = parser.symbolFromIndex(reloc->r_symbolnum());
+               const char* symbolName = parser.nameFromSymbol(sym);
+               if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (symbolName[0] == 'L') || (symbolName[0] == 'l')) ) {
+                       // use direct reference for local symbols
+                       parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
+                       //target.addend += contentValue;
+               }
+               else if ( ((sym.n_type() & N_TYPE) == N_SECT) && (src.atom->_objAddress <= sym.n_value()) && (sym.n_value() < (src.atom->_objAddress+src.atom->size())) ) {
+                       // <rdar://problem/13700961> spurious warning when weak function has reference to itself
+                       // use direct reference when atom targets itself
+                       target.atom = src.atom;
+                       target.name = NULL;
+               }
+               else {
+                       target.name = symbolName;
+                       target.weakImport = parser.weakImportFromSymbol(sym);
+                       //target.addend = contentValue;
+               }
+               // cfstrings should always use direct reference to backing store
+               if ( (this->type() == ld::Section::typeCFString) && (src.offsetInAtom != 0) ) {
+                       parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), target);
+                       //target.addend = contentValue;
+               }
+       }
+       else {
+               if ( reloc->r_pcrel()  )
+                       contentValue += srcAddr;
+               parser.findTargetFromAddressAndSectionNum(contentValue, reloc->r_symbolnum(), target);
+       }
+       switch ( reloc->r_type() ) {
+               case ARM64_RELOC_UNSIGNED:
+                       if ( reloc->r_pcrel() )
+                               throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
+                       if ( reloc->r_extern() )
+                               target.addend = contentValue;
+                       switch ( reloc->r_length() ) {
+                               case 0:
+                               case 1:
+                                       throw "length < 2 and ARM64_RELOC_UNSIGNED not supported";
+                               case 2:
+                                       parser.addFixups(src, ld::Fixup::kindStoreLittleEndian32, target);
+                                       break;
+                               case 3:
+                                       parser.addFixups(src, ld::Fixup::kindStoreLittleEndian64, target);
+                                       break;
+                       }
+                       break;
+               case ARM64_RELOC_BRANCH26:
+                       if ( ! reloc->r_pcrel() )
+                               throw "not pcrel and ARM64_RELOC_BRANCH26 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_BRANCH26 not supported";
+                       if ( reloc->r_length() != 2 )
+                               throw "r_length != 2 and ARM64_RELOC_BRANCH26 not supported";
+                       if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_probe$", 16) == 0) ) {
+                               parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreARM64DtraceCallSiteNop, false, target.name);
+                               parser.addDtraceExtraInfos(src, &target.name[16]);
+                       }
+                       else if ( (target.name != NULL) && (strncmp(target.name, "___dtrace_isenabled$", 20) == 0) ) {
+                               parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear, false, target.name);
+                               parser.addDtraceExtraInfos(src, &target.name[20]);
+                       }
+                       else {
+                               target.addend = prefixRelocAddend;
+                               instruction = contentValue;
+                               encodedAddend = (instruction & 0x03FFFFFF) << 2;
+                               if ( encodedAddend != 0 ) {
+                                       if ( prefixRelocAddend == 0 ) {
+                                               warning("branch26 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc->r_address());
+                                               target.addend = encodedAddend;
+                                       }
+                                       else {
+                                               throwf("branch26 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc->r_address());
+                                       }
+                               }
+                               parser.addFixups(src, ld::Fixup::kindStoreARM64Branch26, target);
+                       }
+                       break;
+               case ARM64_RELOC_PAGE21:
+                       if ( ! reloc->r_pcrel() )
+                               throw "not pcrel and ARM64_RELOC_PAGE21 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_PAGE21 not supported";
+                       if ( reloc->r_length() != 2 ) 
+                               throw "length != 2 and ARM64_RELOC_PAGE21 not supported";
+                       target.addend = prefixRelocAddend;
+                       instruction = contentValue;
+                       encodedAddend  = ((instruction & 0x60000000) >> 29) | ((instruction & 0x01FFFFE0) >> 3);
+                       encodedAddend *= 4096; // internally addend is in bytes, so scale
+                       if ( encodedAddend != 0 ) {
+                               if ( prefixRelocAddend == 0 ) {
+                                       warning("adrp instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc->r_address());
+                                       target.addend = encodedAddend;
+                               }
+                               else {
+                                       throwf("adrp instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc->r_address());
+                               }
+                       }
+                       parser.addFixups(src, ld::Fixup::kindStoreARM64Page21, target);
+                       break;
+               case ARM64_RELOC_PAGEOFF12:
+                       if ( reloc->r_pcrel() )
+                               throw "pcrel and ARM64_RELOC_PAGEOFF12 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_PAGEOFF12 not supported";
+                       if ( reloc->r_length() != 2 ) 
+                               throw "length != 2 and ARM64_RELOC_PAGEOFF12 not supported";
+                       target.addend = prefixRelocAddend;
+                       instruction = contentValue;
+                       encodedAddend = ((instruction & 0x003FFC00) >> 10);
+            // internally addend is in bytes.  Some instructions have an implicit scale factor
+            if ( (instruction & 0x3B000000) == 0x39000000 ) {
+                switch ( instruction & 0xC0000000 ) {
+                    case 0x00000000:
+                        break;
+                    case 0x40000000:
+                        encodedAddend *= 2;
+                        break;
+                    case 0x80000000:
+                        encodedAddend *= 4;
+                        break;
+                    case 0xC0000000:
+                        encodedAddend *= 8;
+                        break;
+                               }
+            }
+                       if ( encodedAddend != 0 ) {
+                               if ( prefixRelocAddend == 0 ) {
+                                       warning("pageoff12 instruction at 0x%08X has embedded addend. ARM64_RELOC_ADDEND should be used instead", reloc->r_address());
+                                       target.addend = encodedAddend;
+                               }
+                               else {  
+                                       throwf("pageoff12 instruction at 0x%08X has embedded addend and ARM64_RELOC_ADDEND also used", reloc->r_address());
+                               }
+                       }
+                       parser.addFixups(src, ld::Fixup::kindStoreARM64PageOff12, target);
+                       break;
+               case ARM64_RELOC_GOT_LOAD_PAGE21:
+                       if ( ! reloc->r_pcrel() )
+                               throw "not pcrel and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
+                       if ( reloc->r_length() != 2 ) 
+                               throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
+                       if ( prefixRelocAddend != 0 )
+                               throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGE21 not supported";
+                       instruction = contentValue;
+                       target.addend = ((instruction & 0x60000000) >> 29) | ((instruction & 0x01FFFFE0) >> 3);
+            if ( target.addend != 0 )
+                throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
+                       parser.addFixups(src, ld::Fixup::kindStoreARM64GOTLoadPage21, target);
+                       break;
+               case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
+                       if ( reloc->r_pcrel() )
+                               throw "pcrel and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
+                       if ( reloc->r_length() != 2 ) 
+                               throw "length != 2 and ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
+                       if ( prefixRelocAddend != 0 )
+                               throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_GOT_LOAD_PAGEOFF12 not supported";
+                       instruction = contentValue;
+                       target.addend = ((instruction & 0x003FFC00) >> 10);
+                       parser.addFixups(src, ld::Fixup::kindStoreARM64GOTLoadPageOff12, target);
+                       break;
+               case ARM64_RELOC_TLVP_LOAD_PAGE21:
+                       if ( ! reloc->r_pcrel() )
+                               throw "not pcrel and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
+                       if ( reloc->r_length() != 2 ) 
+                               throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
+                       if ( prefixRelocAddend != 0 )
+                               throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGE21 not supported";
+                       instruction = contentValue;
+                       target.addend = ((instruction & 0x60000000) >> 29) | ((instruction & 0x01FFFFE0) >> 3);
+            if ( target.addend != 0 )
+                throw "non-zero addend with ARM64_RELOC_GOT_LOAD_PAGE21 is not supported";
+                       parser.addFixups(src, ld::Fixup::kindStoreARM64TLVPLoadPage21, target);
+                       break;
+               case ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
+                       if ( reloc->r_pcrel() )
+                               throw "pcrel and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
+                       if ( reloc->r_length() != 2 ) 
+                               throw "length != 2 and ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
+                       if ( prefixRelocAddend != 0 )
+                               throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_TLVP_LOAD_PAGEOFF12 not supported";
+                       instruction = contentValue;
+                       target.addend = ((instruction & 0x003FFC00) >> 10);
+                       parser.addFixups(src, ld::Fixup::kindStoreARM64TLVPLoadPageOff12, target);
+                       break;
+               case ARM64_RELOC_SUBTRACTOR:
+                       if ( reloc->r_pcrel() )
+                               throw "ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
+                       if ( reloc->r_length() < 2 )
+                               throw "ARM64_RELOC_SUBTRACTOR must have r_length of 2 or 3";
+                       if ( !reloc->r_extern() )
+                               throw "ARM64_RELOC_SUBTRACTOR must have r_extern=1";
+                       if ( nextReloc->r_type() != ARM64_RELOC_UNSIGNED )
+                               throw "ARM64_RELOC_SUBTRACTOR must be followed by ARM64_RELOC_UNSIGNED";
+                       if ( prefixRelocAddend != 0 )
+                               throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_SUBTRACTOR not supported";
+                       result = true;
+                       if ( nextReloc->r_pcrel() )
+                               throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR cannot be pc-relative";
+                       if ( nextReloc->r_length() != reloc->r_length() )
+                               throw "ARM64_RELOC_UNSIGNED following a ARM64_RELOC_SUBTRACTOR must have same r_length";
+                       if ( nextReloc->r_extern() ) {
+                               const macho_nlist<P>& sym = parser.symbolFromIndex(nextReloc->r_symbolnum());
+                               // use direct reference for local symbols
+                               if ( ((sym.n_type() & N_TYPE) == N_SECT) && (((sym.n_type() & N_EXT) == 0) || (parser.nameFromSymbol(sym)[0] == 'L')) ) {
+                                       parser.findTargetFromAddressAndSectionNum(sym.n_value(), sym.n_sect(), toTarget);
+                                       toTarget.addend = contentValue;
+                                       useDirectBinding = true;
+                               }
+                               else {
+                                       toTarget.name = parser.nameFromSymbol(sym);
+                                       toTarget.weakImport = parser.weakImportFromSymbol(sym);
+                                       toTarget.addend = contentValue;
+                                       useDirectBinding = false;
+                               }
+                       }
+                       else {
+                               parser.findTargetFromAddressAndSectionNum(contentValue, nextReloc->r_symbolnum(), toTarget);
+                               useDirectBinding = (toTarget.atom->scope() == ld::Atom::scopeTranslationUnit);
+                       }
+                       if ( useDirectBinding )
+                               parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.atom);
+                       else
+                               parser.addFixup(src, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, toTarget.weakImport, toTarget.name);
+                       parser.addFixup(src, ld::Fixup::k2of4, ld::Fixup::kindAddAddend, toTarget.addend);
+                       if ( target.atom == NULL )
+                               parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, false, target.name);
+                       else
+                               parser.addFixup(src, ld::Fixup::k3of4, ld::Fixup::kindSubtractTargetAddress, target.atom);
+                       if ( reloc->r_length() == 2 )
+                               parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32);
+                       else
+                               parser.addFixup(src, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian64);
+                       break;
+        case ARM64_RELOC_POINTER_TO_GOT:
+                       if ( ! reloc->r_extern() ) 
+                               throw "r_extern == 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
+                       if ( prefixRelocAddend != 0 )
+                               throw "ARM64_RELOC_ADDEND followed by ARM64_RELOC_POINTER_TO_GOT not supported";
+                       if ( reloc->r_pcrel() ) {
+                if ( reloc->r_length() != 2 ) 
+                    throw "r_length != 2 and r_extern = 1 and ARM64_RELOC_POINTER_TO_GOT not supported";
+                parser.addFixups(src, ld::Fixup::kindStoreARM64PCRelToGOT, target);
+            }
+            else {
+                if ( reloc->r_length() != 3 ) 
+                    throw "r_length != 3 and r_extern = 0 and ARM64_RELOC_POINTER_TO_GOT not supported";
+                parser.addFixups(src, ld::Fixup::kindStoreARM64PointerToGOT, target);
+            }
+            break;
+               default:
+                       throwf("unknown relocation type %d", reloc->r_type());
+       }
+       return result;
+}
+#endif
 
 
 template <typename A>
@@ -6112,11 +7555,80 @@ bool Objc1ClassReferences<x86>::addRelocFixup(class Parser<x86>& parser, const m
        return PointerToCStringSection<x86>::addRelocFixup(parser, reloc);
 }
 
+#if SUPPORT_ARCH_arm64
+template <>
+void Section<arm64>::addLOH(class Parser<arm64>& parser, int kind, int count, const uint64_t addrs[]) {
+       switch (kind) {
+               case LOH_ARM64_ADRP_ADRP:
+               case LOH_ARM64_ADRP_LDR:
+               case LOH_ARM64_ADRP_ADD:
+               case LOH_ARM64_ADRP_LDR_GOT:
+                       if ( count != 2 )
+                               warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind);
+                       break;
+               case LOH_ARM64_ADRP_ADD_LDR:
+               case LOH_ARM64_ADRP_LDR_GOT_LDR:
+               case LOH_ARM64_ADRP_ADD_STR:
+               case LOH_ARM64_ADRP_LDR_GOT_STR:
+                       if ( count != 3 )
+                               warning("arm64 Linker Optimiztion Hint %d has wrong number of arguments", kind);
+       }
+       
+       // pick lowest address in tuple for use as offsetInAtom
+       uint64_t lowestAddress = addrs[0];
+       for(int i=1; i < count; ++i) {
+               if ( addrs[i] < lowestAddress )
+                       lowestAddress = addrs[i];
+       }
+       // verify all other address are in same atom
+       Atom<arm64>* inAtom = parser.findAtomByAddress(lowestAddress);
+       const uint64_t atomStartAddr = inAtom->objectAddress();
+       const uint64_t atomEndAddr = atomStartAddr + inAtom->size();
+       for(int i=0; i < count; ++i) {
+               if ( (addrs[i] < atomStartAddr) || (addrs[i] >= atomEndAddr) ) {
+                       warning("arm64 Linker Optimiztion Hint addresses are not in same atom: 0x%08llX and 0x%08llX",
+                               lowestAddress, addrs[i]);
+                       return; // skip this LOH
+               }
+               if ( (addrs[i] & 0x3) != 0 ) {
+                       warning("arm64 Linker Optimiztion Hint address is not 4-byte aligned: 0x%08llX", addrs[i]);
+                       return; // skip this LOH
+               }
+               if ( (addrs[i] - lowestAddress) > 0xFFFF ) {
+                       if ( parser.verboseOptimizationHints() ) {
+                               warning("arm64 Linker Optimiztion Hint addresses are too far apart: 0x%08llX and 0x%08llX",
+                                       lowestAddress, addrs[i]);
+                       }
+                       return; // skip this LOH
+               }
+       }
+       
+       // encoded kind, count, and address deltas in 64-bit addend 
+       ld::Fixup::LOH_arm64 extra;
+       extra.addend = 0;
+       extra.info.kind = kind;
+       extra.info.count = count-1;
+       extra.info.delta1 = (addrs[0] - lowestAddress) >> 2;
+       extra.info.delta2 = (count > 1) ? ((addrs[1] - lowestAddress) >> 2) : 0;
+       extra.info.delta3 = (count > 2) ? ((addrs[2] - lowestAddress) >> 2) : 0;
+       extra.info.delta4 = (count > 3) ? ((addrs[3] - lowestAddress) >> 2) : 0;
+       typename Parser<arm64>::SourceLocation src(inAtom, lowestAddress- inAtom->objectAddress());
+       parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindLinkerOptimizationHint, extra.addend);
+}
+#endif
+
+
+template <typename A>
+void Section<A>::addLOH(class Parser<A>& parser, int kind, int count, const uint64_t addrs[]) {
+
+}
 
 template <typename A>
 void Section<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI_CU_InfoArrays&)
 {
        const macho_section<P>* sect = this->machoSection();
+       if ( sect->reloff() + (sect->nreloc() * sizeof(macho_relocation_info<P>)) > parser.fileLength() )
+               throwf("relocations for section %s/%s extends beyond end of file,", sect->segname(), Section<A>::makeSectionName(sect) );
        const macho_relocation_info<P>* relocs = (macho_relocation_info<P>*)(file().fileContent() + sect->reloff());
        const uint32_t relocCount = sect->nreloc();
        for (uint32_t r = 0; r < relocCount; ++r) {
@@ -6149,6 +7661,22 @@ void Section<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI
                        }
                }
        }
+       if ( !this->_altEntries.empty() && !this->addFollowOnFixups() ) {
+               if ( _altEntries.count(_beginAtoms) != 0 ) 
+                       warning("N_ALT_ENTRY bit set on first atom in section %s/%s", sect->segname(), Section<A>::makeSectionName(sect));
+
+               Atom<A>* end = &_endAtoms[-1];
+               for(Atom<A>* p = _beginAtoms; p < end; ++p) {
+                       Atom<A>* nextAtom = &p[1];
+                       // <rdar://problem/22960070> support alt_entry aliases (alias process already added followOn, don't repeat)
+                       if ( (_altEntries.count(nextAtom) != 0) && (p->_objAddress != nextAtom->_objAddress) ) {
+                               typename Parser<A>::SourceLocation src(p, 0);
+                               parser.addFixup(src, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, nextAtom);
+                               typename Parser<A>::SourceLocation src2(nextAtom, 0);
+                               parser.addFixup(src2, ld::Fixup::k1of1, ld::Fixup::kindNoneGroupSubordinate, p);
+                       }
+               }
+       }
        
        // <rdar://problem/9218847> track data-in-code
        if ( parser.hasDataInCodeLabels() && (this->type() == ld::Section::typeCode) ) {
@@ -6190,6 +7718,80 @@ void Section<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI
                }
        }
        
+       // <rdar://problem/11150575> Handle LC_DATA_IN_CODE in object files
+       if ( this->type() == ld::Section::typeCode ) {
+               const pint_t startAddr = this->_machOSection->addr();
+               const pint_t endAddr = startAddr + this->_machOSection->size();
+               for ( const macho_data_in_code_entry<P>* p = parser.dataInCodeStart(); p != parser.dataInCodeEnd(); ++p ) {
+                       if ( (p->offset() >= startAddr) && (p->offset() < endAddr) ) {
+                               ld::Fixup::Kind kind = ld::Fixup::kindNone;
+                               switch ( p->kind() ) {
+                                       case DICE_KIND_DATA:
+                                               kind = ld::Fixup::kindDataInCodeStartData;
+                                               break;
+                                       case DICE_KIND_JUMP_TABLE8:
+                                               kind = ld::Fixup::kindDataInCodeStartJT8;
+                                               break;
+                                       case DICE_KIND_JUMP_TABLE16:
+                                               kind = ld::Fixup::kindDataInCodeStartJT16;
+                                               break;
+                                       case DICE_KIND_JUMP_TABLE32:
+                                               kind = ld::Fixup::kindDataInCodeStartJT32;
+                                               break;
+                                       case DICE_KIND_ABS_JUMP_TABLE32:
+                                               kind = ld::Fixup::kindDataInCodeStartJTA32;
+                                               break;
+                                       default:
+                                               kind = ld::Fixup::kindDataInCodeStartData;
+                                               warning("uknown LC_DATA_IN_CODE kind (%d) at offset 0x%08X", p->kind(), p->offset());
+                                               break;
+                               }
+                               Atom<A>* inAtom = parser.findAtomByAddress(p->offset());
+                               typename Parser<A>::SourceLocation srcStart(inAtom, p->offset() - inAtom->objectAddress());
+                               parser.addFixup(srcStart, ld::Fixup::k1of1, kind);
+                               typename Parser<A>::SourceLocation srcEnd(inAtom, p->offset() + p->length() - inAtom->objectAddress());
+                               parser.addFixup(srcEnd, ld::Fixup::k1of1, ld::Fixup::kindDataInCodeEnd);
+                       }
+               }
+       }
+       
+       // <rdar://problem/11945700> convert linker optimization hints into internal format
+       if ( this->type() == ld::Section::typeCode && parser.hasOptimizationHints() ) {
+               const pint_t startAddr = this->_machOSection->addr();
+               const pint_t endAddr = startAddr + this->_machOSection->size();
+               for (const uint8_t* p = parser.optimizationHintsStart(); p < parser.optimizationHintsEnd(); ) {
+                       uint64_t addrs[4];
+                       int32_t kind = read_uleb128(&p, parser.optimizationHintsEnd());
+                       if ( kind == 0 ) // padding at end of loh buffer
+                               break;
+                       if ( kind == -1 ) {
+                               warning("malformed uleb128 kind in LC_LINKER_OPTIMIZATION_HINTS");
+                               break;
+                       }
+                       int32_t count = read_uleb128(&p, parser.optimizationHintsEnd());
+                       if ( count == -1 ) {
+                               warning("malformed uleb128 count in LC_LINKER_OPTIMIZATION_HINTS");
+                               break;
+                       }
+                       if ( count > 3 ) {
+                               warning("address count > 3 in LC_LINKER_OPTIMIZATION_HINTS");
+                               break;
+                       }
+                       for (int32_t i=0; i < count; ++i) {
+                               addrs[i] = read_uleb128(&p, parser.optimizationHintsEnd());
+                       }
+                       if ( (startAddr <= addrs[0]) && (addrs[0] < endAddr) ) {
+                               this->addLOH(parser, kind, count, addrs);
+                               //fprintf(stderr, "kind=%d", kind);
+                               //for (int32_t i=0; i < count; ++i) {
+                               //      fprintf(stderr, ", addr=0x%08llX", addrs[i]);
+                               //}
+                               //fprintf(stderr, "\n");
+                       }
+               }
+       }
+       
+       
        // add follow-on fixups for aliases
        if ( _hasAliases ) {
                for(Atom<A>* p = _beginAtoms; p < _endAtoms; ++p) {
@@ -6230,6 +7832,12 @@ ld::relocatable::File* parse(const uint8_t* fileContent, uint64_t fileLength,
                        if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) )
                                return mach_o::relocatable::Parser<arm>::parse(fileContent, fileLength, path, modTime, ordinal, opts);
                        break;
+#endif
+#if SUPPORT_ARCH_arm64
+               case CPU_TYPE_ARM64:
+                       if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) )
+                               return mach_o::relocatable::Parser<arm64>::parse(fileContent, fileLength, path, modTime, ordinal, opts);
+                       break;
 #endif
        }
        return NULL;
@@ -6247,6 +7855,8 @@ bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, const ParserO
                        return ( mach_o::relocatable::Parser<x86>::validFile(fileContent) );
                case CPU_TYPE_ARM:
                        return ( mach_o::relocatable::Parser<arm>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) );
+               case CPU_TYPE_ARM64:
+                       return ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, opts.objSubtypeMustMatch, opts.subType) );
        }
        return false;
 }
@@ -6254,22 +7864,34 @@ bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, const ParserO
 //
 // used by linker to infer architecture when no -arch is on command line
 //
-bool isObjectFile(const uint8_t* fileContent, cpu_type_t* result, cpu_subtype_t* subResult)
+bool isObjectFile(const uint8_t* fileContent, cpu_type_t* result, cpu_subtype_t* subResult, Options::Platform* platform)
 {
        if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) ) {
                *result = CPU_TYPE_X86_64;
-               *subResult = CPU_SUBTYPE_X86_64_ALL;
+               const macho_header<Pointer64<LittleEndian> >* header = (const macho_header<Pointer64<LittleEndian> >*)fileContent;
+               *subResult = header->cpusubtype();
+               *platform = Parser<x86_64>::findPlatform(header);
                return true;
        }
        if ( mach_o::relocatable::Parser<x86>::validFile(fileContent) ) {
+               const macho_header<Pointer32<LittleEndian> >* header = (const macho_header<Pointer32<LittleEndian> >*)fileContent;
                *result = CPU_TYPE_I386;
                *subResult = CPU_SUBTYPE_X86_ALL;
+               *platform = Parser<x86>::findPlatform(header);
                return true;
        }
        if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, false, 0) ) {
-               *result = CPU_TYPE_ARM;
                const macho_header<Pointer32<LittleEndian> >* header = (const macho_header<Pointer32<LittleEndian> >*)fileContent;
+               *result = CPU_TYPE_ARM;
                *subResult = header->cpusubtype();
+               *platform = Parser<arm>::findPlatform(header);
+               return true;
+       }
+       if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, false, 0) ) {
+               const macho_header<Pointer64<LittleEndian> >* header = (const macho_header<Pointer64<LittleEndian> >*)fileContent;
+               *result = CPU_TYPE_ARM64;
+               *subResult = CPU_SUBTYPE_ARM64_ALL;
+               *platform = Parser<arm64>::findPlatform(header);
                return true;
        }
        return false;
@@ -6306,6 +7928,11 @@ bool hasObjC2Categories(const uint8_t* fileContent)
        else if ( mach_o::relocatable::Parser<x86>::validFile(fileContent, false, 0) ) {
                return mach_o::relocatable::Parser<x86>::hasObjC2Categories(fileContent);
        }
+#if SUPPORT_ARCH_arm64
+    else if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, false, 0) ) {
+        return mach_o::relocatable::Parser<arm64>::hasObjC2Categories(fileContent);
+    }
+#endif
        return false;
 }                              
 
@@ -6320,6 +7947,26 @@ bool hasObjC1Categories(const uint8_t* fileContent)
        return false;
 }
 
+//
+// Used by bitcode obfuscator to get a list of non local symbols from object file
+//
+bool getNonLocalSymbols(const uint8_t* fileContent, std::vector<const char*> &syms)
+{
+       if ( mach_o::relocatable::Parser<x86_64>::validFile(fileContent) ) {
+               return mach_o::relocatable::Parser<x86_64>::getNonLocalSymbols(fileContent, syms);
+       }
+       else if ( mach_o::relocatable::Parser<arm>::validFile(fileContent, false, 0) ) {
+               return mach_o::relocatable::Parser<arm>::getNonLocalSymbols(fileContent, syms);
+       }
+       else if ( mach_o::relocatable::Parser<x86>::validFile(fileContent, false, 0) ) {
+               return mach_o::relocatable::Parser<x86>::getNonLocalSymbols(fileContent, syms);
+       }
+       else if ( mach_o::relocatable::Parser<arm64>::validFile(fileContent, false, 0) ) {
+               return mach_o::relocatable::Parser<arm64>::getNonLocalSymbols(fileContent, syms);
+       }
+       return false;
+}
+
 
 
 } // namespace relocatable