LLDB  mainline
AddressRange.cpp
Go to the documentation of this file.
1 //===-- AddressRange.cpp ----------------------------------------*- 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 
10 #include "lldb/Core/Module.h"
11 #include "lldb/Target/Target.h"
13 #include "lldb/Utility/FileSpec.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/lldb-defines.h"
16 
17 #include "llvm/Support/Compiler.h"
18 
19 #include <memory>
20 
21 #include <inttypes.h>
22 
23 namespace lldb_private {
24 class SectionList;
25 }
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 AddressRange::AddressRange() : m_base_addr(), m_byte_size(0) {}
31 
33  const SectionList *section_list)
34  : m_base_addr(file_addr, section_list), m_byte_size(byte_size) {}
35 
36 AddressRange::AddressRange(const lldb::SectionSP &section, addr_t offset,
37  addr_t byte_size)
38  : m_base_addr(section, offset), m_byte_size(byte_size) {}
39 
40 AddressRange::AddressRange(const Address &so_addr, addr_t byte_size)
41  : m_base_addr(so_addr), m_byte_size(byte_size) {}
42 
44 
45 // bool
46 // AddressRange::Contains (const Address &addr) const
47 //{
48 // const addr_t byte_size = GetByteSize();
49 // if (byte_size)
50 // return addr.GetSection() == m_base_addr.GetSection() &&
51 // (addr.GetOffset() - m_base_addr.GetOffset()) < byte_size;
52 //}
53 //
54 // bool
55 // AddressRange::Contains (const Address *addr) const
56 //{
57 // if (addr)
58 // return Contains (*addr);
59 // return false;
60 //}
61 
62 bool AddressRange::ContainsFileAddress(const Address &addr) const {
63  if (addr.GetSection() == m_base_addr.GetSection())
64  return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
65  addr_t file_base_addr = GetBaseAddress().GetFileAddress();
66  if (file_base_addr == LLDB_INVALID_ADDRESS)
67  return false;
68 
69  addr_t file_addr = addr.GetFileAddress();
70  if (file_addr == LLDB_INVALID_ADDRESS)
71  return false;
72 
73  if (file_base_addr <= file_addr)
74  return (file_addr - file_base_addr) < GetByteSize();
75 
76  return false;
77 }
78 
80  if (file_addr == LLDB_INVALID_ADDRESS)
81  return false;
82 
83  addr_t file_base_addr = GetBaseAddress().GetFileAddress();
84  if (file_base_addr == LLDB_INVALID_ADDRESS)
85  return false;
86 
87  if (file_base_addr <= file_addr)
88  return (file_addr - file_base_addr) < GetByteSize();
89 
90  return false;
91 }
92 
94  Target *target) const {
95  if (addr.GetSection() == m_base_addr.GetSection())
96  return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
97  addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
98  if (load_base_addr == LLDB_INVALID_ADDRESS)
99  return false;
100 
101  addr_t load_addr = addr.GetLoadAddress(target);
102  if (load_addr == LLDB_INVALID_ADDRESS)
103  return false;
104 
105  if (load_base_addr <= load_addr)
106  return (load_addr - load_base_addr) < GetByteSize();
107 
108  return false;
109 }
110 
111 bool AddressRange::ContainsLoadAddress(addr_t load_addr, Target *target) const {
112  if (load_addr == LLDB_INVALID_ADDRESS)
113  return false;
114 
115  addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
116  if (load_base_addr == LLDB_INVALID_ADDRESS)
117  return false;
118 
119  if (load_base_addr <= load_addr)
120  return (load_addr - load_base_addr) < GetByteSize();
121 
122  return false;
123 }
124 
125 bool AddressRange::Extend(const AddressRange &rhs_range) {
126  addr_t lhs_end_addr = GetBaseAddress().GetFileAddress() + GetByteSize();
127  addr_t rhs_base_addr = rhs_range.GetBaseAddress().GetFileAddress();
128 
129  if (!ContainsFileAddress(rhs_range.GetBaseAddress()) &&
130  lhs_end_addr != rhs_base_addr)
131  // The ranges don't intersect at all on the right side of this range.
132  return false;
133 
134  addr_t rhs_end_addr = rhs_base_addr + rhs_range.GetByteSize();
135  if (lhs_end_addr >= rhs_end_addr)
136  // The rhs range totally overlaps this one, nothing to add.
137  return false;
138 
139  m_byte_size += rhs_end_addr - lhs_end_addr;
140  return true;
141 }
142 
144  m_base_addr.Clear();
145  m_byte_size = 0;
146 }
147 
149  Address::DumpStyle fallback_style) const {
150  addr_t vmaddr = LLDB_INVALID_ADDRESS;
151  int addr_size = sizeof(addr_t);
152  if (target)
153  addr_size = target->GetArchitecture().GetAddressByteSize();
154 
155  bool show_module = false;
156  switch (style) {
157  default:
158  break;
161  s->PutChar('[');
162  m_base_addr.Dump(s, target, style, fallback_style);
163  s->PutChar('-');
164  s->Address(m_base_addr.GetOffset() + GetByteSize(), addr_size);
165  s->PutChar(')');
166  return true;
167  break;
168 
170  show_module = true;
171  LLVM_FALLTHROUGH;
173  vmaddr = m_base_addr.GetFileAddress();
174  break;
175 
177  vmaddr = m_base_addr.GetLoadAddress(target);
178  break;
179  }
180 
181  if (vmaddr != LLDB_INVALID_ADDRESS) {
182  if (show_module) {
183  ModuleSP module_sp(GetBaseAddress().GetModule());
184  if (module_sp)
185  s->Printf("%s", module_sp->GetFileSpec().GetFilename().AsCString(
186  "<Unknown>"));
187  }
188  s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size);
189  return true;
190  } else if (fallback_style != Address::DumpStyleInvalid) {
191  return Dump(s, target, fallback_style, Address::DumpStyleInvalid);
192  }
193 
194  return false;
195 }
196 
198  s->Printf("%p: AddressRange section = %p, offset = 0x%16.16" PRIx64
199  ", byte_size = 0x%16.16" PRIx64 "\n",
200  static_cast<const void *>(this),
201  static_cast<void *>(m_base_addr.GetSection().get()),
203 }
204 //
205 // bool
206 // lldb::operator== (const AddressRange& lhs, const AddressRange& rhs)
207 //{
208 // if (lhs.GetBaseAddress() == rhs.GetBaseAddress())
209 // return lhs.GetByteSize() == rhs.GetByteSize();
210 // return false;
211 //}
void Address(uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:79
Address m_base_addr
The section offset base address of this range.
Definition: AddressRange.h:252
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
lldb::addr_t GetByteSize() const
Get accessor for the byte size of this range.
Definition: AddressRange.h:232
const ArchSpec & GetArchitecture() const
Definition: Target.h:941
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:742
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:292
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX) const
Dump a description of this object to a Stream.
Definition: Address.cpp:374
bool ContainsFileAddress(const Address &so_addr) const
Check if a section offset address is contained in this range.
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:272
void AddressRange(uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address range to this stream.
Definition: Stream.cpp:92
Display as the section pointer + offset (debug output).
Definition: Address.h:90
DumpStyle
Dump styles allow the Address::Dump(Stream *,DumpStyle) const function to display Address contents in...
Definition: Address.h:84
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
bool ContainsLoadAddress(const Address &so_addr, Target *target) const
Check if a section offset so_addr when represented as a load address is contained within this object&#39;...
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
lldb::addr_t m_byte_size
The size in bytes of this address range.
Definition: AddressRange.h:253
Display as the section name + offset.
Definition: Address.h:86
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
A section + offset based address class.
Definition: Address.h:80
size_t PutChar(char ch)
Definition: Stream.cpp:103
uint64_t addr_t
Definition: lldb-types.h:83
void DumpDebug(Stream *s) const
Dump a debug description of this object to a Stream.
Display as the load address (if resolved).
Definition: Address.h:104
bool Dump(Stream *s, Target *target, Address::DumpStyle style, Address::DumpStyle fallback_style=Address::DumpStyleInvalid) const
Dump a description of this object to a Stream.
Definition: SBAddress.h:15
Display as the file address with the module name prepended (if any).
Definition: Address.h:99
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:220
AddressRange()
Default constructor.
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:410
A section + offset based address range class.
Definition: AddressRange.h:32
void Clear()
Clear the object&#39;s state.
void Clear()
Clear the object&#39;s state.
Definition: Address.h:189
Display as the file address (if any).
Definition: Address.h:95
bool Extend(const AddressRange &rhs_range)
Extends this range with rhs_range if it overlaps this range on the right side.