| ... | ... | @@ -165,16 +165,19 @@ test "sigaltstack" { |
| 165 | 165 | // analyzed |
| 166 | 166 | const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void; |
| 167 | 167 | |
| 168 | | fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 { |
| 169 | | if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx) |
| 170 | | return 0; |
| 171 | | |
| 172 | | var counter = data.?; |
| 168 | const IterFnError = error{ |
| 169 | MissingPtLoadSegment, |
| 170 | MissingLoad, |
| 171 | BadElfMagic, |
| 172 | FailedConsistencyCheck, |
| 173 | }; |
| 174 | |
| 175 | fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void { |
| 173 | 176 | // Count how many libraries are loaded |
| 174 | 177 | counter.* += @as(usize, 1); |
| 175 | 178 | |
| 176 | 179 | // The image should contain at least a PT_LOAD segment |
| 177 | | if (info.dlpi_phnum < 1) return -1; |
| 180 | if (info.dlpi_phnum < 1) return error.MissingPtLoadSegment; |
| 178 | 181 | |
| 179 | 182 | // Quick & dirty validation of the phdr pointers, make sure we're not |
| 180 | 183 | // pointing to some random gibberish |
| ... | ... | @@ -189,17 +192,15 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 { |
| 189 | 192 | // Find the ELF header |
| 190 | 193 | const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset); |
| 191 | 194 | // Validate the magic |
| 192 | | if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return -1; |
| 195 | if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return error.BadElfMagic; |
| 193 | 196 | // Consistency check |
| 194 | | if (elf_header.e_phnum != info.dlpi_phnum) return -1; |
| 197 | if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck; |
| 195 | 198 | |
| 196 | 199 | found_load = true; |
| 197 | 200 | break; |
| 198 | 201 | } |
| 199 | 202 | |
| 200 | | if (!found_load) return -1; |
| 201 | | |
| 202 | | return 42; |
| 203 | if (!found_load) return error.MissingLoad; |
| 203 | 204 | } |
| 204 | 205 | |
| 205 | 206 | test "dl_iterate_phdr" { |
| ... | ... | @@ -207,7 +208,7 @@ test "dl_iterate_phdr" { |
| 207 | 208 | return error.SkipZigTest; |
| 208 | 209 | |
| 209 | 210 | var counter: usize = 0; |
| 210 | | expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0); |
| 211 | try os.dl_iterate_phdr(&counter, IterFnError, iter_fn); |
| 211 | 212 | expect(counter != 0); |
| 212 | 213 | } |
| 213 | 214 | |