20void MachOReader::readHeader(
Object &O)
const {
30template <
typename SectionType>
32 StringRef SegName(Sec.segname, strnlen(Sec.segname,
sizeof(Sec.segname)));
33 StringRef SectName(Sec.sectname, strnlen(Sec.sectname,
sizeof(Sec.sectname)));
59template <
typename SectionType,
typename SegmentType>
63 std::vector<std::unique_ptr<Section>> Sections;
64 for (
auto Curr =
reinterpret_cast<const SectionType *
>(LoadCmd.
Ptr +
66 End =
reinterpret_cast<const SectionType *
>(LoadCmd.
Ptr +
70 memcpy((
void *)&Sec, Curr,
sizeof(SectionType));
88 return Data.takeError();
111 "Incorrect number of relocations");
113 return std::move(Sections);
116Error MachOReader::readLoadCommands(
Object &O)
const {
119 static constexpr char TextSegmentName[] =
"__TEXT";
122 switch (LoadCmd.C.cmd) {
123 case MachO::LC_CODE_SIGNATURE:
124 O.CodeSignatureCommandIndex =
O.LoadCommands.size();
126 case MachO::LC_SEGMENT:
130 if (
StringRef(
reinterpret_cast<const char *
>(
133 O.TextSegmentCommandIndex =
O.LoadCommands.size();
135 if (
Expected<std::vector<std::unique_ptr<Section>>> Sections =
136 extractSections<MachO::section, MachO::segment_command>(
137 LoadCmd, MachOObj, NextSectionIndex))
140 return Sections.takeError();
142 case MachO::LC_SEGMENT_64:
146 if (
StringRef(
reinterpret_cast<const char *
>(
149 O.TextSegmentCommandIndex =
O.LoadCommands.size();
151 if (
Expected<std::vector<std::unique_ptr<Section>>> Sections =
152 extractSections<MachO::section_64, MachO::segment_command_64>(
153 LoadCmd, MachOObj, NextSectionIndex))
156 return Sections.takeError();
158 case MachO::LC_SYMTAB:
159 O.SymTabCommandIndex =
O.LoadCommands.size();
161 case MachO::LC_DYSYMTAB:
162 O.DySymTabCommandIndex =
O.LoadCommands.size();
164 case MachO::LC_DYLD_INFO:
165 case MachO::LC_DYLD_INFO_ONLY:
166 O.DyLdInfoCommandIndex =
O.LoadCommands.size();
168 case MachO::LC_DATA_IN_CODE:
169 O.DataInCodeCommandIndex =
O.LoadCommands.size();
171 case MachO::LC_LINKER_OPTIMIZATION_HINT:
172 O.LinkerOptimizationHintCommandIndex =
O.LoadCommands.size();
174 case MachO::LC_FUNCTION_STARTS:
175 O.FunctionStartsCommandIndex =
O.LoadCommands.size();
177 case MachO::LC_DYLIB_CODE_SIGN_DRS:
178 O.DylibCodeSignDRsIndex =
O.LoadCommands.size();
180 case MachO::LC_DYLD_EXPORTS_TRIE:
181 O.ExportsTrieCommandIndex =
O.LoadCommands.size();
183 case MachO::LC_DYLD_CHAINED_FIXUPS:
184 O.ChainedFixupsCommandIndex =
O.LoadCommands.size();
187#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
188 case MachO::LCName: \
189 memcpy((void *)&(LC.MachOLoadCommand.LCStruct##_data), LoadCmd.Ptr, \
190 sizeof(MachO::LCStruct)); \
191 if (MachOObj.isLittleEndian() != sys::IsLittleEndianHost) \
192 MachO::swapStruct(LC.MachOLoadCommand.LCStruct##_data); \
193 if (LoadCmd.C.cmdsize > sizeof(MachO::LCStruct)) \
194 LC.Payload = ArrayRef<uint8_t>( \
195 reinterpret_cast<uint8_t *>(const_cast<char *>(LoadCmd.Ptr)) + \
196 sizeof(MachO::LCStruct), \
197 LoadCmd.C.cmdsize - sizeof(MachO::LCStruct)); \
200 switch (LoadCmd.C.cmd) {
208 reinterpret_cast<uint8_t *
>(
const_cast<char *
>(LoadCmd.Ptr)) +
212#include "llvm/BinaryFormat/MachO.def"
214 O.LoadCommands.push_back(std::move(LC));
219template <
typename nlist_t>
222 "n_strx exceeds the size of the string table");
232void MachOReader::readSymbolTable(
Object &O)
const {
234 for (
auto Symbol : MachOObj.
symbols()) {
238 Symbol.getRawDataRefImpl()))
240 Symbol.getRawDataRefImpl())));
242 O.SymTable.Symbols.push_back(std::make_unique<SymbolEntry>(SE));
246void MachOReader::setSymbolInRelocationInfo(
Object &O)
const {
247 std::vector<const Section *> Sections;
248 for (
auto &LC :
O.LoadCommands)
249 for (std::unique_ptr<Section> &Sec : LC.
Sections)
250 Sections.push_back(Sec.get());
253 for (std::unique_ptr<Section> &Sec : LC.
Sections)
254 for (
auto &Reloc : Sec->Relocations)
255 if (!Reloc.Scattered && !Reloc.IsAddend) {
259 Reloc.Symbol =
O.SymTable.getSymbolByIndex(SymbolNum);
263 assert(SymbolNum >= 1 && SymbolNum <= Sections.size() &&
264 "Invalid section index.");
265 Reloc.Sec = Sections[SymbolNum - 1];
270void MachOReader::readRebaseInfo(
Object &O)
const {
274void MachOReader::readBindInfo(
Object &O)
const {
278void MachOReader::readWeakBindInfo(
Object &O)
const {
282void MachOReader::readLazyBindInfo(
Object &O)
const {
286void MachOReader::readExportInfo(
Object &O)
const {
291 O.Exports.Trie = Trie;
294void MachOReader::readLinkData(
Object &O, std::optional<size_t> LCIndex,
299 O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data;
304void MachOReader::readDataInCodeData(
Object &O)
const {
305 return readLinkData(O,
O.DataInCodeCommandIndex,
O.DataInCode);
308void MachOReader::readLinkerOptimizationHint(
Object &O)
const {
309 return readLinkData(O,
O.LinkerOptimizationHintCommandIndex,
310 O.LinkerOptimizationHint);
313void MachOReader::readFunctionStartsData(
Object &O)
const {
314 return readLinkData(O,
O.FunctionStartsCommandIndex,
O.FunctionStarts);
317void MachOReader::readDylibCodeSignDRs(
Object &O)
const {
318 return readLinkData(O,
O.DylibCodeSignDRsIndex,
O.DylibCodeSignDRs);
321void MachOReader::readExportsTrie(
Object &O)
const {
322 return readLinkData(O,
O.ExportsTrieCommandIndex,
O.ExportsTrie);
325void MachOReader::readChainedFixups(
Object &O)
const {
326 return readLinkData(O,
O.ChainedFixupsCommandIndex,
O.ChainedFixups);
329void MachOReader::readIndirectSymbolTable(
Object &O)
const {
335 if ((
Index & AbsOrLocalMask) != 0)
336 O.IndirectSymTable.Symbols.emplace_back(
Index, std::nullopt);
338 O.IndirectSymTable.Symbols.emplace_back(
343void MachOReader::readSwiftVersion(
Object &O)
const {
344 struct ObjCImageInfo {
350 for (
const std::unique_ptr<Section> &Sec : LC.
Sections)
351 if (Sec->Sectname ==
"__objc_imageinfo" &&
352 (Sec->Segname ==
"__DATA" || Sec->Segname ==
"__DATA_CONST" ||
353 Sec->Segname ==
"__DATA_DIRTY") &&
354 Sec->Content.size() >=
sizeof(ObjCImageInfo)) {
355 memcpy(&ImageInfo, Sec->Content.data(),
sizeof(ObjCImageInfo));
360 O.SwiftVersion = (ImageInfo.Flags >> 8) & 0xff;
366 auto Obj = std::make_unique<Object>();
368 if (
Error E = readLoadCommands(*Obj))
370 readSymbolTable(*Obj);
371 setSymbolInRelocationInfo(*Obj);
372 readRebaseInfo(*Obj);
374 readWeakBindInfo(*Obj);
375 readLazyBindInfo(*Obj);
376 readExportInfo(*Obj);
377 readDataInCodeData(*Obj);
378 readLinkerOptimizationHint(*Obj);
379 readFunctionStartsData(*Obj);
380 readDylibCodeSignDRs(*Obj);
381 readExportsTrie(*Obj);
382 readChainedFixups(*Obj);
383 readIndirectSymbolTable(*Obj);
384 readSwiftVersion(*Obj);
385 return std::move(Obj);
#define offsetof(TYPE, MEMBER)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static Section constructSectionCommon(const SectionType &Sec, uint32_t Index)
static Expected< std::vector< std::unique_ptr< Section > > > extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd, const object::MachOObjectFile &MachOObj, uint32_t &NextSectionIndex)
Section constructSection(const MachO::section &Sec, uint32_t Index)
SymbolEntry constructSymbolEntry(StringRef StrTable, const nlist_t &nlist)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
empty - Check if the array is empty.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr size_t size() const
size - Get the string size.
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
The instances of the Type class are immutable: once they are created, they are never changed.
Expected< std::unique_ptr< Object > > create() const override
StringRef getData() const
bool isLittleEndian() const
Expected< SectionRef > getSection(unsigned SectionIndex) const
ArrayRef< uint8_t > getDyldInfoLazyBindOpcodes() const
MachO::dysymtab_command getDysymtabLoadCommand() const
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
ArrayRef< uint8_t > getSectionContents(uint32_t Offset, uint64_t Size) const
relocation_iterator section_rel_end(DataRefImpl Sec) const override
ArrayRef< uint8_t > getDyldInfoExportsTrie() const
MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
ArrayRef< uint8_t > getDyldExportsTrie() const
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
const MachO::mach_header & getHeader() const
ArrayRef< uint8_t > getDyldInfoRebaseOpcodes() const
iterator_range< load_command_iterator > load_commands() const
bool is64Bit() const override
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
ArrayRef< uint8_t > getDyldInfoBindOpcodes() const
ArrayRef< uint8_t > getDyldInfoWeakBindOpcodes() const
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
StringRef getStringTableData() const
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
symbol_iterator_range symbols() const
void swapStruct(fat_header &mh)
static const bool IsLittleEndianHost
void swapByteOrder(T &Value)
This is an optimization pass for GlobalISel generic memory operations.
MachO::macho_load_command MachOLoadCommand
std::vector< std::unique_ptr< Section > > Sections
std::vector< uint8_t > Payload
std::optional< uint32_t > OriginalOffset
std::vector< RelocationInfo > Relocations