ROSE  0.9.6a
 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 
34  addr = 0;
35  section = NULL;
36 }
37 
41 {
42  addr = rva;
43  this->section = NULL;
44  set_section(section);
45 }
46 
47 
50 {
51  addr = other.addr;
52  section = other.section;
53 }
54 
58 {
59  addr = other.addr;
60  section = other.section;
61  return *this;
62 }
63 
68 {
69  assert(section!=NULL);
70  assert(section->is_mapped());
72  return rose_rva_t(rva, section);
73 }
74 
76 bool
78 {
79  return section!=NULL;
80 }
81 
86 {
88  if (section) {
89  assert(section->is_mapped());
91  }
92  return rva;
93 }
94 
99 {
100  addr = rva;
101  if (section) {
102  assert(section->is_mapped());
104  }
105  return *this;
106 }
107 
111 {
112  return section;
113 }
114 
117 rose_rva_t&
119 {
120  assert(new_section==NULL || new_section->is_mapped());
121  if (section) {
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&
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 
145 {
146  if (!section)
147  return addr;
148  assert(section->is_mapped());
150 }
151 
156 {
157  return addr;
158 }
159 
163 {
164  assert(s!=NULL && s->is_mapped());
165  return get_rva() - s->get_mapped_preferred_rva();
166 }
167 
169 void
171 {
172  addr += amount;
173 }
174 
177 std::string
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();}