libtransistor
A userland library for the Nintendo Switch
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
elf.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include<libtransistor/types.h>
9 #include<assert.h>
10 #include<stdint.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef struct {
17  int64_t d_tag;
18  union {
19  uint64_t d_val;
20  void *d_ptr;
21  };
22 } Elf64_Dyn;
23 
24 typedef struct {
25  uint64_t r_offset;
26  uint32_t r_reloc_type;
27  uint32_t r_symbol;
28  uint64_t r_addend;
29 } Elf64_Rela;
30 
31 typedef struct {
32  uint64_t r_offset;
33  uint32_t r_reloc_type;
34  uint32_t r_symbol;
35 } Elf64_Rel;
36 
37 typedef struct {
38  uint32_t st_name;
39  uint8_t st_info;
40  uint8_t st_other;
41  uint16_t st_shndx;
42  uint64_t st_value;
43  uint64_t st_size;
44 } Elf64_Sym;
45 
46 enum {
47  EI_MAG0,
48  EI_MAG1,
49  EI_MAG2,
50  EI_MAG3,
51  EI_CLASS,
52  EI_DATA,
53  EI_VERSION,
54  EI_OSABI,
55  EI_ABIVERSION,
56  EI_NIDENT = 16
57 };
58 
59 typedef struct {
60  uint8_t e_ident[EI_NIDENT];
61  uint16_t e_type;
62  uint16_t e_machine;
63  uint32_t e_version;
64  uint64_t e_entry;
65  uint64_t e_phoff;
66  uint64_t e_shoff;
67  uint32_t e_flags;
68  uint16_t e_ehsize;
69  uint16_t e_phentsize;
70  uint16_t e_phnum;
71  uint16_t e_shentsize;
72  uint16_t e_shnum;
73  uint16_t e_shstrndx;
74 } Elf64_Ehdr;
75 
76 enum {
77  PF_X = 0x1,
78  PF_W = 0x2,
79  PF_R = 0x4,
80 };
81 
82 enum {
83  PT_NULL,
84  PT_LOAD,
85  PT_DYNAMIC
86 };
87 
88 typedef struct {
89  uint32_t p_type;
90  uint32_t p_flags;
91  uint64_t p_offset;
92  uint64_t p_vaddr;
93  uint64_t p_paddr;
94  uint64_t p_filesz;
95  uint64_t p_memsz;
96  uint64_t p_align;
97 } Elf64_Phdr;
98 
99 #define STN_UNDEF 0
100 #define SHN_UNDEF 0
101 
102 static_assert(sizeof(Elf64_Rela) == 0x18, "Elf64_Rela size should be 0x18");
103 
104 enum {
105  DT_NULL = 0,
106  DT_NEEDED = 1,
107  DT_PLTRELSZ = 2,
108  DT_PLTGOT = 3,
109  DT_HASH = 4,
110  DT_STRTAB = 5,
111  DT_SYMTAB = 6,
112  DT_RELA = 7,
113  DT_RELASZ = 8,
114  DT_RELAENT = 9,
115  DT_STRSZ = 10,
116  DT_SYMENT = 11,
117  // 12-15
118  DT_SYMBOLIC = 16,
119  DT_REL = 17,
120  DT_RELSZ = 18,
121  DT_RELENT = 19,
122  DT_PLTREL = 20,
123  // 21-22
124  DT_JMPREL = 23,
125  // 24
126  DT_INIT_ARRAY = 25,
127  DT_FINI_ARRAY = 26,
128  DT_INIT_ARRAYSZ = 27,
129  DT_FINI_ARRAYSZ = 28,
130  // 29
131  DT_FLAGS = 30,
132  DT_GNU_HASH = 0x6ffffef5,
133  DT_RELACOUNT = 0x6ffffff9,
134 };
135 
140 result_t elf_dynamic_find_value(Elf64_Dyn *dynamic, int64_t tag, uint64_t *value);
141 
146 result_t elf_dynamic_find_offset(Elf64_Dyn *dynamic, int64_t tag, void **value, void *base);
147 
151 uint64_t elf_hash_string(const char *string);
152 
153 #ifdef __cplusplus
154 }
155 #endif
Various system types.
uint64_t elf_hash_string(const char *string)
Hashes a string for lookup in the .hash table.
Definition: elf.h:24
uint32_t result_t
Function result.
Definition: types.h:51
result_t elf_dynamic_find_value(Elf64_Dyn *dynamic, int64_t tag, uint64_t *value)
Finds the value of the given tag in a dynamic section.
Definition: elf.h:16
Definition: elf.h:31
Definition: elf.h:88
Definition: elf.h:37
Definition: elf.h:59
result_t elf_dynamic_find_offset(Elf64_Dyn *dynamic, int64_t tag, void **value, void *base)
Finds the value of the given tag in the dynamic section, treated as an offset from a given base...