This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Make __ehdr_start point to ELF file headers
ClosedPublic

Authored by phosek on Feb 23 2017, 6:33 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

phosek created this revision.Feb 23 2017, 6:33 PM
pcc accepted this revision.Feb 23 2017, 6:51 PM

LGTM

This should probably be cherry-picked into the 4.0 branch as well.

This revision is now accepted and ready to land.Feb 23 2017, 6:51 PM
This revision was automatically updated to reflect the committed changes.

This appears to be broken on Windows for some reason (http://lab.llvm.org:8011/builders/lld-x86_64-win7/builds/5154), so reverting the change.

ruiu edited edge metadata.Feb 23 2017, 7:37 PM

I have a Windows machine. I can try this patch for you.

When comparing the output from gold, ld and lld, one difference I noticed is that gold emits this symbol as SHN_ABS, ld uses index of .text, while lld uses SHN_UNDEF. It's the same for other reserved symbols like _end, _etext, _edata. Is there a particular reason why lld uses SHN_UNDEF or is that a bug?

ruiu added a comment.Feb 23 2017, 8:00 PM

It's reproducible on my machine. This is an output of llvm-readobj.

D:\b>bin\llvm-readobj -symbols \b\tools\lld\test\ELF\Output\ehdr_start.s.tmp

Error reading file: invalid section index
.

File: \b\tools\lld\test\ELF\Output\ehdr_start.s.tmp
Format: ELF64-x86-64
Arch: x86_64
AddressSize: 64bit
LoadName:
Symbols [

Symbol {
  Name:  (0)
  Value: 0x0
  Size: 0
  Binding: Local (0x0)
  Type: None (0x0)
  Other: 0
  Section: Undefined (0x0)
}
pcc added a comment.Feb 23 2017, 8:05 PM

These symbols don't really belong to a section, so there's no appropriate section index for them. Nor do they participate in dynamic loading (as they have hidden visibility). A section index of 1 would probably be appropriate (it doesn't matter what value it is, as long as it is not SHN_UNDEF or SHN_ABS).

Anyway, I think the problem is that we do not initialize Out<ELFT>::ElfHeader->SectionIndex, and the fix is to change the code here: http://llvm-cs.pcc.me.uk/tools/lld/ELF/Writer.cpp#1137