Author Topic: Need example for linker script with overlays  (Read 829 times)

0 Members and 1 Guest are viewing this topic.

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Need example for linker script with overlays
« on: July 05, 2024, 04:24:35 pm »
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.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4218
  • Country: nz
Re: Need example for linker script with overlays
« Reply #1 on: July 05, 2024, 04:48:36 pm »
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: [Select]
  .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
 
The following users thanked this post: SiliconWizard, Postal2

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #2 on: July 05, 2024, 05:22:04 pm »
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.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4218
  • Country: nz
Re: Need example for linker script with overlays
« Reply #3 on: July 06, 2024, 01:28:39 am »
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.

Oh 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...
 

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #4 on: July 06, 2024, 02:18:29 am »
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.
 

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #5 on: July 08, 2024, 03:56:31 am »
Working.
Code: [Select]
OUTPUT_ARCH(sh)
 ENTRY(_start)
 MEMORY
 {
   itim (rwx) : o = 0xFFFFD000, l = 4k
   ram (rw) : o = 0xFFFFE000, l = 8k
   rom (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)
 }
}
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4218
  • Country: nz
Re: Need example for linker script with overlays
« Reply #6 on: July 08, 2024, 04:09:12 am »
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.
 

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #7 on: July 08, 2024, 04:14:10 am »
It's working right now, in hardware.
I checked with "objdump -t a.out" before.

Start.s
Code: [Select]
!/****************************************************************
!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 code
mov.l stack,r15
! call the hardware initialiser
mov.l hw_initialise,r1
jsr     @r1
nop
! initialise sections
mov.l edata,r1 ! edata in r1
mov.l mdata,r2 ! mdata in r2
mov.l data,r0         ! data in r0
cmp/eq  r0,r1
bt start_1
nop
start_l:
mov.b   @r2,r3  !get from src
mov.b   r3,@r0  !place in dest
add    #1,r2    !inc src
add    #1,r0    !inc dest
cmp/eq  r0,r1   !dest == edata?
bf start_l
nop
start_1:
! initialise itim
mov.l esciexec,r1 ! esciexec in r1
mov.l itim_start,r2 ! itim_start in r2
mov.l sciexec,r0 ! sciexec in r0
start_2:
mov.l   @r2,r3  !get from src
mov.l   r3,@r0  !place in dest
add    #4,r2    !inc src
add    #4,r0    !inc dest
cmp/hi  r0,r1   !dest == esciexec?
bt start_2
nop

mov.l main,r1
jsr     @r1
nop

.ALIGN 4
hw_initialise:
.long _hw_initialise
stack:
.long _stack
data:
.long _data
mdata:
.long _mdata
edata:
.long _edata
main:
.long _main
sciexec:
.long _sciexec
esciexec:
.long _esciexec
itim_start:
.long _itim_start
« Last Edit: July 08, 2024, 05:47:12 am by Postal2 »
 

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #8 on: July 10, 2024, 04:32:58 am »
Reads internal firmware perfectly. If anyone is interested, I'll post the source code.
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3776
  • Country: gb
  • Doing electronics since the 1960s...
Re: Need example for linker script with overlays
« Reply #9 on: July 10, 2024, 06:33:45 am »
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.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #10 on: July 11, 2024, 07:27:00 am »
... I did this ...
Okay.

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.
 

Online Postal2Topic starter

  • Regular Contributor
  • *
  • Posts: 189
  • Country: ru
Re: Need example for linker script with overlays
« Reply #11 on: July 15, 2024, 10:22:18 pm »
Loading of vbr-register:
Code: [Select]
void vbr_set(unsigned int address)
{
unsigned int myvar = address;
__asm__("ldc r1,vbr;");

}

Interrupt handling:
Code: [Select]
void ram_int()
{
//reset errors?

__asm__("mov r14,r15;" "mov.l @r15+,r14;" "rte;" "nop;");
}
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf