Skip to content
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

Enabling static-pie for musl #70740

Merged
merged 1 commit into from
Jun 19, 2020
Merged

Enabling static-pie for musl #70740

merged 1 commit into from
Jun 19, 2020

Conversation

haraldh
Copy link
Contributor

@haraldh haraldh commented Apr 3, 2020

and make it the default for the x86_64-unknown-linux-musl target

This is a quick implementation for #70693

Opening it as a draft PR to gather some feedback, before I put more work in it.

cat hello.rs
fn main() {
    println!("main = {:#x}", &main as *const _ as usize);
}/tmp/rust-musl/bin/rustc  --target x86_64-unknown-linux-musl  ~/hello.rsldd hello
	statically linkedfile hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=fec5cdc170f503a712a63a6958691ce5ce433654, with debug_info, not stripped./hello
main = 0x7f233ca30008./hello
main = 0x7f9ddc529008./hello
main = 0x7f1e5a224008./hello
main = 0x7f4485c7c008/tmp/rust-musl/bin/rustc  --target x86_64-unknown-linux-musl  -Z print-link-args  ~/hello.rs
"cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-m64" "-nostdlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/rcrt1.o" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crti.o" "-L" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "hello.hello.7rcbfp3g-cgu.0.rcgu.o" "hello.hello.7rcbfp3g-cgu.1.rcgu.o" "hello.hello.7rcbfp3g-cgu.2.rcgu.o" "hello.hello.7rcbfp3g-cgu.3.rcgu.o" "hello.hello.7rcbfp3g-cgu.4.rcgu.o" "hello.hello.7rcbfp3g-cgu.5.rcgu.o" "-o" "hello" "hello.1nxjf9so94czdgcz.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-0f9cb7646f9e2c34.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-ba857f2f2e4e7187.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-58ba5e25bbdf9d29.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-886bfe43afa847dc.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace-fbfb8fe99f19a67b.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace_sys-85fa859e7d364cc9.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-07ab026cd3ec0d82.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-a8ec5932d92ea864.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-0ba4cc2f38a198d5.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-c1bb2b3ce4f78b7c.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-0ff673c1cf0d451a.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-c8ff2001db856926.rlib" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-2ae14177140eeca2.rlib" "-Wl,--end-group" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-4fd81b5ce1b08a9c.rlib" "-static" "-Wl,-Bdynamic" "/tmp/rust-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crtn.o"

Closes #70693
Closes #53968

@rust-highfive
Copy link
Collaborator

r? @estebank

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 3, 2020
@haraldh
Copy link
Contributor Author

haraldh commented Apr 3, 2020

r? @alexcrichton

@haraldh haraldh changed the title Implement static-pie for musl Enabling static-pie for musl Apr 3, 2020
@Amanieu Amanieu assigned Amanieu and unassigned alexcrichton Apr 4, 2020
@Amanieu
Copy link
Member

Amanieu commented Apr 4, 2020

Note that the -static-pie flag is only supported in recent versions of GCC (since GCC 8).

cc @rust-lang/release Do we have any requirements for the GCC version that we depend on for linking?

@cuviper
Copy link
Member

cuviper commented Apr 4, 2020

I don't think we have any stated policy on GCC support. ISTR there was some other flag that we try and fallback if not supported, but I don't remember what that was.

@Mark-Simulacrum
Copy link
Member

I agree that we don't have a stated policy, but GCC 8 seems too recent; based on https://gcc.gnu.org/releases.html it was released May 2, 2018 -- that's just under a year ago. I assume that means that it's not available in long term stable distros, though maybe those sorts of distributions don't support musl anyway?

It would be helpful to determine what configurations this drops support for it (i.e. where is GCC prior to 8 the latest available, I guess?)

@cuviper
Copy link
Member

cuviper commented Apr 4, 2020

The other flag I was thinking of is -no-pie, added in #48076.

@elahn
Copy link
Contributor

elahn commented Apr 4, 2020

GCC 7 is the default on Ubuntu 18.04 LTS and derivatives. GCC 8 is the default on Debian 10, RHEL/CentOS 8, Alpine 3.11. GCC 9 is the default on Fedora 31 and Ubuntu 20.04 LTS. (source)

With the same retry on unknown argument strategy as -no-pie, the worst it'll do is slow-down linking of musl builds on older distros.

@bors
Copy link
Contributor

bors commented Apr 6, 2020

☔ The latest upstream changes (presumably #70826) made this pull request unmergeable. Please resolve the merge conflicts.

@alexcrichton
Copy link
Member

Sorry I unfortunately don't have the bandwidth to review a change like this, @cuviper would you mind taking over?

@cuviper
Copy link
Member

cuviper commented Apr 6, 2020

I can review, although I have no particular stake in musl support. But @Amanieu already assigned himself too.

@haraldh This needs a rebase, and I'd like to see that fallback possibility, ideally sharing that code with -no-pie. Do you have a checklist that you know needs doing for your draft?

@haraldh
Copy link
Contributor Author

haraldh commented Apr 6, 2020

I can review, although I have no particular stake in musl support. But @Amanieu already assigned himself too.

@haraldh This needs a rebase, and I'd like to see that fallback possibility, ideally sharing that code with -no-pie. Do you have a checklist that you know needs doing for your draft?

yeah, replacing -static-pie with -static needs a little bit more than the no-pie strategy.. let me see what I can do there.

@haraldh
Copy link
Contributor Author

haraldh commented Apr 6, 2020

@cuviper do you have a CI for some of the musl targets?

@cuviper
Copy link
Member

cuviper commented Apr 6, 2020

@haraldh look in src/ci/docker -- dist-i586-gnu-i586-i686-musl/Dockerfile tests i686-musl, and test-various/Dockerfile tests x86_64-musl.

@haraldh
Copy link
Contributor Author

haraldh commented Apr 7, 2020

Lol... the no-pie strategy only works for LANG=C or english. If you dare to set it to some other language it will horribly fail.

@haraldh
Copy link
Contributor Author

haraldh commented Apr 7, 2020

All right. First draft on resetting the arguments, if -static-pie is not understood.
Passes the test with -static-pies instead of -static-pie. Still have to try on a distribution, which really does not know -static-pie.

@rust-highfive

This comment has been minimized.

Manishearth added a commit to Manishearth/rust that referenced this pull request Jul 17, 2020
Fix src/test/run-make/static-pie/test-aslr.rs

Might be subject to the birthday paradox occasionally, causing spurious failures.

Addresses: rust-lang#70740 (review)
Manishearth added a commit to Manishearth/rust that referenced this pull request Jul 17, 2020
Fix src/test/run-make/static-pie/test-aslr.rs

Might be subject to the birthday paradox occasionally, causing spurious failures.

Addresses: rust-lang#70740 (review)
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Aug 30, 2020
according to various people on tech-pkg@, there are no problems with
the Firefox build

Version 1.46.0 (2020-08-27)
==========================

Language
--------
- [`if`, `match`, and `loop` expressions can now be used in const functions.][72437]
- [Additionally you are now also able to coerce and cast to slices (`&[T]`) in
  const functions.][73862]
- [The `#[track_caller]` attribute can now be added to functions to use the
  function's caller's location information for panic messages.][72445]
- [Recursively indexing into tuples no longer needs parentheses.][71322] E.g.
  `x.0.0` over `(x.0).0`.
- [`mem::transmute` can now be used in static and constants.][72920] **Note**
  You currently can't use `mem::transmute` in constant functions.

Compiler
--------
- [You can now use the `cdylib` target on Apple iOS and tvOS platforms.][73516]
- [Enabled static "Position Independent Executables" by default
  for `x86_64-unknown-linux-musl`.][70740]

Libraries
---------
- [`mem::forget` is now a `const fn`.][73887]
- [`String` now implements `From<char>`.][73466]
- [The `leading_ones`, and `trailing_ones` methods have been stabilised for all
  integer types.][73032]
- [`vec::IntoIter<T>` now implements `AsRef<[T]>`.][72583]
- [All non-zero integer types (`NonZeroU8`) now implement `TryFrom` for their
  zero-able equivalent (e.g. `TryFrom<u8>`).][72717]
- [`&[T]` and `&mut [T]` now implement `PartialEq<Vec<T>>`.][71660]
- [`(String, u16)` now implements `ToSocketAddrs`.][73007]
- [`vec::Drain<'_, T>` now implements `AsRef<[T]>`.][72584]

Stabilized APIs
---------------
- [`Option::zip`]
- [`vec::Drain::as_slice`]

Cargo
-----
Added a number of new environment variables that are now available when
compiling your crate.

- [`CARGO_BIN_NAME` and `CARGO_CRATE_NAME`][cargo/8270] Providing the name of
  the specific binary being compiled and the name of the crate.
- [`CARGO_PKG_LICENSE`][cargo/8325] The license from the manifest of the package.
- [`CARGO_PKG_LICENSE_FILE`][cargo/8387] The path to the license file.

Compatibility Notes
-------------------
- [The target configuration option `abi_blacklist` has been renamed
  to `unsupported_abis`.][74150] The old name will still continue to work.
- [Rustc will now warn if you cast a C-like enum that implements `Drop`.][72331]
  This was previously accepted but will become a hard error in a future release.
- [Rustc will fail to compile if you have a struct with
  `#[repr(i128)]` or `#[repr(u128)]`.][74109] This representation is currently only
  allowed on `enum`s.
- [Tokens passed to `macro_rules!` are now always captured.][73293] This helps
  ensure that spans have the correct information, and may cause breakage if you
  were relying on receiving spans with dummy information.
- [The InnoSetup installer for Windows is no longer available.][72569] This was
  a legacy installer that was replaced by a MSI installer a few years ago but
  was still being built.
- [`{f32, f64}::asinh` now returns the correct values for negative numbers.][72486]
- [Rustc will no longer accept overlapping trait implementations that only
  differ in how the lifetime was bound.][72493]
- [Rustc now correctly relates the lifetime of an existential associated
  type.][71896] This fixes some edge cases where `rustc` would erroneously allow
  you to pass a shorter lifetime than expected.
- [Rustc now dynamically links to `libz` (also called `zlib`) on Linux.][74420]
  The library will need to be installed for `rustc` to work, even though we
  expect it to be already available on most systems.
- [Tests annotated with `#[should_panic]` are broken on ARMv7 while running
  under QEMU.][74820]
- [Pretty printing of some tokens in procedural macros changed.][75453] The
  exact output returned by rustc's pretty printing is an unstable
  implementation detail: we recommend any macro relying on it to switch to a
  more robust parsing system.

[75453]: rust-lang/rust#75453
[74820]: rust-lang/rust#74820
[74420]: rust-lang/rust#74420
[74109]: rust-lang/rust#74109
[74150]: rust-lang/rust#74150
[73862]: rust-lang/rust#73862
[73887]: rust-lang/rust#73887
[73466]: rust-lang/rust#73466
[73516]: rust-lang/rust#73516
[73293]: rust-lang/rust#73293
[73007]: rust-lang/rust#73007
[73032]: rust-lang/rust#73032
[72920]: rust-lang/rust#72920
[72569]: rust-lang/rust#72569
[72583]: rust-lang/rust#72583
[72584]: rust-lang/rust#72584
[72717]: rust-lang/rust#72717
[72437]: rust-lang/rust#72437
[72445]: rust-lang/rust#72445
[72486]: rust-lang/rust#72486
[72493]: rust-lang/rust#72493
[72331]: rust-lang/rust#72331
[71896]: rust-lang/rust#71896
[71660]: rust-lang/rust#71660
[71322]: rust-lang/rust#71322
[70740]: rust-lang/rust#70740
[cargo/8270]: rust-lang/cargo#8270
[cargo/8325]: rust-lang/cargo#8325
[cargo/8387]: rust-lang/cargo#8387
[`Option::zip`]: https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.zip
[`vec::Drain::as_slice`]: https://doc.rust-lang.org/stable/std/vec/struct.Drain.html#method.as_slice
@haraldh haraldh deleted the static-pie branch October 7, 2020 08:19
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Oct 30, 2020
Pkgsrc changes:
 * Portability patches for Illumos have been intregrated upstream,
   so are no longer needed in pkgsrc.
 * Adjust one other patch, and update vendor/libc cargo checksum.

Upstream changes:

Version 1.46.0 (2020-08-27)
==========================

Language
--------
- [`if`, `match`, and `loop` expressions can now be used in const functions.]
  [72437]
- [Additionally you are now also able to coerce and cast to slices (`&[T]`) in
  const functions.][73862]
- [The `#[track_caller]` attribute can now be added to functions to use the
  function's caller's location information for panic messages.][72445]
- [Recursively indexing into tuples no longer needs parentheses.][71322] E.g.
  `x.0.0` over `(x.0).0`.
- [`mem::transmute` can now be used in static and constants.][72920] **Note**
  You currently can't use `mem::transmute` in constant functions.

Compiler
--------
- [You can now use the `cdylib` target on Apple iOS and tvOS platforms.][73516]
- [Enabled static "Position Independent Executables" by default
  for `x86_64-unknown-linux-musl`.][70740]

Libraries
---------
- [`mem::forget` is now a `const fn`.][73887]
- [`String` now implements `From<char>`.][73466]
- [The `leading_ones`, and `trailing_ones` methods have been stabilised for all
  integer types.][73032]
- [`vec::IntoIter<T>` now implements `AsRef<[T]>`.][72583]
- [All non-zero integer types (`NonZeroU8`) now implement `TryFrom` for their
  zero-able equivalent (e.g. `TryFrom<u8>`).][72717]
- [`&[T]` and `&mut [T]` now implement `PartialEq<Vec<T>>`.][71660]
- [`(String, u16)` now implements `ToSocketAddrs`.][73007]
- [`vec::Drain<'_, T>` now implements `AsRef<[T]>`.][72584]

Stabilized APIs
---------------
- [`Option::zip`]
- [`vec::Drain::as_slice`]

Cargo
-----
Added a number of new environment variables that are now available when
compiling your crate.

- [`CARGO_BIN_NAME` and `CARGO_CRATE_NAME`][cargo/8270] Providing the name of
  the specific binary being compiled and the name of the crate.
- [`CARGO_PKG_LICENSE`][cargo/8325] The license from the manifest of the
  package.
- [`CARGO_PKG_LICENSE_FILE`][cargo/8387] The path to the license file.

Compatibility Notes
-------------------
- [The target configuration option `abi_blacklist` has been renamed
  to `unsupported_abis`.][74150] The old name will still continue to work.
- [Rustc will now warn if you have a C-like enum that implements `Drop`.][72331]
  This was previously accepted but will become a hard error in a future release.
- [Rustc will fail to compile if you have a struct with
  `#[repr(i128)]` or `#[repr(u128)]`.][74109] This representation is currently
  only allowed on `enum`s.
- [Tokens passed to `macro_rules!` are now always captured.][73293] This helps
  ensure that spans have the correct information, and may cause breakage if you
  were relying on receiving spans with dummy information.
- [The InnoSetup installer for Windows is no longer available.][72569] This was
  a legacy installer that was replaced by a MSI installer a few years ago but
  was still being built.
- [`{f32, f64}::asinh` now returns the correct values for negative numbers.]
  [72486]
- [Rustc will no longer accept overlapping trait implementations that only
  differ in how the lifetime was bound.][72493]
- [Rustc now correctly relates the lifetime of an existential associated
  type.][71896] This fixes some edge cases where `rustc` would erroneously
  allow you to pass a shorter lifetime than expected.
- [Rustc now dynamically links to `libz` (also called `zlib`) on Linux.][74420]
  The library will need to be installed for `rustc` to work, even though we
  expect it to be already available on most systems.
- [Tests annotated with `#[should_panic]` are broken on ARMv7 while running
  under QEMU.][74820]
- [Pretty printing of some tokens in procedural macros changed.][75453] The
  exact output returned by rustc's pretty printing is an unstable
  implementation detail: we recommend any macro relying on it to switch to a
  more robust parsing system.

[75453]: rust-lang/rust#75453
[74820]: rust-lang/rust#74820
[74420]: rust-lang/rust#74420
[74109]: rust-lang/rust#74109
[74150]: rust-lang/rust#74150
[73862]: rust-lang/rust#73862
[73887]: rust-lang/rust#73887
[73466]: rust-lang/rust#73466
[73516]: rust-lang/rust#73516
[73293]: rust-lang/rust#73293
[73007]: rust-lang/rust#73007
[73032]: rust-lang/rust#73032
[72920]: rust-lang/rust#72920
[72569]: rust-lang/rust#72569
[72583]: rust-lang/rust#72583
[72584]: rust-lang/rust#72584
[72717]: rust-lang/rust#72717
[72437]: rust-lang/rust#72437
[72445]: rust-lang/rust#72445
[72486]: rust-lang/rust#72486
[72493]: rust-lang/rust#72493
[72331]: rust-lang/rust#72331
[71896]: rust-lang/rust#71896
[71660]: rust-lang/rust#71660
[71322]: rust-lang/rust#71322
[70740]: rust-lang/rust#70740
[cargo/8270]: rust-lang/cargo#8270
[cargo/8325]: rust-lang/cargo#8325
[cargo/8387]: rust-lang/cargo#8387
[`Option::zip`]: https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.zip
[`vec::Drain::as_slice`]: https://doc.rust-lang.org/stable/std/vec/struct.Drain.html#method.as_slice
pkgw added a commit to pkgw/tectonic-ci-support that referenced this pull request Jan 14, 2021
It looks like Rust 1.46 introduced a default static PIE link mode for
x86_64-unknown-linux-musl that is not compatible with our epic hacks to
get a static binary going. Work around them.

Cf: rust-lang/rust#70740
@darbysauter
Copy link

Is there any way of doing a static PIE no-std? I want to make a relocatable kernel, people keep telling me it isn't possible with rust.

@npmccallum
Copy link
Contributor

Is there any way of doing a static PIE no-std? I want to make a relocatable kernel, people keep telling me it isn't possible with rust.

I'm working on this with @joshtriplett. Stay tuned!

@Amanieu
Copy link
Member

Amanieu commented Aug 25, 2021

I actually have a working static PIE Linux binary with no_std. The source code is not public, but I can share the part that performs self-relocation early at startup. It uses Linux's auxvec to find the address of the ELF program headers but you should be able to adapt this to work for your kernel.

https://gist.github.com/Amanieu/588e3f9d330019c5d39f3ce60e8e0aae

@haraldh
Copy link
Contributor Author

haraldh commented Aug 25, 2021

It is possible... we have the pendant to Amanieu's gist here and a static pie kernel here and the kvm boot loader here.

I guess --target x86_64-unknown-linux-musl, -C relocation-model=pic, -C -nostartfiles and a self written _start are the recipe.

@petrochenkov
Copy link
Contributor

Perhaps I'm missing something, but why does static PIE requires any special actions with no_std?
CRT objects are still linked in no_std mode, and that's exactly where the self-relocation code running at the start reside.

Do you have to avoid linking in CRT objects for some other reasons, or have custom _start? Then it's not exactly a "vanilla" no_std mode.

@npmccallum
Copy link
Contributor

@petrochenkov Yes. You want to link nothing and provide your own _start. If you're writing your own kernel, you likely can't depend on a crt0 stack or anything from libc. This includes builtins with any CPU features since you likely don't want to have to save/clear/restore extended CPU state during context switches.

@joshtriplett will be writing up an MCP for a new architecture called x86_64-unknown-elf which does precisely this: it turns on no CPU features and links nothing (but probably provides core and builtins for available linking if you want to opt-in; maybe even alloc).

@darbysauter
Copy link

@npmccallum Yes you explained it well. Currently my config is:
-Crelocation-model=pic
build-std = ["core", "compiler_builtins", "alloc"]
x86_64-unknown-none

The issue I am running into is the absolute addresses for GOT or in .data.rel.ro, Technically I could translate these at runtime but I would prefer not too and my .data.rel.ro contains 0x10 and 0x18 sized entries so there isn't really a good way of parsing

@haraldh The issue is not loading, but actually getting rust to compile it as a static PIE. I already have #![no_main] in my code, but I will try some of the suggested flags.

I almost feel like the -pic option shouldn't be the only option. Would it make more sense to have -pie and not compile when it is not possible rather than just "upgrading" from pic to pie essentially hidden from the user?

@Amanieu
Copy link
Member

Amanieu commented Aug 25, 2021

The absolute address in the GOT are fixed-up at runtime by processing the relocations with the code I posted. This needs to be done very early during program startup before the code tries to perform GOT accesses.

@darbysauter
Copy link

@Amanieu I am already doing this, but why couldn't these just be relative addresses from the start of a section? And sure I can do the GOT but what about .data.rel.ro which contains vtables, string references, and other stuff which contains absolute addresses which is translatable... but for some reason mine contains 0x10 byte and 0x18 byte entries (rel and rela)

@petrochenkov
Copy link
Contributor

petrochenkov commented Aug 25, 2021

@darbysauter

The issue I am running into is the absolute addresses for GOT or in .data.rel.ro, Technically I could translate these at runtime

but actually getting rust to compile it as a static PIE

This is a static PIE, or at least the standard way to implement it in current toolchains (C/C++ including).
It indeed contains unresolved relocations, and it must contain a piece of dynamic linker statically linked into the exectuable that resolves them very early at runtime.

(I can't answer right now why more relative addresses are not introduced during codegen instead, perhaps there are technical reasons, or perhaps it's just a to avoid "one more way to codegen things".)

@petrochenkov
Copy link
Contributor

That said, when we are generating code for a rlib we don't know that the rlib will be used for executables only, so codegen will effectively use -fPIC instead of -fPIE.
Perhaps if we provide an option to codegen rlibs with -fPIE, it will help to reduce the amount of relocation work that needs to be performed at runtime.

@darbysauter
Copy link

@petrochenkov
What about this here?: https://wiki.osdev.org/Stivale#Makefile
pie and no dynamic linker.

Realistically if string refs, vtables, GOT, and whatever else that is currently absolute could simply be changed to relative and the instructions that use them be relative, arguably there might be some additional overhead but really nothing much.

But also lets say the only way of doing this is manually translating at runtime how can I even do that when .data.rel.ro is labeled SHT_PROGBITS (not SHT_REL or SHT_RELA) and contains both 0x10 and 0x18 sized entries. There is no way to tell the size of these so no good way of parsing this. Theoretically I could determine the start of the entry by checking if the 8 byte value is in the range of valid addresses, but its definitely a bad way of handling this.

@darbysauter
Copy link

@petrochenkov
I think the issue is as you mentioned because the rlibs are PIC. so therefore the final output would be PIC right? Also this is likely why the final output has a mixed .data.rel.ro.

@petrochenkov
Copy link
Contributor

petrochenkov commented Aug 25, 2021

Looks like there's already an open issue about the rlib problem - #87934.
Adding -C relocation-model=pie (that will be equivalent to -fPIE and will produce code that may be unusable when building shared libraries) looks like a reasonable solution.

@darbysauter
Copy link

that looks to be what I am looking for. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFE: x86_64-unknown-linux-musl static-pie target for ASLR static-pie for musl target (remove crt1.o)