This is an archive of the discontinued LLVM Phabricator instance.

[ELF] do not allow .bss to occupy the file space when producing relocatable output.
ClosedPublic

Authored by grimar on Mar 2 2016, 9:21 AM.

Details

Summary

When generating relocatable output SHT_NOBITS sections
were still occupy the file space.

Patch fixed that.

Diff Detail

Event Timeline

grimar updated this revision to Diff 49638.Mar 2 2016, 9:21 AM
grimar retitled this revision from to [ELF] do not allow .bss to occupy the file space when producing relocatable output..
grimar updated this object.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar.
grimar updated this revision to Diff 49639.Mar 2 2016, 9:24 AM
  • Corrected comment in testcase.
ruiu added inline comments.Mar 2 2016, 9:26 AM
ELF/Writer.cpp
1325

Do you have to set file offsets to bss sections?

grimar added inline comments.Mar 3 2016, 1:59 AM
ELF/Writer.cpp
1325

We just do the same in assignAddresses():

if (Sec->getType() != SHT_NOBITS)
  FileOff = alignTo(FileOff, Align);
Sec->setFileOffset(FileOff);
if (Sec->getType() != SHT_NOBITS)
  FileOff += Sec->getSize();
grimar added inline comments.Mar 3 2016, 6:56 AM
ELF/Writer.cpp
1325

And yes, I think we need to set the offset.
Below is relocatable output from gold for the testcase.
.bss has the Offset set.
I am just not sure why we might not to do that ?

Section Headers:

[Nr] Name              Type             Address           Offset
     Size              EntSize          Flags  Link  Info  Align
[ 0]                   NULL             0000000000000000  00000000
     0000000000000000  0000000000000000           0     0     0
[ 1] .text             PROGBITS         0000000000000000  00000040
     0000000000000001  0000000000000000  AX       0     0     4
[ 2] .bss              NOBITS           0000000000000000  00000041
     0000000000500000  0000000000000000  WA       0     0     1
[ 3] .symtab           SYMTAB           0000000000000000  00000048
     0000000000000030  0000000000000018           4     1     8
ruiu added inline comments.Mar 3 2016, 11:01 AM
ELF/Writer.cpp
1325

That gold sets the file offset does not mean that we have to do the same. I believe that behavior is not implemented to gold intentionally, but just that it happened to be that behavior. For us,

for (OutputSectionBase<ELFT> *Sec : OutputSections) {
  if (Sec->getType() == SHT_NOBITS)
    continue;
  FileOff = alignTo(FileOff, Sec->getAlign());
  Sec->setFileOffset(FileOff);
  FileOff += Sec->getSize();
}

is more straightforward, no?

grimar accepted this revision.Mar 3 2016, 12:30 PM
grimar added a reviewer: grimar.

r262650, got LGTM from Rui in llvm-mails.

This revision is now accepted and ready to land.Mar 3 2016, 12:30 PM
grimar closed this revision.Mar 3 2016, 12:30 PM