Elf_X.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef __ELF_X_H__
00032 #define __ELF_X_H__
00033
00034 #include "libelf.h"
00035 #include <string>
00036 #include <map>
00037 #include <vector>
00038
00039 namespace Dyninst {
00040
00041
00042 class Elf_X;
00043 class Elf_X_Phdr;
00044 class Elf_X_Shdr;
00045 class Elf_X_Data;
00046 class Elf_X_Versym;
00047 class Elf_X_Verdaux;
00048 class Elf_X_Verdef;
00049 class Elf_X_Vernaux;
00050 class Elf_X_Verneed;
00051 class Elf_X_Options;
00052 class Elf_X_Sym;
00053 class Elf_X_Rel;
00054 class Elf_X_Rela;
00055 class Elf_X_RegInfo;
00056 class Elf_32_RegInfo;
00057 class Elf_64_RegInfo;
00058 class Elf_X_Dyn;
00059 class Elf_X_Nhdr;
00060
00061
00062
00063
00064
00065
00066
00067 class Elf_X {
00068 public:
00069 static Elf_X *newElf_X(int input, Elf_Cmd cmd, Elf_X *ref = NULL, std::string name = std::string());
00070 static Elf_X *newElf_X(char *mem_image, size_t mem_size, std::string name = std::string());
00071 void end();
00072
00073
00074 Elf *e_elfp() const;
00075 unsigned char *e_ident() const;
00076 unsigned short e_type() const;
00077 unsigned short e_machine() const;
00078 unsigned long e_version() const;
00079 unsigned long e_entry() const;
00080 unsigned long e_phoff() const;
00081 unsigned long e_shoff() const;
00082 unsigned long e_flags() const;
00083 unsigned short e_ehsize() const;
00084 unsigned short e_phentsize() const;
00085 unsigned short e_phnum() const;
00086 unsigned short e_shentsize() const;
00087 unsigned short e_shnum() const;
00088 unsigned short e_shstrndx() const;
00089 const char *e_rawfile(size_t &nbytes) const;
00090
00091 Elf_X *e_next(Elf_X *ref);
00092 Elf_X *e_rand(unsigned offset);
00093
00094
00095 void e_ident(unsigned char *input);
00096 void e_type(unsigned short input);
00097 void e_machine(unsigned short input);
00098 void e_version(unsigned long input);
00099 void e_entry(unsigned long input);
00100 void e_phoff(unsigned long input);
00101 void e_shoff(unsigned long input);
00102 void e_flags(unsigned long input);
00103 void e_ehsize(unsigned short input);
00104 void e_phentsize(unsigned short input);
00105 void e_phnum(unsigned short input);
00106 void e_shentsize(unsigned short input);
00107 void e_shnum(unsigned short input);
00108 void e_shstrndx(unsigned short input);
00109
00110
00111 bool isValid() const;
00112 int wordSize() const;
00113 Elf_X_Phdr &get_phdr(unsigned int i = 0);
00114 Elf_X_Shdr &get_shdr(unsigned int i);
00115
00116 bool findDebugFile(std::string origfilename, std::string &output_name, char* &output_buffer, unsigned long &output_buffer_size);
00117
00118 protected:
00119 Elf *elf;
00120 Elf32_Ehdr *ehdr32;
00121 Elf64_Ehdr *ehdr64;
00122 Elf32_Phdr *phdr32;
00123 Elf64_Phdr *phdr64;
00124 int filedes;
00125 bool is64;
00126 bool isArchive;
00127 std::vector<Elf_X_Shdr> shdrs;
00128 std::vector<Elf_X_Phdr> phdrs;
00129 unsigned int ref_count;
00130 std::string filename;
00131
00132 char *cached_debug_buffer;
00133 unsigned long cached_debug_size;
00134 std::string cached_debug_name;
00135 bool cached_debug;
00136
00137 Elf_X();
00138 Elf_X(int input, Elf_Cmd cmd, Elf_X *ref = NULL);
00139 Elf_X(char *mem_image, size_t mem_size);
00140 ~Elf_X();
00141
00142
00143
00144
00145
00146 static std::map<std::pair<std::string, int >, Elf_X *> elf_x_by_fd;
00147 static std::map<std::pair<std::string, char *>, Elf_X *> elf_x_by_ptr;
00148
00149 };
00150
00151
00152
00153 class Elf_X_Phdr {
00154 friend class Elf_X;
00155 public:
00156 Elf_X_Phdr();
00157 Elf_X_Phdr(bool is64_, void *input);
00158
00159
00160 unsigned long p_type() const;
00161 unsigned long p_offset() const;
00162 unsigned long p_vaddr() const;
00163 unsigned long p_paddr() const;
00164 unsigned long p_filesz() const;
00165 unsigned long p_memsz() const;
00166 unsigned long p_flags() const;
00167 unsigned long p_align() const;
00168
00169
00170 void p_type(unsigned long input);
00171 void p_offset(unsigned long input);
00172 void p_vaddr(unsigned long input);
00173 void p_paddr(unsigned long input);
00174 void p_filesz(unsigned long input);
00175 void p_memsz(unsigned long input);
00176 void p_flags(unsigned long input);
00177 void p_align(unsigned long input);
00178
00179 bool isValid() const;
00180
00181 private:
00182 Elf32_Phdr *phdr32;
00183 Elf64_Phdr *phdr64;
00184 bool is64;
00185 };
00186
00187
00188
00189 class Elf_X_Shdr {
00190 friend class Elf_X;
00191
00192 public:
00193 Elf_X_Shdr();
00194 Elf_X_Shdr(bool is64_, Elf_Scn *input);
00195
00196
00197 unsigned long sh_name() const;
00198 unsigned long sh_type() const;
00199 unsigned long sh_flags() const;
00200 unsigned long sh_addr() const;
00201 unsigned long sh_offset() const;
00202 unsigned long sh_size() const;
00203 unsigned long sh_link() const;
00204 unsigned long sh_info() const;
00205 unsigned long sh_addralign() const;
00206 unsigned long sh_entsize() const;
00207 bool isFromDebugFile() const;
00208
00209
00210 void sh_name(unsigned long input);
00211 void sh_type(unsigned long input);
00212 void sh_flags(unsigned long input);
00213 void sh_addr(unsigned long input);
00214 void sh_offset(unsigned long input);
00215 void sh_size(unsigned long input);
00216 void sh_link(unsigned long input);
00217 void sh_info(unsigned long input);
00218 void sh_addralign(unsigned long input);
00219 void sh_entsize(unsigned long input);
00220 void setDebugFile(bool b);
00221
00222
00223 Elf_X_Data get_data() const;
00224
00225
00226 void first_data();
00227 bool next_data();
00228
00229 bool isValid() const;
00230 unsigned wordSize() const;
00231 Elf_Scn *getScn() const;
00232
00233 Elf_X_Nhdr get_note() const;
00234
00235 protected:
00236 Elf_Scn *scn;
00237 Elf_Data *data;
00238 Elf32_Shdr *shdr32;
00239 Elf64_Shdr *shdr64;
00240 bool is64;
00241 bool fromDebugFile;
00242 const Elf_X *_elf;
00243 };
00244
00245
00246
00247 class Elf_X_Data {
00248 public:
00249 Elf_X_Data();
00250 Elf_X_Data(bool is64_, Elf_Data *input);
00251
00252
00253 void *d_buf() const;
00254 Elf_Type d_type() const;
00255 unsigned int d_version() const;
00256 size_t d_size() const;
00257 off_t d_off() const;
00258 size_t d_align() const;
00259
00260
00261 void d_buf(void *input);
00262 void d_type(Elf_Type input);
00263 void d_version(unsigned int input);
00264 void d_size(unsigned int input);
00265 void d_off(signed int input);
00266 void d_align(unsigned int input);
00267
00268
00269 const char *get_string() const;
00270 Elf_X_Dyn get_dyn();
00271 Elf_X_Versym get_versyms();
00272 Elf_X_Verneed *get_verNeedSym();
00273 Elf_X_Verdef *get_verDefSym();
00274
00275 Elf_X_Rel get_rel();
00276 Elf_X_Rela get_rela();
00277 Elf_X_Sym get_sym();
00278
00279 bool isValid() const;
00280
00281 protected:
00282 Elf_Data *data;
00283 bool is64;
00284 };
00285
00286
00287
00288 class Elf_X_Versym {
00289 public:
00290 Elf_X_Versym();
00291 Elf_X_Versym(bool is64_, Elf_Data *input);
00292
00293
00294 unsigned long get(int i) const;
00295
00296
00297 unsigned long count() const;
00298 bool isValid() const;
00299
00300 protected:
00301 Elf_Data *data;
00302 Elf32_Half *versym32;
00303 Elf64_Half *versym64;
00304 bool is64;
00305 };
00306
00307
00308
00309 class Elf_X_Verdaux {
00310 public:
00311 Elf_X_Verdaux();
00312 Elf_X_Verdaux(bool is64_, void *input);
00313
00314
00315 unsigned long vda_name() const;
00316 unsigned long vda_next() const;
00317 Elf_X_Verdaux *get_next() const;
00318
00319
00320 bool isValid() const;
00321
00322 protected:
00323 void *data;
00324 Elf32_Verdaux *verdaux32;
00325 Elf64_Verdaux *verdaux64;
00326 bool is64;
00327 };
00328
00329
00330
00331 class Elf_X_Verdef {
00332 public:
00333 Elf_X_Verdef();
00334 Elf_X_Verdef(bool is64_, void *input);
00335
00336
00337 unsigned long vd_version() const;
00338 unsigned long vd_flags() const;
00339 unsigned long vd_ndx() const;
00340 unsigned long vd_cnt() const;
00341 unsigned long vd_hash() const;
00342 unsigned long vd_aux() const;
00343 unsigned long vd_next() const;
00344 Elf_X_Verdaux *get_aux() const;
00345 Elf_X_Verdef *get_next() const;
00346
00347
00348 bool isValid() const;
00349
00350 protected:
00351 void *data;
00352 Elf32_Verdef *verdef32;
00353 Elf64_Verdef *verdef64;
00354 bool is64;
00355 };
00356
00357
00358
00359 class Elf_X_Vernaux {
00360 public:
00361 Elf_X_Vernaux();
00362 Elf_X_Vernaux(bool is64_, void *input);
00363
00364
00365 unsigned long vna_hash() const;
00366 unsigned long vna_flags() const;
00367 unsigned long vna_other() const;
00368 unsigned long vna_name() const;
00369 unsigned long vna_next() const;
00370 Elf_X_Vernaux *get_next() const;
00371
00372
00373 bool isValid() const;
00374
00375 protected:
00376 void *data;
00377 Elf32_Vernaux *vernaux32;
00378 Elf64_Vernaux *vernaux64;
00379 bool is64;
00380 };
00381
00382
00383
00384 class Elf_X_Verneed {
00385 public:
00386 Elf_X_Verneed();
00387 Elf_X_Verneed(bool is64_, void *input);
00388
00389
00390 unsigned long vn_version() const;
00391 unsigned long vn_cnt() const;
00392 unsigned long vn_file() const;
00393 unsigned long vn_aux() const;
00394 unsigned long vn_next() const;
00395 Elf_X_Vernaux *get_aux() const;
00396 Elf_X_Verneed *get_next() const;
00397
00398
00399 bool isValid() const;
00400
00401 protected:
00402 void *data;
00403 Elf32_Verneed *verneed32;
00404 Elf64_Verneed *verneed64;
00405 bool is64;
00406 };
00407
00408
00409
00410
00411 class Elf_X_Sym {
00412 public:
00413 Elf_X_Sym();
00414 Elf_X_Sym(bool is64_, Elf_Data *input);
00415
00416
00417 unsigned long st_name(int i) const;
00418 unsigned long st_value(int i) const;
00419 unsigned long st_size(int i) const;
00420 unsigned char st_info(int i) const;
00421 unsigned char st_other(int i) const;
00422 unsigned short st_shndx(int i) const;
00423 unsigned char ST_BIND(int i) const;
00424 unsigned char ST_TYPE(int i) const;
00425 unsigned char ST_VISIBILITY(int i) const;
00426 void *st_symptr(int i) const;
00427 unsigned st_entsize() const;
00428
00429
00430 void st_name(int i, unsigned long input);
00431 void st_value(int i, unsigned long input);
00432 void st_size(int i, unsigned long input);
00433 void st_info(int i, unsigned char input);
00434 void st_other(int i, unsigned char input);
00435 void st_shndx(int i, unsigned short input);
00436
00437
00438 unsigned long count() const;
00439 bool isValid() const;
00440
00441 protected:
00442 Elf_Data *data;
00443 Elf32_Sym *sym32;
00444 Elf64_Sym *sym64;
00445 bool is64;
00446 };
00447
00448
00449
00450 class Elf_X_Rel {
00451 public:
00452 Elf_X_Rel();
00453 Elf_X_Rel(bool is64_, Elf_Data *input);
00454
00455
00456 unsigned long r_offset(int i) const;
00457 unsigned long r_info(int i) const;
00458 unsigned long R_SYM(int i) const;
00459 unsigned long R_TYPE(int i) const;
00460
00461
00462 void r_offset(int i, unsigned long input);
00463 void r_info(int i, unsigned long input);
00464
00465
00466 unsigned long count() const;
00467 bool isValid() const;
00468
00469 protected:
00470 Elf_Data *data;
00471 Elf32_Rel *rel32;
00472 Elf64_Rel *rel64;
00473 bool is64;
00474 };
00475
00476
00477
00478 class Elf_X_Rela {
00479 public:
00480 Elf_X_Rela();
00481 Elf_X_Rela(bool is64_, Elf_Data *input);
00482
00483
00484 unsigned long r_offset(int i) const;
00485 unsigned long r_info(int i) const;
00486 signed long r_addend(int i) const;
00487 unsigned long R_SYM(int i) const;
00488 unsigned long R_TYPE(int i) const;
00489
00490
00491 void r_offset(int i, unsigned long input);
00492 void r_info(int i, unsigned long input);
00493 void r_addend(int i, signed long input);
00494
00495
00496 unsigned long count() const;
00497 bool isValid() const;
00498
00499 protected:
00500 Elf_Data *data;
00501 Elf32_Rela *rela32;
00502 Elf64_Rela *rela64;
00503 bool is64;
00504 };
00505
00506
00507
00508 class Elf_X_Dyn {
00509 public:
00510 Elf_X_Dyn();
00511 Elf_X_Dyn(bool is64_, Elf_Data *input);
00512
00513
00514 signed long d_tag(int i) const;
00515 unsigned long d_val(int i) const;
00516 unsigned long d_ptr(int i) const;
00517
00518
00519 void d_tag(int i, signed long input);
00520 void d_val(int i, unsigned long input);
00521 void d_ptr(int i, unsigned long input);
00522
00523
00524 unsigned long count() const;
00525 bool isValid() const;
00526
00527 protected:
00528 Elf_Data *data;
00529 Elf32_Dyn *dyn32;
00530 Elf64_Dyn *dyn64;
00531 bool is64;
00532 };
00533
00534
00535
00536 class Elf_X_Nhdr {
00537 friend class Elf_X;
00538
00539 public:
00540 Elf_X_Nhdr();
00541 Elf_X_Nhdr(Elf_Data *data_, size_t offset);
00542
00543
00544 unsigned long n_namesz() const;
00545 unsigned long n_descsz() const;
00546 unsigned long n_type() const;
00547
00548
00549 bool isValid() const;
00550
00551 const char* get_name() const;
00552 const void* get_desc() const;
00553
00554 Elf_X_Nhdr next() const;
00555
00556 protected:
00557 Elf_Data *data;
00558 Elf32_Nhdr *nhdr;
00559 };
00560
00561 }
00562
00563 #endif