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