I'm not sure if this is specific to the processor I'm using, so for what it's worth I'm using a Cortex M0+. I was wondering: if I generate a hex file through gcc using -fPIC, I produce...Position Independent Code. However, the intel hex file format that I get out of objcopy always has address information on each line's header. If I'm trying to write a bootloader, do I just ignore that information, skip the bytes relating to it, and load the actual code into memory wherever I want, or do I have to keep track of it somehow?
-
1well for a bootloader you just need to take care of only one thing. That it does not intersect with the address of the program which it is going to load. For example if bootloader is supposed to boot kernel then it should not intersect with the kernel memory (where kernel image is placed in the memory) or the devicetree. – theadnangondal Jan 24 '17 at 09:28
-
But other than that, the question was: do I strip the hex file of its address information and checksum, then just dump the rest at some address, and jump to where it starts? Or are there segments to the hex file that I need to know about so I need to know where to jump to? – Michael Stachowsky Jan 24 '17 at 11:27
-
Yeah, I guess you can remove that information. I do not see any use of those addresses in there anyway... Jumps are calculated differently independent of those address .... Maybe this help in understanding http://stackoverflow.com/questions/5024387/trying-to-load-position-independent-code-on-cortex-m3 – theadnangondal Jan 24 '17 at 11:46
-
You need some type of hex2bin utility to create a flat image of the boot loader. – rcgldr May 06 '17 at 23:57
1 Answers
The intel-HEX format was specially designed to programm PROMs, EPROMS or processors with an internal EPROM and is normally used with programmers for theses devices. The addresses at the beginning of the records have not much to do with the program code directly. They indicate at which address of the PROM the data will be written. Remember also that the PROM can be mapped anywhere into the address space of the processor, thus the final address can change anyway.
As long as you don't want to program a PROM you must remove anything except the data from the records. (Don't forget the checksum at the end ;-)
As I understand the intel-HEX format the records must not be contiguous, there may be holes in between.
Some remarks:
The -f PIC parameter is not responsible for the intel-HEX format. I think that somewhere in your command lines you'll find -O ihex. If you want to have a file that could be executed, objcopy provides better suited output formats.
As long as you don't write earlier stages of the boot process by yourself, you don't load your bootloader - it will be loaded for you. The address at which this will happen is normally fixed and not changeable. So there is no need for position independent code, but it doesn't hurt either.

- 187
- 10
-
1We'll, it's an old question, but I have solved it. The sections matter, and they are placed at their addresses specified by the Linker file. The data and BSS segments are not position independent unless I also make relocatable code. So long story short, to just load code the address are not relevant, but to let code and data they matter – Michael Stachowsky May 07 '17 at 16:47
-
@MichaelStachowsky, please add your own answer then, and see if you can then change the accepted answer from this one to your own answer if you have answered your own question. It will help future people to see you've answered your own question, and what your answer is. – Gabriel Staples Jan 30 '19 at 23:45