diff --git a/llvm/include/llvm/MC/MCObjectWriter.h b/llvm/include/llvm/MC/MCObjectWriter.h --- a/llvm/include/llvm/MC/MCObjectWriter.h +++ b/llvm/include/llvm/MC/MCObjectWriter.h @@ -85,6 +85,8 @@ bool InSet, bool IsPCRel) const; + 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. diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -222,6 +222,7 @@ DenseMap Renames; + bool SeenGnuAbi = false; bool EmitAddrsigSection = false; std::vector AddrsigSyms; @@ -237,6 +238,7 @@ : TargetObjectWriter(std::move(MOTW)) {} void reset() override { + SeenGnuAbi = false; Relocations.clear(); Renames.clear(); MCObjectWriter::reset(); @@ -260,6 +262,8 @@ 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 @@ 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()); diff --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp --- a/llvm/lib/MC/MCELFStreamer.cpp +++ b/llvm/lib/MC/MCELFStreamer.cpp @@ -156,6 +156,8 @@ 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()); diff --git a/llvm/test/MC/ELF/section-gnu.s b/llvm/test/MC/ELF/section-gnu.s --- a/llvm/test/MC/ELF/section-gnu.s +++ b/llvm/test/MC/ELF/section-gnu.s @@ -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