Skip to content

Commit

Permalink
Improve virtual_size computation
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Sep 28, 2024
1 parent a526c52 commit 68f51a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
13 changes: 12 additions & 1 deletion include/LIEF/MachO/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "LIEF/MachO/Stub.hpp"

#include "LIEF/visibility.h"
#include "LIEF/utils.hpp"

#include "LIEF/Abstract/Binary.hpp"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions src/MachO/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1831,16 +1830,6 @@ SegmentCommand* Binary::get_segment(const std::string& name) {
return const_cast<SegmentCommand*>(static_cast<const Binary*>(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<uint64_t>(page_size()));
return virtual_size;
}

uint64_t Binary::imagebase() const {
if (const SegmentCommand* _TEXT = get_segment("__TEXT")) {
return _TEXT->virtual_address();
Expand Down

0 comments on commit 68f51a7

Please sign in to comment.