Hi, I'd like to propose a new ELF section flag, SHF_GNU_RETAIN, for addition to the GNU gABI. This flag instructs the linker to "retain" the section in the output file, even if garbage collection would remove it because it appears unused. The intention is to be able to apply the "retain" attribute to declarations of functions and data in the source code, and have the SHF_GNU_RETAIN flag be applied to the section containing the declaration. ====================================================== Section Attribute Flags +-------------------------------------+ | Name | Value | +-------------------------------------+ | SHF_GNU_RETAIN | 0x200000 (1 << 21) | +-------------------------------------+ SHF_GNU_RETAIN The section should not be garbage collected by the linker, even if it appears unused. ====================================================== ---------- Motivation ---------- Linker garbage collection is a useful feature to help reduce the size of linked output file, where sections are removed if they exclusively contain definitions of functions or data that are never referenced by other parts of the application. Currently, the KEEP linker script directive can be used to save a section from garbage collection. The new SHF_GNU_RETAIN flag has the same effect as KEEP, but since it is an ELF section flag, it means that this property can be propagated through the toolchain, from the compiler, through the assembler, to the linker. This enables a new "retain" attribute to be set on function and data declarations in the source code, which communicates the need to set the SHF_GNU_RETAIN flag on the containing section to downstream tools. In some situations, this has benefits over applying the KEEP directive in the linker script: - The requirement for protection from garbage collection might be application-specific, so consolidating this requirement to be contained entirely within the source code improves portability. - The user doesn't have to work out which section a function or data object declaration will be placed in, and then set the KEEP directive on that section. They can just set the attribute in the source code and leave the rest for the toolchain to handle. - Generally avoiding linker script modifications can improve the user experience, especially when the user is inexperienced with the linker script format. The boilerplate linker script code required for standard application operation can make modifications error-prone and have unintended side-effects. -------------- Implementation -------------- GCC supports a new "retain" attribute, which can be set on any declaration of function or data which could have a section. This means it can't be set on an automatic variable, for example. GAS supports a new ".retain [section name]" directive, which is used to apply SHF_GNU_RETAIN to the section named "section name". If "section name" is ommitted, SHF_GNU_RETAIN is applied to the current section the directive resides in. Alternatively, the "R" flag is recognized by the "flags" argument to the .section directive and will apply SHF_GNU_RETAIN to that section. It is intended that SHF_GNU_RETAIN does not interfere with any validation when switching to a section. It can be used to augment the section flags in a section which has already been created. When garbage collection is enabled, LD marks sections which have the SHF_GNU_RETAIN flag to save them from garbage collection. The underyling behavior is the same as if the section had the KEEP linker script directive applied. The user can always override SHF_GNU_RETAIN by placing the section in /DISCARD/. For reference, I've attached GCC and Binutils patches which implement the attribute. These are not "production ready", as there are some further tweaks to make, and more testing is required, but the functionality is working and the new tests are passing on msp430-elf, arm-eabi and x86_64-pc-linux-gnu. If this proposal is suitable for the GNU gABI, I will further improve and test the implementation, and submit the patches upstream soon. Thanks, and I look forward to hearing your thoughts. Jozef