0

Lets say we have w simple C project, which we would like to run in the desktop. In order to generate the executable, we need to preprocess, compile and than link it. Lets assum we are using GCC to do it. We can do these 3 operations in one step:

gcc -o program src/*.c

Why we dont need to specify the linker script here? Maybe there is a default one somewhere, how it looks like? Is the OS handling it for us? Or maybe the linker script is only required for the software, that is going to be flashed into the external embedded system?

What are the default options of the linker, preprocessor and compiler here?

Bratw
  • 141
  • 8
  • "Is the OS handling it for us?" What OS? You tagged this embedded system. The OS you develop on and where you keep the "cross compiler" has literally nothing to do with the embedded system. – Lundin Jun 19 '23 at 06:54
  • Since you tagged `embedded`, you should know that the default linker script may not be the right one in many cases. E.g., think of a dual core STM32 MCU - first core memory layout is default, but code section for the second core starts at a different address, and you must specify it in a linker script. – SK-logic Jun 19 '23 at 08:12

2 Answers2

1

Why we dont need to specify the linker script here?

Because there is a default one, built into every linker.

In fact, linker script support is an implementation detail. A linker could work just fine without any linker script support.

Maybe there is a default one somewhere, how it looks like?

If you are using GNU ld, you can see its builtin linker script using ld --verbose.

When do we need to use linker script

When the default one doesn't satisfy your needs, and you need additional control on where various sections are placed in the final linker ouput.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • This is tagged embedded systems and in embedded systems it is industry standard to always have a default linker script as part of the project. Usually provided by the IDE. – Lundin Jun 19 '23 at 06:53
  • @Lundin that is true, but it is mistagged. The question is about linking a desktop application. It appears the OP has experience if embedded development and is wondering why desktop development needs no linker script – Clifford Jun 19 '23 at 11:54
1

A linker script typically defines the memory resources available on the platform, which for embedded systems is application specific and not known to the toolchain.

Moreover in a bare-metal embedded system, the linker must both link and locate the code at build time, while in a hosted system executable object file, data and code will have segment identifiers, that are used by the operating system loader to locate the code/data appropriately when the program is launched. So in that case the linker need have no knowledge of available memory resources, as that is dealt with by the OS loader.

So in an embedded system, the linker performs both inter-module reference resolution (link) and explicit memory location (locate), and it is the latter that requires knowledge of the memory layout. In a hosted system, the static linker performs the link stage only, and location is handled by the OS (in a modern desktop OS that environment is also normally 'virtualised').

None of that means that in a desktop system there is no linker script. In Linux/gnu on x86 64bit for example you will find /lib/x86_64-linux-gnu/ldscripts, where the actual script applied I believe depends on switches passed to the toolchain on build. (Ref Actual default linker script and settings gcc uses).

Clifford
  • 88,407
  • 13
  • 85
  • 165