blob: e125b693d6f64e0d682ebcf1bffec32033f04b20 [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"
Greg Claytonc9800662010-09-10 01:30:46 +000011#include "lldb/Core/Module.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000012#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000013#include "lldb/Utility/ConstString.h" // for ConstString
14#include "lldb/Utility/FileSpec.h" // for FileSpec
Zachary Turnerbf9a7732017-02-02 21:39:50 +000015#include "lldb/Utility/Stream.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000016#include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS
17
18#include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
19
20#include <memory> // for shared_ptr
21
22#include <inttypes.h> // for PRIx64
23
24namespace lldb_private {
25class SectionList;
26}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031AddressRange::AddressRange() : m_base_addr(), m_byte_size(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033AddressRange::AddressRange(addr_t file_addr, addr_t byte_size,
34 const SectionList *section_list)
35 : m_base_addr(file_addr, section_list), m_byte_size(byte_size) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037AddressRange::AddressRange(const lldb::SectionSP &section, addr_t offset,
38 addr_t byte_size)
39 : m_base_addr(section, offset), m_byte_size(byte_size) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041AddressRange::AddressRange(const Address &so_addr, addr_t byte_size)
42 : m_base_addr(so_addr), m_byte_size(byte_size) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044AddressRange::~AddressRange() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
Kate Stoneb9c1b512016-09-06 20:57:50 +000046// bool
47// AddressRange::Contains (const Address &addr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048//{
49// const addr_t byte_size = GetByteSize();
50// if (byte_size)
Kate Stoneb9c1b512016-09-06 20:57:50 +000051// return addr.GetSection() == m_base_addr.GetSection() &&
52// (addr.GetOffset() - m_base_addr.GetOffset()) < byte_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053//}
54//
Kate Stoneb9c1b512016-09-06 20:57:50 +000055// bool
56// AddressRange::Contains (const Address *addr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057//{
58// if (addr)
59// return Contains (*addr);
60// return false;
61//}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063bool AddressRange::ContainsFileAddress(const Address &addr) const {
64 if (addr.GetSection() == m_base_addr.GetSection())
65 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
66 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
67 if (file_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 addr_t file_addr = addr.GetFileAddress();
71 if (file_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000073
74 if (file_base_addr <= file_addr)
75 return (file_addr - file_base_addr) < GetByteSize();
76
77 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080bool AddressRange::ContainsFileAddress(addr_t file_addr) const {
81 if (file_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 addr_t file_base_addr = GetBaseAddress().GetFileAddress();
85 if (file_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000087
88 if (file_base_addr <= file_addr)
89 return (file_addr - file_base_addr) < GetByteSize();
90
91 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094bool AddressRange::ContainsLoadAddress(const Address &addr,
95 Target *target) const {
96 if (addr.GetSection() == m_base_addr.GetSection())
97 return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
98 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
99 if (load_base_addr == LLDB_INVALID_ADDRESS)
100 return false;
101
102 addr_t load_addr = addr.GetLoadAddress(target);
103 if (load_addr == LLDB_INVALID_ADDRESS)
104 return false;
105
106 if (load_base_addr <= load_addr)
107 return (load_addr - load_base_addr) < GetByteSize();
108
109 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112bool AddressRange::ContainsLoadAddress(addr_t load_addr, Target *target) const {
113 if (load_addr == LLDB_INVALID_ADDRESS)
114 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 addr_t load_base_addr = GetBaseAddress().GetLoadAddress(target);
117 if (load_base_addr == LLDB_INVALID_ADDRESS)
118 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 if (load_base_addr <= load_addr)
121 return (load_addr - load_base_addr) < GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 return false;
124}
125
126void AddressRange::Clear() {
127 m_base_addr.Clear();
128 m_byte_size = 0;
129}
130
131bool AddressRange::Dump(Stream *s, Target *target, Address::DumpStyle style,
132 Address::DumpStyle fallback_style) const {
133 addr_t vmaddr = LLDB_INVALID_ADDRESS;
134 int addr_size = sizeof(addr_t);
135 if (target)
136 addr_size = target->GetArchitecture().GetAddressByteSize();
137
138 bool show_module = false;
139 switch (style) {
140 default:
141 break;
142 case Address::DumpStyleSectionNameOffset:
143 case Address::DumpStyleSectionPointerOffset:
144 s->PutChar('[');
145 m_base_addr.Dump(s, target, style, fallback_style);
146 s->PutChar('-');
147 s->Address(m_base_addr.GetOffset() + GetByteSize(), addr_size);
148 s->PutChar(')');
149 return true;
150 break;
151
152 case Address::DumpStyleModuleWithFileAddress:
153 show_module = true;
154 LLVM_FALLTHROUGH;
155 case Address::DumpStyleFileAddress:
156 vmaddr = m_base_addr.GetFileAddress();
157 break;
158
159 case Address::DumpStyleLoadAddress:
160 vmaddr = m_base_addr.GetLoadAddress(target);
161 break;
162 }
163
164 if (vmaddr != LLDB_INVALID_ADDRESS) {
165 if (show_module) {
166 ModuleSP module_sp(GetBaseAddress().GetModule());
167 if (module_sp)
168 s->Printf("%s", module_sp->GetFileSpec().GetFilename().AsCString(
169 "<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size);
172 return true;
173 } else if (fallback_style != Address::DumpStyleInvalid) {
174 return Dump(s, target, fallback_style, Address::DumpStyleInvalid);
175 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180void AddressRange::DumpDebug(Stream *s) const {
181 s->Printf("%p: AddressRange section = %p, offset = 0x%16.16" PRIx64
182 ", byte_size = 0x%16.16" PRIx64 "\n",
183 static_cast<const void *>(this),
184 static_cast<void *>(m_base_addr.GetSection().get()),
185 m_base_addr.GetOffset(), GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188// bool
189// lldb::operator== (const AddressRange& lhs, const AddressRange& rhs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190//{
191// if (lhs.GetBaseAddress() == rhs.GetBaseAddress())
192// return lhs.GetByteSize() == rhs.GetByteSize();
193// return false;
194//}