Address.h revision 24943d2ee8bfaa7cf5893e4709143924157a5c1e
1//===-- Address.h -----------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_Address_h_
11#define liblldb_Address_h_
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17#include "lldb/lldb-private.h"
18#include "lldb/Symbol/SymbolContextScope.h"
19
20namespace lldb_private {
21
22//----------------------------------------------------------------------
23/// @class Address Address.h "lldb/Core/Address.h"
24/// @brief A section + offset based address class.
25///
26/// The Address class allows addresses to be relative to a section
27/// that can move during runtime due to images (executables, shared
28/// libraries, bundles, frameworks) being loaded at different
29/// addresses than the addresses found in the object file that
30/// represents them on disk. There are currently two types of addresses
31/// for a section:
32///     @li file addresses
33///     @li load addresses
34///
35/// File addresses represent the virtual addresses that are in the "on
36/// disk" object files. These virtual addresses are converted to be
37/// relative to unique sections scoped to the object file so that
38/// when/if the addresses slide when the images are loaded/unloaded
39/// in memory, we can easily track these changes without having to
40/// update every object (compile unit ranges, line tables, function
41/// address ranges, lexical block and inlined subroutine address
42/// ranges, global and static variables) each time an image is loaded or
43/// unloaded.
44///
45/// Load addresses represent the virtual addresses where each section
46/// ends up getting loaded at runtime. Before executing a program, it
47/// is common for all of the load addresses to be unresolved. When a
48/// DynamicLoader plug-in receives notification that shared libraries
49/// have been loaded/unloaded, the load addresses of the main executable
50/// and any images (shared libraries) will be  resolved/unresolved. When
51/// this happens, breakpoints that are in one of these sections can be
52/// set/cleared.
53//----------------------------------------------------------------------
54class Address :
55    public SymbolContextScope
56{
57public:
58    //------------------------------------------------------------------
59    /// Dump styles allow the Address::Dump(Stream *,DumpStyle) const
60    /// function to display Address contents in a variety of ways.
61    //------------------------------------------------------------------
62    typedef enum {
63        DumpStyleInvalid,               ///< Invalid dump style
64        DumpStyleSectionNameOffset,     ///< Display as the section name + offset.
65                                        ///< \code
66                                        /// // address for printf in libSystem.B.dylib as a section name + offset
67                                        /// libSystem.B.dylib.__TEXT.__text + 0x0005cfdf
68                                        /// \endcode
69        DumpStyleSectionPointerOffset,  ///< Display as the section pointer + offset (debug output).
70                                        ///< \code
71                                        /// // address for printf in libSystem.B.dylib as a section pointer + offset
72                                        /// (lldb::Section *)0x35cc50 + 0x000000000005cfdf \endcode
73        DumpStyleFileAddress,           ///< Display as the file address (if any).
74                                        ///< \code
75                                        /// // address for printf in libSystem.B.dylib as a file address
76                                        /// 0x000000000005dcff \endcode
77        DumpStyleModuleWithFileAddress, ///< Display as the file address with the module name prepended (if any).
78                                        ///< \code
79                                        /// // address for printf in libSystem.B.dylib as a file address
80                                        /// libSystem.B.dylib[0x000000000005dcff] \endcode
81        DumpStyleLoadAddress,           ///< Display as the load address (if resolved).
82                                        ///< \code
83                                        /// // address for printf in libSystem.B.dylib as a load address
84                                        /// 0x00007fff8306bcff \endcode
85        DumpStyleResolvedDescription           ///< Display the name that an address resolves to
86    } DumpStyle;
87
88    //------------------------------------------------------------------
89    /// Default constructor.
90    ///
91    /// Initialize with a invalid section (NULL) and an invalid
92    /// offset (LLDB_INVALID_ADDRESS).
93    //------------------------------------------------------------------
94    Address ();
95
96    //------------------------------------------------------------------
97    /// Copy constructor
98    ///
99    /// Makes a copy of the another Address object \a rhs.
100    ///
101    /// @param[in] rhs
102    ///     A const Address object reference to copy.
103    //------------------------------------------------------------------
104    Address (const Address& rhs);
105
106    //------------------------------------------------------------------
107    /// Construct with a section pointer and offset.
108    ///
109    /// Initialize the address with the supplied \a section and \a
110    /// offset.
111    ///
112    /// @param[in] section
113    ///     A section pointer to a valid lldb::Section, or NULL if the
114    ///     address doesn't have a section or will get resolved later.
115    ///
116    /// @param[in] offset
117    ///     The offset in bytes into \a section.
118    //------------------------------------------------------------------
119    Address (const Section* section, lldb::addr_t offset);
120
121    //------------------------------------------------------------------
122    /// Construct with a virtual address and section list.
123    ///
124    /// Initialize and resolve the address with the supplied virtual
125    /// address \a file_addr.
126    ///
127    /// @param[in] file_addr
128    ///     A virtual file address.
129    ///
130    /// @param[in] section_list
131    ///     A list of sections, one of which may contain the \a file_addr.
132    //------------------------------------------------------------------
133    Address (lldb::addr_t file_addr, const SectionList * section_list);
134
135    //------------------------------------------------------------------
136    /// Destructor.
137    //------------------------------------------------------------------
138    ~Address ();
139
140    //------------------------------------------------------------------
141    /// Assignment operator.
142    ///
143    /// Copies the address value from another Address object \a rhs
144    /// into \a this object.
145    ///
146    /// @param[in] rhs
147    ///     A const Address object reference to copy.
148    ///
149    /// @return
150    ///     A const Address object reference to \a this.
151    //------------------------------------------------------------------
152#ifndef SWIG
153    const Address&
154    operator= (const Address& rhs);
155#endif
156    //------------------------------------------------------------------
157    /// Clear the object's state.
158    ///
159    /// Sets the section to an invalid value (NULL) and an invalid
160    /// offset (LLDB_INVALID_ADDRESS).
161    //------------------------------------------------------------------
162    void
163    Clear ();
164
165    //------------------------------------------------------------------
166    /// Compare two Address objects.
167    ///
168    /// @param[in] lhs
169    ///     The Left Hand Side const Address object reference.
170    ///
171    /// @param[in] rhs
172    ///     The Right Hand Side const Address object reference.
173    ///
174    /// @return
175    ///     @li -1 if lhs < rhs
176    ///     @li 0 if lhs == rhs
177    ///     @li 1 if lhs > rhs
178    //------------------------------------------------------------------
179    static int
180    CompareFileAddress (const Address& lhs, const Address& rhs);
181
182    static int
183    CompareLoadAddress (const Address& lhs, const Address& rhs, Process *process);
184
185    static int
186    CompareModulePointerAndOffset (const Address& lhs, const Address& rhs);
187
188    // For use with std::map, std::multi_map
189    class ModulePointerAndOffsetLessThanFunctionObject
190    {
191    public:
192        ModulePointerAndOffsetLessThanFunctionObject () {}
193
194        bool
195        operator() (const Address& a, const Address& b) const
196        {
197            return Address::CompareModulePointerAndOffset(a, b) < 0;
198        }
199    };
200
201    //------------------------------------------------------------------
202    /// Dump a description of this object to a Stream.
203    ///
204    /// Dump a description of the contents of this object to the
205    /// supplied stream \a s. There are many ways to display a section
206    /// offset based address, and \a style lets the user choose.
207    ///
208    /// @param[in] s
209    ///     The stream to which to dump the object descripton.
210    ///
211    /// @param[in] style
212    ///     The display style for the address.
213    ///
214    /// @param[in] fallback_style
215    ///     The display style for the address.
216    ///
217    /// @return
218    ///     Returns \b true if the address was able to be displayed.
219    ///     File and load addresses may be unresolved and it may not be
220    ///     possible to display a valid value, \b false will be returned
221    ///     in such cases.
222    ///
223    /// @see Address::DumpStyle
224    //------------------------------------------------------------------
225    bool
226    Dump (Stream *s,
227          ExecutionContextScope *exe_scope,
228          DumpStyle style,
229          DumpStyle fallback_style = DumpStyleInvalid) const;
230
231    //------------------------------------------------------------------
232    /// Dump a debug description of this object to a Stream.
233    ///
234    /// Dump a debug description of the contents of this object to the
235    /// supplied stream \a s.
236    ///
237    /// The debug description contains verbose internal state such
238    /// and pointer values, reference counts, etc.
239    ///
240    /// @param[in] s
241    ///     The stream to which to dump the object descripton.
242    //------------------------------------------------------------------
243    void
244    DumpDebug (Stream *s) const;
245
246    //------------------------------------------------------------------
247    /// Get the file address.
248    ///
249    /// If an address comes from a file on disk that has section
250    /// relative addresses, then it has a virtual address that is
251    /// relative to unique section in the object file.
252    ///
253    /// @return
254    ///     The valid file virtual address, or LLDB_INVALID_ADDRESS if
255    ///     the address doesn't have a file virtual address (image is
256    ///     from memory only with no representation on disk).
257    //------------------------------------------------------------------
258    lldb::addr_t
259    GetFileAddress () const;
260
261    //------------------------------------------------------------------
262    /// Get the load address.
263    ///
264    /// If an address comes from a file on disk that has section
265    /// relative addresses, then it has a virtual address that is
266    /// relative to unique section in the object file. Sections get
267    /// resolved at runtime by DynamicLoader plug-ins as images
268    /// (executables and shared libraries) get loaded/unloaded. If a
269    /// section is loaded, then the load address can be resolved.
270    ///
271    /// @return
272    ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
273    ///     the address is currently not loaded.
274    //------------------------------------------------------------------
275    lldb::addr_t
276    GetLoadAddress (Process *process) const;
277
278    //------------------------------------------------------------------
279    /// Get the section relative offset value.
280    ///
281    /// @return
282    ///     The current offset, or LLDB_INVALID_ADDRESS if this address
283    ///     doesn't contain a valid offset.
284    //------------------------------------------------------------------
285    lldb::addr_t
286    GetOffset () const;
287
288    //------------------------------------------------------------------
289    /// Check if an address is section offset.
290    ///
291    /// When converting a virtual file or load address into a section
292    /// offset based address, we often need to know if, given a section
293    /// list, if the address was able to be converted to section offset.
294    /// This function returns true if the current value contained in
295    /// this object is section offset based.
296    ///
297    /// @return
298    ///     Returns \b true if the address has a valid section and
299    ///     offset, \b false otherwise.
300    //------------------------------------------------------------------
301    bool
302    IsSectionOffset() const;
303
304    //------------------------------------------------------------------
305    /// Check if the object state is valid.
306    ///
307    /// A valid Address object contains either a section pointer and
308    /// and offset (for section offset based addresses), or just a valid
309    /// offset (for absolute addresses that have no section).
310    ///
311    /// @return
312    ///     Returns \b true if the the offset is valid, \b false
313    ///     otherwise.
314    //------------------------------------------------------------------
315    bool
316    IsValid() const;
317
318    //------------------------------------------------------------------
319    /// Get the memory cost of this object.
320    ///
321    /// @return
322    ///     The number of bytes that this object occupies in memory.
323    //------------------------------------------------------------------
324    size_t
325    MemorySize () const;
326
327    //------------------------------------------------------------------
328    /// Resolve a file virtual address using a section list.
329    ///
330    /// Given a list of sections, attempt to resolve \a addr as a
331    /// an offset into one of the file sections.
332    ///
333    /// @return
334    ///     Returns \b true if \a addr was able to be resolved, \b false
335    ///     otherwise.
336    //------------------------------------------------------------------
337    bool
338    ResolveAddressUsingFileSections (lldb::addr_t addr, const SectionList *sections);
339
340    bool
341    IsLinkedAddress () const;
342
343    void
344    ResolveLinkedAddress ();
345
346    //------------------------------------------------------------------
347    /// Get accessor for the module for this address.
348    ///
349    /// @return
350    ///     Returns the Module pointer that this address is an offset
351    ///     in, or NULL if this address doesn't belong in a module, or
352    ///     isn't resolved yet.
353    //------------------------------------------------------------------
354    Module *
355    GetModule () const;
356
357    //------------------------------------------------------------------
358    /// Get const accessor for the section.
359    ///
360    /// @return
361    ///     Returns the const lldb::Section pointer that this address is an
362    ///     offset in, or NULL if this address is absolute.
363    //------------------------------------------------------------------
364    const Section*
365    GetSection() const;
366
367    //------------------------------------------------------------------
368    /// Set accessor for the offset.
369    ///
370    /// @param[in] offset
371    ///     A new offset value for this object.
372    ///
373    /// @return
374    ///     Returns \b true if the offset changed, \b false otherwise.
375    //------------------------------------------------------------------
376    bool
377    SetOffset (lldb::addr_t offset);
378
379    //------------------------------------------------------------------
380    /// Set accessor for the section.
381    ///
382    /// @param[in] section
383    ///     A new lldb::Section pointer to use as the section base. Can
384    ///     be NULL for absolute addresses that are not relative to
385    ///     any section.
386    //------------------------------------------------------------------
387    void
388    SetSection (const Section* section);
389
390    //------------------------------------------------------------------
391    /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
392    ///
393    /// @see SymbolContextScope
394    //------------------------------------------------------------------
395    void
396    CalculateSymbolContext (SymbolContext *sc);
397
398    //------------------------------------------------------------------
399    /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
400    ///
401    /// @see SymbolContextScope
402    //------------------------------------------------------------------
403    void
404    DumpSymbolContext (Stream *s);
405
406protected:
407    //------------------------------------------------------------------
408    // Member variables.
409    //------------------------------------------------------------------
410    const Section* m_section;      ///< The section for the address, can be NULL.
411    lldb::addr_t      m_offset;       ///< Offset into section if \a m_section != NULL, else the absolute address value.
412};
413
414//bool operator<  (const Address& lhs, const Address& rhs);
415//bool operator<= (const Address& lhs, const Address& rhs);
416//bool operator>  (const Address& lhs, const Address& rhs);
417//bool operator>= (const Address& lhs, const Address& rhs);
418bool operator== (const Address& lhs, const Address& rhs);
419bool operator!= (const Address& lhs, const Address& rhs);
420
421//Stream& operator << (Stream& strm, const Address& so_addr);
422
423} // namespace lldb_private
424
425#endif  // liblldb_Address_h_
426