Debian Codegraph @

src/lib/libdwarf/dwarf_elfstructs.h

raw
1 /*
2 Copyright (c) 2021-2023, David Anderson
3 All rights reserved.
4 or without modification, are permitted provided that the
5 following conditions are met:
6
7 Redistributions of source code must retain the above
8 copyright notice, this list of conditions and the following
9 disclaimer.
10
11 Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials
14 provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
17 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /* Typed in from the SystemV Application Binary Interface
32 but using char arrays instead of variables as
33 for reading we don't need the struct members to be
34 variables. This simplifies configure.
35
36 https://www.uclibc.org/docs/elf-64-gen.pdf used as source
37 of Elf64 fields.
38
39 It is expected code including this will have included
40 an official <elf.h> (for various definitions needed)
41 before including this. But that is not strictly necessary
42 given other headers.
43
44 The structs were all officially defined so files
45 could be mapped in. Fields are arranged so
46 there will not be gaps and we need not deal with
47 alignment-gaps.
48 */
49
50 #ifndef DW_ELFSTRUCTS_H
51 #define DW_ELFSTRUCTS_H
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif /* __cplusplus */
56
57 #ifndef EI_NIDENT
58 #define EI_NIDENT 16
59 #endif
60
61 #ifndef TYP
62 #define TYP(n,l) char (n)[(l)]
63 #endif
64
65 typedef struct
66 {
67 unsigned char e_ident[EI_NIDENT];
68 TYP(e_type,2);
69 TYP(e_machine,2);
70 TYP(e_version,4);
71 TYP(e_entry,4);
72 TYP(e_phoff,4);
73 TYP(e_shoff,4);
74 TYP(e_flags,4);
75 TYP(e_ehsize,2);
76 TYP(e_phentsize,2);
77 TYP(e_phnum,2);
78 TYP(e_shentsize,2);
79 TYP(e_shnum,2);
80 TYP(e_shstrndx,2);
81 } dw_elf32_ehdr;
82
83 typedef struct
84 {
85 unsigned char e_ident[EI_NIDENT];
86 TYP(e_type,2);
87 TYP(e_machine,2);
88 TYP(e_version,4);
89 TYP(e_entry,8);
90 TYP(e_phoff,8);
91 TYP(e_shoff,8);
92 TYP(e_flags,4);
93 TYP(e_ehsize,2);
94 TYP(e_phentsize,2);
95 TYP(e_phnum,2);
96 TYP(e_shentsize,2);
97 TYP(e_shnum,2);
98 TYP(e_shstrndx,2);
99 } dw_elf64_ehdr;
100
101 typedef struct
102 {
103 TYP(p_type,4);
104 TYP(p_offset,4);
105 TYP(p_vaddr,4);
106 TYP(p_paddr,4);
107 TYP(p_filesz,4);
108 TYP(p_memsz,4);
109 TYP(p_flags,4);
110 TYP(p_align,4);
111 } dw_elf32_phdr;
112
113 typedef struct
114 {
115 TYP(p_type,4);
116 TYP(p_flags,4);
117 TYP(p_offset,8);
118 TYP(p_vaddr,8);
119 TYP(p_paddr,8);
120 TYP(p_filesz,8);
121 TYP(p_memsz,8);
122 TYP(p_align,8);
123 } dw_elf64_phdr;
124
125 typedef struct
126 {
127 TYP(sh_name,4);
128 TYP(sh_type,4);
129 TYP(sh_flags,4);
130 TYP(sh_addr,4);
131 TYP(sh_offset,4);
132 TYP(sh_size,4);
133 TYP(sh_link,4);
134 TYP(sh_info,4);
135 TYP(sh_addralign,4);
136 TYP(sh_entsize,4);
137 } dw_elf32_shdr;
138
139 typedef struct
140 {
141 TYP(sh_name,4);
142 TYP(sh_type,4);
143 TYP(sh_flags,8);
144 TYP(sh_addr,8);
145 TYP(sh_offset,8);
146 TYP(sh_size,8);
147 TYP(sh_link,4);
148 TYP(sh_info,4);
149 TYP(sh_addralign,8);
150 TYP(sh_entsize,8);
151 } dw_elf64_shdr;
152
153 typedef struct
154 {
155 TYP(r_offset,4);
156 TYP(r_info,4);
157 } dw_elf32_rel;
158
159 typedef struct
160 {
161 TYP(r_offset,8);
162 TYP(r_info,8);
163 } dw_elf64_rel;
164
165 typedef struct
166 {
167 TYP(r_offset,4);
168 TYP(r_info,4);
169 TYP(r_addend,4); /* signed */
170 } dw_elf32_rela;
171
172 typedef struct
173 {
174 TYP(r_offset,8);
175 TYP(r_info,8);
176 TYP(r_addend,8); /* signed */
177 } dw_elf64_rela;
178
179 typedef struct {
180 TYP(st_name,4);
181 TYP(st_value,4);
182 TYP(st_size,4);
183 unsigned char st_info[1];
184 unsigned char st_other[1];
185 TYP(st_shndx,2);
186 } dw_elf32_sym;
187
188 typedef struct {
189 TYP(st_name,4);
190 unsigned char st_info[1];
191 unsigned char st_other[1];
192 TYP(st_shndx,2);
193 TYP(st_value,8);
194 TYP(st_size,8);
195 } dw_elf64_sym;
196
197 typedef struct
198 {
199 TYP(d_tag,4); /* signed */
200 TYP(d_val,4); /* Union in original */
201 } dw_elf32_dyn;
202
203 typedef struct
204 {
205 TYP(d_tag,8); /* signed */
206 TYP(d_val,8); /* Union in original */
207 } dw_elf64_dyn;
208
209 #ifdef __cplusplus
210 }
211 #endif /* __cplusplus */
212
213 #endif /* DW_ELFSTRUCTS_H */