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

9 months ago by iliketurtiles to c/c_lang

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 3 points 9 months 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?

path: 0 21633936, hotness: undefined, score: 3, children: 3
iliketurtiles 1 point 9 months 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.

path: 0 21633936 21634114, hotness: undefined, score: 1, children: 2
SpaceNoodle 1 point 9 months ago

That's generally how it's done.

path: 0 21633936 21634114 21634373, hotness: undefined, score: 1, children: 1
iliketurtiles 2 points 9 months ago

Oh I see, many thanks.

path: 0 21633936 21634114 21634373 21635465, hotness: undefined, score: 2, children: 0
c_lang
c_lang

@programming.dev

login for more options
1357
103
28

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

Ā© Dennis Ritchie

🌐 https://en.cppreference.com/w/c

go to feed...