1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
//===-- ObjectFileCOFF.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ObjectFileCOFF.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Utility/LLDBLog.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatAdapters.h"
using namespace lldb;
using namespace lldb_private;
using namespace llvm;
using namespace llvm::object;
static bool IsCOFFObjectFile(const DataBufferSP &data) {
return identify_magic(toStringRef(data->GetData())) ==
file_magic::coff_object;
}
LLDB_PLUGIN_DEFINE(ObjectFileCOFF)
char ObjectFileCOFF::ID;
ObjectFileCOFF::~ObjectFileCOFF() = default;
void ObjectFileCOFF::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance,
CreateMemoryInstance, GetModuleSpecifications);
}
void ObjectFileCOFF::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
lldb_private::ObjectFile *
ObjectFileCOFF::CreateInstance(const ModuleSP &module_sp, DataBufferSP data_sp,
offset_t data_offset, const FileSpec *file,
offset_t file_offset, offset_t length) {
Log *log = GetLog(LLDBLog::Object);
if (!data_sp) {
data_sp = MapFileData(*file, length, file_offset);
if (!data_sp) {
LLDB_LOG(log,
"Failed to create ObjectFileCOFF instance: cannot read file {0}",
file->GetPath());
return nullptr;
}
data_offset = 0;
}
assert(data_sp && "must have mapped file at this point");
if (!IsCOFFObjectFile(data_sp))
return nullptr;
if (data_sp->GetByteSize() < length) {
data_sp = MapFileData(*file, length, file_offset);
if (!data_sp) {
LLDB_LOG(log,
"Failed to create ObjectFileCOFF instance: cannot read file {0}",
file->GetPath());
return nullptr;
}
data_offset = 0;
}
MemoryBufferRef buffer{toStringRef(data_sp->GetData()),
file->GetFilename().GetStringRef()};
Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
if (!binary) {
LLDB_LOG_ERROR(log, binary.takeError(),
"Failed to create binary for file ({1}): {0}",
file->GetPath());
return nullptr;
}
LLDB_LOG(log, "ObjectFileCOFF::ObjectFileCOFF module = {1} ({2}), file = {3}",
module_sp.get(), module_sp->GetSpecificationDescription(),
file->GetPath());
return new ObjectFileCOFF(unique_dyn_cast<COFFObjectFile>(std::move(*binary)),
module_sp, data_sp, data_offset, file, file_offset,
length);
}
lldb_private::ObjectFile *ObjectFileCOFF::CreateMemoryInstance(
const ModuleSP &module_sp, WritableDataBufferSP data_sp,
const ProcessSP &process_sp, addr_t header) {
// FIXME: do we need to worry about construction from a memory region?
return nullptr;
}
size_t ObjectFileCOFF::GetModuleSpecifications(
const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
offset_t file_offset, offset_t length, ModuleSpecList &specs) {
if (!IsCOFFObjectFile(data_sp))
return 0;
MemoryBufferRef buffer{toStringRef(data_sp->GetData()),
file.GetFilename().GetStringRef()};
Expected<std::unique_ptr<Binary>> binary = createBinary(buffer);
if (!binary) {
Log *log = GetLog(LLDBLog::Object);
LLDB_LOG_ERROR(log, binary.takeError(),
"Failed to create binary for file ({1}): {0}",
file.GetFilename());
return 0;
}
std::unique_ptr<COFFObjectFile> object =
unique_dyn_cast<COFFObjectFile>(std::move(*binary));
switch (static_cast<COFF::MachineTypes>(object->getMachine())) {
case COFF::IMAGE_FILE_MACHINE_I386:
specs.Append(ModuleSpec(file, ArchSpec("i686-unknown-windows-msvc")));
return 1;
case COFF::IMAGE_FILE_MACHINE_AMD64:
specs.Append(ModuleSpec(file, ArchSpec("x86_64-unknown-windows-msvc")));
return 1;
case COFF::IMAGE_FILE_MACHINE_ARMNT:
specs.Append(ModuleSpec(file, ArchSpec("armv7-unknown-windows-msvc")));
return 1;
case COFF::IMAGE_FILE_MACHINE_ARM64:
specs.Append(ModuleSpec(file, ArchSpec("aarch64-unknown-windows-msvc")));
return 1;
default:
return 0;
}
}
void ObjectFileCOFF::Dump(Stream *stream) {
ModuleSP module(GetModule());
if (!module)
return;
std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
stream->Printf("%p: ", static_cast<void *>(this));
stream->Indent();
stream->PutCString("ObjectFileCOFF");
*stream << ", file = '" << m_file
<< "', arch = " << GetArchitecture().GetArchitectureName() << '\n';
if (SectionList *sections = GetSectionList())
sections->Dump(stream->AsRawOstream(), stream->GetIndentLevel(), nullptr,
true, std::numeric_limits<uint32_t>::max());
}
uint32_t ObjectFileCOFF::GetAddressByteSize() const {
return const_cast<ObjectFileCOFF *>(this)->GetArchitecture().GetAddressByteSize();
}
ArchSpec ObjectFileCOFF::GetArchitecture() {
switch (static_cast<COFF::MachineTypes>(m_object->getMachine())) {
case COFF::IMAGE_FILE_MACHINE_I386:
return ArchSpec("i686-unknown-windows-msvc");
case COFF::IMAGE_FILE_MACHINE_AMD64:
return ArchSpec("x86_64-unknown-windows-msvc");
case COFF::IMAGE_FILE_MACHINE_ARMNT:
return ArchSpec("armv7-unknown-windows-msvc");
case COFF::IMAGE_FILE_MACHINE_ARM64:
return ArchSpec("aarch64-unknown-windows-msvc");
default:
return ArchSpec();
}
}
void ObjectFileCOFF::CreateSections(lldb_private::SectionList §ions) {
if (m_sections_up)
return;
m_sections_up = std::make_unique<SectionList>();
ModuleSP module(GetModule());
if (!module)
return;
std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
auto SectionType = [](StringRef Name,
const coff_section *Section) -> lldb::SectionType {
lldb::SectionType type =
StringSwitch<lldb::SectionType>(Name)
// DWARF Debug Sections
.Case(".debug_abbrev", eSectionTypeDWARFDebugAbbrev)
.Case(".debug_info", eSectionTypeDWARFDebugInfo)
.Case(".debug_line", eSectionTypeDWARFDebugLine)
.Case(".debug_pubnames", eSectionTypeDWARFDebugPubNames)
.Case(".debug_pubtypes", eSectionTypeDWARFDebugPubTypes)
.Case(".debug_str", eSectionTypeDWARFDebugStr)
// CodeView Debug Sections: .debug$S, .debug$T
.StartsWith(".debug$", eSectionTypeDebug)
.Case("clangast", eSectionTypeOther)
.Default(eSectionTypeInvalid);
if (type != eSectionTypeInvalid)
return type;
if (Section->Characteristics & COFF::IMAGE_SCN_CNT_CODE)
return eSectionTypeCode;
if (Section->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
return eSectionTypeData;
if (Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
return Section->SizeOfRawData ? eSectionTypeData : eSectionTypeZeroFill;
return eSectionTypeOther;
};
auto Permissions = [](const object::coff_section *Section) -> uint32_t {
uint32_t permissions = 0;
if (Section->Characteristics & COFF::IMAGE_SCN_MEM_EXECUTE)
permissions |= lldb::ePermissionsExecutable;
if (Section->Characteristics & COFF::IMAGE_SCN_MEM_READ)
permissions |= lldb::ePermissionsReadable;
if (Section->Characteristics & COFF::IMAGE_SCN_MEM_WRITE)
permissions |= lldb::ePermissionsWritable;
return permissions;
};
for (const auto &SecRef : m_object->sections()) {
const auto COFFSection = m_object->getCOFFSection(SecRef);
llvm::Expected<StringRef> Name = SecRef.getName();
StringRef SectionName = Name ? *Name : COFFSection->Name;
if (!Name)
consumeError(Name.takeError());
SectionSP section =
std::make_unique<Section>(module, this,
static_cast<user_id_t>(SecRef.getIndex()),
ConstString(SectionName),
SectionType(SectionName, COFFSection),
COFFSection->VirtualAddress,
COFFSection->VirtualSize,
COFFSection->PointerToRawData,
COFFSection->SizeOfRawData,
COFFSection->getAlignment(),
0);
section->SetPermissions(Permissions(COFFSection));
m_sections_up->AddSection(section);
sections.AddSection(section);
}
}
void ObjectFileCOFF::ParseSymtab(lldb_private::Symtab &symtab) {
Log *log = GetLog(LLDBLog::Object);
SectionList *sections = GetSectionList();
symtab.Reserve(symtab.GetNumSymbols() + m_object->getNumberOfSymbols());
auto SymbolType = [](const COFFSymbolRef &Symbol) -> lldb::SymbolType {
if (Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
return eSymbolTypeCode;
if (Symbol.getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_NULL)
return eSymbolTypeData;
return eSymbolTypeInvalid;
};
for (const auto &SymRef : m_object->symbols()) {
const auto COFFSymRef = m_object->getCOFFSymbol(SymRef);
Expected<StringRef> NameOrErr = SymRef.getName();
if (!NameOrErr) {
LLDB_LOG_ERROR(log, NameOrErr.takeError(),
"ObjectFileCOFF: failed to get symbol name: {0}");
continue;
}
Symbol symbol;
symbol.GetMangled().SetValue(ConstString(*NameOrErr));
int16_t SecIdx = static_cast<int16_t>(COFFSymRef.getSectionNumber());
if (SecIdx == COFF::IMAGE_SYM_ABSOLUTE) {
symbol.GetAddressRef() = Address{COFFSymRef.getValue()};
symbol.SetType(eSymbolTypeAbsolute);
} else if (SecIdx >= 1) {
symbol.GetAddressRef() = Address(sections->GetSectionAtIndex(SecIdx - 1),
COFFSymRef.getValue());
symbol.SetType(SymbolType(COFFSymRef));
}
symtab.AddSymbol(symbol);
}
LLDB_LOG(log, "ObjectFileCOFF::ParseSymtab processed {0} symbols",
m_object->getNumberOfSymbols());
}
bool ObjectFileCOFF::ParseHeader() {
ModuleSP module(GetModule());
if (!module)
return false;
std::lock_guard<std::recursive_mutex> guard(module->GetMutex());
m_data.SetByteOrder(eByteOrderLittle);
m_data.SetAddressByteSize(GetAddressByteSize());
return true;
}
|