Skip to content

Commit 2a942c4

Browse files
committed
[ELF] Print file:line for unknown PHDR error
Differential revision: https://reviews.llvm.org/D27335 llvm-svn: 288678
1 parent ab139a9 commit 2a942c4

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,21 +972,21 @@ std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
972972

973973
std::vector<size_t> Ret;
974974
for (StringRef PhdrName : Cmd->Phdrs)
975-
Ret.push_back(getPhdrIndex(PhdrName));
975+
Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
976976
return Ret;
977977
}
978978
return {};
979979
}
980980

981981
template <class ELFT>
982-
size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
982+
size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
983983
size_t I = 0;
984984
for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
985985
if (Cmd.Name == PhdrName)
986986
return I;
987987
++I;
988988
}
989-
error("section header '" + PhdrName + "' is not listed in PHDRS");
989+
error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
990990
return 0;
991991
}
992992

@@ -1441,6 +1441,7 @@ uint32_t ScriptParser::readFill() {
14411441
OutputSectionCommand *
14421442
ScriptParser::readOutputSectionDescription(StringRef OutSec) {
14431443
OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
1444+
Cmd->Location = getCurrentLocation();
14441445

14451446
// Read an address expression.
14461447
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address

lld/ELF/LinkerScript.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct OutputSectionCommand : BaseCommand {
124124
std::vector<StringRef> Phdrs;
125125
uint32_t Filler = 0;
126126
ConstraintKind Constraint = ConstraintKind::NoConstraint;
127+
std::string Location;
127128
};
128129

129130
// This struct represents one section match pattern in SECTIONS() command.
@@ -268,7 +269,7 @@ template <class ELFT> class LinkerScript final : public LinkerScriptBase {
268269
ScriptConfiguration &Opt = *ScriptConfig;
269270

270271
std::vector<size_t> getPhdrIndices(StringRef SectionName);
271-
size_t getPhdrIndex(StringRef PhdrName);
272+
size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
272273

273274
uintX_t Dot;
274275
uintX_t LMAOffset = 0;

lld/test/ELF/linkerscript/phdrs.s

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
# RUN: ld.lld -o %t1 --script %t.script %t
4040
# RUN: llvm-readobj -program-headers %t1 | FileCheck --check-prefix=DEFHDR %s
4141

42+
## Check that error is reported when trying to use phdr which is not listed
43+
## inside PHDRS {} block
44+
## TODO: If script doesn't contain PHDRS {} block then default phdr is always
45+
## created and error is not reported.
46+
# RUN: echo "PHDRS { all PT_LOAD; } \
47+
# RUN: SECTIONS { .baz : {*(.foo.*)} :bar }" > %t.script
48+
# RUN: not ld.lld -o %t1 --script %t.script %t 2>&1 | FileCheck --check-prefix=BADHDR %s
49+
4250
# CHECK: ProgramHeaders [
4351
# CHECK-NEXT: ProgramHeader {
4452
# CHECK-NEXT: Type: PT_LOAD (0x1)
@@ -120,6 +128,8 @@
120128
# DEFHDR-NEXT: PF_X (0x1)
121129
# DEFHDR-NEXT: ]
122130

131+
# BADHDR: {{.*}}.script:1: section header 'bar' is not listed in PHDRS
132+
123133
.global _start
124134
_start:
125135
nop

0 commit comments

Comments
 (0)