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