Skip to contentSkip to conversation

Building GRUB on platforms without ELF support

DISCUSSION Christian Franke 3 participants 6 messages Nov 16, 2007 – Dec 6, 2007

lore
Share & tools
lore
#0 lore raw

Building GRUB modules requires ELF support in gas and ld. For platforms where ELF is not the native format, ld may support ELF output. If not (like on Cywin) some conversion to ELF is necessary.

In general, GNU objcopy allows conversion between object file formats. Unfortunately, objcopy (and BFD itself) does not include any support for the conversion of relocation formats (even conversion between ELF variants do not work). In particular, when converting PE (a COFF variant) to ELF, objcopy does not abort but silently produces bad PC-relative relocation offsets. In my first Cygwin patch, there is a hack to fix this in the GRUB ELF loader.

For specific conversions, fixing this in objcopy itself is easy. But there is not much chance that such pragmatic patches will be accepted upstream.
(http://sourceware.org/ml/binutils/2007-10/threads.html#00302)

I have prepared a reduced (~680 LoC) version of objcopy with the PE->ELF fix added. To support build on non-ELF platforms, I would suggest to add this to the GRUB codebase. It can be later extended for other platforms if desired.

Thanks for any comment.

Christian

#1 lore raw
Quoted 17 lines · Christian Franke

Building GRUB modules requires ELF support in gas and ld. For platforms where ELF is not the native format, ld may support ELF output. If not (like on Cywin) some conversion to ELF is necessary.

In general, GNU objcopy allows conversion between object file formats. Unfortunately, objcopy (and BFD itself) does not include any support for the conversion of relocation formats (even conversion between ELF variants do not work). In particular, when converting PE (a COFF variant) to ELF, objcopy does not abort but silently produces bad PC-relative relocation offsets. In my first Cygwin patch, there is a hack to fix this in the GRUB ELF loader.

For specific conversions, fixing this in objcopy itself is easy. But there is not much chance that such pragmatic patches will be accepted upstream.
(http://sourceware.org/ml/binutils/2007-10/threads.html#00302)

Quoted 4 lines

I have prepared a reduced (~680 LoC) version of objcopy with the PE->ELF fix added. To support build on non-ELF platforms, I would suggest to add this to the GRUB codebase. It can be later extended for other platforms if desired.

I'm not sure what the GRUB maintainers will think, but I'm not very inclined to duplicate stuff that binutils already have.

How about building binutils with --enable-targets=i386-elf ? Maybe the Cygwin maintainers would even add it as default. We could also have a configure check that aborts build when -m elf_i386 is not supported (which may also be a problem on pure-x86_64 environment!) and prompt user to rebuild binutils.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call, if you are unable to speak?
(as seen on /.)
#2 lore raw
Quoted 18 lines · Robert Millan
Quoted 17 lines · Christian Franke

Building GRUB modules requires ELF support in gas and ld. For platforms where ELF is not the native format, ld may support ELF output. If not (like on Cywin) some conversion to ELF is necessary.

In general, GNU objcopy allows conversion between object file formats. Unfortunately, objcopy (and BFD itself) does not include any support for the conversion of relocation formats (even conversion between ELF variants do not work). In particular, when converting PE (a COFF variant) to ELF, objcopy does not abort but silently produces bad PC-relative relocation offsets. In my first Cygwin patch, there is a hack to fix this in the GRUB ELF loader.

For specific conversions, fixing this in objcopy itself is easy. But there is not much chance that such pragmatic patches will be accepted upstream.
(http://sourceware.org/ml/binutils/2007-10/threads.html#00302)

Quoted 7 lines
Quoted 4 lines

I have prepared a reduced (~680 LoC) version of objcopy with the PE->ELF fix added. To support build on non-ELF platforms, I would suggest to add this to the GRUB codebase. It can be later extended for other platforms if desired.

I'm not sure what the GRUB maintainers will think, but I'm not very inclined to duplicate stuff that binutils already have.

So am I.

Quoted 5 lines

How about building binutils with --enable-targets=i386-elf ? Maybe the Cygwin maintainers would even add it as default. We could also have a configure check that aborts build when -m elf_i386 is not supported (which may also be a problem on pure-x86_64 environment!) and prompt user to rebuild binutils.

That would be a nice check to have.

--
Marco

#3 lore raw
Quoted 9 lines · Marco Gerards
Quoted 6 lines · Robert Millan
Quoted 4 lines · Christian Franke

...
For specific conversions, fixing this in objcopy itself is easy. But there is not much chance that such pragmatic patches will be accepted upstream.
(http://sourceware.org/ml/binutils/2007-10/threads.html#00302)

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

I have prepared a reduced (~680 LoC) version of objcopy with the PE->ELF fix added. To support build on non-ELF platforms, I would suggest to add this to the GRUB codebase. It can be later extended for other platforms if desired.

I'm not sure what the GRUB maintainers will think, but I'm not very inclined to duplicate stuff that binutils already have.

So am I.

Actually, binutils actually does not have this ("convert relocatable object files from format X to ELF") stuff.

Quoted 2 lines
Quoted 2 lines

How about building binutils with --enable-targets=i386-elf ? Maybe the Cygwin maintainers would even add it as default.

It is not so easy:

Cygwin's libbfd, ld and objcopy support elf32-i386, gas does not.

But ld refuses to output elf32-i386 if pe-i386 is the input format. This restriction is hard-coded into ld because some pe-specific operations are hard-coded into bfd backend (This includes the hack which fixes the pc-relative relocation in the executable).

Enable elf32-i386 as additional output format for gas does not work. The compilation fails, multiple emulations are not supported on i386 platform.

But even building gas as a cross-assembler does not help. Some assembler directives, like symbol type specs, are output format specific. Output from Cygwin's gcc produces syntax errors.

Therefore, you would need also a gcc build as a cross-compiler.

Not really worth the effort, so I posted the patch for kern/i386/dl.c instead.

Quoted 7 lines
Quoted 5 lines

We could also have a
configure check that aborts build when -m elf_i386 is not supported (which may also be a problem on pure-x86_64 environment!) and prompt user to rebuild binutils.

Better prompt "sorry", as rebuilding binutils probably won't work OOTB in many cases ... :-)

Christian

#4 lore raw
Quoted 10 lines · Christian Franke

It is not so easy:

Cygwin's libbfd, ld and objcopy support elf32-i386, gas does not.

But ld refuses to output elf32-i386 if pe-i386 is the input format. This restriction is hard-coded into ld because some pe-specific operations are hard-coded into bfd backend (This includes the hack which fixes the pc-relative relocation in the executable).

Quoted 8 lines

Enable elf32-i386 as additional output format for gas does not work. The compilation fails, multiple emulations are not supported on i386 platform.

But even building gas as a cross-assembler does not help. Some assembler directives, like symbol type specs, are output format specific. Output from Cygwin's gcc produces syntax errors.

Therefore, you would need also a gcc build as a cross-compiler.

Can't the whole toolchain include ELF support as an alternative target? Kinda like "-m32" on x86_64. I don't think it's unreasonable to request that Cygwin provides this (and it'd probably solve other problems somewhere else).

Have you tried asking the Cygwin maintainers what they think about that?

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call, if you are unable to speak?
(as seen on /.)
#5 lore raw
Quoted 8 lines · Robert Millan

...
Can't the whole toolchain include ELF support as an alternative target? Kinda like "-m32" on x86_64. I don't think it's unreasonable to request that Cygwin provides this (and it'd probably solve other problems somewhere else).

Have you tried asking the Cygwin maintainers what they think about that?

Besides GRUB2 build, ELF probably support provides no additional benefit. There are still a lot of open issues to get gcc 4.x working on Cygwin. Therefore it IMO does not make sense to request such additional feature now.

I have submitted a patch to fix the PE->ELF relocation info conversion in objcopy. It was rejected upstream.

Christian