llvm.org GIT mirror llvm / 8894843
[MACHO] Replaced calls to getStruct with getStructOrErr in functions returning Error or Expected or similar git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362526 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Brachet 2 months ago
1 changed file(s) with 88 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
290290 for (unsigned J = 0; J < S.nsects; ++J) {
291291 const char *Sec = getSectionPtr(Obj, Load, J);
292292 Sections.push_back(Sec);
293 Section s = getStruct
(Obj, Sec);
293 auto SectionOrErr = getStructOrErr
(Obj, Sec);
294 if (!SectionOrErr)
295 return SectionOrErr.takeError();
296 Section s = SectionOrErr.get();
294297 if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
295298 Obj.getHeader().filetype != MachO::MH_DSYM &&
296299 s.flags != MachO::S_ZEROFILL &&
400403 " LC_SYMTAB cmdsize too small");
401404 if (*SymtabLoadCmd != nullptr)
402405 return malformedError("more than one LC_SYMTAB command");
403 MachO::symtab_command Symtab =
404 getStruct(Obj, Load.Ptr);
406 auto SymtabOrErr = getStructOrErr(Obj, Load.Ptr);
407 if (!SymtabOrErr)
408 return SymtabOrErr.takeError();
409 MachO::symtab_command Symtab = SymtabOrErr.get();
405410 if (Symtab.cmdsize != sizeof(MachO::symtab_command))
406411 return malformedError("LC_SYMTAB command " + Twine(LoadCommandIndex) +
407412 " has incorrect cmdsize");
456461 " LC_DYSYMTAB cmdsize too small");
457462 if (*DysymtabLoadCmd != nullptr)
458463 return malformedError("more than one LC_DYSYMTAB command");
459 MachO::dysymtab_command Dysymtab =
460 getStruct(Obj, Load.Ptr);
464 auto DysymtabOrErr =
465 getStructOrErr(Obj, Load.Ptr);
466 if (!DysymtabOrErr)
467 return DysymtabOrErr.takeError();
468 MachO::dysymtab_command Dysymtab = DysymtabOrErr.get();
461469 if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command))
462470 return malformedError("LC_DYSYMTAB command " + Twine(LoadCommandIndex) +
463471 " has incorrect cmdsize");
587595 CmdName + " cmdsize too small");
588596 if (*LoadCmd != nullptr)
589597 return malformedError("more than one " + Twine(CmdName) + " command");
590 MachO::linkedit_data_command LinkData =
591 getStruct(Obj, Load.Ptr);
598 auto LinkDataOrError =
599 getStructOrErr(Obj, Load.Ptr);
600 if (!LinkDataOrError)
601 return LinkDataOrError.takeError();
602 MachO::linkedit_data_command LinkData = LinkDataOrError.get();
592603 if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command))
593604 return malformedError(Twine(CmdName) + " command " +
594605 Twine(LoadCommandIndex) + " has incorrect cmdsize");
622633 if (*LoadCmd != nullptr)
623634 return malformedError("more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY "
624635 "command");
625 MachO::dyld_info_command DyldInfo =
626 getStruct(Obj, Load.Ptr);
636 auto DyldInfoOrErr =
637 getStructOrErr(Obj, Load.Ptr);
638 if (!DyldInfoOrErr)
639 return DyldInfoOrErr.takeError();
640 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
627641 if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command))
628642 return malformedError(Twine(CmdName) + " command " +
629643 Twine(LoadCommandIndex) + " has incorrect cmdsize");
713727 if (Load.C.cmdsize < sizeof(MachO::dylib_command))
714728 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
715729 CmdName + " cmdsize too small");
716 MachO::dylib_command D = getStruct(Obj, Load.Ptr);
730 auto CommandOrErr = getStructOrErr(Obj, Load.Ptr);
731 if (!CommandOrErr)
732 return CommandOrErr.takeError();
733 MachO::dylib_command D = CommandOrErr.get();
717734 if (D.dylib.name < sizeof(MachO::dylib_command))
718735 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
719736 CmdName + " name.offset field too small, not past "
759776 if (Load.C.cmdsize < sizeof(MachO::dylinker_command))
760777 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
761778 CmdName + " cmdsize too small");
762 MachO::dylinker_command D = getStruct(Obj, Load.Ptr);
779 auto CommandOrErr = getStructOrErr(Obj, Load.Ptr);
780 if (!CommandOrErr)
781 return CommandOrErr.takeError();
782 MachO::dylinker_command D = CommandOrErr.get();
763783 if (D.name < sizeof(MachO::dylinker_command))
764784 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
765785 CmdName + " name.offset field too small, not past "
804824 if (Load.C.cmdsize != sizeof(MachO::note_command))
805825 return malformedError("load command " + Twine(LoadCommandIndex) +
806826 " LC_NOTE has incorrect cmdsize");
807 MachO::note_command Nt = getStruct(Obj, Load.Ptr);
827 auto NoteCmdOrErr = getStructOrErr(Obj, Load.Ptr);
828 if (!NoteCmdOrErr)
829 return NoteCmdOrErr.takeError();
830 MachO::note_command Nt = NoteCmdOrErr.get();
808831 uint64_t FileSize = Obj.getData().size();
809832 if (Nt.offset > FileSize)
810833 return malformedError("offset field of LC_NOTE command " +
827850 const MachOObjectFile::LoadCommandInfo &Load,
828851 SmallVectorImpl &BuildTools,
829852 uint32_t LoadCommandIndex) {
830 MachO::build_version_command BVC =
831 getStruct(Obj, Load.Ptr);
853 auto BVCOrErr =
854 getStructOrErr(Obj, Load.Ptr);
855 if (!BVCOrErr)
856 return BVCOrErr.takeError();
857 MachO::build_version_command BVC = BVCOrErr.get();
832858 if (Load.C.cmdsize !=
833859 sizeof(MachO::build_version_command) +
834860 BVC.ntools * sizeof(MachO::build_tool_version))
849875 if (Load.C.cmdsize < sizeof(MachO::rpath_command))
850876 return malformedError("load command " + Twine(LoadCommandIndex) +
851877 " LC_RPATH cmdsize too small");
852 MachO::rpath_command R = getStruct(Obj, Load.Ptr);
878 auto ROrErr = getStructOrErr(Obj, Load.Ptr);
879 if (!ROrErr)
880 return ROrErr.takeError();
881 MachO::rpath_command R = ROrErr.get();
853882 if (R.path < sizeof(MachO::rpath_command))
854883 return malformedError("load command " + Twine(LoadCommandIndex) +
855884 " LC_RPATH path.offset field too small, not past "
902931 if (Load.C.cmdsize < sizeof(MachO::linker_option_command))
903932 return malformedError("load command " + Twine(LoadCommandIndex) +
904933 " LC_LINKER_OPTION cmdsize too small");
905 MachO::linker_option_command L =
906 getStruct(Obj, Load.Ptr);
934 auto LinkOptionOrErr =
935 getStructOrErr(Obj, Load.Ptr);
936 if (!LinkOptionOrErr)
937 return LinkOptionOrErr.takeError();
938 MachO::linker_option_command L = LinkOptionOrErr.get();
907939 // Make sure the count of strings is correct.
908940 const char *string = (const char *)Load.Ptr +
909941 sizeof(struct MachO::linker_option_command);
967999 if (Load.C.cmdsize < sizeof(MachO::thread_command))
9681000 return malformedError("load command " + Twine(LoadCommandIndex) +
9691001 CmdName + " cmdsize too small");
970 MachO::thread_command T =
971 getStruct(Obj, Load.Ptr);
1002 auto ThreadCommandOrErr =
1003 getStructOrErr(Obj, Load.Ptr);
1004 if (!ThreadCommandOrErr)
1005 return ThreadCommandOrErr.takeError();
1006 MachO::thread_command T = ThreadCommandOrErr.get();
9721007 const char *state = Load.Ptr + sizeof(MachO::thread_command);
9731008 const char *end = Load.Ptr + T.cmdsize;
9741009 uint32_t nflavor = 0;
11591194 " LC_TWOLEVEL_HINTS has incorrect cmdsize");
11601195 if (*LoadCmd != nullptr)
11611196 return malformedError("more than one LC_TWOLEVEL_HINTS command");
1162 MachO::twolevel_hints_command Hints =
1163 getStruct(Obj, Load.Ptr);
1197 auto HintsOrErr = getStructOrErr(Obj, Load.Ptr);
1198 if(!HintsOrErr)
1199 return HintsOrErr.takeError();
1200 MachO::twolevel_hints_command Hints = HintsOrErr.get();
11641201 uint64_t FileSize = Obj.getData().size();
11651202 if (Hints.offset > FileSize)
11661203 return malformedError("offset field of LC_TWOLEVEL_HINTS command " +
23952432 // all the Libraries.
23962433 if (LibrariesShortNames.size() == 0) {
23972434 for (unsigned i = 0; i < Libraries.size(); i++) {
2398 MachO::dylib_command D =
2399 getStruct(*this, Libraries[i]);
2435 auto CommandOrErr =
2436 getStructOrErr(*this, Libraries[i]);
2437 if (!CommandOrErr)
2438 return object_error::parse_failed;
2439 MachO::dylib_command D = CommandOrErr.get();
24002440 if (D.dylib.name >= D.cmdsize)
24012441 return object_error::parse_failed;
24022442 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
44904530 if (!DyldInfoLoadCmd)
44914531 return None;
44924532
4493 MachO::dyld_info_command DyldInfo =
4494 getStruct(*this, DyldInfoLoadCmd);
4533 auto DyldInfoOrErr =
4534 getStructOrErr(*this, DyldInfoLoadCmd);
4535 if (!DyldInfoOrErr)
4536 return None;
4537 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
44954538 const uint8_t *Ptr =
44964539 reinterpret_cast(getPtr(*this, DyldInfo.rebase_off));
44974540 return makeArrayRef(Ptr, DyldInfo.rebase_size);
45014544 if (!DyldInfoLoadCmd)
45024545 return None;
45034546
4504 MachO::dyld_info_command DyldInfo =
4505 getStruct(*this, DyldInfoLoadCmd);
4547 auto DyldInfoOrErr =
4548 getStructOrErr(*this, DyldInfoLoadCmd);
4549 if (!DyldInfoOrErr)
4550 return None;
4551 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
45064552 const uint8_t *Ptr =
45074553 reinterpret_cast(getPtr(*this, DyldInfo.bind_off));
45084554 return makeArrayRef(Ptr, DyldInfo.bind_size);
45124558 if (!DyldInfoLoadCmd)
45134559 return None;
45144560
4515 MachO::dyld_info_command DyldInfo =
4516 getStruct(*this, DyldInfoLoadCmd);
4561 auto DyldInfoOrErr =
4562 getStructOrErr(*this, DyldInfoLoadCmd);
4563 if (!DyldInfoOrErr)
4564 return None;
4565 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
45174566 const uint8_t *Ptr =
45184567 reinterpret_cast(getPtr(*this, DyldInfo.weak_bind_off));
45194568 return makeArrayRef(Ptr, DyldInfo.weak_bind_size);
45234572 if (!DyldInfoLoadCmd)
45244573 return None;
45254574
4526 MachO::dyld_info_command DyldInfo =
4527 getStruct(*this, DyldInfoLoadCmd);
4575 auto DyldInfoOrErr =
4576 getStructOrErr(*this, DyldInfoLoadCmd);
4577 if (!DyldInfoOrErr)
4578 return None;
4579 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
45284580 const uint8_t *Ptr =
45294581 reinterpret_cast(getPtr(*this, DyldInfo.lazy_bind_off));
45304582 return makeArrayRef(Ptr, DyldInfo.lazy_bind_size);
45344586 if (!DyldInfoLoadCmd)
45354587 return None;
45364588
4537 MachO::dyld_info_command DyldInfo =
4538 getStruct(*this, DyldInfoLoadCmd);
4589 auto DyldInfoOrErr =
4590 getStructOrErr(*this, DyldInfoLoadCmd);
4591 if (!DyldInfoOrErr)
4592 return None;
4593 MachO::dyld_info_command DyldInfo = DyldInfoOrErr.get();
45394594 const uint8_t *Ptr =
45404595 reinterpret_cast(getPtr(*this, DyldInfo.export_off));
45414596 return makeArrayRef(Ptr, DyldInfo.export_size);