This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Linkerscript: support assignment outside SECTIONS
ClosedPublic

Authored by phosek on Aug 16 2016, 7:40 PM.

Details

Summary

We only support assignments inside SECTIONS, but this does not matches the behavior of GNU linker which also allows them outside SECTIONS. The only restriction on assignments outside SECTIONS is that they cannot reference . (they have to be absolute expressions).

Diff Detail

Repository
rL LLVM

Event Timeline

phosek updated this revision to Diff 68298.Aug 16 2016, 7:40 PM
phosek retitled this revision from to [ELF] Linkerscript: support assignment outside SECTIONS.
phosek updated this object.
phosek added reviewers: ruiu, rafael.
phosek added a project: lld.
phosek added a subscriber: llvm-commits.
grimar added a subscriber: grimar.Aug 17 2016, 12:34 AM
ruiu edited edge metadata.Aug 17 2016, 12:32 PM

I'd rather ignore errors and evaluate . as 0 instead of adding the second parameter to many functions.

ruiu added inline comments.Aug 17 2016, 12:34 PM
ELF/LinkerScript.cpp
641–643 ↗(On Diff #68298)
} else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, false)) {
phosek updated this revision to Diff 68453.Aug 17 2016, 4:42 PM
phosek edited edge metadata.
phosek marked an inline comment as done.
In D23598#518449, @ruiu wrote:

I'd rather ignore errors and evaluate . as 0 instead of adding the second parameter to many functions.

Is the new version what you had in mind? Do you want to avoid completely ignoring errors, even if . is on the LHS? A different variant I've been also experimenting with is to change the Expr type to something like typedef std::function<uint64_t(optional<uint64_t>)> Expr; and when evaluating Dot inside getSymbolValue, throw an error when . is not available.

ruiu added a comment.Aug 17 2016, 5:17 PM

I'd even completely ignore the error and not try to make efforts to detect it. It is what we do for . in other contexts such as PHDRs addresses and section's AT command.

ELF/LinkerScript.cpp
1078–1079 ↗(On Diff #68453)

Please remove dead code.

phosek updated this revision to Diff 68472.Aug 17 2016, 6:43 PM
phosek marked an inline comment as done.
ruiu added inline comments.Aug 17 2016, 6:47 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

Let's remove this -- as I wrote in the previous comment, this is not an error we are detecting in other contexts, and it is okay to ignore them. I don't think it's a popular error, and it is "undefined behavior" (if there were the linker script language specification.)

phosek added inline comments.Aug 17 2016, 6:55 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

I believe we still need some way to recognize whether the assignment is inside SECTIONS or not to avoid modifying . in case it's not when assigning addresses inside assignAddress()?

ruiu added inline comments.Aug 17 2016, 6:59 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

Users shouldn't write an assignment outside SECTIONS with . as the RHS, so I don't think we need to worry too much about it.

phosek added inline comments.Aug 17 2016, 7:09 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

The problem is if . is on LHS (in the assignment outside SECTIONS) because this will affect all subsequent assignments (even those within SECTIONS).

ruiu added inline comments.Aug 17 2016, 7:11 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

Users shouldn't write . on LHS too, no?

phosek added inline comments.Aug 17 2016, 7:14 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

No they shouldn't, but if they do, shall we just produce incorrect output? I guess that's probably fine since we're not reporting error anyway?

phosek updated this revision to Diff 68473.Aug 17 2016, 7:15 PM
ruiu added inline comments.Aug 17 2016, 7:21 PM
ELF/LinkerScript.h
70–72 ↗(On Diff #68472)

Please do nothing and let it just create incorrect outputs for incorrect inputs. We may want to do more fine-grained error checking in future, but at this moment, this error check would be too much compared to other error checks. There are many interesting ways to produce broken output using linker scripts, and we are not trying to stop them from shooting them in the foot.

phosek marked 7 inline comments as done.Aug 17 2016, 7:33 PM
ruiu accepted this revision.Aug 17 2016, 7:34 PM
ruiu edited edge metadata.

LGTM. Thanks!

This revision is now accepted and ready to land.Aug 17 2016, 7:34 PM
This revision was automatically updated to reflect the committed changes.