|
|
|||
File indexing completed on 2026-05-10 08:42:44
0001 //===-- Address.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_CORE_ADDRESS_H 0010 #define LLDB_CORE_ADDRESS_H 0011 0012 #include "lldb/Utility/Stream.h" 0013 #include "lldb/lldb-defines.h" 0014 #include "lldb/lldb-forward.h" 0015 #include "lldb/lldb-private-enumerations.h" 0016 #include "lldb/lldb-types.h" 0017 0018 #include "llvm/ADT/StringRef.h" 0019 0020 #include <cstddef> 0021 #include <cstdint> 0022 0023 namespace lldb_private { 0024 class Block; 0025 class CompileUnit; 0026 class ExecutionContextScope; 0027 class Function; 0028 class SectionList; 0029 class Stream; 0030 class Symbol; 0031 class SymbolContext; 0032 class Target; 0033 struct LineEntry; 0034 0035 /// \class Address Address.h "lldb/Core/Address.h" 0036 /// A section + offset based address class. 0037 /// 0038 /// The Address class allows addresses to be relative to a section that can 0039 /// move during runtime due to images (executables, shared libraries, bundles, 0040 /// frameworks) being loaded at different addresses than the addresses found 0041 /// in the object file that represents them on disk. There are currently two 0042 /// types of addresses for a section: 0043 /// \li file addresses 0044 /// \li load addresses 0045 /// 0046 /// File addresses represent the virtual addresses that are in the "on disk" 0047 /// object files. These virtual addresses are converted to be relative to 0048 /// unique sections scoped to the object file so that when/if the addresses 0049 /// slide when the images are loaded/unloaded in memory, we can easily track 0050 /// these changes without having to update every object (compile unit ranges, 0051 /// line tables, function address ranges, lexical block and inlined subroutine 0052 /// address ranges, global and static variables) each time an image is loaded 0053 /// or unloaded. 0054 /// 0055 /// Load addresses represent the virtual addresses where each section ends up 0056 /// getting loaded at runtime. Before executing a program, it is common for 0057 /// all of the load addresses to be unresolved. When a DynamicLoader plug-in 0058 /// receives notification that shared libraries have been loaded/unloaded, the 0059 /// load addresses of the main executable and any images (shared libraries) 0060 /// will be resolved/unresolved. When this happens, breakpoints that are in 0061 /// one of these sections can be set/cleared. 0062 class Address { 0063 public: 0064 /// Dump styles allow the Address::Dump(Stream *,DumpStyle) const function 0065 /// to display Address contents in a variety of ways. 0066 enum DumpStyle { 0067 /// Invalid dump style. 0068 DumpStyleInvalid, 0069 /// Display as the section name + offset. 0070 /// \code 0071 /// // address for printf in libSystem.B.dylib as a section name + offset 0072 /// libSystem.B.dylib.__TEXT.__text + 0x0005cfdf 0073 /// \endcode 0074 DumpStyleSectionNameOffset, 0075 /// Display as the section pointer + offset (debug output). 0076 /// \code 0077 /// // address for printf in libSystem.B.dylib as a section pointer + 0078 /// offset (lldb::Section *)0x35cc50 + 0x000000000005cfdf 0079 /// \endcode 0080 DumpStyleSectionPointerOffset, 0081 /// Display as the file address (if any). 0082 /// \code 0083 /// // address for printf in libSystem.B.dylib as a file address 0084 /// 0x000000000005dcff 0085 /// \endcode 0086 /// 0087 DumpStyleFileAddress, 0088 /// Display as the file address with the module name prepended (if any). 0089 /// \code 0090 /// // address for printf in libSystem.B.dylib as a file address 0091 /// libSystem.B.dylib[0x000000000005dcff] 0092 /// \endcode 0093 DumpStyleModuleWithFileAddress, 0094 /// Display as the load address (if resolved). 0095 /// \code 0096 /// // address for printf in libSystem.B.dylib as a load address 0097 /// 0x00007fff8306bcff 0098 /// \endcode 0099 DumpStyleLoadAddress, 0100 /// Display the details about what an address resolves to. This can be 0101 /// anything from a symbol context summary (module, function/symbol, and 0102 /// file and line), to information about what the pointer points to if the 0103 /// address is in a section (section of pointers, c strings, etc). 0104 DumpStyleResolvedDescription, 0105 DumpStyleResolvedDescriptionNoModule, 0106 DumpStyleResolvedDescriptionNoFunctionArguments, 0107 /// Elide the function name; display an offset into the current function. 0108 /// Used primarily in disassembly symbolication 0109 DumpStyleNoFunctionName, 0110 /// Detailed symbol context information for an address for all symbol 0111 /// context members. 0112 DumpStyleDetailedSymbolContext, 0113 /// Dereference a pointer at the current address and then lookup the 0114 /// dereferenced address using DumpStyleResolvedDescription 0115 DumpStyleResolvedPointerDescription 0116 }; 0117 0118 /// Default constructor. 0119 /// 0120 /// Initialize with a invalid section (NULL) and an invalid offset 0121 /// (LLDB_INVALID_ADDRESS). 0122 Address() = default; 0123 0124 /// Copy constructor 0125 /// 0126 /// Makes a copy of the another Address object \a rhs. 0127 /// 0128 /// \param[in] rhs 0129 /// A const Address object reference to copy. 0130 Address(const Address &rhs) 0131 : m_section_wp(rhs.m_section_wp), m_offset(rhs.m_offset) {} 0132 0133 /// Construct with a section pointer and offset. 0134 /// 0135 /// Initialize the address with the supplied \a section and \a offset. 0136 /// 0137 /// \param[in] section_sp 0138 /// A section pointer to a valid lldb::Section, or NULL if the 0139 /// address doesn't have a section or will get resolved later. 0140 /// 0141 /// \param[in] offset 0142 /// The offset in bytes into \a section. 0143 Address(const lldb::SectionSP §ion_sp, lldb::addr_t offset) 0144 : m_section_wp(), // Don't init with section_sp in case section_sp is 0145 // invalid (the weak_ptr will throw) 0146 m_offset(offset) { 0147 if (section_sp) 0148 m_section_wp = section_sp; 0149 } 0150 0151 /// Construct with a virtual address and section list. 0152 /// 0153 /// Initialize and resolve the address with the supplied virtual address \a 0154 /// file_addr. 0155 /// 0156 /// \param[in] file_addr 0157 /// A virtual file address. 0158 /// 0159 /// \param[in] section_list 0160 /// A list of sections, one of which may contain the \a file_addr. 0161 Address(lldb::addr_t file_addr, const SectionList *section_list); 0162 0163 Address(lldb::addr_t abs_addr); 0164 0165 /// Assignment operator. 0166 /// 0167 /// Copies the address value from another Address object \a rhs into \a this 0168 /// object. 0169 /// 0170 /// \param[in] rhs 0171 /// A const Address object reference to copy. 0172 /// 0173 /// \return 0174 /// A const Address object reference to \a this. 0175 const Address &operator=(const Address &rhs); 0176 0177 /// Clear the object's state. 0178 /// 0179 /// Sets the section to an invalid value (NULL) and an invalid offset 0180 /// (LLDB_INVALID_ADDRESS). 0181 void Clear() { 0182 m_section_wp.reset(); 0183 m_offset = LLDB_INVALID_ADDRESS; 0184 } 0185 0186 /// Compare two Address objects. 0187 /// 0188 /// \param[in] lhs 0189 /// The Left Hand Side const Address object reference. 0190 /// 0191 /// \param[in] rhs 0192 /// The Right Hand Side const Address object reference. 0193 /// 0194 /// \return 0195 /// -1 if lhs < rhs 0196 /// 0 if lhs == rhs 0197 /// 1 if lhs > rhs 0198 static int CompareFileAddress(const Address &lhs, const Address &rhs); 0199 0200 static int CompareLoadAddress(const Address &lhs, const Address &rhs, 0201 Target *target); 0202 0203 static int CompareModulePointerAndOffset(const Address &lhs, 0204 const Address &rhs); 0205 0206 // For use with std::map, std::multi_map 0207 class ModulePointerAndOffsetLessThanFunctionObject { 0208 public: 0209 ModulePointerAndOffsetLessThanFunctionObject() = default; 0210 0211 bool operator()(const Address &a, const Address &b) const { 0212 return Address::CompareModulePointerAndOffset(a, b) < 0; 0213 } 0214 }; 0215 0216 /// Write a description of this object to a Stream. 0217 bool GetDescription(Stream &s, Target &target, 0218 lldb::DescriptionLevel level) const; 0219 0220 /// Dump a description of this object to a Stream. 0221 /// 0222 /// Dump a description of the contents of this object to the supplied stream 0223 /// \a s. There are many ways to display a section offset based address, and 0224 /// \a style lets the user choose. 0225 /// 0226 /// \param[in] s 0227 /// The stream to which to dump the object description. 0228 /// 0229 /// \param[in] style 0230 /// The display style for the address. 0231 /// 0232 /// \param[in] fallback_style 0233 /// The display style for the address. 0234 /// 0235 /// \param[in] addr_byte_size 0236 /// The address byte size for the address. 0237 /// 0238 /// \param[in] all_ranges 0239 /// If true, dump all valid ranges and value ranges for the variable that 0240 /// contains the address, otherwise dumping the range that contains the 0241 /// address. 0242 /// 0243 /// \param[in] pattern 0244 /// An optional regex pattern to match against the description. If 0245 /// specified, parts of the description matching this pattern may be 0246 /// highlighted or processed differently. If this parameter is an empty 0247 /// string or not provided, no highlighting is applied. 0248 /// 0249 /// \return 0250 /// Returns \b true if the address was able to be displayed. 0251 /// File and load addresses may be unresolved and it may not be 0252 /// possible to display a valid value, \b false will be returned 0253 /// in such cases. 0254 /// 0255 /// \see Address::DumpStyle 0256 bool 0257 Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, 0258 DumpStyle fallback_style = DumpStyleInvalid, 0259 uint32_t addr_byte_size = UINT32_MAX, bool all_ranges = false, 0260 std::optional<Stream::HighlightSettings> settings = std::nullopt) const; 0261 0262 AddressClass GetAddressClass() const; 0263 0264 /// Get the file address. 0265 /// 0266 /// If an address comes from a file on disk that has section relative 0267 /// addresses, then it has a virtual address that is relative to unique 0268 /// section in the object file. 0269 /// 0270 /// \return 0271 /// The valid file virtual address, or LLDB_INVALID_ADDRESS if 0272 /// the address doesn't have a file virtual address (image is 0273 /// from memory only with no representation on disk). 0274 lldb::addr_t GetFileAddress() const; 0275 0276 /// Get the load address. 0277 /// 0278 /// If an address comes from a file on disk that has section relative 0279 /// addresses, then it has a virtual address that is relative to unique 0280 /// section in the object file. Sections get resolved at runtime by 0281 /// DynamicLoader plug-ins as images (executables and shared libraries) get 0282 /// loaded/unloaded. If a section is loaded, then the load address can be 0283 /// resolved. 0284 /// 0285 /// \return 0286 /// The valid load virtual address, or LLDB_INVALID_ADDRESS if 0287 /// the address is currently not loaded. 0288 lldb::addr_t GetLoadAddress(Target *target) const; 0289 0290 /// Get the load address as a callable code load address. 0291 /// 0292 /// This function will first resolve its address to a load address. Then, if 0293 /// the address turns out to be in code address, return the load address 0294 /// that would be required to call or return to. The address might have 0295 /// extra bits set (bit zero will be set to Thumb functions for an ARM 0296 /// target) that are required when changing the program counter to setting a 0297 /// return address. 0298 /// 0299 /// \return 0300 /// The valid load virtual address, or LLDB_INVALID_ADDRESS if 0301 /// the address is currently not loaded. 0302 lldb::addr_t GetCallableLoadAddress(Target *target, 0303 bool is_indirect = false) const; 0304 0305 /// Get the load address as an opcode load address. 0306 /// 0307 /// This function will first resolve its address to a load address. Then, if 0308 /// the address turns out to be in code address, return the load address for 0309 /// an opcode. This address object might have extra bits set (bit zero will 0310 /// be set to Thumb functions for an 0311 /// ARM target) that are required for changing the program counter 0312 /// and this function will remove any bits that are intended for these 0313 /// special purposes. The result of this function can be used to safely 0314 /// write a software breakpoint trap to memory. 0315 /// 0316 /// \return 0317 /// The valid load virtual address with extra callable bits 0318 /// removed, or LLDB_INVALID_ADDRESS if the address is currently 0319 /// not loaded. 0320 lldb::addr_t GetOpcodeLoadAddress( 0321 Target *target, 0322 AddressClass addr_class = AddressClass::eInvalid) const; 0323 0324 /// Get the section relative offset value. 0325 /// 0326 /// \return 0327 /// The current offset, or LLDB_INVALID_ADDRESS if this address 0328 /// doesn't contain a valid offset. 0329 lldb::addr_t GetOffset() const { return m_offset; } 0330 0331 /// Check if an address is section offset. 0332 /// 0333 /// When converting a virtual file or load address into a section offset 0334 /// based address, we often need to know if, given a section list, if the 0335 /// address was able to be converted to section offset. This function 0336 /// returns true if the current value contained in this object is section 0337 /// offset based. 0338 /// 0339 /// \return 0340 /// Returns \b true if the address has a valid section and 0341 /// offset, \b false otherwise. 0342 bool IsSectionOffset() const { 0343 return IsValid() && (GetSection().get() != nullptr); 0344 } 0345 0346 /// Check if the object state is valid. 0347 /// 0348 /// A valid Address object contains either a section pointer and 0349 /// offset (for section offset based addresses), or just a valid offset 0350 /// (for absolute addresses that have no section). 0351 /// 0352 /// \return 0353 /// Returns \b true if the offset is valid, \b false 0354 /// otherwise. 0355 bool IsValid() const { return m_offset != LLDB_INVALID_ADDRESS; } 0356 0357 /// Get the memory cost of this object. 0358 /// 0359 /// \return 0360 /// The number of bytes that this object occupies in memory. 0361 size_t MemorySize() const; 0362 0363 /// Resolve a file virtual address using a section list. 0364 /// 0365 /// Given a list of sections, attempt to resolve \a addr as an offset into 0366 /// one of the file sections. 0367 /// 0368 /// \return 0369 /// Returns \b true if \a addr was able to be resolved, \b false 0370 /// otherwise. 0371 bool ResolveAddressUsingFileSections(lldb::addr_t addr, 0372 const SectionList *sections); 0373 0374 /// Resolve this address to its containing function and optionally get 0375 /// that function's address range. 0376 /// 0377 /// \param[out] sym_ctx 0378 /// The symbol context describing the function in which this address lies 0379 /// 0380 /// \parm[out] addr_range_ptr 0381 /// Pointer to the AddressRange to fill in with the function's address 0382 /// range. Caller may pass null if they don't need the address range. 0383 /// 0384 /// \return 0385 /// Returns \b false if the function/symbol could not be resolved 0386 /// or if the address range was requested and could not be resolved; 0387 /// returns \b true otherwise. 0388 bool ResolveFunctionScope(lldb_private::SymbolContext &sym_ctx, 0389 lldb_private::AddressRange *addr_range_ptr = nullptr); 0390 0391 /// Set the address to represent \a load_addr. 0392 /// 0393 /// The address will attempt to find a loaded section within \a target that 0394 /// contains \a load_addr. If successful, this address object will have a 0395 /// valid section and offset. Else this address object will have no section 0396 /// (NULL) and the offset will be \a load_addr. 0397 /// 0398 /// \param[in] load_addr 0399 /// A load address from a current process. 0400 /// 0401 /// \param[in] target 0402 /// The target to use when trying resolve the address into 0403 /// a section + offset. The Target's SectionLoadList object 0404 /// is used to resolve the address. 0405 /// 0406 /// \param[in] allow_section_end 0407 /// If true, treat an address pointing to the end of the module as 0408 /// belonging to that module. 0409 /// 0410 /// \return 0411 /// Returns \b true if the load address was resolved to be 0412 /// section/offset, \b false otherwise. It is often ok for an 0413 /// address to not resolve to a section in a module, this often 0414 /// happens for JIT'ed code, or any load addresses on the stack 0415 /// or heap. 0416 bool SetLoadAddress(lldb::addr_t load_addr, Target *target, 0417 bool allow_section_end = false); 0418 0419 bool SetOpcodeLoadAddress( 0420 lldb::addr_t load_addr, Target *target, 0421 AddressClass addr_class = AddressClass::eInvalid, 0422 bool allow_section_end = false); 0423 0424 bool SetCallableLoadAddress(lldb::addr_t load_addr, Target *target); 0425 0426 /// Get accessor for the module for this address. 0427 /// 0428 /// \return 0429 /// Returns the Module pointer that this address is an offset 0430 /// in, or NULL if this address doesn't belong in a module, or 0431 /// isn't resolved yet. 0432 lldb::ModuleSP GetModule() const; 0433 0434 /// Get const accessor for the section. 0435 /// 0436 /// \return 0437 /// Returns the const lldb::Section pointer that this address is an 0438 /// offset in, or NULL if this address is absolute. 0439 lldb::SectionSP GetSection() const { return m_section_wp.lock(); } 0440 0441 /// Set accessor for the offset. 0442 /// 0443 /// \param[in] offset 0444 /// A new offset value for this object. 0445 /// 0446 /// \return 0447 /// Returns \b true if the offset changed, \b false otherwise. 0448 bool SetOffset(lldb::addr_t offset) { 0449 bool changed = m_offset != offset; 0450 m_offset = offset; 0451 return changed; 0452 } 0453 0454 void SetRawAddress(lldb::addr_t addr) { 0455 m_section_wp.reset(); 0456 m_offset = addr; 0457 } 0458 0459 bool Slide(int64_t offset) { 0460 if (m_offset != LLDB_INVALID_ADDRESS) { 0461 m_offset += offset; 0462 return true; 0463 } 0464 return false; 0465 } 0466 0467 /// Set accessor for the section. 0468 /// 0469 /// \param[in] section_sp 0470 /// A new lldb::Section pointer to use as the section base. Can 0471 /// be NULL for absolute addresses that are not relative to 0472 /// any section. 0473 void SetSection(const lldb::SectionSP §ion_sp) { 0474 m_section_wp = section_sp; 0475 } 0476 0477 void ClearSection() { m_section_wp.reset(); } 0478 0479 /// Reconstruct a symbol context from an address. 0480 /// 0481 /// This class doesn't inherit from SymbolContextScope because many address 0482 /// objects have short lifespans. Address objects that are section offset 0483 /// can reconstruct their symbol context by looking up the address in the 0484 /// module found in the section. 0485 /// 0486 /// \see SymbolContextScope::CalculateSymbolContext(SymbolContext*) 0487 uint32_t CalculateSymbolContext(SymbolContext *sc, 0488 lldb::SymbolContextItem resolve_scope = 0489 lldb::eSymbolContextEverything) const; 0490 0491 lldb::ModuleSP CalculateSymbolContextModule() const; 0492 0493 CompileUnit *CalculateSymbolContextCompileUnit() const; 0494 0495 Function *CalculateSymbolContextFunction() const; 0496 0497 Block *CalculateSymbolContextBlock() const; 0498 0499 Symbol *CalculateSymbolContextSymbol() const; 0500 0501 bool CalculateSymbolContextLineEntry(LineEntry &line_entry) const; 0502 0503 // Returns true if the section should be valid, but isn't because the shared 0504 // pointer to the section can't be reconstructed from a weak pointer that 0505 // contains a valid weak reference to a section. Returns false if the section 0506 // weak pointer has no reference to a section, or if the section is still 0507 // valid 0508 bool SectionWasDeleted() const; 0509 0510 protected: 0511 // Member variables. 0512 lldb::SectionWP m_section_wp; ///< The section for the address, can be NULL. 0513 lldb::addr_t m_offset = LLDB_INVALID_ADDRESS; ///< Offset into section if \a 0514 ///< m_section_wp is valid... 0515 0516 // Returns true if the m_section_wp once had a reference to a valid section 0517 // shared pointer, but no longer does. This can happen if we have an address 0518 // from a module that gets unloaded and deleted. This function should only be 0519 // called if GetSection() returns an empty shared pointer and you want to 0520 // know if this address used to have a valid section. 0521 bool SectionWasDeletedPrivate() const; 0522 }; 0523 0524 // NOTE: Be careful using this operator. It can correctly compare two 0525 // addresses from the same Module correctly. It can't compare two addresses 0526 // from different modules in any meaningful way, but it will compare the module 0527 // pointers. 0528 // 0529 // To sum things up: 0530 // - works great for addresses within the same module - it works for addresses 0531 // across multiple modules, but don't expect the 0532 // address results to make much sense 0533 // 0534 // This basically lets Address objects be used in ordered collection classes. 0535 bool operator<(const Address &lhs, const Address &rhs); 0536 bool operator>(const Address &lhs, const Address &rhs); 0537 bool operator==(const Address &lhs, const Address &rhs); 0538 bool operator!=(const Address &lhs, const Address &rhs); 0539 0540 } // namespace lldb_private 0541 0542 #endif // LLDB_CORE_ADDRESS_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|