2013-11-07 05:36:55 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp ------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-11-07 05:36:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
///
|
|
|
|
/// \file Converts from in-memory Atoms to in-memory normalized mach-o.
|
|
|
|
///
|
|
|
|
/// +------------+
|
|
|
|
/// | normalized |
|
|
|
|
/// +------------+
|
|
|
|
/// ^
|
|
|
|
/// |
|
|
|
|
/// |
|
2014-01-27 11:09:26 +08:00
|
|
|
/// +-------+
|
2013-11-07 05:36:55 +08:00
|
|
|
/// | Atoms |
|
2014-01-27 11:09:26 +08:00
|
|
|
/// +-------+
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
#include "ArchHandler.h"
|
2016-07-28 06:55:30 +08:00
|
|
|
#include "DebugInfo.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "MachONormalizedFile.h"
|
2014-06-10 09:50:00 +08:00
|
|
|
#include "MachONormalizedFileBinaryUtils.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "lld/Core/Error.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include <map>
|
2014-06-13 01:15:58 +08:00
|
|
|
#include <system_error>
|
2016-07-28 06:55:30 +08:00
|
|
|
#include <unordered_set>
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
using llvm::StringRef;
|
|
|
|
using llvm::isa;
|
|
|
|
using namespace llvm::MachO;
|
|
|
|
using namespace lld::mach_o::normalized;
|
|
|
|
using namespace lld;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct AtomInfo {
|
|
|
|
const DefinedAtom *atom;
|
|
|
|
uint64_t offsetInSection;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SectionInfo {
|
2014-07-30 08:58:06 +08:00
|
|
|
SectionInfo(StringRef seg, StringRef sect, SectionType type,
|
2016-01-08 05:07:26 +08:00
|
|
|
const MachOLinkingContext &ctxt, uint32_t attr,
|
|
|
|
bool relocsToDefinedCanBeImplicit);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
StringRef segmentName;
|
|
|
|
StringRef sectionName;
|
|
|
|
SectionType type;
|
|
|
|
uint32_t attributes;
|
|
|
|
uint64_t address;
|
|
|
|
uint64_t size;
|
2015-03-26 10:20:25 +08:00
|
|
|
uint16_t alignment;
|
2016-01-08 05:07:26 +08:00
|
|
|
|
|
|
|
/// If this is set, the any relocs in this section which point to defined
|
|
|
|
/// addresses can be implicitly generated. This is the case for the
|
|
|
|
/// __eh_frame section where references to the function can be implicit if the
|
|
|
|
/// function is defined.
|
|
|
|
bool relocsToDefinedCanBeImplicit;
|
|
|
|
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
std::vector<AtomInfo> atomsAndOffsets;
|
|
|
|
uint32_t normalizedSectionIndex;
|
|
|
|
uint32_t finalSectionIndex;
|
|
|
|
};
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
SectionInfo::SectionInfo(StringRef sg, StringRef sct, SectionType t,
|
2016-01-08 05:07:26 +08:00
|
|
|
const MachOLinkingContext &ctxt, uint32_t attrs,
|
|
|
|
bool relocsToDefinedCanBeImplicit)
|
2014-07-30 08:58:06 +08:00
|
|
|
: segmentName(sg), sectionName(sct), type(t), attributes(attrs),
|
2015-03-26 09:44:01 +08:00
|
|
|
address(0), size(0), alignment(1),
|
2016-01-08 05:07:26 +08:00
|
|
|
relocsToDefinedCanBeImplicit(relocsToDefinedCanBeImplicit),
|
2013-11-07 05:36:55 +08:00
|
|
|
normalizedSectionIndex(0), finalSectionIndex(0) {
|
2015-03-26 10:20:25 +08:00
|
|
|
uint16_t align = 1;
|
2014-07-30 08:58:06 +08:00
|
|
|
if (ctxt.sectionAligned(segmentName, sectionName, align)) {
|
|
|
|
alignment = align;
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SegmentInfo {
|
|
|
|
SegmentInfo(StringRef name);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
StringRef name;
|
|
|
|
uint64_t address;
|
|
|
|
uint64_t size;
|
2016-02-06 08:51:16 +08:00
|
|
|
uint32_t init_access;
|
|
|
|
uint32_t max_access;
|
2013-11-07 05:36:55 +08:00
|
|
|
std::vector<SectionInfo*> sections;
|
2014-07-30 08:58:06 +08:00
|
|
|
uint32_t normalizedSegmentIndex;
|
2013-11-07 05:36:55 +08:00
|
|
|
};
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
SegmentInfo::SegmentInfo(StringRef n)
|
2016-02-06 08:51:16 +08:00
|
|
|
: name(n), address(0), size(0), init_access(0), max_access(0),
|
|
|
|
normalizedSegmentIndex(0) {
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Util {
|
|
|
|
public:
|
2014-09-10 18:39:57 +08:00
|
|
|
Util(const MachOLinkingContext &ctxt)
|
2015-06-24 04:35:31 +08:00
|
|
|
: _ctx(ctxt), _archHandler(ctxt.archHandler()), _entryAtom(nullptr),
|
2016-01-16 09:09:23 +08:00
|
|
|
_hasTLVDescriptors(false), _subsectionsViaSymbols(true) {}
|
2014-12-06 06:03:28 +08:00
|
|
|
~Util();
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2016-01-16 09:09:23 +08:00
|
|
|
void processDefinedAtoms(const lld::File &atomFile);
|
|
|
|
void processAtomAttributes(const DefinedAtom *atom);
|
|
|
|
void assignAtomToSection(const DefinedAtom *atom);
|
2013-11-07 05:36:55 +08:00
|
|
|
void organizeSections();
|
2014-07-30 08:58:06 +08:00
|
|
|
void assignAddressesToSections(const NormalizedFile &file);
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t fileFlags();
|
|
|
|
void copySegmentInfo(NormalizedFile &file);
|
2014-07-30 08:58:06 +08:00
|
|
|
void copySectionInfo(NormalizedFile &file);
|
|
|
|
void updateSectionInfo(NormalizedFile &file);
|
2013-11-07 05:36:55 +08:00
|
|
|
void buildAtomToAddressMap();
|
2016-07-28 06:55:30 +08:00
|
|
|
llvm::Error synthesizeDebugNotes(NormalizedFile &file);
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error addSymbols(const lld::File &atomFile, NormalizedFile &file);
|
2013-11-07 05:36:55 +08:00
|
|
|
void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file);
|
|
|
|
void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file);
|
2014-09-04 03:52:50 +08:00
|
|
|
void addExportInfo(const lld::File &, NormalizedFile &file);
|
2013-11-07 05:36:55 +08:00
|
|
|
void addSectionRelocs(const lld::File &, NormalizedFile &file);
|
2016-02-09 09:38:13 +08:00
|
|
|
void addFunctionStarts(const lld::File &, NormalizedFile &file);
|
2014-07-25 07:06:56 +08:00
|
|
|
void buildDataInCodeArray(const lld::File &, NormalizedFile &file);
|
2013-11-07 05:36:55 +08:00
|
|
|
void addDependentDylibs(const lld::File &, NormalizedFile &file);
|
|
|
|
void copyEntryPointAddress(NormalizedFile &file);
|
2014-07-30 08:58:06 +08:00
|
|
|
void copySectionContent(NormalizedFile &file);
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2016-02-04 10:16:08 +08:00
|
|
|
bool allSourceFilesHaveMinVersions() const {
|
|
|
|
return _allSourceFilesHaveMinVersions;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t minVersion() const {
|
|
|
|
return _minVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadCommandType minVersionCommandType() const {
|
|
|
|
return _minVersionCommandType;
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
private:
|
|
|
|
typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection;
|
|
|
|
typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; };
|
|
|
|
typedef llvm::StringMap<DylibInfo> DylibPathToInfo;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
SectionInfo *sectionForAtom(const DefinedAtom*);
|
2014-06-11 09:30:55 +08:00
|
|
|
SectionInfo *getRelocatableSection(DefinedAtom::ContentType type);
|
|
|
|
SectionInfo *getFinalSection(DefinedAtom::ContentType type);
|
2013-11-07 05:36:55 +08:00
|
|
|
void appendAtom(SectionInfo *sect, const DefinedAtom *atom);
|
|
|
|
SegmentInfo *segmentForName(StringRef segName);
|
|
|
|
void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr);
|
2014-07-30 08:58:06 +08:00
|
|
|
void layoutSectionsInTextSegment(size_t, SegmentInfo *, uint64_t &);
|
2013-11-07 05:36:55 +08:00
|
|
|
void copySectionContent(SectionInfo *si, ContentBytes &content);
|
2014-06-11 08:24:16 +08:00
|
|
|
uint16_t descBits(const DefinedAtom* atom);
|
2013-11-07 05:36:55 +08:00
|
|
|
int dylibOrdinal(const SharedLibraryAtom *sa);
|
2014-01-27 11:09:26 +08:00
|
|
|
void segIndexForSection(const SectionInfo *sect,
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t &segmentIndex, uint64_t &segmentStartAddr);
|
|
|
|
const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom);
|
|
|
|
const Atom *targetOfStub(const DefinedAtom *stubAtom);
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error getSymbolTableRegion(const DefinedAtom* atom,
|
|
|
|
bool &inGlobalsRegion,
|
|
|
|
SymbolScope &symbolScope);
|
2013-11-07 05:36:55 +08:00
|
|
|
void appendSection(SectionInfo *si, NormalizedFile &file);
|
2014-07-18 07:16:21 +08:00
|
|
|
uint32_t sectionIndexForAtom(const Atom *atom);
|
2017-08-25 07:51:40 +08:00
|
|
|
void fixLazyReferenceImm(const DefinedAtom *atom, uint32_t offset,
|
|
|
|
NormalizedFile &file);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex;
|
2014-08-21 09:59:11 +08:00
|
|
|
struct AtomAndIndex { const Atom *atom; uint32_t index; SymbolScope scope; };
|
2013-12-26 03:39:08 +08:00
|
|
|
struct AtomSorter {
|
|
|
|
bool operator()(const AtomAndIndex &left, const AtomAndIndex &right);
|
2013-11-07 05:36:55 +08:00
|
|
|
};
|
2013-12-26 03:39:08 +08:00
|
|
|
struct SegmentSorter {
|
|
|
|
bool operator()(const SegmentInfo *left, const SegmentInfo *right);
|
2013-11-07 05:36:55 +08:00
|
|
|
static unsigned weight(const SegmentInfo *);
|
|
|
|
};
|
2013-12-26 03:39:08 +08:00
|
|
|
struct TextSectionSorter {
|
|
|
|
bool operator()(const SectionInfo *left, const SectionInfo *right);
|
2013-11-07 05:36:55 +08:00
|
|
|
static unsigned weight(const SectionInfo *);
|
|
|
|
};
|
|
|
|
|
2015-04-11 05:40:59 +08:00
|
|
|
const MachOLinkingContext &_ctx;
|
2014-07-18 07:16:21 +08:00
|
|
|
mach_o::ArchHandler &_archHandler;
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::BumpPtrAllocator _allocator;
|
|
|
|
std::vector<SectionInfo*> _sectionInfos;
|
|
|
|
std::vector<SegmentInfo*> _segmentInfos;
|
|
|
|
TypeToSection _sectionMap;
|
2014-05-31 06:51:04 +08:00
|
|
|
std::vector<SectionInfo*> _customSections;
|
2013-11-07 05:36:55 +08:00
|
|
|
AtomToAddress _atomToAddress;
|
|
|
|
DylibPathToInfo _dylibInfo;
|
|
|
|
const DefinedAtom *_entryAtom;
|
|
|
|
AtomToIndex _atomToSymbolIndex;
|
2014-11-13 06:21:56 +08:00
|
|
|
std::vector<const Atom *> _machHeaderAliasAtoms;
|
2015-06-24 04:35:31 +08:00
|
|
|
bool _hasTLVDescriptors;
|
2016-01-16 09:09:23 +08:00
|
|
|
bool _subsectionsViaSymbols;
|
2016-02-04 10:16:08 +08:00
|
|
|
bool _allSourceFilesHaveMinVersions = true;
|
|
|
|
LoadCommandType _minVersionCommandType = (LoadCommandType)0;
|
|
|
|
uint32_t _minVersion = 0;
|
2016-07-28 06:55:30 +08:00
|
|
|
std::vector<lld::mach_o::Stab> _stabs;
|
2013-11-07 05:36:55 +08:00
|
|
|
};
|
|
|
|
|
2014-12-06 06:03:28 +08:00
|
|
|
Util::~Util() {
|
|
|
|
// The SectionInfo structs are BumpPtr allocated, but atomsAndOffsets needs
|
|
|
|
// to be deleted.
|
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
|
|
|
// clear() destroys vector elements, but does not deallocate.
|
|
|
|
// Instead use swap() to deallocate vector buffer.
|
|
|
|
std::vector<AtomInfo> empty;
|
|
|
|
si->atomsAndOffsets.swap(empty);
|
|
|
|
}
|
|
|
|
// The SegmentInfo structs are BumpPtr allocated, but sections needs
|
|
|
|
// to be deleted.
|
|
|
|
for (SegmentInfo *sgi : _segmentInfos) {
|
|
|
|
std::vector<SectionInfo*> empty2;
|
|
|
|
sgi->sections.swap(empty2);
|
|
|
|
}
|
|
|
|
}
|
2014-06-10 09:50:00 +08:00
|
|
|
|
2014-06-11 09:30:55 +08:00
|
|
|
SectionInfo *Util::getRelocatableSection(DefinedAtom::ContentType type) {
|
2014-06-10 09:50:00 +08:00
|
|
|
StringRef segmentName;
|
|
|
|
StringRef sectionName;
|
|
|
|
SectionType sectionType;
|
|
|
|
SectionAttr sectionAttrs;
|
2016-01-08 05:07:26 +08:00
|
|
|
bool relocsToDefinedCanBeImplicit;
|
2014-06-10 09:50:00 +08:00
|
|
|
|
|
|
|
// Use same table used by when parsing .o files.
|
|
|
|
relocatableSectionInfoForContentType(type, segmentName, sectionName,
|
2016-01-08 05:07:26 +08:00
|
|
|
sectionType, sectionAttrs,
|
|
|
|
relocsToDefinedCanBeImplicit);
|
2014-06-10 09:50:00 +08:00
|
|
|
// If we already have a SectionInfo with this name, re-use it.
|
|
|
|
// This can happen if two ContentType map to the same mach-o section.
|
|
|
|
for (auto sect : _sectionMap) {
|
|
|
|
if (sect.second->sectionName.equals(sectionName) &&
|
|
|
|
sect.second->segmentName.equals(segmentName)) {
|
|
|
|
return sect.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise allocate new SectionInfo object.
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *sect = new (_allocator)
|
2016-01-08 05:07:26 +08:00
|
|
|
SectionInfo(segmentName, sectionName, sectionType, _ctx, sectionAttrs,
|
|
|
|
relocsToDefinedCanBeImplicit);
|
2014-06-11 09:30:55 +08:00
|
|
|
_sectionInfos.push_back(sect);
|
|
|
|
_sectionMap[type] = sect;
|
|
|
|
return sect;
|
2014-06-10 09:50:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ENTRY(seg, sect, type, atomType) \
|
|
|
|
{seg, sect, type, DefinedAtom::atomType }
|
|
|
|
|
|
|
|
struct MachOFinalSectionFromAtomType {
|
|
|
|
StringRef segmentName;
|
|
|
|
StringRef sectionName;
|
|
|
|
SectionType sectionType;
|
|
|
|
DefinedAtom::ContentType atomType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const MachOFinalSectionFromAtomType sectsToAtomType[] = {
|
|
|
|
ENTRY("__TEXT", "__text", S_REGULAR, typeCode),
|
2016-02-03 05:37:15 +08:00
|
|
|
ENTRY("__TEXT", "__text", S_REGULAR, typeMachHeader),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString),
|
|
|
|
ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String),
|
|
|
|
ENTRY("__TEXT", "__const", S_REGULAR, typeConstant),
|
|
|
|
ENTRY("__TEXT", "__const", S_4BYTE_LITERALS, typeLiteral4),
|
|
|
|
ENTRY("__TEXT", "__const", S_8BYTE_LITERALS, typeLiteral8),
|
|
|
|
ENTRY("__TEXT", "__const", S_16BYTE_LITERALS, typeLiteral16),
|
|
|
|
ENTRY("__TEXT", "__stubs", S_SYMBOL_STUBS, typeStub),
|
|
|
|
ENTRY("__TEXT", "__stub_helper", S_REGULAR, typeStubHelper),
|
|
|
|
ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA),
|
|
|
|
ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI),
|
2014-10-01 05:29:54 +08:00
|
|
|
ENTRY("__TEXT", "__unwind_info", S_REGULAR, typeProcessedUnwindInfo),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__DATA", "__data", S_REGULAR, typeData),
|
|
|
|
ENTRY("__DATA", "__const", S_REGULAR, typeConstData),
|
|
|
|
ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString),
|
|
|
|
ENTRY("__DATA", "__la_symbol_ptr", S_LAZY_SYMBOL_POINTERS,
|
|
|
|
typeLazyPointer),
|
|
|
|
ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS,
|
|
|
|
typeInitializerPtr),
|
|
|
|
ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS,
|
|
|
|
typeTerminatorPtr),
|
2014-11-06 09:14:12 +08:00
|
|
|
ENTRY("__DATA", "__got", S_NON_LAZY_SYMBOL_POINTERS,
|
2014-06-10 09:50:00 +08:00
|
|
|
typeGOT),
|
2016-02-10 04:11:17 +08:00
|
|
|
ENTRY("__DATA", "__nl_symbol_ptr", S_NON_LAZY_SYMBOL_POINTERS,
|
|
|
|
typeNonLazyPointer),
|
2015-06-24 04:35:31 +08:00
|
|
|
ENTRY("__DATA", "__thread_vars", S_THREAD_LOCAL_VARIABLES,
|
|
|
|
typeThunkTLV),
|
|
|
|
ENTRY("__DATA", "__thread_data", S_THREAD_LOCAL_REGULAR,
|
|
|
|
typeTLVInitialData),
|
|
|
|
ENTRY("__DATA", "__thread_ptrs", S_THREAD_LOCAL_VARIABLE_POINTERS,
|
|
|
|
typeTLVInitializerPtr),
|
2015-12-12 07:25:09 +08:00
|
|
|
ENTRY("__DATA", "__thread_bss", S_THREAD_LOCAL_ZEROFILL,
|
|
|
|
typeTLVInitialZeroFill),
|
2014-11-06 09:14:12 +08:00
|
|
|
ENTRY("__DATA", "__bss", S_ZEROFILL, typeZeroFill),
|
2014-11-06 09:09:13 +08:00
|
|
|
ENTRY("__DATA", "__interposing", S_INTERPOSING, typeInterposingTuples),
|
2014-06-10 09:50:00 +08:00
|
|
|
};
|
|
|
|
#undef ENTRY
|
|
|
|
|
2014-06-11 09:30:55 +08:00
|
|
|
SectionInfo *Util::getFinalSection(DefinedAtom::ContentType atomType) {
|
2014-06-30 18:30:00 +08:00
|
|
|
for (auto &p : sectsToAtomType) {
|
|
|
|
if (p.atomType != atomType)
|
2014-06-10 09:50:00 +08:00
|
|
|
continue;
|
|
|
|
SectionAttr sectionAttrs = 0;
|
|
|
|
switch (atomType) {
|
2016-02-09 09:27:30 +08:00
|
|
|
case DefinedAtom::typeMachHeader:
|
2014-06-10 09:50:00 +08:00
|
|
|
case DefinedAtom::typeCode:
|
|
|
|
case DefinedAtom::typeStub:
|
2014-11-11 09:31:18 +08:00
|
|
|
case DefinedAtom::typeStubHelper:
|
2016-02-04 10:23:34 +08:00
|
|
|
sectionAttrs = S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS;
|
2014-06-10 09:50:00 +08:00
|
|
|
break;
|
2015-06-24 04:35:31 +08:00
|
|
|
case DefinedAtom::typeThunkTLV:
|
|
|
|
_hasTLVDescriptors = true;
|
|
|
|
break;
|
2014-06-10 09:50:00 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// If we already have a SectionInfo with this name, re-use it.
|
|
|
|
// This can happen if two ContentType map to the same mach-o section.
|
|
|
|
for (auto sect : _sectionMap) {
|
2014-06-30 18:30:00 +08:00
|
|
|
if (sect.second->sectionName.equals(p.sectionName) &&
|
|
|
|
sect.second->segmentName.equals(p.segmentName)) {
|
2014-06-10 09:50:00 +08:00
|
|
|
return sect.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise allocate new SectionInfo object.
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *sect = new (_allocator) SectionInfo(
|
2016-01-08 05:07:26 +08:00
|
|
|
p.segmentName, p.sectionName, p.sectionType, _ctx, sectionAttrs,
|
|
|
|
/* relocsToDefinedCanBeImplicit */ false);
|
2014-06-11 09:30:55 +08:00
|
|
|
_sectionInfos.push_back(sect);
|
|
|
|
_sectionMap[atomType] = sect;
|
|
|
|
return sect;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2014-06-10 09:50:00 +08:00
|
|
|
llvm_unreachable("content type not yet supported");
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
|
2014-05-31 06:51:04 +08:00
|
|
|
if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
|
|
|
|
// Section for this atom is derived from content type.
|
|
|
|
DefinedAtom::ContentType type = atom->contentType();
|
|
|
|
auto pos = _sectionMap.find(type);
|
|
|
|
if ( pos != _sectionMap.end() )
|
|
|
|
return pos->second;
|
2015-04-11 05:40:59 +08:00
|
|
|
bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT);
|
2014-06-11 09:30:55 +08:00
|
|
|
return rMode ? getRelocatableSection(type) : getFinalSection(type);
|
2014-05-31 06:51:04 +08:00
|
|
|
} else {
|
|
|
|
// This atom needs to be in a custom section.
|
|
|
|
StringRef customName = atom->customSectionName();
|
|
|
|
// Look to see if we have already allocated the needed custom section.
|
|
|
|
for(SectionInfo *sect : _customSections) {
|
|
|
|
const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
|
|
|
|
if (firstAtom->customSectionName().equals(customName)) {
|
|
|
|
return sect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not found, so need to create a new custom section.
|
|
|
|
size_t seperatorIndex = customName.find('/');
|
|
|
|
assert(seperatorIndex != StringRef::npos);
|
2014-06-30 17:49:33 +08:00
|
|
|
StringRef segName = customName.slice(0, seperatorIndex);
|
|
|
|
StringRef sectName = customName.drop_front(seperatorIndex + 1);
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *sect =
|
2016-01-08 05:07:26 +08:00
|
|
|
new (_allocator) SectionInfo(segName, sectName, S_REGULAR, _ctx,
|
|
|
|
0, /* relocsToDefinedCanBeImplicit */ false);
|
2014-05-31 06:51:04 +08:00
|
|
|
_customSections.push_back(sect);
|
2014-06-30 17:49:33 +08:00
|
|
|
_sectionInfos.push_back(sect);
|
2014-05-31 06:51:04 +08:00
|
|
|
return sect;
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
|
|
|
|
// Figure out offset for atom in this section given alignment constraints.
|
|
|
|
uint64_t offset = sect->size;
|
|
|
|
DefinedAtom::Alignment atomAlign = atom->alignment();
|
2015-03-26 10:23:45 +08:00
|
|
|
uint64_t align = atomAlign.value;
|
2013-11-07 05:36:55 +08:00
|
|
|
uint64_t requiredModulus = atomAlign.modulus;
|
2015-03-26 10:23:45 +08:00
|
|
|
uint64_t currentModulus = (offset % align);
|
2013-11-07 05:36:55 +08:00
|
|
|
if ( currentModulus != requiredModulus ) {
|
|
|
|
if ( requiredModulus > currentModulus )
|
|
|
|
offset += requiredModulus-currentModulus;
|
|
|
|
else
|
2015-03-26 10:23:45 +08:00
|
|
|
offset += align+requiredModulus-currentModulus;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
// Record max alignment of any atom in this section.
|
2015-03-26 10:23:45 +08:00
|
|
|
if (align > sect->alignment)
|
2015-03-26 10:03:44 +08:00
|
|
|
sect->alignment = atomAlign.value;
|
2013-11-07 05:36:55 +08:00
|
|
|
// Assign atom to this section with this offset.
|
|
|
|
AtomInfo ai = {atom, offset};
|
|
|
|
sect->atomsAndOffsets.push_back(ai);
|
|
|
|
// Update section size to include this atom.
|
|
|
|
sect->size = offset + atom->size();
|
|
|
|
}
|
|
|
|
|
2016-01-16 09:09:23 +08:00
|
|
|
void Util::processDefinedAtoms(const lld::File &atomFile) {
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const DefinedAtom *atom : atomFile.defined()) {
|
2016-01-16 09:09:23 +08:00
|
|
|
processAtomAttributes(atom);
|
|
|
|
assignAtomToSection(atom);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 09:09:23 +08:00
|
|
|
void Util::processAtomAttributes(const DefinedAtom *atom) {
|
2016-02-04 10:16:08 +08:00
|
|
|
if (auto *machoFile = dyn_cast<mach_o::MachOFile>(&atom->file())) {
|
|
|
|
// If the file doesn't use subsections via symbols, then make sure we don't
|
|
|
|
// add that flag to the final output file if we have a relocatable file.
|
2016-02-02 05:42:17 +08:00
|
|
|
if (!machoFile->subsectionsViaSymbols())
|
|
|
|
_subsectionsViaSymbols = false;
|
2016-02-04 10:16:08 +08:00
|
|
|
|
|
|
|
// All the source files must have min versions for us to output an object
|
|
|
|
// file with a min version.
|
|
|
|
if (auto v = machoFile->minVersion())
|
|
|
|
_minVersion = std::max(_minVersion, v);
|
|
|
|
else
|
|
|
|
_allSourceFilesHaveMinVersions = false;
|
|
|
|
|
|
|
|
// If we don't have a platform load command, but one of the source files
|
|
|
|
// does, then take the one from the file.
|
|
|
|
if (!_minVersionCommandType)
|
|
|
|
if (auto v = machoFile->minVersionLoadCommandKind())
|
|
|
|
_minVersionCommandType = v;
|
|
|
|
}
|
2016-01-16 09:09:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Util::assignAtomToSection(const DefinedAtom *atom) {
|
2016-02-03 05:37:15 +08:00
|
|
|
if (atom->contentType() == DefinedAtom::typeMachHeader) {
|
|
|
|
_machHeaderAliasAtoms.push_back(atom);
|
|
|
|
// Assign atom to this section with this offset.
|
|
|
|
AtomInfo ai = {atom, 0};
|
|
|
|
sectionForAtom(atom)->atomsAndOffsets.push_back(ai);
|
|
|
|
} else if (atom->contentType() == DefinedAtom::typeDSOHandle)
|
2016-01-16 09:09:23 +08:00
|
|
|
_machHeaderAliasAtoms.push_back(atom);
|
|
|
|
else
|
|
|
|
appendAtom(sectionForAtom(atom), atom);
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
SegmentInfo *Util::segmentForName(StringRef segName) {
|
|
|
|
for (SegmentInfo *si : _segmentInfos) {
|
|
|
|
if ( si->name.equals(segName) )
|
|
|
|
return si;
|
|
|
|
}
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *info = new (_allocator) SegmentInfo(segName);
|
2016-02-06 08:51:16 +08:00
|
|
|
|
|
|
|
// Set the initial segment protection.
|
2013-11-07 05:36:55 +08:00
|
|
|
if (segName.equals("__TEXT"))
|
2016-02-06 08:51:16 +08:00
|
|
|
info->init_access = VM_PROT_READ | VM_PROT_EXECUTE;
|
2013-11-07 05:36:55 +08:00
|
|
|
else if (segName.equals("__PAGEZERO"))
|
2016-02-06 08:51:16 +08:00
|
|
|
info->init_access = 0;
|
|
|
|
else if (segName.equals("__LINKEDIT"))
|
|
|
|
info->init_access = VM_PROT_READ;
|
|
|
|
else {
|
|
|
|
// All others default to read-write
|
|
|
|
info->init_access = VM_PROT_READ | VM_PROT_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set max segment protection
|
|
|
|
// Note, its overkill to use a switch statement here, but makes it so much
|
|
|
|
// easier to use switch coverage to catch new cases.
|
|
|
|
switch (_ctx.os()) {
|
|
|
|
case lld::MachOLinkingContext::OS::unknown:
|
|
|
|
case lld::MachOLinkingContext::OS::macOSX:
|
|
|
|
case lld::MachOLinkingContext::OS::iOS_simulator:
|
|
|
|
if (segName.equals("__PAGEZERO")) {
|
|
|
|
info->max_access = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// All others default to all
|
|
|
|
info->max_access = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
|
|
|
|
break;
|
|
|
|
case lld::MachOLinkingContext::OS::iOS:
|
|
|
|
// iPhoneOS always uses same protection for max and initial
|
|
|
|
info->max_access = info->init_access;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
_segmentInfos.push_back(info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
|
|
|
|
return llvm::StringSwitch<unsigned>(seg->name)
|
|
|
|
.Case("__PAGEZERO", 1)
|
|
|
|
.Case("__TEXT", 2)
|
|
|
|
.Case("__DATA", 3)
|
|
|
|
.Default(100);
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
bool Util::SegmentSorter::operator()(const SegmentInfo *left,
|
2013-11-07 05:36:55 +08:00
|
|
|
const SegmentInfo *right) {
|
|
|
|
return (weight(left) < weight(right));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
|
|
|
|
return llvm::StringSwitch<unsigned>(sect->sectionName)
|
|
|
|
.Case("__text", 1)
|
|
|
|
.Case("__stubs", 2)
|
|
|
|
.Case("__stub_helper", 3)
|
|
|
|
.Case("__const", 4)
|
|
|
|
.Case("__cstring", 5)
|
|
|
|
.Case("__unwind_info", 98)
|
|
|
|
.Case("__eh_frame", 99)
|
|
|
|
.Default(10);
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
bool Util::TextSectionSorter::operator()(const SectionInfo *left,
|
2013-11-07 05:36:55 +08:00
|
|
|
const SectionInfo *right) {
|
|
|
|
return (weight(left) < weight(right));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Util::organizeSections() {
|
2016-01-05 06:29:36 +08:00
|
|
|
// NOTE!: Keep this in sync with assignAddressesToSections.
|
|
|
|
switch (_ctx.outputMachOType()) {
|
2014-10-25 06:19:22 +08:00
|
|
|
case llvm::MachO::MH_EXECUTE:
|
|
|
|
// Main executables, need a zero-page segment
|
2013-11-07 05:36:55 +08:00
|
|
|
segmentForName("__PAGEZERO");
|
2014-10-25 06:19:22 +08:00
|
|
|
// Fall into next case.
|
2017-06-10 16:06:17 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2014-10-25 06:19:22 +08:00
|
|
|
case llvm::MachO::MH_DYLIB:
|
|
|
|
case llvm::MachO::MH_BUNDLE:
|
|
|
|
// All dynamic code needs TEXT segment to hold the load commands.
|
|
|
|
segmentForName("__TEXT");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-01-05 06:29:36 +08:00
|
|
|
}
|
2016-02-06 08:14:15 +08:00
|
|
|
segmentForName("__LINKEDIT");
|
|
|
|
|
2016-01-05 06:29:36 +08:00
|
|
|
// Group sections into segments.
|
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
|
|
|
SegmentInfo *seg = segmentForName(si->segmentName);
|
|
|
|
seg->sections.push_back(si);
|
|
|
|
}
|
|
|
|
// Sort segments.
|
|
|
|
std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2016-01-05 06:29:36 +08:00
|
|
|
// Sort sections within segments.
|
|
|
|
for (SegmentInfo *seg : _segmentInfos) {
|
|
|
|
if (seg->name.equals("__TEXT")) {
|
|
|
|
std::sort(seg->sections.begin(), seg->sections.end(),
|
|
|
|
TextSectionSorter());
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 06:29:36 +08:00
|
|
|
|
|
|
|
// Record final section indexes.
|
|
|
|
uint32_t segmentIndex = 0;
|
|
|
|
uint32_t sectionIndex = 1;
|
|
|
|
for (SegmentInfo *seg : _segmentInfos) {
|
|
|
|
seg->normalizedSegmentIndex = segmentIndex++;
|
|
|
|
for (SectionInfo *sect : seg->sections)
|
|
|
|
sect->finalSectionIndex = sectionIndex++;
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
|
|
|
|
seg->address = addr;
|
|
|
|
for (SectionInfo *sect : seg->sections) {
|
2016-01-15 04:53:50 +08:00
|
|
|
sect->address = llvm::alignTo(addr, sect->alignment);
|
2014-12-10 08:24:37 +08:00
|
|
|
addr = sect->address + sect->size;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2016-01-15 04:53:50 +08:00
|
|
|
seg->size = llvm::alignTo(addr - seg->address, _ctx.pageSize());
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// __TEXT segment lays out backwards so padding is at front after load commands.
|
2014-07-30 08:58:06 +08:00
|
|
|
void Util::layoutSectionsInTextSegment(size_t hlcSize, SegmentInfo *seg,
|
|
|
|
uint64_t &addr) {
|
2013-11-07 05:36:55 +08:00
|
|
|
seg->address = addr;
|
|
|
|
// Walks sections starting at end to calculate padding for start.
|
|
|
|
int64_t taddr = 0;
|
2014-01-27 11:09:26 +08:00
|
|
|
for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
|
2013-11-07 05:36:55 +08:00
|
|
|
SectionInfo *sect = *it;
|
|
|
|
taddr -= sect->size;
|
2015-03-26 10:03:44 +08:00
|
|
|
taddr = taddr & (0 - sect->alignment);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2014-07-30 08:58:06 +08:00
|
|
|
int64_t padding = taddr - hlcSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
while (padding < 0)
|
2015-04-11 05:40:59 +08:00
|
|
|
padding += _ctx.pageSize();
|
2013-11-07 05:36:55 +08:00
|
|
|
// Start assigning section address starting at padded offset.
|
2014-07-30 08:58:06 +08:00
|
|
|
addr += (padding + hlcSize);
|
2013-11-07 05:36:55 +08:00
|
|
|
for (SectionInfo *sect : seg->sections) {
|
2016-01-15 04:53:50 +08:00
|
|
|
sect->address = llvm::alignTo(addr, sect->alignment);
|
2013-11-07 05:36:55 +08:00
|
|
|
addr = sect->address + sect->size;
|
|
|
|
}
|
2016-01-15 04:53:50 +08:00
|
|
|
seg->size = llvm::alignTo(addr - seg->address, _ctx.pageSize());
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
void Util::assignAddressesToSections(const NormalizedFile &file) {
|
2016-01-05 06:29:36 +08:00
|
|
|
// NOTE!: Keep this in sync with organizeSections.
|
lld: Fix initial Mach-O load commands size calculation omitting LC_FUNCTION_STARTS
Patch by Nicholas Allegra.
The Mach-O writer calculates the size of load commands multiple times.
First, Util::assignAddressesToSections() (in MachONormalizedFileFromAtoms.cpp)
calculates the size using headerAndLoadCommandsSize() (in
MachONormalizedFileBinaryWriter.cpp), which creates a temporary
MachOFileLayout for the NormalizedFile, only to retrieve its
headerAndLoadCommandsSize. Later, writeBinary() (in
MachONormalizedFileBinaryWriter.cpp) creates a new layout and uses the offsets
from that layout to actually write out everything in the NormalizedFile.
But the NormalizedFile changes between the first computation and the second.
When Util::assignAddressesToSections is called, file.functionStarts is always
empty because Util::addFunctionStarts has not yet been called. Yet
MachOFileLayout decides whether to include a LC_FUNCTION_STARTS command based
on whether file.functionStarts is nonempty. Therefore, the initial computation
always omits it.
Because padding for the __TEXT segment (to make its size a multiple of the
page size) is added between the load commands and the first section, LLD still
generates a valid binary as long as the amount of padding happens to be large
enough to fit LC_FUNCTION_STARTS command, which it usually is.
However, it's easy to reproduce the issue by adding a section of a precise
size. Given foo.c:
__attribute__((section("__TEXT,__foo")))
char foo[0xd78] = {0};
Run:
clang -dynamiclib -o foo.dylib foo.c -fuse-ld=lld -install_name
/usr/lib/foo.dylib
otool -lvv foo.dylib
This should produce:
truncated or malformed object (offset field of section 1 in LC_SEGMENT_64
command 0 not past the headers of the file)
This commit:
- Changes MachOFileLayout to always assume LC_FUNCTION_STARTS is present for
the initial computation, as long as generating LC_FUNCTION_STARTS is
enabled. It would be slightly better to check whether there are actually
any functions, since no LC_FUNCTION_STARTS will be generated if not, but it
doesn't cause a problem if the initial computation is too high.
- Adds a test.
- Adds an assert in MachOFileLayout::writeSectionContent() that we are not
writing section content into the load commands region (which would happen
if the offset was calculated too low due to the initial load commands size
calculation being too low). Adds an assert in
MachOFileLayout::writeLoadCommands to validate a similar situation where
two size-of-load-commands computations are expected to be equivalent.
llvm-svn: 358545
2019-04-17 09:47:16 +08:00
|
|
|
size_t hlcSize = headerAndLoadCommandsSize(file,
|
|
|
|
_ctx.generateFunctionStartsLoadCommand());
|
2014-09-10 18:39:57 +08:00
|
|
|
uint64_t address = 0;
|
2016-01-05 06:29:36 +08:00
|
|
|
for (SegmentInfo *seg : _segmentInfos) {
|
|
|
|
if (seg->name.equals("__PAGEZERO")) {
|
|
|
|
seg->size = _ctx.pageZeroSize();
|
|
|
|
address += seg->size;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2016-01-05 06:29:36 +08:00
|
|
|
else if (seg->name.equals("__TEXT")) {
|
|
|
|
// _ctx.baseAddress() == 0 implies it was either unspecified or
|
|
|
|
// pageZeroSize is also 0. In either case resetting address is safe.
|
|
|
|
address = _ctx.baseAddress() ? _ctx.baseAddress() : address;
|
|
|
|
layoutSectionsInTextSegment(hlcSize, seg, address);
|
|
|
|
} else
|
|
|
|
layoutSectionsInSegment(seg, address);
|
|
|
|
|
2016-01-15 04:53:50 +08:00
|
|
|
address = llvm::alignTo(address, _ctx.pageSize());
|
2016-01-05 06:29:36 +08:00
|
|
|
}
|
|
|
|
DEBUG_WITH_TYPE("WriterMachO-norm",
|
|
|
|
llvm::dbgs() << "assignAddressesToSections()\n";
|
|
|
|
for (SegmentInfo *sgi : _segmentInfos) {
|
|
|
|
llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
|
|
|
|
<< ", size=" << llvm::format("0x%08llX", sgi->size)
|
|
|
|
<< ", segment-name='" << sgi->name
|
|
|
|
<< "'\n";
|
|
|
|
for (SectionInfo *si : sgi->sections) {
|
|
|
|
llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
|
|
|
|
<< ", size=" << llvm::format("0x%08llX", si->size)
|
|
|
|
<< ", section-name='" << si->sectionName
|
|
|
|
<< "\n";
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 06:29:36 +08:00
|
|
|
);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Util::copySegmentInfo(NormalizedFile &file) {
|
|
|
|
for (SegmentInfo *sgi : _segmentInfos) {
|
|
|
|
Segment seg;
|
|
|
|
seg.name = sgi->name;
|
|
|
|
seg.address = sgi->address;
|
|
|
|
seg.size = sgi->size;
|
2016-02-06 08:51:16 +08:00
|
|
|
seg.init_access = sgi->init_access;
|
|
|
|
seg.max_access = sgi->max_access;
|
2013-11-07 05:36:55 +08:00
|
|
|
file.segments.push_back(seg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
|
2014-07-30 08:58:06 +08:00
|
|
|
// Add new empty section to end of file.sections.
|
2013-11-07 05:36:55 +08:00
|
|
|
Section temp;
|
|
|
|
file.sections.push_back(std::move(temp));
|
|
|
|
Section* normSect = &file.sections.back();
|
|
|
|
// Copy fields to normalized section.
|
|
|
|
normSect->segmentName = si->segmentName;
|
|
|
|
normSect->sectionName = si->sectionName;
|
|
|
|
normSect->type = si->type;
|
|
|
|
normSect->attributes = si->attributes;
|
|
|
|
normSect->address = si->address;
|
|
|
|
normSect->alignment = si->alignment;
|
|
|
|
// Record where normalized section is.
|
|
|
|
si->normalizedSectionIndex = file.sections.size()-1;
|
2014-07-30 08:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Util::copySectionContent(NormalizedFile &file) {
|
2015-04-11 05:40:59 +08:00
|
|
|
const bool r = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT);
|
2014-07-30 08:58:06 +08:00
|
|
|
|
|
|
|
// Utility function for ArchHandler to find address of atom in output file.
|
|
|
|
auto addrForAtom = [&] (const Atom &atom) -> uint64_t {
|
|
|
|
auto pos = _atomToAddress.find(&atom);
|
|
|
|
assert(pos != _atomToAddress.end());
|
|
|
|
return pos->second;
|
|
|
|
};
|
|
|
|
|
2014-10-16 03:32:21 +08:00
|
|
|
auto sectionAddrForAtom = [&] (const Atom &atom) -> uint64_t {
|
|
|
|
for (const SectionInfo *sectInfo : _sectionInfos)
|
|
|
|
for (const AtomInfo &atomInfo : sectInfo->atomsAndOffsets)
|
|
|
|
if (atomInfo.atom == &atom)
|
|
|
|
return sectInfo->address;
|
|
|
|
llvm_unreachable("atom not assigned to section");
|
|
|
|
};
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
2014-11-18 08:30:29 +08:00
|
|
|
Section *normSect = &file.sections[si->normalizedSectionIndex];
|
2015-12-12 07:25:09 +08:00
|
|
|
if (isZeroFillSection(si->type)) {
|
2014-11-18 08:30:29 +08:00
|
|
|
const uint8_t *empty = nullptr;
|
|
|
|
normSect->content = llvm::makeArrayRef(empty, si->size);
|
2014-07-30 08:58:06 +08:00
|
|
|
continue;
|
2014-11-18 08:30:29 +08:00
|
|
|
}
|
2014-07-30 08:58:06 +08:00
|
|
|
// Copy content from atoms to content buffer for section.
|
2016-03-24 06:19:16 +08:00
|
|
|
llvm::MutableArrayRef<uint8_t> sectionContent;
|
|
|
|
if (si->size) {
|
|
|
|
uint8_t *sectContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
|
|
|
|
sectionContent = llvm::MutableArrayRef<uint8_t>(sectContent, si->size);
|
|
|
|
normSect->content = sectionContent;
|
|
|
|
}
|
2014-07-30 08:58:06 +08:00
|
|
|
for (AtomInfo &ai : si->atomsAndOffsets) {
|
2016-03-23 07:07:34 +08:00
|
|
|
if (!ai.atom->size()) {
|
|
|
|
assert(ai.atom->begin() == ai.atom->end() &&
|
|
|
|
"Cannot have references without content");
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-24 06:19:16 +08:00
|
|
|
auto atomContent = sectionContent.slice(ai.offsetInSection,
|
|
|
|
ai.atom->size());
|
2014-10-01 05:29:54 +08:00
|
|
|
_archHandler.generateAtomContent(*ai.atom, r, addrForAtom,
|
2015-04-11 05:40:59 +08:00
|
|
|
sectionAddrForAtom, _ctx.baseAddress(),
|
|
|
|
atomContent);
|
2014-07-30 08:58:06 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
void Util::copySectionInfo(NormalizedFile &file) {
|
2013-11-07 05:36:55 +08:00
|
|
|
file.sections.reserve(_sectionInfos.size());
|
2016-01-05 06:29:36 +08:00
|
|
|
// Write sections grouped by segment.
|
|
|
|
for (SegmentInfo *sgi : _segmentInfos) {
|
|
|
|
for (SectionInfo *si : sgi->sections) {
|
2013-11-07 05:36:55 +08:00
|
|
|
appendSection(si, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
void Util::updateSectionInfo(NormalizedFile &file) {
|
|
|
|
file.sections.reserve(_sectionInfos.size());
|
2016-01-05 06:29:36 +08:00
|
|
|
// sections grouped by segment.
|
|
|
|
for (SegmentInfo *sgi : _segmentInfos) {
|
|
|
|
Segment *normSeg = &file.segments[sgi->normalizedSegmentIndex];
|
|
|
|
normSeg->address = sgi->address;
|
|
|
|
normSeg->size = sgi->size;
|
|
|
|
for (SectionInfo *si : sgi->sections) {
|
2014-07-30 08:58:06 +08:00
|
|
|
Section *normSect = &file.sections[si->normalizedSectionIndex];
|
|
|
|
normSect->address = si->address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
void Util::copyEntryPointAddress(NormalizedFile &nFile) {
|
2016-03-24 09:14:10 +08:00
|
|
|
if (!_entryAtom) {
|
|
|
|
nFile.entryAddress = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputTypeHasEntry()) {
|
2014-07-29 07:06:09 +08:00
|
|
|
if (_archHandler.isThumbFunction(*_entryAtom))
|
|
|
|
nFile.entryAddress = (_atomToAddress[_entryAtom] | 1);
|
|
|
|
else
|
|
|
|
nFile.entryAddress = _atomToAddress[_entryAtom];
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Util::buildAtomToAddressMap() {
|
|
|
|
DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
|
|
|
|
<< "assign atom addresses:\n");
|
2015-04-11 05:40:59 +08:00
|
|
|
const bool lookForEntry = _ctx.outputTypeHasEntry();
|
2013-11-07 05:36:55 +08:00
|
|
|
for (SectionInfo *sect : _sectionInfos) {
|
|
|
|
for (const AtomInfo &info : sect->atomsAndOffsets) {
|
|
|
|
_atomToAddress[info.atom] = sect->address + info.offsetInSection;
|
|
|
|
if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
|
|
|
|
(info.atom->size() != 0) &&
|
2015-04-11 05:40:59 +08:00
|
|
|
info.atom->name() == _ctx.entrySymbolName()) {
|
2013-11-07 05:36:55 +08:00
|
|
|
_entryAtom = info.atom;
|
|
|
|
}
|
|
|
|
DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
|
2016-01-05 09:56:59 +08:00
|
|
|
<< " address="
|
|
|
|
<< llvm::format("0x%016X", _atomToAddress[info.atom])
|
|
|
|
<< llvm::format(" 0x%09lX", info.atom)
|
|
|
|
<< ", file=#"
|
|
|
|
<< info.atom->file().ordinal()
|
|
|
|
<< ", atom=#"
|
|
|
|
<< info.atom->ordinal()
|
|
|
|
<< ", name="
|
|
|
|
<< info.atom->name()
|
|
|
|
<< ", type="
|
|
|
|
<< info.atom->contentType()
|
|
|
|
<< "\n");
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 09:56:59 +08:00
|
|
|
DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
|
|
|
|
<< "assign header alias atom addresses:\n");
|
2014-11-13 06:21:56 +08:00
|
|
|
for (const Atom *atom : _machHeaderAliasAtoms) {
|
2015-04-11 05:40:59 +08:00
|
|
|
_atomToAddress[atom] = _ctx.baseAddress();
|
2016-01-05 09:56:59 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (auto *definedAtom = dyn_cast<DefinedAtom>(atom)) {
|
|
|
|
DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
|
|
|
|
<< " address="
|
|
|
|
<< llvm::format("0x%016X", _atomToAddress[atom])
|
|
|
|
<< llvm::format(" 0x%09lX", atom)
|
|
|
|
<< ", file=#"
|
|
|
|
<< definedAtom->file().ordinal()
|
|
|
|
<< ", atom=#"
|
|
|
|
<< definedAtom->ordinal()
|
|
|
|
<< ", name="
|
|
|
|
<< definedAtom->name()
|
|
|
|
<< ", type="
|
|
|
|
<< definedAtom->contentType()
|
|
|
|
<< "\n");
|
|
|
|
} else {
|
|
|
|
DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
|
|
|
|
<< " address="
|
|
|
|
<< llvm::format("0x%016X", _atomToAddress[atom])
|
|
|
|
<< " atom=" << atom
|
|
|
|
<< " name=" << atom->name() << "\n");
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-13 06:21:56 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-28 06:55:30 +08:00
|
|
|
llvm::Error Util::synthesizeDebugNotes(NormalizedFile &file) {
|
|
|
|
|
|
|
|
// Bail out early if we don't need to generate a debug map.
|
|
|
|
if (_ctx.debugInfoMode() == MachOLinkingContext::DebugInfoMode::noDebugMap)
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
|
|
|
std::vector<const DefinedAtom*> atomsNeedingDebugNotes;
|
|
|
|
std::set<const mach_o::MachOFile*> filesWithStabs;
|
|
|
|
bool objFileHasDwarf = false;
|
|
|
|
const File *objFile = nullptr;
|
|
|
|
|
|
|
|
for (SectionInfo *sect : _sectionInfos) {
|
|
|
|
for (const AtomInfo &info : sect->atomsAndOffsets) {
|
|
|
|
if (const DefinedAtom *atom = dyn_cast<DefinedAtom>(info.atom)) {
|
|
|
|
|
|
|
|
// FIXME: No stabs/debug-notes for symbols that wouldn't be in the
|
|
|
|
// symbol table.
|
|
|
|
// FIXME: No stabs/debug-notes for kernel dtrace probes.
|
|
|
|
|
|
|
|
if (atom->contentType() == DefinedAtom::typeCFI ||
|
|
|
|
atom->contentType() == DefinedAtom::typeCString)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Whenever we encounter a new file, update the 'objfileHasDwarf' flag.
|
|
|
|
if (&info.atom->file() != objFile) {
|
|
|
|
objFileHasDwarf = false;
|
|
|
|
if (const mach_o::MachOFile *atomFile =
|
|
|
|
dyn_cast<mach_o::MachOFile>(&info.atom->file())) {
|
|
|
|
if (atomFile->debugInfo()) {
|
|
|
|
if (isa<mach_o::DwarfDebugInfo>(atomFile->debugInfo()))
|
|
|
|
objFileHasDwarf = true;
|
|
|
|
else if (isa<mach_o::StabsDebugInfo>(atomFile->debugInfo()))
|
|
|
|
filesWithStabs.insert(atomFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this atom is from a file that needs dwarf, add it to the list.
|
|
|
|
if (objFileHasDwarf)
|
|
|
|
atomsNeedingDebugNotes.push_back(info.atom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort atoms needing debug notes by file ordinal, then atom ordinal.
|
|
|
|
std::sort(atomsNeedingDebugNotes.begin(), atomsNeedingDebugNotes.end(),
|
|
|
|
[](const DefinedAtom *lhs, const DefinedAtom *rhs) {
|
|
|
|
if (lhs->file().ordinal() != rhs->file().ordinal())
|
|
|
|
return (lhs->file().ordinal() < rhs->file().ordinal());
|
|
|
|
return (lhs->ordinal() < rhs->ordinal());
|
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME: Handle <rdar://problem/17689030>: Add -add_ast_path option to \
|
|
|
|
// linker which add N_AST stab entry to output
|
|
|
|
// See OutputFile::synthesizeDebugNotes in ObjectFile.cpp in ld64.
|
|
|
|
|
|
|
|
StringRef oldFileName = "";
|
|
|
|
StringRef oldDirPath = "";
|
|
|
|
bool wroteStartSO = false;
|
|
|
|
std::unordered_set<std::string> seenFiles;
|
|
|
|
for (const DefinedAtom *atom : atomsNeedingDebugNotes) {
|
|
|
|
const auto &atomFile = cast<mach_o::MachOFile>(atom->file());
|
|
|
|
assert(dyn_cast_or_null<lld::mach_o::DwarfDebugInfo>(atomFile.debugInfo())
|
|
|
|
&& "file for atom needing debug notes does not contain dwarf");
|
|
|
|
auto &dwarf = cast<lld::mach_o::DwarfDebugInfo>(*atomFile.debugInfo());
|
|
|
|
|
|
|
|
auto &tu = dwarf.translationUnitSource();
|
|
|
|
StringRef newFileName = tu.name;
|
|
|
|
StringRef newDirPath = tu.path;
|
|
|
|
|
|
|
|
// Add an SO whenever the TU source file changes.
|
|
|
|
if (newFileName != oldFileName || newDirPath != oldDirPath) {
|
|
|
|
// Translation unit change, emit ending SO
|
|
|
|
if (oldFileName != "")
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_SO, 1, 0, 0, ""));
|
|
|
|
|
|
|
|
oldFileName = newFileName;
|
|
|
|
oldDirPath = newDirPath;
|
|
|
|
|
|
|
|
// If newDirPath doesn't end with a '/' we need to add one:
|
|
|
|
if (newDirPath.back() != '/') {
|
2016-07-30 04:04:18 +08:00
|
|
|
char *p =
|
|
|
|
file.ownedAllocations.Allocate<char>(newDirPath.size() + 2);
|
|
|
|
memcpy(p, newDirPath.data(), newDirPath.size());
|
|
|
|
p[newDirPath.size()] = '/';
|
|
|
|
p[newDirPath.size() + 1] = '\0';
|
|
|
|
newDirPath = p;
|
2016-07-28 06:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New translation unit, emit start SOs:
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_SO, 0, 0, 0, newDirPath));
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_SO, 0, 0, 0, newFileName));
|
|
|
|
|
|
|
|
// Synthesize OSO for start of file.
|
2016-07-30 04:04:18 +08:00
|
|
|
char *fullPath = nullptr;
|
2016-07-28 06:55:30 +08:00
|
|
|
{
|
|
|
|
SmallString<1024> pathBuf(atomFile.path());
|
|
|
|
if (auto EC = llvm::sys::fs::make_absolute(pathBuf))
|
|
|
|
return llvm::errorCodeToError(EC);
|
2016-07-30 04:04:18 +08:00
|
|
|
fullPath = file.ownedAllocations.Allocate<char>(pathBuf.size() + 1);
|
|
|
|
memcpy(fullPath, pathBuf.c_str(), pathBuf.size() + 1);
|
2016-07-28 06:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get mod time.
|
|
|
|
uint32_t modTime = 0;
|
|
|
|
llvm::sys::fs::file_status stat;
|
2016-07-30 04:04:18 +08:00
|
|
|
if (!llvm::sys::fs::status(fullPath, stat))
|
2016-07-28 06:55:30 +08:00
|
|
|
if (llvm::sys::fs::exists(stat))
|
2016-10-24 18:59:14 +08:00
|
|
|
modTime = llvm::sys::toTimeT(stat.getLastModificationTime());
|
2016-07-28 06:55:30 +08:00
|
|
|
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_OSO, _ctx.getCPUSubType(), 1,
|
2016-07-30 04:04:18 +08:00
|
|
|
modTime, fullPath));
|
2016-07-28 06:55:30 +08:00
|
|
|
// <rdar://problem/6337329> linker should put cpusubtype in n_sect field
|
|
|
|
// of nlist entry for N_OSO debug note entries.
|
|
|
|
wroteStartSO = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atom->contentType() == DefinedAtom::typeCode) {
|
|
|
|
// Synthesize BNSYM and start FUN stabs.
|
|
|
|
_stabs.push_back(mach_o::Stab(atom, N_BNSYM, 1, 0, 0, ""));
|
|
|
|
_stabs.push_back(mach_o::Stab(atom, N_FUN, 1, 0, 0, atom->name()));
|
|
|
|
// Synthesize any SOL stabs needed
|
|
|
|
// FIXME: add SOL stabs.
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_FUN, 0, 0,
|
|
|
|
atom->rawContent().size(), ""));
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_ENSYM, 1, 0,
|
|
|
|
atom->rawContent().size(), ""));
|
|
|
|
} else {
|
|
|
|
if (atom->scope() == Atom::scopeTranslationUnit)
|
|
|
|
_stabs.push_back(mach_o::Stab(atom, N_STSYM, 1, 0, 0, atom->name()));
|
|
|
|
else
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_GSYM, 1, 0, 0, atom->name()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit ending SO if necessary.
|
|
|
|
if (wroteStartSO)
|
|
|
|
_stabs.push_back(mach_o::Stab(nullptr, N_SO, 1, 0, 0, ""));
|
|
|
|
|
|
|
|
// Copy any stabs from .o file.
|
|
|
|
for (const auto *objFile : filesWithStabs) {
|
|
|
|
const auto &stabsList =
|
|
|
|
cast<mach_o::StabsDebugInfo>(objFile->debugInfo())->stabs();
|
|
|
|
for (auto &stab : stabsList) {
|
|
|
|
// FIXME: Drop stabs whose atoms have been dead-stripped.
|
|
|
|
_stabs.push_back(stab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
2014-06-11 08:24:16 +08:00
|
|
|
uint16_t Util::descBits(const DefinedAtom* atom) {
|
|
|
|
uint16_t desc = 0;
|
|
|
|
switch (atom->merge()) {
|
|
|
|
case lld::DefinedAtom::mergeNo:
|
|
|
|
case lld::DefinedAtom::mergeAsTentative:
|
|
|
|
break;
|
|
|
|
case lld::DefinedAtom::mergeAsWeak:
|
|
|
|
case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
|
|
|
|
desc |= N_WEAK_DEF;
|
|
|
|
break;
|
|
|
|
case lld::DefinedAtom::mergeSameNameAndSize:
|
|
|
|
case lld::DefinedAtom::mergeByLargestSection:
|
|
|
|
case lld::DefinedAtom::mergeByContent:
|
|
|
|
llvm_unreachable("Unsupported DefinedAtom::merge()");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (atom->contentType() == lld::DefinedAtom::typeResolver)
|
|
|
|
desc |= N_SYMBOL_RESOLVER;
|
2016-02-03 06:19:01 +08:00
|
|
|
if (atom->contentType() == lld::DefinedAtom::typeMachHeader)
|
|
|
|
desc |= REFERENCED_DYNAMICALLY;
|
2014-07-23 08:51:37 +08:00
|
|
|
if (_archHandler.isThumbFunction(*atom))
|
|
|
|
desc |= N_ARM_THUMB_DEF;
|
2016-08-12 05:27:18 +08:00
|
|
|
if (atom->deadStrip() == DefinedAtom::deadStripNever &&
|
|
|
|
_ctx.outputMachOType() == llvm::MachO::MH_OBJECT) {
|
2014-08-22 06:18:30 +08:00
|
|
|
if ((atom->contentType() != DefinedAtom::typeInitializerPtr)
|
|
|
|
&& (atom->contentType() != DefinedAtom::typeTerminatorPtr))
|
|
|
|
desc |= N_NO_DEAD_STRIP;
|
|
|
|
}
|
2014-06-11 08:24:16 +08:00
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
bool Util::AtomSorter::operator()(const AtomAndIndex &left,
|
2013-11-07 05:36:55 +08:00
|
|
|
const AtomAndIndex &right) {
|
|
|
|
return (left.atom->name().compare(right.atom->name()) < 0);
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error Util::getSymbolTableRegion(const DefinedAtom* atom,
|
|
|
|
bool &inGlobalsRegion,
|
|
|
|
SymbolScope &scope) {
|
2015-04-11 05:40:59 +08:00
|
|
|
bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT);
|
2014-08-21 09:59:11 +08:00
|
|
|
switch (atom->scope()) {
|
|
|
|
case Atom::scopeTranslationUnit:
|
|
|
|
scope = 0;
|
|
|
|
inGlobalsRegion = false;
|
2016-11-12 06:28:19 +08:00
|
|
|
return llvm::Error::success();
|
2014-08-21 09:59:11 +08:00
|
|
|
case Atom::scopeLinkageUnit:
|
2020-06-20 12:50:14 +08:00
|
|
|
if ((_ctx.exportMode() == MachOLinkingContext::ExportMode::exported) &&
|
2015-04-11 05:40:59 +08:00
|
|
|
_ctx.exportSymbolNamed(atom->name())) {
|
2016-03-31 07:10:39 +08:00
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
Twine("cannot export hidden symbol ") + atom->name());
|
2014-08-21 09:59:11 +08:00
|
|
|
}
|
|
|
|
if (rMode) {
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.keepPrivateExterns()) {
|
2014-08-21 09:59:11 +08:00
|
|
|
// -keep_private_externs means keep in globals region as N_PEXT.
|
|
|
|
scope = N_PEXT | N_EXT;
|
|
|
|
inGlobalsRegion = true;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-08-21 09:59:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// scopeLinkageUnit symbols are no longer global once linked.
|
|
|
|
scope = N_PEXT;
|
|
|
|
inGlobalsRegion = false;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-08-21 09:59:11 +08:00
|
|
|
case Atom::scopeGlobal:
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.exportRestrictMode()) {
|
|
|
|
if (_ctx.exportSymbolNamed(atom->name())) {
|
2014-08-21 09:59:11 +08:00
|
|
|
scope = N_EXT;
|
|
|
|
inGlobalsRegion = true;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-08-21 09:59:11 +08:00
|
|
|
} else {
|
|
|
|
scope = N_PEXT;
|
|
|
|
inGlobalsRegion = false;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-08-21 09:59:11 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scope = N_EXT;
|
|
|
|
inGlobalsRegion = true;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-08-21 09:59:11 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-03-31 09:23:23 +08:00
|
|
|
llvm_unreachable("atom->scope() unknown enum value");
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-28 06:55:30 +08:00
|
|
|
|
|
|
|
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error Util::addSymbols(const lld::File &atomFile,
|
|
|
|
NormalizedFile &file) {
|
2015-04-11 05:40:59 +08:00
|
|
|
bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT);
|
2016-07-28 06:55:30 +08:00
|
|
|
// Mach-O symbol table has four regions: stabs, locals, globals, undefs.
|
|
|
|
|
|
|
|
// Add all stabs.
|
|
|
|
for (auto &stab : _stabs) {
|
2020-07-02 12:02:09 +08:00
|
|
|
lld::mach_o::normalized::Symbol sym;
|
2016-07-28 06:55:30 +08:00
|
|
|
sym.type = static_cast<NListType>(stab.type);
|
|
|
|
sym.scope = 0;
|
|
|
|
sym.sect = stab.other;
|
|
|
|
sym.desc = stab.desc;
|
|
|
|
if (stab.atom)
|
|
|
|
sym.value = _atomToAddress[stab.atom];
|
|
|
|
else
|
|
|
|
sym.value = stab.value;
|
|
|
|
sym.name = stab.str;
|
|
|
|
file.stabsSymbols.push_back(sym);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add all local (non-global) symbols in address order
|
|
|
|
std::vector<AtomAndIndex> globals;
|
|
|
|
globals.reserve(512);
|
|
|
|
for (SectionInfo *sect : _sectionInfos) {
|
|
|
|
for (const AtomInfo &info : sect->atomsAndOffsets) {
|
|
|
|
const DefinedAtom *atom = info.atom;
|
|
|
|
if (!atom->name().empty()) {
|
2014-08-21 09:59:11 +08:00
|
|
|
SymbolScope symbolScope;
|
|
|
|
bool inGlobalsRegion;
|
|
|
|
if (auto ec = getSymbolTableRegion(atom, inGlobalsRegion, symbolScope)){
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
if (inGlobalsRegion) {
|
|
|
|
AtomAndIndex ai = { atom, sect->finalSectionIndex, symbolScope };
|
2013-11-07 05:36:55 +08:00
|
|
|
globals.push_back(ai);
|
|
|
|
} else {
|
2020-07-02 12:02:09 +08:00
|
|
|
lld::mach_o::normalized::Symbol sym;
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.name = atom->name();
|
2014-01-27 11:09:26 +08:00
|
|
|
sym.type = N_SECT;
|
2014-08-21 09:59:11 +08:00
|
|
|
sym.scope = symbolScope;
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.sect = sect->finalSectionIndex;
|
2014-07-23 08:51:37 +08:00
|
|
|
sym.desc = descBits(atom);
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.value = _atomToAddress[atom];
|
2014-07-18 07:16:21 +08:00
|
|
|
_atomToSymbolIndex[atom] = file.localSymbols.size();
|
2013-11-07 05:36:55 +08:00
|
|
|
file.localSymbols.push_back(sym);
|
|
|
|
}
|
2014-07-18 07:16:21 +08:00
|
|
|
} else if (rMode && _archHandler.needsLocalSymbolInRelocatableFile(atom)){
|
|
|
|
// Create 'Lxxx' labels for anonymous atoms if archHandler says so.
|
|
|
|
static unsigned tempNum = 1;
|
|
|
|
char tmpName[16];
|
|
|
|
sprintf(tmpName, "L%04u", tempNum++);
|
|
|
|
StringRef tempRef(tmpName);
|
2020-07-02 12:02:09 +08:00
|
|
|
lld::mach_o::normalized::Symbol sym;
|
2014-07-18 07:16:21 +08:00
|
|
|
sym.name = tempRef.copy(file.ownedAllocations);
|
|
|
|
sym.type = N_SECT;
|
|
|
|
sym.scope = 0;
|
|
|
|
sym.sect = sect->finalSectionIndex;
|
|
|
|
sym.desc = 0;
|
|
|
|
sym.value = _atomToAddress[atom];
|
|
|
|
_atomToSymbolIndex[atom] = file.localSymbols.size();
|
|
|
|
file.localSymbols.push_back(sym);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Sort global symbol alphabetically, then add to symbol table.
|
|
|
|
std::sort(globals.begin(), globals.end(), AtomSorter());
|
2014-07-18 07:16:21 +08:00
|
|
|
const uint32_t globalStartIndex = file.localSymbols.size();
|
2013-11-07 05:36:55 +08:00
|
|
|
for (AtomAndIndex &ai : globals) {
|
2020-07-02 12:02:09 +08:00
|
|
|
lld::mach_o::normalized::Symbol sym;
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.name = ai.atom->name();
|
2014-01-27 11:09:26 +08:00
|
|
|
sym.type = N_SECT;
|
2014-08-21 09:59:11 +08:00
|
|
|
sym.scope = ai.scope;
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.sect = ai.index;
|
2014-06-11 08:24:16 +08:00
|
|
|
sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.value = _atomToAddress[ai.atom];
|
2014-07-18 07:16:21 +08:00
|
|
|
_atomToSymbolIndex[ai.atom] = globalStartIndex + file.globalSymbols.size();
|
2013-11-07 05:36:55 +08:00
|
|
|
file.globalSymbols.push_back(sym);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Sort undefined symbol alphabetically, then add to symbol table.
|
|
|
|
std::vector<AtomAndIndex> undefs;
|
|
|
|
undefs.reserve(128);
|
|
|
|
for (const UndefinedAtom *atom : atomFile.undefined()) {
|
2014-08-21 09:59:11 +08:00
|
|
|
AtomAndIndex ai = { atom, 0, N_EXT };
|
2013-11-07 05:36:55 +08:00
|
|
|
undefs.push_back(ai);
|
|
|
|
}
|
|
|
|
for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
|
2014-08-21 09:59:11 +08:00
|
|
|
AtomAndIndex ai = { atom, 0, N_EXT };
|
2013-11-07 05:36:55 +08:00
|
|
|
undefs.push_back(ai);
|
|
|
|
}
|
|
|
|
std::sort(undefs.begin(), undefs.end(), AtomSorter());
|
|
|
|
const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
|
|
|
|
for (AtomAndIndex &ai : undefs) {
|
2020-07-02 12:02:09 +08:00
|
|
|
lld::mach_o::normalized::Symbol sym;
|
2014-08-14 07:11:42 +08:00
|
|
|
uint16_t desc = 0;
|
|
|
|
if (!rMode) {
|
2015-09-29 04:25:14 +08:00
|
|
|
uint8_t ordinal = 0;
|
|
|
|
if (!_ctx.useFlatNamespace())
|
|
|
|
ordinal = dylibOrdinal(dyn_cast<SharedLibraryAtom>(ai.atom));
|
2014-08-14 07:11:42 +08:00
|
|
|
llvm::MachO::SET_LIBRARY_ORDINAL(desc, ordinal);
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.name = ai.atom->name();
|
2014-01-27 11:09:26 +08:00
|
|
|
sym.type = N_UNDF;
|
2014-08-21 09:59:11 +08:00
|
|
|
sym.scope = ai.scope;
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.sect = 0;
|
2014-08-14 07:11:42 +08:00
|
|
|
sym.desc = desc;
|
2013-11-07 05:36:55 +08:00
|
|
|
sym.value = 0;
|
|
|
|
_atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
|
|
|
|
file.undefinedSymbols.push_back(sym);
|
|
|
|
}
|
2014-08-21 09:59:11 +08:00
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
|
|
|
|
for (const Reference *ref : *lpAtom) {
|
2014-07-18 07:16:21 +08:00
|
|
|
if (_archHandler.isLazyPointer(*ref)) {
|
2013-11-07 05:36:55 +08:00
|
|
|
return ref->target();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
|
|
|
|
for (const Reference *ref : *stubAtom) {
|
|
|
|
if (const Atom *ta = ref->target()) {
|
|
|
|
if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
|
|
|
|
const Atom *target = targetOfLazyPointer(lpAtom);
|
|
|
|
if (target)
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
|
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
|
|
|
Section &normSect = file.sections[si->normalizedSectionIndex];
|
|
|
|
switch (si->type) {
|
|
|
|
case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
|
|
|
|
for (const AtomInfo &info : si->atomsAndOffsets) {
|
|
|
|
bool foundTarget = false;
|
|
|
|
for (const Reference *ref : *info.atom) {
|
|
|
|
const Atom *target = ref->target();
|
|
|
|
if (target) {
|
|
|
|
if (isa<const SharedLibraryAtom>(target)) {
|
|
|
|
uint32_t index = _atomToSymbolIndex[target];
|
|
|
|
normSect.indirectSymbols.push_back(index);
|
|
|
|
foundTarget = true;
|
|
|
|
} else {
|
|
|
|
normSect.indirectSymbols.push_back(
|
|
|
|
llvm::MachO::INDIRECT_SYMBOL_LOCAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundTarget) {
|
|
|
|
normSect.indirectSymbols.push_back(
|
|
|
|
llvm::MachO::INDIRECT_SYMBOL_ABS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
|
|
|
|
for (const AtomInfo &info : si->atomsAndOffsets) {
|
|
|
|
const Atom *target = targetOfLazyPointer(info.atom);
|
|
|
|
if (target) {
|
|
|
|
uint32_t index = _atomToSymbolIndex[target];
|
|
|
|
normSect.indirectSymbols.push_back(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case llvm::MachO::S_SYMBOL_STUBS:
|
|
|
|
for (const AtomInfo &info : si->atomsAndOffsets) {
|
|
|
|
const Atom *target = targetOfStub(info.atom);
|
|
|
|
if (target) {
|
|
|
|
uint32_t index = _atomToSymbolIndex[target];
|
|
|
|
normSect.indirectSymbols.push_back(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 04:10:14 +08:00
|
|
|
void Util::addDependentDylibs(const lld::File &atomFile,
|
|
|
|
NormalizedFile &nFile) {
|
2013-11-07 05:36:55 +08:00
|
|
|
// Scan all imported symbols and build up list of dylibs they are from.
|
|
|
|
int ordinal = 1;
|
2016-08-12 04:10:14 +08:00
|
|
|
for (const auto *dylib : _ctx.allDylibs()) {
|
|
|
|
DylibPathToInfo::iterator pos = _dylibInfo.find(dylib->installName());
|
2013-11-07 05:36:55 +08:00
|
|
|
if (pos == _dylibInfo.end()) {
|
|
|
|
DylibInfo info;
|
2016-08-12 04:10:14 +08:00
|
|
|
bool flatNamespaceAtom = dylib == _ctx.flatNamespaceFile();
|
2015-09-29 04:25:14 +08:00
|
|
|
|
|
|
|
// If we're in -flat_namespace mode (or this atom came from the flat
|
|
|
|
// namespace file under -undefined dynamic_lookup) then use the flat
|
|
|
|
// lookup ordinal.
|
|
|
|
if (flatNamespaceAtom || _ctx.useFlatNamespace())
|
|
|
|
info.ordinal = BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
|
|
|
|
else
|
|
|
|
info.ordinal = ordinal++;
|
2016-08-12 04:10:14 +08:00
|
|
|
info.hasWeak = false;
|
2013-11-07 05:36:55 +08:00
|
|
|
info.hasNonWeak = !info.hasWeak;
|
2016-08-12 04:10:14 +08:00
|
|
|
_dylibInfo[dylib->installName()] = info;
|
2015-09-29 04:25:14 +08:00
|
|
|
|
|
|
|
// Unless this was a flat_namespace atom, record the source dylib.
|
|
|
|
if (!flatNamespaceAtom) {
|
|
|
|
DependentDylib depInfo;
|
2016-08-12 04:10:14 +08:00
|
|
|
depInfo.path = dylib->installName();
|
2015-09-29 04:25:14 +08:00
|
|
|
depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
|
2016-08-12 04:10:14 +08:00
|
|
|
depInfo.currentVersion = _ctx.dylibCurrentVersion(dylib->path());
|
|
|
|
depInfo.compatVersion = _ctx.dylibCompatVersion(dylib->path());
|
2015-09-29 04:25:14 +08:00
|
|
|
nFile.dependentDylibs.push_back(depInfo);
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
} else {
|
2016-08-12 04:10:14 +08:00
|
|
|
pos->second.hasWeak = false;
|
|
|
|
pos->second.hasNonWeak = !pos->second.hasWeak;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Automatically weak link dylib in which all symbols are weak (canBeNull).
|
|
|
|
for (DependentDylib &dep : nFile.dependentDylibs) {
|
|
|
|
DylibInfo &info = _dylibInfo[dep.path];
|
|
|
|
if (info.hasWeak && !info.hasNonWeak)
|
|
|
|
dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
|
2015-04-11 05:40:59 +08:00
|
|
|
else if (_ctx.isUpwardDylib(dep.path))
|
2014-10-17 03:31:28 +08:00
|
|
|
dep.kind = llvm::MachO::LC_LOAD_UPWARD_DYLIB;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
|
|
|
|
return _dylibInfo[sa->loadName()].ordinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
|
|
|
|
uint64_t &segmentStartAddr) {
|
|
|
|
segmentIndex = 0;
|
|
|
|
for (const SegmentInfo *seg : _segmentInfos) {
|
2014-01-27 11:09:26 +08:00
|
|
|
if ((seg->address <= sect->address)
|
2013-11-07 05:36:55 +08:00
|
|
|
&& (seg->address+seg->size >= sect->address+sect->size)) {
|
|
|
|
segmentStartAddr = seg->address;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++segmentIndex;
|
|
|
|
}
|
|
|
|
llvm_unreachable("section not in any segment");
|
|
|
|
}
|
|
|
|
|
2014-07-18 07:16:21 +08:00
|
|
|
uint32_t Util::sectionIndexForAtom(const Atom *atom) {
|
|
|
|
uint64_t address = _atomToAddress[atom];
|
|
|
|
for (const SectionInfo *si : _sectionInfos) {
|
|
|
|
if ((si->address <= address) && (address < si->address+si->size))
|
2016-01-05 06:29:36 +08:00
|
|
|
return si->finalSectionIndex;
|
2014-07-18 07:16:21 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("atom not in any section");
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT)
|
2013-11-07 05:36:55 +08:00
|
|
|
return;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-07-18 07:16:21 +08:00
|
|
|
// Utility function for ArchHandler to find symbol index for an atom.
|
|
|
|
auto symIndexForAtom = [&] (const Atom &atom) -> uint32_t {
|
|
|
|
auto pos = _atomToSymbolIndex.find(&atom);
|
|
|
|
assert(pos != _atomToSymbolIndex.end());
|
|
|
|
return pos->second;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Utility function for ArchHandler to find section index for an atom.
|
|
|
|
auto sectIndexForAtom = [&] (const Atom &atom) -> uint32_t {
|
|
|
|
return sectionIndexForAtom(&atom);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Utility function for ArchHandler to find address of atom in output file.
|
|
|
|
auto addressForAtom = [&] (const Atom &atom) -> uint64_t {
|
|
|
|
auto pos = _atomToAddress.find(&atom);
|
|
|
|
assert(pos != _atomToAddress.end());
|
|
|
|
return pos->second;
|
|
|
|
};
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
|
|
|
Section &normSect = file.sections[si->normalizedSectionIndex];
|
|
|
|
for (const AtomInfo &info : si->atomsAndOffsets) {
|
|
|
|
const DefinedAtom *atom = info.atom;
|
|
|
|
for (const Reference *ref : *atom) {
|
2016-01-08 05:07:26 +08:00
|
|
|
// Skip emitting relocs for sections which are always able to be
|
|
|
|
// implicitly regenerated and where the relocation targets an address
|
|
|
|
// which is defined.
|
|
|
|
if (si->relocsToDefinedCanBeImplicit && isa<DefinedAtom>(ref->target()))
|
|
|
|
continue;
|
2014-07-18 07:16:21 +08:00
|
|
|
_archHandler.appendSectionRelocations(*atom, info.offsetInSection, *ref,
|
|
|
|
symIndexForAtom,
|
|
|
|
sectIndexForAtom,
|
|
|
|
addressForAtom,
|
|
|
|
normSect.relocations);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
void Util::addFunctionStarts(const lld::File &, NormalizedFile &file) {
|
|
|
|
if (!_ctx.generateFunctionStartsLoadCommand())
|
|
|
|
return;
|
|
|
|
file.functionStarts.reserve(8192);
|
|
|
|
// Delta compress function starts, starting with the mach header symbol.
|
|
|
|
const uint64_t badAddress = ~0ULL;
|
|
|
|
uint64_t addr = badAddress;
|
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
|
|
|
for (const AtomInfo &info : si->atomsAndOffsets) {
|
|
|
|
auto type = info.atom->contentType();
|
|
|
|
if (type == DefinedAtom::typeMachHeader) {
|
|
|
|
addr = _atomToAddress[info.atom];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type != DefinedAtom::typeCode)
|
|
|
|
continue;
|
|
|
|
assert(addr != badAddress && "Missing mach header symbol");
|
|
|
|
// Skip atoms which have 0 size. This is so that LC_FUNCTION_STARTS
|
|
|
|
// can't spill in to the next section.
|
|
|
|
if (!info.atom->size())
|
|
|
|
continue;
|
|
|
|
uint64_t nextAddr = _atomToAddress[info.atom];
|
|
|
|
if (_archHandler.isThumbFunction(*info.atom))
|
|
|
|
nextAddr |= 1;
|
|
|
|
uint64_t delta = nextAddr - addr;
|
|
|
|
if (delta) {
|
|
|
|
ByteBuffer buffer;
|
|
|
|
buffer.append_uleb128(delta);
|
|
|
|
file.functionStarts.insert(file.functionStarts.end(), buffer.bytes(),
|
|
|
|
buffer.bytes() + buffer.size());
|
|
|
|
}
|
|
|
|
addr = nextAddr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null terminate, and pad to pointer size for this arch.
|
|
|
|
file.functionStarts.push_back(0);
|
|
|
|
|
|
|
|
auto size = file.functionStarts.size();
|
|
|
|
for (unsigned i = size, e = llvm::alignTo(size, _ctx.is64Bit() ? 8 : 4);
|
|
|
|
i != e; ++i)
|
|
|
|
file.functionStarts.push_back(0);
|
|
|
|
}
|
|
|
|
|
2014-07-25 07:06:56 +08:00
|
|
|
void Util::buildDataInCodeArray(const lld::File &, NormalizedFile &file) {
|
2016-02-09 10:10:39 +08:00
|
|
|
if (!_ctx.generateDataInCodeLoadCommand())
|
|
|
|
return;
|
2014-07-25 07:06:56 +08:00
|
|
|
for (SectionInfo *si : _sectionInfos) {
|
|
|
|
for (const AtomInfo &info : si->atomsAndOffsets) {
|
|
|
|
// Atoms that contain data-in-code have "transition" references
|
|
|
|
// which mark a point where the embedded data starts of ends.
|
|
|
|
// This needs to be converted to the mach-o format which is an array
|
|
|
|
// of data-in-code ranges.
|
|
|
|
uint32_t startOffset = 0;
|
|
|
|
DataRegionType mode = DataRegionType(0);
|
|
|
|
for (const Reference *ref : *info.atom) {
|
|
|
|
if (ref->kindNamespace() != Reference::KindNamespace::mach_o)
|
|
|
|
continue;
|
|
|
|
if (_archHandler.isDataInCodeTransition(ref->kindValue())) {
|
|
|
|
DataRegionType nextMode = (DataRegionType)ref->addend();
|
|
|
|
if (mode != nextMode) {
|
|
|
|
if (mode != 0) {
|
|
|
|
// Found end data range, so make range entry.
|
|
|
|
DataInCode entry;
|
|
|
|
entry.offset = si->address + info.offsetInSection + startOffset;
|
|
|
|
entry.length = ref->offsetInAtom() - startOffset;
|
|
|
|
entry.kind = mode;
|
|
|
|
file.dataInCode.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mode = nextMode;
|
|
|
|
startOffset = ref->offsetInAtom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode != 0) {
|
|
|
|
// Function ends with data (no end transition).
|
|
|
|
DataInCode entry;
|
|
|
|
entry.offset = si->address + info.offsetInSection + startOffset;
|
|
|
|
entry.length = info.atom->size() - startOffset;
|
|
|
|
entry.kind = mode;
|
|
|
|
file.dataInCode.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
|
2013-11-07 05:36:55 +08:00
|
|
|
NormalizedFile &nFile) {
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT)
|
2013-11-07 05:36:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
uint8_t segmentIndex;
|
|
|
|
uint64_t segmentStartAddr;
|
2017-08-25 07:51:40 +08:00
|
|
|
uint32_t offsetInBindInfo = 0;
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
for (SectionInfo *sect : _sectionInfos) {
|
|
|
|
segIndexForSection(sect, segmentIndex, segmentStartAddr);
|
|
|
|
for (const AtomInfo &info : sect->atomsAndOffsets) {
|
|
|
|
const DefinedAtom *atom = info.atom;
|
|
|
|
for (const Reference *ref : *atom) {
|
2014-01-27 11:09:26 +08:00
|
|
|
uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
|
2013-11-07 05:36:55 +08:00
|
|
|
- segmentStartAddr;
|
|
|
|
const Atom* targ = ref->target();
|
2014-07-18 07:16:21 +08:00
|
|
|
if (_archHandler.isPointer(*ref)) {
|
2013-11-07 05:36:55 +08:00
|
|
|
// A pointer to a DefinedAtom requires rebasing.
|
2015-02-13 22:28:18 +08:00
|
|
|
if (isa<DefinedAtom>(targ)) {
|
2013-11-07 05:36:55 +08:00
|
|
|
RebaseLocation rebase;
|
|
|
|
rebase.segIndex = segmentIndex;
|
|
|
|
rebase.segOffset = segmentOffset;
|
|
|
|
rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
|
|
|
|
nFile.rebasingInfo.push_back(rebase);
|
|
|
|
}
|
|
|
|
// A pointer to an SharedLibraryAtom requires binding.
|
|
|
|
if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
|
|
|
|
BindLocation bind;
|
|
|
|
bind.segIndex = segmentIndex;
|
|
|
|
bind.segOffset = segmentOffset;
|
|
|
|
bind.kind = llvm::MachO::BIND_TYPE_POINTER;
|
|
|
|
bind.canBeNull = sa->canBeNullAtRuntime();
|
|
|
|
bind.ordinal = dylibOrdinal(sa);
|
2014-01-27 11:09:26 +08:00
|
|
|
bind.symbolName = targ->name();
|
2013-11-07 05:36:55 +08:00
|
|
|
bind.addend = ref->addend();
|
|
|
|
nFile.bindingInfo.push_back(bind);
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 04:27:28 +08:00
|
|
|
else if (_archHandler.isLazyPointer(*ref)) {
|
|
|
|
BindLocation bind;
|
|
|
|
if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
|
|
|
|
bind.ordinal = dylibOrdinal(sa);
|
|
|
|
} else {
|
|
|
|
bind.ordinal = llvm::MachO::BIND_SPECIAL_DYLIB_SELF;
|
|
|
|
}
|
|
|
|
bind.segIndex = segmentIndex;
|
|
|
|
bind.segOffset = segmentOffset;
|
|
|
|
bind.kind = llvm::MachO::BIND_TYPE_POINTER;
|
|
|
|
bind.canBeNull = false; //sa->canBeNullAtRuntime();
|
|
|
|
bind.symbolName = targ->name();
|
|
|
|
bind.addend = ref->addend();
|
|
|
|
nFile.lazyBindingInfo.push_back(bind);
|
2017-08-25 07:51:40 +08:00
|
|
|
|
|
|
|
// Now that we know the segmentOffset and the ordinal attribute,
|
|
|
|
// we can fix the helper's code
|
|
|
|
|
|
|
|
fixLazyReferenceImm(atom, offsetInBindInfo, nFile);
|
|
|
|
|
|
|
|
// 5 bytes for opcodes + variable sizes (target name + \0 and offset
|
|
|
|
// encode's size)
|
|
|
|
offsetInBindInfo +=
|
|
|
|
6 + targ->name().size() + llvm::getULEB128Size(bind.segOffset);
|
|
|
|
if (bind.ordinal > BIND_IMMEDIATE_MASK)
|
|
|
|
offsetInBindInfo += llvm::getULEB128Size(bind.ordinal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Util::fixLazyReferenceImm(const DefinedAtom *atom, uint32_t offset,
|
|
|
|
NormalizedFile &file) {
|
2020-01-02 07:28:48 +08:00
|
|
|
for (const Reference *ref : *atom) {
|
2017-08-25 07:51:40 +08:00
|
|
|
const DefinedAtom *da = dyn_cast<DefinedAtom>(ref->target());
|
|
|
|
if (da == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Reference *helperRef = nullptr;
|
|
|
|
for (const Reference *hr : *da) {
|
|
|
|
if (hr->kindValue() == _archHandler.lazyImmediateLocationKind()) {
|
|
|
|
helperRef = hr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (helperRef == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO: maybe get the fixed atom content from _archHandler ?
|
|
|
|
for (SectionInfo *sectInfo : _sectionInfos) {
|
|
|
|
for (const AtomInfo &atomInfo : sectInfo->atomsAndOffsets) {
|
|
|
|
if (atomInfo.atom == helperRef->target()) {
|
|
|
|
auto sectionContent =
|
|
|
|
file.sections[sectInfo->normalizedSectionIndex].content;
|
|
|
|
uint8_t *rawb =
|
|
|
|
file.ownedAllocations.Allocate<uint8_t>(sectionContent.size());
|
|
|
|
llvm::MutableArrayRef<uint8_t> newContent{rawb,
|
|
|
|
sectionContent.size()};
|
|
|
|
std::copy(sectionContent.begin(), sectionContent.end(),
|
|
|
|
newContent.begin());
|
|
|
|
llvm::support::ulittle32_t *loc =
|
|
|
|
reinterpret_cast<llvm::support::ulittle32_t *>(
|
|
|
|
&newContent[atomInfo.offsetInSection +
|
|
|
|
helperRef->offsetInAtom()]);
|
|
|
|
*loc = offset;
|
|
|
|
file.sections[sectInfo->normalizedSectionIndex].content = newContent;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 03:52:50 +08:00
|
|
|
void Util::addExportInfo(const lld::File &atomFile, NormalizedFile &nFile) {
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT)
|
2014-09-04 03:52:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (SectionInfo *sect : _sectionInfos) {
|
|
|
|
for (const AtomInfo &info : sect->atomsAndOffsets) {
|
|
|
|
const DefinedAtom *atom = info.atom;
|
|
|
|
if (atom->scope() != Atom::scopeGlobal)
|
|
|
|
continue;
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.exportRestrictMode()) {
|
|
|
|
if (!_ctx.exportSymbolNamed(atom->name()))
|
2014-09-04 03:52:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Export exprt;
|
|
|
|
exprt.name = atom->name();
|
2015-09-22 06:06:02 +08:00
|
|
|
exprt.offset = _atomToAddress[atom] - _ctx.baseAddress();
|
2014-09-04 03:52:50 +08:00
|
|
|
exprt.kind = EXPORT_SYMBOL_FLAGS_KIND_REGULAR;
|
|
|
|
if (atom->merge() == DefinedAtom::mergeAsWeak)
|
|
|
|
exprt.flags = EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
|
|
|
|
else
|
|
|
|
exprt.flags = 0;
|
|
|
|
exprt.otherOffset = 0;
|
|
|
|
exprt.otherName = StringRef();
|
|
|
|
nFile.exportInfo.push_back(exprt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t Util::fileFlags() {
|
2014-07-22 08:49:49 +08:00
|
|
|
// FIXME: these need to determined at runtime.
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputMachOType() == MH_OBJECT) {
|
2020-12-29 00:09:35 +08:00
|
|
|
return _subsectionsViaSymbols ? (uint32_t)MH_SUBSECTIONS_VIA_SYMBOLS : 0;
|
2014-07-22 08:49:49 +08:00
|
|
|
} else {
|
2015-09-29 04:25:14 +08:00
|
|
|
uint32_t flags = MH_DYLDLINK;
|
|
|
|
if (!_ctx.useFlatNamespace())
|
|
|
|
flags |= MH_TWOLEVEL | MH_NOUNDEFS;
|
2015-04-11 05:40:59 +08:00
|
|
|
if ((_ctx.outputMachOType() == MH_EXECUTE) && _ctx.PIE())
|
2015-06-24 04:35:31 +08:00
|
|
|
flags |= MH_PIE;
|
|
|
|
if (_hasTLVDescriptors)
|
|
|
|
flags |= (MH_PIE | MH_HAS_TLV_DESCRIPTORS);
|
|
|
|
return flags;
|
2014-07-22 08:49:49 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
namespace normalized {
|
|
|
|
|
|
|
|
/// Convert a set of Atoms into a normalized mach-o file.
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Expected<std::unique_ptr<NormalizedFile>>
|
2014-01-27 11:09:26 +08:00
|
|
|
normalizedFromAtoms(const lld::File &atomFile,
|
2013-11-07 05:36:55 +08:00
|
|
|
const MachOLinkingContext &context) {
|
2014-01-27 11:09:26 +08:00
|
|
|
// The util object buffers info until the normalized file can be made.
|
2013-11-07 05:36:55 +08:00
|
|
|
Util util(context);
|
2016-01-16 09:09:23 +08:00
|
|
|
util.processDefinedAtoms(atomFile);
|
2013-11-07 05:36:55 +08:00
|
|
|
util.organizeSections();
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
std::unique_ptr<NormalizedFile> f(new NormalizedFile());
|
|
|
|
NormalizedFile &normFile = *f.get();
|
2014-07-30 08:58:06 +08:00
|
|
|
normFile.arch = context.arch();
|
|
|
|
normFile.fileType = context.outputMachOType();
|
|
|
|
normFile.flags = util.fileFlags();
|
2015-05-21 06:10:50 +08:00
|
|
|
normFile.stackSize = context.stackSize();
|
2014-07-30 08:58:06 +08:00
|
|
|
normFile.installName = context.installName();
|
2014-11-19 10:21:53 +08:00
|
|
|
normFile.currentVersion = context.currentVersion();
|
|
|
|
normFile.compatVersion = context.compatibilityVersion();
|
2016-02-04 06:28:29 +08:00
|
|
|
normFile.os = context.os();
|
2016-02-04 10:16:08 +08:00
|
|
|
|
|
|
|
// If we are emitting an object file, then the min version is the maximum
|
|
|
|
// of the min's of all the source files and the cmdline.
|
|
|
|
if (normFile.fileType == llvm::MachO::MH_OBJECT)
|
|
|
|
normFile.minOSverson = std::max(context.osMinVersion(), util.minVersion());
|
|
|
|
else
|
|
|
|
normFile.minOSverson = context.osMinVersion();
|
|
|
|
|
|
|
|
normFile.minOSVersionKind = util.minVersionCommandType();
|
|
|
|
|
2016-02-04 07:39:05 +08:00
|
|
|
normFile.sdkVersion = context.sdkVersion();
|
2016-02-04 10:45:23 +08:00
|
|
|
normFile.sourceVersion = context.sourceVersion();
|
2016-02-04 06:28:29 +08:00
|
|
|
|
|
|
|
if (context.generateVersionLoadCommand() &&
|
|
|
|
context.os() != MachOLinkingContext::OS::unknown)
|
|
|
|
normFile.hasMinVersionLoadCommand = true;
|
2016-02-04 10:16:08 +08:00
|
|
|
else if (normFile.fileType == llvm::MachO::MH_OBJECT &&
|
|
|
|
util.allSourceFilesHaveMinVersions() &&
|
|
|
|
((normFile.os != MachOLinkingContext::OS::unknown) ||
|
|
|
|
util.minVersionCommandType())) {
|
|
|
|
// If we emit an object file, then it should contain a min version load
|
|
|
|
// command if all of the source files also contained min version commands.
|
|
|
|
// Also, we either need to have a platform, or found a platform from the
|
|
|
|
// source object files.
|
|
|
|
normFile.hasMinVersionLoadCommand = true;
|
|
|
|
}
|
2016-02-09 10:10:39 +08:00
|
|
|
normFile.generateDataInCodeLoadCommand =
|
|
|
|
context.generateDataInCodeLoadCommand();
|
2014-09-10 07:52:59 +08:00
|
|
|
normFile.pageSize = context.pageSize();
|
2014-12-19 05:33:38 +08:00
|
|
|
normFile.rpaths = context.rpaths();
|
2013-11-07 05:36:55 +08:00
|
|
|
util.addDependentDylibs(atomFile, normFile);
|
2014-07-30 08:58:06 +08:00
|
|
|
util.copySegmentInfo(normFile);
|
|
|
|
util.copySectionInfo(normFile);
|
|
|
|
util.assignAddressesToSections(normFile);
|
|
|
|
util.buildAtomToAddressMap();
|
2016-07-28 06:55:30 +08:00
|
|
|
if (auto err = util.synthesizeDebugNotes(normFile))
|
|
|
|
return std::move(err);
|
2014-07-30 08:58:06 +08:00
|
|
|
util.updateSectionInfo(normFile);
|
|
|
|
util.copySectionContent(normFile);
|
2014-08-21 09:59:11 +08:00
|
|
|
if (auto ec = util.addSymbols(atomFile, normFile)) {
|
2016-03-31 07:10:39 +08:00
|
|
|
return std::move(ec);
|
2014-08-21 09:59:11 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
util.addIndirectSymbols(atomFile, normFile);
|
|
|
|
util.addRebaseAndBindingInfo(atomFile, normFile);
|
2014-09-04 03:52:50 +08:00
|
|
|
util.addExportInfo(atomFile, normFile);
|
2013-11-07 05:36:55 +08:00
|
|
|
util.addSectionRelocs(atomFile, normFile);
|
2016-02-09 09:38:13 +08:00
|
|
|
util.addFunctionStarts(atomFile, normFile);
|
2014-07-25 07:06:56 +08:00
|
|
|
util.buildDataInCodeArray(atomFile, normFile);
|
2013-11-07 05:36:55 +08:00
|
|
|
util.copyEntryPointAddress(normFile);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
return std::move(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace normalized
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|