xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_peread.h (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 #ifndef PE_GENERIC_H
2 #define PE_GENERIC_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 #ifdef __cplusplus
34 extern "C" {
35 #endif /* __cplusplus */
36 
37 
38 struct dwarf_pe_generic_file_header
39 {
40     Dwarf_Unsigned Machine;
41     Dwarf_Unsigned NumberOfSections;
42     Dwarf_Unsigned TimeDateStamp;
43     Dwarf_Unsigned PointerToSymbolTable;
44     Dwarf_Unsigned NumberOfSymbols;
45     Dwarf_Unsigned SizeOfOptionalHeader; /* in object file */
46     Dwarf_Unsigned Characteristics;
47 };
48 
49 struct dwarf_pe_generic_data_directory
50 {
51     Dwarf_Unsigned VirtualAddress;
52     Dwarf_Unsigned Size;
53 };
54 
55 #define DWARF_PE_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES 16
56 struct dwarf_pe_generic_optional_header
57 {
58     Dwarf_Unsigned Magic;
59     unsigned char MajorLinkerVersion;
60     unsigned char MinorLinkerVersion;
61     Dwarf_Unsigned SizeOfCode;
62     Dwarf_Unsigned SizeOfInitializedData;
63     Dwarf_Unsigned SizeOfUninitializedData;
64     Dwarf_Unsigned AddressOfEntryPoint;
65     Dwarf_Unsigned BaseOfCode;
66     Dwarf_Unsigned BaseOfData;
67     Dwarf_Unsigned ImageBase;
68     Dwarf_Unsigned SectionAlignment;
69     Dwarf_Unsigned FileAlignment;
70     Dwarf_Unsigned MajorOperatingSystemVersion;
71     Dwarf_Unsigned MinorOperatingSystemVersion;
72     Dwarf_Unsigned MajorImageVersion;
73     Dwarf_Unsigned MinorImageVersion;
74     Dwarf_Unsigned MajorSubsystemVersion;
75     Dwarf_Unsigned MinorSubsystemVersion;
76     Dwarf_Unsigned Win32VersionValue;
77     Dwarf_Unsigned SizeOfImage; /* size in object file */
78     Dwarf_Unsigned SizeOfHeaders; /* size in object file */
79     Dwarf_Unsigned CheckSum;
80     Dwarf_Unsigned Subsystem;
81     Dwarf_Unsigned DllCharacteristics;
82     Dwarf_Unsigned SizeOfStackReserve;
83     Dwarf_Unsigned SizeOfStackCommit;
84     Dwarf_Unsigned SizeOfHeapReserve;
85     Dwarf_Unsigned SizeOfHeapCommit;
86     Dwarf_Unsigned LoaderFlags;
87     Dwarf_Unsigned NumberOfRvaAndSizes;
88     Dwarf_Unsigned SizeOfDataDirEntry; /* size in object file */
89     struct dwarf_pe_generic_data_directory DataDirectory[DWARF_PE_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES];
90 };
91 
92 struct dwarf_pe_generic_image_section_header
93 {
94     char *name; /* Name must be freed */
95     char *dwarfsectname; /* Name must be freed */
96     Dwarf_Unsigned SecHeaderOffset; /* offset in object file */
97         /* union { */
98         /* Dwarf_Unsigned PhysicalAddress; */
99     Dwarf_Unsigned VirtualSize;
100         /* } Misc; */
101     Dwarf_Unsigned VirtualAddress;
102     Dwarf_Unsigned SizeOfRawData; /* size we need */
103     Dwarf_Unsigned PointerToRawData;
104     Dwarf_Unsigned PointerToRelocations;
105     Dwarf_Unsigned PointerToLinenumbers;
106     Dwarf_Unsigned NumberOfRelocations;
107     Dwarf_Unsigned NumberOfLinenumbers;
108     Dwarf_Unsigned Characteristics;
109     Dwarf_Small *  loaded_data; /* must be freed. */
110 };
111 
112 #define DWARF_PE_IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
113 #define DWARF_PE_IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
114 #define DWARF_PE_IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
115 
116 /*  ident[0] == 'P' means this is a PE header.
117     ident[1] will be 1 indicating version 1.
118     Other bytes in ident not defined, should be zero. */
119 typedef struct pe_filedata_s {
120     char             pe_ident[8];
121     const char *     pe_path; /* must free.*/
122     int              pe_fd;
123     int              pe_destruct_close_fd; /*aka: lib owns fd */
124     int              pe_is_64bit;
125     Dwarf_Unsigned   pe_filesize;
126     Dwarf_Small      pe_offsetsize; /* 32 or 64 section data */
127     Dwarf_Small      pe_pointersize;
128     int              pe_ftype;
129     unsigned         pe_endian;
130     /*Dwarf_Small      pe_machine; */
131     void (*pe_copy_word) (void *, const void *, unsigned long);
132     Dwarf_Unsigned   pe_nt_header_offset;
133     Dwarf_Unsigned   pe_optional_header_offset;
134     Dwarf_Unsigned   pe_optional_header_size;
135     Dwarf_Unsigned   pe_symbol_table_offset;
136     Dwarf_Unsigned   pe_string_table_offset;
137     Dwarf_Unsigned   pe_section_table_offset;
138     Dwarf_Unsigned pe_signature;
139 
140     struct dwarf_pe_generic_file_header pe_FileHeader;
141 
142     struct dwarf_pe_generic_optional_header pe_OptionalHeader;
143 
144     Dwarf_Unsigned pe_section_count;
145     struct dwarf_pe_generic_image_section_header *pe_sectionptr;
146 
147     Dwarf_Unsigned pe_string_table_size;
148     char          *pe_string_table;
149 } dwarf_pe_object_access_internals_t;
150 
151 #ifdef __cplusplus
152 }
153 #endif /* __cplusplus */
154 
155 #endif /* PE_GENERIC_H */
156