public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* objcopy - redefine dynamic symbols
@ 2006-03-01 19:20 Thomas Eschenbacher
  2006-03-03 14:56 ` Nick Clifton
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Eschenbacher @ 2006-03-01 19:20 UTC (permalink / raw)
  To: binutils

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: objcopy - redefine dynamic symbols
  2006-03-01 19:20 objcopy - redefine dynamic symbols Thomas Eschenbacher
@ 2006-03-03 14:56 ` Nick Clifton
  2006-03-04  8:33   ` Thomas Eschenbacher
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Clifton @ 2006-03-03 14:56 UTC (permalink / raw)
  To: Thomas Eschenbacher; +Cc: binutils

Hi Thomas,

> 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.

> 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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: objcopy - redefine dynamic symbols
  2006-03-03 14:56 ` Nick Clifton
@ 2006-03-04  8:33   ` Thomas Eschenbacher
  2006-03-07  8:16     ` Nick Clifton
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Eschenbacher @ 2006-03-04  8:33 UTC (permalink / raw)
  To: binutils

Nick Clifton wrote:
> [...]
> 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 ?".

> 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" ?

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: objcopy - redefine dynamic symbols
  2006-03-04  8:33   ` Thomas Eschenbacher
@ 2006-03-07  8:16     ` Nick Clifton
  2006-03-12  7:18       ` Thomas Eschenbacher
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Clifton @ 2006-03-07  8:16 UTC (permalink / raw)
  To: Thomas Eschenbacher; +Cc: binutils

Hi Thomas,

> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: objcopy - redefine dynamic symbols
  2006-03-07  8:16     ` Nick Clifton
@ 2006-03-12  7:18       ` Thomas Eschenbacher
  2006-03-19 20:28         ` Thomas Eschenbacher
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Eschenbacher @ 2006-03-12  7:18 UTC (permalink / raw)
  To: binutils

Nick Clifton wrote:
> [...]
> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: objcopy - redefine dynamic symbols
  2006-03-12  7:18       ` Thomas Eschenbacher
@ 2006-03-19 20:28         ` Thomas Eschenbacher
  0 siblings, 0 replies; 6+ messages in thread
From: Thomas Eschenbacher @ 2006-03-19 20:28 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 1265 bytes --]

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

[-- Attachment #2: rename-dynamic.diff.gz --]
[-- Type: application/x-gzip, Size: 5375 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-03-19 20:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-01 19:20 objcopy - redefine dynamic symbols Thomas Eschenbacher
2006-03-03 14:56 ` Nick Clifton
2006-03-04  8:33   ` Thomas Eschenbacher
2006-03-07  8:16     ` Nick Clifton
2006-03-12  7:18       ` Thomas Eschenbacher
2006-03-19 20:28         ` Thomas Eschenbacher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).