Skip to content

Commit c6dafea

Browse files
committed
[LLMV][LLD] Scope OpenBSD special section handling under that ELFOSABI
As the preexisting comment (that I referred to) says: > section names shouldn't be significant in ELF in spirit. so scoping OSABI-specific magic name hacks to just the OSABI in question limits the degree to which we deviate from that "spirit" for all other OSABIs. OpenBSD in particular is very fast moving, having added a number of special sections, etc. in recent years. It is unclear how possible / reasonable it is for upstream to implement all these features in any event, but scoping like this at least mitigates the fallout for other OSABIs systems which wish to be more slow-moving. LLVM is likewise changed so that binaries have this ABI by default for OpenBSD targets.
1 parent aa25b6b commit c6dafea

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

lld/ELF/Writer.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,16 @@ static bool isRelroSection(const OutputSection *sec) {
601601
// ELF in spirit. But in reality many linker features depend on
602602
// magic section names.
603603
StringRef s = sec->name;
604-
return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" ||
605-
s == ".dtors" || s == ".jcr" || s == ".eh_frame" ||
606-
s == ".fini_array" || s == ".init_array" ||
607-
s == ".openbsd.randomdata" || s == ".preinit_array";
604+
605+
bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" ||
606+
s == ".ctors" || s == ".dtors" || s == ".jcr" ||
607+
s == ".eh_frame" || s == ".fini_array" ||
608+
s == ".init_array" || s == ".preinit_array";
609+
610+
bool abiSpecific =
611+
config->osabi == ELFOSABI_OPENBSD && s == ".openbsd.randomdata";
612+
613+
return abiAgnostic || abiSpecific;
608614
}
609615

610616
// We compute a rank for each section. The rank indicates where the
@@ -2281,21 +2287,26 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
22812287
addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
22822288
->add(part.ehFrameHdr->getParent());
22832289

2284-
// PT_OPENBSD_MUTABLE is an OpenBSD-specific feature. That makes
2285-
// the dynamic linker fill the segment with zero data, like bss, but
2286-
// it can be treated differently.
2287-
if (OutputSection *cmd = findSection(".openbsd.mutable", partNo))
2288-
addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);
2289-
2290-
// PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
2291-
// the dynamic linker fill the segment with random data.
2292-
if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
2293-
addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
2294-
2295-
// PT_OPENBSD_SYSCALLS is an OpenBSD-specific feature. That makes
2296-
// the kernel and dynamic linker register system call sites.
2297-
if (OutputSection *cmd = findSection(".openbsd.syscalls", partNo))
2298-
addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);
2290+
// Handle OpenBSD-specific section types for OpenBSD "OS ABI".
2291+
//
2292+
// Scoped to just this "OS ABI" because, as is written below "section
2293+
// names shouldn't be significant in ELF in spirit".
2294+
if (config->osabi == ELFOSABI_OPENBSD) {
2295+
// PT_OPENBSD_MUTABLE makes the dynamic linker fill the segment with
2296+
// zero data, like bss, but it can be treated differently.
2297+
if (OutputSection *cmd = findSection(".openbsd.mutable", partNo))
2298+
addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);
2299+
2300+
// PT_OPENBSD_RANDOMIZE makes the dynamic linker fill the segment
2301+
// with random data.
2302+
if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
2303+
addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
2304+
2305+
// PT_OPENBSD_SYSCALLS makes the kernel and dynamic linker register
2306+
// system call sites.
2307+
if (OutputSection *cmd = findSection(".openbsd.syscalls", partNo))
2308+
addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);
2309+
}
22992310

23002311
if (config->zGnustack != GnuStackKind::None) {
23012312
// PT_GNU_STACK is a special section to tell the loader to make the

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class MCELFObjectTargetWriter : public MCObjectTargetWriter {
7878
return ELF::ELFOSABI_FREEBSD;
7979
case Triple::Solaris:
8080
return ELF::ELFOSABI_SOLARIS;
81+
case Triple::OpenBSD:
82+
return ELF::ELFOSABI_OPENBSD;
8183
default:
8284
return ELF::ELFOSABI_NONE;
8385
}

0 commit comments

Comments
 (0)