travitch / build-bom

Dynamically discover the commands used to create a piece of software
Apache License 2.0
45 stars 8 forks source link

Bitcode extraction from ar files #42

Open kquick opened 1 year ago

kquick commented 1 year ago

An archive file (e.g. static library) requires special handling. Currently build-bom can be used to extract from an archive file (via objcopy) but the result is only the bitcode for the last member of the archive. It's not clear if objcopy is only extracting this last member, or if it's extracting each member in turn and overwriting the output file each time. Regardless, build-bom should be updated to specifically support extraction from archive files.

kquick commented 3 months ago

Clarification: objdump will extract the section from all members of an ar file, one after another, each overwriting the output section file leaving the final contents of the section file as provided by the final member of the archive (that has the requested section).

To properly handle static libraries, it may be necessary to extract each member first and run objdump on each one, or alternatively extract and link them all together and run objdump on the linked result.

However, it can also be observed that when an executable is linked with the static library, only those members of the static library that are needed are included in the final executable (and they are then available via the bitcode from that executable). Therefore, extracting bitcode from the static library itself may yield code that is not actually present in the final solution, and one can take the position that the bitcode really should be extracted from the final executable and not the static library.

If the latter perspective is taken, it may still be useful to detect when the bitcode extraction is being run on a static library and warn the user about the behavior that will exhibit.