Snapshot. Now able to produce a minimal executable which actually
[external/binutils.git] / gold / archive.h
1 // archive.h -- archive support for gold      -*- C++ -*-
2
3 #ifndef GOLD_ARCHIVE_H
4 #define GOLD_ARCHIVE_H
5
6 #include <string>
7 #include <vector>
8
9 #include "workqueue.h"
10
11 namespace gold
12 {
13
14 class Input_file;
15 class Input_objects;
16 class Symbol_table;
17
18 // This class represents an archive--generally a libNAME.a file.
19 // Archives have a symbol table and a list of objects.
20
21 class Archive
22 {
23  public:
24   Archive(const std::string& name, Input_file* input_file)
25     : name_(name), input_file_(input_file), armap_(), extended_names_()
26   { }
27
28   // The length of the magic string at the start of an archive.
29   static const int sarmag = 8;
30
31   // The magic string at the start of an archive.
32   static const char armag[sarmag];
33
34   // The string expected at the end of an archive member header.
35   static const char arfmag[2];
36
37   // The name of the object.
38   const std::string&
39   name() const
40   { return this->name_; }
41
42   // Set up the archive: read the symbol map.
43   void
44   setup();
45
46   // Lock the underlying file.
47   void
48   lock()
49   { this->input_file_->file().lock(); }
50
51   // Unlock the underlying file.
52   void
53   unlock()
54   { this->input_file_->file().unlock(); }
55
56   // Return whether the underlying file is locked.
57   bool
58   is_locked() const
59   { return this->input_file_->file().is_locked(); }
60
61   // Select members from the archive as needed and add them to the
62   // link.
63   void
64   add_symbols(Symbol_table*, Input_objects*);
65
66  private:
67   Archive(const Archive&);
68   Archive& operator=(const Archive&);
69
70   struct Archive_header;
71   class Add_archive_symbols_locker;
72
73   // Get a view into the underlying file.
74   const unsigned char*
75   get_view(off_t start, off_t size);
76
77   // Read an archive member header at OFF.  Return the size of the
78   // member, and set *PNAME to the name.
79   off_t
80   read_header(off_t off, std::string* pname);
81
82   // Include an archive member in the link.
83   void
84   include_member(Symbol_table*, Input_objects*, off_t off);
85
86   // An entry in the archive map of symbols to object files.
87   struct Armap_entry
88   {
89     // The symbol name.
90     const char* name;
91     // The offset to the file.
92     off_t offset;
93   };
94
95   // Name of object as printed to user.
96   std::string name_;
97   // For reading the file.
98   Input_file* input_file_;
99   // The archive map.
100   std::vector<Armap_entry> armap_;
101   // The extended name table.
102   std::string extended_names_;
103 };
104
105 // This class is used to read an archive and pick out the desired
106 // elements and add them to the link.
107
108 class Add_archive_symbols : public Task
109 {
110  public:
111   Add_archive_symbols(Symbol_table* symtab, Input_objects* input_objects,
112                       Archive* archive, Task_token* this_blocker,
113                       Task_token* next_blocker)
114     : symtab_(symtab), input_objects_(input_objects), archive_(archive),
115       this_blocker_(this_blocker), next_blocker_(next_blocker)
116   { }
117
118   ~Add_archive_symbols();
119
120   // The standard Task methods.
121
122   Is_runnable_type
123   is_runnable(Workqueue*);
124
125   Task_locker*
126   locks(Workqueue*);
127
128   void
129   run(Workqueue*);
130
131  private:
132   class Add_archive_symbols_locker;
133
134   Symbol_table* symtab_;
135   Input_objects* input_objects_;
136   Archive* archive_;
137   Task_token* this_blocker_;
138   Task_token* next_blocker_;
139 };
140
141 } // End namespace gold.
142
143 #endif // !defined(GOLD_ARCHIVE_H)