简体   繁体   中英

Given a fully linked a.out, which defines symbol foo, how can I tell which object file that definition came from, without relinking the a.out?

I'm trying to parse an ELF file and create a list of symbols defined in each object file. I am able to find everything I need except for the link between symbols and object files.

I couldn't find anything like that in the ELF specifications. In this particular file I'm parsing I have some embedded DWARF debug info I could use, but ideally I would like to find a link between symbols and objects that is standard as I want to apply this for many non GCC compilers.

I'm trying to parse an ELF file and create a list of symbols defined in each object file.

Are you processing individual ELF object files, or a fully linked executable or shared library? Since the only way for your question to make sense is the latter, let's assume that your actual question is:

Given a fully linked a.out , which defines symbol foo , how can I tell which object file that definition came from, without relinking the a.out ? .

In general, you can't.

First, not every symbol defined in a.out may even come from an object file: some may be defined via a linker script or a --defsym command line argument.

Second, weak symbols could be defined in multiple object files, and the linker is free to choose any one of them.

Last, there is absolutely no record of object file -> symbol association in the a.out . In fact, you can't even extract the list of .o files that were linked in (without redoing the link and asking the linker to print them).

You may be able to reestablish this association by looking at the debug info, which will tell you what translation unit the symbol came from, and then guess that probably foo.c was compiled into foo.o , but this again may fail as foo.c may have been compiled into bar.o and baz.o (with different -DFOO defines).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM