PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
elf64_get_loadable_bounds.c
Go to the documentation of this file.
1#include "common/elf.h"
2
3void elf64_get_loadable_bounds(const Elf64_File* elf, Elf64_Addr* minAddr, Elf64_Addr* maxAddr)
4{
5 if (elf == NULL || minAddr == NULL || maxAddr == NULL)
6 {
7 return;
8 }
9
10 Elf64_Ehdr* header = (Elf64_Ehdr*)elf->header;
11
12 Elf64_Addr minVaddr = UINT64_MAX;
13 Elf64_Addr maxVaddr = 0;
14 for (uint32_t i = 0; i < header->e_phnum; i++)
15 {
16 Elf64_Phdr* phdr = ELF64_GET_PHDR(elf, i);
17 if (phdr->p_type == PT_LOAD)
18 {
19 if (phdr->p_vaddr < minVaddr)
20 {
21 minVaddr = phdr->p_vaddr;
22 }
23 if (phdr->p_vaddr + phdr->p_memsz > maxVaddr)
24 {
25 maxVaddr = phdr->p_vaddr + phdr->p_memsz;
26 }
27 }
28 }
29
30 *minAddr = minVaddr;
31 *maxAddr = maxVaddr;
32}
#define ELF64_GET_PHDR(elf, index)
Get the program header at the given index from an ELF file.
Definition elf.h:795
void elf64_get_loadable_bounds(const Elf64_File *elf, Elf64_Addr *minAddr, Elf64_Addr *maxAddr)
Get the loadable virtual memory bounds of an ELF file.
uint64_t Elf64_Addr
ELF64 Unsigned program address.
Definition elf.h:30
@ PT_LOAD
Loadable segment.
Definition elf.h:748
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
#define UINT64_MAX
Definition stdint.h:74
ELF64 Header.
Definition elf.h:95
Elf64_Half e_phnum
Number of entries in the program header table.
Definition elf.h:106
ELF File Helper structure.
Definition elf.h:781
Elf64_Ehdr * header
The data in the file, pointed to the start of the ELF header.
Definition elf.h:782
ELF64 Program Header.
Definition elf.h:730
Elf64_Xword p_memsz
Size of segment in memory in bytes.
Definition elf.h:737
Elf64_Addr p_vaddr
Target virtual address in memory.
Definition elf.h:734
Elf64_Word p_type
Segment type.
Definition elf.h:731