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 = {1} ({2}), file = {3}",
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
117 const FileSpec &file, DataExtractorSP &extractor_sp, offset_t data_offset,
118 offset_t file_offset, offset_t length, ModuleSpecList &specs) {
119 if (!extractor_sp || !extractor_sp->HasData())
120 return 0;
121
122 // If this is opearting on a VirtualDataExtractor, it can have
123 // gaps between valid bytes in the DataBuffer. We extract an
124 // ArrayRef of the raw bytes, and can segfault.
125 DataExtractorSP contiguous_extractor_sp =
126 extractor_sp->GetContiguousDataExtractorSP();
127 if (!contiguous_extractor_sp)
128 return 0;
129 if (!IsCOFFObjectFile(contiguous_extractor_sp->GetData()))
130 return 0;
131
132 MemoryBufferRef buffer{toStringRef(contiguous_extractor_sp->GetData()),
133 file.GetFilename().GetStringRef()};
134 Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
135 if (!binary) {
136 Log *log = GetLog(LLDBLog::Object);
137 LLDB_LOG_ERROR(log, binary.takeError(),
138 "Failed to create binary for file ({1}): {0}",
139 file.GetFilename());
140 return 0;
141 }
142
143 std::unique_ptr<COFFObjectFile> object =
144 unique_dyn_cast<COFFObjectFile>(std::move(*binary));
145 switch (static_cast<COFF::MachineTypes>(object->getMachine())) {
146 case COFF::IMAGE_FILE_MACHINE_I386:
147 specs.Append(ModuleSpec(file, ArchSpec("i686-unknown-windows-msvc")));
148 return 1;
149 case COFF::IMAGE_FILE_MACHINE_AMD64:
150 specs.Append(ModuleSpec(file, ArchSpec("x86_64-unknown-windows-msvc")));
151 return 1;
152 case COFF::IMAGE_FILE_MACHINE_ARMNT:
153 specs.Append(ModuleSpec(file, ArchSpec("armv7-unknown-windows-msvc")));
154 return 1;
155 case COFF::IMAGE_FILE_MACHINE_ARM64:
156 specs.Append(ModuleSpec(file, ArchSpec("aarch64-unknown-windows-msvc")));
157 return 1;
158 default:
159 return 0;
160 }
161}
162
164 ModuleSP module(GetModule());
165 if (!module)
166 return;
167
168 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
169
170 stream->Printf("%p: ", static_cast<void *>(this));
171 stream->Indent();
172 stream->PutCString("ObjectFileCOFF");
173 *stream << ", file = '" << m_file
174 << "', arch = " << GetArchitecture().GetArchitectureName() << '\n';
175
176 if (SectionList *sections = GetSectionList())
177 sections->Dump(stream->AsRawOstream(), stream->GetIndentLevel(), nullptr,
178 true, std::numeric_limits<uint32_t>::max());
179}
180
182 return const_cast<ObjectFileCOFF *>(this)->GetArchitecture().GetAddressByteSize();
183}
184
186 switch (static_cast<COFF::MachineTypes>(m_object->getMachine())) {
187 case COFF::IMAGE_FILE_MACHINE_I386:
188 return ArchSpec("i686-unknown-windows-msvc");
189 case COFF::IMAGE_FILE_MACHINE_AMD64:
190 return ArchSpec("x86_64-unknown-windows-msvc");
191 case COFF::IMAGE_FILE_MACHINE_ARMNT:
192 return ArchSpec("armv7-unknown-windows-msvc");
193 case COFF::IMAGE_FILE_MACHINE_ARM64:
194 return ArchSpec("aarch64-unknown-windows-msvc");
195 default:
196 return ArchSpec();
197 }
198}
199
201 if (m_sections_up)
202 return;
203
204 m_sections_up = std::make_unique<SectionList>();
205 ModuleSP module(GetModule());
206 if (!module)
207 return;
208
209 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
210
211 auto SectionType = [](StringRef Name,
212 const coff_section *Section) -> lldb::SectionType {
213 // DWARF Debug Sections
214 if (Name.consume_front(".debug_"))
215 return GetDWARFSectionTypeFromName(Name);
216
217 lldb::SectionType type = StringSwitch<lldb::SectionType>(Name)
218 // CodeView Debug Sections: .debug$S, .debug$T
219 .StartsWith(".debug$", eSectionTypeDebug)
220 .Case("clangast", eSectionTypeOther)
221 .Default(eSectionTypeInvalid);
222 if (type != eSectionTypeInvalid)
223 return type;
224
225 if (Section->Characteristics & COFF::IMAGE_SCN_CNT_CODE)
226 return eSectionTypeCode;
227 if (Section->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
228 return eSectionTypeData;
229 if (Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
230 return Section->SizeOfRawData ? eSectionTypeData : eSectionTypeZeroFill;
231 return eSectionTypeOther;
232 };
233 auto Permissions = [](const object::coff_section *Section) -> uint32_t {
234 uint32_t permissions = 0;
235 if (Section->Characteristics & COFF::IMAGE_SCN_MEM_EXECUTE)
236 permissions |= lldb::ePermissionsExecutable;
237 if (Section->Characteristics & COFF::IMAGE_SCN_MEM_READ)
238 permissions |= lldb::ePermissionsReadable;
239 if (Section->Characteristics & COFF::IMAGE_SCN_MEM_WRITE)
240 permissions |= lldb::ePermissionsWritable;
241 return permissions;
242 };
243
244 for (const auto &SecRef : m_object->sections()) {
245 const auto COFFSection = m_object->getCOFFSection(SecRef);
246
247 llvm::Expected<StringRef> Name = SecRef.getName();
248 StringRef SectionName = Name ? *Name : COFFSection->Name;
249 if (!Name)
250 consumeError(Name.takeError());
251
252 SectionSP section =
253 std::make_unique<Section>(module, this,
254 static_cast<user_id_t>(SecRef.getIndex()),
255 ConstString(SectionName),
256 SectionType(SectionName, COFFSection),
257 COFFSection->VirtualAddress,
258 COFFSection->VirtualSize,
259 COFFSection->PointerToRawData,
260 COFFSection->SizeOfRawData,
261 COFFSection->getAlignment(),
262 0);
263 section->SetPermissions(Permissions(COFFSection));
264
265 m_sections_up->AddSection(section);
266 sections.AddSection(section);
267 }
268}
269
271 Log *log = GetLog(LLDBLog::Object);
272
273 SectionList *sections = GetSectionList();
274 symtab.Reserve(symtab.GetNumSymbols() + m_object->getNumberOfSymbols());
275
276 auto SymbolType = [](const COFFSymbolRef &Symbol) -> lldb::SymbolType {
277 if (Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
278 return eSymbolTypeCode;
279 if (Symbol.getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
280 Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_NULL)
281 return eSymbolTypeData;
282 return eSymbolTypeInvalid;
283 };
284
285 for (const auto &SymRef : m_object->symbols()) {
286 const auto COFFSymRef = m_object->getCOFFSymbol(SymRef);
287
288 Expected<StringRef> NameOrErr = SymRef.getName();
289 if (!NameOrErr) {
290 LLDB_LOG_ERROR(log, NameOrErr.takeError(),
291 "ObjectFileCOFF: failed to get symbol name: {0}");
292 continue;
293 }
294
295 Symbol symbol;
296 symbol.GetMangled().SetValue(ConstString(*NameOrErr));
297
298 int16_t SecIdx = static_cast<int16_t>(COFFSymRef.getSectionNumber());
299 if (SecIdx == COFF::IMAGE_SYM_ABSOLUTE) {
300 symbol.GetAddressRef() = Address{COFFSymRef.getValue()};
302 } else if (SecIdx >= 1) {
303 symbol.GetAddressRef() = Address(sections->GetSectionAtIndex(SecIdx - 1),
304 COFFSymRef.getValue());
305 symbol.SetType(SymbolType(COFFSymRef));
306 }
307
308 symtab.AddSymbol(symbol);
309 }
310
311 LLDB_LOG(log, "ObjectFileCOFF::ParseSymtab processed {0} symbols",
312 m_object->getNumberOfSymbols());
313}
314
316 ModuleSP module(GetModule());
317 if (!module)
318 return false;
319
320 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
321
322 m_data_nsp->SetByteOrder(eByteOrderLittle);
323 m_data_nsp->SetAddressByteSize(GetAddressByteSize());
324
325 return true;
326}
#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:392
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 size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
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
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:31
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition ArchSpec.cpp:685
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:251
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:776
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:769
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:488
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:557
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:406
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:187
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