]> git.saurik.com Git - apple/ld64.git/blobdiff - src/ld/parsers/macho_relocatable_file.cpp
ld64-253.3.tar.gz
[apple/ld64.git] / src / ld / parsers / macho_relocatable_file.cpp
index c22af18c0a4a3d2f2b4628abdbf71ef59c22654c..c023f37c8c893cf552197cc39a6908713ab98146 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,21 +75,27 @@ 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),
                                                                                                _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),
+                                                                                               _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 uint32_t                                                                        cpuSubType() const                              { return _cpuSubType; }
@@ -96,6 +104,10 @@ public:
        virtual bool                                                                            canScatterAtoms() const                 { return _canScatterAtoms; }
        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; }
+       virtual void                                                                            setSourceKind(SourceKind src)   { _srcKind = src; }
        
        const uint8_t*                                                                          fileContent()                                   { return _fileContent; }
 private:
@@ -109,8 +121,10 @@ 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;
@@ -122,9 +136,14 @@ private:
        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;
        std::vector<std::vector<const char*> >  _linkerOptions;
+       std::unique_ptr<ld::Bitcode>                    _bitcode;
+       SourceKind                                                              _srcKind;
 };
 
 
@@ -143,6 +162,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, 
@@ -170,6 +190,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);
@@ -181,6 +202,7 @@ protected:
        class Atom<A>*                                  _beginAtoms;
        class Atom<A>*                                  _endAtoms;
        bool                                                    _hasAliases;
+       std::set<const class Atom<A>*>  _altEntries;
 };
 
 
@@ -190,7 +212,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&);
@@ -428,6 +450,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>
@@ -447,6 +470,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>
@@ -466,6 +490,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;
 };
 
 
@@ -496,6 +521,30 @@ 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 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>
@@ -676,7 +725,7 @@ class Atom : public ld::Atom
 {
 public:
        // overrides of ld::Atom
-       virtual ld::File*                                                       file() const            { return &sect().file(); }
+       virtual const ld::File*                                         file() const;
        virtual const char*                                                     translationUnitSource() const
                                                                                                                                        { return sect().file().translationUnitSource(); }
        virtual const char*                                                     name() const            { return _name; }
@@ -694,6 +743,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:
 
@@ -719,7 +769,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;
@@ -741,7 +791,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()), 
@@ -753,7 +803,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:
@@ -773,10 +825,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)
@@ -843,7 +912,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) )
@@ -851,12 +920,56 @@ 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 
 {
@@ -864,14 +977,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.warnUnwindConversionProblems,
-                                                                                                                                               opts.keepDwarfUnwind, opts.forceDwarfConversion);
+                                                                                                                                               opts.keepDwarfUnwind, opts.forceDwarfConversion,
+                                                                                                                                               opts.neverConvertDwarf, opts.verboseOptimizationHints,
+                                                                                                                                               opts.ignoreMismatchPlatform);
                                                                                                                                return p.parse(opts);
                                                                                                                }
 
@@ -953,6 +1070,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);
@@ -981,9 +1099,16 @@ public:
        bool                                                                                    hasDataInCodeLabels() { return _hasDataInCodeLabels; }
        bool                                                                                    keepDwarfUnwind() { return _keepDwarfUnwind; }
        bool                                                                                    forceDwarfConversion() { return _forceDwarfConversion; }
-       
+       bool                                                                                    verboseOptimizationHints() { return _verboseOptimizationHints; }
+       bool                                                                                    neverConvertDwarf() { return _neverConvertDwarf; }
+       bool                                                                                    armUsesZeroCostExceptions() { return _armUsesZeroCostExceptions; }
+
        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);
        void                                                    addFixups(const SourceLocation& src, ld::Fixup::Kind kind, const TargetDesc& target, const TargetDesc& picBase);
@@ -1033,7 +1158,7 @@ private:
                                                sectionTypeUTF16Strings, sectionTypeCFString, sectionTypeObjC2ClassRefs, typeObjC2CategoryList,
                                                sectionTypeObjC1Classes, sectionTypeSymboled, sectionTypeObjC1ClassRefs,
                                                sectionTypeTentativeDefinitions, sectionTypeAbsoluteSymbols, sectionTypeTLVDefs,
-                                               sectionTypeCompactUnwind };
+                                               sectionTypeCompactUnwind, sectionTypeTLVPointers};
 
        template <typename P>
        struct MachOSectionAndSectionClass
@@ -1059,10 +1184,12 @@ private:
 
                                                                                                        Parser(const uint8_t* fileContent, uint64_t fileLength, 
                                                                                                                        const char* path, time_t modTime, ld::File::Ordinal ordinal, 
-                                                                                                                       bool warnUnwindConversionProblems, bool keepDwarfUnwind, bool forceDwarfConversion);
+                                                                                                                       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[]);
@@ -1073,10 +1200,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);
        
@@ -1092,6 +1222,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;
@@ -1103,6 +1234,8 @@ private:
        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;
@@ -1118,6 +1251,10 @@ private:
        bool                                                                            _hasDataInCodeLabels;
        bool                                                                            _keepDwarfUnwind;
        bool                                                                            _forceDwarfConversion;
+       bool                                                                            _neverConvertDwarf;
+       bool                                                                            _verboseOptimizationHints;
+       bool                                                                            _armUsesZeroCostExceptions;
+       bool                                                                            _ignoreMismatchPlatform;
        unsigned int                                                            _stubsSectionNum;
        const macho_section<P>*                                         _stubsMachOSection;
        std::vector<const char*>                                        _dtraceProviderInfo;
@@ -1128,19 +1265,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, bool keepDwarfUnwind, bool forceDwarfConversion)
+                                       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), _warnUnwindConversionProblems(convertDUI), _hasDataInCodeLabels(false),
                        _keepDwarfUnwind(keepDwarfUnwind), _forceDwarfConversion(forceDwarfConversion),
+                       _neverConvertDwarf(neverConvertDwarf),
+                       _verboseOptimizationHints(verboseOptimizationHints),
+                       _ignoreMismatchPlatform(ignoreMismatchPlatform),
                        _stubsSectionNum(0), _stubsMachOSection(NULL)
 {
 }
@@ -1223,7 +1365,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;
@@ -1251,7 +1393,7 @@ 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 )
+       if ( header->magic() != MH_MAGIC_64 )
                return NULL;
        if ( header->cputype() != CPU_TYPE_ARM64 )
                return NULL;
@@ -1316,6 +1458,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 i = 0; i < symbolCount; ++i) {
+                               // ignore stabs and count only ext symbols
+                               if ( (symbols[i].n_type() & N_STAB) == 0 &&
+                                        (symbols[i].n_type() & N_EXT) != 0 ) {
+                                       const char* symName = &strings[symbols[i].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)
 {
@@ -1509,6 +1684,18 @@ bool Parser<A>::LabelAndCFIBreakIterator::next(Parser<A>& parser, const Section<
        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; \
@@ -1527,12 +1714,17 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
        // create file object
        _file = new File<A>(_path, _modTime, _fileContent, _ordinal);
 
+       // set input source
+       _file->setSourceKind(opts.srcKind);
+
        // respond to -t option
        if ( opts.logAllFiles )
                printf("%s\n", _path);
+               
+       _armUsesZeroCostExceptions = opts.armUsesZeroCostExceptions;
 
        // parse start of mach-o file
-       if ( ! parseLoadCommands() )
+       if ( ! parseLoadCommands(opts.platform, opts.minOSVersion, opts.simulator, opts.ignoreMismatchPlatform) )
                return _file;
        
        // make array of
@@ -1573,7 +1765,7 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
        // stack allocate (if not too large) array of CFI_Atom_Info
        uint32_t countOfCFIs = 0;
        if ( _EHFrameSection != NULL )
-               countOfCFIs = _EHFrameSection->cfiCount();
+               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
@@ -1609,7 +1801,7 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
                        if ( cfiArray[i].isCIE )
                                continue;
                        if ( cfiArray[i].u.fdeInfo.function.targetAddress != CFI_INVALID_ADDRESS )
-                               cfiStartsArray[cfiStartsArrayCount++] = 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[cfiStartsArrayCount++] = cfiArray[i].u.fdeInfo.lsda.targetAddress;
                        ++countOfFDEs;
@@ -1688,7 +1880,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) {
@@ -1746,13 +1938,29 @@ ld::relocatable::File* Parser<A>::parse(const ParserOptions& opts)
                }
        }
        
+       // 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; }
@@ -1760,7 +1968,7 @@ 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;
 
@@ -1774,6 +1982,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;
@@ -1843,6 +2052,27 @@ bool Parser<A>::parseLoadCommands()
                                                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;
                        default:
                                if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
                                        if ( segment != NULL )
@@ -1855,6 +2085,52 @@ bool Parser<A>::parseLoadCommands()
                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:
+                                       // WatchOS errors on cross-linking all the time.
+                                       throwf("building for %s%s, but linking in object file built for %s,",
+                                                  Options::platformName(platform), (simulator ? " simulator" : ""),
+                                                  Options::platformName(lcPlatform));
+                                       break;
+       #if SUPPORT_APPLE_TV
+                               case Options::kPlatform_tvOS:
+                                       // tvOS is a warning temporarily. rdar://problem/21746965
+                                       if (platform == Options::kPlatform_tvOS)
+                                               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 ) 
@@ -1865,6 +2141,35 @@ bool Parser<A>::parseLoadCommands()
        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()
@@ -1895,7 +2200,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);
@@ -1932,6 +2241,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)
 {
@@ -2099,6 +2436,13 @@ 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];
                if ( (sect->flags() & S_ATTR_DEBUG) != 0 ) {
@@ -2126,6 +2470,23 @@ void Parser<A>::makeSections()
                                }
                        }
                }
+               if ( strcmp(sect->segname(), "__LLVM") == 0 ) {
+                       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 it is not single input for ld -r, don't count the section
+                       // otherwise, fall through and add it to the sections.
+                       if (_file->sourceKind() != ld::relocatable::File::kSourceSingle)
+                               continue;
+               }
                // ignore empty __OBJC sections
                if ( (sect->size() == 0) && (strcmp(sect->segname(), "__OBJC") == 0) )
                        continue;
@@ -2151,6 +2512,7 @@ void Parser<A>::makeSections()
                                        _file->_objConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
                                else
                                        _file->_objConstraint = ld::File::objcConstraintRetainRelease;
+                               _file->_swiftVersion = ((flags >> 8) & 0xFF);
                                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());
@@ -2188,6 +2550,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>);
@@ -2244,11 +2610,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);
@@ -2291,6 +2672,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;
@@ -2648,6 +3033,12 @@ void Parser<A>::addFixups(const SourceLocation& src, ld::Fixup::Kind setKind, co
                        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;
@@ -2950,6 +3341,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
@@ -3083,21 +3480,51 @@ 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];
+       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;
        }
-       warning("unknown dwarf string encoding (form=%lld) in %s\n", form, this->_path);
-       return NULL;
+       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;
 }
 
 
@@ -3279,7 +3706,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) {
@@ -3339,6 +3766,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;
@@ -3552,7 +3980,7 @@ bool Parser<A>::read_comp_unit(const char ** name, const char ** comp_dir,
                return false;
 
        vers = A::P::E::get16(*(uint16_t*)di);
-       if (vers < 2 || vers > 3)
+       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;
@@ -3615,20 +4043,23 @@ bool Parser<A>::read_comp_unit(const char ** name, const char ** comp_dir,
                        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;
+               switch (attr) {
+                       case DW_AT_name:
+                               *name = getDwarfString(form, di);
+                               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;
+               }
        }
 }
 
@@ -3647,8 +4078,6 @@ const char* File<A>::translationUnitSource() const
        return _dwarfTranslationUnitPath;
 }
 
-       
-
 template <typename A>
 bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
 {
@@ -3658,7 +4087,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>
@@ -3802,6 +4237,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;
        }
@@ -3845,7 +4282,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>
@@ -3858,10 +4298,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());
@@ -3959,7 +4409,8 @@ 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(), cfiArray, count, (void*)&parser, warnFunc);
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(), 
+                                                       cfiArray, count, (void*)&parser, warnFunc);
        if ( msg != NULL ) 
                throwf("malformed __eh_frame section: %s", msg);
 }
@@ -3976,7 +4427,8 @@ 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(), cfiArray, count, (void*)&parser, warnFunc);
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
+                                                       cfiArray, count, (void*)&parser, warnFunc);
        if ( msg != NULL ) 
                throwf("malformed __eh_frame section: %s", msg);
 }
@@ -3989,10 +4441,27 @@ 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, 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[], 
@@ -4052,7 +4521,7 @@ void CFISection<arm64>::cfiParse(class Parser<arm64>& parser, uint8_t* buffer,
        const char* msg;
        msg = libunwind::DwarfInstructions<OAS, libunwind::Registers_arm64>::parseCFIs(
                                                        oas, this->_machOSection->addr(), this->_machOSection->size(), 
-                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), 
+                                                       cuStarts, cuCount, parser.keepDwarfUnwind(), parser.forceDwarfConversion(), parser.neverConvertDwarf(),
                                                        cfiArray, count, (void*)&parser, warnFunc);
        if ( msg != NULL ) 
                throwf("malformed __eh_frame section: %s", msg);
@@ -4143,8 +4612,6 @@ 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)
@@ -4170,6 +4637,30 @@ void CFISection<arm64>::addCiePersonalityFixups(class Parser<arm64>& parser, con
 }
 #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)
 {
@@ -4415,8 +4906,7 @@ const char* CUSection<x86_64>::personalityName(class Parser<x86_64>& parser, con
        else {
                const pint_t* content = (pint_t*)(this->file().fileContent() + this->_machOSection->offset() + reloc->r_address());
                pint_t personalityAddr = *content;
-               Section<x86_64>* personalitySection = parser.sectionForAddress(personalityAddr);
-               assert((personalitySection->type() == ld::Section::typeCode) && "personality column in __compact_unwind section is not pointer to function");
+               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;
@@ -4463,6 +4953,7 @@ const char* CUSection<arm64>::personalityName(class Parser<arm64>& parser, const
                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);
@@ -4471,6 +4962,36 @@ const char* CUSection<arm64>::personalityName(class Parser<arm64>& parser, const
 }
 #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)
 {
@@ -4493,7 +5014,7 @@ bool CUSection<x86_64>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
 template <>
 bool CUSection<arm>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
 {
-       return false;
+       return ((enc & UNWIND_ARM_MODE_MASK) == UNWIND_ARM_MODE_DWARF);
 }
 #endif
 
@@ -4714,6 +5235,8 @@ 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;
@@ -4851,6 +5374,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
@@ -4876,6 +5404,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
 {
@@ -4910,6 +5444,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
@@ -5156,6 +5695,72 @@ 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 <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)
@@ -5190,7 +5795,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);
@@ -5220,7 +5829,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;
 }
@@ -5249,6 +5859,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;
@@ -6008,7 +6624,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
                                }
@@ -6055,7 +6676,11 @@ bool Section<arm>::addRelocFixup(class Parser<arm>& parser, const macho_relocati
                                        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);
@@ -6494,7 +7119,8 @@ bool Section<arm64>::addRelocFixup(class Parser<arm64>& parser, const macho_relo
                case ARM64_RELOC_UNSIGNED:
                        if ( reloc->r_pcrel() )
                                throw "pcrel and ARM64_RELOC_UNSIGNED not supported";
-                       target.addend = contentValue;
+                       if ( reloc->r_extern() )
+                               target.addend = contentValue;
                        switch ( reloc->r_length() ) {
                                case 0:
                                case 1:
@@ -6814,6 +7440,72 @@ 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&)
@@ -6851,6 +7543,21 @@ 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];
+                       if ( _altEntries.count(nextAtom) != 0 ) {
+                               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) ) {
@@ -6929,6 +7636,42 @@ void Section<A>::makeFixups(class Parser<A>& parser, const struct Parser<A>::CFI
                }
        }
        
+       // <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 ) {
@@ -7002,27 +7745,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;
@@ -7078,6 +7828,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