This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Make defsym to work correctly with reserved symbols.
ClosedPublic

Authored by grimar on Feb 5 2018, 8:15 AM.

Details

Summary

Fixes PR35744.

Bug happens because of different nature of reserved symbols.

We add them with value -1 and output section as target section:

auto Add = [](StringRef S, int64_t Pos) {
  return addOptionalRegular(S, Out::ElfHeader, Pos, STV_DEFAULT);
};
ElfSym::Etext1 = Add("etext", -1);

Then out code handles -1 as special case:

uint64_t SectionBase::getOffset(uint64_t Offset) const {
...
// For output sections we treat offset -1 as the end of the section.
return Offset == uint64_t(-1) ? OS->Size : Offset;

Though when we handle scripts and particulary expressions like
foo = etext + 1;
this might change special (-1) value and break things.

Patch fixes the issue.

Diff Detail

Event Timeline

grimar created this revision.Feb 5 2018, 8:15 AM
ruiu added inline comments.Feb 5 2018, 4:05 PM
ELF/LinkerScript.cpp
74–78

In general, when you change existing code, you should update the comment instead of adding sentences to existing comment so that the comment doesn't look patchy. Comments shouldn't reflect the history of code change but just describe why we are doing this.

So, this is just that if an expression is absolute and has no alignment requirement, its value is simply the output value, no?

grimar updated this revision to Diff 132949.Feb 6 2018, 1:00 AM
  • Updated.
ELF/LinkerScript.cpp
74–78

Technically yes, but we are unable to use something like following here I think.
(I guess you mean that)

if (isAbsolute() && Alignment == 1)
  return Val;

Because, for example we have in early-assign-symbol.s the following script:

SECTIONS { aaa = ABSOLUTE(foo - 1) + 1; .text  : { *(.text*) } }

And we first evaluate expression and then convert it to absolute:

if (Tok == "ABSOLUTE") {
  Expr Inner = readParenExpr();
  return [=] {
    ExprValue I = Inner();
    I.ForceAbsolute = true;
    return I;
  };
}

Not sure we should change something here.

I rewrote the check though.

ruiu accepted this revision.Feb 6 2018, 2:51 PM

LGTM

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