Skip to content

Commit 44458f5

Browse files
committed
Auto merge of rust-lang#842 - sfackler:dl_iterate_phdr, r=alexcrichton
Add dl_iterate_phdr and related types A lot of this is more broadly supported than just Linux, but support for those can be added later. r? @alexcrichton
2 parents ecc3e01 + 8f7839f commit 44458f5

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

libc-test/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ fn main() {
256256

257257
if linux {
258258
cfg.header("linux/random.h");
259+
cfg.header("elf.h");
260+
cfg.header("link.h");
259261
}
260262

261263
if freebsd {
@@ -301,7 +303,9 @@ fn main() {
301303
"FILE" |
302304
"fd_set" |
303305
"Dl_info" |
304-
"DIR" => ty.to_string(),
306+
"DIR" |
307+
"Elf32_Phdr" |
308+
"Elf64_Phdr" => ty.to_string(),
305309

306310
// Fixup a few types on windows that don't actually exist.
307311
"time64_t" if windows => "__time64_t".to_string(),

src/unix/notbsd/linux/mod.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ pub type __s16 = ::c_short;
2323
pub type __u32 = ::c_uint;
2424
pub type __s32 = ::c_int;
2525

26+
pub type Elf32_Half = u16;
27+
pub type Elf32_Word = u32;
28+
pub type Elf32_Off = u32;
29+
pub type Elf32_Addr = u32;
30+
31+
pub type Elf64_Half = u16;
32+
pub type Elf64_Word = u32;
33+
pub type Elf64_Off = u64;
34+
pub type Elf64_Addr = u64;
35+
pub type Elf64_Xword = u64;
36+
2637
pub enum fpos64_t {} // TODO: fill this out with a struct
2738

2839
s! {
@@ -391,6 +402,52 @@ s! {
391402
#[cfg(target_pointer_width = "32")]
392403
pub u: [u32; 7],
393404
}
405+
406+
pub struct dl_phdr_info {
407+
#[cfg(target_pointer_width = "64")]
408+
pub dlpi_addr: Elf64_Addr,
409+
#[cfg(target_pointer_width = "32")]
410+
pub dlpi_addr: Elf32_Addr,
411+
412+
pub dlpi_name: *const ::c_char,
413+
414+
#[cfg(target_pointer_width = "64")]
415+
pub dlpi_phdr: *const Elf64_Phdr,
416+
#[cfg(target_pointer_width = "32")]
417+
pub dlpi_phdr: *const Elf32_Phdr,
418+
419+
#[cfg(target_pointer_width = "64")]
420+
pub dlpi_phnum: Elf64_Half,
421+
#[cfg(target_pointer_width = "32")]
422+
pub dlpi_phnum: Elf32_Half,
423+
424+
pub dlpi_adds: ::c_ulonglong,
425+
pub dlpi_subs: ::c_ulonglong,
426+
pub dlpi_tls_modid: ::size_t,
427+
pub dlpi_tls_data: *mut ::c_void,
428+
}
429+
430+
pub struct Elf32_Phdr {
431+
pub p_type: Elf32_Word,
432+
pub p_offset: Elf32_Off,
433+
pub p_vaddr: Elf32_Addr,
434+
pub p_paddr: Elf32_Addr,
435+
pub p_filesz: Elf32_Word,
436+
pub p_memsz: Elf32_Word,
437+
pub p_flags: Elf32_Word,
438+
pub p_align: Elf32_Word,
439+
}
440+
441+
pub struct Elf64_Phdr {
442+
pub p_type: Elf64_Word,
443+
pub p_flags: Elf64_Word,
444+
pub p_offset: Elf64_Off,
445+
pub p_vaddr: Elf64_Addr,
446+
pub p_paddr: Elf64_Addr,
447+
pub p_filesz: Elf64_Xword,
448+
pub p_memsz: Elf64_Xword,
449+
pub p_align: Elf64_Xword,
450+
}
394451
}
395452

396453
pub const ABDAY_1: ::nl_item = 0x20000;
@@ -1031,6 +1088,23 @@ pub const CMSPAR: ::tcflag_t = 0o10000000000;
10311088
pub const MFD_CLOEXEC: ::c_uint = 0x0001;
10321089
pub const MFD_ALLOW_SEALING: ::c_uint = 0x0002;
10331090

1091+
// these are used in the p_type field of Elf32_Phdr and Elf64_Phdr, which has
1092+
// the type Elf32Word and Elf64Word respectively. Luckily, both of those are u32
1093+
// so we can use that type here to avoid having to cast.
1094+
pub const PT_NULL: u32 = 0;
1095+
pub const PT_LOAD: u32 = 1;
1096+
pub const PT_DYNAMIC: u32 = 2;
1097+
pub const PT_INTERP: u32 = 3;
1098+
pub const PT_NOTE: u32 = 4;
1099+
pub const PT_SHLIB: u32 = 5;
1100+
pub const PT_PHDR: u32 = 6;
1101+
pub const PT_TLS: u32 = 7;
1102+
pub const PT_NUM: u32 = 8;
1103+
pub const PT_LOOS: u32 = 0x60000000;
1104+
pub const PT_GNU_EH_FRAME: u32 = 0x6474e550;
1105+
pub const PT_GNU_STACK: u32 = 0x6474e551;
1106+
pub const PT_GNU_RELRO: u32 = 0x6474e552;
1107+
10341108
f! {
10351109
pub fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
10361110
for slot in cpuset.bits.iter_mut() {
@@ -1488,6 +1562,14 @@ extern {
14881562
attr: *const ::pthread_attr_t,
14891563
f: extern fn(*mut ::c_void) -> *mut ::c_void,
14901564
value: *mut ::c_void) -> ::c_int;
1565+
pub fn dl_iterate_phdr(
1566+
callback: Option<unsafe extern fn(
1567+
info: *mut ::dl_phdr_info,
1568+
size: ::size_t,
1569+
data: *mut ::c_void
1570+
) -> ::c_int>,
1571+
data: *mut ::c_void
1572+
) -> ::c_int;
14911573
}
14921574

14931575
cfg_if! {

0 commit comments

Comments
 (0)