Question

I want to specify alignment for some sections. For ld 2.20.0 the following approach works:

  • create a linker script containing only the sections that should be aligned, e.g.:

    SECTIONS
    {
        .data ALIGN(0x40): { *(.data) }
    
        .text ALIGN(0x40): { *(.text) }
    
        .plt ALIGN(0x10): { *(.plt) }
    }
    
  • then link with:

    -rpath-link=/path/to/LinkerScript.ld /path/to/LinkerScript.ld
    

    e.g. include the script in rpath-link and also pass it as one of the object files.


Now, when linking in the same way using gold 2.23.1, the script is rejected with this error:

LinkerScript.ld: SECTIONS seen after other input files; try -T/--script

So gold wants me to specify a complete linker script, however I only want to change a couple attributes. Is this possible?

I have already tried:

  • Passing the script with the -T option. This produces a broken executable that fails to start. As I understand this is because -T only accepts complete linker scripts, while the above is just an "annotation" to the existing script.
  • In the same vein, when passing the script with the -T option to ld 2.20.0 it fails to link since some sections are unspecified. It is not a "complete" linker script for my use case.
  • So I have tried obtaining a complete linker script. ld --verbose will print the default linker script. I had to remove a couple of symbols that were not understood by gold, afterwards the executable produced with the -T/path/to/LinkerScript.ld option didn't work anymore.
  • Unfortunately gold has no option to show its default linker script (it doesn't use any).
  • The man pages do not specify how to change alignment for the case above with a parameter.

Any help would be greatly appreciated!

Était-ce utile?

La solution

I've found a solution that solves my problem.

  • Alignment of data and text sections can be achieved by including an assembler file with dummy alignments. The alignment of a section will be the maximum alignment sepcified anywhere, so at a minimum it will have this alignment.

    Example file align.s (compile with your assembler and include the object file in your link):

    .section .text
    .balign 0x40

    .section .data
    .balign 0x40

    .end
  • The gold linker aligns PLT and GOT entries by their sizes (16 and 8 bytes, respectively). This is sufficient for my use case.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top