public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] fix objcopy PE -> ELF conversion
@ 2007-10-22 19:19 Christian Franke
  2007-10-22 19:20 ` H.J. Lu
  2007-10-23 11:40 ` Daniel Jacobowitz
  0 siblings, 2 replies; 7+ messages in thread
From: Christian Franke @ 2007-10-22 19:19 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 806 bytes --]

Objcopy cannot convert pe-i386 to elf32-i386. The offsets of the 
pc-relative relocation entries are not converted properly.
(http://sourceware.org/bugzilla/show_bug.cgi?id=970)

I need some PE to ELF conversion for the Cygwin port of GRUB2. The first 
working Cygwin patch for GRUB2 includes a workaround to load the broken 
ELF modules produced by objcopy. It would be desirable to fix this in 
objcopy itself.

The attached patch was tested with latest Cygwin binutils-20060817-1. 
Also applies to current CVS without conflict (but was not actually 
tested with this version).

Christian

2007-10-21  Christian Franke  <franke@computer.org>

	* objcopy.c (fix_relocation): New function to
	fix pc-relative relocation offset, for PE to ELF only.
	(copy_section): Call fix_relocation for section data.




[-- Attachment #2: objcopy-fix-pe-relocation.patch --]
[-- Type: text/x-patch, Size: 1686 bytes --]

--- binutils-20060817-1.orig/binutils/objcopy.c	2006-08-17 07:01:02.001000000 +0200
+++ binutils-20060817-1/binutils/objcopy.c	2007-10-21 21:51:11.468750000 +0200
@@ -2202,6 +2202,31 @@ loser:
   status = 1;
 }
 
+
+/* Fix pc-relative relocation.
+   When converting from PE to ELF, the pc-relative
+   relocation offset is off by 4.
+   (http://sourceware.org/bugzilla/show_bug.cgi?id=970)  */
+
+static void
+fix_relocation (bfd *ibfd, arelent **relpp, long relcount, void *data, bfd *obfd)
+{
+  arelent **p;
+  long i;
+  /* For now, handle only the pe-i386 -> elf32-i386 case */
+  if (!(!strcmp (ibfd->xvec->name, "pe-i386") && !strcmp (obfd->xvec->name, "elf32-i386")))
+    return;
+  for (i = 0, p = relpp; i < relcount && *p; i++, p++)
+    {
+      arelent *q = *p;
+      if (q->howto->pc_relative && q->howto->size == 2)
+	/* elf32-i386 does not support addent in relocation info,
+	   apply it to the section data */
+	*(long *)((char *)data+q->address) -= 4;
+    }
+}
+
+
 /* Copy the data of input section ISECTION of IBFD
    to an output section with the same name in OBFD.
    If stripping then don't copy any relocation info.  */
@@ -2255,6 +2280,7 @@ copy_section (bfd *ibfd, sec_ptr isectio
 	}
     }
 
+  relpp = 0; relcount = 0;
   if (relsize == 0)
     bfd_set_reloc (obfd, osection, NULL, 0);
   else
@@ -2295,6 +2321,9 @@ copy_section (bfd *ibfd, sec_ptr isectio
       if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
 	RETURN_NONFATAL (bfd_get_filename (ibfd));
 
+      if (relcount > 0)
+	fix_relocation (ibfd, relpp, relcount, memhunk, obfd);
+
       if (copy_byte >= 0)
 	{
 	  /* Keep only every `copy_byte'th byte in MEMHUNK.  */

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix objcopy PE -> ELF conversion
  2007-10-22 19:19 [PATCH] fix objcopy PE -> ELF conversion Christian Franke
@ 2007-10-22 19:20 ` H.J. Lu
  2007-10-22 20:43   ` Christian Franke
  2007-10-23 11:40 ` Daniel Jacobowitz
  1 sibling, 1 reply; 7+ messages in thread
From: H.J. Lu @ 2007-10-22 19:20 UTC (permalink / raw)
  To: Christian Franke; +Cc: binutils

On Mon, Oct 22, 2007 at 09:02:39PM +0200, Christian Franke wrote:
> Objcopy cannot convert pe-i386 to elf32-i386. The offsets of the 
> pc-relative relocation entries are not converted properly.
> (http://sourceware.org/bugzilla/show_bug.cgi?id=970)
> 
> I need some PE to ELF conversion for the Cygwin port of GRUB2. The first 
> working Cygwin patch for GRUB2 includes a workaround to load the broken 
> ELF modules produced by objcopy. It would be desirable to fix this in 
> objcopy itself.
> 

There is nothing wrong with objcopy. GRUB2 should be fixed instead.
You should be to link PECOFF .o files with ELF .o files to generate
ELF executable.


H.J.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix objcopy PE -> ELF conversion
  2007-10-22 19:20 ` H.J. Lu
@ 2007-10-22 20:43   ` Christian Franke
  2007-10-22 20:57     ` H.J. Lu
  0 siblings, 1 reply; 7+ messages in thread
From: Christian Franke @ 2007-10-22 20:43 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

H.J. Lu wrote:
> On Mon, Oct 22, 2007 at 09:02:39PM +0200, Christian Franke wrote:
>   
>> Objcopy cannot convert pe-i386 to elf32-i386. The offsets of the 
>> pc-relative relocation entries are not converted properly.
>> (http://sourceware.org/bugzilla/show_bug.cgi?id=970)
>>
>> I need some PE to ELF conversion for the Cygwin port of GRUB2. The first 
>> working Cygwin patch for GRUB2 includes a workaround to load the broken 
>> ELF modules produced by objcopy. It would be desirable to fix this in 
>> objcopy itself.
>>
>>     
>
> There is nothing wrong with objcopy. GRUB2 should be fixed instead.
>   

The ELF loader of GRUB2 is IMO OK.

"objcopy -O elf32-i386 pe-file.o elf-file.o" definitly produces bad 
pc-relative relocation with offset 4. There are some provisions in the 
BFD COFF_WITH_PE code to fix the offset later in the executable. But 
this has no effect when converting relocatable modules.


> You should be to link PECOFF .o files with ELF .o files to generate
> ELF executable.
>
>   

GRUB2 requires relocatable (ld -r ...) ELF modules.
And ld does not support "--oformat elf32-i386" for PE input files
("ld: cannot perform PE operations on non PE output file ...")

Christian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix objcopy PE -> ELF conversion
  2007-10-22 20:43   ` Christian Franke
@ 2007-10-22 20:57     ` H.J. Lu
  2007-10-22 22:31       ` Christian Franke
  0 siblings, 1 reply; 7+ messages in thread
From: H.J. Lu @ 2007-10-22 20:57 UTC (permalink / raw)
  To: Christian Franke; +Cc: binutils

On Mon, Oct 22, 2007 at 09:52:10PM +0200, Christian Franke wrote:
> H.J. Lu wrote:
> >On Mon, Oct 22, 2007 at 09:02:39PM +0200, Christian Franke wrote:
> >  
> >>Objcopy cannot convert pe-i386 to elf32-i386. The offsets of the 
> >>pc-relative relocation entries are not converted properly.
> >>(http://sourceware.org/bugzilla/show_bug.cgi?id=970)
> >>
> >>I need some PE to ELF conversion for the Cygwin port of GRUB2. The first 
> >>working Cygwin patch for GRUB2 includes a workaround to load the broken 
> >>ELF modules produced by objcopy. It would be desirable to fix this in 
> >>objcopy itself.
> >>
> >>    
> >
> >There is nothing wrong with objcopy. GRUB2 should be fixed instead.
> >  
> 
> The ELF loader of GRUB2 is IMO OK.
> 
> "objcopy -O elf32-i386 pe-file.o elf-file.o" definitly produces bad 
> pc-relative relocation with offset 4. There are some provisions in the 
> BFD COFF_WITH_PE code to fix the offset later in the executable. But 
> this has no effect when converting relocatable modules.
> 

I don't think objcopy is designed to convert relocataions between
different formats. If we really want to support it, it should
be done in BFD.

H.J.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix objcopy PE -> ELF conversion
  2007-10-22 20:57     ` H.J. Lu
@ 2007-10-22 22:31       ` Christian Franke
  0 siblings, 0 replies; 7+ messages in thread
From: Christian Franke @ 2007-10-22 22:31 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

H.J. Lu wrote:
>>>> ...   
>>>>         
>>> There is nothing wrong with objcopy. GRUB2 should be fixed instead.
>>>  
>>>       
>> The ELF loader of GRUB2 is IMO OK.
>>
>> "objcopy -O elf32-i386 pe-file.o elf-file.o" definitly produces bad 
>> pc-relative relocation with offset 4. There are some provisions in the 
>> BFD COFF_WITH_PE code to fix the offset later in the executable. But 
>> this has no effect when converting relocatable modules.
>>
>>     
>
> I don't think objcopy is designed to convert relocataions between
> different formats. If we really want to support it, it should
> be done in BFD.
>   

Agree. But objcopy should not produce broken output in this case. It 
should abort with an error message.

Christian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix objcopy PE -> ELF conversion
  2007-10-22 19:19 [PATCH] fix objcopy PE -> ELF conversion Christian Franke
  2007-10-22 19:20 ` H.J. Lu
@ 2007-10-23 11:40 ` Daniel Jacobowitz
  2007-10-23 22:48   ` Christian Franke
  1 sibling, 1 reply; 7+ messages in thread
From: Daniel Jacobowitz @ 2007-10-23 11:40 UTC (permalink / raw)
  To: Christian Franke; +Cc: binutils

On Mon, Oct 22, 2007 at 09:02:39PM +0200, Christian Franke wrote:
> Objcopy cannot convert pe-i386 to elf32-i386. The offsets of the pc-relative 
> relocation entries are not converted properly.
> (http://sourceware.org/bugzilla/show_bug.cgi?id=970)

As HJ said, this should be done in BFD, not in objcopy.c.  Maybe the
generic BFD_RELOC_32_PCREL should not be used for the slightly
different PE semantics?  That'd prevent BFD making the conversion.

-- 
Daniel Jacobowitz
CodeSourcery

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix objcopy PE -> ELF conversion
  2007-10-23 11:40 ` Daniel Jacobowitz
@ 2007-10-23 22:48   ` Christian Franke
  0 siblings, 0 replies; 7+ messages in thread
From: Christian Franke @ 2007-10-23 22:48 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: binutils

Daniel Jacobowitz wrote:
> On Mon, Oct 22, 2007 at 09:02:39PM +0200, Christian Franke wrote:
>   
>> Objcopy cannot convert pe-i386 to elf32-i386. The offsets of the pc-relative 
>> relocation entries are not converted properly.
>> (http://sourceware.org/bugzilla/show_bug.cgi?id=970)
>>     
>
> As HJ said, this should be done in BFD, not in objcopy.c.  Maybe the
> generic BFD_RELOC_32_PCREL should not be used for the slightly
> different PE semantics?  That'd prevent BFD making the conversion.
>
>   

Another alternative would be to keep BFD_RELOC_32_PCREL and let 
bfd_canonicalize_reloc() return arelent.addend = -4 for these entries.
Then the backend can detect the nonzero addend and fail for variants 
without addend in the symbol table (or better fixup section data later 
to support the conversion :-). But apparently the ELF-backend simply 
ignores the addend in this case.

The -4 offset is hard-coded in the COFF_WITH_PE part, therefore it might 
be very complex to fix this in BFD.

I probably better provide a separate conversion utility to build GRUB2 
modules on Cygwin :-)

Christian

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-10-23 20:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-22 19:19 [PATCH] fix objcopy PE -> ELF conversion Christian Franke
2007-10-22 19:20 ` H.J. Lu
2007-10-22 20:43   ` Christian Franke
2007-10-22 20:57     ` H.J. Lu
2007-10-22 22:31       ` Christian Franke
2007-10-23 11:40 ` Daniel Jacobowitz
2007-10-23 22:48   ` Christian Franke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).