This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How initialize .data section


Hi Massimiliano,

> 	My problem is how initilize the .data section at the runtime.
> 	I know that there are many examples for do this from ROM using
> 	the AT() directive.  But my problem is different!
> 	My problem is that I needs to create a copy Image (a clone) of
> 	the content of the .data section at address less than the
> 	.data section itself. I must to create my FMW in a single RAM
> 	image. I use the ELF format for PPC.

There is no easy way to do this using the linker.

The simplest method would be to arrange for the program loader code to
create the duplicate of the .data section when it loads the program.
This is very similar to a loader which copies a program from ROM to
RAM before it starts executing it.

> I think is better to modify the sources of the LD and then clone the
> .data section in the final Link and then build the ".initdata"
> section. But how I can proceed for do this ??

I would recommend against doing this.  It is not something that the
linker is designed to do.

One possible solution - if you need to do this at link-time rather
than load-time - is to use objcopy.  ie proceed like this:

  1.  Create the fully linked executable with the .data section but no
      .initdata section using the normal linker command line.

  2.  Use objcopy to extract the contents of the .data section.  eg:

        objcopy --only-section .data <foo> <foo>.data

  3.  Then use objcopy to rename the section. eg:
      
        objcopy --rename-section .data=.initdata <foo>.data <foo>.initdata

  4.  Followed by adjusting its address.  eg:
      
        objcopy --change-section-address .initdata-0xXXXX <foo>.initdata

      Working out the value for XXXX here is the trick.  You
      presumably want to look at the size of the .data section and use
      at least this amount in order to make sure that the .initdata
      section is located before the .data section.
      
  5.  Finally you can then insert this new section back into the
      original executable.  eg:

        objcopy --add-section .initdata=<foo>.initdata <foo>


The problem with this approach is that it does guarantee that there is
room in the <foo> executable for the new section at the address you
want.  If the offset you choose in step 4 does not move the .initdata
section to a free space in the address map, the objcopy in step 5 will
fail.

Cheers
        Nick
        


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]