Skip to content

Instantly share code, notes, and snippets.

@EthanArbuckle
Created January 26, 2025 02:14
Show Gist options
  • Save EthanArbuckle/3a183d14fea7974c333cb5296ae6e3e7 to your computer and use it in GitHub Desktop.
Save EthanArbuckle/3a183d14fea7974c333cb5296ae6e3e7 to your computer and use it in GitHub Desktop.
get size of a loaded image
uint64_t get_image_size(struct mach_header_64 *mh) {
struct load_command *cmd = (struct load_command *)((uint8_t *)mh + sizeof(struct mach_header_64));
uint64_t max_addr = 0;
for (uint32_t i = 0; i < mh->ncmds; i++) {
if (cmd->cmd == LC_SEGMENT_64) {
struct segment_command_64 *seg = (struct segment_command_64 *)cmd;
uint64_t seg_end = seg->vmaddr + seg->vmsize;
if (seg_end > max_addr) {
max_addr = seg_end;
}
}
cmd = (struct load_command *)((uint8_t *)cmd + cmd->cmdsize);
}
return max_addr - ((struct segment_command_64 *)((uint8_t *)mh + sizeof(struct mach_header_64)))->vmaddr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment