Add -N support to ELF?

Ian Lance Taylor
Mon Feb 20 15:48:00 GMT 1995

   From: (H.J. Lu)
   Date: Mon, 20 Feb 95 17:42:10 EST

   I think it is wrong to put the writable section into the .text section.
   It will break any schemes which uses the .text section like

   const char * const foo = "You should die if you modify it.";

>From the linker's point of view, there is nothing wrong with having a
writable .text section.  The current linker will handle it correctly,
in the sense that it will produce a program segment which is marked

   There must be a bug in somewhere else if ld dies. It is not my
   patch's fault.

The bug is that given the standard ELF linker script in, the
linker has to know the number of program segments it is going to
generate before it assigns addresses to any of the sections.  This is
because of the use of SIZEOF_HEADERS in, which is used as is
normal for an ELF target.  Unfortunately, the GNU linker will ask the
BFD backend for the size of the header information before it has fully
sorted out the section layout of the input files.  It has no mechanism
for adjusting the header size based on the section layout.  Therefore,
the BFD backend will guess that only two segments are required.  If
more than two segments are required, the linker must abort.

You are absolutely correct in saying that this bug is not related to
your patch.

However, look at it from my perspective.  Right now, the linker will
always work correctly given the script in, and (thanks to some
patches from Doug Evans) it will always work given a script which does
not use SIZEOF_HEADERS.  It will currently fail when given a script
which uses SIZEOF_HEADERS which does not arrange the sections
carefully.  It will also sometimes allocate an unnecessary page of
virtual address space.

Your patch fixes the last problem.  However, if your patch is applied,
it will no longer be the case that the linker will always work
correctly given the script in

To my mind, that is too heavy a price to pay.  The bug you are fixing
is a rather theoretical one--I may misunderstand something, but I
think I would find it difficult to construct a program which failed to
run without your patch.  If your patch were harmless, I would put it
in.  However, I believe that it is not harmless.

I believe that the correct thing to do is to permit the linker to
adjust SIZEOF_HEADERS based on the section layout.  However, that may
be a complicated task, I have very little time, and I am not aware of
any person who has actually encountered the problem.  If I receive
patches to fix that problem, I will put your patch in at that time.


More information about the Gas2 mailing list