cprover
Loading...
Searching...
No Matches
elf_reader.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Read ELF
4
5Author:
6
7\*******************************************************************/
8
11
12#include "elf_reader.h"
14
15#include <istream>
16
18{
19 // read 32-bit header
20 in.read(
21 reinterpret_cast<char*>(&elf32_header),
22 sizeof(elf32_header));
23
24 if(!in)
25 throw deserialization_exceptiont("failed to read ELF header");
26
27 if(elf32_header.e_ident[0]!=0x7f ||
28 elf32_header.e_ident[1]!='E' ||
29 elf32_header.e_ident[2]!='L' ||
30 elf32_header.e_ident[3]!='F')
31 throw deserialization_exceptiont("ELF header malformed (magic)");
32
34
35 if(elf_class==ELF32)
36 {
37 const auto ei_data = elf32_header.e_ident[5];
38
39 if(ei_data==1)
40 little_endian=true;
41 else if(ei_data==2)
42 little_endian=false;
43 else
44 throw deserialization_exceptiont("ELF32 header malformed (EI_DATA)");
45
47 throw deserialization_exceptiont("unknown ELF32 version");
48
49 // get offset for section header
50 if(elf32_header.e_shoff==0 ||
52 throw deserialization_exceptiont("ELF32 without section header");
53
56
57 // iterate over these
58 for(std::size_t i=0; i<elf32_section_header_table.size(); i++)
59 {
60 // go to right place
62
63 // read section header
64 in.read(
65 reinterpret_cast<char*>(&elf32_section_header_table[i]),
66 sizeof(Elf32_Shdr));
67 }
68
69 // string table
72 throw deserialization_exceptiont("ELF32 without string table");
73
75 }
76 else if(elf_class==ELF64)
77 {
78 // read 64-bit header
79 in.seekg(0);
80 in.read(
81 reinterpret_cast<char*>(&elf64_header),
82 sizeof(elf64_header));
83
84 const auto ei_data = elf64_header.e_ident[5];
85
86 if(ei_data==1)
87 little_endian=true;
88 else if(ei_data==2)
89 little_endian=false;
90 else
91 throw deserialization_exceptiont("ELF64 header malformed (EI_DATA)");
92
94 throw deserialization_exceptiont("unknown ELF64 version");
95
96 // get offset for section header
97 if(elf64_header.e_shoff==0 ||
99 throw deserialization_exceptiont("ELF64 without section header");
100
103
104 // iterate over these
105 for(std::size_t i=0; i<elf64_section_header_table.size(); i++)
106 {
107 // go to right place
109
110 // read section header
111 in.read(
112 reinterpret_cast<char*>(&elf64_section_header_table[i]),
113 sizeof(Elf64_Shdr));
114 }
115
116 // string table
119 throw deserialization_exceptiont("ELF64 without string table");
120
122 }
123}
124
125std::string elf_readert::get_string(std::streampos index) const
126{
127 in.seekg(string_table_offset+index);
128
129 std::string result;
130
131 while(in)
132 {
133 char ch;
134 in.read(&ch, 1);
135 if(ch==0)
136 break;
137 result+=ch;
138 }
139
140 return result;
141}
142
143bool elf_readert::has_section(const std::string &name) const
144{
145 for(std::size_t i=0; i<number_of_sections; i++)
146 if(section_name(i)==name)
147 return true;
148
149 return false;
150}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:564
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
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)
std::string get_string(std::streampos index) const
elf64_section_header_tablet elf64_section_header_table
Definition elf_reader.h:119
std::istream & in
Definition elf_reader.h:154
bool has_section(const std::string &name) const
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
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
Read ELF.
Elf32_Off e_shoff
Definition elf_reader.h:44
Elf32_Half e_shnum
Definition elf_reader.h:50
Elf32_Half e_shstrndx
Definition elf_reader.h:51
unsigned char e_ident[16]
Definition elf_reader.h:38
Elf32_Word e_version
Definition elf_reader.h:41
Elf32_Half e_shentsize
Definition elf_reader.h:49
Elf64_Half e_shentsize
Definition elf_reader.h:67
unsigned char e_ident[16]
Definition elf_reader.h:56
Elf64_Half e_shnum
Definition elf_reader.h:68
Elf64_Word e_version
Definition elf_reader.h:59
Elf64_Off e_shoff
Definition elf_reader.h:62
Elf64_Half e_shstrndx
Definition elf_reader.h:69