Need example for linker script with overlays - Page 1
Forum section: electronics / general

author: Postal2
author_rank: Frequent Contributor
timestamp: July 05, 2024, 04:24:35 pm
content: I hope not everyone programs only blink programs. I need to create some piece of program as an overlay, which is in ROM at startup, then loads into RAM and the program running in ROM calls functions from RAM.

author: brucehoult
author_rank: Super Contributor
timestamp: July 05, 2024, 04:48:36 pm
content: That's not an overlay. That's just a program section that lives in RAM, but is copied there from ROM at startup, just like the DATA section in every C program ever (initialised global variables).Code:.itim :{*(.itim)*(.itim*)} >ITIM AT> ROM... where you have previously defined ITIM in the memory section with appropriate start address and size.Then you just mark whatever functions you want with__attribute__(section("section name"))Calls between ROM and ITIM and back just automagically work.Complete example is included in e.g.https://github.com/five-embeddev/riscv-scratchpad/blob/master/baremetal-startup-c/src/linker.lds

author: Postal2
author_rank: Frequent Contributor
timestamp: July 05, 2024, 05:22:04 pm
content: That is what I need. Thanks a lot. This is for Hitachi mcu with external rom, where I need to remove the external rom chip on the fly.

author: brucehoult
author_rank: Super Contributor
timestamp: July 06, 2024, 01:28:39 am
content: Quote from: Postal2 on July 05, 2024, 05:22:04 pmOh well, in that case just go through the entire linker script and change EVERY ">ROM" to ">some_RAM_area AT>ROM". Except the startup code itself...

author: Postal2
author_rank: Frequent Contributor
timestamp: July 06, 2024, 02:18:29 am
content: I write the downloadable part into a separate C file. So I can send the entire object file to the specified section. I still need to write a bufferless xmodem there.

author: Postal2
author_rank: Frequent Contributor
timestamp: July 08, 2024, 03:56:31 am
content: Working.Code:OUTPUT_ARCH(sh)ENTRY(_start)MEMORY{itim (rwx) : o = 0xFFFFD000, l = 4kram (rw) : o = 0xFFFFE000, l = 8krom (rx) : o = 0x00000000, l = 256k}SECTIONS{.vects 0x00000000:{_vects = .;*(.vects);} > rom.text 0x00000400 :{CREATE_OBJECT_SYMBOLS*(EXCLUDE_FILE (*sci_mon.o) .text)etext = .;} > rom.init :{*(.init)} > rom.fini :{*(.fini)} > rom.got :{*(.got)*(.got.plt)} > rom.rodata :{*(.rodata)*(.rodata.*)_erodata = .;} > rom_itim_start = .;.itim :{_sciexec = .;sci_mon.o(.text)_esciexec = .;} > itim AT>rom.eh_frame_hdr :{*(.eh_frame_hdr)}> rom.eh_frame :{*(.eh_frame)}> rom.jcr :{*(.jcr)} > rom.tors :{__CTOR_LIST__ = .;LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)___ctors = . ;*(.ctors)___ctors_end = . ;LONG(0)__CTOR_END__ = .;__DTOR_LIST__ = .;LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)___dtors = . ;*(.dtors)___dtors_end = . ;LONG(0)__DTOR_END__ = .;_mdata = .;} > rom.data : AT (_mdata) {_data = .;*(.data)_edata = .;} > ram.gcc_exc :{*(.gcc_exc)} > ram.bss :{_bss = . ;*(.bss)*(COMMON)_ebss = . ;_end = . ;} > ram.stack 0xFFFFFC00 :{_stack = .;*(.stack)} >ram.stab . (NOLOAD) :{[ .stab ]}.stabstr . (NOLOAD) :{[ .stabstr ]}/DISCARD/ :{*(.comment)}}

author: brucehoult
author_rank: Super Contributor
timestamp: July 08, 2024, 04:09:12 am
content: At a glance, that looks to me that it will work on initial download, but not if you power the board off and back on again.

author: Postal2
author_rank: Frequent Contributor
timestamp: July 08, 2024, 04:14:10 am
content: It's working right now, in hardware.I checked with "objdump -t a.out" before.Start.sCode:!/****************************************************************!KPIT Infosystems Ltd, Pune, India. - 04-Dec-2001.!!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.!!*****************************************************************/! Start.s.list.section .text.global _start ! global Start routine.extern _main.extern _hw_initialise ! external Sub-routine to initialise Hardware.extern _data.extern _mdata.extern _edata.extern _stack.extern _vects_start:! initialise the SP for non-vectored codemov.lstack,r15! call the hardware initialisermov.lhw_initialise,r1jsr @r1nop! initialise sectionsmov.ledata,r1! edata in r1mov.lmdata,r2! mdata in r2mov.ldata,r0! data in r0cmp/eq r0,r1btstart_1nopstart_l:mov.b @r2,r3 !get from srcmov.b r3,@r0 !place in destadd#1,r2 !inc srcadd#1,r0 !inc destcmp/eq r0,r1 !dest == edata?bfstart_lnopstart_1:! initialise itimmov.lesciexec,r1! esciexec in r1mov.litim_start,r2! itim_start in r2mov.lsciexec,r0! sciexec in r0start_2:mov.l @r2,r3 !get from srcmov.l r3,@r0 !place in destadd#4,r2 !inc srcadd#4,r0 !inc destcmp/hi r0,r1 !dest == esciexec?btstart_2nopmov.lmain,r1jsr @r1nop.ALIGN 4hw_initialise:.long_hw_initialisestack:.long_stackdata:.long_datamdata:.long_mdataedata:.long_edatamain:.long_mainsciexec:.long_sciexecesciexec:.long_esciexecitim_start:.long_itim_start

author: Postal2
author_rank: Frequent Contributor
timestamp: July 10, 2024, 04:32:58 am
content: Reads internal firmware perfectly. If anyone is interested, I'll post the source code.

author: peter-h
author_rank: Super Contributor
timestamp: July 10, 2024, 06:33:45 am
content: Do a search here for e.g. a boot loader. I did this and posted a load of code. But it was STM, not Hitachi.

author: Postal2
author_rank: Frequent Contributor
timestamp: July 11, 2024, 07:27:00 am
content: Quote from: peter-h on July 10, 2024, 06:33:45 amOkay.I’ll try to come up with a trick and insert an overlay SH7047 flashing, 4kb RAM is definitely not enough, but the result will look beautiful. I understand about FDT, but if everything is already connected, why not write it at once.

author: Postal2
author_rank: Frequent Contributor
timestamp: July 15, 2024, 10:22:18 pm
content: Loading of vbr-register:Code:void vbr_set(unsigned int address){unsigned int myvar = address;__asm__("ldc r1,vbr;");}Interrupt handling:Code:void ram_int(){//reset errors?__asm__("mov r14,r15;" "mov.l @r15+,r14;" "rte;" "nop;");}