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 file manages access the image contained in various file |
---|
22 | * formats. |
---|
23 | * |
---|
24 | * The base element is a file. It references a object file that is either |
---|
25 | * inside an archive or stand alone. You access a object file by constructing a |
---|
26 | * handle. A handle is the object file with the specific file descriptor |
---|
27 | * created when the archive or object file was opened. |
---|
28 | * |
---|
29 | * |
---|
30 | */ |
---|
31 | |
---|
32 | #if !defined (_RLD_FILES_H_) |
---|
33 | #define _RLD_FILES_H_ |
---|
34 | |
---|
35 | #include <list> |
---|
36 | #include <map> |
---|
37 | #include <string> |
---|
38 | #include <vector> |
---|
39 | |
---|
40 | #include <rld.h> |
---|
41 | |
---|
42 | namespace rld |
---|
43 | { |
---|
44 | namespace files |
---|
45 | { |
---|
46 | /** |
---|
47 | * Container of file paths. |
---|
48 | */ |
---|
49 | typedef std::vector < std::string > paths; |
---|
50 | |
---|
51 | /** |
---|
52 | * Container of files. |
---|
53 | */ |
---|
54 | typedef std::vector < file > files; |
---|
55 | |
---|
56 | /** |
---|
57 | * Container of archive files. |
---|
58 | */ |
---|
59 | typedef std::map < const std::string, archive* > archives; |
---|
60 | |
---|
61 | /** |
---|
62 | * Container of object files. |
---|
63 | */ |
---|
64 | typedef std::map < const std::string, object* > objects; |
---|
65 | |
---|
66 | /** |
---|
67 | * Container list of object files. |
---|
68 | */ |
---|
69 | typedef std::list < object* > object_list; |
---|
70 | |
---|
71 | /** |
---|
72 | * Return the basename of the file name. |
---|
73 | * |
---|
74 | * @param name The full file name. |
---|
75 | * @return std::string The basename of the file. |
---|
76 | */ |
---|
77 | std::string basename (const std::string& name); |
---|
78 | |
---|
79 | /** |
---|
80 | * Return the dirname of the file name. |
---|
81 | * |
---|
82 | * @param name The full file name. |
---|
83 | * @return std::string The dirname of the file. |
---|
84 | */ |
---|
85 | std::string dirname (const std::string& name); |
---|
86 | |
---|
87 | /** |
---|
88 | * Return the extension of the file name. |
---|
89 | * |
---|
90 | * @param name The full file name. |
---|
91 | * @return std::string The extension of the file. |
---|
92 | */ |
---|
93 | std::string extension (const std::string& name); |
---|
94 | |
---|
95 | /** |
---|
96 | * Split a path from a string with a delimiter to the path container. Add |
---|
97 | * only the paths that exist and ignore those that do not. |
---|
98 | * |
---|
99 | * @param path The paths as a single string delimited by the path |
---|
100 | * separator. |
---|
101 | * @param paths The split path paths. |
---|
102 | */ |
---|
103 | void path_split (const std::string& path, |
---|
104 | paths& paths); |
---|
105 | |
---|
106 | /** |
---|
107 | * Make a path by joining the parts with required separator. |
---|
108 | * |
---|
109 | * @param path_ The path component to be joined. |
---|
110 | * @param file_ The file name to add to the path. |
---|
111 | * @param joined The joined path and file name with a path separator. |
---|
112 | */ |
---|
113 | void path_join (const std::string& path_, |
---|
114 | const std::string& file_, |
---|
115 | std::string& joined); |
---|
116 | |
---|
117 | /** |
---|
118 | * Check the path is a file using a stat call. |
---|
119 | * |
---|
120 | * @param path The path to check. |
---|
121 | * @retval true The path is valid. |
---|
122 | * @retval false The path is not valid. |
---|
123 | */ |
---|
124 | bool check_file (const std::string& path); |
---|
125 | |
---|
126 | /** |
---|
127 | * Check if the path is a directory. |
---|
128 | * |
---|
129 | * @param path The path to check. |
---|
130 | * @retval false The path is not a directory. |
---|
131 | * @retval true The path is a directory. |
---|
132 | */ |
---|
133 | bool check_directory (const std::string& path); |
---|
134 | |
---|
135 | /** |
---|
136 | * Find the file given a container of paths and file names. |
---|
137 | * |
---|
138 | * @param path The path of the file if found else empty. |
---|
139 | * @param name The name of the file to search for. |
---|
140 | * @param search_paths The container of paths to search. |
---|
141 | */ |
---|
142 | void find_file (std::string& path, |
---|
143 | const std::string& name, |
---|
144 | paths& search_paths); |
---|
145 | |
---|
146 | /** |
---|
147 | * A file is a single object file that is either in an @ref archive or |
---|
148 | * a separate stand alone @ref object file. |
---|
149 | */ |
---|
150 | class file |
---|
151 | { |
---|
152 | public: |
---|
153 | /** |
---|
154 | * Construct the file from the component parts when part of an archive. |
---|
155 | * |
---|
156 | * @param aname The archive name. |
---|
157 | * @param oname The object file name. |
---|
158 | * @param offset The offset in the archive the object file starts. |
---|
159 | * @param size The size of the archive the object file starts. |
---|
160 | */ |
---|
161 | file (const std::string& aname, |
---|
162 | const std::string& oname, |
---|
163 | off_t offset, |
---|
164 | size_t size); |
---|
165 | |
---|
166 | /** |
---|
167 | * Construct the name by splitting the full path into an archive, |
---|
168 | * object file name and offset. |
---|
169 | * |
---|
170 | * @param path The path to the image. |
---|
171 | * @param is_object If true (default) the name is for an object file. |
---|
172 | */ |
---|
173 | file (const std::string& path, bool is_object = true); |
---|
174 | |
---|
175 | /** |
---|
176 | * Contruct an empty file. |
---|
177 | */ |
---|
178 | file (); |
---|
179 | |
---|
180 | /** |
---|
181 | * Set a name from the path. |
---|
182 | * |
---|
183 | * @param path The path to the image. |
---|
184 | * @param is_object If true (default) the name is for an object file. |
---|
185 | */ |
---|
186 | void set (const std::string& path, bool is_object = true); |
---|
187 | |
---|
188 | /** |
---|
189 | * Is an archive returns true if the file is in an archive. |
---|
190 | * |
---|
191 | * @retval true The object file is in an archive. |
---|
192 | * @retval false The object file is stand alone. |
---|
193 | */ |
---|
194 | bool is_archive () const; |
---|
195 | |
---|
196 | /** |
---|
197 | * Is object file stand alone. |
---|
198 | * |
---|
199 | * @retval true The object file is stand alone. |
---|
200 | * @retval false The object could be part of an archive. |
---|
201 | */ |
---|
202 | bool is_object () const; |
---|
203 | |
---|
204 | /** |
---|
205 | * Valid returns true if there is a valid name. |
---|
206 | * |
---|
207 | * @retval true There is a valid name. |
---|
208 | * @retval false There is not name assigned. |
---|
209 | */ |
---|
210 | bool is_valid () const; |
---|
211 | |
---|
212 | /** |
---|
213 | * Exists returns true if the archive or object file is present on disk |
---|
214 | * and a regular file. |
---|
215 | * |
---|
216 | * @retval true The file is valid and a regular file. |
---|
217 | * @retval false The file is either not present, not accessable or not a |
---|
218 | * regular file. |
---|
219 | */ |
---|
220 | bool exists () const; |
---|
221 | |
---|
222 | /** |
---|
223 | * The path maps to the real file on disk. The file may not be valid. |
---|
224 | * |
---|
225 | * @return const std::string The real path to the file on disk. |
---|
226 | */ |
---|
227 | const std::string path () const; |
---|
228 | |
---|
229 | /** |
---|
230 | * The full path. |
---|
231 | * |
---|
232 | * @return const std::string The full path to the image. |
---|
233 | */ |
---|
234 | const std::string full () const; |
---|
235 | |
---|
236 | /** |
---|
237 | * The base path. It is the basename of the full path. |
---|
238 | * |
---|
239 | * @return const std::string The basename of the full path to the image. |
---|
240 | */ |
---|
241 | const std::string basename () const; |
---|
242 | |
---|
243 | /** |
---|
244 | * The archive name component. A length of 0 means there was not |
---|
245 | * archive component. |
---|
246 | * |
---|
247 | * @return const std::string& The archive name. |
---|
248 | */ |
---|
249 | const std::string& aname () const; |
---|
250 | |
---|
251 | /** |
---|
252 | * The object name. There is always an object name. |
---|
253 | * |
---|
254 | * @return const std::string& The object name. |
---|
255 | */ |
---|
256 | const std::string& oname () const; |
---|
257 | |
---|
258 | /** |
---|
259 | * The object's offset in the archive or on disk. |
---|
260 | * |
---|
261 | * @return off_t The offset part of the file name. |
---|
262 | */ |
---|
263 | off_t offset () const; |
---|
264 | |
---|
265 | /** |
---|
266 | * The object's size in the archive. |
---|
267 | * |
---|
268 | * @return size_t The size of the file in bytes. |
---|
269 | */ |
---|
270 | size_t size () const; |
---|
271 | |
---|
272 | private: |
---|
273 | std::string aname_; //< The archive name. |
---|
274 | std::string oname_; //< The object name. |
---|
275 | off_t offset_; //< The object's offset in the archive. |
---|
276 | size_t size_; //< The object's size in the archive or on disk. |
---|
277 | }; |
---|
278 | |
---|
279 | /** |
---|
280 | * Image is the base file type and lets us have a single container to hold |
---|
281 | * the types of images we need to support. |
---|
282 | */ |
---|
283 | class image |
---|
284 | { |
---|
285 | public: |
---|
286 | /** |
---|
287 | * Construct the image. |
---|
288 | * |
---|
289 | * @param name The name of the image. |
---|
290 | */ |
---|
291 | image (file& name); |
---|
292 | |
---|
293 | /** |
---|
294 | * Construct the image. |
---|
295 | * |
---|
296 | * @param path The file path. |
---|
297 | * @param is_object If true (default) the name is for an object file. |
---|
298 | */ |
---|
299 | image (const std::string& path, bool is_object = true); |
---|
300 | |
---|
301 | /** |
---|
302 | * Construct the image. |
---|
303 | */ |
---|
304 | image (); |
---|
305 | |
---|
306 | /** |
---|
307 | * Destruct the image. |
---|
308 | */ |
---|
309 | virtual ~image (); |
---|
310 | |
---|
311 | /** |
---|
312 | * Open the image. You can open the image more than once but you need to |
---|
313 | * close it the same number of times. |
---|
314 | * |
---|
315 | * @param name The @ref file name. |
---|
316 | */ |
---|
317 | virtual void open (file& name); |
---|
318 | |
---|
319 | /** |
---|
320 | * Open the image. You can open the image more than once but you need to |
---|
321 | * close it the same number of times. |
---|
322 | * |
---|
323 | * @param writeable If true the image is open as writable. The default is |
---|
324 | * false. |
---|
325 | */ |
---|
326 | virtual void open (bool writable = false); |
---|
327 | |
---|
328 | /** |
---|
329 | * Close the image. |
---|
330 | */ |
---|
331 | virtual void close (); |
---|
332 | |
---|
333 | /** |
---|
334 | * Read a block from the file. |
---|
335 | * |
---|
336 | * @param buffer The buffer to read the data into. |
---|
337 | * @param size The amount of data to read. |
---|
338 | * @return ssize_t The amount of data read. |
---|
339 | */ |
---|
340 | virtual ssize_t read (void* buffer, size_t size); |
---|
341 | |
---|
342 | /** |
---|
343 | * Write a block from the file. |
---|
344 | * |
---|
345 | * @param buffer The buffer of data to write. |
---|
346 | * @param size The amount of data to write. |
---|
347 | * @return ssize_t The amount of data written. |
---|
348 | */ |
---|
349 | virtual ssize_t write (const void* buffer, size_t size); |
---|
350 | |
---|
351 | /** |
---|
352 | * Seek to the offset in the image. |
---|
353 | * |
---|
354 | * @param offset The offset to seek too. |
---|
355 | */ |
---|
356 | virtual void seek (off_t offset); |
---|
357 | |
---|
358 | /** |
---|
359 | * Seek and then read. |
---|
360 | * |
---|
361 | * @param offset The offset to seek too before reading. |
---|
362 | * @param buffer The buffer to read the data into. |
---|
363 | * @param size The amount of data to read. |
---|
364 | * @retval true The data requested was read. |
---|
365 | * @retval false The request data was not read. |
---|
366 | */ |
---|
367 | virtual bool seek_read (off_t offset, uint8_t* buffer, size_t size); |
---|
368 | |
---|
369 | /** |
---|
370 | * Seek and then write. |
---|
371 | * |
---|
372 | * @param offset The offset to seek too before writing. |
---|
373 | * @param buffer The buffer of data to write. |
---|
374 | * @param size The amount of data to write. |
---|
375 | * @retval true The data requested was written. |
---|
376 | * @retval false The request data was not written. |
---|
377 | */ |
---|
378 | virtual bool seek_write (off_t offset, const void* buffer, size_t size); |
---|
379 | |
---|
380 | /** |
---|
381 | * The name of the image. |
---|
382 | * |
---|
383 | * @param const file& The @ref file name of the image. |
---|
384 | */ |
---|
385 | const file& name () const; |
---|
386 | |
---|
387 | /** |
---|
388 | * References to the image. |
---|
389 | * |
---|
390 | * @return int The number of references the image has. |
---|
391 | */ |
---|
392 | virtual int references () const; |
---|
393 | |
---|
394 | /** |
---|
395 | * The file size. |
---|
396 | * |
---|
397 | * @return size_t The size of the image. |
---|
398 | */ |
---|
399 | virtual size_t size () const; |
---|
400 | |
---|
401 | /** |
---|
402 | * The file descriptor. |
---|
403 | * |
---|
404 | * @return int The operating system file descriptor handle. |
---|
405 | */ |
---|
406 | virtual int fd () const; |
---|
407 | |
---|
408 | /** |
---|
409 | * The ELF reference. |
---|
410 | * |
---|
411 | * @return elf::file& The @ref elf::file object of the image. |
---|
412 | */ |
---|
413 | elf::file& elf (); |
---|
414 | |
---|
415 | /** |
---|
416 | * A symbol in the image has been referenced. |
---|
417 | */ |
---|
418 | virtual void symbol_referenced (); |
---|
419 | |
---|
420 | /** |
---|
421 | * Return the number of symbol references. |
---|
422 | * |
---|
423 | * @return int The symbol references count. |
---|
424 | */ |
---|
425 | virtual int symbol_references () const; |
---|
426 | |
---|
427 | /** |
---|
428 | * The path maps to the real file on disk. The file may not be valid. |
---|
429 | * |
---|
430 | * @return const std::string The real path to the file on disk. |
---|
431 | */ |
---|
432 | const std::string path () const { |
---|
433 | return name ().path (); |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * Is the image open ? |
---|
438 | * |
---|
439 | * @retval true The image is open. |
---|
440 | * @retval false The image is not open. |
---|
441 | */ |
---|
442 | bool is_open () const { |
---|
443 | return fd () != -1; |
---|
444 | } |
---|
445 | |
---|
446 | /** |
---|
447 | * Is the image writable ? |
---|
448 | * |
---|
449 | * @retval true The image is writable. |
---|
450 | * @retval false The image is not writable. |
---|
451 | */ |
---|
452 | bool is_writable () const { |
---|
453 | return writable; |
---|
454 | } |
---|
455 | |
---|
456 | private: |
---|
457 | |
---|
458 | file name_; //< The name of the file. |
---|
459 | int references_; //< The number of handles open. |
---|
460 | int fd_; //< The file descriptor of the archive. |
---|
461 | elf::file elf_; //< The libelf reference. |
---|
462 | int symbol_refs; //< The number of symbols references made. |
---|
463 | bool writable; //< The image is writable. |
---|
464 | }; |
---|
465 | |
---|
466 | /** |
---|
467 | * Copy the input section of the image to the output section. The file |
---|
468 | * positions in the images must be set before making the call. |
---|
469 | * |
---|
470 | * @param in The input image. |
---|
471 | * @param out The output image. |
---|
472 | * @param size The amouint of data to copy. |
---|
473 | */ |
---|
474 | void copy (image& in, image& out, size_t size); |
---|
475 | |
---|
476 | /** |
---|
477 | * The archive class proivdes access to object files that are held in a AR |
---|
478 | * format file. GNU AR extensions are supported. The archive is a kind of |
---|
479 | * @ref image and provides the container for the @ref object's that it |
---|
480 | * contains. |
---|
481 | */ |
---|
482 | class archive: |
---|
483 | public image |
---|
484 | { |
---|
485 | public: |
---|
486 | /** |
---|
487 | * Open a archive format file that contains ELF object files. |
---|
488 | * |
---|
489 | * @param name The name of the @ref archive. |
---|
490 | */ |
---|
491 | archive (const std::string& name); |
---|
492 | |
---|
493 | /** |
---|
494 | * Close the archive. |
---|
495 | */ |
---|
496 | virtual ~archive (); |
---|
497 | |
---|
498 | /** |
---|
499 | * Begin the ELF session. |
---|
500 | */ |
---|
501 | void begin (); |
---|
502 | |
---|
503 | /** |
---|
504 | * End the ELF session. |
---|
505 | */ |
---|
506 | void end (); |
---|
507 | |
---|
508 | /** |
---|
509 | * Match the archive name. |
---|
510 | * |
---|
511 | * @param name The name of the archive to check. |
---|
512 | * @retval true The name matches. |
---|
513 | * @retval false The name does not match. |
---|
514 | */ |
---|
515 | bool is (const std::string& name) const; |
---|
516 | |
---|
517 | /** |
---|
518 | * Check this is a valid archive. |
---|
519 | * |
---|
520 | * @retval true It is a valid archive. |
---|
521 | * @retval false It is not a valid archive. |
---|
522 | */ |
---|
523 | bool is_valid (); |
---|
524 | |
---|
525 | /** |
---|
526 | * Load @ref object's from the @ref archive adding each to the provided |
---|
527 | * @ref objects container. |
---|
528 | * |
---|
529 | * @param objs The container the loaded object files are added too. |
---|
530 | */ |
---|
531 | void load_objects (objects& objs); |
---|
532 | |
---|
533 | /** |
---|
534 | * Get the name. |
---|
535 | * |
---|
536 | * @return const std::string& Return a reference to the archive's name. |
---|
537 | */ |
---|
538 | const std::string& get_name () const; |
---|
539 | |
---|
540 | /** |
---|
541 | * Less than operator for the map container. It compares the name of the |
---|
542 | * the @ref archive. |
---|
543 | * |
---|
544 | * @param rhs The right hand side of the '<' operator. |
---|
545 | * @return true The right hand side is less than this archive. |
---|
546 | * @return false The right hand side is greater than or equal to this |
---|
547 | * archive. |
---|
548 | */ |
---|
549 | bool operator< (const archive& rhs) const; |
---|
550 | |
---|
551 | /** |
---|
552 | * Create a new archive containing the given set of objects. If |
---|
553 | * referening an existing archive it is overwritten. |
---|
554 | * |
---|
555 | * @param objects The list of objects to place in the archive. |
---|
556 | */ |
---|
557 | void create (object_list& objects); |
---|
558 | |
---|
559 | private: |
---|
560 | |
---|
561 | /** |
---|
562 | * Read the archive header and check the magic number is valid. |
---|
563 | * |
---|
564 | * @param offset The offset in the file to read the header. |
---|
565 | * @param header Read the header into here. There must be enough space. |
---|
566 | * @retval true The header was read successfull and the magic number |
---|
567 | * matched. |
---|
568 | * @retval false The header could not be read from the @ref image. |
---|
569 | */ |
---|
570 | bool read_header (off_t offset, uint8_t* header); |
---|
571 | |
---|
572 | /** |
---|
573 | * Add the object file from the archive to the object's container. |
---|
574 | * |
---|
575 | * @param objs The container to add the object to. |
---|
576 | * @param name The name of the object file being added. |
---|
577 | * @param offset The offset in the @ref archive of the object file. |
---|
578 | * @param size The size of the object file. |
---|
579 | */ |
---|
580 | void add_object (objects& objs, |
---|
581 | const char* name, |
---|
582 | off_t offset, |
---|
583 | size_t size); |
---|
584 | |
---|
585 | /** |
---|
586 | * Write a file header into the archive. |
---|
587 | * |
---|
588 | * @param name The name of the archive. |
---|
589 | * @param mtime The modified time of the archive. |
---|
590 | * @param uid The user id of the archive. |
---|
591 | * @param gid The group id of the archive. |
---|
592 | * @param mode The mode of the archive. |
---|
593 | * @param size The size of the archive. |
---|
594 | */ |
---|
595 | void write_header (const std::string& name, |
---|
596 | uint32_t mtime, |
---|
597 | int uid, |
---|
598 | int gid, |
---|
599 | int mode, |
---|
600 | size_t size); |
---|
601 | |
---|
602 | /** |
---|
603 | * Cannot copy via a copy constructor. |
---|
604 | */ |
---|
605 | archive (const archive& orig); |
---|
606 | |
---|
607 | /** |
---|
608 | * Cannot assign using the assignment operator. |
---|
609 | */ |
---|
610 | archive& operator= (const archive& rhs); |
---|
611 | }; |
---|
612 | |
---|
613 | /** |
---|
614 | * A relocation record. We extract what we want because the elf::section |
---|
615 | * class requires the image be left open as references are alive. We |
---|
616 | * extract and keep the data we need to create the image. |
---|
617 | */ |
---|
618 | struct relocation |
---|
619 | { |
---|
620 | const uint32_t offset; //< The section offset. |
---|
621 | const uint32_t type; //< The type of relocation record. |
---|
622 | const uint32_t info; //< The ELF info field. |
---|
623 | const int32_t addend; //< The constant addend. |
---|
624 | const std::string symname; //< The name of the symbol. |
---|
625 | const uint32_t symtype; //< The type of symbol. |
---|
626 | const int symsect; //< The symbol's section symbol. |
---|
627 | const uint32_t symvalue; //< The symbol's value. |
---|
628 | |
---|
629 | /** |
---|
630 | * Construct from an ELF relocation record. |
---|
631 | */ |
---|
632 | relocation (const elf::relocation& er); |
---|
633 | |
---|
634 | private: |
---|
635 | /** |
---|
636 | * The default constructor is not allowed due to all elements being |
---|
637 | * const. |
---|
638 | */ |
---|
639 | relocation (); |
---|
640 | }; |
---|
641 | |
---|
642 | /** |
---|
643 | * A container of relocations. |
---|
644 | */ |
---|
645 | typedef std::list < relocation > relocations; |
---|
646 | |
---|
647 | /** |
---|
648 | * The sections attributes. We extract what we want because the |
---|
649 | * elf::section class requires the image be left open as references are |
---|
650 | * alive. We extract and keep the data we need to create the image. |
---|
651 | */ |
---|
652 | struct section |
---|
653 | { |
---|
654 | const std::string name; //< The name of the section. |
---|
655 | const int index; //< The section's index in the object file. |
---|
656 | const uint32_t type; //< The type of section. |
---|
657 | const size_t size; //< The size of the section. |
---|
658 | const uint32_t alignment; //< The alignment of the section. |
---|
659 | const uint32_t link; //< The ELF link field. |
---|
660 | const uint32_t info; //< The ELF info field. |
---|
661 | const uint32_t flags; //< The ELF flags. |
---|
662 | const off_t offset; //< The ELF file offset. |
---|
663 | bool rela; //< Relocation records have the addend field. |
---|
664 | relocations relocs; //< The sections relocations. |
---|
665 | |
---|
666 | /** |
---|
667 | * Construct from an ELF section. |
---|
668 | * |
---|
669 | * @param es The ELF section to load the object file section from. |
---|
670 | */ |
---|
671 | section (const elf::section& es); |
---|
672 | |
---|
673 | /** |
---|
674 | * Load the ELF relocations. |
---|
675 | * |
---|
676 | * @param es The ELF section to load the relocations from. |
---|
677 | */ |
---|
678 | void load_relocations (const elf::section& es); |
---|
679 | |
---|
680 | private: |
---|
681 | /** |
---|
682 | * The default constructor is not allowed due to all elements being |
---|
683 | * const. |
---|
684 | */ |
---|
685 | section (); |
---|
686 | }; |
---|
687 | |
---|
688 | /** |
---|
689 | * A container of sections. |
---|
690 | */ |
---|
691 | typedef std::list < section > sections; |
---|
692 | |
---|
693 | /** |
---|
694 | * Sum the sizes of a container of sections. |
---|
695 | */ |
---|
696 | size_t sum_sizes (const sections& secs); |
---|
697 | |
---|
698 | /** |
---|
699 | * Find the section that matches the index in the sections provided. |
---|
700 | */ |
---|
701 | const section* find (const sections& secs, const int index); |
---|
702 | |
---|
703 | /** |
---|
704 | * The object file cab be in an archive or a file. |
---|
705 | */ |
---|
706 | class object: |
---|
707 | public image |
---|
708 | { |
---|
709 | public: |
---|
710 | /** |
---|
711 | * Construct an object image that is part of an archive. |
---|
712 | * |
---|
713 | * @param archive_ The archive the object file is part of. |
---|
714 | * @param file_ The image file. |
---|
715 | */ |
---|
716 | object (archive& archive_, file& file_); |
---|
717 | |
---|
718 | /** |
---|
719 | * Construct the object file. |
---|
720 | * |
---|
721 | * @param path The object file path. |
---|
722 | */ |
---|
723 | object (const std::string& path); |
---|
724 | |
---|
725 | /** |
---|
726 | * Construct the object file. |
---|
727 | */ |
---|
728 | object (); |
---|
729 | |
---|
730 | /** |
---|
731 | * Destruct the object file. |
---|
732 | */ |
---|
733 | virtual ~object (); |
---|
734 | |
---|
735 | /** |
---|
736 | * Open the object file. |
---|
737 | */ |
---|
738 | virtual void open (bool writable = false); |
---|
739 | |
---|
740 | /** |
---|
741 | * Close the object. |
---|
742 | */ |
---|
743 | virtual void close (); |
---|
744 | |
---|
745 | /** |
---|
746 | * Begin the object file session. |
---|
747 | */ |
---|
748 | void begin (); |
---|
749 | |
---|
750 | /** |
---|
751 | * End the object file session. |
---|
752 | */ |
---|
753 | void end (); |
---|
754 | |
---|
755 | /** |
---|
756 | * If valid returns true the begin has been called and the object has |
---|
757 | * been validated as being in a suitable format. |
---|
758 | */ |
---|
759 | bool valid () const; |
---|
760 | |
---|
761 | /** |
---|
762 | * Load the symbols into the symbols table. |
---|
763 | * |
---|
764 | * @param symbols The symbol table to load. |
---|
765 | * @param local Include local symbols. The default is not to. |
---|
766 | */ |
---|
767 | void load_symbols (symbols::table& symbols, bool local = false); |
---|
768 | |
---|
769 | /** |
---|
770 | * Load the relocations. |
---|
771 | */ |
---|
772 | void load_relocations (); |
---|
773 | |
---|
774 | /** |
---|
775 | * References to the image. |
---|
776 | */ |
---|
777 | virtual int references () const; |
---|
778 | |
---|
779 | /** |
---|
780 | * The file size. |
---|
781 | */ |
---|
782 | virtual size_t size () const; |
---|
783 | |
---|
784 | /** |
---|
785 | * The file descriptor. |
---|
786 | */ |
---|
787 | virtual int fd () const; |
---|
788 | |
---|
789 | /** |
---|
790 | * A symbol in the image has been referenced. |
---|
791 | */ |
---|
792 | virtual void symbol_referenced (); |
---|
793 | |
---|
794 | /** |
---|
795 | * The archive the object file is contained in. If 0 the object file is |
---|
796 | * not contained in an archive. |
---|
797 | */ |
---|
798 | archive* get_archive (); |
---|
799 | |
---|
800 | /** |
---|
801 | * Return the unresolved symbol table for this object file. |
---|
802 | */ |
---|
803 | symbols::symtab& unresolved_symbols (); |
---|
804 | |
---|
805 | /** |
---|
806 | * Return the list external symbols. |
---|
807 | */ |
---|
808 | symbols::pointers& external_symbols (); |
---|
809 | |
---|
810 | /** |
---|
811 | * Return a container sections that match the requested type and |
---|
812 | * flags. The filtered section container is not cleared so any matching |
---|
813 | * sections are appended. |
---|
814 | * |
---|
815 | * @param filtered_secs The container of the matching sections. |
---|
816 | * @param type The section type. Must match. If 0 matches any. |
---|
817 | * @param flags_in The sections flags that must be set. This is a |
---|
818 | * mask. If 0 matches any. |
---|
819 | * @param flags_out The sections flags that must be clear. This is a |
---|
820 | * mask. If 0 this value is ignored. |
---|
821 | */ |
---|
822 | void get_sections (sections& filtered_secs, |
---|
823 | uint32_t type = 0, |
---|
824 | uint64_t flags_in = 0, |
---|
825 | uint64_t flags_out = 0); |
---|
826 | |
---|
827 | /** |
---|
828 | * Return a container sections that match the requested name. The |
---|
829 | * filtered section container is not cleared so any matching sections are |
---|
830 | * appended. |
---|
831 | * |
---|
832 | * @param filtered_secs The container of the matching sections. |
---|
833 | * @param name The name of the section. |
---|
834 | */ |
---|
835 | void get_sections (sections& filtered_secs, const std::string& name); |
---|
836 | |
---|
837 | /** |
---|
838 | * Get a section given an index number. |
---|
839 | * |
---|
840 | * @param index The section index to search for. |
---|
841 | */ |
---|
842 | const section& get_section (int index) const; |
---|
843 | |
---|
844 | /** |
---|
845 | * Set the object file's resolving flag. |
---|
846 | */ |
---|
847 | void resolve_set (); |
---|
848 | |
---|
849 | /** |
---|
850 | * Clear the object file's resolving flag. |
---|
851 | */ |
---|
852 | void resolve_clear (); |
---|
853 | |
---|
854 | /** |
---|
855 | * The resolving state. |
---|
856 | */ |
---|
857 | bool resolving () const; |
---|
858 | |
---|
859 | /** |
---|
860 | * Set the object file resolved flag. |
---|
861 | */ |
---|
862 | void resolved_set (); |
---|
863 | |
---|
864 | /** |
---|
865 | * The resolved state. |
---|
866 | */ |
---|
867 | bool resolved () const; |
---|
868 | |
---|
869 | private: |
---|
870 | archive* archive_; //< Points to the archive if part of an |
---|
871 | // archive. |
---|
872 | bool valid_; //< If true begin has run and finished. |
---|
873 | symbols::symtab unresolved; //< This object's unresolved symbols. |
---|
874 | symbols::pointers externals; //< This object's external symbols. |
---|
875 | sections secs; //< The sections. |
---|
876 | bool resolving_; //< The object is being resolved. |
---|
877 | bool resolved_; //< The object has been resolved. |
---|
878 | |
---|
879 | /** |
---|
880 | * Cannot copy via a copy constructor. |
---|
881 | */ |
---|
882 | object (const object& orig); |
---|
883 | |
---|
884 | /** |
---|
885 | * Cannot assign using the assignment operator. |
---|
886 | */ |
---|
887 | object& operator= (const object& rhs); |
---|
888 | }; |
---|
889 | |
---|
890 | /** |
---|
891 | * A collection of objects files as a cache. This currently is not a cache |
---|
892 | * but it could become one. |
---|
893 | */ |
---|
894 | class cache |
---|
895 | { |
---|
896 | public: |
---|
897 | /** |
---|
898 | * Construct the cache. |
---|
899 | */ |
---|
900 | cache (); |
---|
901 | |
---|
902 | /** |
---|
903 | * Destruct the objects. |
---|
904 | */ |
---|
905 | virtual ~cache (); |
---|
906 | |
---|
907 | /** |
---|
908 | * Open the cache by collecting the file names, loading object headers |
---|
909 | * and loading the archive file names. |
---|
910 | */ |
---|
911 | void open (); |
---|
912 | |
---|
913 | /** |
---|
914 | * Close the cache. |
---|
915 | */ |
---|
916 | void close (); |
---|
917 | |
---|
918 | /** |
---|
919 | * Add a file path to the cache. |
---|
920 | */ |
---|
921 | void add (const std::string& path); |
---|
922 | |
---|
923 | /** |
---|
924 | * Add a container of path to the cache. |
---|
925 | */ |
---|
926 | void add (paths& paths__); |
---|
927 | |
---|
928 | /** |
---|
929 | * Add a container of path to the cache. |
---|
930 | */ |
---|
931 | void add_libraries (paths& paths__); |
---|
932 | |
---|
933 | /** |
---|
934 | * Being a session on an archive. |
---|
935 | */ |
---|
936 | void archive_begin (const std::string& path); |
---|
937 | |
---|
938 | /** |
---|
939 | * End a session on an archive. |
---|
940 | */ |
---|
941 | void archive_end (const std::string& path); |
---|
942 | |
---|
943 | /** |
---|
944 | * Being sessions on all archives. |
---|
945 | */ |
---|
946 | void archives_begin (); |
---|
947 | |
---|
948 | /** |
---|
949 | * End the archive sessions. |
---|
950 | */ |
---|
951 | void archives_end (); |
---|
952 | |
---|
953 | /** |
---|
954 | * Collect the object names and add them to the cache. |
---|
955 | */ |
---|
956 | void collect_object_files (); |
---|
957 | |
---|
958 | /** |
---|
959 | * Collect the object file names by verifing the paths to the files are |
---|
960 | * valid or read the object file names contained in any archives. |
---|
961 | */ |
---|
962 | void collect_object_files (const std::string& path); |
---|
963 | |
---|
964 | /** |
---|
965 | * Load the symbols into the symbol table. |
---|
966 | * |
---|
967 | * @param symbols The symbol table to load. |
---|
968 | * @param locals Include local symbols. The default does not include them. |
---|
969 | */ |
---|
970 | void load_symbols (symbols::table& symbols, bool locals = false); |
---|
971 | |
---|
972 | /** |
---|
973 | * Output the unresolved symbol table to the output stream. |
---|
974 | */ |
---|
975 | void output_unresolved_symbols (std::ostream& out); |
---|
976 | |
---|
977 | /** |
---|
978 | * Get the archives. |
---|
979 | */ |
---|
980 | archives& get_archives (); |
---|
981 | |
---|
982 | /** |
---|
983 | * Get the objects inlcuding those in archives. |
---|
984 | */ |
---|
985 | objects& get_objects (); |
---|
986 | |
---|
987 | /** |
---|
988 | * Get the added objects. Does not include the ones in th archives. |
---|
989 | */ |
---|
990 | void get_objects (object_list& list) const; |
---|
991 | |
---|
992 | /** |
---|
993 | * Get the paths. |
---|
994 | */ |
---|
995 | const paths& get_paths () const; |
---|
996 | |
---|
997 | /** |
---|
998 | * Get the archive files. |
---|
999 | */ |
---|
1000 | void get_archive_files (files& afiles); |
---|
1001 | |
---|
1002 | /** |
---|
1003 | * Get the object files including those in archives. |
---|
1004 | */ |
---|
1005 | void get_object_files (files& ofiles); |
---|
1006 | |
---|
1007 | /** |
---|
1008 | * Get the archive count. |
---|
1009 | */ |
---|
1010 | int archive_count () const; |
---|
1011 | |
---|
1012 | /** |
---|
1013 | * Get the object count. |
---|
1014 | */ |
---|
1015 | int object_count () const; |
---|
1016 | |
---|
1017 | /** |
---|
1018 | * Get the path count. |
---|
1019 | */ |
---|
1020 | int path_count () const; |
---|
1021 | |
---|
1022 | /** |
---|
1023 | * Output archive files. |
---|
1024 | */ |
---|
1025 | void output_archive_files (std::ostream& out); |
---|
1026 | |
---|
1027 | /** |
---|
1028 | * Output archive files. |
---|
1029 | */ |
---|
1030 | void output_object_files (std::ostream& out); |
---|
1031 | |
---|
1032 | protected: |
---|
1033 | |
---|
1034 | /** |
---|
1035 | * Input a path into the cache. |
---|
1036 | */ |
---|
1037 | virtual void input (const std::string& path); |
---|
1038 | |
---|
1039 | private: |
---|
1040 | paths paths_; //< The names of the files to process. |
---|
1041 | archives archives_; //< The archive files. |
---|
1042 | objects objects_; //< The object files. |
---|
1043 | bool opened; //< The cache is open. |
---|
1044 | }; |
---|
1045 | |
---|
1046 | /** |
---|
1047 | * Copy the in file to the out file. |
---|
1048 | * |
---|
1049 | * @param in The input file. |
---|
1050 | * @param out The output file. |
---|
1051 | * @param size The amount to copy. If 0 the whole on in is copied. |
---|
1052 | */ |
---|
1053 | void copy_file (image& in, image& out, size_t size = 0); |
---|
1054 | |
---|
1055 | /** |
---|
1056 | * Find the libraries given the list of libraries as bare name which |
---|
1057 | * have 'lib' and '.a' added. |
---|
1058 | */ |
---|
1059 | void find_libraries (paths& libraries, paths& libpaths, paths& libs); |
---|
1060 | |
---|
1061 | } |
---|
1062 | } |
---|
1063 | |
---|
1064 | #endif |
---|