How to make duplicate sections in ELF file
Asked Answered
H

1

6

I have a requirement where I need to create a duplicate/copy section of .data section.

I've tried creating a dummy section with same size of data section in linker script and copy the contents of data section to the dummy section in the init functions of my ELF image, but that doesn't suit my requirement, as I want the copy/duplicate section to be created along with final ELF image not during the execution of it.

Below is what I wanted in my linker script,

SECTIONS {
    .data : { <data section contents> }
    .dummydata : { <copy of .data section> } 
}

Can anyone help to write the linker script to match above requirement?

Hostess answered 29/6, 2015 at 20:43 Comment(3)
And why do you think you want to do this? This feels like an XY Problem.Rescind
@Jonathan My platform expects a copy of contents of the data section to be loaded into a predefined location to use it as a ROM data. Basically this arrangement has been by a third party compiler tool chain till now, but we want to migrate to GNU open source tool chain now. There is a way possible by changing assembly code which works with the ROM data to satisfy the requirements of this new version of our ELF image to boot up, but i don't want to disturb the assembly code, so i want to achieve it by creating a duplicate copy of data section in linker script.Hostess
Have fun! I don't know that it is impossible, but I've no clue how to go about doing it.Rescind
F
4

I don't think this can be done with just ld and a linker script. Given this line from here:

If a file name matches more than one wildcard pattern, or if a file name appears explicitly and is also matched by a wildcard pattern, the linker will use the first match in the linker script.

It sounds like the linker script will only put the data (or anything) in one section.

However all hope is not lost. You can copy the section using objcopy and then add the section using objcopy again

objcopy -O binary --only-section=.data your-file temp.bin
objcopy --add-section .dummydata=temp.bin your-file

This will append the section to be the last section with a VMA/LMA of 0. You can then use objcopy to move the section to the desired location.

objcopy --change-section-address .dummydata=desired-address your-file

Of course if there is something already there that would be problematic. Luckily you can create a hole right after your first .data with something like:

data_start = .;
.data : { *(.data) }
data_end = .;
. += (data_end - data_start);

This should create a hole right after your first data, big enough to put another copy of data right after it. If this isn't exactly where you want it to be just add (data_end - data_start) where you want the hole.

Finally you can change the section flags, again with objcopy

objcopy --set-section-flags .dummydata=the-flags-you-want your-file

Not as clean as just duplicating something in the linker script but it should work.

Filature answered 29/6, 2015 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.