Skip to contentSkip to conversation

futile c relocation attempt

PATCH New Reinhard Meyer 4 participants 9 messages Oct 5, 2010 – Oct 6, 2010 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 futile c relocation attempt

---
 arch/arm/cpu/arm926ejs/start.S |    8 ++++-
 arch/arm/lib/board.c           |   57 +++++++++++++++++++++++++++++++++++++++-
 include/configs/top9000_9xe.h  |    1 +
 3 files changed, 63 insertions(+), 3 deletions(-)
arch/arm/cpu/arm926ejs/start.S +6 -2
index 5a7ae7e..b7517db 100644
@@ -213,7 +213,7 @@ relocate_code:
 	/* Set up the stack						    */
 stack_setup:
 	mov	sp, r4
-
+#ifndef CONFIG_USE_C_RELOCATION
 	adr	r0, _start
 	ldr	r2, _TEXT_BASE
 	ldr	r3, _bss_start_ofs
@@ -266,7 +266,7 @@ fixnext:
 	str	r1, [r0]
 	add	r2, r2, #8	/* each rel.dyn entry is 8 bytes */
 	cmp	r2, r3
-	ble	fixloop
+	blt	fixloop
 #endif
 #endif	/* #ifndef CONFIG_SKIP_RELOCATE_UBOOT */
 
@@ -288,6 +288,7 @@ clbss_l:str	r2, [r0]		/* clear loop...		    */
 	bl coloured_LED_init
 	bl red_LED_on
 #endif
+#endif /* CONFIG_USE_C_RELOCATION */
 
 /*
  * We are done. Do not return, instead branch to second part of board
@@ -314,10 +315,13 @@ _board_init_r_ofs:
 	.word board_init_r - _start
 #endif
 
+.globl _rel_dyn_start_ofs
 _rel_dyn_start_ofs:
 	.word __rel_dyn_start - _start
+.globl _rel_dyn_end_ofs
 _rel_dyn_end_ofs:
 	.word __rel_dyn_end - _start
+.globl _dynsym_start_ofs
 _dynsym_start_ofs:
 	.word __dynsym_start - _start
 
arch/arm/lib/board.c +56 -1

81 lines not on this page · raw message

include/configs/top9000_9xe.h +1 -0
index f7fa198..e4ca026 100644
@@ -73,6 +73,7 @@
 #define	CONFIG_SKIP_LOWLEVEL_INIT
 /*#define CONFIG_SKIP_RELOCATE_UBOOT*/
 /*#define CONFIG_SYS_ARM_WITHOUT_RELOC*/
+#define CONFIG_USE_C_RELOCATION
 #define CONFIG_RELOC_FIXUP_WORKS
 #define	CONFIG_SYS_NO_ICACHE
 #define	CONFIG_SYS_NO_DCACHE
Signature · 1 line
1.5.6.5
#1 lore raw

PATCH 1/1 futile c relocation attempt

Quoted 5 lines · Reinhard Meyer
---
 arch/arm/cpu/arm926ejs/start.S |    8 ++++-
 arch/arm/lib/board.c           |   57 +++++++++++++++++++++++++++++++++++++++-
 include/configs/top9000_9xe.h  |    1 +
 3 files changed, 63 insertions(+), 3 deletions(-)

PLEASE - this is only for study, not to be applied !

Reinhard

#2 lore raw

PATCH 1/1 futile c relocation attempt

In reply to Reinhard Meyer

Le 05/10/2010 16:48, Reinhard Meyer a ?crit :

Quoted 1 line
 include/configs/top9000_9xe.h  |    1 +

Can't find this one in my u-boot tree. Did I miss something?

Quoted 31 lines
arch/arm/lib/board.c +31 -0
+#ifdef CONFIG_USE_C_RELOCATION
+	/* TODO: check for identical source and destination */
+	/* TODO: check for overlapping */
+	/* copy image, including initialized data */
+	debug ("memcpy(%08lx,%08lx,%ld)\n",
+		addr, _TEXT_BASE, _bss_start_ofs);
+	memcpy ((void *)addr, (void *)_TEXT_BASE, _bss_start_ofs);
+	/* now fix the code */
+	debug ("_dynsym_start_ofs=%08lx _rel_dyn_start_ofs=%08lx _rel_dyn_end_ofs=%08lx\n",
+		_dynsym_start_ofs, _rel_dyn_start_ofs, _rel_dyn_end_ofs);
+	for (rel_dyn_ptr = (Elf32_Rel *)(_TEXT_BASE + _rel_dyn_start_ofs);
+			rel_dyn_ptr<  (Elf32_Rel *)(_TEXT_BASE + _rel_dyn_end_ofs);
+			rel_dyn_ptr++) {
+		ulong *patchaddr = (ulong *) (rel_dyn_ptr->r_offset + gd->reloc_off);
+		debug ("patch %08lx : %08lx\n",
+			(ulong)patchaddr, (ulong)rel_dyn_ptr->r_info);
+		switch (ELF32_R_TYPE(rel_dyn_ptr->r_info)) {
+		case 23: /* rel fixup */
+			*patchaddr += gd->reloc_off;
+			break;
+		case 2: /* abs fixup */
+			{
+				Elf32_Sym *sym = (Elf32_Sym *)(_TEXT_BASE + _dynsym_start_ofs);
+				sym += ELF32_R_SYM(rel_dyn_ptr->r_info);
+				*patchaddr = gd->reloc_off + sym->st_value;
+			}
+			break;
+		default: /* unhandled fixup */
+			break;
+		}
+	}

arch/arm/lib/board.c void board_init_f (ulong bootflag) · line 717

Quoted 10 lines
arch/arm/lib/board.c +10 -0
+	/* clear BSS */
+# ifndef CONFIG_PRELOADER
+	debug ("clearing BSS %08lx..%08lx\n",
+		addr + _bss_start_ofs, addr + _bss_end_ofs);
+	for (p = (ulong *)(addr + _bss_start_ofs);
+			p<  (ulong *)(addr + _bss_end_ofs);
+			*p++ = 0)
+		;
+# endif
+#endif

All of _bss_start_ofs, _rel_dyn_start_ofs, _rel_dyn_end_ofs, _dynsym_start_ofs and _bss_end_ofs are going to be modified by the relocation fixup loop. The compiler *might* preload them into registers... or not; which could cause issues with ..._end_ofs ones, because it could cause the loops to overflow/underflow.

arch/arm/lib/board.c void board_init_f (ulong bootflag) · line 719

Quoted 2 lines
arch/arm/lib/board.c +1 -0
+	debug ("calling relocate_code\n");
  	relocate_code (addr_sp, id, addr);

Have you not just relocated and fixed up in the C code? If so, relocate_code (which would then become a misnomer) should have its own relocate and fixup code removed (your patch does not), and only perform the C environment (stack, etc) switch.

Side note: if board_init_f handles relocation and fixup and relocate_code just does stack switch between board_init_f and board_init_r, then I'd rather board_init_f *return* to start.S rather than *call* it if that's possible. That would make the control flow less
circuitous: begin in start.S, set a minimal stack, call board_init_f, returns to start.S, switch to final stack, calls (branches to) board_init_r.

Amicalement,

Signature · 1 line
Albert.
#3 lore raw

PATCH 1/1 futile c relocation attempt

Dear Albert ARIBAUD,

Quoted 1 line
Quoted 1 line
  include/configs/top9000_9xe.h  |    1 +

That's my board. Not in mainstream yet. Just sets the define there.

Quoted 39 lines
Quoted 39 lines
arch/arm/lib/board.c +39 -0
+#ifdef CONFIG_USE_C_RELOCATION
+	/* TODO: check for identical source and destination */
+	/* TODO: check for overlapping */
+	/* copy image, including initialized data */
+	debug ("memcpy(%08lx,%08lx,%ld)\n",
+		addr, _TEXT_BASE, _bss_start_ofs);
+	memcpy ((void *)addr, (void *)_TEXT_BASE, _bss_start_ofs);
+	/* now fix the code */
+	debug ("_dynsym_start_ofs=%08lx _rel_dyn_start_ofs=%08lx _rel_dyn_end_ofs=%08lx\n",
+		_dynsym_start_ofs, _rel_dyn_start_ofs, _rel_dyn_end_ofs);
+	for (rel_dyn_ptr = (Elf32_Rel *)(_TEXT_BASE + _rel_dyn_start_ofs);
+			rel_dyn_ptr<   (Elf32_Rel *)(_TEXT_BASE + _rel_dyn_end_ofs);
+			rel_dyn_ptr++) {
+		ulong *patchaddr = (ulong *) (rel_dyn_ptr->r_offset + gd->reloc_off);
+		debug ("patch %08lx : %08lx\n",
+			(ulong)patchaddr, (ulong)rel_dyn_ptr->r_info);
+		switch (ELF32_R_TYPE(rel_dyn_ptr->r_info)) {
+		case 23: /* rel fixup */
+			*patchaddr += gd->reloc_off;
+			break;
+		case 2: /* abs fixup */
+			{
+				Elf32_Sym *sym = (Elf32_Sym *)(_TEXT_BASE + _dynsym_start_ofs);
+				sym += ELF32_R_SYM(rel_dyn_ptr->r_info);
+				*patchaddr = gd->reloc_off + sym->st_value;
+			}
+			break;
+		default: /* unhandled fixup */
+			break;
+		}
+	}
+	/* clear BSS */
+# ifndef CONFIG_PRELOADER
+	debug ("clearing BSS %08lx..%08lx\n",
+		addr + _bss_start_ofs, addr + _bss_end_ofs);
+	for (p = (ulong *)(addr + _bss_start_ofs);
+			p<   (ulong *)(addr + _bss_end_ofs);
+			*p++ = 0)
+		;
Quoted 8 lines

arch/arm/lib/board.c void board_init_f (ulong bootflag) · line 717

Quoted 2 lines
arch/arm/lib/board.c +2 -0
+# endif
+#endif

All of _bss_start_ofs, _rel_dyn_start_ofs, _rel_dyn_end_ofs, _dynsym_start_ofs and _bss_end_ofs are going to be modified by the relocation fixup loop. The compiler *might* preload them into registers... or not; which could cause issues with ..._end_ofs ones, because it could cause the loops to overflow/underflow.

They are not going to be modified, they are in the unrelocated location!

Quoted 8 lines

arch/arm/lib/board.c void board_init_f (ulong bootflag) · line 719

Quoted 3 lines
arch/arm/lib/board.c +1 -0
+	debug ("calling relocate_code\n");
   	relocate_code (addr_sp, id, addr);

Have you not just relocated and fixed up in the C code? If so, relocate_code (which would then become a misnomer) should have its own relocate and fixup code removed (your patch does not), and only perform the C environment (stack, etc) switch.

Incorrect. I removed the relocation (#ifndef) in start.S leaving only setting of the SP and calling of board_init_r active. True for the misnomer... It was supposed to be a quick hack, beauty comes later ;)

Quoted 6 lines

Side note: if board_init_f handles relocation and fixup and relocate_code just does stack switch between board_init_f and board_init_r, then I'd rather board_init_f *return* to start.S rather than *call* it if that's possible. That would make the control flow less
circuitous: begin in start.S, set a minimal stack, call board_init_f, returns to start.S, switch to final stack, calls (branches to) board_init_r.

I agree here. I would have board_init_f return the desired SP value, for example, or let it return the gd pointer - something along that line.

Or one completely skips returning back to asm and sets the sp with an asm() statement in "C", obtains a function pointer to board_init_r, adjusts that and calls that function.

Amicalement,

Reinhard

#4 lore raw

PATCH 1/1 futile c relocation attempt

Le 05/10/2010 19:41, Reinhard Meyer a ?crit :

Quoted 53 lines

Dear Albert ARIBAUD,

Quoted 1 line
Quoted 1 line

include/configs/top9000_9xe.h | 1 +

That's my board. Not in mainstream yet. Just sets the define there.

Quoted 40 lines
Quoted 40 lines

+#ifdef CONFIG_USE_C_RELOCATION
+ /* TODO: check for identical source and destination */
+ /* TODO: check for overlapping */
+ /* copy image, including initialized data */
+ debug ("memcpy(%08lx,%08lx,%ld)\n",
+ addr, _TEXT_BASE, _bss_start_ofs);
+ memcpy ((void *)addr, (void *)_TEXT_BASE, _bss_start_ofs);
+ /* now fix the code */
+ debug ("_dynsym_start_ofs=%08lx _rel_dyn_start_ofs=%08lx _rel_dyn_end_ofs=%08lx\n",
+ _dynsym_start_ofs, _rel_dyn_start_ofs, _rel_dyn_end_ofs);
+ for (rel_dyn_ptr = (Elf32_Rel *)(_TEXT_BASE + _rel_dyn_start_ofs);
+ rel_dyn_ptr< (Elf32_Rel *)(_TEXT_BASE + _rel_dyn_end_ofs);
+ rel_dyn_ptr++) {
+ ulong *patchaddr = (ulong *) (rel_dyn_ptr->r_offset + gd->reloc_off);
+ debug ("patch %08lx : %08lx\n",
+ (ulong)patchaddr, (ulong)rel_dyn_ptr->r_info);
+ switch (ELF32_R_TYPE(rel_dyn_ptr->r_info)) {
+ case 23: /* rel fixup */
+ *patchaddr += gd->reloc_off;
+ break;
+ case 2: /* abs fixup */
+ {
+ Elf32_Sym *sym = (Elf32_Sym *)(_TEXT_BASE + _dynsym_start_ofs);
+ sym += ELF32_R_SYM(rel_dyn_ptr->r_info);
+ *patchaddr = gd->reloc_off + sym->st_value;
+ }
+ break;
+ default: /* unhandled fixup */
+ break;
+ }
+ }
+ /* clear BSS */
+# ifndef CONFIG_PRELOADER
+ debug ("clearing BSS %08lx..%08lx\n",
+ addr + _bss_start_ofs, addr + _bss_end_ofs);
+ for (p = (ulong *)(addr + _bss_start_ofs);
+ p< (ulong *)(addr + _bss_end_ofs);
+ *p++ = 0)
+ ;

Quoted 8 lines

arch/arm/lib/board.c void board_init_f (ulong bootflag) · line 717

Quoted 2 lines
arch/arm/lib/board.c +2 -0
+# endif
+#endif

All of _bss_start_ofs, _rel_dyn_start_ofs, _rel_dyn_end_ofs, _dynsym_start_ofs and _bss_end_ofs are going to be modified by the relocation fixup loop. The compiler *might* preload them into registers... or not; which could cause issues with ..._end_ofs ones, because it could cause the loops to overflow/underflow.

Quoted 1 line

They are not going to be modified, they are in the unrelocated location!

Oops. Correct.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

+ debug ("calling relocate_code\n");
relocate_code (addr_sp, id, addr);

Have you not just relocated and fixed up in the C code? If so, relocate_code (which would then become a misnomer) should have its own relocate and fixup code removed (your patch does not), and only perform the C environment (stack, etc) switch.

Quoted 4 lines

Incorrect. I removed the relocation (#ifndef) in start.S leaving only setting of the SP and calling of board_init_r active. True for the misnomer...
It was supposed to be a quick hack, beauty comes later ;)

Ok.

Quoted 8 lines
Quoted 7 lines

Side note: if board_init_f handles relocation and fixup and relocate_code just does stack switch between board_init_f and board_init_r, then I'd rather board_init_f *return* to start.S rather than *call* it if that's possible. That would make the control flow less
circuitous: begin in start.S, set a minimal stack, call board_init_f, returns to start.S, switch to final stack, calls (branches to) board_init_r.

Quoted 6 lines

I agree here. I would have board_init_f return the desired SP value, for example, or let it return the gd pointer - something along that line.

Or one completely skips returning back to asm and sets the sp with an asm() statement in "C", obtains a function pointer to board_init_r, adjusts that and calls that function.

I'd rather not leave asm statements in board.c as it is supposed to converge between archs IIUC.

Quoted 1 line

Amicalement,

:)

Quoted 1 line

Reinhard

Amicalement,

Signature · 1 line
Albert.
#5 lore raw

PATCH 1/1 futile c relocation attempt

In reply to Reinhard Meyer
Quoted 6 lines · Reinhard Meyer
---
 arch/arm/cpu/arm926ejs/start.S |    8 ++++-
 arch/arm/lib/board.c           |   57 +++++++++++++++++++++++++++++++++++++++-
 include/configs/top9000_9xe.h  |    1 +
 3 files changed, 63 insertions(+), 3 deletions(-)

I had a quick look at this and nothing is jumping out at me. Of course I am not familiar with ARM asm...

I don't see any reason why this ultimately will not work eventually. You may be having some issues with the transition from asm->C->asm through the relocation - This was an especially painful thing for me involving an intermediate trampoline which I have only recently figured out how to remove.

Maybe some memory barriers are needed to stop the C optimiser mangling things?

I am sure what you have is very close to the real solution :)

I do think the main relocation fixup loop can be moved into a common library in which case we can add additional case statements. The nice thing is that x86 as all Type 8 which is specifically allocated to x86 so my "if

Quoted 1 line

TEXT_BASE" checks can be kept. For size freaks, we could litter the code

with #ifdefs to remove un-needed cases ;)

Interestingly, ARM is adding gd->reloc_off while x86 is subtracting gd->reloc_off. If this is correct, I need to change the calculation of gd_reloc_off to be consistent

Regards,

Graeme

#6 lore raw

PATCH 1/1 futile c relocation attempt

Dear Graeme Russ,

Quoted 7 lines

I had a quick look at this and nothing is jumping out at me. Of course I am not familiar with ARM asm...

I don't see any reason why this ultimately will not work eventually. You may be having some issues with the transition from asm->C->asm through the relocation - This was an especially painful thing for me involving an intermediate trampoline which I have only recently figured out how to remove.

I think the relocation itself works fine, when looking at the debug output.

Quoted 1 line

Maybe some memory barriers are needed to stop the C optimiser mangling things?

It can't really optimize across the call to the (now wrongly named) reloc_code in assembly.

Quoted 3 lines

Interestingly, ARM is adding gd->reloc_off while x86 is subtracting gd->reloc_off. If this is correct, I need to change the calculation of gd_reloc_off to be consistent

It should always ADD it, when it is calculated as "Final Location" - "Current Location". Note, of course, that the value itself might well be negative.

If, for example, the NOR Flash is at a higher address than RAM...

Best Regards
Reinhard

#7 lore raw

PATCH 1/1 futile c relocation attempt

Quoted 11 lines · Reinhard Meyer

Dear Graeme Russ,

Quoted 7 lines

I had a quick look at this and nothing is jumping out at me. Of course I am not familiar with ARM asm...

I don't see any reason why this ultimately will not work eventually. You may be having some issues with the transition from asm->C->asm through the relocation - This was an especially painful thing for me involving an intermediate trampoline which I have only recently figured out how to remove.

I think the relocation itself works fine, when looking at the debug output.

Time to disassemble using objdump

Quoted 4 lines
Quoted 1 line

Maybe some memory barriers are needed to stop the C optimiser mangling things?

It can't really optimize across the call to the (now wrongly named) reloc_code in assembly.

True, objdump will clarify

Quoted 6 lines
Quoted 4 lines

Interestingly, ARM is adding gd->reloc_off while x86 is subtracting gd->reloc_off. If this is correct, I need to change the calculation of gd_reloc_off to be consistent

It should always ADD it, when it is calculated as "Final Location" - "Current Location".

I have my calculation the wrong way around

Quoted 4 lines

Note, of course, that the value itself might well be negative.

If, for example, the NOR Flash is at a higher address than RAM...

Which it is for x86

Regards

Graeme

#8 lore raw

PATCH 1/1 futile c relocation attempt

In reply to Graeme Russ
Quoted 15 lines · Graeme Russ
Quoted 6 lines · Reinhard Meyer
---
  arch/arm/cpu/arm926ejs/start.S |    8 ++++-
  arch/arm/lib/board.c           |   57 +++++++++++++++++++++++++++++++++++++++-
  include/configs/top9000_9xe.h  |    1 +
  3 files changed, 63 insertions(+), 3 deletions(-)

I had a quick look at this and nothing is jumping out at me. Of course I am not familiar with ARM asm...

I don't see any reason why this ultimately will not work eventually. You may be having some issues with the transition from asm->C->asm through the relocation - This was an especially painful thing for me involving an intermediate trampoline which I have only recently figured out how to remove.

Quoted 7 lines

Maybe some memory barriers are needed to stop the C optimiser mangling things?

I am sure what you have is very close to the real solution :)

I do think the main relocation fixup loop can be moved into a common library in which case we can add additional case statements. The nice thing is that x86 as all Type 8 which is specifically allocated to x86 so my "if

Hi All,
I think that type 8 IS NOT allocated to the 386. For instance,
R_PPC_ADDR14_BRTAKEN also has a value of 8. So does R_ARM_ABS8. I think that there will be a lot of #ifdefs just to keep the references symbolic. Many of the different platform relocation types will come out to be the same code in the switch statement, but different symbolic names. There will also be some entries that are processor specific and have no equivalent on other processors. I think it would be a good idea to use the symbolic values of the Relocation types (as opposed to integer constants), as it will make the code clearer. There are sort of two ways to organize the code inside the switch statement. Since the code inside the switch statement is very short, it might be best for each architecture (ELF format) to be bunched together, even at the expense of repeating the same executable statements that some other formats may use, as follows:

#ifdef PPC
            case R_PPC_ADDR32:  /* S + A */

<code to do the deed>
break;

case R_PPC_RELATIVE: /* B + A */
<code to do the deed>

               break;
#endif
#ifdef I386
            case R_386_32:  /* S + A */ /* I think this is the other 
location type Graeme used, but I could be wrong */
<code to do the deed>
               break;

            case R_386_RELATIVE:  /* B + A */
<code to do the deed>
               break;

#endif
#ifdef ARM
case R_ARM_ABS32: /* S + A */ /* I don't remember the ARM
relocation types used */
<code to do the deed>

               break;

            case R_ARM_REL32:  /* B + A  - P */
<code to do the deed>
               break;

#endif

Or we could group the various Relocation types by what they actually do:

#ifdef PPC
            case R_PPC_ADDR32:  /* S + A */
#endif
#ifdef I386
            case R_386_32:  /* S + A */ /* I think this is the other 

location type Graeme used, but I could be wrong */
#endif
#ifdef ARM
case R_ARM_ABS32: /* S + A */ /* I don't remember the ARM
relocation types used */
#endif
<code to do the deed>

               break;

#ifdef PPC
case R_PPC_RELATIVE: /* B + A */
#endif

  #ifdef I386
             case R_386_RELATIVE:  /* B + A */
#endif
<code to do the deed>
               break;
#ifdef ARM
            case R_ARM_REL32:  /* B + A  - P */
<code to do the deed>
               break;

#endif

Note that the ARM_REL32 is defined differently than the PPC/I386 relative, FWIW. I also don't know what to use for the names of the binary formats. It would be nice if we could use something already in the header files? Thoughts on all this solicited!

Best Regards,
Bill Campbell

Quoted 6 lines
Quoted 1 line

TEXT_BASE" checks can be kept. For size freaks, we could litter the code

with #ifdefs to remove un-needed cases ;)

Interestingly, ARM is adding gd->reloc_off while x86 is subtracting gd->reloc_off. If this is correct, I need to change the calculation of gd_reloc_off to be consistent

Adding is the way the specifications define it. add B+A.

Best Regards,
Bill Campbell

Quoted 9 lines

Regards,

Graeme
_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot