Skip to content

Instantly share code, notes, and snippets.

@cedriczirtacic
Last active October 15, 2017 15:14
Show Gist options
  • Select an option

  • Save cedriczirtacic/be2c0c17d670142f204fd7017ef3319a to your computer and use it in GitHub Desktop.

Select an option

Save cedriczirtacic/be2c0c17d670142f204fd7017ef3319a to your computer and use it in GitHub Desktop.
quick & dirty read Macho-O binary headers
// like: otool -h <binary>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <mach-o/loader.h>
int main(int argc, char *argv[]) {
struct mach_header_64 *hdr;
unsigned int hdr_size = sizeof( struct mach_header_64 );
char *object;
if (argc < 2)
return 1;
object = argv[1];
int fd = open(object, O_RDONLY);
hdr = (struct mach_header_64 *)calloc(1, hdr_size);
if ((read(fd, (void *)hdr, hdr_size)) > 0) {
printf(
"magic: 0x%08x %s\n"
"cpu type: %u\n"
"cpu subtype: %d\n"
"filetype: %x\n"
"number of commands: %u\n"
"size of commands: %u\n"
"flags: 0x%x\n",
hdr->magic, ((hdr->magic^0xfeedfa00) == 0xcf ? "(64-bit)" : "(32-bit)"),
(cpu_type_t)hdr->cputype,
(cpu_subtype_t)hdr->cpusubtype & 0x000000ff,
hdr->filetype,
hdr->ncmds,
hdr->sizeofcmds,
hdr->flags
);
} else {
perror("error");
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment