blob: 53e89a2fe6228b690b27d0fc26859125d0b3aa84 [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
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/COFF.h"
Greg Claytonf754f882011-09-09 20:33:05 +000014
15#include "lldb/Core/ArchSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000016#include "lldb/Core/FileSpecList.h"
17#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000018#include "lldb/Core/ModuleSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000019#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/Section.h"
21#include "lldb/Core/StreamFile.h"
Greg Claytonf754f882011-09-09 20:33:05 +000022#include "lldb/Symbol/ObjectFile.h"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000023#include "lldb/Target/Process.h"
Virgile Bello2756adf2014-03-08 17:17:20 +000024#include "lldb/Target/SectionLoadList.h"
25#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000026#include "lldb/Utility/DataBufferHeap.h"
27#include "lldb/Utility/DataBufferLLVM.h"
Zachary Turner5713a052017-03-22 18:40:07 +000028#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000029#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000030#include "lldb/Utility/Timer.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000031#include "lldb/Utility/UUID.h"
Greg Claytonf754f882011-09-09 20:33:05 +000032
Zachary Turner3f4a4b32017-02-24 18:56:49 +000033#include "llvm/Support/MemoryBuffer.h"
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
36#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
37#define OPT_HEADER_MAGIC_PE32 0x010b
38#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
Greg Claytonf754f882011-09-09 20:33:05 +000039
Greg Claytonf754f882011-09-09 20:33:05 +000040using namespace lldb;
41using namespace lldb_private;
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043void ObjectFilePECOFF::Initialize() {
44 PluginManager::RegisterPlugin(
45 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
46 CreateMemoryInstance, GetModuleSpecifications, SaveCore);
Greg Claytonf754f882011-09-09 20:33:05 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void ObjectFilePECOFF::Terminate() {
50 PluginManager::UnregisterPlugin(CreateInstance);
Greg Claytonf754f882011-09-09 20:33:05 +000051}
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
54 static ConstString g_name("pe-coff");
55 return g_name;
Greg Claytonf754f882011-09-09 20:33:05 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
59 return "Portable Executable and Common Object File Format object file reader "
60 "(32 and 64 bit)";
Greg Claytonf754f882011-09-09 20:33:05 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
64 DataBufferSP &data_sp,
65 lldb::offset_t data_offset,
66 const lldb_private::FileSpec *file,
67 lldb::offset_t file_offset,
68 lldb::offset_t length) {
69 if (!data_sp) {
Zachary Turner666cc0b2017-03-04 01:30:05 +000070 data_sp =
Zachary Turner7f6a7a32017-03-06 23:42:14 +000071 DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000072 if (!data_sp)
73 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 data_offset = 0;
75 }
Greg Claytonf754f882011-09-09 20:33:05 +000076
Zachary Turner3f4a4b32017-02-24 18:56:49 +000077 if (!ObjectFilePECOFF::MagicBytesMatch(data_sp))
78 return nullptr;
79
80 // Update the data to contain the entire file if it doesn't already
81 if (data_sp->GetByteSize() < length) {
Zachary Turner666cc0b2017-03-04 01:30:05 +000082 data_sp =
Zachary Turner7f6a7a32017-03-06 23:42:14 +000083 DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
Zachary Turner3f4a4b32017-02-24 18:56:49 +000084 if (!data_sp)
85 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 }
Zachary Turner3f4a4b32017-02-24 18:56:49 +000087
88 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
89 module_sp, data_sp, data_offset, file, file_offset, length);
90 if (!objfile_ap || !objfile_ap->ParseHeader())
91 return nullptr;
92
93 return objfile_ap.release();
Greg Claytonf754f882011-09-09 20:33:05 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
97 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
98 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
Walter Erquinigo344546b2016-10-17 20:28:19 +000099 if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
100 return nullptr;
101 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
102 module_sp, data_sp, process_sp, header_addr);
103 if (objfile_ap.get() && objfile_ap->ParseHeader()) {
104 return objfile_ap.release();
105 }
106 return nullptr;
Greg Claytonc9660542012-02-05 02:38:54 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109size_t ObjectFilePECOFF::GetModuleSpecifications(
110 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
111 lldb::offset_t data_offset, lldb::offset_t file_offset,
112 lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
113 const size_t initial_count = specs.GetSize();
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
116 DataExtractor data;
117 data.SetData(data_sp, data_offset, length);
118 data.SetByteOrder(eByteOrderLittle);
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 dos_header_t dos_header;
121 coff_header_t coff_header;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 if (ParseDOSHeader(data, dos_header)) {
124 lldb::offset_t offset = dos_header.e_lfanew;
125 uint32_t pe_signature = data.GetU32(&offset);
126 if (pe_signature != IMAGE_NT_SIGNATURE)
127 return false;
128 if (ParseCOFFHeader(data, &offset, coff_header)) {
129 ArchSpec spec;
130 if (coff_header.machine == MachineAmd64) {
131 spec.SetTriple("x86_64-pc-windows");
132 specs.Append(ModuleSpec(file, spec));
133 } else if (coff_header.machine == MachineX86) {
134 spec.SetTriple("i386-pc-windows");
135 specs.Append(ModuleSpec(file, spec));
136 spec.SetTriple("i686-pc-windows");
137 specs.Append(ModuleSpec(file, spec));
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000138 }
Stephane Sezer7b6e8ef2017-10-24 23:40:59 +0000139 else if (coff_header.machine == MachineArmNt)
140 {
141 spec.SetTriple("arm-pc-windows");
142 specs.Append(ModuleSpec(file, spec));
143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
152 const lldb_private::FileSpec &outfile,
Zachary Turner97206d52017-05-12 04:51:55 +0000153 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 return SaveMiniDump(process_sp, outfile, error);
Adrian McCarthyf7d18932015-11-20 23:09:11 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
158 DataExtractor data(data_sp, eByteOrderLittle, 4);
159 lldb::offset_t offset = 0;
160 uint16_t magic = data.GetU16(&offset);
161 return magic == IMAGE_DOS_SIGNATURE;
162}
Greg Claytonf4d6de62013-04-24 22:29:28 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
165 // TODO: We need to complete this mapping of COFF symbol types to LLDB ones.
166 // For now, here's a hack to make sure our function have types.
167 const auto complex_type =
168 coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
169 if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
170 return lldb::eSymbolTypeCode;
171 }
172 return lldb::eSymbolTypeInvalid;
173}
174
175ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
176 DataBufferSP &data_sp,
177 lldb::offset_t data_offset,
178 const FileSpec *file,
179 lldb::offset_t file_offset,
180 lldb::offset_t length)
181 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
182 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
183 m_entry_point_address() {
184 ::memset(&m_dos_header, 0, sizeof(m_dos_header));
185 ::memset(&m_coff_header, 0, sizeof(m_coff_header));
186 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
187}
188
Walter Erquinigo344546b2016-10-17 20:28:19 +0000189ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
190 DataBufferSP &header_data_sp,
191 const lldb::ProcessSP &process_sp,
192 addr_t header_addr)
193 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
194 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
195 m_entry_point_address() {
196 ::memset(&m_dos_header, 0, sizeof(m_dos_header));
197 ::memset(&m_coff_header, 0, sizeof(m_coff_header));
198 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201ObjectFilePECOFF::~ObjectFilePECOFF() {}
202
203bool ObjectFilePECOFF::ParseHeader() {
204 ModuleSP module_sp(GetModule());
205 if (module_sp) {
206 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
207 m_sect_headers.clear();
208 m_data.SetByteOrder(eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000209 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 if (ParseDOSHeader(m_data, m_dos_header)) {
212 offset = m_dos_header.e_lfanew;
213 uint32_t pe_signature = m_data.GetU32(&offset);
214 if (pe_signature != IMAGE_NT_SIGNATURE)
215 return false;
216 if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
217 if (m_coff_header.hdrsize > 0)
218 ParseCOFFOptionalHeader(&offset);
219 ParseSectionHeaders(offset);
220 }
221 return true;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000222 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 }
224 return false;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000225}
Greg Claytonf754f882011-09-09 20:33:05 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
228 bool value_is_offset) {
229 bool changed = false;
230 ModuleSP module_sp = GetModule();
231 if (module_sp) {
232 size_t num_loaded_sections = 0;
233 SectionList *section_list = GetSectionList();
234 if (section_list) {
235 if (!value_is_offset) {
236 value -= m_image_base;
237 }
Greg Claytonf754f882011-09-09 20:33:05 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 const size_t num_sections = section_list->GetSize();
240 size_t sect_idx = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
243 // Iterate through the object file sections to find all
244 // of the sections that have SHF_ALLOC in their flag bits.
245 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
246 if (section_sp && !section_sp->IsThreadSpecific()) {
247 if (target.GetSectionLoadList().SetSectionLoadAddress(
248 section_sp, section_sp->GetFileAddress() + value))
249 ++num_loaded_sections;
Greg Clayton28469ca2011-09-10 01:04:42 +0000250 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 }
252 changed = num_loaded_sections > 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 }
255 return changed;
Greg Claytonf754f882011-09-09 20:33:05 +0000256}
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
Virgile Bello2756adf2014-03-08 17:17:20 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260bool ObjectFilePECOFF::IsExecutable() const {
261 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000262}
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
265 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
266 return 8;
267 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
Greg Claytonf754f882011-09-09 20:33:05 +0000268 return 4;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 return 4;
Greg Claytonf754f882011-09-09 20:33:05 +0000270}
271
272//----------------------------------------------------------------------
273// NeedsEndianSwap
274//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275// Return true if an endian swap needs to occur when extracting data
Greg Claytonf754f882011-09-09 20:33:05 +0000276// from this file.
277//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278bool ObjectFilePECOFF::NeedsEndianSwap() const {
Greg Claytonf754f882011-09-09 20:33:05 +0000279#if defined(__LITTLE_ENDIAN__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 return false;
Greg Claytonf754f882011-09-09 20:33:05 +0000281#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 return true;
Greg Claytonf754f882011-09-09 20:33:05 +0000283#endif
284}
285//----------------------------------------------------------------------
286// ParseDOSHeader
287//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
289 dos_header_t &dos_header) {
290 bool success = false;
291 lldb::offset_t offset = 0;
292 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 if (success) {
295 dos_header.e_magic = data.GetU16(&offset); // Magic number
296 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
297
298 if (success) {
299 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
300 dos_header.e_cp = data.GetU16(&offset); // Pages in file
301 dos_header.e_crlc = data.GetU16(&offset); // Relocations
302 dos_header.e_cparhdr =
303 data.GetU16(&offset); // Size of header in paragraphs
304 dos_header.e_minalloc =
305 data.GetU16(&offset); // Minimum extra paragraphs needed
306 dos_header.e_maxalloc =
307 data.GetU16(&offset); // Maximum extra paragraphs needed
308 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
309 dos_header.e_sp = data.GetU16(&offset); // Initial SP value
310 dos_header.e_csum = data.GetU16(&offset); // Checksum
311 dos_header.e_ip = data.GetU16(&offset); // Initial IP value
312 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
313 dos_header.e_lfarlc =
314 data.GetU16(&offset); // File address of relocation table
315 dos_header.e_ovno = data.GetU16(&offset); // Overlay number
316
317 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
318 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
319 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
320 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
321
322 dos_header.e_oemid =
323 data.GetU16(&offset); // OEM identifier (for e_oeminfo)
324 dos_header.e_oeminfo =
325 data.GetU16(&offset); // OEM information; e_oemid specific
326 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
327 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
328 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
329 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
330 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
331 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
332 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
333 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
334 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
335 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
336
337 dos_header.e_lfanew =
338 data.GetU32(&offset); // File address of new exe header
339 }
340 }
341 if (!success)
342 memset(&dos_header, 0, sizeof(dos_header));
343 return success;
344}
Greg Claytonf754f882011-09-09 20:33:05 +0000345
346//----------------------------------------------------------------------
347// ParserCOFFHeader
348//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
350 lldb::offset_t *offset_ptr,
351 coff_header_t &coff_header) {
352 bool success =
353 data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
354 if (success) {
355 coff_header.machine = data.GetU16(offset_ptr);
356 coff_header.nsects = data.GetU16(offset_ptr);
357 coff_header.modtime = data.GetU32(offset_ptr);
358 coff_header.symoff = data.GetU32(offset_ptr);
359 coff_header.nsyms = data.GetU32(offset_ptr);
360 coff_header.hdrsize = data.GetU16(offset_ptr);
361 coff_header.flags = data.GetU16(offset_ptr);
362 }
363 if (!success)
364 memset(&coff_header, 0, sizeof(coff_header));
365 return success;
Greg Claytonf754f882011-09-09 20:33:05 +0000366}
367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
369 bool success = false;
370 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
371 if (*offset_ptr < end_offset) {
372 success = true;
373 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
374 m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
375 m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
376 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
377 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
378 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
379 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
380 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 const uint32_t addr_byte_size = GetAddressByteSize();
Greg Claytonf754f882011-09-09 20:33:05 +0000383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 if (*offset_ptr < end_offset) {
385 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
386 // PE32 only
387 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
388 } else
389 m_coff_header_opt.data_offset = 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 if (*offset_ptr < end_offset) {
392 m_coff_header_opt.image_base =
393 m_data.GetMaxU64(offset_ptr, addr_byte_size);
394 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
395 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
396 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
397 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
398 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
399 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
400 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
401 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
402 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
403 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
404 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
405 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
406 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
407 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
408 m_coff_header_opt.stack_reserve_size =
409 m_data.GetMaxU64(offset_ptr, addr_byte_size);
410 m_coff_header_opt.stack_commit_size =
411 m_data.GetMaxU64(offset_ptr, addr_byte_size);
412 m_coff_header_opt.heap_reserve_size =
413 m_data.GetMaxU64(offset_ptr, addr_byte_size);
414 m_coff_header_opt.heap_commit_size =
415 m_data.GetMaxU64(offset_ptr, addr_byte_size);
416 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
417 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
418 m_coff_header_opt.data_dirs.clear();
419 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
420 uint32_t i;
421 for (i = 0; i < num_data_dir_entries; i++) {
422 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
423 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000424 }
Greg Claytonf754f882011-09-09 20:33:05 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 m_file_offset = m_coff_header_opt.image_base;
427 m_image_base = m_coff_header_opt.image_base;
428 }
429 }
430 }
431 // Make sure we are on track for section data which follows
432 *offset_ptr = end_offset;
433 return success;
434}
Greg Claytonf754f882011-09-09 20:33:05 +0000435
Walter Erquinigo344546b2016-10-17 20:28:19 +0000436DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
437 if (m_file) {
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000438 // A bit of a hack, but we intend to write to this buffer, so we can't
439 // mmap it.
440 auto buffer_sp =
441 DataBufferLLVM::CreateSliceFromPath(m_file.GetPath(), size, offset, true);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000442 return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
443 }
444 ProcessSP process_sp(m_process_wp.lock());
445 DataExtractor data;
446 if (process_sp) {
447 auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0);
Zachary Turner97206d52017-05-12 04:51:55 +0000448 Status readmem_error;
Walter Erquinigo344546b2016-10-17 20:28:19 +0000449 size_t bytes_read =
450 process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(),
451 data_ap->GetByteSize(), readmem_error);
452 if (bytes_read == size) {
453 DataBufferSP buffer_sp(data_ap.release());
454 data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
455 }
456 }
457 return data;
458}
459
Greg Claytonf754f882011-09-09 20:33:05 +0000460//----------------------------------------------------------------------
461// ParseSectionHeaders
462//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463bool ObjectFilePECOFF::ParseSectionHeaders(
464 uint32_t section_header_data_offset) {
465 const uint32_t nsects = m_coff_header.nsects;
466 m_sect_headers.clear();
Greg Claytonf754f882011-09-09 20:33:05 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 if (nsects > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000470 DataExtractor section_header_data =
471 ReadImageData(section_header_data_offset, section_header_byte_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472
473 lldb::offset_t offset = 0;
474 if (section_header_data.ValidOffsetForDataOfSize(
475 offset, section_header_byte_size)) {
476 m_sect_headers.resize(nsects);
477
478 for (uint32_t idx = 0; idx < nsects; ++idx) {
479 const void *name_data = section_header_data.GetData(&offset, 8);
480 if (name_data) {
481 memcpy(m_sect_headers[idx].name, name_data, 8);
482 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
483 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
484 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
485 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
486 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
487 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
488 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
489 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
490 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
Greg Claytonf754f882011-09-09 20:33:05 +0000491 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 }
Greg Claytonf754f882011-09-09 20:33:05 +0000493 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 }
495
496 return m_sect_headers.empty() == false;
Greg Claytonf754f882011-09-09 20:33:05 +0000497}
498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499bool ObjectFilePECOFF::GetSectionName(std::string &sect_name,
500 const section_header_t &sect) {
501 if (sect.name[0] == '/') {
502 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
503 lldb::offset_t string_file_offset =
504 m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
505 const char *name = m_data.GetCStr(&string_file_offset);
506 if (name) {
507 sect_name = name;
508 return true;
Greg Claytonf754f882011-09-09 20:33:05 +0000509 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510
511 return false;
512 }
513 sect_name = sect.name;
514 return true;
515}
Greg Claytonf754f882011-09-09 20:33:05 +0000516
517//----------------------------------------------------------------------
518// GetNListSymtab
519//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520Symtab *ObjectFilePECOFF::GetSymtab() {
521 ModuleSP module_sp(GetModule());
522 if (module_sp) {
523 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
524 if (m_symtab_ap.get() == NULL) {
525 SectionList *sect_list = GetSectionList();
526 m_symtab_ap.reset(new Symtab(this));
527 std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
528
529 const uint32_t num_syms = m_coff_header.nsyms;
530
Walter Erquinigo344546b2016-10-17 20:28:19 +0000531 if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 const uint32_t symbol_size = 18;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 const size_t symbol_data_size = num_syms * symbol_size;
534 // Include the 4-byte string table size at the end of the symbols
Walter Erquinigo344546b2016-10-17 20:28:19 +0000535 DataExtractor symtab_data =
536 ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 lldb::offset_t offset = symbol_data_size;
538 const uint32_t strtab_size = symtab_data.GetU32(&offset);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000539 if (strtab_size > 0) {
540 DataExtractor strtab_data = ReadImageData(
541 m_coff_header.symoff + symbol_data_size, strtab_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542
Walter Erquinigo344546b2016-10-17 20:28:19 +0000543 // First 4 bytes should be zeroed after strtab_size has been read,
544 // because it is used as offset 0 to encode a NULL string.
Saleem Abdulrasoolf76e0992017-07-19 15:46:21 +0000545 uint32_t *strtab_data_start = const_cast<uint32_t *>(
546 reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
Walter Erquinigo344546b2016-10-17 20:28:19 +0000547 strtab_data_start[0] = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548
Walter Erquinigo344546b2016-10-17 20:28:19 +0000549 offset = 0;
550 std::string symbol_name;
551 Symbol *symbols = m_symtab_ap->Resize(num_syms);
552 for (uint32_t i = 0; i < num_syms; ++i) {
553 coff_symbol_t symbol;
554 const uint32_t symbol_offset = offset;
555 const char *symbol_name_cstr = NULL;
556 // If the first 4 bytes of the symbol string are zero, then they
557 // are followed by a 4-byte string table offset. Else these
558 // 8 bytes contain the symbol name
559 if (symtab_data.GetU32(&offset) == 0) {
560 // Long string that doesn't fit into the symbol table name,
561 // so now we must read the 4 byte string table offset
562 uint32_t strtab_offset = symtab_data.GetU32(&offset);
563 symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
564 symbol_name.assign(symbol_name_cstr);
565 } else {
566 // Short string that fits into the symbol table name which is 8
567 // bytes
568 offset += sizeof(symbol.name) - 4; // Skip remaining
569 symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
570 if (symbol_name_cstr == NULL)
571 break;
572 symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
573 }
574 symbol.value = symtab_data.GetU32(&offset);
575 symbol.sect = symtab_data.GetU16(&offset);
576 symbol.type = symtab_data.GetU16(&offset);
577 symbol.storage = symtab_data.GetU8(&offset);
578 symbol.naux = symtab_data.GetU8(&offset);
579 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
580 if ((int16_t)symbol.sect >= 1) {
581 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
582 symbol.value);
583 symbols[i].GetAddressRef() = symbol_addr;
584 symbols[i].SetType(MapSymbolType(symbol.type));
585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586
Walter Erquinigo344546b2016-10-17 20:28:19 +0000587 if (symbol.naux > 0) {
588 i += symbol.naux;
589 offset += symbol_size;
590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 }
592 }
593 }
594
595 // Read export header
596 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
597 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
598 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
599 export_directory_entry export_table;
600 uint32_t data_start =
601 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
Walter Erquinigo344546b2016-10-17 20:28:19 +0000602
603 uint32_t address_rva = data_start;
604 if (m_file) {
605 Address address(m_coff_header_opt.image_base + data_start, sect_list);
606 address_rva =
607 address.GetSection()->GetFileOffset() + address.GetOffset();
608 }
609 DataExtractor symtab_data =
610 ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 lldb::offset_t offset = 0;
612
613 // Read export_table header
614 export_table.characteristics = symtab_data.GetU32(&offset);
615 export_table.time_date_stamp = symtab_data.GetU32(&offset);
616 export_table.major_version = symtab_data.GetU16(&offset);
617 export_table.minor_version = symtab_data.GetU16(&offset);
618 export_table.name = symtab_data.GetU32(&offset);
619 export_table.base = symtab_data.GetU32(&offset);
620 export_table.number_of_functions = symtab_data.GetU32(&offset);
621 export_table.number_of_names = symtab_data.GetU32(&offset);
622 export_table.address_of_functions = symtab_data.GetU32(&offset);
623 export_table.address_of_names = symtab_data.GetU32(&offset);
624 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
625
626 bool has_ordinal = export_table.address_of_name_ordinals != 0;
627
628 lldb::offset_t name_offset = export_table.address_of_names - data_start;
629 lldb::offset_t name_ordinal_offset =
630 export_table.address_of_name_ordinals - data_start;
631
632 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
633
634 std::string symbol_name;
635
636 // Read each export table entry
637 for (size_t i = 0; i < export_table.number_of_names; ++i) {
638 uint32_t name_ordinal =
639 has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
640 uint32_t name_address = symtab_data.GetU32(&name_offset);
641
642 const char *symbol_name_cstr =
643 symtab_data.PeekCStr(name_address - data_start);
644 symbol_name.assign(symbol_name_cstr);
645
646 lldb::offset_t function_offset = export_table.address_of_functions -
647 data_start +
648 sizeof(uint32_t) * name_ordinal;
649 uint32_t function_rva = symtab_data.GetU32(&function_offset);
650
651 Address symbol_addr(m_coff_header_opt.image_base + function_rva,
652 sect_list);
653 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
654 symbols[i].GetAddressRef() = symbol_addr;
655 symbols[i].SetType(lldb::eSymbolTypeCode);
656 symbols[i].SetDebug(true);
657 }
658 }
659 m_symtab_ap->CalculateSymbolSizes();
660 }
661 }
662 return m_symtab_ap.get();
663}
664
665bool ObjectFilePECOFF::IsStripped() {
666 // TODO: determine this for COFF
667 return false;
668}
669
670void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
671 if (!m_sections_ap.get()) {
672 m_sections_ap.reset(new SectionList());
673
Greg Claytona1743492012-03-13 23:14:29 +0000674 ModuleSP module_sp(GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000675 if (module_sp) {
676 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
677 const uint32_t nsects = m_sect_headers.size();
678 ModuleSP module_sp(GetModule());
679 for (uint32_t idx = 0; idx < nsects; ++idx) {
680 std::string sect_name;
681 GetSectionName(sect_name, m_sect_headers[idx]);
682 ConstString const_sect_name(sect_name.c_str());
683 static ConstString g_code_sect_name(".code");
684 static ConstString g_CODE_sect_name("CODE");
685 static ConstString g_data_sect_name(".data");
686 static ConstString g_DATA_sect_name("DATA");
687 static ConstString g_bss_sect_name(".bss");
688 static ConstString g_BSS_sect_name("BSS");
689 static ConstString g_debug_sect_name(".debug");
690 static ConstString g_reloc_sect_name(".reloc");
691 static ConstString g_stab_sect_name(".stab");
692 static ConstString g_stabstr_sect_name(".stabstr");
693 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
694 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
695 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
696 static ConstString g_sect_name_dwarf_debug_info(".debug_info");
697 static ConstString g_sect_name_dwarf_debug_line(".debug_line");
698 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
699 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
700 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
701 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
702 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
703 static ConstString g_sect_name_dwarf_debug_str(".debug_str");
704 static ConstString g_sect_name_eh_frame(".eh_frame");
705 static ConstString g_sect_name_go_symtab(".gosymtab");
706 SectionType section_type = eSectionTypeOther;
707 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
708 ((const_sect_name == g_code_sect_name) ||
709 (const_sect_name == g_CODE_sect_name))) {
710 section_type = eSectionTypeCode;
711 } else if (m_sect_headers[idx].flags &
712 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
713 ((const_sect_name == g_data_sect_name) ||
714 (const_sect_name == g_DATA_sect_name))) {
715 section_type = eSectionTypeData;
716 } else if (m_sect_headers[idx].flags &
717 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
718 ((const_sect_name == g_bss_sect_name) ||
719 (const_sect_name == g_BSS_sect_name))) {
720 if (m_sect_headers[idx].size == 0)
721 section_type = eSectionTypeZeroFill;
722 else
723 section_type = eSectionTypeData;
724 } else if (const_sect_name == g_debug_sect_name) {
725 section_type = eSectionTypeDebug;
726 } else if (const_sect_name == g_stabstr_sect_name) {
727 section_type = eSectionTypeDataCString;
728 } else if (const_sect_name == g_reloc_sect_name) {
729 section_type = eSectionTypeOther;
730 } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
731 section_type = eSectionTypeDWARFDebugAbbrev;
732 else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
733 section_type = eSectionTypeDWARFDebugAranges;
734 else if (const_sect_name == g_sect_name_dwarf_debug_frame)
735 section_type = eSectionTypeDWARFDebugFrame;
736 else if (const_sect_name == g_sect_name_dwarf_debug_info)
737 section_type = eSectionTypeDWARFDebugInfo;
738 else if (const_sect_name == g_sect_name_dwarf_debug_line)
739 section_type = eSectionTypeDWARFDebugLine;
740 else if (const_sect_name == g_sect_name_dwarf_debug_loc)
741 section_type = eSectionTypeDWARFDebugLoc;
742 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
743 section_type = eSectionTypeDWARFDebugMacInfo;
744 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
745 section_type = eSectionTypeDWARFDebugPubNames;
746 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
747 section_type = eSectionTypeDWARFDebugPubTypes;
748 else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
749 section_type = eSectionTypeDWARFDebugRanges;
750 else if (const_sect_name == g_sect_name_dwarf_debug_str)
751 section_type = eSectionTypeDWARFDebugStr;
752 else if (const_sect_name == g_sect_name_eh_frame)
753 section_type = eSectionTypeEHFrame;
754 else if (const_sect_name == g_sect_name_go_symtab)
755 section_type = eSectionTypeGoSymtab;
756 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
757 section_type = eSectionTypeCode;
758 } else if (m_sect_headers[idx].flags &
759 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
760 section_type = eSectionTypeData;
761 } else if (m_sect_headers[idx].flags &
762 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
763 if (m_sect_headers[idx].size == 0)
764 section_type = eSectionTypeZeroFill;
765 else
766 section_type = eSectionTypeData;
Greg Claytonf754f882011-09-09 20:33:05 +0000767 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768
769 // Use a segment ID of the segment index shifted left by 8 so they
770 // never conflict with any of the sections.
771 SectionSP section_sp(new Section(
772 module_sp, // Module to which this section belongs
773 this, // Object file to which this section belongs
774 idx + 1, // Section ID is the 1 based segment index shifted right by
775 // 8 bits as not to collide with any of the 256 section IDs
776 // that are possible
777 const_sect_name, // Name of this section
778 section_type, // This section is a container of other sections.
779 m_coff_header_opt.image_base +
780 m_sect_headers[idx].vmaddr, // File VM address == addresses as
781 // they are found in the object file
782 m_sect_headers[idx].vmsize, // VM size in bytes of this section
783 m_sect_headers[idx]
784 .offset, // Offset to the data for this section in the file
785 m_sect_headers[idx]
786 .size, // Size in bytes of this section as found in the file
787 m_coff_header_opt.sect_alignment, // Section alignment
788 m_sect_headers[idx].flags)); // Flags for this section
789
790 // section_sp->SetIsEncrypted (segment_is_encrypted);
791
792 unified_section_list.AddSection(section_sp);
793 m_sections_ap->AddSection(section_sp);
794 }
Greg Claytonf754f882011-09-09 20:33:05 +0000795 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 }
Greg Claytonf754f882011-09-09 20:33:05 +0000797}
798
Kate Stoneb9c1b512016-09-06 20:57:50 +0000799bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
800
801uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
802 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +0000803}
804
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
806 if (m_entry_point_address.IsValid())
Stephane Sezer8e38c662016-03-23 18:00:13 +0000807 return m_entry_point_address;
Stephane Sezer8e38c662016-03-23 18:00:13 +0000808
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 if (!ParseHeader() || !IsExecutable())
810 return m_entry_point_address;
811
812 SectionList *section_list = GetSectionList();
813 addr_t offset = m_coff_header_opt.entry;
814
815 if (!section_list)
816 m_entry_point_address.SetOffset(offset);
817 else
818 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
819 return m_entry_point_address;
820}
Greg Claytonf754f882011-09-09 20:33:05 +0000821
822//----------------------------------------------------------------------
823// Dump
824//
825// Dump the specifics of the runtime file container (such as any headers
826// segments, sections, etc).
827//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828void ObjectFilePECOFF::Dump(Stream *s) {
829 ModuleSP module_sp(GetModule());
830 if (module_sp) {
831 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
832 s->Printf("%p: ", static_cast<void *>(this));
833 s->Indent();
834 s->PutCString("ObjectFilePECOFF");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000835
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 ArchSpec header_arch;
837 GetArchitecture(header_arch);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000838
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 *s << ", file = '" << m_file
840 << "', arch = " << header_arch.GetArchitectureName() << "\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 SectionList *sections = GetSectionList();
843 if (sections)
844 sections->Dump(s, NULL, true, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000845
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 if (m_symtab_ap.get())
847 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Greg Claytona1743492012-03-13 23:14:29 +0000848
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 if (m_dos_header.e_magic)
850 DumpDOSHeader(s, m_dos_header);
851 if (m_coff_header.machine) {
852 DumpCOFFHeader(s, m_coff_header);
853 if (m_coff_header.hdrsize)
854 DumpOptCOFFHeader(s, m_coff_header_opt);
Greg Claytonf754f882011-09-09 20:33:05 +0000855 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856 s->EOL();
857 DumpSectionHeaders(s);
858 s->EOL();
859 }
Greg Claytonf754f882011-09-09 20:33:05 +0000860}
861
862//----------------------------------------------------------------------
863// DumpDOSHeader
864//
865// Dump the MS-DOS header to the specified output stream
866//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
868 s->PutCString("MSDOS Header\n");
869 s->Printf(" e_magic = 0x%4.4x\n", header.e_magic);
870 s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp);
871 s->Printf(" e_cp = 0x%4.4x\n", header.e_cp);
872 s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc);
873 s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
874 s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc);
875 s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
876 s->Printf(" e_ss = 0x%4.4x\n", header.e_ss);
877 s->Printf(" e_sp = 0x%4.4x\n", header.e_sp);
878 s->Printf(" e_csum = 0x%4.4x\n", header.e_csum);
879 s->Printf(" e_ip = 0x%4.4x\n", header.e_ip);
880 s->Printf(" e_cs = 0x%4.4x\n", header.e_cs);
881 s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
882 s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno);
883 s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
884 header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
885 s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid);
886 s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
887 s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
888 "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
889 header.e_res2[0], header.e_res2[1], header.e_res2[2],
890 header.e_res2[3], header.e_res2[4], header.e_res2[5],
891 header.e_res2[6], header.e_res2[7], header.e_res2[8],
892 header.e_res2[9]);
893 s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew);
Greg Claytonf754f882011-09-09 20:33:05 +0000894}
895
896//----------------------------------------------------------------------
897// DumpCOFFHeader
898//
899// Dump the COFF header to the specified output stream
900//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
902 s->PutCString("COFF Header\n");
903 s->Printf(" machine = 0x%4.4x\n", header.machine);
904 s->Printf(" nsects = 0x%4.4x\n", header.nsects);
905 s->Printf(" modtime = 0x%8.8x\n", header.modtime);
906 s->Printf(" symoff = 0x%8.8x\n", header.symoff);
907 s->Printf(" nsyms = 0x%8.8x\n", header.nsyms);
908 s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize);
Greg Claytonf754f882011-09-09 20:33:05 +0000909}
910
911//----------------------------------------------------------------------
912// DumpOptCOFFHeader
913//
914// Dump the optional COFF header to the specified output stream
915//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
917 const coff_opt_header_t &header) {
918 s->PutCString("Optional COFF Header\n");
919 s->Printf(" magic = 0x%4.4x\n", header.magic);
920 s->Printf(" major_linker_version = 0x%2.2x\n",
921 header.major_linker_version);
922 s->Printf(" minor_linker_version = 0x%2.2x\n",
923 header.minor_linker_version);
924 s->Printf(" code_size = 0x%8.8x\n", header.code_size);
925 s->Printf(" data_size = 0x%8.8x\n", header.data_size);
926 s->Printf(" bss_size = 0x%8.8x\n", header.bss_size);
927 s->Printf(" entry = 0x%8.8x\n", header.entry);
928 s->Printf(" code_offset = 0x%8.8x\n", header.code_offset);
929 s->Printf(" data_offset = 0x%8.8x\n", header.data_offset);
930 s->Printf(" image_base = 0x%16.16" PRIx64 "\n",
931 header.image_base);
932 s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment);
933 s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment);
934 s->Printf(" major_os_system_version = 0x%4.4x\n",
935 header.major_os_system_version);
936 s->Printf(" minor_os_system_version = 0x%4.4x\n",
937 header.minor_os_system_version);
938 s->Printf(" major_image_version = 0x%4.4x\n",
939 header.major_image_version);
940 s->Printf(" minor_image_version = 0x%4.4x\n",
941 header.minor_image_version);
942 s->Printf(" major_subsystem_version = 0x%4.4x\n",
943 header.major_subsystem_version);
944 s->Printf(" minor_subsystem_version = 0x%4.4x\n",
945 header.minor_subsystem_version);
946 s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1);
947 s->Printf(" image_size = 0x%8.8x\n", header.image_size);
948 s->Printf(" header_size = 0x%8.8x\n", header.header_size);
949 s->Printf(" checksum = 0x%8.8x\n", header.checksum);
950 s->Printf(" subsystem = 0x%4.4x\n", header.subsystem);
951 s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags);
952 s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n",
953 header.stack_reserve_size);
954 s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n",
955 header.stack_commit_size);
956 s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n",
957 header.heap_reserve_size);
958 s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n",
959 header.heap_commit_size);
960 s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags);
961 s->Printf(" num_data_dir_entries = 0x%8.8x\n",
962 (uint32_t)header.data_dirs.size());
963 uint32_t i;
964 for (i = 0; i < header.data_dirs.size(); i++) {
965 s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
966 header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
967 }
Greg Claytonf754f882011-09-09 20:33:05 +0000968}
969//----------------------------------------------------------------------
970// DumpSectionHeader
971//
972// Dump a single ELF section header to the specified output stream
973//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
975 const section_header_t &sh) {
976 std::string name;
977 GetSectionName(name, sh);
978 s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x "
979 "0x%4.4x 0x%8.8x\n",
980 name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
981 sh.lineoff, sh.nreloc, sh.nline, sh.flags);
Greg Claytonf754f882011-09-09 20:33:05 +0000982}
983
Greg Claytonf754f882011-09-09 20:33:05 +0000984//----------------------------------------------------------------------
985// DumpSectionHeaders
986//
987// Dump all of the ELF section header to the specified output stream
988//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
990
991 s->PutCString("Section Headers\n");
992 s->PutCString("IDX name vm addr vm size file off file "
993 "size reloc off line off nreloc nline flags\n");
994 s->PutCString("==== ---------------- ---------- ---------- ---------- "
995 "---------- ---------- ---------- ------ ------ ----------\n");
996
997 uint32_t idx = 0;
998 SectionHeaderCollIter pos, end = m_sect_headers.end();
999
1000 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1001 s->Printf("[%2u] ", idx);
1002 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1003 }
Greg Claytonf754f882011-09-09 20:33:05 +00001004}
1005
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +00001006bool ObjectFilePECOFF::IsWindowsSubsystem() {
1007 switch (m_coff_header_opt.subsystem) {
1008 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1009 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1010 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1011 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1012 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1013 case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1014 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1015 return true;
1016 default:
1017 return false;
1018 }
1019}
1020
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) {
1022 uint16_t machine = m_coff_header.machine;
1023 switch (machine) {
1024 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1025 case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1026 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1027 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1028 case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
1029 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1030 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +00001031 arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1032 IsWindowsSubsystem() ? llvm::Triple::Win32
1033 : llvm::Triple::UnknownOS);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 return true;
1035 default:
1036 break;
1037 }
1038 return false;
Greg Claytonf754f882011-09-09 20:33:05 +00001039}
1040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1042 if (m_coff_header.machine != 0) {
1043 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1044 return eTypeExecutable;
1045 else
1046 return eTypeSharedLibrary;
1047 }
1048 return eTypeExecutable;
Greg Claytonf754f882011-09-09 20:33:05 +00001049}
1050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
Greg Claytonf754f882011-09-09 20:33:05 +00001052//------------------------------------------------------------------
1053// PluginInterface protocol
1054//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
Greg Claytonf754f882011-09-09 20:33:05 +00001056
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }