[PATCH] Fix uninitialized elf_segment_map fields in rewrite_elf_program_header

Joe Seymour jseymour@codesourcery.com
Tue Nov 13 20:12:00 GMT 2012

When using objcopy to adjust section LMA's, via --change-section-lma, I'm
observing errors of the form "<section name> can't be allocated in segment <number>"

This happens because elf_segment_map structure allocated in
rewrite_elf_program_header isn't fully initialized (valgrind provides copious
amounts of output as evidence of this). In this case p_vaddr_offset having a
garbage value is the problem, however there are some other fields not obviously


Elsewhere in rewrite_elf_program_header bfd_zalloc is used, instead of
bfd_alloc. Therefore this patch seems appropriate. The structure definition
elf/internal.h supports this assessment:

- The values of p_align and p_size are irrelevant as p_align_valid and
p_size_valid will both be zero.

- The surrounding code sets both includes_filehdr and includes_phdrs to zero, so
header_size should be irrelevant.

- No sections have been added to the segment yet so having zero count is
appropriate, and sections is irrelevant. By the same logic it is appropriate
that p_vaddr_offset be zero, as it records the difference between the segment
vma and that for its first section.

Unfortunately the port I'm seeing this on isn't upstream, and I haven't managed
to observe it for other targets. Hopefully this is trivial/obvious enough to be OK?

As I don't have commit access perhaps someone could commit for me, if this is


2012-11-13  Joe Seymour  <jseymour@codesourcery.com>

	* elf.c (rewrite_elf_program_header): Allocate elf_segment_map with
	bfd_zalloc, instead of bfd_alloc.

Index: src/binutils-2.20-quic-5.0/bfd/elf.c
--- src/binutils-2.20-quic-5.0/bfd/elf.c	(revision 394664)
+++ src/binutils-2.20-quic-5.0/bfd/elf.c	(working copy)
@@ -5735,7 +5735,7 @@ rewrite_elf_program_header (bfd *ibfd, b
 		 and carry on looping.  */
 	      amt = sizeof (struct elf_segment_map);
 	      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
-	      map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
+	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
 	      if (map == NULL)
 		  free (sections);

More information about the Binutils mailing list