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