# HG changeset patch # User Paul Boddie # Date 1540570924 -7200 # Node ID bb0c949a94e1b4c1783321aea34968e2c95270a1 # Parent 45a99a68c2ed4abc30a2fffbb6f6b7b58a43968b Attempted to initialise writable data by relocating the .data section. Demonstrate initialisation by setting UART echo in the demo example. diff -r 45a99a68c2ed -r bb0c949a94e1 examples/demo/main.c --- a/examples/demo/main.c Thu Oct 25 21:19:01 2018 +0200 +++ b/examples/demo/main.c Fri Oct 26 18:22:04 2018 +0200 @@ -43,7 +43,7 @@ #define CELLSIZE 4 #endif -static int uart_echo; +static int uart_echo = 1; @@ -75,8 +75,6 @@ void main(void) { - uart_echo = 0; - init_memory(); init_pins(); init_outputs(); diff -r 45a99a68c2ed -r bb0c949a94e1 lib/payload.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/payload.c Fri Oct 26 18:22:04 2018 +0200 @@ -0,0 +1,36 @@ +/* + * Payload initialisation functions. + * + * Copyright (C) 2018 Paul Boddie + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +/* Relocate the .data section's contents to initialise mutable structures. */ + +void relocate_data(uint32_t *original, uint32_t *current, uint32_t size) +{ + /* Reference stored content as values. */ + + uint32_t *source, *target; + + /* Copy data from program memory to the relocated data area in RAM. */ + + for (source = original, target = current; source < original + size; + source++, target++) + + *target = *source; +} diff -r 45a99a68c2ed -r bb0c949a94e1 lib/payload.ld --- a/lib/payload.ld Thu Oct 25 21:19:01 2018 +0200 +++ b/lib/payload.ld Fri Oct 26 18:22:04 2018 +0200 @@ -43,7 +43,7 @@ .irqstack : { . += IRQ_STACK_SIZE; - } > kseg0_data_mem AT > physical_data_mem + } > kseg0_data_mem AT > physical_data_mem /* Add other data after the IRQ stack. */ @@ -51,13 +51,24 @@ /* Store constant data in program memory. */ - .data : { *(.data*) } > kseg0_program_mem AT > physical_program_mem .rodata : { *(.rodata*) } > kseg0_program_mem AT > physical_program_mem .got : { _gp = ALIGN(16); *(.got*) } > kseg0_program_mem AT > physical_program_mem + /* Store initialised non-constant data in program memory for relocation. + Employ data memory addresses for generated content. */ + + .data : { + _data = .; + *(.data*) + _data_end = .; + } > kseg0_data_mem AT > physical_program_mem + + _data_original = LOADADDR(.data) - ORIGIN(physical_program_mem) + ORIGIN(kseg0_program_mem); + _data_size = SIZEOF(.data); + /* Device configuration registers to be flashed. */ .devcfg0 : { *(.devcfg0) } > config0 AT > physical_config0 diff -r 45a99a68c2ed -r bb0c949a94e1 lib/start.S --- a/lib/start.S Thu Oct 25 21:19:01 2018 +0200 +++ b/lib/start.S Fri Oct 26 18:22:04 2018 +0200 @@ -44,6 +44,10 @@ .section .boot, "a" .globl _start +.extern relocate_data +.extern _data_original +.extern _data +.extern _data_size .extern main _start: @@ -72,11 +76,29 @@ ori $gp, $gp, %lo(_GLOBAL_OFFSET_TABLE_) /* - Jump to the main program. Since the boot code is separate from the - other code, the address cannot be obtained via the GOT. - ("relocation truncated to fit: R_MIPS_PC16 against `main'") + Relocate mutable data and then jump to the main program. + + Since the boot code is separate from the other code, addresses cannot be + obtained via the GOT: + + "relocation truncated to fit: R_MIPS_PC16 against `main'" */ + lui $a0, %hi(_data_original) + ori $a0, $a0, %lo(_data_original) + + lui $a1, %hi(_data) + ori $a1, $a1, %lo(_data) + + lui $a2, %hi(_data_size) + ori $a2, $a2, %lo(_data_size) + lw $a2, 0($a2) + + lui $t9, %hi(relocate_data) + ori $t9, $t9, %lo(relocate_data) + jal $t9 + nop + lui $t9, %hi(main) ori $t9, $t9, %lo(main) jr $t9 diff -r 45a99a68c2ed -r bb0c949a94e1 mk/common.mk --- a/mk/common.mk Thu Oct 25 21:19:01 2018 +0200 +++ b/mk/common.mk Fri Oct 26 18:22:04 2018 +0200 @@ -48,8 +48,8 @@ # Application-specific files appear after the above but before those below in # the application Makefiles. -COMMON_SRC = $(LIBDIR)/init.c $(LIBDIR)/debug.c $(LIBDIR)/cpu.S -COMMON_OBJ = $(LIBDIR)/init.o $(LIBDIR)/debug.o $(LIBDIR)/cpu.o +COMMON_SRC = $(LIBDIR)/payload.c $(LIBDIR)/init.c $(LIBDIR)/debug.c $(LIBDIR)/cpu.S +COMMON_OBJ = $(LIBDIR)/payload.o $(LIBDIR)/init.o $(LIBDIR)/debug.o $(LIBDIR)/cpu.o DISPLAY_SRC = $(LIBDIR)/display.c $(LIBDIR)/vga_display.c DISPLAY_OBJ = $(LIBDIR)/display.o $(LIBDIR)/vga_display.o