Search code examples
c++macosmemorymach

How to get a base address of a module in C++ on Os X


I've been experimenting with reading/writing to memory of another process in C++ on Os X.

The issue I've been having is that I get a pointer (e.g. server.dylib+0x123AB) but I can't seem to find a way to get the memory address/base address of server.dylib dynamically in c++. Are there any methods that would be recommended to try to find it. It's probably my skill level but I've found that trying to tinker with memory on OsX has been an uphill struggle (there's very little documentation around).

Any advice would be appreciated.


Solution

  • Merlin's answer is somewhat inaccurate. ASLR is not meant to prevent you from getting addresses in runtime - it's meant to prevent you from relying on FIXED addresses (i.e. when code injection). If you can already execute code, you can definitely get addresses (heck, GDB does, why can't you?)

    DYLD exposes a very rich API (, and dyld_images.h) which enables you to easily get a list of all the images loaded into a process address space either from within the process or from outside of it. You can also get the "slide" , which is the ASLR offset used. This is, however, assuming you're already running code on that machine - i.e. it won't work when injecting code.