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