Skip to content

Instantly share code, notes, and snippets.

@YukiSakamoto
Last active February 19, 2020 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YukiSakamoto/a7a863fd0633040edb375087acfb1f4a to your computer and use it in GitHub Desktop.
Save YukiSakamoto/a7a863fd0633040edb375087acfb1f4a to your computer and use it in GitHub Desktop.
Get binary type on Mac.
#include <mach-o/loader.h>
#include <cstdio>
void print_filetype(const uint32_t filetype)
{
const char *msg;
switch(filetype) {
case MH_OBJECT:
msg = "MH_OBJECT";
break;
case MH_EXECUTE:
msg = "MH_EXECUTE";
break;
case MH_FVMLIB:
msg = "MF_FVMLIB";
break;
case MH_CORE:
msg = "MH_CORE";
break;
case MH_PRELOAD:
msg = "MH_PRELOAD";
break;
case MH_DYLIB:
msg = "MH_DYLIB";
break;
case MH_DYLINKER:
msg = "MH_DYLINKER";
break;
case MH_BUNDLE:
msg = "MH_BUNDLE";
break;
case MH_DYLIB_STUB:
msg = "MH_DYLIB_STUB";
break;
default:
msg = "Unknown";
break;
};
std::printf("%s", msg);
}
int main(void)
{
const char *filename = "./a.out";
struct mach_header_64 mh64;
std::FILE *fp = std::fopen(filename, "r");
std::fread(&mh64, sizeof(mach_header_64), 1, fp);
if (mh64.magic == MH_MAGIC_64) {
std::printf("MH_MAGIC_64\n");
} else if (mh64.magic == MH_CIGAM_64) {
std::printf("MH_CIGAM_64\n");
} else {
std::printf("Unknown\n");
}
print_filetype(mh64.filetype);
std::fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment