Fix seg-fault when attempting to link x86_64 binaries as if they were i386 binaries

Nick Clifton
Fri Mar 27 11:38:00 GMT 2009

Hi Guys,

  Try the following on an x86_64 system:

    % echo "int main (void) { return 0; }" > main.c
    % gcc -c main.c
    % gcc -m32 -march=i686 main.o
    collect2: ld terminated with signal 11 [Segmentation fault], core dumped
    /usr/bin/ld: i386:x86-64 architecture of input file `main.o' is incompatible with i386 output

  The warning message makes it clear what the user did wrong, but
  nevertheless the linker should not have seg-faulted.

  The patch below prevents the seg-fault and causes the linker to
  return a suitable error message, although it does not fix the
  underlying problem.  (Somewhere a dynamic section is being created
  but no contents are being allocated to it).  I will try to find time
  to track that problem down later on, but for now I am going to check
  in the patch so that we get:

    /usr/bin/ld: i386:x86-64 architecture of input file `t.o' is incompatible with i386 output
    /usr/bin/ld: final link failed: Invalid operation


2009-03-27  Nick Clifton  <>

	* section.c (bfd_get_section_contents): Detect and handle the case
	where a section has the SEC_IN_MEMORY flag set but no actual
	contents allocated.

Index: bfd/section.c
RCS file: /cvs/src/src/bfd/section.c,v
retrieving revision 1.101
diff -c -3 -p -r1.101 section.c
*** bfd/section.c	19 Mar 2009 11:02:09 -0000	1.101
--- bfd/section.c	27 Mar 2009 11:29:58 -0000
*************** bfd_get_section_contents (bfd *abfd,
*** 1436,1441 ****
--- 1436,1451 ----
    if ((section->flags & SEC_IN_MEMORY) != 0)
+       if (section->contents == NULL)
+ 	{
+ 	  /* This can happen because of errors earlier on in the linking process.
+ 	     We do not want to seg-fault here, so clear the flag and return an
+ 	     error code.  */
+ 	  section->flags &= ~ SEC_IN_MEMORY;
+ 	  bfd_set_error (bfd_error_invalid_operation);
+ 	  return FALSE;
+ 	}
        memcpy (location, section->contents + offset, (size_t) count);
        return TRUE;

More information about the Binutils mailing list