1

I have a statically linked application binary that links against multiple user libraries and the pthread library. The application only uses a limited set of functions from each of these libraries. From a previous post Size of a library and the executable and from my experiments I realize that the linker only includes functions(in the executable) that are used/needed and not the entire contents of library.

I want to find out which functions from each of the respective libraries are linked to the executable and their addresses (VMA). Ultimately I want to compile a list that contains the start and the end Virtual Memory Addresses (VMAs) for the each of the libraries based on the functions (in the library) that are mapped to the text segment.

One approach to do this is to make a list of functions in the library and then look for each of these functions in the executable and the corresponding Virtual Memory address it is mapped to. But this seems rather tedious to me. Is there a simpler way to accomplish this? Thanks.

Community
  • 1
  • 1
madman
  • 15
  • 5
  • Why not just use "libelf" to find out which functions the executable contains? –  Feb 28 '13 at 22:35
  • @H2CO3 Since I have no prior experience with using libelf it would be great if there was a workaround for this. I could use nm to extract the VMAs for the function and the names. The problem however is that they dont seem to provide any info on the parent library of the function. However, On you mention I had a brief look at the libelf documentation. On the first glance, I could not make out if it is possible to extract information about the parent library of a function from the executable. Is it possible to have this information? Thanks. – madman Feb 28 '13 at 23:15
  • No, it isn't, linkage strips that information off. –  Feb 28 '13 at 23:19

1 Answers1

0

I want to find out which functions from each of the respective libraries are linked to the executable and their addresses (VMA).

Add -Wl,-Map=foo.map argument to your link line. The resulting foo.map file will tell you all of the above.

Ultimately I want to compile a list that contains the start and the end Virtual Memory Addresses (VMAs) for the each of the libraries

That assumes that the linker does not re-order functions (and thus all functions from a single library occupy continuous range of text addresses). This assumption is probably true in simple cases, but in no way is guaranteed. See e.g. this patch.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • I used nm and sorted the output by address to get a list of functions used in the executable. After sorting I realized that the linker was not re-ordering functions across libraries. Thanks for your answer and especially for the link on re-ordering functions. – madman Mar 04 '13 at 14:15