Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SymbolFileDWARFDebugMap.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 "SymbolFileDWARFDebugMap.h" |
| 11 | |
| 12 | #include "lldb/Core/Module.h" |
| 13 | #include "lldb/Core/ModuleList.h" |
| 14 | #include "lldb/Core/PluginManager.h" |
| 15 | #include "lldb/Core/RegularExpression.h" |
| 16 | #include "lldb/Core/StreamFile.h" |
| 17 | #include "lldb/Core/Timer.h" |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 18 | |
| 19 | #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/ObjectFile.h" |
| 21 | #include "lldb/Symbol/SymbolVendor.h" |
| 22 | #include "lldb/Symbol/VariableList.h" |
| 23 | |
| 24 | #include "SymbolFileDWARF.h" |
| 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | void |
| 30 | SymbolFileDWARFDebugMap::Initialize() |
| 31 | { |
| 32 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 33 | GetPluginDescriptionStatic(), |
| 34 | CreateInstance); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | SymbolFileDWARFDebugMap::Terminate() |
| 39 | { |
| 40 | PluginManager::UnregisterPlugin (CreateInstance); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | const char * |
| 45 | SymbolFileDWARFDebugMap::GetPluginNameStatic() |
| 46 | { |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 47 | return "dwarf-debugmap"; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | const char * |
| 51 | SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() |
| 52 | { |
| 53 | return "DWARF and DWARF3 debug symbol file reader (debug map)."; |
| 54 | } |
| 55 | |
| 56 | SymbolFile* |
| 57 | SymbolFileDWARFDebugMap::CreateInstance (ObjectFile* obj_file) |
| 58 | { |
| 59 | return new SymbolFileDWARFDebugMap (obj_file); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap (ObjectFile* ofile) : |
| 64 | SymbolFile(ofile), |
| 65 | m_flags(), |
| 66 | m_compile_unit_infos(), |
| 67 | m_func_indexes(), |
| 68 | m_glob_indexes() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | |
| 73 | SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() |
| 74 | { |
| 75 | } |
| 76 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 77 | void |
| 78 | SymbolFileDWARFDebugMap::InitializeObject() |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 79 | { |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 80 | // Install our external AST source callbacks so we can complete Clang types. |
| 81 | llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap ( |
| 82 | new ClangExternalASTSourceCallbacks (SymbolFileDWARFDebugMap::CompleteTagDecl, |
| 83 | SymbolFileDWARFDebugMap::CompleteObjCInterfaceDecl, |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 84 | NULL, |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 85 | this)); |
| 86 | |
| 87 | GetClangASTContext().SetExternalSource (ast_source_ap); |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 90 | |
| 91 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | void |
| 93 | SymbolFileDWARFDebugMap::InitOSO () |
| 94 | { |
| 95 | if (m_flags.test(kHaveInitializedOSOs)) |
| 96 | return; |
| 97 | |
| 98 | m_flags.set(kHaveInitializedOSOs); |
| 99 | // In order to get the abilities of this plug-in, we look at the list of |
| 100 | // N_OSO entries (object files) from the symbol table and make sure that |
| 101 | // these files exist and also contain valid DWARF. If we get any of that |
| 102 | // then we return the abilities of the first N_OSO's DWARF. |
| 103 | |
| 104 | Symtab* symtab = m_obj_file->GetSymtab(); |
| 105 | if (symtab) |
| 106 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | std::vector<uint32_t> oso_indexes; |
Greg Clayton | 16b2d2b | 2011-01-20 06:08:59 +0000 | [diff] [blame] | 108 | // StreamFile s(stdout); |
| 109 | // symtab->Dump(&s, NULL, eSortOrderNone); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | |
Greg Clayton | 16b2d2b | 2011-01-20 06:08:59 +0000 | [diff] [blame] | 111 | // When a mach-o symbol is encoded, the n_type field is encoded in bits |
| 112 | // 23:16, and the n_desc field is encoded in bits 15:0. |
| 113 | // |
| 114 | // To find all N_OSO entries that are part of the DWARF + debug map |
| 115 | // we find only object file symbols with the flags value as follows: |
| 116 | // bits 23:16 == 0x66 (N_OSO) |
| 117 | // bits 15: 0 == 0x0001 (specifies this is a debug map object file) |
| 118 | const uint32_t k_oso_symbol_flags_value = 0x660001u; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | |
Greg Clayton | 16b2d2b | 2011-01-20 06:08:59 +0000 | [diff] [blame] | 120 | const uint32_t oso_index_count = symtab->AppendSymbolIndexesWithTypeAndFlagsValue(eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | |
| 122 | if (oso_index_count > 0) |
| 123 | { |
Greg Clayton | 16b2d2b | 2011-01-20 06:08:59 +0000 | [diff] [blame] | 124 | symtab->AppendSymbolIndexesWithType (eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes); |
| 125 | symtab->AppendSymbolIndexesWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, m_glob_indexes); |
| 126 | |
| 127 | symtab->SortSymbolIndexesByValue(m_func_indexes, true); |
| 128 | symtab->SortSymbolIndexesByValue(m_glob_indexes, true); |
| 129 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | m_compile_unit_infos.resize(oso_index_count); |
| 131 | // s.Printf("%s N_OSO symbols:\n", __PRETTY_FUNCTION__); |
| 132 | // symtab->Dump(&s, oso_indexes); |
| 133 | |
| 134 | for (uint32_t i=0; i<oso_index_count; ++i) |
| 135 | { |
| 136 | m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 1); |
| 137 | if (m_compile_unit_infos[i].so_symbol->GetSiblingIndex() == 0) |
| 138 | m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 2); |
| 139 | m_compile_unit_infos[i].oso_symbol = symtab->SymbolAtIndex(oso_indexes[i]); |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 140 | uint32_t sibling_idx = m_compile_unit_infos[i].so_symbol->GetSiblingIndex(); |
| 141 | assert (sibling_idx != 0); |
| 142 | assert (sibling_idx > i + 1); |
| 143 | m_compile_unit_infos[i].last_symbol = symtab->SymbolAtIndex (sibling_idx - 1); |
| 144 | m_compile_unit_infos[i].first_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].so_symbol); |
| 145 | m_compile_unit_infos[i].last_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].last_symbol); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | Module * |
| 152 | SymbolFileDWARFDebugMap::GetModuleByOSOIndex (uint32_t oso_idx) |
| 153 | { |
| 154 | const uint32_t cu_count = GetNumCompileUnits(); |
| 155 | if (oso_idx < cu_count) |
| 156 | return GetModuleByCompUnitInfo (&m_compile_unit_infos[oso_idx]); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | Module * |
| 161 | SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info) |
| 162 | { |
| 163 | if (comp_unit_info->oso_module_sp.get() == NULL) |
| 164 | { |
| 165 | Symbol *oso_symbol = comp_unit_info->oso_symbol; |
| 166 | if (oso_symbol) |
| 167 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 168 | FileSpec oso_file_spec(oso_symbol->GetMangled().GetName().AsCString(), true); |
Greg Clayton | 762f713 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 169 | // Always create a new module for .o files. Why? Because we |
| 170 | // use the debug map, to add new sections to each .o file and |
| 171 | // even though a .o file might not have changed, the sections |
| 172 | // that get added to the .o file can change. |
| 173 | comp_unit_info->oso_module_sp = new Module (oso_file_spec, |
| 174 | m_obj_file->GetModule()->GetArchitecture(), |
| 175 | NULL, |
| 176 | 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | return comp_unit_info->oso_module_sp.get(); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | bool |
| 184 | SymbolFileDWARFDebugMap::GetFileSpecForSO (uint32_t oso_idx, FileSpec &file_spec) |
| 185 | { |
| 186 | if (oso_idx < m_compile_unit_infos.size()) |
| 187 | { |
| 188 | if (!m_compile_unit_infos[oso_idx].so_file) |
| 189 | { |
| 190 | |
| 191 | if (m_compile_unit_infos[oso_idx].so_symbol == NULL) |
| 192 | return false; |
| 193 | |
| 194 | std::string so_path (m_compile_unit_infos[oso_idx].so_symbol->GetMangled().GetName().AsCString()); |
| 195 | if (m_compile_unit_infos[oso_idx].so_symbol[1].GetType() == eSymbolTypeSourceFile) |
| 196 | so_path += m_compile_unit_infos[oso_idx].so_symbol[1].GetMangled().GetName().AsCString(); |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 197 | m_compile_unit_infos[oso_idx].so_file.SetFile(so_path.c_str(), true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | } |
| 199 | file_spec = m_compile_unit_infos[oso_idx].so_file; |
| 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | ObjectFile * |
| 208 | SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex (uint32_t oso_idx) |
| 209 | { |
| 210 | Module *oso_module = GetModuleByOSOIndex (oso_idx); |
| 211 | if (oso_module) |
| 212 | return oso_module->GetObjectFile(); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | SymbolFileDWARF * |
| 217 | SymbolFileDWARFDebugMap::GetSymbolFile (const SymbolContext& sc) |
| 218 | { |
| 219 | CompileUnitInfo *comp_unit_info = GetCompUnitInfo (sc); |
| 220 | if (comp_unit_info) |
| 221 | return GetSymbolFileByCompUnitInfo (comp_unit_info); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | ObjectFile * |
| 226 | SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info) |
| 227 | { |
| 228 | Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info); |
| 229 | if (oso_module) |
| 230 | return oso_module->GetObjectFile(); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | SymbolFileDWARF * |
| 235 | SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex (uint32_t oso_idx) |
| 236 | { |
| 237 | if (oso_idx < m_compile_unit_infos.size()) |
| 238 | return GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[oso_idx]); |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | SymbolFileDWARF * |
| 243 | SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info) |
| 244 | { |
| 245 | if (comp_unit_info->oso_symbol_vendor == NULL) |
| 246 | { |
| 247 | ObjectFile *oso_objfile = GetObjectFileByCompUnitInfo (comp_unit_info); |
| 248 | |
| 249 | if (oso_objfile) |
| 250 | { |
| 251 | comp_unit_info->oso_symbol_vendor = oso_objfile->GetModule()->GetSymbolVendor(); |
| 252 | // SymbolFileDWARF *oso_dwarf = new SymbolFileDWARF(oso_objfile); |
| 253 | // comp_unit_info->oso_dwarf_sp.reset (oso_dwarf); |
| 254 | if (comp_unit_info->oso_symbol_vendor) |
| 255 | { |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 256 | // Set a a pointer to this class to set our OSO DWARF file know |
| 257 | // that the DWARF is being used along with a debug map and that |
| 258 | // it will have the remapped sections that we do below. |
| 259 | ((SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile())->SetDebugMapSymfile(this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | comp_unit_info->debug_map_sections_sp.reset(new SectionList); |
| 261 | |
| 262 | Symtab *exe_symtab = m_obj_file->GetSymtab(); |
| 263 | Module *oso_module = oso_objfile->GetModule(); |
| 264 | Symtab *oso_symtab = oso_objfile->GetSymtab(); |
| 265 | //#define DEBUG_OSO_DMAP // Do not check in with this defined... |
| 266 | #if defined(DEBUG_OSO_DMAP) |
| 267 | StreamFile s(stdout); |
| 268 | s << "OSO symtab:\n"; |
| 269 | oso_symtab->Dump(&s, NULL); |
| 270 | s << "OSO sections before:\n"; |
| 271 | oso_objfile->GetSectionList()->Dump(&s, NULL, true); |
| 272 | #endif |
| 273 | |
| 274 | ///const uint32_t fun_resolve_flags = SymbolContext::Module | eSymbolContextCompUnit | eSymbolContextFunction; |
| 275 | //SectionList *oso_sections = oso_objfile->Sections(); |
| 276 | // Now we need to make sections that map from zero based object |
| 277 | // file addresses to where things eneded up in the main executable. |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 278 | uint32_t oso_start_idx = exe_symtab->GetIndexForSymbol (comp_unit_info->oso_symbol); |
| 279 | assert (oso_start_idx != UINT32_MAX); |
| 280 | oso_start_idx += 1; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | const uint32_t oso_end_idx = comp_unit_info->so_symbol->GetSiblingIndex(); |
| 282 | uint32_t sect_id = 0x10000; |
| 283 | for (uint32_t idx = oso_start_idx; idx < oso_end_idx; ++idx) |
| 284 | { |
| 285 | Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx); |
| 286 | if (exe_symbol) |
| 287 | { |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 288 | if (exe_symbol->IsDebug() == false) |
| 289 | continue; |
| 290 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | switch (exe_symbol->GetType()) |
| 292 | { |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 293 | default: |
| 294 | break; |
| 295 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 296 | case eSymbolTypeCode: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 297 | { |
| 298 | // For each N_FUN, or function that we run into in the debug map |
| 299 | // we make a new section that we add to the sections found in the |
| 300 | // .o file. This new section has the file address set to what the |
| 301 | // addresses are in the .o file, and the load address is adjusted |
| 302 | // to match where it ended up in the final executable! We do this |
| 303 | // before we parse any dwarf info so that when it goes get parsed |
| 304 | // all section/offset addresses that get registered will resolve |
| 305 | // correctly to the new addresses in the main executable. |
| 306 | |
| 307 | // First we find the original symbol in the .o file's symbol table |
Greg Clayton | 65e364e | 2010-12-07 07:37:38 +0000 | [diff] [blame] | 308 | Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | if (oso_fun_symbol) |
| 310 | { |
| 311 | // If we found the symbol, then we |
| 312 | Section* exe_fun_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); |
| 313 | Section* oso_fun_section = const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); |
| 314 | if (oso_fun_section) |
| 315 | { |
| 316 | // Now we create a section that we will add as a child of the |
| 317 | // section in which the .o symbol (the N_FUN) exists. |
| 318 | |
| 319 | // We use the exe_symbol size because the one in the .o file |
| 320 | // will just be a symbol with no size, and the exe_symbol |
| 321 | // size will reflect any size changes (ppc has been known to |
| 322 | // shrink function sizes when it gets rid of jump islands that |
| 323 | // aren't needed anymore). |
| 324 | SectionSP oso_fun_section_sp (new Section (const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()), |
| 325 | oso_module, // Module (the .o file) |
| 326 | sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs |
Greg Clayton | 65e364e | 2010-12-07 07:37:38 +0000 | [diff] [blame] | 327 | exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated! |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | eSectionTypeDebug, |
| 329 | oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section |
| 330 | exe_symbol->GetByteSize(), // File size (we need the size from the executable) |
| 331 | 0, 0, 0)); |
| 332 | |
| 333 | oso_fun_section_sp->SetLinkedLocation (exe_fun_section, |
| 334 | exe_symbol->GetValue().GetFileAddress() - exe_fun_section->GetFileAddress()); |
| 335 | oso_fun_section->GetChildren().AddSection(oso_fun_section_sp); |
| 336 | comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | break; |
| 341 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 342 | case eSymbolTypeData: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | { |
| 344 | // For each N_GSYM we remap the address for the global by making |
| 345 | // a new section that we add to the sections found in the .o file. |
| 346 | // This new section has the file address set to what the |
| 347 | // addresses are in the .o file, and the load address is adjusted |
| 348 | // to match where it ended up in the final executable! We do this |
| 349 | // before we parse any dwarf info so that when it goes get parsed |
| 350 | // all section/offset addresses that get registered will resolve |
| 351 | // correctly to the new addresses in the main executable. We |
| 352 | // initially set the section size to be 1 byte, but will need to |
| 353 | // fix up these addresses further after all globals have been |
| 354 | // parsed to span the gaps, or we can find the global variable |
| 355 | // sizes from the DWARF info as we are parsing. |
| 356 | |
| 357 | #if 0 |
| 358 | // First we find the non-stab entry that corresponds to the N_GSYM in the executable |
Greg Clayton | 65e364e | 2010-12-07 07:37:38 +0000 | [diff] [blame] | 359 | Symbol *exe_gsym_symbol = exe_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | #else |
| 361 | // The mach-o object file parser already matches up the N_GSYM with with the non-stab |
| 362 | // entry, so we shouldn't have to do that. If this ever changes, enable the code above |
| 363 | // in the "#if 0" block. STSYM's always match the symbol as found below. |
| 364 | Symbol *exe_gsym_symbol = exe_symbol; |
| 365 | #endif |
| 366 | // Next we find the non-stab entry that corresponds to the N_GSYM in the .o file |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 367 | Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny); |
Greg Clayton | 7012049 | 2011-02-08 02:40:32 +0000 | [diff] [blame] | 368 | if (exe_gsym_symbol && oso_gsym_symbol && exe_gsym_symbol->GetAddressRangePtr() && oso_gsym_symbol->GetAddressRangePtr()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 369 | { |
| 370 | // If we found the symbol, then we |
| 371 | Section* exe_gsym_section = const_cast<Section *>(exe_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); |
| 372 | Section* oso_gsym_section = const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); |
| 373 | if (oso_gsym_section) |
| 374 | { |
| 375 | SectionSP oso_gsym_section_sp (new Section (const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()), |
| 376 | oso_module, // Module (the .o file) |
| 377 | sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs |
Greg Clayton | 65e364e | 2010-12-07 07:37:38 +0000 | [diff] [blame] | 378 | exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated! |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | eSectionTypeDebug, |
| 380 | oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section |
| 381 | 1, // We don't know the size of the global, just do the main address for now. |
| 382 | 0, 0, 0)); |
| 383 | |
| 384 | oso_gsym_section_sp->SetLinkedLocation (exe_gsym_section, |
| 385 | exe_gsym_symbol->GetValue().GetFileAddress() - exe_gsym_section->GetFileAddress()); |
| 386 | oso_gsym_section->GetChildren().AddSection(oso_gsym_section_sp); |
| 387 | comp_unit_info->debug_map_sections_sp->AddSection(oso_gsym_section_sp); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | break; |
| 392 | |
| 393 | // case eSymbolTypeStatic: |
| 394 | // { |
| 395 | // // For each N_STSYM we remap the address for the global by making |
| 396 | // // a new section that we add to the sections found in the .o file. |
| 397 | // // This new section has the file address set to what the |
| 398 | // // addresses are in the .o file, and the load address is adjusted |
| 399 | // // to match where it ended up in the final executable! We do this |
| 400 | // // before we parse any dwarf info so that when it goes get parsed |
| 401 | // // all section/offset addresses that get registered will resolve |
| 402 | // // correctly to the new addresses in the main executable. We |
| 403 | // // initially set the section size to be 1 byte, but will need to |
| 404 | // // fix up these addresses further after all globals have been |
| 405 | // // parsed to span the gaps, or we can find the global variable |
| 406 | // // sizes from the DWARF info as we are parsing. |
| 407 | // |
| 408 | // |
| 409 | // Symbol *exe_stsym_symbol = exe_symbol; |
| 410 | // // First we find the non-stab entry that corresponds to the N_STSYM in the .o file |
| 411 | // Symbol *oso_stsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(), eSymbolTypeData); |
| 412 | // if (exe_stsym_symbol && oso_stsym_symbol) |
| 413 | // { |
| 414 | // // If we found the symbol, then we |
| 415 | // Section* exe_stsym_section = const_cast<Section *>(exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); |
| 416 | // Section* oso_stsym_section = const_cast<Section *>(oso_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); |
| 417 | // if (oso_stsym_section) |
| 418 | // { |
| 419 | // // The load address of the symbol will use the section in the |
| 420 | // // executable that contains the debug map that corresponds to |
| 421 | // // the N_FUN symbol. We set the offset to reflect the offset |
| 422 | // // into that section since we are creating a new section. |
| 423 | // AddressRange stsym_load_range(exe_stsym_section, exe_stsym_symbol->GetValue().GetFileAddress() - exe_stsym_section->GetFileAddress(), 1); |
| 424 | // // We need the symbol's section offset address from the .o file, but |
| 425 | // // we need a non-zero size. |
| 426 | // AddressRange stsym_file_range(exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection(), exe_stsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), 1); |
| 427 | // |
| 428 | // // Now we create a section that we will add as a child of the |
| 429 | // // section in which the .o symbol (the N_FUN) exists. |
| 430 | // |
| 431 | //// TODO: mimic what I did for N_FUN if that works... |
| 432 | //// // We use the 1 byte for the size because we don't know the |
| 433 | //// // size of the global symbol without seeing the DWARF. |
| 434 | //// SectionSP oso_fun_section_sp (new Section ( NULL, oso_module, // Module (the .o file) |
| 435 | //// sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs |
| 436 | //// exe_symbol->GetMangled().GetName(),// Name the section the same as the symbol for which is was generated! |
| 437 | //// // &stsym_load_range, // Load offset is the offset into the executable section for the N_FUN from the debug map |
| 438 | //// &stsym_file_range, // File section/offset is just the same os the symbol on the .o file |
| 439 | //// 0, 0, 0)); |
| 440 | //// |
| 441 | //// // Now we add the new section to the .o file's sections as a child |
| 442 | //// // of the section in which the N_SECT symbol exists. |
| 443 | //// oso_stsym_section->GetChildren().AddSection(oso_fun_section_sp); |
| 444 | //// comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp); |
| 445 | // } |
| 446 | // } |
| 447 | // } |
| 448 | // break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | #if defined(DEBUG_OSO_DMAP) |
| 453 | s << "OSO sections after:\n"; |
| 454 | oso_objfile->GetSectionList()->Dump(&s, NULL, true); |
| 455 | #endif |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | if (comp_unit_info->oso_symbol_vendor) |
| 460 | return (SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile(); |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | uint32_t |
| 465 | SymbolFileDWARFDebugMap::GetAbilities () |
| 466 | { |
| 467 | // In order to get the abilities of this plug-in, we look at the list of |
| 468 | // N_OSO entries (object files) from the symbol table and make sure that |
| 469 | // these files exist and also contain valid DWARF. If we get any of that |
| 470 | // then we return the abilities of the first N_OSO's DWARF. |
| 471 | |
| 472 | const uint32_t oso_index_count = GetNumCompileUnits(); |
| 473 | if (oso_index_count > 0) |
| 474 | { |
| 475 | const uint32_t dwarf_abilities = SymbolFile::CompileUnits | |
| 476 | SymbolFile::Functions | |
| 477 | SymbolFile::Blocks | |
| 478 | SymbolFile::GlobalVariables | |
| 479 | SymbolFile::LocalVariables | |
| 480 | SymbolFile::VariableTypes | |
| 481 | SymbolFile::LineTables; |
| 482 | |
| 483 | for (uint32_t oso_idx=0; oso_idx<oso_index_count; ++oso_idx) |
| 484 | { |
| 485 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); |
| 486 | if (oso_dwarf) |
| 487 | { |
| 488 | uint32_t oso_abilities = oso_dwarf->GetAbilities(); |
| 489 | if ((oso_abilities & dwarf_abilities) == dwarf_abilities) |
| 490 | return oso_abilities; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | uint32_t |
| 498 | SymbolFileDWARFDebugMap::GetNumCompileUnits() |
| 499 | { |
| 500 | InitOSO (); |
| 501 | return m_compile_unit_infos.size(); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | CompUnitSP |
| 506 | SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) |
| 507 | { |
| 508 | CompUnitSP comp_unit_sp; |
| 509 | const uint32_t cu_count = GetNumCompileUnits(); |
| 510 | |
| 511 | if (cu_idx < cu_count) |
| 512 | { |
| 513 | if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL) |
| 514 | { |
| 515 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (cu_idx); |
| 516 | if (oso_dwarf) |
| 517 | { |
| 518 | // There is only one compile unit for N_OSO entry right now, so |
| 519 | // it will always exist at index zero. |
| 520 | m_compile_unit_infos[cu_idx].oso_compile_unit_sp = m_compile_unit_infos[cu_idx].oso_symbol_vendor->GetCompileUnitAtIndex (0); |
| 521 | } |
| 522 | |
| 523 | if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL) |
| 524 | { |
| 525 | // We weren't able to get the DWARF for this N_OSO entry (the |
| 526 | // .o file may be missing or not at the specified path), make |
| 527 | // one up as best we can from the debug map. We set the uid |
| 528 | // of the compile unit to the symbol index with the MSBit set |
| 529 | // so that it doesn't collide with any uid values from the DWARF |
| 530 | Symbol *so_symbol = m_compile_unit_infos[cu_idx].so_symbol; |
| 531 | if (so_symbol) |
| 532 | { |
| 533 | m_compile_unit_infos[cu_idx].oso_compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(), |
| 534 | NULL, |
| 535 | so_symbol->GetMangled().GetName().AsCString(), |
| 536 | cu_idx, |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 537 | eLanguageTypeUnknown)); |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 538 | |
| 539 | // Let our symbol vendor know about this compile unit |
| 540 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex (m_compile_unit_infos[cu_idx].oso_compile_unit_sp, |
| 541 | cu_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | comp_unit_sp = m_compile_unit_infos[cu_idx].oso_compile_unit_sp; |
| 546 | } |
| 547 | |
| 548 | return comp_unit_sp; |
| 549 | } |
| 550 | |
| 551 | SymbolFileDWARFDebugMap::CompileUnitInfo * |
| 552 | SymbolFileDWARFDebugMap::GetCompUnitInfo (const SymbolContext& sc) |
| 553 | { |
| 554 | const uint32_t cu_count = GetNumCompileUnits(); |
| 555 | for (uint32_t i=0; i<cu_count; ++i) |
| 556 | { |
| 557 | if (sc.comp_unit == m_compile_unit_infos[i].oso_compile_unit_sp.get()) |
| 558 | return &m_compile_unit_infos[i]; |
| 559 | } |
| 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | size_t |
| 564 | SymbolFileDWARFDebugMap::ParseCompileUnitFunctions (const SymbolContext& sc) |
| 565 | { |
| 566 | SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 567 | if (oso_dwarf) |
| 568 | return oso_dwarf->ParseCompileUnitFunctions (sc); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | bool |
| 573 | SymbolFileDWARFDebugMap::ParseCompileUnitLineTable (const SymbolContext& sc) |
| 574 | { |
| 575 | SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 576 | if (oso_dwarf) |
| 577 | return oso_dwarf->ParseCompileUnitLineTable (sc); |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | bool |
| 582 | SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files) |
| 583 | { |
| 584 | SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 585 | if (oso_dwarf) |
| 586 | return oso_dwarf->ParseCompileUnitSupportFiles (sc, support_files); |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | size_t |
| 592 | SymbolFileDWARFDebugMap::ParseFunctionBlocks (const SymbolContext& sc) |
| 593 | { |
| 594 | SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 595 | if (oso_dwarf) |
| 596 | return oso_dwarf->ParseFunctionBlocks (sc); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | size_t |
| 602 | SymbolFileDWARFDebugMap::ParseTypes (const SymbolContext& sc) |
| 603 | { |
| 604 | SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 605 | if (oso_dwarf) |
| 606 | return oso_dwarf->ParseTypes (sc); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | size_t |
| 612 | SymbolFileDWARFDebugMap::ParseVariablesForContext (const SymbolContext& sc) |
| 613 | { |
| 614 | SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 615 | if (oso_dwarf) |
| 616 | return oso_dwarf->ParseTypes (sc); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | |
| 622 | Type* |
| 623 | SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) |
| 624 | { |
| 625 | return NULL; |
| 626 | } |
| 627 | |
Greg Clayton | 1be10fc | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 628 | lldb::clang_type_t |
| 629 | SymbolFileDWARFDebugMap::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type) |
| 630 | { |
| 631 | // We have a struct/union/class/enum that needs to be fully resolved. |
| 632 | return NULL; |
| 633 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 634 | |
| 635 | uint32_t |
| 636 | SymbolFileDWARFDebugMap::ResolveSymbolContext (const Address& exe_so_addr, uint32_t resolve_scope, SymbolContext& sc) |
| 637 | { |
| 638 | uint32_t resolved_flags = 0; |
| 639 | Symtab* symtab = m_obj_file->GetSymtab(); |
| 640 | if (symtab) |
| 641 | { |
| 642 | const addr_t exe_file_addr = exe_so_addr.GetFileAddress(); |
| 643 | sc.symbol = symtab->FindSymbolContainingFileAddress (exe_file_addr, &m_func_indexes[0], m_func_indexes.size()); |
| 644 | |
| 645 | if (sc.symbol != NULL) |
| 646 | { |
| 647 | resolved_flags |= eSymbolContextSymbol; |
| 648 | |
| 649 | uint32_t oso_idx = 0; |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 650 | CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 651 | if (comp_unit_info) |
| 652 | { |
| 653 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); |
| 654 | ObjectFile *oso_objfile = GetObjectFileByOSOIndex (oso_idx); |
| 655 | if (oso_dwarf && oso_objfile) |
| 656 | { |
| 657 | SectionList *oso_section_list = oso_objfile->GetSectionList(); |
| 658 | |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 659 | SectionSP oso_symbol_section_sp (oso_section_list->FindSectionContainingLinkedFileAddress (exe_file_addr, UINT32_MAX)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 660 | |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 661 | if (oso_symbol_section_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 662 | { |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 663 | const addr_t linked_file_addr = oso_symbol_section_sp->GetLinkedFileAddress(); |
| 664 | Address oso_so_addr (oso_symbol_section_sp.get(), exe_file_addr - linked_file_addr); |
| 665 | if (oso_so_addr.IsSectionOffset()) |
| 666 | resolved_flags |= oso_dwarf->ResolveSymbolContext (oso_so_addr, resolve_scope, sc); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 667 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | return resolved_flags; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | uint32_t |
| 677 | SymbolFileDWARFDebugMap::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) |
| 678 | { |
| 679 | uint32_t initial = sc_list.GetSize(); |
| 680 | const uint32_t cu_count = GetNumCompileUnits(); |
| 681 | |
| 682 | FileSpec so_file_spec; |
| 683 | for (uint32_t i=0; i<cu_count; ++i) |
| 684 | { |
| 685 | if (GetFileSpecForSO (i, so_file_spec)) |
| 686 | { |
| 687 | // By passing false to the comparison we will be able to match |
| 688 | // and files given a filename only. If both file_spec and |
| 689 | // so_file_spec have directories, we will still do a full match. |
| 690 | if (FileSpec::Compare (file_spec, so_file_spec, false) == 0) |
| 691 | { |
| 692 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (i); |
| 693 | |
| 694 | oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | return sc_list.GetSize() - initial; |
| 699 | } |
| 700 | |
| 701 | uint32_t |
| 702 | SymbolFileDWARFDebugMap::PrivateFindGlobalVariables |
| 703 | ( |
| 704 | const ConstString &name, |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 705 | const ClangNamespaceDecl *namespace_decl, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 706 | const std::vector<uint32_t> &indexes, // Indexes into the symbol table that match "name" |
| 707 | uint32_t max_matches, |
| 708 | VariableList& variables |
| 709 | ) |
| 710 | { |
| 711 | const uint32_t original_size = variables.GetSize(); |
| 712 | const size_t match_count = indexes.size(); |
| 713 | for (size_t i=0; i<match_count; ++i) |
| 714 | { |
| 715 | uint32_t oso_idx; |
| 716 | CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithIndex (indexes[i], &oso_idx); |
| 717 | if (comp_unit_info) |
| 718 | { |
| 719 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); |
| 720 | if (oso_dwarf) |
| 721 | { |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 722 | if (oso_dwarf->FindGlobalVariables(name, namespace_decl, true, max_matches, variables)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 723 | if (variables.GetSize() > max_matches) |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | return variables.GetSize() - original_size; |
| 729 | } |
| 730 | |
| 731 | uint32_t |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 732 | SymbolFileDWARFDebugMap::FindGlobalVariables (const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 733 | { |
| 734 | |
| 735 | // If we aren't appending the results to this list, then clear the list |
| 736 | if (!append) |
| 737 | variables.Clear(); |
| 738 | |
| 739 | // Remember how many variables are in the list before we search in case |
| 740 | // we are appending the results to a variable list. |
| 741 | const uint32_t original_size = variables.GetSize(); |
| 742 | |
Greg Clayton | ba2d22d | 2010-11-13 22:57:37 +0000 | [diff] [blame] | 743 | uint32_t total_matches = 0; |
| 744 | SymbolFileDWARF *oso_dwarf; |
| 745 | for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | { |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 747 | const uint32_t oso_matches = oso_dwarf->FindGlobalVariables (name, |
| 748 | namespace_decl, |
Greg Clayton | ba2d22d | 2010-11-13 22:57:37 +0000 | [diff] [blame] | 749 | true, |
| 750 | max_matches, |
| 751 | variables); |
| 752 | if (oso_matches > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 753 | { |
Greg Clayton | ba2d22d | 2010-11-13 22:57:37 +0000 | [diff] [blame] | 754 | total_matches += oso_matches; |
| 755 | |
| 756 | // Are we getting all matches? |
| 757 | if (max_matches == UINT32_MAX) |
| 758 | continue; // Yep, continue getting everything |
| 759 | |
| 760 | // If we have found enough matches, lets get out |
| 761 | if (max_matches >= total_matches) |
| 762 | break; |
| 763 | |
| 764 | // Update the max matches for any subsequent calls to find globals |
| 765 | // in any other object files with DWARF |
| 766 | max_matches -= oso_matches; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | // Return the number of variable that were appended to the list |
| 770 | return variables.GetSize() - original_size; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | uint32_t |
| 775 | SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) |
| 776 | { |
Greg Clayton | ba2d22d | 2010-11-13 22:57:37 +0000 | [diff] [blame] | 777 | // If we aren't appending the results to this list, then clear the list |
| 778 | if (!append) |
| 779 | variables.Clear(); |
| 780 | |
| 781 | // Remember how many variables are in the list before we search in case |
| 782 | // we are appending the results to a variable list. |
| 783 | const uint32_t original_size = variables.GetSize(); |
| 784 | |
| 785 | uint32_t total_matches = 0; |
| 786 | SymbolFileDWARF *oso_dwarf; |
| 787 | for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) |
| 788 | { |
| 789 | const uint32_t oso_matches = oso_dwarf->FindGlobalVariables (regex, |
| 790 | true, |
| 791 | max_matches, |
| 792 | variables); |
| 793 | if (oso_matches > 0) |
| 794 | { |
| 795 | total_matches += oso_matches; |
| 796 | |
| 797 | // Are we getting all matches? |
| 798 | if (max_matches == UINT32_MAX) |
| 799 | continue; // Yep, continue getting everything |
| 800 | |
| 801 | // If we have found enough matches, lets get out |
| 802 | if (max_matches >= total_matches) |
| 803 | break; |
| 804 | |
| 805 | // Update the max matches for any subsequent calls to find globals |
| 806 | // in any other object files with DWARF |
| 807 | max_matches -= oso_matches; |
| 808 | } |
| 809 | } |
| 810 | // Return the number of variable that were appended to the list |
| 811 | return variables.GetSize() - original_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | |
| 815 | int |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 816 | SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 817 | { |
| 818 | const uint32_t symbol_idx = *symbol_idx_ptr; |
| 819 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 820 | if (symbol_idx < comp_unit_info->first_symbol_index) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 821 | return -1; |
| 822 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 823 | if (symbol_idx <= comp_unit_info->last_symbol_index) |
| 824 | return 0; |
| 825 | |
| 826 | return 1; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | int |
| 831 | SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID (user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) |
| 832 | { |
| 833 | const user_id_t symbol_id = *symbol_idx_ptr; |
| 834 | |
| 835 | if (symbol_id < comp_unit_info->so_symbol->GetID()) |
| 836 | return -1; |
| 837 | |
| 838 | if (symbol_id <= comp_unit_info->last_symbol->GetID()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 839 | return 0; |
| 840 | |
| 841 | return 1; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | SymbolFileDWARFDebugMap::CompileUnitInfo* |
| 846 | SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr) |
| 847 | { |
| 848 | const uint32_t oso_index_count = m_compile_unit_infos.size(); |
| 849 | CompileUnitInfo *comp_unit_info = NULL; |
| 850 | if (oso_index_count) |
| 851 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 852 | comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_idx, |
| 853 | &m_compile_unit_infos[0], |
| 854 | m_compile_unit_infos.size(), |
| 855 | sizeof(CompileUnitInfo), |
| 856 | (ComparisonFunction)SymbolContainsSymbolWithIndex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | if (oso_idx_ptr) |
| 860 | { |
| 861 | if (comp_unit_info != NULL) |
| 862 | *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; |
| 863 | else |
| 864 | *oso_idx_ptr = UINT32_MAX; |
| 865 | } |
| 866 | return comp_unit_info; |
| 867 | } |
| 868 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 869 | SymbolFileDWARFDebugMap::CompileUnitInfo* |
| 870 | SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID (user_id_t symbol_id, uint32_t *oso_idx_ptr) |
| 871 | { |
| 872 | const uint32_t oso_index_count = m_compile_unit_infos.size(); |
| 873 | CompileUnitInfo *comp_unit_info = NULL; |
| 874 | if (oso_index_count) |
| 875 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 876 | comp_unit_info = (CompileUnitInfo*)::bsearch (&symbol_id, |
| 877 | &m_compile_unit_infos[0], |
| 878 | m_compile_unit_infos.size(), |
| 879 | sizeof(CompileUnitInfo), |
| 880 | (ComparisonFunction)SymbolContainsSymbolWithID); |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | if (oso_idx_ptr) |
| 884 | { |
| 885 | if (comp_unit_info != NULL) |
| 886 | *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; |
| 887 | else |
| 888 | *oso_idx_ptr = UINT32_MAX; |
| 889 | } |
| 890 | return comp_unit_info; |
| 891 | } |
| 892 | |
| 893 | |
Greg Clayton | 3ef3fc6 | 2010-08-17 23:16:15 +0000 | [diff] [blame] | 894 | static void |
| 895 | RemoveFunctionsWithModuleNotEqualTo (Module *module, SymbolContextList &sc_list, uint32_t start_idx) |
| 896 | { |
| 897 | // We found functions in .o files. Not all functions in the .o files |
| 898 | // will have made it into the final output file. The ones that did |
| 899 | // make it into the final output file will have a section whose module |
| 900 | // matches the module from the ObjectFile for this SymbolFile. When |
| 901 | // the modules don't match, then we have something that was in a |
| 902 | // .o file, but doesn't map to anything in the final executable. |
| 903 | uint32_t i=start_idx; |
| 904 | while (i < sc_list.GetSize()) |
| 905 | { |
| 906 | SymbolContext sc; |
| 907 | sc_list.GetContextAtIndex(i, sc); |
| 908 | if (sc.function) |
| 909 | { |
| 910 | const Section *section = sc.function->GetAddressRange().GetBaseAddress().GetSection(); |
| 911 | if (section->GetModule() != module) |
| 912 | { |
| 913 | sc_list.RemoveContextAtIndex(i); |
| 914 | continue; |
| 915 | } |
| 916 | } |
| 917 | ++i; |
| 918 | } |
| 919 | } |
| 920 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 921 | uint32_t |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 922 | SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool append, SymbolContextList& sc_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 923 | { |
| 924 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 925 | "SymbolFileDWARFDebugMap::FindFunctions (name = %s)", |
| 926 | name.GetCString()); |
| 927 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 928 | uint32_t initial_size = 0; |
| 929 | if (append) |
| 930 | initial_size = sc_list.GetSize(); |
| 931 | else |
| 932 | sc_list.Clear(); |
| 933 | |
Greg Clayton | 57a6b99 | 2010-08-17 00:35:34 +0000 | [diff] [blame] | 934 | uint32_t oso_idx = 0; |
| 935 | SymbolFileDWARF *oso_dwarf; |
| 936 | while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 937 | { |
Greg Clayton | 3ef3fc6 | 2010-08-17 23:16:15 +0000 | [diff] [blame] | 938 | uint32_t sc_idx = sc_list.GetSize(); |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 939 | if (oso_dwarf->FindFunctions(name, namespace_decl, name_type_mask, true, sc_list)) |
Greg Clayton | 3ef3fc6 | 2010-08-17 23:16:15 +0000 | [diff] [blame] | 940 | { |
| 941 | RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx); |
| 942 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | return sc_list.GetSize() - initial_size; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | uint32_t |
| 950 | SymbolFileDWARFDebugMap::FindFunctions (const RegularExpression& regex, bool append, SymbolContextList& sc_list) |
| 951 | { |
| 952 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 953 | "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')", |
| 954 | regex.GetText()); |
| 955 | |
Greg Clayton | 57a6b99 | 2010-08-17 00:35:34 +0000 | [diff] [blame] | 956 | uint32_t initial_size = 0; |
| 957 | if (append) |
| 958 | initial_size = sc_list.GetSize(); |
| 959 | else |
| 960 | sc_list.Clear(); |
| 961 | |
| 962 | uint32_t oso_idx = 0; |
| 963 | SymbolFileDWARF *oso_dwarf; |
| 964 | while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL) |
| 965 | { |
Greg Clayton | 3ef3fc6 | 2010-08-17 23:16:15 +0000 | [diff] [blame] | 966 | uint32_t sc_idx = sc_list.GetSize(); |
| 967 | |
| 968 | if (oso_dwarf->FindFunctions(regex, true, sc_list)) |
| 969 | { |
| 970 | RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx); |
| 971 | } |
Greg Clayton | 57a6b99 | 2010-08-17 00:35:34 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | return sc_list.GetSize() - initial_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Greg Clayton | 2ccf8cf | 2010-11-07 21:02:03 +0000 | [diff] [blame] | 977 | TypeSP |
| 978 | SymbolFileDWARFDebugMap::FindDefinitionTypeForDIE ( |
| 979 | DWARFCompileUnit* cu, |
| 980 | const DWARFDebugInfoEntry *die, |
| 981 | const ConstString &type_name |
| 982 | ) |
| 983 | { |
| 984 | TypeSP type_sp; |
| 985 | SymbolFileDWARF *oso_dwarf; |
| 986 | for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) |
| 987 | { |
| 988 | type_sp = oso_dwarf->FindDefinitionTypeForDIE (cu, die, type_name); |
| 989 | if (type_sp) |
| 990 | break; |
| 991 | } |
| 992 | return type_sp; |
| 993 | } |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 994 | |
| 995 | uint32_t |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 996 | SymbolFileDWARFDebugMap::FindTypes |
| 997 | ( |
| 998 | const SymbolContext& sc, |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 999 | const ConstString &name, |
| 1000 | const ClangNamespaceDecl *namespace_decl, |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 1001 | bool append, |
| 1002 | uint32_t max_matches, |
| 1003 | TypeList& types |
| 1004 | ) |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1005 | { |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1006 | if (!append) |
| 1007 | types.Clear(); |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 1008 | |
| 1009 | const uint32_t initial_types_size = types.GetSize(); |
| 1010 | SymbolFileDWARF *oso_dwarf; |
| 1011 | |
| 1012 | if (sc.comp_unit) |
| 1013 | { |
| 1014 | oso_dwarf = GetSymbolFile (sc); |
| 1015 | if (oso_dwarf) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 1016 | return oso_dwarf->FindTypes (sc, name, namespace_decl, append, max_matches, types); |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 1017 | } |
| 1018 | else |
| 1019 | { |
| 1020 | uint32_t oso_idx = 0; |
| 1021 | while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 1022 | oso_dwarf->FindTypes (sc, name, namespace_decl, append, max_matches, types); |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | return types.GetSize() - initial_types_size; |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1028 | // |
| 1029 | //uint32_t |
| 1030 | //SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, lldb::user_id_t udt_uid, TypeList& types) |
| 1031 | //{ |
| 1032 | // SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); |
| 1033 | // if (oso_dwarf) |
| 1034 | // return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types); |
| 1035 | // return 0; |
| 1036 | //} |
| 1037 | |
Greg Clayton | 96d7d74 | 2010-11-10 23:42:09 +0000 | [diff] [blame] | 1038 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 1039 | ClangNamespaceDecl |
Greg Clayton | 96d7d74 | 2010-11-10 23:42:09 +0000 | [diff] [blame] | 1040 | SymbolFileDWARFDebugMap::FindNamespace (const lldb_private::SymbolContext& sc, |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 1041 | const lldb_private::ConstString &name, |
| 1042 | const ClangNamespaceDecl *parent_namespace_decl) |
Greg Clayton | 96d7d74 | 2010-11-10 23:42:09 +0000 | [diff] [blame] | 1043 | { |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 1044 | ClangNamespaceDecl matching_namespace; |
Greg Clayton | 96d7d74 | 2010-11-10 23:42:09 +0000 | [diff] [blame] | 1045 | SymbolFileDWARF *oso_dwarf; |
| 1046 | |
| 1047 | if (sc.comp_unit) |
| 1048 | { |
| 1049 | oso_dwarf = GetSymbolFile (sc); |
| 1050 | if (oso_dwarf) |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 1051 | matching_namespace = oso_dwarf->FindNamespace (sc, name, parent_namespace_decl); |
Greg Clayton | 96d7d74 | 2010-11-10 23:42:09 +0000 | [diff] [blame] | 1052 | } |
| 1053 | else |
| 1054 | { |
| 1055 | for (uint32_t oso_idx = 0; |
| 1056 | ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); |
| 1057 | ++oso_idx) |
| 1058 | { |
Sean Callanan | 213fdb8 | 2011-10-13 01:49:10 +0000 | [diff] [blame^] | 1059 | matching_namespace = oso_dwarf->FindNamespace (sc, name, parent_namespace_decl); |
Greg Clayton | 96d7d74 | 2010-11-10 23:42:09 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (matching_namespace) |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return matching_namespace; |
| 1067 | } |
| 1068 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1069 | //------------------------------------------------------------------ |
| 1070 | // PluginInterface protocol |
| 1071 | //------------------------------------------------------------------ |
| 1072 | const char * |
| 1073 | SymbolFileDWARFDebugMap::GetPluginName() |
| 1074 | { |
| 1075 | return "SymbolFileDWARFDebugMap"; |
| 1076 | } |
| 1077 | |
| 1078 | const char * |
| 1079 | SymbolFileDWARFDebugMap::GetShortPluginName() |
| 1080 | { |
| 1081 | return GetPluginNameStatic(); |
| 1082 | } |
| 1083 | |
| 1084 | uint32_t |
| 1085 | SymbolFileDWARFDebugMap::GetPluginVersion() |
| 1086 | { |
| 1087 | return 1; |
| 1088 | } |
| 1089 | |
| 1090 | void |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 1091 | SymbolFileDWARFDebugMap::SetCompileUnit (SymbolFileDWARF *oso_dwarf, const CompUnitSP &cu_sp) |
| 1092 | { |
| 1093 | const uint32_t cu_count = GetNumCompileUnits(); |
| 1094 | for (uint32_t i=0; i<cu_count; ++i) |
| 1095 | { |
| 1096 | if (m_compile_unit_infos[i].oso_symbol_vendor && |
| 1097 | m_compile_unit_infos[i].oso_symbol_vendor->GetSymbolFile() == oso_dwarf) |
| 1098 | { |
| 1099 | if (m_compile_unit_infos[i].oso_compile_unit_sp) |
| 1100 | { |
| 1101 | assert (m_compile_unit_infos[i].oso_compile_unit_sp.get() == cu_sp.get()); |
| 1102 | } |
| 1103 | else |
| 1104 | { |
| 1105 | m_compile_unit_infos[i].oso_compile_unit_sp = cu_sp; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1111 | |
| 1112 | void |
| 1113 | SymbolFileDWARFDebugMap::CompleteTagDecl (void *baton, clang::TagDecl *decl) |
| 1114 | { |
| 1115 | SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton; |
| 1116 | clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); |
| 1117 | if (clang_type) |
| 1118 | { |
| 1119 | SymbolFileDWARF *oso_dwarf; |
| 1120 | |
| 1121 | for (uint32_t oso_idx = 0; ((oso_dwarf = symbol_file_dwarf->GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) |
| 1122 | { |
| 1123 | if (oso_dwarf->HasForwardDeclForClangType (clang_type)) |
| 1124 | { |
| 1125 | oso_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); |
| 1126 | return; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | void |
| 1133 | SymbolFileDWARFDebugMap::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl) |
| 1134 | { |
| 1135 | SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton; |
| 1136 | clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); |
| 1137 | if (clang_type) |
| 1138 | { |
| 1139 | SymbolFileDWARF *oso_dwarf; |
| 1140 | |
| 1141 | for (uint32_t oso_idx = 0; ((oso_dwarf = symbol_file_dwarf->GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) |
| 1142 | { |
| 1143 | if (oso_dwarf->HasForwardDeclForClangType (clang_type)) |
| 1144 | { |
| 1145 | oso_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); |
| 1146 | return; |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |