44 GetPluginDescriptionStatic(), CreateInstance,
53 return "The Dynamic Loader Plugin For FreeBSD Kernel";
109 const llvm::Triple &triple_ref =
111 if (!triple_ref.isOSFreeBSD()) {
158 llvm::ELF::Elf32_Ehdr &header,
171 if (!header.checkMagic())
180 Log *log =
GetLog(LLDBLog::DynamicLoader);
189 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
190 "looking for kernel binary at 0x%" PRIx64,
193 llvm::ELF::Elf32_Ehdr header;
200 if (header.e_type != llvm::ELF::ET_EXEC)
206 if (!memory_module_sp.get()) {
211 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
212 if (exe_objfile ==
nullptr) {
214 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress "
215 "found a binary at 0x%" PRIx64
216 " but could not create an object file from memory",
224 ArchSpec kernel_arch(llvm::ELF::convertEMachineToArchName(header.e_machine));
229 std::string uuid_str;
230 if (memory_module_sp->GetUUID().IsValid()) {
231 uuid_str =
"with UUID ";
232 uuid_str += memory_module_sp->GetUUID().GetAsString();
234 uuid_str =
"and no LC_UUID found in load commands ";
237 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
238 "kernel binary image found at 0x%" PRIx64
" with arch '%s' %s",
239 addr, kernel_arch.
GetTriple().str().c_str(), uuid_str.c_str());
241 return memory_module_sp->GetUUID();
252 m_kernel_load_address(kernel_address), m_mutex() {
266 Log *log =
GetLog(LLDBLog::DynamicLoader);
276 llvm::ELF::Elf32_Ehdr elf_eheader;
277 size_t size_to_read = 512;
280 if (elf_eheader.e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32) {
281 size_to_read =
sizeof(llvm::ELF::Elf32_Ehdr) +
282 elf_eheader.e_phnum * elf_eheader.e_phentsize;
283 }
else if (elf_eheader.e_ident[llvm::ELF::EI_CLASS] ==
284 llvm::ELF::ELFCLASS64) {
285 llvm::ELF::Elf64_Ehdr elf_eheader;
288 error) ==
sizeof(elf_eheader))
289 size_to_read =
sizeof(llvm::ELF::Elf64_Ehdr) +
290 elf_eheader.e_phnum * elf_eheader.e_phentsize;
297 if (!memory_module_sp)
300 bool this_is_kernel =
is_kernel(memory_module_sp.get());
302 if (!
m_uuid.
IsValid() && memory_module_sp->GetUUID().IsValid())
303 m_uuid = memory_module_sp->GetUUID();
309 if (this_is_kernel) {
310 LLDB_LOGF(log,
"KextImageInfo::ReadMemoryModule read the kernel binary out "
313 if (memory_module_sp->GetArchitecture().IsValid())
322 Log *log =
GetLog(LLDBLog::DynamicLoader);
329 if (IsKernel() && m_uuid.IsValid()) {
331 s.
Printf(
"Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
332 s.
Printf(
"Load Address: 0x%" PRIx64
"\n", m_load_address);
340 m_module_sp = target_images.
FindModule(m_uuid);
350 m_module_sp = std::make_shared<Module>(module_spec.
GetFileSpec(),
357 if (IsKernel() && !m_module_sp) {
359 s.
Printf(
"WARNING: Unable to locate kernel binary on the debugger "
367 if (!IsKernel() && !
is_kmod(m_module_sp.get())) {
369 if (existing_module_sp &&
370 existing_module_sp->IsLoadedInTarget(&target)) {
372 "'%s' with UUID %s is not a kmod or kernel, and is "
373 "already registered in target, not loading.",
374 m_name.c_str(), m_uuid.GetAsString().c_str());
378 m_uuid = m_module_sp->GetUUID();
391 bool changed =
false;
392 m_module_sp->SetLoadAddress(target, m_load_address,
true, changed);
397 ReadMemoryModule(process);
400 if (!m_memory_module_sp || !m_module_sp) {
405 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
406 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
408 if (!ondisk_object_file || !memory_object_file)
413 if (llvm::dyn_cast<ObjectFileELF>(memory_object_file)) {
417 m_load_address != load_address) {
418 fixed_slide = m_load_address - load_address;
420 "kmod %s in-memory LOAD vmaddr is not correct, using a "
421 "fixed slide of 0x%" PRIx64,
422 m_name.c_str(), fixed_slide);
429 if (memory_section_list && ondisk_object_file) {
430 const uint32_t num_ondisk_sections = ondisk_section_list->
GetSize();
431 uint32_t num_load_sections = 0;
433 for (uint32_t section_idx = 0; section_idx < num_ondisk_sections;
438 if (!on_disk_section_sp)
442 on_disk_section_sp->GetFileAddress() +
446 const Section *memory_section =
450 if (memory_section) {
458 if (num_load_sections)
466 if (IsLoaded() && m_module_sp && IsKernel()) {
468 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
469 if (kernel_object_file) {
474 s.
Printf(
"Kernel slide 0x%" PRIx64
" in memory.\n",
475 m_load_address - file_address);
476 s.
Printf(
"Loaded kernel file %s\n",
477 m_module_sp->GetFileSpec().GetPath().c_str());
494 bool changed =
false;
495 if (m_module_sp->SetLoadAddress(process->
GetTarget(), 0,
true, changed))
532 Log *log =
GetLog(LLDBLog::DynamicLoader);
534 if (!
ReadAllKmods(linker_files_head_addr, linker_files_list))
538 "Kmod-changed breakpoint hit, there are %zu kernel modules currently.\n",
539 linker_files_list.size());
558 bool failed_to_load =
false;
559 if (!image_info.LoadImageUsingMemoryModule(
m_process)) {
560 image_info.LoadImageUsingFileAddress(
m_process);
561 failed_to_load =
true;
576 Address linker_files_head_addr,
580 static ConstString kld_off_address_symbol_name(
"kld_off_address");
581 static ConstString kld_off_next_symbol_name(
"kld_off_next");
582 static ConstString kld_off_filename_symbol_name(
"kld_off_filename");
583 static ConstString kld_off_pathname_symbol_name(
"kld_off_pathname");
584 const Symbol *kld_off_address_symbol =
587 const Symbol *kld_off_next_symbol =
590 const Symbol *kld_off_filename_symbol =
593 const Symbol *kld_off_pathname_symbol =
597 if (!kld_off_address_symbol || !kld_off_next_symbol ||
598 !kld_off_filename_symbol || !kld_off_pathname_symbol)
629 char kld_filename[255];
630 char kld_pathname[255];
634 while (current_kld != 0) {
635 addr_t kld_filename_addr =
639 addr_t kld_pathname_addr =
645 sizeof(kld_filename),
error);
649 sizeof(kld_pathname),
error);
657 kmods_list.emplace_back();
659 kmod_info.
SetName(kld_filename);
661 kmod_info.
SetPath(kld_pathname);
665 if (kmod_info.
GetName() ==
"kernel")
666 kmods_list.pop_back();
688 Log *log =
GetLog(LLDBLog::DynamicLoader);
689 LLDB_LOGF(log,
"DynamicLoaderFreeBSDKernel::LoadKernelModules "
690 "Start loading Kernel Module");
701 llvm::StringRef kernel_name(
"freebsd_kernel");
703 if (module_sp.get() && module_sp->GetObjectFile() &&
704 !module_sp->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
705 kernel_name = module_sp->GetObjectFile()
730 static ConstString modlist_symbol_name(
"linker_files");
740 LLDB_LOGF(log,
"DynamicLoaderFreeBSDKernel::LoadKernelModules "
741 "cannot file modlist symbol");
780 LLDB_LOGF(log,
"DynamicLoaderFreeBSDKernel::GetStepThroughTrampolinePlan is "
781 "not yet implemented.");
786 Status error(
"shared object cannot be loaded into kernel");
static llvm::raw_ostream & error(Stream &strm)
static bool is_kernel(Module *module)
static bool is_reloc(Module *module)
static bool is_kernel(Module *module)
static bool is_kmod(Module *module)
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
bool LoadImageUsingFileAddress(lldb_private::Process *process)
lldb::addr_t GetLoadAddress() const
lldb::addr_t m_load_address
std::string GetName() const
bool ReadMemoryModule(lldb_private::Process *process)
void SetLoadAddress(lldb::addr_t load_address)
bool LoadImageUsingMemoryModule(lldb_private::Process *process)
void SetModule(lldb::ModuleSP module)
void SetPath(const char *path)
lldb::ModuleSP GetModule()
lldb::ModuleSP m_memory_module_sp
lldb_private::UUID m_uuid
std::vector< KModImageInfo > collection_type
void SetName(const char *name)
void SetIsKernel(bool is_kernel)
void DidAttach() override
Called after attaching a process.
DynamicLoaderFreeBSDKernel(lldb_private::Process *process, lldb::addr_t kernel_addr)
KModImageInfo::collection_type m_linker_files_list
bool ReadKmodsListHeader()
lldb_private::Address m_linker_file_list_struct_addr
lldb_private::Process * m_process
void PrivateInitialize(lldb_private::Process *process)
lldb_private::Address m_linker_file_head_addr
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
bool ParseKmods(lldb_private::Address linker_files_head_address)
static void DebuggerInit(lldb_private::Debugger &debugger)
void SetNotificationBreakPoint()
static lldb::addr_t FindFreeBSDKernel(lldb_private::Process *process)
void Clear(bool clear_process)
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
~DynamicLoaderFreeBSDKernel() override
std::unordered_map< std::string, lldb_private::UUID > m_kld_name_to_uuid
void DidLaunch() override
Called after launching a process.
lldb::addr_t m_kernel_load_address
static lldb::addr_t FindKernelAtLoadAddress(lldb_private::Process *process)
KModImageInfo m_kernel_image_info
static llvm::StringRef GetPluginDescriptionStatic()
static lldb_private::UUID CheckForKernelImageAtAddress(lldb_private::Process *process, lldb::addr_t address, bool *read_error=nullptr)
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
static bool ReadELFHeader(lldb_private::Process *process, lldb::addr_t address, llvm::ELF::Elf32_Ehdr &header, bool *read_error=nullptr)
std::recursive_mutex m_mutex
A section + offset based address class.
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
void Clear()
Clear the object's state.
lldb::addr_t GetFileAddress() const
Get the file address.
bool IsValid() const
Check if the object state is valid.
An architecture specification class.
llvm::Triple & GetTriple()
Architecture triple accessor.
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
A uniqued constant string class.
A class to manage flag bits.
StreamFile & GetOutputStream()
A plug-in interface definition class for dynamic loaders.
void LoadOperatingSystemPlugin(bool flush)
static FileSystem & Instance()
A collection class for Module objects.
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
lldb::ModuleSP FindModule(const Module *module_ptr) const
ModuleIterable Modules() const
A class that describes an executable image and its associated object and symbol files.
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
A plug-in interface definition class for object file parsers.
@ eTypeExecutable
A normal executable.
@ eTypeObjectFile
An intermediate object file.
@ eTypeSharedLibrary
A shared library that can be used during execution.
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
virtual lldb_private::Address GetBaseAddress()
Returns base address of this object file.
static bool DownloadObjectAndSymbolFile(ModuleSpec &module_spec, Status &error, bool force_lookup=true, bool copy_executable=true)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
int64_t ReadSignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, int64_t fail_value, Status &error)
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
void SetCanRunCode(bool can_run_code)
Sets whether executing code in this process is possible.
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
uint32_t GetStopID() const
lldb::ModuleSP ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Target & GetTarget()
Get the target object pointer for this module.
lldb::SectionSP FindSectionByName(ConstString section_dstr) const
lldb::SectionSP GetSectionAtIndex(size_t idx) const
lldb::addr_t GetFileAddress() const
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
virtual void Flush()=0
Flush the stream.
Address GetAddress() const
void ModulesDidLoad(ModuleList &module_list)
Module * GetExecutableModulePointer()
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false, bool merge=true)
Set the architecture for this target.
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
void ModulesDidUnload(ModuleList &module_list, bool delete_locations)
const ModuleList & GetImages() const
Get accessor for the images for this process.
const ArchSpec & GetArchitecture() const
bool SetSectionLoadAddress(const lldb::SectionSP §ion, lldb::addr_t load_addr, bool warn_multiple=false)
#define LLDB_INVALID_ADDRESS
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::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::Module > ModuleSP