Bartleby 0.1.0
A symbol renaming toolkit
|
This page is a brief overview of the objcopy(1)
tool.
objcopy(1)
offers several options that could be used to solve symbol collisions across libraries.
However, it has some limitations that makes it harder to use on large set of libraries.
Through this overview, we demonstrate that objcopy
is not a suitable solution to the symbol name collisions problem.
The --redefine-sym
option renames a given symbol:
–redefine-sym old=new
- Change the name of a symbol old, to new. This can be useful when one is trying link two things together for which you have no source, and there are name collisions.
Here is a small example of --redefine-sym
, ran on a simple object that defines the SSL_CTX_new
symbol:
We successfully renamed the SSL_CTX_new
symbol to __private_SSL_CTX_new
. However, it does not scale for large libraries, nor for large set of objects. Thus, objcopy(1)
provides the --redefine-syms
option, to solve that scaling issue.
--redefine-syms
acts like --redefine-sym
, but takes a file containing the symbols to rename:
–redefine-syms=filename
Apply –redefine-sym to each symbol pair "<em>old new</em>" listed in the file filename.
filename is simply a flat file, with one symbol pair per line. Line comments may be introduced by the hash character.
This option may be given more than once.
Although this option may solve the scaling issue --redefine-sym
has, it is not easy to use: one has to generate the list of symbols defined by the target library, and also has to map the new name for each of these symbols.
It is often sufficient to append a prefix to all the symbol names, in order to make then unique.
For instance, one can prefix all the symbol names from an external library with __private_mylib_external_
.
objcopy(1)
has the --prefix-symbols
option for that purpose:
–prefix-symbols=string
Prefix all symbols in the output file with string.
The following is an example of that option on the same simple object used in our previous example:
But this option has a major drawback: it renames all symbols in the target object, even the undefined ones that may be defined by an external library, such as the libc
.
Giving the following C code:
We end up with the symbol list below:
display_filename
is defined by our object (indicated by the flag T
). fputs
is undefined (indicated by the flag U
). stdout
is also undefined.These undefined symbols are well-known symbols belonging to the libc
.
Now, we run --prefix-symbols
on a.o:
As expected, display_filename
has been prefixed correctly, however our undefined symbols have also been prefixed. This is not correct, because when this object is linked to produce a final binary, the link will attempt to find the symbols __private_fputs
and __private_stdout
, which are obviously not defined in the libc:
Because it would not be a great solution to also prefix the symbols from the libc
, --prefix-symbols
is not a solution to our problem neither.
Even if objcopy(1)
offers at least three mechanism for renaming symbols, we saw that none of them really fit our use case.
We can also add the techniques we described earlier imply that the developer has to somehow specify for each symbol its new name. This is a major issue when it comes with external dependencies: it means that every header file has to be patched according to the new symbol names.
Bartleby is based on the LLVM implementation of objcopy
. LLVM also provides its own objcopy
tool, called llvm-objcopy
.
objcopy:
objcopy(1)
llvm-objcopy
: llvm-objcopy - object copying and editing tool llvm::objcopy:
llvm::objcopy Namespace Reference