LLDB  mainline
ObjectFilePECOFF.cpp
Go to the documentation of this file.
1 //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ObjectFilePECOFF.h"
10 #include "WindowsMiniDump.h"
11 
12 #include "lldb/Core/FileSpecList.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/ArchSpec.h"
24 #include "lldb/Utility/FileSpec.h"
25 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/Timer.h"
28 #include "lldb/Utility/UUID.h"
29 #include "llvm/BinaryFormat/COFF.h"
30 
31 #include "llvm/Object/COFFImportFile.h"
32 #include "llvm/Support/Error.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 
35 #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
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 
43 struct CVInfoPdb70 {
44  // 16-byte GUID
45  struct _Guid {
46  llvm::support::ulittle32_t Data1;
47  llvm::support::ulittle16_t Data2;
48  llvm::support::ulittle16_t Data3;
49  uint8_t Data4[8];
50  } Guid;
51 
52  llvm::support::ulittle32_t Age;
53 };
54 
55 static UUID GetCoffUUID(llvm::object::COFFObjectFile *coff_obj) {
56  if (!coff_obj)
57  return UUID();
58 
59  const llvm::codeview::DebugInfo *pdb_info = nullptr;
60  llvm::StringRef pdb_file;
61 
62  // This part is similar with what has done in minidump parser.
63  if (!coff_obj->getDebugPDBInfo(pdb_info, pdb_file) && pdb_info) {
64  if (pdb_info->PDB70.CVSignature == llvm::OMF::Signature::PDB70) {
65  using llvm::support::endian::read16be;
66  using llvm::support::endian::read32be;
67 
68  const uint8_t *sig = pdb_info->PDB70.Signature;
69  struct CVInfoPdb70 info;
70  info.Guid.Data1 = read32be(sig);
71  sig += 4;
72  info.Guid.Data2 = read16be(sig);
73  sig += 2;
74  info.Guid.Data3 = read16be(sig);
75  sig += 2;
76  memcpy(info.Guid.Data4, sig, 8);
77 
78  // Return 20-byte UUID if the Age is not zero
79  if (pdb_info->PDB70.Age) {
80  info.Age = read32be(&pdb_info->PDB70.Age);
81  return UUID::fromOptionalData(&info, sizeof(info));
82  }
83  // Otherwise return 16-byte GUID
84  return UUID::fromOptionalData(&info.Guid, sizeof(info.Guid));
85  }
86  }
87 
88  return UUID();
89 }
90 
92  PluginManager::RegisterPlugin(
93  GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
94  CreateMemoryInstance, GetModuleSpecifications, SaveCore);
95 }
96 
98  PluginManager::UnregisterPlugin(CreateInstance);
99 }
100 
102  static ConstString g_name("pe-coff");
103  return g_name;
104 }
105 
107  return "Portable Executable and Common Object File Format object file reader "
108  "(32 and 64 bit)";
109 }
110 
111 ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
112  DataBufferSP &data_sp,
113  lldb::offset_t data_offset,
114  const lldb_private::FileSpec *file,
115  lldb::offset_t file_offset,
116  lldb::offset_t length) {
117  if (!data_sp) {
118  data_sp = MapFileData(file, length, file_offset);
119  if (!data_sp)
120  return nullptr;
121  data_offset = 0;
122  }
123 
124  if (!ObjectFilePECOFF::MagicBytesMatch(data_sp))
125  return nullptr;
126 
127  // Update the data to contain the entire file if it doesn't already
128  if (data_sp->GetByteSize() < length) {
129  data_sp = MapFileData(file, length, file_offset);
130  if (!data_sp)
131  return nullptr;
132  }
133 
134  auto objfile_up = llvm::make_unique<ObjectFilePECOFF>(
135  module_sp, data_sp, data_offset, file, file_offset, length);
136  if (!objfile_up || !objfile_up->ParseHeader())
137  return nullptr;
138 
139  // Cache coff binary.
140  if (!objfile_up->CreateBinary())
141  return nullptr;
142 
143  return objfile_up.release();
144 }
145 
147  const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
148  const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
149  if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
150  return nullptr;
151  auto objfile_up = llvm::make_unique<ObjectFilePECOFF>(
152  module_sp, data_sp, process_sp, header_addr);
153  if (objfile_up.get() && objfile_up->ParseHeader()) {
154  return objfile_up.release();
155  }
156  return nullptr;
157 }
158 
160  const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
161  lldb::offset_t data_offset, lldb::offset_t file_offset,
163  const size_t initial_count = specs.GetSize();
164  if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
165  return initial_count;
166 
167  auto binary = llvm::object::createBinary(file.GetPath());
168  if (!binary)
169  return initial_count;
170 
171  if (!binary->getBinary()->isCOFF() &&
172  !binary->getBinary()->isCOFFImportFile())
173  return initial_count;
174 
175  auto COFFObj =
176  llvm::cast<llvm::object::COFFObjectFile>(binary->getBinary());
177 
178  ModuleSpec module_spec(file);
179  ArchSpec &spec = module_spec.GetArchitecture();
180  lldb_private::UUID &uuid = module_spec.GetUUID();
181  if (!uuid.IsValid())
182  uuid = GetCoffUUID(COFFObj);
183 
184  switch (COFFObj->getMachine()) {
185  case MachineAmd64:
186  spec.SetTriple("x86_64-pc-windows");
187  specs.Append(module_spec);
188  break;
189  case MachineX86:
190  spec.SetTriple("i386-pc-windows");
191  specs.Append(module_spec);
192  spec.SetTriple("i686-pc-windows");
193  specs.Append(module_spec);
194  break;
195  case MachineArmNt:
196  spec.SetTriple("arm-pc-windows");
197  specs.Append(module_spec);
198  break;
199  default:
200  break;
201  }
202 
203  return specs.GetSize() - initial_count;
204 }
205 
206 bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
207  const lldb_private::FileSpec &outfile,
208  lldb_private::Status &error) {
209  return SaveMiniDump(process_sp, outfile, error);
210 }
211 
212 bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
213  DataExtractor data(data_sp, eByteOrderLittle, 4);
214  lldb::offset_t offset = 0;
215  uint16_t magic = data.GetU16(&offset);
216  return magic == IMAGE_DOS_SIGNATURE;
217 }
218 
220  // TODO: We need to complete this mapping of COFF symbol types to LLDB ones.
221  // For now, here's a hack to make sure our function have types.
222  const auto complex_type =
223  coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
224  if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
225  return lldb::eSymbolTypeCode;
226  }
228 }
229 
230 bool ObjectFilePECOFF::CreateBinary() {
231  if (m_owningbin)
232  return true;
233 
235 
236  auto binary = llvm::object::createBinary(m_file.GetPath());
237  if (!binary) {
238  if (log)
239  log->Printf("ObjectFilePECOFF::CreateBinary() - failed to create binary "
240  "for file (%s): %s",
241  m_file ? m_file.GetPath().c_str() : "<NULL>",
242  errorToErrorCode(binary.takeError()).message().c_str());
243  return false;
244  }
245 
246  // Make sure we only handle COFF format.
247  if (!binary->getBinary()->isCOFF() &&
248  !binary->getBinary()->isCOFFImportFile())
249  return false;
250 
251  m_owningbin = OWNBINType(std::move(*binary));
252  if (log)
253  log->Printf("%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = "
254  "%s, binary = %p (Bin = %p)",
255  static_cast<void *>(this),
256  static_cast<void *>(GetModule().get()),
257  GetModule()->GetSpecificationDescription().c_str(),
258  m_file ? m_file.GetPath().c_str() : "<NULL>",
259  static_cast<void *>(m_owningbin.getPointer()),
260  static_cast<void *>(m_owningbin->getBinary()));
261  return true;
262 }
263 
264 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
265  DataBufferSP &data_sp,
266  lldb::offset_t data_offset,
267  const FileSpec *file,
268  lldb::offset_t file_offset,
269  lldb::offset_t length)
270  : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
271  m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
272  m_entry_point_address(), m_deps_filespec(), m_owningbin() {
273  ::memset(&m_dos_header, 0, sizeof(m_dos_header));
274  ::memset(&m_coff_header, 0, sizeof(m_coff_header));
275  ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
276 }
277 
278 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
279  DataBufferSP &header_data_sp,
280  const lldb::ProcessSP &process_sp,
281  addr_t header_addr)
282  : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
283  m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
284  m_entry_point_address(), m_deps_filespec(), m_owningbin() {
285  ::memset(&m_dos_header, 0, sizeof(m_dos_header));
286  ::memset(&m_coff_header, 0, sizeof(m_coff_header));
287  ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
288 }
289 
291 
293  ModuleSP module_sp(GetModule());
294  if (module_sp) {
295  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
296  m_sect_headers.clear();
297  m_data.SetByteOrder(eByteOrderLittle);
298  lldb::offset_t offset = 0;
299 
300  if (ParseDOSHeader(m_data, m_dos_header)) {
301  offset = m_dos_header.e_lfanew;
302  uint32_t pe_signature = m_data.GetU32(&offset);
303  if (pe_signature != IMAGE_NT_SIGNATURE)
304  return false;
305  if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
306  if (m_coff_header.hdrsize > 0)
307  ParseCOFFOptionalHeader(&offset);
308  ParseSectionHeaders(offset);
309  }
310  return true;
311  }
312  }
313  return false;
314 }
315 
317  bool value_is_offset) {
318  bool changed = false;
319  ModuleSP module_sp = GetModule();
320  if (module_sp) {
321  size_t num_loaded_sections = 0;
322  SectionList *section_list = GetSectionList();
323  if (section_list) {
324  if (!value_is_offset) {
325  value -= m_image_base;
326  }
327 
328  const size_t num_sections = section_list->GetSize();
329  size_t sect_idx = 0;
330 
331  for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
332  // Iterate through the object file sections to find all of the sections
333  // that have SHF_ALLOC in their flag bits.
334  SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
335  if (section_sp && !section_sp->IsThreadSpecific()) {
337  section_sp, section_sp->GetFileAddress() + value))
338  ++num_loaded_sections;
339  }
340  }
341  changed = num_loaded_sections > 0;
342  }
343  }
344  return changed;
345 }
346 
348 
350  return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
351 }
352 
354  if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
355  return 8;
356  else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
357  return 4;
358  return 4;
359 }
360 
361 // NeedsEndianSwap
362 //
363 // Return true if an endian swap needs to occur when extracting data from this
364 // file.
366 #if defined(__LITTLE_ENDIAN__)
367  return false;
368 #else
369  return true;
370 #endif
371 }
372 // ParseDOSHeader
375  bool success = false;
376  lldb::offset_t offset = 0;
377  success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
378 
379  if (success) {
380  dos_header.e_magic = data.GetU16(&offset); // Magic number
381  success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
382 
383  if (success) {
384  dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
385  dos_header.e_cp = data.GetU16(&offset); // Pages in file
386  dos_header.e_crlc = data.GetU16(&offset); // Relocations
387  dos_header.e_cparhdr =
388  data.GetU16(&offset); // Size of header in paragraphs
389  dos_header.e_minalloc =
390  data.GetU16(&offset); // Minimum extra paragraphs needed
391  dos_header.e_maxalloc =
392  data.GetU16(&offset); // Maximum extra paragraphs needed
393  dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
394  dos_header.e_sp = data.GetU16(&offset); // Initial SP value
395  dos_header.e_csum = data.GetU16(&offset); // Checksum
396  dos_header.e_ip = data.GetU16(&offset); // Initial IP value
397  dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
398  dos_header.e_lfarlc =
399  data.GetU16(&offset); // File address of relocation table
400  dos_header.e_ovno = data.GetU16(&offset); // Overlay number
401 
402  dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
403  dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
404  dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
405  dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
406 
407  dos_header.e_oemid =
408  data.GetU16(&offset); // OEM identifier (for e_oeminfo)
409  dos_header.e_oeminfo =
410  data.GetU16(&offset); // OEM information; e_oemid specific
411  dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
412  dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
413  dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
414  dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
415  dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
416  dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
417  dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
418  dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
419  dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
420  dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
421 
422  dos_header.e_lfanew =
423  data.GetU32(&offset); // File address of new exe header
424  }
425  }
426  if (!success)
427  memset(&dos_header, 0, sizeof(dos_header));
428  return success;
429 }
430 
431 // ParserCOFFHeader
433  lldb::offset_t *offset_ptr,
435  bool success =
436  data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
437  if (success) {
438  coff_header.machine = data.GetU16(offset_ptr);
439  coff_header.nsects = data.GetU16(offset_ptr);
440  coff_header.modtime = data.GetU32(offset_ptr);
441  coff_header.symoff = data.GetU32(offset_ptr);
442  coff_header.nsyms = data.GetU32(offset_ptr);
443  coff_header.hdrsize = data.GetU16(offset_ptr);
444  coff_header.flags = data.GetU16(offset_ptr);
445  }
446  if (!success)
447  memset(&coff_header, 0, sizeof(coff_header));
448  return success;
449 }
450 
452  bool success = false;
453  const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
454  if (*offset_ptr < end_offset) {
455  success = true;
456  m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
457  m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
458  m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
459  m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
460  m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
461  m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
462  m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
463  m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
464 
465  const uint32_t addr_byte_size = GetAddressByteSize();
466 
467  if (*offset_ptr < end_offset) {
468  if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
469  // PE32 only
470  m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
471  } else
472  m_coff_header_opt.data_offset = 0;
473 
474  if (*offset_ptr < end_offset) {
475  m_coff_header_opt.image_base =
476  m_data.GetMaxU64(offset_ptr, addr_byte_size);
477  m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
478  m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
479  m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
480  m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
481  m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
482  m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
483  m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
484  m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
485  m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
486  m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
487  m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
488  m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
489  m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
490  m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
491  m_coff_header_opt.stack_reserve_size =
492  m_data.GetMaxU64(offset_ptr, addr_byte_size);
493  m_coff_header_opt.stack_commit_size =
494  m_data.GetMaxU64(offset_ptr, addr_byte_size);
495  m_coff_header_opt.heap_reserve_size =
496  m_data.GetMaxU64(offset_ptr, addr_byte_size);
497  m_coff_header_opt.heap_commit_size =
498  m_data.GetMaxU64(offset_ptr, addr_byte_size);
499  m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
500  uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
501  m_coff_header_opt.data_dirs.clear();
502  m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
503  uint32_t i;
504  for (i = 0; i < num_data_dir_entries; i++) {
505  m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
506  m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
507  }
508 
509  m_image_base = m_coff_header_opt.image_base;
510  }
511  }
512  }
513  // Make sure we are on track for section data which follows
514  *offset_ptr = end_offset;
515  return success;
516 }
517 
519  if (m_file) {
520  // A bit of a hack, but we intend to write to this buffer, so we can't
521  // mmap it.
522  auto buffer_sp = MapFileData(m_file, size, offset);
523  return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
524  }
525  ProcessSP process_sp(m_process_wp.lock());
526  DataExtractor data;
527  if (process_sp) {
528  auto data_up = llvm::make_unique<DataBufferHeap>(size, 0);
529  Status readmem_error;
530  size_t bytes_read =
531  process_sp->ReadMemory(m_image_base + offset, data_up->GetBytes(),
532  data_up->GetByteSize(), readmem_error);
533  if (bytes_read == size) {
534  DataBufferSP buffer_sp(data_up.release());
535  data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
536  }
537  }
538  return data;
539 }
540 
541 // ParseSectionHeaders
543  uint32_t section_header_data_offset) {
544  const uint32_t nsects = m_coff_header.nsects;
545  m_sect_headers.clear();
546 
547  if (nsects > 0) {
548  const size_t section_header_byte_size = nsects * sizeof(section_header_t);
549  DataExtractor section_header_data =
550  ReadImageData(section_header_data_offset, section_header_byte_size);
551 
552  lldb::offset_t offset = 0;
553  if (section_header_data.ValidOffsetForDataOfSize(
554  offset, section_header_byte_size)) {
555  m_sect_headers.resize(nsects);
556 
557  for (uint32_t idx = 0; idx < nsects; ++idx) {
558  const void *name_data = section_header_data.GetData(&offset, 8);
559  if (name_data) {
560  memcpy(m_sect_headers[idx].name, name_data, 8);
561  m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
562  m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
563  m_sect_headers[idx].size = section_header_data.GetU32(&offset);
564  m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
565  m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
566  m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
567  m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
568  m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
569  m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
570  }
571  }
572  }
573  }
574 
575  return !m_sect_headers.empty();
576 }
577 
579  llvm::StringRef hdr_name(sect.name, llvm::array_lengthof(sect.name));
580  hdr_name = hdr_name.split('\0').first;
581  if (hdr_name.consume_front("/")) {
582  lldb::offset_t stroff;
583  if (!to_integer(hdr_name, stroff, 10))
584  return "";
585  lldb::offset_t string_file_offset =
586  m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
587  if (const char *name = m_data.GetCStr(&string_file_offset))
588  return name;
589  return "";
590  }
591  return hdr_name;
592 }
593 
594 // GetNListSymtab
596  ModuleSP module_sp(GetModule());
597  if (module_sp) {
598  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
599  if (m_symtab_up == NULL) {
600  SectionList *sect_list = GetSectionList();
601  m_symtab_up.reset(new Symtab(this));
602  std::lock_guard<std::recursive_mutex> guard(m_symtab_up->GetMutex());
603 
604  const uint32_t num_syms = m_coff_header.nsyms;
605 
606  if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
607  const uint32_t symbol_size = 18;
608  const size_t symbol_data_size = num_syms * symbol_size;
609  // Include the 4-byte string table size at the end of the symbols
610  DataExtractor symtab_data =
611  ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
612  lldb::offset_t offset = symbol_data_size;
613  const uint32_t strtab_size = symtab_data.GetU32(&offset);
614  if (strtab_size > 0) {
615  DataExtractor strtab_data = ReadImageData(
616  m_coff_header.symoff + symbol_data_size, strtab_size);
617 
618  // First 4 bytes should be zeroed after strtab_size has been read,
619  // because it is used as offset 0 to encode a NULL string.
620  uint32_t *strtab_data_start = const_cast<uint32_t *>(
621  reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart()));
622  strtab_data_start[0] = 0;
623 
624  offset = 0;
625  std::string symbol_name;
626  Symbol *symbols = m_symtab_up->Resize(num_syms);
627  for (uint32_t i = 0; i < num_syms; ++i) {
628  coff_symbol_t symbol;
629  const uint32_t symbol_offset = offset;
630  const char *symbol_name_cstr = NULL;
631  // If the first 4 bytes of the symbol string are zero, then they
632  // are followed by a 4-byte string table offset. Else these
633  // 8 bytes contain the symbol name
634  if (symtab_data.GetU32(&offset) == 0) {
635  // Long string that doesn't fit into the symbol table name, so
636  // now we must read the 4 byte string table offset
637  uint32_t strtab_offset = symtab_data.GetU32(&offset);
638  symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
639  symbol_name.assign(symbol_name_cstr);
640  } else {
641  // Short string that fits into the symbol table name which is 8
642  // bytes
643  offset += sizeof(symbol.name) - 4; // Skip remaining
644  symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
645  if (symbol_name_cstr == NULL)
646  break;
647  symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
648  }
649  symbol.value = symtab_data.GetU32(&offset);
650  symbol.sect = symtab_data.GetU16(&offset);
651  symbol.type = symtab_data.GetU16(&offset);
652  symbol.storage = symtab_data.GetU8(&offset);
653  symbol.naux = symtab_data.GetU8(&offset);
654  symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
655  if ((int16_t)symbol.sect >= 1) {
656  Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
657  symbol.value);
658  symbols[i].GetAddressRef() = symbol_addr;
659  symbols[i].SetType(MapSymbolType(symbol.type));
660  }
661 
662  if (symbol.naux > 0) {
663  i += symbol.naux;
664  offset += symbol_size;
665  }
666  }
667  }
668  }
669 
670  // Read export header
671  if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
672  m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
673  m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
674  export_directory_entry export_table;
675  uint32_t data_start =
676  m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
677 
678  uint32_t address_rva = data_start;
679  if (m_file) {
680  Address address(m_coff_header_opt.image_base + data_start, sect_list);
681  address_rva =
682  address.GetSection()->GetFileOffset() + address.GetOffset();
683  }
684  DataExtractor symtab_data =
685  ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
686  lldb::offset_t offset = 0;
687 
688  // Read export_table header
689  export_table.characteristics = symtab_data.GetU32(&offset);
690  export_table.time_date_stamp = symtab_data.GetU32(&offset);
691  export_table.major_version = symtab_data.GetU16(&offset);
692  export_table.minor_version = symtab_data.GetU16(&offset);
693  export_table.name = symtab_data.GetU32(&offset);
694  export_table.base = symtab_data.GetU32(&offset);
695  export_table.number_of_functions = symtab_data.GetU32(&offset);
696  export_table.number_of_names = symtab_data.GetU32(&offset);
697  export_table.address_of_functions = symtab_data.GetU32(&offset);
698  export_table.address_of_names = symtab_data.GetU32(&offset);
699  export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
700 
701  bool has_ordinal = export_table.address_of_name_ordinals != 0;
702 
703  lldb::offset_t name_offset = export_table.address_of_names - data_start;
704  lldb::offset_t name_ordinal_offset =
705  export_table.address_of_name_ordinals - data_start;
706 
707  Symbol *symbols = m_symtab_up->Resize(export_table.number_of_names);
708 
709  std::string symbol_name;
710 
711  // Read each export table entry
712  for (size_t i = 0; i < export_table.number_of_names; ++i) {
713  uint32_t name_ordinal =
714  has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
715  uint32_t name_address = symtab_data.GetU32(&name_offset);
716 
717  const char *symbol_name_cstr =
718  symtab_data.PeekCStr(name_address - data_start);
719  symbol_name.assign(symbol_name_cstr);
720 
721  lldb::offset_t function_offset = export_table.address_of_functions -
722  data_start +
723  sizeof(uint32_t) * name_ordinal;
724  uint32_t function_rva = symtab_data.GetU32(&function_offset);
725 
726  Address symbol_addr(m_coff_header_opt.image_base + function_rva,
727  sect_list);
728  symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
729  symbols[i].GetAddressRef() = symbol_addr;
730  symbols[i].SetType(lldb::eSymbolTypeCode);
731  symbols[i].SetDebug(true);
732  }
733  }
734  m_symtab_up->CalculateSymbolSizes();
735  }
736  }
737  return m_symtab_up.get();
738 }
739 
741  // TODO: determine this for COFF
742  return false;
743 }
744 
745 void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
746  if (m_sections_up)
747  return;
748  m_sections_up.reset(new SectionList());
749 
750  ModuleSP module_sp(GetModule());
751  if (module_sp) {
752  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
753 
754  SectionSP image_sp = std::make_shared<Section>(
755  module_sp, this, ~user_id_t(0), ConstString(), eSectionTypeContainer,
756  m_coff_header_opt.image_base, m_coff_header_opt.image_size,
757  /*file_offset*/ 0, /*file_size*/ 0, m_coff_header_opt.sect_alignment,
758  /*flags*/ 0);
759  m_sections_up->AddSection(image_sp);
760  unified_section_list.AddSection(image_sp);
761 
762  const uint32_t nsects = m_sect_headers.size();
763  ModuleSP module_sp(GetModule());
764  for (uint32_t idx = 0; idx < nsects; ++idx) {
765  ConstString const_sect_name(GetSectionName(m_sect_headers[idx]));
766  static ConstString g_code_sect_name(".code");
767  static ConstString g_CODE_sect_name("CODE");
768  static ConstString g_data_sect_name(".data");
769  static ConstString g_DATA_sect_name("DATA");
770  static ConstString g_bss_sect_name(".bss");
771  static ConstString g_BSS_sect_name("BSS");
772  static ConstString g_debug_sect_name(".debug");
773  static ConstString g_reloc_sect_name(".reloc");
774  static ConstString g_stab_sect_name(".stab");
775  static ConstString g_stabstr_sect_name(".stabstr");
776  static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
777  static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
778  static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
779  static ConstString g_sect_name_dwarf_debug_info(".debug_info");
780  static ConstString g_sect_name_dwarf_debug_line(".debug_line");
781  static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
782  static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists");
783  static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
784  static ConstString g_sect_name_dwarf_debug_names(".debug_names");
785  static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
786  static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
787  static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
788  static ConstString g_sect_name_dwarf_debug_str(".debug_str");
789  static ConstString g_sect_name_dwarf_debug_types(".debug_types");
790  static ConstString g_sect_name_eh_frame(".eh_frame");
791  static ConstString g_sect_name_go_symtab(".gosymtab");
792  SectionType section_type = eSectionTypeOther;
793  if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
794  ((const_sect_name == g_code_sect_name) ||
795  (const_sect_name == g_CODE_sect_name))) {
796  section_type = eSectionTypeCode;
797  } else if (m_sect_headers[idx].flags &
798  llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
799  ((const_sect_name == g_data_sect_name) ||
800  (const_sect_name == g_DATA_sect_name))) {
801  if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0)
802  section_type = eSectionTypeZeroFill;
803  else
804  section_type = eSectionTypeData;
805  } else if (m_sect_headers[idx].flags &
806  llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
807  ((const_sect_name == g_bss_sect_name) ||
808  (const_sect_name == g_BSS_sect_name))) {
809  if (m_sect_headers[idx].size == 0)
810  section_type = eSectionTypeZeroFill;
811  else
812  section_type = eSectionTypeData;
813  } else if (const_sect_name == g_debug_sect_name) {
814  section_type = eSectionTypeDebug;
815  } else if (const_sect_name == g_stabstr_sect_name) {
816  section_type = eSectionTypeDataCString;
817  } else if (const_sect_name == g_reloc_sect_name) {
818  section_type = eSectionTypeOther;
819  } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
820  section_type = eSectionTypeDWARFDebugAbbrev;
821  else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
822  section_type = eSectionTypeDWARFDebugAranges;
823  else if (const_sect_name == g_sect_name_dwarf_debug_frame)
824  section_type = eSectionTypeDWARFDebugFrame;
825  else if (const_sect_name == g_sect_name_dwarf_debug_info)
826  section_type = eSectionTypeDWARFDebugInfo;
827  else if (const_sect_name == g_sect_name_dwarf_debug_line)
828  section_type = eSectionTypeDWARFDebugLine;
829  else if (const_sect_name == g_sect_name_dwarf_debug_loc)
830  section_type = eSectionTypeDWARFDebugLoc;
831  else if (const_sect_name == g_sect_name_dwarf_debug_loclists)
832  section_type = eSectionTypeDWARFDebugLocLists;
833  else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
834  section_type = eSectionTypeDWARFDebugMacInfo;
835  else if (const_sect_name == g_sect_name_dwarf_debug_names)
836  section_type = eSectionTypeDWARFDebugNames;
837  else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
838  section_type = eSectionTypeDWARFDebugPubNames;
839  else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
840  section_type = eSectionTypeDWARFDebugPubTypes;
841  else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
842  section_type = eSectionTypeDWARFDebugRanges;
843  else if (const_sect_name == g_sect_name_dwarf_debug_str)
844  section_type = eSectionTypeDWARFDebugStr;
845  else if (const_sect_name == g_sect_name_dwarf_debug_types)
846  section_type = eSectionTypeDWARFDebugTypes;
847  else if (const_sect_name == g_sect_name_eh_frame)
848  section_type = eSectionTypeEHFrame;
849  else if (const_sect_name == g_sect_name_go_symtab)
850  section_type = eSectionTypeGoSymtab;
851  else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
852  section_type = eSectionTypeCode;
853  } else if (m_sect_headers[idx].flags &
854  llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
855  section_type = eSectionTypeData;
856  } else if (m_sect_headers[idx].flags &
857  llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
858  if (m_sect_headers[idx].size == 0)
859  section_type = eSectionTypeZeroFill;
860  else
861  section_type = eSectionTypeData;
862  }
863 
864  SectionSP section_sp(new Section(
865  image_sp, // Parent section
866  module_sp, // Module to which this section belongs
867  this, // Object file to which this section belongs
868  idx + 1, // Section ID is the 1 based section index.
869  const_sect_name, // Name of this section
870  section_type,
871  m_sect_headers[idx].vmaddr, // File VM address == addresses as
872  // they are found in the object file
873  m_sect_headers[idx].vmsize, // VM size in bytes of this section
874  m_sect_headers[idx]
875  .offset, // Offset to the data for this section in the file
876  m_sect_headers[idx]
877  .size, // Size in bytes of this section as found in the file
878  m_coff_header_opt.sect_alignment, // Section alignment
879  m_sect_headers[idx].flags)); // Flags for this section
880 
881  image_sp->GetChildren().AddSection(std::move(section_sp));
882  }
883  }
884 }
885 
887  if (m_uuid.IsValid())
888  return m_uuid;
889 
890  if (!CreateBinary())
891  return UUID();
892 
893  auto COFFObj =
894  llvm::cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary());
895 
896  m_uuid = GetCoffUUID(COFFObj);
897  return m_uuid;
898 }
899 
901  ModuleSP module_sp(GetModule());
902  if (!module_sp)
903  return 0;
904 
905  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
906  if (m_deps_filespec)
907  return m_deps_filespec->GetSize();
908 
909  // Cache coff binary if it is not done yet.
910  if (!CreateBinary())
911  return 0;
912 
914  if (log)
915  log->Printf("%p ObjectFilePECOFF::ParseDependentModules() module = %p "
916  "(%s), binary = %p (Bin = %p)",
917  static_cast<void *>(this), static_cast<void *>(module_sp.get()),
918  module_sp->GetSpecificationDescription().c_str(),
919  static_cast<void *>(m_owningbin.getPointer()),
920  static_cast<void *>(m_owningbin->getBinary()));
921 
922  auto COFFObj =
923  llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary());
924  if (!COFFObj)
925  return 0;
926 
927  m_deps_filespec = FileSpecList();
928 
929  for (const auto &entry : COFFObj->import_directories()) {
930  llvm::StringRef dll_name;
931  auto ec = entry.getName(dll_name);
932  // Report a bogus entry.
933  if (ec != std::error_code()) {
934  if (log)
935  log->Printf("ObjectFilePECOFF::ParseDependentModules() - failed to get "
936  "import directory entry name: %s",
937  ec.message().c_str());
938  continue;
939  }
940 
941  // At this moment we only have the base name of the DLL. The full path can
942  // only be seen after the dynamic loading. Our best guess is Try to get it
943  // with the help of the object file's directory.
944  llvm::SmallString<128> dll_fullpath;
945  FileSpec dll_specs(dll_name);
946  dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString());
947 
948  if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath))
949  m_deps_filespec->Append(FileSpec(dll_fullpath));
950  else {
951  // Known DLLs or DLL not found in the object file directory.
952  m_deps_filespec->Append(FileSpec(dll_name));
953  }
954  }
955  return m_deps_filespec->GetSize();
956 }
957 
959  auto num_modules = ParseDependentModules();
960  auto original_size = files.GetSize();
961 
962  for (unsigned i = 0; i < num_modules; ++i)
963  files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i));
964 
965  return files.GetSize() - original_size;
966 }
967 
969  if (m_entry_point_address.IsValid())
970  return m_entry_point_address;
971 
972  if (!ParseHeader() || !IsExecutable())
973  return m_entry_point_address;
974 
975  SectionList *section_list = GetSectionList();
976  addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base;
977 
978  if (!section_list)
979  m_entry_point_address.SetOffset(file_addr);
980  else
981  m_entry_point_address.ResolveAddressUsingFileSections(file_addr,
982  section_list);
983  return m_entry_point_address;
984 }
985 
987  return Address(GetSectionList()->GetSectionAtIndex(0), 0);
988 }
989 
990 // Dump
991 //
992 // Dump the specifics of the runtime file container (such as any headers
993 // segments, sections, etc).
995  ModuleSP module_sp(GetModule());
996  if (module_sp) {
997  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
998  s->Printf("%p: ", static_cast<void *>(this));
999  s->Indent();
1000  s->PutCString("ObjectFilePECOFF");
1001 
1002  ArchSpec header_arch = GetArchitecture();
1003 
1004  *s << ", file = '" << m_file
1005  << "', arch = " << header_arch.GetArchitectureName() << "\n";
1006 
1007  SectionList *sections = GetSectionList();
1008  if (sections)
1009  sections->Dump(s, NULL, true, UINT32_MAX);
1010 
1011  if (m_symtab_up)
1012  m_symtab_up->Dump(s, NULL, eSortOrderNone);
1013 
1014  if (m_dos_header.e_magic)
1015  DumpDOSHeader(s, m_dos_header);
1016  if (m_coff_header.machine) {
1017  DumpCOFFHeader(s, m_coff_header);
1018  if (m_coff_header.hdrsize)
1019  DumpOptCOFFHeader(s, m_coff_header_opt);
1020  }
1021  s->EOL();
1022  DumpSectionHeaders(s);
1023  s->EOL();
1024 
1025  DumpDependentModules(s);
1026  s->EOL();
1027  }
1028 }
1029 
1030 // DumpDOSHeader
1031 //
1032 // Dump the MS-DOS header to the specified output stream
1034  s->PutCString("MSDOS Header\n");
1035  s->Printf(" e_magic = 0x%4.4x\n", header.e_magic);
1036  s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp);
1037  s->Printf(" e_cp = 0x%4.4x\n", header.e_cp);
1038  s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc);
1039  s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
1040  s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc);
1041  s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
1042  s->Printf(" e_ss = 0x%4.4x\n", header.e_ss);
1043  s->Printf(" e_sp = 0x%4.4x\n", header.e_sp);
1044  s->Printf(" e_csum = 0x%4.4x\n", header.e_csum);
1045  s->Printf(" e_ip = 0x%4.4x\n", header.e_ip);
1046  s->Printf(" e_cs = 0x%4.4x\n", header.e_cs);
1047  s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
1048  s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno);
1049  s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
1050  header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
1051  s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid);
1052  s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
1053  s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
1054  "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
1055  header.e_res2[0], header.e_res2[1], header.e_res2[2],
1056  header.e_res2[3], header.e_res2[4], header.e_res2[5],
1057  header.e_res2[6], header.e_res2[7], header.e_res2[8],
1058  header.e_res2[9]);
1059  s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew);
1060 }
1061 
1062 // DumpCOFFHeader
1063 //
1064 // Dump the COFF header to the specified output stream
1066  s->PutCString("COFF Header\n");
1067  s->Printf(" machine = 0x%4.4x\n", header.machine);
1068  s->Printf(" nsects = 0x%4.4x\n", header.nsects);
1069  s->Printf(" modtime = 0x%8.8x\n", header.modtime);
1070  s->Printf(" symoff = 0x%8.8x\n", header.symoff);
1071  s->Printf(" nsyms = 0x%8.8x\n", header.nsyms);
1072  s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize);
1073 }
1074 
1075 // DumpOptCOFFHeader
1076 //
1077 // Dump the optional COFF header to the specified output stream
1079  const coff_opt_header_t &header) {
1080  s->PutCString("Optional COFF Header\n");
1081  s->Printf(" magic = 0x%4.4x\n", header.magic);
1082  s->Printf(" major_linker_version = 0x%2.2x\n",
1083  header.major_linker_version);
1084  s->Printf(" minor_linker_version = 0x%2.2x\n",
1085  header.minor_linker_version);
1086  s->Printf(" code_size = 0x%8.8x\n", header.code_size);
1087  s->Printf(" data_size = 0x%8.8x\n", header.data_size);
1088  s->Printf(" bss_size = 0x%8.8x\n", header.bss_size);
1089  s->Printf(" entry = 0x%8.8x\n", header.entry);
1090  s->Printf(" code_offset = 0x%8.8x\n", header.code_offset);
1091  s->Printf(" data_offset = 0x%8.8x\n", header.data_offset);
1092  s->Printf(" image_base = 0x%16.16" PRIx64 "\n",
1093  header.image_base);
1094  s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment);
1095  s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment);
1096  s->Printf(" major_os_system_version = 0x%4.4x\n",
1097  header.major_os_system_version);
1098  s->Printf(" minor_os_system_version = 0x%4.4x\n",
1099  header.minor_os_system_version);
1100  s->Printf(" major_image_version = 0x%4.4x\n",
1101  header.major_image_version);
1102  s->Printf(" minor_image_version = 0x%4.4x\n",
1103  header.minor_image_version);
1104  s->Printf(" major_subsystem_version = 0x%4.4x\n",
1105  header.major_subsystem_version);
1106  s->Printf(" minor_subsystem_version = 0x%4.4x\n",
1107  header.minor_subsystem_version);
1108  s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1);
1109  s->Printf(" image_size = 0x%8.8x\n", header.image_size);
1110  s->Printf(" header_size = 0x%8.8x\n", header.header_size);
1111  s->Printf(" checksum = 0x%8.8x\n", header.checksum);
1112  s->Printf(" subsystem = 0x%4.4x\n", header.subsystem);
1113  s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags);
1114  s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n",
1115  header.stack_reserve_size);
1116  s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n",
1117  header.stack_commit_size);
1118  s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n",
1119  header.heap_reserve_size);
1120  s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n",
1121  header.heap_commit_size);
1122  s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags);
1123  s->Printf(" num_data_dir_entries = 0x%8.8x\n",
1124  (uint32_t)header.data_dirs.size());
1125  uint32_t i;
1126  for (i = 0; i < header.data_dirs.size(); i++) {
1127  s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
1128  header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
1129  }
1130 }
1131 // DumpSectionHeader
1132 //
1133 // Dump a single ELF section header to the specified output stream
1135  const section_header_t &sh) {
1136  std::string name = GetSectionName(sh);
1137  s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x "
1138  "0x%4.4x 0x%8.8x\n",
1139  name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
1140  sh.lineoff, sh.nreloc, sh.nline, sh.flags);
1141 }
1142 
1143 // DumpSectionHeaders
1144 //
1145 // Dump all of the ELF section header to the specified output stream
1147 
1148  s->PutCString("Section Headers\n");
1149  s->PutCString("IDX name vm addr vm size file off file "
1150  "size reloc off line off nreloc nline flags\n");
1151  s->PutCString("==== ---------------- ---------- ---------- ---------- "
1152  "---------- ---------- ---------- ------ ------ ----------\n");
1153 
1154  uint32_t idx = 0;
1155  SectionHeaderCollIter pos, end = m_sect_headers.end();
1156 
1157  for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
1158  s->Printf("[%2u] ", idx);
1160  }
1161 }
1162 
1163 // DumpDependentModules
1164 //
1165 // Dump all of the dependent modules to the specified output stream
1167  auto num_modules = ParseDependentModules();
1168  if (num_modules > 0) {
1169  s->PutCString("Dependent Modules\n");
1170  for (unsigned i = 0; i < num_modules; ++i) {
1171  auto spec = m_deps_filespec->GetFileSpecAtIndex(i);
1172  s->Printf(" %s\n", spec.GetFilename().GetCString());
1173  }
1174  }
1175 }
1176 
1178  switch (m_coff_header_opt.subsystem) {
1179  case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
1180  case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
1181  case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
1182  case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
1183  case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1184  case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
1185  case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
1186  return true;
1187  default:
1188  return false;
1189  }
1190 }
1191 
1193  uint16_t machine = m_coff_header.machine;
1194  switch (machine) {
1195  default:
1196  break;
1197  case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1198  case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1199  case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1200  case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1201  case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
1202  case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1203  case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
1204  ArchSpec arch;
1206  IsWindowsSubsystem() ? llvm::Triple::Win32
1207  : llvm::Triple::UnknownOS);
1208  return arch;
1209  }
1210  return ArchSpec();
1211 }
1212 
1214  if (m_coff_header.machine != 0) {
1215  if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1216  return eTypeExecutable;
1217  else
1218  return eTypeSharedLibrary;
1219  }
1220  return eTypeExecutable;
1221 }
1222 
1224 
1225 // PluginInterface protocol
1226 ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
1227 
bool IsExecutable() const override
Tells whether this object file is capable of being the main executable for a process.
SectionHeaderColl::iterator SectionHeaderCollIter
Address & GetAddressRef()
Definition: Symbol.h:56
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
An data extractor class.
Definition: DataExtractor.h:47
void SetString(const llvm::StringRef &s)
#define LIBLLDB_LOG_OBJECT
Definition: Logging.h:25
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:797
bool IsValid() const
Definition: UUID.h:65
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
bool IsStripped() override
Detect if this object file has been stripped of local symbols.
llvm::support::ulittle32_t Data1
lldb_private::ConstString GetPluginName() override
static void DumpDOSHeader(lldb_private::Stream *s, const dos_header_t &header)
static void Initialize()
uint32_t address_of_names
static bool SaveCore(const lldb::ProcessSP &process_sp, const lldb_private::FileSpec &outfile, lldb_private::Status &error)
lldb_private::Address GetBaseAddress() override
Returns base address of this object file.
A file utility class.
Definition: FileSpec.h:55
An architecture specification class.
Definition: ArchSpec.h:32
#define IMAGE_NT_SIGNATURE
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
const char * PeekCStr(lldb::offset_t offset) const
Peek at a C string at offset.
bool ParseHeader() override
Attempts to parse the object header.
uint32_t ParseDependentModules()
bool NeedsEndianSwap() const
#define LLDB_INVALID_CPUTYPE
Definition: lldb-defines.h:104
uint32_t base
uint16_t major_version
lldb_private::DataExtractor ReadImageData(uint32_t offset, size_t size)
bool SetSectionLoadAddress(const lldb::SectionSP &section_sp, lldb::addr_t load_addr, bool warn_multiple=false)
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
lldb::ByteOrder GetByteOrder() const override
Gets whether endian swapping should occur when extracting data from this object file.
llvm::support::ulittle32_t Age
static void DumpOptCOFFHeader(lldb_private::Stream *s, const coff_opt_header_t &header)
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition: ArchSpec.cpp:591
#define UINT32_MAX
Definition: lldb-defines.h:31
#define OPT_HEADER_MAGIC_PE32
void DumpSectionHeader(lldb_private::Stream *s, const section_header_t &sh)
uint64_t user_id_t
Definition: lldb-types.h:84
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override
bool ParseSectionHeaders(uint32_t offset)
uint32_t GetPluginVersion() override
ObjectFilePECOFF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
uint64_t offset_t
Definition: lldb-types.h:87
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
bool ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
static UUID GetCoffUUID(llvm::object::COFFObjectFile *coff_obj)
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1012
std::vector< data_directory > data_dirs
uint32_t number_of_names
void DumpSectionHeaders(lldb_private::Stream *s)
static lldb::SymbolType MapSymbolType(uint16_t coff_symbol_type)
lldb_private::Address GetEntryPointAddress() override
Returns the address of the Entry Point in this object file - if the object file doesn&#39;t have an entry...
void Append(const ModuleSpec &spec)
Definition: ModuleSpec.h:333
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
static ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:497
void DumpDependentModules(lldb_private::Stream *s)
void SetDebug(bool b)
Definition: Symbol.h:159
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
ObjectFile::Type CalculateType() override
The object file should be able to calculate its type by looking at its file header and possibly the s...
A section + offset based address class.
Definition: Address.h:80
llvm::StringRef GetSectionName(const section_header_t &sect)
struct CVInfoPdb70::_Guid Guid
void Dump(lldb_private::Stream *s) override
Dump a description of this object to a Stream.
bool SaveMiniDump(const lldb::ProcessSP &process_sp, const lldb_private::FileSpec &outfile, lldb_private::Status &error)
static bool MagicBytesMatch(lldb::DataBufferSP &data_sp)
ObjectFile::Strata CalculateStrata() override
The object file should be able to calculate the strata of the object file.
llvm::support::ulittle16_t Data2
ConstString & GetDirectory()
Directory string get accessor.
Definition: FileSpec.cpp:363
uint64_t addr_t
Definition: lldb-types.h:83
const uint8_t * GetDataStart() const
Get the data start pointer.
A uniqued constant string class.
Definition: ConstString.h:38
uint32_t name
llvm::support::ulittle16_t Data3
static bool ParseDOSHeader(lldb_private::DataExtractor &data, dos_header_t &dos_header)
lldb_private::ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
void CreateSections(lldb_private::SectionList &unified_section_list) override
Definition: SBAddress.h:15
uint32_t address_of_functions
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
size_t AddSection(const lldb::SectionSP &section_sp)
Definition: Section.cpp:428
size_t GetSize() const
Definition: Section.h:83
~ObjectFilePECOFF() override
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
uint16_t minor_version
static const char * GetPluginDescriptionStatic()
static lldb_private::ConstString GetPluginNameStatic()
#define IMAGE_DOS_SIGNATURE
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:410
uint32_t time_date_stamp
bool SetArchitecture(ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os=0)
Change the architecture object type, CPU type and OS type.
Definition: ArchSpec.cpp:892
#define OPT_HEADER_MAGIC_PE32_PLUS
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
static bool ParseCOFFHeader(lldb_private::DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header)
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
static void DumpCOFFHeader(lldb_private::Stream *s, const coff_header_t &header)
uint32_t address_of_name_ordinals
uint32_t number_of_functions
uint32_t characteristics
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
lldb_private::Symtab * GetSymtab() override
Gets the symbol table for the currently selected architecture (and object for archives).
void SetType(lldb::SymbolType type)
Definition: Symbol.h:138
static void Terminate()
An error handling class.
Definition: Status.h:44
void Dump(Stream *s, Target *target, bool show_header, uint32_t depth) const
Definition: Section.cpp:591
bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override
Sets the load address for an entire module, assuming a rigid slide of sections, if possible in the im...
Mangled & GetMangled()
Definition: Symbol.h:120