Using bfd to change the .text section of an executable

Eric Schulte schulte.eric@gmail.com
Wed Mar 23 20:30:00 GMT 2011


Hi,

I am trying to use bfd to change the .text section of existing elf
executable files.  I have successfully used bfd to change selected bytes
in the .text section of a simple elf executable resulting in a new
functioning variant executable.

However, any changes which change the size of the .text section (in this
case appending four noops to the end) the resulting file is no longer a
working executable but rather throws segfaults on execution.

After some investigation it appears that while bfd is updating the lma
address of all sections following the .text section in memory (as shown
in the bfd warnings printed in [1] when running my `copy' program, the
source code for which is included below in [2]) bfd is not making
similar adjustments to the vma field of these sections.

Is it possible to convince bfd to manage vma addresses for me?  I've
tried setting the `user_set_vma' flag to 0 before making the copy, but
to no avail.  Also, attempts to set the vma field of these sections
manually before calling bfd_close don't have any effect on the resulting
output file.

Many Thanks -- Eric

p.s. Please let me know if there is a better forum in which to place
     this question, or if I've left out any information required to
     answer this question.  Thanks

Footnotes: 
[1]  The `copy' program uses bfd to copy the hello executable (compiled
     form a simple hello world C program) into the goodbye executable
     (identical to hello but with four noops appended to .text)
     
     $ ./copy hello goodbye
     BFD: goodbye: section .fini lma 0x804846c adjusted to 0x8048470
     BFD: goodbye: section .rodata lma 0x8048488 adjusted to 0x804848c
     BFD: goodbye: section .eh_frame lma 0x804849c adjusted to 0x80484a0
     .fini has lma 0x8048470 user_set_vma is 1
     .rodata has lma 0x804848c user_set_vma is 1
     .eh_frame has lma 0x80484a0 user_set_vma is 1

     $ objdump -h goodbye |egrep -i "fini|rodata|eh_frame|LMA"
     Idx Name          Size      VMA       LMA       File off  Algn
      13 .fini         0000001c  0804846c  08048470  00000470  2**2
      14 .rodata       00000014  08048488  0804848c  0000048c  2**2
      15 .eh_frame     00000004  0804849c  080484a0  000004a0  2**2

[2] this copy.c program consists of a short main, and a good deal of
     code taken from objcopy.c --- after trying unsuccessfully to expose
     some objcopy functions through a shared library I just copied the
     required code directly into my program.
     
-------------- next part --------------
A non-text attachment was scrubbed...
Name: copy.c
Type: text/x-csrc
Size: 25641 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20110323/77b24709/attachment.bin>


More information about the Binutils mailing list