This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Fix combination of -script and relocatable output.
AbandonedPublic

Authored by grimar on Sep 21 2016, 8:00 AM.

Details

Summary

Previously this was completely broken. If -script and -r were used together,
section headers had size 0, because assignOffsets or assignAddresses() were not called.

This patch fixes that, testcase demonstrates that linkerscript section rules (if any)
also works correctly.

Diff Detail

Event Timeline

grimar updated this revision to Diff 72043.Sep 21 2016, 8:00 AM
grimar retitled this revision from to [ELF] - Fix combination of -script and relocatable output..
grimar updated this object.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: grimar, llvm-commits, evgeny777.
grimar added a subscriber: peter.smith.
grimar updated this object.Sep 21 2016, 8:05 AM
ruiu edited edge metadata.Sep 21 2016, 8:19 AM

Shouldn't we simply report an error if both -relocatable and linker script SECTIONS command are used? It is hard to imagine that there's a valid use case for that combination.

In D24801#548662, @ruiu wrote:

Shouldn't we simply report an error if both -relocatable and linker script SECTIONS command are used? It is hard to imagine that there's a valid use case for that combination.

That was mentioned in summary for D24032 that we have such issue and also in comments for D24782. So I guess there is a use case. I fixed it because zero sized sections is too obvious issue and it was very simple to support the correct scripted behavior.

rafael added inline comments.Oct 7 2016, 6:49 AM
ELF/LinkerScript.cpp
638

So Out<ELFT>::ProgramHeaders is the only outsection with a non 0 VA?

grimar updated this revision to Diff 73930.Oct 7 2016, 7:47 AM
grimar edited edge metadata.
  • Reimplemented.
ELF/LinkerScript.cpp
638

Relocatable output does not have program headers table, it probably was copypaste mistake.

And patch was not correct. We are calculating file offsets using VA now. It has also incorrect calculation
of output section size because of that, what is noticable in testcase.
I think if we want to support such functionality with minimal changes, the best way I can imagine is to let
script set VA and do what it do, but then set VA to 0 for all sections. That what I do in updated diff.

rafael accepted this revision.Oct 7 2016, 10:35 AM
rafael edited edge metadata.

The implementation is fine by me.

The only big question is: do we need this?

Peter, you mentioned the combination of -r and script on another review. Do you guys need this?

This revision is now accepted and ready to land.Oct 7 2016, 10:35 AM
ruiu added inline comments.Oct 7 2016, 12:49 PM
ELF/Writer.cpp
276–277

I do not understand this piece of code. VAs are initialized to zero by default, no?

ruiu added a comment.Oct 7 2016, 5:19 PM

That's what I was thinking. Linker scripts are generally to create final outputs. The behavior of the linker script in combination with -r is not well defined, I guess. I think we should rather ban the combination of -r and linker scripts so that we don't have to care about that.

peter.smith edited edge metadata.Oct 10 2016, 1:39 AM

I don't have a personal use case for ld -r and scripts, it is just one of the possible combinations that someone can use, and make exceptions more difficult.

The only use case that I know of from ld.bfd is the creation of linux kernel modules.
The script in question seems to be: https://github.com/torvalds/linux/blob/master/scripts/module-common.lds

As to whether we need them, I think anyone using linux will happily use ld.bfd or gold. For BSD there may not be an option other that lld. From what I can dig up via searching the web I found the makefile for BSD kernel modules: http://fxr.watson.org/fxr/source/conf/kmod.mk
This seems to use ld -r but does not use an ldscript:
.if ${__KLD_SHARED} == yes

232 ${KMOD}.kld: ${OBJS}
233 .else
234 ${FULLPROG}: ${OBJS}
235 .endif
236         ${LD} ${_LDFLAGS} -r -d -o ${.TARGET} ${OBJS}
237 .if ${MK_CTF} != "no"
238         ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
239 .endif

I'm not a Linux or BSD expert, my background is tools for bare-metal embedded systems. This may be out of date.

In summary I think a case could be made for not supporting the combination until someone actually needs it.

grimar abandoned this revision.Oct 11 2016, 1:32 AM

Closing as we seems do not plan to support this until have a reason to.