[PATCH] D72756: [LLD][ELF] Add support for INPUT_SECTION_FLAGS

Peter Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 15 02:47:02 PST 2020


peter.smith created this revision.
peter.smith added reviewers: MaskRay, grimar, ruiu.
Herald added subscribers: kristof.beyls, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

The INPUT_SECTION_FLAGS linker script command is used to constrain the section pattern matching to sections that match certain combinations of flags.

      

There are two ways to express the constraint.

- withFlags: Section must have these flags.
- withoutFlags: Section must not have these flags.

The syntax of the command is:

  INPUT_SECTION_FLAGS '(' sect_flag_list ')'
  sect_flag_list: NAME
  | sect_flag_list '&' NAME

Where NAME matches a section flag name such as SHF_EXECINSTR, or the integer value of a section flag. If the first character of NAME is ! then it means must not contain flag.
As an example from the ld man page: https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html

  SECTIONS {
    .text : { INPUT_SECTION_FLAGS (SHF_MERGE & SHF_STRINGS) *(.text) }
    .text2 :  { INPUT_SECTION_FLAGS (!SHF_WRITE) *(.text) }
  }

- .text will match sections called .text that have both the SHF_MERGE and SHF_STRINGS flag.
- .text2 will match sections called .text that don't have the SHF_WRITE flag.

The flag names have been taken from ELF.h and is a superset of what ld.bfd accepts. It has a hard-coded list of supported flags. Arguably many of the section flags in ELF.h are unlikely to be used in a linkerscript, there is always the option to use the hex value of the flag.

      

fixes PR44265 https://bugs.llvm.org/show_bug.cgi?id=44265

The GNU ld documentation for this feature is limited to the link above. I found the grammar https://github.com/bminor/binutils-gdb/blob/master/ld/ldgram.y clear enough, particularly sect_flags.

As I see it the main use case for INPUT_SECTION_FLAGS is porting projects from other linkers that primarily match by section flags rather than by name. For example Arm's proprietary linker scatter file notation uses notation like:

  RO 0x8000 { *(+ro) }
  RW +0 { *(+rw, +zi) }

Projects moving to linker scripts have to approximate this by section name and this can be a challenge. The INPUT_SECTION_FLAGS can ease this transition.


https://reviews.llvm.org/D72756

Files:
  lld/ELF/LinkerScript.cpp
  lld/ELF/LinkerScript.h
  lld/ELF/ScriptParser.cpp
  lld/test/ELF/input-section-flags-diag1.test
  lld/test/ELF/input-section-flags-diag2.test
  lld/test/ELF/input-section-flags-diag3.test
  lld/test/ELF/input-section-flags-keep.s
  lld/test/ELF/input-section-flags.s

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72756.238200.patch
Type: text/x-patch
Size: 17005 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200115/9f2f510e/attachment.bin>


More information about the llvm-commits mailing list