LLDB mainline
ObjectFileCOFF.cpp
Go to the documentation of this file.
1//===-- ObjectFileCOFF.cpp ------------------------------------------------===//
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 "ObjectFileCOFF.h"
10
11#include "lldb/Core/Module.h"
16
17#include "llvm/Support/Error.h"
18#include "llvm/Support/FormatAdapters.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
23using namespace llvm;
24using namespace llvm::object;
25
26static bool IsCOFFObjectFile(const llvm::ArrayRef<uint8_t> data) {
27 return identify_magic(toStringRef(data)) == file_magic::coff_object;
28}
29
31
33
35
41
45
48 DataExtractorSP extractor_sp,
49 offset_t data_offset, const FileSpec *file,
50 offset_t file_offset, offset_t length) {
52
53 if (!extractor_sp || !extractor_sp->HasData()) {
54 DataBufferSP data_sp = MapFileData(*file, length, file_offset);
55 if (!data_sp) {
56 LLDB_LOG(log,
57 "Failed to create ObjectFileCOFF instance: cannot read file {0}",
58 file->GetPath());
59 return nullptr;
60 }
61 extractor_sp = std::make_shared<lldb_private::DataExtractor>(data_sp);
62 data_offset = 0;
63 }
64
65 assert(extractor_sp && extractor_sp->HasData() &&
66 "must have mapped file at this point");
67
68 // If this is operating on a VirtualDataExtractor, it can have
69 // gaps between valid bytes in the DataBuffer. We extract an
70 // ArrayRef of the raw bytes, and can segfault.
71 DataExtractorSP contiguous_extractor_sp =
72 extractor_sp->GetContiguousDataExtractorSP();
73 if (!IsCOFFObjectFile(contiguous_extractor_sp->GetData()))
74 return nullptr;
75
76 if (contiguous_extractor_sp->GetByteSize() < length) {
77 DataBufferSP data_sp = MapFileData(*file, length, file_offset);
78 if (!data_sp) {
79 LLDB_LOG(log,
80 "Failed to create ObjectFileCOFF instance: cannot read file {0}",
81 file->GetPath());
82 return nullptr;
83 }
84 contiguous_extractor_sp =
85 std::make_shared<lldb_private::DataExtractor>(data_sp);
86 data_offset = 0;
87 }
88
89 MemoryBufferRef buffer{toStringRef(contiguous_extractor_sp->GetData()),
90 file->GetFilename().GetStringRef()};
91
92 Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
93 if (!binary) {
94 LLDB_LOG_ERROR(log, binary.takeError(),
95 "Failed to create binary for file ({1}): {0}",
96 file->GetPath());
97 return nullptr;
98 }
99
100 LLDB_LOG(log, "ObjectFileCOFF::ObjectFileCOFF module = {0} ({1}), file = {2}",
101 module_sp.get(), module_sp->GetSpecificationDescription(),
102 file->GetPath());
103
104 return new ObjectFileCOFF(unique_dyn_cast<COFFObjectFile>(std::move(*binary)),
105 module_sp, contiguous_extractor_sp, data_offset,
106 file, file_offset, length);
107}
108
110 const ModuleSP &module_sp, WritableDataBufferSP data_sp,
111 const ProcessSP &process_sp, addr_t header) {
112 // FIXME: do we need to worry about construction from a memory region?
113 return nullptr;
114}
115
118 DataExtractorSP &extractor_sp,
119 offset_t file_offset, offset_t length) {
120 if (!extractor_sp || !extractor_sp->HasData())
121 return {};
122
123 // If this is opearting on a VirtualDataExtractor, it can have
124 // gaps between valid bytes in the DataBuffer. We extract an
125 // ArrayRef of the raw bytes, and can segfault.
126 DataExtractorSP contiguous_extractor_sp =
127 extractor_sp->GetContiguousDataExtractorSP();
128 if (!contiguous_extractor_sp)
129 return {};
130 if (!IsCOFFObjectFile(contiguous_extractor_sp->GetData()))
131 return {};
132
133 MemoryBufferRef buffer{toStringRef(contiguous_extractor_sp->GetData()),
134 file.GetFilename().GetStringRef()};
135 Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
136 if (!binary) {
137 Log *log = GetLog(LLDBLog::Object);
138 LLDB_LOG_ERROR(log, binary.takeError(),
139 "Failed to create binary for file ({1}): {0}",
140 file.GetFilename());
141 return {};
142 }
143
144 std::unique_ptr<COFFObjectFile> object =
145 unique_dyn_cast<COFFObjectFile>(std::move(*binary));
146 ModuleSpecList specs;
147 switch (static_cast<COFF::MachineTypes>(object->getMachine())) {
148 specs.Append(ModuleSpec(file, ArchSpec("i686-unknown-windows-msvc")));
149 return specs;
150 case COFF::IMAGE_FILE_MACHINE_AMD64:
151 specs.Append(ModuleSpec(file, ArchSpec("x86_64-unknown-windows-msvc")));
152 return specs;
153 case COFF::IMAGE_FILE_MACHINE_ARMNT:
154 specs.Append(ModuleSpec(file, ArchSpec("armv7-unknown-windows-msvc")));
155 return specs;
156 case COFF::IMAGE_FILE_MACHINE_ARM64:
157 specs.Append(ModuleSpec(file, ArchSpec("aarch64-unknown-windows-msvc")));
158 return specs;
159 default:
160 break;
161 }
162 return {};
163}
164
166 ModuleSP module(GetModule());
167 if (!module)
168 return;
169
170 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
171
172 stream->Printf("%p: ", static_cast<void *>(this));
173 stream->Indent();
174 stream->PutCString("ObjectFileCOFF");
175 *stream << ", file = '" << m_file
176 << "', arch = " << GetArchitecture().GetArchitectureName() << '\n';
177
178 if (SectionList *sections = GetSectionList())
179 sections->Dump(stream->AsRawOstream(), stream->GetIndentLevel(), nullptr,
180 true, std::numeric_limits<uint32_t>::max());
181}
182
184 return const_cast<ObjectFileCOFF *>(this)->GetArchitecture().GetAddressByteSize();
185}
186
188 switch (static_cast<COFF::MachineTypes>(m_object->getMachine())) {
189 case COFF::IMAGE_FILE_MACHINE_I386:
190 return ArchSpec("i686-unknown-windows-msvc");
191 case COFF::IMAGE_FILE_MACHINE_AMD64:
192 return ArchSpec("x86_64-unknown-windows-msvc");
193 case COFF::IMAGE_FILE_MACHINE_ARMNT:
194 return ArchSpec("armv7-unknown-windows-msvc");
195 case COFF::IMAGE_FILE_MACHINE_ARM64:
196 return ArchSpec("aarch64-unknown-windows-msvc");
197 default:
198 return ArchSpec();
199 }
200}
201
203 if (m_sections_up)
204 return;
205
206 m_sections_up = std::make_unique<SectionList>();
207 ModuleSP module(GetModule());
208 if (!module)
209 return;
210
211 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
212
213 auto SectionType = [](StringRef Name,
214 const coff_section *Section) -> lldb::SectionType {
215 // DWARF Debug Sections
216 if (Name.consume_front(".debug_"))
217 return GetDWARFSectionTypeFromName(Name);
218
219 lldb::SectionType type = StringSwitch<lldb::SectionType>(Name)
220 // CodeView Debug Sections: .debug$S, .debug$T
221 .StartsWith(".debug$", eSectionTypeDebug)
222 .Case("clangast", eSectionTypeOther)
223 .Default(eSectionTypeInvalid);
224 if (type != eSectionTypeInvalid)
225 return type;
226
227 if (Section->Characteristics & COFF::IMAGE_SCN_CNT_CODE)
228 return eSectionTypeCode;
229 if (Section->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
230 return eSectionTypeData;
231 if (Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
232 return Section->SizeOfRawData ? eSectionTypeData : eSectionTypeZeroFill;
233 return eSectionTypeOther;
234 };
235 auto Permissions = [](const object::coff_section *Section) -> uint32_t {
236 uint32_t permissions = 0;
237 if (Section->Characteristics & COFF::IMAGE_SCN_MEM_EXECUTE)
238 permissions |= lldb::ePermissionsExecutable;
239 if (Section->Characteristics & COFF::IMAGE_SCN_MEM_READ)
240 permissions |= lldb::ePermissionsReadable;
241 if (Section->Characteristics & COFF::IMAGE_SCN_MEM_WRITE)
242 permissions |= lldb::ePermissionsWritable;
243 return permissions;
244 };
245
246 for (const auto &SecRef : m_object->sections()) {
247 const auto COFFSection = m_object->getCOFFSection(SecRef);
248
249 llvm::Expected<StringRef> Name = SecRef.getName();
250 StringRef SectionName = Name ? *Name : COFFSection->Name;
251 if (!Name)
252 consumeError(Name.takeError());
253
254 SectionSP section =
255 std::make_unique<Section>(module, this,
256 static_cast<user_id_t>(SecRef.getIndex()),
257 ConstString(SectionName),
258 SectionType(SectionName, COFFSection),
259 COFFSection->VirtualAddress,
260 COFFSection->VirtualSize,
261 COFFSection->PointerToRawData,
262 COFFSection->SizeOfRawData,
263 COFFSection->getAlignment(),
264 0);
265 section->SetPermissions(Permissions(COFFSection));
266
267 m_sections_up->AddSection(section);
268 sections.AddSection(section);
269 }
270}
271
273 Log *log = GetLog(LLDBLog::Object);
274
275 SectionList *sections = GetSectionList();
276 symtab.Reserve(symtab.GetNumSymbols() + m_object->getNumberOfSymbols());
277
278 auto SymbolType = [](const COFFSymbolRef &Symbol) -> lldb::SymbolType {
279 if (Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
280 return eSymbolTypeCode;
281 if (Symbol.getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
282 Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_NULL)
283 return eSymbolTypeData;
284 return eSymbolTypeInvalid;
285 };
286
287 for (const auto &SymRef : m_object->symbols()) {
288 const auto COFFSymRef = m_object->getCOFFSymbol(SymRef);
289
290 Expected<StringRef> NameOrErr = SymRef.getName();
291 if (!NameOrErr) {
292 LLDB_LOG_ERROR(log, NameOrErr.takeError(),
293 "ObjectFileCOFF: failed to get symbol name: {0}");
294 continue;
295 }
296
297 Symbol symbol;
298 symbol.GetMangled().SetValue(ConstString(*NameOrErr));
299
300 int16_t SecIdx = static_cast<int16_t>(COFFSymRef.getSectionNumber());
301 if (SecIdx == COFF::IMAGE_SYM_ABSOLUTE) {
302 symbol.GetAddressRef() = Address{COFFSymRef.getValue()};
304 } else if (SecIdx >= 1) {
305 symbol.GetAddressRef() = Address(sections->GetSectionAtIndex(SecIdx - 1),
306 COFFSymRef.getValue());
307 symbol.SetType(SymbolType(COFFSymRef));
308 }
309
310 symtab.AddSymbol(symbol);
311 }
312
313 LLDB_LOG(log, "ObjectFileCOFF::ParseSymtab processed {0} symbols",
314 m_object->getNumberOfSymbols());
315}
316
318 ModuleSP module(GetModule());
319 if (!module)
320 return false;
321
322 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
323
324 m_data_nsp->SetByteOrder(eByteOrderLittle);
325 m_data_nsp->SetAddressByteSize(GetAddressByteSize());
326
327 return true;
328}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:399
static bool IsCOFFObjectFile(const llvm::ArrayRef< uint8_t > data)
#define LLDB_PLUGIN_DEFINE(PluginName)
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
void ParseSymtab(lldb_private::Symtab &) override
Parse the symbol table into the provides symbol table object.
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header)
static void Initialize()
void Dump(lldb_private::Stream *stream) override
Dump a description of this object to a Stream.
void CreateSections(lldb_private::SectionList &) override
static char ID
static llvm::StringRef GetPluginDescriptionStatic()
lldb_private::ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
~ObjectFileCOFF() override
bool ParseHeader() override
Attempts to parse the object header.
static void Terminate()
std::unique_ptr< llvm::object::COFFObjectFile > m_object
static lldb_private::ModuleSpecList GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp, lldb::offset_t file_offset, lldb::offset_t length)
ObjectFileCOFF(std::unique_ptr< llvm::object::COFFObjectFile > object, const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
static lldb_private::ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
static llvm::StringRef GetPluginNameStatic()
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:32
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition ArchSpec.cpp:681
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition ArchSpec.cpp:548
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
A file utility class.
Definition FileSpec.h:57
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:250
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
void SetValue(ConstString name)
Set the string value in this object.
Definition Mangled.cpp:124
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
void Append(const ModuleSpec &spec)
Definition ModuleSpec.h:341
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h:774
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset)
static lldb::SectionType GetDWARFSectionTypeFromName(llvm::StringRef name)
Parses the section type from a section name for DWARF sections.
DataExtractorNSP m_data_nsp
The data for this object file so things can be parsed lazily.
Definition ObjectFile.h:767
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
size_t AddSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:483
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:552
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:418
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
unsigned GetIndentLevel() const
Get the current indentation level.
Definition Stream.cpp:193
void SetType(lldb::SymbolType type)
Definition Symbol.h:171
Mangled & GetMangled()
Definition Symbol.h:147
Address & GetAddressRef()
Definition Symbol.h:73
uint32_t AddSymbol(const Symbol &symbol)
Definition Symtab.cpp:64
size_t GetNumSymbols() const
Definition Symtab.cpp:77
void Reserve(size_t count)
Definition Symtab.cpp:51
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
SymbolType
Symbol types.
@ eSymbolTypeAbsolute
uint64_t user_id_t
Definition lldb-types.h:82
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
@ eSectionTypeInvalid
@ eSectionTypeZeroFill
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP