this post was submitted on 18 Jan 2026
1 points (100.0% liked)

C Programming Language

1286 readers
2 users here now

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

founded 2 years ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] iliketurtiles@programming.dev 0 points 3 months ago (1 children)

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 0 points 3 months ago (1 children)

That's generally how it's done.

Oh I see, many thanks.