70

Is there a tool for reading Mac OS X binaries that would print information about relocation tables and symbol offsets similar to this readelf output?

readelf -r app

Relocation section '.rel.dyn' at offset 0x5ec contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049d58  00001706 R_386_GLOB_DAT    00000000   __gmon_start__
08049d60  00000305 R_386_COPY        08049d60   _ZSt4cout

Relocation section '.rel.plt' at offset 0x5fc contains 13 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049d24  00000107 R_386_JUMP_SLOT   0804868c   print
08049d28  00000207 R_386_JUMP_SLOT   0804869c   _ZNSt8ios_base4InitC1E
08049d2c  00000507 R_386_JUMP_SLOT   080486ac   _ZStlsISt11char_traits
08049d30  00000607 R_386_JUMP_SLOT   080486bc   _ZNSolsEPFRSoS_E
08049d34  00000707 R_386_JUMP_SLOT   08048664   _init
08049d38  00000807 R_386_JUMP_SLOT   080486dc   sleep
08049d3c  00000907 R_386_JUMP_SLOT   080486ec   _ZNKSsixEj
08049d40  00000b07 R_386_JUMP_SLOT   080486fc   _ZNKSs4sizeEv
08049d44  00000c07 R_386_JUMP_SLOT   0804870c   __libc_start_main
08049d48  00000d07 R_386_JUMP_SLOT   08048ae4   _fini
08049d4c  00001307 R_386_JUMP_SLOT   0804872c   _ZSt4endlIcSt11char_tr
08049d50  00001507 R_386_JUMP_SLOT   0804873c   __gxx_personality_v0
08049d54  00001607 R_386_JUMP_SLOT   0804874c   _ZNSt8ios_base4InitD1E

In this example, print is a function loaded from a shared library (I wrote) at runtime. readelf is able to display information about its location inside the app binary.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
karlphillip
  • 92,053
  • 36
  • 243
  • 426

5 Answers5

105

Using Macports:

  1. Install macports.
  2. Using macports port install binutils
  3. gobjdump -p /path/to/app

Using Homebrew:

  1. Install Homebrew
  2. Do brew update && brew install binutils
  3. /usr/local/path/to/gobjdump -p /path/to/app # or follow instructions to add it to your PATH

There are, of course, many other command-line options. Note that the binutils port cautions that installing it may cause other ports to fail to build. Having it around to port install/look at something/port uninstall can still be handy.

It's rather amazing that there isn't already some native tool to do this.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
cbehanna
  • 1,456
  • 1
  • 11
  • 9
  • 28
    If you use Homebrew, then you can install this via "brew install binutils". – louielouie May 23 '13 at 21:24
  • 29
    For Homebrew users, as @louielouie mentioned, `brew install binutils` then all tools will be prefixed with a `g`, ie `greadelf`. See https://github.com/Homebrew/homebrew/blob/master/Library/Formula/binutils.rb – acw Sep 09 '14 at 09:49
  • I get a message: `gobjdump: Matching formats: elf32-littlearm elf32-littlearm-symbian elf32-littlearm-vxworks` – IgorGanapolsky May 11 '16 at 22:01
  • 1
    @IgorGanapolsky you can use greadelf -Ws /path/to/your/so/files – ck1910 Apr 12 '17 at 07:55
23

otool -l test.o

ergosys
  • 47,835
  • 5
  • 49
  • 70
13
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install binutils
greadelf filepath
  1. install homebrew.
  2. use homebrew to install binutils
  3. the readelf in binutils is called greadelf.
Jichao
  • 40,341
  • 47
  • 125
  • 198
7

You might try dwarfdump.

⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣

l'L'l
  • 44,951
  • 10
  • 95
  • 146
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • +1 thanks nice tool to know about for those dealing with dwarf debug info – Bogatyr Mar 01 '12 at 07:29
  • 3
    +1 for available in the Apple toolchain. It is available at `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/dwarfdump` after Xcode is installed. – Siu Ching Pong -Asuka Kenji- Mar 21 '16 at 15:03
  • 1
    Though you generally don't need the full path: You can just use `xcrun dwarfdump`. And you may have `/usr/bin/dwarfdump` as a shortcut to that, which means you can just type `dwarfdump` and it will Just Work. – Peter Hosey Jun 10 '16 at 05:57
5

Have you tried ObjConv? http://agner.org/optimize/#objconv

This utility can be used for converting object files between COFF/PE, OMF, ELF and Mach-O (used by Mac OS) formats for all 32-bit and 64-bit x86 platforms. Can modify symbol names in object files. Can build, modify and convert function libraries across platforms. Can dump object files and executable files. Also includes a very good disassembler supporting the SSE4, AVX, FMA and XOP instruction sets.

Ps.: The source code is included (GPL).

carlfilips
  • 2,574
  • 3
  • 21
  • 27