Linking/ Compiling a program so that it is flash address independent?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Linking/ Compiling a program so that it is flash address independent?

3,787 Views
gearhead1107
Contributor IV

I'm trying to develop a way to update the boot-loader application on my S32. To do this, I'm trying to copy the "old" boot-loader to a new location in flash, and jump to it. After running the "old" boot-loader in the new location, the idea is that it will erase the old location and flash the *new* boot-loader.

I've also seen this done where there are always two boot-loaders, one new and one old, in two different locations. However, the problem I'm having exists for both scenarios:

After jumping to the old binary in the new location (0x70000 rather than 0x10000), there are inevitably address for functions that get jumped to explicitly. The following example is from the startup code (although this happens elsewhere in text.main if I skip startup), where the CPU is jumping to an address 0x19E01 as indicated in the binary file that used to be at 0x10000.Program Relocation Issue.jpg

How can I compile this program so that subroutines that used to run at 0x19E01 will now just run at 0x79E01?

Thank you

0 Kudos
9 Replies

2,871 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

here is thread realted to fPIC - it  is for Kinetis, but it is similar to s32k144: 
kinetis with fPIC (position independent code) 

0 Kudos

2,871 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

I did more research - If I understand it correctly - you need -fPIE linker switch isntead -fPIC. -fPIE is for position independent executable. I played with simple hello_world and it is not working at all. In disassembly there are always absolute addresses instead loading address from table.

Jiri

0 Kudos

2,871 Views
gearhead1107
Contributor IV

Minor Update:

It looks like the actual term for this is "position independent code" - however when I enable this option, I get linker errors for the  ".got" section overlapping with .code. Any hints on how to edit the linker script to properly place these new sections?

fpic.jpg

0 Kudos

2,871 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

it looks like that your issue is also as a ticket in our internal system. In this particular case - It looks like that your code is too big for memory. The .got section doesn't fit in remaining memory. Just try to shrink your executable size. 

Jiri 

0 Kudos

2,871 Views
gearhead1107
Contributor IV

I placed the offset table in the data section, however the code doesn't run now.. any help would be appreciated:

.data : AT(__DATA_ROM)
{
. = ALIGN(4);
__DATA_RAM = .;
__data_start__ = .; /* Create a global symbol at data start. */
*(.got*) /* .got and .plt for position independent code */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
KEEP(*(.jcr*))
. = ALIGN(4);
__data_end__ = .; /* Define a global symbol at data end. */
} > m_data

0 Kudos

2,871 Views
gearhead1107
Contributor IV

Still messing with this - albeit blindly. It occurred to me that even though I have "-fpic" set in my compiler settings, I don't have any such setting in my linker. I added "--pic-executable" to the linker's options, which generated sections for "interp", "dyn", "rel.dyn" and "hash". I tried throwing these into the data section as well, but got the following error:

c:/nxp/s32ds_arm_v2.0/cross_tools/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld.exe:  could not find output section .hash
c:/nxp/s32ds_arm_v2.0/cross_tools/gcc-arm-none-eabi-4_9/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/bin/ld.exe: final link failed: Nonrepresentable section on output

I'm guessing this is because even though the compiler has been given "-fPIC", it isn't generating a .hash section expected by the linker?

0 Kudos

2,871 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

there is no special settings needed for position independent code. The .got section is created automatically.  When you enable -fPIC option, the absolute addresses are converted to offsets. In attachment is objdump for hello_world_clocks examples without and with -fPIC options. 

0 Kudos

2,871 Views
mikedoidge
NXP Employee
NXP Employee

Hello,

It would be much simpler to reach your objective if you setup your bootloader to always execute out of RAM. This way you can easily overwrite the bootloader code stored in flash anytime.

Hope that helps,

Mike

0 Kudos

2,551 Views
d_shivamkumar
Contributor III

how to achieve that sir?

how to make bootloader always run from RAM instead of p-flash?

0 Kudos