xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_pe_descr.h (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1 #ifndef DWARF_PE_DESCR_H
2 #define DWARF_PE_DESCR_H
3 /*
4 Copyright (c) 2018, David Anderson All rights reserved.
5 
6 Redistribution and use in source and binary forms, with
7 or without modification, are permitted provided that the
8 following conditions are met:
9 
10     Redistributions of source code must retain the above
11     copyright notice, this list of conditions and the following
12     disclaimer.
13 
14     Redistributions in binary form must reproduce the above
15     copyright notice, this list of conditions and the following
16     disclaimer in the documentation and/or other materials
17     provided with the distribution.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37 
38 #define IMAGE_DOS_SIGNATURE_dw    0x5a4d /* le on disk 'M' 'Z' */
39 #define IMAGE_DOS_REVSIGNATURE_dw 0x4d5a /* be on disk */
40 #define IMAGE_NT_SIGNATURE_dw     0x00004550
41 
42 #ifndef TYP
43 #define TYP(n,l) char n[l]
44 #endif /* TYPE */
45 
46 
47 /*  Data types
48     see https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx */
49 
50 /*#define FIELD_OFFSET(type,field) ((LONG)(LONG_PTR)&(((type *)0)->field))*/
51 
52 #define IMAGE_SIZEOF_SYMBOL 18
53 
54 struct dos_header_dw {
55     TYP(dh_mz,2);
56     TYP(dh_dos_data,58);
57     TYP(dh_image_offset,4);
58 };
59 
60 /*  IMAGE_FILE_HEADER_dw
61     see https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms680313(v=vs.85).aspx */
62 
63 typedef struct
64 {
65     TYP(Machine,2);
66     TYP(NumberOfSections,2);
67     TYP(TimeDateStamp,4);
68     TYP(PointerToSymbolTable,4);
69     TYP(NumberOfSymbols,4);
70     TYP(SizeOfOptionalHeader,2);
71     TYP(Characteristics,2);
72 } IMAGE_FILE_HEADER_dw;
73 
74 /*  IMAGE_DATA_DIRECTORY_dw
75     see https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms680305(v=vs.85).aspx */
76 
77 typedef struct
78 {
79     TYP(VirtualAddress,4);
80     TYP(Size,4);
81 } IMAGE_DATA_DIRECTORY_dw;
82 
83 /*  IMAGE_OPTIONAL_HEADER
84     see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx */
85 
86 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
87 
88 typedef struct
89 {
90     TYP(Magic,2);
91     unsigned char MajorLinkerVersion;
92     unsigned char MinorLinkerVersion;
93     TYP(SizeOfCode,4);
94     TYP(SizeOfInitializedData,4);
95     TYP(SizeOfUninitializedData,4);
96     TYP(AddressOfEntryPoint,4);
97     TYP(BaseOfCode,4);
98     TYP(BaseOfData,4);
99     TYP(ImageBase,4);
100     TYP(SectionAlignment,4);
101     TYP(FileAlignment,4);
102     TYP(MajorOperatingSystemVersion,2);
103     TYP(MinorOperatingSystemVersion,2);
104     TYP(MajorImageVersion,2);
105     TYP(MinorImageVersion,2);
106     TYP(MajorSubsystemVersion,2);
107     TYP(MinorSubsystemVersion,2);
108     TYP(Win32VersionValue,4);
109     TYP(SizeOfImage,4);
110     TYP(SizeOfHeaders,4);
111     TYP(CheckSum,4);
112     TYP(Subsystem,2);
113     TYP(DllCharacteristics,2);
114     TYP(SizeOfStackReserve,4);
115     TYP(SizeOfStackCommit,4);
116     TYP(SizeOfHeapReserve,4);
117     TYP(SizeOfHeapCommit,4);
118     TYP(LoaderFlags,4);
119     TYP(NumberOfRvaAndSizes,4);
120     IMAGE_DATA_DIRECTORY_dw DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
121 } IMAGE_OPTIONAL_HEADER32_dw;
122 
123 typedef struct
124 {
125     TYP(Magic,2);
126     unsigned char MajorLinkerVersion;
127     unsigned char MinorLinkerVersion;
128     TYP(SizeOfCode,4);
129     TYP(SizeOfInitializedData,4);
130     TYP(SizeOfUninitializedData,4);
131     TYP(AddressOfEntryPoint,4);
132     TYP(BaseOfCode,4);
133     TYP(ImageBase,8);
134     TYP(SectionAlignment,4);
135     TYP(FileAlignment,4);
136     TYP(MajorOperatingSystemVersion,2);
137     TYP(MinorOperatingSystemVersion,2);
138     TYP(MajorImageVersion,2);
139     TYP(MinorImageVersion,2);
140     TYP(MajorSubsystemVersion,2);
141     TYP(MinorSubsystemVersion,2);
142     TYP(Win32VersionValue,4);
143     TYP(SizeOfImage,4);
144     TYP(SizeOfHeaders,4);
145     TYP(CheckSum,4);
146     TYP(Subsystem,2);
147     TYP(DllCharacteristics,2);
148     TYP(SizeOfStackReserve,8);
149     TYP(SizeOfStackCommit,8);
150     TYP(SizeOfHeapReserve,8);
151     TYP(SizeOfHeapCommit,8);
152     TYP(LoaderFlags,4);
153     TYP(NumberOfRvaAndSizes,4);
154     IMAGE_DATA_DIRECTORY_dw DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
155 } IMAGE_OPTIONAL_HEADER64_dw;
156 
157 /*  IMAGE_NT_HEADERS
158     see https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms680336(v=vs.85).aspx */
159 
160 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
161 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
162 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
163 
164 typedef struct
165 {
166     TYP(Signature,4);
167     IMAGE_FILE_HEADER_dw FileHeader;
168     IMAGE_OPTIONAL_HEADER64_dw OptionalHeader;
169 } IMAGE_NT_HEADERS64_dw, *PIMAGE_NT_HEADERS64_dw;
170 
171 typedef struct
172 {
173     TYP(Signature,4);
174     IMAGE_FILE_HEADER_dw FileHeader;
175     IMAGE_OPTIONAL_HEADER32_dw OptionalHeader;
176 } IMAGE_NT_HEADERS32_dw, *PIMAGE_NT_HEADERS32_dw;
177 
178 
179 /*  IMAGE_SECTION_HEADER_dw
180     see:
181     https://msdn.microsoft.com/en-us/library/windows/desktop/ms680341(v=vs.85).aspx
182     and, for details on VirtualSize and SizeOfRawData:
183     https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_section_header */
184 
185 #define IMAGE_SIZEOF_SHORT_NAME 8
186 
187 typedef struct
188 {
189     char Name[IMAGE_SIZEOF_SHORT_NAME];
190     union {
191         TYP(PhysicalAddress,4);
192         TYP(VirtualSize,4);
193     } Misc;
194     TYP(VirtualAddress,4);
195     TYP(SizeOfRawData,4);
196     TYP(PointerToRawData,4);
197     TYP(PointerToRelocations,4);
198     TYP(PointerToLinenumbers,4);
199     TYP(NumberOfRelocations,2);
200     TYP(NumberOfLinenumbers,2);
201     TYP(Characteristics,4);
202 } IMAGE_SECTION_HEADER_dw, *PIMAGE_SECTION_HEADER_dw;
203 
204 #define IMAGE_SCN_SCALE_INDEX            0x00000001
205 #define IMAGE_SCN_TYPE_NO_PAD            0x00000008
206 #define IMAGE_SCN_CNT_CODE               0x00000020
207 #define IMAGE_SCN_CNT_INITIALIZED_DATA   0x00000040
208 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
209 
210 #define IMAGE_SCN_LNK_OTHER              0x00000100
211 #define IMAGE_SCN_LNK_INFO               0x00000200
212 #define IMAGE_SCN_LNK_REMOVE             0x00000800
213 #define IMAGE_SCN_LNK_COMDAT             0x00001000
214 #define IMAGE_SCN_NO_DEFER_SPEC_EXC      0x00004000
215 #define IMAGE_SCN_MEM_FARDATA            0x00008000
216 #define IMAGE_SCN_MEM_PURGEABLE          0x00020000
217 #define IMAGE_SCN_MEM_LOCKED             0x00040000
218 #define IMAGE_SCN_MEM_PRELOAD            0x00080000
219 
220 #define IMAGE_SCN_ALIGN_1BYTES           0x00100000
221 #define IMAGE_SCN_ALIGN_2BYTES           0x00200000
222 #define IMAGE_SCN_ALIGN_4BYTES           0x00300000
223 #define IMAGE_SCN_ALIGN_8BYTES           0x00400000
224 #define IMAGE_SCN_ALIGN_16BYTES          0x00500000
225 #define IMAGE_SCN_ALIGN_32BYTES          0x00600000
226 #define IMAGE_SCN_ALIGN_64BYTES          0x00700000
227 #define IMAGE_SCN_ALIGN_128BYTES         0x00800000
228 #define IMAGE_SCN_ALIGN_256BYTES         0x00900000
229 #define IMAGE_SCN_ALIGN_512BYTES         0x00A00000
230 #define IMAGE_SCN_ALIGN_1024BYTES        0x00B00000
231 #define IMAGE_SCN_ALIGN_2048BYTES        0x00C00000
232 #define IMAGE_SCN_ALIGN_4096BYTES        0x00D00000
233 #define IMAGE_SCN_ALIGN_8192BYTES        0x00E00000
234 
235 #define IMAGE_SCN_ALIGN_MASK             0x00F00000
236 #define IMAGE_SCN_LNK_NRELOC_OVFL        0x01000000
237 #define IMAGE_SCN_MEM_DISCARDABLE        0x02000000
238 #define IMAGE_SCN_MEM_NOT_CACHED         0x04000000
239 #define IMAGE_SCN_MEM_NOT_PAGED          0x08000000
240 #define IMAGE_SCN_MEM_SHARED             0x10000000
241 #define IMAGE_SCN_MEM_EXECUTE            0x20000000
242 #define IMAGE_SCN_MEM_READ               0x40000000
243 #define IMAGE_SCN_MEM_WRITE              0x80000000
244 
245 #ifdef __cplusplus
246 }
247 #endif /* __cplusplus */
248 
249 
250 #endif /* DWARF_PE_DESCR_H */
251