Search code examples
gccprogram-entry-pointldlinker-scriptsobjcopy

GCC: how to tell GCC to put the 'main' function at the start of the .text section?


I've just started learning some ARM programming and I've got stuck in a slightly annoying problem. The toolchain I'm using to compile my sources is Sourcery CodeBench Lite 2013.05-23 (can be found here: https://sourcery.mentor.com/GNUToolchain/release2449)

What I would need is to tell GCC or LD or OBJCOPY to put the compiled bytecode of the 'main' function at the beginning of the .text section.

Is there any way to achieve this? (maybe through a linker script?)

Thank you


Solution

  • Solved the problem. For whoever faces it:

    • When compiling with GCC, add the -ffunction-sections option in the command-line. This will tell GCC to put each function in a separate section. The format of the section name will be .text.#function name#, without the # (that is, if the function belongs to the .text section [ which by default is true ]).
    • Secondly, use a linker script to order these "function-sections" into the final big .text section. As an example, putting the main function at the beginning of the .text section would result in an LD script that looks approximately like this:

      ENTRY(main)
      SECTIONS
      {
          .text :
          {
              *(.text.main);
              *(.text*);
          }
      }