authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-29 00:08:01+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-29 12:05:36-05:00
log1ba455485519001f23f9ea8c5b7e02b334f6d019
treed3cb27442e56f9d57e0360c4a1e009ac9d8ed78a
parent3ec37c979ec6ad3e375b391791dff2fbc0a7dddf

Correct dl_iterate_phdr address

The base should be zero so that p_vaddr + dlpi_addr = p_vaddr

3 files changed, 3 insertions(+), 59 deletions(-)

lib/std/os.zig+1-1
......@@ -2697,7 +2697,7 @@ pub fn dl_iterate_phdr(
26972697 // the whole ELF image
26982698 if (it.end()) {
26992699 var info = dl_phdr_info{
2700 .dlpi_addr = elf_base,
2700 .dlpi_addr = 0,
27012701 .dlpi_name = "/proc/self/exe",
27022702 .dlpi_phdr = phdrs.ptr,
27032703 .dlpi_phnum = ehdr.e_phnum,
lib/std/os/linux.zig-57
......@@ -1041,63 +1041,6 @@ pub fn uname(uts: *utsname) usize {
10411041 return syscall1(SYS_uname, @ptrToInt(uts));
10421042}
10431043
1044// XXX: This should be weak
1045extern const __ehdr_start: elf.Ehdr;
1046
1047pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {
1048 if (builtin.link_libc) {
1049 return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data));
1050 }
1051
1052 const elf_base = @ptrToInt(&__ehdr_start);
1053 const n_phdr = __ehdr_start.e_phnum;
1054 const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr];
1055
1056 var it = dl.linkmap_iterator(phdrs) catch return 0;
1057
1058 // The executable has no dynamic link segment, create a single entry for
1059 // the whole ELF image
1060 if (it.end()) {
1061 var info = dl_phdr_info{
1062 .dlpi_addr = elf_base,
1063 .dlpi_name = "/proc/self/exe",
1064 .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff),
1065 .dlpi_phnum = __ehdr_start.e_phnum,
1066 };
1067
1068 return callback(&info, @sizeOf(dl_phdr_info), data);
1069 }
1070
1071 // Last return value from the callback function
1072 var last_r: isize = 0;
1073 while (it.next()) |entry| {
1074 var dlpi_phdr: usize = undefined;
1075 var dlpi_phnum: u16 = undefined;
1076
1077 if (entry.l_addr != 0) {
1078 const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr);
1079 dlpi_phdr = entry.l_addr + elf_header.e_phoff;
1080 dlpi_phnum = elf_header.e_phnum;
1081 } else {
1082 // This is the running ELF image
1083 dlpi_phdr = elf_base + __ehdr_start.e_phoff;
1084 dlpi_phnum = __ehdr_start.e_phnum;
1085 }
1086
1087 var info = dl_phdr_info{
1088 .dlpi_addr = entry.l_addr,
1089 .dlpi_name = entry.l_name,
1090 .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr),
1091 .dlpi_phnum = dlpi_phnum,
1092 };
1093
1094 last_r = callback(&info, @sizeOf(dl_phdr_info), data);
1095 if (last_r != 0) break;
1096 }
1097
1098 return last_r;
1099}
1100
11011044pub fn io_uring_setup(entries: u32, p: *io_uring_params) usize {
11021045 return syscall2(SYS_io_uring_setup, entries, @ptrToInt(p));
11031046}
lib/std/os/test.zig+2-1
......@@ -186,8 +186,9 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 {
186186
187187 if (phdr.p_type != elf.PT_LOAD) continue;
188188
189 const reloc_addr = info.dlpi_addr + phdr.p_vaddr;
189190 // Find the ELF header
190 const elf_header = @intToPtr(*elf.Ehdr, phdr.p_vaddr - phdr.p_offset);
191 const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
191192 // Validate the magic
192193 if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return -1;
193194 // Consistency check