Skip to contentSkip to conversation

board_r - fixup functions table after relocation

PATCH Superseded Alexey Brodkin 3 participants 4 messages Jan 20, 2014 – Jan 27, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 board_r - fixup functions table after relocation

This is only required for "PIC" relocation and doesn't apply to modern "PIE" relocation which does data relocation as well as code.

"init_sequence_r" is just an array that consists of compile-time adresses of init functions. Since this is basically an array of integers (pointers to "void" to be more precise) it won't be modified during relocation - it will be just copied to new location as it is.

As a consequence on execution after relocation "initcall_run_list" will be jumping to pre-relocation addresses. As long as we don't overwrite pre-relocation memory area init calls are executed correctly. But still it is dangerous because after relocation we don't expect initially used memory to stay untouched.

Cc Tom Rini <[email protected]>
Cc Simon Glass <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Doug Anderson <[email protected]>
Cc Thomas Langer <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Changes to v2:
* Use ARRAY_SIZE as suggested by Simon Glass
* Added CONFIG_NEEDS_MANUAL_RELOC wrapper for temporary index variable
"i" to fix compile-time warning for boards without CONFIG_NEEDS_MANUAL_RELOC

---
 common/board_r.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
common/board_r.c +10 -0
index 86ca1cb..c2d0763 100644
@@ -903,9 +903,19 @@ init_fnc_t init_sequence_r[] = {
 
 void board_init_r(gd_t *new_gd, ulong dest_addr)
 {
+#ifdef CONFIG_NEEDS_MANUAL_RELOC
+	int i;
+#endif
+
 #ifndef CONFIG_X86
 	gd = new_gd;
 #endif
+
+#ifdef CONFIG_NEEDS_MANUAL_RELOC
+	for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
+		init_sequence_r[i] += gd->reloc_off;
+#endif
+
 	if (initcall_run_list(init_sequence_r))
 		hang();
 
Signature · 1 line
1.8.4.2
#1 lore raw

PATCH v3 1/1 board_r - fixup functions table after relocation

Hi,

Quoted 9 lines · Alexey Brodkin

This is only required for "PIC" relocation and doesn't apply to modern "PIE" relocation which does data relocation as well as code.

"init_sequence_r" is just an array that consists of compile-time adresses of init functions. Since this is basically an array of integers (pointers to "void" to be more precise) it won't be modified during relocation - it will be just copied to new location as it is.

Quoted 7 lines

As a consequence on execution after relocation "initcall_run_list" will be jumping to pre-relocation addresses. As long as we don't overwrite pre-relocation memory area init calls are executed correctly. But still it is dangerous because after relocation we don't expect initially used memory to stay untouched.

It's unfortunate that we need to carry over CONFIG_NEEDS_MANUAL_RELOC to the new common board init code. But if we do, this patch looks reasonable, so:

Quoted 44 lines
Cc Tom Rini <[email protected]>
Cc Simon Glass <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Doug Anderson <[email protected]>
Cc Thomas Langer <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Changes to v2:
* Use ARRAY_SIZE as suggested by Simon Glass
* Added CONFIG_NEEDS_MANUAL_RELOC wrapper for temporary index variable
"i" to fix compile-time warning for boards without CONFIG_NEEDS_MANUAL_RELOC

---
 common/board_r.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
common/board_r.c +10 -1
index 86ca1cb..c2d0763 100644
@@ -903,9 +903,19 @@ init_fnc_t init_sequence_r[] = {
 
 void board_init_r(gd_t *new_gd, ulong dest_addr)
 {
+#ifdef CONFIG_NEEDS_MANUAL_RELOC
+       int i;
+#endif
+
 #ifndef CONFIG_X86
        gd = new_gd;
 #endif
+
+#ifdef CONFIG_NEEDS_MANUAL_RELOC
+       for (i = 0; i < ARRAY_SIZE(init_sequence_r); i++)
+               init_sequence_r[i] += gd->reloc_off;
+#endif
+
        if (initcall_run_list(init_sequence_r))
                hang();
 
--

1.8.4.2

#2 lore raw

PATCH v3 1/1 board_r - fixup functions table after relocation

Hi Simon,

Quoted 5 lines · Simon Glass

Hi,

It's unfortunate that we need to carry over CONFIG_NEEDS_MANUAL_RELOC to the new common board init code. But if we do, this patch looks reasonable, so:

I think as long as PIC relocation is supported in U-Boot (for some architectures) it's good to accept fixes for it. And I have to confess that this kind of fixes are missing in many other places.

That's why I switched to PIE as well so this particular patch doesn't change anything for me now. But for those who still use PIC it might be helpful - that's why I did another re-spin.

-Alexey

#3 lore raw

PATCH v3 1/1 board_r - fixup functions table after relocation

In reply to Alexey Brodkin
Quoted 14 lines · Alexey Brodkin

This is only required for "PIC" relocation and doesn't apply to modern "PIE" relocation which does data relocation as well as code.

"init_sequence_r" is just an array that consists of compile-time adresses of init functions. Since this is basically an array of integers (pointers to "void" to be more precise) it won't be modified during relocation - it will be just copied to new location as it is.

As a consequence on execution after relocation "initcall_run_list" will be jumping to pre-relocation addresses. As long as we don't overwrite pre-relocation memory area init calls are executed correctly. But still it is dangerous because after relocation we don't expect initially used memory to stay untouched.

Quoted 8 lines
Cc Tom Rini <[email protected]>
Cc Simon Glass <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Doug Anderson <[email protected]>
Cc Thomas Langer <[email protected]>
Cc Albert ARIBAUD <[email protected]>

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140127/2deb1dc2/attachment.pgp>