Skip to content

Commit

Permalink
[ELF] Disallow input section description without a filename
Browse files Browse the repository at this point in the history
GNU ld does not allow `.foo : { (*foo) }`, but we may recognize it as three
input section descriptions: file "(" with any section name, file "*foo" with
any section name, file ")" with any section name. Disallow the error-prone usage.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D125523
  • Loading branch information
MaskRay committed May 13, 2022
1 parent a291897 commit 177fd72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lld/ELF/ScriptParser.cpp
Expand Up @@ -945,6 +945,8 @@ OutputDesc *ScriptParser::readOutputSectionDescription(StringRef outSec) {
readSort();
} else if (tok == "INCLUDE") {
readInclude();
} else if (tok == "(" || tok == ")") {
setError("expected filename pattern");
} else if (peek() == "(") {
osec->commands.push_back(readInputSectionDescription(tok));
} else {
Expand Down
21 changes: 19 additions & 2 deletions lld/test/ELF/linkerscript/no-filename-spec.s
@@ -1,17 +1,34 @@
# REQUIRES: x86
# RUN: rm -rf %t && split-file %s %t
# RUN: echo '.section .bar, "a"; .quad 1;' | \
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %tfile1.o
# RUN: echo '.section .zed, "a"; .quad 2;' | \
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %tfile2.o

## We have a file name and no input sections description. In that case, all
## sections from the file specified should be included. Check that.
# RUN: ld.lld -o %t --script %s %tfile1.o %tfile2.o
# RUN: llvm-objdump -s %t | FileCheck %s
# RUN: ld.lld -o %t/a -T %t/a.t %tfile1.o %tfile2.o
# RUN: llvm-objdump -s %t/a | FileCheck %s

# CHECK: Contents of section .foo:
# CHECK-NEXT: 01000000 00000000 02000000 00000000

# RUN: not ld.lld -o /dev/null -T %t/b.t %tfile1.o 2>&1 | FileCheck %s --check-prefix=ERR
# RUN: not ld.lld -o /dev/null -T %t/c.t %tfile1.o 2>&1 | FileCheck %s --check-prefix=ERR
# RUN: not ld.lld -o /dev/null -T %t/d.t %tfile1.o 2>&1 | FileCheck %s --check-prefix=ERR

# ERR: error: {{.*}}.t:1: expected filename pattern

#--- a.t
SECTIONS {
.foo : { *file1.o *file2.o }
}

#--- b.t
SECTIONS { .foo : { (*foo) } }

#--- c.t
SECTIONS { .foo : { (*(foo)) } }

#--- d.t
SECTIONS { .foo : { )(*foo) } }

0 comments on commit 177fd72

Please sign in to comment.