Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -80,6 +80,7 @@ ByteCommand *readByteCommand(StringRef Tok); uint32_t readFill(); uint32_t parseFill(StringRef Tok); + bool readOutputSectionType(OutputSection *Cmd); void readSectionAddressType(OutputSection *Cmd); OutputSection *readOverlaySectionDescription(); OutputSection *readOutputSectionDescription(StringRef OutSec); @@ -699,6 +700,18 @@ return V; } +bool ScriptParser::readOutputSectionType(OutputSection *Cmd) { + if (consume("NOLOAD")) { + Cmd->Noload = true; + return true; + } + if (consume("COPY") || consume("INFO") || consume("OVERLAY")) { + Cmd->NonAlloc = true; + return true; + } + return false; +} + // Reads an expression and/or the special directive for an output // section definition. Directive is one of following: "(NOLOAD)", // "(COPY)", "(INFO)" or "(OVERLAY)". @@ -712,16 +725,11 @@ // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html void ScriptParser::readSectionAddressType(OutputSection *Cmd) { if (consume("(")) { - if (consume("NOLOAD")) { + if (readOutputSectionType(Cmd)) { expect(")"); - Cmd->Noload = true; - return; - } - if (consume("COPY") || consume("INFO") || consume("OVERLAY")) { - expect(")"); - Cmd->NonAlloc = true; return; } + Cmd->AddrExpr = readExpr(); expect(")"); } else { @@ -729,9 +737,9 @@ } if (consume("(")) { - expect("NOLOAD"); + if (!readOutputSectionType(Cmd)) + setError("unknown section directive: " + peek()); expect(")"); - Cmd->Noload = true; } } Index: test/ELF/linkerscript/info-section-type.s =================================================================== --- test/ELF/linkerscript/info-section-type.s +++ test/ELF/linkerscript/info-section-type.s @@ -29,5 +29,14 @@ # RUN: ld.lld -o %t --script %t.script %t.o # RUN: llvm-readobj -sections %t | FileCheck %s --check-prefix=NONALLOC +# RUN: echo "SECTIONS { .bar 0x20000 (INFO) : { *(.foo) } };" > %t.script +# RUN: ld.lld -o %t --script %t.script %t.o +# RUN: llvm-readobj -sections %t | FileCheck %s --check-prefix=NONALLOC + +# RUN: echo "SECTIONS { .bar 0x20000 (BAR) : { *(.foo) } };" > %t.script +# RUN: not ld.lld -o %t --script %t.script %t.o 2>&1 |\ +# RUN: FileCheck %s --check-prefix=UNKNOWN +# UNKNOWN: unknown section directive: BAR + .section .foo,"a",@progbits .zero 1