From 68f51a77082c5dadece7d0e6ea59f7c783ab6c2c Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sat, 28 Sep 2024 04:52:34 +0200 Subject: [PATCH] Improve `virtual_size` computation --- include/LIEF/MachO/Binary.hpp | 13 ++++++++++++- src/MachO/Binary.cpp | 11 ----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/LIEF/MachO/Binary.hpp b/include/LIEF/MachO/Binary.hpp index d17fb835aa..7cdacdacd1 100644 --- a/include/LIEF/MachO/Binary.hpp +++ b/include/LIEF/MachO/Binary.hpp @@ -28,6 +28,7 @@ #include "LIEF/MachO/Stub.hpp" #include "LIEF/visibility.h" +#include "LIEF/utils.hpp" #include "LIEF/Abstract/Binary.hpp" @@ -92,6 +93,14 @@ class LIEF_API Binary : public LIEF::Binary { struct range_t { uint64_t start = 0; uint64_t end = 0; + + uint64_t size() const { + return end - start; + } + + bool empty() const { + return start == end; + } }; //! Internal container for storing Mach-O LoadCommand @@ -388,7 +397,9 @@ class LIEF_API Binary : public LIEF::Binary { uint64_t imagebase() const override; //! Size of the binary in memory when mapped by the loader (``dyld``) - uint64_t virtual_size() const; + uint64_t virtual_size() const { + return align(va_ranges().size(), (uint64_t)page_size()); + } //! Return the binary's loader (e.g. ``/usr/lib/dyld``) or an //! empty string if the binary does not use a loader/linker diff --git a/src/MachO/Binary.cpp b/src/MachO/Binary.cpp index bf95281b2a..b28fecec19 100644 --- a/src/MachO/Binary.cpp +++ b/src/MachO/Binary.cpp @@ -591,7 +591,6 @@ uint32_t Binary::page_size() const { return get_pagesize(*this); } - void Binary::sort_segments() { commands_t::iterator start = commands_.end(); commands_t::iterator end = commands_.end(); @@ -1831,16 +1830,6 @@ SegmentCommand* Binary::get_segment(const std::string& name) { return const_cast(static_cast(this)->get_segment(name)); } -uint64_t Binary::virtual_size() const { - uint64_t virtual_size = 0; - for (const SegmentCommand* segment : segments_) { - virtual_size = std::max(virtual_size, segment->virtual_address() + segment->virtual_size()); - } - virtual_size -= imagebase(); - virtual_size = align(virtual_size, static_cast(page_size())); - return virtual_size; -} - uint64_t Binary::imagebase() const { if (const SegmentCommand* _TEXT = get_segment("__TEXT")) { return _TEXT->virtual_address();