1 //===-- SBModule.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBModule.h"
10 #include "lldb/Utility/ReproducerInstrumentation.h"
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBModuleSpec.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBSymbolContextList.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Core/ValueObjectList.h"
20 #include "lldb/Core/ValueObjectVariable.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Symbol/SymbolFile.h"
23 #include "lldb/Symbol/Symtab.h"
24 #include "lldb/Symbol/TypeSystem.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/StreamString.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 SBModule::SBModule() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBModule); }
33 
34 SBModule::SBModule(const lldb::ModuleSP &module_sp) : m_opaque_sp(module_sp) {}
35 
36 SBModule::SBModule(const SBModuleSpec &module_spec) {
37   LLDB_RECORD_CONSTRUCTOR(SBModule, (const lldb::SBModuleSpec &), module_spec);
38 
39   ModuleSP module_sp;
40   Status error = ModuleList::GetSharedModule(
41       *module_spec.m_opaque_up, module_sp, nullptr, nullptr, nullptr);
42   if (module_sp)
43     SetSP(module_sp);
44 }
45 
46 SBModule::SBModule(const SBModule &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
47   LLDB_RECORD_CONSTRUCTOR(SBModule, (const lldb::SBModule &), rhs);
48 }
49 
50 SBModule::SBModule(lldb::SBProcess &process, lldb::addr_t header_addr) {
51   LLDB_RECORD_CONSTRUCTOR(SBModule, (lldb::SBProcess &, lldb::addr_t), process,
52                           header_addr);
53 
54   ProcessSP process_sp(process.GetSP());
55   if (process_sp) {
56     m_opaque_sp = process_sp->ReadModuleFromMemory(FileSpec(), header_addr);
57     if (m_opaque_sp) {
58       Target &target = process_sp->GetTarget();
59       bool changed = false;
60       m_opaque_sp->SetLoadAddress(target, 0, true, changed);
61       target.GetImages().Append(m_opaque_sp);
62     }
63   }
64 }
65 
66 const SBModule &SBModule::operator=(const SBModule &rhs) {
67   LLDB_RECORD_METHOD(const lldb::SBModule &, SBModule, operator=,
68                      (const lldb::SBModule &), rhs);
69 
70   if (this != &rhs)
71     m_opaque_sp = rhs.m_opaque_sp;
72   return *this;
73 }
74 
75 SBModule::~SBModule() = default;
76 
77 bool SBModule::IsValid() const {
78   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModule, IsValid);
79   return this->operator bool();
80 }
81 SBModule::operator bool() const {
82   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBModule, operator bool);
83 
84   return m_opaque_sp.get() != nullptr;
85 }
86 
87 void SBModule::Clear() {
88   LLDB_RECORD_METHOD_NO_ARGS(void, SBModule, Clear);
89 
90   m_opaque_sp.reset();
91 }
92 
93 SBFileSpec SBModule::GetFileSpec() const {
94   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule, GetFileSpec);
95 
96   SBFileSpec file_spec;
97   ModuleSP module_sp(GetSP());
98   if (module_sp)
99     file_spec.SetFileSpec(module_sp->GetFileSpec());
100 
101   return file_spec;
102 }
103 
104 lldb::SBFileSpec SBModule::GetPlatformFileSpec() const {
105   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule,
106                                    GetPlatformFileSpec);
107 
108   SBFileSpec file_spec;
109   ModuleSP module_sp(GetSP());
110   if (module_sp)
111     file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
112 
113   return file_spec;
114 }
115 
116 bool SBModule::SetPlatformFileSpec(const lldb::SBFileSpec &platform_file) {
117   LLDB_RECORD_METHOD(bool, SBModule, SetPlatformFileSpec,
118                      (const lldb::SBFileSpec &), platform_file);
119 
120   bool result = false;
121 
122   ModuleSP module_sp(GetSP());
123   if (module_sp) {
124     module_sp->SetPlatformFileSpec(*platform_file);
125     result = true;
126   }
127 
128   return result;
129 }
130 
131 lldb::SBFileSpec SBModule::GetRemoteInstallFileSpec() {
132   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBModule,
133                              GetRemoteInstallFileSpec);
134 
135   SBFileSpec sb_file_spec;
136   ModuleSP module_sp(GetSP());
137   if (module_sp)
138     sb_file_spec.SetFileSpec(module_sp->GetRemoteInstallFileSpec());
139   return sb_file_spec;
140 }
141 
142 bool SBModule::SetRemoteInstallFileSpec(lldb::SBFileSpec &file) {
143   LLDB_RECORD_METHOD(bool, SBModule, SetRemoteInstallFileSpec,
144                      (lldb::SBFileSpec &), file);
145 
146   ModuleSP module_sp(GetSP());
147   if (module_sp) {
148     module_sp->SetRemoteInstallFileSpec(file.ref());
149     return true;
150   }
151   return false;
152 }
153 
154 const uint8_t *SBModule::GetUUIDBytes() const {
155   LLDB_RECORD_METHOD_CONST_NO_ARGS(const uint8_t *, SBModule, GetUUIDBytes);
156 
157   const uint8_t *uuid_bytes = nullptr;
158   ModuleSP module_sp(GetSP());
159   if (module_sp)
160     uuid_bytes = module_sp->GetUUID().GetBytes().data();
161 
162   return uuid_bytes;
163 }
164 
165 const char *SBModule::GetUUIDString() const {
166   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBModule, GetUUIDString);
167 
168   const char *uuid_cstr = nullptr;
169   ModuleSP module_sp(GetSP());
170   if (module_sp) {
171     // We are going to return a "const char *" value through the public API, so
172     // we need to constify it so it gets added permanently the string pool and
173     // then we don't need to worry about the lifetime of the string as it will
174     // never go away once it has been put into the ConstString string pool
175     uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString();
176   }
177 
178   if (uuid_cstr && uuid_cstr[0]) {
179     return uuid_cstr;
180   }
181 
182   return nullptr;
183 }
184 
185 bool SBModule::operator==(const SBModule &rhs) const {
186   LLDB_RECORD_METHOD_CONST(bool, SBModule, operator==, (const lldb::SBModule &),
187                            rhs);
188 
189   if (m_opaque_sp)
190     return m_opaque_sp.get() == rhs.m_opaque_sp.get();
191   return false;
192 }
193 
194 bool SBModule::operator!=(const SBModule &rhs) const {
195   LLDB_RECORD_METHOD_CONST(bool, SBModule, operator!=, (const lldb::SBModule &),
196                            rhs);
197 
198   if (m_opaque_sp)
199     return m_opaque_sp.get() != rhs.m_opaque_sp.get();
200   return false;
201 }
202 
203 ModuleSP SBModule::GetSP() const { return m_opaque_sp; }
204 
205 void SBModule::SetSP(const ModuleSP &module_sp) { m_opaque_sp = module_sp; }
206 
207 SBAddress SBModule::ResolveFileAddress(lldb::addr_t vm_addr) {
208   LLDB_RECORD_METHOD(lldb::SBAddress, SBModule, ResolveFileAddress,
209                      (lldb::addr_t), vm_addr);
210 
211   lldb::SBAddress sb_addr;
212   ModuleSP module_sp(GetSP());
213   if (module_sp) {
214     Address addr;
215     if (module_sp->ResolveFileAddress(vm_addr, addr))
216       sb_addr.ref() = addr;
217   }
218   return sb_addr;
219 }
220 
221 SBSymbolContext
222 SBModule::ResolveSymbolContextForAddress(const SBAddress &addr,
223                                          uint32_t resolve_scope) {
224   LLDB_RECORD_METHOD(lldb::SBSymbolContext, SBModule,
225                      ResolveSymbolContextForAddress,
226                      (const lldb::SBAddress &, uint32_t), addr, resolve_scope);
227 
228   SBSymbolContext sb_sc;
229   ModuleSP module_sp(GetSP());
230   SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
231   if (module_sp && addr.IsValid())
232     module_sp->ResolveSymbolContextForAddress(addr.ref(), scope, *sb_sc);
233   return sb_sc;
234 }
235 
236 bool SBModule::GetDescription(SBStream &description) {
237   LLDB_RECORD_METHOD(bool, SBModule, GetDescription, (lldb::SBStream &),
238                      description);
239 
240   Stream &strm = description.ref();
241 
242   ModuleSP module_sp(GetSP());
243   if (module_sp) {
244     module_sp->GetDescription(strm.AsRawOstream());
245   } else
246     strm.PutCString("No value");
247 
248   return true;
249 }
250 
251 uint32_t SBModule::GetNumCompileUnits() {
252   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBModule, GetNumCompileUnits);
253 
254   ModuleSP module_sp(GetSP());
255   if (module_sp) {
256     return module_sp->GetNumCompileUnits();
257   }
258   return 0;
259 }
260 
261 SBCompileUnit SBModule::GetCompileUnitAtIndex(uint32_t index) {
262   LLDB_RECORD_METHOD(lldb::SBCompileUnit, SBModule, GetCompileUnitAtIndex,
263                      (uint32_t), index);
264 
265   SBCompileUnit sb_cu;
266   ModuleSP module_sp(GetSP());
267   if (module_sp) {
268     CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(index);
269     sb_cu.reset(cu_sp.get());
270   }
271   return sb_cu;
272 }
273 
274 SBSymbolContextList SBModule::FindCompileUnits(const SBFileSpec &sb_file_spec) {
275   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindCompileUnits,
276                      (const lldb::SBFileSpec &), sb_file_spec);
277 
278   SBSymbolContextList sb_sc_list;
279   const ModuleSP module_sp(GetSP());
280   if (sb_file_spec.IsValid() && module_sp) {
281     module_sp->FindCompileUnits(*sb_file_spec, *sb_sc_list);
282   }
283   return sb_sc_list;
284 }
285 
286 static Symtab *GetUnifiedSymbolTable(const lldb::ModuleSP &module_sp) {
287   if (module_sp)
288     return module_sp->GetSymtab();
289   return nullptr;
290 }
291 
292 size_t SBModule::GetNumSymbols() {
293   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModule, GetNumSymbols);
294 
295   ModuleSP module_sp(GetSP());
296   if (Symtab *symtab = GetUnifiedSymbolTable(module_sp))
297     return symtab->GetNumSymbols();
298   return 0;
299 }
300 
301 SBSymbol SBModule::GetSymbolAtIndex(size_t idx) {
302   LLDB_RECORD_METHOD(lldb::SBSymbol, SBModule, GetSymbolAtIndex, (size_t), idx);
303 
304   SBSymbol sb_symbol;
305   ModuleSP module_sp(GetSP());
306   Symtab *symtab = GetUnifiedSymbolTable(module_sp);
307   if (symtab)
308     sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx));
309   return sb_symbol;
310 }
311 
312 lldb::SBSymbol SBModule::FindSymbol(const char *name,
313                                     lldb::SymbolType symbol_type) {
314   LLDB_RECORD_METHOD(lldb::SBSymbol, SBModule, FindSymbol,
315                      (const char *, lldb::SymbolType), name, symbol_type);
316 
317   SBSymbol sb_symbol;
318   if (name && name[0]) {
319     ModuleSP module_sp(GetSP());
320     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
321     if (symtab)
322       sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(
323           ConstString(name), symbol_type, Symtab::eDebugAny,
324           Symtab::eVisibilityAny));
325   }
326   return sb_symbol;
327 }
328 
329 lldb::SBSymbolContextList SBModule::FindSymbols(const char *name,
330                                                 lldb::SymbolType symbol_type) {
331   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindSymbols,
332                      (const char *, lldb::SymbolType), name, symbol_type);
333 
334   SBSymbolContextList sb_sc_list;
335   if (name && name[0]) {
336     ModuleSP module_sp(GetSP());
337     Symtab *symtab = GetUnifiedSymbolTable(module_sp);
338     if (symtab) {
339       std::vector<uint32_t> matching_symbol_indexes;
340       symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type,
341                                             matching_symbol_indexes);
342       const size_t num_matches = matching_symbol_indexes.size();
343       if (num_matches) {
344         SymbolContext sc;
345         sc.module_sp = module_sp;
346         SymbolContextList &sc_list = *sb_sc_list;
347         for (size_t i = 0; i < num_matches; ++i) {
348           sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]);
349           if (sc.symbol)
350             sc_list.Append(sc);
351         }
352       }
353     }
354   }
355   return sb_sc_list;
356 }
357 
358 size_t SBModule::GetNumSections() {
359   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBModule, GetNumSections);
360 
361   ModuleSP module_sp(GetSP());
362   if (module_sp) {
363     // Give the symbol vendor a chance to add to the unified section list.
364     module_sp->GetSymbolFile();
365     SectionList *section_list = module_sp->GetSectionList();
366     if (section_list)
367       return section_list->GetSize();
368   }
369   return 0;
370 }
371 
372 SBSection SBModule::GetSectionAtIndex(size_t idx) {
373   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, GetSectionAtIndex, (size_t),
374                      idx);
375 
376   SBSection sb_section;
377   ModuleSP module_sp(GetSP());
378   if (module_sp) {
379     // Give the symbol vendor a chance to add to the unified section list.
380     module_sp->GetSymbolFile();
381     SectionList *section_list = module_sp->GetSectionList();
382 
383     if (section_list)
384       sb_section.SetSP(section_list->GetSectionAtIndex(idx));
385   }
386   return sb_section;
387 }
388 
389 lldb::SBSymbolContextList SBModule::FindFunctions(const char *name,
390                                                   uint32_t name_type_mask) {
391   LLDB_RECORD_METHOD(lldb::SBSymbolContextList, SBModule, FindFunctions,
392                      (const char *, uint32_t), name, name_type_mask);
393 
394   lldb::SBSymbolContextList sb_sc_list;
395   ModuleSP module_sp(GetSP());
396   if (name && module_sp) {
397 
398     ModuleFunctionSearchOptions function_options;
399     function_options.include_symbols = true;
400     function_options.include_inlines = true;
401     FunctionNameType type = static_cast<FunctionNameType>(name_type_mask);
402     module_sp->FindFunctions(ConstString(name), CompilerDeclContext(), type,
403                              function_options, *sb_sc_list);
404   }
405   return sb_sc_list;
406 }
407 
408 SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name,
409                                           uint32_t max_matches) {
410   LLDB_RECORD_METHOD(lldb::SBValueList, SBModule, FindGlobalVariables,
411                      (lldb::SBTarget &, const char *, uint32_t), target, name,
412                      max_matches);
413 
414   SBValueList sb_value_list;
415   ModuleSP module_sp(GetSP());
416   if (name && module_sp) {
417     VariableList variable_list;
418     module_sp->FindGlobalVariables(ConstString(name), CompilerDeclContext(),
419                                    max_matches, variable_list);
420     for (const VariableSP &var_sp : variable_list) {
421       lldb::ValueObjectSP valobj_sp;
422       TargetSP target_sp(target.GetSP());
423       valobj_sp = ValueObjectVariable::Create(target_sp.get(), var_sp);
424       if (valobj_sp)
425         sb_value_list.Append(SBValue(valobj_sp));
426     }
427   }
428 
429   return sb_value_list;
430 }
431 
432 lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target,
433                                                 const char *name) {
434   LLDB_RECORD_METHOD(lldb::SBValue, SBModule, FindFirstGlobalVariable,
435                      (lldb::SBTarget &, const char *), target, name);
436 
437   SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
438   if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
439     return sb_value_list.GetValueAtIndex(0);
440   return SBValue();
441 }
442 
443 lldb::SBType SBModule::FindFirstType(const char *name_cstr) {
444   LLDB_RECORD_METHOD(lldb::SBType, SBModule, FindFirstType, (const char *),
445                      name_cstr);
446 
447   SBType sb_type;
448   ModuleSP module_sp(GetSP());
449   if (name_cstr && module_sp) {
450     SymbolContext sc;
451     const bool exact_match = false;
452     ConstString name(name_cstr);
453 
454     sb_type = SBType(module_sp->FindFirstType(sc, name, exact_match));
455 
456     if (!sb_type.IsValid()) {
457       auto type_system_or_err =
458           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
459       if (auto err = type_system_or_err.takeError()) {
460         llvm::consumeError(std::move(err));
461         return SBType();
462       }
463       sb_type = SBType(type_system_or_err->GetBuiltinTypeByName(name));
464     }
465   }
466   return sb_type;
467 }
468 
469 lldb::SBType SBModule::GetBasicType(lldb::BasicType type) {
470   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetBasicType, (lldb::BasicType),
471                      type);
472 
473   ModuleSP module_sp(GetSP());
474   if (module_sp) {
475     auto type_system_or_err =
476         module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
477     if (auto err = type_system_or_err.takeError()) {
478       llvm::consumeError(std::move(err));
479     } else {
480       return SBType(type_system_or_err->GetBasicTypeFromAST(type));
481     }
482   }
483   return SBType();
484 }
485 
486 lldb::SBTypeList SBModule::FindTypes(const char *type) {
487   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, FindTypes, (const char *),
488                      type);
489 
490   SBTypeList retval;
491 
492   ModuleSP module_sp(GetSP());
493   if (type && module_sp) {
494     TypeList type_list;
495     const bool exact_match = false;
496     ConstString name(type);
497     llvm::DenseSet<SymbolFile *> searched_symbol_files;
498     module_sp->FindTypes(name, exact_match, UINT32_MAX, searched_symbol_files,
499                          type_list);
500 
501     if (type_list.Empty()) {
502       auto type_system_or_err =
503           module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
504       if (auto err = type_system_or_err.takeError()) {
505         llvm::consumeError(std::move(err));
506       } else {
507         CompilerType compiler_type =
508             type_system_or_err->GetBuiltinTypeByName(name);
509         if (compiler_type)
510           retval.Append(SBType(compiler_type));
511       }
512     } else {
513       for (size_t idx = 0; idx < type_list.GetSize(); idx++) {
514         TypeSP type_sp(type_list.GetTypeAtIndex(idx));
515         if (type_sp)
516           retval.Append(SBType(type_sp));
517       }
518     }
519   }
520   return retval;
521 }
522 
523 lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) {
524   LLDB_RECORD_METHOD(lldb::SBType, SBModule, GetTypeByID, (lldb::user_id_t),
525                      uid);
526 
527   ModuleSP module_sp(GetSP());
528   if (module_sp) {
529     if (SymbolFile *symfile = module_sp->GetSymbolFile()) {
530       Type *type_ptr = symfile->ResolveTypeUID(uid);
531       if (type_ptr)
532         return SBType(type_ptr->shared_from_this());
533     }
534   }
535   return SBType();
536 }
537 
538 lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) {
539   LLDB_RECORD_METHOD(lldb::SBTypeList, SBModule, GetTypes, (uint32_t),
540                      type_mask);
541 
542   SBTypeList sb_type_list;
543 
544   ModuleSP module_sp(GetSP());
545   if (!module_sp)
546     return sb_type_list;
547   SymbolFile *symfile = module_sp->GetSymbolFile();
548   if (!symfile)
549     return sb_type_list;
550 
551   TypeClass type_class = static_cast<TypeClass>(type_mask);
552   TypeList type_list;
553   symfile->GetTypes(nullptr, type_class, type_list);
554   sb_type_list.m_opaque_up->Append(type_list);
555   return sb_type_list;
556 }
557 
558 SBSection SBModule::FindSection(const char *sect_name) {
559   LLDB_RECORD_METHOD(lldb::SBSection, SBModule, FindSection, (const char *),
560                      sect_name);
561 
562   SBSection sb_section;
563 
564   ModuleSP module_sp(GetSP());
565   if (sect_name && module_sp) {
566     // Give the symbol vendor a chance to add to the unified section list.
567     module_sp->GetSymbolFile();
568     SectionList *section_list = module_sp->GetSectionList();
569     if (section_list) {
570       ConstString const_sect_name(sect_name);
571       SectionSP section_sp(section_list->FindSectionByName(const_sect_name));
572       if (section_sp) {
573         sb_section.SetSP(section_sp);
574       }
575     }
576   }
577   return sb_section;
578 }
579 
580 lldb::ByteOrder SBModule::GetByteOrder() {
581   LLDB_RECORD_METHOD_NO_ARGS(lldb::ByteOrder, SBModule, GetByteOrder);
582 
583   ModuleSP module_sp(GetSP());
584   if (module_sp)
585     return module_sp->GetArchitecture().GetByteOrder();
586   return eByteOrderInvalid;
587 }
588 
589 const char *SBModule::GetTriple() {
590   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBModule, GetTriple);
591 
592   ModuleSP module_sp(GetSP());
593   if (module_sp) {
594     std::string triple(module_sp->GetArchitecture().GetTriple().str());
595     // Unique the string so we don't run into ownership issues since the const
596     // strings put the string into the string pool once and the strings never
597     // comes out
598     ConstString const_triple(triple.c_str());
599     return const_triple.GetCString();
600   }
601   return nullptr;
602 }
603 
604 uint32_t SBModule::GetAddressByteSize() {
605   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBModule, GetAddressByteSize);
606 
607   ModuleSP module_sp(GetSP());
608   if (module_sp)
609     return module_sp->GetArchitecture().GetAddressByteSize();
610   return sizeof(void *);
611 }
612 
613 uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) {
614   LLDB_RECORD_METHOD(uint32_t, SBModule, GetVersion, (uint32_t *, uint32_t),
615                      versions, num_versions);
616 
617   llvm::VersionTuple version;
618   if (ModuleSP module_sp = GetSP())
619     version = module_sp->GetVersion();
620   uint32_t result = 0;
621   if (!version.empty())
622     ++result;
623   if (version.getMinor())
624     ++result;
625   if (version.getSubminor())
626     ++result;
627 
628   if (!versions)
629     return result;
630 
631   if (num_versions > 0)
632     versions[0] = version.empty() ? UINT32_MAX : version.getMajor();
633   if (num_versions > 1)
634     versions[1] = version.getMinor().getValueOr(UINT32_MAX);
635   if (num_versions > 2)
636     versions[2] = version.getSubminor().getValueOr(UINT32_MAX);
637   for (uint32_t i = 3; i < num_versions; ++i)
638     versions[i] = UINT32_MAX;
639   return result;
640 }
641 
642 lldb::SBFileSpec SBModule::GetSymbolFileSpec() const {
643   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBModule,
644                                    GetSymbolFileSpec);
645 
646   lldb::SBFileSpec sb_file_spec;
647   ModuleSP module_sp(GetSP());
648   if (module_sp) {
649     if (SymbolFile *symfile = module_sp->GetSymbolFile())
650       sb_file_spec.SetFileSpec(symfile->GetObjectFile()->GetFileSpec());
651   }
652   return sb_file_spec;
653 }
654 
655 lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const {
656   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBModule,
657                                    GetObjectFileHeaderAddress);
658 
659   lldb::SBAddress sb_addr;
660   ModuleSP module_sp(GetSP());
661   if (module_sp) {
662     ObjectFile *objfile_ptr = module_sp->GetObjectFile();
663     if (objfile_ptr)
664       sb_addr.ref() = objfile_ptr->GetBaseAddress();
665   }
666   return sb_addr;
667 }
668 
669 lldb::SBAddress SBModule::GetObjectFileEntryPointAddress() const {
670   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBModule,
671                                    GetObjectFileEntryPointAddress);
672 
673   lldb::SBAddress sb_addr;
674   ModuleSP module_sp(GetSP());
675   if (module_sp) {
676     ObjectFile *objfile_ptr = module_sp->GetObjectFile();
677     if (objfile_ptr)
678       sb_addr.ref() = objfile_ptr->GetEntryPointAddress();
679   }
680   return sb_addr;
681 }
682 
683 uint32_t SBModule::GetNumberAllocatedModules() {
684   LLDB_RECORD_STATIC_METHOD_NO_ARGS(uint32_t, SBModule,
685                                     GetNumberAllocatedModules);
686 
687   return Module::GetNumberAllocatedModules();
688 }
689 
690 void SBModule::GarbageCollectAllocatedModules() {
691   LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBModule,
692                                     GarbageCollectAllocatedModules);
693 
694   const bool mandatory = false;
695   ModuleList::RemoveOrphanSharedModules(mandatory);
696 }
697