Search code examples
clinuxbinarymemory-addressperf

Could I load elf and get virtual address from /proc/pid/maps before actually running it?


Take a look at how perf works, user can use command like "perf record -e cpu-clock ./binary" to run the binary and get information. Does perf load the binary, make some config and then running it? If it is true, could I get virtual address of some code by looking at the /proc/pid/maps? I am a rookie in binary execution and hope for your advice!


Solution

  • Could I load elf and get virtual address from /proc/pid/maps before actually running it?

    No. The address space is built as the program runs. "Memory mapping" is a process (of software being run and requesting memory mappings), not a property of the binary file. This becomes doubly clear now that basically all mainstream operating systems use address space layout randomization (ASLR)!

    Often, a lot of that is done before your C main() even runs - by the loader (ld.so on Linux), but that's still executing code from your binary.

    What your ELF file does contain are sections which can be loaded at fixed addresses, but as said, ASLR makes that rare. Much of the code compiled these days is relocateable, so all it needs is to know is relative addresses.

    Your question regarding perf record is unrelated, but what perf does is set up some kernel-space observables (using, mostly, eBPF) and then quite regularly executing the binary, while probing said observables.