15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===-- Address.h -----------------------------------------------*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef liblldb_Address_h_
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define liblldb_Address_h_
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// C Includes
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// C++ Includes
15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <atomic>
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Other libraries and framework includes
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Project includes
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "lldb/lldb-private.h"
195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "lldb/Symbol/SymbolContextScope.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace lldb_private {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
23b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)//----------------------------------------------------------------------
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// @class Address Address.h "lldb/Core/Address.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// @brief A section + offset based address class.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// The Address class allows addresses to be relative to a section
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// that can move during runtime due to images (executables, shared
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// libraries, bundles, frameworks) being loaded at different
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// addresses than the addresses found in the object file that
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// represents them on disk. There are currently two types of addresses
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// for a section:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///     @li file addresses
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///     @li load addresses
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// File addresses represent the virtual addresses that are in the "on
377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// disk" object files. These virtual addresses are converted to be
387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// relative to unique sections scoped to the object file so that
397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// when/if the addresses slide when the images are loaded/unloaded
407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// in memory, we can easily track these changes without having to
417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// update every object (compile unit ranges, line tables, function
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// address ranges, lexical block and inlined subroutine address
437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// ranges, global and static variables) each time an image is loaded or
44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)/// unloaded.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Load addresses represent the virtual addresses where each section
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// ends up getting loaded at runtime. Before executing a program, it
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// is common for all of the load addresses to be unresolved. When a
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// DynamicLoader plug-in receives notification that shared libraries
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// have been loaded/unloaded, the load addresses of the main executable
515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// and any images (shared libraries) will be  resolved/unresolved. When
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// this happens, breakpoints that are in one of these sections can be
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// set/cleared.
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)//----------------------------------------------------------------------
5546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class Address
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles){
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)public:
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Dump styles allow the Address::Dump(Stream *,DumpStyle) const
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// function to display Address contents in a variety of ways.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    typedef enum {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleInvalid,               ///< Invalid dump style
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleSectionNameOffset,     ///< Display as the section name + offset.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< \code
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// // address for printf in libSystem.B.dylib as a section name + offset
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// libSystem.B.dylib.__TEXT.__text + 0x0005cfdf
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// \endcode
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleSectionPointerOffset,  ///< Display as the section pointer + offset (debug output).
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< \code
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// // address for printf in libSystem.B.dylib as a section pointer + offset
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// (lldb::Section *)0x35cc50 + 0x000000000005cfdf \endcode
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleFileAddress,           ///< Display as the file address (if any).
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< \code
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// // address for printf in libSystem.B.dylib as a file address
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// 0x000000000005dcff \endcode
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleModuleWithFileAddress, ///< Display as the file address with the module name prepended (if any).
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< \code
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// // address for printf in libSystem.B.dylib as a file address
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// libSystem.B.dylib[0x000000000005dcff] \endcode
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleLoadAddress,           ///< Display as the load address (if resolved).
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< \code
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// // address for printf in libSystem.B.dylib as a load address
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        /// 0x00007fff8306bcff \endcode
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleResolvedDescription,   ///< Display the details about what an address resolves to. This can
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< be anything from a symbol context summary (module, function/symbol,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< and file and line), to information about what the pointer points to
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< if the address is in a section (section of pointers, c strings, etc).
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleResolvedDescriptionNoModule,
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleDetailedSymbolContext, ///< Detailed symbol context information for an address for all symbol
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ///< context members.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DumpStyleResolvedPointerDescription ///< Dereference a pointer at the current address and then lookup the
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             ///< dereferenced address using DumpStyleResolvedDescription
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } DumpStyle;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Default constructor.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Initialize with a invalid section (NULL) and an invalid
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// offset (LLDB_INVALID_ADDRESS).
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Address () :
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        m_section_wp (),
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        m_offset (LLDB_INVALID_ADDRESS)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Copy constructor
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Makes a copy of the another Address object \a rhs.
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// @param[in] rhs
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///     A const Address object reference to copy.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Address (const Address& rhs) :
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        m_section_wp (rhs.m_section_wp),
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        m_offset(rhs.m_offset.load())
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //------------------------------------------------------------------
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Construct with a section pointer and offset.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Initialize the address with the supplied \a section and \a
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// offset.
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// @param[in] section
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///     A section pointer to a valid lldb::Section, or NULL if the
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///     address doesn't have a section or will get resolved later.
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// @param[in] offset
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ///     The offset in bytes into \a section.
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Address (const lldb::SectionSP &section_sp, lldb::addr_t offset) :
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        m_section_wp (), // Don't init with section_sp in case section_sp is invalid (the weak_ptr will throw)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        m_offset (offset)
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    {
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (section_sp)
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            m_section_wp = section_sp;
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Construct with a virtual address and section list.
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Initialize and resolve the address with the supplied virtual
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// address \a file_addr.
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// @param[in] file_addr
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///     A virtual file address.
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// @param[in] section_list
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///     A list of sections, one of which may contain the \a file_addr.
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Address (lldb::addr_t file_addr, const SectionList * section_list);
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Address (lldb::addr_t abs_addr);
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Assignment operator.
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Copies the address value from another Address object \a rhs
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// into \a this object.
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// @param[in] rhs
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///     A const Address object reference to copy.
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// @return
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///     A const Address object reference to \a this.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef SWIG
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Address&
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    operator= (const Address& rhs);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Clear the object's state.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Sets the section to an invalid value (NULL) and an invalid
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// offset (LLDB_INVALID_ADDRESS).
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //------------------------------------------------------------------
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    void
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Clear ()
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    {
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        m_section_wp.reset();
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        m_offset = LLDB_INVALID_ADDRESS;
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //------------------------------------------------------------------
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Compare two Address objects.
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///
1927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    /// @param[in] lhs
1937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ///     The Left Hand Side const Address object reference.
1947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ///
1957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    /// @param[in] rhs
1967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ///     The Right Hand Side const Address object reference.
1977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ///
1987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    /// @return
1997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ///     @li -1 if lhs < rhs
2007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ///     @li 0 if lhs == rhs
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///     @li 1 if lhs > rhs
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    static int
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CompareFileAddress (const Address& lhs, const Address& rhs);
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    static int
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CompareLoadAddress (const Address& lhs, const Address& rhs, Target *target);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static int
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CompareModulePointerAndOffset (const Address& lhs, const Address& rhs);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For use with std::map, std::multi_map
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    class ModulePointerAndOffsetLessThanFunctionObject
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public:
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ModulePointerAndOffsetLessThanFunctionObject () {}
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        bool
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        operator() (const Address& a, const Address& b) const
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        {
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            return Address::CompareModulePointerAndOffset(a, b) < 0;
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    };
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    //------------------------------------------------------------------
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Dump a description of this object to a Stream.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Dump a description of the contents of this object to the
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// supplied stream \a s. There are many ways to display a section
230    /// offset based address, and \a style lets the user choose.
231    ///
232    /// @param[in] s
233    ///     The stream to which to dump the object descripton.
234    ///
235    /// @param[in] style
236    ///     The display style for the address.
237    ///
238    /// @param[in] fallback_style
239    ///     The display style for the address.
240    ///
241    /// @return
242    ///     Returns \b true if the address was able to be displayed.
243    ///     File and load addresses may be unresolved and it may not be
244    ///     possible to display a valid value, \b false will be returned
245    ///     in such cases.
246    ///
247    /// @see Address::DumpStyle
248    //------------------------------------------------------------------
249    bool
250    Dump (Stream *s,
251          ExecutionContextScope *exe_scope,
252          DumpStyle style,
253          DumpStyle fallback_style = DumpStyleInvalid,
254          uint32_t addr_byte_size = UINT32_MAX) const;
255
256    lldb::AddressClass
257    GetAddressClass () const;
258
259    //------------------------------------------------------------------
260    /// Get the file address.
261    ///
262    /// If an address comes from a file on disk that has section
263    /// relative addresses, then it has a virtual address that is
264    /// relative to unique section in the object file.
265    ///
266    /// @return
267    ///     The valid file virtual address, or LLDB_INVALID_ADDRESS if
268    ///     the address doesn't have a file virtual address (image is
269    ///     from memory only with no representation on disk).
270    //------------------------------------------------------------------
271    lldb::addr_t
272    GetFileAddress () const;
273
274    //------------------------------------------------------------------
275    /// Get the load address.
276    ///
277    /// If an address comes from a file on disk that has section
278    /// relative addresses, then it has a virtual address that is
279    /// relative to unique section in the object file. Sections get
280    /// resolved at runtime by DynamicLoader plug-ins as images
281    /// (executables and shared libraries) get loaded/unloaded. If a
282    /// section is loaded, then the load address can be resolved.
283    ///
284    /// @return
285    ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
286    ///     the address is currently not loaded.
287    //------------------------------------------------------------------
288    lldb::addr_t
289    GetLoadAddress (Target *target) const;
290
291    //------------------------------------------------------------------
292    /// Get the load address as a callable code load address.
293    ///
294    /// This function will first resolve its address to a load address.
295    /// Then, if the address turns out to be in code address, return the
296    /// load address that would be required to call or return to. The
297    /// address might have extra bits set (bit zero will be set to Thumb
298    /// functions for an ARM target) that are required when changing the
299    /// program counter to setting a return address.
300    ///
301    /// @return
302    ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
303    ///     the address is currently not loaded.
304    //------------------------------------------------------------------
305    lldb::addr_t
306    GetCallableLoadAddress (Target *target, bool is_indirect = false) const;
307
308    //------------------------------------------------------------------
309    /// Get the load address as an opcode load address.
310    ///
311    /// This function will first resolve its address to a load address.
312    /// Then, if the address turns out to be in code address, return the
313    /// load address for a an opcode. This address object might have
314    /// extra bits set (bit zero will be set to Thumb functions for an
315    /// ARM target) that are required for changing the program counter
316    /// and this function will remove any bits that are intended for
317    /// these special purposes. The result of this function can be used
318    /// to safely write a software breakpoint trap to memory.
319    ///
320    /// @return
321    ///     The valid load virtual address with extra callable bits
322    ///     removed, or LLDB_INVALID_ADDRESS if the address is currently
323    ///     not loaded.
324    //------------------------------------------------------------------
325    lldb::addr_t
326    GetOpcodeLoadAddress (Target *target) const;
327
328    //------------------------------------------------------------------
329    /// Get the section relative offset value.
330    ///
331    /// @return
332    ///     The current offset, or LLDB_INVALID_ADDRESS if this address
333    ///     doesn't contain a valid offset.
334    //------------------------------------------------------------------
335    lldb::addr_t
336    GetOffset () const { return m_offset; }
337
338    //------------------------------------------------------------------
339    /// Check if an address is section offset.
340    ///
341    /// When converting a virtual file or load address into a section
342    /// offset based address, we often need to know if, given a section
343    /// list, if the address was able to be converted to section offset.
344    /// This function returns true if the current value contained in
345    /// this object is section offset based.
346    ///
347    /// @return
348    ///     Returns \b true if the address has a valid section and
349    ///     offset, \b false otherwise.
350    //------------------------------------------------------------------
351    bool
352    IsSectionOffset() const
353    {
354        return IsValid() && (GetSection().get() != NULL);
355    }
356
357    //------------------------------------------------------------------
358    /// Check if the object state is valid.
359    ///
360    /// A valid Address object contains either a section pointer and
361    /// and offset (for section offset based addresses), or just a valid
362    /// offset (for absolute addresses that have no section).
363    ///
364    /// @return
365    ///     Returns \b true if the the offset is valid, \b false
366    ///     otherwise.
367    //------------------------------------------------------------------
368    bool
369    IsValid() const
370    {
371        return m_offset != LLDB_INVALID_ADDRESS;
372    }
373
374
375    //------------------------------------------------------------------
376    /// Get the memory cost of this object.
377    ///
378    /// @return
379    ///     The number of bytes that this object occupies in memory.
380    //------------------------------------------------------------------
381    size_t
382    MemorySize () const;
383
384    //------------------------------------------------------------------
385    /// Resolve a file virtual address using a section list.
386    ///
387    /// Given a list of sections, attempt to resolve \a addr as a
388    /// an offset into one of the file sections.
389    ///
390    /// @return
391    ///     Returns \b true if \a addr was able to be resolved, \b false
392    ///     otherwise.
393    //------------------------------------------------------------------
394    bool
395    ResolveAddressUsingFileSections (lldb::addr_t addr, const SectionList *sections);
396
397    //------------------------------------------------------------------
398    /// Set the address to represent \a load_addr.
399    ///
400    /// The address will attempt to find a loaded section within
401    /// \a target that contains \a load_addr. If successful, this
402    /// address object will have a valid section and offset. Else this
403    /// address object will have no section (NULL) and the offset will
404    /// be \a load_addr.
405    ///
406    /// @param[in] load_addr
407    ///     A load address from a current process.
408    ///
409    /// @param[in] target
410    ///     The target to use when trying resolve the address into
411    ///     a section + offset. The Target's SectionLoadList object
412    ///     is used to resolve the address.
413    ///
414    /// @return
415    ///     Returns \b true if the load address was resolved to be
416    ///     section/offset, \b false otherwise. It is often ok for an
417    ///     address no not resolve to a section in a module, this often
418    ///     happens for JIT'ed code, or any load addresses on the stack
419    ///     or heap.
420    //------------------------------------------------------------------
421    bool
422    SetLoadAddress (lldb::addr_t load_addr, Target *target);
423
424    bool
425    SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target);
426
427    bool
428    SetCallableLoadAddress (lldb::addr_t load_addr, Target *target);
429
430    //------------------------------------------------------------------
431    /// Get accessor for the module for this address.
432    ///
433    /// @return
434    ///     Returns the Module pointer that this address is an offset
435    ///     in, or NULL if this address doesn't belong in a module, or
436    ///     isn't resolved yet.
437    //------------------------------------------------------------------
438    lldb::ModuleSP
439    GetModule () const;
440
441    //------------------------------------------------------------------
442    /// Get const accessor for the section.
443    ///
444    /// @return
445    ///     Returns the const lldb::Section pointer that this address is an
446    ///     offset in, or NULL if this address is absolute.
447    //------------------------------------------------------------------
448    lldb::SectionSP
449    GetSection () const { return m_section_wp.lock(); }
450
451    //------------------------------------------------------------------
452    /// Set accessor for the offset.
453    ///
454    /// @param[in] offset
455    ///     A new offset value for this object.
456    ///
457    /// @return
458    ///     Returns \b true if the offset changed, \b false otherwise.
459    //------------------------------------------------------------------
460    bool
461    SetOffset (lldb::addr_t offset)
462    {
463        bool changed = m_offset != offset;
464        m_offset = offset;
465        return changed;
466    }
467
468    void
469    SetRawAddress (lldb::addr_t addr)
470    {
471        m_section_wp.reset();
472        m_offset = addr;
473    }
474
475    bool
476    Slide (int64_t offset)
477    {
478        if (m_offset != LLDB_INVALID_ADDRESS)
479        {
480            m_offset += offset;
481            return true;
482        }
483        return false;
484    }
485
486    //------------------------------------------------------------------
487    /// Set accessor for the section.
488    ///
489    /// @param[in] section
490    ///     A new lldb::Section pointer to use as the section base. Can
491    ///     be NULL for absolute addresses that are not relative to
492    ///     any section.
493    //------------------------------------------------------------------
494    void
495    SetSection (const lldb::SectionSP &section_sp)
496    {
497        m_section_wp = section_sp;
498    }
499
500    void
501    ClearSection ()
502    {
503        m_section_wp.reset();
504    }
505    //------------------------------------------------------------------
506    /// Reconstruct a symbol context from an address.
507    ///
508    /// This class doesn't inherit from SymbolContextScope because many
509    /// address objects have short lifespans. Address objects that are
510    /// section offset can reconstruct their symbol context by looking
511    /// up the address in the module found in the section.
512    ///
513    /// @see SymbolContextScope::CalculateSymbolContext(SymbolContext*)
514    //------------------------------------------------------------------
515    uint32_t
516    CalculateSymbolContext (SymbolContext *sc,
517                            uint32_t resolve_scope = lldb::eSymbolContextEverything) const;
518
519    lldb::ModuleSP
520    CalculateSymbolContextModule () const;
521
522    CompileUnit *
523    CalculateSymbolContextCompileUnit () const;
524
525    Function *
526    CalculateSymbolContextFunction () const;
527
528    Block *
529    CalculateSymbolContextBlock () const;
530
531    Symbol *
532    CalculateSymbolContextSymbol () const;
533
534    bool
535    CalculateSymbolContextLineEntry (LineEntry &line_entry) const;
536
537protected:
538    //------------------------------------------------------------------
539    // Member variables.
540    //------------------------------------------------------------------
541    lldb::SectionWP m_section_wp;   ///< The section for the address, can be NULL.
542    std::atomic<lldb::addr_t> m_offset;      ///< Offset into section if \a m_section_wp is valid...
543};
544
545
546//----------------------------------------------------------------------
547// NOTE: Be careful using this operator. It can correctly compare two
548// addresses from the same Module correctly. It can't compare two
549// addresses from different modules in any meaningful way, but it will
550// compare the module pointers.
551//
552// To sum things up:
553// - works great for addresses within the same module
554// - it works for addresses across multiple modules, but don't expect the
555//   address results to make much sense
556//
557// This basically lets Address objects be used in ordered collection
558// classes.
559//----------------------------------------------------------------------
560bool operator<  (const Address& lhs, const Address& rhs);
561bool operator>  (const Address& lhs, const Address& rhs);
562
563
564
565bool operator== (const Address& lhs, const Address& rhs);
566bool operator!= (const Address& lhs, const Address& rhs);
567
568} // namespace lldb_private
569
570#endif  // liblldb_Address_h_
571