1 //===-- ObjectFilePECOFF.h --------------------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_PECOFF_OBJECTFILEPECOFF_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_PECOFF_OBJECTFILEPECOFF_H
11 
12 #include <vector>
13 
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "llvm/Object/COFF.h"
16 
17 class ObjectFilePECOFF : public lldb_private::ObjectFile {
18 public:
19   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   };
43 
44   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);
48 
49   ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
50                    lldb::WritableDataBufferSP header_data_sp,
51                    const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
52 
53   ~ObjectFilePECOFF() override;
54 
55   // Static Functions
56   static void Initialize();
57 
58   static void DebuggerInitialize(lldb_private::Debugger &debugger);
59 
60   static void Terminate();
61 
GetPluginNameStatic()62   static llvm::StringRef GetPluginNameStatic() { return "pe-coff"; }
63 
64   static llvm::StringRef GetPluginDescriptionStatic();
65 
66   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);
70 
71   static lldb_private::ObjectFile *CreateMemoryInstance(
72       const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
73       const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
74 
75   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);
81 
82   static bool SaveCore(const lldb::ProcessSP &process_sp,
83                        const lldb_private::FileSpec &outfile,
84                        lldb::SaveCoreStyle &core_style,
85                        lldb_private::Status &error);
86 
87   static bool MagicBytesMatch(lldb::DataBufferSP data_sp);
88 
89   static lldb::SymbolType MapSymbolType(uint16_t coff_symbol_type);
90 
91   // LLVM RTTI support
92   static char ID;
isA(const void * ClassID)93   bool isA(const void *ClassID) const override {
94     return ClassID == &ID || ObjectFile::isA(ClassID);
95   }
classof(const ObjectFile * obj)96   static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
97 
98   bool ParseHeader() override;
99 
100   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
101                       bool value_is_offset) override;
102 
103   lldb::ByteOrder GetByteOrder() const override;
104 
105   bool IsExecutable() const override;
106 
107   uint32_t GetAddressByteSize() const override;
108 
109   //    virtual lldb_private::AddressClass
110   //    GetAddressClass (lldb::addr_t file_addr);
111 
112   void ParseSymtab(lldb_private::Symtab &symtab) override;
113 
114   bool IsStripped() override;
115 
116   void CreateSections(lldb_private::SectionList &unified_section_list) override;
117 
118   void Dump(lldb_private::Stream *s) override;
119 
120   lldb_private::ArchSpec GetArchitecture() override;
121 
122   lldb_private::UUID GetUUID() override;
123 
124   /// Return the contents of the .gnu_debuglink section, if the object file
125   /// contains it.
126   llvm::Optional<lldb_private::FileSpec> GetDebugLink();
127 
128   uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
129 
130   lldb_private::Address GetEntryPointAddress() override;
131 
132   lldb_private::Address GetBaseAddress() override;
133 
134   ObjectFile::Type CalculateType() override;
135 
136   ObjectFile::Strata CalculateStrata() override;
137 
138   // PluginInterface protocol
GetPluginName()139   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
140 
141   bool IsWindowsSubsystem();
142 
143   uint32_t GetRVA(const lldb_private::Address &addr) const;
144   lldb_private::Address GetAddress(uint32_t rva);
145   lldb::addr_t GetFileAddress(uint32_t rva) const;
146 
147   lldb_private::DataExtractor ReadImageData(uint32_t offset, size_t size);
148   lldb_private::DataExtractor ReadImageDataByRVA(uint32_t rva, size_t size);
149 
150   std::unique_ptr<lldb_private::CallFrameInfo> CreateCallFrameInfo() override;
151 
152 protected:
153   bool NeedsEndianSwap() const;
154 
155   typedef struct dos_header { // DOS .EXE header
156     uint16_t e_magic = 0;     // Magic number
157     uint16_t e_cblp = 0;      // Bytes on last page of file
158     uint16_t e_cp = 0;        // Pages in file
159     uint16_t e_crlc = 0;      // Relocations
160     uint16_t e_cparhdr = 0;   // Size of header in paragraphs
161     uint16_t e_minalloc = 0;  // Minimum extra paragraphs needed
162     uint16_t e_maxalloc = 0;  // Maximum extra paragraphs needed
163     uint16_t e_ss = 0;        // Initial (relative) SS value
164     uint16_t e_sp = 0;        // Initial SP value
165     uint16_t e_csum = 0;      // Checksum
166     uint16_t e_ip = 0;        // Initial IP value
167     uint16_t e_cs = 0;        // Initial (relative) CS value
168     uint16_t e_lfarlc = 0;    // File address of relocation table
169     uint16_t e_ovno = 0;      // Overlay number
170     uint16_t e_res[4];        // Reserved words
171     uint16_t e_oemid = 0;     // OEM identifier (for e_oeminfo)
172     uint16_t e_oeminfo = 0;   // OEM information; e_oemid specific
173     uint16_t e_res2[10] = {}; // Reserved words
174     uint32_t e_lfanew = 0;    // File address of new exe header
175   } dos_header_t;
176 
177   typedef struct coff_header {
178     uint16_t machine = 0;
179     uint16_t nsects = 0;
180     uint32_t modtime = 0;
181     uint32_t symoff = 0;
182     uint32_t nsyms = 0;
183     uint16_t hdrsize = 0;
184     uint16_t flags = 0;
185   } coff_header_t;
186 
187   typedef struct data_directory {
188     uint32_t vmaddr = 0;
189     uint32_t vmsize = 0;
190   } data_directory_t;
191 
192   typedef struct coff_opt_header {
193     uint16_t magic = 0;
194     uint8_t major_linker_version = 0;
195     uint8_t minor_linker_version = 0;
196     uint32_t code_size = 0;
197     uint32_t data_size = 0;
198     uint32_t bss_size = 0;
199     uint32_t entry = 0;
200     uint32_t code_offset = 0;
201     uint32_t data_offset = 0;
202 
203     uint64_t image_base = 0;
204     uint32_t sect_alignment = 0;
205     uint32_t file_alignment = 0;
206     uint16_t major_os_system_version = 0;
207     uint16_t minor_os_system_version = 0;
208     uint16_t major_image_version = 0;
209     uint16_t minor_image_version = 0;
210     uint16_t major_subsystem_version = 0;
211     uint16_t minor_subsystem_version = 0;
212     uint32_t reserved1 = 0;
213     uint32_t image_size = 0;
214     uint32_t header_size = 0;
215     uint32_t checksum = 0;
216     uint16_t subsystem = 0;
217     uint16_t dll_flags = 0;
218     uint64_t stack_reserve_size = 0;
219     uint64_t stack_commit_size = 0;
220     uint64_t heap_reserve_size = 0;
221     uint64_t heap_commit_size = 0;
222     uint32_t loader_flags = 0;
223     //    uint32_t	num_data_dir_entries;
224     std::vector<data_directory>
225         data_dirs; // will contain num_data_dir_entries entries
226   } coff_opt_header_t;
227 
228   enum coff_data_dir_type {
229     coff_data_dir_export_table = 0,
230     coff_data_dir_import_table = 1,
231     coff_data_dir_exception_table = 3
232   };
233 
234   typedef struct section_header {
235     char name[8] = {};
236     uint32_t vmsize = 0;  // Virtual Size
237     uint32_t vmaddr = 0;  // Virtual Addr
238     uint32_t size = 0;    // File size
239     uint32_t offset = 0;  // File offset
240     uint32_t reloff = 0;  // Offset to relocations
241     uint32_t lineoff = 0; // Offset to line table entries
242     uint16_t nreloc = 0;  // Number of relocation entries
243     uint16_t nline = 0;   // Number of line table entries
244     uint32_t flags = 0;
245   } section_header_t;
246 
247   typedef struct coff_symbol {
248     char name[8] = {};
249     uint32_t value = 0;
250     uint16_t sect = 0;
251     uint16_t type = 0;
252     uint8_t storage = 0;
253     uint8_t naux = 0;
254   } coff_symbol_t;
255 
256   typedef struct export_directory_entry {
257     uint32_t characteristics = 0;
258     uint32_t time_date_stamp = 0;
259     uint16_t major_version = 0;
260     uint16_t minor_version = 0;
261     uint32_t name = 0;
262     uint32_t base = 0;
263     uint32_t number_of_functions = 0;
264     uint32_t number_of_names = 0;
265     uint32_t address_of_functions = 0;
266     uint32_t address_of_names = 0;
267     uint32_t address_of_name_ordinals = 0;
268   } export_directory_entry;
269 
270   static bool ParseDOSHeader(lldb_private::DataExtractor &data,
271                              dos_header_t &dos_header);
272   static bool ParseCOFFHeader(lldb_private::DataExtractor &data,
273                               lldb::offset_t *offset_ptr,
274                               coff_header_t &coff_header);
275   bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr);
276   bool ParseSectionHeaders(uint32_t offset);
277 
278   uint32_t ParseDependentModules();
279 
280   static void DumpDOSHeader(lldb_private::Stream *s,
281                             const dos_header_t &header);
282   static void DumpCOFFHeader(lldb_private::Stream *s,
283                              const coff_header_t &header);
284   static void DumpOptCOFFHeader(lldb_private::Stream *s,
285                                 const coff_opt_header_t &header);
286   void DumpSectionHeaders(lldb_private::Stream *s);
287   void DumpSectionHeader(lldb_private::Stream *s, const section_header_t &sh);
288   void DumpDependentModules(lldb_private::Stream *s);
289 
290   llvm::StringRef GetSectionName(const section_header_t &sect);
291   static lldb::SectionType GetSectionType(llvm::StringRef sect_name,
292                                           const section_header_t &sect);
293 
294   typedef std::vector<section_header_t> SectionHeaderColl;
295   typedef SectionHeaderColl::iterator SectionHeaderCollIter;
296   typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
297 
298 private:
299   bool CreateBinary();
300 
301   dos_header_t m_dos_header;
302   coff_header_t m_coff_header;
303   coff_opt_header_t m_coff_header_opt;
304   SectionHeaderColl m_sect_headers;
305   lldb::addr_t m_image_base;
306   lldb_private::Address m_entry_point_address;
307   llvm::Optional<lldb_private::FileSpecList> m_deps_filespec;
308   std::unique_ptr<llvm::object::COFFObjectFile> m_binary;
309   lldb_private::UUID m_uuid;
310 };
311 
312 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_PECOFF_OBJECTFILEPECOFF_H
313