I’m trying to write a linker file to get C code running on a risc-v MCU (using riscv64-unknown-elf toolchain). My linker script looks like this, simplified:

MEMORY {
        IRAM (rx) : ORIGIN = 0x00000000, LENGTH = 1024
}

ENTRY(_start)

SECTIONS {
    .text : ALIGN(4) {
        *(.text)
    } >IRAM

...

readelf -h correctly shows the entry point’s address to be the same as where _start is in the output executable. But I want _start to be at 0x0, not somewhere else, so that when I objcopy it to a flat binary, the start code will be at the beginning. How can I do this?

Right now I’m having the _start function go in a new section called “.boot” I’ve defined in the C file using __attribute__((section(".boot"))), then, placing this at the start of .text in the linker script like so

...
    .text : ALIGN(4) {
        *(.boot)
        *(.text)
...

But IDK if this how it should be done.

Thanks in advance :)

  • SpaceNoodle@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    9 days ago

    That looks like it should work. Have your linker generate a .map file - where does it show your _start symbol? Is it also showing it in the .boot section?

    • iliketurtilesOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      9 days ago

      I don’t know what a .map file is, will look into that. This does work, when I objdump it _start is at 0x0. But I’m curious if there is a better solution, I’d like to learn how it’s properly done.