This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Implemented --oformat binary option.
ClosedPublic

Authored by grimar on Aug 22 2016, 7:29 AM.

Details

Summary

-oformat output-format
`-oformat' option can be used to specify the binary format for the output object file.

Patch implements binary format output type.

Diff Detail

Event Timeline

grimar updated this revision to Diff 68857.Aug 22 2016, 7:29 AM
grimar retitled this revision from to [ELF{ - Implemented --oformat binary option..
grimar updated this object.
grimar added reviewers: ruiu, rafael.
grimar retitled this revision from [ELF{ - Implemented --oformat binary option. to [ELF] - Implemented --oformat binary option..
grimar added subscribers: llvm-commits, davide, grimar and 2 others.

For reference https://reviews.freebsd.org/D7409 is the proposed FreeBSD change to switch to a linker script for the boot components (and make use of --oformat binary).

For reference https://reviews.freebsd.org/D7409 is the proposed FreeBSD change to switch to a linker script for the boot components (and make use of --oformat binary).

As I mentioned earlier in llvm-mails, gold implementation is differs with ld for --oformat binary.

I am looking on sys/boot/i386/boot.ldscript at the page you posted and see the next lines:

SECTIONS {
  . = 0x08048000 + SIZEOF_HEADERS;

That means that gold will produce output > 128mb while gnu ld will do the same layout as this patch do and output will depend only on size of sections.

I just re-checked this using stub files:
test.s:

.text
.globl _start
_start:

test.script:

SECTIONS { 
 . = 0x08048000 + SIZEOF_HEADERS;
 .text  : { *(.text*) }
}

llvm-mc -filetype=obj -triple=x86_64-pc-linux test.s -o test.o
gold --script test.script --oformat binary test.o -o outgold
Size of outgold is 134.5 MB for me, size of GNU ld output is 408 bytes.

So I think that means this patch do what is expected.

ruiu added inline comments.Aug 24 2016, 3:42 AM
ELF/Config.h
122

This can be bool OFormatBinary = false.

ELF/Driver.cpp
337

isOutputFormatBinary

338

This is technically not correct. If you pass --oformat "", it ignores the error. Please use Args.getLastArg(OPT_oformat) explicitly.

ELF/Writer.cpp
267–268

Do you have to not call this function? I don't like to sprinkle OFormatBinary check everywhere. If it works with this function call, remove this check.

1112

This seems to set only one offset, so setOffset?

grimar added inline comments.Aug 24 2016, 3:48 AM
ELF/Writer.cpp
267–268

I have to call it because it sets size for Out<ELFT>::ProgramHeaders:

Out<ELFT>::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size());

What affects on:

template <class ELFT> void Writer<ELFT>::assignAddresses() {
  uintX_t VA = Config->ImageBase + Out<ELFT>::ElfHeader->getSize() +
               Out<ELFT>::ProgramHeaders->getSize();

I think this minimal change is better then add code to avoid using these 2 special sections at all ?

ruiu added inline comments.Aug 24 2016, 4:04 AM
ELF/Writer.cpp
1071–1072

It seems to me that

uintX_t VA = Config->ImageBase;
if (!OFormatBinary)
  VA += Out<ELFT>::ElfHeader->getSize() +
             Out<ELFT>::ProgramHeaders->getSize();

is easier to understand.

grimar added inline comments.Aug 24 2016, 4:07 AM
ELF/Writer.cpp
1071–1072

It is not only that place unfortunately. These sections are added to output sections as well and that brings more changes. I can update this patch to avoid

if (Config->OFormat == OutputFormat::Elf)
      fixHeaders();

just to show the amount of changes required.

grimar updated this revision to Diff 69107.Aug 24 2016, 6:32 AM
  • Addressed review comments.
  • Improved testcase.
ELF/Config.h
122

Done.

ELF/Driver.cpp
338

Fixed.

ELF/Writer.cpp
1071–1072

Hmm, I was mistaken, this change should be enough.
It is definetely possible to live without disabling fixHeaders() and changes at Writer.cpp(131).
Troubles I had were because of another changes I tried during development. And I was incorrect when was saying "These sections are added to output sections as well", I was thinking about PHDRS at that moment, but we do not write them in this patch, so should not care.

1112

Done.

ruiu accepted this revision.Aug 25 2016, 1:22 AM
ruiu edited edge metadata.

LGTM. Thank you for doing this!

ELF/Config.h
97

Remove = false.

This revision is now accepted and ready to land.Aug 25 2016, 1:22 AM
This revision was automatically updated to reflect the committed changes.