0

Assume we have a C application that uses a large library. I am trying to find a way to know at what memory address library functions and symbols are located. Or say exactly to compiler/linker where to put them in memory. What's the approach?

Edit: This is an embedded application so I can't use desktop compiler toolchains.

doubleE
  • 1,027
  • 1
  • 12
  • 32
  • 1
    you can run some tools over the executable to get offsets, etc. You can, in the executing code, say something like: int (*pprinf)(const char*) = printf; the the pprinf variable would contain the address of the printf function. – user3629249 Feb 06 '15 at 02:39
  • 1
    Knowing the address of a function at run-time and dictating during your build where it will be are pretty different. I get the sense you want this for some functional reason and things like gdb mentioned below won't meet your needs, but it's hard to tell. Can you give more detail about what you need? – 1203_dube Feb 06 '15 at 02:48
  • 1
    You're going to need to specify the embedded platform, and the tool chain you're using to build for it. What you're trying to do depends on the platform, tool chain, executable file format on the platform, and a bunch of other things. That said, there is rarely benefit to controlling where library functions go in memory (the code is the same size, and probably consumes the same resources) except on machines with partitioned memory hierarchies. – Rob Feb 06 '15 at 08:05
  • Thanks for your comments. Say I have library image downloaded to flash. What I want to achieve is have memory address's of all the library functions and symbols that application is going to use. My toolchain is IAR8051 and platform is 8051 based SoC. Image that is going to be downloaded is binary of course. – doubleE Feb 06 '15 at 14:32

2 Answers2

3

Function names, like arrays, decay into pointers when used. That means you can just:

printf("%p", myFunction);

On most systems, anyway. To be strictly standard-compliant, check out How to format a function pointer?

Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Thanks Carl. Please note this is an embedded application and I don't have PC compiler/linker toolchain. I want to know/control where the library functions are going to sit in memory. – doubleE Feb 05 '15 at 22:14
  • 1
    Oh, like everything is statically linked? Just have your linker output a map file. Check your documentation for that. – Carl Norum Feb 05 '15 at 22:33
3

There are a few ways to get at this. The easiest is probably to use a debugger.

Using GDB

With gdb you can connect to a running program. Say for example I want to connect to a running vim process. First I need to know it's PID number:

$ pidof vim
15425

I can now connect to the process using gdb:

$ gdb `which vim` 15425

At the prompt, I can now enquire about different symbols:

$ info symbol fprintf
fprintf in section .text of /lib/x86_64-linux-gnu/libc.so.6
$ info address fprintf
Symbol "fprintf" is at 0x7fc9b44314a0 in a file compiled without debugging.

Using /proc

Another way to get a dump of memory locations of libraries from /proc.

Again, you need the PID (see above) and you can dump out the libraries and their locations in the virtual memory of a process.

$ cat /proc/15425/maps
7fc9a9427000-7fc9a9432000 r-xp 00000000 fd:02 539295973                  /lib/x86_64-linux-gnu/libnss_files-2.19.so
7fc9a9432000-7fc9a9631000 ---p 0000b000 fd:02 539295973                  /lib/x86_64-linux-gnu/libnss_files-2.19.so
7fc9a9631000-7fc9a9632000 r--p 0000a000 fd:02 539295973                  /lib/x86_64-linux-gnu/libnss_files-2.19.so
7fc9a9632000-7fc9a9633000 rw-p 0000b000 fd:02 539295973                  /lib/x86_64-linux-gnu/libnss_files-2.19.so

Each library will have multiple sections and this will depend on how it was compiled/linked.

bartman
  • 164
  • 3
  • Thanks. I edited question to be more clear. This is an embedded application and I can't use this method. – doubleE Feb 05 '15 at 22:09
  • sure you can use GDB. one instance on the embedded device, as a server and one instance locally as a client. (have I got that backwards, I rarely use it this way) Here is a wiki reference to how to do it. – user3629249 Feb 06 '15 at 02:37