xref: /illumos-gate/usr/src/lib/libdwarf/common/dw_elfstructs.h (revision d464f34577edaa31dd6978ec04d66a57529dd2c8)
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.
44 
45     The structs were all officially defined so files
46     could be mapped in. Fields are arranged so
47     there will not be gaps and we need not deal with
48     alignment-gaps.
49 */
50 
51 #ifndef EI_NIDENT
52 #define EI_NIDENT 16
53 #endif
54 
55 #ifndef TYP
56 #define TYP(n,l) char n[l]
57 #endif
58 
59 typedef struct
60 {
61     unsigned char e_ident[EI_NIDENT];
62     TYP(e_type,2);
63     TYP(e_machine,2);
64     TYP(e_version,4);
65     TYP(e_entry,4);
66     TYP(e_phoff,4);
67     TYP(e_shoff,4);
68     TYP(e_flags,4);
69     TYP(e_ehsize,2);
70     TYP(e_phentsize,2);
71     TYP(e_phnum,2);
72     TYP(e_shentsize,2);
73     TYP(e_shnum,2);
74     TYP(e_shstrndx,2);
75 } dw_elf32_ehdr;
76 
77 typedef struct
78 {
79     unsigned char e_ident[EI_NIDENT];
80     TYP(e_type,2);
81     TYP(e_machine,2);
82     TYP(e_version,4);
83     TYP(e_entry,8);
84     TYP(e_phoff,8);
85     TYP(e_shoff,8);
86     TYP(e_flags,4);
87     TYP(e_ehsize,2);
88     TYP(e_phentsize,2);
89     TYP(e_phnum,2);
90     TYP(e_shentsize,2);
91     TYP(e_shnum,2);
92     TYP(e_shstrndx,2);
93 } dw_elf64_ehdr;
94 
95 typedef struct
96 {
97     TYP(p_type,4);
98     TYP(p_offset,4);
99     TYP(p_vaddr,4);
100     TYP(p_paddr,4);
101     TYP(p_filesz,4);
102     TYP(p_memsz,4);
103     TYP(p_flags,4);
104     TYP(p_align,4);
105 } dw_elf32_phdr;
106 
107 typedef struct
108 {
109     TYP(p_type,4);
110     TYP(p_flags,4);
111     TYP(p_offset,8);
112     TYP(p_vaddr,8);
113     TYP(p_paddr,8);
114     TYP(p_filesz,8);
115     TYP(p_memsz,8);
116     TYP(p_align,8);
117 } dw_elf64_phdr;
118 
119 typedef struct
120 {
121     TYP(sh_name,4);
122     TYP(sh_type,4);
123     TYP(sh_flags,4);
124     TYP(sh_addr,4);
125     TYP(sh_offset,4);
126     TYP(sh_size,4);
127     TYP(sh_link,4);
128     TYP(sh_info,4);
129     TYP(sh_addralign,4);
130     TYP(sh_entsize,4);
131 }dw_elf32_shdr;
132 
133 typedef struct
134 {
135     TYP(sh_name,4);
136     TYP(sh_type,4);
137     TYP(sh_flags,8);
138     TYP(sh_addr,8);
139     TYP(sh_offset,8);
140     TYP(sh_size,8);
141     TYP(sh_link,4);
142     TYP(sh_info,4);
143     TYP(sh_addralign,8);
144     TYP(sh_entsize,8);
145 }dw_elf64_shdr;
146 
147 typedef struct
148 {
149     TYP(r_offset,4);
150     TYP(r_info,4);
151 } dw_elf32_rel;
152 
153 typedef struct
154 {
155     TYP(r_offset,8);
156     TYP(r_info,8);
157 } dw_elf64_rel;
158 
159 typedef struct
160 {
161     TYP(r_offset,4);
162     TYP(r_info,4);
163     TYP(r_addend,4); /* signed */
164 } dw_elf32_rela;
165 
166 typedef struct
167 {
168     TYP(r_offset,8);
169     TYP(r_info,8);
170     TYP(r_addend,8); /* signed */
171 } dw_elf64_rela;
172 
173 
174 typedef struct {
175     TYP(st_name,4);
176     TYP(st_value,4);
177     TYP(st_size,4);
178     unsigned char st_info[1];
179     unsigned char st_other[1];
180     TYP(st_shndx,2);
181 } dw_elf32_sym;
182 
183 typedef struct {
184     TYP(st_name,4);
185     unsigned char st_info[1];
186     unsigned char st_other[1];
187     TYP(st_shndx,2);
188     TYP(st_value,8);
189     TYP(st_size,8);
190 } dw_elf64_sym;
191 
192 
193 typedef struct
194 {
195     TYP(d_tag,4); /* signed */
196     TYP(d_val,4); /* Union in original */
197 } dw_elf32_dyn;
198 
199 typedef struct
200 {
201     TYP(d_tag,8); /* signed */
202     TYP(d_val,8); /* Union in original */
203 } dw_elf64_dyn;
204