How can I force the entry-point to be at start of .text?

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 :)

9 points · 4 comments · view on lemmy.world

4 Comments

SpaceNoodle@lemmy.world · 3 pts · 210d (3 replies)

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?

iliketurtiles@programming.dev · 1 pts · 210d (2 replies)

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.

SpaceNoodle@lemmy.world · 1 pts · 210d (1 reply)

That's generally how it's done.

iliketurtiles@programming.dev · 2 pts · 210d

Oh I see, many thanks.