CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
ElfBinaries.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2017 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 enum: uint64_t {
55  BINGEN64_DEFAULT = UINT64_MAX,
56  BINGEN64_NOTSUPPLIED = UINT64_MAX-1
57 };
58 
59 enum: uint16_t {
60  BINGEN16_DEFAULT = UINT16_MAX,
61  BINGEN16_NOTSUPPLIED = UINT16_MAX-1
62 };
63 
64 enum: uint8_t {
65  BINGEN8_DEFAULT = UINT8_MAX,
66  BINGEN8_NOTSUPPLIED = UINT8_MAX-1
67 };
68 
69 enum : Flags {
74 };
75 
77 class BinException: public Exception
78 {
79 public:
81  BinException() = default;
83  explicit BinException(const std::string& message);
85  virtual ~BinException() noexcept = default;
86 };
87 
90 {
91 public:
93  BinGenException() = default;
95  explicit BinGenException(const std::string& message);
97  virtual ~BinGenException() noexcept = default;
98 };
99 
102 {
103  typedef uint32_t Size;
104  typedef uint32_t Word;
105  typedef uint32_t SectionFlags;
106  typedef Elf32_Ehdr Ehdr;
107  typedef Elf32_Shdr Shdr;
108  typedef Elf32_Phdr Phdr;
109  typedef Elf32_Sym Sym;
110  typedef Elf32_Nhdr Nhdr;
111  typedef Elf32_Dyn Dyn;
112  typedef Elf32_Rel Rel;
113  typedef Elf32_Rela Rela;
114  static const cxbyte ELFCLASS;
115  static const cxuint bitness;
116  static const char* bitName;
117  static const Word nobase = Word(0)-1;
118 };
119 
122 {
123  typedef size_t Size;
124  typedef uint64_t Word;
125  typedef uint64_t SectionFlags;
126  typedef Elf64_Ehdr Ehdr;
127  typedef Elf64_Shdr Shdr;
128  typedef Elf64_Phdr Phdr;
129  typedef Elf64_Sym Sym;
130  typedef Elf64_Nhdr Nhdr;
131  typedef Elf64_Dyn Dyn;
132  typedef Elf64_Rel Rel;
133  typedef Elf64_Rela Rela;
134  static const cxbyte ELFCLASS;
135  static const cxuint bitness;
136  static const char* bitName;
137  static const Word nobase = Word(0)-1;
138 };
139 
141 
144 template<typename Types>
146 {
147 public:
152 protected:
154  size_t binaryCodeSize;
163  SectionIndexMap sectionIndexMap;
164  SymbolIndexMap symbolIndexMap;
165  SymbolIndexMap dynSymIndexMap;
166 
167  typename Types::Size symbolsNum;
168  typename Types::Size dynSymbolsNum;
169  typename Types::Size noteTableSize;
170  typename Types::Size dynamicsNum;
171  uint16_t symbolEntSize;
172  uint16_t dynSymEntSize;
173  typename Types::Size dynamicEntSize;
174 
175 public:
182  ElfBinaryTemplate(size_t binaryCodeSize, cxbyte* binaryCode,
183  Flags creationFlags = ELF_CREATE_ALL);
184  virtual ~ElfBinaryTemplate();
185 
188  { return creationFlags; }
189 
191  bool hasSectionMap() const
192  { return (creationFlags & ELF_CREATE_SECTIONMAP) != 0; }
193 
195  bool hasSymbolMap() const
196  { return (creationFlags & ELF_CREATE_SYMBOLMAP) != 0; }
197 
199  bool hasDynSymbolMap() const
200  { return (creationFlags & ELF_CREATE_DYNSYMMAP) != 0; }
201 
203  size_t getSize() const
204  { return binaryCodeSize; }
205 
207  operator bool() const
208  { return binaryCode!=nullptr; }
209 
211  bool operator!() const
212  { return binaryCode==nullptr; }
213 
215  const cxbyte* getBinaryCode() const
216  { return binaryCode; }
219  { return binaryCode; }
220 
222  const typename Types::Ehdr& getHeader() const
223  { return *reinterpret_cast<const typename Types::Ehdr*>(binaryCode); }
224 
226  typename Types::Ehdr& getHeader()
227  { return *reinterpret_cast<typename Types::Ehdr*>(binaryCode); }
228 
230  uint16_t getSectionHeadersNum() const
231  { return ULEV(getHeader().e_shnum); }
232 
234  const typename Types::Shdr& getSectionHeader(uint16_t index) const
235  {
236  const typename Types::Ehdr& ehdr = getHeader();
237  return *reinterpret_cast<const typename Types::Shdr*>(binaryCode +
238  ULEV(ehdr.e_shoff) + size_t(ULEV(ehdr.e_shentsize))*index);
239  }
240 
242  typename Types::Shdr& getSectionHeader(uint16_t index)
243  {
244  const typename Types::Ehdr& ehdr = getHeader();
245  return *reinterpret_cast<typename Types::Shdr*>(binaryCode +
246  ULEV(ehdr.e_shoff) + size_t(ULEV(ehdr.e_shentsize))*index);
247  }
248 
250  uint16_t getProgramHeadersNum() const
251  { return ULEV(getHeader().e_phnum); }
252 
254  const typename Types::Phdr& getProgramHeader(uint16_t index) const
255  {
256  const typename Types::Ehdr& ehdr = getHeader();
257  return *reinterpret_cast<const typename Types::Phdr*>(binaryCode +
258  ULEV(ehdr.e_phoff) + size_t(ULEV(ehdr.e_phentsize))*index);
259  }
260 
262  typename Types::Phdr& getProgramHeader(uint16_t index)
263  {
264  const typename Types::Ehdr& ehdr = getHeader();
265  return *reinterpret_cast<typename Types::Phdr*>(binaryCode +
266  ULEV(ehdr.e_phoff) + size_t(ULEV(ehdr.e_phentsize))*index);
267  }
268 
270  typename Types::Size getSymbolsNum() const
271  { return symbolsNum; }
272 
274  typename Types::Size getDynSymbolsNum() const
275  { return dynSymbolsNum; }
276 
278  const typename Types::Sym& getSymbol(typename Types::Size index) const
279  {
280  return *reinterpret_cast<const typename Types::Sym*>(symbolTable +
281  size_t(index)*symbolEntSize);
282  }
283 
285  typename Types::Sym& getSymbol(typename Types::Size index)
286  {
287  return *reinterpret_cast<typename Types::Sym*>(
288  symbolTable + size_t(index)*symbolEntSize);
289  }
290 
292  const typename Types::Sym& getDynSymbol(typename Types::Size index) const
293  {
294  return *reinterpret_cast<const typename Types::Sym*>(dynSymTable +
295  size_t(index)*dynSymEntSize);
296  }
297 
299  typename Types::Sym& getDynSymbol(typename Types::Size index)
300  {
301  return *reinterpret_cast<typename Types::Sym*>(dynSymTable +
302  size_t(index)*dynSymEntSize);
303  }
304 
306  const char* getSymbolName(typename Types::Size index) const
307  {
308  const typename Types::Sym& sym = getSymbol(index);
309  return reinterpret_cast<const char*>(symbolStringTable + ULEV(sym.st_name));
310  }
311 
313  const char* getDynSymbolName(typename Types::Size index) const
314  {
315  const typename Types::Sym& sym = getDynSymbol(index);
316  return reinterpret_cast<const char*>(dynSymStringTable + ULEV(sym.st_name));
317  }
318 
320  const char* getSectionName(uint16_t index) const
321  {
322  const typename Types::Shdr& section = getSectionHeader(index);
323  return reinterpret_cast<const char*>(sectionStringTable + ULEV(section.sh_name));
324  }
325 
328  { return sectionIndexMap.end(); }
329 
332  {
334  sectionIndexMap.begin(), sectionIndexMap.end(), name, CStringLess());
335  if (it == sectionIndexMap.end())
336  throw BinException(std::string("Can't find Elf")+Types::bitName+" Section");
337  return it;
338  }
339 
341  uint16_t getSectionIndex(const char* name) const;
342 
344  typename Types::Size getSymbolIndex(const char* name) const;
345 
347  typename Types::Size getDynSymbolIndex(const char* name) const;
348 
351  { return symbolIndexMap.end(); }
352 
355  { return dynSymIndexMap.end(); }
356 
359  {
361  symbolIndexMap.begin(), symbolIndexMap.end(), name, CStringLess());
362  if (it == symbolIndexMap.end())
363  throw BinException(std::string("Can't find Elf")+Types::bitName+" Symbol");
364  return it;
365  }
366 
369  {
371  dynSymIndexMap.begin(), dynSymIndexMap.end(), name, CStringLess());
372  if (it == dynSymIndexMap.end())
373  throw BinException(std::string("Can't find Elf")+Types::bitName+" DynSymbol");
374  return it;
375  }
376 
378  const typename Types::Shdr& getSectionHeader(const char* name) const
379  { return getSectionHeader(getSectionIndex(name)); }
380 
382  typename Types::Shdr& getSectionHeader(const char* name)
383  { return getSectionHeader(getSectionIndex(name)); }
384 
386  const typename Types::Sym& getSymbol(const char* name) const
387  { return getSymbol(getSymbolIndex(name)); }
388 
390  typename Types::Sym& getSymbol(const char* name)
391  { return getSymbol(getSymbolIndex(name)); }
392 
394  const typename Types::Sym& getDynSymbol(const char* name) const
395  { return getDynSymbol(getDynSymbolIndex(name)); }
396 
398  typename Types::Sym& getDynSymbol(const char* name)
399  { return getDynSymbol(getDynSymbolIndex(name)); }
400 
402  const typename Types::Nhdr* getNotes() const
403  { return reinterpret_cast<typename Types::Nhdr*>(noteTable); }
404 
406  typename Types::Nhdr* getNotes()
407  { return reinterpret_cast<typename Types::Nhdr*>(noteTable); }
408 
410  typename Types::Size getNotesSize() const
411  { return noteTableSize; }
412 
414  const typename Types::Size getDynamicsNum() const
415  { return dynamicsNum; }
416 
418  const typename Types::Size getDynamicEntrySize() const
419  { return dynamicEntSize; }
420 
422  const typename Types::Dyn* getDynamicTable() const
423  { return reinterpret_cast<const typename Types::Dyn*>(dynamicTable); }
424 
426  typename Types::Dyn* getDynamicTable()
427  { return reinterpret_cast<typename Types::Dyn*>(dynamicTable); }
428 
430  const cxbyte* getSectionContent(uint16_t index) const
431  {
432  const typename Types::Shdr& shdr = getSectionHeader(index);
433  return binaryCode + ULEV(shdr.sh_offset);
434  }
435 
437  cxbyte* getSectionContent(uint16_t index)
438  {
439  typename Types::Shdr& shdr = getSectionHeader(index);
440  return binaryCode + ULEV(shdr.sh_offset);
441  }
442 
444  const cxbyte* getSectionContent(const char* name) const
445  { return getSectionContent(getSectionIndex(name)); }
446 
448  cxbyte* getSectionContent(const char* name)
449  { return getSectionContent(getSectionIndex(name)); }
450 };
451 
452 extern template class ElfBinaryTemplate<Elf32Types>;
453 extern template class ElfBinaryTemplate<Elf64Types>;
454 
456 extern bool isElfBinary(size_t binarySize, const cxbyte* binary);
457 
462 
465 {
466  PHDR_TABLE,
467  SHDR_TABLE,
468  SECTION,
469  USER
470 };
471 
474 {
475 public:
476  virtual ~ElfRegionContent();
477 
479  virtual void operator()(FastOutputBuffer& fob) const = 0;
480 };
481 
483 template<typename Types>
485 {
486  typename Types::Word paddrBase;
487  typename Types::Word vaddrBase;
490  uint16_t type;
491  uint16_t machine;
492  uint32_t version;
494  typename Types::Word entry;
495  uint32_t flags;
496 };
497 
502 
503 
504 enum: cxuint {
505  ELFSECTID_START = UINT_MAX-255,
506  ELFSECTID_SHSTRTAB = ELFSECTID_START,
507  ELFSECTID_STRTAB,
508  ELFSECTID_SYMTAB,
509  ELFSECTID_DYNSTR,
510  ELFSECTID_DYNSYM,
511  ELFSECTID_TEXT,
512  ELFSECTID_RODATA,
513  ELFSECTID_DATA,
514  ELFSECTID_BSS,
515  ELFSECTID_COMMENT,
516  ELFSECTID_STD_MAX = ELFSECTID_COMMENT,
517  ELFSECTID_OTHER_BUILTIN = ELFSECTID_STD_MAX+1,
518  ELFSECTID_NULL = UINT_MAX-2,
519  ELFSECTID_ABS = UINT_MAX-1,
520  ELFSECTID_UNDEF = UINT_MAX
521 };
522 
525 {
527  size_t size;
528  const cxbyte* data;
529  uint64_t align;
530  uint32_t type;
531  uint64_t flags;
533  uint32_t info;
534  size_t entSize;
535 };
536 
538 struct BinSymbol
539 {
541  uint64_t value;
542  uint64_t size;
544  bool valueIsAddr;
547 };
548 
550 extern uint16_t convertSectionId(cxuint sectionIndex, const uint16_t* builtinSections,
551  cxuint maxBuiltinSection, cxuint extraSectionIndex);
552 
554 template<typename Types>
556 {
558 
561 
563 
565  typename Types::Word size;
566  typename Types::Word align;
567  union
568  {
569  const cxbyte* data;
571  };
572  struct {
573  const char* name;
574  uint32_t type;
575  typename Types::SectionFlags flags;
576  uint32_t link;
577  uint32_t info;
578  typename Types::Word addrBase;
579  typename Types::Word entSize;
580  bool zeroOffset;
581  typename Types::Word align;
582  } section;
583 
584  ElfRegionTemplate() : type(ElfRegionType::USER), dataFromPointer(false), size(0),
585  align(0), data(0)
586  { }
588  ElfRegionTemplate(typename Types::Word _size,
589  const cxbyte* _data, typename Types::Word _align)
590  : type(ElfRegionType::USER), dataFromPointer(true), size(_size),
591  align(_align), data(_data)
592  { }
593 
595  ElfRegionTemplate(typename Types::Word _size,
596  const ElfRegionContent* contentGen, typename Types::Word _align)
597  : type(ElfRegionType::USER), dataFromPointer(false), size(_size),
598  align(_align), dataGen(contentGen)
599  { }
600 
602  ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size,
603  const cxbyte* _data, typename Types::Word _align)
604  : type(_type), dataFromPointer(true), size(_size),
605  align(_align), data(_data)
606  { }
607 
609  ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size,
610  const ElfRegionContent* contentGen, typename Types::Word _align)
611  : type(_type), dataFromPointer(false), size(_size),
612  align(_align), dataGen(contentGen)
613  { }
614 
616  ElfRegionTemplate(typename Types::Word _size, const cxbyte* _data,
617  typename Types::Word _align, const char* _name, uint32_t _type,
618  typename Types::SectionFlags _flags, uint32_t _link = 0, uint32_t _info = 0,
619  typename Types::Word _addrBase = 0,
620  typename Types::Word _entSize = 0, bool _zeroOffset = false,
621  typename Types::Word _sectAlign = 0)
622  : type(ElfRegionType::SECTION), dataFromPointer(true), size(_size),
623  align(_align), data(_data)
624  {
625  section = {_name, _type, _flags, _link, _info, _addrBase,
626  _entSize, _zeroOffset, _sectAlign};
627  }
628 
630  ElfRegionTemplate(typename Types::Word _size, const ElfRegionContent* _data,
631  typename Types::Word _align, const char* inName, uint32_t _type,
632  typename Types::SectionFlags _flags, uint32_t _link = 0, uint32_t _info = 0,
633  typename Types::Word _addrBase = 0,
634  typename Types::Word _entSize = 0, bool _zeroOffset = false,
635  typename Types::Word _sectAlign = 0)
636  : type(ElfRegionType::SECTION), dataFromPointer(false), size(_size),
637  align(_align), dataGen(_data)
638  {
639  section = {inName, _type, _flags, _link, _info, _addrBase,
640  _entSize, _zeroOffset, _sectAlign};
641  }
643 
649  ElfRegionTemplate(const BinSection& binSection, const uint16_t* builtinSections,
650  cxuint maxBuiltinSection, cxuint startExtraIndex)
651  : type(ElfRegionType::SECTION), dataFromPointer(true), size(binSection.size),
652  align(binSection.align), data(binSection.data)
653  {
654  section = { binSection.name.c_str(), binSection.type,
655  typename Types::SectionFlags(binSection.flags),
656  uint32_t(convertSectionId(binSection.linkId, builtinSections,
657  maxBuiltinSection, startExtraIndex)),
658  binSection.info, 0, typename Types::Word(binSection.entSize), 0 };
659  }
660 
663  { return ElfRegionTemplate(ElfRegionType::PHDR_TABLE, 0, (const cxbyte*)nullptr,
664  sizeof(typename Types::Word)); }
665 
668  { return ElfRegionTemplate(ElfRegionType::SHDR_TABLE, 0, (const cxbyte*)nullptr,
669  sizeof(typename Types::Word)); }
670 
673  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 1, ".strtab", SHT_STRTAB, 0); }
674 
677  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB,
678  SHF_ALLOC); }
679 
682  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 1, ".shstrtab", SHT_STRTAB, 0); }
683 
686  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
687  ".symtab", SHT_SYMTAB, 0); }
688 
691  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
692  ".dynsym", SHT_DYNSYM, SHF_ALLOC); }
693 
695  static ElfRegionTemplate hashSection(uint16_t link)
696  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
697  ".hash", SHT_HASH, SHF_ALLOC, link); }
698 
701  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, 4, ".note", SHT_NOTE, 0); }
702 
704  static ElfRegionTemplate dynamicSection(uint16_t link)
705  { return ElfRegionTemplate(0, (const cxbyte*)nullptr, sizeof(typename Types::Word),
706  ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE, link); }
707 };
708 
710 struct ElfNote
711 {
712  const char* name;
713  size_t descSize;
714  const cxbyte* desc;
715  uint32_t type;
716 };
717 
722 
723 enum: cxuint {
724  PHREGION_FILESTART = UINT_MAX
725 };
727 template<typename Types>
729 {
730  uint32_t type;
731  uint32_t flags;
734  bool haveMemSize;
735  typename Types::Word paddrBase;
736  typename Types::Word vaddrBase;
737  typename Types::Word memSize;
738  typename Types::Word align;
739 };
740 
745 
747 template<typename Types>
749 {
750  const char* name;
751  uint16_t sectionIndex;
754  bool valueIsAddr;
755  typename Types::Word value;
756  typename Types::Word size;
757 
758  ElfSymbolTemplate() : name(nullptr), sectionIndex(0), info(0), other(0),
759  valueIsAddr(false), value(0), size(0)
760  { }
761 
763  ElfSymbolTemplate(const char* _name, uint16_t _sectionIndex,
764  cxbyte _info, cxbyte _other, bool _valueIsAddr,
765  typename Types::Word _value, typename Types::Word _size)
766  : name(_name), sectionIndex(_sectionIndex), info(_info), other(_other),
767  valueIsAddr(_valueIsAddr), value(_value), size(_size)
768  { }
770 
776  ElfSymbolTemplate(const BinSymbol& binSymbol, const uint16_t* builtinSections,
777  cxuint maxBuiltinSection, cxuint startExtraIndex)
778  {
779  name = binSymbol.name.c_str();
780  sectionIndex = convertSectionId(binSymbol.sectionId, builtinSections,
781  maxBuiltinSection, startExtraIndex);
782  info = binSymbol.info;
783  other = binSymbol.other;
784  valueIsAddr = binSymbol.valueIsAddr;
785  value = binSymbol.value;
786  size = binSymbol.size;
787  }
788 };
789 
794 
796 template<typename Types>
798 {
799 private:
800  bool sizeComputed;
801  bool addNullSym, addNullDynSym;
802  bool addNullSection;
803  cxuint addrStartRegion;
804  uint16_t shStrTab, strTab, dynStr;
805  cxuint shdrTabRegion, phdrTabRegion;
806  uint16_t sectionsNum;
807  typename Types::Word size;
809  std::vector<ElfRegionTemplate<Types> > regions;
810  std::unique_ptr<typename Types::Word[]> regionOffsets;
811  std::unique_ptr<typename Types::Word[]> regionAddresses;
812  std::unique_ptr<cxuint[]> sectionRegions;
813  std::vector<ElfProgramHeaderTemplate<Types> > progHeaders;
814  std::vector<ElfSymbolTemplate<Types> > symbols;
815  std::vector<ElfSymbolTemplate<Types> > dynSymbols;
816  std::vector<ElfNote> notes;
817  std::vector<int32_t> dynamics;
818  std::unique_ptr<typename Types::Word[]> dynamicValues;
819  uint32_t bucketsNum;
820  std::unique_ptr<uint32_t[]> hashCodes;
821  bool isHashDynSym;
822 
823  void computeSize();
824 public:
827 
834  explicit ElfBinaryGenTemplate(const ElfHeaderTemplate<Types>& header,
835  bool addNullSym = true, bool addNullDynSym = true,
836  bool addNullSection = true, cxuint addrCountingFromRegion = 0);
837 
840  { this->header = header; }
841 
843  void addRegion(const ElfRegionTemplate<Types>& region);
845  void addProgramHeader(const ElfProgramHeaderTemplate<Types>& progHeader);
846 
849  { symbols.push_back(symbol); }
852  { dynSymbols.push_back(symbol); }
854  void addNote(const ElfNote& note)
855  { notes.push_back(note); }
857  void addDynamic(int32_t dynamicTag)
858  { dynamics.push_back(dynamicTag); }
860  void addDynamics(size_t dynamicsNum, const int32_t* dynTags)
861  { dynamics.insert(dynamics.end(), dynTags, dynTags + dynamicsNum); }
862 
864  uint64_t countSize();
865 
867  void generate(FastOutputBuffer& fob);
868 
870  void generate(std::ostream& os)
871  {
872  FastOutputBuffer fob(256, os);
873  generate(fob);
874  }
875 };
876 
877 extern template class ElfBinaryGenTemplate<Elf32Types>;
878 extern template class ElfBinaryGenTemplate<Elf64Types>;
879 
884 
885 };
886 
887 #endif
const char * getDynSymbolName(typename Types::Size index) const
get dynamic symbol name with specified index
Definition: ElfBinaries.h:313
Types::Word memSize
size in memory
Definition: ElfBinaries.h:737
const Types::Sym & getDynSymbol(typename Types::Size index) const
get dynamic symbol with specified index
Definition: ElfBinaries.h:292
const Types::Shdr & getSectionHeader(uint16_t index) const
get section header with specified index
Definition: ElfBinaries.h:234
uint32_t link
section link
Definition: ElfBinaries.h:576
ELF note structure.
Definition: ElfBinaries.h:710
ElfSymbolTemplate(const BinSymbol &binSymbol, const uint16_t *builtinSections, cxuint maxBuiltinSection, cxuint startExtraIndex)
constructor for extra symbol
Definition: ElfBinaries.h:776
uint32_t Word
word size in ELF
Definition: ElfBinaries.h:104
SymbolIndexMap::const_iterator getDynSymbolIterEnd() const
get end iterator of dynamic symbol index map
Definition: ElfBinaries.h:354
Elf64_Sym Sym
symbol header
Definition: ElfBinaries.h:129
program header table
void addDynamic(int32_t dynamicTag)
add dynamic
Definition: ElfBinaries.h:857
Elf64_Ehdr Ehdr
ELF header.
Definition: ElfBinaries.h:126
const cxbyte * data
data content
Definition: ElfBinaries.h:528
cxbyte info
info
Definition: ElfBinaries.h:545
Types::Shdr & getSectionHeader(const char *name)
get section header with specified name
Definition: ElfBinaries.h:382
ELF binary class.
Definition: ElfBinaries.h:145
Types::Word paddrBase
physical address base
Definition: ElfBinaries.h:486
Types::Size noteTableSize
size of note table
Definition: ElfBinaries.h:169
Definition: Elf.h:73
uint32_t type
section type
Definition: ElfBinaries.h:530
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
Definition: Elf.h:244
const Types::Ehdr & getHeader() const
get ELF binary header
Definition: ElfBinaries.h:222
const T * begin() const
get iterator to first element
Definition: Containers.h:250
Elf32_Nhdr Nhdr
note header
Definition: ElfBinaries.h:110
if set in field then field has been ignored
Definition: ElfBinaries.h:56
Types::Word vaddrBase
vaddr base
Definition: ElfBinaries.h:736
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:386
size_t binaryCodeSize
binary code size
Definition: ElfBinaries.h:154
ElfRegionType type
type of region
Definition: ElfBinaries.h:557
fast and direct output buffer
Definition: InputOutput.h:350
const char * c_str() const
return C-style string pointer
Definition: CString.h:252
uint32_t info
section info
Definition: ElfBinaries.h:577
template of ElfRegion
Definition: ElfBinaries.h:555
static ElfRegionTemplate shstrtabSection()
get .shstrtab section
Definition: ElfBinaries.h:681
uint64_t flags
section flags
Definition: ElfBinaries.h:531
elf header template
Definition: ElfBinaries.h:484
void addDynSymbol(const ElfSymbolTemplate< Types > &symbol)
add dynamic symbol
Definition: ElfBinaries.h:851
Types::Sym & getSymbol(typename Types::Size index)
get symbol with specified index
Definition: ElfBinaries.h:285
uint64_t value
symbol value
Definition: ElfBinaries.h:541
const Types::Sym & getDynSymbol(const char *name) const
get dynamic symbol with specified name (requires dynamic symbol index map)
Definition: ElfBinaries.h:394
Elf32_Rela Rela
relocation with addend
Definition: ElfBinaries.h:113
uint64_t size
symbol size
Definition: ElfBinaries.h:542
const cxbyte * desc
description
Definition: ElfBinaries.h:714
const Types::Dyn * getDynamicTable() const
get dynamic table
Definition: ElfBinaries.h:422
bool hasSectionMap() const
returns true if object has a section&#39;s index map
Definition: ElfBinaries.h:191
bool valueIsAddr
true if value should be treats as address
Definition: ElfBinaries.h:754
ELF 32-bit types.
Definition: ElfBinaries.h:101
ElfRegionTemplate< Elf32Types > ElfRegion32
32-bit region (for 32-bit elf)
Definition: ElfBinaries.h:719
Definition: Elf.h:353
Elf64_Rela Rela
relocation with addend
Definition: ElfBinaries.h:133
Binary generator exception class.
Definition: ElfBinaries.h:89
section header table
bool haveMemSize
true if program header has memory size
Definition: ElfBinaries.h:734
class ElfBinaryTemplate< Elf64Types > ElfBinary64
type for 64-bit ELF binary
Definition: ElfBinaries.h:461
Types::Nhdr * getNotes()
get note table
Definition: ElfBinaries.h:406
Elf64_Rel Rel
relocation
Definition: ElfBinaries.h:132
ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size, const cxbyte *_data, typename Types::Word _align)
constructor for region
Definition: ElfBinaries.h:602
cxbyte * binaryCode
pointer to binary code
Definition: ElfBinaries.h:155
static ElfRegionTemplate sectionHeaderTable()
get program header table region
Definition: ElfBinaries.h:667
SymbolIndexMap::const_iterator getSymbolIterEnd() const
get end iterator of symbol index map
Definition: ElfBinaries.h:350
symbol structure to external usage (fo example in the binary generator input)
Definition: ElfBinaries.h:538
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, typename Types::Word _sectAlign=0)
constructor for section with generator
Definition: ElfBinaries.h:630
cxbyte info
info
Definition: ElfBinaries.h:752
Types::Size dynSymbolsNum
dynamic symbols number
Definition: ElfBinaries.h:168
Types::Word addrBase
section address base
Definition: ElfBinaries.h:578
Elf64_Nhdr Nhdr
note header
Definition: ElfBinaries.h:130
bool dataFromPointer
true if content from pointer, otherwise will be generated from class
Definition: ElfBinaries.h:560
CString name
name of section
Definition: ElfBinaries.h:526
uint64_t align
region alignment
Definition: ElfBinaries.h:529
Elf64_Shdr Shdr
Section header.
Definition: ElfBinaries.h:127
Definition: Elf.h:455
Array< std::pair< const char *, size_t > > SymbolIndexMap
symbol index map
Definition: ElfBinaries.h:151
static ElfRegionTemplate programHeaderTable()
get program header table region
Definition: ElfBinaries.h:662
const cxbyte * getSectionContent(uint16_t index) const
get section content pointer
Definition: ElfBinaries.h:430
Types::Size symbolsNum
symbols number
Definition: ElfBinaries.h:167
Elf32_Dyn Dyn
dynamic entry
Definition: ElfBinaries.h:111
Types::Size dynamicEntSize
get dynamic entry size
Definition: ElfBinaries.h:173
an array class
Definition: Containers.h:38
Elf32_Sym Sym
symbol header
Definition: ElfBinaries.h:109
Types::Word entSize
entries size
Definition: ElfBinaries.h:579
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:489
Configuration header.
uint32_t version
version
Definition: ElfBinaries.h:492
static ElfRegionTemplate symtabSection()
get symtab section
Definition: ElfBinaries.h:685
ElfHeaderTemplate< Elf64Types > ElfHeader64
64-bit elf header
Definition: ElfBinaries.h:501
create map of dynamic symbols
Definition: ElfBinaries.h:72
cxbyte * getBinaryCode()
get binary code
Definition: ElfBinaries.h:218
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:870
const Types::Sym & getSymbol(const char *name) const
get symbol with specified name (requires symbol index map)
Definition: ElfBinaries.h:386
Types::Phdr & getProgramHeader(uint16_t index)
get program header with specified index
Definition: ElfBinaries.h:262
Definition: Elf.h:462
CString name
name
Definition: ElfBinaries.h:540
Types::Sym & getSymbol(const char *name)
get symbol with specified name (requires symbol index map)
Definition: ElfBinaries.h:390
static ElfRegionTemplate dynamicSection(uint16_t link)
get dynamic
Definition: ElfBinaries.h:704
static ElfRegionTemplate hashSection(uint16_t link)
get hash section
Definition: ElfBinaries.h:695
const Types::Size getDynamicsNum() const
get dynamic entries number
Definition: ElfBinaries.h:414
Definition: Elf.h:609
size_t descSize
description size
Definition: ElfBinaries.h:713
ELF binary generator.
Definition: ElfBinaries.h:797
uint32_t info
section info
Definition: ElfBinaries.h:533
Definition: Elf.h:436
const char * getSymbolName(typename Types::Size index) const
get symbol name with specified index
Definition: ElfBinaries.h:306
uint32_t type
section type
Definition: ElfBinaries.h:574
section structure to external usage (for example in the binary generator input)
Definition: ElfBinaries.h:524
Types::Dyn * getDynamicTable()
get dynamic table
Definition: ElfBinaries.h:426
const Types::Sym & getSymbol(typename Types::Size index) const
get symbol with specified index
Definition: ElfBinaries.h:278
void addNote(const ElfNote &note)
add note
Definition: ElfBinaries.h:854
void addSymbol(const ElfSymbolTemplate< Types > &symbol)
add symbol
Definition: ElfBinaries.h:848
uint16_t getSectionHeadersNum() const
get section headers number
Definition: ElfBinaries.h:230
static ElfRegionTemplate noteSection()
get note section
Definition: ElfBinaries.h:700
cxbyte * symbolStringTable
pointer to symbol&#39;s string table
Definition: ElfBinaries.h:157
Types::Sym & getDynSymbol(typename Types::Size index)
get dynamic symbol with specified index
Definition: ElfBinaries.h:299
elf region content generator for elf region
Definition: ElfBinaries.h:473
const char * name
name
Definition: ElfBinaries.h:750
cxuint regionsNum
number of regions whose is in program header data
Definition: ElfBinaries.h:733
cxbyte osABI
os abi
Definition: ElfBinaries.h:488
const char * getSectionName(uint16_t index) const
get section name with specified index
Definition: ElfBinaries.h:320
ElfSymbolTemplate< Elf64Types > ElfSymbol64
64-bit elf symbol
Definition: ElfBinaries.h:793
ElfRegionTemplate(ElfRegionType _type, typename Types::Word _size, const ElfRegionContent *contentGen, typename Types::Word _align)
constructor for region with content generator
Definition: ElfBinaries.h:609
Types::Size getNotesSize() const
get size of notes in bytes
Definition: ElfBinaries.h:410
ElfRegionTemplate< Elf64Types > ElfRegion64
64-bit region (for 64-bit elf)
Definition: ElfBinaries.h:721
const char * name
section name
Definition: ElfBinaries.h:573
input output utilities
static const char * bitName
bitness name
Definition: ElfBinaries.h:116
SectionIndexMap::const_iterator getSectionIter(const char *name) const
get section iterator with specified name (requires section index map)
Definition: ElfBinaries.h:331
Types::Size dynamicsNum
get dynamic entries number
Definition: ElfBinaries.h:170
const Types::Shdr & getSectionHeader(const char *name) const
get section header with specified name
Definition: ElfBinaries.h:378
SymbolIndexMap symbolIndexMap
symbol&#39;s index map
Definition: ElfBinaries.h:164
ElfProgramHeaderTemplate< Elf64Types > ElfProgramHeader64
64-bit elf program header
Definition: ElfBinaries.h:744
SymbolIndexMap::const_iterator getSymbolIter(const char *name) const
get symbol iterator with specified name (requires symbol index map)
Definition: ElfBinaries.h:358
size_t entSize
entries size
Definition: ElfBinaries.h:534
const cxbyte * getSectionContent(const char *name) const
get section content pointer
Definition: ElfBinaries.h:444
unsigned char cxbyte
unsigned byte
Definition: Config.h:215
if set in field then field has been ignored
Definition: ElfBinaries.h:51
uint32_t flags
flags
Definition: ElfBinaries.h:495
void setHeader(const ElfHeaderTemplate< Types > &header)
set elf header
Definition: ElfBinaries.h:839
static const char * bitName
bitness name
Definition: ElfBinaries.h:136
Types::Ehdr & getHeader()
get ELF binary header
Definition: ElfBinaries.h:226
Flags creationFlags
creation flags holder
Definition: ElfBinaries.h:153
uint32_t flags
flags
Definition: ElfBinaries.h:731
Types::Word entry
entry offset relative to region
Definition: ElfBinaries.h:494
main namespace
Definition: AsmDefs.h:38
bool hasDynSymbolMap() const
returns true if object has a dynamic symbol&#39;s index map
Definition: ElfBinaries.h:199
uint16_t symbolEntSize
symbol entry size in a symbol&#39;s table
Definition: ElfBinaries.h:171
size_t size
size of content
Definition: ElfBinaries.h:527
uint16_t dynSymEntSize
dynamic symbol entry size in a dynamic symbol&#39;s table
Definition: ElfBinaries.h:172
static const cxuint bitness
ELF bitness.
Definition: ElfBinaries.h:135
if set in field then field has been filled later
Definition: ElfBinaries.h:55
if set in field then field has been filled later
Definition: ElfBinaries.h:50
cxbyte other
other
Definition: ElfBinaries.h:546
unsigned int cxuint
unsigned int
Definition: Config.h:223
size_t getSize() const
get size of binaries
Definition: ElfBinaries.h:203
void addDynamics(size_t dynamicsNum, const int32_t *dynTags)
add dynamic
Definition: ElfBinaries.h:860
if set in field then field has been filled later
Definition: ElfBinaries.h:65
const char * name
note name
Definition: ElfBinaries.h:712
Definition: Elf.h:552
cxuint entryRegion
region in which is entry
Definition: ElfBinaries.h:493
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, typename Types::Word _sectAlign=0)
constructor for section
Definition: ElfBinaries.h:616
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:544
uint32_t Size
size used to return size value
Definition: ElfBinaries.h:103
const Types::Nhdr * getNotes() const
get note table
Definition: ElfBinaries.h:402
uint64_t SectionFlags
section flags
Definition: ElfBinaries.h:125
ElfProgramHeaderTemplate< Elf32Types > ElfProgramHeader32
32-bit elf program header
Definition: ElfBinaries.h:742
inlines for accessing memory words in LittleEndian and unaligned
Types::Word vaddrBase
virtual address base
Definition: ElfBinaries.h:487
Definition: Elf.h:545
const cxbyte * data
content from pointer
Definition: ElfBinaries.h:569
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:763
ELF symbol template.
Definition: ElfBinaries.h:748
Types::Word align
region alignment
Definition: ElfBinaries.h:566
Types::Size getDynSymbolsNum() const
get dynamic symbols number
Definition: ElfBinaries.h:274
cxbyte * dynSymTable
pointer to dynamic symbol table
Definition: ElfBinaries.h:160
uint32_t type
type
Definition: ElfBinaries.h:715
ElfHeaderTemplate< Elf32Types > ElfHeader32
32-bit elf header
Definition: ElfBinaries.h:499
Definition: Elf.h:619
template of ELF program header
Definition: ElfBinaries.h:728
virtual ~BinException() noexcept=default
destructor
SymbolIndexMap dynSymIndexMap
dynamic symbol&#39;s index map
Definition: ElfBinaries.h:165
uint16_t getProgramHeadersNum() const
get program headers number
Definition: ElfBinaries.h:250
Elf64_Phdr Phdr
program header
Definition: ElfBinaries.h:128
cxbyte * getSectionContent(const char *name)
get section content pointer
Definition: ElfBinaries.h:448
bool hasSymbolMap() const
returns true if object has a symbol&#39;s index map
Definition: ElfBinaries.h:195
Flags getCreationFlags() const
get creation flags
Definition: ElfBinaries.h:187
if set in field then field has been ignored
Definition: ElfBinaries.h:61
utilities for other libraries and programs
uint16_t sectionIndex
section index for which symbol is
Definition: ElfBinaries.h:751
ElfRegionType
type of Elf region
Definition: ElfBinaries.h:464
create map of symbols
Definition: ElfBinaries.h:71
std::string message
message
Definition: Utilities.h:61
uint32_t SectionFlags
section flags
Definition: ElfBinaries.h:105
const cxbyte * getBinaryCode() const
get binary code
Definition: ElfBinaries.h:215
exception class
Definition: Utilities.h:58
cxbyte * sectionStringTable
pointer to section&#39;s string table
Definition: ElfBinaries.h:156
cxbyte other
other
Definition: ElfBinaries.h:753
const Types::Phdr & getProgramHeader(uint16_t index) const
get program header with specified index
Definition: ElfBinaries.h:254
Types::Word size
region size
Definition: ElfBinaries.h:565
static ElfRegionTemplate strtabSection()
get .strtab section
Definition: ElfBinaries.h:672
Elf32_Rel Rel
relocation
Definition: ElfBinaries.h:112
class ElfBinaryGenTemplate< Elf32Types > ElfBinaryGen32
type for 32-bit ELF binary generator
Definition: ElfBinaries.h:881
cxuint linkId
link section id (ELFSECTID_* or an extra section index)
Definition: ElfBinaries.h:532
uint32_t type
type
Definition: ElfBinaries.h:730
static ElfRegionTemplate dynstrSection()
get .dynstr section
Definition: ElfBinaries.h:676
cxbyte * getSectionContent(uint16_t index)
get section content pointer
Definition: ElfBinaries.h:437
Elf32_Ehdr Ehdr
ELF header.
Definition: ElfBinaries.h:106
Types::Word size
symbol size
Definition: ElfBinaries.h:756
Types::Size getSymbolsNum() const
get symbols number
Definition: ElfBinaries.h:270
SectionIndexMap sectionIndexMap
section&#39;s index map
Definition: ElfBinaries.h:163
Bin exception class.
Definition: ElfBinaries.h:77
Types::Shdr & getSectionHeader(uint16_t index)
get section header with specified index
Definition: ElfBinaries.h:242
creation flags for ELF binaries
Definition: ElfBinaries.h:73
Definition: Elf.h:447
const ElfRegionContent * dataGen
content generator pointer
Definition: ElfBinaries.h:570
cxuint sectionId
section id (ELFSECTID_* or an extra section index)
Definition: ElfBinaries.h:543
static const cxbyte ELFCLASS
ELF class.
Definition: ElfBinaries.h:114
ElfSymbolTemplate< Elf32Types > ElfSymbol32
32-bit elf symbol
Definition: ElfBinaries.h:791
class ElfBinaryTemplate< Elf32Types > ElfBinary32
type for 32-bit ELF binary
Definition: ElfBinaries.h:459
ElfRegionTemplate(typename Types::Word _size, const ElfRegionContent *contentGen, typename Types::Word _align)
constructor for user region with content generator
Definition: ElfBinaries.h:595
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:735
Types::SectionFlags flags
section flags
Definition: ElfBinaries.h:575
SymbolIndexMap::const_iterator getDynSymbolIter(const char *name) const
get dynamic symbol iterator with specified name (requires dynamic symbol index map) ...
Definition: ElfBinaries.h:368
Definition: Elf.h:493
cxbyte * dynSymStringTable
pointer to dynamic symbol&#39;s string table
Definition: ElfBinaries.h:159
Array< std::pair< const char *, size_t > > SectionIndexMap
section index map
Definition: ElfBinaries.h:149
if set in field then field has been filled later
Definition: ElfBinaries.h:60
static const cxbyte ELFCLASS
ELF class.
Definition: ElfBinaries.h:134
uint16_t machine
machine
Definition: ElfBinaries.h:491
uint16_t type
type
Definition: ElfBinaries.h:490
Definition: Elf.h:363
Types::Sym & getDynSymbol(const char *name)
get dynamic symbol with specified name (requires dynamic symbol index map)
Definition: ElfBinaries.h:398
Types::Word value
symbol value
Definition: ElfBinaries.h:755
Elf32_Shdr Shdr
Section header.
Definition: ElfBinaries.h:107
const T * end() const
get iterator to after last element
Definition: Containers.h:257
SectionIndexMap::const_iterator getSectionIterEnd() const
get end iterator if section index map
Definition: ElfBinaries.h:327
class ElfBinaryGenTemplate< Elf64Types > ElfBinaryGen64
type for 64-bit ELF binary generator
Definition: ElfBinaries.h:883
Elf32_Phdr Phdr
program header
Definition: ElfBinaries.h:108
Definition: Elf.h:481
BinException()=default
empty constructor
size_t Size
size used to return size value
Definition: ElfBinaries.h:123
Types::Word align
alignment
Definition: ElfBinaries.h:738
cxbyte * dynamicTable
pointer to dynamic table
Definition: ElfBinaries.h:162
bool operator!() const
returns true if object is uninitialized
Definition: ElfBinaries.h:211
create map of sections
Definition: ElfBinaries.h:70
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:690
simple C-string container
Definition: CString.h:38
Elf.h definitions.
if set in field then field has been ignored
Definition: ElfBinaries.h:66
ELF 32-bit types.
Definition: ElfBinaries.h:121
containers and other utils for other libraries and programs
const Types::Size getDynamicEntrySize() const
get dynamic entries size
Definition: ElfBinaries.h:418
cxbyte * noteTable
pointer to note table
Definition: ElfBinaries.h:161
static const cxuint bitness
ELF bitness.
Definition: ElfBinaries.h:115
cxbyte * symbolTable
pointer to symbol table
Definition: ElfBinaries.h:158
uint64_t Word
word size in ELF
Definition: ElfBinaries.h:124
Elf64_Dyn Dyn
dynamic entry
Definition: ElfBinaries.h:131
Definition: Elf.h:91
ElfRegionTemplate(typename Types::Word _size, const cxbyte *_data, typename Types::Word _align)
constructor for user region
Definition: ElfBinaries.h:588
ElfRegionTemplate(const BinSection &binSection, const uint16_t *builtinSections, cxuint maxBuiltinSection, cxuint startExtraIndex)
constructor for external section (BinSection)
Definition: ElfBinaries.h:649
Definition: Elf.h:258
cxuint regionStart
number of first region which is in program header data
Definition: ElfBinaries.h:732