Skip to content

Commit 28045ce

Browse files
committed
[ELF] Support (TYPE=<value>) beside output section address
Support `preinit_array . (TYPE=SHT_PREINIT_ARRAY) : { QUAD(16) }` Follow-up to https://reviews.llvm.org/D118840 peek2() could be eliminated by a future change.
1 parent 1f00c42 commit 28045ce

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

lld/ELF/ScriptParser.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ScriptParser final : ScriptLexer {
9292
SymbolAssignment *readSymbolAssignment(StringRef name);
9393
ByteCommand *readByteCommand(StringRef tok);
9494
std::array<uint8_t, 4> readFill();
95-
bool readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2);
95+
bool readSectionDirective(OutputSection *cmd, StringRef tok2);
9696
void readSectionAddressType(OutputSection *cmd);
9797
OutputDesc *readOverlaySectionDescription();
9898
OutputDesc *readOutputSectionDescription(StringRef outSec);
@@ -875,9 +875,7 @@ constexpr std::pair<const char *, unsigned> typeMap[] = {
875875
// "(TYPE=<value>)".
876876
// Tok1 and Tok2 are next 2 tokens peeked. See comment for
877877
// readSectionAddressType below.
878-
bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2) {
879-
if (tok1 != "(")
880-
return false;
878+
bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok2) {
881879
if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" &&
882880
tok2 != "OVERLAY" && tok2 != "TYPE")
883881
return false;
@@ -921,16 +919,20 @@ bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, Stri
921919
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
922920
// https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
923921
void ScriptParser::readSectionAddressType(OutputSection *cmd) {
924-
// Temporarily set inExpr to support TYPE=<value> without spaces.
925-
bool saved = std::exchange(inExpr, true);
926-
bool isDirective = readSectionDirective(cmd, peek(), peek2());
927-
inExpr = saved;
928-
if (isDirective)
929-
return;
930-
922+
if (peek() == "(") {
923+
// Temporarily set inExpr to support TYPE=<value> without spaces.
924+
SaveAndRestore saved(inExpr, true);
925+
if (readSectionDirective(cmd, peek2()))
926+
return;
927+
}
931928
cmd->addrExpr = readExpr();
932-
if (peek() == "(" && !readSectionDirective(cmd, "(", peek2()))
933-
setError("unknown section directive: " + peek2());
929+
930+
if (peek() == "(") {
931+
SaveAndRestore saved(inExpr, true);
932+
StringRef tok2 = peek2();
933+
if (!readSectionDirective(cmd, tok2))
934+
setError("unknown section directive: " + tok2);
935+
}
934936
}
935937

936938
static Expr checkAlignment(Expr e, std::string &loc) {

lld/test/ELF/linkerscript/custom-section-type.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ SECTIONS {
6767
nobits ( TYPE=SHT_NOBITS) : { BYTE(8) }
6868
init_array (TYPE=SHT_INIT_ARRAY ) : { QUAD(myinit) }
6969
fini_array (TYPE=SHT_FINI_ARRAY) : { QUAD(15) }
70-
preinit_array (TYPE=SHT_PREINIT_ARRAY) : { QUAD(16) }
70+
preinit_array . (TYPE=SHT_PREINIT_ARRAY) : { QUAD(16) }
7171
group (TYPE=17) : { LONG(17) }
7272
expr (TYPE=0x41+1) : { BYTE(0x42) *(expr) }
7373
}

0 commit comments

Comments
 (0)