Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 1 | //===-- 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 McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 11 | #include "WindowsMiniDump.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 12 | |
Charles Davis | 237ad97 | 2013-08-27 05:04:33 +0000 | [diff] [blame] | 13 | #include "llvm/Support/COFF.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 14 | |
| 15 | #include "lldb/Core/ArchSpec.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 16 | #include "lldb/Core/FileSpecList.h" |
| 17 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ModuleSpec.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/Section.h" |
| 21 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 22 | #include "lldb/Core/Timer.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ObjectFile.h" |
Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Process.h" |
Virgile Bello | 2756adf | 2014-03-08 17:17:20 +0000 | [diff] [blame] | 25 | #include "lldb/Target/SectionLoadList.h" |
| 26 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/DataBufferHeap.h" |
| 28 | #include "lldb/Utility/DataBufferLLVM.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/UUID.h" |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 32 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
| 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 39 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | void ObjectFilePECOFF::Initialize() { |
| 44 | PluginManager::RegisterPlugin( |
| 45 | GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, |
| 46 | CreateMemoryInstance, GetModuleSpecifications, SaveCore); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | void ObjectFilePECOFF::Terminate() { |
| 50 | PluginManager::UnregisterPlugin(CreateInstance); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() { |
| 54 | static ConstString g_name("pe-coff"); |
| 55 | return g_name; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | const char *ObjectFilePECOFF::GetPluginDescriptionStatic() { |
| 59 | return "Portable Executable and Common Object File Format object file reader " |
| 60 | "(32 and 64 bit)"; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | ObjectFile *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 Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 70 | data_sp = |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 71 | DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 72 | if (!data_sp) |
| 73 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | data_offset = 0; |
| 75 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 76 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 77 | 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 Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 82 | data_sp = |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 83 | DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 84 | if (!data_sp) |
| 85 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | } |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 87 | |
| 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 Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( |
| 97 | const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, |
| 98 | const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 99 | 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 Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | size_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 Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) { |
| 116 | DataExtractor data; |
| 117 | data.SetData(data_sp, data_offset, length); |
| 118 | data.SetByteOrder(eByteOrderLittle); |
Virgile Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | dos_header_t dos_header; |
| 121 | coff_header_t coff_header; |
Virgile Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | 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 Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 138 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | } |
Virgile Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 140 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | } |
Virgile Bello | 89eb1ba | 2014-03-09 09:59:36 +0000 | [diff] [blame] | 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | return specs.GetSize() - initial_count; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, |
| 147 | const lldb_private::FileSpec &outfile, |
| 148 | lldb_private::Error &error) { |
| 149 | return SaveMiniDump(process_sp, outfile, error); |
Adrian McCarthy | f7d1893 | 2015-11-20 23:09:11 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) { |
| 153 | DataExtractor data(data_sp, eByteOrderLittle, 4); |
| 154 | lldb::offset_t offset = 0; |
| 155 | uint16_t magic = data.GetU16(&offset); |
| 156 | return magic == IMAGE_DOS_SIGNATURE; |
| 157 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { |
| 160 | // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. |
| 161 | // For now, here's a hack to make sure our function have types. |
| 162 | const auto complex_type = |
| 163 | coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; |
| 164 | if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { |
| 165 | return lldb::eSymbolTypeCode; |
| 166 | } |
| 167 | return lldb::eSymbolTypeInvalid; |
| 168 | } |
| 169 | |
| 170 | ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, |
| 171 | DataBufferSP &data_sp, |
| 172 | lldb::offset_t data_offset, |
| 173 | const FileSpec *file, |
| 174 | lldb::offset_t file_offset, |
| 175 | lldb::offset_t length) |
| 176 | : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
| 177 | m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(), |
| 178 | m_entry_point_address() { |
| 179 | ::memset(&m_dos_header, 0, sizeof(m_dos_header)); |
| 180 | ::memset(&m_coff_header, 0, sizeof(m_coff_header)); |
| 181 | ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); |
| 182 | } |
| 183 | |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 184 | ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, |
| 185 | DataBufferSP &header_data_sp, |
| 186 | const lldb::ProcessSP &process_sp, |
| 187 | addr_t header_addr) |
| 188 | : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), |
| 189 | m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(), |
| 190 | m_entry_point_address() { |
| 191 | ::memset(&m_dos_header, 0, sizeof(m_dos_header)); |
| 192 | ::memset(&m_coff_header, 0, sizeof(m_coff_header)); |
| 193 | ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); |
| 194 | } |
| 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | ObjectFilePECOFF::~ObjectFilePECOFF() {} |
| 197 | |
| 198 | bool ObjectFilePECOFF::ParseHeader() { |
| 199 | ModuleSP module_sp(GetModule()); |
| 200 | if (module_sp) { |
| 201 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 202 | m_sect_headers.clear(); |
| 203 | m_data.SetByteOrder(eByteOrderLittle); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 204 | lldb::offset_t offset = 0; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | if (ParseDOSHeader(m_data, m_dos_header)) { |
| 207 | offset = m_dos_header.e_lfanew; |
| 208 | uint32_t pe_signature = m_data.GetU32(&offset); |
| 209 | if (pe_signature != IMAGE_NT_SIGNATURE) |
| 210 | return false; |
| 211 | if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { |
| 212 | if (m_coff_header.hdrsize > 0) |
| 213 | ParseCOFFOptionalHeader(&offset); |
| 214 | ParseSectionHeaders(offset); |
| 215 | } |
| 216 | return true; |
Adrian McCarthy | c35b91c | 2016-01-26 00:58:09 +0000 | [diff] [blame] | 217 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | } |
| 219 | return false; |
Adrian McCarthy | c35b91c | 2016-01-26 00:58:09 +0000 | [diff] [blame] | 220 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, |
| 223 | bool value_is_offset) { |
| 224 | bool changed = false; |
| 225 | ModuleSP module_sp = GetModule(); |
| 226 | if (module_sp) { |
| 227 | size_t num_loaded_sections = 0; |
| 228 | SectionList *section_list = GetSectionList(); |
| 229 | if (section_list) { |
| 230 | if (!value_is_offset) { |
| 231 | value -= m_image_base; |
| 232 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | const size_t num_sections = section_list->GetSize(); |
| 235 | size_t sect_idx = 0; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 236 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
| 238 | // Iterate through the object file sections to find all |
| 239 | // of the sections that have SHF_ALLOC in their flag bits. |
| 240 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
| 241 | if (section_sp && !section_sp->IsThreadSpecific()) { |
| 242 | if (target.GetSectionLoadList().SetSectionLoadAddress( |
| 243 | section_sp, section_sp->GetFileAddress() + value)) |
| 244 | ++num_loaded_sections; |
Greg Clayton | 28469ca | 2011-09-10 01:04:42 +0000 | [diff] [blame] | 245 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | } |
| 247 | changed = num_loaded_sections > 0; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 248 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | } |
| 250 | return changed; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } |
Virgile Bello | 2756adf | 2014-03-08 17:17:20 +0000 | [diff] [blame] | 254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | bool ObjectFilePECOFF::IsExecutable() const { |
| 256 | return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; |
Virgile Bello | 2756adf | 2014-03-08 17:17:20 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | uint32_t ObjectFilePECOFF::GetAddressByteSize() const { |
| 260 | if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) |
| 261 | return 8; |
| 262 | else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 263 | return 4; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | return 4; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | //---------------------------------------------------------------------- |
| 268 | // NeedsEndianSwap |
| 269 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | // Return true if an endian swap needs to occur when extracting data |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 271 | // from this file. |
| 272 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | bool ObjectFilePECOFF::NeedsEndianSwap() const { |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 274 | #if defined(__LITTLE_ENDIAN__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | return false; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 276 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | return true; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 278 | #endif |
| 279 | } |
| 280 | //---------------------------------------------------------------------- |
| 281 | // ParseDOSHeader |
| 282 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, |
| 284 | dos_header_t &dos_header) { |
| 285 | bool success = false; |
| 286 | lldb::offset_t offset = 0; |
| 287 | success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 288 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 289 | if (success) { |
| 290 | dos_header.e_magic = data.GetU16(&offset); // Magic number |
| 291 | success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; |
| 292 | |
| 293 | if (success) { |
| 294 | dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file |
| 295 | dos_header.e_cp = data.GetU16(&offset); // Pages in file |
| 296 | dos_header.e_crlc = data.GetU16(&offset); // Relocations |
| 297 | dos_header.e_cparhdr = |
| 298 | data.GetU16(&offset); // Size of header in paragraphs |
| 299 | dos_header.e_minalloc = |
| 300 | data.GetU16(&offset); // Minimum extra paragraphs needed |
| 301 | dos_header.e_maxalloc = |
| 302 | data.GetU16(&offset); // Maximum extra paragraphs needed |
| 303 | dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value |
| 304 | dos_header.e_sp = data.GetU16(&offset); // Initial SP value |
| 305 | dos_header.e_csum = data.GetU16(&offset); // Checksum |
| 306 | dos_header.e_ip = data.GetU16(&offset); // Initial IP value |
| 307 | dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value |
| 308 | dos_header.e_lfarlc = |
| 309 | data.GetU16(&offset); // File address of relocation table |
| 310 | dos_header.e_ovno = data.GetU16(&offset); // Overlay number |
| 311 | |
| 312 | dos_header.e_res[0] = data.GetU16(&offset); // Reserved words |
| 313 | dos_header.e_res[1] = data.GetU16(&offset); // Reserved words |
| 314 | dos_header.e_res[2] = data.GetU16(&offset); // Reserved words |
| 315 | dos_header.e_res[3] = data.GetU16(&offset); // Reserved words |
| 316 | |
| 317 | dos_header.e_oemid = |
| 318 | data.GetU16(&offset); // OEM identifier (for e_oeminfo) |
| 319 | dos_header.e_oeminfo = |
| 320 | data.GetU16(&offset); // OEM information; e_oemid specific |
| 321 | dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words |
| 322 | dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words |
| 323 | dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words |
| 324 | dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words |
| 325 | dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words |
| 326 | dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words |
| 327 | dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words |
| 328 | dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words |
| 329 | dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words |
| 330 | dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words |
| 331 | |
| 332 | dos_header.e_lfanew = |
| 333 | data.GetU32(&offset); // File address of new exe header |
| 334 | } |
| 335 | } |
| 336 | if (!success) |
| 337 | memset(&dos_header, 0, sizeof(dos_header)); |
| 338 | return success; |
| 339 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 340 | |
| 341 | //---------------------------------------------------------------------- |
| 342 | // ParserCOFFHeader |
| 343 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, |
| 345 | lldb::offset_t *offset_ptr, |
| 346 | coff_header_t &coff_header) { |
| 347 | bool success = |
| 348 | data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); |
| 349 | if (success) { |
| 350 | coff_header.machine = data.GetU16(offset_ptr); |
| 351 | coff_header.nsects = data.GetU16(offset_ptr); |
| 352 | coff_header.modtime = data.GetU32(offset_ptr); |
| 353 | coff_header.symoff = data.GetU32(offset_ptr); |
| 354 | coff_header.nsyms = data.GetU32(offset_ptr); |
| 355 | coff_header.hdrsize = data.GetU16(offset_ptr); |
| 356 | coff_header.flags = data.GetU16(offset_ptr); |
| 357 | } |
| 358 | if (!success) |
| 359 | memset(&coff_header, 0, sizeof(coff_header)); |
| 360 | return success; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { |
| 364 | bool success = false; |
| 365 | const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; |
| 366 | if (*offset_ptr < end_offset) { |
| 367 | success = true; |
| 368 | m_coff_header_opt.magic = m_data.GetU16(offset_ptr); |
| 369 | m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); |
| 370 | m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); |
| 371 | m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); |
| 372 | m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); |
| 373 | m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); |
| 374 | m_coff_header_opt.entry = m_data.GetU32(offset_ptr); |
| 375 | m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 378 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | if (*offset_ptr < end_offset) { |
| 380 | if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { |
| 381 | // PE32 only |
| 382 | m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); |
| 383 | } else |
| 384 | m_coff_header_opt.data_offset = 0; |
Virgile Bello | 2756adf | 2014-03-08 17:17:20 +0000 | [diff] [blame] | 385 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 386 | if (*offset_ptr < end_offset) { |
| 387 | m_coff_header_opt.image_base = |
| 388 | m_data.GetMaxU64(offset_ptr, addr_byte_size); |
| 389 | m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); |
| 390 | m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); |
| 391 | m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); |
| 392 | m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); |
| 393 | m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); |
| 394 | m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); |
| 395 | m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); |
| 396 | m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); |
| 397 | m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); |
| 398 | m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); |
| 399 | m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); |
| 400 | m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); |
| 401 | m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); |
| 402 | m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); |
| 403 | m_coff_header_opt.stack_reserve_size = |
| 404 | m_data.GetMaxU64(offset_ptr, addr_byte_size); |
| 405 | m_coff_header_opt.stack_commit_size = |
| 406 | m_data.GetMaxU64(offset_ptr, addr_byte_size); |
| 407 | m_coff_header_opt.heap_reserve_size = |
| 408 | m_data.GetMaxU64(offset_ptr, addr_byte_size); |
| 409 | m_coff_header_opt.heap_commit_size = |
| 410 | m_data.GetMaxU64(offset_ptr, addr_byte_size); |
| 411 | m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); |
| 412 | uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); |
| 413 | m_coff_header_opt.data_dirs.clear(); |
| 414 | m_coff_header_opt.data_dirs.resize(num_data_dir_entries); |
| 415 | uint32_t i; |
| 416 | for (i = 0; i < num_data_dir_entries; i++) { |
| 417 | m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); |
| 418 | m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 419 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | m_file_offset = m_coff_header_opt.image_base; |
| 422 | m_image_base = m_coff_header_opt.image_base; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | // Make sure we are on track for section data which follows |
| 427 | *offset_ptr = end_offset; |
| 428 | return success; |
| 429 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 430 | |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 431 | DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { |
| 432 | if (m_file) { |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 433 | // A bit of a hack, but we intend to write to this buffer, so we can't |
| 434 | // mmap it. |
| 435 | auto buffer_sp = |
| 436 | DataBufferLLVM::CreateSliceFromPath(m_file.GetPath(), size, offset, true); |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 437 | return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()); |
| 438 | } |
| 439 | ProcessSP process_sp(m_process_wp.lock()); |
| 440 | DataExtractor data; |
| 441 | if (process_sp) { |
| 442 | auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0); |
| 443 | Error readmem_error; |
| 444 | size_t bytes_read = |
| 445 | process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(), |
| 446 | data_ap->GetByteSize(), readmem_error); |
| 447 | if (bytes_read == size) { |
| 448 | DataBufferSP buffer_sp(data_ap.release()); |
| 449 | data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); |
| 450 | } |
| 451 | } |
| 452 | return data; |
| 453 | } |
| 454 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 455 | //---------------------------------------------------------------------- |
| 456 | // ParseSectionHeaders |
| 457 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | bool ObjectFilePECOFF::ParseSectionHeaders( |
| 459 | uint32_t section_header_data_offset) { |
| 460 | const uint32_t nsects = m_coff_header.nsects; |
| 461 | m_sect_headers.clear(); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 462 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | if (nsects > 0) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 464 | const size_t section_header_byte_size = nsects * sizeof(section_header_t); |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 465 | DataExtractor section_header_data = |
| 466 | ReadImageData(section_header_data_offset, section_header_byte_size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | |
| 468 | lldb::offset_t offset = 0; |
| 469 | if (section_header_data.ValidOffsetForDataOfSize( |
| 470 | offset, section_header_byte_size)) { |
| 471 | m_sect_headers.resize(nsects); |
| 472 | |
| 473 | for (uint32_t idx = 0; idx < nsects; ++idx) { |
| 474 | const void *name_data = section_header_data.GetData(&offset, 8); |
| 475 | if (name_data) { |
| 476 | memcpy(m_sect_headers[idx].name, name_data, 8); |
| 477 | m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); |
| 478 | m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); |
| 479 | m_sect_headers[idx].size = section_header_data.GetU32(&offset); |
| 480 | m_sect_headers[idx].offset = section_header_data.GetU32(&offset); |
| 481 | m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); |
| 482 | m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); |
| 483 | m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); |
| 484 | m_sect_headers[idx].nline = section_header_data.GetU16(&offset); |
| 485 | m_sect_headers[idx].flags = section_header_data.GetU32(&offset); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 486 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 488 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return m_sect_headers.empty() == false; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | bool ObjectFilePECOFF::GetSectionName(std::string §_name, |
| 495 | const section_header_t §) { |
| 496 | if (sect.name[0] == '/') { |
| 497 | lldb::offset_t stroff = strtoul(§.name[1], NULL, 10); |
| 498 | lldb::offset_t string_file_offset = |
| 499 | m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; |
| 500 | const char *name = m_data.GetCStr(&string_file_offset); |
| 501 | if (name) { |
| 502 | sect_name = name; |
| 503 | return true; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 504 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 505 | |
| 506 | return false; |
| 507 | } |
| 508 | sect_name = sect.name; |
| 509 | return true; |
| 510 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 511 | |
| 512 | //---------------------------------------------------------------------- |
| 513 | // GetNListSymtab |
| 514 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 515 | Symtab *ObjectFilePECOFF::GetSymtab() { |
| 516 | ModuleSP module_sp(GetModule()); |
| 517 | if (module_sp) { |
| 518 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 519 | if (m_symtab_ap.get() == NULL) { |
| 520 | SectionList *sect_list = GetSectionList(); |
| 521 | m_symtab_ap.reset(new Symtab(this)); |
| 522 | std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex()); |
| 523 | |
| 524 | const uint32_t num_syms = m_coff_header.nsyms; |
| 525 | |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 526 | if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | const uint32_t symbol_size = 18; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 528 | const size_t symbol_data_size = num_syms * symbol_size; |
| 529 | // Include the 4-byte string table size at the end of the symbols |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 530 | DataExtractor symtab_data = |
| 531 | ReadImageData(m_coff_header.symoff, symbol_data_size + 4); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | lldb::offset_t offset = symbol_data_size; |
| 533 | const uint32_t strtab_size = symtab_data.GetU32(&offset); |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 534 | if (strtab_size > 0) { |
| 535 | DataExtractor strtab_data = ReadImageData( |
| 536 | m_coff_header.symoff + symbol_data_size, strtab_size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 538 | // First 4 bytes should be zeroed after strtab_size has been read, |
| 539 | // because it is used as offset 0 to encode a NULL string. |
| 540 | uint32_t *strtab_data_start = (uint32_t *)strtab_data.GetDataStart(); |
| 541 | strtab_data_start[0] = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 542 | |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 543 | offset = 0; |
| 544 | std::string symbol_name; |
| 545 | Symbol *symbols = m_symtab_ap->Resize(num_syms); |
| 546 | for (uint32_t i = 0; i < num_syms; ++i) { |
| 547 | coff_symbol_t symbol; |
| 548 | const uint32_t symbol_offset = offset; |
| 549 | const char *symbol_name_cstr = NULL; |
| 550 | // If the first 4 bytes of the symbol string are zero, then they |
| 551 | // are followed by a 4-byte string table offset. Else these |
| 552 | // 8 bytes contain the symbol name |
| 553 | if (symtab_data.GetU32(&offset) == 0) { |
| 554 | // Long string that doesn't fit into the symbol table name, |
| 555 | // so now we must read the 4 byte string table offset |
| 556 | uint32_t strtab_offset = symtab_data.GetU32(&offset); |
| 557 | symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); |
| 558 | symbol_name.assign(symbol_name_cstr); |
| 559 | } else { |
| 560 | // Short string that fits into the symbol table name which is 8 |
| 561 | // bytes |
| 562 | offset += sizeof(symbol.name) - 4; // Skip remaining |
| 563 | symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); |
| 564 | if (symbol_name_cstr == NULL) |
| 565 | break; |
| 566 | symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); |
| 567 | } |
| 568 | symbol.value = symtab_data.GetU32(&offset); |
| 569 | symbol.sect = symtab_data.GetU16(&offset); |
| 570 | symbol.type = symtab_data.GetU16(&offset); |
| 571 | symbol.storage = symtab_data.GetU8(&offset); |
| 572 | symbol.naux = symtab_data.GetU8(&offset); |
| 573 | symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); |
| 574 | if ((int16_t)symbol.sect >= 1) { |
| 575 | Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1), |
| 576 | symbol.value); |
| 577 | symbols[i].GetAddressRef() = symbol_addr; |
| 578 | symbols[i].SetType(MapSymbolType(symbol.type)); |
| 579 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 581 | if (symbol.naux > 0) { |
| 582 | i += symbol.naux; |
| 583 | offset += symbol_size; |
| 584 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // Read export header |
| 590 | if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && |
| 591 | m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && |
| 592 | m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { |
| 593 | export_directory_entry export_table; |
| 594 | uint32_t data_start = |
| 595 | m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; |
Walter Erquinigo | 344546b | 2016-10-17 20:28:19 +0000 | [diff] [blame] | 596 | |
| 597 | uint32_t address_rva = data_start; |
| 598 | if (m_file) { |
| 599 | Address address(m_coff_header_opt.image_base + data_start, sect_list); |
| 600 | address_rva = |
| 601 | address.GetSection()->GetFileOffset() + address.GetOffset(); |
| 602 | } |
| 603 | DataExtractor symtab_data = |
| 604 | ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 605 | lldb::offset_t offset = 0; |
| 606 | |
| 607 | // Read export_table header |
| 608 | export_table.characteristics = symtab_data.GetU32(&offset); |
| 609 | export_table.time_date_stamp = symtab_data.GetU32(&offset); |
| 610 | export_table.major_version = symtab_data.GetU16(&offset); |
| 611 | export_table.minor_version = symtab_data.GetU16(&offset); |
| 612 | export_table.name = symtab_data.GetU32(&offset); |
| 613 | export_table.base = symtab_data.GetU32(&offset); |
| 614 | export_table.number_of_functions = symtab_data.GetU32(&offset); |
| 615 | export_table.number_of_names = symtab_data.GetU32(&offset); |
| 616 | export_table.address_of_functions = symtab_data.GetU32(&offset); |
| 617 | export_table.address_of_names = symtab_data.GetU32(&offset); |
| 618 | export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); |
| 619 | |
| 620 | bool has_ordinal = export_table.address_of_name_ordinals != 0; |
| 621 | |
| 622 | lldb::offset_t name_offset = export_table.address_of_names - data_start; |
| 623 | lldb::offset_t name_ordinal_offset = |
| 624 | export_table.address_of_name_ordinals - data_start; |
| 625 | |
| 626 | Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names); |
| 627 | |
| 628 | std::string symbol_name; |
| 629 | |
| 630 | // Read each export table entry |
| 631 | for (size_t i = 0; i < export_table.number_of_names; ++i) { |
| 632 | uint32_t name_ordinal = |
| 633 | has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; |
| 634 | uint32_t name_address = symtab_data.GetU32(&name_offset); |
| 635 | |
| 636 | const char *symbol_name_cstr = |
| 637 | symtab_data.PeekCStr(name_address - data_start); |
| 638 | symbol_name.assign(symbol_name_cstr); |
| 639 | |
| 640 | lldb::offset_t function_offset = export_table.address_of_functions - |
| 641 | data_start + |
| 642 | sizeof(uint32_t) * name_ordinal; |
| 643 | uint32_t function_rva = symtab_data.GetU32(&function_offset); |
| 644 | |
| 645 | Address symbol_addr(m_coff_header_opt.image_base + function_rva, |
| 646 | sect_list); |
| 647 | symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); |
| 648 | symbols[i].GetAddressRef() = symbol_addr; |
| 649 | symbols[i].SetType(lldb::eSymbolTypeCode); |
| 650 | symbols[i].SetDebug(true); |
| 651 | } |
| 652 | } |
| 653 | m_symtab_ap->CalculateSymbolSizes(); |
| 654 | } |
| 655 | } |
| 656 | return m_symtab_ap.get(); |
| 657 | } |
| 658 | |
| 659 | bool ObjectFilePECOFF::IsStripped() { |
| 660 | // TODO: determine this for COFF |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { |
| 665 | if (!m_sections_ap.get()) { |
| 666 | m_sections_ap.reset(new SectionList()); |
| 667 | |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 668 | ModuleSP module_sp(GetModule()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | if (module_sp) { |
| 670 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 671 | const uint32_t nsects = m_sect_headers.size(); |
| 672 | ModuleSP module_sp(GetModule()); |
| 673 | for (uint32_t idx = 0; idx < nsects; ++idx) { |
| 674 | std::string sect_name; |
| 675 | GetSectionName(sect_name, m_sect_headers[idx]); |
| 676 | ConstString const_sect_name(sect_name.c_str()); |
| 677 | static ConstString g_code_sect_name(".code"); |
| 678 | static ConstString g_CODE_sect_name("CODE"); |
| 679 | static ConstString g_data_sect_name(".data"); |
| 680 | static ConstString g_DATA_sect_name("DATA"); |
| 681 | static ConstString g_bss_sect_name(".bss"); |
| 682 | static ConstString g_BSS_sect_name("BSS"); |
| 683 | static ConstString g_debug_sect_name(".debug"); |
| 684 | static ConstString g_reloc_sect_name(".reloc"); |
| 685 | static ConstString g_stab_sect_name(".stab"); |
| 686 | static ConstString g_stabstr_sect_name(".stabstr"); |
| 687 | static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); |
| 688 | static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); |
| 689 | static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); |
| 690 | static ConstString g_sect_name_dwarf_debug_info(".debug_info"); |
| 691 | static ConstString g_sect_name_dwarf_debug_line(".debug_line"); |
| 692 | static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); |
| 693 | static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); |
| 694 | static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); |
| 695 | static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); |
| 696 | static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); |
| 697 | static ConstString g_sect_name_dwarf_debug_str(".debug_str"); |
| 698 | static ConstString g_sect_name_eh_frame(".eh_frame"); |
| 699 | static ConstString g_sect_name_go_symtab(".gosymtab"); |
| 700 | SectionType section_type = eSectionTypeOther; |
| 701 | if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE && |
| 702 | ((const_sect_name == g_code_sect_name) || |
| 703 | (const_sect_name == g_CODE_sect_name))) { |
| 704 | section_type = eSectionTypeCode; |
| 705 | } else if (m_sect_headers[idx].flags & |
| 706 | llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && |
| 707 | ((const_sect_name == g_data_sect_name) || |
| 708 | (const_sect_name == g_DATA_sect_name))) { |
| 709 | section_type = eSectionTypeData; |
| 710 | } else if (m_sect_headers[idx].flags & |
| 711 | llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && |
| 712 | ((const_sect_name == g_bss_sect_name) || |
| 713 | (const_sect_name == g_BSS_sect_name))) { |
| 714 | if (m_sect_headers[idx].size == 0) |
| 715 | section_type = eSectionTypeZeroFill; |
| 716 | else |
| 717 | section_type = eSectionTypeData; |
| 718 | } else if (const_sect_name == g_debug_sect_name) { |
| 719 | section_type = eSectionTypeDebug; |
| 720 | } else if (const_sect_name == g_stabstr_sect_name) { |
| 721 | section_type = eSectionTypeDataCString; |
| 722 | } else if (const_sect_name == g_reloc_sect_name) { |
| 723 | section_type = eSectionTypeOther; |
| 724 | } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) |
| 725 | section_type = eSectionTypeDWARFDebugAbbrev; |
| 726 | else if (const_sect_name == g_sect_name_dwarf_debug_aranges) |
| 727 | section_type = eSectionTypeDWARFDebugAranges; |
| 728 | else if (const_sect_name == g_sect_name_dwarf_debug_frame) |
| 729 | section_type = eSectionTypeDWARFDebugFrame; |
| 730 | else if (const_sect_name == g_sect_name_dwarf_debug_info) |
| 731 | section_type = eSectionTypeDWARFDebugInfo; |
| 732 | else if (const_sect_name == g_sect_name_dwarf_debug_line) |
| 733 | section_type = eSectionTypeDWARFDebugLine; |
| 734 | else if (const_sect_name == g_sect_name_dwarf_debug_loc) |
| 735 | section_type = eSectionTypeDWARFDebugLoc; |
| 736 | else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) |
| 737 | section_type = eSectionTypeDWARFDebugMacInfo; |
| 738 | else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) |
| 739 | section_type = eSectionTypeDWARFDebugPubNames; |
| 740 | else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) |
| 741 | section_type = eSectionTypeDWARFDebugPubTypes; |
| 742 | else if (const_sect_name == g_sect_name_dwarf_debug_ranges) |
| 743 | section_type = eSectionTypeDWARFDebugRanges; |
| 744 | else if (const_sect_name == g_sect_name_dwarf_debug_str) |
| 745 | section_type = eSectionTypeDWARFDebugStr; |
| 746 | else if (const_sect_name == g_sect_name_eh_frame) |
| 747 | section_type = eSectionTypeEHFrame; |
| 748 | else if (const_sect_name == g_sect_name_go_symtab) |
| 749 | section_type = eSectionTypeGoSymtab; |
| 750 | else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) { |
| 751 | section_type = eSectionTypeCode; |
| 752 | } else if (m_sect_headers[idx].flags & |
| 753 | llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) { |
| 754 | section_type = eSectionTypeData; |
| 755 | } else if (m_sect_headers[idx].flags & |
| 756 | llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { |
| 757 | if (m_sect_headers[idx].size == 0) |
| 758 | section_type = eSectionTypeZeroFill; |
| 759 | else |
| 760 | section_type = eSectionTypeData; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 761 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 762 | |
| 763 | // Use a segment ID of the segment index shifted left by 8 so they |
| 764 | // never conflict with any of the sections. |
| 765 | SectionSP section_sp(new Section( |
| 766 | module_sp, // Module to which this section belongs |
| 767 | this, // Object file to which this section belongs |
| 768 | idx + 1, // Section ID is the 1 based segment index shifted right by |
| 769 | // 8 bits as not to collide with any of the 256 section IDs |
| 770 | // that are possible |
| 771 | const_sect_name, // Name of this section |
| 772 | section_type, // This section is a container of other sections. |
| 773 | m_coff_header_opt.image_base + |
| 774 | m_sect_headers[idx].vmaddr, // File VM address == addresses as |
| 775 | // they are found in the object file |
| 776 | m_sect_headers[idx].vmsize, // VM size in bytes of this section |
| 777 | m_sect_headers[idx] |
| 778 | .offset, // Offset to the data for this section in the file |
| 779 | m_sect_headers[idx] |
| 780 | .size, // Size in bytes of this section as found in the file |
| 781 | m_coff_header_opt.sect_alignment, // Section alignment |
| 782 | m_sect_headers[idx].flags)); // Flags for this section |
| 783 | |
| 784 | // section_sp->SetIsEncrypted (segment_is_encrypted); |
| 785 | |
| 786 | unified_section_list.AddSection(section_sp); |
| 787 | m_sections_ap->AddSection(section_sp); |
| 788 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 789 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 790 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 793 | bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; } |
| 794 | |
| 795 | uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { |
| 796 | return 0; |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 799 | lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { |
| 800 | if (m_entry_point_address.IsValid()) |
Stephane Sezer | 8e38c66 | 2016-03-23 18:00:13 +0000 | [diff] [blame] | 801 | return m_entry_point_address; |
Stephane Sezer | 8e38c66 | 2016-03-23 18:00:13 +0000 | [diff] [blame] | 802 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 803 | if (!ParseHeader() || !IsExecutable()) |
| 804 | return m_entry_point_address; |
| 805 | |
| 806 | SectionList *section_list = GetSectionList(); |
| 807 | addr_t offset = m_coff_header_opt.entry; |
| 808 | |
| 809 | if (!section_list) |
| 810 | m_entry_point_address.SetOffset(offset); |
| 811 | else |
| 812 | m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); |
| 813 | return m_entry_point_address; |
| 814 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 815 | |
| 816 | //---------------------------------------------------------------------- |
| 817 | // Dump |
| 818 | // |
| 819 | // Dump the specifics of the runtime file container (such as any headers |
| 820 | // segments, sections, etc). |
| 821 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | void ObjectFilePECOFF::Dump(Stream *s) { |
| 823 | ModuleSP module_sp(GetModule()); |
| 824 | if (module_sp) { |
| 825 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 826 | s->Printf("%p: ", static_cast<void *>(this)); |
| 827 | s->Indent(); |
| 828 | s->PutCString("ObjectFilePECOFF"); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 829 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 830 | ArchSpec header_arch; |
| 831 | GetArchitecture(header_arch); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | *s << ", file = '" << m_file |
| 834 | << "', arch = " << header_arch.GetArchitectureName() << "\n"; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 835 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 836 | SectionList *sections = GetSectionList(); |
| 837 | if (sections) |
| 838 | sections->Dump(s, NULL, true, UINT32_MAX); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 839 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 840 | if (m_symtab_ap.get()) |
| 841 | m_symtab_ap->Dump(s, NULL, eSortOrderNone); |
Greg Clayton | a174349 | 2012-03-13 23:14:29 +0000 | [diff] [blame] | 842 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 843 | if (m_dos_header.e_magic) |
| 844 | DumpDOSHeader(s, m_dos_header); |
| 845 | if (m_coff_header.machine) { |
| 846 | DumpCOFFHeader(s, m_coff_header); |
| 847 | if (m_coff_header.hdrsize) |
| 848 | DumpOptCOFFHeader(s, m_coff_header_opt); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 849 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 850 | s->EOL(); |
| 851 | DumpSectionHeaders(s); |
| 852 | s->EOL(); |
| 853 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | //---------------------------------------------------------------------- |
| 857 | // DumpDOSHeader |
| 858 | // |
| 859 | // Dump the MS-DOS header to the specified output stream |
| 860 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 861 | void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { |
| 862 | s->PutCString("MSDOS Header\n"); |
| 863 | s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); |
| 864 | s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); |
| 865 | s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); |
| 866 | s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); |
| 867 | s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); |
| 868 | s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); |
| 869 | s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); |
| 870 | s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); |
| 871 | s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); |
| 872 | s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); |
| 873 | s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); |
| 874 | s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); |
| 875 | s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); |
| 876 | s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); |
| 877 | s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", |
| 878 | header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); |
| 879 | s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); |
| 880 | s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); |
| 881 | s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " |
| 882 | "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", |
| 883 | header.e_res2[0], header.e_res2[1], header.e_res2[2], |
| 884 | header.e_res2[3], header.e_res2[4], header.e_res2[5], |
| 885 | header.e_res2[6], header.e_res2[7], header.e_res2[8], |
| 886 | header.e_res2[9]); |
| 887 | s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | //---------------------------------------------------------------------- |
| 891 | // DumpCOFFHeader |
| 892 | // |
| 893 | // Dump the COFF header to the specified output stream |
| 894 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { |
| 896 | s->PutCString("COFF Header\n"); |
| 897 | s->Printf(" machine = 0x%4.4x\n", header.machine); |
| 898 | s->Printf(" nsects = 0x%4.4x\n", header.nsects); |
| 899 | s->Printf(" modtime = 0x%8.8x\n", header.modtime); |
| 900 | s->Printf(" symoff = 0x%8.8x\n", header.symoff); |
| 901 | s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); |
| 902 | s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | //---------------------------------------------------------------------- |
| 906 | // DumpOptCOFFHeader |
| 907 | // |
| 908 | // Dump the optional COFF header to the specified output stream |
| 909 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 910 | void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, |
| 911 | const coff_opt_header_t &header) { |
| 912 | s->PutCString("Optional COFF Header\n"); |
| 913 | s->Printf(" magic = 0x%4.4x\n", header.magic); |
| 914 | s->Printf(" major_linker_version = 0x%2.2x\n", |
| 915 | header.major_linker_version); |
| 916 | s->Printf(" minor_linker_version = 0x%2.2x\n", |
| 917 | header.minor_linker_version); |
| 918 | s->Printf(" code_size = 0x%8.8x\n", header.code_size); |
| 919 | s->Printf(" data_size = 0x%8.8x\n", header.data_size); |
| 920 | s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); |
| 921 | s->Printf(" entry = 0x%8.8x\n", header.entry); |
| 922 | s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); |
| 923 | s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); |
| 924 | s->Printf(" image_base = 0x%16.16" PRIx64 "\n", |
| 925 | header.image_base); |
| 926 | s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); |
| 927 | s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); |
| 928 | s->Printf(" major_os_system_version = 0x%4.4x\n", |
| 929 | header.major_os_system_version); |
| 930 | s->Printf(" minor_os_system_version = 0x%4.4x\n", |
| 931 | header.minor_os_system_version); |
| 932 | s->Printf(" major_image_version = 0x%4.4x\n", |
| 933 | header.major_image_version); |
| 934 | s->Printf(" minor_image_version = 0x%4.4x\n", |
| 935 | header.minor_image_version); |
| 936 | s->Printf(" major_subsystem_version = 0x%4.4x\n", |
| 937 | header.major_subsystem_version); |
| 938 | s->Printf(" minor_subsystem_version = 0x%4.4x\n", |
| 939 | header.minor_subsystem_version); |
| 940 | s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); |
| 941 | s->Printf(" image_size = 0x%8.8x\n", header.image_size); |
| 942 | s->Printf(" header_size = 0x%8.8x\n", header.header_size); |
| 943 | s->Printf(" checksum = 0x%8.8x\n", header.checksum); |
| 944 | s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); |
| 945 | s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); |
| 946 | s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", |
| 947 | header.stack_reserve_size); |
| 948 | s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", |
| 949 | header.stack_commit_size); |
| 950 | s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", |
| 951 | header.heap_reserve_size); |
| 952 | s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", |
| 953 | header.heap_commit_size); |
| 954 | s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); |
| 955 | s->Printf(" num_data_dir_entries = 0x%8.8x\n", |
| 956 | (uint32_t)header.data_dirs.size()); |
| 957 | uint32_t i; |
| 958 | for (i = 0; i < header.data_dirs.size(); i++) { |
| 959 | s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, |
| 960 | header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); |
| 961 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 962 | } |
| 963 | //---------------------------------------------------------------------- |
| 964 | // DumpSectionHeader |
| 965 | // |
| 966 | // Dump a single ELF section header to the specified output stream |
| 967 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 968 | void ObjectFilePECOFF::DumpSectionHeader(Stream *s, |
| 969 | const section_header_t &sh) { |
| 970 | std::string name; |
| 971 | GetSectionName(name, sh); |
| 972 | s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x " |
| 973 | "0x%4.4x 0x%8.8x\n", |
| 974 | name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, |
| 975 | sh.lineoff, sh.nreloc, sh.nline, sh.flags); |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 978 | //---------------------------------------------------------------------- |
| 979 | // DumpSectionHeaders |
| 980 | // |
| 981 | // Dump all of the ELF section header to the specified output stream |
| 982 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 983 | void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { |
| 984 | |
| 985 | s->PutCString("Section Headers\n"); |
| 986 | s->PutCString("IDX name vm addr vm size file off file " |
| 987 | "size reloc off line off nreloc nline flags\n"); |
| 988 | s->PutCString("==== ---------------- ---------- ---------- ---------- " |
| 989 | "---------- ---------- ---------- ------ ------ ----------\n"); |
| 990 | |
| 991 | uint32_t idx = 0; |
| 992 | SectionHeaderCollIter pos, end = m_sect_headers.end(); |
| 993 | |
| 994 | for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { |
| 995 | s->Printf("[%2u] ", idx); |
| 996 | ObjectFilePECOFF::DumpSectionHeader(s, *pos); |
| 997 | } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Zachary Turner | fb3b3bd | 2016-09-20 20:44:50 +0000 | [diff] [blame] | 1000 | bool ObjectFilePECOFF::IsWindowsSubsystem() { |
| 1001 | switch (m_coff_header_opt.subsystem) { |
| 1002 | case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: |
| 1003 | case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: |
| 1004 | case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: |
| 1005 | case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: |
| 1006 | case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: |
| 1007 | case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: |
| 1008 | case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: |
| 1009 | return true; |
| 1010 | default: |
| 1011 | return false; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1015 | bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) { |
| 1016 | uint16_t machine = m_coff_header.machine; |
| 1017 | switch (machine) { |
| 1018 | case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: |
| 1019 | case llvm::COFF::IMAGE_FILE_MACHINE_I386: |
| 1020 | case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: |
| 1021 | case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: |
| 1022 | case llvm::COFF::IMAGE_FILE_MACHINE_ARM: |
| 1023 | case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: |
| 1024 | case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: |
Zachary Turner | fb3b3bd | 2016-09-20 20:44:50 +0000 | [diff] [blame] | 1025 | arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, |
| 1026 | IsWindowsSubsystem() ? llvm::Triple::Win32 |
| 1027 | : llvm::Triple::UnknownOS); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1028 | return true; |
| 1029 | default: |
| 1030 | break; |
| 1031 | } |
| 1032 | return false; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1035 | ObjectFile::Type ObjectFilePECOFF::CalculateType() { |
| 1036 | if (m_coff_header.machine != 0) { |
| 1037 | if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) |
| 1038 | return eTypeExecutable; |
| 1039 | else |
| 1040 | return eTypeSharedLibrary; |
| 1041 | } |
| 1042 | return eTypeExecutable; |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1045 | ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 1046 | //------------------------------------------------------------------ |
| 1047 | // PluginInterface protocol |
| 1048 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1049 | ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); } |
Greg Clayton | f754f88 | 2011-09-09 20:33:05 +0000 | [diff] [blame] | 1050 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1051 | uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; } |