Open dakkshesh07 opened 2 weeks ago
@llvm/issue-subscribers-lld-elf
Author: Dakkshesh (dakkshesh07)
A minimal reproducer
SECTIONS {
.text :
{
SORT(*)(.ctors)
}
}
With ld.lld --script=avr.lds start.o
ld.lld: error: avr.lds:4: CONSTRUCTORS expected, but got *
>>> SORT(*)(.ctors)
>>>
The documentation for SORT in the GNU manual isn't particularly helpful https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html
It says
You can change this by using the SORT_BY_NAME keyword, which appears before a wildcard pattern in parentheses (e.g., SORT_BY_NAME(.text*))
Given https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html
An input section description consists of a file name optionally followed by a list of section names in parentheses.
I think (SORT()(.ctors)) is sort the files denoted by () by name followed the input section pattern (.ctors). I've not yet gone through the parsing code to work out what LLD is expecting.
As to what to do about this. What where you intending by that linker script pattern? To me it looks superfluous as there is already a *(.ctors) higher up that will match .ctors before the KEEP pattern.
As an aside. the LLD error message is a bit different to when I use KEEP(SORT(*)(.CTORS))
ld.lld: error: avr.lds:4: ) expected, but got (
>>> KEEP(SORT(*)(.ctors))
>>>
Looks like LLD's SORT grammar does not permit separate sorting of filenames. From ScriptParser.cpp
// Reads contents of "SECTIONS" directive. That directive contains a
// list of glob patterns for input sections. The grammar is as follows.
//
// <patterns> ::= <section-list>
// | <sort> "(" <section-list> ")"
// | <sort> "(" <sort> "(" <section-list> ")" ")"
//
// <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
// | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
//
// <section-list> is parsed by readInputSectionsList().
GNU ld's grammar separates out SORT for filenames and sections.
If you are able to change your issue title I recommend something like: LLD linker script unable to SORT by filename pattern.
I think (SORT()(.ctors)) is sort the files denoted by () by name followed the input section pattern (.ctors).
Not quite, I believe SORT in SORT(*)(.ctors) sorts the .ctors sections by section name within the .ctors pattern across all matching input files, not the files themselves.
As to what to do about this. What where you intending by that linker script pattern? To me it looks superfluous as there is already a *(.ctors) higher up that will match .ctors before the KEEP pattern.
Not quite sure about it but its origin dates way back when initial linker relaxation support was added to AVR targets.
If you are able to change your issue title I recommend something like: LLD linker script unable to SORT by filename pattern.
Yes sure, let me do that.
Trying to cross compile AVR program but the final linking seems to fail due to some syntax error in the linkerscript.
error:
linkerscript snip:
Is this because LLD expects it in a different syntax? because all these linkerscripts from gnu binutils have been in use with gcc avr from a long time. Im just trying to fix things and get it working with LLVM as well.