BrockLee
BrockLee

Reputation: 981

Linker script notation for handling unspecified sections

I have an object file that has a bunch of compiler-generated sections I don't care about. When I link together a flat binary using a linker script, I can sometimes see these sections placed at the start or end of my binary. For example, if I have the linker script:

SECTIONS {
  __START = .;
  .text : { *(.text) }  /* I want all .text to go at the very start. */
  .data : { *(.data) }  /* Followed by .data. */
  /* Every other section can go somewhere here. */
  __END = ;.
}

Then it's possible for a section like .interp or .note.gnu.build-id to be placed at the start of the binary when I really want .text to be at the start. I can work around this by placing these sections at the end via:

SECTIONS {
  __START = .;
  .text : { *(.text) }  /* I want all .text to go at the very start. */
  .data : { *(.data) }  /* Followed by .data. */
  /* Every other section can go somewhere here. */
  .interp : { *(.interp) }
  __END = ;.
}

or somehow getting the compiler to just not emit that section at all, but I don't want to manually do this for every section I don't knowingly account for. Is there a way to kinda just "glob" all sections I don't specify at the end of the binary?

Upvotes: 2

Views: 399

Answers (1)

Djzin
Djzin

Reputation: 1268

The syntax /DISCARD/ : { * } (or /DISCARD/ : { *(*) }) discards all otherwise unnamed sections, for example:

SECTIONS {
    section1 : { liba.o(section1a, section1b) }
    section2 : { lib*.o(section2*) }
    /DISCARD/ : { * }
}

In general the basic syntax for a section placement expression is fileglob[(sectionglob, [sectionglobs...])], where the special section name /DISCARD/ will drop the sections listed.

Note that order matters - putting /DISCARD/ : { * } at the beginning of the linker script will discard all sections of all input files, even if they are specified in later sections.

Upvotes: 0

Related Questions