blob: 17f5f4de2d1752feec08683c696d58e02152d7f0 [file] [log] [blame]
Greg Claytonf754f882011-09-09 20:33:05 +00001//===-- ObjectFilePECOFF.h --------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Greg Claytonf754f882011-09-09 20:33:05 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_ObjectFilePECOFF_h_
10#define liblldb_ObjectFilePECOFF_h_
11
12#include <vector>
13
Greg Claytonf754f882011-09-09 20:33:05 +000014#include "lldb/Symbol/ObjectFile.h"
Aaron Smith037ed1b2018-12-06 21:36:39 +000015#include "llvm/Object/Binary.h"
Greg Claytonf754f882011-09-09 20:33:05 +000016
Kate Stoneb9c1b512016-09-06 20:57:50 +000017class ObjectFilePECOFF : public lldb_private::ObjectFile {
Greg Claytonf754f882011-09-09 20:33:05 +000018public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000019 typedef enum MachineType {
20 MachineUnknown = 0x0,
21 MachineAm33 = 0x1d3,
22 MachineAmd64 = 0x8664,
23 MachineArm = 0x1c0,
24 MachineArmNt = 0x1c4,
25 MachineArm64 = 0xaa64,
26 MachineEbc = 0xebc,
27 MachineX86 = 0x14c,
28 MachineIA64 = 0x200,
29 MachineM32R = 0x9041,
30 MachineMips16 = 0x266,
31 MachineMipsFpu = 0x366,
32 MachineMipsFpu16 = 0x466,
33 MachinePowerPc = 0x1f0,
34 MachinePowerPcfp = 0x1f1,
35 MachineR4000 = 0x166,
36 MachineSh3 = 0x1a2,
37 MachineSh3dsp = 0x1a3,
38 MachineSh4 = 0x1a6,
39 MachineSh5 = 0x1a8,
40 MachineThumb = 0x1c2,
41 MachineWcemIpsv2 = 0x169
42 } MachineType;
Eugene Zelenko8157a882015-10-23 16:56:07 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 ObjectFilePECOFF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
45 lldb::offset_t data_offset,
46 const lldb_private::FileSpec *file,
47 lldb::offset_t file_offset, lldb::offset_t length);
Eugene Zelenko8157a882015-10-23 16:56:07 +000048
Walter Erquinigo344546b2016-10-17 20:28:19 +000049 ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
50 lldb::DataBufferSP &header_data_sp,
51 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 ~ObjectFilePECOFF() override;
Greg Claytonf4d6de62013-04-24 22:29:28 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 //------------------------------------------------------------------
56 // Static Functions
57 //------------------------------------------------------------------
58 static void Initialize();
Adrian McCarthyf7d18932015-11-20 23:09:11 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 static void Terminate();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 static lldb_private::ConstString GetPluginNameStatic();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 static const char *GetPluginDescriptionStatic();
Virgile Bello2756adf2014-03-08 17:17:20 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 static ObjectFile *
67 CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
68 lldb::offset_t data_offset, const lldb_private::FileSpec *file,
69 lldb::offset_t offset, lldb::offset_t length);
Adrian McCarthyc35b91c2016-01-26 00:58:09 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 static lldb_private::ObjectFile *CreateMemoryInstance(
72 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
73 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
Greg Clayton3046e662013-07-10 01:23:25 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
76 lldb::DataBufferSP &data_sp,
77 lldb::offset_t data_offset,
78 lldb::offset_t file_offset,
79 lldb::offset_t length,
80 lldb_private::ModuleSpecList &specs);
Eugene Zelenko8157a882015-10-23 16:56:07 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 static bool SaveCore(const lldb::ProcessSP &process_sp,
83 const lldb_private::FileSpec &outfile,
Zachary Turner97206d52017-05-12 04:51:55 +000084 lldb_private::Status &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +000085
86 static bool MagicBytesMatch(lldb::DataBufferSP &data_sp);
87
88 static lldb::SymbolType MapSymbolType(uint16_t coff_symbol_type);
89
90 bool ParseHeader() override;
91
92 bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
93 bool value_is_offset) override;
94
95 lldb::ByteOrder GetByteOrder() const override;
96
97 bool IsExecutable() const override;
98
99 uint32_t GetAddressByteSize() const override;
100
101 // virtual lldb_private::AddressClass
102 // GetAddressClass (lldb::addr_t file_addr);
103
104 lldb_private::Symtab *GetSymtab() override;
105
106 bool IsStripped() override;
107
108 void CreateSections(lldb_private::SectionList &unified_section_list) override;
109
110 void Dump(lldb_private::Stream *s) override;
111
Pavel Labathf760f5a2019-01-03 10:37:19 +0000112 lldb_private::ArchSpec GetArchitecture() override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113
Pavel Labathbd334ef2019-02-11 16:14:02 +0000114 lldb_private::UUID GetUUID() override;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115
116 uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
117
118 virtual lldb_private::Address GetEntryPointAddress() override;
119
120 ObjectFile::Type CalculateType() override;
121
122 ObjectFile::Strata CalculateStrata() override;
123
124 //------------------------------------------------------------------
125 // PluginInterface protocol
126 //------------------------------------------------------------------
127 lldb_private::ConstString GetPluginName() override;
128
129 uint32_t GetPluginVersion() override;
Eugene Zelenko8157a882015-10-23 16:56:07 +0000130
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +0000131 bool IsWindowsSubsystem();
132
Walter Erquinigo344546b2016-10-17 20:28:19 +0000133 lldb_private::DataExtractor ReadImageData(uint32_t offset, size_t size);
134
Greg Claytonf754f882011-09-09 20:33:05 +0000135protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 bool NeedsEndianSwap() const;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 typedef struct dos_header { // DOS .EXE header
139 uint16_t e_magic; // Magic number
140 uint16_t e_cblp; // Bytes on last page of file
141 uint16_t e_cp; // Pages in file
142 uint16_t e_crlc; // Relocations
143 uint16_t e_cparhdr; // Size of header in paragraphs
144 uint16_t e_minalloc; // Minimum extra paragraphs needed
145 uint16_t e_maxalloc; // Maximum extra paragraphs needed
146 uint16_t e_ss; // Initial (relative) SS value
147 uint16_t e_sp; // Initial SP value
148 uint16_t e_csum; // Checksum
149 uint16_t e_ip; // Initial IP value
150 uint16_t e_cs; // Initial (relative) CS value
151 uint16_t e_lfarlc; // File address of relocation table
152 uint16_t e_ovno; // Overlay number
153 uint16_t e_res[4]; // Reserved words
154 uint16_t e_oemid; // OEM identifier (for e_oeminfo)
155 uint16_t e_oeminfo; // OEM information; e_oemid specific
156 uint16_t e_res2[10]; // Reserved words
157 uint32_t e_lfanew; // File address of new exe header
158 } dos_header_t;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 typedef struct coff_header {
161 uint16_t machine;
162 uint16_t nsects;
163 uint32_t modtime;
164 uint32_t symoff;
165 uint32_t nsyms;
166 uint16_t hdrsize;
167 uint16_t flags;
168 } coff_header_t;
169
170 typedef struct data_directory {
171 uint32_t vmaddr;
172 uint32_t vmsize;
173 } data_directory_t;
174
175 typedef struct coff_opt_header {
176 uint16_t magic;
177 uint8_t major_linker_version;
178 uint8_t minor_linker_version;
179 uint32_t code_size;
180 uint32_t data_size;
181 uint32_t bss_size;
182 uint32_t entry;
183 uint32_t code_offset;
184 uint32_t data_offset;
185
186 uint64_t image_base;
187 uint32_t sect_alignment;
188 uint32_t file_alignment;
189 uint16_t major_os_system_version;
190 uint16_t minor_os_system_version;
191 uint16_t major_image_version;
192 uint16_t minor_image_version;
193 uint16_t major_subsystem_version;
194 uint16_t minor_subsystem_version;
195 uint32_t reserved1;
196 uint32_t image_size;
197 uint32_t header_size;
198 uint32_t checksum;
199 uint16_t subsystem;
200 uint16_t dll_flags;
201 uint64_t stack_reserve_size;
202 uint64_t stack_commit_size;
203 uint64_t heap_reserve_size;
204 uint64_t heap_commit_size;
205 uint32_t loader_flags;
206 // uint32_t num_data_dir_entries;
207 std::vector<data_directory>
208 data_dirs; // will contain num_data_dir_entries entries
209 } coff_opt_header_t;
210
211 typedef enum coff_data_dir_type {
212 coff_data_dir_export_table = 0,
213 coff_data_dir_import_table = 1,
214 } coff_data_dir_type;
215
216 typedef struct section_header {
217 char name[8];
218 uint32_t vmsize; // Virtual Size
219 uint32_t vmaddr; // Virtual Addr
220 uint32_t size; // File size
221 uint32_t offset; // File offset
222 uint32_t reloff; // Offset to relocations
223 uint32_t lineoff; // Offset to line table entries
224 uint16_t nreloc; // Number of relocation entries
225 uint16_t nline; // Number of line table entries
226 uint32_t flags;
227 } section_header_t;
228
229 typedef struct coff_symbol {
230 char name[8];
231 uint32_t value;
232 uint16_t sect;
233 uint16_t type;
234 uint8_t storage;
235 uint8_t naux;
236 } coff_symbol_t;
237
238 typedef struct export_directory_entry {
239 uint32_t characteristics;
240 uint32_t time_date_stamp;
241 uint16_t major_version;
242 uint16_t minor_version;
243 uint32_t name;
244 uint32_t base;
245 uint32_t number_of_functions;
246 uint32_t number_of_names;
247 uint32_t address_of_functions;
248 uint32_t address_of_names;
249 uint32_t address_of_name_ordinals;
250 } export_directory_entry;
251
252 static bool ParseDOSHeader(lldb_private::DataExtractor &data,
253 dos_header_t &dos_header);
254 static bool ParseCOFFHeader(lldb_private::DataExtractor &data,
255 lldb::offset_t *offset_ptr,
256 coff_header_t &coff_header);
257 bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr);
258 bool ParseSectionHeaders(uint32_t offset);
259
Aaron Smith037ed1b2018-12-06 21:36:39 +0000260 uint32_t ParseDependentModules();
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 static void DumpDOSHeader(lldb_private::Stream *s,
263 const dos_header_t &header);
264 static void DumpCOFFHeader(lldb_private::Stream *s,
265 const coff_header_t &header);
266 static void DumpOptCOFFHeader(lldb_private::Stream *s,
267 const coff_opt_header_t &header);
268 void DumpSectionHeaders(lldb_private::Stream *s);
269 void DumpSectionHeader(lldb_private::Stream *s, const section_header_t &sh);
Aaron Smith037ed1b2018-12-06 21:36:39 +0000270 void DumpDependentModules(lldb_private::Stream *s);
271
Pavel Labath2886e4a2019-01-10 10:23:19 +0000272 llvm::StringRef GetSectionName(const section_header_t &sect);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273
274 typedef std::vector<section_header_t> SectionHeaderColl;
275 typedef SectionHeaderColl::iterator SectionHeaderCollIter;
276 typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
277
Greg Claytonf754f882011-09-09 20:33:05 +0000278private:
Aaron Smith037ed1b2018-12-06 21:36:39 +0000279 bool CreateBinary();
280
281private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 dos_header_t m_dos_header;
283 coff_header_t m_coff_header;
284 coff_opt_header_t m_coff_header_opt;
285 SectionHeaderColl m_sect_headers;
286 lldb::addr_t m_image_base;
287 lldb_private::Address m_entry_point_address;
Aaron Smith037ed1b2018-12-06 21:36:39 +0000288 llvm::Optional<lldb_private::FileSpecList> m_deps_filespec;
289 typedef llvm::object::OwningBinary<llvm::object::Binary> OWNBINType;
290 llvm::Optional<OWNBINType> m_owningbin;
Greg Claytonf754f882011-09-09 20:33:05 +0000291};
292
Eugene Zelenko8157a882015-10-23 16:56:07 +0000293#endif // liblldb_ObjectFilePECOFF_h_