This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [PATCH] Fail on erroneous linker script
- From: Alan Modra <amodra at bigpond dot net dot au>
- To: Maxim Kuvyrkov <maxim at codesourcery dot com>
- Cc: binutils <binutils at sourceware dot org>
- Date: Mon, 7 Dec 2009 13:42:08 +1030
- Subject: Re: [PATCH] Fail on erroneous linker script
- References: <4B1940EE.3050609@codesourcery.com>
On Fri, Dec 04, 2009 at 08:03:42PM +0300, Maxim Kuvyrkov wrote:
> In most valid cases NOBITS sections (.bss, usually) are placed at the
> end of the loaded segments. However, if a user provides a custom linker
> scripts which puts, a .bss section in the middle of the data segment,
> the linker will produce a broken binary. The proper behavior for linker
> is to fail with appropriate error message.
We could also treat such NOBITS sections as if they were PROGBITS.
Like the following untested patch. What do you think?
* elf.c (write_zeros): New function.
(assign_file_positions_for_load_sections): Allocate file space for
NOBITS sections that are followed by PROGBITS sections in a segment.
Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.493
diff -u -p -r1.493 elf.c
--- bfd/elf.c 28 Sep 2009 09:45:33 -0000 1.493
+++ bfd/elf.c 7 Dec 2009 02:45:08 -0000
@@ -4127,6 +4127,22 @@ print_segment_map (const struct elf_segm
putc ('\n',stderr);
}
+static bfd_boolean
+write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
+{
+ void *buf;
+ bfd_boolean ret;
+
+ if (bfd_seek (abfd, pos, SEEK_SET) != 0)
+ return FALSE;
+ buf = bfd_zmalloc (len);
+ if (buf == NULL)
+ return FALSE;
+ ret = bfd_bwrite (buf, len, abfd) == len;
+ free (buf);
+ return ret;
+}
+
/* Assign file positions to the sections based on the mapping from
sections to segments. This function also sets up some fields in
the file header. */
@@ -4448,6 +4464,15 @@ assign_file_positions_for_load_sections
if (this_hdr->sh_type != SHT_NOBITS)
{
+ if (p->p_filesz + adjust < p->p_memsz)
+ {
+ /* We have a PROGBITS section following NOBITS ones.
+ Allocate file space for the NOBITS section(s) and
+ zero it. */
+ adjust = p->p_memsz - p->p_filesz;
+ if (!write_zeros (abfd, off, adjust))
+ return FALSE;
+ }
off += adjust;
p->p_filesz += adjust;
}
--
Alan Modra
Australia Development Lab, IBM