16#include "llvm/Support/Error.h"
17#include "llvm/Support/FormatAdapters.h"
23using namespace llvm::object;
26 return identify_magic(toStringRef(data->GetData())) ==
27 file_magic::coff_object;
56 "Failed to create ObjectFileCOFF instance: cannot read file {0}",
63 assert(data_sp &&
"must have mapped file at this point");
68 if (data_sp->GetByteSize() < length) {
72 "Failed to create ObjectFileCOFF instance: cannot read file {0}",
80 MemoryBufferRef buffer{toStringRef(data_sp->GetData()),
83 Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
86 "Failed to create binary for file ({1}): {0}",
91 LLDB_LOG(log,
"ObjectFileCOFF::ObjectFileCOFF module = {1} ({2}), file = {3}",
92 module_sp.get(), module_sp->GetSpecificationDescription(),
95 return new ObjectFileCOFF(unique_dyn_cast<COFFObjectFile>(std::move(*binary)),
96 module_sp, data_sp, data_offset, file, file_offset,
113 MemoryBufferRef buffer{toStringRef(data_sp->GetData()),
115 Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
119 "Failed to create binary for file ({1}): {0}",
124 std::unique_ptr<COFFObjectFile>
object =
125 unique_dyn_cast<COFFObjectFile>(std::move(*binary));
126 switch (
static_cast<COFF::MachineTypes
>(object->getMachine())) {
127 case COFF::IMAGE_FILE_MACHINE_I386:
130 case COFF::IMAGE_FILE_MACHINE_AMD64:
133 case COFF::IMAGE_FILE_MACHINE_ARMNT:
136 case COFF::IMAGE_FILE_MACHINE_ARM64:
149 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
151 stream->
Printf(
"%p: ",
static_cast<void *
>(
this));
154 *stream <<
", file = '" <<
m_file
159 true, std::numeric_limits<uint32_t>::max());
167 switch (
static_cast<COFF::MachineTypes
>(
m_object->getMachine())) {
168 case COFF::IMAGE_FILE_MACHINE_I386:
169 return ArchSpec(
"i686-unknown-windows-msvc");
170 case COFF::IMAGE_FILE_MACHINE_AMD64:
171 return ArchSpec(
"x86_64-unknown-windows-msvc");
172 case COFF::IMAGE_FILE_MACHINE_ARMNT:
173 return ArchSpec(
"armv7-unknown-windows-msvc");
174 case COFF::IMAGE_FILE_MACHINE_ARM64:
175 return ArchSpec(
"aarch64-unknown-windows-msvc");
190 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
195 StringSwitch<lldb::SectionType>(Name)
210 if (
Section->Characteristics & COFF::IMAGE_SCN_CNT_CODE)
212 if (
Section->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
214 if (
Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
218 auto Permissions = [](
const object::coff_section *
Section) -> uint32_t {
219 uint32_t permissions = 0;
220 if (
Section->Characteristics & COFF::IMAGE_SCN_MEM_EXECUTE)
221 permissions |= lldb::ePermissionsExecutable;
222 if (
Section->Characteristics & COFF::IMAGE_SCN_MEM_READ)
223 permissions |= lldb::ePermissionsReadable;
224 if (
Section->Characteristics & COFF::IMAGE_SCN_MEM_WRITE)
225 permissions |= lldb::ePermissionsWritable;
229 for (
const auto &SecRef :
m_object->sections()) {
230 const auto COFFSection =
m_object->getCOFFSection(SecRef);
232 llvm::Expected<StringRef> Name = SecRef.getName();
233 StringRef SectionName = Name ? *Name : COFFSection->Name;
235 consumeError(Name.takeError());
238 std::make_unique<Section>(module,
this,
239 static_cast<user_id_t>(SecRef.getIndex()),
242 COFFSection->VirtualAddress,
243 COFFSection->VirtualSize,
244 COFFSection->PointerToRawData,
245 COFFSection->SizeOfRawData,
246 COFFSection->getAlignment(),
248 section->SetPermissions(Permissions(COFFSection));
262 if (
Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
264 if (
Symbol.getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
265 Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_NULL)
270 for (
const auto &SymRef :
m_object->symbols()) {
271 const auto COFFSymRef =
m_object->getCOFFSymbol(SymRef);
273 Expected<StringRef> NameOrErr = SymRef.getName();
276 "ObjectFileCOFF: failed to get symbol name: {0}");
283 int16_t SecIdx =
static_cast<int16_t
>(COFFSymRef.getSectionNumber());
284 if (SecIdx == COFF::IMAGE_SYM_ABSOLUTE) {
287 }
else if (SecIdx >= 1) {
289 COFFSymRef.getValue());
296 LLDB_LOG(log,
"ObjectFileCOFF::ParseSymtab processed {0} symbols",
305 std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOG_ERROR(log, error,...)
static bool IsCOFFObjectFile(const DataBufferSP &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 * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header)
void Dump(lldb_private::Stream *stream) override
Dump a description of this object to a Stream.
void CreateSections(lldb_private::SectionList &) override
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.
std::unique_ptr< llvm::object::COFFObjectFile > m_object
static llvm::StringRef GetPluginNameStatic()
A section + offset based address class.
An architecture specification class.
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
A uniqued constant string class.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const ConstString & GetFilename() const
Filename string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void SetValue(ConstString name)
Set the string value in this object.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
void Append(const ModuleSpec &spec)
A plug-in interface definition class for object file parsers.
DataExtractor m_data
The data for this object file so things can be parsed lazily.
std::unique_ptr< lldb_private::SectionList > m_sections_up
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset)
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 §ion_sp)
lldb::SectionSP GetSectionAtIndex(size_t idx) const
A stream class that can stream formatted output to a file.
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
unsigned GetIndentLevel() const
Get the current indentation level.
void SetType(lldb::SymbolType type)
Address & GetAddressRef()
uint32_t AddSymbol(const Symbol &symbol)
size_t GetNumSymbols() const
void Reserve(size_t count)
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.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
@ eSectionTypeDWARFDebugPubNames
@ eSectionTypeDWARFDebugInfo
@ eSectionTypeDWARFDebugLine
@ eSectionTypeDWARFDebugPubTypes
@ eSectionTypeDWARFDebugStr
@ eSectionTypeDWARFDebugAbbrev
std::shared_ptr< lldb_private::Module > ModuleSP