1 //===- MachOObjectFile.cpp - Mach-O object file binding -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the MachOObjectFile class, which binds the MachOObject
10 // class to the generic ObjectFile wrapper.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/BinaryFormat/MachO.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Object/MachO.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Object/SymbolicFile.h"
27 #include "llvm/Support/DataExtractor.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Error.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/Host.h"
33 #include "llvm/Support/LEB128.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/SwapByteOrder.h"
36 #include "llvm/Support/raw_ostream.h"
46 #include <system_error>
49 using namespace object;
58 } // end anonymous namespace
60 static Error malformedError(const Twine &Msg) {
61 return make_error<GenericBinaryError>("truncated or malformed object (" +
63 object_error::parse_failed);
66 // FIXME: Replace all uses of this function with getStructOrErr.
68 static T getStruct(const MachOObjectFile &O, const char *P) {
69 // Don't read before the beginning or past the end of the file
70 if (P < O.getData().begin() || P + sizeof(T) > O.getData().end())
71 report_fatal_error("Malformed MachO file.");
74 memcpy(&Cmd, P, sizeof(T));
75 if (O.isLittleEndian() != sys::IsLittleEndianHost)
76 MachO::swapStruct(Cmd);
81 static Expected<T> getStructOrErr(const MachOObjectFile &O, const char *P) {
82 // Don't read before the beginning or past the end of the file
83 if (P < O.getData().begin() || P + sizeof(T) > O.getData().end())
84 return malformedError("Structure read out-of-range");
87 memcpy(&Cmd, P, sizeof(T));
88 if (O.isLittleEndian() != sys::IsLittleEndianHost)
89 MachO::swapStruct(Cmd);
94 getSectionPtr(const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L,
96 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
98 bool Is64 = O.is64Bit();
99 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
100 sizeof(MachO::segment_command);
101 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
102 sizeof(MachO::section);
104 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
105 return reinterpret_cast<const char*>(SectionAddr);
108 static const char *getPtr(const MachOObjectFile &O, size_t Offset) {
109 assert(Offset <= O.getData().size());
110 return O.getData().data() + Offset;
113 static MachO::nlist_base
114 getSymbolTableEntryBase(const MachOObjectFile &O, DataRefImpl DRI) {
115 const char *P = reinterpret_cast<const char *>(DRI.p);
116 return getStruct<MachO::nlist_base>(O, P);
119 static StringRef parseSegmentOrSectionName(const char *P) {
123 // Not null terminated, so this is a 16 char string.
124 return StringRef(P, 16);
127 static unsigned getCPUType(const MachOObjectFile &O) {
128 return O.getHeader().cputype;
131 static unsigned getCPUSubType(const MachOObjectFile &O) {
132 return O.getHeader().cpusubtype;
136 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
141 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
142 return RE.r_word0 & 0xffffff;
145 static bool getPlainRelocationPCRel(const MachOObjectFile &O,
146 const MachO::any_relocation_info &RE) {
147 if (O.isLittleEndian())
148 return (RE.r_word1 >> 24) & 1;
149 return (RE.r_word1 >> 7) & 1;
153 getScatteredRelocationPCRel(const MachO::any_relocation_info &RE) {
154 return (RE.r_word0 >> 30) & 1;
157 static unsigned getPlainRelocationLength(const MachOObjectFile &O,
158 const MachO::any_relocation_info &RE) {
159 if (O.isLittleEndian())
160 return (RE.r_word1 >> 25) & 3;
161 return (RE.r_word1 >> 5) & 3;
165 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
166 return (RE.r_word0 >> 28) & 3;
169 static unsigned getPlainRelocationType(const MachOObjectFile &O,
170 const MachO::any_relocation_info &RE) {
171 if (O.isLittleEndian())
172 return RE.r_word1 >> 28;
173 return RE.r_word1 & 0xf;
176 static uint32_t getSectionFlags(const MachOObjectFile &O,
179 MachO::section_64 Sect = O.getSection64(Sec);
182 MachO::section Sect = O.getSection(Sec);
186 static Expected<MachOObjectFile::LoadCommandInfo>
187 getLoadCommandInfo(const MachOObjectFile &Obj, const char *Ptr,
188 uint32_t LoadCommandIndex) {
189 if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) {
190 if (CmdOrErr->cmdsize + Ptr > Obj.getData().end())
191 return malformedError("load command " + Twine(LoadCommandIndex) +
192 " extends past end of file");
193 if (CmdOrErr->cmdsize < 8)
194 return malformedError("load command " + Twine(LoadCommandIndex) +
195 " with size less than 8 bytes");
196 return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr});
198 return CmdOrErr.takeError();
201 static Expected<MachOObjectFile::LoadCommandInfo>
202 getFirstLoadCommandInfo(const MachOObjectFile &Obj) {
203 unsigned HeaderSize = Obj.is64Bit() ? sizeof(MachO::mach_header_64)
204 : sizeof(MachO::mach_header);
205 if (sizeof(MachO::load_command) > Obj.getHeader().sizeofcmds)
206 return malformedError("load command 0 extends past the end all load "
207 "commands in the file");
208 return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0);
211 static Expected<MachOObjectFile::LoadCommandInfo>
212 getNextLoadCommandInfo(const MachOObjectFile &Obj, uint32_t LoadCommandIndex,
213 const MachOObjectFile::LoadCommandInfo &L) {
214 unsigned HeaderSize = Obj.is64Bit() ? sizeof(MachO::mach_header_64)
215 : sizeof(MachO::mach_header);
216 if (L.Ptr + L.C.cmdsize + sizeof(MachO::load_command) >
217 Obj.getData().data() + HeaderSize + Obj.getHeader().sizeofcmds)
218 return malformedError("load command " + Twine(LoadCommandIndex + 1) +
219 " extends past the end all load commands in the file");
220 return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1);
223 template <typename T>
224 static void parseHeader(const MachOObjectFile &Obj, T &Header,
226 if (sizeof(T) > Obj.getData().size()) {
227 Err = malformedError("the mach header extends past the end of the "
231 if (auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0)))
232 Header = *HeaderOrErr;
234 Err = HeaderOrErr.takeError();
237 // This is used to check for overlapping of Mach-O elements.
238 struct MachOElement {
244 static Error checkOverlappingElement(std::list<MachOElement> &Elements,
245 uint64_t Offset, uint64_t Size,
248 return Error::success();
250 for (auto it=Elements.begin() ; it != Elements.end(); ++it) {
252 if ((Offset >= E.Offset && Offset < E.Offset + E.Size) ||
253 (Offset + Size > E.Offset && Offset + Size < E.Offset + E.Size) ||
254 (Offset <= E.Offset && Offset + Size >= E.Offset + E.Size))
255 return malformedError(Twine(Name) + " at offset " + Twine(Offset) +
256 " with a size of " + Twine(Size) + ", overlaps " +
257 E.Name + " at offset " + Twine(E.Offset) + " with "
258 "a size of " + Twine(E.Size));
261 if (nt != Elements.end()) {
263 if (Offset + Size <= N.Offset) {
264 Elements.insert(nt, {Offset, Size, Name});
265 return Error::success();
269 Elements.push_back({Offset, Size, Name});
270 return Error::success();
273 // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all
274 // sections to \param Sections, and optionally sets
275 // \param IsPageZeroSegment to true.
276 template <typename Segment, typename Section>
277 static Error parseSegmentLoadCommand(
278 const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load,
279 SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment,
280 uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders,
281 std::list<MachOElement> &Elements) {
282 const unsigned SegmentLoadSize = sizeof(Segment);
283 if (Load.C.cmdsize < SegmentLoadSize)
284 return malformedError("load command " + Twine(LoadCommandIndex) +
285 " " + CmdName + " cmdsize too small");
286 if (auto SegOrErr = getStructOrErr<Segment>(Obj, Load.Ptr)) {
287 Segment S = SegOrErr.get();
288 const unsigned SectionSize = sizeof(Section);
289 uint64_t FileSize = Obj.getData().size();
290 if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
291 S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
292 return malformedError("load command " + Twine(LoadCommandIndex) +
293 " inconsistent cmdsize in " + CmdName +
294 " for the number of sections");
295 for (unsigned J = 0; J < S.nsects; ++J) {
296 const char *Sec = getSectionPtr(Obj, Load, J);
297 Sections.push_back(Sec);
298 auto SectionOrErr = getStructOrErr<Section>(Obj, Sec);
300 return SectionOrErr.takeError();
301 Section s = SectionOrErr.get();
302 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
303 Obj.getHeader().filetype != MachO::MH_DSYM &&
304 s.flags != MachO::S_ZEROFILL &&
305 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
307 return malformedError("offset field of section " + Twine(J) + " in " +
308 CmdName + " command " + Twine(LoadCommandIndex) +
309 " extends past the end of the file");
310 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
311 Obj.getHeader().filetype != MachO::MH_DSYM &&
312 s.flags != MachO::S_ZEROFILL &&
313 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && S.fileoff == 0 &&
314 s.offset < SizeOfHeaders && s.size != 0)
315 return malformedError("offset field of section " + Twine(J) + " in " +
316 CmdName + " command " + Twine(LoadCommandIndex) +
317 " not past the headers of the file");
318 uint64_t BigSize = s.offset;
320 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
321 Obj.getHeader().filetype != MachO::MH_DSYM &&
322 s.flags != MachO::S_ZEROFILL &&
323 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
325 return malformedError("offset field plus size field of section " +
326 Twine(J) + " in " + CmdName + " command " +
327 Twine(LoadCommandIndex) +
328 " extends past the end of the file");
329 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
330 Obj.getHeader().filetype != MachO::MH_DSYM &&
331 s.flags != MachO::S_ZEROFILL &&
332 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
334 return malformedError("size field of section " +
335 Twine(J) + " in " + CmdName + " command " +
336 Twine(LoadCommandIndex) +
337 " greater than the segment");
338 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
339 Obj.getHeader().filetype != MachO::MH_DSYM && s.size != 0 &&
341 return malformedError("addr field of section " + Twine(J) + " in " +
342 CmdName + " command " + Twine(LoadCommandIndex) +
343 " less than the segment's vmaddr");
346 uint64_t BigEnd = S.vmaddr;
348 if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd)
349 return malformedError("addr field plus size of section " + Twine(J) +
350 " in " + CmdName + " command " +
351 Twine(LoadCommandIndex) +
352 " greater than than "
353 "the segment's vmaddr plus vmsize");
354 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
355 Obj.getHeader().filetype != MachO::MH_DSYM &&
356 s.flags != MachO::S_ZEROFILL &&
357 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL)
358 if (Error Err = checkOverlappingElement(Elements, s.offset, s.size,
361 if (s.reloff > FileSize)
362 return malformedError("reloff field of section " + Twine(J) + " in " +
363 CmdName + " command " + Twine(LoadCommandIndex) +
364 " extends past the end of the file");
366 BigSize *= sizeof(struct MachO::relocation_info);
368 if (BigSize > FileSize)
369 return malformedError("reloff field plus nreloc field times sizeof("
370 "struct relocation_info) of section " +
371 Twine(J) + " in " + CmdName + " command " +
372 Twine(LoadCommandIndex) +
373 " extends past the end of the file");
374 if (Error Err = checkOverlappingElement(Elements, s.reloff, s.nreloc *
376 MachO::relocation_info),
377 "section relocation entries"))
380 if (S.fileoff > FileSize)
381 return malformedError("load command " + Twine(LoadCommandIndex) +
382 " fileoff field in " + CmdName +
383 " extends past the end of the file");
384 uint64_t BigSize = S.fileoff;
385 BigSize += S.filesize;
386 if (BigSize > FileSize)
387 return malformedError("load command " + Twine(LoadCommandIndex) +
388 " fileoff field plus filesize field in " +
389 CmdName + " extends past the end of the file");
390 if (S.vmsize != 0 && S.filesize > S.vmsize)
391 return malformedError("load command " + Twine(LoadCommandIndex) +
392 " filesize field in " + CmdName +
393 " greater than vmsize field");
394 IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
396 return SegOrErr.takeError();
398 return Error::success();
401 static Error checkSymtabCommand(const MachOObjectFile &Obj,
402 const MachOObjectFile::LoadCommandInfo &Load,
403 uint32_t LoadCommandIndex,
404 const char **SymtabLoadCmd,
405 std::list<MachOElement> &Elements) {
406 if (Load.C.cmdsize < sizeof(MachO::symtab_command))
407 return malformedError("load command " + Twine(LoadCommandIndex) +
408 " LC_SYMTAB cmdsize too small");
409 if (*SymtabLoadCmd != nullptr)
410 return malformedError("more than one LC_SYMTAB command");
411 auto SymtabOrErr = getStructOrErr<MachO::symtab_command>(Obj, Load.Ptr);
413 return SymtabOrErr.takeError();
414 MachO::symtab_command Symtab = SymtabOrErr.get();
415 if (Symtab.cmdsize != sizeof(MachO::symtab_command))
416 return malformedError("LC_SYMTAB command " + Twine(LoadCommandIndex) +
417 " has incorrect cmdsize");
418 uint64_t FileSize = Obj.getData().size();
419 if (Symtab.symoff > FileSize)
420 return malformedError("symoff field of LC_SYMTAB command " +
421 Twine(LoadCommandIndex) + " extends past the end "
423 uint64_t SymtabSize = Symtab.nsyms;
424 const char *struct_nlist_name;
426 SymtabSize *= sizeof(MachO::nlist_64);
427 struct_nlist_name = "struct nlist_64";
429 SymtabSize *= sizeof(MachO::nlist);
430 struct_nlist_name = "struct nlist";
432 uint64_t BigSize = SymtabSize;
433 BigSize += Symtab.symoff;
434 if (BigSize > FileSize)
435 return malformedError("symoff field plus nsyms field times sizeof(" +
436 Twine(struct_nlist_name) + ") of LC_SYMTAB command " +
437 Twine(LoadCommandIndex) + " extends past the end "
439 if (Error Err = checkOverlappingElement(Elements, Symtab.symoff, SymtabSize,
442 if (Symtab.stroff > FileSize)
443 return malformedError("stroff field of LC_SYMTAB command " +
444 Twine(LoadCommandIndex) + " extends past the end "
446 BigSize = Symtab.stroff;
447 BigSize += Symtab.strsize;
448 if (BigSize > FileSize)
449 return malformedError("stroff field plus strsize field of LC_SYMTAB "
450 "command " + Twine(LoadCommandIndex) + " extends "
451 "past the end of the file");
452 if (Error Err = checkOverlappingElement(Elements, Symtab.stroff,
453 Symtab.strsize, "string table"))
455 *SymtabLoadCmd = Load.Ptr;
456 return Error::success();
459 static Error checkDysymtabCommand(const MachOObjectFile &Obj,
460 const MachOObjectFile::LoadCommandInfo &Load,
461 uint32_t LoadCommandIndex,
462 const char **DysymtabLoadCmd,
463 std::list<MachOElement> &Elements) {
464 if (Load.C.cmdsize < sizeof(MachO::dysymtab_command))
465 return malformedError("load command " + Twine(LoadCommandIndex) +
466 " LC_DYSYMTAB cmdsize too small");
467 if (*DysymtabLoadCmd != nullptr)
468 return malformedError("more than one LC_DYSYMTAB command");
470 getStructOrErr<MachO::dysymtab_command>(Obj, Load.Ptr);
472 return DysymtabOrErr.takeError();
473 MachO::dysymtab_command Dysymtab = DysymtabOrErr.get();
474 if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command))
475 return malformedError("LC_DYSYMTAB command " + Twine(LoadCommandIndex) +
476 " has incorrect cmdsize");
477 uint64_t FileSize = Obj.getData().size();
478 if (Dysymtab.tocoff > FileSize)
479 return malformedError("tocoff field of LC_DYSYMTAB command " +
480 Twine(LoadCommandIndex) + " extends past the end of "
482 uint64_t BigSize = Dysymtab.ntoc;
483 BigSize *= sizeof(MachO::dylib_table_of_contents);
484 BigSize += Dysymtab.tocoff;
485 if (BigSize > FileSize)
486 return malformedError("tocoff field plus ntoc field times sizeof(struct "
487 "dylib_table_of_contents) of LC_DYSYMTAB command " +
488 Twine(LoadCommandIndex) + " extends past the end of "
490 if (Error Err = checkOverlappingElement(Elements, Dysymtab.tocoff,
491 Dysymtab.ntoc * sizeof(struct
492 MachO::dylib_table_of_contents),
493 "table of contents"))
495 if (Dysymtab.modtaboff > FileSize)
496 return malformedError("modtaboff field of LC_DYSYMTAB command " +
497 Twine(LoadCommandIndex) + " extends past the end of "
499 BigSize = Dysymtab.nmodtab;
500 const char *struct_dylib_module_name;
501 uint64_t sizeof_modtab;
503 sizeof_modtab = sizeof(MachO::dylib_module_64);
504 struct_dylib_module_name = "struct dylib_module_64";
506 sizeof_modtab = sizeof(MachO::dylib_module);
507 struct_dylib_module_name = "struct dylib_module";
509 BigSize *= sizeof_modtab;
510 BigSize += Dysymtab.modtaboff;
511 if (BigSize > FileSize)
512 return malformedError("modtaboff field plus nmodtab field times sizeof(" +
513 Twine(struct_dylib_module_name) + ") of LC_DYSYMTAB "
514 "command " + Twine(LoadCommandIndex) + " extends "
515 "past the end of the file");
516 if (Error Err = checkOverlappingElement(Elements, Dysymtab.modtaboff,
517 Dysymtab.nmodtab * sizeof_modtab,
520 if (Dysymtab.extrefsymoff > FileSize)
521 return malformedError("extrefsymoff field of LC_DYSYMTAB command " +
522 Twine(LoadCommandIndex) + " extends past the end of "
524 BigSize = Dysymtab.nextrefsyms;
525 BigSize *= sizeof(MachO::dylib_reference);
526 BigSize += Dysymtab.extrefsymoff;
527 if (BigSize > FileSize)
528 return malformedError("extrefsymoff field plus nextrefsyms field times "
529 "sizeof(struct dylib_reference) of LC_DYSYMTAB "
530 "command " + Twine(LoadCommandIndex) + " extends "
531 "past the end of the file");
532 if (Error Err = checkOverlappingElement(Elements, Dysymtab.extrefsymoff,
533 Dysymtab.nextrefsyms *
534 sizeof(MachO::dylib_reference),
537 if (Dysymtab.indirectsymoff > FileSize)
538 return malformedError("indirectsymoff field of LC_DYSYMTAB command " +
539 Twine(LoadCommandIndex) + " extends past the end of "
541 BigSize = Dysymtab.nindirectsyms;
542 BigSize *= sizeof(uint32_t);
543 BigSize += Dysymtab.indirectsymoff;
544 if (BigSize > FileSize)
545 return malformedError("indirectsymoff field plus nindirectsyms field times "
546 "sizeof(uint32_t) of LC_DYSYMTAB command " +
547 Twine(LoadCommandIndex) + " extends past the end of "
549 if (Error Err = checkOverlappingElement(Elements, Dysymtab.indirectsymoff,
550 Dysymtab.nindirectsyms *
554 if (Dysymtab.extreloff > FileSize)
555 return malformedError("extreloff field of LC_DYSYMTAB command " +
556 Twine(LoadCommandIndex) + " extends past the end of "
558 BigSize = Dysymtab.nextrel;
559 BigSize *= sizeof(MachO::relocation_info);
560 BigSize += Dysymtab.extreloff;
561 if (BigSize > FileSize)
562 return malformedError("extreloff field plus nextrel field times sizeof"
563 "(struct relocation_info) of LC_DYSYMTAB command " +
564 Twine(LoadCommandIndex) + " extends past the end of "
566 if (Error Err = checkOverlappingElement(Elements, Dysymtab.extreloff,
568 sizeof(MachO::relocation_info),
569 "external relocation table"))
571 if (Dysymtab.locreloff > FileSize)
572 return malformedError("locreloff field of LC_DYSYMTAB command " +
573 Twine(LoadCommandIndex) + " extends past the end of "
575 BigSize = Dysymtab.nlocrel;
576 BigSize *= sizeof(MachO::relocation_info);
577 BigSize += Dysymtab.locreloff;
578 if (BigSize > FileSize)
579 return malformedError("locreloff field plus nlocrel field times sizeof"
580 "(struct relocation_info) of LC_DYSYMTAB command " +
581 Twine(LoadCommandIndex) + " extends past the end of "
583 if (Error Err = checkOverlappingElement(Elements, Dysymtab.locreloff,
585 sizeof(MachO::relocation_info),
586 "local relocation table"))
588 *DysymtabLoadCmd = Load.Ptr;
589 return Error::success();
592 static Error checkLinkeditDataCommand(const MachOObjectFile &Obj,
593 const MachOObjectFile::LoadCommandInfo &Load,
594 uint32_t LoadCommandIndex,
595 const char **LoadCmd, const char *CmdName,
596 std::list<MachOElement> &Elements,
597 const char *ElementName) {
598 if (Load.C.cmdsize < sizeof(MachO::linkedit_data_command))
599 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
600 CmdName + " cmdsize too small");
601 if (*LoadCmd != nullptr)
602 return malformedError("more than one " + Twine(CmdName) + " command");
603 auto LinkDataOrError =
604 getStructOrErr<MachO::linkedit_data_command>(Obj, Load.Ptr);
605 if (!LinkDataOrError)
606 return LinkDataOrError.takeError();
607 MachO::linkedit_data_command LinkData = LinkDataOrError.get();
608 if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command))
609 return malformedError(Twine(CmdName) + " command " +
610 Twine(LoadCommandIndex) + " has incorrect cmdsize");
611 uint64_t FileSize = Obj.getData().size();
612 if (LinkData.dataoff > FileSize)
613 return malformedError("dataoff field of " + Twine(CmdName) + " command " +
614 Twine(LoadCommandIndex) + " extends past the end of "
616 uint64_t BigSize = LinkData.dataoff;
617 BigSize += LinkData.datasize;
618 if (BigSize > FileSize)
619 return malformedError("dataoff field plus datasize field of " +
620 Twine(CmdName) + " command " +
621 Twine(LoadCommandIndex) + " extends past the end of "
623 if (Error Err = checkOverlappingElement(Elements, LinkData.dataoff,
624 LinkData.datasize, ElementName))
627 return Error::success();
630 static Error checkDyldInfoCommand(const MachOObjectFile &Obj,
631 const MachOObjectFile::LoadCommandInfo &Load,
632 uint32_t LoadCommandIndex,
633 const char **LoadCmd, const char *CmdName,
634 std::list<MachOElement> &Elements) {
635 if (Load.C.cmdsize < sizeof(MachO::dyld_info_command))
636 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
637 CmdName + " cmdsize too small");
638 if (*LoadCmd != nullptr)
639 return malformedError("more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY "
642 getStructOrErr<MachO::dyld_info_command>(Obj, Load.Ptr);
644 return DyldInfoOrErr.takeError();
645 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
646 if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command))
647 return malformedError(Twine(CmdName) + " command " +
648 Twine(LoadCommandIndex) + " has incorrect cmdsize");
649 uint64_t FileSize = Obj.getData().size();
650 if (DyldInfo.rebase_off > FileSize)
651 return malformedError("rebase_off field of " + Twine(CmdName) +
652 " command " + Twine(LoadCommandIndex) + " extends "
653 "past the end of the file");
654 uint64_t BigSize = DyldInfo.rebase_off;
655 BigSize += DyldInfo.rebase_size;
656 if (BigSize > FileSize)
657 return malformedError("rebase_off field plus rebase_size field of " +
658 Twine(CmdName) + " command " +
659 Twine(LoadCommandIndex) + " extends past the end of "
661 if (Error Err = checkOverlappingElement(Elements, DyldInfo.rebase_off,
662 DyldInfo.rebase_size,
665 if (DyldInfo.bind_off > FileSize)
666 return malformedError("bind_off field of " + Twine(CmdName) +
667 " command " + Twine(LoadCommandIndex) + " extends "
668 "past the end of the file");
669 BigSize = DyldInfo.bind_off;
670 BigSize += DyldInfo.bind_size;
671 if (BigSize > FileSize)
672 return malformedError("bind_off field plus bind_size field of " +
673 Twine(CmdName) + " command " +
674 Twine(LoadCommandIndex) + " extends past the end of "
676 if (Error Err = checkOverlappingElement(Elements, DyldInfo.bind_off,
680 if (DyldInfo.weak_bind_off > FileSize)
681 return malformedError("weak_bind_off field of " + Twine(CmdName) +
682 " command " + Twine(LoadCommandIndex) + " extends "
683 "past the end of the file");
684 BigSize = DyldInfo.weak_bind_off;
685 BigSize += DyldInfo.weak_bind_size;
686 if (BigSize > FileSize)
687 return malformedError("weak_bind_off field plus weak_bind_size field of " +
688 Twine(CmdName) + " command " +
689 Twine(LoadCommandIndex) + " extends past the end of "
691 if (Error Err = checkOverlappingElement(Elements, DyldInfo.weak_bind_off,
692 DyldInfo.weak_bind_size,
693 "dyld weak bind info"))
695 if (DyldInfo.lazy_bind_off > FileSize)
696 return malformedError("lazy_bind_off field of " + Twine(CmdName) +
697 " command " + Twine(LoadCommandIndex) + " extends "
698 "past the end of the file");
699 BigSize = DyldInfo.lazy_bind_off;
700 BigSize += DyldInfo.lazy_bind_size;
701 if (BigSize > FileSize)
702 return malformedError("lazy_bind_off field plus lazy_bind_size field of " +
703 Twine(CmdName) + " command " +
704 Twine(LoadCommandIndex) + " extends past the end of "
706 if (Error Err = checkOverlappingElement(Elements, DyldInfo.lazy_bind_off,
707 DyldInfo.lazy_bind_size,
708 "dyld lazy bind info"))
710 if (DyldInfo.export_off > FileSize)
711 return malformedError("export_off field of " + Twine(CmdName) +
712 " command " + Twine(LoadCommandIndex) + " extends "
713 "past the end of the file");
714 BigSize = DyldInfo.export_off;
715 BigSize += DyldInfo.export_size;
716 if (BigSize > FileSize)
717 return malformedError("export_off field plus export_size field of " +
718 Twine(CmdName) + " command " +
719 Twine(LoadCommandIndex) + " extends past the end of "
721 if (Error Err = checkOverlappingElement(Elements, DyldInfo.export_off,
722 DyldInfo.export_size,
726 return Error::success();
729 static Error checkDylibCommand(const MachOObjectFile &Obj,
730 const MachOObjectFile::LoadCommandInfo &Load,
731 uint32_t LoadCommandIndex, const char *CmdName) {
732 if (Load.C.cmdsize < sizeof(MachO::dylib_command))
733 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
734 CmdName + " cmdsize too small");
735 auto CommandOrErr = getStructOrErr<MachO::dylib_command>(Obj, Load.Ptr);
737 return CommandOrErr.takeError();
738 MachO::dylib_command D = CommandOrErr.get();
739 if (D.dylib.name < sizeof(MachO::dylib_command))
740 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
741 CmdName + " name.offset field too small, not past "
742 "the end of the dylib_command struct");
743 if (D.dylib.name >= D.cmdsize)
744 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
745 CmdName + " name.offset field extends past the end "
746 "of the load command");
747 // Make sure there is a null between the starting offset of the name and
748 // the end of the load command.
750 const char *P = (const char *)Load.Ptr;
751 for (i = D.dylib.name; i < D.cmdsize; i++)
755 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
756 CmdName + " library name extends past the end of the "
758 return Error::success();
761 static Error checkDylibIdCommand(const MachOObjectFile &Obj,
762 const MachOObjectFile::LoadCommandInfo &Load,
763 uint32_t LoadCommandIndex,
764 const char **LoadCmd) {
765 if (Error Err = checkDylibCommand(Obj, Load, LoadCommandIndex,
768 if (*LoadCmd != nullptr)
769 return malformedError("more than one LC_ID_DYLIB command");
770 if (Obj.getHeader().filetype != MachO::MH_DYLIB &&
771 Obj.getHeader().filetype != MachO::MH_DYLIB_STUB)
772 return malformedError("LC_ID_DYLIB load command in non-dynamic library "
775 return Error::success();
778 static Error checkDyldCommand(const MachOObjectFile &Obj,
779 const MachOObjectFile::LoadCommandInfo &Load,
780 uint32_t LoadCommandIndex, const char *CmdName) {
781 if (Load.C.cmdsize < sizeof(MachO::dylinker_command))
782 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
783 CmdName + " cmdsize too small");
784 auto CommandOrErr = getStructOrErr<MachO::dylinker_command>(Obj, Load.Ptr);
786 return CommandOrErr.takeError();
787 MachO::dylinker_command D = CommandOrErr.get();
788 if (D.name < sizeof(MachO::dylinker_command))
789 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
790 CmdName + " name.offset field too small, not past "
791 "the end of the dylinker_command struct");
792 if (D.name >= D.cmdsize)
793 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
794 CmdName + " name.offset field extends past the end "
795 "of the load command");
796 // Make sure there is a null between the starting offset of the name and
797 // the end of the load command.
799 const char *P = (const char *)Load.Ptr;
800 for (i = D.name; i < D.cmdsize; i++)
804 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
805 CmdName + " dyld name extends past the end of the "
807 return Error::success();
810 static Error checkVersCommand(const MachOObjectFile &Obj,
811 const MachOObjectFile::LoadCommandInfo &Load,
812 uint32_t LoadCommandIndex,
813 const char **LoadCmd, const char *CmdName) {
814 if (Load.C.cmdsize != sizeof(MachO::version_min_command))
815 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
816 CmdName + " has incorrect cmdsize");
817 if (*LoadCmd != nullptr)
818 return malformedError("more than one LC_VERSION_MIN_MACOSX, "
819 "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or "
820 "LC_VERSION_MIN_WATCHOS command");
822 return Error::success();
825 static Error checkNoteCommand(const MachOObjectFile &Obj,
826 const MachOObjectFile::LoadCommandInfo &Load,
827 uint32_t LoadCommandIndex,
828 std::list<MachOElement> &Elements) {
829 if (Load.C.cmdsize != sizeof(MachO::note_command))
830 return malformedError("load command " + Twine(LoadCommandIndex) +
831 " LC_NOTE has incorrect cmdsize");
832 auto NoteCmdOrErr = getStructOrErr<MachO::note_command>(Obj, Load.Ptr);
834 return NoteCmdOrErr.takeError();
835 MachO::note_command Nt = NoteCmdOrErr.get();
836 uint64_t FileSize = Obj.getData().size();
837 if (Nt.offset > FileSize)
838 return malformedError("offset field of LC_NOTE command " +
839 Twine(LoadCommandIndex) + " extends "
840 "past the end of the file");
841 uint64_t BigSize = Nt.offset;
843 if (BigSize > FileSize)
844 return malformedError("size field plus offset field of LC_NOTE command " +
845 Twine(LoadCommandIndex) + " extends past the end of "
847 if (Error Err = checkOverlappingElement(Elements, Nt.offset, Nt.size,
850 return Error::success();
854 parseBuildVersionCommand(const MachOObjectFile &Obj,
855 const MachOObjectFile::LoadCommandInfo &Load,
856 SmallVectorImpl<const char*> &BuildTools,
857 uint32_t LoadCommandIndex) {
859 getStructOrErr<MachO::build_version_command>(Obj, Load.Ptr);
861 return BVCOrErr.takeError();
862 MachO::build_version_command BVC = BVCOrErr.get();
863 if (Load.C.cmdsize !=
864 sizeof(MachO::build_version_command) +
865 BVC.ntools * sizeof(MachO::build_tool_version))
866 return malformedError("load command " + Twine(LoadCommandIndex) +
867 " LC_BUILD_VERSION_COMMAND has incorrect cmdsize");
869 auto Start = Load.Ptr + sizeof(MachO::build_version_command);
870 BuildTools.resize(BVC.ntools);
871 for (unsigned i = 0; i < BVC.ntools; ++i)
872 BuildTools[i] = Start + i * sizeof(MachO::build_tool_version);
874 return Error::success();
877 static Error checkRpathCommand(const MachOObjectFile &Obj,
878 const MachOObjectFile::LoadCommandInfo &Load,
879 uint32_t LoadCommandIndex) {
880 if (Load.C.cmdsize < sizeof(MachO::rpath_command))
881 return malformedError("load command " + Twine(LoadCommandIndex) +
882 " LC_RPATH cmdsize too small");
883 auto ROrErr = getStructOrErr<MachO::rpath_command>(Obj, Load.Ptr);
885 return ROrErr.takeError();
886 MachO::rpath_command R = ROrErr.get();
887 if (R.path < sizeof(MachO::rpath_command))
888 return malformedError("load command " + Twine(LoadCommandIndex) +
889 " LC_RPATH path.offset field too small, not past "
890 "the end of the rpath_command struct");
891 if (R.path >= R.cmdsize)
892 return malformedError("load command " + Twine(LoadCommandIndex) +
893 " LC_RPATH path.offset field extends past the end "
894 "of the load command");
895 // Make sure there is a null between the starting offset of the path and
896 // the end of the load command.
898 const char *P = (const char *)Load.Ptr;
899 for (i = R.path; i < R.cmdsize; i++)
903 return malformedError("load command " + Twine(LoadCommandIndex) +
904 " LC_RPATH library name extends past the end of the "
906 return Error::success();
909 static Error checkEncryptCommand(const MachOObjectFile &Obj,
910 const MachOObjectFile::LoadCommandInfo &Load,
911 uint32_t LoadCommandIndex,
912 uint64_t cryptoff, uint64_t cryptsize,
913 const char **LoadCmd, const char *CmdName) {
914 if (*LoadCmd != nullptr)
915 return malformedError("more than one LC_ENCRYPTION_INFO and or "
916 "LC_ENCRYPTION_INFO_64 command");
917 uint64_t FileSize = Obj.getData().size();
918 if (cryptoff > FileSize)
919 return malformedError("cryptoff field of " + Twine(CmdName) +
920 " command " + Twine(LoadCommandIndex) + " extends "
921 "past the end of the file");
922 uint64_t BigSize = cryptoff;
923 BigSize += cryptsize;
924 if (BigSize > FileSize)
925 return malformedError("cryptoff field plus cryptsize field of " +
926 Twine(CmdName) + " command " +
927 Twine(LoadCommandIndex) + " extends past the end of "
930 return Error::success();
933 static Error checkLinkerOptCommand(const MachOObjectFile &Obj,
934 const MachOObjectFile::LoadCommandInfo &Load,
935 uint32_t LoadCommandIndex) {
936 if (Load.C.cmdsize < sizeof(MachO::linker_option_command))
937 return malformedError("load command " + Twine(LoadCommandIndex) +
938 " LC_LINKER_OPTION cmdsize too small");
939 auto LinkOptionOrErr =
940 getStructOrErr<MachO::linker_option_command>(Obj, Load.Ptr);
941 if (!LinkOptionOrErr)
942 return LinkOptionOrErr.takeError();
943 MachO::linker_option_command L = LinkOptionOrErr.get();
944 // Make sure the count of strings is correct.
945 const char *string = (const char *)Load.Ptr +
946 sizeof(struct MachO::linker_option_command);
947 uint32_t left = L.cmdsize - sizeof(struct MachO::linker_option_command);
950 while (*string == '\0' && left > 0) {
956 uint32_t NullPos = StringRef(string, left).find('\0');
957 if (0xffffffff == NullPos)
958 return malformedError("load command " + Twine(LoadCommandIndex) +
959 " LC_LINKER_OPTION string #" + Twine(i) +
960 " is not NULL terminated");
961 uint32_t len = std::min(NullPos, left) + 1;
967 return malformedError("load command " + Twine(LoadCommandIndex) +
968 " LC_LINKER_OPTION string count " + Twine(L.count) +
969 " does not match number of strings");
970 return Error::success();
973 static Error checkSubCommand(const MachOObjectFile &Obj,
974 const MachOObjectFile::LoadCommandInfo &Load,
975 uint32_t LoadCommandIndex, const char *CmdName,
976 size_t SizeOfCmd, const char *CmdStructName,
977 uint32_t PathOffset, const char *PathFieldName) {
978 if (PathOffset < SizeOfCmd)
979 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
980 CmdName + " " + PathFieldName + ".offset field too "
981 "small, not past the end of the " + CmdStructName);
982 if (PathOffset >= Load.C.cmdsize)
983 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
984 CmdName + " " + PathFieldName + ".offset field "
985 "extends past the end of the load command");
986 // Make sure there is a null between the starting offset of the path and
987 // the end of the load command.
989 const char *P = (const char *)Load.Ptr;
990 for (i = PathOffset; i < Load.C.cmdsize; i++)
993 if (i >= Load.C.cmdsize)
994 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
995 CmdName + " " + PathFieldName + " name extends past "
996 "the end of the load command");
997 return Error::success();
1000 static Error checkThreadCommand(const MachOObjectFile &Obj,
1001 const MachOObjectFile::LoadCommandInfo &Load,
1002 uint32_t LoadCommandIndex,
1003 const char *CmdName) {
1004 if (Load.C.cmdsize < sizeof(MachO::thread_command))
1005 return malformedError("load command " + Twine(LoadCommandIndex) +
1006 CmdName + " cmdsize too small");
1007 auto ThreadCommandOrErr =
1008 getStructOrErr<MachO::thread_command>(Obj, Load.Ptr);
1009 if (!ThreadCommandOrErr)
1010 return ThreadCommandOrErr.takeError();
1011 MachO::thread_command T = ThreadCommandOrErr.get();
1012 const char *state = Load.Ptr + sizeof(MachO::thread_command);
1013 const char *end = Load.Ptr + T.cmdsize;
1014 uint32_t nflavor = 0;
1015 uint32_t cputype = getCPUType(Obj);
1016 while (state < end) {
1017 if(state + sizeof(uint32_t) > end)
1018 return malformedError("load command " + Twine(LoadCommandIndex) +
1019 "flavor in " + CmdName + " extends past end of "
1022 memcpy(&flavor, state, sizeof(uint32_t));
1023 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
1024 sys::swapByteOrder(flavor);
1025 state += sizeof(uint32_t);
1027 if(state + sizeof(uint32_t) > end)
1028 return malformedError("load command " + Twine(LoadCommandIndex) +
1029 " count in " + CmdName + " extends past end of "
1032 memcpy(&count, state, sizeof(uint32_t));
1033 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
1034 sys::swapByteOrder(count);
1035 state += sizeof(uint32_t);
1037 if (cputype == MachO::CPU_TYPE_I386) {
1038 if (flavor == MachO::x86_THREAD_STATE32) {
1039 if (count != MachO::x86_THREAD_STATE32_COUNT)
1040 return malformedError("load command " + Twine(LoadCommandIndex) +
1041 " count not x86_THREAD_STATE32_COUNT for "
1042 "flavor number " + Twine(nflavor) + " which is "
1043 "a x86_THREAD_STATE32 flavor in " + CmdName +
1045 if (state + sizeof(MachO::x86_thread_state32_t) > end)
1046 return malformedError("load command " + Twine(LoadCommandIndex) +
1047 " x86_THREAD_STATE32 extends past end of "
1048 "command in " + CmdName + " command");
1049 state += sizeof(MachO::x86_thread_state32_t);
1051 return malformedError("load command " + Twine(LoadCommandIndex) +
1052 " unknown flavor (" + Twine(flavor) + ") for "
1053 "flavor number " + Twine(nflavor) + " in " +
1054 CmdName + " command");
1056 } else if (cputype == MachO::CPU_TYPE_X86_64) {
1057 if (flavor == MachO::x86_THREAD_STATE) {
1058 if (count != MachO::x86_THREAD_STATE_COUNT)
1059 return malformedError("load command " + Twine(LoadCommandIndex) +
1060 " count not x86_THREAD_STATE_COUNT for "
1061 "flavor number " + Twine(nflavor) + " which is "
1062 "a x86_THREAD_STATE flavor in " + CmdName +
1064 if (state + sizeof(MachO::x86_thread_state_t) > end)
1065 return malformedError("load command " + Twine(LoadCommandIndex) +
1066 " x86_THREAD_STATE extends past end of "
1067 "command in " + CmdName + " command");
1068 state += sizeof(MachO::x86_thread_state_t);
1069 } else if (flavor == MachO::x86_FLOAT_STATE) {
1070 if (count != MachO::x86_FLOAT_STATE_COUNT)
1071 return malformedError("load command " + Twine(LoadCommandIndex) +
1072 " count not x86_FLOAT_STATE_COUNT for "
1073 "flavor number " + Twine(nflavor) + " which is "
1074 "a x86_FLOAT_STATE flavor in " + CmdName +
1076 if (state + sizeof(MachO::x86_float_state_t) > end)
1077 return malformedError("load command " + Twine(LoadCommandIndex) +
1078 " x86_FLOAT_STATE extends past end of "
1079 "command in " + CmdName + " command");
1080 state += sizeof(MachO::x86_float_state_t);
1081 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
1082 if (count != MachO::x86_EXCEPTION_STATE_COUNT)
1083 return malformedError("load command " + Twine(LoadCommandIndex) +
1084 " count not x86_EXCEPTION_STATE_COUNT for "
1085 "flavor number " + Twine(nflavor) + " which is "
1086 "a x86_EXCEPTION_STATE flavor in " + CmdName +
1088 if (state + sizeof(MachO::x86_exception_state_t) > end)
1089 return malformedError("load command " + Twine(LoadCommandIndex) +
1090 " x86_EXCEPTION_STATE extends past end of "
1091 "command in " + CmdName + " command");
1092 state += sizeof(MachO::x86_exception_state_t);
1093 } else if (flavor == MachO::x86_THREAD_STATE64) {
1094 if (count != MachO::x86_THREAD_STATE64_COUNT)
1095 return malformedError("load command " + Twine(LoadCommandIndex) +
1096 " count not x86_THREAD_STATE64_COUNT for "
1097 "flavor number " + Twine(nflavor) + " which is "
1098 "a x86_THREAD_STATE64 flavor in " + CmdName +
1100 if (state + sizeof(MachO::x86_thread_state64_t) > end)
1101 return malformedError("load command " + Twine(LoadCommandIndex) +
1102 " x86_THREAD_STATE64 extends past end of "
1103 "command in " + CmdName + " command");
1104 state += sizeof(MachO::x86_thread_state64_t);
1105 } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
1106 if (count != MachO::x86_EXCEPTION_STATE64_COUNT)
1107 return malformedError("load command " + Twine(LoadCommandIndex) +
1108 " count not x86_EXCEPTION_STATE64_COUNT for "
1109 "flavor number " + Twine(nflavor) + " which is "
1110 "a x86_EXCEPTION_STATE64 flavor in " + CmdName +
1112 if (state + sizeof(MachO::x86_exception_state64_t) > end)
1113 return malformedError("load command " + Twine(LoadCommandIndex) +
1114 " x86_EXCEPTION_STATE64 extends past end of "
1115 "command in " + CmdName + " command");
1116 state += sizeof(MachO::x86_exception_state64_t);
1118 return malformedError("load command " + Twine(LoadCommandIndex) +
1119 " unknown flavor (" + Twine(flavor) + ") for "
1120 "flavor number " + Twine(nflavor) + " in " +
1121 CmdName + " command");
1123 } else if (cputype == MachO::CPU_TYPE_ARM) {
1124 if (flavor == MachO::ARM_THREAD_STATE) {
1125 if (count != MachO::ARM_THREAD_STATE_COUNT)
1126 return malformedError("load command " + Twine(LoadCommandIndex) +
1127 " count not ARM_THREAD_STATE_COUNT for "
1128 "flavor number " + Twine(nflavor) + " which is "
1129 "a ARM_THREAD_STATE flavor in " + CmdName +
1131 if (state + sizeof(MachO::arm_thread_state32_t) > end)
1132 return malformedError("load command " + Twine(LoadCommandIndex) +
1133 " ARM_THREAD_STATE extends past end of "
1134 "command in " + CmdName + " command");
1135 state += sizeof(MachO::arm_thread_state32_t);
1137 return malformedError("load command " + Twine(LoadCommandIndex) +
1138 " unknown flavor (" + Twine(flavor) + ") for "
1139 "flavor number " + Twine(nflavor) + " in " +
1140 CmdName + " command");
1142 } else if (cputype == MachO::CPU_TYPE_ARM64 ||
1143 cputype == MachO::CPU_TYPE_ARM64_32) {
1144 if (flavor == MachO::ARM_THREAD_STATE64) {
1145 if (count != MachO::ARM_THREAD_STATE64_COUNT)
1146 return malformedError("load command " + Twine(LoadCommandIndex) +
1147 " count not ARM_THREAD_STATE64_COUNT for "
1148 "flavor number " + Twine(nflavor) + " which is "
1149 "a ARM_THREAD_STATE64 flavor in " + CmdName +
1151 if (state + sizeof(MachO::arm_thread_state64_t) > end)
1152 return malformedError("load command " + Twine(LoadCommandIndex) +
1153 " ARM_THREAD_STATE64 extends past end of "
1154 "command in " + CmdName + " command");
1155 state += sizeof(MachO::arm_thread_state64_t);
1157 return malformedError("load command " + Twine(LoadCommandIndex) +
1158 " unknown flavor (" + Twine(flavor) + ") for "
1159 "flavor number " + Twine(nflavor) + " in " +
1160 CmdName + " command");
1162 } else if (cputype == MachO::CPU_TYPE_POWERPC) {
1163 if (flavor == MachO::PPC_THREAD_STATE) {
1164 if (count != MachO::PPC_THREAD_STATE_COUNT)
1165 return malformedError("load command " + Twine(LoadCommandIndex) +
1166 " count not PPC_THREAD_STATE_COUNT for "
1167 "flavor number " + Twine(nflavor) + " which is "
1168 "a PPC_THREAD_STATE flavor in " + CmdName +
1170 if (state + sizeof(MachO::ppc_thread_state32_t) > end)
1171 return malformedError("load command " + Twine(LoadCommandIndex) +
1172 " PPC_THREAD_STATE extends past end of "
1173 "command in " + CmdName + " command");
1174 state += sizeof(MachO::ppc_thread_state32_t);
1176 return malformedError("load command " + Twine(LoadCommandIndex) +
1177 " unknown flavor (" + Twine(flavor) + ") for "
1178 "flavor number " + Twine(nflavor) + " in " +
1179 CmdName + " command");
1182 return malformedError("unknown cputype (" + Twine(cputype) + ") load "
1183 "command " + Twine(LoadCommandIndex) + " for " +
1184 CmdName + " command can't be checked");
1188 return Error::success();
1191 static Error checkTwoLevelHintsCommand(const MachOObjectFile &Obj,
1192 const MachOObjectFile::LoadCommandInfo
1194 uint32_t LoadCommandIndex,
1195 const char **LoadCmd,
1196 std::list<MachOElement> &Elements) {
1197 if (Load.C.cmdsize != sizeof(MachO::twolevel_hints_command))
1198 return malformedError("load command " + Twine(LoadCommandIndex) +
1199 " LC_TWOLEVEL_HINTS has incorrect cmdsize");
1200 if (*LoadCmd != nullptr)
1201 return malformedError("more than one LC_TWOLEVEL_HINTS command");
1202 auto HintsOrErr = getStructOrErr<MachO::twolevel_hints_command>(Obj, Load.Ptr);
1204 return HintsOrErr.takeError();
1205 MachO::twolevel_hints_command Hints = HintsOrErr.get();
1206 uint64_t FileSize = Obj.getData().size();
1207 if (Hints.offset > FileSize)
1208 return malformedError("offset field of LC_TWOLEVEL_HINTS command " +
1209 Twine(LoadCommandIndex) + " extends past the end of "
1211 uint64_t BigSize = Hints.nhints;
1212 BigSize *= sizeof(MachO::twolevel_hint);
1213 BigSize += Hints.offset;
1214 if (BigSize > FileSize)
1215 return malformedError("offset field plus nhints times sizeof(struct "
1216 "twolevel_hint) field of LC_TWOLEVEL_HINTS command " +
1217 Twine(LoadCommandIndex) + " extends past the end of "
1219 if (Error Err = checkOverlappingElement(Elements, Hints.offset, Hints.nhints *
1220 sizeof(MachO::twolevel_hint),
1223 *LoadCmd = Load.Ptr;
1224 return Error::success();
1227 // Returns true if the libObject code does not support the load command and its
1228 // contents. The cmd value it is treated as an unknown load command but with
1229 // an error message that says the cmd value is obsolete.
1230 static bool isLoadCommandObsolete(uint32_t cmd) {
1231 if (cmd == MachO::LC_SYMSEG ||
1232 cmd == MachO::LC_LOADFVMLIB ||
1233 cmd == MachO::LC_IDFVMLIB ||
1234 cmd == MachO::LC_IDENT ||
1235 cmd == MachO::LC_FVMFILE ||
1236 cmd == MachO::LC_PREPAGE ||
1237 cmd == MachO::LC_PREBOUND_DYLIB ||
1238 cmd == MachO::LC_TWOLEVEL_HINTS ||
1239 cmd == MachO::LC_PREBIND_CKSUM)
1244 Expected<std::unique_ptr<MachOObjectFile>>
1245 MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian,
1246 bool Is64Bits, uint32_t UniversalCputype,
1247 uint32_t UniversalIndex) {
1248 Error Err = Error::success();
1249 std::unique_ptr<MachOObjectFile> Obj(
1250 new MachOObjectFile(std::move(Object), IsLittleEndian,
1251 Is64Bits, Err, UniversalCputype,
1254 return std::move(Err);
1255 return std::move(Obj);
1258 MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
1259 bool Is64bits, Error &Err,
1260 uint32_t UniversalCputype,
1261 uint32_t UniversalIndex)
1262 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object) {
1263 ErrorAsOutParameter ErrAsOutParam(&Err);
1264 uint64_t SizeOfHeaders;
1267 parseHeader(*this, Header64, Err);
1268 SizeOfHeaders = sizeof(MachO::mach_header_64);
1269 cputype = Header64.cputype;
1271 parseHeader(*this, Header, Err);
1272 SizeOfHeaders = sizeof(MachO::mach_header);
1273 cputype = Header.cputype;
1277 SizeOfHeaders += getHeader().sizeofcmds;
1278 if (getData().data() + SizeOfHeaders > getData().end()) {
1279 Err = malformedError("load commands extend past the end of the file");
1282 if (UniversalCputype != 0 && cputype != UniversalCputype) {
1283 Err = malformedError("universal header architecture: " +
1284 Twine(UniversalIndex) + "'s cputype does not match "
1285 "object file's mach header");
1288 std::list<MachOElement> Elements;
1289 Elements.push_back({0, SizeOfHeaders, "Mach-O headers"});
1291 uint32_t LoadCommandCount = getHeader().ncmds;
1292 LoadCommandInfo Load;
1293 if (LoadCommandCount != 0) {
1294 if (auto LoadOrErr = getFirstLoadCommandInfo(*this))
1297 Err = LoadOrErr.takeError();
1302 const char *DyldIdLoadCmd = nullptr;
1303 const char *FuncStartsLoadCmd = nullptr;
1304 const char *SplitInfoLoadCmd = nullptr;
1305 const char *CodeSignDrsLoadCmd = nullptr;
1306 const char *CodeSignLoadCmd = nullptr;
1307 const char *VersLoadCmd = nullptr;
1308 const char *SourceLoadCmd = nullptr;
1309 const char *EntryPointLoadCmd = nullptr;
1310 const char *EncryptLoadCmd = nullptr;
1311 const char *RoutinesLoadCmd = nullptr;
1312 const char *UnixThreadLoadCmd = nullptr;
1313 const char *TwoLevelHintsLoadCmd = nullptr;
1314 for (unsigned I = 0; I < LoadCommandCount; ++I) {
1316 if (Load.C.cmdsize % 8 != 0) {
1317 // We have a hack here to allow 64-bit Mach-O core files to have
1318 // LC_THREAD commands that are only a multiple of 4 and not 8 to be
1319 // allowed since the macOS kernel produces them.
1320 if (getHeader().filetype != MachO::MH_CORE ||
1321 Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) {
1322 Err = malformedError("load command " + Twine(I) + " cmdsize not a "
1328 if (Load.C.cmdsize % 4 != 0) {
1329 Err = malformedError("load command " + Twine(I) + " cmdsize not a "
1334 LoadCommands.push_back(Load);
1335 if (Load.C.cmd == MachO::LC_SYMTAB) {
1336 if ((Err = checkSymtabCommand(*this, Load, I, &SymtabLoadCmd, Elements)))
1338 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
1339 if ((Err = checkDysymtabCommand(*this, Load, I, &DysymtabLoadCmd,
1342 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
1343 if ((Err = checkLinkeditDataCommand(*this, Load, I, &DataInCodeLoadCmd,
1344 "LC_DATA_IN_CODE", Elements,
1345 "data in code info")))
1347 } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
1348 if ((Err = checkLinkeditDataCommand(*this, Load, I, &LinkOptHintsLoadCmd,
1349 "LC_LINKER_OPTIMIZATION_HINT",
1350 Elements, "linker optimization "
1353 } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) {
1354 if ((Err = checkLinkeditDataCommand(*this, Load, I, &FuncStartsLoadCmd,
1355 "LC_FUNCTION_STARTS", Elements,
1356 "function starts data")))
1358 } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) {
1359 if ((Err = checkLinkeditDataCommand(*this, Load, I, &SplitInfoLoadCmd,
1360 "LC_SEGMENT_SPLIT_INFO", Elements,
1361 "split info data")))
1363 } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) {
1364 if ((Err = checkLinkeditDataCommand(*this, Load, I, &CodeSignDrsLoadCmd,
1365 "LC_DYLIB_CODE_SIGN_DRS", Elements,
1366 "code signing RDs data")))
1368 } else if (Load.C.cmd == MachO::LC_CODE_SIGNATURE) {
1369 if ((Err = checkLinkeditDataCommand(*this, Load, I, &CodeSignLoadCmd,
1370 "LC_CODE_SIGNATURE", Elements,
1371 "code signature data")))
1373 } else if (Load.C.cmd == MachO::LC_DYLD_INFO) {
1374 if ((Err = checkDyldInfoCommand(*this, Load, I, &DyldInfoLoadCmd,
1375 "LC_DYLD_INFO", Elements)))
1377 } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
1378 if ((Err = checkDyldInfoCommand(*this, Load, I, &DyldInfoLoadCmd,
1379 "LC_DYLD_INFO_ONLY", Elements)))
1381 } else if (Load.C.cmd == MachO::LC_UUID) {
1382 if (Load.C.cmdsize != sizeof(MachO::uuid_command)) {
1383 Err = malformedError("LC_UUID command " + Twine(I) + " has incorrect "
1388 Err = malformedError("more than one LC_UUID command");
1391 UuidLoadCmd = Load.Ptr;
1392 } else if (Load.C.cmd == MachO::LC_SEGMENT_64) {
1393 if ((Err = parseSegmentLoadCommand<MachO::segment_command_64,
1395 *this, Load, Sections, HasPageZeroSegment, I,
1396 "LC_SEGMENT_64", SizeOfHeaders, Elements)))
1398 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
1399 if ((Err = parseSegmentLoadCommand<MachO::segment_command,
1401 *this, Load, Sections, HasPageZeroSegment, I,
1402 "LC_SEGMENT", SizeOfHeaders, Elements)))
1404 } else if (Load.C.cmd == MachO::LC_ID_DYLIB) {
1405 if ((Err = checkDylibIdCommand(*this, Load, I, &DyldIdLoadCmd)))
1407 } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) {
1408 if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_DYLIB")))
1410 Libraries.push_back(Load.Ptr);
1411 } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) {
1412 if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_WEAK_DYLIB")))
1414 Libraries.push_back(Load.Ptr);
1415 } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) {
1416 if ((Err = checkDylibCommand(*this, Load, I, "LC_LAZY_LOAD_DYLIB")))
1418 Libraries.push_back(Load.Ptr);
1419 } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) {
1420 if ((Err = checkDylibCommand(*this, Load, I, "LC_REEXPORT_DYLIB")))
1422 Libraries.push_back(Load.Ptr);
1423 } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
1424 if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_UPWARD_DYLIB")))
1426 Libraries.push_back(Load.Ptr);
1427 } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) {
1428 if ((Err = checkDyldCommand(*this, Load, I, "LC_ID_DYLINKER")))
1430 } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) {
1431 if ((Err = checkDyldCommand(*this, Load, I, "LC_LOAD_DYLINKER")))
1433 } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
1434 if ((Err = checkDyldCommand(*this, Load, I, "LC_DYLD_ENVIRONMENT")))
1436 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
1437 if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
1438 "LC_VERSION_MIN_MACOSX")))
1440 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
1441 if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
1442 "LC_VERSION_MIN_IPHONEOS")))
1444 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) {
1445 if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
1446 "LC_VERSION_MIN_TVOS")))
1448 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
1449 if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
1450 "LC_VERSION_MIN_WATCHOS")))
1452 } else if (Load.C.cmd == MachO::LC_NOTE) {
1453 if ((Err = checkNoteCommand(*this, Load, I, Elements)))
1455 } else if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
1456 if ((Err = parseBuildVersionCommand(*this, Load, BuildTools, I)))
1458 } else if (Load.C.cmd == MachO::LC_RPATH) {
1459 if ((Err = checkRpathCommand(*this, Load, I)))
1461 } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) {
1462 if (Load.C.cmdsize != sizeof(MachO::source_version_command)) {
1463 Err = malformedError("LC_SOURCE_VERSION command " + Twine(I) +
1464 " has incorrect cmdsize");
1467 if (SourceLoadCmd) {
1468 Err = malformedError("more than one LC_SOURCE_VERSION command");
1471 SourceLoadCmd = Load.Ptr;
1472 } else if (Load.C.cmd == MachO::LC_MAIN) {
1473 if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) {
1474 Err = malformedError("LC_MAIN command " + Twine(I) +
1475 " has incorrect cmdsize");
1478 if (EntryPointLoadCmd) {
1479 Err = malformedError("more than one LC_MAIN command");
1482 EntryPointLoadCmd = Load.Ptr;
1483 } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO) {
1484 if (Load.C.cmdsize != sizeof(MachO::encryption_info_command)) {
1485 Err = malformedError("LC_ENCRYPTION_INFO command " + Twine(I) +
1486 " has incorrect cmdsize");
1489 MachO::encryption_info_command E =
1490 getStruct<MachO::encryption_info_command>(*this, Load.Ptr);
1491 if ((Err = checkEncryptCommand(*this, Load, I, E.cryptoff, E.cryptsize,
1492 &EncryptLoadCmd, "LC_ENCRYPTION_INFO")))
1494 } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
1495 if (Load.C.cmdsize != sizeof(MachO::encryption_info_command_64)) {
1496 Err = malformedError("LC_ENCRYPTION_INFO_64 command " + Twine(I) +
1497 " has incorrect cmdsize");
1500 MachO::encryption_info_command_64 E =
1501 getStruct<MachO::encryption_info_command_64>(*this, Load.Ptr);
1502 if ((Err = checkEncryptCommand(*this, Load, I, E.cryptoff, E.cryptsize,
1503 &EncryptLoadCmd, "LC_ENCRYPTION_INFO_64")))
1505 } else if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
1506 if ((Err = checkLinkerOptCommand(*this, Load, I)))
1508 } else if (Load.C.cmd == MachO::LC_SUB_FRAMEWORK) {
1509 if (Load.C.cmdsize < sizeof(MachO::sub_framework_command)) {
1510 Err = malformedError("load command " + Twine(I) +
1511 " LC_SUB_FRAMEWORK cmdsize too small");
1514 MachO::sub_framework_command S =
1515 getStruct<MachO::sub_framework_command>(*this, Load.Ptr);
1516 if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_FRAMEWORK",
1517 sizeof(MachO::sub_framework_command),
1518 "sub_framework_command", S.umbrella,
1521 } else if (Load.C.cmd == MachO::LC_SUB_UMBRELLA) {
1522 if (Load.C.cmdsize < sizeof(MachO::sub_umbrella_command)) {
1523 Err = malformedError("load command " + Twine(I) +
1524 " LC_SUB_UMBRELLA cmdsize too small");
1527 MachO::sub_umbrella_command S =
1528 getStruct<MachO::sub_umbrella_command>(*this, Load.Ptr);
1529 if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_UMBRELLA",
1530 sizeof(MachO::sub_umbrella_command),
1531 "sub_umbrella_command", S.sub_umbrella,
1534 } else if (Load.C.cmd == MachO::LC_SUB_LIBRARY) {
1535 if (Load.C.cmdsize < sizeof(MachO::sub_library_command)) {
1536 Err = malformedError("load command " + Twine(I) +
1537 " LC_SUB_LIBRARY cmdsize too small");
1540 MachO::sub_library_command S =
1541 getStruct<MachO::sub_library_command>(*this, Load.Ptr);
1542 if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_LIBRARY",
1543 sizeof(MachO::sub_library_command),
1544 "sub_library_command", S.sub_library,
1547 } else if (Load.C.cmd == MachO::LC_SUB_CLIENT) {
1548 if (Load.C.cmdsize < sizeof(MachO::sub_client_command)) {
1549 Err = malformedError("load command " + Twine(I) +
1550 " LC_SUB_CLIENT cmdsize too small");
1553 MachO::sub_client_command S =
1554 getStruct<MachO::sub_client_command>(*this, Load.Ptr);
1555 if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_CLIENT",
1556 sizeof(MachO::sub_client_command),
1557 "sub_client_command", S.client, "client")))
1559 } else if (Load.C.cmd == MachO::LC_ROUTINES) {
1560 if (Load.C.cmdsize != sizeof(MachO::routines_command)) {
1561 Err = malformedError("LC_ROUTINES command " + Twine(I) +
1562 " has incorrect cmdsize");
1565 if (RoutinesLoadCmd) {
1566 Err = malformedError("more than one LC_ROUTINES and or LC_ROUTINES_64 "
1570 RoutinesLoadCmd = Load.Ptr;
1571 } else if (Load.C.cmd == MachO::LC_ROUTINES_64) {
1572 if (Load.C.cmdsize != sizeof(MachO::routines_command_64)) {
1573 Err = malformedError("LC_ROUTINES_64 command " + Twine(I) +
1574 " has incorrect cmdsize");
1577 if (RoutinesLoadCmd) {
1578 Err = malformedError("more than one LC_ROUTINES_64 and or LC_ROUTINES "
1582 RoutinesLoadCmd = Load.Ptr;
1583 } else if (Load.C.cmd == MachO::LC_UNIXTHREAD) {
1584 if ((Err = checkThreadCommand(*this, Load, I, "LC_UNIXTHREAD")))
1586 if (UnixThreadLoadCmd) {
1587 Err = malformedError("more than one LC_UNIXTHREAD command");
1590 UnixThreadLoadCmd = Load.Ptr;
1591 } else if (Load.C.cmd == MachO::LC_THREAD) {
1592 if ((Err = checkThreadCommand(*this, Load, I, "LC_THREAD")))
1594 // Note: LC_TWOLEVEL_HINTS is really obsolete and is not supported.
1595 } else if (Load.C.cmd == MachO::LC_TWOLEVEL_HINTS) {
1596 if ((Err = checkTwoLevelHintsCommand(*this, Load, I,
1597 &TwoLevelHintsLoadCmd, Elements)))
1599 } else if (isLoadCommandObsolete(Load.C.cmd)) {
1600 Err = malformedError("load command " + Twine(I) + " for cmd value of: " +
1601 Twine(Load.C.cmd) + " is obsolete and not "
1605 // TODO: generate a error for unknown load commands by default. But still
1606 // need work out an approach to allow or not allow unknown values like this
1607 // as an option for some uses like lldb.
1608 if (I < LoadCommandCount - 1) {
1609 if (auto LoadOrErr = getNextLoadCommandInfo(*this, I, Load))
1612 Err = LoadOrErr.takeError();
1617 if (!SymtabLoadCmd) {
1618 if (DysymtabLoadCmd) {
1619 Err = malformedError("contains LC_DYSYMTAB load command without a "
1620 "LC_SYMTAB load command");
1623 } else if (DysymtabLoadCmd) {
1624 MachO::symtab_command Symtab =
1625 getStruct<MachO::symtab_command>(*this, SymtabLoadCmd);
1626 MachO::dysymtab_command Dysymtab =
1627 getStruct<MachO::dysymtab_command>(*this, DysymtabLoadCmd);
1628 if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
1629 Err = malformedError("ilocalsym in LC_DYSYMTAB load command "
1630 "extends past the end of the symbol table");
1633 uint64_t BigSize = Dysymtab.ilocalsym;
1634 BigSize += Dysymtab.nlocalsym;
1635 if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) {
1636 Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load "
1637 "command extends past the end of the symbol table");
1640 if (Dysymtab.nextdefsym != 0 && Dysymtab.iextdefsym > Symtab.nsyms) {
1641 Err = malformedError("iextdefsym in LC_DYSYMTAB load command "
1642 "extends past the end of the symbol table");
1645 BigSize = Dysymtab.iextdefsym;
1646 BigSize += Dysymtab.nextdefsym;
1647 if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) {
1648 Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB "
1649 "load command extends past the end of the symbol "
1653 if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) {
1654 Err = malformedError("iundefsym in LC_DYSYMTAB load command "
1655 "extends past the end of the symbol table");
1658 BigSize = Dysymtab.iundefsym;
1659 BigSize += Dysymtab.nundefsym;
1660 if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) {
1661 Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load "
1662 " command extends past the end of the symbol table");
1666 if ((getHeader().filetype == MachO::MH_DYLIB ||
1667 getHeader().filetype == MachO::MH_DYLIB_STUB) &&
1668 DyldIdLoadCmd == nullptr) {
1669 Err = malformedError("no LC_ID_DYLIB load command in dynamic library "
1673 assert(LoadCommands.size() == LoadCommandCount);
1675 Err = Error::success();
1678 Error MachOObjectFile::checkSymbolTable() const {
1681 MachO::mach_header_64 H_64 = MachOObjectFile::getHeader64();
1684 MachO::mach_header H = MachOObjectFile::getHeader();
1691 uint64_t NValue = 0;
1692 uint32_t SymbolIndex = 0;
1693 MachO::symtab_command S = getSymtabLoadCommand();
1694 for (const SymbolRef &Symbol : symbols()) {
1695 DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
1697 MachO::nlist_64 STE_64 = getSymbol64TableEntry(SymDRI);
1698 NType = STE_64.n_type;
1699 NSect = STE_64.n_sect;
1700 NDesc = STE_64.n_desc;
1701 NStrx = STE_64.n_strx;
1702 NValue = STE_64.n_value;
1704 MachO::nlist STE = getSymbolTableEntry(SymDRI);
1709 NValue = STE.n_value;
1711 if ((NType & MachO::N_STAB) == 0) {
1712 if ((NType & MachO::N_TYPE) == MachO::N_SECT) {
1713 if (NSect == 0 || NSect > Sections.size())
1714 return malformedError("bad section index: " + Twine((int)NSect) +
1715 " for symbol at index " + Twine(SymbolIndex));
1717 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
1718 if (NValue >= S.strsize)
1719 return malformedError("bad n_value: " + Twine((int)NValue) + " past "
1720 "the end of string table, for N_INDR symbol at "
1721 "index " + Twine(SymbolIndex));
1723 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
1724 (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
1725 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
1726 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
1727 if (LibraryOrdinal != 0 &&
1728 LibraryOrdinal != MachO::EXECUTABLE_ORDINAL &&
1729 LibraryOrdinal != MachO::DYNAMIC_LOOKUP_ORDINAL &&
1730 LibraryOrdinal - 1 >= Libraries.size() ) {
1731 return malformedError("bad library ordinal: " + Twine(LibraryOrdinal) +
1732 " for symbol at index " + Twine(SymbolIndex));
1736 if (NStrx >= S.strsize)
1737 return malformedError("bad string table index: " + Twine((int)NStrx) +
1738 " past the end of string table, for symbol at "
1739 "index " + Twine(SymbolIndex));
1742 return Error::success();
1745 void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
1746 unsigned SymbolTableEntrySize = is64Bit() ?
1747 sizeof(MachO::nlist_64) :
1748 sizeof(MachO::nlist);
1749 Symb.p += SymbolTableEntrySize;
1752 Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
1753 StringRef StringTable = getStringTableData();
1754 MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
1755 if (Entry.n_strx == 0)
1756 // A n_strx value of 0 indicates that no name is associated with a
1757 // particular symbol table entry.
1759 const char *Start = &StringTable.data()[Entry.n_strx];
1760 if (Start < getData().begin() || Start >= getData().end()) {
1761 return malformedError("bad string index: " + Twine(Entry.n_strx) +
1762 " for symbol at index " + Twine(getSymbolIndex(Symb)));
1764 return StringRef(Start);
1767 unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
1768 DataRefImpl DRI = Sec.getRawDataRefImpl();
1769 uint32_t Flags = getSectionFlags(*this, DRI);
1770 return Flags & MachO::SECTION_TYPE;
1773 uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
1775 MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
1776 return Entry.n_value;
1778 MachO::nlist Entry = getSymbolTableEntry(Sym);
1779 return Entry.n_value;
1782 // getIndirectName() returns the name of the alias'ed symbol who's string table
1783 // index is in the n_value field.
1784 std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
1785 StringRef &Res) const {
1786 StringRef StringTable = getStringTableData();
1787 MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
1788 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
1789 return object_error::parse_failed;
1790 uint64_t NValue = getNValue(Symb);
1791 if (NValue >= StringTable.size())
1792 return object_error::parse_failed;
1793 const char *Start = &StringTable.data()[NValue];
1794 Res = StringRef(Start);
1795 return std::error_code();
1798 uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
1799 return getNValue(Sym);
1802 Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
1803 return getSymbolValue(Sym);
1806 uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
1807 uint32_t Flags = cantFail(getSymbolFlags(DRI));
1808 if (Flags & SymbolRef::SF_Common) {
1809 MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI);
1810 return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
1815 uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
1816 return getNValue(DRI);
1819 Expected<SymbolRef::Type>
1820 MachOObjectFile::getSymbolType(DataRefImpl Symb) const {
1821 MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
1822 uint8_t n_type = Entry.n_type;
1824 // If this is a STAB debugging symbol, we can do nothing more.
1825 if (n_type & MachO::N_STAB)
1826 return SymbolRef::ST_Debug;
1828 switch (n_type & MachO::N_TYPE) {
1829 case MachO::N_UNDF :
1830 return SymbolRef::ST_Unknown;
1831 case MachO::N_SECT :
1832 Expected<section_iterator> SecOrError = getSymbolSection(Symb);
1834 return SecOrError.takeError();
1835 section_iterator Sec = *SecOrError;
1836 if (Sec->isData() || Sec->isBSS())
1837 return SymbolRef::ST_Data;
1838 return SymbolRef::ST_Function;
1840 return SymbolRef::ST_Other;
1843 Expected<uint32_t> MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
1844 MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI);
1846 uint8_t MachOType = Entry.n_type;
1847 uint16_t MachOFlags = Entry.n_desc;
1849 uint32_t Result = SymbolRef::SF_None;
1851 if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
1852 Result |= SymbolRef::SF_Indirect;
1854 if (MachOType & MachO::N_STAB)
1855 Result |= SymbolRef::SF_FormatSpecific;
1857 if (MachOType & MachO::N_EXT) {
1858 Result |= SymbolRef::SF_Global;
1859 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
1861 Result |= SymbolRef::SF_Common;
1863 Result |= SymbolRef::SF_Undefined;
1866 if (!(MachOType & MachO::N_PEXT))
1867 Result |= SymbolRef::SF_Exported;
1870 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
1871 Result |= SymbolRef::SF_Weak;
1873 if (MachOFlags & (MachO::N_ARM_THUMB_DEF))
1874 Result |= SymbolRef::SF_Thumb;
1876 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
1877 Result |= SymbolRef::SF_Absolute;
1882 Expected<section_iterator>
1883 MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
1884 MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
1885 uint8_t index = Entry.n_sect;
1888 return section_end();
1890 DRI.d.a = index - 1;
1891 if (DRI.d.a >= Sections.size()){
1892 return malformedError("bad section index: " + Twine((int)index) +
1893 " for symbol at index " + Twine(getSymbolIndex(Symb)));
1895 return section_iterator(SectionRef(DRI, this));
1898 unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
1899 MachO::nlist_base Entry =
1900 getSymbolTableEntryBase(*this, Sym.getRawDataRefImpl());
1901 return Entry.n_sect - 1;
1904 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
1908 Expected<StringRef> MachOObjectFile::getSectionName(DataRefImpl Sec) const {
1909 ArrayRef<char> Raw = getSectionRawName(Sec);
1910 return parseSegmentOrSectionName(Raw.data());
1913 uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
1915 return getSection64(Sec).addr;
1916 return getSection(Sec).addr;
1919 uint64_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
1923 uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
1924 // In the case if a malformed Mach-O file where the section offset is past
1925 // the end of the file or some part of the section size is past the end of
1926 // the file return a size of zero or a size that covers the rest of the file
1927 // but does not extend past the end of the file.
1928 uint32_t SectOffset, SectType;
1932 MachO::section_64 Sect = getSection64(Sec);
1933 SectOffset = Sect.offset;
1934 SectSize = Sect.size;
1935 SectType = Sect.flags & MachO::SECTION_TYPE;
1937 MachO::section Sect = getSection(Sec);
1938 SectOffset = Sect.offset;
1939 SectSize = Sect.size;
1940 SectType = Sect.flags & MachO::SECTION_TYPE;
1942 if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL)
1944 uint64_t FileSize = getData().size();
1945 if (SectOffset > FileSize)
1947 if (FileSize - SectOffset < SectSize)
1948 return FileSize - SectOffset;
1952 ArrayRef<uint8_t> MachOObjectFile::getSectionContents(uint32_t Offset,
1953 uint64_t Size) const {
1954 return arrayRefFromStringRef(getData().substr(Offset, Size));
1957 Expected<ArrayRef<uint8_t>>
1958 MachOObjectFile::getSectionContents(DataRefImpl Sec) const {
1963 MachO::section_64 Sect = getSection64(Sec);
1964 Offset = Sect.offset;
1967 MachO::section Sect = getSection(Sec);
1968 Offset = Sect.offset;
1972 return getSectionContents(Offset, Size);
1975 uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
1978 MachO::section_64 Sect = getSection64(Sec);
1981 MachO::section Sect = getSection(Sec);
1985 return uint64_t(1) << Align;
1988 Expected<SectionRef> MachOObjectFile::getSection(unsigned SectionIndex) const {
1989 if (SectionIndex < 1 || SectionIndex > Sections.size())
1990 return malformedError("bad section index: " + Twine((int)SectionIndex));
1993 DRI.d.a = SectionIndex - 1;
1994 return SectionRef(DRI, this);
1997 Expected<SectionRef> MachOObjectFile::getSection(StringRef SectionName) const {
1998 for (const SectionRef &Section : sections()) {
1999 auto NameOrErr = Section.getName();
2001 return NameOrErr.takeError();
2002 if (*NameOrErr == SectionName)
2005 return errorCodeToError(object_error::parse_failed);
2008 bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const {
2012 bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
2013 uint32_t Flags = getSectionFlags(*this, Sec);
2014 return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
2017 bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
2018 uint32_t Flags = getSectionFlags(*this, Sec);
2019 unsigned SectionType = Flags & MachO::SECTION_TYPE;
2020 return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
2021 !(SectionType == MachO::S_ZEROFILL ||
2022 SectionType == MachO::S_GB_ZEROFILL);
2025 bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
2026 uint32_t Flags = getSectionFlags(*this, Sec);
2027 unsigned SectionType = Flags & MachO::SECTION_TYPE;
2028 return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
2029 (SectionType == MachO::S_ZEROFILL ||
2030 SectionType == MachO::S_GB_ZEROFILL);
2033 bool MachOObjectFile::isDebugSection(StringRef SectionName) const {
2034 return SectionName.startswith("__debug") ||
2035 SectionName.startswith("__zdebug") || SectionName == "__gdb_index";
2038 unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
2039 return Sec.getRawDataRefImpl().d.a;
2042 bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
2043 uint32_t Flags = getSectionFlags(*this, Sec);
2044 unsigned SectionType = Flags & MachO::SECTION_TYPE;
2045 return SectionType == MachO::S_ZEROFILL ||
2046 SectionType == MachO::S_GB_ZEROFILL;
2049 bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const {
2050 StringRef SegmentName = getSectionFinalSegmentName(Sec);
2051 if (Expected<StringRef> NameOrErr = getSectionName(Sec))
2052 return (SegmentName == "__LLVM" && *NameOrErr == "__bitcode");
2056 bool MachOObjectFile::isSectionStripped(DataRefImpl Sec) const {
2058 return getSection64(Sec).offset == 0;
2059 return getSection(Sec).offset == 0;
2062 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
2066 return relocation_iterator(RelocationRef(Ret, this));
2070 MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
2073 MachO::section_64 Sect = getSection64(Sec);
2076 MachO::section Sect = getSection(Sec);
2083 return relocation_iterator(RelocationRef(Ret, this));
2086 relocation_iterator MachOObjectFile::extrel_begin() const {
2088 // for DYSYMTAB symbols, Ret.d.a == 0 for external relocations
2089 Ret.d.a = 0; // Would normally be a section index.
2090 Ret.d.b = 0; // Index into the external relocations
2091 return relocation_iterator(RelocationRef(Ret, this));
2094 relocation_iterator MachOObjectFile::extrel_end() const {
2095 MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand();
2097 // for DYSYMTAB symbols, Ret.d.a == 0 for external relocations
2098 Ret.d.a = 0; // Would normally be a section index.
2099 Ret.d.b = DysymtabLoadCmd.nextrel; // Index into the external relocations
2100 return relocation_iterator(RelocationRef(Ret, this));
2103 relocation_iterator MachOObjectFile::locrel_begin() const {
2105 // for DYSYMTAB symbols, Ret.d.a == 1 for local relocations
2106 Ret.d.a = 1; // Would normally be a section index.
2107 Ret.d.b = 0; // Index into the local relocations
2108 return relocation_iterator(RelocationRef(Ret, this));
2111 relocation_iterator MachOObjectFile::locrel_end() const {
2112 MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand();
2114 // for DYSYMTAB symbols, Ret.d.a == 1 for local relocations
2115 Ret.d.a = 1; // Would normally be a section index.
2116 Ret.d.b = DysymtabLoadCmd.nlocrel; // Index into the local relocations
2117 return relocation_iterator(RelocationRef(Ret, this));
2120 void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
2124 uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
2125 assert((getHeader().filetype == MachO::MH_OBJECT ||
2126 getHeader().filetype == MachO::MH_KEXT_BUNDLE) &&
2127 "Only implemented for MH_OBJECT && MH_KEXT_BUNDLE");
2128 MachO::any_relocation_info RE = getRelocation(Rel);
2129 return getAnyRelocationAddress(RE);
2133 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
2134 MachO::any_relocation_info RE = getRelocation(Rel);
2135 if (isRelocationScattered(RE))
2136 return symbol_end();
2138 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
2139 bool isExtern = getPlainRelocationExternal(RE);
2141 return symbol_end();
2143 MachO::symtab_command S = getSymtabLoadCommand();
2144 unsigned SymbolTableEntrySize = is64Bit() ?
2145 sizeof(MachO::nlist_64) :
2146 sizeof(MachO::nlist);
2147 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
2149 Sym.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset));
2150 return symbol_iterator(SymbolRef(Sym, this));
2154 MachOObjectFile::getRelocationSection(DataRefImpl Rel) const {
2155 return section_iterator(getAnyRelocationSection(getRelocation(Rel)));
2158 uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const {
2159 MachO::any_relocation_info RE = getRelocation(Rel);
2160 return getAnyRelocationType(RE);
2163 void MachOObjectFile::getRelocationTypeName(
2164 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
2166 uint64_t RType = getRelocationType(Rel);
2168 unsigned Arch = this->getArch();
2172 static const char *const Table[] = {
2173 "GENERIC_RELOC_VANILLA",
2174 "GENERIC_RELOC_PAIR",
2175 "GENERIC_RELOC_SECTDIFF",
2176 "GENERIC_RELOC_PB_LA_PTR",
2177 "GENERIC_RELOC_LOCAL_SECTDIFF",
2178 "GENERIC_RELOC_TLV" };
2186 case Triple::x86_64: {
2187 static const char *const Table[] = {
2188 "X86_64_RELOC_UNSIGNED",
2189 "X86_64_RELOC_SIGNED",
2190 "X86_64_RELOC_BRANCH",
2191 "X86_64_RELOC_GOT_LOAD",
2193 "X86_64_RELOC_SUBTRACTOR",
2194 "X86_64_RELOC_SIGNED_1",
2195 "X86_64_RELOC_SIGNED_2",
2196 "X86_64_RELOC_SIGNED_4",
2197 "X86_64_RELOC_TLV" };
2206 static const char *const Table[] = {
2207 "ARM_RELOC_VANILLA",
2209 "ARM_RELOC_SECTDIFF",
2210 "ARM_RELOC_LOCAL_SECTDIFF",
2211 "ARM_RELOC_PB_LA_PTR",
2213 "ARM_THUMB_RELOC_BR22",
2214 "ARM_THUMB_32BIT_BRANCH",
2216 "ARM_RELOC_HALF_SECTDIFF" };
2224 case Triple::aarch64:
2225 case Triple::aarch64_32: {
2226 static const char *const Table[] = {
2227 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
2228 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
2229 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
2230 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
2231 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
2232 "ARM64_RELOC_ADDEND"
2235 if (RType >= array_lengthof(Table))
2242 static const char *const Table[] = {
2243 "PPC_RELOC_VANILLA",
2251 "PPC_RELOC_SECTDIFF",
2252 "PPC_RELOC_PB_LA_PTR",
2253 "PPC_RELOC_HI16_SECTDIFF",
2254 "PPC_RELOC_LO16_SECTDIFF",
2255 "PPC_RELOC_HA16_SECTDIFF",
2257 "PPC_RELOC_LO14_SECTDIFF",
2258 "PPC_RELOC_LOCAL_SECTDIFF" };
2266 case Triple::UnknownArch:
2270 Result.append(res.begin(), res.end());
2273 uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const {
2274 MachO::any_relocation_info RE = getRelocation(Rel);
2275 return getAnyRelocationLength(RE);
2279 // guessLibraryShortName() is passed a name of a dynamic library and returns a
2280 // guess on what the short name is. Then name is returned as a substring of the
2281 // StringRef Name passed in. The name of the dynamic library is recognized as
2282 // a framework if it has one of the two following forms:
2283 // Foo.framework/Versions/A/Foo
2284 // Foo.framework/Foo
2285 // Where A and Foo can be any string. And may contain a trailing suffix
2286 // starting with an underbar. If the Name is recognized as a framework then
2287 // isFramework is set to true else it is set to false. If the Name has a
2288 // suffix then Suffix is set to the substring in Name that contains the suffix
2289 // else it is set to a NULL StringRef.
2291 // The Name of the dynamic library is recognized as a library name if it has
2292 // one of the two following forms:
2296 // The library may have a suffix trailing the name Foo of the form:
2297 // libFoo_profile.A.dylib
2298 // libFoo_profile.dylib
2299 // These dyld image suffixes are separated from the short name by a '_'
2300 // character. Because the '_' character is commonly used to separate words in
2301 // filenames guessLibraryShortName() cannot reliably separate a dylib's short
2302 // name from an arbitrary image suffix; imagine if both the short name and the
2303 // suffix contains an '_' character! To better deal with this ambiguity,
2304 // guessLibraryShortName() will recognize only "_debug" and "_profile" as valid
2305 // Suffix values. Calling code needs to be tolerant of guessLibraryShortName()
2306 // guessing incorrectly.
2308 // The Name of the dynamic library is also recognized as a library name if it
2309 // has the following form:
2312 // If the Name of the dynamic library is none of the forms above then a NULL
2313 // StringRef is returned.
2314 StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
2316 StringRef &Suffix) {
2317 StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
2318 size_t a, b, c, d, Idx;
2320 isFramework = false;
2321 Suffix = StringRef();
2323 // Pull off the last component and make Foo point to it
2324 a = Name.rfind('/');
2325 if (a == Name.npos || a == 0)
2327 Foo = Name.slice(a+1, Name.npos);
2329 // Look for a suffix starting with a '_'
2330 Idx = Foo.rfind('_');
2331 if (Idx != Foo.npos && Foo.size() >= 2) {
2332 Suffix = Foo.slice(Idx, Foo.npos);
2333 if (Suffix != "_debug" && Suffix != "_profile")
2334 Suffix = StringRef();
2336 Foo = Foo.slice(0, Idx);
2339 // First look for the form Foo.framework/Foo
2340 b = Name.rfind('/', a);
2345 F = Name.slice(Idx, Idx + Foo.size());
2346 DotFramework = Name.slice(Idx + Foo.size(),
2347 Idx + Foo.size() + sizeof(".framework/")-1);
2348 if (F == Foo && DotFramework == ".framework/") {
2353 // Next look for the form Foo.framework/Versions/A/Foo
2356 c = Name.rfind('/', b);
2357 if (c == Name.npos || c == 0)
2359 V = Name.slice(c+1, Name.npos);
2360 if (!V.startswith("Versions/"))
2362 d = Name.rfind('/', c);
2367 F = Name.slice(Idx, Idx + Foo.size());
2368 DotFramework = Name.slice(Idx + Foo.size(),
2369 Idx + Foo.size() + sizeof(".framework/")-1);
2370 if (F == Foo && DotFramework == ".framework/") {
2376 // pull off the suffix after the "." and make a point to it
2377 a = Name.rfind('.');
2378 if (a == Name.npos || a == 0)
2380 Dylib = Name.slice(a, Name.npos);
2381 if (Dylib != ".dylib")
2384 // First pull off the version letter for the form Foo.A.dylib if any.
2386 Dot = Name.slice(a-2, a-1);
2391 b = Name.rfind('/', a);
2396 // ignore any suffix after an underbar like Foo_profile.A.dylib
2397 Idx = Name.rfind('_');
2398 if (Idx != Name.npos && Idx != b) {
2399 Lib = Name.slice(b, Idx);
2400 Suffix = Name.slice(Idx, a);
2401 if (Suffix != "_debug" && Suffix != "_profile") {
2402 Suffix = StringRef();
2403 Lib = Name.slice(b, a);
2407 Lib = Name.slice(b, a);
2408 // There are incorrect library names of the form:
2409 // libATS.A_profile.dylib so check for these.
2410 if (Lib.size() >= 3) {
2411 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
2413 Lib = Lib.slice(0, Lib.size()-2);
2418 Qtx = Name.slice(a, Name.npos);
2421 b = Name.rfind('/', a);
2423 Lib = Name.slice(0, a);
2425 Lib = Name.slice(b+1, a);
2426 // There are library names of the form: QT.A.qtx so check for these.
2427 if (Lib.size() >= 3) {
2428 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
2430 Lib = Lib.slice(0, Lib.size()-2);
2435 // getLibraryShortNameByIndex() is used to get the short name of the library
2436 // for an undefined symbol in a linked Mach-O binary that was linked with the
2437 // normal two-level namespace default (that is MH_TWOLEVEL in the header).
2438 // It is passed the index (0 - based) of the library as translated from
2439 // GET_LIBRARY_ORDINAL (1 - based).
2440 std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
2441 StringRef &Res) const {
2442 if (Index >= Libraries.size())
2443 return object_error::parse_failed;
2445 // If the cache of LibrariesShortNames is not built up do that first for
2446 // all the Libraries.
2447 if (LibrariesShortNames.size() == 0) {
2448 for (unsigned i = 0; i < Libraries.size(); i++) {
2450 getStructOrErr<MachO::dylib_command>(*this, Libraries[i]);
2452 return object_error::parse_failed;
2453 MachO::dylib_command D = CommandOrErr.get();
2454 if (D.dylib.name >= D.cmdsize)
2455 return object_error::parse_failed;
2456 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
2457 StringRef Name = StringRef(P);
2458 if (D.dylib.name+Name.size() >= D.cmdsize)
2459 return object_error::parse_failed;
2462 StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
2463 if (shortName.empty())
2464 LibrariesShortNames.push_back(Name);
2466 LibrariesShortNames.push_back(shortName);
2470 Res = LibrariesShortNames[Index];
2471 return std::error_code();
2474 uint32_t MachOObjectFile::getLibraryCount() const {
2475 return Libraries.size();
2479 MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const {
2481 Sec.d.a = Rel->getRawDataRefImpl().d.a;
2482 return section_iterator(SectionRef(Sec, this));
2485 basic_symbol_iterator MachOObjectFile::symbol_begin() const {
2487 MachO::symtab_command Symtab = getSymtabLoadCommand();
2488 if (!SymtabLoadCmd || Symtab.nsyms == 0)
2489 return basic_symbol_iterator(SymbolRef(DRI, this));
2491 return getSymbolByIndex(0);
2494 basic_symbol_iterator MachOObjectFile::symbol_end() const {
2496 MachO::symtab_command Symtab = getSymtabLoadCommand();
2497 if (!SymtabLoadCmd || Symtab.nsyms == 0)
2498 return basic_symbol_iterator(SymbolRef(DRI, this));
2500 unsigned SymbolTableEntrySize = is64Bit() ?
2501 sizeof(MachO::nlist_64) :
2502 sizeof(MachO::nlist);
2503 unsigned Offset = Symtab.symoff +
2504 Symtab.nsyms * SymbolTableEntrySize;
2505 DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset));
2506 return basic_symbol_iterator(SymbolRef(DRI, this));
2509 symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
2510 MachO::symtab_command Symtab = getSymtabLoadCommand();
2511 if (!SymtabLoadCmd || Index >= Symtab.nsyms)
2512 report_fatal_error("Requested symbol index is out of range.");
2513 unsigned SymbolTableEntrySize =
2514 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
2516 DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Symtab.symoff));
2517 DRI.p += Index * SymbolTableEntrySize;
2518 return basic_symbol_iterator(SymbolRef(DRI, this));
2521 uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const {
2522 MachO::symtab_command Symtab = getSymtabLoadCommand();
2524 report_fatal_error("getSymbolIndex() called with no symbol table symbol");
2525 unsigned SymbolTableEntrySize =
2526 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
2527 DataRefImpl DRIstart;
2528 DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(*this, Symtab.symoff));
2529 uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize;
2533 section_iterator MachOObjectFile::section_begin() const {
2535 return section_iterator(SectionRef(DRI, this));
2538 section_iterator MachOObjectFile::section_end() const {
2540 DRI.d.a = Sections.size();
2541 return section_iterator(SectionRef(DRI, this));
2544 uint8_t MachOObjectFile::getBytesInAddress() const {
2545 return is64Bit() ? 8 : 4;
2548 StringRef MachOObjectFile::getFileFormatName() const {
2549 unsigned CPUType = getCPUType(*this);
2552 case MachO::CPU_TYPE_I386:
2553 return "Mach-O 32-bit i386";
2554 case MachO::CPU_TYPE_ARM:
2555 return "Mach-O arm";
2556 case MachO::CPU_TYPE_ARM64_32:
2557 return "Mach-O arm64 (ILP32)";
2558 case MachO::CPU_TYPE_POWERPC:
2559 return "Mach-O 32-bit ppc";
2561 return "Mach-O 32-bit unknown";
2566 case MachO::CPU_TYPE_X86_64:
2567 return "Mach-O 64-bit x86-64";
2568 case MachO::CPU_TYPE_ARM64:
2569 return "Mach-O arm64";
2570 case MachO::CPU_TYPE_POWERPC64:
2571 return "Mach-O 64-bit ppc64";
2573 return "Mach-O 64-bit unknown";
2577 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType) {
2579 case MachO::CPU_TYPE_I386:
2581 case MachO::CPU_TYPE_X86_64:
2582 return Triple::x86_64;
2583 case MachO::CPU_TYPE_ARM:
2585 case MachO::CPU_TYPE_ARM64:
2586 return Triple::aarch64;
2587 case MachO::CPU_TYPE_ARM64_32:
2588 return Triple::aarch64_32;
2589 case MachO::CPU_TYPE_POWERPC:
2591 case MachO::CPU_TYPE_POWERPC64:
2592 return Triple::ppc64;
2594 return Triple::UnknownArch;
2598 Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
2599 const char **McpuDefault,
2600 const char **ArchFlag) {
2602 *McpuDefault = nullptr;
2604 *ArchFlag = nullptr;
2607 case MachO::CPU_TYPE_I386:
2608 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2609 case MachO::CPU_SUBTYPE_I386_ALL:
2612 return Triple("i386-apple-darwin");
2616 case MachO::CPU_TYPE_X86_64:
2617 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2618 case MachO::CPU_SUBTYPE_X86_64_ALL:
2620 *ArchFlag = "x86_64";
2621 return Triple("x86_64-apple-darwin");
2622 case MachO::CPU_SUBTYPE_X86_64_H:
2624 *ArchFlag = "x86_64h";
2625 return Triple("x86_64h-apple-darwin");
2629 case MachO::CPU_TYPE_ARM:
2630 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2631 case MachO::CPU_SUBTYPE_ARM_V4T:
2633 *ArchFlag = "armv4t";
2634 return Triple("armv4t-apple-darwin");
2635 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
2637 *ArchFlag = "armv5e";
2638 return Triple("armv5e-apple-darwin");
2639 case MachO::CPU_SUBTYPE_ARM_XSCALE:
2641 *ArchFlag = "xscale";
2642 return Triple("xscale-apple-darwin");
2643 case MachO::CPU_SUBTYPE_ARM_V6:
2645 *ArchFlag = "armv6";
2646 return Triple("armv6-apple-darwin");
2647 case MachO::CPU_SUBTYPE_ARM_V6M:
2649 *McpuDefault = "cortex-m0";
2651 *ArchFlag = "armv6m";
2652 return Triple("armv6m-apple-darwin");
2653 case MachO::CPU_SUBTYPE_ARM_V7:
2655 *ArchFlag = "armv7";
2656 return Triple("armv7-apple-darwin");
2657 case MachO::CPU_SUBTYPE_ARM_V7EM:
2659 *McpuDefault = "cortex-m4";
2661 *ArchFlag = "armv7em";
2662 return Triple("thumbv7em-apple-darwin");
2663 case MachO::CPU_SUBTYPE_ARM_V7K:
2665 *McpuDefault = "cortex-a7";
2667 *ArchFlag = "armv7k";
2668 return Triple("armv7k-apple-darwin");
2669 case MachO::CPU_SUBTYPE_ARM_V7M:
2671 *McpuDefault = "cortex-m3";
2673 *ArchFlag = "armv7m";
2674 return Triple("thumbv7m-apple-darwin");
2675 case MachO::CPU_SUBTYPE_ARM_V7S:
2677 *McpuDefault = "cortex-a7";
2679 *ArchFlag = "armv7s";
2680 return Triple("armv7s-apple-darwin");
2684 case MachO::CPU_TYPE_ARM64:
2685 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2686 case MachO::CPU_SUBTYPE_ARM64_ALL:
2688 *McpuDefault = "cyclone";
2690 *ArchFlag = "arm64";
2691 return Triple("arm64-apple-darwin");
2695 case MachO::CPU_TYPE_ARM64_32:
2696 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2697 case MachO::CPU_SUBTYPE_ARM64_32_V8:
2699 *McpuDefault = "cyclone";
2701 *ArchFlag = "arm64_32";
2702 return Triple("arm64_32-apple-darwin");
2706 case MachO::CPU_TYPE_POWERPC:
2707 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2708 case MachO::CPU_SUBTYPE_POWERPC_ALL:
2711 return Triple("ppc-apple-darwin");
2715 case MachO::CPU_TYPE_POWERPC64:
2716 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
2717 case MachO::CPU_SUBTYPE_POWERPC_ALL:
2719 *ArchFlag = "ppc64";
2720 return Triple("ppc64-apple-darwin");
2729 Triple MachOObjectFile::getHostArch() {
2730 return Triple(sys::getDefaultTargetTriple());
2733 bool MachOObjectFile::isValidArch(StringRef ArchFlag) {
2734 auto validArchs = getValidArchs();
2735 return llvm::find(validArchs, ArchFlag) != validArchs.end();
2738 ArrayRef<StringRef> MachOObjectFile::getValidArchs() {
2739 static const std::array<StringRef, 17> validArchs = {{
2740 "i386", "x86_64", "x86_64h", "armv4t", "arm", "armv5e",
2741 "armv6", "armv6m", "armv7", "armv7em", "armv7k", "armv7m",
2742 "armv7s", "arm64", "arm64_32", "ppc", "ppc64",
2748 Triple::ArchType MachOObjectFile::getArch() const {
2749 return getArch(getCPUType(*this), getCPUSubType(*this));
2752 Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const {
2753 return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault);
2756 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
2759 return section_rel_begin(DRI);
2762 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
2765 return section_rel_end(DRI);
2768 dice_iterator MachOObjectFile::begin_dices() const {
2770 if (!DataInCodeLoadCmd)
2771 return dice_iterator(DiceRef(DRI, this));
2773 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
2774 DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, DicLC.dataoff));
2775 return dice_iterator(DiceRef(DRI, this));
2778 dice_iterator MachOObjectFile::end_dices() const {
2780 if (!DataInCodeLoadCmd)
2781 return dice_iterator(DiceRef(DRI, this));
2783 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
2784 unsigned Offset = DicLC.dataoff + DicLC.datasize;
2785 DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset));
2786 return dice_iterator(DiceRef(DRI, this));
2789 ExportEntry::ExportEntry(Error *E, const MachOObjectFile *O,
2790 ArrayRef<uint8_t> T) : E(E), O(O), Trie(T) {}
2792 void ExportEntry::moveToFirst() {
2793 ErrorAsOutParameter ErrAsOutParam(E);
2797 pushDownUntilBottom();
2800 void ExportEntry::moveToEnd() {
2805 bool ExportEntry::operator==(const ExportEntry &Other) const {
2806 // Common case, one at end, other iterating from begin.
2807 if (Done || Other.Done)
2808 return (Done == Other.Done);
2809 // Not equal if different stack sizes.
2810 if (Stack.size() != Other.Stack.size())
2812 // Not equal if different cumulative strings.
2813 if (!CumulativeString.equals(Other.CumulativeString))
2815 // Equal if all nodes in both stacks match.
2816 for (unsigned i=0; i < Stack.size(); ++i) {
2817 if (Stack[i].Start != Other.Stack[i].Start)
2823 uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr, const char **error) {
2825 uint64_t Result = decodeULEB128(Ptr, &Count, Trie.end(), error);
2827 if (Ptr > Trie.end())
2832 StringRef ExportEntry::name() const {
2833 return CumulativeString;
2836 uint64_t ExportEntry::flags() const {
2837 return Stack.back().Flags;
2840 uint64_t ExportEntry::address() const {
2841 return Stack.back().Address;
2844 uint64_t ExportEntry::other() const {
2845 return Stack.back().Other;
2848 StringRef ExportEntry::otherName() const {
2849 const char* ImportName = Stack.back().ImportName;
2851 return StringRef(ImportName);
2855 uint32_t ExportEntry::nodeOffset() const {
2856 return Stack.back().Start - Trie.begin();
2859 ExportEntry::NodeState::NodeState(const uint8_t *Ptr)
2860 : Start(Ptr), Current(Ptr) {}
2862 void ExportEntry::pushNode(uint64_t offset) {
2863 ErrorAsOutParameter ErrAsOutParam(E);
2864 const uint8_t *Ptr = Trie.begin() + offset;
2865 NodeState State(Ptr);
2867 uint64_t ExportInfoSize = readULEB128(State.Current, &error);
2869 *E = malformedError("export info size " + Twine(error) +
2870 " in export trie data at node: 0x" +
2871 Twine::utohexstr(offset));
2875 State.IsExportNode = (ExportInfoSize != 0);
2876 const uint8_t* Children = State.Current + ExportInfoSize;
2877 if (Children > Trie.end()) {
2878 *E = malformedError(
2879 "export info size: 0x" + Twine::utohexstr(ExportInfoSize) +
2880 " in export trie data at node: 0x" + Twine::utohexstr(offset) +
2881 " too big and extends past end of trie data");
2885 if (State.IsExportNode) {
2886 const uint8_t *ExportStart = State.Current;
2887 State.Flags = readULEB128(State.Current, &error);
2889 *E = malformedError("flags " + Twine(error) +
2890 " in export trie data at node: 0x" +
2891 Twine::utohexstr(offset));
2895 uint64_t Kind = State.Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK;
2896 if (State.Flags != 0 &&
2897 (Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_REGULAR &&
2898 Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE &&
2899 Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL)) {
2900 *E = malformedError(
2901 "unsupported exported symbol kind: " + Twine((int)Kind) +
2902 " in flags: 0x" + Twine::utohexstr(State.Flags) +
2903 " in export trie data at node: 0x" + Twine::utohexstr(offset));
2907 if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
2909 State.Other = readULEB128(State.Current, &error); // dylib ordinal
2911 *E = malformedError("dylib ordinal of re-export " + Twine(error) +
2912 " in export trie data at node: 0x" +
2913 Twine::utohexstr(offset));
2918 if (State.Other > O->getLibraryCount()) {
2919 *E = malformedError(
2920 "bad library ordinal: " + Twine((int)State.Other) + " (max " +
2921 Twine((int)O->getLibraryCount()) +
2922 ") in export trie data at node: 0x" + Twine::utohexstr(offset));
2927 State.ImportName = reinterpret_cast<const char*>(State.Current);
2928 if (*State.ImportName == '\0') {
2931 const uint8_t *End = State.Current + 1;
2932 if (End >= Trie.end()) {
2933 *E = malformedError("import name of re-export in export trie data at "
2935 Twine::utohexstr(offset) +
2936 " starts past end of trie data");
2940 while(*End != '\0' && End < Trie.end())
2943 *E = malformedError("import name of re-export in export trie data at "
2945 Twine::utohexstr(offset) +
2946 " extends past end of trie data");
2950 State.Current = End + 1;
2953 State.Address = readULEB128(State.Current, &error);
2955 *E = malformedError("address " + Twine(error) +
2956 " in export trie data at node: 0x" +
2957 Twine::utohexstr(offset));
2961 if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
2962 State.Other = readULEB128(State.Current, &error);
2964 *E = malformedError("resolver of stub and resolver " + Twine(error) +
2965 " in export trie data at node: 0x" +
2966 Twine::utohexstr(offset));
2972 if(ExportStart + ExportInfoSize != State.Current) {
2973 *E = malformedError(
2974 "inconsistant export info size: 0x" +
2975 Twine::utohexstr(ExportInfoSize) + " where actual size was: 0x" +
2976 Twine::utohexstr(State.Current - ExportStart) +
2977 " in export trie data at node: 0x" + Twine::utohexstr(offset));
2982 State.ChildCount = *Children;
2983 if (State.ChildCount != 0 && Children + 1 >= Trie.end()) {
2984 *E = malformedError("byte for count of childern in export trie data at "
2986 Twine::utohexstr(offset) +
2987 " extends past end of trie data");
2991 State.Current = Children + 1;
2992 State.NextChildIndex = 0;
2993 State.ParentStringLength = CumulativeString.size();
2994 Stack.push_back(State);
2997 void ExportEntry::pushDownUntilBottom() {
2998 ErrorAsOutParameter ErrAsOutParam(E);
3000 while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
3001 NodeState &Top = Stack.back();
3002 CumulativeString.resize(Top.ParentStringLength);
3003 for (;*Top.Current != 0 && Top.Current < Trie.end(); Top.Current++) {
3004 char C = *Top.Current;
3005 CumulativeString.push_back(C);
3007 if (Top.Current >= Trie.end()) {
3008 *E = malformedError("edge sub-string in export trie data at node: 0x" +
3009 Twine::utohexstr(Top.Start - Trie.begin()) +
3010 " for child #" + Twine((int)Top.NextChildIndex) +
3011 " extends past end of trie data");
3016 uint64_t childNodeIndex = readULEB128(Top.Current, &error);
3018 *E = malformedError("child node offset " + Twine(error) +
3019 " in export trie data at node: 0x" +
3020 Twine::utohexstr(Top.Start - Trie.begin()));
3024 for (const NodeState &node : nodes()) {
3025 if (node.Start == Trie.begin() + childNodeIndex){
3026 *E = malformedError("loop in childern in export trie data at node: 0x" +
3027 Twine::utohexstr(Top.Start - Trie.begin()) +
3028 " back to node: 0x" +
3029 Twine::utohexstr(childNodeIndex));
3034 Top.NextChildIndex += 1;
3035 pushNode(childNodeIndex);
3039 if (!Stack.back().IsExportNode) {
3040 *E = malformedError("node is not an export node in export trie data at "
3042 Twine::utohexstr(Stack.back().Start - Trie.begin()));
3048 // We have a trie data structure and need a way to walk it that is compatible
3049 // with the C++ iterator model. The solution is a non-recursive depth first
3050 // traversal where the iterator contains a stack of parent nodes along with a
3051 // string that is the accumulation of all edge strings along the parent chain
3054 // There is one "export" node for each exported symbol. But because some
3055 // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export
3056 // node may have child nodes too.
3058 // The algorithm for moveNext() is to keep moving down the leftmost unvisited
3059 // child until hitting a node with no children (which is an export node or
3060 // else the trie is malformed). On the way down, each node is pushed on the
3061 // stack ivar. If there is no more ways down, it pops up one and tries to go
3062 // down a sibling path until a childless node is reached.
3063 void ExportEntry::moveNext() {
3064 assert(!Stack.empty() && "ExportEntry::moveNext() with empty node stack");
3065 if (!Stack.back().IsExportNode) {
3066 *E = malformedError("node is not an export node in export trie data at "
3068 Twine::utohexstr(Stack.back().Start - Trie.begin()));
3074 while (!Stack.empty()) {
3075 NodeState &Top = Stack.back();
3076 if (Top.NextChildIndex < Top.ChildCount) {
3077 pushDownUntilBottom();
3078 // Now at the next export node.
3081 if (Top.IsExportNode) {
3082 // This node has no children but is itself an export node.
3083 CumulativeString.resize(Top.ParentStringLength);
3092 iterator_range<export_iterator>
3093 MachOObjectFile::exports(Error &E, ArrayRef<uint8_t> Trie,
3094 const MachOObjectFile *O) {
3095 ExportEntry Start(&E, O, Trie);
3099 Start.moveToFirst();
3101 ExportEntry Finish(&E, O, Trie);
3104 return make_range(export_iterator(Start), export_iterator(Finish));
3107 iterator_range<export_iterator> MachOObjectFile::exports(Error &Err) const {
3108 return exports(Err, getDyldInfoExportsTrie(), this);
3111 MachORebaseEntry::MachORebaseEntry(Error *E, const MachOObjectFile *O,
3112 ArrayRef<uint8_t> Bytes, bool is64Bit)
3113 : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()),
3114 PointerSize(is64Bit ? 8 : 4) {}
3116 void MachORebaseEntry::moveToFirst() {
3117 Ptr = Opcodes.begin();
3121 void MachORebaseEntry::moveToEnd() {
3122 Ptr = Opcodes.end();
3123 RemainingLoopCount = 0;
3127 void MachORebaseEntry::moveNext() {
3128 ErrorAsOutParameter ErrAsOutParam(E);
3129 // If in the middle of some loop, move to next rebasing in loop.
3130 SegmentOffset += AdvanceAmount;
3131 if (RemainingLoopCount) {
3132 --RemainingLoopCount;
3135 // REBASE_OPCODE_DONE is only used for padding if we are not aligned to
3136 // pointer size. Therefore it is possible to reach the end without ever having
3137 // seen REBASE_OPCODE_DONE.
3138 if (Ptr == Opcodes.end()) {
3144 // Parse next opcode and set up next loop.
3145 const uint8_t *OpcodeStart = Ptr;
3146 uint8_t Byte = *Ptr++;
3147 uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK;
3148 uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK;
3149 uint32_t Count, Skip;
3150 const char *error = nullptr;
3152 case MachO::REBASE_OPCODE_DONE:
3156 DEBUG_WITH_TYPE("mach-o-rebase", dbgs() << "REBASE_OPCODE_DONE\n");
3158 case MachO::REBASE_OPCODE_SET_TYPE_IMM:
3159 RebaseType = ImmValue;
3160 if (RebaseType > MachO::REBASE_TYPE_TEXT_PCREL32) {
3161 *E = malformedError("for REBASE_OPCODE_SET_TYPE_IMM bad bind type: " +
3162 Twine((int)RebaseType) + " for opcode at: 0x" +
3163 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3169 dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: "
3170 << "RebaseType=" << (int) RebaseType << "\n");
3172 case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
3173 SegmentIndex = ImmValue;
3174 SegmentOffset = readULEB128(&error);
3176 *E = malformedError("for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
3177 Twine(error) + " for opcode at: 0x" +
3178 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3182 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3185 *E = malformedError("for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
3186 Twine(error) + " for opcode at: 0x" +
3187 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3193 dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
3194 << "SegmentIndex=" << SegmentIndex << ", "
3195 << format("SegmentOffset=0x%06X", SegmentOffset)
3198 case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
3199 SegmentOffset += readULEB128(&error);
3201 *E = malformedError("for REBASE_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
3202 " for opcode at: 0x" +
3203 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3207 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3210 *E = malformedError("for REBASE_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
3211 " for opcode at: 0x" +
3212 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3216 DEBUG_WITH_TYPE("mach-o-rebase",
3217 dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: "
3218 << format("SegmentOffset=0x%06X",
3219 SegmentOffset) << "\n");
3221 case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
3222 SegmentOffset += ImmValue * PointerSize;
3223 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3226 *E = malformedError("for REBASE_OPCODE_ADD_ADDR_IMM_SCALED " +
3227 Twine(error) + " for opcode at: 0x" +
3228 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3232 DEBUG_WITH_TYPE("mach-o-rebase",
3233 dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
3234 << format("SegmentOffset=0x%06X",
3235 SegmentOffset) << "\n");
3237 case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
3238 AdvanceAmount = PointerSize;
3242 RemainingLoopCount = ImmValue - 1;
3244 RemainingLoopCount = 0;
3245 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3246 PointerSize, Count, Skip);
3248 *E = malformedError("for REBASE_OPCODE_DO_REBASE_IMM_TIMES " +
3249 Twine(error) + " for opcode at: 0x" +
3250 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3256 dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
3257 << format("SegmentOffset=0x%06X", SegmentOffset)
3258 << ", AdvanceAmount=" << AdvanceAmount
3259 << ", RemainingLoopCount=" << RemainingLoopCount
3262 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
3263 AdvanceAmount = PointerSize;
3265 Count = readULEB128(&error);
3267 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " +
3268 Twine(error) + " for opcode at: 0x" +
3269 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3274 RemainingLoopCount = Count - 1;
3276 RemainingLoopCount = 0;
3277 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3278 PointerSize, Count, Skip);
3280 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " +
3281 Twine(error) + " for opcode at: 0x" +
3282 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3288 dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
3289 << format("SegmentOffset=0x%06X", SegmentOffset)
3290 << ", AdvanceAmount=" << AdvanceAmount
3291 << ", RemainingLoopCount=" << RemainingLoopCount
3294 case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
3295 Skip = readULEB128(&error);
3297 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
3298 Twine(error) + " for opcode at: 0x" +
3299 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3303 AdvanceAmount = Skip + PointerSize;
3305 RemainingLoopCount = 0;
3306 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3307 PointerSize, Count, Skip);
3309 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
3310 Twine(error) + " for opcode at: 0x" +
3311 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3317 dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
3318 << format("SegmentOffset=0x%06X", SegmentOffset)
3319 << ", AdvanceAmount=" << AdvanceAmount
3320 << ", RemainingLoopCount=" << RemainingLoopCount
3323 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
3324 Count = readULEB128(&error);
3326 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
3328 Twine(error) + " for opcode at: 0x" +
3329 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3334 RemainingLoopCount = Count - 1;
3336 RemainingLoopCount = 0;
3337 Skip = readULEB128(&error);
3339 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
3341 Twine(error) + " for opcode at: 0x" +
3342 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3346 AdvanceAmount = Skip + PointerSize;
3348 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3349 PointerSize, Count, Skip);
3351 *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
3353 Twine(error) + " for opcode at: 0x" +
3354 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3360 dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
3361 << format("SegmentOffset=0x%06X", SegmentOffset)
3362 << ", AdvanceAmount=" << AdvanceAmount
3363 << ", RemainingLoopCount=" << RemainingLoopCount
3367 *E = malformedError("bad rebase info (bad opcode value 0x" +
3368 Twine::utohexstr(Opcode) + " for opcode at: 0x" +
3369 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3376 uint64_t MachORebaseEntry::readULEB128(const char **error) {
3378 uint64_t Result = decodeULEB128(Ptr, &Count, Opcodes.end(), error);
3380 if (Ptr > Opcodes.end())
3381 Ptr = Opcodes.end();
3385 int32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; }
3387 uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; }
3389 StringRef MachORebaseEntry::typeName() const {
3390 switch (RebaseType) {
3391 case MachO::REBASE_TYPE_POINTER:
3393 case MachO::REBASE_TYPE_TEXT_ABSOLUTE32:
3394 return "text abs32";
3395 case MachO::REBASE_TYPE_TEXT_PCREL32:
3396 return "text rel32";
3401 // For use with the SegIndex of a checked Mach-O Rebase entry
3402 // to get the segment name.
3403 StringRef MachORebaseEntry::segmentName() const {
3404 return O->BindRebaseSegmentName(SegmentIndex);
3407 // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry
3408 // to get the section name.
3409 StringRef MachORebaseEntry::sectionName() const {
3410 return O->BindRebaseSectionName(SegmentIndex, SegmentOffset);
3413 // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry
3414 // to get the address.
3415 uint64_t MachORebaseEntry::address() const {
3416 return O->BindRebaseAddress(SegmentIndex, SegmentOffset);
3419 bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const {
3420 #ifdef EXPENSIVE_CHECKS
3421 assert(Opcodes == Other.Opcodes && "compare iterators of different files");
3423 assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files");
3425 return (Ptr == Other.Ptr) &&
3426 (RemainingLoopCount == Other.RemainingLoopCount) &&
3427 (Done == Other.Done);
3430 iterator_range<rebase_iterator>
3431 MachOObjectFile::rebaseTable(Error &Err, MachOObjectFile *O,
3432 ArrayRef<uint8_t> Opcodes, bool is64) {
3433 if (O->BindRebaseSectionTable == nullptr)
3434 O->BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(O);
3435 MachORebaseEntry Start(&Err, O, Opcodes, is64);
3436 Start.moveToFirst();
3438 MachORebaseEntry Finish(&Err, O, Opcodes, is64);
3441 return make_range(rebase_iterator(Start), rebase_iterator(Finish));
3444 iterator_range<rebase_iterator> MachOObjectFile::rebaseTable(Error &Err) {
3445 return rebaseTable(Err, this, getDyldInfoRebaseOpcodes(), is64Bit());
3448 MachOBindEntry::MachOBindEntry(Error *E, const MachOObjectFile *O,
3449 ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK)
3450 : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()),
3451 PointerSize(is64Bit ? 8 : 4), TableKind(BK) {}
3453 void MachOBindEntry::moveToFirst() {
3454 Ptr = Opcodes.begin();
3458 void MachOBindEntry::moveToEnd() {
3459 Ptr = Opcodes.end();
3460 RemainingLoopCount = 0;
3464 void MachOBindEntry::moveNext() {
3465 ErrorAsOutParameter ErrAsOutParam(E);
3466 // If in the middle of some loop, move to next binding in loop.
3467 SegmentOffset += AdvanceAmount;
3468 if (RemainingLoopCount) {
3469 --RemainingLoopCount;
3472 // BIND_OPCODE_DONE is only used for padding if we are not aligned to
3473 // pointer size. Therefore it is possible to reach the end without ever having
3474 // seen BIND_OPCODE_DONE.
3475 if (Ptr == Opcodes.end()) {
3481 // Parse next opcode and set up next loop.
3482 const uint8_t *OpcodeStart = Ptr;
3483 uint8_t Byte = *Ptr++;
3484 uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK;
3485 uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK;
3486 int8_t SignExtended;
3487 const uint8_t *SymStart;
3488 uint32_t Count, Skip;
3489 const char *error = nullptr;
3491 case MachO::BIND_OPCODE_DONE:
3492 if (TableKind == Kind::Lazy) {
3493 // Lazying bindings have a DONE opcode between entries. Need to ignore
3494 // it to advance to next entry. But need not if this is last entry.
3495 bool NotLastEntry = false;
3496 for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) {
3498 NotLastEntry = true;
3506 DEBUG_WITH_TYPE("mach-o-bind", dbgs() << "BIND_OPCODE_DONE\n");
3508 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
3509 if (TableKind == Kind::Weak) {
3510 *E = malformedError("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM not allowed in "
3511 "weak bind table for opcode at: 0x" +
3512 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3517 LibraryOrdinalSet = true;
3518 if (ImmValue > O->getLibraryCount()) {
3519 *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad "
3520 "library ordinal: " +
3521 Twine((int)ImmValue) + " (max " +
3522 Twine((int)O->getLibraryCount()) +
3523 ") for opcode at: 0x" +
3524 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3530 dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
3531 << "Ordinal=" << Ordinal << "\n");
3533 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
3534 if (TableKind == Kind::Weak) {
3535 *E = malformedError("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB not allowed in "
3536 "weak bind table for opcode at: 0x" +
3537 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3541 Ordinal = readULEB128(&error);
3542 LibraryOrdinalSet = true;
3544 *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB " +
3545 Twine(error) + " for opcode at: 0x" +
3546 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3550 if (Ordinal > (int)O->getLibraryCount()) {
3551 *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad "
3552 "library ordinal: " +
3553 Twine((int)Ordinal) + " (max " +
3554 Twine((int)O->getLibraryCount()) +
3555 ") for opcode at: 0x" +
3556 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3562 dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
3563 << "Ordinal=" << Ordinal << "\n");
3565 case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
3566 if (TableKind == Kind::Weak) {
3567 *E = malformedError("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM not allowed in "
3568 "weak bind table for opcode at: 0x" +
3569 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3574 SignExtended = MachO::BIND_OPCODE_MASK | ImmValue;
3575 Ordinal = SignExtended;
3576 if (Ordinal < MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP) {
3577 *E = malformedError("for BIND_OPCODE_SET_DYLIB_SPECIAL_IMM unknown "
3578 "special ordinal: " +
3579 Twine((int)Ordinal) + " for opcode at: 0x" +
3580 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3586 LibraryOrdinalSet = true;
3589 dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
3590 << "Ordinal=" << Ordinal << "\n");
3592 case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
3595 while (*Ptr && (Ptr < Opcodes.end())) {
3598 if (Ptr == Opcodes.end()) {
3599 *E = malformedError(
3600 "for BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM "
3601 "symbol name extends past opcodes for opcode at: 0x" +
3602 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3606 SymbolName = StringRef(reinterpret_cast<const char*>(SymStart),
3611 dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
3612 << "SymbolName=" << SymbolName << "\n");
3613 if (TableKind == Kind::Weak) {
3614 if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
3618 case MachO::BIND_OPCODE_SET_TYPE_IMM:
3619 BindType = ImmValue;
3620 if (ImmValue > MachO::BIND_TYPE_TEXT_PCREL32) {
3621 *E = malformedError("for BIND_OPCODE_SET_TYPE_IMM bad bind type: " +
3622 Twine((int)ImmValue) + " for opcode at: 0x" +
3623 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3629 dbgs() << "BIND_OPCODE_SET_TYPE_IMM: "
3630 << "BindType=" << (int)BindType << "\n");
3632 case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
3633 Addend = readSLEB128(&error);
3635 *E = malformedError("for BIND_OPCODE_SET_ADDEND_SLEB " + Twine(error) +
3636 " for opcode at: 0x" +
3637 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3643 dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: "
3644 << "Addend=" << Addend << "\n");
3646 case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
3647 SegmentIndex = ImmValue;
3648 SegmentOffset = readULEB128(&error);
3650 *E = malformedError("for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
3651 Twine(error) + " for opcode at: 0x" +
3652 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3656 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3659 *E = malformedError("for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
3660 Twine(error) + " for opcode at: 0x" +
3661 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3667 dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
3668 << "SegmentIndex=" << SegmentIndex << ", "
3669 << format("SegmentOffset=0x%06X", SegmentOffset)
3672 case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
3673 SegmentOffset += readULEB128(&error);
3675 *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
3676 " for opcode at: 0x" +
3677 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3681 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3684 *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
3685 " for opcode at: 0x" +
3686 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3690 DEBUG_WITH_TYPE("mach-o-bind",
3691 dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: "
3692 << format("SegmentOffset=0x%06X",
3693 SegmentOffset) << "\n");
3695 case MachO::BIND_OPCODE_DO_BIND:
3696 AdvanceAmount = PointerSize;
3697 RemainingLoopCount = 0;
3698 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3701 *E = malformedError("for BIND_OPCODE_DO_BIND " + Twine(error) +
3702 " for opcode at: 0x" +
3703 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3707 if (SymbolName == StringRef()) {
3708 *E = malformedError(
3709 "for BIND_OPCODE_DO_BIND missing preceding "
3710 "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode at: 0x" +
3711 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3715 if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
3717 malformedError("for BIND_OPCODE_DO_BIND missing preceding "
3718 "BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" +
3719 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3723 DEBUG_WITH_TYPE("mach-o-bind",
3724 dbgs() << "BIND_OPCODE_DO_BIND: "
3725 << format("SegmentOffset=0x%06X",
3726 SegmentOffset) << "\n");
3728 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
3729 if (TableKind == Kind::Lazy) {
3730 *E = malformedError("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB not allowed in "
3731 "lazy bind table for opcode at: 0x" +
3732 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3736 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3739 *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " +
3740 Twine(error) + " for opcode at: 0x" +
3741 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3745 if (SymbolName == StringRef()) {
3746 *E = malformedError(
3747 "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing "
3748 "preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode "
3750 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3754 if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
3755 *E = malformedError(
3756 "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing "
3757 "preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" +
3758 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3762 AdvanceAmount = readULEB128(&error) + PointerSize;
3764 *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " +
3765 Twine(error) + " for opcode at: 0x" +
3766 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3770 // Note, this is not really an error until the next bind but make no sense
3771 // for a BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB to not be followed by another
3773 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset +
3774 AdvanceAmount, PointerSize);
3776 *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB (after adding "
3778 Twine(error) + " for opcode at: 0x" +
3779 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3783 RemainingLoopCount = 0;
3786 dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
3787 << format("SegmentOffset=0x%06X", SegmentOffset)
3788 << ", AdvanceAmount=" << AdvanceAmount
3789 << ", RemainingLoopCount=" << RemainingLoopCount
3792 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
3793 if (TableKind == Kind::Lazy) {
3794 *E = malformedError("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED not "
3795 "allowed in lazy bind table for opcode at: 0x" +
3796 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3800 if (SymbolName == StringRef()) {
3801 *E = malformedError(
3802 "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
3803 "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for "
3805 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3809 if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
3810 *E = malformedError(
3811 "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
3812 "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode "
3814 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3818 AdvanceAmount = ImmValue * PointerSize + PointerSize;
3819 RemainingLoopCount = 0;
3820 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset +
3821 AdvanceAmount, PointerSize);
3823 *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " +
3824 Twine(error) + " for opcode at: 0x" +
3825 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3829 DEBUG_WITH_TYPE("mach-o-bind",
3831 << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
3832 << format("SegmentOffset=0x%06X", SegmentOffset) << "\n");
3834 case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
3835 if (TableKind == Kind::Lazy) {
3836 *E = malformedError("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB not "
3837 "allowed in lazy bind table for opcode at: 0x" +
3838 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3842 Count = readULEB128(&error);
3844 RemainingLoopCount = Count - 1;
3846 RemainingLoopCount = 0;
3848 *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
3850 Twine(error) + " for opcode at: 0x" +
3851 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3855 Skip = readULEB128(&error);
3856 AdvanceAmount = Skip + PointerSize;
3858 *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
3860 Twine(error) + " for opcode at: 0x" +
3861 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3865 if (SymbolName == StringRef()) {
3866 *E = malformedError(
3867 "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
3868 "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for "
3870 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3874 if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
3875 *E = malformedError(
3876 "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
3877 "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode "
3879 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3883 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3884 PointerSize, Count, Skip);
3887 malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " +
3888 Twine(error) + " for opcode at: 0x" +
3889 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3895 dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
3896 << format("SegmentOffset=0x%06X", SegmentOffset)
3897 << ", AdvanceAmount=" << AdvanceAmount
3898 << ", RemainingLoopCount=" << RemainingLoopCount
3902 *E = malformedError("bad bind info (bad opcode value 0x" +
3903 Twine::utohexstr(Opcode) + " for opcode at: 0x" +
3904 Twine::utohexstr(OpcodeStart - Opcodes.begin()));
3911 uint64_t MachOBindEntry::readULEB128(const char **error) {
3913 uint64_t Result = decodeULEB128(Ptr, &Count, Opcodes.end(), error);
3915 if (Ptr > Opcodes.end())
3916 Ptr = Opcodes.end();
3920 int64_t MachOBindEntry::readSLEB128(const char **error) {
3922 int64_t Result = decodeSLEB128(Ptr, &Count, Opcodes.end(), error);
3924 if (Ptr > Opcodes.end())
3925 Ptr = Opcodes.end();
3929 int32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; }
3931 uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; }
3933 StringRef MachOBindEntry::typeName() const {
3935 case MachO::BIND_TYPE_POINTER:
3937 case MachO::BIND_TYPE_TEXT_ABSOLUTE32:
3938 return "text abs32";
3939 case MachO::BIND_TYPE_TEXT_PCREL32:
3940 return "text rel32";
3945 StringRef MachOBindEntry::symbolName() const { return SymbolName; }
3947 int64_t MachOBindEntry::addend() const { return Addend; }
3949 uint32_t MachOBindEntry::flags() const { return Flags; }
3951 int MachOBindEntry::ordinal() const { return Ordinal; }
3953 // For use with the SegIndex of a checked Mach-O Bind entry
3954 // to get the segment name.
3955 StringRef MachOBindEntry::segmentName() const {
3956 return O->BindRebaseSegmentName(SegmentIndex);
3959 // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry
3960 // to get the section name.
3961 StringRef MachOBindEntry::sectionName() const {
3962 return O->BindRebaseSectionName(SegmentIndex, SegmentOffset);
3965 // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry
3966 // to get the address.
3967 uint64_t MachOBindEntry::address() const {
3968 return O->BindRebaseAddress(SegmentIndex, SegmentOffset);
3971 bool MachOBindEntry::operator==(const MachOBindEntry &Other) const {
3972 #ifdef EXPENSIVE_CHECKS
3973 assert(Opcodes == Other.Opcodes && "compare iterators of different files");
3975 assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files");
3977 return (Ptr == Other.Ptr) &&
3978 (RemainingLoopCount == Other.RemainingLoopCount) &&
3979 (Done == Other.Done);
3982 // Build table of sections so SegIndex/SegOffset pairs can be translated.
3983 BindRebaseSegInfo::BindRebaseSegInfo(const object::MachOObjectFile *Obj) {
3984 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
3985 StringRef CurSegName;
3986 uint64_t CurSegAddress;
3987 for (const SectionRef &Section : Obj->sections()) {
3989 Expected<StringRef> NameOrErr = Section.getName();
3991 consumeError(NameOrErr.takeError());
3993 Info.SectionName = *NameOrErr;
3994 Info.Address = Section.getAddress();
3995 Info.Size = Section.getSize();
3997 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
3998 if (!Info.SegmentName.equals(CurSegName)) {
4000 CurSegName = Info.SegmentName;
4001 CurSegAddress = Info.Address;
4003 Info.SegmentIndex = CurSegIndex - 1;
4004 Info.OffsetInSegment = Info.Address - CurSegAddress;
4005 Info.SegmentStartAddress = CurSegAddress;
4006 Sections.push_back(Info);
4008 MaxSegIndex = CurSegIndex;
4011 // For use with a SegIndex, SegOffset, and PointerSize triple in
4012 // MachOBindEntry::moveNext() to validate a MachOBindEntry or MachORebaseEntry.
4014 // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
4015 // that fully contains a pointer at that location. Multiple fixups in a bind
4016 // (such as with the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode) can
4017 // be tested via the Count and Skip parameters.
4018 const char * BindRebaseSegInfo::checkSegAndOffsets(int32_t SegIndex,
4020 uint8_t PointerSize,
4024 return "missing preceding *_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB";
4025 if (SegIndex >= MaxSegIndex)
4026 return "bad segIndex (too large)";
4027 for (uint32_t i = 0; i < Count; ++i) {
4028 uint32_t Start = SegOffset + i * (PointerSize + Skip);
4029 uint32_t End = Start + PointerSize;
4031 for (const SectionInfo &SI : Sections) {
4032 if (SI.SegmentIndex != SegIndex)
4034 if ((SI.OffsetInSegment<=Start) && (Start<(SI.OffsetInSegment+SI.Size))) {
4035 if (End <= SI.OffsetInSegment + SI.Size) {
4040 return "bad offset, extends beyond section boundary";
4044 return "bad offset, not in section";
4049 // For use with the SegIndex of a checked Mach-O Bind or Rebase entry
4050 // to get the segment name.
4051 StringRef BindRebaseSegInfo::segmentName(int32_t SegIndex) {
4052 for (const SectionInfo &SI : Sections) {
4053 if (SI.SegmentIndex == SegIndex)
4054 return SI.SegmentName;
4056 llvm_unreachable("invalid SegIndex");
4059 // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase
4060 // to get the SectionInfo.
4061 const BindRebaseSegInfo::SectionInfo &BindRebaseSegInfo::findSection(
4062 int32_t SegIndex, uint64_t SegOffset) {
4063 for (const SectionInfo &SI : Sections) {
4064 if (SI.SegmentIndex != SegIndex)
4066 if (SI.OffsetInSegment > SegOffset)
4068 if (SegOffset >= (SI.OffsetInSegment + SI.Size))
4072 llvm_unreachable("SegIndex and SegOffset not in any section");
4075 // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase
4076 // entry to get the section name.
4077 StringRef BindRebaseSegInfo::sectionName(int32_t SegIndex,
4078 uint64_t SegOffset) {
4079 return findSection(SegIndex, SegOffset).SectionName;
4082 // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase
4083 // entry to get the address.
4084 uint64_t BindRebaseSegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
4085 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
4086 return SI.SegmentStartAddress + OffsetInSeg;
4089 iterator_range<bind_iterator>
4090 MachOObjectFile::bindTable(Error &Err, MachOObjectFile *O,
4091 ArrayRef<uint8_t> Opcodes, bool is64,
4092 MachOBindEntry::Kind BKind) {
4093 if (O->BindRebaseSectionTable == nullptr)
4094 O->BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(O);
4095 MachOBindEntry Start(&Err, O, Opcodes, is64, BKind);
4096 Start.moveToFirst();
4098 MachOBindEntry Finish(&Err, O, Opcodes, is64, BKind);
4101 return make_range(bind_iterator(Start), bind_iterator(Finish));
4104 iterator_range<bind_iterator> MachOObjectFile::bindTable(Error &Err) {
4105 return bindTable(Err, this, getDyldInfoBindOpcodes(), is64Bit(),
4106 MachOBindEntry::Kind::Regular);
4109 iterator_range<bind_iterator> MachOObjectFile::lazyBindTable(Error &Err) {
4110 return bindTable(Err, this, getDyldInfoLazyBindOpcodes(), is64Bit(),
4111 MachOBindEntry::Kind::Lazy);
4114 iterator_range<bind_iterator> MachOObjectFile::weakBindTable(Error &Err) {
4115 return bindTable(Err, this, getDyldInfoWeakBindOpcodes(), is64Bit(),
4116 MachOBindEntry::Kind::Weak);
4119 MachOObjectFile::load_command_iterator
4120 MachOObjectFile::begin_load_commands() const {
4121 return LoadCommands.begin();
4124 MachOObjectFile::load_command_iterator
4125 MachOObjectFile::end_load_commands() const {
4126 return LoadCommands.end();
4129 iterator_range<MachOObjectFile::load_command_iterator>
4130 MachOObjectFile::load_commands() const {
4131 return make_range(begin_load_commands(), end_load_commands());
4135 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
4136 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
4137 return parseSegmentOrSectionName(Raw.data());
4141 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
4142 assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
4143 const section_base *Base =
4144 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
4145 return makeArrayRef(Base->sectname);
4149 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
4150 assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
4151 const section_base *Base =
4152 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
4153 return makeArrayRef(Base->segname);
4157 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
4159 if (getCPUType(*this) == MachO::CPU_TYPE_X86_64)
4161 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
4164 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
4165 const MachO::any_relocation_info &RE) const {
4166 if (isLittleEndian())
4167 return RE.r_word1 & 0xffffff;
4168 return RE.r_word1 >> 8;
4171 bool MachOObjectFile::getPlainRelocationExternal(
4172 const MachO::any_relocation_info &RE) const {
4173 if (isLittleEndian())
4174 return (RE.r_word1 >> 27) & 1;
4175 return (RE.r_word1 >> 4) & 1;
4178 bool MachOObjectFile::getScatteredRelocationScattered(
4179 const MachO::any_relocation_info &RE) const {
4180 return RE.r_word0 >> 31;
4183 uint32_t MachOObjectFile::getScatteredRelocationValue(
4184 const MachO::any_relocation_info &RE) const {
4188 uint32_t MachOObjectFile::getScatteredRelocationType(
4189 const MachO::any_relocation_info &RE) const {
4190 return (RE.r_word0 >> 24) & 0xf;
4193 unsigned MachOObjectFile::getAnyRelocationAddress(
4194 const MachO::any_relocation_info &RE) const {
4195 if (isRelocationScattered(RE))
4196 return getScatteredRelocationAddress(RE);
4197 return getPlainRelocationAddress(RE);
4200 unsigned MachOObjectFile::getAnyRelocationPCRel(
4201 const MachO::any_relocation_info &RE) const {
4202 if (isRelocationScattered(RE))
4203 return getScatteredRelocationPCRel(RE);
4204 return getPlainRelocationPCRel(*this, RE);
4207 unsigned MachOObjectFile::getAnyRelocationLength(
4208 const MachO::any_relocation_info &RE) const {
4209 if (isRelocationScattered(RE))
4210 return getScatteredRelocationLength(RE);
4211 return getPlainRelocationLength(*this, RE);
4215 MachOObjectFile::getAnyRelocationType(
4216 const MachO::any_relocation_info &RE) const {
4217 if (isRelocationScattered(RE))
4218 return getScatteredRelocationType(RE);
4219 return getPlainRelocationType(*this, RE);
4223 MachOObjectFile::getAnyRelocationSection(
4224 const MachO::any_relocation_info &RE) const {
4225 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
4226 return *section_end();
4227 unsigned SecNum = getPlainRelocationSymbolNum(RE);
4228 if (SecNum == MachO::R_ABS || SecNum > Sections.size())
4229 return *section_end();
4231 DRI.d.a = SecNum - 1;
4232 return SectionRef(DRI, this);
4235 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
4236 assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
4237 return getStruct<MachO::section>(*this, Sections[DRI.d.a]);
4240 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
4241 assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
4242 return getStruct<MachO::section_64>(*this, Sections[DRI.d.a]);
4245 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
4246 unsigned Index) const {
4247 const char *Sec = getSectionPtr(*this, L, Index);
4248 return getStruct<MachO::section>(*this, Sec);
4251 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
4252 unsigned Index) const {
4253 const char *Sec = getSectionPtr(*this, L, Index);
4254 return getStruct<MachO::section_64>(*this, Sec);
4258 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
4259 const char *P = reinterpret_cast<const char *>(DRI.p);
4260 return getStruct<MachO::nlist>(*this, P);
4264 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
4265 const char *P = reinterpret_cast<const char *>(DRI.p);
4266 return getStruct<MachO::nlist_64>(*this, P);
4269 MachO::linkedit_data_command
4270 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
4271 return getStruct<MachO::linkedit_data_command>(*this, L.Ptr);
4274 MachO::segment_command
4275 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
4276 return getStruct<MachO::segment_command>(*this, L.Ptr);
4279 MachO::segment_command_64
4280 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
4281 return getStruct<MachO::segment_command_64>(*this, L.Ptr);
4284 MachO::linker_option_command
4285 MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const {
4286 return getStruct<MachO::linker_option_command>(*this, L.Ptr);
4289 MachO::version_min_command
4290 MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
4291 return getStruct<MachO::version_min_command>(*this, L.Ptr);
4295 MachOObjectFile::getNoteLoadCommand(const LoadCommandInfo &L) const {
4296 return getStruct<MachO::note_command>(*this, L.Ptr);
4299 MachO::build_version_command
4300 MachOObjectFile::getBuildVersionLoadCommand(const LoadCommandInfo &L) const {
4301 return getStruct<MachO::build_version_command>(*this, L.Ptr);
4304 MachO::build_tool_version
4305 MachOObjectFile::getBuildToolVersion(unsigned index) const {
4306 return getStruct<MachO::build_tool_version>(*this, BuildTools[index]);
4309 MachO::dylib_command
4310 MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
4311 return getStruct<MachO::dylib_command>(*this, L.Ptr);
4314 MachO::dyld_info_command
4315 MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const {
4316 return getStruct<MachO::dyld_info_command>(*this, L.Ptr);
4319 MachO::dylinker_command
4320 MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const {
4321 return getStruct<MachO::dylinker_command>(*this, L.Ptr);
4325 MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const {
4326 return getStruct<MachO::uuid_command>(*this, L.Ptr);
4329 MachO::rpath_command
4330 MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const {
4331 return getStruct<MachO::rpath_command>(*this, L.Ptr);
4334 MachO::source_version_command
4335 MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const {
4336 return getStruct<MachO::source_version_command>(*this, L.Ptr);
4339 MachO::entry_point_command
4340 MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const {
4341 return getStruct<MachO::entry_point_command>(*this, L.Ptr);
4344 MachO::encryption_info_command
4345 MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const {
4346 return getStruct<MachO::encryption_info_command>(*this, L.Ptr);
4349 MachO::encryption_info_command_64
4350 MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const {
4351 return getStruct<MachO::encryption_info_command_64>(*this, L.Ptr);
4354 MachO::sub_framework_command
4355 MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const {
4356 return getStruct<MachO::sub_framework_command>(*this, L.Ptr);
4359 MachO::sub_umbrella_command
4360 MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const {
4361 return getStruct<MachO::sub_umbrella_command>(*this, L.Ptr);
4364 MachO::sub_library_command
4365 MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const {
4366 return getStruct<MachO::sub_library_command>(*this, L.Ptr);
4369 MachO::sub_client_command
4370 MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const {
4371 return getStruct<MachO::sub_client_command>(*this, L.Ptr);
4374 MachO::routines_command
4375 MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const {
4376 return getStruct<MachO::routines_command>(*this, L.Ptr);
4379 MachO::routines_command_64
4380 MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const {
4381 return getStruct<MachO::routines_command_64>(*this, L.Ptr);
4384 MachO::thread_command
4385 MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const {
4386 return getStruct<MachO::thread_command>(*this, L.Ptr);
4389 MachO::any_relocation_info
4390 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
4392 if (getHeader().filetype == MachO::MH_OBJECT) {
4396 MachO::section_64 Sect = getSection64(Sec);
4397 Offset = Sect.reloff;
4399 MachO::section Sect = getSection(Sec);
4400 Offset = Sect.reloff;
4403 MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand();
4405 Offset = DysymtabLoadCmd.extreloff; // Offset to the external relocations
4407 Offset = DysymtabLoadCmd.locreloff; // Offset to the local relocations
4410 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
4411 getPtr(*this, Offset)) + Rel.d.b;
4412 return getStruct<MachO::any_relocation_info>(
4413 *this, reinterpret_cast<const char *>(P));
4416 MachO::data_in_code_entry
4417 MachOObjectFile::getDice(DataRefImpl Rel) const {
4418 const char *P = reinterpret_cast<const char *>(Rel.p);
4419 return getStruct<MachO::data_in_code_entry>(*this, P);
4422 const MachO::mach_header &MachOObjectFile::getHeader() const {
4426 const MachO::mach_header_64 &MachOObjectFile::getHeader64() const {
4431 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
4432 const MachO::dysymtab_command &DLC,
4433 unsigned Index) const {
4434 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
4435 return getStruct<uint32_t>(*this, getPtr(*this, Offset));
4438 MachO::data_in_code_entry
4439 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
4440 unsigned Index) const {
4441 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
4442 return getStruct<MachO::data_in_code_entry>(*this, getPtr(*this, Offset));
4445 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
4447 return getStruct<MachO::symtab_command>(*this, SymtabLoadCmd);
4449 // If there is no SymtabLoadCmd return a load command with zero'ed fields.
4450 MachO::symtab_command Cmd;
4451 Cmd.cmd = MachO::LC_SYMTAB;
4452 Cmd.cmdsize = sizeof(MachO::symtab_command);
4460 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
4461 if (DysymtabLoadCmd)
4462 return getStruct<MachO::dysymtab_command>(*this, DysymtabLoadCmd);
4464 // If there is no DysymtabLoadCmd return a load command with zero'ed fields.
4465 MachO::dysymtab_command Cmd;
4466 Cmd.cmd = MachO::LC_DYSYMTAB;
4467 Cmd.cmdsize = sizeof(MachO::dysymtab_command);
4478 Cmd.extrefsymoff = 0;
4479 Cmd.nextrefsyms = 0;
4480 Cmd.indirectsymoff = 0;
4481 Cmd.nindirectsyms = 0;
4489 MachO::linkedit_data_command
4490 MachOObjectFile::getDataInCodeLoadCommand() const {
4491 if (DataInCodeLoadCmd)
4492 return getStruct<MachO::linkedit_data_command>(*this, DataInCodeLoadCmd);
4494 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
4495 MachO::linkedit_data_command Cmd;
4496 Cmd.cmd = MachO::LC_DATA_IN_CODE;
4497 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
4503 MachO::linkedit_data_command
4504 MachOObjectFile::getLinkOptHintsLoadCommand() const {
4505 if (LinkOptHintsLoadCmd)
4506 return getStruct<MachO::linkedit_data_command>(*this, LinkOptHintsLoadCmd);
4508 // If there is no LinkOptHintsLoadCmd return a load command with zero'ed
4510 MachO::linkedit_data_command Cmd;
4511 Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
4512 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
4518 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const {
4519 if (!DyldInfoLoadCmd)
4522 auto DyldInfoOrErr =
4523 getStructOrErr<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
4526 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
4527 const uint8_t *Ptr =
4528 reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.rebase_off));
4529 return makeArrayRef(Ptr, DyldInfo.rebase_size);
4532 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const {
4533 if (!DyldInfoLoadCmd)
4536 auto DyldInfoOrErr =
4537 getStructOrErr<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
4540 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
4541 const uint8_t *Ptr =
4542 reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.bind_off));
4543 return makeArrayRef(Ptr, DyldInfo.bind_size);
4546 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const {
4547 if (!DyldInfoLoadCmd)
4550 auto DyldInfoOrErr =
4551 getStructOrErr<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
4554 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
4555 const uint8_t *Ptr =
4556 reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.weak_bind_off));
4557 return makeArrayRef(Ptr, DyldInfo.weak_bind_size);
4560 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const {
4561 if (!DyldInfoLoadCmd)
4564 auto DyldInfoOrErr =
4565 getStructOrErr<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
4568 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
4569 const uint8_t *Ptr =
4570 reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.lazy_bind_off));
4571 return makeArrayRef(Ptr, DyldInfo.lazy_bind_size);
4574 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const {
4575 if (!DyldInfoLoadCmd)
4578 auto DyldInfoOrErr =
4579 getStructOrErr<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
4582 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
4583 const uint8_t *Ptr =
4584 reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.export_off));
4585 return makeArrayRef(Ptr, DyldInfo.export_size);
4588 ArrayRef<uint8_t> MachOObjectFile::getUuid() const {
4591 // Returning a pointer is fine as uuid doesn't need endian swapping.
4592 const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid);
4593 return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16);
4596 StringRef MachOObjectFile::getStringTableData() const {
4597 MachO::symtab_command S = getSymtabLoadCommand();
4598 return getData().substr(S.stroff, S.strsize);
4601 bool MachOObjectFile::is64Bit() const {
4602 return getType() == getMachOType(false, true) ||
4603 getType() == getMachOType(true, true);
4606 void MachOObjectFile::ReadULEB128s(uint64_t Index,
4607 SmallVectorImpl<uint64_t> &Out) const {
4608 DataExtractor extractor(ObjectFile::getData(), true, 0);
4610 uint64_t offset = Index;
4612 while (uint64_t delta = extractor.getULEB128(&offset)) {
4614 Out.push_back(data);
4618 bool MachOObjectFile::isRelocatableObject() const {
4619 return getHeader().filetype == MachO::MH_OBJECT;
4622 Expected<std::unique_ptr<MachOObjectFile>>
4623 ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer,
4624 uint32_t UniversalCputype,
4625 uint32_t UniversalIndex) {
4626 StringRef Magic = Buffer.getBuffer().slice(0, 4);
4627 if (Magic == "\xFE\xED\xFA\xCE")
4628 return MachOObjectFile::create(Buffer, false, false,
4629 UniversalCputype, UniversalIndex);
4630 if (Magic == "\xCE\xFA\xED\xFE")
4631 return MachOObjectFile::create(Buffer, true, false,
4632 UniversalCputype, UniversalIndex);
4633 if (Magic == "\xFE\xED\xFA\xCF")
4634 return MachOObjectFile::create(Buffer, false, true,
4635 UniversalCputype, UniversalIndex);
4636 if (Magic == "\xCF\xFA\xED\xFE")
4637 return MachOObjectFile::create(Buffer, true, true,
4638 UniversalCputype, UniversalIndex);
4639 return make_error<GenericBinaryError>("Unrecognized MachO magic number",
4640 object_error::invalid_file_type);
4643 StringRef MachOObjectFile::mapDebugSectionName(StringRef Name) const {
4644 return StringSwitch<StringRef>(Name)
4645 .Case("debug_str_offs", "debug_str_offsets")