diff --git a/include/ADT/AddressResolver.h b/include/ADT/AddressResolver.h index ae9d05e..bb8e646 100644 --- a/include/ADT/AddressResolver.h +++ b/include/ADT/AddressResolver.h @@ -124,7 +124,7 @@ namespace ADT { std::variant; static auto - FromLoadCommands(const ADT::MemoryMap &Map, + FromLoadCommands(const ADT::MemoryMap Map, const MachO::Header &Header, const MachO::DyldInfoCommand *DyldInfo, const MachO::LinkeditDataCommand *ChainedFixups, diff --git a/include/ADT/DeVirtualizer.h b/include/ADT/DeVirtualizer.h index 1b9a2e8..3b767ce 100644 --- a/include/ADT/DeVirtualizer.h +++ b/include/ADT/DeVirtualizer.h @@ -18,7 +18,7 @@ namespace ADT { void **EndOut = nullptr) const noexcept -> void * = 0; [[nodiscard]] virtual auto - getMapForVmRange(const ADT::Range &Range, + getMapForVmRange(const ADT::Range Range, bool IgnoreSectionBounds = false) const noexcept -> std::optional = 0; diff --git a/include/ADT/Tree.h b/include/ADT/Tree.h index 6de4f16..9a0f351 100644 --- a/include/ADT/Tree.h +++ b/include/ADT/Tree.h @@ -484,7 +484,10 @@ namespace ADT { auto Node = this->Current; this->Current = nullptr; - for (; DepthLevel != 0; DepthLevel--, Node = Node->parent()) { + for (; + this->DepthLevel != 0; + this->DepthLevel--, Node = Node->parent()) + { if (const auto PrevSibling = Node->prevSibling()) { Current = PrevSibling->get(PrevSibling); break; diff --git a/include/ADT/Trie.h b/include/ADT/Trie.h index ecdfeb6..0c4765d 100644 --- a/include/ADT/Trie.h +++ b/include/ADT/Trie.h @@ -305,7 +305,7 @@ namespace ADT { std::unique_ptr Info; std::unique_ptr NextStack; - TrieParser &Parser; + TrieParser *Parser; void SetupInfoForNewStack() noexcept { this->Info->stringRef().append(NextStack->node().prefix()); @@ -465,11 +465,11 @@ namespace ADT { // Prepare the next-stack before returning. const auto Error = - Parser.ParseNextNode(this->Begin, - Ptr, - this->End, - this->Info->rangeListRef(), - &this->NextStack->node()); + Parser->ParseNextNode(this->Begin, + Ptr, + this->End, + this->Info->rangeListRef(), + &this->NextStack->node()); UpdateOffset(); if (Error != Error::None) { @@ -496,6 +496,11 @@ namespace ADT { return Error::None; } public: + using difference_type = std::ptrdiff_t; + using value_type = IterateInfo; + + explicit Iterator() noexcept = default; + explicit Iterator(uint8_t *const Begin, uint8_t *const End, @@ -504,7 +509,7 @@ namespace ADT { const ParseOptions &Options = ParseOptions()) noexcept : Begin(Begin), End(End), Info(std::make_unique(ExportInfoParser)), - Parser(Parser) + Parser(&Parser) { Info->setMaxDepth(Options.MaxDepth); @@ -594,6 +599,34 @@ namespace ADT { return this->Info.get(); } + [[nodiscard]] constexpr + auto operator<=>(const Iterator &Other) const noexcept = default; + + [[nodiscard]] + inline auto operator==(const Iterator &Other) const noexcept { + if (this->Begin != Other.Begin && this->End != Other.End) { + return false; + } + + if (this->Info.StackList.empty()) { + if (Other.Info.StackList.empty()) { + return true; + } else { + return false; + } + } else if (Other.Info.StackList.empty()) { + return false; + } + + const auto &Stack = this->Info.StackList.back(); + const auto &OtherStack = Other.Info.StackList.back(); + + const auto &Node = Stack.node(); + const auto &OtherNode = OtherStack.node(); + + return Node.offset() == OtherNode.offset(); + } + [[nodiscard]] inline auto operator==(const IteratorEnd &) const noexcept { return this->isAtEnd(); @@ -629,6 +662,11 @@ namespace ADT { return *this; } public: + using difference_type = std::ptrdiff_t; + using value_type = IterateInfo; + + ExportIterator() noexcept = default; + explicit ExportIterator( uint8_t *Begin, @@ -647,7 +685,7 @@ namespace ADT { explicit ExportIterator( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, TrieParser &TrieParser, T &ExportInfoParser, const ParseOptions &Options = ParseOptions()) noexcept @@ -660,6 +698,10 @@ namespace ADT { } } + inline ExportIterator(const ExportIterator &Other) noexcept { + this->Iter = Other.Iter; + } + [[nodiscard]] inline auto &info() noexcept { return this->Iter.info(); } @@ -703,7 +745,7 @@ namespace ADT { return this->Iter.info(); } - [[nodiscard]] inline const auto &operator*() const noexcept { + [[nodiscard]] inline auto &operator*() const noexcept { return this->Iter.info(); } @@ -715,6 +757,21 @@ namespace ADT { return &this->Iter.info(); } + inline auto operator=(const ExportIterator &Other) noexcept + -> decltype(*this) + { + this->Iter = Other.Iter; + return *this; + } + + [[nodiscard]] constexpr auto + operator<=>(const ExportIterator &Other) const noexcept = default; + + [[nodiscard]] + inline auto operator==(const Iterator &Other) const noexcept { + return this->Iter == Other.Iter; + } + [[nodiscard]] inline auto operator==(const IteratorEnd &End) const noexcept { return this->Iter== End; @@ -741,7 +798,7 @@ namespace ADT { ExportInfoParser(ExportInfoParser) {}; constexpr explicit - Trie(const ADT::MemoryMap &Map, + Trie(const ADT::MemoryMap Map, TrieParser &TrieParser, T &ExportInfoParser) noexcept : Begin(Map.base()), @@ -803,7 +860,7 @@ namespace ADT { ExportInfoParser(ExportInfoParser) {} explicit - ExportMap(const ADT::MemoryMap &Map, + ExportMap(const ADT::MemoryMap Map, TrieParser &Parser, T &ExportInfoParser) noexcept : Begin(Map.base()), diff --git a/include/Dyld3/Platform.h b/include/Dyld3/Platform.h index c80e77d..c73b0bd 100644 --- a/include/Dyld3/Platform.h +++ b/include/Dyld3/Platform.h @@ -9,6 +9,7 @@ #include #include + #include "Utils/Assert.h" namespace Dyld3 { diff --git a/include/DyldSharedCache/DeVirtualizer.h b/include/DyldSharedCache/DeVirtualizer.h index f67dcdd..824b5c7 100644 --- a/include/DyldSharedCache/DeVirtualizer.h +++ b/include/DyldSharedCache/DeVirtualizer.h @@ -28,7 +28,7 @@ namespace DyldSharedCache { void **EndOut = nullptr) const noexcept override; [[nodiscard]] std::optional - getMapForVmRange(const ADT::Range &Range, + getMapForVmRange(const ADT::Range Range, bool IgnoreProtBounds = false) const noexcept override; [[nodiscard]] constexpr auto map() const noexcept { diff --git a/include/DyldSharedCache/Headers.h b/include/DyldSharedCache/Headers.h index 44a107c..ef227c0 100644 --- a/include/DyldSharedCache/Headers.h +++ b/include/DyldSharedCache/Headers.h @@ -483,10 +483,6 @@ namespace DyldSharedCache { [[nodiscard]] constexpr auto hasSubCacheArray() const noexcept; }; - struct SlideInfoBase { - uint32_t Version; - }; - enum class SlideInfoVersion { V1 = 1, V2, @@ -495,6 +491,14 @@ namespace DyldSharedCache { V5 }; + struct SlideInfoBase { + uint32_t Version; + + [[nodiscard]] constexpr auto version() const noexcept { + return SlideInfoVersion(this->Version); + } + }; + struct SlideInfoV1 : public SlideInfoBase { struct Entry { uint8_t Bits[4096 / (8 * 4)]; diff --git a/include/DyldSharedCache/ProgramTrie.h b/include/DyldSharedCache/ProgramTrie.h index 92a5a56..d33748a 100644 --- a/include/DyldSharedCache/ProgramTrie.h +++ b/include/DyldSharedCache/ProgramTrie.h @@ -54,7 +54,7 @@ namespace DyldSharedCache { : ADT::Trie(Begin, End, TrieParser, Info) {} explicit - ProgramTrieMap(const ADT::MemoryMap &Map, + ProgramTrieMap(const ADT::MemoryMap Map, ADT::TrieParser &TrieParser) noexcept : ADT::Trie(Map, TrieParser, Info) {} diff --git a/include/Mach/Machine.h b/include/Mach/Machine.h index b149ebf..a46b384 100644 --- a/include/Mach/Machine.h +++ b/include/Mach/Machine.h @@ -9,6 +9,7 @@ #include #include + #include "Utils/Assert.h" namespace Mach { diff --git a/include/MachO/BindInfo.h b/include/MachO/BindInfo.h index 398bcc1..1d40bcc 100644 --- a/include/MachO/BindInfo.h +++ b/include/MachO/BindInfo.h @@ -733,7 +733,7 @@ namespace MachO { bool Is64Bit : 1; public: explicit - BindOpcodeListBase(const ADT::MemoryMap &Map, + BindOpcodeListBase(const ADT::MemoryMap Map, const bool Is64Bit) noexcept : Begin(Map.base()), End(Map.end()), Is64Bit(Is64Bit) {} @@ -1179,7 +1179,7 @@ namespace MachO { }; for (; !this->isAtEnd(); this->Iter++) { - if (Iter->hasError()) { + if (this->Iter->hasError()) { return this->Iter->error(); } @@ -1380,8 +1380,8 @@ namespace MachO { Is64Bit(Is64Bit) {} constexpr explicit - BindActionListBase(const ADT::MemoryMap &Map, - const ADT::Range &Range, + BindActionListBase(const ADT::MemoryMap Map, + const ADT::Range Range, const SegmentList &SegList, const bool Is64Bit) noexcept : Map(Map.base()), SegList(SegList), @@ -1461,7 +1461,7 @@ namespace MachO { } [[nodiscard]] inline auto - getMapForVmRange(ADT::Range &VmRange, + getMapForVmRange(ADT::Range VmRange, const SegmentList &SegmentList, UnorderedMap &MapOut) const noexcept -> BindOpcodeParseResult @@ -1541,4 +1541,3 @@ namespace MachO { using LazyBindActionList = BindActionListBase; using WeakBindActionList = BindActionListBase; } - diff --git a/include/MachO/DeVirtualizer.h b/include/MachO/DeVirtualizer.h index 9b481ea..85fe6de 100644 --- a/include/MachO/DeVirtualizer.h +++ b/include/MachO/DeVirtualizer.h @@ -17,7 +17,7 @@ namespace MachO { MachO::SegmentList SegmentList; public: explicit - DeVirtualizer(const ADT::MemoryMap &Map, + DeVirtualizer(const ADT::MemoryMap Map, MachO::SegmentList &SegmentList) noexcept : Map(Map), SegmentList(SegmentList) {} @@ -37,7 +37,7 @@ namespace MachO { void **EndOut = nullptr) const noexcept override; [[nodiscard]] std::optional - getMapForVmRange(const ADT::Range &Range, + getMapForVmRange(const ADT::Range Range, bool IgnoreProtBounds = false) const noexcept override; }; } \ No newline at end of file diff --git a/include/MachO/ExportTrie.h b/include/MachO/ExportTrie.h index 9ccaddd..c21d519 100644 --- a/include/MachO/ExportTrie.h +++ b/include/MachO/ExportTrie.h @@ -468,7 +468,7 @@ namespace MachO { : ADT::Trie(Begin, End, TrieParser, ExportInfo) {} explicit - ExportTrieMap(const ADT::MemoryMap &Map, + ExportTrieMap(const ADT::MemoryMap Map, ADT::TrieParser &TrieParser) noexcept : ADT::Trie(Map, TrieParser, ExportInfo) {} diff --git a/include/MachO/LoadCommands.h b/include/MachO/LoadCommands.h index 5bafca1..5cbb56b 100644 --- a/include/MachO/LoadCommands.h +++ b/include/MachO/LoadCommands.h @@ -794,7 +794,7 @@ namespace MachO { } [[nodiscard]] - constexpr static auto KindGetDesc(const Kind Kind) noexcept + constexpr static auto KindGetDesc(const Kind Kind) noexcept -> std::optional { switch (Kind) { @@ -2278,7 +2278,7 @@ namespace MachO { } [[nodiscard]] inline auto - symbolEntryList(const ADT::MemoryMap &Map, + symbolEntryList(const ADT::MemoryMap Map, const bool IsBigEndian) const noexcept -> std::optional> { @@ -2287,7 +2287,7 @@ namespace MachO { } [[nodiscard]] inline auto - symbolEntryList(const ADT::MemoryMap &Map, + symbolEntryList(const ADT::MemoryMap Map, const bool IsBigEndian) noexcept -> std::optional> { @@ -2296,7 +2296,7 @@ namespace MachO { } [[nodiscard]] inline auto - symbolEntryList64(const ADT::MemoryMap &Map, + symbolEntryList64(const ADT::MemoryMap Map, const bool IsBigEndian) const noexcept -> std::optional> { @@ -2305,7 +2305,7 @@ namespace MachO { } [[nodiscard]] inline auto - symbolEntryList64(const ADT::MemoryMap &Map, + symbolEntryList64(const ADT::MemoryMap Map, const bool IsBigEndian) noexcept -> std::optional> { @@ -2314,7 +2314,7 @@ namespace MachO { } [[nodiscard]] inline auto - stringTable(const ADT::MemoryMap &Map, const bool IsBigEndian) noexcept + stringTable(const ADT::MemoryMap Map, const bool IsBigEndian) noexcept -> std::optional> { return Map.getRange(this->strRange(IsBigEndian)); diff --git a/include/MachO/LoadCommandsMap.h b/include/MachO/LoadCommandsMap.h index 927ac20..c12a7b1 100644 --- a/include/MachO/LoadCommandsMap.h +++ b/include/MachO/LoadCommandsMap.h @@ -6,6 +6,7 @@ // #pragma once +#include #include "ADT/MemoryMap.h" #include "LoadCommands.h" @@ -158,4 +159,5 @@ namespace MachO { }; static_assert(std::forward_iterator); + static_assert(std::ranges::forward_range); } diff --git a/include/MachO/ObjcInfo.h b/include/MachO/ObjcInfo.h index a6339fa..c21a247 100644 --- a/include/MachO/ObjcInfo.h +++ b/include/MachO/ObjcInfo.h @@ -356,11 +356,10 @@ namespace MachO { return Lhs->name() == ActionSymbol; }; - const auto Begin = ExternalAndRootClassList.cbegin(); - const auto End = ExternalAndRootClassList.cend(); - const auto Iter = std::find_if(Begin, End, Pred); + const auto Iter = + std::ranges::find_if(ExternalAndRootClassList, Pred); - if (Iter != End) { + if (Iter != ExternalAndRootClassList.end()) { SetSuperClassForClassInfo(*Iter, Info); return; } @@ -880,7 +879,7 @@ namespace MachO { ObjcClassCategoryInfoList() noexcept = default; auto - CollectFrom(const ADT::MemoryMap &Map, + CollectFrom(const ADT::MemoryMap Map, const ADT::DeVirtualizer &DeVirtualizer, const ADT::AddressResolver &AddrResolver, const SegmentList &SegmentList, diff --git a/include/MachO/OpcodeList.h b/include/MachO/OpcodeList.h index b641581..73b6be3 100644 --- a/include/MachO/OpcodeList.h +++ b/include/MachO/OpcodeList.h @@ -47,17 +47,21 @@ namespace MachO { template [[nodiscard]] inline auto asByte() const noexcept { - return reinterpret_cast(Iter); + return reinterpret_cast(this->Iter); } template [[nodiscard]] auto ReadUleb128() noexcept { - return Utils::ReadUleb128(Iter, End, &Iter); + return Utils::ReadUleb128(this->Iter, + this->End, + &this->Iter); } template [[nodiscard]] auto ReadSleb128() noexcept { - return Utils::ReadSleb128(Iter, End, &Iter); + return Utils::ReadSleb128(this->Iter, + this->End, + &this->Iter); } [[nodiscard]] diff --git a/include/MachO/RebaseInfo.h b/include/MachO/RebaseInfo.h index 1bceb19..d788b6b 100644 --- a/include/MachO/RebaseInfo.h +++ b/include/MachO/RebaseInfo.h @@ -519,7 +519,7 @@ namespace MachO { Is64Bit(Is64Bit) {} explicit - RebaseOpcodeList(const ADT::MemoryMap &Map, + RebaseOpcodeList(const ADT::MemoryMap Map, const bool Is64Bit) noexcept : Begin(Map.base()), End(Map.end()), Is64Bit(Is64Bit) {} @@ -946,7 +946,7 @@ namespace MachO { : Begin(Begin), End(End), Is64Bit(Is64Bit) {} explicit - RebaseActionList(const ADT::MemoryMap &Map, const bool Is64Bit) noexcept + RebaseActionList(const ADT::MemoryMap Map, const bool Is64Bit) noexcept : Begin(Map.base()), End(Map.end()), Is64Bit(Is64Bit) {} @@ -1022,7 +1022,7 @@ namespace MachO { [[nodiscard]] inline auto - getMapForVmRange(ADT::Range &VmRange, + getMapForVmRange(const ADT::Range VmRange, const SegmentList &SegmentList, UnorderedMap &MapOut) const noexcept -> RebaseOpcodeParseResult diff --git a/include/MachO/SegmentList.h b/include/MachO/SegmentList.h index 99ed007..cb37354 100644 --- a/include/MachO/SegmentList.h +++ b/include/MachO/SegmentList.h @@ -5,6 +5,7 @@ #pragma once +#include #include #include #include @@ -59,23 +60,25 @@ namespace MachO { auto findSectionWithName(const std::string_view Name) const noexcept -> const SectionInfo * { - for (const auto &Section : this->SectionList) { - if (Section.Name == Name) { - return &Section; - } - } + const auto Iter = + std::ranges::find(this->SectionList, Name, &SectionInfo::Name); - return nullptr; + return Iter != this->SectionList.end() ? &*Iter : nullptr; } [[nodiscard]] constexpr auto findSectionWithFileOffset(const uint64_t Offset) const noexcept -> const SectionInfo * { - for (const auto &Section : this->SectionList) { - if (Section.fileRange().hasLoc(Offset)) { - return &Section; - } + const auto Iter = + std::ranges::find_if(this->SectionList, + [Offset](const auto &Section) noexcept { + return Section.fileRange() + .hasLoc(Offset); + }); + + if (Iter != this->SectionList.end()) { + return &*Iter; } return nullptr; @@ -85,10 +88,15 @@ namespace MachO { auto findSectionWithVmAddr(const uint64_t VmAddr) const noexcept -> const SectionInfo * { - for (const auto &Section : SectionList) { - if (Section.vmRange().hasLoc(VmAddr)) { - return &Section; - } + const auto Iter = + std::ranges::find_if(this->SectionList, + [VmAddr](const auto &Section) noexcept { + return Section.vmRange() + .hasLoc(VmAddr); + }); + + if (Iter != this->SectionList.end()) { + return &*Iter; } return nullptr; @@ -100,9 +108,9 @@ namespace MachO { uint64_t *const AddrOut = nullptr) const noexcept -> const SectionInfo * { - assert(VmRange.hasIndex(AddrIndex)); + assert(this->VmRange.hasIndex(AddrIndex)); - const auto FullAddr = VmRange.locForIndex(AddrIndex); + const auto FullAddr = this->VmRange.locForIndex(AddrIndex); if (AddrOut != nullptr) { *AddrOut = FullAddr; } @@ -111,13 +119,18 @@ namespace MachO { } [[nodiscard]] constexpr auto - findSectionContainingVmRange(const ADT::Range &VmRange) const noexcept + findSectionContainingVmRange(const ADT::Range VmRange) const noexcept -> const SectionInfo * { - for (const auto &Section : SectionList) { - if (Section.vmRange().contains(VmRange)) { - return &Section; - } + const auto Iter = + std::ranges::find_if(this->SectionList, + [VmRange](const auto &Section) noexcept { + return Section.vmRange() + .contains(VmRange); + }); + + if (Iter != this->SectionList.end()) { + return &*Iter; } return nullptr; @@ -172,10 +185,11 @@ namespace MachO { auto findSegmentWithName(const std::string_view Name) const noexcept -> const SegmentInfo * { - for (const auto &Info : this->List) { - if (Info.Name == Name) { - return &Info; - } + const auto Iter = + std::ranges::find(this->List, Name, &SegmentInfo::Name); + + if (Iter != this->List.end()) { + return &*Iter; } return nullptr; @@ -185,10 +199,14 @@ namespace MachO { auto findSegmentWithFileOffset(const uint64_t Offset) const noexcept -> const SegmentInfo * { - for (const auto &Info : this->List) { - if (Info.FileRange.hasLoc(Offset)) { - return &Info; - } + const auto Iter = + std::ranges::find_if(this->List, + [Offset](const auto &Segment) noexcept { + return Segment.FileRange.hasLoc(Offset); + }); + + if (Iter != this->List.end()) { + return &*Iter; } return nullptr; @@ -198,23 +216,31 @@ namespace MachO { virtual auto findSegmentWithVmAddr(const uint64_t Addr) const noexcept -> const SegmentInfo * { - for (const auto &Info : List) { - if (Info.VmRange.hasLoc(Addr)) { - return &Info; - } + const auto Iter = + std::ranges::find_if(this->List, + [Addr](const auto &Segment) noexcept { + return Segment.VmRange.hasLoc(Addr); + }); + + if (Iter != this->List.end()) { + return &*Iter; } return nullptr; } [[nodiscard]] virtual - auto findSegmentWithVmRange(const ADT::Range &Range) const noexcept + auto findSegmentWithVmRange(const ADT::Range Range) const noexcept -> const SegmentInfo * { - for (const auto &Info : List) { - if (Info.VmRange.contains(Range)) { - return &Info; - } + const auto Iter = + std::ranges::find_if(this->List, + [Range](const auto &Segment) noexcept { + return Segment.VmRange.contains(Range); + }); + + if (Iter != this->List.end()) { + return &*Iter; } return nullptr; diff --git a/include/Objects/Base.h b/include/Objects/Base.h index 3887f96..675f841 100644 --- a/include/Objects/Base.h +++ b/include/Objects/Base.h @@ -19,7 +19,7 @@ namespace Objects { assert(Kind != Kind::None); } - explicit Base(const ADT::MemoryMap &Map) noexcept = delete; + explicit Base(const ADT::MemoryMap Map) noexcept = delete; virtual ~Base() noexcept = 0; [[nodiscard]] constexpr auto kind() const noexcept { diff --git a/include/Objects/DscImage.h b/include/Objects/DscImage.h index f8d62a1..2260b60 100644 --- a/include/Objects/DscImage.h +++ b/include/Objects/DscImage.h @@ -25,7 +25,7 @@ namespace Objects { DscImage(const DyldSharedCache &Dsc, const DyldSharedSingleCacheInfo &DscInfo, const ::DyldSharedCache::ImageInfo &ImageInfo, - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const uint32_t ImageIndex) noexcept : MachO(Map, Kind::DscImage), Dsc(Dsc), ImageInfo(ImageInfo), DscInfo(DscInfo), ImageIndex(ImageIndex) {} @@ -109,7 +109,7 @@ namespace Objects { template [[nodiscard]] inline auto - getMapForAddrRange(const ADT::Range &AddrRange, + getMapForAddrRange(const ADT::Range AddrRange, const bool InsideMappings = true) const noexcept -> std::optional< std::pair> @@ -120,7 +120,7 @@ namespace Objects { template [[nodiscard]] inline auto - getMapForFileRange(const ADT::Range &FileRange, + getMapForFileRange(const ADT::Range FileRange, const bool InsideMappings = true) const noexcept -> T * { diff --git a/include/Objects/DyldSharedCache.h b/include/Objects/DyldSharedCache.h index 63cfff2..1f72d14 100644 --- a/include/Objects/DyldSharedCache.h +++ b/include/Objects/DyldSharedCache.h @@ -5,8 +5,10 @@ #pragma once +#include #include #include +#include #include #include @@ -135,12 +137,12 @@ namespace Objects { auto OpenSubCacheFileMap(const SubCacheInfo &Info) const noexcept -> Error; - auto VerifySubCacheMap(const ADT::MemoryMap &Map) const noexcept + auto VerifySubCacheMap(const ADT::MemoryMap Map) const noexcept -> Error; explicit DyldSharedCache( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const enum CpuKind CpuKind, const std::filesystem::path &Path) noexcept : Base(Kind::DyldSharedCache), Info(Map, /*VmOffset=*/0, UINT64_MAX), @@ -163,7 +165,7 @@ namespace Objects { } public: static auto - Open(const ADT::MemoryMap &Map, + Open(const ADT::MemoryMap Map, const std::filesystem::path &Path, ADT::FileMap::Prot SubCacheProt, const SubCacheProvidedPathMap &SubCachePathMap = {}) noexcept @@ -414,7 +416,7 @@ namespace Objects { template [[nodiscard]] inline auto - getMapForAddrRange(const ADT::Range &AddrRange, + getMapForAddrRange(const ADT::Range AddrRange, const bool InsideMappings = true) const noexcept -> std::expected, Error> { @@ -429,11 +431,15 @@ namespace Objects { return Result.value(); } - for (const auto &[Name, SubCache] : this->SubCacheList) { - if (!this->subCacheHasAddress(SubCache, AddrRange.front())) { - continue; - } + const auto HasAddrFilter = + [this, AddrRange](const auto &Pair) noexcept { + return this->subCacheHasAddress(Pair.second, + AddrRange.front()); + }; + for (const auto &[Name, SubCache] : + this->SubCacheList | std::views::filter(HasAddrFilter)) + { if (const auto Error = this->OpenSubCacheFileMap(SubCache); Error.Kind != OpenError::None) { @@ -467,12 +473,18 @@ namespace Objects { } if (InsideMappings) { - for (const auto &Mapping : mappingInfoList()) { - if (Mapping.fileRange().contains(FileRange)) { - return - this->map().get( - FileRange.front()); - } + const auto MappingIter = + std::ranges::find_if(this->mappingInfoList(), + [FileRange]( + const auto &Mapping) noexcept + { + return Mapping.fileRange() + .contains(FileRange); + }); + + if (MappingIter != this->mappingInfoList().end()) { + return this->map().get( + MappingIter->fileRange().front()); } return nullptr; @@ -499,11 +511,14 @@ namespace Objects { return std::make_pair(Info, Result); } - for (const auto &[Name, SubCacheInfo] : this->SubCacheList) { - if (!this->subCacheHasAddress(SubCacheInfo, Address)) { - continue; - } + const auto HasAddrFilter = + [this, Address](const auto &Pair) noexcept { + return this->subCacheHasAddress(Pair.second, Address); + }; + for (const auto &[Name, SubCacheInfo] : + this->SubCacheList | std::views::filter(HasAddrFilter)) + { if (const auto Error = this->OpenSubCacheFileMap(SubCacheInfo); Error.Kind != OpenError::None) { @@ -512,10 +527,11 @@ namespace Objects { const auto &SubCache = SubCacheInfo.Info; if (const auto Result = - SubCache.getPtrForAddress(Address, - InsideMappings, - TotalAvailSize, - FileOffsetOut)) + SubCache.template getPtrForAddress( + Address, + InsideMappings, + TotalAvailSize, + FileOffsetOut)) { return std::make_pair(SubCache, Result); } diff --git a/include/Objects/DyldSharedCache/CacheInfo.h b/include/Objects/DyldSharedCache/CacheInfo.h index ece35b5..3b6c771 100644 --- a/include/Objects/DyldSharedCache/CacheInfo.h +++ b/include/Objects/DyldSharedCache/CacheInfo.h @@ -4,6 +4,7 @@ */ #pragma once +#include #include "ADT/MemoryMap.h" #include "DyldSharedCache/Headers.h" @@ -26,7 +27,7 @@ namespace Objects { : FileSuffix(FileSuffix), VmOffset(VmOffset), MaxVmSize(MaxVmSize) {} constexpr - DyldSharedSingleCacheInfo(const ADT::MemoryMap &Map, + DyldSharedSingleCacheInfo(const ADT::MemoryMap Map, const uint64_t VmOffset, const uint64_t MaxVmSize) noexcept : Map(Map), VmOffset(VmOffset), MaxVmSize(MaxVmSize) {} @@ -187,41 +188,45 @@ namespace Objects { } if (InsideMappings) { - for (const auto &Mapping : this->mappingInfoList()) { - if (const auto Offset = - Mapping.getFileOffsetFromAddr(Addr, MaxSizeOut)) - { - return Offset.value(); - } + auto Mappings = this->mappingInfoList() + | std::views::transform([=](const auto &Mapping) noexcept { + return Mapping.getFileOffsetFromAddr(Addr, MaxSizeOut); + }) + | std::views::filter([](const auto &Opt) noexcept { + return Opt.has_value(); + }); + + if (!Mappings.empty()) { + return Mappings.front(); } - } else { - const auto FirstMappingAddress = - this->mappingInfoList().front().Address; - if (Addr < FirstMappingAddress) { - return std::nullopt; - } + return std::nullopt; + } - const auto Offset = Addr - FirstMappingAddress; - const auto DscSize = this->map().range().size(); + const auto FirstMappingAddress = + this->mappingInfoList().front().Address; - if (Offset >= DscSize) { - return std::nullopt; - } + if (Addr < FirstMappingAddress) { + return std::nullopt; + } - if (MaxSizeOut != nullptr) { - *MaxSizeOut = DscSize - Offset; - } + const auto Offset = Addr - FirstMappingAddress; + const auto DscSize = this->map().range().size(); - return Offset; + if (Offset >= DscSize) { + return std::nullopt; } - return std::nullopt; + if (MaxSizeOut != nullptr) { + *MaxSizeOut = DscSize - Offset; + } + + return Offset; } [[nodiscard]] inline auto getFileRangeForAddrRange( - const ADT::Range &AddrRange, + const ADT::Range AddrRange, const bool InsideMappings = true) const noexcept -> std::optional { @@ -307,7 +312,7 @@ namespace Objects { template [[nodiscard]] inline auto - getMapForAddrRange(const ADT::Range &AddrRange, + getMapForAddrRange(const ADT::Range AddrRange, const bool InsideMappings = true) const noexcept -> std::optional< std::pair> diff --git a/include/Objects/FatMachO.h b/include/Objects/FatMachO.h index 7fc3160..0305dae 100644 --- a/include/Objects/FatMachO.h +++ b/include/Objects/FatMachO.h @@ -16,7 +16,7 @@ namespace Objects { protected: ADT::MemoryMap Map; - explicit FatMachO(const ADT::MemoryMap &Map) noexcept + explicit FatMachO(const ADT::MemoryMap Map) noexcept : Base(Kind::FatMachO), Map(Map) {} public: enum class OpenError { @@ -38,10 +38,10 @@ namespace Objects { ~FatMachO() noexcept override {} - static auto Open(const ADT::MemoryMap &Map) noexcept + static auto Open(const ADT::MemoryMap Map) noexcept -> std::expected; - static auto OpenAndValidateArchs(const ADT::MemoryMap &Map) noexcept + static auto OpenAndValidateArchs(const ADT::MemoryMap Map) noexcept -> std::expected; [[nodiscard]] constexpr auto map() const noexcept { diff --git a/include/Objects/MachO.h b/include/Objects/MachO.h index 8bce7cb..616f740 100644 --- a/include/Objects/MachO.h +++ b/include/Objects/MachO.h @@ -32,19 +32,19 @@ namespace Objects { protected: ADT::MemoryMap Map; - static auto VerifyMap(const ADT::MemoryMap &Map) noexcept -> OpenError; + static auto VerifyMap(const ADT::MemoryMap Map) noexcept -> OpenError; static auto - VerifyLoadCommands(const ADT::MemoryMap &Map) noexcept -> OpenError; + VerifyLoadCommands(const ADT::MemoryMap Map) noexcept -> OpenError; - explicit MachO(const ADT::MemoryMap &Map) noexcept + explicit MachO(const ADT::MemoryMap Map) noexcept : Base(Kind::MachO), Map(Map) {} - explicit MachO(const ADT::MemoryMap &Map, const enum Kind Kind) noexcept + explicit MachO(const ADT::MemoryMap Map, const enum Kind Kind) noexcept : Base(Kind), Map(Map) {} public: ~MachO() noexcept override {} - static auto Open(const ADT::MemoryMap &Map) noexcept -> + static auto Open(const ADT::MemoryMap Map) noexcept -> std::expected; [[nodiscard]] constexpr auto map() const noexcept { diff --git a/include/Objects/Open.h b/include/Objects/Open.h index 6b320a2..81a4425 100644 --- a/include/Objects/Open.h +++ b/include/Objects/Open.h @@ -67,12 +67,12 @@ namespace Objects { }; auto - Open(const ADT::MemoryMap &Map, + Open(const ADT::MemoryMap Map, std::string_view Path, ADT::FileMap::Prot Prot) noexcept -> std::expected; - auto OpenFrom(const ADT::MemoryMap &Map, const Kind FromKind) noexcept + auto OpenFrom(const ADT::MemoryMap Map, const Kind FromKind) noexcept -> std::expected; auto OpenArch(const FatMachO &MachO, const uint32_t Index) noexcept diff --git a/include/Utils/Misc.h b/include/Utils/Misc.h index aeb617d..8852dcf 100644 --- a/include/Utils/Misc.h +++ b/include/Utils/Misc.h @@ -102,7 +102,7 @@ namespace Utils { } template - constexpr auto to_uint(const std::string_view String) noexcept + [[nodiscard]] constexpr auto to_uint(const std::string_view String) noexcept -> std::optional { auto Front = String.front(); diff --git a/include/Utils/Overflow.h b/include/Utils/Overflow.h index 976970a..05fe1cc 100644 --- a/include/Utils/Overflow.h +++ b/include/Utils/Overflow.h @@ -79,8 +79,8 @@ namespace Utils { std::integral V, std::integral... Rest> - [[nodiscard]] constexpr auto - SubAndCheckOverflow(const T Lhs, const T Rhs, Rest... rest) noexcept + [[nodiscard]] constexpr + auto SubAndCheckOverflow(const T Lhs, const T Rhs, Rest... rest) noexcept -> std::optional { if (const auto FirstOpt = SubAndCheckOverflow(Lhs, Rhs)) { @@ -108,8 +108,8 @@ namespace Utils { std::integral V, std::integral... Rest> - [[nodiscard]] constexpr auto - MulAndCheckOverflow(const U Lhs, const V Rhs, Rest... TheRest) noexcept + [[nodiscard]] constexpr + auto MulAndCheckOverflow(const U Lhs, const V Rhs, Rest... TheRest) noexcept -> std::optional { if (const auto FirstOpt = MulAndCheckOverflow(Lhs, Rhs)) { @@ -124,8 +124,8 @@ namespace Utils { std::integral V, std::integral W> - [[nodiscard]] constexpr auto - MulAddAndCheckOverflow(const U F, const V S, const W Th) noexcept + [[nodiscard]] constexpr + auto MulAddAndCheckOverflow(const U F, const V S, const W Th) noexcept -> std::optional { if (const auto FirstOpt = MulAndCheckOverflow(F, S)) { @@ -140,8 +140,8 @@ namespace Utils { std::integral V, std::integral W> - [[nodiscard]] constexpr auto - AddMulAndCheckOverflow(const U F, const V S, const W Th) noexcept + [[nodiscard]] constexpr + auto AddMulAndCheckOverflow(const U F, const V S, const W Th) noexcept -> std::optional { if (const auto FirstOpt = AddAndCheckOverflow(F, S)) { diff --git a/include/Utils/Print.h b/include/Utils/Print.h index 3dcccdd..9ceb695 100644 --- a/include/Utils/Print.h +++ b/include/Utils/Print.h @@ -103,7 +103,7 @@ namespace Utils { explicit PrintRange(const T Begin, const U Size) noexcept : Begin(Begin), Size(Size) {} - explicit PrintRange(const ADT::Range &Range) noexcept + explicit PrintRange(const ADT::Range Range) noexcept : Begin(Range.front()), Size(Range.size()) {} [[nodiscard]] constexpr auto front() const noexcept { @@ -142,7 +142,7 @@ namespace Utils { auto PrintOffsetSizeInfo(FILE *OutFile, - const ADT::Range &Range, + const ADT::Range Range, bool Is64Bit, bool IsSize64Bit, bool IsOutOfBounds, @@ -567,7 +567,7 @@ struct std::formatter { return ctx.begin(); } - auto format(const ADT::Range &Range, auto &ctx) const { + auto format(const ADT::Range Range, auto &ctx) const { if (Range.front() == 0) { return std::format_to(ctx.out(), "0x0-0x{:08x}", Range.size()); } diff --git a/src/ADT/AddressResolver.cpp b/src/ADT/AddressResolver.cpp index eb1eaed..fad7462 100644 --- a/src/ADT/AddressResolver.cpp +++ b/src/ADT/AddressResolver.cpp @@ -7,12 +7,13 @@ #include "ADT/AddressResolver.h" #include "Dyld3/ChainedFixups.h" +#include "DyldSharedCache/Headers.h" #include "MachO/LoadCommands.h" namespace ADT { static auto ParseBindAndRebaseMapFromDyldInfo( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const MachO::DyldInfoCommand &DyldInfo, const MachO::SegmentList &SegmentList, const bool IsBigEndian, @@ -143,24 +144,33 @@ namespace ADT { return Dyld3::ChainedPointerKind::None; } - const auto SegmentCount = Starts->segmentCount(IsBigEndian); - for (auto I = uint32_t(); I != SegmentCount; I++) { - const auto SegOffset = Starts->segmentOffset(I, IsBigEndian); - if (SegOffset == 0) { - continue; - } - - const auto Segment = - Map.get( - FixupsHeaderRange.front() + StartsOffset + SegOffset); - - if (Segment == nullptr) { - continue; - } - - if (Segment->pageCount(IsBigEndian) != 0) { + auto Monad = + std::ranges::iota_view(static_cast(0), + Starts->segmentCount(IsBigEndian)) + | std::views::transform( + [Starts, IsBigEndian](const auto I) noexcept { + return Starts->segmentOffset(I, IsBigEndian); + }) + | std::views::transform( + [Map, FixupsHeaderRange, StartsOffset](const auto Offset) { + return + Map.get( + FixupsHeaderRange.front() + + StartsOffset + + Offset); + }) + | std::views::filter([](const auto Segment) noexcept { + return Segment != nullptr; + }) + | std::views::filter([](const auto Segment) noexcept { + return Segment->pageCount(0); + }) + | std::views::transform([IsBigEndian](const auto Segment) { return Segment->pointerFormat(IsBigEndian); - } + }); + + if (!Monad.empty()) { + return Monad.front(); } return Dyld3::ChainedPointerKind::None; @@ -168,7 +178,7 @@ namespace ADT { auto AddressResolver::FromLoadCommands( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const MachO::Header &Header, const MachO::DyldInfoCommand *const DyldInfo, const MachO::LinkeditDataCommand *const ChainedFixups, @@ -280,25 +290,25 @@ namespace ADT { return std::unexpected(PatchParseResult); } - auto SlideInfoVersion = static_cast(0); + auto SlideInfoVersion = + DyldSharedCache::SlideInfoVersion(static_cast(0)); + if (SlideInfo != nullptr) { - SlideInfoVersion = SlideInfo->Version; + SlideInfoVersion = SlideInfo->version(); } auto SlideInfoBaseAddress = static_cast(0); switch (SlideInfoVersion) { - case 0: - break; - case 1: - case 3: - case 5: + case DyldSharedCache::SlideInfoVersion::V1: + case DyldSharedCache::SlideInfoVersion::V3: + case DyldSharedCache::SlideInfoVersion::V5: if (const auto BaseAddressOpt = Image.dsc().baseAddress()) { SlideInfoBaseAddress = BaseAddressOpt.value(); } break; - case 2: - case 4: + case DyldSharedCache::SlideInfoVersion::V2: + case DyldSharedCache::SlideInfoVersion::V4: SlideInfoBaseAddress = static_cast( SlideInfo)->ValueAdd; @@ -310,7 +320,7 @@ namespace ADT { ChainedFixupsKind, std::move(PatchExportMap), SegmentList, - SlideInfoVersion, + static_cast(SlideInfoVersion), SlideInfoBaseAddress, BaseAddress); } diff --git a/src/ADT/Trie.cpp b/src/ADT/Trie.cpp index 04d2596..f173907 100644 --- a/src/ADT/Trie.cpp +++ b/src/ADT/Trie.cpp @@ -39,14 +39,11 @@ namespace ADT { } const auto Range = ADT::Range::FromEnd(Offset, OffsetEndOpt.value()); - const auto Predicate = [&Range](const ADT::Range &RhsRange) noexcept { + const auto Predicate = [Range](const ADT::Range RhsRange) noexcept { return Range.overlaps(RhsRange); }; - const auto RangeListEnd = RangeList.cend(); - if (std::find_if(RangeList.cbegin(), RangeListEnd, Predicate) != - RangeListEnd) - { + if (std::ranges::find_if(RangeList, Predicate) != RangeList.end()) { return Error::OverlappingRanges; } diff --git a/src/DyldSharedCache/DeVirtualizer.cpp b/src/DyldSharedCache/DeVirtualizer.cpp index 13c7db8..cbef330 100644 --- a/src/DyldSharedCache/DeVirtualizer.cpp +++ b/src/DyldSharedCache/DeVirtualizer.cpp @@ -29,7 +29,7 @@ namespace DyldSharedCache { } auto - DeVirtualizer::getMapForVmRange(const ADT::Range &Range, + DeVirtualizer::getMapForVmRange(const ADT::Range Range, const bool IgnoreProtBounds) const noexcept -> std::optional { diff --git a/src/DyldSharedCache/PatchInfo.cpp b/src/DyldSharedCache/PatchInfo.cpp index feefa61..d0f7194 100644 --- a/src/DyldSharedCache/PatchInfo.cpp +++ b/src/DyldSharedCache/PatchInfo.cpp @@ -5,6 +5,7 @@ // Created by Suhas Pai on 12/16/24. // +#include #include #include "DyldSharedCache/PatchInfo.h" @@ -289,7 +290,7 @@ namespace DyldSharedCache { } const auto ImagePatchLocationsList = ImagePatchLocationsListOpt.value(); - const auto &ImageExportList = + const auto ImageExportList = ImagePatchExportsList.subspan(ImageInfo.PatchExportsStartIndex, ImageInfo.PatchExportsCount); @@ -311,7 +312,7 @@ namespace DyldSharedCache { } const auto DscBaseAddress = this->DeVirtualizer.getBaseAddress(); - const auto &ExportPatchLocations = + const auto ExportPatchLocations = ImagePatchLocationsList.subspan(Export.PatchLocationsStartIndex, Export.PatchLocationsCount); @@ -492,7 +493,7 @@ namespace DyldSharedCache { continue; } - const auto &ImageClientPatches = + const auto ImageClientPatches = ImageClientPatchesList.subspan( ImageClient.PatchExportsStartIndex, ImageClient.PatchExportsCount); @@ -534,34 +535,43 @@ namespace DyldSharedCache { continue; } - const auto &LocationList = + const auto ImagePatchLocationListForClient = ImagePatchLocationsList.subspan( ClientPatch.PatchLocationsStartIndex, ClientPatch.PatchLocationsCount); - for (const auto &Loc : LocationList) { - const auto FullOffset = - this->ImageBaseAddress + Loc.DylibOffsetOfUse; - - if (!VmRange.hasLoc(FullOffset)) { - continue; - } - - const auto PatchLoc = PatchLocation { - .ExportName = ExportName, - .Addend = Loc.getAddend(), - .FullImplAddress = - this->ImageBaseAddress + - ExportInfo.DylibOffsetOfImpl, - .IsAuthenticated = Loc.Authenticated != 0, - .UsesAddressDiversity = - Loc.UsesAddressDiversity != 0, - .IsWeakImport = false, - .Key = static_cast(Loc.Key) - }; - - Map.emplace(FullOffset, PatchLoc); - } + auto LocationList = ImagePatchLocationListForClient + | std::views::filter( + [this, VmRange](const auto &Loc) { + return VmRange.hasLoc( + this->ImageBaseAddress + + Loc.DylibOffsetOfUse); + }) + | std::views::transform( + [this, &ExportInfo, ExportName](const auto &Loc) + -> std::pair + { + const auto PatchLoc = PatchLocation { + .ExportName = ExportName, + .Addend = Loc.Addend, + .FullImplAddress = + this->ImageBaseAddress + + ExportInfo.DylibOffsetOfImpl, + .IsAuthenticated = + Loc.Authenticated != 0, + .UsesAddressDiversity = + Loc.UsesAddressDiversity != 0, + .IsWeakImport = false, + .Key = static_cast(Loc.Key) + }; + + return std::make_pair( + this->ImageBaseAddress + + Loc.DylibOffsetOfUse, + std::move(PatchLoc)); + }); + + Map.insert_range(LocationList); } break; @@ -607,32 +617,38 @@ namespace DyldSharedCache { continue; } - const auto &LocationList = + const auto ImagePatchLocationListForClient = ImagePatchLocationsList.subspan( ClientPatch.PatchLocationsStartIndex, ClientPatch.PatchLocationsCount); - for (const auto &Loc : LocationList) { - const auto PatchLoc = PatchLocation { - .ExportName = ExportName, - .Addend = Loc.Addend, - .FullImplAddress = - this->ImageBaseAddress + - ExportInfo.DylibOffsetOfImpl, - .IsAuthenticated = Loc.Authenticated != 0, - .UsesAddressDiversity = - Loc.UsesAddressDiversity != 0, - .IsWeakImport = false, - .Key = static_cast(Loc.Key) - }; - - if (!VmRange.hasLoc(Loc.CacheOffsetOfUse)) { - continue; - } - - Map.emplace(Loc.CacheOffsetOfUse, PatchLoc); - } - + auto PatchLocList = ImagePatchLocationListForClient + | std::views::filter([VmRange](const auto &Loc) { + return VmRange.hasLoc(Loc.CacheOffsetOfUse); + }) + | std::views::transform( + [this, &ExportInfo, ExportName](const auto &Loc) + -> std::pair + { + const auto PatchLoc = PatchLocation { + .ExportName = ExportName, + .Addend = Loc.Addend, + .FullImplAddress = + this->ImageBaseAddress + + ExportInfo.DylibOffsetOfImpl, + .IsAuthenticated = + Loc.Authenticated != 0, + .UsesAddressDiversity = + Loc.UsesAddressDiversity != 0, + .IsWeakImport = false, + .Key = static_cast(Loc.Key) + }; + + return std::make_pair(Loc.CacheOffsetOfUse, + std::move(PatchLoc)); + }); + + Map.insert_range(PatchLocList); if (Utils::IndexAndCountOutOfBounds( ClientPatch.PatchLocationsStartIndex, ClientPatch.PatchLocationsCount, @@ -641,31 +657,38 @@ namespace DyldSharedCache { continue; } - const auto &GOTLocationList = + const auto GotPatchListForClient = GOTPatchList.subspan( ClientPatch.PatchLocationsStartIndex, ClientPatch.PatchLocationsCount); - for (const auto &Loc : GOTLocationList) { - if (!VmRange.hasLoc(Loc.CacheOffsetOfUse)) { - continue; - } - - const auto PatchLoc = PatchLocation { - .ExportName = ExportName, - .Addend = Loc.Addend, - .FullImplAddress = - this->ImageBaseAddress + - ExportInfo.DylibOffsetOfImpl, - .IsAuthenticated = Loc.Authenticated != 0, - .UsesAddressDiversity = - Loc.UsesAddressDiversity != 0, - .IsWeakImport = false, - .Key = static_cast(Loc.Key) - }; - - Map.emplace(Loc.CacheOffsetOfUse, PatchLoc); - } + auto GOTLocList = GotPatchListForClient + | std::views::filter([VmRange](const auto &Loc) { + return VmRange.hasLoc(Loc.CacheOffsetOfUse); + }) + | std::views::transform( + [this, &ExportInfo, ExportName](const auto &Loc) + -> std::pair + { + const auto PatchLoc = PatchLocation { + .ExportName = ExportName, + .Addend = Loc.Addend, + .FullImplAddress = + this->ImageBaseAddress + + ExportInfo.DylibOffsetOfImpl, + .IsAuthenticated = + Loc.Authenticated != 0, + .UsesAddressDiversity = + Loc.UsesAddressDiversity != 0, + .IsWeakImport = false, + .Key = static_cast(Loc.Key) + }; + + return std::make_pair(Loc.CacheOffsetOfUse, + std::move(PatchLoc)); + }); + + Map.insert_range(GOTLocList); } break; @@ -707,34 +730,45 @@ namespace DyldSharedCache { continue; } - const auto &LocationList = + const auto ImagePatchLocationListForClient = ImagePatchLocationsList.subspan( ClientPatch.PatchLocationsStartIndex, ClientPatch.PatchLocationsCount); - for (const auto &Loc : LocationList) { - const auto FullAddress = - this->ImageBaseAddress + Loc.DylibOffsetOfUse; - - if (!VmRange.hasLoc(FullAddress)) { - continue; - } - - const auto PatchLoc = PatchLocation { - .ExportName = ExportName, - .Addend = Loc.getAddend(), - .FullImplAddress = - this->ImageBaseAddress + - ExportInfo.DylibOffsetOfImpl, - .IsAuthenticated = Loc.Auth.Authenticated != 0, - .UsesAddressDiversity = - Loc.Auth.UsesAddressDiversity != 0, - .IsWeakImport = Loc.isWeakImport(), - .Key = static_cast(Loc.Auth.KeyIsD), - }; - - Map.emplace(FullAddress, PatchLoc); - } + auto LocationList = ImagePatchLocationListForClient + | std::views::filter( + [this, VmRange](const auto &Loc) { + return VmRange.hasLoc( + this->ImageBaseAddress + + Loc.DylibOffsetOfUse); + } + ) + | std::views::transform( + [this, &ExportInfo, ExportName](const auto &Loc) + -> std::pair + { + const auto PatchLoc = PatchLocation { + .ExportName = ExportName, + .Addend = Loc.getAddend(), + .FullImplAddress = + this->ImageBaseAddress + + ExportInfo.DylibOffsetOfImpl, + .IsAuthenticated = + Loc.Auth.Authenticated != 0, + .UsesAddressDiversity = + Loc.Auth.UsesAddressDiversity != 0, + .IsWeakImport = Loc.isWeakImport(), + .Key = static_cast( + Loc.Auth.KeyIsD) + }; + + return std::make_pair( + this->ImageBaseAddress + + Loc.DylibOffsetOfUse, + std::move(PatchLoc)); + }); + + Map.insert_range(LocationList); if (Utils::IndexAndCountOutOfBounds( ClientPatch.PatchLocationsStartIndex, @@ -744,31 +778,41 @@ namespace DyldSharedCache { continue; } - const auto &GOTLocationList = + const auto GOTPatchLocationListForClient = GOTPatchList.subspan( ClientPatch.PatchLocationsStartIndex, ClientPatch.PatchLocationsCount); - for (const auto &Loc : GOTLocationList) { - if (!VmRange.hasLoc(Loc.CacheOffsetOfUse)) { - continue; - } - - const auto PatchLoc = PatchLocation { - .ExportName = ExportName, - .Addend = Loc.getAddend(), - .FullImplAddress = - this->ImageBaseAddress + - ExportInfo.DylibOffsetOfImpl, - .IsAuthenticated = Loc.Auth.Authenticated != 0, - .UsesAddressDiversity = - Loc.Auth.UsesAddressDiversity != 0, - .IsWeakImport = Loc.isWeakImport(), - .Key = static_cast(Loc.Auth.KeyIsD), - }; - - Map.emplace(Loc.CacheOffsetOfUse, PatchLoc); - } + auto GOTLocationList = GOTPatchLocationListForClient + | std::views::filter( + [VmRange](const auto &Loc) { + return VmRange.hasLoc(Loc.CacheOffsetOfUse); + } + ) + | std::views::transform( + [this, &ExportInfo, ExportName](const auto &Loc) + -> std::pair + { + const auto PatchLoc = PatchLocation { + .ExportName = ExportName, + .Addend = Loc.getAddend(), + .FullImplAddress = + this->ImageBaseAddress + + ExportInfo.DylibOffsetOfImpl, + .IsAuthenticated = + Loc.Auth.Authenticated != 0, + .UsesAddressDiversity = + Loc.Auth.UsesAddressDiversity != 0, + .IsWeakImport = Loc.isWeakImport(), + .Key = static_cast( + Loc.Auth.KeyIsD) + }; + + return std::make_pair(Loc.CacheOffsetOfUse, + std::move(PatchLoc)); + }); + + Map.insert_range(GOTLocationList); } break; diff --git a/src/MachO/DeVirtualizer.cpp b/src/MachO/DeVirtualizer.cpp index 43053b8..ab1a3ab 100644 --- a/src/MachO/DeVirtualizer.cpp +++ b/src/MachO/DeVirtualizer.cpp @@ -58,7 +58,7 @@ namespace MachO { auto DeVirtualizer::getMapForVmRange( - const ADT::Range &Range, + const ADT::Range Range, const bool IgnoreSectionBounds) const noexcept -> std::optional { diff --git a/src/MachO/LibraryList.cpp b/src/MachO/LibraryList.cpp index 014d24f..95032cd 100644 --- a/src/MachO/LibraryList.cpp +++ b/src/MachO/LibraryList.cpp @@ -3,6 +3,8 @@ * © suhas pai */ +#include + #include "MachO/LibraryList.h" #include "MachO/LoadCommands.h" @@ -10,10 +12,14 @@ namespace MachO { LibraryList::LibraryList(const MachO::LoadCommandsMap &Map, const bool IsBigEndian) noexcept { - for (const auto &LC : Map) { - if (LC.isSharedLibrary(IsBigEndian)) { - this->add(cast(LC, IsBigEndian), IsBigEndian); - } - } + const auto Filter = [IsBigEndian](const auto &LC) noexcept { + return LC.isSharedLibrary(IsBigEndian); + }; + + std::ranges::for_each(Map | std::views::filter(Filter), + [this, IsBigEndian](const auto &LC) { + this->add(cast(LC, IsBigEndian), + IsBigEndian); + }); } } diff --git a/src/MachO/ObjcInfo.cpp b/src/MachO/ObjcInfo.cpp index b8c2b1d..f30835c 100644 --- a/src/MachO/ObjcInfo.cpp +++ b/src/MachO/ObjcInfo.cpp @@ -21,9 +21,9 @@ namespace MachO { Info(), /*ClassAddr=*/0)); - Root->setIsNull(); + this->Root->setIsNull(); for (const auto &Node : List) { - Root->addChild(*Node); + this->Root->addChild(*Node); } } else { this->setRoot(List.front()); @@ -210,9 +210,9 @@ namespace MachO { auto ObjcClassInfoList::getAsList() const noexcept -> std::vector { auto Vector = std::vector(); - Vector.reserve(List.size()); + Vector.reserve(this->List.size()); - for (const auto &Info : List) { + for (const auto &Info : this->List) { Vector.emplace_back(Info.second.get()); } @@ -260,10 +260,14 @@ namespace MachO { auto ObjcClassInfoList::getInfoForClassName( const std::string_view Name) const noexcept -> ObjcClassInfo * { - for (const auto &Info : List) { - if (Info.second->name() == Name) { - return Info.second.get(); - } + const auto Iter = + std::ranges::find_if(this->List, + [Name](const auto &Pair) noexcept { + return Name == Pair.second->name(); + }); + + if (Iter != this->List.end()) { + return Iter->second.get(); } return nullptr; @@ -272,7 +276,7 @@ namespace MachO { template static void ParseObjcClassCategorySection( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const ADT::DeVirtualizer &DeVirt, const ADT::AddressResolver &AddrResolver, const SectionInfo &SectInfo, @@ -405,7 +409,7 @@ namespace MachO { auto ObjcClassCategoryInfoList::CollectFrom( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const ADT::DeVirtualizer &DeVirtualizer, const ADT::AddressResolver &AddrResolver, const SegmentList &SegmentList, diff --git a/src/MachO/SegmentList.cpp b/src/MachO/SegmentList.cpp index 7b8a70c..74bdfa5 100644 --- a/src/MachO/SegmentList.cpp +++ b/src/MachO/SegmentList.cpp @@ -11,21 +11,31 @@ namespace MachO { { const auto IsBigEndian = Map.isBigEndian(); if (Is64Bit) { - for (const auto &LC : Map) { - if (const auto Segment = - dyn_cast(&LC, IsBigEndian)) - { - this->add(*Segment, IsBigEndian); - } - } + auto SegmentList = Map + | std::views::transform([IsBigEndian](const auto &Lc) noexcept { + return dyn_cast(&Lc, IsBigEndian); + }) + | std::views::filter([](const auto Segment) noexcept { + return Segment != nullptr; + }); + + std::ranges::for_each(SegmentList, + [this, IsBigEndian](const auto Segment) { + this->add(*Segment, IsBigEndian); + }); } else { - for (const auto &LC : Map) { - if (const auto Segment = - dyn_cast(&LC, IsBigEndian)) - { - this->add(*Segment, IsBigEndian); - } - } + auto SegmentList = Map + | std::views::transform([IsBigEndian](const auto &Lc) noexcept { + return dyn_cast(&Lc, IsBigEndian); + }) + | std::views::filter([](const auto Segment) noexcept { + return Segment != nullptr; + }); + + std::ranges::for_each(SegmentList, + [this, IsBigEndian](const auto Segment) { + this->add(*Segment, IsBigEndian); + }); } } @@ -45,22 +55,26 @@ namespace MachO { .Index = static_cast(List.size()) }); - Info.SectionList.reserve(Segment.sectionCount(IsBigEndian)); - for (const auto &Section : Segment.sectionList(IsBigEndian)) { - Info.SectionList.emplace_back(SectionInfo { - .Name = std::string(Section.sectionName()), - .Addr = Section.addr(IsBigEndian), - .Size = Section.size(IsBigEndian), - .FileOffset = Section.fileOffset(IsBigEndian), - .Align = Section.align(IsBigEndian), - .RelocFileOffset = Section.relocFileOffset(IsBigEndian), - .RelocsCount = Section.relocsCount(IsBigEndian), - .Flags = Section.flags(IsBigEndian), - .Reserved1 = Section.reserved1(IsBigEndian), - .Reserved2 = Section.reserved2(IsBigEndian), - .Reserved3 = 0 - }); - } + const auto SectionListRange = Segment.sectionList(IsBigEndian) + | std::views::transform( + [IsBigEndian](const auto &Section) noexcept { + return SectionInfo { + .Name = std::string(Section.sectionName()), + .Addr = Section.addr(IsBigEndian), + .Size = Section.size(IsBigEndian), + .FileOffset = Section.fileOffset(IsBigEndian), + .Align = Section.align(IsBigEndian), + .RelocFileOffset = Section.relocFileOffset(IsBigEndian), + .RelocsCount = Section.relocsCount(IsBigEndian), + .Flags = Section.flags(IsBigEndian), + .Reserved1 = Section.reserved1(IsBigEndian), + .Reserved2 = Section.reserved2(IsBigEndian), + .Reserved3 = 0 + }; + }); + + Info.SectionList.insert_range( + Info.SectionList.begin(), SectionListRange); return *this; } @@ -81,22 +95,26 @@ namespace MachO { .Index = static_cast(List.size()) }); - Info.SectionList.reserve(Segment.sectionCount(IsBigEndian)); - for (const auto &Section : Segment.sectionList(IsBigEndian)) { - Info.SectionList.emplace_back(SectionInfo { - .Name = std::string(Section.sectionName()), - .Addr = Section.addr(IsBigEndian), - .Size = Section.size(IsBigEndian), - .FileOffset = Section.fileOffset(IsBigEndian), - .Align = Section.align(IsBigEndian), - .RelocFileOffset = Section.relocFileOffset(IsBigEndian), - .RelocsCount = Section.relocsCount(IsBigEndian), - .Flags = Section.flags(IsBigEndian), - .Reserved1 = Section.reserved1(IsBigEndian), - .Reserved2 = Section.reserved2(IsBigEndian), - .Reserved3 = Section.reserved3(IsBigEndian) - }); - } + const auto SectionListRange = Segment.sectionList(IsBigEndian) + | std::views::transform( + [IsBigEndian](const auto &Section) noexcept { + return SectionInfo { + .Name = std::string(Section.sectionName()), + .Addr = Section.addr(IsBigEndian), + .Size = Section.size(IsBigEndian), + .FileOffset = Section.fileOffset(IsBigEndian), + .Align = Section.align(IsBigEndian), + .RelocFileOffset = Section.relocFileOffset(IsBigEndian), + .RelocsCount = Section.relocsCount(IsBigEndian), + .Flags = Section.flags(IsBigEndian), + .Reserved1 = Section.reserved1(IsBigEndian), + .Reserved2 = Section.reserved2(IsBigEndian), + .Reserved3 = Section.reserved3(IsBigEndian) + }; + }); + + Info.SectionList.insert_range( + Info.SectionList.begin(), SectionListRange); return *this; } @@ -106,11 +124,11 @@ namespace MachO { const uint64_t Size) const noexcept -> std::optional { - for (const auto &SegInfo : this->List) { - if (!SegInfo.VmRange.hasLoc(VmAddr)) { - continue; - } + const auto Filter = [VmAddr](const auto &SegInfo) noexcept { + return SegInfo.VmRange.hasLoc(VmAddr); + }; + for (const auto &SegInfo : this->List | std::views::filter(Filter)) { const uint64_t VmIndex = SegInfo.VmRange.indexForLoc(VmAddr); if (!SegInfo.FileRange.hasIndex(VmIndex)) { return std::nullopt; diff --git a/src/Objects/Base.cpp b/src/Objects/Base.cpp index ed9754f..0b7283f 100644 --- a/src/Objects/Base.cpp +++ b/src/Objects/Base.cpp @@ -15,7 +15,7 @@ namespace Objects { Base::~Base() noexcept {} auto - Open(const ADT::MemoryMap &Map, + Open(const ADT::MemoryMap Map, const std::string_view Path, const ADT::FileMap::Prot Prot) noexcept -> std::expected @@ -71,7 +71,7 @@ namespace Objects { return std::unexpected(OpenError::unrecognized()); } - auto OpenFrom(const ADT::MemoryMap &Map, const Kind FromKind) noexcept + auto OpenFrom(const ADT::MemoryMap Map, const Kind FromKind) noexcept -> std::expected { const auto Kind = Kind::None; diff --git a/src/Objects/DyldSharedCache.cpp b/src/Objects/DyldSharedCache.cpp index 62b6586..70214e1 100644 --- a/src/Objects/DyldSharedCache.cpp +++ b/src/Objects/DyldSharedCache.cpp @@ -108,7 +108,7 @@ namespace Objects { static auto VerifyHeaderMagicAndCpuKind( - const std::string_view &Magic, + const std::string_view Magic, enum DyldSharedCache::CpuKind &CpuKindOut) noexcept -> DyldSharedCache::OpenError { @@ -178,7 +178,7 @@ namespace Objects { } static auto - VerifyMap(const ADT::MemoryMap &Map, + VerifyMap(const ADT::MemoryMap Map, enum DyldSharedCache::CpuKind &CpuKindOut) noexcept -> DyldSharedCache::OpenError { @@ -244,7 +244,7 @@ namespace Objects { } auto - DyldSharedCache::VerifySubCacheMap(const ADT::MemoryMap &Map) const noexcept + DyldSharedCache::VerifySubCacheMap(const ADT::MemoryMap Map) const noexcept -> Error { using OpenError = DyldSharedCache::OpenError; @@ -579,7 +579,7 @@ namespace Objects { auto DyldSharedCache::Open( - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const std::filesystem::path &Path, const ADT::FileMap::Prot SubCacheProt, const SubCacheProvidedPathMap &SubCacheProvidedPathMap) noexcept @@ -624,13 +624,17 @@ namespace Objects { const auto MappingAndSlideInfoSpan = this->headerV7().mappingWithSlideInfoSpan(); + const auto HasSlideInfoFilter = + [](const auto &MappingWithSlideInfo) noexcept { + return MappingWithSlideInfo.SlideInfoFileOffset != 0; + }; for (const auto &MappingWithSlideInfo : - std::views::reverse(MappingAndSlideInfoSpan)) + MappingAndSlideInfoSpan | + std::views::reverse | + std::views::filter(HasSlideInfoFilter)) { - if (MappingWithSlideInfo.SlideInfoFileOffset != 0) { - return MappingWithSlideInfo.slideInfoFileRange(); - } + return MappingWithSlideInfo.slideInfoFileRange(); } return std::nullopt; diff --git a/src/Objects/FatMachO.cpp b/src/Objects/FatMachO.cpp index 3a4af34..0fdb769 100644 --- a/src/Objects/FatMachO.cpp +++ b/src/Objects/FatMachO.cpp @@ -9,7 +9,7 @@ #include "Utils/Overflow.h" namespace Objects { - static auto ValidateHeader(const ADT::MemoryMap &Map) noexcept { + static auto ValidateHeader(const ADT::MemoryMap Map) noexcept { const auto Header = Map.base<::MachO::FatHeader>(); if (Header == nullptr) { const auto Magic = Map.base<::MachO::Magic>(); @@ -53,7 +53,7 @@ namespace Objects { return FatMachO::OpenError::None; } - auto FatMachO::Open(const ADT::MemoryMap &Map) noexcept + auto FatMachO::Open(const ADT::MemoryMap Map) noexcept -> std::expected { if (const auto Err = ValidateHeader(Map); Err != OpenError::None) { @@ -63,7 +63,7 @@ namespace Objects { return new FatMachO(Map); } - auto FatMachO::OpenAndValidateArchs(const ADT::MemoryMap &Map) noexcept + auto FatMachO::OpenAndValidateArchs(const ADT::MemoryMap Map) noexcept -> std::expected { if (const auto Err = ValidateHeader(Map); Err != OpenError::None) { diff --git a/src/Objects/MachO.cpp b/src/Objects/MachO.cpp index fcab03a..70112c9 100644 --- a/src/Objects/MachO.cpp +++ b/src/Objects/MachO.cpp @@ -9,7 +9,7 @@ #include "Utils/Overflow.h" namespace Objects { - auto MachO::VerifyMap(const ADT::MemoryMap &Map) noexcept -> OpenError { + auto MachO::VerifyMap(const ADT::MemoryMap Map) noexcept -> OpenError { const auto Header = Map.base<::MachO::Header>(); if (Header == nullptr) { const auto Magic = Map.base<::MachO::Magic>(); @@ -31,7 +31,7 @@ namespace Objects { return OpenError::None; } - auto MachO::VerifyLoadCommands(const ADT::MemoryMap &Map) noexcept + auto MachO::VerifyLoadCommands(const ADT::MemoryMap Map) noexcept -> OpenError { const auto Header = Map.base<::MachO::Header, /*Verify=*/false>(); @@ -56,7 +56,7 @@ namespace Objects { return OpenError::None; } - auto MachO::Open(const ADT::MemoryMap &Map) noexcept + auto MachO::Open(const ADT::MemoryMap Map) noexcept -> std::expected { const auto Header = Map.base<::MachO::Header>(); diff --git a/src/Operations/PrintArchs.cpp b/src/Operations/PrintArchs.cpp index 0281825..47b7cde 100644 --- a/src/Operations/PrintArchs.cpp +++ b/src/Operations/PrintArchs.cpp @@ -49,12 +49,12 @@ namespace Operations { return Verbose ? Mach::CpuKindGetString(CpuKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()) : Mach::CpuKindGetDesc(CpuKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()); @@ -72,12 +72,12 @@ namespace Operations { return Verbose ? Mach::CpuKindAndSubKindGetString(CpuKind, SubKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()) : Mach::CpuKindAndSubKindGetDesc(CpuKind, SubKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()); @@ -226,4 +226,3 @@ namespace Operations { assert(false && "Got unrecognized Object-Kind in PrintArchs::run()"); } } - diff --git a/src/Operations/PrintBindActionList.cpp b/src/Operations/PrintBindActionList.cpp index cb2f0aa..ee119da 100644 --- a/src/Operations/PrintBindActionList.cpp +++ b/src/Operations/PrintBindActionList.cpp @@ -4,6 +4,7 @@ */ #include +#include #include "ADT/Maximizer.h" #include "MachO/BindInfo.h" @@ -43,41 +44,30 @@ namespace Operations { "PrintBindActionList::supportsObjectKind()"); } - static int + static auto CompareActionsBySortKind( const MachO::BindActionInfo &Lhs, const MachO::BindActionInfo &Rhs, const PrintBindActionList::Options::SortKind SortKind) noexcept + -> std::strong_ordering { switch (SortKind) { case PrintBindActionList::Options::SortKind::None: assert(false && "Unrecognized PrintBindActionList::Options::SortKind"); case PrintBindActionList::Options::SortKind::ByName: - return Lhs.SymbolName.compare(Rhs.SymbolName); + return Lhs.SymbolName <=> Rhs.SymbolName; case PrintBindActionList::Options::SortKind::ByDylibOrdinal: - if (Lhs.DylibOrdinal < Rhs.DylibOrdinal) { - return -1; - } else if (Lhs.DylibOrdinal == Rhs.DylibOrdinal) { - return 0; - } - - return 1; + return Lhs.DylibOrdinal <=> Rhs.DylibOrdinal; case PrintBindActionList::Options::SortKind::ByKind: { const auto LhsKind = static_cast(Lhs.WriteKind); const auto RhsKind = static_cast(Rhs.WriteKind); - if (LhsKind < RhsKind) { - return -1; - } else if (LhsKind == RhsKind) { - return 0; - } - - return 1; + return LhsKind <=> RhsKind; } } - return false; + assert(false && "Unrecognized PrintBindActionList::Options::SortKind"); } template @@ -163,7 +153,7 @@ namespace Operations { PrintBindActionInfoList( FILE *const OutFile, const std::string_view Name, - const std::vector &List, + const std::span List, const MachO::SegmentList &SegmentList, const MachO::LibraryList &LibraryList, const bool Is64Bit, @@ -330,28 +320,22 @@ namespace Operations { [&](const MachO::BindActionInfo &Lhs, const MachO::BindActionInfo &Rhs) noexcept { - auto Compare = int(); + auto Compare = std::strong_ordering::equivalent; for (const auto &SortKind : Opt.SortKindList) { Compare = CompareActionsBySortKind(Lhs, Rhs, SortKind); - if (Compare != 0) { + if (Compare != std::strong_ordering::equivalent) { break; } continue; } - return Compare < 0; + return Compare == std::strong_ordering::less; }; - std::sort(BindActionInfoList.begin(), - BindActionInfoList.end(), - Comparator); - std::sort(LazyBindActionInfoList.begin(), - LazyBindActionInfoList.end(), - Comparator); - std::sort(WeakBindActionInfoList.begin(), - WeakBindActionInfoList.end(), - Comparator); + std::ranges::sort(BindActionInfoList, Comparator); + std::ranges::sort(LazyBindActionInfoList, Comparator); + std::ranges::sort(WeakBindActionInfoList, Comparator); } const auto OutFile = this->OutFile; diff --git a/src/Operations/PrintBindOpcodeList.cpp b/src/Operations/PrintBindOpcodeList.cpp index ee4600c..c790ed4 100644 --- a/src/Operations/PrintBindOpcodeList.cpp +++ b/src/Operations/PrintBindOpcodeList.cpp @@ -156,8 +156,8 @@ namespace Operations { const auto PtrSize = Utils::PointerSize(Is64Bit); const auto AddrInSegOpt = - Utils::AddAndCheckOverflow( - OpcodeInfo.AddrInSeg, PtrSize); + Utils::AddAndCheckOverflow(OpcodeInfo.AddrInSeg, + PtrSize); if (!AddrInSegOpt.has_value()) { OpcodeInfo.AddrInSegOverflows = true; @@ -279,7 +279,7 @@ namespace Operations { FILE *const OutFile, const std::string_view Name, const MachO::LibraryList &LibraryList, - const std::vector &List, + const std::span List, const bool Is64Bit, const struct PrintBindOpcodeList::Options &Options) noexcept { diff --git a/src/Operations/PrintBindSymbolList.cpp b/src/Operations/PrintBindSymbolList.cpp index 50d118c..d5eb364 100644 --- a/src/Operations/PrintBindSymbolList.cpp +++ b/src/Operations/PrintBindSymbolList.cpp @@ -155,7 +155,7 @@ namespace Operations { PrintBindActionInfoList( FILE *const OutFile, const std::string_view Name, - const std::vector &List, + const std::span List, const MachO::SegmentList &SegmentList, const MachO::LibraryList &LibraryList, bool Is64Bit, @@ -336,15 +336,9 @@ namespace Operations { return false; }; - std::sort(BindActionInfoList.begin(), - BindActionInfoList.end(), - Comparator); - std::sort(LazyBindActionInfoList.begin(), - LazyBindActionInfoList.end(), - Comparator); - std::sort(WeakBindActionInfoList.begin(), - WeakBindActionInfoList.end(), - Comparator); + std::ranges::sort(BindActionInfoList, Comparator); + std::ranges::sort(LazyBindActionInfoList, Comparator); + std::ranges::sort(WeakBindActionInfoList, Comparator); } const auto OutFile = this->OutFile; diff --git a/src/Operations/PrintCStringSection.cpp b/src/Operations/PrintCStringSection.cpp index 3869d38..bd99272 100644 --- a/src/Operations/PrintCStringSection.cpp +++ b/src/Operations/PrintCStringSection.cpp @@ -88,6 +88,16 @@ namespace Operations { uint64_t FileOffset; uint64_t Address; + + [[nodiscard]] + constexpr auto operator==(const CStringInfo &Other) const noexcept { + return this->String == Other.String; + } + + [[nodiscard]] + constexpr auto operator<=>(const CStringInfo &Other) const noexcept { + return this->String <=> Other.String; + } }; static auto @@ -290,11 +300,7 @@ namespace Operations { } if (Opt.Sort) { - std::sort(CStringInfoList.begin(), - CStringInfoList.end(), - [](const auto &Left, const auto &Right) noexcept { - return Left.String < Right.String; - }); + std::ranges::sort(CStringInfoList); } const auto OutFile = this->OutFile; diff --git a/src/Operations/PrintExportTrie.cpp b/src/Operations/PrintExportTrie.cpp index 3cc40db..c58a030 100644 --- a/src/Operations/PrintExportTrie.cpp +++ b/src/Operations/PrintExportTrie.cpp @@ -97,6 +97,16 @@ namespace Operations { std::string_view SectionName; std::string String; + + [[nodiscard]] + constexpr auto operator==(const ExportInfo &Other) const noexcept { + return this->String == Other.String; + } + + [[nodiscard]] + constexpr auto operator<=>(const ExportInfo &Other) const noexcept { + return this->String <=> Other.String; + } }; [[nodiscard]] static auto @@ -499,13 +509,7 @@ namespace Operations { } if (Opt.Sort) { - const auto Comparator = - [](const ExportInfo &Lhs, const ExportInfo &Rhs) noexcept - { - return Lhs.String < Rhs.String; - }; - - std::sort(ExportList.begin(), ExportList.end(), Comparator); + std::ranges::sort(ExportList); } auto Counter = static_cast(1); diff --git a/src/Operations/PrintHeader.cpp b/src/Operations/PrintHeader.cpp index 3657a16..850a680 100644 --- a/src/Operations/PrintHeader.cpp +++ b/src/Operations/PrintHeader.cpp @@ -89,12 +89,12 @@ namespace Operations { return Verbose ? Mach::CpuKindGetDesc(CpuKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()) : Mach::CpuKindGetString(CpuKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()); @@ -112,12 +112,12 @@ namespace Operations { return Verbose ? Mach::CpuKindAndSubKindGetString(CpuKind, SubKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()) : Mach::CpuKindAndSubKindGetDesc(CpuKind, SubKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()); @@ -140,7 +140,7 @@ namespace Operations { const auto CpuKindString = StringForCpuKind(CpuKind, Opt.Verbose); const auto SubKindString = StringForSubKind(CpuKind, SubKind, Opt.Verbose); - + const auto FallBack = [FileKind]() noexcept { return std::format("", static_cast(FileKind)); @@ -149,12 +149,12 @@ namespace Operations { const auto FileKindString = Opt.Verbose ? MachO::FileKindGetString(FileKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()) : MachO::FileKindGetDesc(FileKind) - .and_then([](const auto V) noexcept { + .and_then([](const auto &&V) noexcept { return std::optional(V); }) .value_or(FallBack()); diff --git a/src/Operations/PrintId.cpp b/src/Operations/PrintId.cpp index 27c615f..b05cbdb 100644 --- a/src/Operations/PrintId.cpp +++ b/src/Operations/PrintId.cpp @@ -50,16 +50,17 @@ namespace Operations { { using Kind = MachO::LoadCommandKind; if (const auto ID = Iter.dyn_cast()) { - auto Name = std::string_view(""); - if (const auto NameOpt = ID->name(IsBigEndian)) { - Name = NameOpt.value(); - } else { - if (!Opt.Verbose) { + const auto NameOpt = ID->name(IsBigEndian); + if (!Opt.Verbose) { + if (!NameOpt.has_value()) { return RunResult(RunResult::Error::BadIdString); } } - std::println(OutFile, "\"{}\"", Name); + std::println(OutFile, + "\"{}\"", + NameOpt.value_or("")); + if (Opt.Verbose) { const auto &Dylib = ID->Dylib; const auto CurrentVersion = diff --git a/src/Operations/PrintImageList.cpp b/src/Operations/PrintImageList.cpp index 28f74d9..b15b137 100644 --- a/src/Operations/PrintImageList.cpp +++ b/src/Operations/PrintImageList.cpp @@ -103,17 +103,14 @@ namespace Operations { ImageInfoList.reserve(ImageCount); for (const auto &Info : Dsc.imageInfoList()) { auto NewInfo = ImageInfo(); - if (const auto PathOpt = Map.string(Info.PathFileOffset)) { - NewInfo.Path = PathOpt.value(); - } else { - NewInfo.Path = ""; - } NewInfo.Address = Info.Address; NewInfo.ModTime = Info.ModTime; NewInfo.Inode = Info.Inode; NewInfo.PathFileOffset = Info.PathFileOffset; NewInfo.Pad = Info.Pad; + NewInfo.Path = + Map.string(Info.PathFileOffset).value_or(""); LongestImagePath.set(NewInfo.Path.length()); ImageInfoList.emplace_back(std::move(NewInfo)); @@ -133,7 +130,7 @@ namespace Operations { return false; }; - std::sort(ImageInfoList.begin(), ImageInfoList.end(), Comparator); + std::ranges::sort(ImageInfoList, Comparator); } PrintImageCount(OutFile, ImageCount, /*PrintColon=*/true); @@ -143,11 +140,10 @@ namespace Operations { auto Counter = static_cast(1); for (const auto &Info : ImageInfoList) { std::print(OutFile, - "Image {:>{}}: ", + "Image {:>{}}: \"{}\"", Counter, - ImageInfoListSizeDigitCount); - - std::print(OutFile, "\"{}\"", Info.Path); + ImageInfoListSizeDigitCount, + Info.Path); const auto WrittenOut = STR_LENGTH("\"\"") + Info.Path.length(); if (Opt.Verbose) { diff --git a/src/Operations/PrintLibraries.cpp b/src/Operations/PrintLibraries.cpp index a984cf4..a9d71f4 100644 --- a/src/Operations/PrintLibraries.cpp +++ b/src/Operations/PrintLibraries.cpp @@ -106,7 +106,9 @@ namespace Operations { const auto &Opt = this->Opt; if (!Opt.SortKindList.empty()) { - const auto Lambda = [&](const auto &Lhs, const auto &Rhs) noexcept { + const auto Comparator = + [&](const auto &Lhs, const auto &Rhs) noexcept + { auto Compare = std::strong_ordering::equivalent; for (const auto &Sort : Opt.SortKindList) { Compare = CompareEntriesBySortKind(Lhs, Rhs, Sort); @@ -118,7 +120,7 @@ namespace Operations { return Compare == std::strong_ordering::less; }; - std::sort(DylibList.begin(), DylibList.end(), Lambda); + std::ranges::sort(DylibList, Comparator); } const auto OutFile = this->OutFile; @@ -142,8 +144,7 @@ namespace Operations { Counter, DylibInfo.Index, NcmdsDigitCount, - MachO::LoadCommandKindGetString(DylibInfo.Kind) - .value_or(""), + MachO::LoadCommandKindGetString(DylibInfo.Kind).value(), LongestLCDylibKindLength, DylibInfo.Name, DylibInfo.CurrentVersion, diff --git a/src/Operations/PrintObjcClassList.cpp b/src/Operations/PrintObjcClassList.cpp index 4383161..5edd35d 100644 --- a/src/Operations/PrintObjcClassList.cpp +++ b/src/Operations/PrintObjcClassList.cpp @@ -172,7 +172,7 @@ namespace Operations { static void PrintCategoryList( FILE *const OutFile, - const std::vector &CategoryList, + const std::vector CategoryList, const bool Is64Bit) noexcept { switch (CategoryList.size()) { @@ -327,12 +327,12 @@ namespace Operations { } else { auto ObjcClassList = ObjcClassCollection.getAsList(); if (!Options.SortKindList.empty()) { - std::sort(ObjcClassList.begin(), - ObjcClassList.end(), - [&](const auto &Lhs, const auto &Rhs) noexcept - { - return CompareObjcClasses(*Lhs, *Rhs, Options); - }); + std::ranges::sort(ObjcClassList, + [&](const auto Lhs, const auto Rhs) noexcept { + return CompareObjcClasses(*Lhs, + *Rhs, + Options); + }); } const auto ObjcClassListSize = ObjcClassList.size(); @@ -381,8 +381,7 @@ namespace Operations { std::println(OutFile, ""); if (Options.PrintCategories) { - const auto &CategoryList = Node->categoryList(); - PrintCategoryList(OutFile, CategoryList, Is64Bit); + PrintCategoryList(OutFile, Node->categoryList(), Is64Bit); } I++; diff --git a/src/Operations/PrintProgramTrie.cpp b/src/Operations/PrintProgramTrie.cpp index 1ad0d9d..5b19e59 100644 --- a/src/Operations/PrintProgramTrie.cpp +++ b/src/Operations/PrintProgramTrie.cpp @@ -152,6 +152,16 @@ namespace Operations { struct SExportInfo { std::string String; uint32_t Index; + + [[nodiscard]] + constexpr auto operator==(const SExportInfo &Rhs) const noexcept { + return this->String == Rhs.String; + } + + [[nodiscard]] + constexpr auto operator<=>(const SExportInfo &Rhs) const noexcept { + return this->String <=> Rhs.String; + } }; static auto @@ -199,13 +209,7 @@ namespace Operations { } if (Opt.Sort) { - const auto Comparator = - [](const SExportInfo &Lhs, const SExportInfo &Rhs) noexcept - { - return Lhs.String < Rhs.String; - }; - - std::sort(ExportList.begin(), ExportList.end(), Comparator); + std::ranges::sort(ExportList); } auto Counter = static_cast(1); diff --git a/src/Operations/PrintRebaseOpcodeList.cpp b/src/Operations/PrintRebaseOpcodeList.cpp index 24a12af..642dec1 100644 --- a/src/Operations/PrintRebaseOpcodeList.cpp +++ b/src/Operations/PrintRebaseOpcodeList.cpp @@ -213,7 +213,7 @@ namespace Operations { static auto PrintRebaseOpcodeCollection( FILE *const OutFile, - const std::vector &Collection, + const std::span Collection, const bool Is64Bit, const PrintRebaseOpcodeList::Options &Opt) noexcept { @@ -445,11 +445,7 @@ namespace Operations { const auto OutFile = this->OutFile; const auto &Opt = this->Opt; - PrintRebaseOpcodeCollection(OutFile, - RebaseOpcodeList, - Is64Bit, - Opt); - + PrintRebaseOpcodeCollection(OutFile, RebaseOpcodeList, Is64Bit, Opt); return RunResult(RunResult::Error::None); } diff --git a/src/Operations/PrintSymbolPtrSection.cpp b/src/Operations/PrintSymbolPtrSection.cpp index c681498..dd738d4 100644 --- a/src/Operations/PrintSymbolPtrSection.cpp +++ b/src/Operations/PrintSymbolPtrSection.cpp @@ -91,9 +91,9 @@ namespace Operations { }; static auto - GetSectionAtOrdinal(const std::vector &SegmentList, + GetSectionAtOrdinal(const std::span SegmentList, const uint32_t Ordinal, - std::string_view &SegmentName) noexcept + std::string_view SegmentName) noexcept -> std::optional { assert(Ordinal != 0); @@ -115,8 +115,8 @@ namespace Operations { struct SymbolInfo { std::string_view String; - uint64_t Index; + MachO::SymTabCommand::Entry::Kind Kind; uint8_t Section; uint8_t DylibOrdinal; @@ -129,7 +129,7 @@ namespace Operations { auto IterateIndices(const MachO::SymTabCommand &SymTab, const MachO::DynamicSymTabCommand &DynamicSymTab, - const ADT::MemoryMap &Map, + const ADT::MemoryMap Map, const uint32_t Reserved1, const bool SkipInvalidIndices, const uint64_t Limit, @@ -243,7 +243,7 @@ namespace Operations { CompareEntriesBySortKind( const SymbolInfo &Lhs, const SymbolInfo &Rhs, - const std::vector &DylibInfoList, + const std::span DylibInfoList, const PrintSymbolPtrSection::Options::SortKind SortKind) noexcept { switch (SortKind) { @@ -264,8 +264,8 @@ namespace Operations { return std::strong_ordering::less; } - LhsDylibPath = DylibInfoList.at(Lhs.DylibOrdinal - 1).Path; - RhsDylibPath = DylibInfoList.at(Rhs.DylibOrdinal - 1).Path; + LhsDylibPath = DylibInfoList[Lhs.DylibOrdinal - 1].Path; + RhsDylibPath = DylibInfoList[Rhs.DylibOrdinal - 1].Path; return LhsDylibPath <=> RhsDylibPath; } @@ -536,7 +536,7 @@ namespace Operations { return Compare == std::strong_ordering::less; }; - std::sort(SymbolInfoList.begin(), SymbolInfoList.end(), Lambda); + std::ranges::sort(SymbolInfoList, Lambda); } const auto MaxIndexDigitCount =