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