#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"
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; }
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:
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;
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;
};
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,
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);
class Atom<A>* _beginAtoms;
class Atom<A>* _endAtoms;
bool _hasAliases;
+ std::set<const class Atom<A>*> _altEntries;
};
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&);
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>
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>
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;
};
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>
{
public:
// overrides of ld::Atom
- virtual ld::File* file() const { return §().file(); }
+ virtual const ld::File* file() const;
virtual const char* translationUnitSource() const
{ return sect().file().translationUnitSource(); }
virtual const char* name() const { return _name; }
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:
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;
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()),
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:
_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 §().file();
+}
template <typename A>
void Atom<A>::setFixupsRange(uint32_t startIndex, uint32_t count)
}
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) )
}
}
+#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
{
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);
}
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);
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);
sectionTypeUTF16Strings, sectionTypeCFString, sectionTypeObjC2ClassRefs, typeObjC2CategoryList,
sectionTypeObjC1Classes, sectionTypeSymboled, sectionTypeObjC1ClassRefs,
sectionTypeTentativeDefinitions, sectionTypeAbsoluteSymbols, sectionTypeTLVDefs,
- sectionTypeCompactUnwind };
+ sectionTypeCompactUnwind, sectionTypeTLVPointers};
template <typename P>
struct MachOSectionAndSectionClass
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[]);
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);
File<A>* _file;
const macho_nlist<P>* _symbols;
uint32_t _symbolCount;
+ uint32_t _indirectSymbolCount;
const char* _strings;
uint32_t _stringsSize;
const uint32_t* _indirectTable;
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;
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;
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)
{
}
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;
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;
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)
{
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; \
// 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
// 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
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;
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) {
}
}
+ // process indirect symbols which become AliasAtoms
+ _file->_aliasAtomsArray = NULL;
+ _file->_aliasAtomsArrayCount = 0;
+ if ( _indirectSymbolCount != 0 ) {
+ _file->_aliasAtomsArrayCount = _indirectSymbolCount;
+ _file->_aliasAtomsArray = new uint8_t[_file->_aliasAtomsArrayCount*sizeof(AliasAtom)];
+ this->appendAliasAtoms(_file->_aliasAtomsArray);
+ }
+
+
// parse dwarf debug info to get line info
this->parseDebugInfo();
return _file;
}
-
+static void versionToString(uint32_t value, char buffer[32])
+{
+ if ( value & 0xFF )
+ sprintf(buffer, "%d.%d.%d", value >> 16, (value >> 8) & 0xFF, value & 0xFF);
+ else
+ sprintf(buffer, "%d.%d", value >> 16, (value >> 8) & 0xFF);
+}
template <> uint8_t Parser<x86>::loadCommandSizeMask() { return 0x03; }
template <> uint8_t Parser<x86_64>::loadCommandSizeMask() { return 0x07; }
template <> uint8_t Parser<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;
// <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;
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 )
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 )
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()
}
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);
}
}
+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)
{
// 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 ) {
}
}
}
+ 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;
_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());
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>);
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);
*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;
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;
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
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;
}
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) {
case N_LSYM:
case N_RSYM:
case N_PSYM:
+ case N_AST:
// not associated with an atom, just copy
stab.string = symString;
break;
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;
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;
+ }
}
}
return _dwarfTranslationUnitPath;
}
-
-
template <typename A>
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>
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;
}
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>
}
// 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());
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);
}
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);
}
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[],
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);
}
}
-
-
#if SUPPORT_ARCH_arm64
template <>
void CFISection<arm64>::addCiePersonalityFixups(class Parser<arm64>& parser, const CFI_Atom_Info* cieInfo)
}
#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)
{
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;
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);
}
#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)
{
template <>
bool CUSection<arm>::encodingMeansUseDwarf(compact_unwind_encoding_t enc)
{
- return false;
+ return ((enc & UNWIND_ARM_MODE_MASK) == UNWIND_ARM_MODE_DWARF);
}
#endif
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;
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
}
+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
{
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
}
+
+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)
*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);
}
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;
}
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;
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
}
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);
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:
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&)
}
}
}
+ 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) ) {
}
}
+ // <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 ) {
//
// 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;
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