Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stripping unneeded symbols from a static library

I have a set of absolutely massive .a files from a third party. I have my own library that calls just 5 or 6 functions from this set of libraries. I would like to produce a much smaller .a file that includes my code and its (small number of) dependencies in the external library.

To be concrete:

What I Have

  1. external.h

    int foo();
    int bar();
    
  2. libexternal.a

    0000000000000000 T foo()
    0000000000000010 T bar()
    
  3. mylibrary.c

    #include "external.h"
    int foo_wrapper() { return foo(); }
    
  4. mylibrary.h

    int foo_wrapper();
    

What I want

I want to create a file libmylibrary.a that has the symbols for foo_wrapper, its dependency foo, and anything that foo calls internally, but not bar (which actually represents thousands of functions). This way, people could include my library without having to include external.h or link in libexternal.a. I am not willing to manually list foo because there are actually a whole bunch of functions.

Can it be done?

More Detail

I'm willing to list the symbols of mine that I need. So for example, I'm ok with specifying foo_wrapper on the command line. But I'm not ok also specifying foo on the command line, as well as whatever functions it calls. The reason I'm ok specifying foo_wrapper is that it allows me to write one function called callEverything() that just calls all of the functions I want to make available, and then put that symbol on the command line.

like image 749
E_G Avatar asked Dec 18 '12 22:12

E_G


1 Answers

Like KevinDTimm said: You can edit your .a file using "ar" to extract all the object files inside it and to add the necessary ones back to the .a archive.

The author of the library could have put all his functions into a single .c files or in multiple files. Each .c file results in one object file.

I think you have no chance to only take a part of the object file, you need all of it or nothing. At link time it's too late to separate a .obj file again into it's functions: There are too many directly embedded addresses and function calls.

Also your final program will contain a subset of the obj files that are "archived" into your linked .a static libraries. But each single obj file is either in your final exectuable completely or not at all!

like image 84
Christian Avatar answered Oct 27 '22 01:10

Christian