49#include <system_error>
66 object_error::parse_failed);
73 if (
P < O.getData().begin() ||
P +
sizeof(
T) > O.getData().end())
77 memcpy(&Cmd,
P,
sizeof(
T));
86 if (
P < O.getData().begin() ||
P +
sizeof(
T) > O.getData().end())
90 memcpy(&Cmd,
P,
sizeof(
T));
101 bool Is64 = O.is64Bit();
107 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
108 return reinterpret_cast<const char*
>(SectionAddr);
112 size_t MachOFilesetEntryOffset = 0) {
114 MachOFilesetEntryOffset <= O.getData().size());
115 return O.getData().data() +
Offset + MachOFilesetEntryOffset;
120 const char *
P =
reinterpret_cast<const char *
>(DRI.
p);
133 return O.getHeader().cputype;
137 return O.getHeader().cpusubtype &
~MachO::CPU_SUBTYPE_MASK;
152 if (O.isLittleEndian())
164 if (O.isLittleEndian())
176 if (O.isLittleEndian())
195 assert(Ptr <= Obj.getData().end() &&
"Start must be before end");
196 if (CmdOrErr->cmdsize > (
uintptr_t)(Obj.getData().end() - Ptr))
198 " extends past end of file");
199 if (CmdOrErr->cmdsize < 8)
201 " with size less than 8 bytes");
212 return malformedError(
"load command 0 extends past the end all load "
213 "commands in the file");
215 Obj,
getPtr(Obj, HeaderSize, Obj.getMachOFilesetEntryOffset()), 0);
224 Obj.getData().data() + Obj.getMachOFilesetEntryOffset() + HeaderSize +
225 Obj.getHeader().sizeofcmds)
227 " extends past the end all load commands in the file");
234 if (
sizeof(
T) > Obj.getData().size()) {
235 Err =
malformedError(
"the mach header extends past the end of the "
240 Obj,
getPtr(Obj, 0, Obj.getMachOFilesetEntryOffset())))
241 Header = *HeaderOrErr;
243 Err = HeaderOrErr.takeError();
259 for (
auto it = Elements.begin(); it != Elements.end(); ++it) {
265 " with a size of " +
Twine(
Size) +
", overlaps " +
266 E.Name +
" at offset " +
Twine(
E.Offset) +
" with "
267 "a size of " +
Twine(
E.Size));
270 if (nt != Elements.end()) {
285template <
typename Segment,
typename Section>
290 std::list<MachOElement> &Elements) {
291 const unsigned SegmentLoadSize =
sizeof(Segment);
292 if (
Load.C.cmdsize < SegmentLoadSize)
294 " " + CmdName +
" cmdsize too small");
296 Segment S = SegOrErr.get();
297 const unsigned SectionSize =
sizeof(Section);
298 uint64_t FileSize = Obj.getData().size();
299 if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
300 S.nsects * SectionSize >
Load.C.cmdsize - SegmentLoadSize)
302 " inconsistent cmdsize in " + CmdName +
303 " for the number of sections");
304 for (
unsigned J = 0; J < S.nsects; ++J) {
309 return SectionOrErr.takeError();
310 Section s = SectionOrErr.get();
317 CmdName +
" command " +
Twine(LoadCommandIndex) +
318 " extends past the end of the file");
323 s.offset < SizeOfHeaders && s.size != 0)
325 CmdName +
" command " +
Twine(LoadCommandIndex) +
326 " not past the headers of the file");
334 return malformedError(
"offset field plus size field of section " +
335 Twine(J) +
" in " + CmdName +
" command " +
336 Twine(LoadCommandIndex) +
337 " extends past the end of the file");
344 Twine(J) +
" in " + CmdName +
" command " +
345 Twine(LoadCommandIndex) +
346 " greater than the segment");
351 CmdName +
" command " +
Twine(LoadCommandIndex) +
352 " less than the segment's vmaddr");
357 if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd)
359 " in " + CmdName +
" command " +
360 Twine(LoadCommandIndex) +
361 " greater than than "
362 "the segment's vmaddr plus vmsize");
370 if (s.reloff > FileSize)
372 CmdName +
" command " +
Twine(LoadCommandIndex) +
373 " extends past the end of the file");
377 if (BigSize > FileSize)
378 return malformedError(
"reloff field plus nreloc field times sizeof("
379 "struct relocation_info) of section " +
380 Twine(J) +
" in " + CmdName +
" command " +
381 Twine(LoadCommandIndex) +
382 " extends past the end of the file");
386 "section relocation entries"))
389 if (S.fileoff > FileSize)
391 " fileoff field in " + CmdName +
392 " extends past the end of the file");
394 BigSize += S.filesize;
395 if (BigSize > FileSize)
397 " fileoff field plus filesize field in " +
398 CmdName +
" extends past the end of the file");
399 if (S.vmsize != 0 && S.filesize > S.vmsize)
401 " filesize field in " + CmdName +
402 " greater than vmsize field");
403 IsPageZeroSegment |=
StringRef(
"__PAGEZERO") == S.segname;
405 return SegOrErr.takeError();
413 const char **SymtabLoadCmd,
414 std::list<MachOElement> &Elements) {
417 " LC_SYMTAB cmdsize too small");
418 if (*SymtabLoadCmd !=
nullptr)
422 return SymtabOrErr.takeError();
426 " has incorrect cmdsize");
427 uint64_t FileSize = Obj.getData().size();
428 if (Symtab.
symoff > FileSize)
430 Twine(LoadCommandIndex) +
" extends past the end "
433 const char *struct_nlist_name;
436 struct_nlist_name =
"struct nlist_64";
439 struct_nlist_name =
"struct nlist";
443 if (BigSize > FileSize)
444 return malformedError(
"symoff field plus nsyms field times sizeof(" +
445 Twine(struct_nlist_name) +
") of LC_SYMTAB command " +
446 Twine(LoadCommandIndex) +
" extends past the end "
451 if (Symtab.
stroff > FileSize)
453 Twine(LoadCommandIndex) +
" extends past the end "
457 if (BigSize > FileSize)
458 return malformedError(
"stroff field plus strsize field of LC_SYMTAB "
459 "command " +
Twine(LoadCommandIndex) +
" extends "
460 "past the end of the file");
462 Symtab.
strsize,
"string table"))
464 *SymtabLoadCmd =
Load.Ptr;
471 const char **DysymtabLoadCmd,
472 std::list<MachOElement> &Elements) {
475 " LC_DYSYMTAB cmdsize too small");
476 if (*DysymtabLoadCmd !=
nullptr)
481 return DysymtabOrErr.takeError();
485 " has incorrect cmdsize");
486 uint64_t FileSize = Obj.getData().size();
487 if (Dysymtab.
tocoff > FileSize)
489 Twine(LoadCommandIndex) +
" extends past the end of "
493 BigSize += Dysymtab.
tocoff;
494 if (BigSize > FileSize)
495 return malformedError(
"tocoff field plus ntoc field times sizeof(struct "
496 "dylib_table_of_contents) of LC_DYSYMTAB command " +
497 Twine(LoadCommandIndex) +
" extends past the end of "
500 Dysymtab.
ntoc *
sizeof(
struct
502 "table of contents"))
506 Twine(LoadCommandIndex) +
" extends past the end of "
509 const char *struct_dylib_module_name;
513 struct_dylib_module_name =
"struct dylib_module_64";
516 struct_dylib_module_name =
"struct dylib_module";
518 BigSize *= sizeof_modtab;
520 if (BigSize > FileSize)
521 return malformedError(
"modtaboff field plus nmodtab field times sizeof(" +
522 Twine(struct_dylib_module_name) +
") of LC_DYSYMTAB "
523 "command " +
Twine(LoadCommandIndex) +
" extends "
524 "past the end of the file");
526 Dysymtab.
nmodtab * sizeof_modtab,
530 return malformedError(
"extrefsymoff field of LC_DYSYMTAB command " +
531 Twine(LoadCommandIndex) +
" extends past the end of "
536 if (BigSize > FileSize)
537 return malformedError(
"extrefsymoff field plus nextrefsyms field times "
538 "sizeof(struct dylib_reference) of LC_DYSYMTAB "
539 "command " +
Twine(LoadCommandIndex) +
" extends "
540 "past the end of the file");
547 return malformedError(
"indirectsymoff field of LC_DYSYMTAB command " +
548 Twine(LoadCommandIndex) +
" extends past the end of "
553 if (BigSize > FileSize)
554 return malformedError(
"indirectsymoff field plus nindirectsyms field times "
555 "sizeof(uint32_t) of LC_DYSYMTAB command " +
556 Twine(LoadCommandIndex) +
" extends past the end of "
565 Twine(LoadCommandIndex) +
" extends past the end of "
570 if (BigSize > FileSize)
571 return malformedError(
"extreloff field plus nextrel field times sizeof"
572 "(struct relocation_info) of LC_DYSYMTAB command " +
573 Twine(LoadCommandIndex) +
" extends past the end of "
578 "external relocation table"))
582 Twine(LoadCommandIndex) +
" extends past the end of "
587 if (BigSize > FileSize)
588 return malformedError(
"locreloff field plus nlocrel field times sizeof"
589 "(struct relocation_info) of LC_DYSYMTAB command " +
590 Twine(LoadCommandIndex) +
" extends past the end of "
595 "local relocation table"))
597 *DysymtabLoadCmd =
Load.Ptr;
604 const char **LoadCmd,
const char *CmdName,
605 std::list<MachOElement> &Elements,
606 const char *ElementName) {
609 CmdName +
" cmdsize too small");
610 if (*LoadCmd !=
nullptr)
612 auto LinkDataOrError =
614 if (!LinkDataOrError)
615 return LinkDataOrError.takeError();
619 Twine(LoadCommandIndex) +
" has incorrect cmdsize");
620 uint64_t FileSize = Obj.getData().size();
621 if (LinkData.
dataoff > FileSize)
623 Twine(LoadCommandIndex) +
" extends past the end of "
627 if (BigSize > FileSize)
629 Twine(CmdName) +
" command " +
630 Twine(LoadCommandIndex) +
" extends past the end of "
642 const char **LoadCmd,
const char *CmdName,
643 std::list<MachOElement> &Elements) {
646 CmdName +
" cmdsize too small");
647 if (*LoadCmd !=
nullptr)
648 return malformedError(
"more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY "
653 return DyldInfoOrErr.takeError();
657 Twine(LoadCommandIndex) +
" has incorrect cmdsize");
658 uint64_t FileSize = Obj.getData().size();
661 " command " +
Twine(LoadCommandIndex) +
" extends "
662 "past the end of the file");
665 if (BigSize > FileSize)
666 return malformedError(
"rebase_off field plus rebase_size field of " +
667 Twine(CmdName) +
" command " +
668 Twine(LoadCommandIndex) +
" extends past the end of "
676 " command " +
Twine(LoadCommandIndex) +
" extends "
677 "past the end of the file");
680 if (BigSize > FileSize)
682 Twine(CmdName) +
" command " +
683 Twine(LoadCommandIndex) +
" extends past the end of "
691 " command " +
Twine(LoadCommandIndex) +
" extends "
692 "past the end of the file");
695 if (BigSize > FileSize)
696 return malformedError(
"weak_bind_off field plus weak_bind_size field of " +
697 Twine(CmdName) +
" command " +
698 Twine(LoadCommandIndex) +
" extends past the end of "
702 "dyld weak bind info"))
706 " command " +
Twine(LoadCommandIndex) +
" extends "
707 "past the end of the file");
710 if (BigSize > FileSize)
711 return malformedError(
"lazy_bind_off field plus lazy_bind_size field of " +
712 Twine(CmdName) +
" command " +
713 Twine(LoadCommandIndex) +
" extends past the end of "
717 "dyld lazy bind info"))
721 " command " +
Twine(LoadCommandIndex) +
" extends "
722 "past the end of the file");
725 if (BigSize > FileSize)
726 return malformedError(
"export_off field plus export_size field of " +
727 Twine(CmdName) +
" command " +
728 Twine(LoadCommandIndex) +
" extends past the end of "
740 uint32_t LoadCommandIndex,
const char *CmdName) {
743 CmdName +
" cmdsize too small");
746 return CommandOrErr.takeError();
750 CmdName +
" name.offset field too small, not past "
751 "the end of the dylib_command struct");
752 if (
D.dylib.name >=
D.cmdsize)
754 CmdName +
" name.offset field extends past the end "
755 "of the load command");
759 const char *
P = (
const char *)
Load.Ptr;
760 for (i =
D.dylib.name; i <
D.cmdsize; i++)
765 CmdName +
" library name extends past the end of the "
773 const char **LoadCmd) {
777 if (*LoadCmd !=
nullptr)
781 return malformedError(
"LC_ID_DYLIB load command in non-dynamic library "
789 uint32_t LoadCommandIndex,
const char *CmdName) {
792 CmdName +
" cmdsize too small");
795 return CommandOrErr.takeError();
799 CmdName +
" name.offset field too small, not past "
800 "the end of the dylinker_command struct");
801 if (
D.name >=
D.cmdsize)
803 CmdName +
" name.offset field extends past the end "
804 "of the load command");
808 const char *
P = (
const char *)
Load.Ptr;
809 for (i =
D.name; i <
D.cmdsize; i++)
814 CmdName +
" dyld name extends past the end of the "
822 const char **LoadCmd,
const char *CmdName) {
825 CmdName +
" has incorrect cmdsize");
826 if (*LoadCmd !=
nullptr)
828 "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or "
829 "LC_VERSION_MIN_WATCHOS command");
837 std::list<MachOElement> &Elements) {
840 " LC_NOTE has incorrect cmdsize");
843 return NoteCmdOrErr.takeError();
845 uint64_t FileSize = Obj.getData().size();
848 Twine(LoadCommandIndex) +
" extends "
849 "past the end of the file");
852 if (BigSize > FileSize)
853 return malformedError(
"size field plus offset field of LC_NOTE command " +
854 Twine(LoadCommandIndex) +
" extends past the end of "
870 return BVCOrErr.takeError();
872 if (
Load.C.cmdsize !=
876 " LC_BUILD_VERSION_COMMAND has incorrect cmdsize");
880 for (
unsigned i = 0; i < BVC.
ntools; ++i)
893 " LC_TARGET_TRIPLE cmdsize too small");
897 return TTOrErr.takeError();
903 "load command " +
Twine(LoadCommandIndex) +
904 " LC_TARGET_TRIPLE triple.offset field too small, not past the end of "
905 "the target_triple_command struct");
908 if (TT.triple >= TT.cmdsize)
910 " LC_TARGET_TRIPLE triple.offset field extends past "
911 "the end of the load command");
916 const char *
P = (
const char *)
Load.Ptr;
917 for (i = TT.triple; i < TT.cmdsize; i++)
922 " LC_TARGET_TRIPLE triple name extends past the end "
923 "of the load command");
933 " LC_RPATH cmdsize too small");
936 return ROrErr.takeError();
940 " LC_RPATH path.offset field too small, not past "
941 "the end of the rpath_command struct");
942 if (R.path >= R.cmdsize)
944 " LC_RPATH path.offset field extends past the end "
945 "of the load command");
949 const char *
P = (
const char *)
Load.Ptr;
950 for (i = R.path; i < R.cmdsize; i++)
955 " LC_RPATH library name extends past the end of the "
964 const char **LoadCmd,
const char *CmdName) {
965 if (*LoadCmd !=
nullptr)
967 "LC_ENCRYPTION_INFO_64 command");
968 uint64_t FileSize = Obj.getData().size();
969 if (cryptoff > FileSize)
971 " command " +
Twine(LoadCommandIndex) +
" extends "
972 "past the end of the file");
974 BigSize += cryptsize;
975 if (BigSize > FileSize)
977 Twine(CmdName) +
" command " +
978 Twine(LoadCommandIndex) +
" extends past the end of "
989 " LC_LINKER_OPTION cmdsize too small");
990 auto LinkOptionOrErr =
992 if (!LinkOptionOrErr)
993 return LinkOptionOrErr.takeError();
996 const char *
string = (
const char *)
Load.Ptr +
1001 while (*
string ==
'\0' && left > 0) {
1008 if (0xffffffff == NullPos)
1010 " LC_LINKER_OPTION string #" +
Twine(i) +
1011 " is not NULL terminated");
1012 uint32_t len = std::min(NullPos, left) + 1;
1019 " LC_LINKER_OPTION string count " +
Twine(L.count) +
1020 " does not match number of strings");
1026 uint32_t LoadCommandIndex,
const char *CmdName,
1027 size_t SizeOfCmd,
const char *CmdStructName,
1028 uint32_t PathOffset,
const char *PathFieldName) {
1029 if (PathOffset < SizeOfCmd)
1031 CmdName +
" " + PathFieldName +
".offset field too "
1032 "small, not past the end of the " + CmdStructName);
1033 if (PathOffset >=
Load.C.cmdsize)
1035 CmdName +
" " + PathFieldName +
".offset field "
1036 "extends past the end of the load command");
1040 const char *
P = (
const char *)
Load.Ptr;
1041 for (i = PathOffset; i <
Load.C.cmdsize; i++)
1044 if (i >=
Load.C.cmdsize)
1046 CmdName +
" " + PathFieldName +
" name extends past "
1047 "the end of the load command");
1054 const char *CmdName) {
1057 CmdName +
" cmdsize too small");
1058 auto ThreadCommandOrErr =
1060 if (!ThreadCommandOrErr)
1061 return ThreadCommandOrErr.takeError();
1064 const char *end =
Load.Ptr +
T.cmdsize;
1067 while (state < end) {
1070 "flavor in " + CmdName +
" extends past end of "
1073 memcpy(&flavor, state,
sizeof(
uint32_t));
1080 " count in " + CmdName +
" extends past end of "
1092 " count not x86_THREAD_STATE32_COUNT for "
1093 "flavor number " +
Twine(nflavor) +
" which is "
1094 "a x86_THREAD_STATE32 flavor in " + CmdName +
1098 " x86_THREAD_STATE32 extends past end of "
1099 "command in " + CmdName +
" command");
1103 " unknown flavor (" +
Twine(flavor) +
") for "
1104 "flavor number " +
Twine(nflavor) +
" in " +
1105 CmdName +
" command");
1111 " count not x86_THREAD_STATE_COUNT for "
1112 "flavor number " +
Twine(nflavor) +
" which is "
1113 "a x86_THREAD_STATE flavor in " + CmdName +
1117 " x86_THREAD_STATE extends past end of "
1118 "command in " + CmdName +
" command");
1123 " count not x86_FLOAT_STATE_COUNT for "
1124 "flavor number " +
Twine(nflavor) +
" which is "
1125 "a x86_FLOAT_STATE flavor in " + CmdName +
1129 " x86_FLOAT_STATE extends past end of "
1130 "command in " + CmdName +
" command");
1135 " count not x86_EXCEPTION_STATE_COUNT for "
1136 "flavor number " +
Twine(nflavor) +
" which is "
1137 "a x86_EXCEPTION_STATE flavor in " + CmdName +
1141 " x86_EXCEPTION_STATE extends past end of "
1142 "command in " + CmdName +
" command");
1147 " count not x86_THREAD_STATE64_COUNT for "
1148 "flavor number " +
Twine(nflavor) +
" which is "
1149 "a x86_THREAD_STATE64 flavor in " + CmdName +
1153 " x86_THREAD_STATE64 extends past end of "
1154 "command in " + CmdName +
" command");
1159 " count not x86_EXCEPTION_STATE64_COUNT for "
1160 "flavor number " +
Twine(nflavor) +
" which is "
1161 "a x86_EXCEPTION_STATE64 flavor in " + CmdName +
1165 " x86_EXCEPTION_STATE64 extends past end of "
1166 "command in " + CmdName +
" command");
1170 " unknown flavor (" +
Twine(flavor) +
") for "
1171 "flavor number " +
Twine(nflavor) +
" in " +
1172 CmdName +
" command");
1178 " count not ARM_THREAD_STATE_COUNT for "
1179 "flavor number " +
Twine(nflavor) +
" which is "
1180 "a ARM_THREAD_STATE flavor in " + CmdName +
1184 " ARM_THREAD_STATE extends past end of "
1185 "command in " + CmdName +
" command");
1189 " unknown flavor (" +
Twine(flavor) +
") for "
1190 "flavor number " +
Twine(nflavor) +
" in " +
1191 CmdName +
" command");
1198 " count not ARM_THREAD_STATE64_COUNT for "
1199 "flavor number " +
Twine(nflavor) +
" which is "
1200 "a ARM_THREAD_STATE64 flavor in " + CmdName +
1204 " ARM_THREAD_STATE64 extends past end of "
1205 "command in " + CmdName +
" command");
1209 " unknown flavor (" +
Twine(flavor) +
") for "
1210 "flavor number " +
Twine(nflavor) +
" in " +
1211 CmdName +
" command");
1217 " count not PPC_THREAD_STATE_COUNT for "
1218 "flavor number " +
Twine(nflavor) +
" which is "
1219 "a PPC_THREAD_STATE flavor in " + CmdName +
1223 " PPC_THREAD_STATE extends past end of "
1224 "command in " + CmdName +
" command");
1228 " unknown flavor (" +
Twine(flavor) +
") for "
1229 "flavor number " +
Twine(nflavor) +
" in " +
1230 CmdName +
" command");
1234 "command " +
Twine(LoadCommandIndex) +
" for " +
1235 CmdName +
" command can't be checked");
1246 const char **LoadCmd,
1247 std::list<MachOElement> &Elements) {
1250 " LC_TWOLEVEL_HINTS has incorrect cmdsize");
1251 if (*LoadCmd !=
nullptr)
1252 return malformedError(
"more than one LC_TWOLEVEL_HINTS command");
1255 return HintsOrErr.takeError();
1257 uint64_t FileSize = Obj.getData().size();
1258 if (Hints.
offset > FileSize)
1259 return malformedError(
"offset field of LC_TWOLEVEL_HINTS command " +
1260 Twine(LoadCommandIndex) +
" extends past the end of "
1265 if (BigSize > FileSize)
1266 return malformedError(
"offset field plus nhints times sizeof(struct "
1267 "twolevel_hint) field of LC_TWOLEVEL_HINTS command " +
1268 Twine(LoadCommandIndex) +
" extends past the end of "
1274 *LoadCmd =
Load.Ptr;
1282 if (
cmd == MachO::LC_SYMSEG ||
1283 cmd == MachO::LC_LOADFVMLIB ||
1284 cmd == MachO::LC_IDFVMLIB ||
1285 cmd == MachO::LC_IDENT ||
1286 cmd == MachO::LC_FVMFILE ||
1287 cmd == MachO::LC_PREPAGE ||
1288 cmd == MachO::LC_PREBOUND_DYLIB ||
1289 cmd == MachO::LC_TWOLEVEL_HINTS ||
1290 cmd == MachO::LC_PREBIND_CKSUM)
1297 bool Is64Bits,
uint32_t UniversalCputype,
1299 size_t MachOFilesetEntryOffset) {
1301 std::unique_ptr<MachOObjectFile> Obj(
new MachOObjectFile(
1302 std::move(Object), IsLittleEndian, Is64Bits, Err, UniversalCputype,
1303 UniversalIndex, MachOFilesetEntryOffset));
1305 return std::move(Err);
1306 return std::move(Obj);
1309MachOObjectFile::MachOObjectFile(
MemoryBufferRef Object,
bool IsLittleEndian,
1310 bool Is64bits,
Error &Err,
1313 size_t MachOFilesetEntryOffset)
1314 :
ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
1315 MachOFilesetEntryOffset(MachOFilesetEntryOffset) {
1317 uint64_t SizeOfHeaders;
1326 cputype = Header.cputype;
1332 Err =
malformedError(
"load commands extend past the end of the file");
1335 if (UniversalCputype != 0 && cputype != UniversalCputype) {
1337 Twine(UniversalIndex) +
"'s cputype does not match "
1338 "object file's mach header");
1342 Elements.push_back({0, SizeOfHeaders,
"Mach-O headers"});
1346 if (LoadCommandCount != 0) {
1350 Err = LoadOrErr.takeError();
1355 const char *DyldIdLoadCmd =
nullptr;
1356 const char *SplitInfoLoadCmd =
nullptr;
1357 const char *CodeSignDrsLoadCmd =
nullptr;
1358 const char *CodeSignLoadCmd =
nullptr;
1359 const char *VersLoadCmd =
nullptr;
1360 const char *SourceLoadCmd =
nullptr;
1361 const char *EntryPointLoadCmd =
nullptr;
1362 const char *EncryptLoadCmd =
nullptr;
1363 const char *RoutinesLoadCmd =
nullptr;
1364 const char *UnixThreadLoadCmd =
nullptr;
1365 const char *TwoLevelHintsLoadCmd =
nullptr;
1366 for (
unsigned I = 0;
I < LoadCommandCount; ++
I) {
1368 if (
Load.C.cmdsize % 8 != 0) {
1373 Load.C.cmd != MachO::LC_THREAD ||
Load.C.cmdsize % 4) {
1380 if (
Load.C.cmdsize % 4 != 0) {
1386 LoadCommands.push_back(
Load);
1387 if (
Load.C.cmd == MachO::LC_SYMTAB) {
1390 }
else if (
Load.C.cmd == MachO::LC_DYSYMTAB) {
1394 }
else if (
Load.C.cmd == MachO::LC_DATA_IN_CODE) {
1396 "LC_DATA_IN_CODE", Elements,
1397 "data in code info")))
1399 }
else if (
Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
1401 "LC_LINKER_OPTIMIZATION_HINT",
1402 Elements,
"linker optimization "
1405 }
else if (
Load.C.cmd == MachO::LC_FUNCTION_STARTS) {
1407 "LC_FUNCTION_STARTS", Elements,
1408 "function starts data")))
1410 }
else if (
Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) {
1412 "LC_SEGMENT_SPLIT_INFO", Elements,
1413 "split info data")))
1415 }
else if (
Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) {
1417 "LC_DYLIB_CODE_SIGN_DRS", Elements,
1418 "code signing RDs data")))
1420 }
else if (
Load.C.cmd == MachO::LC_CODE_SIGNATURE) {
1422 "LC_CODE_SIGNATURE", Elements,
1423 "code signature data")))
1425 }
else if (
Load.C.cmd == MachO::LC_DYLD_INFO) {
1427 "LC_DYLD_INFO", Elements)))
1429 }
else if (
Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
1431 "LC_DYLD_INFO_ONLY", Elements)))
1433 }
else if (
Load.C.cmd == MachO::LC_DYLD_CHAINED_FIXUPS) {
1435 *
this,
Load,
I, &DyldChainedFixupsLoadCmd,
1436 "LC_DYLD_CHAINED_FIXUPS", Elements,
"chained fixups")))
1438 }
else if (
Load.C.cmd == MachO::LC_DYLD_EXPORTS_TRIE) {
1440 *
this,
Load,
I, &DyldExportsTrieLoadCmd,
"LC_DYLD_EXPORTS_TRIE",
1441 Elements,
"exports trie")))
1443 }
else if (
Load.C.cmd == MachO::LC_UUID) {
1444 if (
Load.C.cmdsize !=
sizeof(MachO::uuid_command)) {
1445 Err =
malformedError(
"LC_UUID command " + Twine(
I) +
" has incorrect "
1453 UuidLoadCmd =
Load.Ptr;
1454 }
else if (
Load.C.cmd == MachO::LC_SEGMENT_64) {
1457 *
this,
Load, Sections, HasPageZeroSegment,
I,
1458 "LC_SEGMENT_64", SizeOfHeaders, Elements)))
1460 }
else if (
Load.C.cmd == MachO::LC_SEGMENT) {
1463 *
this,
Load, Sections, HasPageZeroSegment,
I,
1464 "LC_SEGMENT", SizeOfHeaders, Elements)))
1466 }
else if (
Load.C.cmd == MachO::LC_ID_DYLIB) {
1469 }
else if (
Load.C.cmd == MachO::LC_LOAD_DYLIB) {
1472 Libraries.push_back(
Load.Ptr);
1473 }
else if (
Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) {
1476 Libraries.push_back(
Load.Ptr);
1477 }
else if (
Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) {
1480 Libraries.push_back(
Load.Ptr);
1481 }
else if (
Load.C.cmd == MachO::LC_REEXPORT_DYLIB) {
1484 Libraries.push_back(
Load.Ptr);
1485 }
else if (
Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
1488 Libraries.push_back(
Load.Ptr);
1489 }
else if (
Load.C.cmd == MachO::LC_ID_DYLINKER) {
1492 }
else if (
Load.C.cmd == MachO::LC_LOAD_DYLINKER) {
1495 }
else if (
Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
1498 }
else if (
Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
1500 "LC_VERSION_MIN_MACOSX")))
1502 }
else if (
Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
1504 "LC_VERSION_MIN_IPHONEOS")))
1506 }
else if (
Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) {
1508 "LC_VERSION_MIN_TVOS")))
1510 }
else if (
Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
1512 "LC_VERSION_MIN_WATCHOS")))
1514 }
else if (
Load.C.cmd == MachO::LC_NOTE) {
1517 }
else if (
Load.C.cmd == MachO::LC_BUILD_VERSION) {
1520 }
else if (
Load.C.cmd == MachO::LC_TARGET_TRIPLE) {
1523 }
else if (
Load.C.cmd == MachO::LC_RPATH) {
1526 }
else if (
Load.C.cmd == MachO::LC_SOURCE_VERSION) {
1527 if (
Load.C.cmdsize !=
sizeof(MachO::source_version_command)) {
1529 " has incorrect cmdsize");
1532 if (SourceLoadCmd) {
1536 SourceLoadCmd =
Load.Ptr;
1537 }
else if (
Load.C.cmd == MachO::LC_MAIN) {
1538 if (
Load.C.cmdsize !=
sizeof(MachO::entry_point_command)) {
1540 " has incorrect cmdsize");
1543 if (EntryPointLoadCmd) {
1547 EntryPointLoadCmd =
Load.Ptr;
1548 }
else if (
Load.C.cmd == MachO::LC_ENCRYPTION_INFO) {
1549 if (
Load.C.cmdsize !=
sizeof(MachO::encryption_info_command)) {
1551 " has incorrect cmdsize");
1554 MachO::encryption_info_command
E =
1557 &EncryptLoadCmd,
"LC_ENCRYPTION_INFO")))
1559 }
else if (
Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
1560 if (
Load.C.cmdsize !=
sizeof(MachO::encryption_info_command_64)) {
1562 " has incorrect cmdsize");
1565 MachO::encryption_info_command_64
E =
1568 &EncryptLoadCmd,
"LC_ENCRYPTION_INFO_64")))
1570 }
else if (
Load.C.cmd == MachO::LC_LINKER_OPTION) {
1573 }
else if (
Load.C.cmd == MachO::LC_SUB_FRAMEWORK) {
1574 if (
Load.C.cmdsize <
sizeof(MachO::sub_framework_command)) {
1576 " LC_SUB_FRAMEWORK cmdsize too small");
1579 MachO::sub_framework_command S =
1582 sizeof(MachO::sub_framework_command),
1583 "sub_framework_command", S.
umbrella,
1586 }
else if (
Load.C.cmd == MachO::LC_SUB_UMBRELLA) {
1587 if (
Load.C.cmdsize <
sizeof(MachO::sub_umbrella_command)) {
1589 " LC_SUB_UMBRELLA cmdsize too small");
1592 MachO::sub_umbrella_command S =
1595 sizeof(MachO::sub_umbrella_command),
1599 }
else if (
Load.C.cmd == MachO::LC_SUB_LIBRARY) {
1600 if (
Load.C.cmdsize <
sizeof(MachO::sub_library_command)) {
1602 " LC_SUB_LIBRARY cmdsize too small");
1605 MachO::sub_library_command S =
1608 sizeof(MachO::sub_library_command),
1612 }
else if (
Load.C.cmd == MachO::LC_SUB_CLIENT) {
1613 if (
Load.C.cmdsize <
sizeof(MachO::sub_client_command)) {
1615 " LC_SUB_CLIENT cmdsize too small");
1618 MachO::sub_client_command S =
1621 sizeof(MachO::sub_client_command),
1622 "sub_client_command", S.
client,
"client")))
1624 }
else if (
Load.C.cmd == MachO::LC_ROUTINES) {
1625 if (
Load.C.cmdsize !=
sizeof(MachO::routines_command)) {
1627 " has incorrect cmdsize");
1630 if (RoutinesLoadCmd) {
1631 Err =
malformedError(
"more than one LC_ROUTINES and or LC_ROUTINES_64 "
1635 RoutinesLoadCmd =
Load.Ptr;
1636 }
else if (
Load.C.cmd == MachO::LC_ROUTINES_64) {
1637 if (
Load.C.cmdsize !=
sizeof(MachO::routines_command_64)) {
1639 " has incorrect cmdsize");
1642 if (RoutinesLoadCmd) {
1643 Err =
malformedError(
"more than one LC_ROUTINES_64 and or LC_ROUTINES "
1647 RoutinesLoadCmd =
Load.Ptr;
1648 }
else if (
Load.C.cmd == MachO::LC_UNIXTHREAD) {
1651 if (UnixThreadLoadCmd) {
1655 UnixThreadLoadCmd =
Load.Ptr;
1656 }
else if (
Load.C.cmd == MachO::LC_THREAD) {
1660 }
else if (
Load.C.cmd == MachO::LC_TWOLEVEL_HINTS) {
1662 &TwoLevelHintsLoadCmd, Elements)))
1664 }
else if (
Load.C.cmd == MachO::LC_IDENT) {
1668 Err =
malformedError(
"load command " + Twine(
I) +
" for cmd value of: " +
1669 Twine(
Load.C.cmd) +
" is obsolete and not "
1676 if (
I < LoadCommandCount - 1) {
1680 Err = LoadOrErr.takeError();
1685 if (!SymtabLoadCmd) {
1686 if (DysymtabLoadCmd) {
1687 Err =
malformedError(
"contains LC_DYSYMTAB load command without a "
1688 "LC_SYMTAB load command");
1691 }
else if (DysymtabLoadCmd) {
1692 MachO::symtab_command Symtab =
1694 MachO::dysymtab_command Dysymtab =
1698 "extends past the end of the symbol table");
1704 Err =
malformedError(
"ilocalsym plus nlocalsym in LC_DYSYMTAB load "
1705 "command extends past the end of the symbol table");
1710 "extends past the end of the symbol table");
1716 Err =
malformedError(
"iextdefsym plus nextdefsym in LC_DYSYMTAB "
1717 "load command extends past the end of the symbol "
1723 "extends past the end of the symbol table");
1729 Err =
malformedError(
"iundefsym plus nundefsym in LC_DYSYMTAB load "
1730 " command extends past the end of the symbol table");
1736 DyldIdLoadCmd ==
nullptr) {
1737 Err =
malformedError(
"no LC_ID_DYLIB load command in dynamic library "
1741 assert(LoadCommands.size() == LoadCommandCount);
1759 uint64_t NValue = 0;
1781 if (NSect == 0 || NSect > Sections.size())
1783 " for symbol at index " +
Twine(SymbolIndex));
1788 "the end of string table, for N_INDR symbol at "
1789 "index " +
Twine(SymbolIndex));
1795 if (LibraryOrdinal != 0 &&
1798 LibraryOrdinal - 1 >= Libraries.size() ) {
1800 " for symbol at index " +
Twine(SymbolIndex));
1806 " past the end of string table, for symbol at "
1807 "index " +
Twine(SymbolIndex));
1823 if (Entry.n_strx == 0)
1827 const char *Start = &
StringTable.data()[Entry.n_strx];
1844 return Entry.n_value;
1847 return Entry.n_value;
1863 return std::error_code();
1866uint64_t MachOObjectFile::getSymbolValueImpl(
DataRefImpl Sym)
const {
1890 uint8_t n_type = Entry.n_type;
1916 uint8_t MachOType = Entry.n_type;
1917 uint16_t MachOFlags = Entry.n_desc;
1964 DRI.
d.
a = index - 1;
1965 if (DRI.
d.
a >= Sections.size()){
1975 return Entry.n_sect - 1;
2007 SectOffset = Sect.
offset;
2008 SectSize = Sect.
size;
2012 SectOffset = Sect.
offset;
2013 SectSize = Sect.
size;
2019 if (SectOffset > FileSize)
2021 if (FileSize - SectOffset < SectSize)
2022 return FileSize - SectOffset;
2027 uint64_t
Size)
const {
2046 uint64_t PrevTrueOffset = 0;
2047 uint64_t SectOffsetAdjust = 0;
2048 for (
uint32_t SectIdx = 0; SectIdx < Sec.
d.
a; ++SectIdx) {
2051 uint64_t CurrTrueOffset = (uint64_t)CurrSect.
offset + SectOffsetAdjust;
2052 if ((SectOffsetAdjust > 0) && (PrevTrueOffset > CurrTrueOffset))
2053 return malformedError(
"section data exceeds 4GB and section file "
2054 "offsets are not ordered");
2055 const uint64_t EndSectFileOffset =
2057 if (EndSectFileOffset > UINT32_MAX)
2058 SectOffsetAdjust += EndSectFileOffset & 0xFFFFFFFF00000000ull;
2059 PrevTrueOffset = CurrTrueOffset;
2061 Offset += SectOffsetAdjust;
2085 if (SectionIndex < 1 || SectionIndex > Sections.size())
2089 DRI.
d.
a = SectionIndex - 1;
2095 auto NameOrErr = Section.getName();
2097 return NameOrErr.takeError();
2131 if (!SectionNameOrErr) {
2144template <
typename LoadCommandType>
2149 if (!SegmentOrErr) {
2153 auto &Segment = SegmentOrErr.get();
2156 Segment.fileoff, Segment.fileoff + Segment.filesize));
2160template <
typename LoadCommandType>
2161ArrayRef<uint8_t> getSegmentContents(
const MachOObjectFile &Obj,
2162 MachOObjectFile::LoadCommandInfo LoadCmd) {
2164 if (!SegmentOrErr) {
2168 auto &Segment = SegmentOrErr.get();
2170 Obj.getData().substr(Segment.fileoff, Segment.filesize));
2178 switch (LoadCmd.
C.
cmd) {
2179 case MachO::LC_SEGMENT:
2183 case MachO::LC_SEGMENT_64:
2190 if (!Contents.
empty())
2200 switch (LoadCmd.
C.
cmd) {
2201 case MachO::LC_SEGMENT:
2202 if (Idx == SegmentIndex)
2203 return ::getSegmentContents<MachO::segment_command>(*
this, LoadCmd);
2206 case MachO::LC_SEGMENT_64:
2207 if (Idx == SegmentIndex)
2208 return ::getSegmentContents<MachO::segment_command_64>(*
this, LoadCmd);
2232 return (SegmentName ==
"__LLVM" && *NameOrErr ==
"__bitcode");
2279 Ret.
d.
b = DysymtabLoadCmd.nextrel;
2296 Ret.
d.
b = DysymtabLoadCmd.nlocrel;
2307 "Only implemented for MH_OBJECT && MH_KEXT_BUNDLE");
2348 unsigned Arch = this->
getArch();
2352 static const char *
const Table[] = {
2353 "GENERIC_RELOC_VANILLA",
2354 "GENERIC_RELOC_PAIR",
2355 "GENERIC_RELOC_SECTDIFF",
2356 "GENERIC_RELOC_PB_LA_PTR",
2357 "GENERIC_RELOC_LOCAL_SECTDIFF",
2358 "GENERIC_RELOC_TLV" };
2367 static const char *
const Table[] = {
2368 "X86_64_RELOC_UNSIGNED",
2369 "X86_64_RELOC_SIGNED",
2370 "X86_64_RELOC_BRANCH",
2371 "X86_64_RELOC_GOT_LOAD",
2373 "X86_64_RELOC_SUBTRACTOR",
2374 "X86_64_RELOC_SIGNED_1",
2375 "X86_64_RELOC_SIGNED_2",
2376 "X86_64_RELOC_SIGNED_4",
2377 "X86_64_RELOC_TLV" };
2386 static const char *
const Table[] = {
2387 "ARM_RELOC_VANILLA",
2389 "ARM_RELOC_SECTDIFF",
2390 "ARM_RELOC_LOCAL_SECTDIFF",
2391 "ARM_RELOC_PB_LA_PTR",
2393 "ARM_THUMB_RELOC_BR22",
2394 "ARM_THUMB_32BIT_BRANCH",
2396 "ARM_RELOC_HALF_SECTDIFF" };
2406 static const char *
const Table[] = {
2407 "ARM64_RELOC_UNSIGNED",
"ARM64_RELOC_SUBTRACTOR",
2408 "ARM64_RELOC_BRANCH26",
"ARM64_RELOC_PAGE21",
2409 "ARM64_RELOC_PAGEOFF12",
"ARM64_RELOC_GOT_LOAD_PAGE21",
2410 "ARM64_RELOC_GOT_LOAD_PAGEOFF12",
"ARM64_RELOC_POINTER_TO_GOT",
2411 "ARM64_RELOC_TLVP_LOAD_PAGE21",
"ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
2412 "ARM64_RELOC_ADDEND",
"ARM64_RELOC_AUTHENTICATED_POINTER"
2415 if (RType >= std::size(
Table))
2422 static const char *
const Table[] = {
2423 "PPC_RELOC_VANILLA",
2431 "PPC_RELOC_SECTDIFF",
2432 "PPC_RELOC_PB_LA_PTR",
2433 "PPC_RELOC_HI16_SECTDIFF",
2434 "PPC_RELOC_LO16_SECTDIFF",
2435 "PPC_RELOC_HA16_SECTDIFF",
2437 "PPC_RELOC_LO14_SECTDIFF",
2438 "PPC_RELOC_LOCAL_SECTDIFF" };
2447 static const char *
const Table[] = {
2448 "RISCV_RELOC_UNSIGNED",
"RISCV_RELOC_SUBTRACTOR",
2449 "RISCV_RELOC_BRANCH21",
"RISCV_RELOC_HI20",
2450 "RISCV_RELOC_LO12",
"RISCV_RELOC_GOT_HI20",
2451 "RISCV_RELOC_GOT_LO12",
"RISCV_RELOC_POINTER_TO_GOT",
2452 "RISCV_RELOC_ADDEND",
2455 if (RType >= std::size(
Table))
2459 Result.append(res.
begin(), res.
end());
2466 Result.append(PCRel.
begin(), PCRel.
end());
2474 Result.append(res.
begin(), res.
end());
2522 size_t a, b, c, d, Idx;
2524 isFramework =
false;
2528 a = Name.rfind(
'/');
2529 if (a == Name.npos || a == 0)
2531 Foo = Name.substr(a + 1);
2534 Idx = Foo.
rfind(
'_');
2535 if (Idx != Foo.
npos && Foo.
size() >= 2) {
2536 Suffix = Foo.
substr(Idx);
2537 if (Suffix !=
"_debug" && Suffix !=
"_profile")
2540 Foo = Foo.
slice(0, Idx);
2544 b = Name.rfind(
'/', a);
2549 F = Name.substr(Idx, Foo.
size());
2550 DotFramework = Name.substr(Idx + Foo.
size(),
sizeof(
".framework/") - 1);
2551 if (
F == Foo && DotFramework ==
".framework/") {
2559 c = Name.rfind(
'/', b);
2560 if (c == Name.npos || c == 0)
2562 V = Name.substr(c + 1);
2563 if (!V.starts_with(
"Versions/"))
2565 d = Name.rfind(
'/', c);
2570 F = Name.substr(Idx, Foo.
size());
2571 DotFramework = Name.substr(Idx + Foo.
size(),
sizeof(
".framework/") - 1);
2572 if (
F == Foo && DotFramework ==
".framework/") {
2579 a = Name.rfind(
'.');
2580 if (a == Name.npos || a == 0)
2582 Dylib = Name.substr(a);
2583 if (Dylib !=
".dylib")
2588 Dot = Name.substr(a - 2, 1);
2593 b = Name.rfind(
'/', a);
2599 Idx = Name.rfind(
'_');
2600 if (Idx != Name.npos && Idx != b) {
2601 Lib = Name.slice(b, Idx);
2602 Suffix = Name.slice(Idx, a);
2603 if (Suffix !=
"_debug" && Suffix !=
"_profile") {
2605 Lib = Name.slice(b, a);
2609 Lib = Name.slice(b, a);
2612 if (
Lib.size() >= 3) {
2613 Dot =
Lib.substr(
Lib.size() - 2, 1);
2620 Qtx = Name.substr(a);
2623 b = Name.rfind(
'/', a);
2625 Lib = Name.slice(0, a);
2627 Lib = Name.slice(b+1, a);
2629 if (
Lib.size() >= 3) {
2630 Dot =
Lib.substr(
Lib.size() - 2, 1);
2644 if (Index >= Libraries.size())
2649 if (LibrariesShortNames.size() == 0) {
2650 for (
unsigned i = 0; i < Libraries.size(); i++) {
2656 if (
D.dylib.name >=
D.cmdsize)
2658 const char *
P = (
const char *)(Libraries[i]) +
D.dylib.name;
2660 if (
D.dylib.name+Name.size() >=
D.cmdsize)
2665 if (shortName.
empty())
2666 LibrariesShortNames.push_back(Name);
2668 LibrariesShortNames.push_back(shortName);
2672 Res = LibrariesShortNames[Index];
2673 return std::error_code();
2677 return Libraries.size();
2690 if (!SymtabLoadCmd || Symtab.
nsyms == 0)
2699 if (!SymtabLoadCmd || Symtab.
nsyms == 0)
2713 if (!SymtabLoadCmd || Index >= Symtab.
nsyms)
2742 DRI.
d.
a = Sections.size();
2755 return "Mach-O 32-bit i386";
2757 return "Mach-O arm";
2759 return "Mach-O arm64 (ILP32)";
2761 return "Mach-O 32-bit ppc";
2763 return "Mach-O 32-bit RISC-V";
2765 return "Mach-O 32-bit unknown";
2771 return "Mach-O 64-bit x86-64";
2773 return "Mach-O arm64";
2775 return "Mach-O 64-bit ppc64";
2777 return "Mach-O 64-bit unknown";
2805 const char **McpuDefault,
2806 const char **ArchFlag) {
2808 *McpuDefault =
nullptr;
2810 *ArchFlag =
nullptr;
2818 return Triple(
"i386-apple-darwin");
2826 *ArchFlag =
"x86_64";
2827 return Triple(
"x86_64-apple-darwin");
2830 *ArchFlag =
"x86_64h";
2831 return Triple(
"x86_64h-apple-darwin");
2839 *ArchFlag =
"armv4t";
2840 return Triple(
"armv4t-apple-darwin");
2843 *ArchFlag =
"armv5e";
2844 return Triple(
"armv5e-apple-darwin");
2847 *ArchFlag =
"xscale";
2848 return Triple(
"xscale-apple-darwin");
2851 *ArchFlag =
"armv6";
2852 return Triple(
"armv6-apple-darwin");
2855 *McpuDefault =
"cortex-m0";
2857 *ArchFlag =
"armv6m";
2858 return Triple(
"armv6m-apple-darwin");
2861 *ArchFlag =
"armv7";
2862 return Triple(
"armv7-apple-darwin");
2865 *McpuDefault =
"cortex-m4";
2867 *ArchFlag =
"armv7em";
2868 return Triple(
"thumbv7em-apple-darwin");
2871 *McpuDefault =
"cortex-a7";
2873 *ArchFlag =
"armv7k";
2874 return Triple(
"armv7k-apple-darwin");
2877 *McpuDefault =
"cortex-m3";
2879 *ArchFlag =
"armv7m";
2880 return Triple(
"thumbv7m-apple-darwin");
2883 *McpuDefault =
"cortex-a7";
2885 *ArchFlag =
"armv7s";
2886 return Triple(
"armv7s-apple-darwin");
2889 *McpuDefault =
"cortex-m23";
2891 *ArchFlag =
"armv8m.base";
2892 return Triple(
"thumbv8m-apple-darwin");
2895 *McpuDefault =
"cortex-m33";
2897 *ArchFlag =
"armv8m.main";
2898 return Triple(
"thumbv8m-apple-darwin");
2901 *McpuDefault =
"cortex-m52";
2903 *ArchFlag =
"armv8.1m.main";
2904 return Triple(
"thumbv8m-apple-darwin");
2912 *McpuDefault =
"cyclone";
2914 *ArchFlag =
"arm64";
2915 return Triple(
"arm64-apple-darwin");
2918 *McpuDefault =
"apple-a12";
2920 *ArchFlag =
"arm64e";
2921 return Triple(
"arm64e-apple-darwin");
2924 *McpuDefault =
"apple-a20";
2926 *ArchFlag =
"arm64e.x1";
2927 return Triple(
"arm64e.x1-apple-darwin");
2935 *McpuDefault =
"cyclone";
2937 *ArchFlag =
"arm64_32";
2938 return Triple(
"arm64_32-apple-darwin");
2947 return Triple(
"ppc-apple-darwin");
2955 *ArchFlag =
"ppc64";
2956 return Triple(
"ppc64-apple-darwin");
2964 *ArchFlag =
"riscv32";
2965 return Triple(
"riscv32-apple-macho");
2984 static const std::array<StringRef, 21> ValidArchs = {{
2985 "i386",
"x86_64",
"x86_64h",
"armv4t",
"arm",
2986 "armv5e",
"armv6",
"armv6m",
"armv7",
"armv7em",
2987 "armv7k",
"armv7m",
"armv7s",
"armv8m.base",
"armv8m.main",
2988 "armv8.1m.main",
"arm64",
"arm64e",
"arm64_32",
"ppc",
3017 if (!DataInCodeLoadCmd)
3027 if (!DataInCodeLoadCmd)
3039void ExportEntry::moveToFirst() {
3044 pushDownUntilBottom();
3047void ExportEntry::moveToEnd() {
3054 if (Done ||
Other.Done)
3055 return (Done ==
Other.Done);
3057 if (Stack.size() !=
Other.Stack.size())
3060 if (!CumulativeString.equals(
Other.CumulativeString))
3063 for (
unsigned i=0; i < Stack.size(); ++i) {
3064 if (Stack[i].Start !=
Other.Stack[i].Start)
3070uint64_t ExportEntry::readULEB128(
const uint8_t *&Ptr,
const char **
error) {
3074 if (Ptr > Trie.
end())
3080 return CumulativeString;
3084 return Stack.back().Flags;
3088 return Stack.back().Address;
3092 return Stack.back().Other;
3096 const char* ImportName = Stack.
back().ImportName;
3103 return Stack.back().Start - Trie.begin();
3106ExportEntry::NodeState::NodeState(
const uint8_t *Ptr)
3107 : Start(Ptr), Current(Ptr) {}
3109void ExportEntry::pushNode(uint64_t offset) {
3112 NodeState State(Ptr);
3113 const char *
error =
nullptr;
3117 " in export trie data at node: 0x" +
3122 State.IsExportNode = (ExportInfoSize != 0);
3123 const uint8_t* Children = State.Current + ExportInfoSize;
3124 if (Children > Trie.
end()) {
3128 " too big and extends past end of trie data");
3132 if (State.IsExportNode) {
3133 const uint8_t *ExportStart = State.Current;
3134 State.Flags = readULEB128(State.Current, &
error);
3137 " in export trie data at node: 0x" +
3143 if (State.Flags != 0 &&
3148 "unsupported exported symbol kind: " + Twine((
int)
Kind) +
3156 State.Other = readULEB128(State.Current, &
error);
3159 " in export trie data at node: 0x" +
3167 if ((int64_t)State.Other > 0 && State.Other > O->getLibraryCount()) {
3169 "bad library ordinal: " + Twine((
int)State.Other) +
" (max " +
3170 Twine((
int)O->getLibraryCount()) +
3176 State.ImportName =
reinterpret_cast<const char*
>(State.Current);
3177 if (*State.ImportName ==
'\0') {
3180 const uint8_t *End = State.Current + 1;
3181 if (End >= Trie.end()) {
3182 *E =
malformedError(
"import name of re-export in export trie data at "
3185 " starts past end of trie data");
3189 while(*End !=
'\0' && End < Trie.end())
3192 *E =
malformedError(
"import name of re-export in export trie data at "
3195 " extends past end of trie data");
3199 State.Current = End + 1;
3202 State.Address = readULEB128(State.Current, &
error);
3205 " in export trie data at node: 0x" +
3211 State.Other = readULEB128(State.Current, &
error);
3214 " in export trie data at node: 0x" +
3221 if (ExportStart + ExportInfoSize < State.Current) {
3223 "inconsistent export info size: 0x" +
3232 if (State.ChildCount != 0 && Children + 1 >= Trie.end()) {
3233 *E =
malformedError(
"byte for count of children in export trie data at "
3236 " extends past end of trie data");
3241 State.NextChildIndex = 0;
3242 State.ParentStringLength = CumulativeString.size();
3243 Stack.push_back(State);
3246void ExportEntry::pushDownUntilBottom() {
3247 ErrorAsOutParameter ErrAsOutParam(E);
3248 const char *
error =
nullptr;
3249 while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
3250 NodeState &Top = Stack.back();
3251 CumulativeString.resize(Top.ParentStringLength);
3252 for (;*Top.Current != 0 && Top.Current < Trie.end(); Top.Current++) {
3253 char C = *Top.Current;
3254 CumulativeString.push_back(
C);
3256 if (Top.Current >= Trie.end()) {
3257 *E =
malformedError(
"edge sub-string in export trie data at node: 0x" +
3259 " for child #" + Twine((
int)Top.NextChildIndex) +
3260 " extends past end of trie data");
3268 " in export trie data at node: 0x" +
3273 for (
const NodeState &node :
nodes()) {
3274 if (node.Start == Trie.begin() + childNodeIndex){
3275 *E =
malformedError(
"loop in children in export trie data at node: 0x" +
3277 " back to node: 0x" +
3283 Top.NextChildIndex += 1;
3284 pushNode(childNodeIndex);
3288 if (!Stack.back().IsExportNode) {
3289 *E =
malformedError(
"node is not an export node in export trie data at "
3313 assert(!Stack.empty() &&
"ExportEntry::moveNext() with empty node stack");
3314 if (!Stack.back().IsExportNode) {
3315 *E =
malformedError(
"node is not an export node in export trie data at "
3323 while (!Stack.empty()) {
3324 NodeState &Top = Stack.back();
3325 if (Top.NextChildIndex < Top.ChildCount) {
3326 pushDownUntilBottom();
3330 if (Top.IsExportNode) {
3332 CumulativeString.resize(Top.ParentStringLength);
3343 const MachOObjectFile *O) {
3348 Start.moveToFirst();
3358 if (DyldInfoLoadCmd)
3360 else if (DyldExportsTrieLoadCmd)
3363 return exports(Err, Trie,
this);
3370 for (
const auto &Command :
O->load_commands()) {
3371 if (Command.C.cmd == MachO::LC_SEGMENT) {
3372 MachO::segment_command SLC = O->getSegmentLoadCommand(Command);
3373 if (StringRef(SLC.segname) ==
"__TEXT") {
3374 TextAddress = SLC.vmaddr;
3377 }
else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
3378 MachO::segment_command_64 SLC_64 = O->getSegment64LoadCommand(Command);
3379 if (StringRef(SLC_64.segname) ==
"__TEXT") {
3380 TextAddress = SLC_64.vmaddr;
3440 if (
auto FixupTargetsOrErr =
O->getDyldChainedFixupTargets()) {
3441 FixupTargets = *FixupTargetsOrErr;
3443 *
E = FixupTargetsOrErr.takeError();
3447 if (
auto SegmentsOrErr =
O->getChainedFixupsSegments()) {
3448 Segments = std::move(SegmentsOrErr->second);
3450 *
E = SegmentsOrErr.takeError();
3455void MachOChainedFixupEntry::findNextPageWithFixups() {
3456 auto FindInSegment = [
this]() {
3458 while (PageIndex < SegInfo.
PageStarts.size() &&
3461 return PageIndex < SegInfo.
PageStarts.size();
3464 while (InfoSegIndex < Segments.size()) {
3465 if (FindInSegment()) {
3466 PageOffset = Segments[InfoSegIndex].PageStarts[PageIndex];
3478 if (Segments.empty()) {
3486 findNextPageWithFixups();
3497 if (InfoSegIndex == Segments.size()) {
3511 " has unsupported chained fixup pointer_format " +
3512 Twine(PointerFormat));
3526 " extends past segment's end");
3537 assert(
O->isLittleEndian() &&
"big-endian object should have been rejected "
3538 "by getDyldChainedFixupTargets()");
3545 bool IsBind =
Field(63, 1);
3552 if (ImportOrdinal >= FixupTargets.size()) {
3555 " has out-of range import ordinal " +
3556 Twine(ImportOrdinal));
3563 Addend = InlineAddend ? InlineAddend :
Target.addend();
3568 uint64_t High8 =
Field(36, 8);
3577 PageOffset += 4 *
Next;
3580 findNextPageWithFixups();
3590 return InfoSegIndex ==
Other.InfoSegIndex && PageIndex ==
Other.PageIndex &&
3591 PageOffset ==
Other.PageOffset;
3596 : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()),
3597 PointerSize(
is64Bit ? 8 : 4) {}
3599void MachORebaseEntry::moveToFirst() {
3600 Ptr = Opcodes.
begin();
3604void MachORebaseEntry::moveToEnd() {
3605 Ptr = Opcodes.
end();
3606 RemainingLoopCount = 0;
3613 SegmentOffset += AdvanceAmount;
3614 if (RemainingLoopCount) {
3615 --RemainingLoopCount;
3624 if (Ptr == Opcodes.end()) {
3630 const uint8_t *OpcodeStart = Ptr;
3634 uint64_t
Count, Skip;
3635 const char *
error =
nullptr;
3644 RebaseType = ImmValue;
3646 *E =
malformedError(
"for REBASE_OPCODE_SET_TYPE_IMM bad bind type: " +
3647 Twine((
int)RebaseType) +
" for opcode at: 0x" +
3654 dbgs() <<
"REBASE_OPCODE_SET_TYPE_IMM: "
3655 <<
"RebaseType=" << (
int) RebaseType <<
"\n");
3658 SegmentIndex = ImmValue;
3659 SegmentOffset = readULEB128(&
error);
3661 *E =
malformedError(
"for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
3667 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3670 *E =
malformedError(
"for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
3678 dbgs() <<
"REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
3679 <<
"SegmentIndex=" << SegmentIndex <<
", "
3680 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
3684 SegmentOffset += readULEB128(&
error);
3687 " for opcode at: 0x" +
3692 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3696 " for opcode at: 0x" +
3702 dbgs() <<
"REBASE_OPCODE_ADD_ADDR_ULEB: "
3703 <<
format(
"SegmentOffset=0x%06X",
3704 SegmentOffset) <<
"\n");
3707 SegmentOffset += ImmValue * PointerSize;
3708 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3718 dbgs() <<
"REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
3719 <<
format(
"SegmentOffset=0x%06X",
3720 SegmentOffset) <<
"\n");
3723 AdvanceAmount = PointerSize;
3727 RemainingLoopCount = ImmValue - 1;
3729 RemainingLoopCount = 0;
3730 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3731 PointerSize,
Count, Skip);
3741 dbgs() <<
"REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
3742 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
3743 <<
", AdvanceAmount=" << AdvanceAmount
3744 <<
", RemainingLoopCount=" << RemainingLoopCount
3748 AdvanceAmount = PointerSize;
3759 RemainingLoopCount =
Count - 1;
3761 RemainingLoopCount = 0;
3762 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3763 PointerSize,
Count, Skip);
3773 dbgs() <<
"REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
3774 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
3775 <<
", AdvanceAmount=" << AdvanceAmount
3776 <<
", RemainingLoopCount=" << RemainingLoopCount
3780 Skip = readULEB128(&
error);
3782 *E =
malformedError(
"for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
3788 AdvanceAmount = Skip + PointerSize;
3790 RemainingLoopCount = 0;
3791 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3792 PointerSize,
Count, Skip);
3794 *E =
malformedError(
"for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
3802 dbgs() <<
"REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
3803 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
3804 <<
", AdvanceAmount=" << AdvanceAmount
3805 <<
", RemainingLoopCount=" << RemainingLoopCount
3811 *E =
malformedError(
"for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
3819 RemainingLoopCount =
Count - 1;
3821 RemainingLoopCount = 0;
3822 Skip = readULEB128(&
error);
3824 *E =
malformedError(
"for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
3831 AdvanceAmount = Skip + PointerSize;
3833 error = O->RebaseEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
3834 PointerSize,
Count, Skip);
3836 *E =
malformedError(
"for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
3845 dbgs() <<
"REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
3846 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
3847 <<
", AdvanceAmount=" << AdvanceAmount
3848 <<
", RemainingLoopCount=" << RemainingLoopCount
3861uint64_t MachORebaseEntry::readULEB128(
const char **
error) {
3865 if (Ptr > Opcodes.
end())
3866 Ptr = Opcodes.
end();
3875 switch (RebaseType) {
3879 return "text abs32";
3881 return "text rel32";
3889 return O->BindRebaseSegmentName(SegmentIndex);
3895 return O->BindRebaseSectionName(SegmentIndex, SegmentOffset);
3901 return O->BindRebaseAddress(SegmentIndex, SegmentOffset);
3905#ifdef EXPENSIVE_CHECKS
3906 assert(Opcodes ==
Other.Opcodes &&
"compare iterators of different files");
3908 assert(Opcodes.data() ==
Other.Opcodes.data() &&
"compare iterators of different files");
3910 return (Ptr ==
Other.Ptr) &&
3911 (RemainingLoopCount ==
Other.RemainingLoopCount) &&
3912 (Done ==
Other.Done);
3918 if (O->BindRebaseSectionTable ==
nullptr)
3919 O->BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(O);
3921 Start.moveToFirst();
3935 : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()),
3936 PointerSize(
is64Bit ? 8 : 4), TableKind(BK) {}
3938void MachOBindEntry::moveToFirst() {
3939 Ptr = Opcodes.
begin();
3943void MachOBindEntry::moveToEnd() {
3944 Ptr = Opcodes.
end();
3945 RemainingLoopCount = 0;
3952 SegmentOffset += AdvanceAmount;
3953 if (RemainingLoopCount) {
3954 --RemainingLoopCount;
3963 if (Ptr == Opcodes.end()) {
3969 const uint8_t *OpcodeStart = Ptr;
3973 int8_t SignExtended;
3975 uint64_t
Count, Skip;
3976 const char *
error =
nullptr;
3982 bool NotLastEntry =
false;
3983 for (
const uint8_t *
P = Ptr;
P < Opcodes.end(); ++
P) {
3985 NotLastEntry =
true;
3997 *E =
malformedError(
"BIND_OPCODE_SET_DYLIB_ORDINAL_IMM not allowed in "
3998 "weak bind table for opcode at: 0x" +
4004 LibraryOrdinalSet =
true;
4005 if (ImmValue > O->getLibraryCount()) {
4006 *E =
malformedError(
"for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad "
4007 "library ordinal: " +
4008 Twine((
int)ImmValue) +
" (max " +
4009 Twine((
int)O->getLibraryCount()) +
4010 ") for opcode at: 0x" +
4017 dbgs() <<
"BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
4018 <<
"Ordinal=" << Ordinal <<
"\n");
4022 *E =
malformedError(
"BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB not allowed in "
4023 "weak bind table for opcode at: 0x" +
4028 Ordinal = readULEB128(&
error);
4029 LibraryOrdinalSet =
true;
4037 if (Ordinal > (
int)O->getLibraryCount()) {
4038 *E =
malformedError(
"for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad "
4039 "library ordinal: " +
4040 Twine((
int)Ordinal) +
" (max " +
4041 Twine((
int)O->getLibraryCount()) +
4042 ") for opcode at: 0x" +
4049 dbgs() <<
"BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
4050 <<
"Ordinal=" << Ordinal <<
"\n");
4054 *E =
malformedError(
"BIND_OPCODE_SET_DYLIB_SPECIAL_IMM not allowed in "
4055 "weak bind table for opcode at: 0x" +
4062 Ordinal = SignExtended;
4064 *E =
malformedError(
"for BIND_OPCODE_SET_DYLIB_SPECIAL_IMM unknown "
4065 "special ordinal: " +
4066 Twine((
int)Ordinal) +
" for opcode at: 0x" +
4073 LibraryOrdinalSet =
true;
4076 dbgs() <<
"BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
4077 <<
"Ordinal=" << Ordinal <<
"\n");
4082 while (*Ptr && (Ptr < Opcodes.end())) {
4085 if (Ptr == Opcodes.end()) {
4087 "for BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM "
4088 "symbol name extends past opcodes for opcode at: 0x" +
4093 SymbolName =
StringRef(
reinterpret_cast<const char*
>(SymStart),
4098 dbgs() <<
"BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
4099 <<
"SymbolName=" << SymbolName <<
"\n");
4106 BindType = ImmValue;
4108 *E =
malformedError(
"for BIND_OPCODE_SET_TYPE_IMM bad bind type: " +
4109 Twine((
int)ImmValue) +
" for opcode at: 0x" +
4116 dbgs() <<
"BIND_OPCODE_SET_TYPE_IMM: "
4117 <<
"BindType=" << (
int)BindType <<
"\n");
4120 Addend = readSLEB128(&
error);
4123 " for opcode at: 0x" +
4130 dbgs() <<
"BIND_OPCODE_SET_ADDEND_SLEB: "
4131 <<
"Addend=" << Addend <<
"\n");
4134 SegmentIndex = ImmValue;
4135 SegmentOffset = readULEB128(&
error);
4137 *E =
malformedError(
"for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
4143 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
4146 *E =
malformedError(
"for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
4154 dbgs() <<
"BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
4155 <<
"SegmentIndex=" << SegmentIndex <<
", "
4156 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
4160 SegmentOffset += readULEB128(&
error);
4163 " for opcode at: 0x" +
4168 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
4172 " for opcode at: 0x" +
4178 dbgs() <<
"BIND_OPCODE_ADD_ADDR_ULEB: "
4179 <<
format(
"SegmentOffset=0x%06X",
4180 SegmentOffset) <<
"\n");
4183 AdvanceAmount = PointerSize;
4184 RemainingLoopCount = 0;
4185 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
4189 " for opcode at: 0x" +
4196 "for BIND_OPCODE_DO_BIND missing preceding "
4197 "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode at: 0x" +
4202 if (!LibraryOrdinalSet && TableKind !=
Kind::Weak) {
4205 "BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" +
4211 dbgs() <<
"BIND_OPCODE_DO_BIND: "
4212 <<
format(
"SegmentOffset=0x%06X",
4213 SegmentOffset) <<
"\n");
4217 *E =
malformedError(
"BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB not allowed in "
4218 "lazy bind table for opcode at: 0x" +
4223 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
4234 "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing "
4235 "preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode "
4241 if (!LibraryOrdinalSet && TableKind !=
Kind::Weak) {
4243 "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing "
4244 "preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" +
4249 AdvanceAmount = readULEB128(&
error) + PointerSize;
4260 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset +
4261 AdvanceAmount, PointerSize);
4263 *E =
malformedError(
"for BIND_OPCODE_ADD_ADDR_ULEB (after adding "
4270 RemainingLoopCount = 0;
4273 dbgs() <<
"BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
4274 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
4275 <<
", AdvanceAmount=" << AdvanceAmount
4276 <<
", RemainingLoopCount=" << RemainingLoopCount
4281 *E =
malformedError(
"BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED not "
4282 "allowed in lazy bind table for opcode at: 0x" +
4289 "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
4290 "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for "
4296 if (!LibraryOrdinalSet && TableKind !=
Kind::Weak) {
4298 "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
4299 "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode "
4305 AdvanceAmount = ImmValue * PointerSize + PointerSize;
4306 RemainingLoopCount = 0;
4307 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset +
4308 AdvanceAmount, PointerSize);
4310 *E =
malformedError(
"for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " +
4318 <<
"BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
4319 <<
format(
"SegmentOffset=0x%06X", SegmentOffset) <<
"\n");
4323 *E =
malformedError(
"BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB not "
4324 "allowed in lazy bind table for opcode at: 0x" +
4331 RemainingLoopCount =
Count - 1;
4333 RemainingLoopCount = 0;
4335 *E =
malformedError(
"for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
4342 Skip = readULEB128(&
error);
4343 AdvanceAmount = Skip + PointerSize;
4345 *E =
malformedError(
"for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
4354 "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
4355 "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for "
4361 if (!LibraryOrdinalSet && TableKind !=
Kind::Weak) {
4363 "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
4364 "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode "
4370 error = O->BindEntryCheckSegAndOffsets(SegmentIndex, SegmentOffset,
4371 PointerSize,
Count, Skip);
4374 malformedError(
"for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " +
4382 dbgs() <<
"BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
4383 <<
format(
"SegmentOffset=0x%06X", SegmentOffset)
4384 <<
", AdvanceAmount=" << AdvanceAmount
4385 <<
", RemainingLoopCount=" << RemainingLoopCount
4398uint64_t MachOBindEntry::readULEB128(
const char **
error) {
4402 if (Ptr > Opcodes.
end())
4403 Ptr = Opcodes.
end();
4407int64_t MachOBindEntry::readSLEB128(
const char **
error) {
4411 if (Ptr > Opcodes.
end())
4412 Ptr = Opcodes.
end();
4425 return "text abs32";
4427 return "text rel32";
4443 return O->BindRebaseSegmentName(SegmentIndex);
4449 return O->BindRebaseSectionName(SegmentIndex, SegmentOffset);
4455 return O->BindRebaseAddress(SegmentIndex, SegmentOffset);
4459#ifdef EXPENSIVE_CHECKS
4460 assert(Opcodes ==
Other.Opcodes &&
"compare iterators of different files");
4462 assert(Opcodes.data() ==
Other.Opcodes.data() &&
"compare iterators of different files");
4464 return (Ptr ==
Other.Ptr) &&
4465 (RemainingLoopCount ==
Other.RemainingLoopCount) &&
4466 (Done ==
Other.Done);
4471 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
4473 uint64_t CurSegAddress;
4474 for (
const SectionRef &Section : Obj->sections()) {
4480 Info.SectionName = *NameOrErr;
4481 Info.Address = Section.getAddress();
4482 Info.Size = Section.getSize();
4484 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
4485 if (Info.SegmentName != CurSegName) {
4487 CurSegName = Info.SegmentName;
4488 CurSegAddress = Info.Address;
4490 Info.SegmentIndex = CurSegIndex - 1;
4491 Info.OffsetInSegment = Info.Address - CurSegAddress;
4492 Info.SegmentStartAddress = CurSegAddress;
4493 Sections.push_back(Info);
4495 MaxSegIndex = CurSegIndex;
4511 return "missing preceding *_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB";
4512 if (SegIndex >= MaxSegIndex)
4513 return "bad segIndex (too large)";
4514 for (uint64_t i = 0; i <
Count; ++i) {
4515 uint64_t Start = SegOffset + i * (PointerSize + Skip);
4516 uint64_t End = Start + PointerSize;
4518 for (
const SectionInfo &
SI : Sections) {
4519 if (
SI.SegmentIndex != SegIndex)
4521 if ((
SI.OffsetInSegment<=Start) && (Start<(
SI.OffsetInSegment+
SI.Size))) {
4522 if (End <=
SI.OffsetInSegment +
SI.Size) {
4527 return "bad offset, extends beyond section boundary";
4531 return "bad offset, not in section";
4539 for (
const SectionInfo &
SI : Sections) {
4540 if (
SI.SegmentIndex == SegIndex)
4541 return SI.SegmentName;
4548const BindRebaseSegInfo::SectionInfo &BindRebaseSegInfo::findSection(
4549 int32_t SegIndex, uint64_t SegOffset) {
4550 for (
const SectionInfo &
SI : Sections) {
4551 if (
SI.SegmentIndex != SegIndex)
4553 if (
SI.OffsetInSegment > SegOffset)
4555 if (SegOffset >= (
SI.OffsetInSegment +
SI.Size))
4565 uint64_t SegOffset) {
4566 return findSection(SegIndex, SegOffset).SectionName;
4572 const SectionInfo &
SI = findSection(SegIndex, OffsetInSeg);
4573 return SI.SegmentStartAddress + OffsetInSeg;
4580 if (O->BindRebaseSectionTable ==
nullptr)
4581 O->BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(O);
4583 Start.moveToFirst();
4607 if (BindRebaseSectionTable ==
nullptr)
4608 BindRebaseSectionTable = std::make_unique<BindRebaseSegInfo>(
this);
4611 Start.moveToFirst();
4621 return LoadCommands.begin();
4626 return LoadCommands.end();
4642 assert(Sec.
d.
a < Sections.size() &&
"Should have detected this earlier");
4643 const section_base *
Base =
4644 reinterpret_cast<const section_base *
>(Sections[Sec.
d.
a]);
4650 assert(Sec.
d.
a < Sections.size() &&
"Should have detected this earlier");
4651 const section_base *
Base =
4652 reinterpret_cast<const section_base *
>(Sections[Sec.
d.
a]);
4674 return (RE.
r_word1 >> 27) & 1;
4690 return (RE.
r_word0 >> 24) & 0xf;
4728 if (SecNum ==
MachO::R_ABS || SecNum > Sections.size())
4731 DRI.
d.
a = SecNum - 1;
4736 assert(DRI.
d.
a < Sections.size() &&
"Should have detected this earlier");
4741 assert(DRI.
d.
a < Sections.size() &&
"Should have detected this earlier");
4746 unsigned Index)
const {
4752 unsigned Index)
const {
4759 const char *
P =
reinterpret_cast<const char *
>(DRI.
p);
4765 const char *
P =
reinterpret_cast<const char *
>(DRI.
p);
4915 Offset = DysymtabLoadCmd.extreloff;
4917 Offset = DysymtabLoadCmd.locreloff;
4923 *
this,
reinterpret_cast<const char *
>(
P));
4928 const char *
P =
reinterpret_cast<const char *
>(Rel.
p);
4943 unsigned Index)
const {
4950 unsigned Index)
const {
4961 Cmd.
cmd = MachO::LC_SYMTAB;
4971 if (DysymtabLoadCmd)
4976 Cmd.
cmd = MachO::LC_DYSYMTAB;
5001 if (DataInCodeLoadCmd)
5006 Cmd.
cmd = MachO::LC_DATA_IN_CODE;
5015 if (LinkOptHintsLoadCmd)
5021 Cmd.
cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
5029 if (!DyldInfoLoadCmd)
5032 auto DyldInfoOrErr =
5043 if (!DyldInfoLoadCmd)
5046 auto DyldInfoOrErr =
5057 if (!DyldInfoLoadCmd)
5060 auto DyldInfoOrErr =
5071 if (!DyldInfoLoadCmd)
5074 auto DyldInfoOrErr =
5085 if (!DyldInfoLoadCmd)
5088 auto DyldInfoOrErr =
5101 if (!DyldChainedFixupsLoadCmd)
5102 return std::nullopt;
5104 *
this, DyldChainedFixupsLoadCmd);
5105 if (!DyldChainedFixupsOrErr)
5106 return DyldChainedFixupsOrErr.takeError();
5108 *DyldChainedFixupsOrErr;
5112 if (!DyldChainedFixups.
dataoff)
5113 return std::nullopt;
5114 return DyldChainedFixups;
5121 return CFOrErr.takeError();
5122 if (!CFOrErr->has_value())
5123 return std::nullopt;
5127 uint64_t CFHeaderOffset = DyldChainedFixups.
dataoff;
5128 uint64_t CFSize = DyldChainedFixups.
datasize;
5131 const char *CFHeaderPtr =
getPtr(*
this, CFHeaderOffset);
5132 auto CFHeaderOrErr =
5135 return CFHeaderOrErr.takeError();
5144 Twine(
"bad chained fixups: unknown imports format: ") +
5150 uint64_t CFImageStartsOffset = (CFHeaderOffset + CFHeader.
starts_offset);
5154 " overlaps with chained fixups header");
5156 uint32_t EndOffset = CFHeaderOffset + CFSize;
5160 Twine(CFImageStartsOffset +
5162 " extends past end " +
Twine(EndOffset));
5172 return CFOrErr.takeError();
5174 std::vector<ChainedFixupsSegment> Segments;
5175 if (!CFOrErr->has_value())
5176 return std::make_pair(0, Segments);
5182 return HeaderOrErr.takeError();
5183 if (!HeaderOrErr->has_value())
5184 return std::make_pair(0, Segments);
5187 const char *Contents =
getPtr(*
this, DyldChainedFixups.
dataoff);
5190 *
this, Contents +
Header.starts_offset);
5191 if (!ImageStartsOrErr)
5192 return ImageStartsOrErr.takeError();
5195 const char *SegOffsPtr =
5196 Contents +
Header.starts_offset +
5198 const char *SegOffsEnd =
5200 if (SegOffsEnd > Contents + DyldChainedFixups.
datasize)
5202 "bad chained fixups: seg_info_offset extends past end");
5204 const char *LastSegEnd =
nullptr;
5209 return OffOrErr.takeError();
5217 " at offset " +
Twine(*OffOrErr) + Message);
5220 const char *SegPtr = Contents +
Header.starts_offset + *OffOrErr;
5221 if (LastSegEnd && SegPtr < LastSegEnd)
5222 return Fail(
" overlaps with previous segment info");
5227 return SegOrErr.takeError();
5230 LastSegEnd = SegPtr + Seg.
size;
5234 const char *PageStart =
5236 const char *PageEnd = PageStart + Seg.
page_count *
sizeof(uint16_t);
5237 if (PageEnd > SegPtr + Seg.
size)
5238 return Fail(
" : page_starts extend past seg_info size");
5242 std::vector<uint16_t> PageStarts;
5243 for (
size_t PageIdx = 0; PageIdx < Seg.
page_count; ++PageIdx) {
5245 memcpy(&Start, PageStart + PageIdx *
sizeof(uint16_t),
sizeof(uint16_t));
5248 PageStarts.push_back(Start);
5251 Segments.emplace_back(
I, *OffOrErr, Seg, std::move(PageStarts));
5254 return std::make_pair(ImageStarts.
seg_count, Segments);
5267template <
typename T,
unsigned N>
5269 std::array<T, N> RawValue;
5270 memcpy(RawValue.data(), Ptr,
N *
sizeof(
T));
5272 for (
auto &Element : RawValue)
5277Expected<std::vector<ChainedFixupTarget>>
5281 return CFOrErr.takeError();
5283 std::vector<ChainedFixupTarget> Targets;
5284 if (!CFOrErr->has_value())
5291 return CFHeaderOrErr.takeError();
5292 if (!(*CFHeaderOrErr))
5296 size_t ImportSize = 0;
5304 return malformedError(
"bad chained fixups: unknown imports format: " +
5307 const char *Contents =
getPtr(*
this, DyldChainedFixups.
dataoff);
5308 const char *Imports = Contents +
Header.imports_offset;
5309 size_t ImportsEndOffset =
5310 Header.imports_offset + ImportSize *
Header.imports_count;
5311 const char *ImportsEnd = Contents + ImportsEndOffset;
5312 const char *Symbols = Contents +
Header.symbols_offset;
5313 const char *SymbolsEnd = Contents + DyldChainedFixups.
datasize;
5315 if (ImportsEnd > Symbols)
5317 Twine(ImportsEndOffset) +
" overlaps with symbols");
5322 return createError(
"parsing big-endian chained fixups is not implemented");
5323 for (
const char *ImportPtr = Imports; ImportPtr < ImportsEnd;
5324 ImportPtr += ImportSize) {
5334 WeakImport = (RawValue[0] >> 8) & 1;
5335 NameOffset = RawValue[0] >> 9;
5338 static_assert(
sizeof(uint64_t) ==
5343 WeakImport = (RawValue[0] >> 8) & 1;
5344 NameOffset = RawValue[0] >> 9;
5347 static_assert(2 *
sizeof(uint64_t) ==
5352 NameOffset = (RawValue[0] >> 16) & 1;
5353 WeakImport = RawValue[0] >> 17;
5354 Addend = RawValue[1];
5359 const char *Str = Symbols + NameOffset;
5360 if (Str >= SymbolsEnd)
5362 Twine(NameOffset) +
" extends past end " +
5364 Targets.emplace_back(LibOrdinal, NameOffset, Str, Addend, WeakImport);
5367 return std::move(Targets);
5371 if (!DyldExportsTrieLoadCmd)
5375 *
this, DyldExportsTrieLoadCmd);
5376 if (!DyldExportsTrieOrError)
5385 if (!FuncStartsLoadCmd)
5396 return std::move(FunctionStarts);
5421 uint64_t offset = Index;
5423 while (uint64_t delta = extractor.
getULEB128(&offset)) {
5425 Out.push_back(
data);
5442 size_t MachOFilesetEntryOffset) {
5444 if (Magic ==
"\xFE\xED\xFA\xCE")
5446 UniversalIndex, MachOFilesetEntryOffset);
5447 if (Magic ==
"\xCE\xFA\xED\xFE")
5449 UniversalIndex, MachOFilesetEntryOffset);
5450 if (Magic ==
"\xFE\xED\xFA\xCF")
5452 UniversalIndex, MachOFilesetEntryOffset);
5453 if (Magic ==
"\xCF\xFA\xED\xFE")
5455 UniversalIndex, MachOFilesetEntryOffset);
5462 .
Case(
"debug_str_offs",
"debug_str_offsets")
5473 return std::vector<std::string>();
5479 EC,
"%s: expected directory 'Contents/Resources/DWARF' in dSYM bundle",
5480 Path.str().c_str());
5484 std::vector<std::string> ObjectPaths;
5486 Dir != DirEnd && !EC; Dir.increment(EC)) {
5495 ObjectPaths.push_back(ObjectPath.
str());
5502 if (ObjectPaths.empty())
5504 "%s: no objects found in dSYM bundle",
5505 Path.str().c_str());
5512#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \
5513 .Case(MACHO, llvm::binaryformat::Swift5ReflectionSectionKind::KIND)
5516#include "llvm/BinaryFormat/Swift.def"
5518#undef HANDLE_SWIFT_SECTION
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Unify divergent function exit nodes
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define offsetof(TYPE, MEMBER)
static MachO::nlist_base getSymbolTableEntryBase(const MachOObjectFile &O, DataRefImpl DRI)
static Error checkVersCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, const char *CmdName)
static Error checkSymtabCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **SymtabLoadCmd, std::list< MachOElement > &Elements)
static Error checkTwoLevelHintsCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, std::list< MachOElement > &Elements)
static Error parseBuildVersionCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, SmallVectorImpl< const char * > &BuildTools, uint32_t LoadCommandIndex)
static unsigned getPlainRelocationType(const MachOObjectFile &O, const MachO::any_relocation_info &RE)
static Error checkDysymtabCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **DysymtabLoadCmd, std::list< MachOElement > &Elements)
static Error checkDylibCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName)
static Expected< T > getStructOrErr(const MachOObjectFile &O, const char *P)
static Expected< MachOObjectFile::LoadCommandInfo > getFirstLoadCommandInfo(const MachOObjectFile &Obj)
static const char * getPtr(const MachOObjectFile &O, size_t Offset, size_t MachOFilesetEntryOffset=0)
static Error parseSegmentLoadCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, SmallVectorImpl< const char * > &Sections, bool &IsPageZeroSegment, uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders, std::list< MachOElement > &Elements)
static Error checkDyldInfoCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, const char *CmdName, std::list< MachOElement > &Elements)
static unsigned getScatteredRelocationLength(const MachO::any_relocation_info &RE)
static unsigned getPlainRelocationLength(const MachOObjectFile &O, const MachO::any_relocation_info &RE)
static Error checkSubCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName, size_t SizeOfCmd, const char *CmdStructName, uint32_t PathOffset, const char *PathFieldName)
static Error checkRpathCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex)
static T getStruct(const MachOObjectFile &O, const char *P)
static uint32_t getPlainRelocationAddress(const MachO::any_relocation_info &RE)
static const char * getSectionPtr(const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L, unsigned Sec)
static Error checkLinkerOptCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex)
static bool getPlainRelocationPCRel(const MachOObjectFile &O, const MachO::any_relocation_info &RE)
static std::array< T, N > getArray(const MachOObjectFile &O, const void *Ptr)
static unsigned getScatteredRelocationAddress(const MachO::any_relocation_info &RE)
static Error checkThreadCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName)
static Error checkLinkeditDataCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, const char *CmdName, std::list< MachOElement > &Elements, const char *ElementName)
static Error malformedError(const Twine &Msg)
static bool isLoadCommandObsolete(uint32_t cmd)
static uint32_t getSectionFlags(const MachOObjectFile &O, DataRefImpl Sec)
static int getEncodedOrdinal(T Value)
static Error checkTargetTripleCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex)
static bool getScatteredRelocationPCRel(const MachO::any_relocation_info &RE)
static Error checkOverlappingElement(std::list< MachOElement > &Elements, uint64_t Offset, uint64_t Size, const char *Name)
static StringRef parseSegmentOrSectionName(const char *P)
static Expected< MachOObjectFile::LoadCommandInfo > getLoadCommandInfo(const MachOObjectFile &Obj, const char *Ptr, uint32_t LoadCommandIndex)
static void parseHeader(const MachOObjectFile &Obj, T &Header, Error &Err)
static unsigned getCPUType(const MachOObjectFile &O)
static Error checkDyldCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName)
static Error checkNoteCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, std::list< MachOElement > &Elements)
static Error checkDylibIdCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd)
static unsigned getCPUSubType(const MachOObjectFile &O)
static Error checkEncryptCommand(const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, uint64_t cryptoff, uint64_t cryptsize, const char **LoadCmd, const char *CmdName)
static Expected< MachOObjectFile::LoadCommandInfo > getNextLoadCommandInfo(const MachOObjectFile &Obj, uint32_t LoadCommandIndex, const MachOObjectFile::LoadCommandInfo &L)
static Error malformedError(Twine Msg)
OptimizedStructLayoutField Field
static StringRef substr(StringRef Str, uint64_t Len)
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
static bool is64Bit(const char *name)
This file implements the C++20 <bit> header.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
Check if the array is empty.
Helper for Errors used as out-parameters.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
StringRef getBuffer() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
static constexpr size_t npos
std::string str() const
Get the contents as an std::string.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
Check if the string is empty.
char back() const
Get the last character in the string.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
Get the string size.
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
A table of densely packed, null-terminated strings indexed by offset.
constexpr size_t size() const
Returns the byte size of the table.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Twine utohexstr(uint64_t Val)
LLVM Value Representation.
A range adaptor for a pair of iterators.
DataRefImpl getRawDataRefImpl() const
StringRef getData() const
unsigned int getType() const
bool isLittleEndian() const
static unsigned int getMachOType(bool isLE, bool is64Bits)
LLVM_ABI StringRef segmentName(int32_t SegIndex)
LLVM_ABI StringRef sectionName(int32_t SegIndex, uint64_t SegOffset)
LLVM_ABI BindRebaseSegInfo(const MachOObjectFile *Obj)
LLVM_ABI const char * checkSegAndOffsets(int32_t SegIndex, uint64_t SegOffset, uint8_t PointerSize, uint64_t Count=1, uint64_t Skip=0)
LLVM_ABI uint64_t address(uint32_t SegIndex, uint64_t SegOffset)
DiceRef - This is a value type class that represents a single data in code entry in the table in a Ma...
ExportEntry encapsulates the current-state-of-the-walk used when doing a non-recursive walk of the tr...
LLVM_ABI StringRef name() const
LLVM_ABI ExportEntry(Error *Err, const MachOObjectFile *O, ArrayRef< uint8_t > Trie)
LLVM_ABI bool operator==(const ExportEntry &) const
LLVM_ABI StringRef otherName() const
LLVM_ABI uint64_t address() const
LLVM_ABI uint64_t flags() const
LLVM_ABI uint32_t nodeOffset() const
friend class MachOObjectFile
LLVM_ABI uint64_t other() const
LLVM_ABI StringRef sectionName() const
LLVM_ABI uint64_t segmentAddress() const
LLVM_ABI int32_t segmentIndex() const
LLVM_ABI uint64_t address() const
LLVM_ABI StringRef typeName() const
LLVM_ABI int64_t addend() const
LLVM_ABI MachOAbstractFixupEntry(Error *Err, const MachOObjectFile *O)
uint64_t textAddress() const
LLVM_ABI uint32_t flags() const
LLVM_ABI StringRef symbolName() const
LLVM_ABI void moveToFirst()
LLVM_ABI int ordinal() const
LLVM_ABI StringRef segmentName() const
LLVM_ABI void moveToEnd()
const MachOObjectFile * O
LLVM_ABI uint64_t segmentOffset() const
MachOBindEntry encapsulates the current state in the decompression of binding opcodes.
LLVM_ABI uint32_t flags() const
LLVM_ABI bool operator==(const MachOBindEntry &) const
LLVM_ABI StringRef symbolName() const
LLVM_ABI int ordinal() const
LLVM_ABI StringRef sectionName() const
LLVM_ABI MachOBindEntry(Error *Err, const MachOObjectFile *O, ArrayRef< uint8_t > Opcodes, bool is64Bit, MachOBindEntry::Kind)
LLVM_ABI StringRef segmentName() const
LLVM_ABI uint64_t segmentOffset() const
LLVM_ABI int64_t addend() const
LLVM_ABI uint64_t address() const
friend class MachOObjectFile
LLVM_ABI int32_t segmentIndex() const
LLVM_ABI StringRef typeName() const
LLVM_ABI bool operator==(const MachOChainedFixupEntry &) const
LLVM_ABI MachOChainedFixupEntry(Error *Err, const MachOObjectFile *O, bool Parse)
LLVM_ABI void moveToEnd()
LLVM_ABI void moveToFirst()
MachO::sub_client_command getSubClientCommand(const LoadCommandInfo &L) const
void moveSectionNext(DataRefImpl &Sec) const override
ArrayRef< char > getSectionRawFinalSegmentName(DataRefImpl Sec) const
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
Triple::ArchType getArch() const override
MachO::mach_header_64 Header64
bool isSectionData(DataRefImpl Sec) const override
const MachO::mach_header_64 & getHeader64() const
Expected< std::vector< ChainedFixupTarget > > getDyldChainedFixupTargets() const
uint64_t getSectionAlignment(DataRefImpl Sec) const override
uint32_t getScatteredRelocationType(const MachO::any_relocation_info &RE) const
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
Expected< SectionRef > getSection(unsigned SectionIndex) const
iterator_range< rebase_iterator > rebaseTable(Error &Err)
For use iterating over all rebase table entries.
std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const
load_command_iterator begin_load_commands() const
MachO::encryption_info_command_64 getEncryptionInfoCommand64(const LoadCommandInfo &L) const
StringRef getFileFormatName() const override
dice_iterator begin_dices() const
basic_symbol_iterator symbol_begin() const override
Expected< std::optional< MachO::linkedit_data_command > > getChainedFixupsLoadCommand() const
iterator_range< export_iterator > exports(Error &Err) const
For use iterating over all exported symbols.
uint64_t getSymbolIndex(DataRefImpl Symb) const
MachO::build_version_command getBuildVersionLoadCommand(const LoadCommandInfo &L) const
section_iterator section_end() const override
MachO::build_tool_version getBuildToolVersion(unsigned index) const
MachO::linkedit_data_command getDataInCodeLoadCommand() const
MachO::routines_command getRoutinesCommand(const LoadCommandInfo &L) const
MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const
unsigned getSymbolSectionID(SymbolRef Symb) const
static Expected< std::vector< std::string > > findDsymObjectMembers(StringRef Path)
If the input path is a .dSYM bundle (as created by the dsymutil tool), return the paths to the object...
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
MachO::linker_option_command getLinkerOptionLoadCommand(const LoadCommandInfo &L) const
uint32_t getLibraryCount() const
MachO::entry_point_command getEntryPointCommand(const LoadCommandInfo &L) const
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
ArrayRef< uint8_t > getDyldInfoLazyBindOpcodes() const
void moveSymbolNext(DataRefImpl &Symb) const override
SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const
MachO::dysymtab_command getDysymtabLoadCommand() const
iterator_range< bind_iterator > bindTable(Error &Err)
For use iterating over all bind table entries.
MachO::mach_header Header
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
MachO::section_64 getSection64(DataRefImpl DRI) const
MachO::fileset_entry_command getFilesetEntryLoadCommand(const LoadCommandInfo &L) const
MachO::note_command getNoteLoadCommand(const LoadCommandInfo &L) const
MachO::thread_command getThreadCommand(const LoadCommandInfo &L) const
section_iterator section_begin() const override
Error checkSymbolTable() const
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
MachO::segment_command_64 getSegment64LoadCommand(const LoadCommandInfo &L) const
relocation_iterator section_rel_end(DataRefImpl Sec) const override
ArrayRef< uint8_t > getDyldInfoExportsTrie() const
bool isDebugSection(DataRefImpl Sec) const override
MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const
unsigned getSectionType(SectionRef Sec) const
MachO::segment_command getSegmentLoadCommand(const LoadCommandInfo &L) const
static Expected< std::unique_ptr< MachOObjectFile > > create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const
MachO::linkedit_data_command getLinkOptHintsLoadCommand() const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
MachO::rpath_command getRpathCommand(const LoadCommandInfo &L) const
dice_iterator end_dices() const
MachO::routines_command_64 getRoutinesCommand64(const LoadCommandInfo &L) const
MachO::sub_framework_command getSubFrameworkCommand(const LoadCommandInfo &L) const
SmallVector< uint64_t > getFunctionStarts() const
MachO::sub_library_command getSubLibraryCommand(const LoadCommandInfo &L) const
MachO::dyld_info_command getDyldInfoLoadCommand(const LoadCommandInfo &L) const
MachO::sub_umbrella_command getSubUmbrellaCommand(const LoadCommandInfo &L) const
ArrayRef< uint8_t > getDyldExportsTrie() const
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const
bool isSectionBSS(DataRefImpl Sec) const override
Expected< std::pair< size_t, std::vector< ChainedFixupsSegment > > > getChainedFixupsSegments() const
bool isSectionVirtual(DataRefImpl Sec) const override
bool getScatteredRelocationScattered(const MachO::any_relocation_info &RE) const
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
LoadCommandList::const_iterator load_command_iterator
symbol_iterator getSymbolByIndex(unsigned Index) const
static Triple getHostArch()
MachO::encryption_info_command getEncryptionInfoCommand(const LoadCommandInfo &L) const
const MachO::mach_header & getHeader() const
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
iterator_range< bind_iterator > weakBindTable(Error &Err)
For use iterating over all weak bind table entries.
static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch)
ArrayRef< uint8_t > getDyldInfoRebaseOpcodes() const
iterator_range< load_command_iterator > load_commands() const
MachO::target_triple_command getTargetTripleLoadCommand(const LoadCommandInfo &L) const
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
MachO::symtab_command getSymtabLoadCommand() const
Triple getArchTriple(const char **McpuDefault=nullptr) const
MachO::uuid_command getUuidCommand(const LoadCommandInfo &L) const
unsigned getPlainRelocationSymbolNum(const MachO::any_relocation_info &RE) const
ArrayRef< uint8_t > getUuid() const
bool is64Bit() const override
MachO::version_min_command getVersionMinLoadCommand(const LoadCommandInfo &L) const
StringRef mapDebugSectionName(StringRef Name) const override
Maps a debug section name to a standard DWARF section name.
MachO::dylinker_command getDylinkerCommand(const LoadCommandInfo &L) const
uint64_t getRelocationType(DataRefImpl Rel) const override
relocation_iterator extrel_begin() const
void moveRelocationNext(DataRefImpl &Rel) const override
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
basic_symbol_iterator symbol_end() const override
MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset, unsigned Index) const
MachO::data_in_code_entry getDice(DataRefImpl Rel) const
bool isSectionStripped(DataRefImpl Sec) const override
When dsymutil generates the companion file, it strips all unnecessary sections (e....
uint64_t getSectionIndex(DataRefImpl Sec) const override
iterator_range< fixup_iterator > fixupTable(Error &Err)
For iterating over all chained fixups.
void ReadULEB128s(uint64_t Index, SmallVectorImpl< uint64_t > &Out) const
iterator_range< bind_iterator > lazyBindTable(Error &Err)
For use iterating over all lazy bind table entries.
load_command_iterator end_load_commands() const
ArrayRef< uint8_t > getDyldInfoBindOpcodes() const
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
uint8_t getRelocationLength(DataRefImpl Rel) const
llvm::binaryformat::Swift5ReflectionSectionKind mapReflectionSectionNameToEnumValue(StringRef SectionName) const override
ArrayRef< uint8_t > getDyldInfoWeakBindOpcodes() const
static bool isValidArch(StringRef ArchFlag)
bool isSectionText(DataRefImpl Sec) const override
bool isSectionCompressed(DataRefImpl Sec) const override
static ArrayRef< StringRef > getValidArchs()
bool isSectionBitcode(DataRefImpl Sec) const override
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
relocation_iterator locrel_begin() const
Expected< std::optional< MachO::dyld_chained_fixups_header > > getChainedFixupsHeader() const
If the optional is std::nullopt, no header was found, but the object was well-formed.
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
MachO::source_version_command getSourceVersionCommand(const LoadCommandInfo &L) const
unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const
StringRef getStringTableData() const
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
ArrayRef< char > getSectionRawName(DataRefImpl Sec) const
uint64_t getNValue(DataRefImpl Sym) const
ArrayRef< uint8_t > getSegmentContents(StringRef SegmentName) const
Return the raw contents of an entire segment.
section_iterator getRelocationSection(DataRefImpl Rel) const
unsigned getSectionID(SectionRef Sec) const
MachO::linkedit_data_command getLinkeditDataLoadCommand(const LoadCommandInfo &L) const
ArrayRef< uint8_t > getSectionContents(uint64_t Offset, uint64_t Size) const
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
MachO::dylib_command getDylibIDLoadCommand(const LoadCommandInfo &L) const
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
uint64_t getSectionSize(DataRefImpl Sec) const override
relocation_iterator extrel_end() const
static StringRef guessLibraryShortName(StringRef Name, bool &isFramework, StringRef &Suffix)
relocation_iterator locrel_end() const
std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const
MachORebaseEntry encapsulates the current state in the decompression of rebasing opcodes.
LLVM_ABI int32_t segmentIndex() const
LLVM_ABI StringRef segmentName() const
LLVM_ABI MachORebaseEntry(Error *Err, const MachOObjectFile *O, ArrayRef< uint8_t > opcodes, bool is64Bit)
LLVM_ABI bool operator==(const MachORebaseEntry &) const
LLVM_ABI uint64_t address() const
LLVM_ABI StringRef sectionName() const
friend class MachOObjectFile
LLVM_ABI uint64_t segmentOffset() const
LLVM_ABI StringRef typeName() const
This class is the base class for all object file types.
friend class RelocationRef
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
Create a MachOObjectFile instance from a given buffer.
section_iterator_range sections() const
symbol_iterator_range symbols() const
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
DataRefImpl getRawDataRefImpl() const
This is a value type class that represents a single section in the list of sections in the object fil...
DataRefImpl getRawDataRefImpl() const
bool isData() const
Whether this section contains data, not instructions.
bool isBSS() const
Whether this section contains BSS uninitialized data.
directory_iterator - Iterates through the entries in path.
Represents the result of a call to sys::fs::status().
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const uint32_t x86_FLOAT_STATE_COUNT
@ DYLD_CHAINED_IMPORT_ADDEND
@ DYLD_CHAINED_IMPORT_ADDEND64
const uint32_t ARM_THREAD_STATE64_COUNT
@ EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
@ EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
@ EXPORT_SYMBOL_FLAGS_KIND_REGULAR
@ BIND_TYPE_TEXT_ABSOLUTE32
const uint32_t x86_EXCEPTION_STATE_COUNT
@ EXPORT_SYMBOL_FLAGS_REEXPORT
@ EXPORT_SYMBOL_FLAGS_KIND_MASK
@ EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
@ REBASE_TYPE_TEXT_ABSOLUTE32
@ REBASE_TYPE_TEXT_PCREL32
@ S_GB_ZEROFILL
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
@ S_THREAD_LOCAL_ZEROFILL
S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
@ S_ZEROFILL
S_ZEROFILL - Zero fill on demand section.
@ BIND_SPECIAL_DYLIB_WEAK_LOOKUP
@ BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
@ BIND_SPECIAL_DYLIB_FLAT_LOOKUP
@ DYLD_CHAINED_PTR_START_NONE
uint8_t GET_COMM_ALIGN(uint16_t n_desc)
void swapStruct(fat_header &mh)
const uint32_t x86_THREAD_STATE32_COUNT
@ BIND_SYMBOL_FLAGS_WEAK_IMPORT
@ BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
@ CPU_SUBTYPE_POWERPC_ALL
@ BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB
@ BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB
@ BIND_OPCODE_SET_ADDEND_SLEB
@ BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
@ BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM
@ BIND_OPCODE_ADD_ADDR_ULEB
@ BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED
@ BIND_OPCODE_SET_DYLIB_SPECIAL_IMM
@ BIND_OPCODE_SET_TYPE_IMM
@ BIND_OPCODE_SET_DYLIB_ORDINAL_IMM
@ BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
@ DYLD_CHAINED_PTR_64_OFFSET
const uint32_t PPC_THREAD_STATE_COUNT
const uint32_t ARM_THREAD_STATE_COUNT
@ REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
@ REBASE_OPCODE_DO_REBASE_IMM_TIMES
@ REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB
@ REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
@ REBASE_OPCODE_DO_REBASE_ULEB_TIMES
@ REBASE_OPCODE_ADD_ADDR_ULEB
@ REBASE_OPCODE_SET_TYPE_IMM
@ REBASE_OPCODE_ADD_ADDR_IMM_SCALED
@ CPU_SUBTYPE_ARM_V8M_BASE
@ CPU_SUBTYPE_ARM_V8M_MAIN
@ CPU_SUBTYPE_ARM_V8_1M_MAIN
const uint32_t x86_THREAD_STATE_COUNT
@ CPU_SUBTYPE_ARM64_32_V8
@ GENERIC_RELOC_LOCAL_SECTDIFF
@ ARM_RELOC_LOCAL_SECTDIFF
@ ARM_RELOC_HALF_SECTDIFF
@ X86_64_RELOC_SUBTRACTOR
uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc)
@ S_ATTR_PURE_INSTRUCTIONS
S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine instructions.
const uint32_t x86_EXCEPTION_STATE64_COUNT
const uint32_t x86_THREAD_STATE64_COUNT
constexpr size_t SymbolTableEntrySize
content_iterator< SectionRef > section_iterator
Error createError(const Twine &Err)
content_iterator< MachOBindEntry > bind_iterator
content_iterator< MachOChainedFixupEntry > fixup_iterator
content_iterator< MachORebaseEntry > rebase_iterator
content_iterator< BasicSymbolRef > basic_symbol_iterator
content_iterator< ExportEntry > export_iterator
content_iterator< RelocationRef > relocation_iterator
content_iterator< DiceRef > dice_iterator
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI bool is_directory(const basic_file_status &status)
Does status represent a directory?
LLVM_ABI bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
Remove '.
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
LLVM_ABI StringRef extension(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get extension.
constexpr bool IsLittleEndianHost
LLVM_ABI std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
void swapByteOrder(T &Value)
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct an array ref of bytes from a string ref.
@ Load
The value being inserted comes from a load (InsertElement only).
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
@ no_such_file_or_directory
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
To bit_cast(const From &from) noexcept
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
void consumeError(Error Err)
Consume a Error without doing anything.
This struct is a compact representation of a valid (non-zero power of two) alignment.
dyld_chained_starts_in_image is embedded in LC_DYLD_CHAINED_FIXUPS payload.
uint16_t page_count
Length of the page_start array.
uint16_t page_size
Page size in bytes (0x1000 or 0x4000)
uint16_t pointer_format
DYLD_CHAINED_PTR*.
uint32_t size
Size of this, including chain_starts entries.
ChainedFixupTarget holds all the information about an external symbol necessary to bind this binary t...
MachO::dyld_chained_starts_in_segment Header
std::vector< uint16_t > PageStarts
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d