[PATCH] PE aouthdr SizeOfHeaders field

Brian Ford ford@vss.fsi.com
Thu Mar 25 12:46:00 GMT 2004


While trying to add DWARF2 support to Cygwin/mingw, I came accross the
following buglet:

After compiling and linking a Cygwin app with DWARF2, executing the app
fails with:

-bash: ./app: Permission denied

ls -l app.exe
-rwxr-xr-x    1 ford     None     17876798 Mar 21 17:11 app.exe*

strace ./app.exe
strace.exe: error creating process app.exe, (error 193)

MSDN says error 193 is:
ERROR_BADE_EXE_FORMAT: %1 is not a valid Win32 application

I stumbled on to the fact that objcopy app.exe fixes it.  The file size
grows to:
-rwxr-xr-x    1 ford     None     17898525 Mar 21 17:11 app.exe*

Diffing objdump -x shows (amoung other things):
-SizeOfImage            0194d000
-SizeOfHeaders          00000400
-CheckSum               0111987e
+SizeOfImage            0194c000
+SizeOfHeaders          00000000
+CheckSum               0110d9f8

Hmm..., SizeOfHeaders is output from peXXigen.c
(_bfd_XXi_swap_aouthdr_out).  It is assumed to be the same as the file
offset of the first section (aligned to the section alignment, although,
shouldn't that already be reflected there by definition?).

Anyway, setting a break point there showed the first section to be one I had
accidentally left out of the linker script (.debug_ranges, an orphan) with
a file offset of 0.  Looking at the output of objdump -h shows that alloc
only sections also have a zero file offset.  So,

2004-03-24  Brian Ford  <ford@vss.fsi.com>

	* peXXigen.c (_bfd_XXi_swap_aouthdr_out): Orphan and alloc only
	sections can have a filepos of 0, so use the first non-zero
	filepos for the SizeOfHeaders field.

-- 
Brian Ford
Senior Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
Phone: 314-551-8460
Fax:   314-551-8444
-------------- next part --------------
Index: peXXigen.c
===================================================================
RCS file: /cvs/src/src/bfd/peXXigen.c,v
retrieving revision 1.22
diff -u -p -r1.22 peXXigen.c
--- peXXigen.c	21 Feb 2004 06:16:02 -0000	1.22
+++ peXXigen.c	24 Mar 2004 23:43:53 -0000
@@ -657,14 +657,19 @@ _bfd_XXi_swap_aouthdr_out (abfd, in, out
 
   {
     asection *sec;
+    bfd_vma hsize = 0;
     bfd_vma dsize = 0;
-    bfd_vma isize = SA(abfd->sections->filepos);
+    bfd_vma isize = 0;
     bfd_vma tsize = 0;
 
     for (sec = abfd->sections; sec; sec = sec->next)
       {
 	int rounded = FA(sec->_raw_size);
 
+	/* The first non-zero section filepos is the header size.
+	 * Orphaned and alloc only sections may have a filepos of 0. */
+	if (hsize == 0)
+	  hsize = sec->filepos;
 	if (sec->flags & SEC_DATA)
 	  dsize += rounded;
 	if (sec->flags & SEC_CODE)
@@ -681,10 +686,10 @@ _bfd_XXi_swap_aouthdr_out (abfd, in, out
 
     aouthdr_in->dsize = dsize;
     aouthdr_in->tsize = tsize;
-    extra->SizeOfImage = isize;
+    extra->SizeOfHeaders = hsize;
+    extra->SizeOfImage = SA(hsize) + isize;
   }
 
-  extra->SizeOfHeaders = abfd->sections->filepos;
   H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
 
 #define LINKER_VERSION 256 /* That is, 2.56 */


More information about the Binutils mailing list