Debian Codegraph @

src/bin/dwarfgen/dwarf_elfstructs.h

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