0

I have a bunch of .a files whose generation process is not controlled by me, nor are their sources. When I use them for linking, I want to know their dependencies (libA.a depends on libB.a if there is some symbol undefined in libA.a but defined in libB.a), so that I can put them in the correct order in the ld/gcc command line.

I don't want to do over linking (i.e. specify those libraries twice), because I want to persist those dependencies into BUILD file of bazel, so I want to know the precise dependency.

I wonder if there is some command line tool, given libA.a and libB.a, can tell whether libA.a depends on libB.a? If there is not such, how do I write such a script?

Note: my definition for dependency may not be 100% accurate. Let me know if there are other types of dependency other than defined/undefined symbols.

The simplest way is to process the output of nm libA.a and nm libB.a and look for U symbols, but there are many types of symbols listed in man nm, each of them have different semantic, so I am concerned I might miss some if I use such simplified approach.

Kan Li
  • 8,557
  • 8
  • 53
  • 93
  • static libraries don't depend on any other lib in definitive. – Ipor Sircer Sep 21 '16 at 01:13
  • @IporSircer, they don't have dependency encoded into the file format like `.so` files, but they do have dependencies. If `libA.a` depends on `libB.a`, then in `ld` command line `libA.a` should be placed before `libB.a`, due to the way `ld` works. – Kan Li Sep 21 '16 at 01:16

1 Answers1

0

I would use the approach beginning with U symbols. In practice, the uppercase symbol types are all you need to be concerned with (those are what you link against). I wrote scripts to print the exported and imported symbols, and for this case, it would be enough to do

exports libB.a >libB-exports
externs libA.a >libA-externs
comm libB-exports libA-externs >libA-needs-libB

to list symbols where libA would use a symbol from libB (the lists are sorted, so comm should "just work"). If those were shared libraries, the scripts would have to be modified (adding a -D option to `nm).

Further reading:

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105