1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00

[MC] Change ELFOSABI_NONE to ELFOSABI_GNU for SHF_GNU_RETAIN

GNU ld does not give SHF_GNU_RETAIN GC root semantics for ELFOSABI_NONE.
(https://sourceware.org/pipermail/binutils/2021-March/115581.html)

This allows GNU ld to interpret SHF_GNU_RETAIN and avoids a gold quirk
https://sourceware.org/bugzilla/show_bug.cgi?id=27490

Because ELFObjectWriter is in an anonymous namespace, I have to place
`markGnuAbi` in the parent MCObjectWriter.

Differential Revision: https://reviews.llvm.org/D97976
This commit is contained in:
Fangrui Song 2021-03-09 09:59:47 -08:00
parent adea7a264e
commit 9bcdc90dd9
4 changed files with 18 additions and 4 deletions

View File

@ -85,6 +85,9 @@ public:
bool InSet,
bool IsPCRel) const;
/// ELF only. Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN).
virtual void markGnuAbi() {}
/// Tell the object writer to emit an address-significance table during
/// writeObject(). If this function is not called, all symbols are treated as
/// address-significant.

View File

@ -222,6 +222,7 @@ class ELFObjectWriter : public MCObjectWriter {
DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
bool SeenGnuAbi = false;
bool EmitAddrsigSection = false;
std::vector<const MCSymbol *> AddrsigSyms;
@ -237,6 +238,7 @@ public:
: TargetObjectWriter(std::move(MOTW)) {}
void reset() override {
SeenGnuAbi = false;
Relocations.clear();
Renames.clear();
MCObjectWriter::reset();
@ -260,6 +262,8 @@ public:
void executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override;
void markGnuAbi() override { SeenGnuAbi = true; }
bool seenGnuAbi() const { return SeenGnuAbi; }
void emitAddrsigSection() override { EmitAddrsigSection = true; }
void addAddrsigSymbol(const MCSymbol *Sym) override {
AddrsigSyms.push_back(Sym);
@ -412,7 +416,10 @@ void ELFWriter::writeHeader(const MCAssembler &Asm) {
W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION]
// e_ident[EI_OSABI]
W.OS << char(OWriter.TargetObjectWriter->getOSABI());
uint8_t OSABI = OWriter.TargetObjectWriter->getOSABI();
W.OS << char(OSABI == ELF::ELFOSABI_NONE && OWriter.seenGnuAbi()
? ELF::ELFOSABI_GNU
: OSABI);
// e_ident[EI_ABIVERSION]
W.OS << char(OWriter.TargetObjectWriter->getABIVersion());

View File

@ -156,6 +156,8 @@ void MCELFStreamer::changeSection(MCSection *Section,
const MCSymbol *Grp = SectionELF->getGroup();
if (Grp)
Asm.registerSymbol(*Grp);
if (SectionELF->getFlags() & ELF::SHF_GNU_RETAIN)
Asm.getWriter().markGnuAbi();
changeSectionImpl(Section, Subsection);
Asm.registerSymbol(*Section->getBeginSymbol());

View File

@ -1,10 +1,12 @@
# RUN: llvm-mc -triple=x86_64 %s | FileCheck %s --check-prefix=ASM
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s | llvm-readobj -hS - | FileCheck %s --check-prefix=OBJ
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s | llvm-readobj -hS - | FileCheck %s --check-prefixes=GNU,OBJ
# RUN: llvm-mc -filetype=obj -triple=aarch64-freebsd %s | llvm-readobj -hS - | FileCheck %s --check-prefixes=FREEBSD,OBJ
# ASM: .section retain,"aR",@progbits
## Note: GNU as sets OSABI to GNU.
# OBJ: OS/ABI: SystemV (0x0)
## ELFOSABI_NONE is changed to ELFOSABI_GNU. Other OSABI values are unchanged.
# GNU: OS/ABI: GNU/Linux
# FREEBSD: OS/ABI: FreeBSD
# OBJ: Name: retain
# OBJ-NEXT: Type: SHT_PROGBITS