Output section type (READONLY)

Fangrui Song i@maskray.me
Wed Feb 2 07:29:12 GMT 2022


Hi Luca and Nick,

On 2022-02-01, Luca Boccassi wrote:
>> > For READONLY, I am thinking more in line with "whether we need this
>> > feature at all"... If no input has SHF_WRITE, the output naturally
>> does
>> > not have SHF_WRITE; if an input has SHF_WRITE, chancing the output
>> to
>> > non-SHF_WRITE is weird and error-prone.
>>
>> I do not know if this counts as a *justified* use of READONLY, but it
>> was
>> used recently in an attempt to add a new feature to Fedora rawhide:
>>
>>
>> https://fedoraproject.org/wiki/Changes/Package_information_on_ELF_objects#New_system:_.note.package
>>
>> The feature used a custom linker script fragment to add a note
>> section
>> into the executable being created, and this script used the READONLY
>> keyword:
>>
>>    SECTIONS
>>    {
>>      .note.package (READONLY) : ALIGN(4) {
>>          BYTE(0x04) BYTE(0x00) BYTE(0x00) BYTE(0x00) /* Length of
>> Owner including NUL */
>>   [...]
>>      }
>>    }
>>    INSERT AFTER .note.gnu.build-id;

Yes, I recently learned that Fedora started to use (READONLY).
The usage feels strange to me as .note.package should not have the
SHF_WRITE flag, even without READONLY....

In https://sourceware.org/bugzilla/show_bug.cgi?id=26378#c11 , Alan
changed such sections to be read-only by default in 2020 to support a
Linux use case. I do not understand why a new keyword has to be
invented...

The next thing I feel uncomfortable with is the abuse of .note* indicates
a SHT_NOTE section. The type, in my opinion, should be explicitly
specified. In ELF, attributes are identified as well known integers.
The magic section prefix ".note" is contrary to this.

I shall add a note that gold doesn't magically set an output section .note*
to SHT_NOTE and this behavior makes a lot of sense to me.

To allow forward progress, I created
https://sourceware.org/pipermail/binutils/2022-February/119591.html
(ld: Support customized output section type) and I'd recommend that the
linker script fragment

   .note.package (READONLY) : ALIGN(4) {

switches to

   .note.package (TYPE=SHT_NOTE) : ALIGN(4) {

>> This turned out to be problematic however as gold does not support
>> the
>> READONLY attribute, nor the INSERT AFTER directive...

This is the reason that I really recommend a synthesized .o file,
instead of a synthesized output section description using data
commands...


>Yes, we are aware of the issue with ld.gold, and are thinking about
>what to do there. The main problem is that even without those
>unsupported attributes, ld.gold generates broken ELFs that crash
>immediately. For now, we just suggest to just use bfd whenever
>possible, and skip this feature otherwise with a simple flag. It's not
>great to lose debuggability and useful information, but it doesn't need
>to have 100% coverage to provide utility, it's perfectly ok to miss
>some packages.

>> Anyway I think that the point here was that was no input section, so
>> the author needed another way to tell the linker that the output
>> section
>> should be readonly.
>
>Yes, this is precisely the use case, and the attribute is working as
>intended - not just in Fedora, but in CBL-Mariner too, and other
>internal Yocto-based use cases.
>
>--
>Kind regards,
>Luca Boccassi


More information about the Binutils mailing list