blob: c1507797b374cddb1eefd996883231cefb2d317d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- AddressRange.cpp ----------------------------------------*- 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#include "lldb/Core/AddressRange.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000011#include "lldb/Core/ArchSpec.h" // for ArchSpec
Greg Claytonc9800662010-09-10 01:30:46 +000012#include "lldb/Core/Module.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000013#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000014#include "lldb/Utility/ConstString.h" // for ConstString
15#include "lldb/Utility/FileSpec.h" // for FileSpec
Zachary Turnerbf9a7732017-02-02 21:39:50 +000016#include "lldb/Utility/Stream.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000017#include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS
18
19#include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
20
21#include <memory> // for shared_ptr
22
23#include <inttypes.h> // for PRIx64
24
25namespace lldb_private {
26class SectionList;
27}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032AddressRange::AddressRange() : m_base_addr(), m_byte_size(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034AddressRange::AddressRange(addr_t file_addr, addr_t byte_size,
35 const SectionList *section_list)
36 : m_base_addr(file_addr, section_list), m_byte_size(byte_size) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038AddressRange::AddressRange(const lldb::SectionSP &section, addr_t offset,
39 addr_t byte_size)
40 : m_base_addr(section, offset), m_byte_size(byte_size) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042AddressRange::AddressRange(const Address &so_addr, addr_t byte_size)
43 : m_base_addr(so_addr), m_byte_size(byte_size) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045AddressRange::~AddressRange() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047// bool
48// AddressRange::Contains (const Address &addr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049//{
50// const addr_t byte_size = GetByteSize();
51// if (byte_size)
Kate Stoneb9c1b512016-09-06 20:57:50 +000052// return addr.GetSection() == m_base_addr.GetSection() &&
53// (addr.GetOffset() - m_base_addr.GetOffset()) < byte_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054//}
55//
Kate Stoneb9c1b512016-09-06 20:57:50 +000056// bool
57// AddressRange::Contains (const Address *addr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058//{
59// if (addr)
60// return Contains (*addr);
61// return false;
62//}
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064bool AddressRange::ContainsFileAddress(const Address &addr) const {
65 if (addr.GetSection() == m_base_addr.GetSection())
66 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
67 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
68 if (file_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 addr_t file_addr = addr.GetFileAddress();
72 if (file_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000074
75 if (file_base_addr <= file_addr)
76 return (file_addr - file_base_addr) < GetByteSize();
77
78 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081bool AddressRange::ContainsFileAddress(addr_t file_addr) const {
82 if (file_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
86 if (file_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000088
89 if (file_base_addr <= file_addr)
90 return (file_addr - file_base_addr) < GetByteSize();
91
92 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095bool AddressRange::ContainsLoadAddress(const Address &addr,
96 Target *target) const {
97 if (addr.GetSection() == m_base_addr.GetSection())
98 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
99 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
100 if (load_base_addr == LLDB_INVALID_ADDRESS)
101 return false;
102
103 addr_t load_addr = addr.GetLoadAddress(target);
104 if (load_addr == LLDB_INVALID_ADDRESS)
105 return false;
106
107 if (load_base_addr <= load_addr)
108 return (load_addr - load_base_addr) < GetByteSize();
109
110 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113bool AddressRange::ContainsLoadAddress(addr_t load_addr, Target *target) const {
114 if (load_addr == LLDB_INVALID_ADDRESS)
115 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
118 if (load_base_addr == LLDB_INVALID_ADDRESS)
119 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 if (load_base_addr <= load_addr)
122 return (load_addr - load_base_addr) < GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 return false;
125}
126
127void AddressRange::Clear() {
128 m_base_addr.Clear();
129 m_byte_size = 0;
130}
131
132bool AddressRange::Dump(Stream *s, Target *target, Address::DumpStyle style,
133 Address::DumpStyle fallback_style) const {
134 addr_t vmaddr = LLDB_INVALID_ADDRESS;
135 int addr_size = sizeof(addr_t);
136 if (target)
137 addr_size = target->GetArchitecture().GetAddressByteSize();
138
139 bool show_module = false;
140 switch (style) {
141 default:
142 break;
143 case Address::DumpStyleSectionNameOffset:
144 case Address::DumpStyleSectionPointerOffset:
145 s->PutChar('[');
146 m_base_addr.Dump(s, target, style, fallback_style);
147 s->PutChar('-');
148 s->Address(m_base_addr.GetOffset() + GetByteSize(), addr_size);
149 s->PutChar(')');
150 return true;
151 break;
152
153 case Address::DumpStyleModuleWithFileAddress:
154 show_module = true;
155 LLVM_FALLTHROUGH;
156 case Address::DumpStyleFileAddress:
157 vmaddr = m_base_addr.GetFileAddress();
158 break;
159
160 case Address::DumpStyleLoadAddress:
161 vmaddr = m_base_addr.GetLoadAddress(target);
162 break;
163 }
164
165 if (vmaddr != LLDB_INVALID_ADDRESS) {
166 if (show_module) {
167 ModuleSP module_sp(GetBaseAddress().GetModule());
168 if (module_sp)
169 s->Printf("%s", module_sp->GetFileSpec().GetFilename().AsCString(
170 "<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size);
173 return true;
174 } else if (fallback_style != Address::DumpStyleInvalid) {
175 return Dump(s, target, fallback_style, Address::DumpStyleInvalid);
176 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181void AddressRange::DumpDebug(Stream *s) const {
182 s->Printf("%p: AddressRange section = %p, offset = 0x%16.16" PRIx64
183 ", byte_size = 0x%16.16" PRIx64 "\n",
184 static_cast<const void *>(this),
185 static_cast<void *>(m_base_addr.GetSection().get()),
186 m_base_addr.GetOffset(), GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189// bool
190// lldb::operator== (const AddressRange& lhs, const AddressRange& rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191//{
192// if (lhs.GetBaseAddress() == rhs.GetBaseAddress())
193// return lhs.GetByteSize() == rhs.GetByteSize();
194// return false;
195//}