Skip to content

Commit 540deb6

Browse files
committed
Address review feedback
- `auto` over `const auto` for lambdas - Inline `alreadyAssignedToOutCmd` - Use `.lds` as linker script file extension in test - Move RUN, CHECK and test descriptors into split files - Add another duplicate class definition to test - Remove dead `loc` variable - Add missing braces to an `if` statement with braced `else` - Remove "An error is reported when" from test descriptions - Remove the 'd' from "SHF_MERGEd sections" - Use `errorOrWarn` for errors that still allow an object file to be produced - Add tests that a class reference can be used in /DISCARD/ or INSERT. - Update spill comment. - Tighten doc wording. - Add PR number to release notes.
1 parent 6b68da2 commit 540deb6

File tree

5 files changed

+241
-244
lines changed

5 files changed

+241
-244
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,9 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
497497
SmallVector<InputSectionBase *, 0> ret;
498498
DenseSet<InputSectionBase *> spills;
499499

500-
// Returns whether an input section was already assigned to an earlier input
501-
// section description in this output section or section class.
502-
const auto alreadyAssignedToOutCmd =
503-
[&outCmd](InputSectionBase *sec) { return sec->parent == &outCmd; };
504-
505500
// Returns whether an input section's flags match the input section
506501
// description's specifiers.
507-
const auto flagsMatch = [cmd](InputSectionBase *sec) {
502+
auto flagsMatch = [cmd](InputSectionBase *sec) {
508503
return (sec->flags & cmd->withFlags) == cmd->withFlags &&
509504
(sec->flags & cmd->withoutFlags) == 0;
510505
};
@@ -547,7 +542,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
547542
continue;
548543

549544
if (!cmd->matchesFile(sec->file) || pat.excludesFile(sec->file) ||
550-
alreadyAssignedToOutCmd(sec) || !flagsMatch(sec))
545+
sec->parent == &outCmd || !flagsMatch(sec))
551546
continue;
552547

553548
if (sec->parent) {
@@ -597,33 +592,35 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
597592
} else {
598593
SectionClassDesc *scd = script->sectionClasses.lookup(cmd->classRef);
599594
if (!scd) {
600-
error("undefined section class '" + cmd->classRef + "'");
595+
errorOrWarn("undefined section class '" + cmd->classRef + "'");
601596
return ret;
602597
}
603598
if (!scd->sc.assigned) {
604-
error("section class '" + cmd->classRef + "' referenced by '" +
605-
outCmd.name + "' before class definition");
599+
errorOrWarn("section class '" + cmd->classRef + "' referenced by '" +
600+
outCmd.name + "' before class definition");
606601
return ret;
607602
}
608603

609604
for (InputSectionDescription *isd : scd->sc.commands) {
610605
for (InputSectionBase *sec : isd->sectionBases) {
611-
if (alreadyAssignedToOutCmd(sec) || !flagsMatch(sec))
606+
if (sec->parent == &outCmd || !flagsMatch(sec))
612607
continue;
613608
bool isSpill = sec->parent && isa<OutputSection>(sec->parent);
614-
if (!sec->parent || (isSpill && outCmd.name == "/DISCARD/"))
615-
error("section '" + sec->name + "' cannot spill from/to /DISCARD/");
609+
if (!sec->parent || (isSpill && outCmd.name == "/DISCARD/")) {
610+
errorOrWarn("section '" + sec->name + "' cannot spill from/to /DISCARD/");
611+
continue;
612+
}
616613
if (isSpill)
617614
spills.insert(sec);
618615
ret.push_back(sec);
619616
}
620617
}
621618
}
622619

623-
// The flag --enable-non-contiguous-regions may cause sections to match an
624-
// InputSectionDescription in more than one OutputSection. Matches after the
625-
// first were collected in the spills set, so replace these with potential
626-
// spill sections.
620+
// The flag --enable-non-contiguous-regions or the section CLASS syntax may
621+
// cause sections to match an InputSectionDescription in more than one
622+
// OutputSection. Matches after the first were collected in the spills set, so
623+
// replace these with potential spill sections.
627624
if (!spills.empty()) {
628625
for (InputSectionBase *&sec : ret) {
629626
if (!spills.contains(sec))
@@ -790,8 +787,9 @@ void LinkerScript::processSectionCommands() {
790787
for (InputSectionBase *isec : isd->sectionBases)
791788
if (isa<PotentialSpillSection>(isec) ||
792789
potentialSpillLists.contains(isec))
793-
error("section '" + isec->name +
794-
"' cannot spill from/to INSERT section '" + os->name + "'");
790+
errorOrWarn("section '" + isec->name +
791+
"' cannot spill from/to INSERT section '" + os->name +
792+
"'");
795793
}
796794
}
797795
}
@@ -809,8 +807,9 @@ void LinkerScript::processSectionCommands() {
809807
for (InputSectionDescription *isd : sc->sc.commands)
810808
for (InputSectionBase *sec : isd->sectionBases)
811809
if (sec->parent && isa<SectionClass>(sec->parent))
812-
error("section '" + sec->name + "' assigned to class '" +
813-
sec->parent->name + "' but unreferenced by any output section");
810+
errorOrWarn("section '" + sec->name + "' assigned to class '" +
811+
sec->parent->name +
812+
"' but unreferenced by any output section");
814813
}
815814

816815
void LinkerScript::processSymbolAssignments() {

lld/ELF/ScriptParser.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,16 @@ SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() {
608608
}
609609

610610
SectionClassDesc *ScriptParser::readSectionClassDescription() {
611-
std::string loc = getCurrentLocation();
612611
StringRef name = readSectionClassName();
613612
SectionClassDesc *desc = make<SectionClassDesc>(name);
614613
if (!script->sectionClasses.insert({name, desc}).second)
615614
setError("section class '" + name + "' already defined");
616615
expect("{");
617616
while (!errorCount() && !consume("}")) {
618617
StringRef tok = next();
619-
if (tok == "(" || tok == ")")
618+
if (tok == "(" || tok == ")") {
620619
setError("expected filename pattern");
621-
else if (peek() == "(") {
620+
} else if (peek() == "(") {
622621
InputSectionDescription *isd = readInputSectionDescription(tok);
623622
if (!isd->classRef.empty())
624623
setError("section class '" + name + "' references class '" +

lld/docs/ELF/linker_script.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ using ``DATA_SEGMENT_RELRO_END``.
201201
Section Classes
202202
~~~~~~~~~~~~~~~
203203

204-
``SECTIONS`` commands can define classes of input sections:
204+
The ``CLASS`` keyword inside a ``SECTIONS`` command defines classes of input
205+
sections:
205206

206207
::
207208

@@ -213,7 +214,7 @@ Section Classes
213214
}
214215
}
215216

216-
Input section descriptions can refer to a class using ``CLASS(class_name)``
217+
Input section descriptions refer to a class using ``CLASS(class_name)``
217218
instead of the usual filename and section name patterns. For example:
218219

219220
::
@@ -223,11 +224,11 @@ instead of the usual filename and section name patterns. For example:
223224
.rodata { *(.rodata) CLASS(c) (*.rodata.later) }
224225
}
225226

226-
Input sections that are assigned to a class cannot be matched by later
227-
wildcards, just as if they had been assigned to an earlier output section. If a
228-
class is referenced in multiple output sections, when a memory region would
229-
overflow, the linker spills input sections from a reference to later references
230-
rather than failing the link.
227+
Input sections that are assigned to a class are not matched by later patterns,
228+
just as if they had been assigned to an earlier output section. If a class is
229+
referenced in multiple output sections, when a memory region would overflow,
230+
the linker spills input sections from a reference to later references rather
231+
than failing the link.
231232

232233
Classes cannot reference other classes; an input section is assigned to at most
233234
one class.
@@ -240,10 +241,9 @@ Non-contiguous regions
240241

241242
The flag ``--enable-non-contiguous-regions`` provides a version of the above
242243
spilling functionality that is more compatible with GNU LD. It allows input
243-
sections to spill to later wildcard matches. (This globally changes the
244-
behavior of wildcards.) Unlike GNU ld, ``/DISCARD/`` only matches
245-
previously-unmatched sections (i.e., the flag does not affect it). Also, if a
246-
section fails to fit at any of its matches, the link fails instead of
247-
discarding the section. Accordingly, the GNU flag
248-
``--enable-non-contiguous-regions-warnings`` is not implemented, as it exists
249-
to warn about such occurrences.
244+
sections to spill to later pattern matches. (This globally changes the behavior
245+
of patterns.) Unlike GNU ld, ``/DISCARD/`` only matches previously-unmatched
246+
sections (i.e., the flag does not affect it). Also, if a section fails to fit
247+
at any of its matches, the link fails instead of discarding the section.
248+
Accordingly, the GNU flag ``--enable-non-contiguous-regions-warnings`` is not
249+
implemented, as it exists to warn about such occurrences.

lld/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ ELF Improvements
3131
regions by automatically spilling to later class references if a region would
3232
overflow. This reduces the toil of manually packing regions (typical for
3333
embedded). It also makes full LTO feasible in such cases, since IR merging
34-
currently prevents the linker script from referring to input files. (TODO: PR
35-
Reference)
34+
currently prevents the linker script from referring to input files.
35+
(`#95323 <https://github.com/llvm/llvm-project/pull/95323>`_)
3636

3737
Breaking changes
3838
----------------

0 commit comments

Comments
 (0)