ROSE  0.9.9.139
BinaryLoaderElf.h
1 #ifndef ROSE_BinaryAnalysis_BinaryLoaderElf_H
2 #define ROSE_BinaryAnalysis_BinaryLoaderElf_H
3 
4 #include "BinaryLoader.h"
5 
6 namespace Rose {
7 namespace BinaryAnalysis {
8 
10 public:
11  BinaryLoaderElf() {}
12 
13  BinaryLoaderElf(const BinaryLoaderElf &other)
14  : BinaryLoader(other)
15  {}
16 
17  virtual ~BinaryLoaderElf() {}
18 
20  virtual BinaryLoaderElf *clone() const ROSE_OVERRIDE {
21  return new BinaryLoaderElf(*this);
22  }
23 
25  virtual bool can_load(SgAsmGenericHeader*) const ROSE_OVERRIDE;
26 
42  void add_lib_defaults(SgAsmGenericHeader *header=NULL);
43 
45  static void get_dynamic_vars(SgAsmGenericHeader*, std::string &rpath/*out*/, std::string &runpath/*out*/);
46 
47  // documented in superclass
48  virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL) ROSE_OVERRIDE;
49 
50  /* FIXME: These should probably be in SgAsmElfSymver* classes instead. [RPM 2010-09-14] */
52  enum {
53  VER_FLG_BASE=0x1,
54  VER_FLG_WEAK=0x2,
55  VERSYM_HIDDEN=0x8000
56  };
57 
66  private:
67  SgAsmElfSymbol* p_symbol;
68  SgAsmElfSymverEntry* p_version_entry;
69  SgAsmElfSymverDefinedEntry* p_version_def;
70  SgAsmElfSymverNeededAux* p_version_need;
71  public:
72  explicit VersionedSymbol(SgAsmElfSymbol* symbol)
73  : p_symbol(symbol), p_version_entry(NULL), p_version_def(NULL), p_version_need(NULL)
74  {}
75 
77  bool is_local() const;
78 
80  bool is_hidden() const;
81 
83  bool is_reference() const;
84 
87  bool is_base_definition() const;
88 
90  void set_symbol(SgAsmElfSymbol *symbol) {
91  p_symbol = symbol;
92  }
93 
96  return p_symbol;
97  }
98 
101  SgAsmElfSymbolSection *retval = SageInterface::getEnclosingNode<SgAsmElfSymbolSection>(p_symbol);
102  ROSE_ASSERT(retval!=NULL);
103  return retval;
104  }
105 
107  std::string get_version() const;
108 
110  std::string get_name() const {
111  return p_symbol->get_name()->get_string();
112  }
113 
115  std::string get_versioned_name() const;
116 
119  p_version_entry = entry;
120  }
121 
124  ROSE_ASSERT(def->get_flags() == 0 || def->get_flags() == VER_FLG_BASE);
125  p_version_def = def;
126  }
127 
130  ROSE_ASSERT(need->get_flags() == 0 || need->get_flags() == VER_FLG_WEAK);
131  p_version_need = need;
132  }
133 
136  return p_version_need;
137  }
138 
141  return p_version_def;
142  }
143 
145  void print(std::ostream&) const;
146 
148  void dump(FILE*, const char *prefix, ssize_t idx) const;
149  };
150 
153  struct SymbolMapEntry {
154  private:
155  /* Base version will be at the front if we have one; other entries are unsorted. */
156  std::vector<VersionedSymbol> p_versions;
157  public:
159  const VersionedSymbol &get_vsymbol() const {
160  return get_base_version();
161  }
162 
165  return get_vsymbol().get_symbol();
166  }
167 
171  VersionedSymbol get_vsymbol(const VersionedSymbol &version) const;
172 
175  return get_vsymbol().get_section();
176  }
177 
180  void addVersion(const VersionedSymbol &vsymbol);
181 
183  void merge(const SymbolMapEntry&);
184 
186  void dump(FILE*, const char *prefix) const ;
187 
188  private:
189  const VersionedSymbol& get_base_version() const {
190  ROSE_ASSERT(!p_versions.empty());
191  return p_versions.front();
192  }
193  };
194 
196  class SymbolMap: public std::map<std::string/*symbol name*/, SymbolMapEntry> {
197  public:
199  const SymbolMapEntry *lookup(std::string name) const;
200 
204  const SymbolMapEntry *lookup(std::string name, std::string version) const;
205 
207  void dump(FILE*, const char *prefix) const;
208  };
209 
211  public:
213  ctor(header);
214  }
215 
219 
221  void dump(FILE*, const char *prefix, ssize_t idx) const;
222 
223  private:
225  void ctor(SgAsmGenericHeader*);
226 
229  void makeSymbolVersionDefMap(SgAsmElfSymverDefinedSection*);
230 
234  void makeSymbolVersionNeedMap(SgAsmElfSymverNeededSection*);
235 
239  void makeVersionedSymbolMap(SgAsmElfSymbolSection*, SgAsmElfSymverSection*);
240 
241  typedef std::map<SgAsmElfSymbol*, VersionedSymbol*> VersionedSymbolMap;
242  typedef std::map<uint16_t, SgAsmElfSymverDefinedEntry*> SymbolVersionDefinitionMap;
243  typedef std::map<uint16_t, SgAsmElfSymverNeededAux*> SymbolVersionNeededMap;
244 
246  SymbolVersionDefinitionMap p_symbolVersionDefMap;
247 
252  SymbolVersionNeededMap p_symbolVersionNeedMap;
253 
255  VersionedSymbolMap p_versionedSymbolMap;
256  };
257 
262 
263 protected:
267  virtual SgAsmGenericSectionPtrList get_remap_sections(SgAsmGenericHeader*) ROSE_OVERRIDE;
268 
269 public:
271  virtual rose_addr_t rebase(const MemoryMap::Ptr&, SgAsmGenericHeader*, const SgAsmGenericSectionPtrList&) ROSE_OVERRIDE;
272 
273 protected:
276  rose_addr_t *malign_lo, rose_addr_t *malign_hi,
277  rose_addr_t *va, rose_addr_t *mem_size,
278  rose_addr_t *offset, rose_addr_t *file_size, bool *map_private,
279  rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi,
280  ConflictResolution *resolve) ROSE_OVERRIDE;
281 
286 
287  /*========================================================================================================================
288  * Methods returning prerequisite information for fixups. These names all begin with "fixup_info_".
289  *======================================================================================================================== */
290 protected:
291 
301 
311  rose_addr_t fixup_info_target_va(SgAsmElfRelocEntry*, SgAsmGenericSection **section_p=NULL, rose_addr_t *adj_p=NULL);
312 
322  rose_addr_t fixup_info_symbol_va(SgAsmElfSymbol*, SgAsmGenericSection **section_p=NULL, rose_addr_t *adj_p=NULL);
323 
333  rose_addr_t fixup_info_addend(SgAsmElfRelocEntry*, rose_addr_t target_va, const MemoryMap::Ptr&, size_t nbytes=0);
334 
367  rose_addr_t fixup_info_expr(const std::string &expression, SgAsmElfRelocEntry *reloc, const SymverResolver &resolver,
368  const MemoryMap::Ptr &memmap, rose_addr_t *target_va_p=NULL);
369 
370 
371 
372  /*========================================================================================================================
373  * Methods that apply a relocation fixup. These names all begin with "fixup_apply_".
374  *======================================================================================================================== */
375 protected:
386  void fixup_apply(rose_addr_t value, SgAsmElfRelocEntry*, const MemoryMap::Ptr&, rose_addr_t target_va=0, size_t nbytes=0);
387 
390  void fixup_apply_symbol_copy(SgAsmElfRelocEntry*, const SymverResolver&, const MemoryMap::Ptr&);
391 
392  /*========================================================================================================================
393  * Functions moved here from the BinaryLoader_ElfSupport name space.
394  *======================================================================================================================== */
395 protected:
396  void performRelocation(SgAsmElfRelocEntry*, const SymverResolver&, const MemoryMap::Ptr&);
397  void performRelocations(SgAsmElfFileHeader*, const MemoryMap::Ptr&);
398 
399  /*========================================================================================================================
400  * Data members
401  *======================================================================================================================== */
402 protected:
403 
407 };
408 
409 std::ostream& operator<<(std::ostream&, const BinaryLoaderElf::VersionedSymbol&);
410 
411 } // namespace
412 } // namespace
413 
414 #endif /*ROSE_BINARYLOADERELF_H*/
void build_master_symbol_table(SgAsmInterpretation *)
Builds the master symbol table.
The GNU symbol version definitions.
Contiguous region of a file.
void set_version_need(SgAsmElfSymverNeededAux *need)
Set the version requirement of this symbol.
rose_addr_t fixup_info_expr(const std::string &expression, SgAsmElfRelocEntry *reloc, const SymverResolver &resolver, const MemoryMap::Ptr &memmap, rose_addr_t *target_va_p=NULL)
Evaluates a simple postfix expression and returns the result.
void fixup_apply(rose_addr_t value, SgAsmElfRelocEntry *, const MemoryMap::Ptr &, rose_addr_t target_va=0, size_t nbytes=0)
Writes a value into memory at the relocation target.
virtual SgAsmGenericSection * find_section_by_preferred_va(SgAsmGenericHeader *, rose_addr_t va)
Find the section containing the specified virtual address.
void dump(FILE *, const char *prefix) const
Print debugging information about this SymbolMap.
rose_addr_t fixup_info_target_va(SgAsmElfRelocEntry *, SgAsmGenericSection **section_p=NULL, rose_addr_t *adj_p=NULL)
Returns the virtual address where a relocation should be supplied.
Auxiliary info for needed symbol version.
One entry of an ELF relocation table.
Represents the file header of an ELF binary container.
std::string get_versioned_name() const
Returns the full, versionioned name of this symbol.
SgAsmElfSymbolSection * get_section() const
Returns the symbol section (.dynsym) where this symbol was defined.
std::string get_name() const
Returns the name of this symbol.
rose_addr_t fixup_info_addend(SgAsmElfRelocEntry *, rose_addr_t target_va, const MemoryMap::Ptr &, size_t nbytes=0)
Returns the addend associated with a relocation.
void set_symbol(SgAsmElfSymbol *symbol)
(Re)initializes this symbol.
void merge(const SymbolMapEntry &)
Merge the versions from the specified entry into this entry.
void fixup_apply_symbol_copy(SgAsmElfRelocEntry *, const SymverResolver &, const MemoryMap::Ptr &)
Copies symbol memory to the relocation target.
static void get_dynamic_vars(SgAsmGenericHeader *, std::string &rpath, std::string &runpath)
Returns the strings associated with certain variables in the ".dynamic" section.
void set_version_entry(SgAsmElfSymverEntry *entry)
Set the version pointer for this symbol.
STL namespace.
virtual SgAsmGenericSectionPtrList get_remap_sections(SgAsmGenericHeader *) ROSE_OVERRIDE
Returns mappable sections in a particular order.
SgAsmElfSymverDefinedEntry * get_version_def() const
Get the version definition of this symbol.
void add_lib_defaults(SgAsmGenericHeader *header=NULL)
Sets up library search paths and preloads from the environment.
Main namespace for the ROSE library.
Entry in an ELF symbol version table.
virtual BinaryLoaderElf * clone() const ROSE_OVERRIDE
Copy constructor.
int get_flags() const
Property: Flags.
GNU symbol version requirements table.
rose_addr_t fixup_info_symbol_va(SgAsmElfSymbol *, SgAsmGenericSection **section_p=NULL, rose_addr_t *adj_p=NULL)
Returns the virtual address of a symbol adjusted for remapping.
virtual bool can_load(SgAsmGenericHeader *) const ROSE_OVERRIDE
Capability query.
SgAsmElfSymverNeededAux * get_version_need() const
Get the version requirement of this symbol.
virtual rose_addr_t rebase(const MemoryMap::Ptr &, SgAsmGenericHeader *, const SgAsmGenericSectionPtrList &) ROSE_OVERRIDE
Returns a new, temporary base address which is greater than everything that's been mapped already...
bool is_base_definition() const
Returns tru if this symbol is a base definition.
A mapping from symbol name (with optional version in parentheses) to SymbolMapEntry.
Base class for container file headers.
const SymbolMapEntry * lookup(std::string name) const
Finds and returns the entry having the specified name.
ELF file section containing symbols.
bool is_reference() const
Returns true if this symbol is a reference to an object rather than the definition of the object...
bool is_hidden() const
Returns true if this symbol is hidden.
SgAsmElfSymbol * get_symbol() const
Returns the ELF symbol from the base version.
An efficient mapping from an address space to stored data.
Definition: MemoryMap.h:96
One entry from an ELF symbol version definition table.
Represents a single ELF symbol.
SgAsmElfSymbol * fixup_info_reloc_symbol(SgAsmElfRelocEntry *, const SymverResolver &)
Returns the defining symbol for a relocation, if any.
VersionedSymbol get_versioned_symbol(SgAsmElfSymbol *symbol) const
Returns the VersionedSymbol corresponding to the specified symbol.
MappingContribution
Describes how a section contributes to the overall memory map.
Definition: BinaryLoader.h:55
bool is_local() const
Returns true if this symbol is visible only locally.
virtual void fixup(SgAsmInterpretation *interp, FixupErrors *errors=NULL) ROSE_OVERRIDE
Performs relocation fixups on the specified interpretation.
int get_flags() const
Property: Flags.
std::string get_version() const
Returns the version string of this symbol.
void addVersion(const VersionedSymbol &vsymbol)
Add an additional versioned symbol to this entry.
void dump(FILE *, const char *prefix, ssize_t idx) const
Dump info like for SgAsm* objects.
ConflictResolution
Describes how conflicts are resolved when mapping a section.
Definition: BinaryLoader.h:63
Base class for loading a static or dynamic object.
Definition: BinaryLoader.h:48
SgAsmElfSymbolSection * get_section() const
Returns the section where the base version symbol was defined.
SgAsmElfSymbol * get_symbol() const
Returns the symbol part of this versioned symbol.
virtual MappingContribution align_values(SgAsmGenericSection *, const MemoryMap::Ptr &, rose_addr_t *malign_lo, rose_addr_t *malign_hi, rose_addr_t *va, rose_addr_t *mem_size, rose_addr_t *offset, rose_addr_t *file_size, bool *map_private, rose_addr_t *va_offset, bool *anon_lo, bool *anon_hi, ConflictResolution *resolve) ROSE_OVERRIDE
Linux-specific ELF Segment and Section alignment.
SymbolMap p_symbols
Symbol table for an entire interpretation.
const VersionedSymbol & get_vsymbol() const
Returns the base version.
void set_version_def(SgAsmElfSymverDefinedEntry *def)
Set the version definition of this symbol.
Symbol from .dynsym combined with additional information.
The ELF symbol version table.
Represents an interpretation of a binary container.
void dump(FILE *, const char *prefix) const
Print info about this symbol map entry.
void print(std::ostream &) const
Print used by operator<<.
void dump(FILE *, const char *prefix, ssize_t idx) const
Print some info about the resolver.