1 | /* |
---|
2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | /** |
---|
17 | * @file |
---|
18 | * |
---|
19 | * @ingroup rtems-ld |
---|
20 | * |
---|
21 | * @brief RTEMS Linker ELF module manages the libelf interface. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #if !defined (_RLD_ELF_H_) |
---|
26 | #define _RLD_ELF_H_ |
---|
27 | |
---|
28 | #include <list> |
---|
29 | |
---|
30 | #include <rld.h> |
---|
31 | |
---|
32 | namespace rld |
---|
33 | { |
---|
34 | namespace elf |
---|
35 | { |
---|
36 | /** |
---|
37 | * Forward decl. |
---|
38 | */ |
---|
39 | class file; |
---|
40 | |
---|
41 | /** |
---|
42 | * An ELF Section. |
---|
43 | */ |
---|
44 | class section |
---|
45 | { |
---|
46 | public: |
---|
47 | /** |
---|
48 | * Construct the section getting the details. |
---|
49 | * |
---|
50 | * @param elf The ELF file this section is part of. |
---|
51 | * @param index The sections index in the ELF file. |
---|
52 | */ |
---|
53 | section (file& file_, int index); |
---|
54 | |
---|
55 | /** |
---|
56 | * Copy constructor. |
---|
57 | */ |
---|
58 | section (const section& orig); |
---|
59 | |
---|
60 | /** |
---|
61 | * Default constructor. |
---|
62 | */ |
---|
63 | section (); |
---|
64 | |
---|
65 | /** |
---|
66 | * The section's index in the ELF file. |
---|
67 | * |
---|
68 | * @return int The section number. |
---|
69 | */ |
---|
70 | int index () const; |
---|
71 | |
---|
72 | /** |
---|
73 | * The name of the section. |
---|
74 | * |
---|
75 | * @return const std::string& The section's name. |
---|
76 | */ |
---|
77 | const std::string& name () const; |
---|
78 | |
---|
79 | /** |
---|
80 | * The section's data. |
---|
81 | */ |
---|
82 | elf_data* data (); |
---|
83 | |
---|
84 | /** |
---|
85 | * Get the type of the section. |
---|
86 | */ |
---|
87 | elf_word type () const; |
---|
88 | |
---|
89 | /** |
---|
90 | * The section flags. |
---|
91 | */ |
---|
92 | elf_xword flags () const; |
---|
93 | |
---|
94 | /** |
---|
95 | * In-memory address of the section. |
---|
96 | */ |
---|
97 | elf_addr address () const; |
---|
98 | |
---|
99 | /** |
---|
100 | * Alignment constraint. |
---|
101 | */ |
---|
102 | elf_xword alignment () const; |
---|
103 | |
---|
104 | /** |
---|
105 | * The file offset of the section. |
---|
106 | */ |
---|
107 | elf_off offset () const; |
---|
108 | |
---|
109 | /** |
---|
110 | * The header table link. |
---|
111 | */ |
---|
112 | elf_word link () const; |
---|
113 | |
---|
114 | /** |
---|
115 | * Extra information. |
---|
116 | */ |
---|
117 | elf_word info () const; |
---|
118 | |
---|
119 | /** |
---|
120 | * Size of the section. |
---|
121 | */ |
---|
122 | elf_xword size () const; |
---|
123 | |
---|
124 | /** |
---|
125 | * Size of the entries in the section. |
---|
126 | */ |
---|
127 | elf_xword entry_size () const; |
---|
128 | |
---|
129 | /** |
---|
130 | * Number of entries. |
---|
131 | */ |
---|
132 | int entries () const; |
---|
133 | |
---|
134 | private: |
---|
135 | |
---|
136 | /** |
---|
137 | * Check the section is acrtual valid. |
---|
138 | */ |
---|
139 | void check () const; |
---|
140 | |
---|
141 | file* file_; //< The ELF file. |
---|
142 | int index_; //< The section header index. |
---|
143 | std::string name_; //< The section's name. |
---|
144 | elf_scn* scn; //< ELF private section data. |
---|
145 | elf_shdr shdr; //< The section header. |
---|
146 | elf_data* data_; //< The section's data. |
---|
147 | }; |
---|
148 | |
---|
149 | /** |
---|
150 | * Container of ELF sections. |
---|
151 | */ |
---|
152 | typedef std::list < section > sections; |
---|
153 | |
---|
154 | /** |
---|
155 | * An ELF file. |
---|
156 | */ |
---|
157 | class file |
---|
158 | { |
---|
159 | public: |
---|
160 | /** |
---|
161 | * Construct an ELF file. |
---|
162 | */ |
---|
163 | file (); |
---|
164 | |
---|
165 | /** |
---|
166 | * Destruct the ELF file object. |
---|
167 | */ |
---|
168 | ~file (); |
---|
169 | |
---|
170 | /** |
---|
171 | * Begin using the ELF file. |
---|
172 | * |
---|
173 | * @param name The full name of the file. |
---|
174 | * @param fd The file descriptor to read or write the file. |
---|
175 | * @param writable The file is writeable. The default is false. |
---|
176 | */ |
---|
177 | void begin (const std::string& name, int fd, const bool writable = false); |
---|
178 | |
---|
179 | /** |
---|
180 | * Begin using the ELF file in an archive. |
---|
181 | * |
---|
182 | * @param name The full name of the file. |
---|
183 | * @param archive The file that is the archive. |
---|
184 | * @param offset The offset of the ELF file in the archive. |
---|
185 | */ |
---|
186 | void begin (const std::string& name, file& archive, off_t offset); |
---|
187 | |
---|
188 | /** |
---|
189 | * End using the ELF file. |
---|
190 | */ |
---|
191 | void end (); |
---|
192 | |
---|
193 | /** |
---|
194 | * Load the header. Done automatically. |
---|
195 | */ |
---|
196 | void load_header (); |
---|
197 | |
---|
198 | /** |
---|
199 | * Get the machine type. |
---|
200 | */ |
---|
201 | unsigned int machinetype () const; |
---|
202 | |
---|
203 | /** |
---|
204 | * Get the type of ELF file. |
---|
205 | */ |
---|
206 | unsigned int type () const; |
---|
207 | |
---|
208 | /** |
---|
209 | * Get the class of the object file. |
---|
210 | */ |
---|
211 | unsigned int object_class () const; |
---|
212 | |
---|
213 | /** |
---|
214 | * Get the data type, ie LSB or MSB. |
---|
215 | */ |
---|
216 | unsigned int data_type () const; |
---|
217 | |
---|
218 | /** |
---|
219 | * Is the file an archive format file ? |
---|
220 | */ |
---|
221 | bool is_archive () const; |
---|
222 | |
---|
223 | /** |
---|
224 | * Is the file an executable ? |
---|
225 | */ |
---|
226 | bool is_executable () const; |
---|
227 | |
---|
228 | /** |
---|
229 | * Is the file relocatable ? |
---|
230 | */ |
---|
231 | bool is_relocatable() const; |
---|
232 | |
---|
233 | /** |
---|
234 | * The number of sections in the file. |
---|
235 | */ |
---|
236 | int section_count () const; |
---|
237 | |
---|
238 | /** |
---|
239 | * Load the sections. |
---|
240 | */ |
---|
241 | void load_sections (); |
---|
242 | |
---|
243 | /** |
---|
244 | * Get a filtered container of the sections. The key is the section |
---|
245 | * type. If the sections are not loaded they are loaded. If the type is 0 |
---|
246 | * all sections are returned. |
---|
247 | * |
---|
248 | * @param filtered_secs The container the copy of the filtered sections |
---|
249 | * are placed in. |
---|
250 | * @param type The type of sections to filter on. If 0 all sections are |
---|
251 | * matched. |
---|
252 | */ |
---|
253 | void get_sections (sections& filtered_secs, unsigned int type); |
---|
254 | |
---|
255 | /** |
---|
256 | * Return the index of the string section. |
---|
257 | */ |
---|
258 | int strings_section () const; |
---|
259 | |
---|
260 | /** |
---|
261 | * Get the string from the specified section at the requested offset. |
---|
262 | * |
---|
263 | * @param section The section to search for the string. |
---|
264 | * @param offset The offset in the string section. |
---|
265 | * @return std::string The string. |
---|
266 | */ |
---|
267 | std::string get_string (int section, size_t offset); |
---|
268 | |
---|
269 | /** |
---|
270 | * Get the string from the ELF header declared string section at the |
---|
271 | * requested offset. |
---|
272 | * |
---|
273 | * @param offset The offset in the string section. |
---|
274 | * @return std::string The string. |
---|
275 | */ |
---|
276 | std::string get_string (size_t offset); |
---|
277 | |
---|
278 | /** |
---|
279 | * Load the symbols. |
---|
280 | */ |
---|
281 | void load_symbols (); |
---|
282 | |
---|
283 | /** |
---|
284 | * Get a filtered container of symbols given the various types. If the |
---|
285 | * symbols are not loaded they are loaded. |
---|
286 | * |
---|
287 | * @param filter_syms The filtered symbols found in the file. This is a |
---|
288 | * container of pointers. |
---|
289 | * @param local Return local symbols. |
---|
290 | * @param weak Return weak symbols. |
---|
291 | * @param global Return global symbols. |
---|
292 | * @param unresolved Return unresolved symbols. |
---|
293 | */ |
---|
294 | void get_symbols (rld::symbols::pointers& filtered_syms, |
---|
295 | bool unresolved = false, |
---|
296 | bool local = false, |
---|
297 | bool weak = true, |
---|
298 | bool global = true); |
---|
299 | |
---|
300 | /** |
---|
301 | * Set the ELF header. Must be writable. |
---|
302 | * |
---|
303 | * @param type The type of ELF file, ie executable, relocatable etc. |
---|
304 | * @param machinetype The type of machine code present in the ELF file. |
---|
305 | * @param datatype The data type, ie LSB or MSB. |
---|
306 | */ |
---|
307 | void set_header (elf_half type, |
---|
308 | int class_, |
---|
309 | elf_half machinetype, |
---|
310 | unsigned char datatype); |
---|
311 | |
---|
312 | /** |
---|
313 | * Get the ELF reference. |
---|
314 | */ |
---|
315 | elf* get_elf (); |
---|
316 | |
---|
317 | /** |
---|
318 | * Get the name of the file. |
---|
319 | */ |
---|
320 | const std::string& name () const; |
---|
321 | |
---|
322 | /** |
---|
323 | * Is the file writable ? |
---|
324 | */ |
---|
325 | bool is_writable () const; |
---|
326 | |
---|
327 | private: |
---|
328 | |
---|
329 | /** |
---|
330 | * Begin using the ELF file. |
---|
331 | * |
---|
332 | * @param name The full name of the file. |
---|
333 | * @param fd The file descriptor to read or write the file. |
---|
334 | * @param writable The file is writeable. It cannot be part of an archive. |
---|
335 | * @param archive The archive's ELF handle or 0 if not an archive. |
---|
336 | * @param offset The offset of the ELF file in the archive if elf is non-zero. |
---|
337 | */ |
---|
338 | void begin (const std::string& name, |
---|
339 | int fd, |
---|
340 | const bool writable, |
---|
341 | file* archive, |
---|
342 | off_t offset); |
---|
343 | |
---|
344 | /** |
---|
345 | * Check if the file is usable. Throw an exception if not. |
---|
346 | * |
---|
347 | * @param where Where the check is performed. |
---|
348 | */ |
---|
349 | void check (const char* where) const; |
---|
350 | |
---|
351 | /** |
---|
352 | * Check if the file is usable and writable. Throw an exception if not. |
---|
353 | * |
---|
354 | * @param where Where the check is performed. |
---|
355 | */ |
---|
356 | void check_writable (const char* where) const; |
---|
357 | |
---|
358 | /** |
---|
359 | * Check if the ELF header is valid. Throw an exception if not. |
---|
360 | * |
---|
361 | * @param where Where the check is performed. |
---|
362 | */ |
---|
363 | void check_ehdr (const char* where) const; |
---|
364 | |
---|
365 | /** |
---|
366 | * Check if the ELF program header is valid. Throw an exception if not. |
---|
367 | * |
---|
368 | * @param where Where the check is performed. |
---|
369 | */ |
---|
370 | void check_phdr (const char* where) const; |
---|
371 | |
---|
372 | /** |
---|
373 | * Generate libelf error. |
---|
374 | * |
---|
375 | * @param where Where the error is generated. |
---|
376 | */ |
---|
377 | void error (const char* where) const; |
---|
378 | |
---|
379 | int fd_; //< The file handle. |
---|
380 | std::string name_; //< The name of the file. |
---|
381 | bool archive; //< The ELF file is part of an archive. |
---|
382 | bool writable; //< The file is writeable. |
---|
383 | elf* elf_; //< The ELF handle. |
---|
384 | unsigned int mtype; //< The machine type. |
---|
385 | unsigned int oclass; //< The object class. |
---|
386 | const char* ident_str; //< The ELF file's ident string. |
---|
387 | size_t ident_size; //< The size of the ident. |
---|
388 | elf_ehdr* ehdr; //< The ELF header. |
---|
389 | elf_phdr* phdr; //< The ELF program header. |
---|
390 | std::string stab; //< The string table. |
---|
391 | sections secs; //< The sections. |
---|
392 | rld::symbols::bucket symbols; //< The symbols. All tables point here. |
---|
393 | }; |
---|
394 | |
---|
395 | /** |
---|
396 | * Return the machine type label given the machine type. |
---|
397 | * |
---|
398 | * @param machinetype The ELF machine type. |
---|
399 | */ |
---|
400 | const std::string machine_type (unsigned int machinetype); |
---|
401 | |
---|
402 | /** |
---|
403 | * Return the global machine type set by the check_file call. |
---|
404 | */ |
---|
405 | const std::string machine_type (); |
---|
406 | |
---|
407 | /** |
---|
408 | * Check the file against the global machine type, object class and data |
---|
409 | * type. If this is the first file checked it becomes the default all |
---|
410 | * others are checked against. This is a simple way to make sure all files |
---|
411 | * are the same type. |
---|
412 | * |
---|
413 | * @param file The check to check. |
---|
414 | */ |
---|
415 | void check_file(const file& file); |
---|
416 | |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | #endif |
---|