Kishore SN
Kishore SN

Reputation: 21

How to build relocatable binary which includes only selected symbols from libraries?

I am trying to build a relocatable binary file for an embedded system which includes only some libraries. It will not include any source file, just the libraries will be linked into a relocatable file as follows:

ld -r -o <binary_name> --whole-archive <list of libraries>

If I do not use the --whole-archive option, then the resulting binary file does not contain any symbols because, I am not linking any object files which will use the symbols. So, I am using the --whole-archive and the binary file is generated successfully.

Now, I want to reduce the size of the binary file. I have only few apps on my embedded system, and so, I know exactly which symbols from the list of libraries will be used by my apps. So, while linking, I want to include only this set of symbols in the final binary file.

For example, suppose I am trying to link libc into a relocatable binary file. And suppose, my application will use only printf symbol from libc, then I want my relocatable binary to contain only printf and any symbols used by printf.

Is there any way to achieve this?

Upvotes: 1

Views: 982

Answers (2)

Employed Russian
Employed Russian

Reputation: 213446

Is there any way to achieve this?

Yes:

ld -r -o <binary_name> -u printf <list of libraries>

Upvotes: 1

rtx13
rtx13

Reputation: 2610

A quick and dirty way may be to compile and link a table of function pointers to wanted library functions:

#include <stdio.h>

typedef void (*fn_t)(void);
fn_t fns[] = {
    (fn_t)printf,
};

Upvotes: 1

Related Questions