build/unix/elfhack/elfxx.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/NullPtr.h"
michael@0 6
michael@0 7 #include <stdexcept>
michael@0 8 #include <list>
michael@0 9 #include <vector>
michael@0 10 #include <cstring>
michael@0 11 #include <iostream>
michael@0 12 #include <fstream>
michael@0 13 #include <algorithm>
michael@0 14 #include <elf.h>
michael@0 15 #include <asm/byteorder.h>
michael@0 16
michael@0 17 // Technically, __*_to_cpu and __cpu_to* function are equivalent,
michael@0 18 // so swap can use either of both.
michael@0 19 #define def_swap(endian, type, bits) \
michael@0 20 static inline type ## bits ## _t swap(type ## bits ## _t i) { \
michael@0 21 return __ ## endian ## bits ## _to_cpu(i); \
michael@0 22 }
michael@0 23
michael@0 24 class little_endian {
michael@0 25 public:
michael@0 26 def_swap(le, uint, 16);
michael@0 27 def_swap(le, uint, 32);
michael@0 28 def_swap(le, uint, 64);
michael@0 29 def_swap(le, int, 16);
michael@0 30 def_swap(le, int, 32);
michael@0 31 def_swap(le, int, 64);
michael@0 32 };
michael@0 33
michael@0 34 class big_endian {
michael@0 35 public:
michael@0 36 def_swap(be, uint, 16);
michael@0 37 def_swap(be, uint, 32);
michael@0 38 def_swap(be, uint, 64);
michael@0 39 def_swap(be, int, 16);
michael@0 40 def_swap(be, int, 32);
michael@0 41 def_swap(be, int, 64);
michael@0 42 };
michael@0 43
michael@0 44 // forward declaration
michael@0 45 class ElfSection;
michael@0 46 class ElfSegment;
michael@0 47 // TODO: Rename Elf_* types
michael@0 48 class Elf_Ehdr;
michael@0 49 class Elf_Phdr;
michael@0 50 class Elf;
michael@0 51 class ElfDynamic_Section;
michael@0 52 class ElfStrtab_Section;
michael@0 53
michael@0 54 class Elf_Ehdr_Traits {
michael@0 55 public:
michael@0 56 typedef Elf32_Ehdr Type32;
michael@0 57 typedef Elf64_Ehdr Type64;
michael@0 58
michael@0 59 template <class endian, typename R, typename T>
michael@0 60 static void swap(T &t, R &r);
michael@0 61 };
michael@0 62
michael@0 63 class Elf_Phdr_Traits {
michael@0 64 public:
michael@0 65 typedef Elf32_Phdr Type32;
michael@0 66 typedef Elf64_Phdr Type64;
michael@0 67
michael@0 68 template <class endian, typename R, typename T>
michael@0 69 static void swap(T &t, R &r);
michael@0 70 };
michael@0 71
michael@0 72 class Elf_Shdr_Traits {
michael@0 73 public:
michael@0 74 typedef Elf32_Shdr Type32;
michael@0 75 typedef Elf64_Shdr Type64;
michael@0 76
michael@0 77 template <class endian, typename R, typename T>
michael@0 78 static void swap(T &t, R &r);
michael@0 79 };
michael@0 80
michael@0 81 class Elf_Dyn_Traits {
michael@0 82 public:
michael@0 83 typedef Elf32_Dyn Type32;
michael@0 84 typedef Elf64_Dyn Type64;
michael@0 85
michael@0 86 template <class endian, typename R, typename T>
michael@0 87 static void swap(T &t, R &r);
michael@0 88 };
michael@0 89
michael@0 90 class Elf_Sym_Traits {
michael@0 91 public:
michael@0 92 typedef Elf32_Sym Type32;
michael@0 93 typedef Elf64_Sym Type64;
michael@0 94
michael@0 95 template <class endian, typename R, typename T>
michael@0 96 static void swap(T &t, R &r);
michael@0 97 };
michael@0 98
michael@0 99 class Elf_Rel_Traits {
michael@0 100 public:
michael@0 101 typedef Elf32_Rel Type32;
michael@0 102 typedef Elf64_Rel Type64;
michael@0 103
michael@0 104 template <class endian, typename R, typename T>
michael@0 105 static void swap(T &t, R &r);
michael@0 106 };
michael@0 107
michael@0 108 class Elf_Rela_Traits {
michael@0 109 public:
michael@0 110 typedef Elf32_Rela Type32;
michael@0 111 typedef Elf64_Rela Type64;
michael@0 112
michael@0 113 template <class endian, typename R, typename T>
michael@0 114 static void swap(T &t, R &r);
michael@0 115 };
michael@0 116
michael@0 117 class ElfValue {
michael@0 118 public:
michael@0 119 virtual unsigned int getValue() { return 0; }
michael@0 120 virtual ElfSection *getSection() { return nullptr; }
michael@0 121 };
michael@0 122
michael@0 123 class ElfPlainValue: public ElfValue {
michael@0 124 unsigned int value;
michael@0 125 public:
michael@0 126 ElfPlainValue(unsigned int val): value(val) {};
michael@0 127 unsigned int getValue() { return value; }
michael@0 128 };
michael@0 129
michael@0 130 class ElfLocation: public ElfValue {
michael@0 131 ElfSection *section;
michael@0 132 unsigned int offset;
michael@0 133 public:
michael@0 134 enum position { ABSOLUTE, RELATIVE };
michael@0 135 ElfLocation(): section(nullptr), offset(0) {};
michael@0 136 ElfLocation(ElfSection *section, unsigned int off, enum position pos = RELATIVE);
michael@0 137 ElfLocation(unsigned int location, Elf *elf);
michael@0 138 unsigned int getValue();
michael@0 139 ElfSection *getSection() { return section; }
michael@0 140 const char *getBuffer();
michael@0 141 };
michael@0 142
michael@0 143 class ElfSize: public ElfValue {
michael@0 144 ElfSection *section;
michael@0 145 public:
michael@0 146 ElfSize(ElfSection *s): section(s) {};
michael@0 147 unsigned int getValue();
michael@0 148 ElfSection *getSection() { return section; }
michael@0 149 };
michael@0 150
michael@0 151 class ElfEntSize: public ElfValue {
michael@0 152 ElfSection *section;
michael@0 153 public:
michael@0 154 ElfEntSize(ElfSection *s): section(s) {};
michael@0 155 unsigned int getValue();
michael@0 156 ElfSection *getSection() { return section; }
michael@0 157 };
michael@0 158
michael@0 159 template <typename T>
michael@0 160 class serializable: public T::Type32 {
michael@0 161 public:
michael@0 162 serializable() {};
michael@0 163 serializable(const typename T::Type32 &p): T::Type32(p) {};
michael@0 164
michael@0 165 private:
michael@0 166 template <typename R>
michael@0 167 void init(const char *buf, size_t len, char ei_data)
michael@0 168 {
michael@0 169 R e;
michael@0 170 assert(len >= sizeof(e));
michael@0 171 memcpy(&e, buf, sizeof(e));
michael@0 172 if (ei_data == ELFDATA2LSB) {
michael@0 173 T::template swap<little_endian>(e, *this);
michael@0 174 return;
michael@0 175 } else if (ei_data == ELFDATA2MSB) {
michael@0 176 T::template swap<big_endian>(e, *this);
michael@0 177 return;
michael@0 178 }
michael@0 179 throw std::runtime_error("Unsupported ELF data encoding");
michael@0 180 }
michael@0 181
michael@0 182 template <typename R>
michael@0 183 void serialize(const char *buf, size_t len, char ei_data)
michael@0 184 {
michael@0 185 assert(len >= sizeof(R));
michael@0 186 if (ei_data == ELFDATA2LSB) {
michael@0 187 T::template swap<little_endian>(*this, *(R *)buf);
michael@0 188 return;
michael@0 189 } else if (ei_data == ELFDATA2MSB) {
michael@0 190 T::template swap<big_endian>(*this, *(R *)buf);
michael@0 191 return;
michael@0 192 }
michael@0 193 throw std::runtime_error("Unsupported ELF data encoding");
michael@0 194 }
michael@0 195
michael@0 196 public:
michael@0 197 serializable(const char *buf, size_t len, char ei_class, char ei_data)
michael@0 198 {
michael@0 199 if (ei_class == ELFCLASS32) {
michael@0 200 init<typename T::Type32>(buf, len, ei_data);
michael@0 201 return;
michael@0 202 } else if (ei_class == ELFCLASS64) {
michael@0 203 init<typename T::Type64>(buf, len, ei_data);
michael@0 204 return;
michael@0 205 }
michael@0 206 throw std::runtime_error("Unsupported ELF class");
michael@0 207 }
michael@0 208
michael@0 209 serializable(std::ifstream &file, char ei_class, char ei_data)
michael@0 210 {
michael@0 211 if (ei_class == ELFCLASS32) {
michael@0 212 typename T::Type32 e;
michael@0 213 file.read((char *)&e, sizeof(e));
michael@0 214 init<typename T::Type32>((char *)&e, sizeof(e), ei_data);
michael@0 215 return;
michael@0 216 } else if (ei_class == ELFCLASS64) {
michael@0 217 typename T::Type64 e;
michael@0 218 file.read((char *)&e, sizeof(e));
michael@0 219 init<typename T::Type64>((char *)&e, sizeof(e), ei_data);
michael@0 220 return;
michael@0 221 }
michael@0 222 throw std::runtime_error("Unsupported ELF class or data encoding");
michael@0 223 }
michael@0 224
michael@0 225 void serialize(std::ofstream &file, char ei_class, char ei_data)
michael@0 226 {
michael@0 227 if (ei_class == ELFCLASS32) {
michael@0 228 typename T::Type32 e;
michael@0 229 serialize<typename T::Type32>((char *)&e, sizeof(e), ei_data);
michael@0 230 file.write((char *)&e, sizeof(e));
michael@0 231 return;
michael@0 232 } else if (ei_class == ELFCLASS64) {
michael@0 233 typename T::Type64 e;
michael@0 234 serialize<typename T::Type64>((char *)&e, sizeof(e), ei_data);
michael@0 235 file.write((char *)&e, sizeof(e));
michael@0 236 return;
michael@0 237 }
michael@0 238 throw std::runtime_error("Unsupported ELF class or data encoding");
michael@0 239 }
michael@0 240
michael@0 241 void serialize(char *buf, size_t len, char ei_class, char ei_data)
michael@0 242 {
michael@0 243 if (ei_class == ELFCLASS32) {
michael@0 244 serialize<typename T::Type32>(buf, len, ei_data);
michael@0 245 return;
michael@0 246 } else if (ei_class == ELFCLASS64) {
michael@0 247 serialize<typename T::Type64>(buf, len, ei_data);
michael@0 248 return;
michael@0 249 }
michael@0 250 throw std::runtime_error("Unsupported ELF class");
michael@0 251 }
michael@0 252
michael@0 253 static inline unsigned int size(char ei_class)
michael@0 254 {
michael@0 255 if (ei_class == ELFCLASS32)
michael@0 256 return sizeof(typename T::Type32);
michael@0 257 else if (ei_class == ELFCLASS64)
michael@0 258 return sizeof(typename T::Type64);
michael@0 259 return 0;
michael@0 260 }
michael@0 261 };
michael@0 262
michael@0 263 typedef serializable<Elf_Shdr_Traits> Elf_Shdr;
michael@0 264
michael@0 265 class Elf {
michael@0 266 public:
michael@0 267 Elf(std::ifstream &file);
michael@0 268 ~Elf();
michael@0 269
michael@0 270 /* index == -1 is treated as index == ehdr.e_shstrndx */
michael@0 271 ElfSection *getSection(int index);
michael@0 272
michael@0 273 ElfSection *getSectionAt(unsigned int offset);
michael@0 274
michael@0 275 ElfSegment *getSegmentByType(unsigned int type, ElfSegment *last = nullptr);
michael@0 276
michael@0 277 ElfDynamic_Section *getDynSection();
michael@0 278
michael@0 279 void normalize();
michael@0 280 void write(std::ofstream &file);
michael@0 281
michael@0 282 char getClass();
michael@0 283 char getData();
michael@0 284 char getType();
michael@0 285 char getMachine();
michael@0 286 unsigned int getSize();
michael@0 287
michael@0 288 void insertSegmentAfter(ElfSegment *previous, ElfSegment *segment) {
michael@0 289 std::vector<ElfSegment *>::iterator prev = std::find(segments.begin(), segments.end(), previous);
michael@0 290 segments.insert(prev + 1, segment);
michael@0 291 }
michael@0 292
michael@0 293 void removeSegment(ElfSegment *segment);
michael@0 294
michael@0 295 private:
michael@0 296 Elf_Ehdr *ehdr;
michael@0 297 ElfLocation eh_entry;
michael@0 298 ElfStrtab_Section *eh_shstrndx;
michael@0 299 ElfSection **sections;
michael@0 300 std::vector<ElfSegment *> segments;
michael@0 301 ElfSection *shdr_section, *phdr_section;
michael@0 302 /* Values used only during initialization */
michael@0 303 Elf_Shdr **tmp_shdr;
michael@0 304 std::ifstream *tmp_file;
michael@0 305 };
michael@0 306
michael@0 307 class ElfSection {
michael@0 308 public:
michael@0 309 typedef union {
michael@0 310 ElfSection *section;
michael@0 311 int index;
michael@0 312 } SectionInfo;
michael@0 313
michael@0 314 ElfSection(Elf_Shdr &s, std::ifstream *file, Elf *parent);
michael@0 315
michael@0 316 virtual ~ElfSection() {
michael@0 317 delete[] data;
michael@0 318 }
michael@0 319
michael@0 320 const char *getName() { return name; }
michael@0 321 unsigned int getType() { return shdr.sh_type; }
michael@0 322 unsigned int getFlags() { return shdr.sh_flags; }
michael@0 323 unsigned int getAddr();
michael@0 324 unsigned int getSize() { return shdr.sh_size; }
michael@0 325 unsigned int getAddrAlign() { return shdr.sh_addralign; }
michael@0 326 unsigned int getEntSize() { return shdr.sh_entsize; }
michael@0 327 const char *getData() { return data; }
michael@0 328 ElfSection *getLink() { return link; }
michael@0 329 SectionInfo getInfo() { return info; }
michael@0 330
michael@0 331 void shrink(unsigned int newsize) {
michael@0 332 if (newsize < shdr.sh_size)
michael@0 333 shdr.sh_size = newsize;
michael@0 334 }
michael@0 335
michael@0 336 unsigned int getOffset();
michael@0 337 int getIndex();
michael@0 338 Elf_Shdr &getShdr();
michael@0 339
michael@0 340 ElfSection *getNext() { return next; }
michael@0 341 ElfSection *getPrevious() { return previous; }
michael@0 342
michael@0 343 virtual bool isRelocatable() {
michael@0 344 return ((getType() == SHT_SYMTAB) ||
michael@0 345 (getType() == SHT_STRTAB) ||
michael@0 346 (getType() == SHT_RELA) ||
michael@0 347 (getType() == SHT_HASH) ||
michael@0 348 (getType() == SHT_NOTE) ||
michael@0 349 (getType() == SHT_REL) ||
michael@0 350 (getType() == SHT_DYNSYM) ||
michael@0 351 (getType() == SHT_GNU_HASH) ||
michael@0 352 (getType() == SHT_GNU_verdef) ||
michael@0 353 (getType() == SHT_GNU_verneed) ||
michael@0 354 (getType() == SHT_GNU_versym) ||
michael@0 355 getSegmentByType(PT_INTERP)) &&
michael@0 356 (getFlags() & SHF_ALLOC);
michael@0 357 }
michael@0 358
michael@0 359 void insertAfter(ElfSection *section, bool dirty = true) {
michael@0 360 if (previous != nullptr)
michael@0 361 previous->next = next;
michael@0 362 if (next != nullptr)
michael@0 363 next->previous = previous;
michael@0 364 previous = section;
michael@0 365 if (section != nullptr) {
michael@0 366 next = section->next;
michael@0 367 section->next = this;
michael@0 368 } else
michael@0 369 next = nullptr;
michael@0 370 if (next != nullptr)
michael@0 371 next->previous = this;
michael@0 372 if (dirty)
michael@0 373 markDirty();
michael@0 374 insertInSegments(section->segments);
michael@0 375 }
michael@0 376
michael@0 377 void insertBefore(ElfSection *section, bool dirty = true) {
michael@0 378 if (previous != nullptr)
michael@0 379 previous->next = next;
michael@0 380 if (next != nullptr)
michael@0 381 next->previous = previous;
michael@0 382 next = section;
michael@0 383 if (section != nullptr) {
michael@0 384 previous = section->previous;
michael@0 385 section->previous = this;
michael@0 386 } else
michael@0 387 previous = nullptr;
michael@0 388 if (previous != nullptr)
michael@0 389 previous->next = this;
michael@0 390 if (dirty)
michael@0 391 markDirty();
michael@0 392 insertInSegments(section->segments);
michael@0 393 }
michael@0 394
michael@0 395 void markDirty() {
michael@0 396 if (link != nullptr)
michael@0 397 shdr.sh_link = -1;
michael@0 398 if (info.index)
michael@0 399 shdr.sh_info = -1;
michael@0 400 shdr.sh_offset = -1;
michael@0 401 if (isRelocatable())
michael@0 402 shdr.sh_addr = -1;
michael@0 403 if (next)
michael@0 404 next->markDirty();
michael@0 405 }
michael@0 406
michael@0 407 virtual void serialize(std::ofstream &file, char ei_class, char ei_data)
michael@0 408 {
michael@0 409 if (getType() == SHT_NOBITS)
michael@0 410 return;
michael@0 411 file.seekp(getOffset());
michael@0 412 file.write(data, getSize());
michael@0 413 }
michael@0 414
michael@0 415 private:
michael@0 416 friend class ElfSegment;
michael@0 417
michael@0 418 void addToSegment(ElfSegment *segment) {
michael@0 419 segments.push_back(segment);
michael@0 420 }
michael@0 421
michael@0 422 void removeFromSegment(ElfSegment *segment) {
michael@0 423 std::vector<ElfSegment *>::iterator i = std::find(segments.begin(), segments.end(), segment);
michael@0 424 segments.erase(i, i + 1);
michael@0 425 }
michael@0 426
michael@0 427 ElfSegment *getSegmentByType(unsigned int type);
michael@0 428
michael@0 429 void insertInSegments(std::vector<ElfSegment *> &segs);
michael@0 430
michael@0 431 protected:
michael@0 432 Elf_Shdr shdr;
michael@0 433 char *data;
michael@0 434 const char *name;
michael@0 435 private:
michael@0 436 ElfSection *link;
michael@0 437 SectionInfo info;
michael@0 438 ElfSection *next, *previous;
michael@0 439 int index;
michael@0 440 std::vector<ElfSegment *> segments;
michael@0 441 };
michael@0 442
michael@0 443 class ElfSegment {
michael@0 444 public:
michael@0 445 ElfSegment(Elf_Phdr *phdr);
michael@0 446
michael@0 447 unsigned int getType() { return type; }
michael@0 448 unsigned int getFlags() { return flags; }
michael@0 449 unsigned int getAlign() { return align; }
michael@0 450
michael@0 451 ElfSection *getFirstSection() { return sections.empty() ? nullptr : sections.front(); }
michael@0 452 int getVPDiff() { return v_p_diff; }
michael@0 453 unsigned int getFileSize();
michael@0 454 unsigned int getMemSize();
michael@0 455 unsigned int getOffset();
michael@0 456 unsigned int getAddr();
michael@0 457
michael@0 458 void addSection(ElfSection *section);
michael@0 459 void removeSection(ElfSection *section);
michael@0 460
michael@0 461 std::list<ElfSection *>::iterator begin() { return sections.begin(); }
michael@0 462 std::list<ElfSection *>::iterator end() { return sections.end(); }
michael@0 463
michael@0 464 void clear();
michael@0 465
michael@0 466 bool isElfHackFillerSegment() {
michael@0 467 return type == PT_LOAD && flags == 0;
michael@0 468 }
michael@0 469 private:
michael@0 470 unsigned int type;
michael@0 471 int v_p_diff; // Difference between physical and virtual address
michael@0 472 unsigned int flags;
michael@0 473 unsigned int align;
michael@0 474 std::list<ElfSection *> sections;
michael@0 475 // The following are only really used for PT_GNU_RELRO until something
michael@0 476 // better is found.
michael@0 477 unsigned int vaddr;
michael@0 478 unsigned int filesz, memsz;
michael@0 479 };
michael@0 480
michael@0 481 class Elf_Ehdr: public serializable<Elf_Ehdr_Traits>, public ElfSection {
michael@0 482 public:
michael@0 483 Elf_Ehdr(std::ifstream &file, char ei_class, char ei_data);
michael@0 484 void serialize(std::ofstream &file, char ei_class, char ei_data)
michael@0 485 {
michael@0 486 serializable<Elf_Ehdr_Traits>::serialize(file, ei_class, ei_data);
michael@0 487 }
michael@0 488 };
michael@0 489
michael@0 490 class Elf_Phdr: public serializable<Elf_Phdr_Traits> {
michael@0 491 public:
michael@0 492 Elf_Phdr() {};
michael@0 493 Elf_Phdr(std::ifstream &file, char ei_class, char ei_data)
michael@0 494 : serializable<Elf_Phdr_Traits>(file, ei_class, ei_data) {};
michael@0 495 bool contains(ElfSection *section)
michael@0 496 {
michael@0 497 unsigned int size = section->getSize();
michael@0 498 unsigned int addr = section->getAddr();
michael@0 499 // This may be biased, but should work in most cases
michael@0 500 if ((section->getFlags() & SHF_ALLOC) == 0)
michael@0 501 return false;
michael@0 502 // Special case for PT_DYNAMIC. Eventually, this should
michael@0 503 // be better handled than special cases
michael@0 504 if ((p_type == PT_DYNAMIC) && (section->getType() != SHT_DYNAMIC))
michael@0 505 return false;
michael@0 506 // Special case for PT_TLS.
michael@0 507 if ((p_type == PT_TLS) && !(section->getFlags() & SHF_TLS))
michael@0 508 return false;
michael@0 509 return (addr >= p_vaddr) &&
michael@0 510 (addr + size <= p_vaddr + p_memsz);
michael@0 511
michael@0 512 }
michael@0 513 };
michael@0 514
michael@0 515 typedef serializable<Elf_Dyn_Traits> Elf_Dyn;
michael@0 516
michael@0 517 struct Elf_DynValue {
michael@0 518 unsigned int tag;
michael@0 519 ElfValue *value;
michael@0 520 };
michael@0 521
michael@0 522 class ElfDynamic_Section: public ElfSection {
michael@0 523 public:
michael@0 524 ElfDynamic_Section(Elf_Shdr &s, std::ifstream *file, Elf *parent);
michael@0 525 ~ElfDynamic_Section();
michael@0 526
michael@0 527 void serialize(std::ofstream &file, char ei_class, char ei_data);
michael@0 528
michael@0 529 ElfValue *getValueForType(unsigned int tag);
michael@0 530 ElfSection *getSectionForType(unsigned int tag);
michael@0 531 bool setValueForType(unsigned int tag, ElfValue *val);
michael@0 532 private:
michael@0 533 std::vector<Elf_DynValue> dyns;
michael@0 534 };
michael@0 535
michael@0 536 typedef serializable<Elf_Sym_Traits> Elf_Sym;
michael@0 537
michael@0 538 struct Elf_SymValue {
michael@0 539 const char *name;
michael@0 540 unsigned char info;
michael@0 541 unsigned char other;
michael@0 542 ElfLocation value;
michael@0 543 unsigned int size;
michael@0 544 bool defined;
michael@0 545 };
michael@0 546
michael@0 547 #define STT(type) (1 << STT_ ##type)
michael@0 548
michael@0 549 class ElfSymtab_Section: public ElfSection {
michael@0 550 public:
michael@0 551 ElfSymtab_Section(Elf_Shdr &s, std::ifstream *file, Elf *parent);
michael@0 552
michael@0 553 void serialize(std::ofstream &file, char ei_class, char ei_data);
michael@0 554
michael@0 555 Elf_SymValue *lookup(const char *name, unsigned int type_filter = STT(OBJECT) | STT(FUNC));
michael@0 556
michael@0 557 //private: // Until we have a real API
michael@0 558 std::vector<Elf_SymValue> syms;
michael@0 559 };
michael@0 560
michael@0 561 class Elf_Rel: public serializable<Elf_Rel_Traits> {
michael@0 562 public:
michael@0 563 Elf_Rel(std::ifstream &file, char ei_class, char ei_data)
michael@0 564 : serializable<Elf_Rel_Traits>(file, ei_class, ei_data) {};
michael@0 565
michael@0 566 static const unsigned int sh_type = SHT_REL;
michael@0 567 static const unsigned int d_tag = DT_REL;
michael@0 568 static const unsigned int d_tag_count = DT_RELCOUNT;
michael@0 569 };
michael@0 570
michael@0 571 class Elf_Rela: public serializable<Elf_Rela_Traits> {
michael@0 572 public:
michael@0 573 Elf_Rela(std::ifstream &file, char ei_class, char ei_data)
michael@0 574 : serializable<Elf_Rela_Traits>(file, ei_class, ei_data) {};
michael@0 575
michael@0 576 static const unsigned int sh_type = SHT_RELA;
michael@0 577 static const unsigned int d_tag = DT_RELA;
michael@0 578 static const unsigned int d_tag_count = DT_RELACOUNT;
michael@0 579 };
michael@0 580
michael@0 581 template <class Rel>
michael@0 582 class ElfRel_Section: public ElfSection {
michael@0 583 public:
michael@0 584 ElfRel_Section(Elf_Shdr &s, std::ifstream *file, Elf *parent)
michael@0 585 : ElfSection(s, file, parent)
michael@0 586 {
michael@0 587 int pos = file->tellg();
michael@0 588 file->seekg(shdr.sh_offset);
michael@0 589 for (unsigned int i = 0; i < s.sh_size / s.sh_entsize; i++) {
michael@0 590 Rel r(*file, parent->getClass(), parent->getData());
michael@0 591 rels.push_back(r);
michael@0 592 }
michael@0 593 file->seekg(pos);
michael@0 594 }
michael@0 595
michael@0 596 void serialize(std::ofstream &file, char ei_class, char ei_data)
michael@0 597 {
michael@0 598 for (typename std::vector<Rel>::iterator i = rels.begin();
michael@0 599 i != rels.end(); ++i)
michael@0 600 (*i).serialize(file, ei_class, ei_data);
michael@0 601 }
michael@0 602 //private: // Until we have a real API
michael@0 603 std::vector<Rel> rels;
michael@0 604 };
michael@0 605
michael@0 606 class ElfStrtab_Section: public ElfSection {
michael@0 607 public:
michael@0 608 ElfStrtab_Section(Elf_Shdr &s, std::ifstream *file, Elf *parent)
michael@0 609 : ElfSection(s, file, parent)
michael@0 610 {
michael@0 611 table.push_back(table_storage(data, shdr.sh_size));
michael@0 612 }
michael@0 613
michael@0 614 ~ElfStrtab_Section()
michael@0 615 {
michael@0 616 for (std::vector<table_storage>::iterator t = table.begin() + 1;
michael@0 617 t != table.end(); t++)
michael@0 618 delete[] t->buf;
michael@0 619 }
michael@0 620
michael@0 621 const char *getStr(unsigned int index);
michael@0 622
michael@0 623 const char *getStr(const char *string);
michael@0 624
michael@0 625 unsigned int getStrIndex(const char *string);
michael@0 626
michael@0 627 void serialize(std::ofstream &file, char ei_class, char ei_data);
michael@0 628 private:
michael@0 629 struct table_storage {
michael@0 630 unsigned int size, used;
michael@0 631 char *buf;
michael@0 632
michael@0 633 table_storage(): size(4096), used(0), buf(new char[4096]) {}
michael@0 634 table_storage(const char *data, unsigned int sz)
michael@0 635 : size(sz), used(sz), buf(const_cast<char *>(data)) {}
michael@0 636 };
michael@0 637 std::vector<table_storage> table;
michael@0 638 };
michael@0 639
michael@0 640 inline char Elf::getClass() {
michael@0 641 return ehdr->e_ident[EI_CLASS];
michael@0 642 }
michael@0 643
michael@0 644 inline char Elf::getData() {
michael@0 645 return ehdr->e_ident[EI_DATA];
michael@0 646 }
michael@0 647
michael@0 648 inline char Elf::getType() {
michael@0 649 return ehdr->e_type;
michael@0 650 }
michael@0 651
michael@0 652 inline char Elf::getMachine() {
michael@0 653 return ehdr->e_machine;
michael@0 654 }
michael@0 655
michael@0 656 inline unsigned int Elf::getSize() {
michael@0 657 ElfSection *section;
michael@0 658 for (section = shdr_section /* It's usually not far from the end */;
michael@0 659 section->getNext() != nullptr; section = section->getNext());
michael@0 660 return section->getOffset() + section->getSize();
michael@0 661 }
michael@0 662
michael@0 663 inline ElfSegment *ElfSection::getSegmentByType(unsigned int type) {
michael@0 664 for (std::vector<ElfSegment *>::iterator seg = segments.begin(); seg != segments.end(); seg++)
michael@0 665 if ((*seg)->getType() == type)
michael@0 666 return *seg;
michael@0 667 return nullptr;
michael@0 668 }
michael@0 669
michael@0 670 inline void ElfSection::insertInSegments(std::vector<ElfSegment *> &segs) {
michael@0 671 for (std::vector<ElfSegment *>::iterator it = segs.begin(); it != segs.end(); ++it) {
michael@0 672 (*it)->addSection(this);
michael@0 673 }
michael@0 674 }
michael@0 675
michael@0 676 inline ElfLocation::ElfLocation(ElfSection *section, unsigned int off, enum position pos)
michael@0 677 : section(section) {
michael@0 678 if ((pos == ABSOLUTE) && section)
michael@0 679 offset = off - section->getAddr();
michael@0 680 else
michael@0 681 offset = off;
michael@0 682 }
michael@0 683
michael@0 684 inline ElfLocation::ElfLocation(unsigned int location, Elf *elf) {
michael@0 685 section = elf->getSectionAt(location);
michael@0 686 offset = location - (section ? section->getAddr() : 0);
michael@0 687 }
michael@0 688
michael@0 689 inline unsigned int ElfLocation::getValue() {
michael@0 690 return (section ? section->getAddr() : 0) + offset;
michael@0 691 }
michael@0 692
michael@0 693 inline const char *ElfLocation::getBuffer() {
michael@0 694 return section ? section->getData() + offset : nullptr;
michael@0 695 }
michael@0 696
michael@0 697 inline unsigned int ElfSize::getValue() {
michael@0 698 return section->getSize();
michael@0 699 }
michael@0 700
michael@0 701 inline unsigned int ElfEntSize::getValue() {
michael@0 702 return section->getEntSize();
michael@0 703 }

mercurial