ROSE
0.9.6a
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
Rva.C
Go to the documentation of this file.
1
// Relative Virtual Addresses (RVA)
3
// An RVA is always relative to the base virtual address (base_va) defined in an executable file header.
4
// A rose_rva_t is optionally tied to an SgAsmGenericSection so that if the preferred mapped address of the section is
5
// modified then the RVA stored in the rose_rva_t object is also adjusted. The section-relative offset is always treated as
6
// an unsigned quantity, but negative offsets can be accommodated via integer overflow.
7
//
8
// Be careful about adjusting the RVA (the address or section) using ROSETTA's accessors.
9
// symbol.p_address.set_section(section); // this works
10
// symbol.get_address().set_section(section); // using ROSETTA accessor modifies a temporary copy of the RVA
11
// But if ROSETTA returns a vector then we can modify the RVA:
12
// symbol.p_addresses[0].set_section(section); // this works
13
// symbol.get_addresses()[0].set_section(section); // so does this.
15
16
#include "
sage3basic.h
"
17
33
rose_rva_t::rose_rva_t
() {
34
addr
= 0;
35
section
= NULL;
36
}
37
40
rose_rva_t::rose_rva_t
(
rose_addr_t
rva
,
SgAsmGenericSection
*section
/*=NULL*/
)
41
{
42
addr
=
rva
;
43
this->section = NULL;
44
set_section
(section);
45
}
46
47
49
rose_rva_t::rose_rva_t
(
const
rose_rva_t
&other)
50
{
51
addr
= other.
addr
;
52
section
= other.
section
;
53
}
54
56
rose_rva_t
57
rose_rva_t::operator=
(
const
rose_rva_t
&other)
58
{
59
addr
= other.
addr
;
60
section
= other.
section
;
61
return
*
this
;
62
}
63
66
rose_rva_t
67
rose_rva_t::section_relative
(
SgAsmGenericSection
*section,
rose_addr_t
offset
)
68
{
69
assert(section!=NULL);
70
assert(section->
is_mapped
());
71
rose_addr_t
rva
=section->
get_mapped_preferred_rva
() +
offset
;
72
return
rose_rva_t
(rva, section);
73
}
74
76
bool
77
rose_rva_t::is_bound
()
const
78
{
79
return
section
!=NULL;
80
}
81
84
rose_addr_t
85
rose_rva_t::get_rva
()
const
86
{
87
rose_addr_t
rva
=
addr
;
88
if
(
section
) {
89
assert(
section
->
is_mapped
());
90
rva +=
section
->
get_mapped_preferred_rva
();
91
}
92
return
rva
;
93
}
94
97
rose_rva_t
&
98
rose_rva_t::set_rva
(
rose_addr_t
rva
)
99
{
100
addr
=
rva
;
101
if
(
section
) {
102
assert(
section
->
is_mapped
());
103
addr
-=
section
->
get_mapped_preferred_rva
();
104
}
105
return
*
this
;
106
}
107
109
SgAsmGenericSection
*
110
rose_rva_t::get_section
()
const
111
{
112
return
section
;
113
}
114
117
rose_rva_t
&
118
rose_rva_t::set_section
(
SgAsmGenericSection
*new_section)
119
{
120
assert(new_section==NULL || new_section->
is_mapped
());
121
if
(
section
) {
122
addr
+=
section
->
get_mapped_preferred_rva
();
123
section
= NULL;
124
}
125
if
(new_section)
126
addr
-= new_section->
get_mapped_preferred_rva
();
127
section
= new_section;
128
return
*
this
;
129
}
130
133
rose_rva_t
&
134
rose_rva_t::bind
(
SgAsmGenericHeader
*fhdr)
135
{
136
rose_addr_t
va =
get_rva
() + fhdr->
get_base_va
();
137
SgAsmGenericSection
*secbind = fhdr->
get_best_section_by_va
(va,
true
);
138
return
set_section
(secbind);
139
}
140
143
rose_addr_t
144
rose_rva_t::get_va
()
const
145
{
146
if
(!
section
)
147
return
addr
;
148
assert(
section
->
is_mapped
());
149
return
addr
+
section
->
get_mapped_preferred_rva
() +
section
->
get_base_va
();
150
}
151
154
rose_addr_t
155
rose_rva_t::get_rel
()
const
156
{
157
return
addr
;
158
}
159
161
rose_addr_t
162
rose_rva_t::get_rel
(
SgAsmGenericSection
*s)
163
{
164
assert(s!=NULL && s->
is_mapped
());
165
return
get_rva
() - s->
get_mapped_preferred_rva
();
166
}
167
169
void
170
rose_rva_t::increment
(
rose_addr_t
amount)
171
{
172
addr
+= amount;
173
}
174
177
std::string
178
rose_rva_t::to_string
()
const
179
{
180
char
s[1024];
181
sprintf(s,
"0x%08"
PRIx64
" (%"
PRIu64
")"
,
get_rva
(),
get_rva
());
182
std::string ss = s;
183
184
if
(
get_section
()) {
185
sprintf(s,
" + 0x%08"
PRIx64
" (%"
PRIu64
")"
,
get_rel
(),
get_rel
());
186
ss +=
" <"
+
get_section
()->
get_name
()->
get_string
(
true
) + s +
">"
;
187
}
188
return
ss;
189
}
190
191
192
std::ostream &
193
operator<<
(std::ostream &os,
const
rose_rva_t
&
rva
)
194
{
195
os << rva.
to_string
();
196
return
os;
197
}
198
199
/* Arithmetic */
200
rose_addr_t
operator+
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() + a2.
get_rva
();}
201
rose_addr_t
operator-
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() - a2.
get_rva
();}
202
203
/* Comparisons */
204
bool
operator<
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() < a2.
get_rva
();}
205
bool
operator<=
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() <= a2.
get_rva
();}
206
bool
operator>
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() > a2.
get_rva
();}
207
bool
operator>=
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() >= a2.
get_rva
();}
208
bool
operator==
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() == a2.
get_rva
();}
209
bool
operator!=
(
const
rose_rva_t
&a1,
const
rose_rva_t
&a2) {
return
a1.
get_rva
() != a2.
get_rva
();}
rose-edg4x
src
frontend
BinaryFormats
Rva.C
Generated on Mon May 5 2014 17:29:26 for ROSE by
1.8.4