Bug 28841 - ld: Customize output section type
Summary: ld: Customize output section type
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: 2.39
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-29 08:12 UTC by Fangrui Song
Modified: 2023-05-25 07:59 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2022-01-29 08:12:59 UTC
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.
Comment 1 Fangrui Song 2022-02-02 19:03:57 UTC
(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
Comment 2 Sourceware Commits 2022-02-16 17:41:52 UTC
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.
Comment 3 Sourceware Commits 2022-02-21 23:04:34 UTC
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.
Comment 4 Alan Modra 2023-05-25 07:59:15 UTC
Fixed for 2.39