Created
January 26, 2025 02:14
-
-
Save EthanArbuckle/3a183d14fea7974c333cb5296ae6e3e7 to your computer and use it in GitHub Desktop.
get size of a loaded image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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