This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Change the way how we compute offsets for binary output.
ClosedPublic

Authored by grimar on Dec 2 2016, 6:33 AM.

Details

Summary

Binary output feature is a bit confuzing. bfd and gold output differs a lot sometimes,
though it is important for FreeBSD mbr loaders.

Patch change the way how we compute file offsets for binary output.
This fixes PR31196.

Previously offsets were calculated basing on a sections offsets in the same loads.

bfd assigns offsets for each section to VA - MinVA:
https://github.com/redox-os/binutils-gdb/blob/master/bfd/binary.c#L27
https://github.com/redox-os/binutils-gdb/blob/master/bfd/binary.c#L255
(LMA == VA usually)

This patch for now just stops creating phdrs for binary output. An effect from this that
that offset remains unchanged:

OutputSectionBase *First = Sec->FirstInPtLoad;
// If the section is not in a PT_LOAD, we have no other constraint.
if (!First)
  return Off; //**Always happens**
if (Sec == First)
  return alignTo(Off, Target->MaxPageSize, Sec->Addr);
return First->Offset + Sec->Addr - First->Addr;

That is enough with combination of another 2 patches to generate output
that is similar to what bfd produce.
I uploaded the demo patch showing that: D27340

Diff Detail

Repository
rL LLVM

Event Timeline

grimar updated this revision to Diff 80062.Dec 2 2016, 6:33 AM
grimar retitled this revision from to [ELF] - Change the way how we compute offsets for binary output..
grimar updated this object.
grimar added reviewers: ruiu, rafael, emaste.
grimar added subscribers: llvm-commits, grimar, evgeny777.
grimar updated this object.Dec 2 2016, 6:36 AM
grimar updated this object.
emaste edited edge metadata.Dec 2 2016, 6:57 AM

I'm testing this patch (as part of applying D27340), and have sucessfully booted a VM using pmbr linked by it.

grimar added a comment.Dec 2 2016, 7:00 AM

I'm testing this patch (as part of applying D27340), and have sucessfully booted a VM using pmbr linked by it.

Cool ! :)

ruiu accepted this revision.Dec 2 2016, 11:03 AM
ruiu edited edge metadata.

LGTM

This revision is now accepted and ready to land.Dec 2 2016, 11:03 AM
This revision was automatically updated to reflect the committed changes.