2013-11-07 05:36:55 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
///
|
2014-01-27 11:09:26 +08:00
|
|
|
/// \file For mach-o object files, this implementation converts normalized
|
2013-11-07 05:36:55 +08:00
|
|
|
/// mach-o in memory to mach-o binary on disk.
|
|
|
|
///
|
2014-01-27 11:09:26 +08:00
|
|
|
/// +---------------+
|
|
|
|
/// | binary mach-o |
|
|
|
|
/// +---------------+
|
2013-11-07 05:36:55 +08:00
|
|
|
/// ^
|
|
|
|
/// |
|
|
|
|
/// |
|
2014-01-27 11:09:26 +08:00
|
|
|
/// +------------+
|
|
|
|
/// | normalized |
|
|
|
|
/// +------------+
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
#include "MachONormalizedFile.h"
|
|
|
|
#include "MachONormalizedFileBinaryUtils.h"
|
|
|
|
#include "lld/Core/Error.h"
|
|
|
|
#include "lld/Core/LLVM.h"
|
2016-01-26 05:50:54 +08:00
|
|
|
#include "llvm/ADT/ilist.h"
|
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2014-10-18 13:33:55 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2014-09-04 03:52:50 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/MachO.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <functional>
|
2014-12-02 09:50:44 +08:00
|
|
|
#include <list>
|
2013-11-07 05:36:55 +08:00
|
|
|
#include <map>
|
2014-06-13 01:15:58 +08:00
|
|
|
#include <system_error>
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
using namespace llvm::MachO;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
namespace normalized {
|
|
|
|
|
2016-01-26 05:50:54 +08:00
|
|
|
struct TrieNode; // Forward declaration.
|
|
|
|
|
|
|
|
struct TrieEdge : public llvm::ilist_node<TrieEdge> {
|
|
|
|
TrieEdge(StringRef s, TrieNode *node) : _subString(s), _child(node) {}
|
|
|
|
|
|
|
|
StringRef _subString;
|
|
|
|
struct TrieNode *_child;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace normalized
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using lld::mach_o::normalized::TrieEdge;
|
|
|
|
template <>
|
|
|
|
struct ilist_traits<TrieEdge>
|
|
|
|
: public ilist_default_traits<TrieEdge> {
|
|
|
|
private:
|
|
|
|
mutable ilist_half_node<TrieEdge> Sentinel;
|
|
|
|
public:
|
|
|
|
TrieEdge *createSentinel() const {
|
|
|
|
return static_cast<TrieEdge*>(&Sentinel);
|
|
|
|
}
|
|
|
|
void destroySentinel(TrieEdge *) const {}
|
|
|
|
|
|
|
|
TrieEdge *provideInitialHead() const { return createSentinel(); }
|
|
|
|
TrieEdge *ensureHead(TrieEdge*) const { return createSentinel(); }
|
|
|
|
static void noteHead(TrieEdge*, TrieEdge*) {}
|
|
|
|
void deleteNode(TrieEdge *N) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void createNode(const TrieEdge &);
|
|
|
|
};
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
namespace normalized {
|
|
|
|
|
|
|
|
struct TrieNode {
|
|
|
|
typedef llvm::ilist<TrieEdge> TrieEdgeList;
|
|
|
|
|
|
|
|
TrieNode(StringRef s)
|
|
|
|
: _cummulativeString(s), _address(0), _flags(0), _other(0),
|
|
|
|
_trieOffset(0), _hasExportInfo(false) {}
|
|
|
|
~TrieNode() = default;
|
|
|
|
|
|
|
|
void addSymbol(const Export &entry, BumpPtrAllocator &allocator,
|
|
|
|
std::vector<TrieNode *> &allNodes);
|
|
|
|
bool updateOffset(uint32_t &offset);
|
|
|
|
void appendToByteBuffer(ByteBuffer &out);
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef _cummulativeString;
|
|
|
|
TrieEdgeList _children;
|
|
|
|
uint64_t _address;
|
|
|
|
uint64_t _flags;
|
|
|
|
uint64_t _other;
|
|
|
|
StringRef _importedName;
|
|
|
|
uint32_t _trieOffset;
|
|
|
|
bool _hasExportInfo;
|
|
|
|
};
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
/// Utility class for writing a mach-o binary file given an in-memory
|
2014-01-27 11:09:26 +08:00
|
|
|
/// normalized file.
|
2013-11-07 05:36:55 +08:00
|
|
|
class MachOFileLayout {
|
|
|
|
public:
|
2013-12-24 07:29:50 +08:00
|
|
|
/// All layout computation is done in the constructor.
|
|
|
|
MachOFileLayout(const NormalizedFile &file);
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
/// Returns the final file size as computed in the constructor.
|
|
|
|
size_t size() const;
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
// Returns size of the mach_header and load commands.
|
|
|
|
size_t headerAndLoadCommandsSize() const;
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
/// Writes the normalized file as a binary mach-o file to the specified
|
2013-11-07 05:36:55 +08:00
|
|
|
/// path. This does not have a stream interface because the generated
|
|
|
|
/// file may need the 'x' bit set.
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error writeBinary(StringRef path);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
private:
|
|
|
|
uint32_t loadCommandsSize(uint32_t &count);
|
|
|
|
void buildFileOffsets();
|
|
|
|
void writeMachHeader();
|
2016-03-31 08:08:16 +08:00
|
|
|
llvm::Error writeLoadCommands();
|
2013-11-07 05:36:55 +08:00
|
|
|
void writeSectionContent();
|
|
|
|
void writeRelocations();
|
|
|
|
void writeSymbolTable();
|
|
|
|
void writeRebaseInfo();
|
|
|
|
void writeBindingInfo();
|
|
|
|
void writeLazyBindingInfo();
|
2014-09-04 03:52:50 +08:00
|
|
|
void writeExportInfo();
|
2016-02-09 09:38:13 +08:00
|
|
|
void writeFunctionStartsInfo();
|
2014-07-25 07:06:56 +08:00
|
|
|
void writeDataInCodeInfo();
|
2013-11-07 05:36:55 +08:00
|
|
|
void writeLinkEditContent();
|
|
|
|
void buildLinkEditInfo();
|
|
|
|
void buildRebaseInfo();
|
|
|
|
void buildBindInfo();
|
|
|
|
void buildLazyBindInfo();
|
2014-09-04 03:52:50 +08:00
|
|
|
void buildExportTrie();
|
2016-02-09 09:38:13 +08:00
|
|
|
void computeFunctionStartsSize();
|
2014-07-25 07:06:56 +08:00
|
|
|
void computeDataInCodeSize();
|
2013-11-07 05:36:55 +08:00
|
|
|
void computeSymbolTableSizes();
|
|
|
|
void buildSectionRelocations();
|
|
|
|
void appendSymbols(const std::vector<Symbol> &symbols,
|
|
|
|
uint32_t &symOffset, uint32_t &strOffset);
|
|
|
|
uint32_t indirectSymbolIndex(const Section §, uint32_t &index);
|
|
|
|
uint32_t indirectSymbolElementSize(const Section §);
|
|
|
|
|
2013-11-09 08:07:28 +08:00
|
|
|
// For use as template parameter to load command methods.
|
|
|
|
struct MachO64Trait {
|
|
|
|
typedef llvm::MachO::segment_command_64 command;
|
|
|
|
typedef llvm::MachO::section_64 section;
|
|
|
|
enum { LC = llvm::MachO::LC_SEGMENT_64 };
|
|
|
|
};
|
|
|
|
|
|
|
|
// For use as template parameter to load command methods.
|
|
|
|
struct MachO32Trait {
|
|
|
|
typedef llvm::MachO::segment_command command;
|
|
|
|
typedef llvm::MachO::section section;
|
|
|
|
enum { LC = llvm::MachO::LC_SEGMENT };
|
|
|
|
};
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-09 08:07:28 +08:00
|
|
|
template <typename T>
|
2016-03-31 08:08:16 +08:00
|
|
|
llvm::Error writeSingleSegmentLoadCommand(uint8_t *&lc);
|
|
|
|
template <typename T> llvm::Error writeSegmentLoadCommands(uint8_t *&lc);
|
2013-11-09 08:07:28 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t pointerAlign(uint32_t value);
|
|
|
|
static StringRef dyldPath();
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
struct SegExtraInfo {
|
|
|
|
uint32_t fileOffset;
|
2014-06-30 17:49:30 +08:00
|
|
|
uint32_t fileSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
std::vector<const Section*> sections;
|
|
|
|
};
|
|
|
|
typedef std::map<const Segment*, SegExtraInfo> SegMap;
|
|
|
|
struct SectionExtraInfo {
|
|
|
|
uint32_t fileOffset;
|
|
|
|
};
|
|
|
|
typedef std::map<const Section*, SectionExtraInfo> SectionMap;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
const NormalizedFile &_file;
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code _ec;
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t *_buffer;
|
|
|
|
const bool _is64;
|
|
|
|
const bool _swap;
|
|
|
|
const bool _bigEndianArch;
|
|
|
|
uint64_t _seg1addr;
|
|
|
|
uint32_t _startOfLoadCommands;
|
|
|
|
uint32_t _countOfLoadCommands;
|
|
|
|
uint32_t _endOfLoadCommands;
|
|
|
|
uint32_t _startOfRelocations;
|
2016-02-09 09:38:13 +08:00
|
|
|
uint32_t _startOfFunctionStarts;
|
2014-07-25 07:06:56 +08:00
|
|
|
uint32_t _startOfDataInCode;
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t _startOfSymbols;
|
|
|
|
uint32_t _startOfIndirectSymbols;
|
|
|
|
uint32_t _startOfSymbolStrings;
|
|
|
|
uint32_t _endOfSymbolStrings;
|
|
|
|
uint32_t _symbolTableLocalsStartIndex;
|
|
|
|
uint32_t _symbolTableGlobalsStartIndex;
|
|
|
|
uint32_t _symbolTableUndefinesStartIndex;
|
|
|
|
uint32_t _symbolStringPoolSize;
|
|
|
|
uint32_t _symbolTableSize;
|
2016-02-09 09:38:13 +08:00
|
|
|
uint32_t _functionStartsSize;
|
2014-07-25 07:06:56 +08:00
|
|
|
uint32_t _dataInCodeSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t _indirectSymbolTableCount;
|
|
|
|
// Used in object file creation only
|
|
|
|
uint32_t _startOfSectionsContent;
|
|
|
|
uint32_t _endOfSectionsContent;
|
|
|
|
// Used in final linked image only
|
|
|
|
uint32_t _startOfLinkEdit;
|
|
|
|
uint32_t _startOfRebaseInfo;
|
|
|
|
uint32_t _endOfRebaseInfo;
|
|
|
|
uint32_t _startOfBindingInfo;
|
|
|
|
uint32_t _endOfBindingInfo;
|
|
|
|
uint32_t _startOfLazyBindingInfo;
|
|
|
|
uint32_t _endOfLazyBindingInfo;
|
2014-09-04 03:52:50 +08:00
|
|
|
uint32_t _startOfExportTrie;
|
|
|
|
uint32_t _endOfExportTrie;
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t _endOfLinkEdit;
|
|
|
|
uint64_t _addressOfLinkEdit;
|
|
|
|
SegMap _segInfo;
|
|
|
|
SectionMap _sectInfo;
|
|
|
|
ByteBuffer _rebaseInfo;
|
|
|
|
ByteBuffer _bindingInfo;
|
|
|
|
ByteBuffer _lazyBindingInfo;
|
|
|
|
ByteBuffer _weakBindingInfo;
|
2014-09-04 03:52:50 +08:00
|
|
|
ByteBuffer _exportTrie;
|
2013-11-07 05:36:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t headerAndLoadCommandsSize(const NormalizedFile &file) {
|
|
|
|
MachOFileLayout layout(file);
|
2014-07-30 08:58:06 +08:00
|
|
|
return layout.headerAndLoadCommandsSize();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef MachOFileLayout::dyldPath() {
|
|
|
|
return "/usr/lib/dyld";
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MachOFileLayout::pointerAlign(uint32_t value) {
|
2016-01-15 04:53:50 +08:00
|
|
|
return llvm::alignTo(value, _is64 ? 8 : 4);
|
2014-01-27 11:09:26 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
|
2014-07-30 08:58:06 +08:00
|
|
|
size_t MachOFileLayout::headerAndLoadCommandsSize() const {
|
|
|
|
return _endOfLoadCommands;
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
MachOFileLayout::MachOFileLayout(const NormalizedFile &file)
|
2013-11-07 05:36:55 +08:00
|
|
|
: _file(file),
|
|
|
|
_is64(MachOLinkingContext::is64Bit(file.arch)),
|
|
|
|
_swap(!MachOLinkingContext::isHostEndian(file.arch)),
|
|
|
|
_bigEndianArch(MachOLinkingContext::isBigEndian(file.arch)),
|
|
|
|
_seg1addr(INT64_MAX) {
|
|
|
|
_startOfLoadCommands = _is64 ? sizeof(mach_header_64) : sizeof(mach_header);
|
2014-01-27 11:09:26 +08:00
|
|
|
const size_t segCommandBaseSize =
|
2013-11-07 05:36:55 +08:00
|
|
|
(_is64 ? sizeof(segment_command_64) : sizeof(segment_command));
|
|
|
|
const size_t sectsSize = (_is64 ? sizeof(section_64) : sizeof(section));
|
|
|
|
if (file.fileType == llvm::MachO::MH_OBJECT) {
|
|
|
|
// object files have just one segment load command containing all sections
|
|
|
|
_endOfLoadCommands = _startOfLoadCommands
|
|
|
|
+ segCommandBaseSize
|
|
|
|
+ file.sections.size() * sectsSize
|
|
|
|
+ sizeof(symtab_command);
|
|
|
|
_countOfLoadCommands = 2;
|
2016-02-04 10:16:08 +08:00
|
|
|
if (file.hasMinVersionLoadCommand) {
|
|
|
|
_endOfLoadCommands += sizeof(version_min_command);
|
|
|
|
_countOfLoadCommands++;
|
|
|
|
}
|
2016-02-09 09:38:13 +08:00
|
|
|
if (!_file.functionStarts.empty()) {
|
|
|
|
_endOfLoadCommands += sizeof(linkedit_data_command);
|
|
|
|
_countOfLoadCommands++;
|
|
|
|
}
|
2016-02-09 10:10:39 +08:00
|
|
|
if (_file.generateDataInCodeLoadCommand) {
|
2014-07-25 07:06:56 +08:00
|
|
|
_endOfLoadCommands += sizeof(linkedit_data_command);
|
|
|
|
_countOfLoadCommands++;
|
|
|
|
}
|
2014-11-18 08:30:29 +08:00
|
|
|
// Assign file offsets to each section.
|
2013-11-07 05:36:55 +08:00
|
|
|
_startOfSectionsContent = _endOfLoadCommands;
|
|
|
|
unsigned relocCount = 0;
|
2014-11-18 08:30:29 +08:00
|
|
|
uint64_t offset = _startOfSectionsContent;
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const Section § : file.sections) {
|
2015-12-12 07:25:09 +08:00
|
|
|
if (isZeroFillSection(sect.type))
|
|
|
|
_sectInfo[§].fileOffset = 0;
|
|
|
|
else {
|
2016-01-15 04:53:50 +08:00
|
|
|
offset = llvm::alignTo(offset, sect.alignment);
|
2014-11-18 08:30:29 +08:00
|
|
|
_sectInfo[§].fileOffset = offset;
|
|
|
|
offset += sect.content.size();
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
relocCount += sect.relocations.size();
|
|
|
|
}
|
2014-11-18 08:30:29 +08:00
|
|
|
_endOfSectionsContent = offset;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
computeSymbolTableSizes();
|
2016-02-09 09:38:13 +08:00
|
|
|
computeFunctionStartsSize();
|
2014-07-25 07:06:56 +08:00
|
|
|
computeDataInCodeSize();
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Align start of relocations.
|
2014-01-27 11:09:26 +08:00
|
|
|
_startOfRelocations = pointerAlign(_endOfSectionsContent);
|
2016-02-09 09:38:13 +08:00
|
|
|
_startOfFunctionStarts = _startOfRelocations + relocCount * 8;
|
|
|
|
_startOfDataInCode = _startOfFunctionStarts + _functionStartsSize;
|
2014-07-25 07:06:56 +08:00
|
|
|
_startOfSymbols = _startOfDataInCode + _dataInCodeSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add Indirect symbol table.
|
|
|
|
_startOfIndirectSymbols = _startOfSymbols + _symbolTableSize;
|
|
|
|
// Align start of symbol table and symbol strings.
|
2014-01-27 11:09:26 +08:00
|
|
|
_startOfSymbolStrings = _startOfIndirectSymbols
|
2013-11-07 05:36:55 +08:00
|
|
|
+ pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t));
|
2014-01-27 11:09:26 +08:00
|
|
|
_endOfSymbolStrings = _startOfSymbolStrings
|
2013-11-07 05:36:55 +08:00
|
|
|
+ pointerAlign(_symbolStringPoolSize);
|
|
|
|
_endOfLinkEdit = _endOfSymbolStrings;
|
2014-01-27 11:09:26 +08:00
|
|
|
DEBUG_WITH_TYPE("MachOFileLayout",
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::dbgs() << "MachOFileLayout()\n"
|
|
|
|
<< " startOfLoadCommands=" << _startOfLoadCommands << "\n"
|
|
|
|
<< " countOfLoadCommands=" << _countOfLoadCommands << "\n"
|
|
|
|
<< " endOfLoadCommands=" << _endOfLoadCommands << "\n"
|
|
|
|
<< " startOfRelocations=" << _startOfRelocations << "\n"
|
|
|
|
<< " startOfSymbols=" << _startOfSymbols << "\n"
|
|
|
|
<< " startOfSymbolStrings=" << _startOfSymbolStrings << "\n"
|
|
|
|
<< " endOfSymbolStrings=" << _endOfSymbolStrings << "\n"
|
|
|
|
<< " startOfSectionsContent=" << _startOfSectionsContent << "\n"
|
|
|
|
<< " endOfSectionsContent=" << _endOfSectionsContent << "\n");
|
|
|
|
} else {
|
|
|
|
// Final linked images have one load command per segment.
|
2014-01-27 11:09:26 +08:00
|
|
|
_endOfLoadCommands = _startOfLoadCommands
|
2013-11-07 05:36:55 +08:00
|
|
|
+ loadCommandsSize(_countOfLoadCommands);
|
|
|
|
|
|
|
|
// Assign section file offsets.
|
|
|
|
buildFileOffsets();
|
|
|
|
buildLinkEditInfo();
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// LINKEDIT of final linked images has in order:
|
|
|
|
// rebase info, binding info, lazy binding info, weak binding info,
|
2014-07-25 07:06:56 +08:00
|
|
|
// data-in-code, symbol table, indirect symbol table, symbol table strings.
|
2013-11-07 05:36:55 +08:00
|
|
|
_startOfRebaseInfo = _startOfLinkEdit;
|
|
|
|
_endOfRebaseInfo = _startOfRebaseInfo + _rebaseInfo.size();
|
|
|
|
_startOfBindingInfo = _endOfRebaseInfo;
|
|
|
|
_endOfBindingInfo = _startOfBindingInfo + _bindingInfo.size();
|
|
|
|
_startOfLazyBindingInfo = _endOfBindingInfo;
|
|
|
|
_endOfLazyBindingInfo = _startOfLazyBindingInfo + _lazyBindingInfo.size();
|
2014-09-04 03:52:50 +08:00
|
|
|
_startOfExportTrie = _endOfLazyBindingInfo;
|
|
|
|
_endOfExportTrie = _startOfExportTrie + _exportTrie.size();
|
2016-02-09 09:38:13 +08:00
|
|
|
_startOfFunctionStarts = _endOfExportTrie;
|
|
|
|
_startOfDataInCode = _startOfFunctionStarts + _functionStartsSize;
|
2014-07-25 07:06:56 +08:00
|
|
|
_startOfSymbols = _startOfDataInCode + _dataInCodeSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
_startOfIndirectSymbols = _startOfSymbols + _symbolTableSize;
|
2014-01-27 11:09:26 +08:00
|
|
|
_startOfSymbolStrings = _startOfIndirectSymbols
|
2013-11-07 05:36:55 +08:00
|
|
|
+ pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t));
|
2014-01-27 11:09:26 +08:00
|
|
|
_endOfSymbolStrings = _startOfSymbolStrings
|
2013-11-07 05:36:55 +08:00
|
|
|
+ pointerAlign(_symbolStringPoolSize);
|
|
|
|
_endOfLinkEdit = _endOfSymbolStrings;
|
2014-01-27 11:09:26 +08:00
|
|
|
DEBUG_WITH_TYPE("MachOFileLayout",
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::dbgs() << "MachOFileLayout()\n"
|
|
|
|
<< " startOfLoadCommands=" << _startOfLoadCommands << "\n"
|
|
|
|
<< " countOfLoadCommands=" << _countOfLoadCommands << "\n"
|
|
|
|
<< " endOfLoadCommands=" << _endOfLoadCommands << "\n"
|
|
|
|
<< " startOfLinkEdit=" << _startOfLinkEdit << "\n"
|
|
|
|
<< " startOfRebaseInfo=" << _startOfRebaseInfo << "\n"
|
|
|
|
<< " endOfRebaseInfo=" << _endOfRebaseInfo << "\n"
|
|
|
|
<< " startOfBindingInfo=" << _startOfBindingInfo << "\n"
|
|
|
|
<< " endOfBindingInfo=" << _endOfBindingInfo << "\n"
|
|
|
|
<< " startOfLazyBindingInfo=" << _startOfLazyBindingInfo << "\n"
|
|
|
|
<< " endOfLazyBindingInfo=" << _endOfLazyBindingInfo << "\n"
|
2014-09-04 03:52:50 +08:00
|
|
|
<< " startOfExportTrie=" << _startOfExportTrie << "\n"
|
|
|
|
<< " endOfExportTrie=" << _endOfExportTrie << "\n"
|
2016-02-09 09:38:13 +08:00
|
|
|
<< " startOfFunctionStarts=" << _startOfFunctionStarts << "\n"
|
2014-07-25 07:06:56 +08:00
|
|
|
<< " startOfDataInCode=" << _startOfDataInCode << "\n"
|
2013-11-07 05:36:55 +08:00
|
|
|
<< " startOfSymbols=" << _startOfSymbols << "\n"
|
|
|
|
<< " startOfSymbolStrings=" << _startOfSymbolStrings << "\n"
|
|
|
|
<< " endOfSymbolStrings=" << _endOfSymbolStrings << "\n"
|
|
|
|
<< " addressOfLinkEdit=" << _addressOfLinkEdit << "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MachOFileLayout::loadCommandsSize(uint32_t &count) {
|
|
|
|
uint32_t size = 0;
|
|
|
|
count = 0;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
|
|
|
const size_t segCommandSize =
|
2013-11-07 05:36:55 +08:00
|
|
|
(_is64 ? sizeof(segment_command_64) : sizeof(segment_command));
|
|
|
|
const size_t sectionSize = (_is64 ? sizeof(section_64) : sizeof(section));
|
|
|
|
|
|
|
|
// Add LC_SEGMENT for each segment.
|
|
|
|
size += _file.segments.size() * segCommandSize;
|
|
|
|
count += _file.segments.size();
|
|
|
|
// Add section record for each section.
|
|
|
|
size += _file.sections.size() * sectionSize;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-07-01 16:15:41 +08:00
|
|
|
// If creating a dylib, add LC_ID_DYLIB.
|
|
|
|
if (_file.fileType == llvm::MachO::MH_DYLIB) {
|
|
|
|
size += sizeof(dylib_command) + pointerAlign(_file.installName.size() + 1);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_DYLD_INFO
|
|
|
|
size += sizeof(dyld_info_command);
|
|
|
|
++count;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_SYMTAB
|
|
|
|
size += sizeof(symtab_command);
|
|
|
|
++count;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_DYSYMTAB
|
|
|
|
if (_file.fileType != llvm::MachO::MH_PRELOAD) {
|
|
|
|
size += sizeof(dysymtab_command);
|
|
|
|
++count;
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2016-02-04 06:28:29 +08:00
|
|
|
// If main executable add LC_LOAD_DYLINKER
|
2013-11-07 05:36:55 +08:00
|
|
|
if (_file.fileType == llvm::MachO::MH_EXECUTE) {
|
|
|
|
size += pointerAlign(sizeof(dylinker_command) + dyldPath().size()+1);
|
|
|
|
++count;
|
2016-02-04 06:28:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS,
|
|
|
|
// LC_VERSION_MIN_TVOS
|
|
|
|
if (_file.hasMinVersionLoadCommand) {
|
|
|
|
size += sizeof(version_min_command);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2016-02-04 10:45:23 +08:00
|
|
|
// Add LC_SOURCE_VERSION
|
|
|
|
size += sizeof(source_version_command);
|
|
|
|
++count;
|
|
|
|
|
2016-02-04 06:28:29 +08:00
|
|
|
// If main executable add LC_MAIN
|
|
|
|
if (_file.fileType == llvm::MachO::MH_EXECUTE) {
|
2013-11-07 05:36:55 +08:00
|
|
|
size += sizeof(entry_point_command);
|
|
|
|
++count;
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_LOAD_DYLIB for each dependent dylib.
|
|
|
|
for (const DependentDylib &dep : _file.dependentDylibs) {
|
|
|
|
size += sizeof(dylib_command) + pointerAlign(dep.path.size()+1);
|
|
|
|
++count;
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-12-19 05:33:38 +08:00
|
|
|
// Add LC_RPATH
|
|
|
|
for (const StringRef &path : _file.rpaths) {
|
2015-10-30 00:50:26 +08:00
|
|
|
size += pointerAlign(sizeof(rpath_command) + path.size() + 1);
|
2014-12-19 05:33:38 +08:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
// Add LC_FUNCTION_STARTS if needed
|
|
|
|
if (!_file.functionStarts.empty()) {
|
|
|
|
size += sizeof(linkedit_data_command);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2016-02-09 10:10:39 +08:00
|
|
|
// Add LC_DATA_IN_CODE if requested. Note, we do encode zero length entries.
|
|
|
|
// FIXME: Zero length entries is only to match ld64. Should we change this?
|
|
|
|
if (_file.generateDataInCodeLoadCommand) {
|
2014-10-29 06:21:10 +08:00
|
|
|
size += sizeof(linkedit_data_command);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool overlaps(const Segment &s1, const Segment &s2) {
|
|
|
|
if (s2.address >= s1.address+s1.size)
|
|
|
|
return false;
|
|
|
|
if (s1.address >= s2.address+s2.size)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool overlaps(const Section &s1, const Section &s2) {
|
|
|
|
if (s2.address >= s1.address+s1.content.size())
|
|
|
|
return false;
|
|
|
|
if (s1.address >= s2.address+s2.content.size())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::buildFileOffsets() {
|
|
|
|
// Verify no segments overlap
|
|
|
|
for (const Segment &sg1 : _file.segments) {
|
|
|
|
for (const Segment &sg2 : _file.segments) {
|
|
|
|
if (&sg1 == &sg2)
|
|
|
|
continue;
|
|
|
|
if (overlaps(sg1,sg2)) {
|
2014-06-14 01:20:48 +08:00
|
|
|
_ec = make_error_code(llvm::errc::executable_format_error);
|
2013-11-07 05:36:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
|
|
|
// Verify no sections overlap
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const Section &s1 : _file.sections) {
|
|
|
|
for (const Section &s2 : _file.sections) {
|
|
|
|
if (&s1 == &s2)
|
|
|
|
continue;
|
|
|
|
if (overlaps(s1,s2)) {
|
2014-06-14 01:20:48 +08:00
|
|
|
_ec = make_error_code(llvm::errc::executable_format_error);
|
2013-11-07 05:36:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Build side table of extra info about segments and sections.
|
|
|
|
SegExtraInfo t;
|
|
|
|
t.fileOffset = 0;
|
|
|
|
for (const Segment &sg : _file.segments) {
|
|
|
|
_segInfo[&sg] = t;
|
|
|
|
}
|
|
|
|
SectionExtraInfo t2;
|
|
|
|
t2.fileOffset = 0;
|
|
|
|
// Assign sections to segments.
|
|
|
|
for (const Section &s : _file.sections) {
|
|
|
|
_sectInfo[&s] = t2;
|
2014-09-10 07:52:59 +08:00
|
|
|
bool foundSegment = false;
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const Segment &sg : _file.segments) {
|
2014-09-10 07:52:59 +08:00
|
|
|
if (sg.name.equals(s.segmentName)) {
|
|
|
|
if ((s.address >= sg.address)
|
2013-11-07 05:36:55 +08:00
|
|
|
&& (s.address+s.content.size() <= sg.address+sg.size)) {
|
2014-09-10 07:52:59 +08:00
|
|
|
_segInfo[&sg].sections.push_back(&s);
|
|
|
|
foundSegment = true;
|
|
|
|
break;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 07:52:59 +08:00
|
|
|
if (!foundSegment) {
|
|
|
|
_ec = make_error_code(llvm::errc::executable_format_error);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Assign file offsets.
|
|
|
|
uint32_t fileOffset = 0;
|
2014-01-27 11:09:26 +08:00
|
|
|
DEBUG_WITH_TYPE("MachOFileLayout",
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::dbgs() << "buildFileOffsets()\n");
|
|
|
|
for (const Segment &sg : _file.segments) {
|
2014-06-30 17:49:30 +08:00
|
|
|
_segInfo[&sg].fileOffset = fileOffset;
|
2016-02-06 08:51:16 +08:00
|
|
|
if ((_seg1addr == INT64_MAX) && sg.init_access)
|
2013-11-07 05:36:55 +08:00
|
|
|
_seg1addr = sg.address;
|
2014-01-27 11:09:26 +08:00
|
|
|
DEBUG_WITH_TYPE("MachOFileLayout",
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::dbgs() << " segment=" << sg.name
|
|
|
|
<< ", fileOffset=" << _segInfo[&sg].fileOffset << "\n");
|
2014-06-30 17:49:30 +08:00
|
|
|
|
|
|
|
uint32_t segFileSize = 0;
|
2014-10-25 06:19:22 +08:00
|
|
|
// A segment that is not zero-fill must use a least one page of disk space.
|
2016-02-06 08:51:16 +08:00
|
|
|
if (sg.init_access)
|
2014-10-25 06:19:22 +08:00
|
|
|
segFileSize = _file.pageSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const Section *s : _segInfo[&sg].sections) {
|
2014-06-30 17:49:30 +08:00
|
|
|
uint32_t sectOffset = s->address - sg.address;
|
|
|
|
uint32_t sectFileSize =
|
2015-12-12 07:25:09 +08:00
|
|
|
isZeroFillSection(s->type) ? 0 : s->content.size();
|
2014-06-30 17:49:30 +08:00
|
|
|
segFileSize = std::max(segFileSize, sectOffset + sectFileSize);
|
|
|
|
|
|
|
|
_sectInfo[s].fileOffset = _segInfo[&sg].fileOffset + sectOffset;
|
2014-01-27 11:09:26 +08:00
|
|
|
DEBUG_WITH_TYPE("MachOFileLayout",
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::dbgs() << " section=" << s->sectionName
|
|
|
|
<< ", fileOffset=" << fileOffset << "\n");
|
|
|
|
}
|
2014-06-30 17:49:30 +08:00
|
|
|
|
2016-02-06 08:14:15 +08:00
|
|
|
// round up all segments to page aligned, except __LINKEDIT
|
|
|
|
if (!sg.name.equals("__LINKEDIT")) {
|
|
|
|
_segInfo[&sg].fileSize = llvm::alignTo(segFileSize, _file.pageSize);
|
|
|
|
fileOffset = llvm::alignTo(fileOffset + segFileSize, _file.pageSize);
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
_addressOfLinkEdit = sg.address + sg.size;
|
|
|
|
}
|
2014-06-30 17:49:30 +08:00
|
|
|
_startOfLinkEdit = fileOffset;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t MachOFileLayout::size() const {
|
|
|
|
return _endOfSymbolStrings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::writeMachHeader() {
|
2016-02-05 04:43:43 +08:00
|
|
|
auto cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(_file.arch);
|
|
|
|
// dynamic x86 executables on newer OS version should also set the
|
|
|
|
// CPU_SUBTYPE_LIB64 mask in the CPU subtype.
|
|
|
|
// FIXME: Check that this is a dynamic executable, not a static one.
|
|
|
|
if (_file.fileType == llvm::MachO::MH_EXECUTE &&
|
|
|
|
cpusubtype == CPU_SUBTYPE_X86_64_ALL &&
|
|
|
|
_file.os == MachOLinkingContext::OS::macOSX) {
|
|
|
|
uint32_t version;
|
|
|
|
bool failed = MachOLinkingContext::parsePackedVersion("10.5", version);
|
|
|
|
if (!failed && _file.minOSverson >= version)
|
|
|
|
cpusubtype |= CPU_SUBTYPE_LIB64;
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
mach_header *mh = reinterpret_cast<mach_header*>(_buffer);
|
|
|
|
mh->magic = _is64 ? llvm::MachO::MH_MAGIC_64 : llvm::MachO::MH_MAGIC;
|
|
|
|
mh->cputype = MachOLinkingContext::cpuTypeFromArch(_file.arch);
|
2016-02-05 04:43:43 +08:00
|
|
|
mh->cpusubtype = cpusubtype;
|
2013-11-07 05:36:55 +08:00
|
|
|
mh->filetype = _file.fileType;
|
|
|
|
mh->ncmds = _countOfLoadCommands;
|
|
|
|
mh->sizeofcmds = _endOfLoadCommands - _startOfLoadCommands;
|
|
|
|
mh->flags = _file.flags;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*mh);
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
uint32_t MachOFileLayout::indirectSymbolIndex(const Section §,
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t &index) {
|
|
|
|
if (sect.indirectSymbols.empty())
|
|
|
|
return 0;
|
|
|
|
uint32_t result = index;
|
|
|
|
index += sect.indirectSymbols.size();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MachOFileLayout::indirectSymbolElementSize(const Section §) {
|
|
|
|
if (sect.indirectSymbols.empty())
|
|
|
|
return 0;
|
|
|
|
if (sect.type != S_SYMBOL_STUBS)
|
|
|
|
return 0;
|
|
|
|
return sect.content.size() / sect.indirectSymbols.size();
|
|
|
|
}
|
|
|
|
|
2013-11-09 08:07:28 +08:00
|
|
|
template <typename T>
|
2016-03-31 08:08:16 +08:00
|
|
|
llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
|
2013-11-09 08:07:28 +08:00
|
|
|
typename T::command* seg = reinterpret_cast<typename T::command*>(lc);
|
|
|
|
seg->cmd = T::LC;
|
2014-01-27 11:09:26 +08:00
|
|
|
seg->cmdsize = sizeof(typename T::command)
|
2013-11-09 08:07:28 +08:00
|
|
|
+ _file.sections.size() * sizeof(typename T::section);
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t *next = lc + seg->cmdsize;
|
|
|
|
memset(seg->segname, 0, 16);
|
|
|
|
seg->vmaddr = 0;
|
2014-11-18 08:30:29 +08:00
|
|
|
seg->vmsize = _file.sections.back().address
|
|
|
|
+ _file.sections.back().content.size();
|
2013-11-07 05:36:55 +08:00
|
|
|
seg->fileoff = _endOfLoadCommands;
|
|
|
|
seg->filesize = seg->vmsize;
|
|
|
|
seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
|
|
|
|
seg->initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
|
|
|
|
seg->nsects = _file.sections.size();
|
|
|
|
seg->flags = 0;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*seg);
|
2013-11-09 08:07:28 +08:00
|
|
|
typename T::section *sout = reinterpret_cast<typename T::section*>
|
|
|
|
(lc+sizeof(typename T::command));
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t relOffset = _startOfRelocations;
|
|
|
|
uint32_t indirectSymRunningIndex = 0;
|
|
|
|
for (const Section &sin : _file.sections) {
|
|
|
|
setString16(sin.sectionName, sout->sectname);
|
|
|
|
setString16(sin.segmentName, sout->segname);
|
|
|
|
sout->addr = sin.address;
|
|
|
|
sout->size = sin.content.size();
|
2014-11-18 08:30:29 +08:00
|
|
|
sout->offset = _sectInfo[&sin].fileOffset;
|
2015-03-26 10:03:44 +08:00
|
|
|
sout->align = llvm::Log2_32(sin.alignment);
|
2013-11-07 05:36:55 +08:00
|
|
|
sout->reloff = sin.relocations.empty() ? 0 : relOffset;
|
|
|
|
sout->nreloc = sin.relocations.size();
|
|
|
|
sout->flags = sin.type | sin.attributes;
|
|
|
|
sout->reserved1 = indirectSymbolIndex(sin, indirectSymRunningIndex);
|
|
|
|
sout->reserved2 = indirectSymbolElementSize(sin);
|
|
|
|
relOffset += sin.relocations.size() * sizeof(any_relocation_info);
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*sout);
|
|
|
|
++sout;
|
|
|
|
}
|
|
|
|
lc = next;
|
2016-03-31 08:08:16 +08:00
|
|
|
return llvm::Error();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2013-11-09 08:07:28 +08:00
|
|
|
template <typename T>
|
2016-03-31 08:08:16 +08:00
|
|
|
llvm::Error MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t indirectSymRunningIndex = 0;
|
|
|
|
for (const Segment &seg : _file.segments) {
|
2016-02-06 08:14:15 +08:00
|
|
|
// Link edit has no sections and a custom range of address, so handle it
|
|
|
|
// specially.
|
2013-11-07 05:36:55 +08:00
|
|
|
SegExtraInfo &segInfo = _segInfo[&seg];
|
2016-02-06 08:14:15 +08:00
|
|
|
if (seg.name.equals("__LINKEDIT")) {
|
|
|
|
size_t linkeditSize = _endOfLinkEdit - _startOfLinkEdit;
|
|
|
|
typename T::command* cmd = reinterpret_cast<typename T::command*>(lc);
|
|
|
|
cmd->cmd = T::LC;
|
|
|
|
cmd->cmdsize = sizeof(typename T::command);
|
|
|
|
uint8_t *next = lc + cmd->cmdsize;
|
|
|
|
setString16("__LINKEDIT", cmd->segname);
|
|
|
|
cmd->vmaddr = _addressOfLinkEdit;
|
|
|
|
cmd->vmsize = llvm::alignTo(linkeditSize, _file.pageSize);
|
|
|
|
cmd->fileoff = _startOfLinkEdit;
|
|
|
|
cmd->filesize = linkeditSize;
|
2016-02-06 08:51:16 +08:00
|
|
|
cmd->initprot = seg.init_access;
|
|
|
|
cmd->maxprot = seg.max_access;
|
2016-02-06 08:14:15 +08:00
|
|
|
cmd->nsects = 0;
|
|
|
|
cmd->flags = 0;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*cmd);
|
|
|
|
lc = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Write segment command with trailing sections.
|
2013-11-09 08:07:28 +08:00
|
|
|
typename T::command* cmd = reinterpret_cast<typename T::command*>(lc);
|
|
|
|
cmd->cmd = T::LC;
|
2014-01-27 11:09:26 +08:00
|
|
|
cmd->cmdsize = sizeof(typename T::command)
|
2013-11-09 08:07:28 +08:00
|
|
|
+ segInfo.sections.size() * sizeof(typename T::section);
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t *next = lc + cmd->cmdsize;
|
|
|
|
setString16(seg.name, cmd->segname);
|
|
|
|
cmd->vmaddr = seg.address;
|
|
|
|
cmd->vmsize = seg.size;
|
|
|
|
cmd->fileoff = segInfo.fileOffset;
|
2014-06-30 17:49:30 +08:00
|
|
|
cmd->filesize = segInfo.fileSize;
|
2016-02-06 08:51:16 +08:00
|
|
|
cmd->initprot = seg.init_access;
|
|
|
|
cmd->maxprot = seg.max_access;
|
2013-11-07 05:36:55 +08:00
|
|
|
cmd->nsects = segInfo.sections.size();
|
|
|
|
cmd->flags = 0;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*cmd);
|
2013-11-09 08:07:28 +08:00
|
|
|
typename T::section *sect = reinterpret_cast<typename T::section*>
|
|
|
|
(lc+sizeof(typename T::command));
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const Section *section : segInfo.sections) {
|
|
|
|
setString16(section->sectionName, sect->sectname);
|
|
|
|
setString16(section->segmentName, sect->segname);
|
|
|
|
sect->addr = section->address;
|
|
|
|
sect->size = section->content.size();
|
2015-12-12 07:25:09 +08:00
|
|
|
if (isZeroFillSection(section->type))
|
2014-11-18 08:30:29 +08:00
|
|
|
sect->offset = 0;
|
|
|
|
else
|
|
|
|
sect->offset = section->address - seg.address + segInfo.fileOffset;
|
2015-03-26 10:03:44 +08:00
|
|
|
sect->align = llvm::Log2_32(section->alignment);
|
2013-11-07 05:36:55 +08:00
|
|
|
sect->reloff = 0;
|
|
|
|
sect->nreloc = 0;
|
|
|
|
sect->flags = section->type | section->attributes;
|
|
|
|
sect->reserved1 = indirectSymbolIndex(*section, indirectSymRunningIndex);
|
|
|
|
sect->reserved2 = indirectSymbolElementSize(*section);
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*sect);
|
|
|
|
++sect;
|
2014-01-27 11:09:26 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
lc = reinterpret_cast<uint8_t*>(next);
|
|
|
|
}
|
2016-03-31 08:08:16 +08:00
|
|
|
return llvm::Error();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 10:16:08 +08:00
|
|
|
static void writeVersionMinLoadCommand(const NormalizedFile &_file,
|
|
|
|
bool _swap,
|
|
|
|
uint8_t *&lc) {
|
|
|
|
if (!_file.hasMinVersionLoadCommand)
|
|
|
|
return;
|
|
|
|
version_min_command *vm = reinterpret_cast<version_min_command*>(lc);
|
|
|
|
switch (_file.os) {
|
|
|
|
case MachOLinkingContext::OS::unknown:
|
|
|
|
vm->cmd = _file.minOSVersionKind;
|
|
|
|
vm->cmdsize = sizeof(version_min_command);
|
|
|
|
vm->version = _file.minOSverson;
|
|
|
|
vm->sdk = 0;
|
|
|
|
break;
|
|
|
|
case MachOLinkingContext::OS::macOSX:
|
|
|
|
vm->cmd = LC_VERSION_MIN_MACOSX;
|
|
|
|
vm->cmdsize = sizeof(version_min_command);
|
|
|
|
vm->version = _file.minOSverson;
|
|
|
|
vm->sdk = _file.sdkVersion;
|
|
|
|
break;
|
|
|
|
case MachOLinkingContext::OS::iOS:
|
|
|
|
case MachOLinkingContext::OS::iOS_simulator:
|
|
|
|
vm->cmd = LC_VERSION_MIN_IPHONEOS;
|
|
|
|
vm->cmdsize = sizeof(version_min_command);
|
|
|
|
vm->version = _file.minOSverson;
|
|
|
|
vm->sdk = _file.sdkVersion;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*vm);
|
|
|
|
lc += sizeof(version_min_command);
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:08:16 +08:00
|
|
|
llvm::Error MachOFileLayout::writeLoadCommands() {
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t *lc = &_buffer[_startOfLoadCommands];
|
|
|
|
if (_file.fileType == llvm::MachO::MH_OBJECT) {
|
|
|
|
// Object files have one unnamed segment which holds all sections.
|
2016-03-31 08:08:16 +08:00
|
|
|
if (_is64) {
|
|
|
|
if (auto ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc))
|
2016-03-31 08:35:50 +08:00
|
|
|
return ec;
|
2016-03-31 08:08:16 +08:00
|
|
|
} else {
|
|
|
|
if (auto ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc))
|
2016-03-31 08:38:02 +08:00
|
|
|
return ec;
|
2016-03-31 08:08:16 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_SYMTAB with symbol table info
|
|
|
|
symtab_command* st = reinterpret_cast<symtab_command*>(lc);
|
|
|
|
st->cmd = LC_SYMTAB;
|
|
|
|
st->cmdsize = sizeof(symtab_command);
|
|
|
|
st->symoff = _startOfSymbols;
|
2014-01-27 11:09:26 +08:00
|
|
|
st->nsyms = _file.localSymbols.size() + _file.globalSymbols.size()
|
2013-11-07 05:36:55 +08:00
|
|
|
+ _file.undefinedSymbols.size();
|
|
|
|
st->stroff = _startOfSymbolStrings;
|
|
|
|
st->strsize = _endOfSymbolStrings - _startOfSymbolStrings;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*st);
|
2014-07-25 07:06:56 +08:00
|
|
|
lc += sizeof(symtab_command);
|
2016-02-04 10:16:08 +08:00
|
|
|
|
|
|
|
// Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
|
|
|
|
// LC_VERSION_MIN_WATCHOS, LC_VERSION_MIN_TVOS
|
|
|
|
writeVersionMinLoadCommand(_file, _swap, lc);
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
// Add LC_FUNCTION_STARTS if needed.
|
|
|
|
if (_functionStartsSize != 0) {
|
|
|
|
linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
|
|
|
|
dl->cmd = LC_FUNCTION_STARTS;
|
|
|
|
dl->cmdsize = sizeof(linkedit_data_command);
|
|
|
|
dl->dataoff = _startOfFunctionStarts;
|
|
|
|
dl->datasize = _functionStartsSize;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dl);
|
|
|
|
lc += sizeof(linkedit_data_command);
|
|
|
|
}
|
|
|
|
|
2016-02-09 10:10:39 +08:00
|
|
|
// Add LC_DATA_IN_CODE if requested.
|
|
|
|
if (_file.generateDataInCodeLoadCommand) {
|
2014-07-25 07:06:56 +08:00
|
|
|
linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
|
|
|
|
dl->cmd = LC_DATA_IN_CODE;
|
|
|
|
dl->cmdsize = sizeof(linkedit_data_command);
|
|
|
|
dl->dataoff = _startOfDataInCode;
|
|
|
|
dl->datasize = _dataInCodeSize;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dl);
|
|
|
|
lc += sizeof(linkedit_data_command);
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
} else {
|
|
|
|
// Final linked images have sections under segments.
|
2016-03-31 08:08:16 +08:00
|
|
|
if (_is64) {
|
|
|
|
if (auto ec = writeSegmentLoadCommands<MachO64Trait>(lc))
|
2016-03-31 08:38:02 +08:00
|
|
|
return ec;
|
2016-03-31 08:08:16 +08:00
|
|
|
} else {
|
|
|
|
if (auto ec = writeSegmentLoadCommands<MachO32Trait>(lc))
|
2016-03-31 08:38:02 +08:00
|
|
|
return ec;
|
2016-03-31 08:08:16 +08:00
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-07-01 16:15:41 +08:00
|
|
|
// Add LC_ID_DYLIB command for dynamic libraries.
|
|
|
|
if (_file.fileType == llvm::MachO::MH_DYLIB) {
|
|
|
|
dylib_command *dc = reinterpret_cast<dylib_command*>(lc);
|
|
|
|
StringRef path = _file.installName;
|
|
|
|
uint32_t size = sizeof(dylib_command) + pointerAlign(path.size() + 1);
|
|
|
|
dc->cmd = LC_ID_DYLIB;
|
|
|
|
dc->cmdsize = size;
|
|
|
|
dc->dylib.name = sizeof(dylib_command); // offset
|
2014-12-20 17:22:56 +08:00
|
|
|
// needs to be some constant value different than the one in LC_LOAD_DYLIB
|
|
|
|
dc->dylib.timestamp = 1;
|
2014-11-19 10:21:53 +08:00
|
|
|
dc->dylib.current_version = _file.currentVersion;
|
|
|
|
dc->dylib.compatibility_version = _file.compatVersion;
|
2014-07-01 16:15:41 +08:00
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dc);
|
|
|
|
memcpy(lc + sizeof(dylib_command), path.begin(), path.size());
|
|
|
|
lc[sizeof(dylib_command) + path.size()] = '\0';
|
|
|
|
lc += size;
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_DYLD_INFO_ONLY.
|
|
|
|
dyld_info_command* di = reinterpret_cast<dyld_info_command*>(lc);
|
|
|
|
di->cmd = LC_DYLD_INFO_ONLY;
|
|
|
|
di->cmdsize = sizeof(dyld_info_command);
|
|
|
|
di->rebase_off = _rebaseInfo.size() ? _startOfRebaseInfo : 0;
|
|
|
|
di->rebase_size = _rebaseInfo.size();
|
|
|
|
di->bind_off = _bindingInfo.size() ? _startOfBindingInfo : 0;
|
|
|
|
di->bind_size = _bindingInfo.size();
|
|
|
|
di->weak_bind_off = 0;
|
|
|
|
di->weak_bind_size = 0;
|
|
|
|
di->lazy_bind_off = _lazyBindingInfo.size() ? _startOfLazyBindingInfo : 0;
|
|
|
|
di->lazy_bind_size = _lazyBindingInfo.size();
|
2014-09-04 03:52:50 +08:00
|
|
|
di->export_off = _exportTrie.size() ? _startOfExportTrie : 0;
|
|
|
|
di->export_size = _exportTrie.size();
|
2013-11-07 05:36:55 +08:00
|
|
|
if (_swap)
|
|
|
|
swapStruct(*di);
|
|
|
|
lc += sizeof(dyld_info_command);
|
|
|
|
|
|
|
|
// Add LC_SYMTAB with symbol table info.
|
|
|
|
symtab_command* st = reinterpret_cast<symtab_command*>(lc);
|
|
|
|
st->cmd = LC_SYMTAB;
|
|
|
|
st->cmdsize = sizeof(symtab_command);
|
|
|
|
st->symoff = _startOfSymbols;
|
2014-01-27 11:09:26 +08:00
|
|
|
st->nsyms = _file.localSymbols.size() + _file.globalSymbols.size()
|
2013-11-07 05:36:55 +08:00
|
|
|
+ _file.undefinedSymbols.size();
|
|
|
|
st->stroff = _startOfSymbolStrings;
|
|
|
|
st->strsize = _endOfSymbolStrings - _startOfSymbolStrings;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*st);
|
|
|
|
lc += sizeof(symtab_command);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_DYSYMTAB
|
|
|
|
if (_file.fileType != llvm::MachO::MH_PRELOAD) {
|
|
|
|
dysymtab_command* dst = reinterpret_cast<dysymtab_command*>(lc);
|
|
|
|
dst->cmd = LC_DYSYMTAB;
|
|
|
|
dst->cmdsize = sizeof(dysymtab_command);
|
|
|
|
dst->ilocalsym = _symbolTableLocalsStartIndex;
|
|
|
|
dst->nlocalsym = _file.localSymbols.size();
|
|
|
|
dst->iextdefsym = _symbolTableGlobalsStartIndex;
|
|
|
|
dst->nextdefsym = _file.globalSymbols.size();
|
|
|
|
dst->iundefsym = _symbolTableUndefinesStartIndex;
|
|
|
|
dst->nundefsym = _file.undefinedSymbols.size();
|
|
|
|
dst->tocoff = 0;
|
|
|
|
dst->ntoc = 0;
|
|
|
|
dst->modtaboff = 0;
|
|
|
|
dst->nmodtab = 0;
|
|
|
|
dst->extrefsymoff = 0;
|
|
|
|
dst->nextrefsyms = 0;
|
2014-01-27 11:09:26 +08:00
|
|
|
dst->indirectsymoff = _startOfIndirectSymbols;
|
2013-11-07 05:36:55 +08:00
|
|
|
dst->nindirectsyms = _indirectSymbolTableCount;
|
|
|
|
dst->extreloff = 0;
|
|
|
|
dst->nextrel = 0;
|
|
|
|
dst->locreloff = 0;
|
|
|
|
dst->nlocrel = 0;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dst);
|
|
|
|
lc += sizeof(dysymtab_command);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2016-02-04 06:28:29 +08:00
|
|
|
// If main executable, add LC_LOAD_DYLINKER
|
2013-11-07 05:36:55 +08:00
|
|
|
if (_file.fileType == llvm::MachO::MH_EXECUTE) {
|
|
|
|
// Build LC_LOAD_DYLINKER load command.
|
|
|
|
uint32_t size=pointerAlign(sizeof(dylinker_command)+dyldPath().size()+1);
|
|
|
|
dylinker_command* dl = reinterpret_cast<dylinker_command*>(lc);
|
|
|
|
dl->cmd = LC_LOAD_DYLINKER;
|
|
|
|
dl->cmdsize = size;
|
|
|
|
dl->name = sizeof(dylinker_command); // offset
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dl);
|
|
|
|
memcpy(lc+sizeof(dylinker_command), dyldPath().data(), dyldPath().size());
|
|
|
|
lc[sizeof(dylinker_command)+dyldPath().size()] = '\0';
|
|
|
|
lc += size;
|
2016-02-04 06:28:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS,
|
|
|
|
// LC_VERSION_MIN_TVOS
|
2016-02-04 10:16:08 +08:00
|
|
|
writeVersionMinLoadCommand(_file, _swap, lc);
|
2016-02-04 06:28:29 +08:00
|
|
|
|
2016-02-04 10:45:23 +08:00
|
|
|
// Add LC_SOURCE_VERSION
|
|
|
|
{
|
2016-03-24 06:00:09 +08:00
|
|
|
// Note, using a temporary here to appease UB as we may not be aligned
|
|
|
|
// enough for a struct containing a uint64_t when emitting a 32-bit binary
|
|
|
|
source_version_command sv;
|
|
|
|
sv.cmd = LC_SOURCE_VERSION;
|
|
|
|
sv.cmdsize = sizeof(source_version_command);
|
|
|
|
sv.version = _file.sourceVersion;
|
2016-02-04 10:45:23 +08:00
|
|
|
if (_swap)
|
2016-03-24 06:00:09 +08:00
|
|
|
swapStruct(sv);
|
|
|
|
memcpy(lc, &sv, sizeof(source_version_command));
|
2016-02-04 10:45:23 +08:00
|
|
|
lc += sizeof(source_version_command);
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:28:29 +08:00
|
|
|
// If main executable, add LC_MAIN.
|
|
|
|
if (_file.fileType == llvm::MachO::MH_EXECUTE) {
|
2013-11-07 05:36:55 +08:00
|
|
|
// Build LC_MAIN load command.
|
2016-03-24 09:05:17 +08:00
|
|
|
// Note, using a temporary here to appease UB as we may not be aligned
|
|
|
|
// enough for a struct containing a uint64_t when emitting a 32-bit binary
|
|
|
|
entry_point_command ep;
|
|
|
|
ep.cmd = LC_MAIN;
|
|
|
|
ep.cmdsize = sizeof(entry_point_command);
|
|
|
|
ep.entryoff = _file.entryAddress - _seg1addr;
|
|
|
|
ep.stacksize = _file.stackSize;
|
2013-11-07 05:36:55 +08:00
|
|
|
if (_swap)
|
2016-03-24 09:05:17 +08:00
|
|
|
swapStruct(ep);
|
|
|
|
memcpy(lc, &ep, sizeof(entry_point_command));
|
2013-11-07 05:36:55 +08:00
|
|
|
lc += sizeof(entry_point_command);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Add LC_LOAD_DYLIB commands
|
|
|
|
for (const DependentDylib &dep : _file.dependentDylibs) {
|
|
|
|
dylib_command* dc = reinterpret_cast<dylib_command*>(lc);
|
|
|
|
uint32_t size = sizeof(dylib_command) + pointerAlign(dep.path.size()+1);
|
2014-10-17 03:31:28 +08:00
|
|
|
dc->cmd = dep.kind;
|
2013-11-07 05:36:55 +08:00
|
|
|
dc->cmdsize = size;
|
|
|
|
dc->dylib.name = sizeof(dylib_command); // offset
|
2014-12-20 17:22:56 +08:00
|
|
|
// needs to be some constant value different than the one in LC_ID_DYLIB
|
2014-11-19 10:21:53 +08:00
|
|
|
dc->dylib.timestamp = 2;
|
|
|
|
dc->dylib.current_version = dep.currentVersion;
|
|
|
|
dc->dylib.compatibility_version = dep.compatVersion;
|
2013-11-07 05:36:55 +08:00
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dc);
|
|
|
|
memcpy(lc+sizeof(dylib_command), dep.path.begin(), dep.path.size());
|
|
|
|
lc[sizeof(dylib_command)+dep.path.size()] = '\0';
|
|
|
|
lc += size;
|
|
|
|
}
|
2014-12-19 05:33:38 +08:00
|
|
|
|
|
|
|
// Add LC_RPATH
|
|
|
|
for (const StringRef &path : _file.rpaths) {
|
|
|
|
rpath_command *rpc = reinterpret_cast<rpath_command *>(lc);
|
2015-10-30 00:50:26 +08:00
|
|
|
uint32_t size = pointerAlign(sizeof(rpath_command) + path.size() + 1);
|
2014-12-19 05:33:38 +08:00
|
|
|
rpc->cmd = LC_RPATH;
|
|
|
|
rpc->cmdsize = size;
|
|
|
|
rpc->path = sizeof(rpath_command); // offset
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*rpc);
|
|
|
|
memcpy(lc+sizeof(rpath_command), path.begin(), path.size());
|
|
|
|
lc[sizeof(rpath_command)+path.size()] = '\0';
|
|
|
|
lc += size;
|
|
|
|
}
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
// Add LC_FUNCTION_STARTS if needed.
|
|
|
|
if (_functionStartsSize != 0) {
|
|
|
|
linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
|
|
|
|
dl->cmd = LC_FUNCTION_STARTS;
|
|
|
|
dl->cmdsize = sizeof(linkedit_data_command);
|
|
|
|
dl->dataoff = _startOfFunctionStarts;
|
|
|
|
dl->datasize = _functionStartsSize;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dl);
|
|
|
|
lc += sizeof(linkedit_data_command);
|
|
|
|
}
|
|
|
|
|
2016-02-09 10:10:39 +08:00
|
|
|
// Add LC_DATA_IN_CODE if requested.
|
|
|
|
if (_file.generateDataInCodeLoadCommand) {
|
2014-10-29 06:21:10 +08:00
|
|
|
linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
|
|
|
|
dl->cmd = LC_DATA_IN_CODE;
|
|
|
|
dl->cmdsize = sizeof(linkedit_data_command);
|
|
|
|
dl->dataoff = _startOfDataInCode;
|
|
|
|
dl->datasize = _dataInCodeSize;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dl);
|
|
|
|
lc += sizeof(linkedit_data_command);
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2016-03-31 08:08:16 +08:00
|
|
|
return llvm::Error();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::writeSectionContent() {
|
|
|
|
for (const Section &s : _file.sections) {
|
|
|
|
// Copy all section content to output buffer.
|
2015-12-12 07:25:09 +08:00
|
|
|
if (isZeroFillSection(s.type))
|
2014-05-16 04:59:23 +08:00
|
|
|
continue;
|
2014-09-10 07:52:59 +08:00
|
|
|
if (s.content.empty())
|
|
|
|
continue;
|
2013-11-07 05:36:55 +08:00
|
|
|
uint32_t offset = _sectInfo[&s].fileOffset;
|
|
|
|
uint8_t *p = &_buffer[offset];
|
|
|
|
memcpy(p, &s.content[0], s.content.size());
|
|
|
|
p += s.content.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::writeRelocations() {
|
|
|
|
uint32_t relOffset = _startOfRelocations;
|
|
|
|
for (Section sect : _file.sections) {
|
|
|
|
for (Relocation r : sect.relocations) {
|
|
|
|
any_relocation_info* rb = reinterpret_cast<any_relocation_info*>(
|
|
|
|
&_buffer[relOffset]);
|
|
|
|
*rb = packRelocation(r, _swap, _bigEndianArch);
|
|
|
|
relOffset += sizeof(any_relocation_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::appendSymbols(const std::vector<Symbol> &symbols,
|
|
|
|
uint32_t &symOffset, uint32_t &strOffset) {
|
|
|
|
for (const Symbol &sym : symbols) {
|
|
|
|
if (_is64) {
|
|
|
|
nlist_64* nb = reinterpret_cast<nlist_64*>(&_buffer[symOffset]);
|
|
|
|
nb->n_strx = strOffset - _startOfSymbolStrings;
|
|
|
|
nb->n_type = sym.type | sym.scope;
|
|
|
|
nb->n_sect = sym.sect;
|
|
|
|
nb->n_desc = sym.desc;
|
|
|
|
nb->n_value = sym.value;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*nb);
|
|
|
|
symOffset += sizeof(nlist_64);
|
|
|
|
} else {
|
|
|
|
nlist* nb = reinterpret_cast<nlist*>(&_buffer[symOffset]);
|
|
|
|
nb->n_strx = strOffset - _startOfSymbolStrings;
|
|
|
|
nb->n_type = sym.type | sym.scope;
|
|
|
|
nb->n_sect = sym.sect;
|
|
|
|
nb->n_desc = sym.desc;
|
|
|
|
nb->n_value = sym.value;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*nb);
|
|
|
|
symOffset += sizeof(nlist);
|
|
|
|
}
|
|
|
|
memcpy(&_buffer[strOffset], sym.name.begin(), sym.name.size());
|
|
|
|
strOffset += sym.name.size();
|
|
|
|
_buffer[strOffset++] ='\0'; // Strings in table have nul terminator.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
void MachOFileLayout::writeFunctionStartsInfo() {
|
2016-03-23 06:51:03 +08:00
|
|
|
if (!_functionStartsSize)
|
|
|
|
return;
|
2016-02-09 09:38:13 +08:00
|
|
|
memcpy(&_buffer[_startOfFunctionStarts], _file.functionStarts.data(),
|
|
|
|
_functionStartsSize);
|
|
|
|
}
|
|
|
|
|
2014-07-25 07:06:56 +08:00
|
|
|
void MachOFileLayout::writeDataInCodeInfo() {
|
|
|
|
uint32_t offset = _startOfDataInCode;
|
|
|
|
for (const DataInCode &entry : _file.dataInCode) {
|
|
|
|
data_in_code_entry *dst = reinterpret_cast<data_in_code_entry*>(
|
|
|
|
&_buffer[offset]);
|
|
|
|
dst->offset = entry.offset;
|
|
|
|
dst->length = entry.length;
|
|
|
|
dst->kind = entry.kind;
|
|
|
|
if (_swap)
|
|
|
|
swapStruct(*dst);
|
|
|
|
offset += sizeof(data_in_code_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
void MachOFileLayout::writeSymbolTable() {
|
|
|
|
// Write symbol table and symbol strings in parallel.
|
|
|
|
uint32_t symOffset = _startOfSymbols;
|
|
|
|
uint32_t strOffset = _startOfSymbolStrings;
|
|
|
|
_buffer[strOffset++] = '\0'; // Reserve n_strx offset of zero to mean no name.
|
|
|
|
appendSymbols(_file.localSymbols, symOffset, strOffset);
|
|
|
|
appendSymbols(_file.globalSymbols, symOffset, strOffset);
|
|
|
|
appendSymbols(_file.undefinedSymbols, symOffset, strOffset);
|
|
|
|
// Write indirect symbol table array.
|
|
|
|
uint32_t *indirects = reinterpret_cast<uint32_t*>
|
|
|
|
(&_buffer[_startOfIndirectSymbols]);
|
|
|
|
if (_file.fileType == llvm::MachO::MH_OBJECT) {
|
|
|
|
// Object files have sections in same order as input normalized file.
|
|
|
|
for (const Section §ion : _file.sections) {
|
|
|
|
for (uint32_t index : section.indirectSymbols) {
|
|
|
|
if (_swap)
|
2014-06-14 20:40:04 +08:00
|
|
|
*indirects++ = llvm::sys::getSwappedBytes(index);
|
2013-11-07 05:36:55 +08:00
|
|
|
else
|
|
|
|
*indirects++ = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Final linked images must sort sections from normalized file.
|
|
|
|
for (const Segment &seg : _file.segments) {
|
|
|
|
SegExtraInfo &segInfo = _segInfo[&seg];
|
|
|
|
for (const Section *section : segInfo.sections) {
|
|
|
|
for (uint32_t index : section->indirectSymbols) {
|
|
|
|
if (_swap)
|
2014-06-14 20:40:04 +08:00
|
|
|
*indirects++ = llvm::sys::getSwappedBytes(index);
|
2013-11-07 05:36:55 +08:00
|
|
|
else
|
|
|
|
*indirects++ = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::writeRebaseInfo() {
|
|
|
|
memcpy(&_buffer[_startOfRebaseInfo], _rebaseInfo.bytes(), _rebaseInfo.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::writeBindingInfo() {
|
2014-01-27 11:09:26 +08:00
|
|
|
memcpy(&_buffer[_startOfBindingInfo],
|
2013-11-07 05:36:55 +08:00
|
|
|
_bindingInfo.bytes(), _bindingInfo.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::writeLazyBindingInfo() {
|
2014-01-27 11:09:26 +08:00
|
|
|
memcpy(&_buffer[_startOfLazyBindingInfo],
|
2013-11-07 05:36:55 +08:00
|
|
|
_lazyBindingInfo.bytes(), _lazyBindingInfo.size());
|
|
|
|
}
|
|
|
|
|
2014-09-04 03:52:50 +08:00
|
|
|
void MachOFileLayout::writeExportInfo() {
|
|
|
|
memcpy(&_buffer[_startOfExportTrie], _exportTrie.bytes(), _exportTrie.size());
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
void MachOFileLayout::buildLinkEditInfo() {
|
|
|
|
buildRebaseInfo();
|
|
|
|
buildBindInfo();
|
|
|
|
buildLazyBindInfo();
|
2014-09-04 03:52:50 +08:00
|
|
|
buildExportTrie();
|
2013-11-07 05:36:55 +08:00
|
|
|
computeSymbolTableSizes();
|
2016-02-09 09:38:13 +08:00
|
|
|
computeFunctionStartsSize();
|
2014-07-25 07:06:56 +08:00
|
|
|
computeDataInCodeSize();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::buildSectionRelocations() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::buildRebaseInfo() {
|
|
|
|
// TODO: compress rebasing info.
|
|
|
|
for (const RebaseLocation& entry : _file.rebasingInfo) {
|
|
|
|
_rebaseInfo.append_byte(REBASE_OPCODE_SET_TYPE_IMM | entry.kind);
|
2014-01-27 11:09:26 +08:00
|
|
|
_rebaseInfo.append_byte(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
|
2013-11-07 05:36:55 +08:00
|
|
|
| entry.segIndex);
|
|
|
|
_rebaseInfo.append_uleb128(entry.segOffset);
|
|
|
|
_rebaseInfo.append_uleb128(REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
_rebaseInfo.append_byte(REBASE_OPCODE_DONE);
|
2013-11-07 05:36:55 +08:00
|
|
|
_rebaseInfo.align(_is64 ? 8 : 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::buildBindInfo() {
|
|
|
|
// TODO: compress bind info.
|
2014-11-11 09:31:18 +08:00
|
|
|
uint64_t lastAddend = 0;
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const BindLocation& entry : _file.bindingInfo) {
|
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_SET_TYPE_IMM | entry.kind);
|
2014-01-27 11:09:26 +08:00
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
|
2013-11-07 05:36:55 +08:00
|
|
|
| entry.segIndex);
|
|
|
|
_bindingInfo.append_uleb128(entry.segOffset);
|
2015-09-29 04:25:14 +08:00
|
|
|
if (entry.ordinal > 0)
|
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
|
|
|
|
(entry.ordinal & 0xF));
|
|
|
|
else
|
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
|
|
|
|
(entry.ordinal & 0xF));
|
2013-11-07 05:36:55 +08:00
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);
|
|
|
|
_bindingInfo.append_string(entry.symbolName);
|
2014-11-11 09:31:18 +08:00
|
|
|
if (entry.addend != lastAddend) {
|
2013-11-07 05:36:55 +08:00
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_SET_ADDEND_SLEB);
|
|
|
|
_bindingInfo.append_sleb128(entry.addend);
|
2014-11-11 09:31:18 +08:00
|
|
|
lastAddend = entry.addend;
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_DO_BIND);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
_bindingInfo.append_byte(BIND_OPCODE_DONE);
|
2013-11-07 05:36:55 +08:00
|
|
|
_bindingInfo.align(_is64 ? 8 : 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::buildLazyBindInfo() {
|
|
|
|
for (const BindLocation& entry : _file.lazyBindingInfo) {
|
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_SET_TYPE_IMM | entry.kind);
|
2014-01-27 11:09:26 +08:00
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
|
2013-11-07 05:36:55 +08:00
|
|
|
| entry.segIndex);
|
2014-11-11 09:31:18 +08:00
|
|
|
_lazyBindingInfo.append_uleb128Fixed(entry.segOffset, 5);
|
2015-09-29 04:25:14 +08:00
|
|
|
if (entry.ordinal > 0)
|
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
|
|
|
|
(entry.ordinal & 0xF));
|
|
|
|
else
|
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
|
|
|
|
(entry.ordinal & 0xF));
|
2013-11-07 05:36:55 +08:00
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);
|
|
|
|
_lazyBindingInfo.append_string(entry.symbolName);
|
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_DO_BIND);
|
2014-11-11 09:31:18 +08:00
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_DONE);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
_lazyBindingInfo.append_byte(BIND_OPCODE_DONE);
|
2013-11-07 05:36:55 +08:00
|
|
|
_lazyBindingInfo.align(_is64 ? 8 : 4);
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:50:54 +08:00
|
|
|
void TrieNode::addSymbol(const Export& entry,
|
|
|
|
BumpPtrAllocator &allocator,
|
|
|
|
std::vector<TrieNode*> &allNodes) {
|
2014-09-04 03:52:50 +08:00
|
|
|
StringRef partialStr = entry.name.drop_front(_cummulativeString.size());
|
|
|
|
for (TrieEdge &edge : _children) {
|
|
|
|
StringRef edgeStr = edge._subString;
|
|
|
|
if (partialStr.startswith(edgeStr)) {
|
|
|
|
// Already have matching edge, go down that path.
|
|
|
|
edge._child->addSymbol(entry, allocator, allNodes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// See if string has commmon prefix with existing edge.
|
|
|
|
for (int n=edgeStr.size()-1; n > 0; --n) {
|
|
|
|
if (partialStr.substr(0, n).equals(edgeStr.substr(0, n))) {
|
|
|
|
// Splice in new node: was A -> C, now A -> B -> C
|
|
|
|
StringRef bNodeStr = edge._child->_cummulativeString;
|
|
|
|
bNodeStr = bNodeStr.drop_back(edgeStr.size()-n).copy(allocator);
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *bNode = new (allocator) TrieNode(bNodeStr);
|
2014-09-04 03:52:50 +08:00
|
|
|
allNodes.push_back(bNode);
|
|
|
|
TrieNode* cNode = edge._child;
|
|
|
|
StringRef abEdgeStr = edgeStr.substr(0,n).copy(allocator);
|
|
|
|
StringRef bcEdgeStr = edgeStr.substr(n).copy(allocator);
|
|
|
|
DEBUG_WITH_TYPE("trie-builder", llvm::dbgs()
|
|
|
|
<< "splice in TrieNode('" << bNodeStr
|
|
|
|
<< "') between edge '"
|
|
|
|
<< abEdgeStr << "' and edge='"
|
|
|
|
<< bcEdgeStr<< "'\n");
|
|
|
|
TrieEdge& abEdge = edge;
|
|
|
|
abEdge._subString = abEdgeStr;
|
|
|
|
abEdge._child = bNode;
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *bcEdge = new (allocator) TrieEdge(bcEdgeStr, cNode);
|
2016-01-26 05:50:54 +08:00
|
|
|
bNode->_children.insert(bNode->_children.end(), bcEdge);
|
2014-09-04 03:52:50 +08:00
|
|
|
bNode->addSymbol(entry, allocator, allNodes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
|
|
|
|
assert(entry.otherOffset != 0);
|
|
|
|
}
|
|
|
|
if (entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
|
|
|
|
assert(entry.otherOffset != 0);
|
|
|
|
}
|
|
|
|
// No commonality with any existing child, make a new edge.
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *newNode = new (allocator) TrieNode(entry.name.copy(allocator));
|
|
|
|
auto *newEdge = new (allocator) TrieEdge(partialStr, newNode);
|
2016-01-26 05:50:54 +08:00
|
|
|
_children.insert(_children.end(), newEdge);
|
2014-09-04 03:52:50 +08:00
|
|
|
DEBUG_WITH_TYPE("trie-builder", llvm::dbgs()
|
|
|
|
<< "new TrieNode('" << entry.name << "') with edge '"
|
|
|
|
<< partialStr << "' from node='"
|
|
|
|
<< _cummulativeString << "'\n");
|
|
|
|
newNode->_address = entry.offset;
|
|
|
|
newNode->_flags = entry.flags | entry.kind;
|
|
|
|
newNode->_other = entry.otherOffset;
|
|
|
|
if ((entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) && !entry.otherName.empty())
|
|
|
|
newNode->_importedName = entry.otherName.copy(allocator);
|
|
|
|
newNode->_hasExportInfo = true;
|
|
|
|
allNodes.push_back(newNode);
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:50:54 +08:00
|
|
|
bool TrieNode::updateOffset(uint32_t& offset) {
|
2014-09-04 03:52:50 +08:00
|
|
|
uint32_t nodeSize = 1; // Length when no export info
|
|
|
|
if (_hasExportInfo) {
|
|
|
|
if (_flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
|
|
|
|
nodeSize = llvm::getULEB128Size(_flags);
|
|
|
|
nodeSize += llvm::getULEB128Size(_other); // Other contains ordinal.
|
|
|
|
nodeSize += _importedName.size();
|
|
|
|
++nodeSize; // Trailing zero in imported name.
|
|
|
|
} else {
|
|
|
|
nodeSize = llvm::getULEB128Size(_flags) + llvm::getULEB128Size(_address);
|
|
|
|
if (_flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
|
|
|
|
nodeSize += llvm::getULEB128Size(_other);
|
|
|
|
}
|
|
|
|
// Overall node size so far is uleb128 of export info + actual export info.
|
|
|
|
nodeSize += llvm::getULEB128Size(nodeSize);
|
|
|
|
}
|
|
|
|
// Compute size of all child edges.
|
|
|
|
++nodeSize; // Byte for number of chidren.
|
|
|
|
for (TrieEdge &edge : _children) {
|
|
|
|
nodeSize += edge._subString.size() + 1 // String length.
|
|
|
|
+ llvm::getULEB128Size(edge._child->_trieOffset); // Offset len.
|
|
|
|
}
|
|
|
|
// On input, 'offset' is new prefered location for this node.
|
|
|
|
bool result = (_trieOffset != offset);
|
|
|
|
// Store new location in node object for use by parents.
|
|
|
|
_trieOffset = offset;
|
|
|
|
// Update offset for next iteration.
|
|
|
|
offset += nodeSize;
|
|
|
|
// Return true if _trieOffset was changed.
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:50:54 +08:00
|
|
|
void TrieNode::appendToByteBuffer(ByteBuffer &out) {
|
2014-09-04 03:52:50 +08:00
|
|
|
if (_hasExportInfo) {
|
|
|
|
if (_flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
|
|
|
|
if (!_importedName.empty()) {
|
|
|
|
// nodes with re-export info: size, flags, ordinal, import-name
|
|
|
|
uint32_t nodeSize = llvm::getULEB128Size(_flags)
|
|
|
|
+ llvm::getULEB128Size(_other)
|
|
|
|
+ _importedName.size() + 1;
|
|
|
|
assert(nodeSize < 256);
|
|
|
|
out.append_byte(nodeSize);
|
|
|
|
out.append_uleb128(_flags);
|
|
|
|
out.append_uleb128(_other);
|
|
|
|
out.append_string(_importedName);
|
|
|
|
} else {
|
|
|
|
// nodes without re-export info: size, flags, ordinal, empty-string
|
|
|
|
uint32_t nodeSize = llvm::getULEB128Size(_flags)
|
|
|
|
+ llvm::getULEB128Size(_other) + 1;
|
|
|
|
assert(nodeSize < 256);
|
|
|
|
out.append_byte(nodeSize);
|
|
|
|
out.append_uleb128(_flags);
|
|
|
|
out.append_uleb128(_other);
|
|
|
|
out.append_byte(0);
|
|
|
|
}
|
|
|
|
} else if ( _flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) {
|
|
|
|
// Nodes with export info: size, flags, address, other
|
|
|
|
uint32_t nodeSize = llvm::getULEB128Size(_flags)
|
|
|
|
+ llvm::getULEB128Size(_address)
|
|
|
|
+ llvm::getULEB128Size(_other);
|
|
|
|
assert(nodeSize < 256);
|
|
|
|
out.append_byte(nodeSize);
|
|
|
|
out.append_uleb128(_flags);
|
|
|
|
out.append_uleb128(_address);
|
|
|
|
out.append_uleb128(_other);
|
|
|
|
} else {
|
|
|
|
// Nodes with export info: size, flags, address
|
|
|
|
uint32_t nodeSize = llvm::getULEB128Size(_flags)
|
|
|
|
+ llvm::getULEB128Size(_address);
|
|
|
|
assert(nodeSize < 256);
|
|
|
|
out.append_byte(nodeSize);
|
|
|
|
out.append_uleb128(_flags);
|
|
|
|
out.append_uleb128(_address);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Node with no export info.
|
|
|
|
uint32_t nodeSize = 0;
|
|
|
|
out.append_byte(nodeSize);
|
|
|
|
}
|
|
|
|
// Add number of children.
|
|
|
|
assert(_children.size() < 256);
|
|
|
|
out.append_byte(_children.size());
|
|
|
|
// Append each child edge substring and node offset.
|
|
|
|
for (TrieEdge &edge : _children) {
|
|
|
|
out.append_string(edge._subString);
|
|
|
|
out.append_uleb128(edge._child->_trieOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachOFileLayout::buildExportTrie() {
|
|
|
|
if (_file.exportInfo.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// For all temporary strings and objects used building trie.
|
|
|
|
BumpPtrAllocator allocator;
|
|
|
|
|
|
|
|
// Build trie of all exported symbols.
|
2015-11-11 06:37:38 +08:00
|
|
|
auto *rootNode = new (allocator) TrieNode(StringRef());
|
2014-09-04 03:52:50 +08:00
|
|
|
std::vector<TrieNode*> allNodes;
|
|
|
|
allNodes.reserve(_file.exportInfo.size()*2);
|
|
|
|
allNodes.push_back(rootNode);
|
|
|
|
for (const Export& entry : _file.exportInfo) {
|
|
|
|
rootNode->addSymbol(entry, allocator, allNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign each node in the vector an offset in the trie stream, iterating
|
|
|
|
// until all uleb128 sizes have stabilized.
|
|
|
|
bool more;
|
|
|
|
do {
|
|
|
|
uint32_t offset = 0;
|
|
|
|
more = false;
|
|
|
|
for (TrieNode* node : allNodes) {
|
|
|
|
if (node->updateOffset(offset))
|
|
|
|
more = true;
|
|
|
|
}
|
|
|
|
} while (more);
|
|
|
|
|
|
|
|
// Serialize trie to ByteBuffer.
|
|
|
|
for (TrieNode* node : allNodes) {
|
|
|
|
node->appendToByteBuffer(_exportTrie);
|
|
|
|
}
|
|
|
|
_exportTrie.align(_is64 ? 8 : 4);
|
|
|
|
}
|
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
void MachOFileLayout::computeSymbolTableSizes() {
|
|
|
|
// MachO symbol tables have three ranges: locals, globals, and undefines
|
|
|
|
const size_t nlistSize = (_is64 ? sizeof(nlist_64) : sizeof(nlist));
|
2014-01-27 11:09:26 +08:00
|
|
|
_symbolTableSize = nlistSize * (_file.localSymbols.size()
|
2013-11-07 05:36:55 +08:00
|
|
|
+ _file.globalSymbols.size()
|
|
|
|
+ _file.undefinedSymbols.size());
|
2015-12-10 08:12:24 +08:00
|
|
|
_symbolStringPoolSize = 1; // Always reserve 1-byte for the empty string.
|
2013-11-07 05:36:55 +08:00
|
|
|
for (const Symbol &sym : _file.localSymbols) {
|
|
|
|
_symbolStringPoolSize += (sym.name.size()+1);
|
|
|
|
}
|
|
|
|
for (const Symbol &sym : _file.globalSymbols) {
|
|
|
|
_symbolStringPoolSize += (sym.name.size()+1);
|
|
|
|
}
|
|
|
|
for (const Symbol &sym : _file.undefinedSymbols) {
|
|
|
|
_symbolStringPoolSize += (sym.name.size()+1);
|
|
|
|
}
|
|
|
|
_symbolTableLocalsStartIndex = 0;
|
|
|
|
_symbolTableGlobalsStartIndex = _file.localSymbols.size();
|
2014-01-27 11:09:26 +08:00
|
|
|
_symbolTableUndefinesStartIndex = _symbolTableGlobalsStartIndex
|
2013-11-07 05:36:55 +08:00
|
|
|
+ _file.globalSymbols.size();
|
|
|
|
|
|
|
|
_indirectSymbolTableCount = 0;
|
|
|
|
for (const Section § : _file.sections) {
|
|
|
|
_indirectSymbolTableCount += sect.indirectSymbols.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
void MachOFileLayout::computeFunctionStartsSize() {
|
|
|
|
_functionStartsSize = _file.functionStarts.size();
|
|
|
|
}
|
|
|
|
|
2014-07-25 07:06:56 +08:00
|
|
|
void MachOFileLayout::computeDataInCodeSize() {
|
|
|
|
_dataInCodeSize = _file.dataInCode.size() * sizeof(data_in_code_entry);
|
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
void MachOFileLayout::writeLinkEditContent() {
|
|
|
|
if (_file.fileType == llvm::MachO::MH_OBJECT) {
|
|
|
|
writeRelocations();
|
2016-02-09 09:38:13 +08:00
|
|
|
writeFunctionStartsInfo();
|
2014-07-25 07:06:56 +08:00
|
|
|
writeDataInCodeInfo();
|
2013-11-07 05:36:55 +08:00
|
|
|
writeSymbolTable();
|
|
|
|
} else {
|
|
|
|
writeRebaseInfo();
|
|
|
|
writeBindingInfo();
|
|
|
|
writeLazyBindingInfo();
|
|
|
|
// TODO: add weak binding info
|
2014-09-04 03:52:50 +08:00
|
|
|
writeExportInfo();
|
2016-02-09 09:38:13 +08:00
|
|
|
writeFunctionStartsInfo();
|
2014-10-29 06:21:10 +08:00
|
|
|
writeDataInCodeInfo();
|
2013-11-07 05:36:55 +08:00
|
|
|
writeSymbolTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error MachOFileLayout::writeBinary(StringRef path) {
|
2013-11-07 05:36:55 +08:00
|
|
|
// Check for pending error from constructor.
|
|
|
|
if (_ec)
|
2016-03-31 07:10:39 +08:00
|
|
|
return llvm::errorCodeToError(_ec);
|
2013-11-07 05:36:55 +08:00
|
|
|
// Create FileOutputBuffer with calculated size.
|
|
|
|
unsigned flags = 0;
|
|
|
|
if (_file.fileType != llvm::MachO::MH_OBJECT)
|
|
|
|
flags = llvm::FileOutputBuffer::F_executable;
|
2015-08-13 08:31:46 +08:00
|
|
|
ErrorOr<std::unique_ptr<llvm::FileOutputBuffer>> fobOrErr =
|
|
|
|
llvm::FileOutputBuffer::create(path, size(), flags);
|
|
|
|
if (std::error_code ec = fobOrErr.getError())
|
2016-03-31 07:10:39 +08:00
|
|
|
return llvm::errorCodeToError(ec);
|
2015-08-13 08:31:46 +08:00
|
|
|
std::unique_ptr<llvm::FileOutputBuffer> &fob = *fobOrErr;
|
2013-11-07 05:36:55 +08:00
|
|
|
// Write content.
|
|
|
|
_buffer = fob->getBufferStart();
|
|
|
|
writeMachHeader();
|
2016-03-31 08:08:16 +08:00
|
|
|
if (auto ec = writeLoadCommands())
|
2016-03-31 08:38:02 +08:00
|
|
|
return ec;
|
2013-11-07 05:36:55 +08:00
|
|
|
writeSectionContent();
|
|
|
|
writeLinkEditContent();
|
|
|
|
fob->commit();
|
|
|
|
|
2016-03-31 07:10:39 +08:00
|
|
|
return llvm::Error();
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Takes in-memory normalized view and writes a mach-o object file.
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error writeBinary(const NormalizedFile &file, StringRef path) {
|
2013-11-07 05:36:55 +08:00
|
|
|
MachOFileLayout layout(file);
|
|
|
|
return layout.writeBinary(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace normalized
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|