Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:53

0001 //===-- DynamicLoader.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_TARGET_DYNAMICLOADER_H
0010 #define LLDB_TARGET_DYNAMICLOADER_H
0011 
0012 #include "lldb/Core/Address.h"
0013 #include "lldb/Core/PluginInterface.h"
0014 #include "lldb/Target/CoreFileMemoryRanges.h"
0015 #include "lldb/Utility/FileSpec.h"
0016 #include "lldb/Utility/Status.h"
0017 #include "lldb/Utility/UUID.h"
0018 #include "lldb/lldb-defines.h"
0019 #include "lldb/lldb-forward.h"
0020 #include "lldb/lldb-private-enumerations.h"
0021 #include "lldb/lldb-types.h"
0022 
0023 #include <cstddef>
0024 #include <cstdint>
0025 namespace lldb_private {
0026 class ModuleList;
0027 class Process;
0028 class SectionList;
0029 class Symbol;
0030 class SymbolContext;
0031 class SymbolContextList;
0032 class Thread;
0033 }
0034 
0035 namespace lldb_private {
0036 
0037 /// \class DynamicLoader DynamicLoader.h "lldb/Target/DynamicLoader.h"
0038 /// A plug-in interface definition class for dynamic loaders.
0039 ///
0040 /// Dynamic loader plug-ins track image (shared library) loading and
0041 /// unloading. The class is initialized given a live process that is halted at
0042 /// its entry point or just after attaching.
0043 ///
0044 /// Dynamic loader plug-ins can track the process by registering callbacks
0045 /// using the: Process::RegisterNotificationCallbacks (const Notifications&)
0046 /// function.
0047 ///
0048 /// Breakpoints can also be set in the process which can register functions
0049 /// that get called using: Process::BreakpointSetCallback (lldb::user_id_t,
0050 /// BreakpointHitCallback, void *). These breakpoint callbacks return a
0051 /// boolean value that indicates if the process should continue or halt and
0052 /// should return the global setting for this using:
0053 /// DynamicLoader::StopWhenImagesChange() const.
0054 class DynamicLoader : public PluginInterface {
0055 public:
0056   /// Find a dynamic loader plugin for a given process.
0057   ///
0058   /// Scans the installed DynamicLoader plug-ins and tries to find an instance
0059   /// that can be used to track image changes in \a process.
0060   ///
0061   /// \param[in] process
0062   ///     The process for which to try and locate a dynamic loader
0063   ///     plug-in instance.
0064   ///
0065   /// \param[in] plugin_name
0066   ///     An optional name of a specific dynamic loader plug-in that
0067   ///     should be used. If empty, pick the best plug-in.
0068   static DynamicLoader *FindPlugin(Process *process,
0069                                    llvm::StringRef plugin_name);
0070 
0071   /// Construct with a process.
0072   DynamicLoader(Process *process);
0073 
0074   /// Called after attaching a process.
0075   ///
0076   /// Allow DynamicLoader plug-ins to execute some code after attaching to a
0077   /// process.
0078   virtual void DidAttach() = 0;
0079 
0080   /// Called after launching a process.
0081   ///
0082   /// Allow DynamicLoader plug-ins to execute some code after the process has
0083   /// stopped for the first time on launch.
0084   virtual void DidLaunch() = 0;
0085 
0086   /// Helper function that can be used to detect when a process has called
0087   /// exec and is now a new and different process. This can be called when
0088   /// necessary to try and detect the exec. The process might be able to
0089   /// answer this question, but sometimes it might not be able and the dynamic
0090   /// loader often knows what the program entry point is. So the process and
0091   /// the dynamic loader can work together to detect this.
0092   virtual bool ProcessDidExec() { return false; }
0093   /// Get whether the process should stop when images change.
0094   ///
0095   /// When images (executables and shared libraries) get loaded or unloaded,
0096   /// often debug sessions will want to try and resolve or unresolve
0097   /// breakpoints that are set in these images. Any breakpoints set by
0098   /// DynamicLoader plug-in instances should return this value to ensure
0099   /// consistent debug session behaviour.
0100   ///
0101   /// \return
0102   ///     Returns \b true if the process should stop when images
0103   ///     change, \b false if the process should resume.
0104   bool GetStopWhenImagesChange() const;
0105 
0106   /// Set whether the process should stop when images change.
0107   ///
0108   /// When images (executables and shared libraries) get loaded or unloaded,
0109   /// often debug sessions will want to try and resolve or unresolve
0110   /// breakpoints that are set in these images. The default is set so that the
0111   /// process stops when images change, but this can be overridden using this
0112   /// function callback.
0113   ///
0114   /// \param[in] stop
0115   ///     Boolean value that indicates whether the process should stop
0116   ///     when images change.
0117   void SetStopWhenImagesChange(bool stop);
0118 
0119   /// Provides a plan to step through the dynamic loader trampoline for the
0120   /// current state of \a thread.
0121   ///
0122   ///
0123   /// \param[in] stop_others
0124   ///     Whether the plan should be set to stop other threads.
0125   ///
0126   /// \return
0127   ///    A pointer to the plan (caller owned) or NULL if we are not at such
0128   ///    a trampoline.
0129   virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
0130                                                           bool stop_others) = 0;
0131 
0132   /// Some dynamic loaders provide features where there are a group of symbols
0133   /// "equivalent to" a given symbol one of which will be chosen when the
0134   /// symbol is bound.  If you want to set a breakpoint on one of these
0135   /// symbols, you really need to set it on all the equivalent symbols.
0136   ///
0137   ///
0138   /// \param[in] original_symbol
0139   ///     The symbol for which we are finding equivalences.
0140   ///
0141   /// \param[in] module_list
0142   ///     The set of modules in which to search.
0143   ///
0144   /// \param[out] equivalent_symbols
0145   ///     The equivalent symbol list - any equivalent symbols found are appended
0146   ///     to this list.
0147   ///
0148   virtual void FindEquivalentSymbols(Symbol *original_symbol,
0149                                      ModuleList &module_list,
0150                                      SymbolContextList &equivalent_symbols) {}
0151 
0152   /// Ask if it is ok to try and load or unload an shared library (image).
0153   ///
0154   /// The dynamic loader often knows when it would be ok to try and load or
0155   /// unload a shared library. This function call allows the dynamic loader
0156   /// plug-ins to check any current dyld state to make sure it is an ok time
0157   /// to load a shared library.
0158   ///
0159   /// \return
0160   ///     \b true if it is currently ok to try and load a shared
0161   ///     library into the process, \b false otherwise.
0162   virtual Status CanLoadImage() = 0;
0163 
0164   /// Ask if the eh_frame information for the given SymbolContext should be
0165   /// relied on even when it's the first frame in a stack unwind.
0166   ///
0167   /// The CFI instructions from the eh_frame section are normally only valid
0168   /// at call sites -- places where a program could throw an exception and
0169   /// need to unwind out.  But some Modules may be known to the system as
0170   /// having reliable eh_frame information at all call sites.  This would be
0171   /// the case if the Module's contents are largely hand-written assembly with
0172   /// hand-written eh_frame information. Normally when unwinding from a
0173   /// function at the beginning of a stack unwind lldb will examine the
0174   /// assembly instructions to understand how the stack frame is set up and
0175   /// where saved registers are stored. But with hand-written assembly this is
0176   /// not reliable enough -- we need to consult those function's hand-written
0177   /// eh_frame information.
0178   ///
0179   /// \return
0180   ///     \b True if the symbol context should use eh_frame instructions
0181   ///     unconditionally when unwinding from this frame.  Else \b false,
0182   ///     the normal lldb unwind behavior of only using eh_frame when the
0183   ///     function appears in the middle of the stack.
0184   virtual bool AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) {
0185     return false;
0186   }
0187 
0188   /// Retrieves the per-module TLS block for a given thread.
0189   ///
0190   /// \param[in] module
0191   ///     The module to query TLS data for.
0192   ///
0193   /// \param[in] thread
0194   ///     The specific thread to query TLS data for.
0195   ///
0196   /// \return
0197   ///     If the given thread has TLS data allocated for the
0198   ///     module, the address of the TLS block. Otherwise
0199   ///     LLDB_INVALID_ADDRESS is returned.
0200   virtual lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module,
0201                                           const lldb::ThreadSP thread,
0202                                           lldb::addr_t tls_file_addr) {
0203     return LLDB_INVALID_ADDRESS;
0204   }
0205 
0206   /// Locates or creates a module given by \p file and updates/loads the
0207   /// resulting module at the virtual base address \p base_addr.
0208   /// Note that this calls Target::GetOrCreateModule with notify being false,
0209   /// so it is necessary to call Target::ModulesDidLoad afterwards.
0210   virtual lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file,
0211                                              lldb::addr_t link_map_addr,
0212                                              lldb::addr_t base_addr,
0213                                              bool base_addr_is_offset);
0214 
0215   /// Find/load a binary into lldb given a UUID and the address where it is
0216   /// loaded in memory, or a slide to be applied to the file address.
0217   /// May force an expensive search on the computer to find the binary by
0218   /// UUID, should not be used for a large number of binaries - intended for
0219   /// an environment where there may be one, or a few, binaries resident in
0220   /// memory.
0221   ///
0222   /// Given a UUID, search for a binary and load it at the address provided,
0223   /// or with the slide applied, or at the file address unslid.
0224   ///
0225   /// Given an address, try to read the binary out of memory, get the UUID,
0226   /// find the file if possible and load it unslid, or add the memory module.
0227   ///
0228   /// \param[in] process
0229   ///     The process to add this binary to.
0230   ///
0231   /// \param[in] name
0232   ///     Name of the binary, if available.  If this method cannot find a
0233   ///     matching binary on the debug host, it may create a memory module
0234   ///     out of live memory, and the provided name will be used.  If an
0235   ///     empty StringRef is provided, a name will be constructed for the module
0236   ///     based on the address it is loaded at.
0237   ///
0238   /// \param[in] uuid
0239   ///     UUID of the binary to be loaded.  UUID may be empty, and if a
0240   ///     load address is supplied, will read the binary from memory, get
0241   ///     a UUID and try to find a local binary.  There is a performance
0242   ///     cost to doing this, it is not preferable.
0243   ///
0244   /// \param[in] value
0245   ///     Address where the binary should be loaded, or read out of memory.
0246   ///     Or a slide value, to be applied to the file addresses of the binary.
0247   ///
0248   /// \param[in] value_is_offset
0249   ///     A flag indicating that \p value is an address, or an offset to
0250   ///     be applied to the file addresses.
0251   ///
0252   /// \param[in] force_symbol_search
0253   ///     Allow the search to do a possibly expensive external search for
0254   ///     the ObjectFile and/or SymbolFile.
0255   ///
0256   /// \param[in] notify
0257   ///     Whether ModulesDidLoad should be called when a binary has been added
0258   ///     to the Target.  The caller may prefer to batch up these when loading
0259   ///     multiple binaries.
0260   ///
0261   /// \param[in] set_address_in_target
0262   ///     Whether the address of the binary should be set in the Target if it
0263   ///     is added.  The caller may want to set the section addresses
0264   ///     individually, instead of loading the binary the entire based on the
0265   ///     start address or slide.  The caller is responsible for setting the
0266   ///     load address for the binary or its segments in the Target if it passes
0267   ///     true.
0268   ///
0269   /// \param[in] allow_memory_image_last_resort
0270   ///     If no better binary image can be found, allow reading the binary
0271   ///     out of memory, if possible, and create the Module based on that.
0272   ///     May be slow to read a binary out of memory, and for unusual
0273   ///     environments, may be no symbols mapped in memory at all.
0274   ///
0275   /// \return
0276   ///     Returns a shared pointer for the Module that has been added.
0277   static lldb::ModuleSP LoadBinaryWithUUIDAndAddress(
0278       Process *process, llvm::StringRef name, UUID uuid, lldb::addr_t value,
0279       bool value_is_offset, bool force_symbol_search, bool notify,
0280       bool set_address_in_target, bool allow_memory_image_last_resort);
0281 
0282   /// Get information about the shared cache for a process, if possible.
0283   ///
0284   /// On some systems (e.g. Darwin based systems), a set of libraries that are
0285   /// common to most processes may be put in a single region of memory and
0286   /// mapped into every process, this is called the shared cache, as a
0287   /// performance optimization.
0288   ///
0289   /// Many targets will not have the concept of a shared cache.
0290   ///
0291   /// Depending on how the DynamicLoader gathers information about the shared
0292   /// cache, it may be able to only return basic information - like the UUID
0293   /// of the cache - or it may be able to return additional information about
0294   /// the cache.
0295   ///
0296   /// \param[out] base_address
0297   ///     The base address (load address) of the shared cache.
0298   ///     LLDB_INVALID_ADDRESS if it cannot be determined.
0299   ///
0300   /// \param[out] uuid
0301   ///     The UUID of the shared cache, if it can be determined.
0302   ///     If the UUID cannot be fetched, IsValid() will be false.
0303   ///
0304   /// \param[out] using_shared_cache
0305   ///     If this process is using a shared cache.
0306   ///     If unknown, eLazyBoolCalculate is returned.
0307   ///
0308   /// \param[out] private_shared_cache
0309   ///     A LazyBool indicating whether this process is using a
0310   ///     private shared cache.
0311   ///     If this information cannot be fetched, eLazyBoolCalculate.
0312   ///
0313   /// \return
0314   ///     Returns false if this DynamicLoader cannot gather information
0315   ///     about the shared cache / has no concept of a shared cache.
0316   virtual bool GetSharedCacheInformation(lldb::addr_t &base_address, UUID &uuid,
0317                                          LazyBool &using_shared_cache,
0318                                          LazyBool &private_shared_cache) {
0319     base_address = LLDB_INVALID_ADDRESS;
0320     uuid.Clear();
0321     using_shared_cache = eLazyBoolCalculate;
0322     private_shared_cache = eLazyBoolCalculate;
0323     return false;
0324   }
0325 
0326   /// Return whether the dynamic loader is fully initialized and it's safe to
0327   /// call its APIs.
0328   ///
0329   /// On some systems (e.g. Darwin based systems), lldb will get notified by
0330   /// the dynamic loader before it itself finished initializing and it's not
0331   /// safe to call certain APIs or SPIs.
0332   virtual bool IsFullyInitialized() { return true; }
0333 
0334   /// Return the `start` \b address in the dynamic loader module.
0335   /// This is the address the process will begin executing with
0336   /// `process launch --stop-at-entry`.
0337   virtual std::optional<lldb_private::Address> GetStartAddress() {
0338     return std::nullopt;
0339   }
0340 
0341   /// Returns a list of memory ranges that should be saved in the core file,
0342   /// specific for this dynamic loader.
0343   ///
0344   /// For example, an implementation of this function can save the thread
0345   /// local data of a given thread.
0346   virtual void CalculateDynamicSaveCoreRanges(
0347       lldb_private::Process &process,
0348       std::vector<lldb_private::MemoryRegionInfo> &ranges,
0349       llvm::function_ref<bool(const lldb_private::Thread &)>
0350           save_thread_predicate) {};
0351 
0352 protected:
0353   // Utility methods for derived classes
0354 
0355   lldb::ModuleSP FindModuleViaTarget(const FileSpec &file);
0356 
0357   /// Checks to see if the target module has changed, updates the target
0358   /// accordingly and returns the target executable module.
0359   lldb::ModuleSP GetTargetExecutable();
0360 
0361   /// Updates the load address of every allocatable section in \p module.
0362   ///
0363   /// \param module The module to traverse.
0364   ///
0365   /// \param link_map_addr The virtual address of the link map for the @p
0366   /// module.
0367   ///
0368   /// \param base_addr The virtual base address \p module is loaded at.
0369   virtual void UpdateLoadedSections(lldb::ModuleSP module,
0370                                     lldb::addr_t link_map_addr,
0371                                     lldb::addr_t base_addr,
0372                                     bool base_addr_is_offset);
0373 
0374   // Utility method so base classes can share implementation of
0375   // UpdateLoadedSections
0376   void UpdateLoadedSectionsCommon(lldb::ModuleSP module, lldb::addr_t base_addr,
0377                                   bool base_addr_is_offset);
0378 
0379   /// Removes the loaded sections from the target in \p module.
0380   ///
0381   /// \param module The module to traverse.
0382   virtual void UnloadSections(const lldb::ModuleSP module);
0383 
0384   // Utility method so base classes can share implementation of UnloadSections
0385   void UnloadSectionsCommon(const lldb::ModuleSP module);
0386 
0387   const lldb_private::SectionList *
0388   GetSectionListFromModule(const lldb::ModuleSP module) const;
0389 
0390   // Read an unsigned int of the given size from memory at the given addr.
0391   // Return -1 if the read fails, otherwise return the result as an int64_t.
0392   int64_t ReadUnsignedIntWithSizeInBytes(lldb::addr_t addr, int size_in_bytes);
0393 
0394   // Read a pointer from memory at the given addr. Return LLDB_INVALID_ADDRESS
0395   // if the read fails.
0396   lldb::addr_t ReadPointer(lldb::addr_t addr);
0397 
0398   // Calls into the Process protected method LoadOperatingSystemPlugin:
0399   void LoadOperatingSystemPlugin(bool flush);
0400 
0401 
0402   // Member variables.
0403   Process
0404       *m_process; ///< The process that this dynamic loader plug-in is tracking.
0405 };
0406 
0407 } // namespace lldb_private
0408 
0409 #endif // LLDB_TARGET_DYNAMICLOADER_H