On ELF platforms, an output section is SHT_PROGBITS by default. https://sourceware.org/binutils/docs/ld/Output-Section-Type.html#Output-Section-Type allows one to create a SHT_NOBITS type section. It's useful to support an arbitrary type. Then the user can create an output section with just data commands. Currently there is some magic (bfd/elf.c special_sections_n recognizes `.note*` as SHT_NOTE) which goes against the ELF spirit that attributes should be identified as well known integers, not magic names. % cat a.lds SECTIONS { .noload (NOLOAD) : { BYTE(0) } .info (INFO) : { BYTE(0) } .note.package (SHT_NOTE) : { BYTE(0) } /* or .note.package (0x7) : { BYTE(0) } */ .init_array (SHT_INIT_ARRAY) : { ... } } INSERT AFTER .note.gnu.build-id; % as /dev/null -o a.o % ld.bfd -T a.lds --build-id a.o -o a It may be useful to customize sh_flags as well.
(I wrote `.note.package (SHT_NOTE)` in a hurry. It's better to introduce a keyword) The current proposal is TYPE=<value>, +@item TYPE = @var{type} +Set the section type to the integer @var{type}. When generating an ELF +output file, type names @code{SHT_PROGBITS}, @code{SHT_STRTAB}, +@code{SHT_NOTE}, @code{SHT_INIT_ARRAY}, @code{SHT_FINI_ARRAY}, and +@code{SHT_PREINIT_ARRAY} are also allowed for @var{type}. It is the +user's responsibility to ensure that any special requirements of the +section type are met. Examples: SECTIONS { progbits (TYPE=SHT_PROGBITS) : { BYTE(1) } strtab (TYPE = SHT_STRTAB) : { BYTE(0) } note (TYPE =SHT_NOTE) : { BYTE(8) } init_array (TYPE= 14) : { QUAD(14) } fini_array ( TYPE=SHT_FINI_ARRAY) : { QUAD(15) } preinit_array (TYPE=SHT_PREINIT_ARRAY ) : { QUAD(16) } } Discussion: https://sourceware.org/pipermail/binutils/2022-February/119600.html
The master branch has been updated by Nick Clifton <nickc@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c212f39d9a82c6c09f4a1447d9d2ff09843827c5 commit c212f39d9a82c6c09f4a1447d9d2ff09843827c5 Author: Fangrui Song <maskray@google.com> Date: Wed Feb 16 17:41:23 2022 +0000 ld: Support customized output section type bfd/ PR ld/28841 * bfd-in2.h (struct bfd_section): Add type. (discarded_section): Add field. * elf.c (elf_fake_sections): Handle bfd_section::type. * section.c (BFD_FAKE_SECTION): Add field. * mri.c (mri_draw_tree): Update function call. ld/ PR ld/28841 * ld.texi: Document new output section type. * ldlex.l: Add new token TYPE. * ldgram.y: Handle TYPE=exp. * ldlang.h: Add type_section to list of section types. * ldlang.c (lang_add_section): Handle type_section. (map_input_to_output_sections): Handle type_section. * testsuite/ld-scripts/output-section-types.t: Add tests. * testsuite/ld-scripts/output-section-types.d: Update.
The master branch has been updated by Alan Modra <amodra@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=cf53a97bedd257aa6612129e4bdf8618f6a038cd commit cf53a97bedd257aa6612129e4bdf8618f6a038cd Author: Alan Modra <amodra@gmail.com> Date: Tue Feb 22 09:20:31 2022 +1030 Re: ld: Support customized output section type "DO NOT EDIT!" says the comment at the top of bfd-in2.h. Move the new type field where it belongs. PR ld/28841 * section.c (struct bfd_section): Add type. Formatting. (BFD_FAKE_SECTION): Formatting. * bfd-in2.h: Regenerate.
Fixed for 2.39