This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Place ".text" to be first section if -Ttext is used.
AbandonedPublic

Authored by grimar on Dec 7 2016, 4:42 AM.

Details

Reviewers
ruiu
rafael
Summary

Some loaders, for example one from PR31295 use -N -Ttext combination.
Previously our output was in next order:

.rodata
.text
...

That caused an issue. Imagine .rodata was assigned to 0x10000 and then -Ttext sets .text below that.
That could cause sections addresses intersections.
Both GNU linkers able handles that correctly.

gold just places .text to be first section. In that case
we can start assigning VA from -Ttext value if was specified and do that in a forward loop, just like we
already do in void Writer<ELFT>::assignAddresses().
That is what this patch implements.

Diff Detail

Event Timeline

grimar updated this revision to Diff 80572.Dec 7 2016, 4:42 AM
grimar retitled this revision from to [ELF] - Place ".text" to be first section if -Ttext is used..
grimar updated this object.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar, evgeny777.
ruiu edited edge metadata.Dec 7 2016, 10:07 AM

Isn't this an ad-hoc fix for that specific case? The real problem is that, if it --section-start (or equivalently -T{bss,data,text}) are used, we need to map them without overlapping to other sections.

In D27516#616079, @ruiu wrote:

Isn't this an ad-hoc fix for that specific case? The real problem is that, if it --section-start (or equivalently -T{bss,data,text}) are used, we need to map them without overlapping to other sections.

gold seems does only cares about -Ttext. For example if -Ttext=0x100 -Tdata=0x20000 -Tbss=0x30000 is given, gold output for that is:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000100 0000a0 00ba74 00  AX  0   0 16
  [ 2] .rodata           PROGBITS        0000bb80 00bb20 0035fc 00   A  0   0 16
  [ 3] .got              PROGBITS        0000f17c 00f11c 000000 00  WA  0   0  4
  [ 4] .got.plt          PROGBITS        0000f17c 00f11c 00000c 00  WA  0   0  4
  [ 5] .data             PROGBITS        0000f188 00f128 000130 00  WA  0   0  4
  [ 6] COMMON            NOBITS          0000f2b8 00f260 000044 00  WA  0   0 16
  [ 7] .bss              NOBITS          0000f300 00f2a8 0093a0 00  WA  0   0  8

bfd in opposite tries to layout everything:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000100 0000c0 00ba74 00 WAX  0   0 16
  [ 2] .data             PROGBITS        00020000 00f140 000130 00  WA  0   0  4
  [ 3] .bss              NOBITS          00030000 00f270 0093e8 00  WA  0   0 16
  [ 4] .rodata           PROGBITS        0000bb80 00bb40 0035f4 00   A  0   0 16
  [ 5] .got.plt          PROGBITS        0000f174 00f134 00000c 04  WA  0   0  4
grimar abandoned this revision.Dec 9 2016, 12:17 AM

I'll upload more generic solution.