Discussion:
objcopy - redefine dynamic symbols
Thomas Eschenbacher
2006-03-01 19:20:11 UTC
Permalink
Hi,

I am currently in the situation that I need a tool for redefining
dynamic symbols of a (stripped) ELF file.
I tried objcopy --redefine-sym... , but found out that this does not
have any effect on dynamic symbols.

I also found an old thread on this list (from june 2000)
http://sourceware.org/ml/binutils/2000-06/msg00211.html
which described this - but no real solution.

Should I implement this feature in objcopy,
through modifying .dynstr/.dynsym/.hash ?

Any ideas where to start?

Thomas
Nick Clifton
2006-03-03 15:06:45 UTC
Permalink
Hi Thomas,
Post by Thomas Eschenbacher
I am currently in the situation that I need a tool for redefining
dynamic symbols of a (stripped) ELF file.
Should I implement this feature in objcopy,
through modifying .dynstr/.dynsym/.hash ?
Yes.
Post by Thomas Eschenbacher
Any ideas where to start?
Read the ELF spec. Work out what you are going to have to do in order
to rename the symbols. Have a look at the code in bfd/elfcode.h. You
will find functions here that work with the dynamic symbol table. You
may be able to use them, or you may have to modify them to allow you to
do what you need to do. Also look at the code in binutils/objcopy.c and
see how it renames ordinary symbols.

Cheers
Nick
Thomas Eschenbacher
2006-03-04 08:33:17 UTC
Permalink
Post by Nick Clifton
[...]
Read the ELF spec. Work out what you are going to have to do in order
to rename the symbols.
OK, I already have done that and I now understand how the result should
look like. It's rather a matter of "how can I do that by using as much
as possible from BFD ?".
Post by Nick Clifton
Have a look at the code in bfd/elfcode.h. You
will find functions here that work with the dynamic symbol table. You
may be able to use them, or you may have to modify them to allow you to
do what you need to do.
You think about elf_swap_dyn_in/out? I can find nice functions for
reading dynamic symbols, but nothing for creating/writing. Where is the
opposite of "elf_slurp_symbol_table" ?
Post by Nick Clifton
Also look at the code in binutils/objcopy.c and
see how it renames ordinary symbols.
I started to implement a bit in objcopy, in the copy_object function,
before the call to bfd_set_symtab. I tried to imitate what is already
done with normal symbols, like this:
----------
{
long dynsymsize;
long dynsymcount;
asymbol **idynsympp = NULL;
asymbol **odynsympp = NULL;

dynsymsize = bfd_get_dynamic_symtab_upper_bound (ibfd);
idynsympp = xmalloc (dynsymsize);
dynsymcount = bfd_canonicalize_dynamic_symtab (ibfd, idynsympp);

odynsympp = xmalloc ((dynsymcount + 1) * sizeof (asymbol *));
dynsymcount = filter_symbols (ibfd, obfd, odynsympp,
idynsympp, dynsymcount);
/* ... ? */
}
----------
(omitted error handling / debug printfs)

I added some printf to the filter_symbols function and I can see that my
symbol(s) are successfully renamed during this process, so I guess that
I now have what I want, but only in the internal representation of
objcopy/bfd.

But what now? Nothing of that appears in the output file. So how can I
achieve this? There seems to be nothing like "bfd_set_dynamic_symtab" as
the dynamic counterpart of bfd_set_symtab!?

Thomas
Nick Clifton
2006-03-07 08:27:13 UTC
Permalink
Hi Thomas,
Post by Thomas Eschenbacher
I added some printf to the filter_symbols function and I can see that my
symbol(s) are successfully renamed during this process, so I guess that
I now have what I want, but only in the internal representation of
objcopy/bfd.
But what now? Nothing of that appears in the output file. So how can I
achieve this? There seems to be nothing like "bfd_set_dynamic_symtab" as
the dynamic counterpart of bfd_set_symtab!?
Hmm, maybe you need to call some of the ELF backend functions like
elf_backend_finish_dynamic_symbol or elf_backend_finish_dynamic_section
or elf_backend_adjust_dynamic_symbol or even
elf_backend_size_dynamic_section. These are all defined in
bfd/elf-bfd.h although their implementations may be target specific.

Cheers
Nick
Thomas Eschenbacher
2006-03-12 07:18:24 UTC
Permalink
Post by Nick Clifton
[...]
Hmm, maybe you need to call some of the ELF backend functions like
elf_backend_finish_dynamic_symbol or elf_backend_finish_dynamic_section
or elf_backend_adjust_dynamic_symbol or even
elf_backend_size_dynamic_section. These are all defined in
bfd/elf-bfd.h although their implementations may be target specific.
thanks for the hint - but all these functions seem to handle "link"
information, where should I get this from!? Sorry, but I am still not so
familiar with this monster called "libbfd" :-(

Meanwhile I made a different approach, with creating new ".dynsym" /
".dynstr" section contents and since friday I got that working, that
means I now can rename dynamic symbols!

If someone is interested in the patch, I can post it here on the list.

The next step will be fixing up (re-creating) the hash table in the
".hash" section, which has been broken by the rename...

Thomas
Thomas Eschenbacher
2006-03-19 20:12:49 UTC
Permalink
Hi,

after several weeks of work I have given up in creating a "simple" tool
for renaming dynamic symbols. (I am very very happy that I now have
found an alternative solution to escape from this bfd/elf hell).

I attached my patch to objcopy.c from binutils-0.15, just in case
someone will try to complete that work in future...

Please do not apply this patch and expect anything to work, it still
might break a lot of things!!! It is a brute-force attempt, incomplete,
quick&dirty, with lots of memory leaks, missing sanity checks and so on.

Basically it does the following:
1. loop over .dynsym and re-create .dynstr
2. re-create the .hash table from scratch
3. calculate new lma/vma, new memory layout (.dynstr size changed!)
4. fix the contents of .dynamic, according to the new layout
5. fix .rel.dyn
6. fix .rel.plt
7. fix .dynsym again
8. write out the new section contents

In copy_section: exclude the rewritten sections from being copyied.

Currently it seems to create a syntactically valid ELF file, but as the
distance between .plt and .got changes, the relative addressing in .plt
is broken. Additionally the .got has to be fixed again. It also ignores
any "private" section data.

Might someone find a better way to do all this some day...

Thomas

Loading...