1 //===-- AddressRange.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 liblldb_AddressRange_h_
10 #define liblldb_AddressRange_h_
11 
12 #include "lldb/Core/Address.h"
13 #include "lldb/lldb-forward.h"
14 #include "lldb/lldb-types.h"
15 
16 #include <stddef.h>
17 
18 namespace lldb_private {
19 class SectionList;
20 class Stream;
21 class Target;
22 
23 /// \class AddressRange AddressRange.h "lldb/Core/AddressRange.h"
24 /// A section + offset based address range class.
25 class AddressRange {
26 public:
27   /// Default constructor.
28   ///
29   /// Initialize with a invalid section (NULL), an invalid offset
30   /// (LLDB_INVALID_ADDRESS), and zero byte size.
31   AddressRange();
32 
33   /// Construct with a section pointer, offset, and byte_size.
34   ///
35   /// Initialize the address with the supplied \a section, \a offset and \a
36   /// byte_size.
37   ///
38   /// \param[in] section
39   ///     A section pointer to a valid lldb::Section, or NULL if the
40   ///     address doesn't have a section or will get resolved later.
41   ///
42   /// \param[in] offset
43   ///     The offset in bytes into \a section.
44   ///
45   /// \param[in] byte_size
46   ///     The size in bytes of the address range.
47   AddressRange(const lldb::SectionSP &section, lldb::addr_t offset,
48                lldb::addr_t byte_size);
49 
50   /// Construct with a virtual address, section list and byte size.
51   ///
52   /// Initialize and resolve the address with the supplied virtual address \a
53   /// file_addr, and byte size \a byte_size.
54   ///
55   /// \param[in] file_addr
56   ///     A virtual address.
57   ///
58   /// \param[in] byte_size
59   ///     The size in bytes of the address range.
60   ///
61   /// \param[in] section_list
62   ///     A list of sections, one of which may contain the \a vaddr.
63   AddressRange(lldb::addr_t file_addr, lldb::addr_t byte_size,
64                const SectionList *section_list = nullptr);
65 
66   /// Construct with a Address object address and byte size.
67   ///
68   /// Initialize by copying the section offset address in \a so_addr, and
69   /// setting the byte size to \a byte_size.
70   ///
71   /// \param[in] so_addr
72   ///     A section offset address object.
73   ///
74   /// \param[in] byte_size
75   ///     The size in bytes of the address range.
76   AddressRange(const Address &so_addr, lldb::addr_t byte_size);
77 
78   /// Destructor.
79   ///
80   /// The destructor is virtual in case this class is subclassed.
81   ~AddressRange();
82 
83   /// Clear the object's state.
84   ///
85   /// Sets the section to an invalid value (NULL), an invalid offset
86   /// (LLDB_INVALID_ADDRESS) and a zero byte size.
87   void Clear();
88 
89   /// Check if a section offset address is contained in this range.
90   ///
91   /// \param[in] so_addr
92   ///     A section offset address object reference.
93   ///
94   /// \return
95   ///     Returns \b true if \a so_addr is contained in this range,
96   ///     \b false otherwise.
97   //    bool
98   //    Contains (const Address &so_addr) const;
99 
100   /// Check if a section offset address is contained in this range.
101   ///
102   /// \param[in] so_addr_ptr
103   ///     A section offset address object pointer.
104   ///
105   /// \return
106   ///     Returns \b true if \a so_addr is contained in this range,
107   ///     \b false otherwise.
108   //    bool
109   //    Contains (const Address *so_addr_ptr) const;
110 
111   /// Check if a section offset \a so_addr when represented as a file address
112   /// is contained within this object's file address range.
113   ///
114   /// \param[in] so_addr
115   ///     A section offset address object reference.
116   ///
117   /// \return
118   ///     Returns \b true if both \a this and \a so_addr have
119   ///     resolvable file address values and \a so_addr is contained
120   ///     in the address range, \b false otherwise.
121   bool ContainsFileAddress(const Address &so_addr) const;
122 
123   /// Check if the resolved file address \a file_addr is contained within this
124   /// object's file address range.
125   ///
126   /// \param[in] file_addr
127   ///     A section offset address object reference.
128   ///
129   /// \return
130   ///     Returns \b true if both \a this has a resolvable file
131   ///     address value and \a so_addr is contained in the address
132   ///     range, \b false otherwise.
133   bool ContainsFileAddress(lldb::addr_t file_addr) const;
134 
135   /// Check if a section offset \a so_addr when represented as a load address
136   /// is contained within this object's load address range.
137   ///
138   /// \param[in] so_addr
139   ///     A section offset address object reference.
140   ///
141   /// \return
142   ///     Returns \b true if both \a this and \a so_addr have
143   ///     resolvable load address values and \a so_addr is contained
144   ///     in the address range, \b false otherwise.
145   bool ContainsLoadAddress(const Address &so_addr, Target *target) const;
146 
147   /// Check if the resolved load address \a load_addr is contained within this
148   /// object's load address range.
149   ///
150   /// \return
151   ///     Returns \b true if both \a this has a resolvable load
152   ///     address value and \a so_addr is contained in the address
153   ///     range, \b false otherwise.
154   bool ContainsLoadAddress(lldb::addr_t load_addr, Target *target) const;
155 
156   //------------------------------------------------------------------
157   /// Extends this range with \b rhs_range if it overlaps this range on the
158   /// right side. The range overlaps on the right side if the base address
159   /// of \b rhs_range lies within this range or if it's contiguous on its
160   /// right side.
161   ///
162   /// @param[in] rhs_range
163   ///     The range to extend at the right side.
164   ///
165   /// @return
166   ///     Returns \b true if this range was extended, \b false otherwise.
167   //------------------------------------------------------------------
168   bool Extend(const AddressRange &rhs_range);
169 
170   /// Dump a description of this object to a Stream.
171   ///
172   /// Dump a description of the contents of this object to the supplied stream
173   /// \a s. There are many ways to display a section offset based address
174   /// range, and \a style lets the user choose how the base address gets
175   /// displayed.
176   ///
177   /// \param[in] s
178   ///     The stream to which to dump the object description.
179   ///
180   /// \param[in] style
181   ///     The display style for the address.
182   ///
183   /// \return
184   ///     Returns \b true if the address was able to be displayed.
185   ///     File and load addresses may be unresolved and it may not be
186   ///     possible to display a valid value, \b false will be returned
187   ///     in such cases.
188   ///
189   /// \see Address::DumpStyle
190   bool
191   Dump(Stream *s, Target *target, Address::DumpStyle style,
192        Address::DumpStyle fallback_style = Address::DumpStyleInvalid) const;
193 
194   /// Dump a debug description of this object to a Stream.
195   ///
196   /// Dump a debug description of the contents of this object to the supplied
197   /// stream \a s.
198   ///
199   /// The debug description contains verbose internal state such and pointer
200   /// values, reference counts, etc.
201   ///
202   /// \param[in] s
203   ///     The stream to which to dump the object description.
204   void DumpDebug(Stream *s) const;
205 
206   /// Get accessor for the base address of the range.
207   ///
208   /// \return
209   ///     A reference to the base address object.
210   Address &GetBaseAddress() { return m_base_addr; }
211 
212   /// Get const accessor for the base address of the range.
213   ///
214   /// \return
215   ///     A const reference to the base address object.
216   const Address &GetBaseAddress() const { return m_base_addr; }
217 
218   /// Get accessor for the byte size of this range.
219   ///
220   /// \return
221   ///     The size in bytes of this address range.
222   lldb::addr_t GetByteSize() const { return m_byte_size; }
223 
224   /// Get the memory cost of this object.
225   ///
226   /// \return
227   ///     The number of bytes that this object occupies in memory.
228   size_t MemorySize() const {
229     // Noting special for the memory size of a single AddressRange object, it
230     // is just the size of itself.
231     return sizeof(AddressRange);
232   }
233 
234   /// Set accessor for the byte size of this range.
235   ///
236   /// \param[in] byte_size
237   ///     The new size in bytes of this address range.
238   void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
239 
240 protected:
241   // Member variables
242   Address m_base_addr;      ///< The section offset base address of this range.
243   lldb::addr_t m_byte_size; ///< The size in bytes of this address range.
244 };
245 
246 // bool operator== (const AddressRange& lhs, const AddressRange& rhs);
247 
248 } // namespace lldb_private
249 
250 #endif // liblldb_AddressRange_h_
251