blob: f746c368f3dfa18809d29a7b22d6caf2cd6415aa [file] [log] [blame]
Greg Claytonf754f882011-09-09 20:33:05 +00001//===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFilePECOFF.h"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000011#include "WindowsMiniDump.h"
Greg Claytonf754f882011-09-09 20:33:05 +000012
Greg Claytonf754f882011-09-09 20:33:05 +000013#include "lldb/Core/FileSpecList.h"
14#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000015#include "lldb/Core/ModuleSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000016#include "lldb/Core/PluginManager.h"
17#include "lldb/Core/Section.h"
18#include "lldb/Core/StreamFile.h"
Greg Claytonf754f882011-09-09 20:33:05 +000019#include "lldb/Symbol/ObjectFile.h"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000020#include "lldb/Target/Process.h"
Virgile Bello2756adf2014-03-08 17:17:20 +000021#include "lldb/Target/SectionLoadList.h"
22#include "lldb/Target/Target.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000023#include "lldb/Utility/ArchSpec.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000024#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000025#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000027#include "lldb/Utility/Timer.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000028#include "lldb/Utility/UUID.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000029#include "llvm/BinaryFormat/COFF.h"
Greg Claytonf754f882011-09-09 20:33:05 +000030
Zachary Turner3f4a4b32017-02-24 18:56:49 +000031#include "llvm/Support/MemoryBuffer.h"
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
34#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
35#define OPT_HEADER_MAGIC_PE32 0x010b
36#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
Greg Claytonf754f882011-09-09 20:33:05 +000037
Greg Claytonf754f882011-09-09 20:33:05 +000038using namespace lldb;
39using namespace lldb_private;
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041void ObjectFilePECOFF::Initialize() {
42 PluginManager::RegisterPlugin(
43 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
44 CreateMemoryInstance, GetModuleSpecifications, SaveCore);
Greg Claytonf754f882011-09-09 20:33:05 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047void ObjectFilePECOFF::Terminate() {
48 PluginManager::UnregisterPlugin(CreateInstance);
Greg Claytonf754f882011-09-09 20:33:05 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
52 static ConstString g_name("pe-coff");
53 return g_name;
Greg Claytonf754f882011-09-09 20:33:05 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
57 return "Portable Executable and Common Object File Format object file reader "
58 "(32 and 64 bit)";
Greg Claytonf754f882011-09-09 20:33:05 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
62 DataBufferSP &data_sp,
63 lldb::offset_t data_offset,
64 const lldb_private::FileSpec *file,
65 lldb::offset_t file_offset,
66 lldb::offset_t length) {
67 if (!data_sp) {
Pavel Labath50251fc2017-12-21 10:54:30 +000068 data_sp = MapFileData(file, length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000069 if (!data_sp)
70 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 data_offset = 0;
72 }
Greg Claytonf754f882011-09-09 20:33:05 +000073
Zachary Turner3f4a4b32017-02-24 18:56:49 +000074 if (!ObjectFilePECOFF::MagicBytesMatch(data_sp))
75 return nullptr;
76
77 // Update the data to contain the entire file if it doesn't already
78 if (data_sp->GetByteSize() < length) {
Pavel Labath50251fc2017-12-21 10:54:30 +000079 data_sp = MapFileData(file, length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000080 if (!data_sp)
81 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
Zachary Turner3f4a4b32017-02-24 18:56:49 +000083
84 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
85 module_sp, data_sp, data_offset, file, file_offset, length);
86 if (!objfile_ap || !objfile_ap->ParseHeader())
87 return nullptr;
88
89 return objfile_ap.release();
Greg Claytonf754f882011-09-09 20:33:05 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
93 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
94 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
Walter Erquinigo344546b2016-10-17 20:28:19 +000095 if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
96 return nullptr;
97 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
98 module_sp, data_sp, process_sp, header_addr);
99 if (objfile_ap.get() && objfile_ap->ParseHeader()) {
100 return objfile_ap.release();
101 }
102 return nullptr;
Greg Claytonc9660542012-02-05 02:38:54 +0000103}
104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105size_t ObjectFilePECOFF::GetModuleSpecifications(
106 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
107 lldb::offset_t data_offset, lldb::offset_t file_offset,
108 lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
109 const size_t initial_count = specs.GetSize();
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
112 DataExtractor data;
113 data.SetData(data_sp, data_offset, length);
114 data.SetByteOrder(eByteOrderLittle);
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 dos_header_t dos_header;
117 coff_header_t coff_header;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 if (ParseDOSHeader(data, dos_header)) {
120 lldb::offset_t offset = dos_header.e_lfanew;
121 uint32_t pe_signature = data.GetU32(&offset);
122 if (pe_signature != IMAGE_NT_SIGNATURE)
123 return false;
124 if (ParseCOFFHeader(data, &offset, coff_header)) {
125 ArchSpec spec;
126 if (coff_header.machine == MachineAmd64) {
127 spec.SetTriple("x86_64-pc-windows");
128 specs.Append(ModuleSpec(file, spec));
129 } else if (coff_header.machine == MachineX86) {
130 spec.SetTriple("i386-pc-windows");
131 specs.Append(ModuleSpec(file, spec));
132 spec.SetTriple("i686-pc-windows");
133 specs.Append(ModuleSpec(file, spec));
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000134 }
Stephane Sezer7b6e8ef2017-10-24 23:40:59 +0000135 else if (coff_header.machine == MachineArmNt)
136 {
137 spec.SetTriple("arm-pc-windows");
138 specs.Append(ModuleSpec(file, spec));
139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000141 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
148 const lldb_private::FileSpec &outfile,
Zachary Turner97206d52017-05-12 04:51:55 +0000149 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 return SaveMiniDump(process_sp, outfile, error);
Adrian McCarthyf7d18932015-11-20 23:09:11 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
154 DataExtractor data(data_sp, eByteOrderLittle, 4);
155 lldb::offset_t offset = 0;
156 uint16_t magic = data.GetU16(&offset);
157 return magic == IMAGE_DOS_SIGNATURE;
158}
Greg Claytonf4d6de62013-04-24 22:29:28 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
161 // TODO: We need to complete this mapping of COFF symbol types to LLDB ones.
162 // For now, here's a hack to make sure our function have types.
163 const auto complex_type =
164 coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
165 if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
166 return lldb::eSymbolTypeCode;
167 }
168 return lldb::eSymbolTypeInvalid;
169}
170
171ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
172 DataBufferSP &data_sp,
173 lldb::offset_t data_offset,
174 const FileSpec *file,
175 lldb::offset_t file_offset,
176 lldb::offset_t length)
177 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
178 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
179 m_entry_point_address() {
180 ::memset(&m_dos_header, 0, sizeof(m_dos_header));
181 ::memset(&m_coff_header, 0, sizeof(m_coff_header));
182 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
183}
184
Walter Erquinigo344546b2016-10-17 20:28:19 +0000185ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
186 DataBufferSP &header_data_sp,
187 const lldb::ProcessSP &process_sp,
188 addr_t header_addr)
189 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
190 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
191 m_entry_point_address() {
192 ::memset(&m_dos_header, 0, sizeof(m_dos_header));
193 ::memset(&m_coff_header, 0, sizeof(m_coff_header));
194 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
195}
196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197ObjectFilePECOFF::~ObjectFilePECOFF() {}
198
199bool ObjectFilePECOFF::ParseHeader() {
200 ModuleSP module_sp(GetModule());
201 if (module_sp) {
202 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
203 m_sect_headers.clear();
204 m_data.SetByteOrder(eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000205 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 if (ParseDOSHeader(m_data, m_dos_header)) {
208 offset = m_dos_header.e_lfanew;
209 uint32_t pe_signature = m_data.GetU32(&offset);
210 if (pe_signature != IMAGE_NT_SIGNATURE)
211 return false;
212 if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
213 if (m_coff_header.hdrsize > 0)
214 ParseCOFFOptionalHeader(&offset);
215 ParseSectionHeaders(offset);
216 }
217 return true;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 }
220 return false;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000221}
Greg Claytonf754f882011-09-09 20:33:05 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
224 bool value_is_offset) {
225 bool changed = false;
226 ModuleSP module_sp = GetModule();
227 if (module_sp) {
228 size_t num_loaded_sections = 0;
229 SectionList *section_list = GetSectionList();
230 if (section_list) {
231 if (!value_is_offset) {
232 value -= m_image_base;
233 }
Greg Claytonf754f882011-09-09 20:33:05 +0000234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 const size_t num_sections = section_list->GetSize();
236 size_t sect_idx = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +0000239 // Iterate through the object file sections to find all of the sections
240 // that have SHF_ALLOC in their flag bits.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
242 if (section_sp && !section_sp->IsThreadSpecific()) {
243 if (target.GetSectionLoadList().SetSectionLoadAddress(
244 section_sp, section_sp->GetFileAddress() + value))
245 ++num_loaded_sections;
Greg Clayton28469ca2011-09-10 01:04:42 +0000246 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 }
248 changed = num_loaded_sections > 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000249 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 }
251 return changed;
Greg Claytonf754f882011-09-09 20:33:05 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
Virgile Bello2756adf2014-03-08 17:17:20 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256bool ObjectFilePECOFF::IsExecutable() const {
257 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000258}
259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
261 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
262 return 8;
263 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
Greg Claytonf754f882011-09-09 20:33:05 +0000264 return 4;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 return 4;
Greg Claytonf754f882011-09-09 20:33:05 +0000266}
267
268//----------------------------------------------------------------------
269// NeedsEndianSwap
270//
Adrian Prantl05097242018-04-30 16:49:04 +0000271// Return true if an endian swap needs to occur when extracting data from this
272// file.
Greg Claytonf754f882011-09-09 20:33:05 +0000273//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274bool ObjectFilePECOFF::NeedsEndianSwap() const {
Greg Claytonf754f882011-09-09 20:33:05 +0000275#if defined(__LITTLE_ENDIAN__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 return false;
Greg Claytonf754f882011-09-09 20:33:05 +0000277#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 return true;
Greg Claytonf754f882011-09-09 20:33:05 +0000279#endif
280}
281//----------------------------------------------------------------------
282// ParseDOSHeader
283//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
285 dos_header_t &dos_header) {
286 bool success = false;
287 lldb::offset_t offset = 0;
288 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 if (success) {
291 dos_header.e_magic = data.GetU16(&offset); // Magic number
292 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
293
294 if (success) {
295 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
296 dos_header.e_cp = data.GetU16(&offset); // Pages in file
297 dos_header.e_crlc = data.GetU16(&offset); // Relocations
298 dos_header.e_cparhdr =
299 data.GetU16(&offset); // Size of header in paragraphs
300 dos_header.e_minalloc =
301 data.GetU16(&offset); // Minimum extra paragraphs needed
302 dos_header.e_maxalloc =
303 data.GetU16(&offset); // Maximum extra paragraphs needed
304 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
305 dos_header.e_sp = data.GetU16(&offset); // Initial SP value
306 dos_header.e_csum = data.GetU16(&offset); // Checksum
307 dos_header.e_ip = data.GetU16(&offset); // Initial IP value
308 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
309 dos_header.e_lfarlc =
310 data.GetU16(&offset); // File address of relocation table
311 dos_header.e_ovno = data.GetU16(&offset); // Overlay number
312
313 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
314 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
315 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
316 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
317
318 dos_header.e_oemid =
319 data.GetU16(&offset); // OEM identifier (for e_oeminfo)
320 dos_header.e_oeminfo =
321 data.GetU16(&offset); // OEM information; e_oemid specific
322 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
323 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
324 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
325 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
326 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
327 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
328 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
329 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
330 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
331 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
332
333 dos_header.e_lfanew =
334 data.GetU32(&offset); // File address of new exe header
335 }
336 }
337 if (!success)
338 memset(&dos_header, 0, sizeof(dos_header));
339 return success;
340}
Greg Claytonf754f882011-09-09 20:33:05 +0000341
342//----------------------------------------------------------------------
343// ParserCOFFHeader
344//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
346 lldb::offset_t *offset_ptr,
347 coff_header_t &coff_header) {
348 bool success =
349 data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
350 if (success) {
351 coff_header.machine = data.GetU16(offset_ptr);
352 coff_header.nsects = data.GetU16(offset_ptr);
353 coff_header.modtime = data.GetU32(offset_ptr);
354 coff_header.symoff = data.GetU32(offset_ptr);
355 coff_header.nsyms = data.GetU32(offset_ptr);
356 coff_header.hdrsize = data.GetU16(offset_ptr);
357 coff_header.flags = data.GetU16(offset_ptr);
358 }
359 if (!success)
360 memset(&coff_header, 0, sizeof(coff_header));
361 return success;
Greg Claytonf754f882011-09-09 20:33:05 +0000362}
363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
365 bool success = false;
366 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
367 if (*offset_ptr < end_offset) {
368 success = true;
369 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
370 m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
371 m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
372 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
373 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
374 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
375 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
376 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 const uint32_t addr_byte_size = GetAddressByteSize();
Greg Claytonf754f882011-09-09 20:33:05 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 if (*offset_ptr < end_offset) {
381 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
382 // PE32 only
383 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
384 } else
385 m_coff_header_opt.data_offset = 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 if (*offset_ptr < end_offset) {
388 m_coff_header_opt.image_base =
389 m_data.GetMaxU64(offset_ptr, addr_byte_size);
390 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
391 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
392 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
393 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
394 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
395 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
396 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
397 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
398 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
399 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
400 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
401 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
402 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
403 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
404 m_coff_header_opt.stack_reserve_size =
405 m_data.GetMaxU64(offset_ptr, addr_byte_size);
406 m_coff_header_opt.stack_commit_size =
407 m_data.GetMaxU64(offset_ptr, addr_byte_size);
408 m_coff_header_opt.heap_reserve_size =
409 m_data.GetMaxU64(offset_ptr, addr_byte_size);
410 m_coff_header_opt.heap_commit_size =
411 m_data.GetMaxU64(offset_ptr, addr_byte_size);
412 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
413 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
414 m_coff_header_opt.data_dirs.clear();
415 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
416 uint32_t i;
417 for (i = 0; i < num_data_dir_entries; i++) {
418 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
419 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000420 }
Greg Claytonf754f882011-09-09 20:33:05 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 m_file_offset = m_coff_header_opt.image_base;
423 m_image_base = m_coff_header_opt.image_base;
424 }
425 }
426 }
427 // Make sure we are on track for section data which follows
428 *offset_ptr = end_offset;
429 return success;
430}
Greg Claytonf754f882011-09-09 20:33:05 +0000431
Walter Erquinigo344546b2016-10-17 20:28:19 +0000432DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
433 if (m_file) {
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000434 // A bit of a hack, but we intend to write to this buffer, so we can't
435 // mmap it.
Pavel Labath50251fc2017-12-21 10:54:30 +0000436 auto buffer_sp = MapFileData(m_file, size, offset);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000437 return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
438 }
439 ProcessSP process_sp(m_process_wp.lock());
440 DataExtractor data;
441 if (process_sp) {
442 auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +0000443 Status readmem_error;
Walter Erquinigo344546b2016-10-17 20:28:19 +0000444 size_t bytes_read =
445 process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(),
446 data_ap->GetByteSize(), readmem_error);
447 if (bytes_read == size) {
448 DataBufferSP buffer_sp(data_ap.release());
449 data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
450 }
451 }
452 return data;
453}
454
Greg Claytonf754f882011-09-09 20:33:05 +0000455//----------------------------------------------------------------------
456// ParseSectionHeaders
457//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458bool ObjectFilePECOFF::ParseSectionHeaders(
459 uint32_t section_header_data_offset) {
460 const uint32_t nsects = m_coff_header.nsects;
461 m_sect_headers.clear();
Greg Claytonf754f882011-09-09 20:33:05 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 if (nsects > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000465 DataExtractor section_header_data =
466 ReadImageData(section_header_data_offset, section_header_byte_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467
468 lldb::offset_t offset = 0;
469 if (section_header_data.ValidOffsetForDataOfSize(
470 offset, section_header_byte_size)) {
471 m_sect_headers.resize(nsects);
472
473 for (uint32_t idx = 0; idx < nsects; ++idx) {
474 const void *name_data = section_header_data.GetData(&offset, 8);
475 if (name_data) {
476 memcpy(m_sect_headers[idx].name, name_data, 8);
477 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
478 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
479 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
480 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
481 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
482 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
483 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
484 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
485 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
Greg Claytonf754f882011-09-09 20:33:05 +0000486 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 }
Greg Claytonf754f882011-09-09 20:33:05 +0000488 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 }
490
491 return m_sect_headers.empty() == false;
Greg Claytonf754f882011-09-09 20:33:05 +0000492}
493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494bool ObjectFilePECOFF::GetSectionName(std::string &sect_name,
495 const section_header_t &sect) {
496 if (sect.name[0] == '/') {
497 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
498 lldb::offset_t string_file_offset =
499 m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
500 const char *name = m_data.GetCStr(&string_file_offset);
501 if (name) {
502 sect_name = name;
503 return true;
Greg Claytonf754f882011-09-09 20:33:05 +0000504 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505
506 return false;
507 }
508 sect_name = sect.name;
509 return true;
510}
Greg Claytonf754f882011-09-09 20:33:05 +0000511
512//----------------------------------------------------------------------
513// GetNListSymtab
514//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515Symtab *ObjectFilePECOFF::GetSymtab() {
516 ModuleSP module_sp(GetModule());
517 if (module_sp) {
518 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
519 if (m_symtab_ap.get() == NULL) {
520 SectionList *sect_list = GetSectionList();
521 m_symtab_ap.reset(new Symtab(this));
522 std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
523
524 const uint32_t num_syms = m_coff_header.nsyms;
525
Walter Erquinigo344546b2016-10-17 20:28:19 +0000526 if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 const uint32_t symbol_size = 18;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 const size_t symbol_data_size = num_syms * symbol_size;
529 // Include the 4-byte string table size at the end of the symbols
Walter Erquinigo344546b2016-10-17 20:28:19 +0000530 DataExtractor symtab_data =
531 ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 lldb::offset_t offset = symbol_data_size;
533 const uint32_t strtab_size = symtab_data.GetU32(&offset);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000534 if (strtab_size > 0) {
535 DataExtractor strtab_data = ReadImageData(
536 m_coff_header.symoff + symbol_data_size, strtab_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537
Walter Erquinigo344546b2016-10-17 20:28:19 +0000538 // First 4 bytes should be zeroed after strtab_size has been read,
539 // because it is used as offset 0 to encode a NULL string.
Saleem Abdulrasoolf76e0992017-07-19 15:46:21 +0000540 uint32_t *strtab_data_start = const_cast<uint32_t *>(
541 reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
Walter Erquinigo344546b2016-10-17 20:28:19 +0000542 strtab_data_start[0] = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543
Walter Erquinigo344546b2016-10-17 20:28:19 +0000544 offset = 0;
545 std::string symbol_name;
546 Symbol *symbols = m_symtab_ap->Resize(num_syms);
547 for (uint32_t i = 0; i < num_syms; ++i) {
548 coff_symbol_t symbol;
549 const uint32_t symbol_offset = offset;
550 const char *symbol_name_cstr = NULL;
551 // If the first 4 bytes of the symbol string are zero, then they
552 // are followed by a 4-byte string table offset. Else these
553 // 8 bytes contain the symbol name
554 if (symtab_data.GetU32(&offset) == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +0000555 // Long string that doesn't fit into the symbol table name, so
556 // now we must read the 4 byte string table offset
Walter Erquinigo344546b2016-10-17 20:28:19 +0000557 uint32_t strtab_offset = symtab_data.GetU32(&offset);
558 symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
559 symbol_name.assign(symbol_name_cstr);
560 } else {
561 // Short string that fits into the symbol table name which is 8
562 // bytes
563 offset += sizeof(symbol.name) - 4; // Skip remaining
564 symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
565 if (symbol_name_cstr == NULL)
566 break;
567 symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
568 }
569 symbol.value = symtab_data.GetU32(&offset);
570 symbol.sect = symtab_data.GetU16(&offset);
571 symbol.type = symtab_data.GetU16(&offset);
572 symbol.storage = symtab_data.GetU8(&offset);
573 symbol.naux = symtab_data.GetU8(&offset);
574 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
575 if ((int16_t)symbol.sect >= 1) {
576 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
577 symbol.value);
578 symbols[i].GetAddressRef() = symbol_addr;
579 symbols[i].SetType(MapSymbolType(symbol.type));
580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581
Walter Erquinigo344546b2016-10-17 20:28:19 +0000582 if (symbol.naux > 0) {
583 i += symbol.naux;
584 offset += symbol_size;
585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 }
587 }
588 }
589
590 // Read export header
591 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
592 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
593 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
594 export_directory_entry export_table;
595 uint32_t data_start =
596 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
Walter Erquinigo344546b2016-10-17 20:28:19 +0000597
598 uint32_t address_rva = data_start;
599 if (m_file) {
600 Address address(m_coff_header_opt.image_base + data_start, sect_list);
601 address_rva =
602 address.GetSection()->GetFileOffset() + address.GetOffset();
603 }
604 DataExtractor symtab_data =
605 ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606 lldb::offset_t offset = 0;
607
608 // Read export_table header
609 export_table.characteristics = symtab_data.GetU32(&offset);
610 export_table.time_date_stamp = symtab_data.GetU32(&offset);
611 export_table.major_version = symtab_data.GetU16(&offset);
612 export_table.minor_version = symtab_data.GetU16(&offset);
613 export_table.name = symtab_data.GetU32(&offset);
614 export_table.base = symtab_data.GetU32(&offset);
615 export_table.number_of_functions = symtab_data.GetU32(&offset);
616 export_table.number_of_names = symtab_data.GetU32(&offset);
617 export_table.address_of_functions = symtab_data.GetU32(&offset);
618 export_table.address_of_names = symtab_data.GetU32(&offset);
619 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
620
621 bool has_ordinal = export_table.address_of_name_ordinals != 0;
622
623 lldb::offset_t name_offset = export_table.address_of_names - data_start;
624 lldb::offset_t name_ordinal_offset =
625 export_table.address_of_name_ordinals - data_start;
626
627 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
628
629 std::string symbol_name;
630
631 // Read each export table entry
632 for (size_t i = 0; i < export_table.number_of_names; ++i) {
633 uint32_t name_ordinal =
634 has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
635 uint32_t name_address = symtab_data.GetU32(&name_offset);
636
637 const char *symbol_name_cstr =
638 symtab_data.PeekCStr(name_address - data_start);
639 symbol_name.assign(symbol_name_cstr);
640
641 lldb::offset_t function_offset = export_table.address_of_functions -
642 data_start +
643 sizeof(uint32_t) * name_ordinal;
644 uint32_t function_rva = symtab_data.GetU32(&function_offset);
645
646 Address symbol_addr(m_coff_header_opt.image_base + function_rva,
647 sect_list);
648 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
649 symbols[i].GetAddressRef() = symbol_addr;
650 symbols[i].SetType(lldb::eSymbolTypeCode);
651 symbols[i].SetDebug(true);
652 }
653 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 }
655 }
656 return m_symtab_ap.get();
657}
658
659bool ObjectFilePECOFF::IsStripped() {
660 // TODO: determine this for COFF
661 return false;
662}
663
664void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
665 if (!m_sections_ap.get()) {
666 m_sections_ap.reset(new SectionList());
667
Greg Claytona1743492012-03-13 23:14:29 +0000668 ModuleSP module_sp(GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 if (module_sp) {
670 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
671 const uint32_t nsects = m_sect_headers.size();
672 ModuleSP module_sp(GetModule());
673 for (uint32_t idx = 0; idx < nsects; ++idx) {
674 std::string sect_name;
675 GetSectionName(sect_name, m_sect_headers[idx]);
676 ConstString const_sect_name(sect_name.c_str());
677 static ConstString g_code_sect_name(".code");
678 static ConstString g_CODE_sect_name("CODE");
679 static ConstString g_data_sect_name(".data");
680 static ConstString g_DATA_sect_name("DATA");
681 static ConstString g_bss_sect_name(".bss");
682 static ConstString g_BSS_sect_name("BSS");
683 static ConstString g_debug_sect_name(".debug");
684 static ConstString g_reloc_sect_name(".reloc");
685 static ConstString g_stab_sect_name(".stab");
686 static ConstString g_stabstr_sect_name(".stabstr");
687 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
688 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
689 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
690 static ConstString g_sect_name_dwarf_debug_info(".debug_info");
691 static ConstString g_sect_name_dwarf_debug_line(".debug_line");
692 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
George Rimare4dee262018-10-23 09:46:15 +0000693 static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
Pavel Labatha041d842018-06-01 12:06:45 +0000695 static ConstString g_sect_name_dwarf_debug_names(".debug_names");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
697 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
698 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
699 static ConstString g_sect_name_dwarf_debug_str(".debug_str");
Greg Clayton2550ca12018-05-08 17:19:24 +0000700 static ConstString g_sect_name_dwarf_debug_types(".debug_types");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701 static ConstString g_sect_name_eh_frame(".eh_frame");
702 static ConstString g_sect_name_go_symtab(".gosymtab");
703 SectionType section_type = eSectionTypeOther;
704 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
705 ((const_sect_name == g_code_sect_name) ||
706 (const_sect_name == g_CODE_sect_name))) {
707 section_type = eSectionTypeCode;
708 } else if (m_sect_headers[idx].flags &
709 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
710 ((const_sect_name == g_data_sect_name) ||
711 (const_sect_name == g_DATA_sect_name))) {
712 section_type = eSectionTypeData;
713 } else if (m_sect_headers[idx].flags &
714 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
715 ((const_sect_name == g_bss_sect_name) ||
716 (const_sect_name == g_BSS_sect_name))) {
717 if (m_sect_headers[idx].size == 0)
718 section_type = eSectionTypeZeroFill;
719 else
720 section_type = eSectionTypeData;
721 } else if (const_sect_name == g_debug_sect_name) {
722 section_type = eSectionTypeDebug;
723 } else if (const_sect_name == g_stabstr_sect_name) {
724 section_type = eSectionTypeDataCString;
725 } else if (const_sect_name == g_reloc_sect_name) {
726 section_type = eSectionTypeOther;
727 } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
728 section_type = eSectionTypeDWARFDebugAbbrev;
729 else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
730 section_type = eSectionTypeDWARFDebugAranges;
731 else if (const_sect_name == g_sect_name_dwarf_debug_frame)
732 section_type = eSectionTypeDWARFDebugFrame;
733 else if (const_sect_name == g_sect_name_dwarf_debug_info)
734 section_type = eSectionTypeDWARFDebugInfo;
735 else if (const_sect_name == g_sect_name_dwarf_debug_line)
736 section_type = eSectionTypeDWARFDebugLine;
737 else if (const_sect_name == g_sect_name_dwarf_debug_loc)
738 section_type = eSectionTypeDWARFDebugLoc;
George Rimare4dee262018-10-23 09:46:15 +0000739 else if (const_sect_name == g_sect_name_dwarf_debug_loclists)
740 section_type = eSectionTypeDWARFDebugLocLists;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
742 section_type = eSectionTypeDWARFDebugMacInfo;
Pavel Labatha041d842018-06-01 12:06:45 +0000743 else if (const_sect_name == g_sect_name_dwarf_debug_names)
744 section_type = eSectionTypeDWARFDebugNames;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
746 section_type = eSectionTypeDWARFDebugPubNames;
747 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
748 section_type = eSectionTypeDWARFDebugPubTypes;
749 else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
750 section_type = eSectionTypeDWARFDebugRanges;
751 else if (const_sect_name == g_sect_name_dwarf_debug_str)
752 section_type = eSectionTypeDWARFDebugStr;
Greg Clayton2550ca12018-05-08 17:19:24 +0000753 else if (const_sect_name == g_sect_name_dwarf_debug_types)
754 section_type = eSectionTypeDWARFDebugTypes;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 else if (const_sect_name == g_sect_name_eh_frame)
756 section_type = eSectionTypeEHFrame;
757 else if (const_sect_name == g_sect_name_go_symtab)
758 section_type = eSectionTypeGoSymtab;
759 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
760 section_type = eSectionTypeCode;
761 } else if (m_sect_headers[idx].flags &
762 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
763 section_type = eSectionTypeData;
764 } else if (m_sect_headers[idx].flags &
765 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
766 if (m_sect_headers[idx].size == 0)
767 section_type = eSectionTypeZeroFill;
768 else
769 section_type = eSectionTypeData;
Greg Claytonf754f882011-09-09 20:33:05 +0000770 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771
772 // Use a segment ID of the segment index shifted left by 8 so they
773 // never conflict with any of the sections.
774 SectionSP section_sp(new Section(
775 module_sp, // Module to which this section belongs
776 this, // Object file to which this section belongs
777 idx + 1, // Section ID is the 1 based segment index shifted right by
778 // 8 bits as not to collide with any of the 256 section IDs
779 // that are possible
780 const_sect_name, // Name of this section
781 section_type, // This section is a container of other sections.
782 m_coff_header_opt.image_base +
783 m_sect_headers[idx].vmaddr, // File VM address == addresses as
784 // they are found in the object file
785 m_sect_headers[idx].vmsize, // VM size in bytes of this section
786 m_sect_headers[idx]
787 .offset, // Offset to the data for this section in the file
788 m_sect_headers[idx]
789 .size, // Size in bytes of this section as found in the file
790 m_coff_header_opt.sect_alignment, // Section alignment
791 m_sect_headers[idx].flags)); // Flags for this section
792
793 // section_sp->SetIsEncrypted (segment_is_encrypted);
794
795 unified_section_list.AddSection(section_sp);
796 m_sections_ap->AddSection(section_sp);
797 }
Greg Claytonf754f882011-09-09 20:33:05 +0000798 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799 }
Greg Claytonf754f882011-09-09 20:33:05 +0000800}
801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
803
804uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
805 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +0000806}
807
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
809 if (m_entry_point_address.IsValid())
Stephane Sezer8e38c662016-03-23 18:00:13 +0000810 return m_entry_point_address;
Stephane Sezer8e38c662016-03-23 18:00:13 +0000811
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 if (!ParseHeader() || !IsExecutable())
813 return m_entry_point_address;
814
815 SectionList *section_list = GetSectionList();
816 addr_t offset = m_coff_header_opt.entry;
817
818 if (!section_list)
819 m_entry_point_address.SetOffset(offset);
820 else
821 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
822 return m_entry_point_address;
823}
Greg Claytonf754f882011-09-09 20:33:05 +0000824
825//----------------------------------------------------------------------
826// Dump
827//
828// Dump the specifics of the runtime file container (such as any headers
829// segments, sections, etc).
830//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831void ObjectFilePECOFF::Dump(Stream *s) {
832 ModuleSP module_sp(GetModule());
833 if (module_sp) {
834 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
835 s->Printf("%p: ", static_cast<void *>(this));
836 s->Indent();
837 s->PutCString("ObjectFilePECOFF");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000838
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 ArchSpec header_arch;
840 GetArchitecture(header_arch);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 *s << ", file = '" << m_file
843 << "', arch = " << header_arch.GetArchitectureName() << "\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000844
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 SectionList *sections = GetSectionList();
846 if (sections)
847 sections->Dump(s, NULL, true, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000848
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 if (m_symtab_ap.get())
850 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Greg Claytona1743492012-03-13 23:14:29 +0000851
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 if (m_dos_header.e_magic)
853 DumpDOSHeader(s, m_dos_header);
854 if (m_coff_header.machine) {
855 DumpCOFFHeader(s, m_coff_header);
856 if (m_coff_header.hdrsize)
857 DumpOptCOFFHeader(s, m_coff_header_opt);
Greg Claytonf754f882011-09-09 20:33:05 +0000858 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 s->EOL();
860 DumpSectionHeaders(s);
861 s->EOL();
862 }
Greg Claytonf754f882011-09-09 20:33:05 +0000863}
864
865//----------------------------------------------------------------------
866// DumpDOSHeader
867//
868// Dump the MS-DOS header to the specified output stream
869//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
871 s->PutCString("MSDOS Header\n");
872 s->Printf(" e_magic = 0x%4.4x\n", header.e_magic);
873 s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp);
874 s->Printf(" e_cp = 0x%4.4x\n", header.e_cp);
875 s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc);
876 s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
877 s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc);
878 s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
879 s->Printf(" e_ss = 0x%4.4x\n", header.e_ss);
880 s->Printf(" e_sp = 0x%4.4x\n", header.e_sp);
881 s->Printf(" e_csum = 0x%4.4x\n", header.e_csum);
882 s->Printf(" e_ip = 0x%4.4x\n", header.e_ip);
883 s->Printf(" e_cs = 0x%4.4x\n", header.e_cs);
884 s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
885 s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno);
886 s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
887 header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
888 s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid);
889 s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
890 s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
891 "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
892 header.e_res2[0], header.e_res2[1], header.e_res2[2],
893 header.e_res2[3], header.e_res2[4], header.e_res2[5],
894 header.e_res2[6], header.e_res2[7], header.e_res2[8],
895 header.e_res2[9]);
896 s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew);
Greg Claytonf754f882011-09-09 20:33:05 +0000897}
898
899//----------------------------------------------------------------------
900// DumpCOFFHeader
901//
902// Dump the COFF header to the specified output stream
903//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
905 s->PutCString("COFF Header\n");
906 s->Printf(" machine = 0x%4.4x\n", header.machine);
907 s->Printf(" nsects = 0x%4.4x\n", header.nsects);
908 s->Printf(" modtime = 0x%8.8x\n", header.modtime);
909 s->Printf(" symoff = 0x%8.8x\n", header.symoff);
910 s->Printf(" nsyms = 0x%8.8x\n", header.nsyms);
911 s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize);
Greg Claytonf754f882011-09-09 20:33:05 +0000912}
913
914//----------------------------------------------------------------------
915// DumpOptCOFFHeader
916//
917// Dump the optional COFF header to the specified output stream
918//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
920 const coff_opt_header_t &header) {
921 s->PutCString("Optional COFF Header\n");
922 s->Printf(" magic = 0x%4.4x\n", header.magic);
923 s->Printf(" major_linker_version = 0x%2.2x\n",
924 header.major_linker_version);
925 s->Printf(" minor_linker_version = 0x%2.2x\n",
926 header.minor_linker_version);
927 s->Printf(" code_size = 0x%8.8x\n", header.code_size);
928 s->Printf(" data_size = 0x%8.8x\n", header.data_size);
929 s->Printf(" bss_size = 0x%8.8x\n", header.bss_size);
930 s->Printf(" entry = 0x%8.8x\n", header.entry);
931 s->Printf(" code_offset = 0x%8.8x\n", header.code_offset);
932 s->Printf(" data_offset = 0x%8.8x\n", header.data_offset);
933 s->Printf(" image_base = 0x%16.16" PRIx64 "\n",
934 header.image_base);
935 s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment);
936 s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment);
937 s->Printf(" major_os_system_version = 0x%4.4x\n",
938 header.major_os_system_version);
939 s->Printf(" minor_os_system_version = 0x%4.4x\n",
940 header.minor_os_system_version);
941 s->Printf(" major_image_version = 0x%4.4x\n",
942 header.major_image_version);
943 s->Printf(" minor_image_version = 0x%4.4x\n",
944 header.minor_image_version);
945 s->Printf(" major_subsystem_version = 0x%4.4x\n",
946 header.major_subsystem_version);
947 s->Printf(" minor_subsystem_version = 0x%4.4x\n",
948 header.minor_subsystem_version);
949 s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1);
950 s->Printf(" image_size = 0x%8.8x\n", header.image_size);
951 s->Printf(" header_size = 0x%8.8x\n", header.header_size);
952 s->Printf(" checksum = 0x%8.8x\n", header.checksum);
953 s->Printf(" subsystem = 0x%4.4x\n", header.subsystem);
954 s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags);
955 s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n",
956 header.stack_reserve_size);
957 s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n",
958 header.stack_commit_size);
959 s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n",
960 header.heap_reserve_size);
961 s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n",
962 header.heap_commit_size);
963 s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags);
964 s->Printf(" num_data_dir_entries = 0x%8.8x\n",
965 (uint32_t)header.data_dirs.size());
966 uint32_t i;
967 for (i = 0; i < header.data_dirs.size(); i++) {
968 s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
969 header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
970 }
Greg Claytonf754f882011-09-09 20:33:05 +0000971}
972//----------------------------------------------------------------------
973// DumpSectionHeader
974//
975// Dump a single ELF section header to the specified output stream
976//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
978 const section_header_t &sh) {
979 std::string name;
980 GetSectionName(name, sh);
981 s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x "
982 "0x%4.4x 0x%8.8x\n",
983 name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
984 sh.lineoff, sh.nreloc, sh.nline, sh.flags);
Greg Claytonf754f882011-09-09 20:33:05 +0000985}
986
Greg Claytonf754f882011-09-09 20:33:05 +0000987//----------------------------------------------------------------------
988// DumpSectionHeaders
989//
990// Dump all of the ELF section header to the specified output stream
991//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
993
994 s->PutCString("Section Headers\n");
995 s->PutCString("IDX name vm addr vm size file off file "
996 "size reloc off line off nreloc nline flags\n");
997 s->PutCString("==== ---------------- ---------- ---------- ---------- "
998 "---------- ---------- ---------- ------ ------ ----------\n");
999
1000 uint32_t idx = 0;
1001 SectionHeaderCollIter pos, end = m_sect_headers.end();
1002
1003 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1004 s->Printf("[%2u] ", idx);
1005 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1006 }
Greg Claytonf754f882011-09-09 20:33:05 +00001007}
1008
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +00001009bool ObjectFilePECOFF::IsWindowsSubsystem() {
1010 switch (m_coff_header_opt.subsystem) {
1011 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1012 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1013 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1014 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1015 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1016 case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1017 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1018 return true;
1019 default:
1020 return false;
1021 }
1022}
1023
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) {
1025 uint16_t machine = m_coff_header.machine;
1026 switch (machine) {
1027 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1028 case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1029 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1030 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1031 case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
1032 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1033 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +00001034 arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1035 IsWindowsSubsystem() ? llvm::Triple::Win32
1036 : llvm::Triple::UnknownOS);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 return true;
1038 default:
1039 break;
1040 }
1041 return false;
Greg Claytonf754f882011-09-09 20:33:05 +00001042}
1043
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1045 if (m_coff_header.machine != 0) {
1046 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1047 return eTypeExecutable;
1048 else
1049 return eTypeSharedLibrary;
1050 }
1051 return eTypeExecutable;
Greg Claytonf754f882011-09-09 20:33:05 +00001052}
1053
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
Greg Claytonf754f882011-09-09 20:33:05 +00001055//------------------------------------------------------------------
1056// PluginInterface protocol
1057//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
Greg Claytonf754f882011-09-09 20:33:05 +00001059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }