Skip to content

Commit

Permalink
[ELF] OUTPUT_FORMAT: support "binary" and ignore extra OUTPUT_FORMAT …
Browse files Browse the repository at this point in the history
…commands

This patch improves GNU ld compatibility.

Close llvm#87891: Support `OUTPUT_FORMAT(binary)`, which is like
--oformat=binary. --oformat=binary takes precedence over an ELF
`OUTPUT_FORMAT`.

In addition, if more than one OUTPUT_FORMAT command is specified, only
check the first one.

Pull Request: llvm#98837
  • Loading branch information
MaskRay authored Jul 16, 2024
1 parent ca61bdd commit 6464dd2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
22 changes: 15 additions & 7 deletions lld/ELF/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,28 @@ static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
void ScriptParser::readOutputFormat() {
expect("(");

StringRef s;
config->bfdname = unquote(next());
StringRef s = unquote(next());
if (!consume(")")) {
expect(",");
s = unquote(next());
StringRef tmp = unquote(next());
if (config->optEB)
config->bfdname = s;
s = tmp;
expect(",");
s = unquote(next());
tmp = unquote(next());
if (config->optEL)
config->bfdname = s;
s = tmp;
consume(")");
}
s = config->bfdname;
// If more than one OUTPUT_FORMAT is specified, only the first is checked.
if (!config->bfdname.empty())
return;
config->bfdname = s;

if (s == "binary") {
config->oFormatBinary = true;
return;
}

if (s.consume_back("-freebsd"))
config->osabi = ELFOSABI_FREEBSD;

Expand Down
2 changes: 2 additions & 0 deletions lld/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ ELF Improvements
* ``PROVIDE(lhs = rhs) PROVIDE(rhs = ...)``, ``lhs`` is now defined only if ``rhs`` is needed.
(`#74771 <https://github.com/llvm/llvm-project/issues/74771>`_)
(`#87530 <https://github.com/llvm/llvm-project/pull/87530>`_)
* ``OUTPUT_FORMAT(binary)`` is now supported.
(`#98837 <https://github.com/llvm/llvm-project/pull/98837>`_)
* Orphan placement is refined to prefer the last similar section when its rank <= orphan's rank.
(`#94099 <https://github.com/llvm/llvm-project/pull/94099>`_)
Non-alloc orphan sections are now placed at the end.
Expand Down
4 changes: 4 additions & 0 deletions lld/test/ELF/invalid-linkerscript.test
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@
# RUN: not ld.lld %t9 no-such-file 2>&1 | FileCheck -check-prefix=ERR9 %s
# ERR9: , expected, but got y
# ERR9: cannot open no-such-file:

# RUN: echo 'OUTPUT_FORMAT("")' > %t10
# RUN: not ld.lld %t10 2>&1 | FileCheck -check-prefix=ERR10 %s
# ERR10: error: {{.*}}:1: unknown output format name:
17 changes: 15 additions & 2 deletions lld/test/ELF/oformat-binary.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
# CHECK: 0000000 90 11 22
# CHECK-NEXT: 0000003

## Check case when linkerscript is used.
# RUN: echo "SECTIONS { . = 0x1000; }" > %t.script
## OUTPUT_FORMAT(binary) selects the binary format as well.
# RUN: echo "OUTPUT_FORMAT(binary)" > %t.script
# RUN: ld.lld -o %t2.out -T %t.script %t
# RUN: od -t x1 -v %t2.out | FileCheck %s
## More OUTPUT_FORMAT commands are ignored.
# RUN: echo "OUTPUT_FORMAT("binary")OUTPUT_FORMAT(elf64-x86-64)" > %t.script
# RUN: ld.lld -o %t2.out -T %t.script %t
# RUN: od -t x1 -v %t2.out | FileCheck %s

## --oformat=binary overrides an ELF OUTPUT_FORMAT.
# RUN: echo "OUTPUT_FORMAT(elf64-x86-64) SECTIONS { . = 0x1000; }" > %t.script
# RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary
# RUN: od -t x1 -v %t2.out | FileCheck %s

Expand Down Expand Up @@ -45,6 +54,10 @@
# RUN: | FileCheck %s --check-prefix ERR
# ERR: unknown --oformat value: foo

# RUN: echo "OUTPUT_FORMAT(binary-freebsd)" > %t.script
# RUN: not ld.lld -T %t.script %t -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR2
# ERR2: error: {{.*}}.script:1: unknown output format name: binary-freebsd

# RUN: ld.lld -o /dev/null %t --oformat elf
# RUN: ld.lld -o /dev/null %t --oformat=elf-foo

Expand Down

0 comments on commit 6464dd2

Please sign in to comment.