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 //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() && (addr.GetOffset() - m_base_addr.GetOffset()) < byte_size;
51 //}
52 //
53 //bool
54 //AddressRange::Contains (const Address *addr) const
55 //{
56 //    if (addr)
57 //        return Contains (*addr);
58 //    return false;
59 //}
60 
61 bool
62 AddressRange::ContainsFileAddress (const Address &addr) const
63 {
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)
68         return false;
69 
70     addr_t file_addr = addr.GetFileAddress();
71     if (file_addr == LLDB_INVALID_ADDRESS)
72         return false;
73 
74     if (file_base_addr <= file_addr)
75         return (file_addr - file_base_addr) < GetByteSize();
76 
77     return false;
78 }
79 
80 bool
81 AddressRange::ContainsFileAddress (addr_t file_addr) const
82 {
83     if (file_addr == LLDB_INVALID_ADDRESS)
84         return false;
85 
86     addr_t file_base_addr = GetBaseAddress().GetFileAddress();
87     if (file_base_addr == LLDB_INVALID_ADDRESS)
88         return false;
89 
90     if (file_base_addr <= file_addr)
91         return (file_addr - file_base_addr) < GetByteSize();
92 
93     return false;
94 }
95 
96 
97 bool
98 AddressRange::ContainsLoadAddress (const Address &addr, Process *process) const
99 {
100     if (addr.GetSection() == m_base_addr.GetSection())
101         return (addr.GetOffset() - m_base_addr.GetOffset()) < GetByteSize();
102     addr_t load_base_addr = GetBaseAddress().GetLoadAddress(process);
103     if (load_base_addr == LLDB_INVALID_ADDRESS)
104         return false;
105 
106     addr_t load_addr = addr.GetLoadAddress(process);
107     if (load_addr == LLDB_INVALID_ADDRESS)
108         return false;
109 
110     if (load_base_addr <= load_addr)
111         return (load_addr - load_base_addr) < GetByteSize();
112 
113     return false;
114 }
115 
116 bool
117 AddressRange::ContainsLoadAddress (addr_t load_addr, Process *process) const
118 {
119     if (load_addr == LLDB_INVALID_ADDRESS)
120         return false;
121 
122     addr_t load_base_addr = GetBaseAddress().GetLoadAddress(process);
123     if (load_base_addr == LLDB_INVALID_ADDRESS)
124         return false;
125 
126     if (load_base_addr <= load_addr)
127         return (load_addr - load_base_addr) < GetByteSize();
128 
129     return false;
130 }
131 
132 void
133 AddressRange::Clear()
134 {
135     m_base_addr.Clear();
136     m_byte_size = 0;
137 }
138 
139 bool
140 AddressRange::Dump(Stream *s, Process *process, Address::DumpStyle style, Address::DumpStyle fallback_style) const
141 {
142     addr_t vmaddr = LLDB_INVALID_ADDRESS;
143     int addr_size = sizeof (addr_t);
144     if (process)
145       addr_size = process->GetAddressByteSize ();
146 
147     switch (style)
148     {
149     case Address::DumpStyleSectionNameOffset:
150     case Address::DumpStyleSectionPointerOffset:
151         s->PutChar ('[');
152         m_base_addr.Dump(s, process, style, fallback_style);
153         s->PutChar ('-');
154         s->Address (m_base_addr.GetOffset() + GetByteSize(), addr_size);
155         s->PutChar (')');
156         return true;
157         break;
158 
159     case Address::DumpStyleFileAddress:
160         vmaddr = m_base_addr.GetFileAddress();
161         break;
162 
163     case Address::DumpStyleLoadAddress:
164         vmaddr = m_base_addr.GetLoadAddress(process);
165         break;
166     }
167 
168     if (vmaddr != LLDB_INVALID_ADDRESS)
169     {
170         s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size);
171         return true;
172     }
173 
174     return false;
175 }
176 
177 
178 void
179 AddressRange::DumpDebug (Stream *s) const
180 {
181     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());
182 }
183 //
184 //bool
185 //lldb::operator==    (const AddressRange& lhs, const AddressRange& rhs)
186 //{
187 //    if (lhs.GetBaseAddress() == rhs.GetBaseAddress())
188 //        return lhs.GetByteSize() == rhs.GetByteSize();
189 //    return false;
190 //}
191