-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[LLD] Extend special OpenBSD support, but scope under ELFOSABI #97122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-mc @llvm/pr-subscribers-lld-elf Author: John Ericson (Ericson2314) Changes> (rebaser's note) adapted from: > (rebaser's note) adapted from: Note that I think it would seem better if all this OpenBSD stuff was conditioned on Full diff: https://github.com/llvm/llvm-project/pull/97122.diff 3 Files Affected:
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 1ec796a3bdd99..e1f38eccb1518 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -107,7 +107,8 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
for (StringRef v :
{".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".ldata",
".lrodata", ".lbss", ".gcc_except_table", ".init_array", ".fini_array",
- ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"})
+ ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors",
+ ".openbsd.randomdata", ".openbsd.mutable"})
if (isSectionPrefix(v, s->name))
return v;
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index db46263115242..41bd9a95053f7 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1628,7 +1628,9 @@ unsigned ScriptParser::readPhdrType() {
.Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
.Case("PT_GNU_STACK", PT_GNU_STACK)
.Case("PT_GNU_RELRO", PT_GNU_RELRO)
+ .Case("PT_OPENBSD_MUTABLE", PT_OPENBSD_MUTABLE)
.Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
+ .Case("PT_OPENBSD_SYSCALLS", PT_OPENBSD_SYSCALLS)
.Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
.Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
.Default(-1);
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 640cb2a445f7d..917ed48bf0ec4 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2245,11 +2245,22 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
->add(part.ehFrameHdr->getParent());
+ // PT_OPENBSD_MUTABLE is an OpenBSD-specific feature. That makes
+ // the dynamic linker fill the segment with zero data, like bss, but
+ // it can be treated differently.
+ if (OutputSection *cmd = findSection(".openbsd.mutable", partNo))
+ addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);
+
// PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
// the dynamic linker fill the segment with random data.
if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
+ // PT_OPENBSD_SYSCALLS is an OpenBSD-specific feature. That makes
+ // the kernel and dynamic linker register system call sites.
+ if (OutputSection *cmd = findSection(".openbsd.syscalls", partNo))
+ addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);
+
if (config->zGnustack != GnuStackKind::None) {
// PT_GNU_STACK is a special section to tell the loader to make the
// pages for the stack non-executable. If you really want an executable
|
I'm happy to report that with change, my cross compiled statically linked hello world ran successfully :) |
05716a8
to
cfbef14
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
cfbef14
to
c6dafea
Compare
@MaskRay After testing that my cross compiled binary still runs, I:
I am reasonably happy with this. Per the 3rd commits message, without weighing in on the degree to which it is possible/reasonable for upstream to track something as fast-moving as OpenBSD, the |
c6dafea
to
f5c3bab
Compare
I rebased atop #98158 so as to separate the LLVM an LLD parts, in case that is easier for review. |
f5c3bab
to
7f1f90c
Compare
5a29ce9
to
4010410
Compare
OK @MaskRay I think this is ready now?
|
4010410
to
397eb87
Compare
(Rebased because one thing in #98158 needed fixing.) |
397eb87
to
ac43c29
Compare
I found another such possibly-affected test. I need to make sure it is working --- even if the section rename is not necessary, I figure either keeping it, or making the target |
s == ".fini_array" || s == ".init_array" || | ||
s == ".openbsd.randomdata" || s == ".preinit_array"; | ||
|
||
bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fine to keep this unchanged. We can add the osabi check if the .openbsd.*
list ever gets longer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MaskRay I would sort of like to change it for consistency.
I ended up duplicating the test I had to change so we have a Linux and OpenBSD version, and this makes me feel better about coverage, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(And checked locally that both the new tests worked, and the old test failed.)
- Add support for `.openbsd.mutable` (rebaser's note) adapted from: openbsd/src@bd249b5 New auto-coalescing sections removed In the linkers, collect objects in section "openbsd.mutable" and place them into a page-aligned region in the bss, with the right markers for kernel/ld.so to identify the region and skip making it immutable. While here, fix readelf/objdump versions to show all of this. ok miod kettenis - Add support for `.openbsd.syscalls` (rebaser's note) adapted from: openbsd/src@42a61ac Collect .openbsd.syscalls sections into a new PT_OPENBSD_SYSCALLS segment. This will be used soon to pin system calls to designated call sites. ok deraadt@ - Scope OpenBSD special section handling under that ELFOSABI As a preexisting comment in `ELF/Writer.cpp` 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.
ac43c29
to
76deb33
Compare
Linux CI is stuck in the queue, Windows has
and there is no thing host-specific about this, so I think we are good! |
…97122) - Add support for `.openbsd.mutable` (rebaser's note) adapted from: openbsd/src@bd249b5 New auto-coalescing sections removed In the linkers, collect objects in section "openbsd.mutable" and place them into a page-aligned region in the bss, with the right markers for kernel/ld.so to identify the region and skip making it immutable. While here, fix readelf/objdump versions to show all of this. ok miod kettenis - Add support for `.openbsd.syscalls` (rebaser's note) adapted from: openbsd/src@42a61ac Collect .openbsd.syscalls sections into a new PT_OPENBSD_SYSCALLS segment. This will be used soon to pin system calls to designated call sites. ok deraadt@ - Scope OpenBSD special section handling under that ELFOSABI As a preexisting comment in `ELF/Writer.cpp` 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. Co-authored-by: deraadt <[email protected]>
Add support for
.openbsd.mutable
(rebaser's note) adapted from:
openbsd/src@bd249b5
New auto-coalescing sections removed
In the linkers, collect objects in section "openbsd.mutable" and place
them into a page-aligned region in the bss, with the right markers for
kernel/ld.so to identify the region and skip making it immutable. While
here, fix readelf/objdump versions to show all of this. ok miod kettenis
Add support for
.openbsd.syscalls
(rebaser's note) adapted from:
openbsd/src@42a61ac
Collect .openbsd.syscalls sections into a new PT_OPENBSD_SYSCALLS
segment. This will be used soon to pin system calls to designated call
sites.
ok deraadt@
Scope OpenBSD special section handling under that ELFOSABI
As a preexisting comment in
ELF/Writer.cpp
says: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.
Depends on #98158