0

I have created a raw binary created from an elf file by using objcopy -o binary. Is it possible to relocate it by a given offset? I believe the effect that I'm searching for would be similar to use --change-addresses option and then convert it to binary.

Thank you in advance

josecm
  • 413
  • 3
  • 15

1 Answers1

0

from man obdjdump:

objcopy can be used to generate a raw binary file by using an output target of binary (e.g., use -O binary). When objcopy generates a raw binary file, it will essentially
produce a memory dump of the contents of the input object file. All symbols and relocation information will be discarded. The memory dump will start at the load address of the lowest section copied into the output file.

(emphasis by me)

So unless you're exporting multiple sections that get loaded at different places, then shifting the single section you're pushing into the binary has no effect.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • I don't know if I get what you mean. The code is meant to run in an embedded environment, in a real physical address space. So actually shifting the single section is important. The binary must be prepared to run at the specific location in which it will be loaded. – josecm Feb 19 '17 at 10:56
  • but that's not the kind of binary that `-O binary` produces; "The memory dump will start at the load address of the lowest section". So if you only have one section, than no matter where it begins, its start will always be the start of the binary dump. – Marcus Müller Feb 19 '17 at 10:57
  • Yeah of course. But in the linker script, the actual VMA addresses are specified to the address range of interest. If for example, a global variable it's located at address X, the code will reference that address. I want to change the binary so that the code which references the variable (which now will be at X+OFFSET) is altered to reference the correct address. – josecm Feb 19 '17 at 13:04
  • hm, I see, but how'd you get the info that "this is X, relocate it hereto" from the binary? The binary has lost all information regarding linker sections. – Marcus Müller Feb 19 '17 at 13:21
  • That is exactly my problem. I don't see how can this be done. However, given my limited knowledge on the intrinsics of the subject, I was asking If this could be accomplished even by resorting to non-conventional methods or tools. – josecm Feb 19 '17 at 13:37
  • you could probably add "magic" values in your linker scripts that you can use to later find the sections you need. Or you can simply do the relocation based on the ELF instead – because what you want is to section and relocate an executable, so going for a sectioned & relocatable executable format seems... wise. – Marcus Müller Feb 19 '17 at 13:42