10b57cec5SDimitry Andric //===-- Address.h -----------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_CORE_ADDRESS_H
105ffd83dbSDimitry Andric #define LLDB_CORE_ADDRESS_H
110b57cec5SDimitry Andric 
127a6dacacSDimitry Andric #include "lldb/Utility/Stream.h"
130b57cec5SDimitry Andric #include "lldb/lldb-defines.h"
140b57cec5SDimitry Andric #include "lldb/lldb-forward.h"
150b57cec5SDimitry Andric #include "lldb/lldb-private-enumerations.h"
160b57cec5SDimitry Andric #include "lldb/lldb-types.h"
170b57cec5SDimitry Andric 
185f757f3fSDimitry Andric #include "llvm/ADT/StringRef.h"
195f757f3fSDimitry Andric 
20fe6060f1SDimitry Andric #include <cstddef>
21fe6060f1SDimitry Andric #include <cstdint>
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace lldb_private {
240b57cec5SDimitry Andric class Block;
250b57cec5SDimitry Andric class CompileUnit;
260b57cec5SDimitry Andric class ExecutionContextScope;
270b57cec5SDimitry Andric class Function;
280b57cec5SDimitry Andric class SectionList;
290b57cec5SDimitry Andric class Stream;
300b57cec5SDimitry Andric class Symbol;
310b57cec5SDimitry Andric class SymbolContext;
320b57cec5SDimitry Andric class Target;
330b57cec5SDimitry Andric struct LineEntry;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric /// \class Address Address.h "lldb/Core/Address.h"
360b57cec5SDimitry Andric /// A section + offset based address class.
370b57cec5SDimitry Andric ///
380b57cec5SDimitry Andric /// The Address class allows addresses to be relative to a section that can
390b57cec5SDimitry Andric /// move during runtime due to images (executables, shared libraries, bundles,
400b57cec5SDimitry Andric /// frameworks) being loaded at different addresses than the addresses found
410b57cec5SDimitry Andric /// in the object file that represents them on disk. There are currently two
420b57cec5SDimitry Andric /// types of addresses for a section:
430b57cec5SDimitry Andric ///     \li file addresses
440b57cec5SDimitry Andric ///     \li load addresses
450b57cec5SDimitry Andric ///
460b57cec5SDimitry Andric /// File addresses represent the virtual addresses that are in the "on disk"
470b57cec5SDimitry Andric /// object files. These virtual addresses are converted to be relative to
480b57cec5SDimitry Andric /// unique sections scoped to the object file so that when/if the addresses
490b57cec5SDimitry Andric /// slide when the images are loaded/unloaded in memory, we can easily track
500b57cec5SDimitry Andric /// these changes without having to update every object (compile unit ranges,
510b57cec5SDimitry Andric /// line tables, function address ranges, lexical block and inlined subroutine
520b57cec5SDimitry Andric /// address ranges, global and static variables) each time an image is loaded
530b57cec5SDimitry Andric /// or unloaded.
540b57cec5SDimitry Andric ///
550b57cec5SDimitry Andric /// Load addresses represent the virtual addresses where each section ends up
560b57cec5SDimitry Andric /// getting loaded at runtime. Before executing a program, it is common for
570b57cec5SDimitry Andric /// all of the load addresses to be unresolved. When a DynamicLoader plug-in
580b57cec5SDimitry Andric /// receives notification that shared libraries have been loaded/unloaded, the
590b57cec5SDimitry Andric /// load addresses of the main executable and any images (shared libraries)
600b57cec5SDimitry Andric /// will be  resolved/unresolved. When this happens, breakpoints that are in
610b57cec5SDimitry Andric /// one of these sections can be set/cleared.
620b57cec5SDimitry Andric class Address {
630b57cec5SDimitry Andric public:
640b57cec5SDimitry Andric   /// Dump styles allow the Address::Dump(Stream *,DumpStyle) const function
650b57cec5SDimitry Andric   /// to display Address contents in a variety of ways.
660b57cec5SDimitry Andric   enum DumpStyle {
67480093f4SDimitry Andric     /// Invalid dump style.
68480093f4SDimitry Andric     DumpStyleInvalid,
69480093f4SDimitry Andric     /// Display as the section name + offset.
70480093f4SDimitry Andric     /// \code
710b57cec5SDimitry Andric     /// // address for printf in libSystem.B.dylib as a section name + offset
72480093f4SDimitry Andric     /// libSystem.B.dylib.__TEXT.__text + 0x0005cfdf
73480093f4SDimitry Andric     /// \endcode
74480093f4SDimitry Andric     DumpStyleSectionNameOffset,
75480093f4SDimitry Andric     /// Display as the section pointer + offset (debug output).
76480093f4SDimitry Andric     /// \code
770b57cec5SDimitry Andric     /// // address for printf in libSystem.B.dylib as a section pointer +
78480093f4SDimitry Andric     /// offset (lldb::Section *)0x35cc50 + 0x000000000005cfdf
79480093f4SDimitry Andric     /// \endcode
80480093f4SDimitry Andric     DumpStyleSectionPointerOffset,
81480093f4SDimitry Andric     /// Display as the file address (if any).
82480093f4SDimitry Andric     /// \code
830b57cec5SDimitry Andric     /// // address for printf in libSystem.B.dylib as a file address
84480093f4SDimitry Andric     /// 0x000000000005dcff
85480093f4SDimitry Andric     /// \endcode
86480093f4SDimitry Andric     ///
87480093f4SDimitry Andric     DumpStyleFileAddress,
88480093f4SDimitry Andric     /// Display as the file address with the module name prepended (if any).
89480093f4SDimitry Andric     /// \code
900b57cec5SDimitry Andric     /// // address for printf in libSystem.B.dylib as a file address
91480093f4SDimitry Andric     /// libSystem.B.dylib[0x000000000005dcff]
92480093f4SDimitry Andric     /// \endcode
93480093f4SDimitry Andric     DumpStyleModuleWithFileAddress,
94480093f4SDimitry Andric     /// Display as the load address (if resolved).
95480093f4SDimitry Andric     /// \code
960b57cec5SDimitry Andric     /// // address for printf in libSystem.B.dylib as a load address
97480093f4SDimitry Andric     /// 0x00007fff8306bcff
98480093f4SDimitry Andric     /// \endcode
99480093f4SDimitry Andric     DumpStyleLoadAddress,
100480093f4SDimitry Andric     /// Display the details about what an address resolves to. This can be
101480093f4SDimitry Andric     /// anything from a symbol context summary (module, function/symbol, and
102480093f4SDimitry Andric     /// file and line), to information about what the pointer points to if the
103480093f4SDimitry Andric     /// address is in a section (section of pointers, c strings, etc).
104480093f4SDimitry Andric     DumpStyleResolvedDescription,
1050b57cec5SDimitry Andric     DumpStyleResolvedDescriptionNoModule,
1060b57cec5SDimitry Andric     DumpStyleResolvedDescriptionNoFunctionArguments,
107480093f4SDimitry Andric     /// Elide the function name; display an offset into the current function.
108480093f4SDimitry Andric     /// Used primarily in disassembly symbolication
109480093f4SDimitry Andric     DumpStyleNoFunctionName,
110480093f4SDimitry Andric     /// Detailed symbol context information for an address for all symbol
111480093f4SDimitry Andric     /// context members.
112480093f4SDimitry Andric     DumpStyleDetailedSymbolContext,
113480093f4SDimitry Andric     /// Dereference a pointer at the current address and then lookup the
114480093f4SDimitry Andric     /// dereferenced address using DumpStyleResolvedDescription
115480093f4SDimitry Andric     DumpStyleResolvedPointerDescription
1160b57cec5SDimitry Andric   };
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   /// Default constructor.
1190b57cec5SDimitry Andric   ///
1200b57cec5SDimitry Andric   /// Initialize with a invalid section (NULL) and an invalid offset
1210b57cec5SDimitry Andric   /// (LLDB_INVALID_ADDRESS).
12281ad6265SDimitry Andric   Address() = default;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   /// Copy constructor
1250b57cec5SDimitry Andric   ///
1260b57cec5SDimitry Andric   /// Makes a copy of the another Address object \a rhs.
1270b57cec5SDimitry Andric   ///
1280b57cec5SDimitry Andric   /// \param[in] rhs
1290b57cec5SDimitry Andric   ///     A const Address object reference to copy.
Address(const Address & rhs)1300b57cec5SDimitry Andric   Address(const Address &rhs)
1310b57cec5SDimitry Andric       : m_section_wp(rhs.m_section_wp), m_offset(rhs.m_offset) {}
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   /// Construct with a section pointer and offset.
1340b57cec5SDimitry Andric   ///
1350b57cec5SDimitry Andric   /// Initialize the address with the supplied \a section and \a offset.
1360b57cec5SDimitry Andric   ///
137480093f4SDimitry Andric   /// \param[in] section_sp
1380b57cec5SDimitry Andric   ///     A section pointer to a valid lldb::Section, or NULL if the
1390b57cec5SDimitry Andric   ///     address doesn't have a section or will get resolved later.
1400b57cec5SDimitry Andric   ///
1410b57cec5SDimitry Andric   /// \param[in] offset
1420b57cec5SDimitry Andric   ///     The offset in bytes into \a section.
Address(const lldb::SectionSP & section_sp,lldb::addr_t offset)1430b57cec5SDimitry Andric   Address(const lldb::SectionSP &section_sp, lldb::addr_t offset)
1440b57cec5SDimitry Andric       : m_section_wp(), // Don't init with section_sp in case section_sp is
1450b57cec5SDimitry Andric                         // invalid (the weak_ptr will throw)
1460b57cec5SDimitry Andric         m_offset(offset) {
1470b57cec5SDimitry Andric     if (section_sp)
1480b57cec5SDimitry Andric       m_section_wp = section_sp;
1490b57cec5SDimitry Andric   }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   /// Construct with a virtual address and section list.
1520b57cec5SDimitry Andric   ///
1530b57cec5SDimitry Andric   /// Initialize and resolve the address with the supplied virtual address \a
1540b57cec5SDimitry Andric   /// file_addr.
1550b57cec5SDimitry Andric   ///
1560b57cec5SDimitry Andric   /// \param[in] file_addr
1570b57cec5SDimitry Andric   ///     A virtual file address.
1580b57cec5SDimitry Andric   ///
1590b57cec5SDimitry Andric   /// \param[in] section_list
1600b57cec5SDimitry Andric   ///     A list of sections, one of which may contain the \a file_addr.
1610b57cec5SDimitry Andric   Address(lldb::addr_t file_addr, const SectionList *section_list);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   Address(lldb::addr_t abs_addr);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric /// Assignment operator.
1660b57cec5SDimitry Andric ///
1670b57cec5SDimitry Andric /// Copies the address value from another Address object \a rhs into \a this
1680b57cec5SDimitry Andric /// object.
1690b57cec5SDimitry Andric ///
1700b57cec5SDimitry Andric /// \param[in] rhs
1710b57cec5SDimitry Andric ///     A const Address object reference to copy.
1720b57cec5SDimitry Andric ///
1730b57cec5SDimitry Andric /// \return
1740b57cec5SDimitry Andric ///     A const Address object reference to \a this.
1750b57cec5SDimitry Andric   const Address &operator=(const Address &rhs);
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   /// Clear the object's state.
1780b57cec5SDimitry Andric   ///
1790b57cec5SDimitry Andric   /// Sets the section to an invalid value (NULL) and an invalid offset
1800b57cec5SDimitry Andric   /// (LLDB_INVALID_ADDRESS).
Clear()1810b57cec5SDimitry Andric   void Clear() {
1820b57cec5SDimitry Andric     m_section_wp.reset();
1830b57cec5SDimitry Andric     m_offset = LLDB_INVALID_ADDRESS;
1840b57cec5SDimitry Andric   }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   /// Compare two Address objects.
1870b57cec5SDimitry Andric   ///
1880b57cec5SDimitry Andric   /// \param[in] lhs
1890b57cec5SDimitry Andric   ///     The Left Hand Side const Address object reference.
1900b57cec5SDimitry Andric   ///
1910b57cec5SDimitry Andric   /// \param[in] rhs
1920b57cec5SDimitry Andric   ///     The Right Hand Side const Address object reference.
1930b57cec5SDimitry Andric   ///
1940b57cec5SDimitry Andric   /// \return
195480093f4SDimitry Andric   ///     -1 if lhs < rhs
196480093f4SDimitry Andric   ///     0 if lhs == rhs
197480093f4SDimitry Andric   ///     1 if lhs > rhs
1980b57cec5SDimitry Andric   static int CompareFileAddress(const Address &lhs, const Address &rhs);
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   static int CompareLoadAddress(const Address &lhs, const Address &rhs,
2010b57cec5SDimitry Andric                                 Target *target);
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   static int CompareModulePointerAndOffset(const Address &lhs,
2040b57cec5SDimitry Andric                                            const Address &rhs);
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   // For use with std::map, std::multi_map
2070b57cec5SDimitry Andric   class ModulePointerAndOffsetLessThanFunctionObject {
2080b57cec5SDimitry Andric   public:
2090b57cec5SDimitry Andric     ModulePointerAndOffsetLessThanFunctionObject() = default;
2100b57cec5SDimitry Andric 
operator()2110b57cec5SDimitry Andric     bool operator()(const Address &a, const Address &b) const {
2120b57cec5SDimitry Andric       return Address::CompareModulePointerAndOffset(a, b) < 0;
2130b57cec5SDimitry Andric     }
2140b57cec5SDimitry Andric   };
2150b57cec5SDimitry Andric 
216349cc55cSDimitry Andric   /// Write a description of this object to a Stream.
217349cc55cSDimitry Andric   bool GetDescription(Stream &s, Target &target,
218349cc55cSDimitry Andric                       lldb::DescriptionLevel level) const;
219349cc55cSDimitry Andric 
2200b57cec5SDimitry Andric   /// Dump a description of this object to a Stream.
2210b57cec5SDimitry Andric   ///
2220b57cec5SDimitry Andric   /// Dump a description of the contents of this object to the supplied stream
2230b57cec5SDimitry Andric   /// \a s. There are many ways to display a section offset based address, and
2240b57cec5SDimitry Andric   /// \a style lets the user choose.
2250b57cec5SDimitry Andric   ///
2260b57cec5SDimitry Andric   /// \param[in] s
2270b57cec5SDimitry Andric   ///     The stream to which to dump the object description.
2280b57cec5SDimitry Andric   ///
2290b57cec5SDimitry Andric   /// \param[in] style
2300b57cec5SDimitry Andric   ///     The display style for the address.
2310b57cec5SDimitry Andric   ///
2320b57cec5SDimitry Andric   /// \param[in] fallback_style
2330b57cec5SDimitry Andric   ///     The display style for the address.
2340b57cec5SDimitry Andric   ///
23581ad6265SDimitry Andric   /// \param[in] addr_byte_size
23681ad6265SDimitry Andric   ///     The address byte size for the address.
23781ad6265SDimitry Andric   ///
23881ad6265SDimitry Andric   /// \param[in] all_ranges
23981ad6265SDimitry Andric   ///     If true, dump all valid ranges and value ranges for the variable that
24081ad6265SDimitry Andric   ///     contains the address, otherwise dumping the range that contains the
24181ad6265SDimitry Andric   ///     address.
24281ad6265SDimitry Andric   ///
2435f757f3fSDimitry Andric   /// \param[in] pattern
2445f757f3fSDimitry Andric   ///     An optional regex pattern to match against the description. If
2455f757f3fSDimitry Andric   ///     specified, parts of the description matching this pattern may be
2465f757f3fSDimitry Andric   ///     highlighted or processed differently. If this parameter is an empty
2475f757f3fSDimitry Andric   ///     string or not provided, no highlighting is applied.
2485f757f3fSDimitry Andric   ///
2490b57cec5SDimitry Andric   /// \return
2500b57cec5SDimitry Andric   ///     Returns \b true if the address was able to be displayed.
2510b57cec5SDimitry Andric   ///     File and load addresses may be unresolved and it may not be
2520b57cec5SDimitry Andric   ///     possible to display a valid value, \b false will be returned
2530b57cec5SDimitry Andric   ///     in such cases.
2540b57cec5SDimitry Andric   ///
2550b57cec5SDimitry Andric   /// \see Address::DumpStyle
2567a6dacacSDimitry Andric   bool
2577a6dacacSDimitry Andric   Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
2580b57cec5SDimitry Andric        DumpStyle fallback_style = DumpStyleInvalid,
2595f757f3fSDimitry Andric        uint32_t addr_byte_size = UINT32_MAX, bool all_ranges = false,
2607a6dacacSDimitry Andric        std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   AddressClass GetAddressClass() const;
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   /// Get the file address.
2650b57cec5SDimitry Andric   ///
2660b57cec5SDimitry Andric   /// If an address comes from a file on disk that has section relative
2670b57cec5SDimitry Andric   /// addresses, then it has a virtual address that is relative to unique
2680b57cec5SDimitry Andric   /// section in the object file.
2690b57cec5SDimitry Andric   ///
2700b57cec5SDimitry Andric   /// \return
2710b57cec5SDimitry Andric   ///     The valid file virtual address, or LLDB_INVALID_ADDRESS if
2720b57cec5SDimitry Andric   ///     the address doesn't have a file virtual address (image is
2730b57cec5SDimitry Andric   ///     from memory only with no representation on disk).
2740b57cec5SDimitry Andric   lldb::addr_t GetFileAddress() const;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   /// Get the load address.
2770b57cec5SDimitry Andric   ///
2780b57cec5SDimitry Andric   /// If an address comes from a file on disk that has section relative
2790b57cec5SDimitry Andric   /// addresses, then it has a virtual address that is relative to unique
2800b57cec5SDimitry Andric   /// section in the object file. Sections get resolved at runtime by
2810b57cec5SDimitry Andric   /// DynamicLoader plug-ins as images (executables and shared libraries) get
2820b57cec5SDimitry Andric   /// loaded/unloaded. If a section is loaded, then the load address can be
2830b57cec5SDimitry Andric   /// resolved.
2840b57cec5SDimitry Andric   ///
2850b57cec5SDimitry Andric   /// \return
2860b57cec5SDimitry Andric   ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
2870b57cec5SDimitry Andric   ///     the address is currently not loaded.
2880b57cec5SDimitry Andric   lldb::addr_t GetLoadAddress(Target *target) const;
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   /// Get the load address as a callable code load address.
2910b57cec5SDimitry Andric   ///
2920b57cec5SDimitry Andric   /// This function will first resolve its address to a load address. Then, if
2930b57cec5SDimitry Andric   /// the address turns out to be in code address, return the load address
2940b57cec5SDimitry Andric   /// that would be required to call or return to. The address might have
2950b57cec5SDimitry Andric   /// extra bits set (bit zero will be set to Thumb functions for an ARM
2960b57cec5SDimitry Andric   /// target) that are required when changing the program counter to setting a
2970b57cec5SDimitry Andric   /// return address.
2980b57cec5SDimitry Andric   ///
2990b57cec5SDimitry Andric   /// \return
3000b57cec5SDimitry Andric   ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
3010b57cec5SDimitry Andric   ///     the address is currently not loaded.
3020b57cec5SDimitry Andric   lldb::addr_t GetCallableLoadAddress(Target *target,
3030b57cec5SDimitry Andric                                       bool is_indirect = false) const;
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   /// Get the load address as an opcode load address.
3060b57cec5SDimitry Andric   ///
3070b57cec5SDimitry Andric   /// This function will first resolve its address to a load address. Then, if
3080b57cec5SDimitry Andric   /// the address turns out to be in code address, return the load address for
3090b57cec5SDimitry Andric   /// an opcode. This address object might have extra bits set (bit zero will
3100b57cec5SDimitry Andric   /// be set to Thumb functions for an
3110b57cec5SDimitry Andric   /// ARM target) that are required for changing the program counter
3120b57cec5SDimitry Andric   /// and this function will remove any bits that are intended for these
3130b57cec5SDimitry Andric   /// special purposes. The result of this function can be used to safely
3140b57cec5SDimitry Andric   /// write a software breakpoint trap to memory.
3150b57cec5SDimitry Andric   ///
3160b57cec5SDimitry Andric   /// \return
3170b57cec5SDimitry Andric   ///     The valid load virtual address with extra callable bits
3180b57cec5SDimitry Andric   ///     removed, or LLDB_INVALID_ADDRESS if the address is currently
3190b57cec5SDimitry Andric   ///     not loaded.
3200b57cec5SDimitry Andric   lldb::addr_t GetOpcodeLoadAddress(
3210b57cec5SDimitry Andric       Target *target,
3220b57cec5SDimitry Andric       AddressClass addr_class = AddressClass::eInvalid) const;
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   /// Get the section relative offset value.
3250b57cec5SDimitry Andric   ///
3260b57cec5SDimitry Andric   /// \return
3270b57cec5SDimitry Andric   ///     The current offset, or LLDB_INVALID_ADDRESS if this address
3280b57cec5SDimitry Andric   ///     doesn't contain a valid offset.
GetOffset()3290b57cec5SDimitry Andric   lldb::addr_t GetOffset() const { return m_offset; }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   /// Check if an address is section offset.
3320b57cec5SDimitry Andric   ///
3330b57cec5SDimitry Andric   /// When converting a virtual file or load address into a section offset
3340b57cec5SDimitry Andric   /// based address, we often need to know if, given a section list, if the
3350b57cec5SDimitry Andric   /// address was able to be converted to section offset. This function
3360b57cec5SDimitry Andric   /// returns true if the current value contained in this object is section
3370b57cec5SDimitry Andric   /// offset based.
3380b57cec5SDimitry Andric   ///
3390b57cec5SDimitry Andric   /// \return
3400b57cec5SDimitry Andric   ///     Returns \b true if the address has a valid section and
3410b57cec5SDimitry Andric   ///     offset, \b false otherwise.
IsSectionOffset()3420b57cec5SDimitry Andric   bool IsSectionOffset() const {
3430b57cec5SDimitry Andric     return IsValid() && (GetSection().get() != nullptr);
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   /// Check if the object state is valid.
3470b57cec5SDimitry Andric   ///
3480b57cec5SDimitry Andric   /// A valid Address object contains either a section pointer and
3490b57cec5SDimitry Andric   /// offset (for section offset based addresses), or just a valid offset
3500b57cec5SDimitry Andric   /// (for absolute addresses that have no section).
3510b57cec5SDimitry Andric   ///
3520b57cec5SDimitry Andric   /// \return
3530b57cec5SDimitry Andric   ///     Returns \b true if the offset is valid, \b false
3540b57cec5SDimitry Andric   ///     otherwise.
IsValid()3550b57cec5SDimitry Andric   bool IsValid() const { return m_offset != LLDB_INVALID_ADDRESS; }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   /// Get the memory cost of this object.
3580b57cec5SDimitry Andric   ///
3590b57cec5SDimitry Andric   /// \return
3600b57cec5SDimitry Andric   ///     The number of bytes that this object occupies in memory.
3610b57cec5SDimitry Andric   size_t MemorySize() const;
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   /// Resolve a file virtual address using a section list.
3640b57cec5SDimitry Andric   ///
3650b57cec5SDimitry Andric   /// Given a list of sections, attempt to resolve \a addr as an offset into
3660b57cec5SDimitry Andric   /// one of the file sections.
3670b57cec5SDimitry Andric   ///
3680b57cec5SDimitry Andric   /// \return
3690b57cec5SDimitry Andric   ///     Returns \b true if \a addr was able to be resolved, \b false
3700b57cec5SDimitry Andric   ///     otherwise.
3710b57cec5SDimitry Andric   bool ResolveAddressUsingFileSections(lldb::addr_t addr,
3720b57cec5SDimitry Andric                                        const SectionList *sections);
3730b57cec5SDimitry Andric 
3749dba64beSDimitry Andric   /// Resolve this address to its containing function and optionally get
3759dba64beSDimitry Andric   /// that function's address range.
3769dba64beSDimitry Andric   ///
3779dba64beSDimitry Andric   /// \param[out] sym_ctx
3789dba64beSDimitry Andric   ///     The symbol context describing the function in which this address lies
3799dba64beSDimitry Andric   ///
3809dba64beSDimitry Andric   /// \parm[out] addr_range_ptr
3819dba64beSDimitry Andric   ///     Pointer to the AddressRange to fill in with the function's address
3829dba64beSDimitry Andric   ///     range.  Caller may pass null if they don't need the address range.
3839dba64beSDimitry Andric   ///
3849dba64beSDimitry Andric   /// \return
3859dba64beSDimitry Andric   ///     Returns \b false if the function/symbol could not be resolved
3869dba64beSDimitry Andric   ///     or if the address range was requested and could not be resolved;
3879dba64beSDimitry Andric   ///     returns \b true otherwise.
3889dba64beSDimitry Andric   bool ResolveFunctionScope(lldb_private::SymbolContext &sym_ctx,
3899dba64beSDimitry Andric                             lldb_private::AddressRange *addr_range_ptr = nullptr);
3909dba64beSDimitry Andric 
3910b57cec5SDimitry Andric   /// Set the address to represent \a load_addr.
3920b57cec5SDimitry Andric   ///
3930b57cec5SDimitry Andric   /// The address will attempt to find a loaded section within \a target that
3940b57cec5SDimitry Andric   /// contains \a load_addr. If successful, this address object will have a
3950b57cec5SDimitry Andric   /// valid section and offset. Else this address object will have no section
3960b57cec5SDimitry Andric   /// (NULL) and the offset will be \a load_addr.
3970b57cec5SDimitry Andric   ///
3980b57cec5SDimitry Andric   /// \param[in] load_addr
3990b57cec5SDimitry Andric   ///     A load address from a current process.
4000b57cec5SDimitry Andric   ///
4010b57cec5SDimitry Andric   /// \param[in] target
4020b57cec5SDimitry Andric   ///     The target to use when trying resolve the address into
4030b57cec5SDimitry Andric   ///     a section + offset. The Target's SectionLoadList object
4040b57cec5SDimitry Andric   ///     is used to resolve the address.
4050b57cec5SDimitry Andric   ///
4060b57cec5SDimitry Andric   /// \param[in] allow_section_end
4070b57cec5SDimitry Andric   ///     If true, treat an address pointing to the end of the module as
4080b57cec5SDimitry Andric   ///     belonging to that module.
4090b57cec5SDimitry Andric   ///
4100b57cec5SDimitry Andric   /// \return
4110b57cec5SDimitry Andric   ///     Returns \b true if the load address was resolved to be
4120b57cec5SDimitry Andric   ///     section/offset, \b false otherwise. It is often ok for an
4130b57cec5SDimitry Andric   ///     address to not resolve to a section in a module, this often
4140b57cec5SDimitry Andric   ///     happens for JIT'ed code, or any load addresses on the stack
4150b57cec5SDimitry Andric   ///     or heap.
4160b57cec5SDimitry Andric   bool SetLoadAddress(lldb::addr_t load_addr, Target *target,
4170b57cec5SDimitry Andric                       bool allow_section_end = false);
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   bool SetOpcodeLoadAddress(
4200b57cec5SDimitry Andric       lldb::addr_t load_addr, Target *target,
4210b57cec5SDimitry Andric       AddressClass addr_class = AddressClass::eInvalid,
4220b57cec5SDimitry Andric       bool allow_section_end = false);
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   bool SetCallableLoadAddress(lldb::addr_t load_addr, Target *target);
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   /// Get accessor for the module for this address.
4270b57cec5SDimitry Andric   ///
4280b57cec5SDimitry Andric   /// \return
4290b57cec5SDimitry Andric   ///     Returns the Module pointer that this address is an offset
4300b57cec5SDimitry Andric   ///     in, or NULL if this address doesn't belong in a module, or
4310b57cec5SDimitry Andric   ///     isn't resolved yet.
4320b57cec5SDimitry Andric   lldb::ModuleSP GetModule() const;
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric   /// Get const accessor for the section.
4350b57cec5SDimitry Andric   ///
4360b57cec5SDimitry Andric   /// \return
4370b57cec5SDimitry Andric   ///     Returns the const lldb::Section pointer that this address is an
4380b57cec5SDimitry Andric   ///     offset in, or NULL if this address is absolute.
GetSection()4390b57cec5SDimitry Andric   lldb::SectionSP GetSection() const { return m_section_wp.lock(); }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric   /// Set accessor for the offset.
4420b57cec5SDimitry Andric   ///
4430b57cec5SDimitry Andric   /// \param[in] offset
4440b57cec5SDimitry Andric   ///     A new offset value for this object.
4450b57cec5SDimitry Andric   ///
4460b57cec5SDimitry Andric   /// \return
4470b57cec5SDimitry Andric   ///     Returns \b true if the offset changed, \b false otherwise.
SetOffset(lldb::addr_t offset)4480b57cec5SDimitry Andric   bool SetOffset(lldb::addr_t offset) {
4490b57cec5SDimitry Andric     bool changed = m_offset != offset;
4500b57cec5SDimitry Andric     m_offset = offset;
4510b57cec5SDimitry Andric     return changed;
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric 
SetRawAddress(lldb::addr_t addr)4540b57cec5SDimitry Andric   void SetRawAddress(lldb::addr_t addr) {
4550b57cec5SDimitry Andric     m_section_wp.reset();
4560b57cec5SDimitry Andric     m_offset = addr;
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric 
Slide(int64_t offset)4590b57cec5SDimitry Andric   bool Slide(int64_t offset) {
4600b57cec5SDimitry Andric     if (m_offset != LLDB_INVALID_ADDRESS) {
4610b57cec5SDimitry Andric       m_offset += offset;
4620b57cec5SDimitry Andric       return true;
4630b57cec5SDimitry Andric     }
4640b57cec5SDimitry Andric     return false;
4650b57cec5SDimitry Andric   }
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   /// Set accessor for the section.
4680b57cec5SDimitry Andric   ///
469480093f4SDimitry Andric   /// \param[in] section_sp
4700b57cec5SDimitry Andric   ///     A new lldb::Section pointer to use as the section base. Can
4710b57cec5SDimitry Andric   ///     be NULL for absolute addresses that are not relative to
4720b57cec5SDimitry Andric   ///     any section.
SetSection(const lldb::SectionSP & section_sp)4730b57cec5SDimitry Andric   void SetSection(const lldb::SectionSP &section_sp) {
4740b57cec5SDimitry Andric     m_section_wp = section_sp;
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric 
ClearSection()4770b57cec5SDimitry Andric   void ClearSection() { m_section_wp.reset(); }
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   /// Reconstruct a symbol context from an address.
4800b57cec5SDimitry Andric   ///
4810b57cec5SDimitry Andric   /// This class doesn't inherit from SymbolContextScope because many address
4820b57cec5SDimitry Andric   /// objects have short lifespans. Address objects that are section offset
4830b57cec5SDimitry Andric   /// can reconstruct their symbol context by looking up the address in the
4840b57cec5SDimitry Andric   /// module found in the section.
4850b57cec5SDimitry Andric   ///
4860b57cec5SDimitry Andric   /// \see SymbolContextScope::CalculateSymbolContext(SymbolContext*)
4870b57cec5SDimitry Andric   uint32_t CalculateSymbolContext(SymbolContext *sc,
4880b57cec5SDimitry Andric                                   lldb::SymbolContextItem resolve_scope =
4890b57cec5SDimitry Andric                                       lldb::eSymbolContextEverything) const;
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   lldb::ModuleSP CalculateSymbolContextModule() const;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   CompileUnit *CalculateSymbolContextCompileUnit() const;
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   Function *CalculateSymbolContextFunction() const;
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   Block *CalculateSymbolContextBlock() const;
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   Symbol *CalculateSymbolContextSymbol() const;
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   bool CalculateSymbolContextLineEntry(LineEntry &line_entry) const;
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   // Returns true if the section should be valid, but isn't because the shared
5040b57cec5SDimitry Andric   // pointer to the section can't be reconstructed from a weak pointer that
5050b57cec5SDimitry Andric   // contains a valid weak reference to a section. Returns false if the section
5060b57cec5SDimitry Andric   // weak pointer has no reference to a section, or if the section is still
5070b57cec5SDimitry Andric   // valid
5080b57cec5SDimitry Andric   bool SectionWasDeleted() const;
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric protected:
5110b57cec5SDimitry Andric   // Member variables.
5120b57cec5SDimitry Andric   lldb::SectionWP m_section_wp; ///< The section for the address, can be NULL.
513fe6060f1SDimitry Andric   lldb::addr_t m_offset = LLDB_INVALID_ADDRESS; ///< Offset into section if \a
514fe6060f1SDimitry Andric                                                 ///< m_section_wp is valid...
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   // Returns true if the m_section_wp once had a reference to a valid section
5170b57cec5SDimitry Andric   // shared pointer, but no longer does. This can happen if we have an address
5180b57cec5SDimitry Andric   // from a module that gets unloaded and deleted. This function should only be
5190b57cec5SDimitry Andric   // called if GetSection() returns an empty shared pointer and you want to
5200b57cec5SDimitry Andric   // know if this address used to have a valid section.
5210b57cec5SDimitry Andric   bool SectionWasDeletedPrivate() const;
5220b57cec5SDimitry Andric };
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric // NOTE: Be careful using this operator. It can correctly compare two
5250b57cec5SDimitry Andric // addresses from the same Module correctly. It can't compare two addresses
5260b57cec5SDimitry Andric // from different modules in any meaningful way, but it will compare the module
5270b57cec5SDimitry Andric // pointers.
5280b57cec5SDimitry Andric //
5290b57cec5SDimitry Andric // To sum things up:
5300b57cec5SDimitry Andric // - works great for addresses within the same module - it works for addresses
5310b57cec5SDimitry Andric // across multiple modules, but don't expect the
5320b57cec5SDimitry Andric //   address results to make much sense
5330b57cec5SDimitry Andric //
5340b57cec5SDimitry Andric // This basically lets Address objects be used in ordered collection classes.
5350b57cec5SDimitry Andric bool operator<(const Address &lhs, const Address &rhs);
5360b57cec5SDimitry Andric bool operator>(const Address &lhs, const Address &rhs);
5370b57cec5SDimitry Andric bool operator==(const Address &lhs, const Address &rhs);
5380b57cec5SDimitry Andric bool operator!=(const Address &lhs, const Address &rhs);
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric } // namespace lldb_private
5410b57cec5SDimitry Andric 
5425ffd83dbSDimitry Andric #endif // LLDB_CORE_ADDRESS_H
543