ROSE  0.9.9.139
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 
16 #include "sage3basic.h"
17 
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 
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());
71  rose_addr_t rva =section->get_mapped_preferred_rva() + offset;
72  return rose_rva_t(rva, section);
73 }
74 
76 bool
78 {
79  return section!=NULL;
80 }
81 
84 rose_addr_t
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 
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 
111 {
112  return section;
113 }
114 
117 rose_rva_t&
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&
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
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
156 {
157  return addr;
158 }
159 
161 rose_addr_t
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
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();}
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:67
rose_addr_t get_va() const
Return the absolute address if known.
Definition: Rva.C:144
rose_rva_t & bind(SgAsmGenericHeader *)
Binds this RVA to the best available section from the specified file header.
Definition: Rva.C:134
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:85
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:6819
bool is_bound() const
Determines whether this RVA is associated with a file section.
Definition: Rva.C:77
rose_rva_t & set_rva(rose_addr_t rva)
Resets this RVA to a new value without unbinding from a section.
Definition: Rva.C:98
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:170
rose_rva_t & set_section(SgAsmGenericSection *section)
Changes the section binding.
Definition: Rva.C:118
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:155
SgAsmGenericSection * get_section() const
Returns the section with which this RVA is associated.
Definition: Rva.C:110
std::string to_string() const
Convert to a string representation.
Definition: Rva.C:178
rose_rva_t()
Create a zero RVA not linked to any section.
Definition: Rva.C:33
rose_rva_t operator=(const rose_rva_t &)
Assignment.
Definition: Rva.C:57