]>
Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // archive.h -- archive support for gold -*- C++ -*- |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
61ba1cf9 ILT |
23 | #ifndef GOLD_ARCHIVE_H |
24 | #define GOLD_ARCHIVE_H | |
25 | ||
26 | #include <string> | |
27 | #include <vector> | |
28 | ||
7d9e3d98 | 29 | #include "fileread.h" |
61ba1cf9 ILT |
30 | #include "workqueue.h" |
31 | ||
32 | namespace gold | |
33 | { | |
34 | ||
17a1d0a9 | 35 | class Task; |
61ba1cf9 ILT |
36 | class Input_file; |
37 | class Input_objects; | |
ead1e424 | 38 | class Input_group; |
12e14209 | 39 | class Layout; |
61ba1cf9 ILT |
40 | class Symbol_table; |
41 | ||
42 | // This class represents an archive--generally a libNAME.a file. | |
43 | // Archives have a symbol table and a list of objects. | |
44 | ||
45 | class Archive | |
46 | { | |
47 | public: | |
a1207466 CC |
48 | Archive(const std::string& name, Input_file* input_file, |
49 | bool is_thin_archive, Dirsearch* dirpath, Task* task) | |
9eb9fa57 | 50 | : name_(name), input_file_(input_file), armap_(), armap_names_(), |
a1207466 CC |
51 | extended_names_(), armap_checked_(), seen_offsets_(), |
52 | is_thin_archive_(is_thin_archive), dirpath_(dirpath), task_(task) | |
61ba1cf9 ILT |
53 | { } |
54 | ||
55 | // The length of the magic string at the start of an archive. | |
56 | static const int sarmag = 8; | |
57 | ||
58 | // The magic string at the start of an archive. | |
59 | static const char armag[sarmag]; | |
a1207466 | 60 | static const char armagt[sarmag]; |
61ba1cf9 ILT |
61 | |
62 | // The string expected at the end of an archive member header. | |
63 | static const char arfmag[2]; | |
64 | ||
92de84a6 ILT |
65 | // The name of the object. This is the name used on the command |
66 | // line; e.g., if "-lgcc" is on the command line, this will be | |
67 | // "gcc". | |
61ba1cf9 ILT |
68 | const std::string& |
69 | name() const | |
70 | { return this->name_; } | |
71 | ||
92de84a6 ILT |
72 | // The file name. |
73 | const std::string& | |
74 | filename() const | |
75 | { return this->input_file_->filename(); } | |
76 | ||
61ba1cf9 ILT |
77 | // Set up the archive: read the symbol map. |
78 | void | |
a1207466 | 79 | setup(); |
61ba1cf9 | 80 | |
ead1e424 ILT |
81 | // Get a reference to the underlying file. |
82 | File_read& | |
83 | file() | |
84 | { return this->input_file_->file(); } | |
85 | ||
7d9e3d98 ILT |
86 | const File_read& |
87 | file() const | |
88 | { return this->input_file_->file(); } | |
89 | ||
61ba1cf9 ILT |
90 | // Lock the underlying file. |
91 | void | |
17a1d0a9 ILT |
92 | lock(const Task* t) |
93 | { this->input_file_->file().lock(t); } | |
61ba1cf9 ILT |
94 | |
95 | // Unlock the underlying file. | |
96 | void | |
17a1d0a9 ILT |
97 | unlock(const Task* t) |
98 | { this->input_file_->file().unlock(t); } | |
61ba1cf9 ILT |
99 | |
100 | // Return whether the underlying file is locked. | |
101 | bool | |
102 | is_locked() const | |
103 | { return this->input_file_->file().is_locked(); } | |
104 | ||
17a1d0a9 ILT |
105 | // Return the token, so that the task can be queued. |
106 | Task_token* | |
107 | token() | |
108 | { return this->input_file_->file().token(); } | |
109 | ||
110 | // Release the underlying file. | |
111 | void | |
112 | release() | |
113 | { this->input_file_->file().release(); } | |
114 | ||
39d0cb0e ILT |
115 | // Clear uncached views in the underlying file. |
116 | void | |
117 | clear_uncached_views() | |
118 | { this->input_file_->file().clear_uncached_views(); } | |
119 | ||
92de84a6 ILT |
120 | // Whether this is a thin archive. |
121 | bool | |
122 | is_thin_archive() const | |
123 | { return this->is_thin_archive_; } | |
124 | ||
a1207466 CC |
125 | // Unlock any nested archives. |
126 | void | |
127 | unlock_nested_archives(); | |
128 | ||
61ba1cf9 ILT |
129 | // Select members from the archive as needed and add them to the |
130 | // link. | |
131 | void | |
7d9e3d98 | 132 | add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*); |
61ba1cf9 | 133 | |
92de84a6 ILT |
134 | // Return the number of members in the archive. |
135 | size_t | |
2a00e4fb | 136 | count_members(); |
92de84a6 | 137 | |
61ba1cf9 ILT |
138 | private: |
139 | Archive(const Archive&); | |
140 | Archive& operator=(const Archive&); | |
141 | ||
142 | struct Archive_header; | |
61ba1cf9 ILT |
143 | |
144 | // Get a view into the underlying file. | |
145 | const unsigned char* | |
39d0cb0e ILT |
146 | get_view(off_t start, section_size_type size, bool aligned, bool cache) |
147 | { return this->input_file_->file().get_view(0, start, size, aligned, cache); } | |
ba45d247 | 148 | |
4973341a ILT |
149 | // Read the archive symbol map. |
150 | void | |
8383303e | 151 | read_armap(off_t start, section_size_type size); |
61ba1cf9 | 152 | |
cb295612 ILT |
153 | // Read an archive member header at OFF. CACHE is whether to cache |
154 | // the file view. Return the size of the member, and set *PNAME to | |
155 | // the name. | |
61ba1cf9 | 156 | off_t |
a1207466 | 157 | read_header(off_t off, bool cache, std::string* pname, off_t* nested_off); |
61ba1cf9 | 158 | |
4973341a ILT |
159 | // Interpret an archive header HDR at OFF. Return the size of the |
160 | // member, and set *PNAME to the name. | |
161 | off_t | |
a1207466 | 162 | interpret_header(const Archive_header* hdr, off_t off, std::string* pname, |
92de84a6 | 163 | off_t* nested_off) const; |
4973341a ILT |
164 | |
165 | // Include all the archive members in the link. | |
166 | void | |
7d9e3d98 | 167 | include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*); |
4973341a | 168 | |
61ba1cf9 ILT |
169 | // Include an archive member in the link. |
170 | void | |
7d9e3d98 ILT |
171 | include_member(Symbol_table*, Layout*, Input_objects*, off_t off, |
172 | Mapfile*, Symbol*, const char* why); | |
61ba1cf9 | 173 | |
92de84a6 ILT |
174 | // Iterate over archive members. |
175 | class const_iterator; | |
176 | ||
177 | const_iterator | |
2a00e4fb | 178 | begin(); |
92de84a6 ILT |
179 | |
180 | const_iterator | |
2a00e4fb | 181 | end(); |
92de84a6 ILT |
182 | |
183 | friend class const_iterator; | |
184 | ||
61ba1cf9 ILT |
185 | // An entry in the archive map of symbols to object files. |
186 | struct Armap_entry | |
187 | { | |
9eb9fa57 ILT |
188 | // The offset to the symbol name in armap_names_. |
189 | off_t name_offset; | |
190 | // The file offset to the object in the archive. | |
191 | off_t file_offset; | |
61ba1cf9 ILT |
192 | }; |
193 | ||
a93d6d07 ILT |
194 | // A simple hash code for off_t values. |
195 | class Seen_hash | |
196 | { | |
197 | public: | |
198 | size_t operator()(off_t val) const | |
199 | { return static_cast<size_t>(val); } | |
200 | }; | |
201 | ||
a1207466 CC |
202 | // For keeping track of open nested archives in a thin archive file. |
203 | typedef Unordered_map<std::string, Archive*> Nested_archive_table; | |
204 | ||
61ba1cf9 ILT |
205 | // Name of object as printed to user. |
206 | std::string name_; | |
207 | // For reading the file. | |
208 | Input_file* input_file_; | |
209 | // The archive map. | |
210 | std::vector<Armap_entry> armap_; | |
9eb9fa57 ILT |
211 | // The names in the archive map. |
212 | std::string armap_names_; | |
61ba1cf9 ILT |
213 | // The extended name table. |
214 | std::string extended_names_; | |
a93d6d07 ILT |
215 | // Track which symbols in the archive map are for elements which are |
216 | // defined or which have already been included in the link. | |
217 | std::vector<bool> armap_checked_; | |
218 | // Track which elements have been included by offset. | |
219 | Unordered_set<off_t, Seen_hash> seen_offsets_; | |
a1207466 CC |
220 | // True if this is a thin archive. |
221 | const bool is_thin_archive_; | |
222 | // Table of nested archives, indexed by filename. | |
223 | Nested_archive_table nested_archives_; | |
224 | // The directory search path. | |
225 | Dirsearch* dirpath_; | |
226 | // The task reading this archive. | |
227 | Task *task_; | |
61ba1cf9 ILT |
228 | }; |
229 | ||
230 | // This class is used to read an archive and pick out the desired | |
231 | // elements and add them to the link. | |
232 | ||
233 | class Add_archive_symbols : public Task | |
234 | { | |
235 | public: | |
7e1edb90 | 236 | Add_archive_symbols(Symbol_table* symtab, Layout* layout, |
7d9e3d98 | 237 | Input_objects* input_objects, Mapfile* mapfile, |
ead1e424 ILT |
238 | Archive* archive, Input_group* input_group, |
239 | Task_token* this_blocker, | |
61ba1cf9 | 240 | Task_token* next_blocker) |
7e1edb90 | 241 | : symtab_(symtab), layout_(layout), input_objects_(input_objects), |
7d9e3d98 | 242 | mapfile_(mapfile), archive_(archive), input_group_(input_group), |
7e1edb90 | 243 | this_blocker_(this_blocker), next_blocker_(next_blocker) |
61ba1cf9 ILT |
244 | { } |
245 | ||
246 | ~Add_archive_symbols(); | |
247 | ||
248 | // The standard Task methods. | |
249 | ||
17a1d0a9 ILT |
250 | Task_token* |
251 | is_runnable(); | |
61ba1cf9 | 252 | |
17a1d0a9 ILT |
253 | void |
254 | locks(Task_locker*); | |
61ba1cf9 ILT |
255 | |
256 | void | |
257 | run(Workqueue*); | |
258 | ||
c7912668 ILT |
259 | std::string |
260 | get_name() const | |
261 | { | |
262 | if (this->archive_ == NULL) | |
263 | return "Add_archive_symbols"; | |
264 | return "Add_archive_symbols " + this->archive_->file().filename(); | |
265 | } | |
266 | ||
61ba1cf9 | 267 | private: |
61ba1cf9 | 268 | Symbol_table* symtab_; |
12e14209 | 269 | Layout* layout_; |
61ba1cf9 | 270 | Input_objects* input_objects_; |
7d9e3d98 | 271 | Mapfile* mapfile_; |
61ba1cf9 | 272 | Archive* archive_; |
ead1e424 | 273 | Input_group* input_group_; |
61ba1cf9 ILT |
274 | Task_token* this_blocker_; |
275 | Task_token* next_blocker_; | |
276 | }; | |
277 | ||
278 | } // End namespace gold. | |
279 | ||
280 | #endif // !defined(GOLD_ARCHIVE_H) |