i am currently trying to write a tool based on lld. Specifically I want
to parse a ld command line to determine the input and output files and
the kind of each input file given a ld command line.
Unfortunately, the ELF linker does not expose any API to just parse the
My question: if I would refactor this module to expose such an API,
would this have any chance of being merged back into the official
Could you explain a little bit more about what you are trying to achieve? Most of the input files given to a linker is obvious – if a command line argument does not start with dash and if is not an argument for an option, it is a filename. So I’m guessing you want to resolve library file names given by -l. Am I correct?
what I am trying to achieve is actually getting all input files that are linked into the result, yes.
The first thing I currently try to achieve is to determine the object files that are being linked. My problem there exactly is to decide if an argument without dash belongs to an option or if it is an input file.
To give some context:
I’m currently trying to write a build analysis tool that works independently of build scripts/systems by tracking calls performed during the build.
Therefore, for a C++ project, I get a list of gcc/clang/ld calls and their command lines. From there I try to build a graph of which source files are built into which object files, libraries, …
If you want to analyze file dependencies, analyzing command line arguments are perhaps not enough due to archive files (“.a” files). The linker pulls up only needed files from archive files, so the dependency to static archive files is somewhat partial.
Have you ever tried “-M” linker option? That option lets the linker to print out what files and symbols are included in the output file. If you don’t need an actual executable, you could redirect it to /dev/null by passing
-o /dev/null to the linker. That’s might be better choice than analyzing program dependencies yourself.
The other option is to just pass --verbose to the linker to print out a filename as the linker opens a file.
verbose with -o /dev/null seems like a good option for what I’m currently trying to achieve. Thank you!