|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "elf_file_header.h" |
|
17 #include <stdexcept> |
|
18 #include <string> |
|
19 |
|
20 |
|
21 namespace elf |
|
22 { |
|
23 File_header::File_header(const char* p1, const char* p2, bool a_pedantic) |
|
24 : m_data( reinterpret_cast<const Elf32_Ehdr*>(p1) ) |
|
25 { |
|
26 if ( p1 + sizeof(Elf32_Ehdr) > p2 ) |
|
27 { |
|
28 throw std::runtime_error("not an ELF object; file too small"); |
|
29 } |
|
30 |
|
31 uint8_t m0 = m_data->e_ident[0]; |
|
32 uint8_t m1 = m_data->e_ident[1]; |
|
33 uint8_t m2 = m_data->e_ident[2]; |
|
34 uint8_t m3 = m_data->e_ident[3]; |
|
35 |
|
36 if (m0 != 0x7f || m1 != 'E' || m2 != 'L' || m3 != 'F') |
|
37 { |
|
38 throw std::runtime_error("not an ELF object; bad magic"); |
|
39 } |
|
40 |
|
41 if (a_pedantic) // Do some extra checks for correctness. |
|
42 { |
|
43 //if (m_data->e_ident[4] != 1) |
|
44 //{ |
|
45 // throw std::runtime_error("invalid class; this program can only handle 32-bit objects"); |
|
46 //} |
|
47 |
|
48 //if (m_data->e_ident[5] != 1) |
|
49 //{ |
|
50 // throw std::runtime_error("invalid encoding; this program can only handle little-endian objects"); |
|
51 //} |
|
52 |
|
53 //for (unsigned i = EI_PAD; i < sizeof(m_data->e_ident); ++i) |
|
54 //{ |
|
55 // if (m_data->e_ident[i] != 0) |
|
56 // { |
|
57 // throw std::runtime_error("non-zero pading"); |
|
58 // } |
|
59 //} |
|
60 |
|
61 //if (m_data->e_type == ET_NONE) |
|
62 //{ |
|
63 // throw std::runtime_error("invalid type"); |
|
64 //} |
|
65 |
|
66 //if (m_data->e_phoff == 0 && m_data->e_shoff == 0) |
|
67 //{ |
|
68 // throw std::runtime_error("no program headers and no section headers"); |
|
69 //} |
|
70 |
|
71 //if ( m_data->e_phoff + m_data->e_phentsize * m_data->e_phnum > p2 - p1 + 1 ) |
|
72 //{ |
|
73 // throw std::runtime_error("file too small for header table"); |
|
74 //} |
|
75 |
|
76 //if (m_data->e_shoff + m_data->e_shentsize * m_data->e_shnum > a_mem.get_size() ) |
|
77 //{ |
|
78 // throw std::runtime_error("file too small for header table"); |
|
79 //} |
|
80 |
|
81 //if ( m_data->e_shentsize != sizeof(Elf32_Shdr) ) |
|
82 //{ |
|
83 // throw std::runtime_error("e_shentsize doesn't match the actual size of the type"); |
|
84 //} |
|
85 |
|
86 //if (m_data->e_shoff > 0 && m_data->e_shnum == 0) |
|
87 //{ |
|
88 // throw std::runtime_error("this program can't handle the combination e_shoff > 0 and e_shnum == 0"); |
|
89 //} |
|
90 |
|
91 //if (m_data->e_shstrndx >= 0xff00) |
|
92 //{ |
|
93 // throw std::runtime_error("this program can only handle e_shstrndx < 0xff00"); |
|
94 //} |
|
95 |
|
96 } // if (a_pedantic) |
|
97 } |
|
98 |
|
99 // The following functions are obvious candidates for inlining ... |
|
100 |
|
101 // e_type |
|
102 |
|
103 bool File_header::is_type_rel() const |
|
104 { |
|
105 return (m_data->e_type == ET_REL); |
|
106 } |
|
107 bool File_header::is_type_exec() const |
|
108 { |
|
109 return (m_data->e_type == ET_EXEC); |
|
110 } |
|
111 bool File_header::is_type_dyn() const |
|
112 { |
|
113 return (m_data->e_type == ET_DYN); |
|
114 } |
|
115 |
|
116 // e_machine |
|
117 |
|
118 bool File_header::is_machine_none() const |
|
119 { |
|
120 return (m_data->e_machine == EM_NONE); |
|
121 } |
|
122 bool File_header::is_machine_arm() const |
|
123 { |
|
124 return (m_data->e_machine == EM_ARM); |
|
125 } |
|
126 bool File_header::is_machine_386() const |
|
127 { |
|
128 return (m_data->e_machine == EM_386); |
|
129 } |
|
130 |
|
131 uint32_t File_header::get_entry() const |
|
132 { |
|
133 return m_data->e_entry; |
|
134 } |
|
135 |
|
136 uint32_t File_header::get_phoff() const |
|
137 { |
|
138 return m_data->e_phoff; |
|
139 } |
|
140 |
|
141 uint32_t File_header::get_shoff() const |
|
142 { |
|
143 return m_data->e_shoff; |
|
144 } |
|
145 |
|
146 uint32_t File_header::get_flags() const |
|
147 { |
|
148 return m_data->e_flags; |
|
149 } |
|
150 |
|
151 uint16_t File_header::get_ehsize() const |
|
152 { |
|
153 return m_data->e_ehsize; |
|
154 } |
|
155 |
|
156 uint16_t File_header::get_phentsize() const |
|
157 { |
|
158 return m_data->e_phentsize; |
|
159 } |
|
160 |
|
161 uint16_t File_header::get_phnum() const |
|
162 { |
|
163 return m_data->e_phnum; |
|
164 } |
|
165 |
|
166 uint16_t File_header::get_shentsize() const |
|
167 { |
|
168 return m_data->e_shentsize; |
|
169 } |
|
170 |
|
171 uint16_t File_header::get_shnum() const |
|
172 { |
|
173 return m_data->e_shnum; |
|
174 } |
|
175 |
|
176 uint16_t File_header::get_shstrndx() const |
|
177 { |
|
178 return m_data->e_shstrndx; |
|
179 } |
|
180 |
|
181 } // namespace elf |
|
182 |