How to add read-only section via linker script

Fangrui Song i@maskray.me
Wed May 12 05:09:20 GMT 2021


On Mon, May 10, 2021 at 7:49 AM Luca Boccassi <bluca@debian.org> wrote:
>
> On Mon, 2021-05-10 at 14:16 +0100, Nick Clifton wrote:
> > Hi Luca,
> >
> > > Is there a way to make it read-only, _without_ having to compile some
> > > object code and link it, but exclusively using the linker script?
> >
> > Short answer: no.
> >
> > There are a couple of possible solutions, but I doubt that they would
> > be acceptable:
> >
> >    * You could define a read-only segment and then assign the
> >      section to this segment.
> >
> >    * Similarly you define a read-only memory region and assign
> >      the section to that.
> >
> > There is one other possible solution.  You can specify the "type"
> > of an output section in the linker script.  Currently they can
> > only be used to make the section non-loadable or non-allocateable.
> > But you could always submit a patch to extend the linker to add
> > a READONLY type, and then use that...
> >
> > Cheers
> >    Nick
>
> Thanks for the explanation!
>
> I've put together and tested a patch as suggested, seems to work fine.
> Please have a look when you have a moment.
>
> https://sourceware.org/pipermail/binutils/2021-May/116567.html
>

Shouldn't https://sourceware.org/bugzilla/show_bug.cgi?id=26378 have
made it possible to create a section (consiting of only
BYTE/SHORT/LONG/QUAD) without the SHF_WRITE flag?

> Thank you!
>
> --
> Kind regards,
> Luca Boccassi


More information about the Binutils mailing list