From 76124b8649c85e4e7218b776733d93d364e28845 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Fri, 15 Sep 2017 19:55:22 +0200 Subject: [PATCH] Removed UB from picosoc demo firmware --- picosoc/firmware.c | 8 ++++++-- picosoc/sections.lds | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/picosoc/firmware.c b/picosoc/firmware.c index 660ef56..ce766fc 100644 --- a/picosoc/firmware.c +++ b/picosoc/firmware.c @@ -1,5 +1,9 @@ #include +// a pointer to this is a null pointer, but the compiler does not +// know that because "sram" is a linker symbol from sections.lds. +extern uint32_t sram; + #define reg_spictrl (*(volatile uint32_t*)0x02000000) #define reg_uart_clkdiv (*(volatile uint32_t*)0x02000004) #define reg_uart_data (*(volatile uint32_t*)0x02000008) @@ -53,12 +57,12 @@ extern uint32_t cmd_read_spi_flash_id_worker_end; void cmd_read_spi_flash_id() { uint32_t *src_ptr = &cmd_read_spi_flash_id_worker_begin; - uint32_t *dst_ptr = (uint32_t*)0; + uint32_t *dst_ptr = &sram; while (src_ptr != &cmd_read_spi_flash_id_worker_end) *(dst_ptr++) = *(src_ptr++); - ((void(*)())0)(); + ((void(*)())&sram)(); } // -------------------------------------------------------- diff --git a/picosoc/sections.lds b/picosoc/sections.lds index 1fe7919..6c59cb8 100644 --- a/picosoc/sections.lds +++ b/picosoc/sections.lds @@ -1,5 +1,6 @@ SECTIONS { .memory : { + sram = 0; . = 0x100000; start*(.text); *(.text);