[ELF] Merge SHT_RISCV_ATTRIBUTES sections
authorFangrui Song <i@maskray.me>
Thu, 8 Dec 2022 09:53:40 +0000 (09:53 +0000)
committerFangrui Song <i@maskray.me>
Thu, 8 Dec 2022 09:53:40 +0000 (09:53 +0000)
commit8a900f2438b4a167b98404565ad4da2645cc9330
treef474cc8cc99a995ec9e11ec7408e054fac9f67d7
parent5ba5aefad6e73ea02d3dd2feb18a709c20027efe
[ELF] Merge SHT_RISCV_ATTRIBUTES sections

Currently we take the first SHT_RISCV_ATTRIBUTES (.riscv.attributes) as the
output. If we link an object without an extension with an object with the
extension, the output Tag_RISCV_arch may not contain the extension and some
tools like objdump -d will not decode the related instructions.

This patch implements
Tag_RISCV_stack_align/Tag_RISCV_arch/Tag_RISCV_unaligned_access merge as
specified by
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#attributes

For the deprecated Tag_RISCV_priv_spec{,_minor,_revision}, dump the attribute to
the output iff all input agree on the value. This is different from GNU ld but
our simple approach should be ok for deprecated tags.

`RISCVAttributeParser::handler` currently warns about unknown tags. This
behavior is retained. In GNU ld arm, tags >= 64 (mod 128) are ignored with a
warning. If RISC-V ever wants to do something similar
(https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/352), consider
documenting it in the psABI and changing RISCVAttributeParser.

Like GNU ld, zero value integer attributes and empty string attributes are not
dumped to the output.

Reviewed By: asb, kito-cheng

Differential Revision: https://reviews.llvm.org/D138550
lld/ELF/Arch/RISCV.cpp
lld/ELF/Driver.cpp
lld/ELF/InputFiles.cpp
lld/ELF/SyntheticSections.cpp
lld/ELF/SyntheticSections.h
lld/ELF/Target.h
lld/docs/ReleaseNotes.rst
lld/test/ELF/lto/riscv-attributes.ll [new file with mode: 0644]
lld/test/ELF/riscv-attributes-place.s [new file with mode: 0644]
lld/test/ELF/riscv-attributes.s
llvm/include/llvm/Support/RISCVISAInfo.h