PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
elf64_get_section_by_name.c
Go to the documentation of this file.
1#include "common/elf.h"
2
3Elf64_Shdr* elf64_get_section_by_name(const Elf64_File* elf, const char* name)
4{
5 if (elf == NULL || name == NULL)
6 {
7 return NULL;
8 }
9
10 Elf64_Ehdr* header = (Elf64_Ehdr*)elf->header;
11
12 uint64_t shstrndx = header->e_shstrndx;
13 if (shstrndx == SHN_XINDEX)
14 {
15 Elf64_Shdr* firstShdr = ELF64_GET_SHDR(elf, 0);
16 shstrndx = firstShdr->sh_link;
17 }
18
19 if (shstrndx == SHN_UNDEF)
20 {
21 return NULL;
22 }
23
24 for (uint64_t i = 0; i < header->e_shnum; i++)
25 {
26 Elf64_Shdr* shdr = ELF64_GET_SHDR(elf, i);
27 const char* sectionName = elf64_get_string(elf, shstrndx, shdr->sh_name);
28 if (sectionName != NULL && elf_strcmp(sectionName, name) == 0)
29 {
30 return shdr;
31 }
32 }
33
34 return NULL;
35}
const char * elf64_get_string(const Elf64_File *elf, Elf64_Xword strTabIndex, Elf64_Off offset)
Get a string from the string table section at the given offset.
#define ELF64_GET_SHDR(elf, index)
Get the section header at the given index from an ELF file.
Definition elf.h:805
Elf64_Shdr * elf64_get_section_by_name(const Elf64_File *elf, const char *name)
Get a section by its name.
@ SHN_XINDEX
Indicates that the actual index is too large to fit and is stored elsewhere.
Definition elf.h:454
@ SHN_UNDEF
Undefined section.
Definition elf.h:446
#define NULL
Pointer error value.
Definition NULL.h:23
#define elf_strcmp
Definition elf.h:34
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
ELF64 Header.
Definition elf.h:95
Elf64_Half e_shnum
Definition elf.h:114
Elf64_Half e_shstrndx
Definition elf.h:123
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 Section Header.
Definition elf.h:468
Elf64_Word sh_name
Index of the section name in the string table.
Definition elf.h:469
Elf64_Word sh_link
Definition elf.h:475