xref: /haiku/src/libs/bsd/dl_iterate_phdr.c (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2022, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Trung Nguyen, trungnt282910@gmail.com
7  */
8 
9 
10 #include <kernel/image.h>
11 #include <link.h>
12 #include <stddef.h>
13 
14 
15 #if B_HAIKU_32_BIT
16 	typedef Elf32_Ehdr Elf_Ehdr;
17 #elif B_HAIKU_64_BIT
18 	typedef Elf64_Ehdr Elf_Ehdr;
19 #endif
20 
21 
22 // While get_next_image_info does not return the address of
23 // the ELF header, it does return the first page of the image's
24 // text segment (defined by runtime_loader as the first loaded page
25 // with read-only protection). For most images produced by
26 // normal compilers, including Haiku ELF files, the file header
27 // is always loaded at the beginning of the text segment.
28 //
29 // We can therefore take advantage of this fact and populate
30 // struct dl_phdr_info.
31 int
32 dl_iterate_phdr(int (*callback)(struct dl_phdr_info* info, size_t size, void* data), void* data)
33 {
34 	image_info info;
35 	int32 cookie = 0;
36 	int status;
37 
38 	struct dl_phdr_info phdr_info;
39 
40 	while (get_next_image_info(0, &cookie, &info) == B_OK) {
41 		const Elf_Ehdr* header = (const Elf_Ehdr*)info.text;
42 
43 		// Check for the special commpage info (which is not an ELF image),
44 		// and also guard against any maliciously crafted file which
45 		// does not load its header in memory.
46 		if (!IS_ELF(*header))
47 			continue;
48 
49 		phdr_info.dlpi_addr = (Elf_Addr)info.text;
50 		phdr_info.dlpi_name = info.name;
51 		phdr_info.dlpi_phnum = header->e_phnum;
52 		phdr_info.dlpi_phdr = (const Elf_Phdr*)((const char*)info.text + header->e_phoff);
53 
54 		status = callback(&phdr_info, sizeof(phdr_info), data);
55 
56 		if (status != 0)
57 			return status;
58 	}
59 
60 	return 0;
61 }
62