Search code examples
gcclinkerosdevlinker-scriptslinker-flags

How can i get LD to put always put the entry point at the location of -Ttext?


I'm writing my own operating system (static addresses) and I struggle to get the linker to always put my _start function at my desired location within the processes. I specify the location with -Ttext 0x10000 in my build file (Lets just say 0x10000 for this example).

Normally this works, but when i use -O2 the linker puts my main function on this address instead.

So how can i make sure that it is _start that ends up on this address? And is it possible without writing linker scripts?

The function _start is common for all processes and should enforce a nice exit for the scheduler in cases where the program returns instead of calling the exit(). I have a workaround solution in my head but I would preferred to get this working with the linker instead.


Solution

  • So i found 2 solutions.

    1. Set .section .text.startup in the file containing _start
    2. Set .section .text.mustbefirst (my own section name) in the file containing _start

    In the first variant i just make sure that the start code also is included with the same order as the code containing main, although there are some ambiguity this did work in my case.

    In the second variant i modified the default linker script to make sure my section symbol was first.

    If the first is reliable (eg. depends on order of arguments or something) then this is OK. As an aside; does anybody know? If not then i would recommend a new section symbol and modified linker script.