ROSE  0.11.2.0
Rva.C
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 #include <rosePublicConfig.h>
16 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
17 #include "sage3basic.h"
18 
35  addr = 0;
36  section = NULL;
37 }
38 
41 rose_rva_t::rose_rva_t(rose_addr_t rva, SgAsmGenericSection *section/*=NULL*/)
42 {
43  addr = rva;
44  this->section = NULL;
45  set_section(section);
46 }
47 
48 
51 {
52  addr = other.addr;
53  section = other.section;
54 }
55 
59 {
60  addr = other.addr;
61  section = other.section;
62  return *this;
63 }
64 
69 {
70  assert(section!=NULL);
71  assert(section->is_mapped());
72  rose_addr_t rva =section->get_mapped_preferred_rva() + offset;
73  return rose_rva_t(rva, section);
74 }
75 
77 bool
79 {
80  return section!=NULL;
81 }
82 
85 rose_addr_t
87 {
88  rose_addr_t rva = addr;
89  if (section) {
90  assert(section->is_mapped());
91  rva += section->get_mapped_preferred_rva();
92  }
93  return rva;
94 }
95 
99 rose_rva_t::set_rva(rose_addr_t rva)
100 {
101  addr = rva;
102  if (section) {
103  assert(section->is_mapped());
104  addr -= section->get_mapped_preferred_rva();
105  }
106  return *this;
107 }
108 
112 {
113  return section;
114 }
115 
118 rose_rva_t&
120 {
121  assert(new_section==NULL || new_section->is_mapped());
122  if (section) {
123  addr += section->get_mapped_preferred_rva();
124  section = NULL;
125  }
126  if (new_section)
127  addr -= new_section->get_mapped_preferred_rva();
128  section = new_section;
129  return *this;
130 }
131 
134 rose_rva_t&
136 {
137  rose_addr_t va = get_rva() + fhdr->get_base_va();
138  SgAsmGenericSection *secbind = fhdr->get_best_section_by_va(va, true);
139  return set_section(secbind);
140 }
141 
144 rose_addr_t
146 {
147  if (!section)
148  return addr;
149  assert(section->is_mapped());
150  return addr + section->get_mapped_preferred_rva() + section->get_base_va();
151 }
152 
155 rose_addr_t
157 {
158  return addr;
159 }
160 
162 rose_addr_t
164 {
165  assert(s!=NULL && s->is_mapped());
166  return get_rva() - s->get_mapped_preferred_rva();
167 }
168 
170 void
171 rose_rva_t::increment(rose_addr_t amount)
172 {
173  addr += amount;
174 }
175 
178 std::string
180 {
181  char s[1024];
182  sprintf(s, "0x%08" PRIx64 " (%" PRIu64 ")", get_rva(), get_rva());
183  std::string ss = s;
184 
185  if (get_section()) {
186  sprintf(s, " + 0x%08" PRIx64 " (%" PRIu64 ")", get_rel(), get_rel());
187  ss += " <" + get_section()->get_name()->get_string(true) + s + ">";
188  }
189  return ss;
190 }
191 
192 
193 std::ostream &
194 operator<<(std::ostream &os, const rose_rva_t &rva)
195 {
196  os << rva.to_string();
197  return os;
198 }
199 
200 /* Arithmetic */
201 rose_addr_t operator+(const rose_rva_t &a1, const rose_rva_t &a2) {return a1.get_rva() + a2.get_rva();}
202 rose_addr_t operator-(const rose_rva_t &a1, const rose_rva_t &a2) {return a1.get_rva() - a2.get_rva();}
203 
204 /* Comparisons */
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();}
210 bool operator!=(const rose_rva_t &a1, const rose_rva_t &a2) {return a1.get_rva() != a2.get_rva();}
211 
212 #endif
static rose_rva_t section_relative(SgAsmGenericSection *section, rose_addr_t offset)
Class method to construct a new RVA which is an offset from the beginning of a section.
Definition: Rva.C:68
rose_addr_t get_va() const
Return the absolute address if known.
Definition: Rva.C:145
rose_rva_t & bind(SgAsmGenericHeader *)
Binds this RVA to the best available section from the specified file header.
Definition: Rva.C:135
Contiguous region of a file.
SgAsmGenericSection * get_best_section_by_va(rose_addr_t va, bool use_preferred, size_t *nfound=0) const
Like SgAsmGenericFile::get_best_section_by_va() except considers only sections defined in this header...
rose_addr_t get_rva() const
Returns the numeric RVA.
Definition: Rva.C:86
bool is_mapped() const
Whether section desires to be mapped to memory.
rose_addr_t get_base_va() const
Base virtual address for a section.
Base class for container file headers.
A relative virtual address optionally associated with a SgAsmSection.
Definition: Cxx_Grammar.h:7774
bool is_bound() const
Determines whether this RVA is associated with a file section.
Definition: Rva.C:78
rose_rva_t & set_rva(rose_addr_t rva)
Resets this RVA to a new value without unbinding from a section.
Definition: Rva.C:99
rose_addr_t get_base_va() const
Property: Base virtual address used by all relative virtual addresses.
void increment(rose_addr_t amount)
Increment the address by the specified amount, keeping it attached to the same (if any) section...
Definition: Rva.C:171
rose_rva_t & set_section(SgAsmGenericSection *section)
Changes the section binding.
Definition: Rva.C:119
rose_addr_t get_mapped_preferred_rva() const
Property: Relative virtual address where section prefers to be mapped.
virtual std::string get_string(bool escape=false) const
Property: String value.
SgAsmGenericString * get_name() const
Property: Non-unique name of section.
rose_addr_t get_rel() const
Returns an offset from the currently bound section.
Definition: Rva.C:156
SgAsmGenericSection * get_section() const
Returns the section with which this RVA is associated.
Definition: Rva.C:111
std::string to_string() const
Convert to a string representation.
Definition: Rva.C:179
rose_rva_t()
Create a zero RVA not linked to any section.
Definition: Rva.C:34
rose_rva_t operator=(const rose_rva_t &)
Assignment.
Definition: Rva.C:58