relocatable reGNUal

This commit is contained in:
NIIBE Yutaka
2013-03-07 11:25:59 +09:00
parent 7d4d6cff5e
commit a4f7386b8a
4 changed files with 60 additions and 6 deletions

View File

@@ -1,3 +1,19 @@
2013-03-08 Niibe Yutaka <gniibe@fsij.org>
Relocatable reGNUal.
* regnual/regnual.ld (MEMORY): 0x1400 was the value of Gnuk 1.0.1.
Keep this value.
(.text): Include .text.entry next to the .vectors.
(.got): New.
* regnual/sys.c (entry): Now, it's at .text.entry section.
Do relocations.
Don't use absolute values which causes relocations, but
access at GOT.
* regnual/Makefile (CFLAGS): Add -fpie.
2013-03-07 Niibe Yutaka <gniibe@fsij.org>
* src/usb_stm32f103.c (handle_setup0): Fix selecting handler.

View File

@@ -22,6 +22,7 @@ MCFLAGS= -mcpu=$(MCU) -mfix-cortex-m3-ldrd
DEFS = -DFREE_STANDING
CFLAGS = -O2 -g
CFLAGS += -Wa,-alms=$(notdir $(<:.c=.lst)) -fpie
CFLAGS += $(CWARN) -I . -I ../src -fno-common $(MCFLAGS) $(TOPT) $(DEFS)
LDFLAGS = -T$(LDSCRIPT) -nostartfiles $(MCFLAGS) $(TOPT)

View File

@@ -39,6 +39,7 @@ SECTIONS
{
_text = .;
KEEP(*(.vectors))
*(.text.entry)
*(.text)
*(.text.*)
*(.rodata)
@@ -48,6 +49,12 @@ SECTIONS
*(.gcc*)
} > ram1
.got :
{
*(.got)
*(.got.*)
} > ram1
.ctors :
{
PROVIDE(_ctors_start_ = .);

View File

@@ -9,17 +9,41 @@ static void none (void)
{
}
/* Note: it is not reset */
static __attribute__ ((naked))
/*
* Note: the address of this routine 'entry' will be in the vectors as
* RESET, but this will be called from application. It's not RESET
* state, then.
*/
static __attribute__ ((naked,section(".text.entry")))
void entry (void)
{
asm volatile ("ldr r0, =__ram_end__\n\t"
"ldr r1, =__main_stack_size__\n\t"
asm volatile ("mov r0, pc\n\t"
"bic r0, r0, #255\n\t" /* R0 := vector_table address */
"mov r1, #0x90\n" /* R1 := numbers of entries * 4 */
"0:\n\t"
"ldr r2, [r0, r1]\n\t"
"add r2, r2, #-0x20000000\n\t"
"sub r2, r2, #0x1400\n\t"
"add r2, r2, r0\n\t" /* Relocate: -0x20001400 + R0 */
"str r2, [r0, r1]\n\t"
"subs r1, r1, #4\n\t"
"bne 0b\n\t"
/* Relocation done. We don't care the first entry. */
"ldr r3, .L00\n"
".LPIC00:\n\t"
"add r3, pc\n\t" /* R3 := @_GLOBAL_OFFSET_TABLE_ */
"ldr r4, .L00+4\n\t"
"ldr r0, [r3, r4]\n\t"
"ldr r4, .L00+8\n\t"
"ldr r1, [r3, r4]\n\t"
"sub r0, r0, r1\n\t"
"mov sp, r0\n\t"
/* Clear BSS. */
"mov r0, #0\n\t"
"ldr r1, =_bss_start\n\t"
"ldr r2, =_bss_end\n"
"ldr r4, .L00+12\n\t"
"ldr r1, [r3, r4]\n\t"
"ldr r4, .L00+16\n\t"
"ldr r2, [r3, r4]\n"
"0:\n\t"
"str r0, [r1], #4\n\t"
"cmp r2, r1\n\t"
@@ -30,6 +54,12 @@ void entry (void)
"bl main\n"
"1:\n\t"
"b 1b\n"
".L00:\n\t"
".word _GLOBAL_OFFSET_TABLE_-(.LPIC00+4)\n\t"
".word __ram_end__(GOT)\n\t"
".word __main_stack_size__(GOT)\n\t"
".word _bss_start(GOT)\n\t"
".word _bss_end(GOT)"
: /* no output */ : /* no input */ : "memory");
}