I am writing a linux dynamic linker in rust, and I am having issues preforming initial relocations without segfaulting. I've seen how origin does it and can do the same, but I would like to make my code more readable.
The issue is that calls to the PLT (I am pretty sure it's the PLT...) will segfault because nothing has been relocated yet. I would really like to find a way to get this code working reliably:
// main.rs (called by a custom _start)
let miros = if base.is_null() {
// We are the executable:
StaticPie::from_program_headers(&program_header_table, pseudorandom_bytes)
} else {
// We are the dynmaic linker:
StaticPie::from_base(base, pseudorandom_bytes)
};
miros.relocate().allocate_tls(); // <- PLT call here
// static_pie.rs
use std::{
arch::asm,
marker::PhantomData,
ptr::{null, null_mut},
slice,
};
use crate::{
arch::{
exit, io,
mmap::{mmap, MAP_ANONYMOUS, MAP_PRIVATE, PROT_READ, PROT_WRITE},
thread_pointer::set_thread_pointer,
},
elf::{
dynamic_array::{DynamicArrayItem, DynamicArrayIter, DT_RELA, DT_RELAENT, DT_RELASZ},
header::{ElfHeader, ET_DYN},
program_header::{ProgramHeader, PT_DYNAMIC, PT_PHDR, PT_TLS},
relocate::{Rela, RelocationSlices},
thread_local_storage::ThreadControlBlock,
},
linux::page_size,
syscall_debug_assert,
utils::round_up_to_boundary,
};
pub struct Ingredients;
pub struct Baked;
/// A struct representing a statically relocatable Position Independent Executable (PIE).