2008-04-11 Cary Coutant <ccoutant@google.com>
[binutils.git] / gold / archive.h
blob818bd60c65e4bdfcfc594d8a11e7dd88bfc8e753
1 // archive.h -- archive support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_ARCHIVE_H
24 #define GOLD_ARCHIVE_H
26 #include <string>
27 #include <vector>
29 #include "workqueue.h"
31 namespace gold
34 class Task;
35 class Input_file;
36 class Input_objects;
37 class Input_group;
38 class Layout;
39 class Symbol_table;
41 // This class represents an archive--generally a libNAME.a file.
42 // Archives have a symbol table and a list of objects.
44 class Archive
46 public:
47 Archive(const std::string& name, Input_file* input_file,
48 bool is_thin_archive, Dirsearch* dirpath, Task* task)
49 : name_(name), input_file_(input_file), armap_(), armap_names_(),
50 extended_names_(), armap_checked_(), seen_offsets_(),
51 is_thin_archive_(is_thin_archive), dirpath_(dirpath), task_(task)
52 { }
54 // The length of the magic string at the start of an archive.
55 static const int sarmag = 8;
57 // The magic string at the start of an archive.
58 static const char armag[sarmag];
59 static const char armagt[sarmag];
61 // The string expected at the end of an archive member header.
62 static const char arfmag[2];
64 // The name of the object.
65 const std::string&
66 name() const
67 { return this->name_; }
69 // Set up the archive: read the symbol map.
70 void
71 setup();
73 // Get a reference to the underlying file.
74 File_read&
75 file()
76 { return this->input_file_->file(); }
78 // Lock the underlying file.
79 void
80 lock(const Task* t)
81 { this->input_file_->file().lock(t); }
83 // Unlock the underlying file.
84 void
85 unlock(const Task* t)
86 { this->input_file_->file().unlock(t); }
88 // Return whether the underlying file is locked.
89 bool
90 is_locked() const
91 { return this->input_file_->file().is_locked(); }
93 // Return the token, so that the task can be queued.
94 Task_token*
95 token()
96 { return this->input_file_->file().token(); }
98 // Release the underlying file.
99 void
100 release()
101 { this->input_file_->file().release(); }
103 // Clear uncached views in the underlying file.
104 void
105 clear_uncached_views()
106 { this->input_file_->file().clear_uncached_views(); }
108 // Unlock any nested archives.
109 void
110 unlock_nested_archives();
112 // Select members from the archive as needed and add them to the
113 // link.
114 void
115 add_symbols(Symbol_table*, Layout*, Input_objects*);
117 private:
118 Archive(const Archive&);
119 Archive& operator=(const Archive&);
121 struct Archive_header;
123 // Get a view into the underlying file.
124 const unsigned char*
125 get_view(off_t start, section_size_type size, bool aligned, bool cache)
126 { return this->input_file_->file().get_view(0, start, size, aligned, cache); }
128 // Read the archive symbol map.
129 void
130 read_armap(off_t start, section_size_type size);
132 // Read an archive member header at OFF. CACHE is whether to cache
133 // the file view. Return the size of the member, and set *PNAME to
134 // the name.
135 off_t
136 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
138 // Interpret an archive header HDR at OFF. Return the size of the
139 // member, and set *PNAME to the name.
140 off_t
141 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
142 off_t* nested_off);
144 // Include all the archive members in the link.
145 void
146 include_all_members(Symbol_table*, Layout*, Input_objects*);
148 // Include an archive member in the link.
149 void
150 include_member(Symbol_table*, Layout*, Input_objects*, off_t off);
152 // An entry in the archive map of symbols to object files.
153 struct Armap_entry
155 // The offset to the symbol name in armap_names_.
156 off_t name_offset;
157 // The file offset to the object in the archive.
158 off_t file_offset;
161 // A simple hash code for off_t values.
162 class Seen_hash
164 public:
165 size_t operator()(off_t val) const
166 { return static_cast<size_t>(val); }
169 // For keeping track of open nested archives in a thin archive file.
170 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
172 // Name of object as printed to user.
173 std::string name_;
174 // For reading the file.
175 Input_file* input_file_;
176 // The archive map.
177 std::vector<Armap_entry> armap_;
178 // The names in the archive map.
179 std::string armap_names_;
180 // The extended name table.
181 std::string extended_names_;
182 // Track which symbols in the archive map are for elements which are
183 // defined or which have already been included in the link.
184 std::vector<bool> armap_checked_;
185 // Track which elements have been included by offset.
186 Unordered_set<off_t, Seen_hash> seen_offsets_;
187 // True if this is a thin archive.
188 const bool is_thin_archive_;
189 // Table of nested archives, indexed by filename.
190 Nested_archive_table nested_archives_;
191 // The directory search path.
192 Dirsearch* dirpath_;
193 // The task reading this archive.
194 Task *task_;
197 // This class is used to read an archive and pick out the desired
198 // elements and add them to the link.
200 class Add_archive_symbols : public Task
202 public:
203 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
204 Input_objects* input_objects,
205 Archive* archive, Input_group* input_group,
206 Task_token* this_blocker,
207 Task_token* next_blocker)
208 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
209 archive_(archive), input_group_(input_group),
210 this_blocker_(this_blocker), next_blocker_(next_blocker)
213 ~Add_archive_symbols();
215 // The standard Task methods.
217 Task_token*
218 is_runnable();
220 void
221 locks(Task_locker*);
223 void
224 run(Workqueue*);
226 std::string
227 get_name() const
229 if (this->archive_ == NULL)
230 return "Add_archive_symbols";
231 return "Add_archive_symbols " + this->archive_->file().filename();
234 private:
235 Symbol_table* symtab_;
236 Layout* layout_;
237 Input_objects* input_objects_;
238 Archive* archive_;
239 Input_group* input_group_;
240 Task_token* this_blocker_;
241 Task_token* next_blocker_;
244 } // End namespace gold.
246 #endif // !defined(GOLD_ARCHIVE_H)