CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ElfBinaries.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2016 Mateusz Szpakowski
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
23 #ifndef __CLRX_ELFBINARIES_H__
24 #define __CLRX_ELFBINARIES_H__
25 
26 #include <CLRX/Config.h>
27 #include <cstddef>
28 #include <cstdint>
29 #include <climits>
30 #include <string>
31 #include <utility>
32 #include <ostream>
33 #include <CLRX/amdbin/Elf.h>
34 #include <CLRX/utils/MemAccess.h>
35 #include <CLRX/utils/Utilities.h>
36 #include <CLRX/utils/Containers.h>
37 #include <CLRX/utils/InputOutput.h>
38 
39 /* INFO: in this file is used ULEV function for conversion
40  * from LittleEndian and unaligned access to other memory access policy and endianness
41  * Please use this function whenever you want to get or set word in ELF binary,
42  * because ELF binaries can be unaligned in memory (as inner binaries).
43  */
44 
46 namespace CLRX
47 {
48 
49 enum: cxuint {
50  BINGEN_DEFAULT = UINT_MAX,
51  BINGEN_NOTSUPPLIED = UINT_MAX-1
52 };
53 
54 
55 enum : Flags {
60 };
61 
63 struct Elf32Types
64 {
65  typedef uint32_t Size;
66  typedef uint32_t Word;
67  typedef uint32_t SectionFlags;
68  typedef Elf32_Ehdr Ehdr;
69  typedef Elf32_Shdr Shdr;
70  typedef Elf32_Phdr Phdr;
71  typedef Elf32_Sym Sym;
72  static const cxbyte ELFCLASS;
73  static const cxuint bitness;
74  static const char* bitName;
75  static const Word nobase = Word(0)-1;
76 };
77 
79 struct Elf64Types
80 {
81  typedef size_t Size;
82  typedef uint64_t Word;
83  typedef uint64_t SectionFlags;
84  typedef Elf64_Ehdr Ehdr;
85  typedef Elf64_Shdr Shdr;
86  typedef Elf64_Phdr Phdr;
87  typedef Elf64_Sym Sym;
88  static const cxbyte ELFCLASS;
89  static const cxuint bitness;
90  static const char* bitName;
91  static const Word nobase = Word(0)-1;
92 };
93 
95 
98 template<typename Types>
100 {
101 public:
106 protected:
108  size_t binaryCodeSize;
109  cxbyte* binaryCode;
112  cxbyte* symbolTable;
114  cxbyte* dynSymTable;
118 
119  typename Types::Size symbolsNum;
120  typename Types::Size dynSymbolsNum;
121  uint16_t symbolEntSize;
122  uint16_t dynSymEntSize;
123 
124 public:
133  virtual ~ElfBinaryTemplate();
134 
137  { return creationFlags; }
138 
140  bool hasSectionMap() const
141  { return (creationFlags & ELF_CREATE_SECTIONMAP) != 0; }
142 
144  bool hasSymbolMap() const
145  { return (creationFlags & ELF_CREATE_SYMBOLMAP) != 0; }
146 
148  bool hasDynSymbolMap() const
149  { return (creationFlags & ELF_CREATE_DYNSYMMAP) != 0; }
150 
152  size_t getSize() const
153  { return binaryCodeSize; }
154 
156  operator bool() const
157  { return binaryCode!=nullptr; }
158 
160  bool operator!() const
161  { return binaryCode==nullptr; }
162 
164  const cxbyte* getBinaryCode() const
165  { return binaryCode; }
167  cxbyte* getBinaryCode()
168  { return binaryCode; }
169 
171  const typename Types::Ehdr& getHeader() const
172  { return *reinterpret_cast<const typename Types::Ehdr*>(binaryCode); }
173 
175  typename Types::Ehdr& getHeader()
176  { return *reinterpret_cast<typename Types::Ehdr*>(binaryCode); }
177 
179  uint16_t getSectionHeadersNum() const
180  { return ULEV(getHeader().e_shnum); }
181 
183  const typename Types::Shdr& getSectionHeader(uint16_t index) const
184  {
185  const typename Types::Ehdr& ehdr = getHeader();
186  return *reinterpret_cast<const typename Types::Shdr*>(binaryCode +
187  ULEV(ehdr.e_shoff) + size_t(ULEV(ehdr.e_shentsize))*index);
188  }
189 
191  typename Types::Shdr& getSectionHeader(uint16_t index)
192  {
193  const typename Types::Ehdr& ehdr = getHeader();
194  return *reinterpret_cast<typename Types::Shdr*>(binaryCode +
195  ULEV(ehdr.e_shoff) + size_t(ULEV(ehdr.e_shentsize))*index);
196  }
197 
199  uint16_t getProgramHeadersNum() const
200  { return ULEV(getHeader().e_phnum); }
201 
203  const typename Types::Phdr& getProgramHeader(uint16_t index) const
204  {
205  const typename Types::Ehdr& ehdr = getHeader();
206  return *reinterpret_cast<const typename Types::Phdr*>(binaryCode +
207  ULEV(ehdr.e_phoff) + size_t(ULEV(ehdr.e_phentsize))*index);
208  }
209 
211  typename Types::Phdr& getProgramHeader(uint16_t index)
212  {
213  const typename Types::Ehdr& ehdr = getHeader();
214  return *reinterpret_cast<typename Types::Phdr*>(binaryCode +
215  ULEV(ehdr.e_phoff) + size_t(ULEV(ehdr.e_phentsize))*index);
216  }
217 
219  typename Types::Size getSymbolsNum() const
220  { return symbolsNum; }
221 
223  typename Types::Size getDynSymbolsNum() const
224  { return dynSymbolsNum; }
225 
227  const typename Types::Sym& getSymbol(typename Types::Size index) const
228  {
229  return *reinterpret_cast<const typename Types::Sym*>(symbolTable +
230  size_t(index)*symbolEntSize);
231  }
232 
234  typename Types::Sym& getSymbol(typename Types::Size index)
235  {
236  return *reinterpret_cast<typename Types::Sym*>(
237  symbolTable + size_t(index)*symbolEntSize);
238  }
239 
241  const typename Types::Sym& getDynSymbol(typename Types::Size index) const
242  {
243  return *reinterpret_cast<const typename Types::Sym*>(dynSymTable +
244  size_t(index)*dynSymEntSize);
245  }
246 
248  typename Types::Sym& getDynSymbol(typename Types::Size index)
249  {
250  return *reinterpret_cast<typename Types::Sym*>(dynSymTable +
251  size_t(index)*dynSymEntSize);
252  }
253 
255  const char* getSymbolName(typename Types::Size index) const
256  {
257  const typename Types::Sym& sym = getSymbol(index);
258  return reinterpret_cast<const char*>(symbolStringTable + ULEV(sym.st_name));
259  }
260 
262  const char* getDynSymbolName(typename Types::Size index) const
263  {
264  const typename Types::Sym& sym = getDynSymbol(index);
265  return reinterpret_cast<const char*>(dynSymStringTable + ULEV(sym.st_name));
266  }
267 
269  const char* getSectionName(uint16_t index) const
270  {
271  const typename Types::Shdr& section = getSectionHeader(index);
272  return reinterpret_cast<const char*>(sectionStringTable + ULEV(section.sh_name));
273  }
274 
277  { return sectionIndexMap.end(); }
278 
281  {
284  if (it == sectionIndexMap.end())
285  throw Exception(std::string("Can't find Elf")+Types::bitName+" Section");
286  return it;
287  }
288 
290  uint16_t getSectionIndex(const char* name) const;
291 
293  typename Types::Size getSymbolIndex(const char* name) const;
294 
296  typename Types::Size getDynSymbolIndex(const char* name) const;
297 
300  { return symbolIndexMap.end(); }
301 
304  { return dynSymIndexMap.end(); }
305 
308  {
311  if (it == symbolIndexMap.end())
312  throw Exception(std::string("Can't find Elf")+Types::bitName+" Symbol");
313  return it;
314  }
315 
318  {
321  if (it == dynSymIndexMap.end())
322  throw Exception(std::string("Can't find Elf")+Types::bitName+" DynSymbol");
323  return it;
324  }
325 
327  const typename Types::Shdr& getSectionHeader(const char* name) const
328  { return getSectionHeader(getSectionIndex(name)); }
329 
331  typename Types::Shdr& getSectionHeader(const char* name)
332  { return getSectionHeader(getSectionIndex(name)); }
333 
335  const typename Types::Sym& getSymbol(const char* name) const
336  { return getSymbol(getSymbolIndex(name)); }
337 
339  typename Types::Sym& getSymbol(const char* name)
340  { return getSymbol(getSymbolIndex(name)); }
341 
343  const typename Types::Sym& getDynSymbol(const char* name) const
344  { return getDynSymbol(getDynSymbolIndex(name)); }
345 
347  typename Types::Sym& getDynSymbol(const char* name)
348  { return getDynSymbol(getDynSymbolIndex(name)); }
349 
351  const cxbyte* getSectionContent(uint16_t index) const
352  {
353  const typename Types::Shdr& shdr = getSectionHeader(index);
354  return binaryCode + ULEV(shdr.sh_offset);
355  }
356 
358  cxbyte* getSectionContent(uint16_t index)
359  {
360  typename Types::Shdr& shdr = getSectionHeader(index);
361  return binaryCode + ULEV(shdr.sh_offset);
362  }
363 
365  const cxbyte* getSectionContent(const char* name) const
366  { return getSectionContent(getSectionIndex(name)); }
367 
369  cxbyte* getSectionContent(const char* name)
370  { return getSectionContent(getSectionIndex(name)); }
371 };
372 
373 extern template class ElfBinaryTemplate<Elf32Types>;
374 extern template class ElfBinaryTemplate<Elf64Types>;
375 
377 extern bool isElfBinary(size_t binarySize, const cxbyte* binary);
378 
383 
385 enum class ElfRegionType: cxbyte
386 {
387  PHDR_TABLE,
388  SHDR_TABLE,
389  SECTION,
390  USER
391 };
392 
395 {
396 public:
397  virtual ~ElfRegionContent();
398 
400  virtual void operator()(FastOutputBuffer& fob) const = 0;
401 };
402 
404 template<typename Types>
406 {
407  typename Types::Word paddrBase;
408  typename Types::Word vaddrBase;
409  cxbyte osABI;
410  cxbyte abiVersion;
411  uint16_t type;
412  uint16_t machine;
413  uint32_t version;
414  cxuint entryRegion;
415  typename Types::Word entry;
416  uint32_t flags;
417 };
418 
423 
424 
425 enum: cxuint {
426  ELFSECTID_START = UINT_MAX-255,
427  ELFSECTID_SHSTRTAB = ELFSECTID_START,
428  ELFSECTID_STRTAB,
429  ELFSECTID_SYMTAB,
430  ELFSECTID_DYNSTR,
431  ELFSECTID_DYNSYM,
432  ELFSECTID_TEXT,
433  ELFSECTID_RODATA,
434  ELFSECTID_DATA,
435  ELFSECTID_BSS,
436  ELFSECTID_COMMENT,
437  ELFSECTID_STD_MAX = ELFSECTID_COMMENT,
438  ELFSECTID_OTHER_BUILTIN = ELFSECTID_STD_MAX+1,
439  ELFSECTID_NULL = UINT_MAX-2,
440  ELFSECTID_ABS = UINT_MAX-1,
441  ELFSECTID_UNDEF = UINT_MAX
442 };
443 
446 {
448  size_t size;
449  const cxbyte* data;
450  uint64_t align;
451  uint32_t type;
452  uint64_t flags;
453  cxuint linkId;
454  uint32_t info;
455  size_t entSize;
456 };
457 
459 struct BinSymbol
460 {
462  uint64_t value;
463  uint64_t size;
464  cxuint sectionId;
465  bool valueIsAddr;
466  cxbyte info;
467  cxbyte other;
468 };
469 
471 extern uint16_t convertSectionId(cxuint sectionIndex, const uint16_t* builtinSections,
472  cxuint maxBuiltinSection, cxuint extraSectionIndex);
473 
475 template<typename Types>
477 {
479 
482 
484 
486  typename Types::Word size;
487  typename Types::Word align;
488  union
489  {
490  const cxbyte* data;
492  };
493  struct {
494  const char* name;
495  uint32_t type;
496  typename Types::SectionFlags flags;
497  uint32_t link;
498  uint32_t info;
499  typename Types::Word addrBase;
500  typename Types::Word entSize;
501  bool zeroOffset;
502  } section;
503 
505  ElfRegionTemplate(typename Types::Word _size,
506  const cxbyte* _data, typename Types::Word _align)
507  : type(ElfRegionType::USER), dataFromPointer(true), size(_size),
508  align(_align), data(_data)
509  { }
510 
512  ElfRegionTemplate(typename Types::Word _size,
513  const ElfRegionContent* contentGen, typename Types::Word _align)
514  : type(ElfRegionType::USER), dataFromPointer(false), size(_size),
515  align(_align), dataGen(contentGen)
516  { }
517 
519  ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size,
520  const cxbyte* _data, typename Types::Word _align)
521  : type(_type), dataFromPointer(true), size(_size),
522  align(_align), data(_data)
523  { }
524 
526  ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size,
527  const ElfRegionContent* contentGen, typename Types::Word _align)
528  : type(_type), dataFromPointer(false), size(_size),
529  align(_align), dataGen(contentGen)
530  { }
531 
533  ElfRegionTemplate(typename Types::Word _size, const cxbyte* _data,
534  typename Types::Word _align, const char* _name, uint32_t _type,
535  typename Types::SectionFlags _flags, uint32_t _link = 0, uint32_t _info = 0,
536  typename Types::Word _addrBase = 0,
537  typename Types::Word _entSize = 0, bool _zeroOffset = false)
538  : type(ElfRegionType::SECTION), dataFromPointer(true), size(_size),
539  align(_align), data(_data)
540  {
541  section = {_name, _type, _flags, _link, _info, _addrBase, _entSize, _zeroOffset};
542  }
543 
545  ElfRegionTemplate(typename Types::Word _size, const ElfRegionContent* _data,
546  typename Types::Word _align, const char* inName, uint32_t _type,
547  typename Types::SectionFlags _flags, uint32_t _link = 0, uint32_t _info = 0,
548  typename Types::Word _addrBase = 0,
549  typename Types::Word _entSize = 0, bool _zeroOffset = false)
550  : type(ElfRegionType::SECTION), dataFromPointer(false), size(_size),
551  align(_align), dataGen(_data)
552  {
553  section = {inName, _type, _flags, _link, _info, _addrBase, _entSize, _zeroOffset};
554  }
556 
562  ElfRegionTemplate(const BinSection& binSection, const uint16_t* builtinSections,
563  cxuint maxBuiltinSection, cxuint startExtraIndex)
564  : type(ElfRegionType::SECTION), dataFromPointer(true), size(binSection.size),
565  align(binSection.align), data(binSection.data)
566  {
567  section = { binSection.name.c_str(), binSection.type,
568  typename Types::SectionFlags(binSection.flags),
569  uint32_t(convertSectionId(binSection.linkId, builtinSections,
570  maxBuiltinSection, startExtraIndex)),
571  binSection.info, 0, typename Types::Word(binSection.entSize) };
572  }
573 
576  { return ElfRegionTemplate(ElfRegionType::PHDR_TABLE, 0, (const cxbyte*)nullptr,
577  sizeof(typename Types::Word)); }
578 
581  { return ElfRegionTemplate(ElfRegionType::SHDR_TABLE, 0, (const cxbyte*)nullptr,
582  sizeof(typename Types::Word)); }
583 
586  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 1, ".strtab", SHT_STRTAB, 0); }
587 
590  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB,
591  SHF_ALLOC); }
592 
595  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 1, ".shstrtab", SHT_STRTAB, 0); }
596 
599  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
600  ".symtab", SHT_SYMTAB, 0); }
601 
604  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
605  ".dynsym", SHT_DYNSYM, 0); }
606 };
607 
612 
614 template<typename Types>
616 {
617  uint32_t type;
618  uint32_t flags;
619  cxuint regionStart;
620  cxuint regionsNum;
621  bool haveMemSize;
622  typename Types::Word paddrBase;
623  typename Types::Word vaddrBase;
624  typename Types::Word memSize;
625 };
626 
631 
633 template<typename Types>
635 {
636  const char* name;
637  uint16_t sectionIndex;
638  cxbyte info;
639  cxbyte other;
640  bool valueIsAddr;
641  typename Types::Word value;
642  typename Types::Word size;
643 
645  ElfSymbolTemplate(const char* _name, uint16_t _sectionIndex,
646  cxbyte _info, cxbyte _other, bool _valueIsAddr,
647  typename Types::Word _value, typename Types::Word _size)
648  : name(_name), sectionIndex(_sectionIndex), info(_info), other(_other),
649  valueIsAddr(_valueIsAddr), value(_value), size(_size)
650  { }
652 
658  ElfSymbolTemplate(const BinSymbol& binSymbol, const uint16_t* builtinSections,
659  cxuint maxBuiltinSection, cxuint startExtraIndex)
660  {
661  name = binSymbol.name.c_str();
662  sectionIndex = convertSectionId(binSymbol.sectionId, builtinSections,
663  maxBuiltinSection, startExtraIndex);
664  info = binSymbol.info;
665  other = binSymbol.other;
666  valueIsAddr = binSymbol.valueIsAddr;
667  value = binSymbol.value;
668  size = binSymbol.size;
669  }
670 };
671 
676 
678 template<typename Types>
680 {
681 private:
682  bool sizeComputed;
683  bool addNullSym, addNullDynSym;
684  bool addNullSection;
685  cxuint addrStartRegion;
686  uint16_t shStrTab, strTab, dynStr;
687  cxuint shdrTabRegion, phdrTabRegion;
688  uint16_t sectionsNum;
689  typename Types::Word size;
691  std::vector<ElfRegionTemplate<Types> > regions;
692  std::unique_ptr<typename Types::Word[]> regionOffsets;
693  std::unique_ptr<typename Types::Word[]> regionAddresses;
694  std::unique_ptr<cxuint[]> sectionRegions;
695  std::vector<ElfProgramHeaderTemplate<Types> > progHeaders;
696  std::vector<ElfSymbolTemplate<Types> > symbols;
697  std::vector<ElfSymbolTemplate<Types> > dynSymbols;
698 
699  void computeSize();
700 public:
703 
710  explicit ElfBinaryGenTemplate(const ElfHeaderTemplate<Types>& header,
711  bool addNullSym = true, bool addNullDynSym = true,
712  bool addNullSection = true, cxuint addrCountingFromRegion = 0);
713 
716  { this->header = header; }
717 
719  void addRegion(const ElfRegionTemplate<Types>& region);
721  void addProgramHeader(const ElfProgramHeaderTemplate<Types>& progHeader);
722 
725  { symbols.push_back(symbol); }
728  { dynSymbols.push_back(symbol); }
729 
731  uint64_t countSize();
732 
734  void generate(FastOutputBuffer& fob);
735 
737  void generate(std::ostream& os)
738  {
739  FastOutputBuffer fob(256, os);
740  generate(fob);
741  }
742 };
743 
744 extern template class ElfBinaryGenTemplate<Elf32Types>;
745 extern template class ElfBinaryGenTemplate<Elf64Types>;
746 
751 
752 };
753 
754 #endif
const char * getDynSymbolName(typename Types::Size index) const
get dynamic symbol name with specified index
Definition: ElfBinaries.h:262
Types::Word memSize
size in memory
Definition: ElfBinaries.h:624
const Types::Sym & getDynSymbol(typename Types::Size index) const
get dynamic symbol with specified index
Definition: ElfBinaries.h:241
const Types::Shdr & getSectionHeader(uint16_t index) const
get section header with specified index
Definition: ElfBinaries.h:183
uint32_t link
section link
Definition: ElfBinaries.h:497
ElfSymbolTemplate(const BinSymbol &binSymbol, const uint16_t *builtinSections, cxuint maxBuiltinSection, cxuint startExtraIndex)
constructor for extra symbol
Definition: ElfBinaries.h:658
uint32_t Word
word size in ELF
Definition: ElfBinaries.h:66
SymbolIndexMap::const_iterator getDynSymbolIterEnd() const
get end iterator of dynamic symbol index map
Definition: ElfBinaries.h:303
Elf64_Sym Sym
symbol header
Definition: ElfBinaries.h:87
program header table
Elf64_Ehdr Ehdr
ELF header.
Definition: ElfBinaries.h:84
const cxbyte * data
data content
Definition: ElfBinaries.h:449
cxbyte info
info
Definition: ElfBinaries.h:466
Types::Shdr & getSectionHeader(const char *name)
get section header with specified name
Definition: ElfBinaries.h:331
creation flags for ELF binaries
Definition: ElfBinaries.h:59
ELF binary class.
Definition: ElfBinaries.h:99
Types::Word paddrBase
physical address base
Definition: ElfBinaries.h:407
uint16_t getSectionIndex(const char *name) const
get section index with specified name
Definition: Elf.h:69
uint32_t type
section type
Definition: ElfBinaries.h:451
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
Definition: Elf.h:240
static const Word nobase
address with zero base
Definition: ElfBinaries.h:91
const Types::Ehdr & getHeader() const
get ELF binary header
Definition: ElfBinaries.h:171
const T * begin() const
get iterator to first element
Definition: Containers.h:243
Types::Size getDynSymbolIndex(const char *name) const
get dynamic symbol index with specified name (requires dynamic symbol index map)
Types::Word vaddrBase
vaddr base
Definition: ElfBinaries.h:623
Iter binaryMapFind(Iter begin, Iter end, const typename std::iterator_traits< Iter >::value_type::first_type &k)
binary find helper for array-map
Definition: Containers.h:379
size_t binaryCodeSize
binary code size
Definition: ElfBinaries.h:108
ElfRegionType type
type of region
Definition: ElfBinaries.h:478
fast and direct output buffer
Definition: InputOutput.h:350
const char * c_str() const
return C-style string pointer
Definition: CString.h:245
uint32_t info
section info
Definition: ElfBinaries.h:498
template of ElfRegion
Definition: ElfBinaries.h:476
static ElfRegionTemplate shstrtabSection()
get .shstrtab section
Definition: ElfBinaries.h:594
uint64_t flags
section flags
Definition: ElfBinaries.h:452
elf header template
Definition: ElfBinaries.h:405
void addDynSymbol(const ElfSymbolTemplate< Types > &symbol)
add dynamic symbol
Definition: ElfBinaries.h:727
Types::Sym & getSymbol(typename Types::Size index)
get symbol with specified index
Definition: ElfBinaries.h:234
uint64_t value
symbol value
Definition: ElfBinaries.h:462
const Types::Sym & getDynSymbol(const char *name) const
get dynamic symbol with specified name (requires dynamic symbol index map)
Definition: ElfBinaries.h:343
uint64_t size
symbol size
Definition: ElfBinaries.h:463
bool hasSectionMap() const
returns true if object has a section's index map
Definition: ElfBinaries.h:140
bool valueIsAddr
true if value should be treats as address
Definition: ElfBinaries.h:640
ELF 32-bit types.
Definition: ElfBinaries.h:63
ElfRegionTemplate< Elf32Types > ElfRegion32
32-bit region (for 32-bit elf)
Definition: ElfBinaries.h:609
Definition: Elf.h:349
section header table
bool haveMemSize
true if program header has memory size
Definition: ElfBinaries.h:621
class ElfBinaryTemplate< Elf64Types > ElfBinary64
type for 64-bit ELF binary
Definition: ElfBinaries.h:382
create map of sections
Definition: ElfBinaries.h:56
static const Word nobase
address with zero base
Definition: ElfBinaries.h:75
ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size, const cxbyte *_data, typename Types::Word _align)
constructor for region
Definition: ElfBinaries.h:519
cxbyte * binaryCode
pointer to binary code
Definition: ElfBinaries.h:109
static ElfRegionTemplate sectionHeaderTable()
get program header table region
Definition: ElfBinaries.h:580
SymbolIndexMap::const_iterator getSymbolIterEnd() const
get end iterator of symbol index map
Definition: ElfBinaries.h:299
symbol structure to external usage (fo example in the binary generator input)
Definition: ElfBinaries.h:459
cxbyte info
info
Definition: ElfBinaries.h:638
Types::Size dynSymbolsNum
dynamic symbols number
Definition: ElfBinaries.h:120
Types::Word addrBase
section address base
Definition: ElfBinaries.h:499
bool dataFromPointer
true if content from pointer, otherwise will be generated from class
Definition: ElfBinaries.h:481
CString name
name of section
Definition: ElfBinaries.h:447
uint64_t align
region alignment
Definition: ElfBinaries.h:450
Elf64_Shdr Shdr
Section header.
Definition: ElfBinaries.h:85
Array< std::pair< const char *, size_t > > SymbolIndexMap
symbol index map
Definition: ElfBinaries.h:105
static ElfRegionTemplate programHeaderTable()
get program header table region
Definition: ElfBinaries.h:575
const cxbyte * getSectionContent(uint16_t index) const
get section content pointer
Definition: ElfBinaries.h:351
Types::Size symbolsNum
symbols number
Definition: ElfBinaries.h:119
an array class
Definition: Containers.h:38
Elf32_Sym Sym
symbol header
Definition: ElfBinaries.h:71
Types::Word entSize
entries size
Definition: ElfBinaries.h:500
uint16_t convertSectionId(cxuint sectionIndex, const uint16_t *builtinSections, cxuint maxBuiltinSection, cxuint extraSectionIndex)
convert section id to elf section id
cxbyte abiVersion
ABI version.
Definition: ElfBinaries.h:410
uint32_t version
version
Definition: ElfBinaries.h:413
static ElfRegionTemplate symtabSection()
get symtab section
Definition: ElfBinaries.h:598
ElfHeaderTemplate< Elf64Types > ElfHeader64
64-bit elf header
Definition: ElfBinaries.h:422
cxbyte * getBinaryCode()
get binary code
Definition: ElfBinaries.h:167
const std::pair< const char *, size_t > * const_iterator
type of constant iterator
Definition: Containers.h:42
void generate(std::ostream &os)
generate binary
Definition: ElfBinaries.h:737
const Types::Sym & getSymbol(const char *name) const
get symbol with specified name (requires symbol index map)
Definition: ElfBinaries.h:335
Types::Phdr & getProgramHeader(uint16_t index)
get program header with specified index
Definition: ElfBinaries.h:211
Types::Size getSymbolIndex(const char *name) const
get symbol index with specified name (requires symbol index map)
CString name
name
Definition: ElfBinaries.h:461
Types::Sym & getSymbol(const char *name)
get symbol with specified name (requires symbol index map)
Definition: ElfBinaries.h:339
ELF binary generator.
Definition: ElfBinaries.h:679
uint32_t info
section info
Definition: ElfBinaries.h:454
const char * getSymbolName(typename Types::Size index) const
get symbol name with specified index
Definition: ElfBinaries.h:255
uint32_t type
section type
Definition: ElfBinaries.h:495
section structure to external usage (for example in the binary generator input)
Definition: ElfBinaries.h:445
const Types::Sym & getSymbol(typename Types::Size index) const
get symbol with specified index
Definition: ElfBinaries.h:227
void addSymbol(const ElfSymbolTemplate< Types > &symbol)
add symbol
Definition: ElfBinaries.h:724
uint16_t getSectionHeadersNum() const
get section headers number
Definition: ElfBinaries.h:179
cxbyte * symbolStringTable
pointer to symbol's string table
Definition: ElfBinaries.h:111
Types::Sym & getDynSymbol(typename Types::Size index)
get dynamic symbol with specified index
Definition: ElfBinaries.h:248
elf region content generator for elf region
Definition: ElfBinaries.h:394
const char * name
name
Definition: ElfBinaries.h:636
cxuint regionsNum
number of regions whose is in program header data
Definition: ElfBinaries.h:620
cxbyte osABI
os abi
Definition: ElfBinaries.h:409
const char * getSectionName(uint16_t index) const
get section name with specified index
Definition: ElfBinaries.h:269
ElfSymbolTemplate< Elf64Types > ElfSymbol64
64-bit elf symbol
Definition: ElfBinaries.h:675
ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size, const ElfRegionContent *contentGen, typename Types::Word _align)
constructor for region with content generator
Definition: ElfBinaries.h:526
ElfRegionTemplate< Elf64Types > ElfRegion64
64-bit region (for 64-bit elf)
Definition: ElfBinaries.h:611
if set in field then field has been ignored
Definition: ElfBinaries.h:51
const char * name
section name
Definition: ElfBinaries.h:494
input output utilities
static const char * bitName
bitness name
Definition: ElfBinaries.h:74
SectionIndexMap::const_iterator getSectionIter(const char *name) const
get section iterator with specified name (requires section index map)
Definition: ElfBinaries.h:280
const Types::Shdr & getSectionHeader(const char *name) const
get section header with specified name
Definition: ElfBinaries.h:327
SymbolIndexMap symbolIndexMap
symbol's index map
Definition: ElfBinaries.h:116
ElfProgramHeaderTemplate< Elf64Types > ElfProgramHeader64
64-bit elf program header
Definition: ElfBinaries.h:630
SymbolIndexMap::const_iterator getSymbolIter(const char *name) const
get symbol iterator with specified name (requires symbol index map)
Definition: ElfBinaries.h:307
size_t entSize
entries size
Definition: ElfBinaries.h:455
const cxbyte * getSectionContent(const char *name) const
get section content pointer
Definition: ElfBinaries.h:365
uint32_t flags
flags
Definition: ElfBinaries.h:416
void setHeader(const ElfHeaderTemplate< Types > &header)
set elf header
Definition: ElfBinaries.h:715
static const char * bitName
bitness name
Definition: ElfBinaries.h:90
create map of dynamic symbols
Definition: ElfBinaries.h:58
Types::Ehdr & getHeader()
get ELF binary header
Definition: ElfBinaries.h:175
Flags creationFlags
creation flags holder
Definition: ElfBinaries.h:107
uint32_t flags
flags
Definition: ElfBinaries.h:618
Types::Word entry
entry offset relative to region
Definition: ElfBinaries.h:415
bool hasDynSymbolMap() const
returns true if object has a dynamic symbol's index map
Definition: ElfBinaries.h:148
uint16_t symbolEntSize
symbol entry size in a symbol's table
Definition: ElfBinaries.h:121
size_t size
size of content
Definition: ElfBinaries.h:448
uint16_t dynSymEntSize
dynamic symbol entry size in a dynamic symbol's table
Definition: ElfBinaries.h:122
ElfRegionTemplate(typename Types::Word _size, const cxbyte *_data, typename Types::Word _align, const char *_name, uint32_t _type, typename Types::SectionFlags _flags, uint32_t _link=0, uint32_t _info=0, typename Types::Word _addrBase=0, typename Types::Word _entSize=0, bool _zeroOffset=false)
constructor for section
Definition: ElfBinaries.h:533
static const cxuint bitness
ELF bitness.
Definition: ElfBinaries.h:89
cxbyte other
other
Definition: ElfBinaries.h:467
size_t getSize() const
get size of binaries
Definition: ElfBinaries.h:152
cxuint entryRegion
region in which is entry
Definition: ElfBinaries.h:414
bool isElfBinary(size_t binarySize, const cxbyte *binary)
check whether binary data is is ELF binary
bool valueIsAddr
true if value should be treats as address
Definition: ElfBinaries.h:465
uint32_t Size
size used to return size value
Definition: ElfBinaries.h:65
uint64_t SectionFlags
section flags
Definition: ElfBinaries.h:83
ElfProgramHeaderTemplate< Elf32Types > ElfProgramHeader32
32-bit elf program header
Definition: ElfBinaries.h:628
inlines for accessing memory words in LittleEndian and unaligned
Types::Word vaddrBase
virtual address base
Definition: ElfBinaries.h:408
const cxbyte * data
content from pointer
Definition: ElfBinaries.h:490
if set in field then field has been filled later
Definition: ElfBinaries.h:50
ElfSymbolTemplate(const char *_name, uint16_t _sectionIndex, cxbyte _info, cxbyte _other, bool _valueIsAddr, typename Types::Word _value, typename Types::Word _size)
constructor (to replace initializer list construction)
Definition: ElfBinaries.h:645
ELF symbol template.
Definition: ElfBinaries.h:634
Types::Word align
region alignment
Definition: ElfBinaries.h:487
Types::Size getDynSymbolsNum() const
get dynamic symbols number
Definition: ElfBinaries.h:223
cxbyte * dynSymTable
pointer to dynamic symbol table
Definition: ElfBinaries.h:114
ElfHeaderTemplate< Elf32Types > ElfHeader32
32-bit elf header
Definition: ElfBinaries.h:420
template of ELF program header
Definition: ElfBinaries.h:615
SymbolIndexMap dynSymIndexMap
dynamic symbol's index map
Definition: ElfBinaries.h:117
uint16_t getProgramHeadersNum() const
get program headers number
Definition: ElfBinaries.h:199
Elf64_Phdr Phdr
program header
Definition: ElfBinaries.h:86
cxbyte * getSectionContent(const char *name)
get section content pointer
Definition: ElfBinaries.h:369
bool hasSymbolMap() const
returns true if object has a symbol's index map
Definition: ElfBinaries.h:144
Flags getCreationFlags() const
get creation flags
Definition: ElfBinaries.h:136
utilities for other libraries and programs
uint16_t sectionIndex
section index for which symbol is
Definition: ElfBinaries.h:637
ElfRegionType
type of Elf region
Definition: ElfBinaries.h:385
uint32_t SectionFlags
section flags
Definition: ElfBinaries.h:67
const cxbyte * getBinaryCode() const
get binary code
Definition: ElfBinaries.h:164
exception class
Definition: Utilities.h:58
cxbyte * sectionStringTable
pointer to section's string table
Definition: ElfBinaries.h:110
cxbyte other
other
Definition: ElfBinaries.h:639
const Types::Phdr & getProgramHeader(uint16_t index) const
get program header with specified index
Definition: ElfBinaries.h:203
Types::Word size
region size
Definition: ElfBinaries.h:486
static ElfRegionTemplate strtabSection()
get .strtab section
Definition: ElfBinaries.h:585
class ElfBinaryGenTemplate< Elf32Types > ElfBinaryGen32
type for 32-bit ELF binary generator
Definition: ElfBinaries.h:748
cxuint linkId
link section id (ELFSECTID_* or an extra section index)
Definition: ElfBinaries.h:453
uint32_t type
type
Definition: ElfBinaries.h:617
static ElfRegionTemplate dynstrSection()
get .dynstr section
Definition: ElfBinaries.h:589
cxbyte * getSectionContent(uint16_t index)
get section content pointer
Definition: ElfBinaries.h:358
Elf32_Ehdr Ehdr
ELF header.
Definition: ElfBinaries.h:68
Types::Word size
symbol size
Definition: ElfBinaries.h:642
Types::Size getSymbolsNum() const
get symbols number
Definition: ElfBinaries.h:219
SectionIndexMap sectionIndexMap
section's index map
Definition: ElfBinaries.h:115
Types::Shdr & getSectionHeader(uint16_t index)
get section header with specified index
Definition: ElfBinaries.h:191
create map of symbols
Definition: ElfBinaries.h:57
const ElfRegionContent * dataGen
content generator pointer
Definition: ElfBinaries.h:491
cxuint sectionId
section id (ELFSECTID_* or an extra section index)
Definition: ElfBinaries.h:464
static const cxbyte ELFCLASS
ELF class.
Definition: ElfBinaries.h:72
ElfSymbolTemplate< Elf32Types > ElfSymbol32
32-bit elf symbol
Definition: ElfBinaries.h:673
class ElfBinaryTemplate< Elf32Types > ElfBinary32
type for 32-bit ELF binary
Definition: ElfBinaries.h:380
ElfRegionTemplate(typename Types::Word _size, const ElfRegionContent *contentGen, typename Types::Word _align)
constructor for user region with content generator
Definition: ElfBinaries.h:512
function class that returns true if first C string is less than second
Definition: Utilities.h:298
Types::Word paddrBase
paddr base
Definition: ElfBinaries.h:622
Types::SectionFlags flags
section flags
Definition: ElfBinaries.h:496
SymbolIndexMap::const_iterator getDynSymbolIter(const char *name) const
get dynamic symbol iterator with specified name (requires dynamic symbol index map) ...
Definition: ElfBinaries.h:317
Definition: Elf.h:489
cxbyte * dynSymStringTable
pointer to dynamic symbol's string table
Definition: ElfBinaries.h:113
Array< std::pair< const char *, size_t > > SectionIndexMap
section index map
Definition: ElfBinaries.h:103
static const cxbyte ELFCLASS
ELF class.
Definition: ElfBinaries.h:88
ElfRegionTemplate(typename Types::Word _size, const ElfRegionContent *_data, typename Types::Word _align, const char *inName, uint32_t _type, typename Types::SectionFlags _flags, uint32_t _link=0, uint32_t _info=0, typename Types::Word _addrBase=0, typename Types::Word _entSize=0, bool _zeroOffset=false)
constructor for section with generator
Definition: ElfBinaries.h:545
uint16_t machine
machine
Definition: ElfBinaries.h:412
uint16_t type
type
Definition: ElfBinaries.h:411
Definition: Elf.h:359
Types::Sym & getDynSymbol(const char *name)
get dynamic symbol with specified name (requires dynamic symbol index map)
Definition: ElfBinaries.h:347
Types::Word value
symbol value
Definition: ElfBinaries.h:641
Elf32_Shdr Shdr
Section header.
Definition: ElfBinaries.h:69
const T * end() const
get iterator to after last element
Definition: Containers.h:250
SectionIndexMap::const_iterator getSectionIterEnd() const
get end iterator if section index map
Definition: ElfBinaries.h:276
class ElfBinaryGenTemplate< Elf64Types > ElfBinaryGen64
type for 64-bit ELF binary generator
Definition: ElfBinaries.h:750
Elf32_Phdr Phdr
program header
Definition: ElfBinaries.h:70
Definition: Elf.h:477
size_t Size
size used to return size value
Definition: ElfBinaries.h:81
bool operator!() const
returns true if object is uninitialized
Definition: ElfBinaries.h:160
uint8_t ULEV(const uint8_t &t)
convert from/to little endian value from unaligned memory
static ElfRegionTemplate dynsymSection()
get dynsym section
Definition: ElfBinaries.h:603
simple C-string container
Definition: CString.h:38
ELF 32-bit types.
Definition: ElfBinaries.h:79
containers and other utils for other libraries and programs
static const cxuint bitness
ELF bitness.
Definition: ElfBinaries.h:73
cxbyte * symbolTable
pointer to symbol table
Definition: ElfBinaries.h:112
uint64_t Word
word size in ELF
Definition: ElfBinaries.h:82
Definition: Elf.h:87
ElfRegionTemplate(typename Types::Word _size, const cxbyte *_data, typename Types::Word _align)
constructor for user region
Definition: ElfBinaries.h:505
ElfRegionTemplate(const BinSection &binSection, const uint16_t *builtinSections, cxuint maxBuiltinSection, cxuint startExtraIndex)
constructor for external section (BinSection)
Definition: ElfBinaries.h:562
Definition: Elf.h:254
cxuint regionStart
number of first region which is in program header data
Definition: ElfBinaries.h:619