Address.h revision cf7f1ad9860ad2447680d427be87f85f3a1396d9
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 details about what an address resolves to. This can
86                                        ///< be anything from a symbol context summary (module, function/symbol,
87                                        ///< and file and line), to information about what the pointer points to
88                                        ///< if the address is in a section (section of pointers, c strings, etc).
89        DumpStyleResolvedDescriptionNoModule,
90        DumpStyleDetailedSymbolContext  ///< Detailed symbol context information for an address for all symbol
91                                        ///< context members.
92    } DumpStyle;
93
94    //------------------------------------------------------------------
95    /// Default constructor.
96    ///
97    /// Initialize with a invalid section (NULL) and an invalid
98    /// offset (LLDB_INVALID_ADDRESS).
99    //------------------------------------------------------------------
100    Address ();
101
102    //------------------------------------------------------------------
103    /// Copy constructor
104    ///
105    /// Makes a copy of the another Address object \a rhs.
106    ///
107    /// @param[in] rhs
108    ///     A const Address object reference to copy.
109    //------------------------------------------------------------------
110    Address (const Address& rhs);
111
112    //------------------------------------------------------------------
113    /// Construct with a section pointer and offset.
114    ///
115    /// Initialize the address with the supplied \a section and \a
116    /// offset.
117    ///
118    /// @param[in] section
119    ///     A section pointer to a valid lldb::Section, or NULL if the
120    ///     address doesn't have a section or will get resolved later.
121    ///
122    /// @param[in] offset
123    ///     The offset in bytes into \a section.
124    //------------------------------------------------------------------
125    Address (const Section* section, lldb::addr_t offset);
126
127    //------------------------------------------------------------------
128    /// Construct with a virtual address and section list.
129    ///
130    /// Initialize and resolve the address with the supplied virtual
131    /// address \a file_addr.
132    ///
133    /// @param[in] file_addr
134    ///     A virtual file address.
135    ///
136    /// @param[in] section_list
137    ///     A list of sections, one of which may contain the \a file_addr.
138    //------------------------------------------------------------------
139    Address (lldb::addr_t file_addr, const SectionList * section_list);
140
141    //------------------------------------------------------------------
142    /// Assignment operator.
143    ///
144    /// Copies the address value from another Address object \a rhs
145    /// into \a this object.
146    ///
147    /// @param[in] rhs
148    ///     A const Address object reference to copy.
149    ///
150    /// @return
151    ///     A const Address object reference to \a this.
152    //------------------------------------------------------------------
153#ifndef SWIG
154    const Address&
155    operator= (const Address& rhs);
156#endif
157    //------------------------------------------------------------------
158    /// Clear the object's state.
159    ///
160    /// Sets the section to an invalid value (NULL) and an invalid
161    /// offset (LLDB_INVALID_ADDRESS).
162    //------------------------------------------------------------------
163    void
164    Clear ();
165
166    //------------------------------------------------------------------
167    /// Compare two Address objects.
168    ///
169    /// @param[in] lhs
170    ///     The Left Hand Side const Address object reference.
171    ///
172    /// @param[in] rhs
173    ///     The Right Hand Side const Address object reference.
174    ///
175    /// @return
176    ///     @li -1 if lhs < rhs
177    ///     @li 0 if lhs == rhs
178    ///     @li 1 if lhs > rhs
179    //------------------------------------------------------------------
180    static int
181    CompareFileAddress (const Address& lhs, const Address& rhs);
182
183    static int
184    CompareLoadAddress (const Address& lhs, const Address& rhs, Process *process);
185
186    static int
187    CompareModulePointerAndOffset (const Address& lhs, const Address& rhs);
188
189    // For use with std::map, std::multi_map
190    class ModulePointerAndOffsetLessThanFunctionObject
191    {
192    public:
193        ModulePointerAndOffsetLessThanFunctionObject () {}
194
195        bool
196        operator() (const Address& a, const Address& b) const
197        {
198            return Address::CompareModulePointerAndOffset(a, b) < 0;
199        }
200    };
201
202    //------------------------------------------------------------------
203    /// Dump a description of this object to a Stream.
204    ///
205    /// Dump a description of the contents of this object to the
206    /// supplied stream \a s. There are many ways to display a section
207    /// offset based address, and \a style lets the user choose.
208    ///
209    /// @param[in] s
210    ///     The stream to which to dump the object descripton.
211    ///
212    /// @param[in] style
213    ///     The display style for the address.
214    ///
215    /// @param[in] fallback_style
216    ///     The display style for the address.
217    ///
218    /// @return
219    ///     Returns \b true if the address was able to be displayed.
220    ///     File and load addresses may be unresolved and it may not be
221    ///     possible to display a valid value, \b false will be returned
222    ///     in such cases.
223    ///
224    /// @see Address::DumpStyle
225    //------------------------------------------------------------------
226    bool
227    Dump (Stream *s,
228          ExecutionContextScope *exe_scope,
229          DumpStyle style,
230          DumpStyle fallback_style = DumpStyleInvalid,
231          uint32_t addr_byte_size = UINT32_MAX) const;
232
233    //------------------------------------------------------------------
234    /// Dump a debug description of this object to a Stream.
235    ///
236    /// Dump a debug description of the contents of this object to the
237    /// supplied stream \a s.
238    ///
239    /// The debug description contains verbose internal state such
240    /// and pointer values, reference counts, etc.
241    ///
242    /// @param[in] s
243    ///     The stream to which to dump the object descripton.
244    //------------------------------------------------------------------
245    void
246    DumpDebug (Stream *s) const;
247
248    //------------------------------------------------------------------
249    /// Get the file address.
250    ///
251    /// If an address comes from a file on disk that has section
252    /// relative addresses, then it has a virtual address that is
253    /// relative to unique section in the object file.
254    ///
255    /// @return
256    ///     The valid file virtual address, or LLDB_INVALID_ADDRESS if
257    ///     the address doesn't have a file virtual address (image is
258    ///     from memory only with no representation on disk).
259    //------------------------------------------------------------------
260    lldb::addr_t
261    GetFileAddress () const;
262
263    //------------------------------------------------------------------
264    /// Get the load address.
265    ///
266    /// If an address comes from a file on disk that has section
267    /// relative addresses, then it has a virtual address that is
268    /// relative to unique section in the object file. Sections get
269    /// resolved at runtime by DynamicLoader plug-ins as images
270    /// (executables and shared libraries) get loaded/unloaded. If a
271    /// section is loaded, then the load address can be resolved.
272    ///
273    /// @return
274    ///     The valid load virtual address, or LLDB_INVALID_ADDRESS if
275    ///     the address is currently not loaded.
276    //------------------------------------------------------------------
277    lldb::addr_t
278    GetLoadAddress (Process *process) const;
279
280    //------------------------------------------------------------------
281    /// Get the section relative offset value.
282    ///
283    /// @return
284    ///     The current offset, or LLDB_INVALID_ADDRESS if this address
285    ///     doesn't contain a valid offset.
286    //------------------------------------------------------------------
287    lldb::addr_t
288    GetOffset () const { return m_offset; }
289
290    //------------------------------------------------------------------
291    /// Check if an address is section offset.
292    ///
293    /// When converting a virtual file or load address into a section
294    /// offset based address, we often need to know if, given a section
295    /// list, if the address was able to be converted to section offset.
296    /// This function returns true if the current value contained in
297    /// this object is section offset based.
298    ///
299    /// @return
300    ///     Returns \b true if the address has a valid section and
301    ///     offset, \b false otherwise.
302    //------------------------------------------------------------------
303    bool
304    IsSectionOffset() const;
305
306    //------------------------------------------------------------------
307    /// Check if the object state is valid.
308    ///
309    /// A valid Address object contains either a section pointer and
310    /// and offset (for section offset based addresses), or just a valid
311    /// offset (for absolute addresses that have no section).
312    ///
313    /// @return
314    ///     Returns \b true if the the offset is valid, \b false
315    ///     otherwise.
316    //------------------------------------------------------------------
317    bool
318    IsValid() const;
319
320    //------------------------------------------------------------------
321    /// Get the memory cost of this object.
322    ///
323    /// @return
324    ///     The number of bytes that this object occupies in memory.
325    //------------------------------------------------------------------
326    size_t
327    MemorySize () const;
328
329    //------------------------------------------------------------------
330    /// Resolve a file virtual address using a section list.
331    ///
332    /// Given a list of sections, attempt to resolve \a addr as a
333    /// an offset into one of the file sections.
334    ///
335    /// @return
336    ///     Returns \b true if \a addr was able to be resolved, \b false
337    ///     otherwise.
338    //------------------------------------------------------------------
339    bool
340    ResolveAddressUsingFileSections (lldb::addr_t addr, const SectionList *sections);
341
342    bool
343    IsLinkedAddress () const;
344
345    void
346    ResolveLinkedAddress ();
347
348    //------------------------------------------------------------------
349    /// Get accessor for the module for this address.
350    ///
351    /// @return
352    ///     Returns the Module pointer that this address is an offset
353    ///     in, or NULL if this address doesn't belong in a module, or
354    ///     isn't resolved yet.
355    //------------------------------------------------------------------
356    Module *
357    GetModule () const;
358
359    //------------------------------------------------------------------
360    /// Get const accessor for the section.
361    ///
362    /// @return
363    ///     Returns the const lldb::Section pointer that this address is an
364    ///     offset in, or NULL if this address is absolute.
365    //------------------------------------------------------------------
366    const Section*
367    GetSection() const { return m_section; }
368
369    //------------------------------------------------------------------
370    /// Set accessor for the offset.
371    ///
372    /// @param[in] offset
373    ///     A new offset value for this object.
374    ///
375    /// @return
376    ///     Returns \b true if the offset changed, \b false otherwise.
377    //------------------------------------------------------------------
378    bool
379    SetOffset (lldb::addr_t offset);
380
381    //------------------------------------------------------------------
382    /// Set accessor for the section.
383    ///
384    /// @param[in] section
385    ///     A new lldb::Section pointer to use as the section base. Can
386    ///     be NULL for absolute addresses that are not relative to
387    ///     any section.
388    //------------------------------------------------------------------
389    void
390    SetSection (const Section* section) { m_section = section; }
391
392    //------------------------------------------------------------------
393    /// @copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
394    ///
395    /// @see SymbolContextScope
396    //------------------------------------------------------------------
397    void
398    CalculateSymbolContext (SymbolContext *sc);
399
400    //------------------------------------------------------------------
401    /// @copydoc SymbolContextScope::DumpSymbolContext(Stream*)
402    ///
403    /// @see SymbolContextScope
404    //------------------------------------------------------------------
405    void
406    DumpSymbolContext (Stream *s);
407
408protected:
409    //------------------------------------------------------------------
410    // Member variables.
411    //------------------------------------------------------------------
412    const Section* m_section;      ///< The section for the address, can be NULL.
413    lldb::addr_t      m_offset;       ///< Offset into section if \a m_section != NULL, else the absolute address value.
414};
415
416//bool operator<  (const Address& lhs, const Address& rhs);
417//bool operator<= (const Address& lhs, const Address& rhs);
418//bool operator>  (const Address& lhs, const Address& rhs);
419//bool operator>= (const Address& lhs, const Address& rhs);
420bool operator== (const Address& lhs, const Address& rhs);
421bool operator!= (const Address& lhs, const Address& rhs);
422
423//Stream& operator << (Stream& strm, const Address& so_addr);
424
425} // namespace lldb_private
426
427#endif  // liblldb_Address_h_
428