Inspect macOS / iOS binary type

TLDR: Use otool -hv to inspect the type of macOS / iOS binary.

On Darwin based OS, like macOS / iOS, all binaries use a file format called Mach-O, including object file, executable, static library and shared library.

Normally, we can check a file’s type by its filename extension: .o for object file, .a for static libs, .so and .dylib for shared libs. However, this info is not super reliable since we can easily change a file’s extension. And sometimes we may want to inspect the file type ourselves. One of the common scenarios is to check whether a given library is static lib or shared lib.

To inspect the type of a given macOS / iOS binary, you may use the otool command via otool -hv. For example:

Dynamic Lib:

➜ otool -hv libmylib.dylib
Mach header
      magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
MH_MAGIC_64  X86_64        ALL 0x00    DYLIB    16       1928 NOUNDEFS DYLDLINK TWOLEVEL WEAK_DEFINES BINDS_TO_WEAK NO_REEXPORTED_DYLIBS APP_EXTENSION_SAFE

Static Lib:

➜ otool -hv libmylib.a
Archive : /path/to/libmylib.a
Mach header
      magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
MH_MAGIC_64  X86_64        ALL 0x00   OBJECT    16       1928 SUBSECTIONS_VIA_SYMBOLS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s