1 //===-- Address.h -----------------------------------------------*- C++ -*-===// 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 #ifndef LLDB_CORE_ADDRESS_H 10 #define LLDB_CORE_ADDRESS_H 11 12 #include "lldb/lldb-defines.h" 13 #include "lldb/lldb-forward.h" 14 #include "lldb/lldb-private-enumerations.h" 15 #include "lldb/lldb-types.h" 16 17 #include <cstddef> 18 #include <cstdint> 19 20 namespace lldb_private { 21 class Block; 22 class CompileUnit; 23 class ExecutionContextScope; 24 class Function; 25 class SectionList; 26 class Stream; 27 class Symbol; 28 class SymbolContext; 29 class Target; 30 struct LineEntry; 31 32 /// \class Address Address.h "lldb/Core/Address.h" 33 /// A section + offset based address class. 34 /// 35 /// The Address class allows addresses to be relative to a section that can 36 /// move during runtime due to images (executables, shared libraries, bundles, 37 /// frameworks) being loaded at different addresses than the addresses found 38 /// in the object file that represents them on disk. There are currently two 39 /// types of addresses for a section: 40 /// \li file addresses 41 /// \li load addresses 42 /// 43 /// File addresses represent the virtual addresses that are in the "on disk" 44 /// object files. These virtual addresses are converted to be relative to 45 /// unique sections scoped to the object file so that when/if the addresses 46 /// slide when the images are loaded/unloaded in memory, we can easily track 47 /// these changes without having to update every object (compile unit ranges, 48 /// line tables, function address ranges, lexical block and inlined subroutine 49 /// address ranges, global and static variables) each time an image is loaded 50 /// or unloaded. 51 /// 52 /// Load addresses represent the virtual addresses where each section ends up 53 /// getting loaded at runtime. Before executing a program, it is common for 54 /// all of the load addresses to be unresolved. When a DynamicLoader plug-in 55 /// receives notification that shared libraries have been loaded/unloaded, the 56 /// load addresses of the main executable and any images (shared libraries) 57 /// will be resolved/unresolved. When this happens, breakpoints that are in 58 /// one of these sections can be set/cleared. 59 class Address { 60 public: 61 /// Dump styles allow the Address::Dump(Stream *,DumpStyle) const function 62 /// to display Address contents in a variety of ways. 63 enum DumpStyle { 64 /// Invalid dump style. 65 DumpStyleInvalid, 66 /// Display as the section name + offset. 67 /// \code 68 /// // address for printf in libSystem.B.dylib as a section name + offset 69 /// libSystem.B.dylib.__TEXT.__text + 0x0005cfdf 70 /// \endcode 71 DumpStyleSectionNameOffset, 72 /// Display as the section pointer + offset (debug output). 73 /// \code 74 /// // address for printf in libSystem.B.dylib as a section pointer + 75 /// offset (lldb::Section *)0x35cc50 + 0x000000000005cfdf 76 /// \endcode 77 DumpStyleSectionPointerOffset, 78 /// Display as the file address (if any). 79 /// \code 80 /// // address for printf in libSystem.B.dylib as a file address 81 /// 0x000000000005dcff 82 /// \endcode 83 /// 84 DumpStyleFileAddress, 85 /// Display as the file address with the module name prepended (if any). 86 /// \code 87 /// // address for printf in libSystem.B.dylib as a file address 88 /// libSystem.B.dylib[0x000000000005dcff] 89 /// \endcode 90 DumpStyleModuleWithFileAddress, 91 /// Display as the load address (if resolved). 92 /// \code 93 /// // address for printf in libSystem.B.dylib as a load address 94 /// 0x00007fff8306bcff 95 /// \endcode 96 DumpStyleLoadAddress, 97 /// Display the details about what an address resolves to. This can be 98 /// anything from a symbol context summary (module, function/symbol, and 99 /// file and line), to information about what the pointer points to if the 100 /// address is in a section (section of pointers, c strings, etc). 101 DumpStyleResolvedDescription, 102 DumpStyleResolvedDescriptionNoModule, 103 DumpStyleResolvedDescriptionNoFunctionArguments, 104 /// Elide the function name; display an offset into the current function. 105 /// Used primarily in disassembly symbolication 106 DumpStyleNoFunctionName, 107 /// Detailed symbol context information for an address for all symbol 108 /// context members. 109 DumpStyleDetailedSymbolContext, 110 /// Dereference a pointer at the current address and then lookup the 111 /// dereferenced address using DumpStyleResolvedDescription 112 DumpStyleResolvedPointerDescription 113 }; 114 115 /// Default constructor. 116 /// 117 /// Initialize with a invalid section (NULL) and an invalid offset 118 /// (LLDB_INVALID_ADDRESS). 119 Address() = default; 120 121 /// Copy constructor 122 /// 123 /// Makes a copy of the another Address object \a rhs. 124 /// 125 /// \param[in] rhs 126 /// A const Address object reference to copy. 127 Address(const Address &rhs) 128 : m_section_wp(rhs.m_section_wp), m_offset(rhs.m_offset) {} 129 130 /// Construct with a section pointer and offset. 131 /// 132 /// Initialize the address with the supplied \a section and \a offset. 133 /// 134 /// \param[in] section_sp 135 /// A section pointer to a valid lldb::Section, or NULL if the 136 /// address doesn't have a section or will get resolved later. 137 /// 138 /// \param[in] offset 139 /// The offset in bytes into \a section. 140 Address(const lldb::SectionSP §ion_sp, lldb::addr_t offset) 141 : m_section_wp(), // Don't init with section_sp in case section_sp is 142 // invalid (the weak_ptr will throw) 143 m_offset(offset) { 144 if (section_sp) 145 m_section_wp = section_sp; 146 } 147 148 /// Construct with a virtual address and section list. 149 /// 150 /// Initialize and resolve the address with the supplied virtual address \a 151 /// file_addr. 152 /// 153 /// \param[in] file_addr 154 /// A virtual file address. 155 /// 156 /// \param[in] section_list 157 /// A list of sections, one of which may contain the \a file_addr. 158 Address(lldb::addr_t file_addr, const SectionList *section_list); 159 160 Address(lldb::addr_t abs_addr); 161 162 /// Assignment operator. 163 /// 164 /// Copies the address value from another Address object \a rhs into \a this 165 /// object. 166 /// 167 /// \param[in] rhs 168 /// A const Address object reference to copy. 169 /// 170 /// \return 171 /// A const Address object reference to \a this. 172 const Address &operator=(const Address &rhs); 173 174 /// Clear the object's state. 175 /// 176 /// Sets the section to an invalid value (NULL) and an invalid offset 177 /// (LLDB_INVALID_ADDRESS). 178 void Clear() { 179 m_section_wp.reset(); 180 m_offset = LLDB_INVALID_ADDRESS; 181 } 182 183 /// Compare two Address objects. 184 /// 185 /// \param[in] lhs 186 /// The Left Hand Side const Address object reference. 187 /// 188 /// \param[in] rhs 189 /// The Right Hand Side const Address object reference. 190 /// 191 /// \return 192 /// -1 if lhs < rhs 193 /// 0 if lhs == rhs 194 /// 1 if lhs > rhs 195 static int CompareFileAddress(const Address &lhs, const Address &rhs); 196 197 static int CompareLoadAddress(const Address &lhs, const Address &rhs, 198 Target *target); 199 200 static int CompareModulePointerAndOffset(const Address &lhs, 201 const Address &rhs); 202 203 // For use with std::map, std::multi_map 204 class ModulePointerAndOffsetLessThanFunctionObject { 205 public: 206 ModulePointerAndOffsetLessThanFunctionObject() = default; 207 208 bool operator()(const Address &a, const Address &b) const { 209 return Address::CompareModulePointerAndOffset(a, b) < 0; 210 } 211 }; 212 213 /// Write a description of this object to a Stream. 214 bool GetDescription(Stream &s, Target &target, 215 lldb::DescriptionLevel level) const; 216 217 /// Dump a description of this object to a Stream. 218 /// 219 /// Dump a description of the contents of this object to the supplied stream 220 /// \a s. There are many ways to display a section offset based address, and 221 /// \a style lets the user choose. 222 /// 223 /// \param[in] s 224 /// The stream to which to dump the object description. 225 /// 226 /// \param[in] style 227 /// The display style for the address. 228 /// 229 /// \param[in] fallback_style 230 /// The display style for the address. 231 /// 232 /// \param[in] addr_byte_size 233 /// The address byte size for the address. 234 /// 235 /// \param[in] all_ranges 236 /// If true, dump all valid ranges and value ranges for the variable that 237 /// contains the address, otherwise dumping the range that contains the 238 /// address. 239 /// 240 /// \return 241 /// Returns \b true if the address was able to be displayed. 242 /// File and load addresses may be unresolved and it may not be 243 /// possible to display a valid value, \b false will be returned 244 /// in such cases. 245 /// 246 /// \see Address::DumpStyle 247 bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, 248 DumpStyle fallback_style = DumpStyleInvalid, 249 uint32_t addr_byte_size = UINT32_MAX, 250 bool all_ranges = false) const; 251 252 AddressClass GetAddressClass() const; 253 254 /// Get the file address. 255 /// 256 /// If an address comes from a file on disk that has section relative 257 /// addresses, then it has a virtual address that is relative to unique 258 /// section in the object file. 259 /// 260 /// \return 261 /// The valid file virtual address, or LLDB_INVALID_ADDRESS if 262 /// the address doesn't have a file virtual address (image is 263 /// from memory only with no representation on disk). 264 lldb::addr_t GetFileAddress() const; 265 266 /// Get the load address. 267 /// 268 /// If an address comes from a file on disk that has section relative 269 /// addresses, then it has a virtual address that is relative to unique 270 /// section in the object file. Sections get resolved at runtime by 271 /// DynamicLoader plug-ins as images (executables and shared libraries) get 272 /// loaded/unloaded. If a section is loaded, then the load address can be 273 /// resolved. 274 /// 275 /// \return 276 /// The valid load virtual address, or LLDB_INVALID_ADDRESS if 277 /// the address is currently not loaded. 278 lldb::addr_t GetLoadAddress(Target *target) const; 279 280 /// Get the load address as a callable code load address. 281 /// 282 /// This function will first resolve its address to a load address. Then, if 283 /// the address turns out to be in code address, return the load address 284 /// that would be required to call or return to. The address might have 285 /// extra bits set (bit zero will be set to Thumb functions for an ARM 286 /// target) that are required when changing the program counter to setting a 287 /// return address. 288 /// 289 /// \return 290 /// The valid load virtual address, or LLDB_INVALID_ADDRESS if 291 /// the address is currently not loaded. 292 lldb::addr_t GetCallableLoadAddress(Target *target, 293 bool is_indirect = false) const; 294 295 /// Get the load address as an opcode load address. 296 /// 297 /// This function will first resolve its address to a load address. Then, if 298 /// the address turns out to be in code address, return the load address for 299 /// an opcode. This address object might have extra bits set (bit zero will 300 /// be set to Thumb functions for an 301 /// ARM target) that are required for changing the program counter 302 /// and this function will remove any bits that are intended for these 303 /// special purposes. The result of this function can be used to safely 304 /// write a software breakpoint trap to memory. 305 /// 306 /// \return 307 /// The valid load virtual address with extra callable bits 308 /// removed, or LLDB_INVALID_ADDRESS if the address is currently 309 /// not loaded. 310 lldb::addr_t GetOpcodeLoadAddress( 311 Target *target, 312 AddressClass addr_class = AddressClass::eInvalid) const; 313 314 /// Get the section relative offset value. 315 /// 316 /// \return 317 /// The current offset, or LLDB_INVALID_ADDRESS if this address 318 /// doesn't contain a valid offset. 319 lldb::addr_t GetOffset() const { return m_offset; } 320 321 /// Check if an address is section offset. 322 /// 323 /// When converting a virtual file or load address into a section offset 324 /// based address, we often need to know if, given a section list, if the 325 /// address was able to be converted to section offset. This function 326 /// returns true if the current value contained in this object is section 327 /// offset based. 328 /// 329 /// \return 330 /// Returns \b true if the address has a valid section and 331 /// offset, \b false otherwise. 332 bool IsSectionOffset() const { 333 return IsValid() && (GetSection().get() != nullptr); 334 } 335 336 /// Check if the object state is valid. 337 /// 338 /// A valid Address object contains either a section pointer and 339 /// offset (for section offset based addresses), or just a valid offset 340 /// (for absolute addresses that have no section). 341 /// 342 /// \return 343 /// Returns \b true if the offset is valid, \b false 344 /// otherwise. 345 bool IsValid() const { return m_offset != LLDB_INVALID_ADDRESS; } 346 347 /// Get the memory cost of this object. 348 /// 349 /// \return 350 /// The number of bytes that this object occupies in memory. 351 size_t MemorySize() const; 352 353 /// Resolve a file virtual address using a section list. 354 /// 355 /// Given a list of sections, attempt to resolve \a addr as an offset into 356 /// one of the file sections. 357 /// 358 /// \return 359 /// Returns \b true if \a addr was able to be resolved, \b false 360 /// otherwise. 361 bool ResolveAddressUsingFileSections(lldb::addr_t addr, 362 const SectionList *sections); 363 364 /// Resolve this address to its containing function and optionally get 365 /// that function's address range. 366 /// 367 /// \param[out] sym_ctx 368 /// The symbol context describing the function in which this address lies 369 /// 370 /// \parm[out] addr_range_ptr 371 /// Pointer to the AddressRange to fill in with the function's address 372 /// range. Caller may pass null if they don't need the address range. 373 /// 374 /// \return 375 /// Returns \b false if the function/symbol could not be resolved 376 /// or if the address range was requested and could not be resolved; 377 /// returns \b true otherwise. 378 bool ResolveFunctionScope(lldb_private::SymbolContext &sym_ctx, 379 lldb_private::AddressRange *addr_range_ptr = nullptr); 380 381 /// Set the address to represent \a load_addr. 382 /// 383 /// The address will attempt to find a loaded section within \a target that 384 /// contains \a load_addr. If successful, this address object will have a 385 /// valid section and offset. Else this address object will have no section 386 /// (NULL) and the offset will be \a load_addr. 387 /// 388 /// \param[in] load_addr 389 /// A load address from a current process. 390 /// 391 /// \param[in] target 392 /// The target to use when trying resolve the address into 393 /// a section + offset. The Target's SectionLoadList object 394 /// is used to resolve the address. 395 /// 396 /// \param[in] allow_section_end 397 /// If true, treat an address pointing to the end of the module as 398 /// belonging to that module. 399 /// 400 /// \return 401 /// Returns \b true if the load address was resolved to be 402 /// section/offset, \b false otherwise. It is often ok for an 403 /// address to not resolve to a section in a module, this often 404 /// happens for JIT'ed code, or any load addresses on the stack 405 /// or heap. 406 bool SetLoadAddress(lldb::addr_t load_addr, Target *target, 407 bool allow_section_end = false); 408 409 bool SetOpcodeLoadAddress( 410 lldb::addr_t load_addr, Target *target, 411 AddressClass addr_class = AddressClass::eInvalid, 412 bool allow_section_end = false); 413 414 bool SetCallableLoadAddress(lldb::addr_t load_addr, Target *target); 415 416 /// Get accessor for the module for this address. 417 /// 418 /// \return 419 /// Returns the Module pointer that this address is an offset 420 /// in, or NULL if this address doesn't belong in a module, or 421 /// isn't resolved yet. 422 lldb::ModuleSP GetModule() const; 423 424 /// Get const accessor for the section. 425 /// 426 /// \return 427 /// Returns the const lldb::Section pointer that this address is an 428 /// offset in, or NULL if this address is absolute. 429 lldb::SectionSP GetSection() const { return m_section_wp.lock(); } 430 431 /// Set accessor for the offset. 432 /// 433 /// \param[in] offset 434 /// A new offset value for this object. 435 /// 436 /// \return 437 /// Returns \b true if the offset changed, \b false otherwise. 438 bool SetOffset(lldb::addr_t offset) { 439 bool changed = m_offset != offset; 440 m_offset = offset; 441 return changed; 442 } 443 444 void SetRawAddress(lldb::addr_t addr) { 445 m_section_wp.reset(); 446 m_offset = addr; 447 } 448 449 bool Slide(int64_t offset) { 450 if (m_offset != LLDB_INVALID_ADDRESS) { 451 m_offset += offset; 452 return true; 453 } 454 return false; 455 } 456 457 /// Set accessor for the section. 458 /// 459 /// \param[in] section_sp 460 /// A new lldb::Section pointer to use as the section base. Can 461 /// be NULL for absolute addresses that are not relative to 462 /// any section. 463 void SetSection(const lldb::SectionSP §ion_sp) { 464 m_section_wp = section_sp; 465 } 466 467 void ClearSection() { m_section_wp.reset(); } 468 469 /// Reconstruct a symbol context from an address. 470 /// 471 /// This class doesn't inherit from SymbolContextScope because many address 472 /// objects have short lifespans. Address objects that are section offset 473 /// can reconstruct their symbol context by looking up the address in the 474 /// module found in the section. 475 /// 476 /// \see SymbolContextScope::CalculateSymbolContext(SymbolContext*) 477 uint32_t CalculateSymbolContext(SymbolContext *sc, 478 lldb::SymbolContextItem resolve_scope = 479 lldb::eSymbolContextEverything) const; 480 481 lldb::ModuleSP CalculateSymbolContextModule() const; 482 483 CompileUnit *CalculateSymbolContextCompileUnit() const; 484 485 Function *CalculateSymbolContextFunction() const; 486 487 Block *CalculateSymbolContextBlock() const; 488 489 Symbol *CalculateSymbolContextSymbol() const; 490 491 bool CalculateSymbolContextLineEntry(LineEntry &line_entry) const; 492 493 // Returns true if the section should be valid, but isn't because the shared 494 // pointer to the section can't be reconstructed from a weak pointer that 495 // contains a valid weak reference to a section. Returns false if the section 496 // weak pointer has no reference to a section, or if the section is still 497 // valid 498 bool SectionWasDeleted() const; 499 500 protected: 501 // Member variables. 502 lldb::SectionWP m_section_wp; ///< The section for the address, can be NULL. 503 lldb::addr_t m_offset = LLDB_INVALID_ADDRESS; ///< Offset into section if \a 504 ///< m_section_wp is valid... 505 506 // Returns true if the m_section_wp once had a reference to a valid section 507 // shared pointer, but no longer does. This can happen if we have an address 508 // from a module that gets unloaded and deleted. This function should only be 509 // called if GetSection() returns an empty shared pointer and you want to 510 // know if this address used to have a valid section. 511 bool SectionWasDeletedPrivate() const; 512 }; 513 514 // NOTE: Be careful using this operator. It can correctly compare two 515 // addresses from the same Module correctly. It can't compare two addresses 516 // from different modules in any meaningful way, but it will compare the module 517 // pointers. 518 // 519 // To sum things up: 520 // - works great for addresses within the same module - it works for addresses 521 // across multiple modules, but don't expect the 522 // address results to make much sense 523 // 524 // This basically lets Address objects be used in ordered collection classes. 525 bool operator<(const Address &lhs, const Address &rhs); 526 bool operator>(const Address &lhs, const Address &rhs); 527 bool operator==(const Address &lhs, const Address &rhs); 528 bool operator!=(const Address &lhs, const Address &rhs); 529 530 } // namespace lldb_private 531 532 #endif // LLDB_CORE_ADDRESS_H 533