[MC][ELF] Use SHF_SUNW_NODISCARD instead of SHF_GNU_RETAIN on Solaris

As requested in D107955 <https://reviews.llvm.org/D107955>, this patch
splits off the `MC` and `CodeGen` parts and adds a testcase.

Tested on `sparcv9-sun-solaris2.11`, `amd64-pc-solaris2.11`, and
`x86_64-pc-linux-gnu`.

Differential Revision: https://reviews.llvm.org/D120318
This commit is contained in:
Rainer Orth 2022-02-23 15:43:12 +01:00
parent 42e391e4ca
commit 365be7ac72
5 changed files with 39 additions and 20 deletions

View File

@ -682,9 +682,10 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName,
}
if (Retain) {
if ((Ctx.getAsmInfo()->useIntegratedAssembler() ||
Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) &&
!TM.getTargetTriple().isOSSolaris())
if (TM.getTargetTriple().isOSSolaris())
Flags |= ELF::SHF_SUNW_NODISCARD;
else if (Ctx.getAsmInfo()->useIntegratedAssembler() ||
Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36))
Flags |= ELF::SHF_GNU_RETAIN;
return NextUniqueID++;
}
@ -861,12 +862,15 @@ static MCSection *selectELFSectionForGlobal(
EmitUniqueSection = true;
Flags |= ELF::SHF_LINK_ORDER;
}
if (Retain &&
(Ctx.getAsmInfo()->useIntegratedAssembler() ||
Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) &&
!TM.getTargetTriple().isOSSolaris()) {
EmitUniqueSection = true;
Flags |= ELF::SHF_GNU_RETAIN;
if (Retain) {
if (TM.getTargetTriple().isOSSolaris()) {
EmitUniqueSection = true;
Flags |= ELF::SHF_SUNW_NODISCARD;
} else if (Ctx.getAsmInfo()->useIntegratedAssembler() ||
Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) {
EmitUniqueSection = true;
Flags |= ELF::SHF_GNU_RETAIN;
}
}
MCSectionELF *Section = selectELFSectionForGlobal(

View File

@ -282,7 +282,8 @@ bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
return false;
}
static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr,
bool *UseLastGroup) {
unsigned flags = 0;
// If a valid numerical value is set for the section flag, use it verbatim
@ -331,7 +332,10 @@ static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
flags |= ELF::SHF_GROUP;
break;
case 'R':
flags |= ELF::SHF_GNU_RETAIN;
if (TT.isOSSolaris())
flags |= ELF::SHF_SUNW_NODISCARD;
else
flags |= ELF::SHF_GNU_RETAIN;
break;
case '?':
*UseLastGroup = true;
@ -569,7 +573,8 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
} else {
StringRef FlagsStr = getTok().getStringContents();
Lex();
extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
extraFlags = parseSectionFlags(getContext().getTargetTriple(), FlagsStr,
&UseLastGroup);
}
if (extraFlags == -1U)

View File

@ -105,6 +105,11 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
if (Flags & ELF::SHF_GNU_RETAIN)
OS << 'R';
// If there are os-specific flags, print them.
if (T.isOSSolaris())
if (Flags & ELF::SHF_SUNW_NODISCARD)
OS << 'R';
// If there are target-specific flags, print them.
Triple::ArchType Arch = T.getArch();
if (Arch == Triple::xcore) {

View File

@ -3,6 +3,8 @@
; RUN: llc -mtriple=x86_64 -data-sections=1 < %s | FileCheck %s
; RUN: llc -mtriple=x86_64 -no-integrated-as -binutils-version=2.36 < %s | FileCheck %s
; RUN: llc -mtriple=x86_64 -no-integrated-as -binutils-version=2.35 < %s | FileCheck %s --check-prefix=OLDGAS
;; Solaris uses the equivalent SHF_SUNW_NODISCARD flag, also represented as "R".
; RUN: llc -mtriple=x86_64-solaris < %s | FileCheck %s
; RUN: llc -mtriple=x86_64 -data-sections=1 -unique-section-names=0 < %s | FileCheck %s --check-prefix=NOUNIQUE

View File

@ -1,19 +1,22 @@
# REQUIRES: aarch64-registered-target
# 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-prefixes=GNU,OBJ
# RUN: llvm-mc -filetype=obj -triple=aarch64-freebsd %s | llvm-readobj -hS - | FileCheck %s --check-prefixes=FREEBSD,OBJ
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s | llvm-readobj -hS - | FileCheck %s --check-prefixes=GNU,OBJ,OBJ-GNU
# RUN: llvm-mc -filetype=obj -triple=aarch64-freebsd %s | llvm-readobj -hS - | FileCheck %s --check-prefixes=FREEBSD,OBJ,OBJ-GNU
# RUN: llvm-mc -filetype=obj -triple=x86_64-solaris %s | llvm-readobj -hS - | FileCheck %s --check-prefixes=SOLARIS,OBJ,OBJ-SOLARIS
# ASM: .section retain,"aR",@progbits
## ELFOSABI_NONE is changed to ELFOSABI_GNU. Other OSABI values are unchanged.
# GNU: OS/ABI: GNU/Linux
# FREEBSD: OS/ABI: FreeBSD
# SOLARIS: OS/ABI: Solaris
# OBJ: Name: retain
# OBJ-NEXT: Type: SHT_PROGBITS
# OBJ-NEXT: Flags [
# OBJ-NEXT: SHF_ALLOC
# OBJ-NEXT: SHF_GNU_RETAIN
# OBJ-NEXT: ]
# OBJ: Name: retain
# OBJ-NEXT: Type: SHT_PROGBITS
# OBJ-NEXT: Flags [
# OBJ-NEXT: SHF_ALLOC
# OBJ-GNU-NEXT: SHF_GNU_RETAIN
# OBJ-SOLARIS-NEXT: SHF_SUNW_NODISCARD
# OBJ-NEXT: ]
.section retain,"aR",@progbits