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