CBMC
elf_reader.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Read ELF
4 
5 Author:
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_GOTO_PROGRAMS_ELF_READER_H
13 #define CPROVER_GOTO_PROGRAMS_ELF_READER_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <vector>
18 
19 // we follow
20 // http://www.skyfree.org/linux/references/ELF_Format.pdf
21 // http://downloads.openwatcom.org/ftp/devel/docs/elf-64-gen.pdf
22 
23 typedef unsigned short Elf32_Half; // 2 NOLINT(readability/identifiers)
24 typedef unsigned int Elf32_Word; // 4 NOLINT(readability/identifiers)
25 typedef unsigned int Elf32_Off; // 4 NOLINT(readability/identifiers)
26 typedef unsigned int Elf32_Addr; // 4 NOLINT(readability/identifiers)
27 
28 typedef unsigned short Elf64_Half; // 2 NOLINT(readability/identifiers)
29 typedef unsigned int Elf64_Word; // 4 NOLINT(readability/identifiers)
30 typedef unsigned long long int Elf64_Xword; // 8 NOLINT(readability/identifiers)
31 typedef unsigned long long int Elf64_Off; // 8 NOLINT(readability/identifiers)
32 typedef unsigned long long int Elf64_Addr; // 8 NOLINT(readability/identifiers)
33 
34 #define EI_NIDENT 16
35 
36 typedef struct
37 {
38  unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
39  Elf32_Half e_type; /* Object file type */
40  Elf32_Half e_machine; /* Architecture */
41  Elf32_Word e_version; /* Object file version */
42  Elf32_Addr e_entry; /* Entry point virtual address */
43  Elf32_Off e_phoff; /* Program header table file offset */
44  Elf32_Off e_shoff; /* Section header table file offset */
45  Elf32_Word e_flags; /* Processor-specific flags */
46  Elf32_Half e_ehsize; /* ELF header size in bytes */
47  Elf32_Half e_phentsize; /* Program header table entry size */
48  Elf32_Half e_phnum; /* Program header table entry count */
49  Elf32_Half e_shentsize; /* Section header table entry size */
50  Elf32_Half e_shnum; /* Section header table entry count */
51  Elf32_Half e_shstrndx; /* Section header string table index */
52 } Elf32_Ehdr;
53 
54 typedef struct
55 {
56  unsigned char e_ident[16]; /* ELF identification */
57  Elf64_Half e_type; /* Object file type */
58  Elf64_Half e_machine; /* Machine type */
59  Elf64_Word e_version; /* Object file version */
60  Elf64_Addr e_entry; /* Entry point address */
61  Elf64_Off e_phoff; /* Program header offset */
62  Elf64_Off e_shoff; /* Section header offset */
63  Elf64_Word e_flags; /* Processor-specific flags */
64  Elf64_Half e_ehsize; /* ELF header size */
65  Elf64_Half e_phentsize; /* Size of program header entry */
66  Elf64_Half e_phnum; /* Number of program header entries */
67  Elf64_Half e_shentsize; /* Size of section header entry */
68  Elf64_Half e_shnum; /* Number of section header entries */
69  Elf64_Half e_shstrndx; /* Section name string table index */
70 } Elf64_Ehdr;
71 
72 typedef struct
73 {
74  Elf32_Word sh_name; /* Section name (string tbl index) */
75  Elf32_Word sh_type; /* Section type */
76  Elf32_Word sh_flags; /* Section flags */
77  Elf32_Addr sh_addr; /* Section virtual addr at execution */
78  Elf32_Off sh_offset; /* Section file offset */
79  Elf32_Word sh_size; /* Section size in bytes */
80  Elf32_Word sh_link; /* Link to another section */
81  Elf32_Word sh_info; /* Additional section information */
82  Elf32_Word sh_addralign; /* Section alignment */
83  Elf32_Word sh_entsize; /* Entry size if section holds table */
84 } Elf32_Shdr;
85 
86 typedef struct
87 {
88  Elf64_Word sh_name; /* Section name */
89  Elf64_Word sh_type; /* Section type */
90  Elf64_Xword sh_flags; /* Section attributes */
91  Elf64_Addr sh_addr; /* Virtual address in memory */
92  Elf64_Off sh_offset; /* Offset in file */
93  Elf64_Xword sh_size; /* Size of section */
94  Elf64_Word sh_link; /* Link to other section */
95  Elf64_Word sh_info; /* Miscellaneous information */
96  Elf64_Xword sh_addralign; /* Address alignment boundary */
97  Elf64_Xword sh_entsize; /* Size of entries, if section has table */
98 } Elf64_Shdr;
99 
101 {
102 public:
103  explicit elf_readert(std::istream &_in);
104 
105  enum elf_classt { ELF32=1, ELF64=2 };
107 
108  // the ELF header
111 
113 
114  // section header table
115  typedef std::vector<Elf32_Shdr> elf32_section_header_tablet;
117 
118  typedef std::vector<Elf64_Shdr> elf64_section_header_tablet;
120 
121  // string table
122  std::streampos string_table_offset;
123  std::string get_string(std::streampos index) const;
124 
125  std::string elf32_section_name(std::size_t index) const
126  {
127  return get_string(elf32_section_header_table[index].sh_name);
128  }
129 
130  std::string elf64_section_name(std::size_t index) const
131  {
132  return get_string(elf64_section_header_table[index].sh_name);
133  }
134 
135  std::size_t number_of_sections;
136 
137  std::string section_name(std::size_t index) const
138  {
139  return
141  elf64_section_name(index);
142  }
143 
144  std::streampos section_offset(std::size_t index) const
145  {
146  return
147  elf_class==ELF32?elf32_section_header_table[index].sh_offset:
148  elf64_section_header_table[index].sh_offset;
149  }
150 
151  bool has_section(const std::string &name) const;
152 
153 protected:
154  std::istream &in;
155 };
156 
157 #endif // CPROVER_GOTO_PROGRAMS_ELF_READER_H
std::size_t number_of_sections
Definition: elf_reader.h:135
Elf64_Ehdr elf64_header
Definition: elf_reader.h:110
Elf32_Ehdr elf32_header
Definition: elf_reader.h:109
elf_readert(std::istream &_in)
Definition: elf_reader.cpp:52
std::vector< Elf32_Shdr > elf32_section_header_tablet
Definition: elf_reader.h:115
std::string get_string(std::streampos index) const
Definition: elf_reader.cpp:227
std::string elf64_section_name(std::size_t index) const
Definition: elf_reader.h:130
elf64_section_header_tablet elf64_section_header_table
Definition: elf_reader.h:119
std::vector< Elf64_Shdr > elf64_section_header_tablet
Definition: elf_reader.h:118
std::istream & in
Definition: elf_reader.h:154
bool has_section(const std::string &name) const
Definition: elf_reader.cpp:245
elf32_section_header_tablet elf32_section_header_table
Definition: elf_reader.h:116
std::string section_name(std::size_t index) const
Definition: elf_reader.h:137
std::streampos string_table_offset
Definition: elf_reader.h:122
std::string elf32_section_name(std::size_t index) const
Definition: elf_reader.h:125
bool little_endian
Definition: elf_reader.h:112
elf_classt elf_class
Definition: elf_reader.h:106
std::streampos section_offset(std::size_t index) const
Definition: elf_reader.h:144
unsigned short Elf32_Half
Definition: elf_reader.h:23
unsigned short Elf64_Half
Definition: elf_reader.h:28
unsigned long long int Elf64_Addr
Definition: elf_reader.h:32
unsigned int Elf64_Word
Definition: elf_reader.h:29
unsigned int Elf32_Word
Definition: elf_reader.h:24
unsigned int Elf32_Addr
Definition: elf_reader.h:26
#define EI_NIDENT
Definition: elf_reader.h:34
unsigned long long int Elf64_Xword
Definition: elf_reader.h:30
unsigned int Elf32_Off
Definition: elf_reader.h:25
unsigned long long int Elf64_Off
Definition: elf_reader.h:31
Elf32_Off e_shoff
Definition: elf_reader.h:44
Elf32_Half e_ehsize
Definition: elf_reader.h:46
Elf32_Half e_shnum
Definition: elf_reader.h:50
Elf32_Half e_machine
Definition: elf_reader.h:40
Elf32_Off e_phoff
Definition: elf_reader.h:43
Elf32_Half e_phnum
Definition: elf_reader.h:48
Elf32_Half e_shstrndx
Definition: elf_reader.h:51
Elf32_Half e_type
Definition: elf_reader.h:39
Elf32_Word e_flags
Definition: elf_reader.h:45
Elf32_Word e_version
Definition: elf_reader.h:41
Elf32_Half e_shentsize
Definition: elf_reader.h:49
Elf32_Addr e_entry
Definition: elf_reader.h:42
Elf32_Half e_phentsize
Definition: elf_reader.h:47
Elf32_Word sh_entsize
Definition: elf_reader.h:83
Elf32_Word sh_flags
Definition: elf_reader.h:76
Elf32_Word sh_addralign
Definition: elf_reader.h:82
Elf32_Off sh_offset
Definition: elf_reader.h:78
Elf32_Word sh_name
Definition: elf_reader.h:74
Elf32_Addr sh_addr
Definition: elf_reader.h:77
Elf32_Word sh_size
Definition: elf_reader.h:79
Elf32_Word sh_type
Definition: elf_reader.h:75
Elf32_Word sh_link
Definition: elf_reader.h:80
Elf32_Word sh_info
Definition: elf_reader.h:81
Elf64_Half e_type
Definition: elf_reader.h:57
Elf64_Half e_shentsize
Definition: elf_reader.h:67
Elf64_Half e_shnum
Definition: elf_reader.h:68
Elf64_Word e_version
Definition: elf_reader.h:59
Elf64_Half e_ehsize
Definition: elf_reader.h:64
Elf64_Off e_shoff
Definition: elf_reader.h:62
Elf64_Addr e_entry
Definition: elf_reader.h:60
Elf64_Half e_phentsize
Definition: elf_reader.h:65
Elf64_Off e_phoff
Definition: elf_reader.h:61
Elf64_Half e_machine
Definition: elf_reader.h:58
Elf64_Word e_flags
Definition: elf_reader.h:63
Elf64_Half e_shstrndx
Definition: elf_reader.h:69
Elf64_Half e_phnum
Definition: elf_reader.h:66
Elf64_Word sh_name
Definition: elf_reader.h:88
Elf64_Xword sh_flags
Definition: elf_reader.h:90
Elf64_Word sh_link
Definition: elf_reader.h:94
Elf64_Word sh_type
Definition: elf_reader.h:89
Elf64_Word sh_info
Definition: elf_reader.h:95
Elf64_Xword sh_entsize
Definition: elf_reader.h:97
Elf64_Xword sh_size
Definition: elf_reader.h:93
Elf64_Xword sh_addralign
Definition: elf_reader.h:96
Elf64_Addr sh_addr
Definition: elf_reader.h:91
Elf64_Off sh_offset
Definition: elf_reader.h:92