Output section type (READONLY)

Fangrui Song i@maskray.me
Sat Jan 29 07:45:45 GMT 2022


[PATCH v2] ld: add READONLY attribute for SECTIONS
(https://sourceware.org/pipermail/binutils/2021-July/117492.html) added
READONLY. There was no justification as to why it was added.

It was presumably to make an output section non-writable by default.
But for

   % cat output-section-types.t
   SECTIONS {
     .rom  (NOLOAD)   : { LONG(1234); }
     .ro (READONLY)   : { LONG(5678); }
     .over (OVERLAY)  : { LONG(0123); }
     /DISCARD/        : { *(*) }
   }

.ro does not have the SHF_WRITE flag even without (READONLY).
The unneeded SHF_WRITE flag has been fixed by Alan for
https://sourceware.org/bugzilla/show_bug.cgi?id=26378#c11 .

---

Well, READONLY has another behavior that it forces non-SHF_WRITE output
even if an input section has the SHF_WRITE flag.

   % cat output-section-types.t
   SECTIONS {
     .rom  (NOLOAD)   : { LONG(1234); }
     .ro (READONLY)   : { LONG(5678); *(.ro) }
     .over (OVERLAY)  : { LONG(0123); }
     /DISCARD/        : { *(*) }
   }
   % cat x.s
   .section .ro,"aw"
   .quad 0

The output .ro does not have SHF_WRITE. There is no warning.

  [ 2] .ro               PROGBITS        0000000000000004 001004 000004 00   A  0   0  1

I find this quite dangerous if an input section has the SHF_WRITE flag.
I do not see in what circumstances a user may want this behavior.
This just seems very dangerous to use in general.
What went lucky is that
https://sourceware.org/binutils/docs/ld/Output-Section-Type.html does not have
the feature listed yet, so hopefully very few people will be lured to use this.


More information about the Binutils mailing list