⛏️ index : haiku.git

author Trung Nguyen <trungnt282910@gmail.com> 2022-05-26 20:48:56.0 +07:00:00
committer waddlesplash <waddlesplash@gmail.com> 2022-07-12 1:33:09.0 +00:00:00
commit
598bbbd34ebbbc348a31142c62cecf8a93607e0a [patch]
tree
4cb6242fc188a179076cb2e9cc94ba140341e5dd
parent
0e96df526403ab1a0bf1b02134118dbdc7987e69
download
598bbbd34ebbbc348a31142c62cecf8a93607e0a.tar.gz

libs/bsd: implemented dl_iterate_phdr

dl_iterate_phdr now fills the first four fields of
struct dl_phdr_info. The last four fields remain
unimplemented, as it requires help from the
runtime_loader.

Change-Id: Id96a7c7ac05633a71b9fb62c98b3a40f7d4f255b
Signed-off-by: Augustin Cavalier <waddlesplash@gmail.com>
(cherry picked from commit 908107a15f63582157c7094be7273dbfffa1003c)
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5456

Diff

 src/libs/bsd/Jamfile           |  1 +
 src/libs/bsd/dl_iterate_phdr.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/src/libs/bsd/Jamfile b/src/libs/bsd/Jamfile
index 0b3c70d..9a30045 100644
--- a/src/libs/bsd/Jamfile
+++ b/src/libs/bsd/Jamfile
@@ -10,6 +10,7 @@

		SharedLibrary [ MultiArchDefaultGristFiles libbsd.so ] :
			daemon.c
			dl_iterate_phdr.c
			err.c
			explicit_bzero.c
			fgetln.c
diff --git a/src/libs/bsd/dl_iterate_phdr.c b/src/libs/bsd/dl_iterate_phdr.c
new file mode 100644
index 0000000..540c7c3 100644
--- /dev/null
+++ b/src/libs/bsd/dl_iterate_phdr.c
@@ -1,0 +1,61 @@
/*
 * Copyright 2022, Haiku, Inc. All rights reserved.
 * Distributed under the terms of the MIT License.
 *
 * Authors:
 *		Trung Nguyen, trungnt282910@gmail.com
 */


#include <kernel/image.h>
#include <link.h>
#include <stddef.h>


#if B_HAIKU_32_BIT

	typedef Elf32_Ehdr Elf_Ehdr;
#elif B_HAIKU_64_BIT
	typedef Elf64_Ehdr Elf_Ehdr;
#endif


// While get_next_image_info does not return the address of
// the ELF header, it does return the first page of the image's
// text segment (defined by runtime_loader as the first loaded page
// with read-only protection). For most images produced by
// normal compilers, including Haiku ELF files, the file header
// is always loaded at the beginning of the text segment.
//
// We can therefore take advantage of this fact and populate
// struct dl_phdr_info.
int
dl_iterate_phdr(int (*callback)(struct dl_phdr_info* info, size_t size, void* data), void* data)
{
	image_info info;
	int32 cookie = 0;
	int status;

	struct dl_phdr_info phdr_info;

	while (get_next_image_info(0, &cookie, &info) == B_OK) {
		const Elf_Ehdr* header = (const Elf_Ehdr*)info.text;

		// Check for the special commpage info (which is not an ELF image),
		// and also guard against any maliciously crafted file which
		// does not load its header in memory.
		if (!IS_ELF(*header))
			continue;

		phdr_info.dlpi_addr = (Elf_Addr)info.text;
		phdr_info.dlpi_name = info.name;
		phdr_info.dlpi_phnum = header->e_phnum;
		phdr_info.dlpi_phdr = (const Elf_Phdr*)((const char*)info.text + header->e_phoff);

		status = callback(&phdr_info, sizeof(phdr_info), data);

		if (status != 0)
			return status;
	}

	return 0;
}