source: rtems-tools/linkers/rld-files.h @ ec24a37

4.104.115
Last change on this file since ec24a37 was ec24a37, checked in by Chris Johns <chrisj@…>, on 05/06/12 at 22:47:11

Add to git.

  • Property mode set to 100644
File size: 18.8 KB
Line 
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
42namespace 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 < std::string, archive* > archives;
60
61    /**
62     * Container of object files.
63     */
64    typedef std::map < std::string, object* > objects;
65
66    /**
67     * Container list of object files.
68     */
69    typedef std::list < object* > object_list;
70   
71    /**
72     * Split a path from a string with a delimiter to the path container. Add
73     * only the paths that exist and ignore those that do not.
74     */
75    void path_split (const std::string& path,
76                     rld::files::paths& paths);
77
78    /**
79     * Make a path by joining the parts with required separator.
80     */
81    void path_join (const std::string& path_,
82                    const std::string& file_,
83                    std::string& joined);
84
85    /**
86     * Check the path is a file.
87     */
88    bool check_file (const std::string& path);
89
90    /**
91     * Check the path is a directory.
92     */
93    bool check_directory (const std::string& path);
94   
95    /**
96     * Find the file given a container of paths and file names.
97     *
98     * @param path The path of the file if found else empty.
99     * @param name The name of the file to search for.
100     * @param search_paths The container of paths to search.
101     */
102    void find_file (std::string&       path,
103                    const std::string& name,
104                    paths&             search_paths);
105
106    /**
107     * A file is a single object file that is either in an archive or stand
108     * alone.
109     */
110    class file
111    {
112    public:
113      /**
114       * Construct the file from the component parts when part of an archive.
115       *
116       * @param aname The archive name.
117       * @param oname The object file name.
118       * @param offset The offset in the archive the object file starts.
119       * @param size The size of the archive the object file starts.
120       */
121      file (const std::string& aname,
122            const std::string& oname,
123            off_t              offset,
124            size_t             size);
125
126      /**
127       * Construct the name by splitting the full path into an archive,
128       * object file name and offset.
129       *
130       * @param path The path to the image.
131       * @param is_object If true (default) the name is for an object file.
132       */
133      file (const std::string& path, bool is_object = true);
134
135      /**
136       * Contruct an empty file.
137       */
138      file ();
139
140      /**
141       * Set a name from the path.
142       *
143       * @param path The path to the image.
144       * @param is_object If true (default) the name is for an object file.
145       */
146      void set (const std::string& path, bool is_object = true);
147
148      /**
149       * Is an archive returns true if the file is in an archive.
150       *
151       * @retval true The object file is in an archive.
152       * @retval false The object file is stand alone.
153       */
154      bool is_archive () const;
155
156      /**
157       * Is object file stand alone.
158       *
159       * @retval true The object file is stand alone.
160       * @retval false The object could be part of an archive.
161       */
162      bool is_object () const;
163
164      /**
165       * Valid returns true if there is a valid name.
166       *
167       * @retval true There is a valid name.
168       * @retval false There is not name assigned.
169       */
170      bool is_valid () const;
171
172      /**
173       * Exists returns true if the archive or object file is present on disk
174       * and a regular file.
175       *
176       * @retval true The file is valid and a regular file.
177       * @retval false The file is either not present, not accessable or not a
178       *               regular file.
179       */
180      bool exists () const;
181
182      /**
183       * The path maps to the real file on disk. The file may not be valid.
184       *
185       * @return const std::string The real path to the file on disk.
186       */
187      const std::string path () const;
188
189      /**
190       * The full path.
191       *
192       * @return const std::string The full path to the image.
193       */
194      const std::string full () const;
195
196      /**
197       * The base path. It is the basename of the full path.
198       *
199       * @return const std::string The basename of the full path to the image.
200       */
201      const std::string basename () const;
202
203      /**
204       * The archive name component. A length of 0 means there was not
205       * archive component.
206       */
207      const std::string& aname () const;
208
209      /**
210       * The object name. There is always an object name.
211       */
212      const std::string& oname () const;
213
214      /**
215       * The object's offset in the archive.
216       */
217      off_t offset () const;
218
219      /**
220       * The object's size in the archive.
221       */
222      size_t size () const;
223
224    private:
225      std::string aname_;  //< The archive name.
226      std::string oname_;  //< The object name.
227      off_t       offset_; //< The object's offset in the archive.
228      size_t      size_;   //< The object's size in teh archive.
229    };
230
231    /**
232     * Image is the base file type. A base class is used so we can have a
233     * single container of to hold types of images we need to support.
234     */
235    class image
236    {
237    public:
238      /**
239       * Construct the image.
240       *
241       * @param name The name of the image.
242       */
243      image (file& name);
244
245      /**
246       * Construct the image.
247       *
248       * @param name The file path.
249       * @param is_object If true (default) the name is for an object file.
250       */
251      image (const std::string& path, bool is_object = true);
252
253      /**
254       * Construct the image.
255       */
256      image ();
257
258      /**
259       * Destruct the image.
260       */
261      virtual ~image ();
262
263      /**
264       * Open the image. You can open the image more than once but you need to
265       * close it the same number of times.
266       */
267      virtual void open (file& name);
268
269      /**
270       * Open the image. You can open the image more than once but you need to
271       * close it the same number of times.
272       */
273      virtual void open (bool writable = false);
274
275      /**
276       * Close the image.
277       */
278      virtual void close ();
279
280      /**
281       * Read a block from the file.
282       */
283      virtual ssize_t read (uint8_t* buffer, size_t size);
284
285      /**
286       * Write a block from the file.
287       */
288      virtual ssize_t write (const void* buffer, size_t size);
289
290      /**
291       * Seek.
292       */
293      virtual void seek (off_t offset);
294
295      /**
296       * Seek and read.
297       */
298      virtual bool seek_read (off_t offset, uint8_t* buffer, size_t size);
299
300      /**
301       * Seek and write.
302       */
303      virtual bool seek_write (off_t offset, const void* buffer, size_t size);
304
305      /**
306       * The name of the image.
307       */
308      const file& name () const;
309
310      /**
311       * References to the image.
312       */
313      virtual int references () const;
314
315      /**
316       * The file size.
317       */
318      virtual size_t size () const;
319
320      /**
321       * The file descriptor.
322       */
323      virtual int fd () const;
324
325      /**
326       * The libelf reference. The ELF image could be in an archive container
327       * so set container to true to get the archive's reference.
328       */
329      virtual rld::elf::elf* elf (bool archive = false);
330
331      /**
332       * Set the libelf reference.
333       */
334      void set_elf (rld::elf::elf* elf);
335
336      /**
337       * A symbol in the image has been referenced.
338       */
339      virtual void symbol_referenced ();
340
341      /**
342       * Return the number of symbol references.
343       */
344      virtual int symbol_references () const;
345
346      /**
347       * The path maps to the real file on disk. The file may not be valid.
348       *
349       * @return const std::string The real path to the file on disk.
350       */
351      const std::string path () const {
352        return name ().path ();
353      }
354
355      /**
356       * Is the archive open ?
357       *
358       * @retval true The archive is open.
359       * @retval false The archive is not open.
360       */
361      bool is_open () const {
362        return fd () != -1;
363      }
364
365    private:
366      file      name_;       //< The name of the file.
367      int       references_; //< The number of handles open.
368      int       fd_;         //< The file descriptor of the archive.
369      elf::elf* elf_;        //< The libelf reference.
370      int       symbol_refs; //< The number of symbols references made.
371    };
372
373    /**
374     * Copy the input section of the image to the output section. The file
375     * positions in the images must be set before making the call.
376     */
377    void copy (image& in, image& out, size_t size);
378
379    /**
380     * The archive class proivdes access to ELF object files that are held in a
381     * AR format file. GNU AR extensions are supported.
382     */
383    class archive:
384      public image
385    {
386    public:
387      /**
388       * Open a archive format file that contains ELF object files.
389       *
390       */
391      archive (const std::string& name);
392
393      /**
394       * Close the archive.
395       */
396      virtual ~archive ();
397
398      /**
399       * Match the archive name.
400       *
401       * @param name The name of the archive to check.
402       * @retval true This is the archive.
403       * @retval false This is not the archive.
404       */
405      bool is (const std::string& name) const;
406
407      /**
408       * Check this is a valid archive.
409       */
410      bool is_valid ();
411
412      /**
413       * Load objects.
414       */
415      void load_objects (objects& objs);
416     
417      /**
418       * Get the name.
419       */
420      const std::string& get_name () const;
421
422      /**
423       * Less than operator for the map container.
424       */
425      bool operator< (const archive& rhs) const;
426
427      /**
428       * Create a new archive. If referening an existing archive it is
429       * overwritten.
430       */
431      void create (object_list& objects);
432
433    private:
434      /**
435       * Read header.
436       */
437      bool read_header (off_t offset, uint8_t* header);
438
439      /**
440       * Add the object file from the archive to the object's container.
441       */
442      void add_object (objects&    objs,
443                       const char* name,
444                       off_t       offset,
445                       size_t      size);
446
447      /**
448       * Write a file header into the archive.
449       */
450      void write_header (const std::string& name,
451                         uint32_t           mtime,
452                         int                uid,
453                         int                gid,
454                         int                mode,
455                         size_t             size);
456
457      /**
458       * Cannot copy via a copy constructor.
459       */
460      archive (const archive& orig);
461
462      /**
463       * Cannot assign using the assignment operator..
464       */
465      archive& operator= (const archive& rhs);
466    };
467
468    /**
469     * The object file cab be in an archive or a file.
470     */
471    class object:
472      public image
473    {
474    public:
475      /**
476       * Construct an object image that is part of an archive.
477       *
478       * @param archive_ The archive the object file is part of.
479       * @param file_ The image file.
480       */
481      object (archive& archive_, file& file_);
482
483      /**
484       * Construct the object file.
485       *
486       * @param path The object file path.
487       */
488      object (const std::string& path);
489
490      /**
491       * Construct the object file.
492       */
493      object ();
494
495      /**
496       * Destruct the object file.
497       */
498      virtual ~object ();
499
500      /**
501       * Open the object file.
502       */
503      virtual void open ();
504
505      /**
506       * Close the object.
507       */
508      virtual void close ();
509
510      /**
511       * Begin the ELF session.
512       */
513      void begin ();
514
515      /**
516       * End the ELF session.
517       */
518      void end ();
519
520      /**
521       * Load the symbols into the symbols table.
522       *
523       * @param symbols The symbol table to load.
524       * @param local Include local symbols. The default is not to.
525       */
526      void load_symbols (rld::symbols::table& symbols, bool local = false);
527
528      /**
529       * Get the string from the string table.
530       */
531      std::string get_string (int section, size_t offset);
532
533      /**
534       * References to the image.
535       */
536      virtual int references () const;
537
538      /**
539       * The file size.
540       */
541      virtual size_t size () const;
542
543      /**
544       * The file descriptor.
545       */
546      virtual int fd () const;
547
548      /**
549       * The libelf reference. The ELF image could be in an archive container
550       * so set container to true to get the archive's reference.
551       */
552      virtual elf::elf* elf (bool archive = false);
553
554      /**
555       * A symbol in the image has been referenced.
556       */
557      virtual void symbol_referenced ();
558
559      /**
560       * The archive the object file is contained in. If 0 the object file is
561       * not contained in an archive.
562       */
563      archive* get_archive ();
564
565      /**
566       * Number of sections in the object file.
567       */
568      int sections () const;
569
570      /**
571       * Section string index.
572       */
573      int section_strings () const;
574
575      /**
576       * Return the unresolved symbol table for this object file.
577       */
578      rld::symbols::table& unresolved_symbols ();
579
580      /**
581       * Return the list external symbols.
582       */
583      rld::symbols::list& external_symbols ();
584
585    private:
586      archive*            archive_;   //< Points to the archive if part of an
587                                      //  archive.
588      elf::elf_ehdr       ehdr;       //< The ELF header.
589      rld::symbols::table unresolved; //< This object's unresolved symbols.
590      rld::symbols::list  externals;  //< This object's external symbols.
591
592      /**
593       * Cannot copy via a copy constructor.
594       */
595      object (const object& orig);
596
597      /**
598       * Cannot assign using the assignment operator.
599       */
600      object& operator= (const object& rhs);
601    };
602
603    /**
604     * A collection of objects files as a cache. This currently is not a cache
605     * but it could become one.
606     */
607    class cache
608    {
609    public:
610      /**
611       * Construct the cache.
612       */
613      cache ();
614
615      /**
616       * Destruct the objects.
617       */
618      virtual ~cache ();
619
620      /**
621       * Open the cache by collecting the file names, loading object headers
622       * and loading the archive file names.
623       */
624      void open ();
625
626      /**
627       * Close the cache.
628       */
629      void close ();
630
631      /**
632       * Add a file path to the cache.
633       */
634      void add (const std::string& path);
635
636      /**
637       * Add a container of path to the cache.
638       */
639      void add (paths& paths__);
640
641      /**
642       * Add a container of path to the cache.
643       */
644      void add_libraries (paths& paths__);
645
646      /**
647       * Being an ELF session on an archive.
648       */
649      void archive_begin (const std::string& path);
650
651      /**
652       * End an ELF session on an archive.
653       */
654      void archive_end (const std::string& path);
655
656      /**
657       * Being ELF sessions on all archives.
658       */
659      void archives_begin ();
660
661      /**
662       * End the archive elf sessions.
663       */
664      void archives_end ();
665
666      /**
667       * Collect the object names and add them to the cache.
668       */
669      void collect_object_files ();
670     
671      /**
672       * Collect the object file names by verifing the paths to the files are
673       * valid or read the object file names contained in any archives.
674       */
675      void collect_object_files (const std::string& path);
676     
677      /**
678       * Load the symbols into the symbol table.
679       *
680       * @param symbols The symbol table to load.
681       * @param local Include local symbols. The default is not to.
682       */
683      void load_symbols (rld::symbols::table& symbols, bool locals = false);
684
685      /**
686       * Output the unresolved symbol table to the output stream.
687       */
688      void output_unresolved_symbols (std::ostream& out);
689
690      /**
691       * Get the archives.
692       */
693      archives& get_archives ();
694 
695      /**
696       * Get the objects inlcuding those in archives.
697       */
698      objects& get_objects ();
699 
700      /**
701       * Get the added objects. Does not include the ones in th archives.
702       */
703      void get_objects (object_list& list);
704
705      /**
706       * Get the paths.
707       */
708      const paths& get_paths () const;
709
710      /**
711       * Get the archive files.
712       */
713      void get_archive_files (files& afiles);
714
715      /**
716       * Get the object files including those in archives.
717       */
718      void get_object_files (files& ofiles);
719
720      /**
721       * Get the archive count.
722       */
723      int archive_count () const;
724
725      /**
726       * Get the object count.
727       */
728      int object_count () const;
729
730      /**
731       * Get the path count.
732       */
733      int path_count () const;
734
735      /**
736       * Output archive files.
737       */
738      void output_archive_files (std::ostream& out);
739
740      /**
741       * Output archive files.
742       */
743      void output_object_files (std::ostream& out);
744
745    protected:
746
747      /**
748       * Input a path into the cache.
749       */
750      virtual void input (const std::string& path);
751
752    private:
753      paths    paths_;    //< The names of the files to process.
754      archives archives_; //< The archive files.
755      objects  objects_;  //< The object files.
756      bool     opened;    //< The cache is open.
757    };
758
759    /**
760     * Find the libraries given the list of libraries as bare name which
761     * have 'lib' and '.a' added.
762     */
763    void find_libraries (paths& libraries, paths& libpaths, paths& libs);
764
765  }
766}
767
768#endif
Note: See TracBrowser for help on using the repository browser.