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