*** empty log message ***
[deliverable/binutils-gdb.git] / gold / archive.h
CommitLineData
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
29#include "workqueue.h"
30
31namespace gold
32{
33
17a1d0a9 34class Task;
61ba1cf9
ILT
35class Input_file;
36class Input_objects;
ead1e424 37class Input_group;
12e14209 38class Layout;
61ba1cf9
ILT
39class Symbol_table;
40
41// This class represents an archive--generally a libNAME.a file.
42// Archives have a symbol table and a list of objects.
43
44class Archive
45{
46 public:
a1207466
CC
47 Archive(const std::string& name, Input_file* input_file,
48 bool is_thin_archive, Dirsearch* dirpath, Task* task)
9eb9fa57 49 : name_(name), input_file_(input_file), armap_(), armap_names_(),
a1207466
CC
50 extended_names_(), armap_checked_(), seen_offsets_(),
51 is_thin_archive_(is_thin_archive), dirpath_(dirpath), task_(task)
61ba1cf9
ILT
52 { }
53
54 // The length of the magic string at the start of an archive.
55 static const int sarmag = 8;
56
57 // The magic string at the start of an archive.
58 static const char armag[sarmag];
a1207466 59 static const char armagt[sarmag];
61ba1cf9
ILT
60
61 // The string expected at the end of an archive member header.
62 static const char arfmag[2];
63
64 // The name of the object.
65 const std::string&
66 name() const
67 { return this->name_; }
68
69 // Set up the archive: read the symbol map.
70 void
a1207466 71 setup();
61ba1cf9 72
ead1e424
ILT
73 // Get a reference to the underlying file.
74 File_read&
75 file()
76 { return this->input_file_->file(); }
77
61ba1cf9
ILT
78 // Lock the underlying file.
79 void
17a1d0a9
ILT
80 lock(const Task* t)
81 { this->input_file_->file().lock(t); }
61ba1cf9
ILT
82
83 // Unlock the underlying file.
84 void
17a1d0a9
ILT
85 unlock(const Task* t)
86 { this->input_file_->file().unlock(t); }
61ba1cf9
ILT
87
88 // Return whether the underlying file is locked.
89 bool
90 is_locked() const
91 { return this->input_file_->file().is_locked(); }
92
17a1d0a9
ILT
93 // Return the token, so that the task can be queued.
94 Task_token*
95 token()
96 { return this->input_file_->file().token(); }
97
98 // Release the underlying file.
99 void
100 release()
101 { this->input_file_->file().release(); }
102
a1207466
CC
103 // Unlock any nested archives.
104 void
105 unlock_nested_archives();
106
61ba1cf9
ILT
107 // Select members from the archive as needed and add them to the
108 // link.
109 void
7e1edb90 110 add_symbols(Symbol_table*, Layout*, Input_objects*);
61ba1cf9
ILT
111
112 private:
113 Archive(const Archive&);
114 Archive& operator=(const Archive&);
115
116 struct Archive_header;
61ba1cf9
ILT
117
118 // Get a view into the underlying file.
119 const unsigned char*
8383303e 120 get_view(off_t start, section_size_type size, bool cache)
9eb9fa57 121 { return this->input_file_->file().get_view(start, size, cache); }
ba45d247 122
4973341a
ILT
123 // Read the archive symbol map.
124 void
8383303e 125 read_armap(off_t start, section_size_type size);
61ba1cf9 126
cb295612
ILT
127 // Read an archive member header at OFF. CACHE is whether to cache
128 // the file view. Return the size of the member, and set *PNAME to
129 // the name.
61ba1cf9 130 off_t
a1207466 131 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
61ba1cf9 132
4973341a
ILT
133 // Interpret an archive header HDR at OFF. Return the size of the
134 // member, and set *PNAME to the name.
135 off_t
a1207466
CC
136 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
137 off_t* nested_off);
4973341a
ILT
138
139 // Include all the archive members in the link.
140 void
7e1edb90 141 include_all_members(Symbol_table*, Layout*, Input_objects*);
4973341a 142
61ba1cf9
ILT
143 // Include an archive member in the link.
144 void
7e1edb90 145 include_member(Symbol_table*, Layout*, Input_objects*, off_t off);
61ba1cf9
ILT
146
147 // An entry in the archive map of symbols to object files.
148 struct Armap_entry
149 {
9eb9fa57
ILT
150 // The offset to the symbol name in armap_names_.
151 off_t name_offset;
152 // The file offset to the object in the archive.
153 off_t file_offset;
61ba1cf9
ILT
154 };
155
a93d6d07
ILT
156 // A simple hash code for off_t values.
157 class Seen_hash
158 {
159 public:
160 size_t operator()(off_t val) const
161 { return static_cast<size_t>(val); }
162 };
163
a1207466
CC
164 // For keeping track of open nested archives in a thin archive file.
165 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
166
61ba1cf9
ILT
167 // Name of object as printed to user.
168 std::string name_;
169 // For reading the file.
170 Input_file* input_file_;
171 // The archive map.
172 std::vector<Armap_entry> armap_;
9eb9fa57
ILT
173 // The names in the archive map.
174 std::string armap_names_;
61ba1cf9
ILT
175 // The extended name table.
176 std::string extended_names_;
a93d6d07
ILT
177 // Track which symbols in the archive map are for elements which are
178 // defined or which have already been included in the link.
179 std::vector<bool> armap_checked_;
180 // Track which elements have been included by offset.
181 Unordered_set<off_t, Seen_hash> seen_offsets_;
a1207466
CC
182 // True if this is a thin archive.
183 const bool is_thin_archive_;
184 // Table of nested archives, indexed by filename.
185 Nested_archive_table nested_archives_;
186 // The directory search path.
187 Dirsearch* dirpath_;
188 // The task reading this archive.
189 Task *task_;
61ba1cf9
ILT
190};
191
192// This class is used to read an archive and pick out the desired
193// elements and add them to the link.
194
195class Add_archive_symbols : public Task
196{
197 public:
7e1edb90
ILT
198 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
199 Input_objects* input_objects,
ead1e424
ILT
200 Archive* archive, Input_group* input_group,
201 Task_token* this_blocker,
61ba1cf9 202 Task_token* next_blocker)
7e1edb90
ILT
203 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
204 archive_(archive), input_group_(input_group),
205 this_blocker_(this_blocker), next_blocker_(next_blocker)
61ba1cf9
ILT
206 { }
207
208 ~Add_archive_symbols();
209
210 // The standard Task methods.
211
17a1d0a9
ILT
212 Task_token*
213 is_runnable();
61ba1cf9 214
17a1d0a9
ILT
215 void
216 locks(Task_locker*);
61ba1cf9
ILT
217
218 void
219 run(Workqueue*);
220
c7912668
ILT
221 std::string
222 get_name() const
223 {
224 if (this->archive_ == NULL)
225 return "Add_archive_symbols";
226 return "Add_archive_symbols " + this->archive_->file().filename();
227 }
228
61ba1cf9 229 private:
61ba1cf9 230 Symbol_table* symtab_;
12e14209 231 Layout* layout_;
61ba1cf9
ILT
232 Input_objects* input_objects_;
233 Archive* archive_;
ead1e424 234 Input_group* input_group_;
61ba1cf9
ILT
235 Task_token* this_blocker_;
236 Task_token* next_blocker_;
237};
238
239} // End namespace gold.
240
241#endif // !defined(GOLD_ARCHIVE_H)
This page took 0.082611 seconds and 4 git commands to generate.