Aligning .text segments

Nicholas Clifton nickc@redhat.com
Tue Dec 23 08:46:00 GMT 2014


Hi K Jski,

> foo at 0x40000010
> bar at 0x400000c9
> main in at  0x40000400
>
> so I just would want the following to happen
> foo to be aligned at 0x40000050
> bar to be aligned at 0x40000100
> main stays at 0x40000400
>
> I have an attempt at a linker script (I never worked on linkers in my
> life) that I feed gold as an arg "myscript.x". (source below) what
> happens when gold produces a binary is that it creates another .text
> section and inserts them at the designated addresses.

So, are you saying that the functions get aligned as you wanted, but 
that the program itself does not run ?

> myscript.x
>
> SECTIONS{
>               .text:
>              {
>                 . = ALIGN(0x50);
>                 *(.text.foo)
>                  . = ALIGN(0x50);
>                  *(.text.bar)
>               }
> }

That script should work, assuming that you compiled your code with the 
-ffunction-sections command line option enabled, although you are 
missing the catch-all line to catch the other text sections (eg the one 
containing main):

                .text:
               {
                  . = ALIGN(0x50);
                  *(.text.foo)
                   . = ALIGN(0x50);
                   *(.text.bar)
		  . = ALIGN(0x50);
		  *(.text .text.*)
                }

Also - this script is just a fragment - a full script would also need to 
specify the locations of other sections like .data, .bss and so on. 
Your best bet is to take the linker's builtin default script (shown when 
you run "gold --verbose") and modify that to suit your needs.

Cheers
   Nick



More information about the Binutils mailing list