Greg Clayton | fc7117a | 2011-03-05 01:04:56 +0000 | [diff] [blame^] | 1 | //===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Core/Module.h" |
| 11 | #include "lldb/Core/PluginManager.h" |
| 12 | #include "lldb/Target/Target.h" |
| 13 | |
| 14 | #include "DynamicLoaderStatic.h" |
| 15 | |
| 16 | using namespace lldb; |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | //---------------------------------------------------------------------- |
| 20 | // Create an instance of this class. This function is filled into |
| 21 | // the plugin info class that gets handed out by the plugin factory and |
| 22 | // allows the lldb to instantiate an instance of this class. |
| 23 | //---------------------------------------------------------------------- |
| 24 | DynamicLoader * |
| 25 | DynamicLoaderStatic::CreateInstance (Process* process, bool force) |
| 26 | { |
| 27 | bool create = force; |
| 28 | if (!create) |
| 29 | { |
| 30 | const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple(); |
| 31 | const llvm::Triple::OSType os_type = triple_ref.getOS(); |
| 32 | if ((os_type == llvm::Triple::UnknownOS) || |
| 33 | (os_type == llvm::Triple::NoOS)) |
| 34 | create = true; |
| 35 | } |
| 36 | |
| 37 | if (create) |
| 38 | return new DynamicLoaderStatic (process); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | //---------------------------------------------------------------------- |
| 43 | // Constructor |
| 44 | //---------------------------------------------------------------------- |
| 45 | DynamicLoaderStatic::DynamicLoaderStatic (Process* process) : |
| 46 | DynamicLoader(process) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | //---------------------------------------------------------------------- |
| 51 | // Destructor |
| 52 | //---------------------------------------------------------------------- |
| 53 | DynamicLoaderStatic::~DynamicLoaderStatic() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | //------------------------------------------------------------------ |
| 58 | /// Called after attaching a process. |
| 59 | /// |
| 60 | /// Allow DynamicLoader plug-ins to execute some code after |
| 61 | /// attaching to a process. |
| 62 | //------------------------------------------------------------------ |
| 63 | void |
| 64 | DynamicLoaderStatic::DidAttach () |
| 65 | { |
| 66 | LoadAllImagesAtFileAddresses(); |
| 67 | } |
| 68 | |
| 69 | //------------------------------------------------------------------ |
| 70 | /// Called after attaching a process. |
| 71 | /// |
| 72 | /// Allow DynamicLoader plug-ins to execute some code after |
| 73 | /// attaching to a process. |
| 74 | //------------------------------------------------------------------ |
| 75 | void |
| 76 | DynamicLoaderStatic::DidLaunch () |
| 77 | { |
| 78 | LoadAllImagesAtFileAddresses(); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | DynamicLoaderStatic::LoadAllImagesAtFileAddresses () |
| 83 | { |
| 84 | ModuleList &module_list = m_process->GetTarget().GetImages(); |
| 85 | |
| 86 | ModuleList loaded_module_list; |
| 87 | |
| 88 | const size_t num_modules = module_list.GetSize(); |
| 89 | for (uint32_t idx = 0; idx < num_modules; ++idx) |
| 90 | { |
| 91 | ModuleSP module_sp (module_list.GetModuleAtIndex (idx)); |
| 92 | if (module_sp) |
| 93 | { |
| 94 | bool changed = false; |
| 95 | ObjectFile *image_object_file = module_sp->GetObjectFile(); |
| 96 | if (image_object_file) |
| 97 | { |
| 98 | SectionList *section_list = image_object_file->GetSectionList (); |
| 99 | if (section_list) |
| 100 | { |
| 101 | // All sections listed in the dyld image info structure will all |
| 102 | // either be fixed up already, or they will all be off by a single |
| 103 | // slide amount that is determined by finding the first segment |
| 104 | // that is at file offset zero which also has bytes (a file size |
| 105 | // that is greater than zero) in the object file. |
| 106 | |
| 107 | // Determine the slide amount (if any) |
| 108 | const size_t num_sections = section_list->GetSize(); |
| 109 | size_t sect_idx = 0; |
| 110 | for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) |
| 111 | { |
| 112 | // Iterate through the object file sections to find the |
| 113 | // first section that starts of file offset zero and that |
| 114 | // has bytes in the file... |
| 115 | Section *section = section_list->GetSectionAtIndex (sect_idx).get(); |
| 116 | if (section) |
| 117 | { |
| 118 | if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress())) |
| 119 | changed = true; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (changed) |
| 126 | loaded_module_list.AppendIfNeeded (module_sp); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (loaded_module_list.GetSize()) |
| 131 | m_process->GetTarget().ModulesDidLoad (loaded_module_list); |
| 132 | } |
| 133 | |
| 134 | ThreadPlanSP |
| 135 | DynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others) |
| 136 | { |
| 137 | return ThreadPlanSP(); |
| 138 | } |
| 139 | |
| 140 | Error |
| 141 | DynamicLoaderStatic::CanLoadImage () |
| 142 | { |
| 143 | Error error; |
| 144 | error.SetErrorString ("can't load images on with a static debug session"); |
| 145 | return error; |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | DynamicLoaderStatic::Initialize() |
| 150 | { |
| 151 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 152 | GetPluginDescriptionStatic(), |
| 153 | CreateInstance); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | DynamicLoaderStatic::Terminate() |
| 158 | { |
| 159 | PluginManager::UnregisterPlugin (CreateInstance); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | const char * |
| 164 | DynamicLoaderStatic::GetPluginNameStatic() |
| 165 | { |
| 166 | return "dynamic-loader.static"; |
| 167 | } |
| 168 | |
| 169 | const char * |
| 170 | DynamicLoaderStatic::GetPluginDescriptionStatic() |
| 171 | { |
| 172 | return "Dynamic loader plug-in that will load any images at the static addresses contained in each image."; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | //------------------------------------------------------------------ |
| 177 | // PluginInterface protocol |
| 178 | //------------------------------------------------------------------ |
| 179 | const char * |
| 180 | DynamicLoaderStatic::GetPluginName() |
| 181 | { |
| 182 | return "DynamicLoaderStatic"; |
| 183 | } |
| 184 | |
| 185 | const char * |
| 186 | DynamicLoaderStatic::GetShortPluginName() |
| 187 | { |
| 188 | return GetPluginNameStatic(); |
| 189 | } |
| 190 | |
| 191 | uint32_t |
| 192 | DynamicLoaderStatic::GetPluginVersion() |
| 193 | { |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | DynamicLoaderStatic::GetPluginCommandHelp (const char *command, Stream *strm) |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | Error |
| 203 | DynamicLoaderStatic::ExecutePluginCommand (Args &command, Stream *strm) |
| 204 | { |
| 205 | Error error; |
| 206 | error.SetErrorString("No plug-in command are currently supported."); |
| 207 | return error; |
| 208 | } |
| 209 | |
| 210 | Log * |
| 211 | DynamicLoaderStatic::EnablePluginLogging (Stream *strm, Args &command) |
| 212 | { |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | |