This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Support multiple SORT in an input section description
ClosedPublic

Authored by MaskRay on Nov 10 2020, 8:48 AM.

Details

Summary

The second SORT in *(SORT(...) SORT(...)) is incorrectly parsed as a file pattern.
Fix the bug by stopping at SORT* in readInputSectionsList.

Diff Detail

Event Timeline

MaskRay created this revision.Nov 10 2020, 8:48 AM
MaskRay requested review of this revision.Nov 10 2020, 8:48 AM

Looks OK to me. Will be best to see if George has any comments about how to do the parsing.

lld/ELF/ScriptParser.cpp
651

May be worth adding a SORT example here, as peek2() may look a bit strange without one.

MaskRay updated this revision to Diff 304330.Nov 10 2020, 2:39 PM
MaskRay marked an inline comment as done.

Add a comment and simplify

grimar added inline comments.Nov 11 2020, 1:07 AM
lld/ELF/ScriptParser.cpp
663

I'd reformat the comment to have the sample on a single line, it is hard to read currently.

// peek2() is used to detect "EXCLUDE_FILE(" or "SORT("
// (e.g. *(.foo SORT(.bar)) ) where we should break the loop.

Also, our current grammar is LL(2), but we tried to use LL(1) where was possible.
See (a bit outdated) comment in ScriptLexer.cpp.

Our grammar of the linker script is LL(2), meaning that it needs at
most two-token lookahead to parse. The only place we need two-token
lookahead is labels in version scripts, where we need to parse "local :"
as if "local:".

Given above I'd go with explicit version:

while (!errorCount() && peek() != ")" && peek() != "SORT" &&
       peek() != "EXCLUDE_FILE")

Note:

  1. That "(" is a valid file name. Your version goes into infinite loop with the following input (added the FOO ( to script from test case):

# RUN: echo "SECTIONS { .abc : { *(SORT(.foo.*) .a* .a* SORT(.bar.*) FOO ( .b*) } }" > %t1.script

  1. Perhaps, the comment for readInputSectionsList should be updated.
lld/test/ELF/linkerscript/sort2.s
8–10

This is what is fixed by D91127, right?

MaskRay updated this revision to Diff 304588.Nov 11 2020, 10:56 AM
MaskRay marked 2 inline comments as done.
MaskRay edited the summary of this revision. (Show Details)

Fix infinite loop

lld/ELF/ScriptParser.cpp
663

Thanks for spotting the infinite loop problem! I added peekSortKind and changed back to LL(1).

lld/test/ELF/linkerscript/sort2.s
8–10

Yes.

grimar accepted this revision.Nov 11 2020, 11:29 PM

LGTM!

This revision is now accepted and ready to land.Nov 11 2020, 11:29 PM