Skip to content

Commit b08519f

Browse files
authored
Handle static PIE in startup_tls_info. (#850)
* Handle static PIE in `startup_tls_info`. In static PIE mode, we don't get a `PT_PHDR` segment, but we do get a `PT_DYNAMIC` segment, and we can compare that against the `_DYNAMIC` symbol that the linker creates and dynamic linker relocates to learn our virtual address offset. * Use wrapping arithmetic when computing the TLS base. This makes the code independent of whether the offset is positive or negative.
1 parent d083603 commit b08519f

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

  • src/backend/linux_raw/runtime

src/backend/linux_raw/runtime/tls.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use crate::backend::c;
1010
use crate::backend::param::auxv::exe_phdrs;
11+
use core::arch::global_asm;
1112
use core::ptr::{null, NonNull};
1213
use linux_raw_sys::elf::*;
1314

@@ -25,6 +26,10 @@ pub(crate) fn startup_tls_info() -> StartupTlsInfo {
2526
let (first_phdr, phent, phnum) = exe_phdrs();
2627
let mut current_phdr = first_phdr.cast::<Elf_Phdr>();
2728

29+
// The dynamic address of the dynamic section, which we can compare with
30+
// the `PT_DYNAMIC` header's static address, if present.
31+
let dynamic_addr: *const c::c_void = unsafe { &_DYNAMIC };
32+
2833
// SAFETY: We assume the phdr array pointer and length the kernel provided
2934
// to the process describe a valid phdr array.
3035
unsafe {
@@ -34,7 +39,13 @@ pub(crate) fn startup_tls_info() -> StartupTlsInfo {
3439
current_phdr = current_phdr.cast::<u8>().add(phent).cast();
3540

3641
match phdr.p_type {
37-
PT_PHDR => base = first_phdr.cast::<u8>().sub(phdr.p_vaddr),
42+
// Compute the offset from the static virtual addresses
43+
// in the `p_vaddr` fields to the dynamic addresses. We don't
44+
// always get a `PT_PHDR` or `PT_DYNAMIC` header, so use
45+
// whichever one we get.
46+
PT_PHDR => base = first_phdr.cast::<u8>().wrapping_sub(phdr.p_vaddr),
47+
PT_DYNAMIC => base = dynamic_addr.cast::<u8>().wrapping_sub(phdr.p_vaddr),
48+
3849
PT_TLS => tls_phdr = phdr,
3950
PT_GNU_STACK => stack_size = phdr.p_memsz,
4051
_ => {}
@@ -51,7 +62,7 @@ pub(crate) fn startup_tls_info() -> StartupTlsInfo {
5162
}
5263
} else {
5364
StartupTlsInfo {
54-
addr: base.cast::<u8>().add((*tls_phdr).p_vaddr).cast(),
65+
addr: base.cast::<u8>().wrapping_add((*tls_phdr).p_vaddr).cast(),
5566
mem_size: (*tls_phdr).p_memsz,
5667
file_size: (*tls_phdr).p_filesz,
5768
align: (*tls_phdr).p_align,
@@ -61,6 +72,15 @@ pub(crate) fn startup_tls_info() -> StartupTlsInfo {
6172
}
6273
}
6374

75+
extern "C" {
76+
/// Declare the `_DYNAMIC` symbol so that we can compare its address with
77+
/// the static address in the `PT_DYNAMIC` header to learn our offset. Use
78+
/// a weak symbol because `_DYNAMIC` is not always present.
79+
static _DYNAMIC: c::c_void;
80+
}
81+
// Rust has `extern_weak` but it isn't stable, so use a `global_asm`.
82+
global_asm!(".weak _DYNAMIC");
83+
6484
/// The values returned from [`startup_tls_info`].
6585
///
6686
/// [`startup_tls_info`]: crate::runtime::startup_tls_info

0 commit comments

Comments
 (0)