1 //===-- 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"
11 #include "lldb/Core/Stream.h"
12 #include "lldb/Target/Process.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 AddressRange::AddressRange () :
18     m_base_addr(),
19     m_byte_size(0)
20 {
21 }
22 
23 AddressRange::AddressRange (addr_t file_addr, addr_t byte_size, const SectionList *section_list) :
24     m_base_addr(file_addr, section_list),
25     m_byte_size(byte_size)
26 {
27 }
28 
29 AddressRange::AddressRange (const Section* section, addr_t offset, addr_t byte_size) :
30     m_base_addr(section, offset),
31     m_byte_size(byte_size)
32 {
33 }
34 
35 AddressRange::AddressRange (const Address& so_addr, addr_t byte_size) :
36     m_base_addr(so_addr),
37     m_byte_size(byte_size)
38 {
39 }
40 
41 AddressRange::~AddressRange ()
42 {
43 }
44 
45 Address &
46 AddressRange::GetBaseAddress()
47 {
48     return m_base_addr;
49 }
50 
51 const Address &
52 AddressRange::GetBaseAddress() const
53 {
54     return m_base_addr;
55 }
56 
57 addr_t
58 AddressRange::GetByteSize() const
59 {
60     return m_byte_size;
61 }
62 
63 void
64 AddressRange::SetByteSize(addr_t byte_size)
65 {
66     m_byte_size = byte_size;
67 }
68 
69 //bool
70 //AddressRange::Contains (const Address &addr) const
71 //{
72 //    const addr_t byte_size = GetByteSize();
73 //    if (byte_size)
74 //        return addr.GetSection() == m_base_addr.GetSection() && (addr.GetOffset() - m_base_addr.GetOffset()) < byte_size;
75 //}
76 //
77 //bool
78 //AddressRange::Contains (const Address *addr) const
79 //{
80 //    if (addr)
81 //        return Contains (*addr);
82 //    return false;
83 //}
84 
85 bool
86 AddressRange::ContainsFileAddress (const Address &addr) const
87 {
88     if (addr.GetSection() == m_base_addr.GetSection())
89         return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
90     addr_t file_base_addr = GetBaseAddress().GetFileAddress();
91     if (file_base_addr == LLDB_INVALID_ADDRESS)
92         return false;
93 
94     addr_t file_addr = addr.GetFileAddress();
95     if (file_addr == LLDB_INVALID_ADDRESS)
96         return false;
97 
98     if (file_base_addr <= file_addr)
99         return (file_addr - file_base_addr) < GetByteSize();
100 
101     return false;
102 }
103 
104 bool
105 AddressRange::ContainsFileAddress (addr_t file_addr) const
106 {
107     if (file_addr == LLDB_INVALID_ADDRESS)
108         return false;
109 
110     addr_t file_base_addr = GetBaseAddress().GetFileAddress();
111     if (file_base_addr == LLDB_INVALID_ADDRESS)
112         return false;
113 
114     if (file_base_addr <= file_addr)
115         return (file_addr - file_base_addr) < GetByteSize();
116 
117     return false;
118 }
119 
120 
121 bool
122 AddressRange::ContainsLoadAddress (const Address &addr, Process *process) const
123 {
124     if (addr.GetSection() == m_base_addr.GetSection())
125         return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
126     addr_t load_base_addr = GetBaseAddress().GetLoadAddress(process);
127     if (load_base_addr == LLDB_INVALID_ADDRESS)
128         return false;
129 
130     addr_t load_addr = addr.GetLoadAddress(process);
131     if (load_addr == LLDB_INVALID_ADDRESS)
132         return false;
133 
134     if (load_base_addr <= load_addr)
135         return (load_addr - load_base_addr) < GetByteSize();
136 
137     return false;
138 }
139 
140 bool
141 AddressRange::ContainsLoadAddress (addr_t load_addr, Process *process) const
142 {
143     if (load_addr == LLDB_INVALID_ADDRESS)
144         return false;
145 
146     addr_t load_base_addr = GetBaseAddress().GetLoadAddress(process);
147     if (load_base_addr == LLDB_INVALID_ADDRESS)
148         return false;
149 
150     if (load_base_addr <= load_addr)
151         return (load_addr - load_base_addr) < GetByteSize();
152 
153     return false;
154 }
155 
156 void
157 AddressRange::Clear()
158 {
159     m_base_addr.Clear();
160     m_byte_size = 0;
161 }
162 
163 bool
164 AddressRange::Dump(Stream *s, Process *process, Address::DumpStyle style, Address::DumpStyle fallback_style) const
165 {
166     addr_t vmaddr = LLDB_INVALID_ADDRESS;
167     int addr_size = sizeof (addr_t);
168     if (process)
169       addr_size = process->GetAddressByteSize ();
170 
171     switch (style)
172     {
173     case Address::DumpStyleSectionNameOffset:
174     case Address::DumpStyleSectionPointerOffset:
175         s->PutChar ('[');
176         m_base_addr.Dump(s, process, style, fallback_style);
177         s->PutChar ('-');
178         s->Address (m_base_addr.GetOffset() + GetByteSize(), addr_size);
179         s->PutChar (')');
180         return true;
181         break;
182 
183     case Address::DumpStyleFileAddress:
184         vmaddr = m_base_addr.GetFileAddress();
185         break;
186 
187     case Address::DumpStyleLoadAddress:
188         vmaddr = m_base_addr.GetLoadAddress(process);
189         break;
190     }
191 
192     if (vmaddr != LLDB_INVALID_ADDRESS)
193     {
194         s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size);
195         return true;
196     }
197 
198     return false;
199 }
200 
201 
202 void
203 AddressRange::DumpDebug (Stream *s) const
204 {
205     s->Printf("%.*p: AddressRange section = %*p, offset = 0x%16.16llx, byte_size = 0x%16.16llx\n", (int)sizeof(void*) * 2, this, (int)sizeof(void*) * 2, m_base_addr.GetSection(), m_base_addr.GetOffset(), GetByteSize());
206 }
207 
208 size_t
209 AddressRange::MemorySize () const
210 {
211     // Noting special for the memory size of a single AddressRange object,
212     // it is just the size of itself.
213     return sizeof(AddressRange);
214 }
215 //
216 //bool
217 //lldb::operator==    (const AddressRange& lhs, const AddressRange& rhs)
218 //{
219 //    if (lhs.GetBaseAddress() == rhs.GetBaseAddress())
220 //        return lhs.GetByteSize() == rhs.GetByteSize();
221 //    return false;
222 //}
223