2011-01-24 Pedro Alves <pedro@codesourcery.com>
[external/binutils.git] / gold / readsyms.h
1 // readsyms.h -- read input file symbols for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 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_READSYMS_H
24 #define GOLD_READSYMS_H
25
26 #include <vector>
27
28 #include "workqueue.h"
29 #include "object.h"
30
31 namespace gold
32 {
33
34 class Input_objects;
35 class Symbol_table;
36 class Input_group;
37 class Archive;
38 class Finish_group;
39
40 // This Task is responsible for reading the symbols from an input
41 // file.  This also includes reading the relocations so that we can
42 // check for any that require a PLT and/or a GOT.  After the data has
43 // been read, this queues up another task to actually add the symbols
44 // to the symbol table.  The tasks are separated because the file
45 // reading can occur in parallel but adding the symbols must be done
46 // in the order of the input files.
47
48 class Read_symbols : public Task
49 {
50  public:
51   // DIRPATH is the list of directories to search for libraries.
52   // INPUT is the file to read.  INPUT_GROUP is not NULL if we are in
53   // the middle of an input group.  THIS_BLOCKER is used to prevent
54   // the associated Add_symbols task from running before the previous
55   // one has completed; it will be NULL for the first task.
56   // NEXT_BLOCKER is used to block the next input file from adding
57   // symbols.
58   Read_symbols(Input_objects* input_objects, Symbol_table* symtab,
59                Layout* layout, Dirsearch* dirpath, int dirindex,
60                Mapfile* mapfile, const Input_argument* input_argument,
61                Input_group* input_group, Archive_member* member,
62                Task_token* this_blocker, Task_token* next_blocker)
63     : input_objects_(input_objects), symtab_(symtab), layout_(layout),
64       dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
65       input_argument_(input_argument), input_group_(input_group),
66       member_(member), this_blocker_(this_blocker),
67       next_blocker_(next_blocker)
68   { }
69
70   ~Read_symbols();
71
72   // If appropriate, issue a warning about skipping an incompatible
73   // object.
74   static void
75   incompatible_warning(const Input_argument*, const Input_file*);
76
77   // Requeue a Read_symbols task to search for the next object with
78   // the same name.
79   static void
80   requeue(Workqueue*, Input_objects*, Symbol_table*, Layout*, Dirsearch*,
81           int dirindex, Mapfile*, const Input_argument*, Input_group*,
82           Task_token* next_blocker);
83
84   // The standard Task methods.
85
86   Task_token*
87   is_runnable();
88
89   void
90   locks(Task_locker*);
91
92   void
93   run(Workqueue*);
94
95   std::string
96   get_name() const;
97
98  private:
99   // Handle an archive group.
100   void
101   do_group(Workqueue*);
102
103   // Handle --start-lib ... --end-lib
104   bool
105   do_lib_group(Workqueue*);
106
107   // Handle --whole-archive --start-lib ... --end-lib --no-whole-archive
108   bool
109   do_whole_lib_group(Workqueue*);
110
111   // Open and identify the file.
112   bool
113   do_read_symbols(Workqueue*);
114
115   Input_objects* input_objects_;
116   Symbol_table* symtab_;
117   Layout* layout_;
118   Dirsearch* dirpath_;
119   int dirindex_;
120   Mapfile* mapfile_;
121   const Input_argument* input_argument_;
122   Input_group* input_group_;
123   Archive_member* member_;
124   Task_token* this_blocker_;
125   Task_token* next_blocker_;
126 };
127
128 // This Task handles adding the symbols to the symbol table.  These
129 // tasks must be run in the same order as the arguments appear on the
130 // command line.
131
132 class Add_symbols : public Task
133 {
134  public:
135   // THIS_BLOCKER is used to prevent this task from running before the
136   // one for the previous input file.  NEXT_BLOCKER is used to prevent
137   // the next task from running.
138   Add_symbols(Input_objects* input_objects, Symbol_table* symtab,
139               Layout* layout, Dirsearch* dirpath, int dirindex,
140               Mapfile* mapfile, const Input_argument* input_argument,
141               Object* object,
142               Read_symbols_data* sd, Task_token* this_blocker,
143               Task_token* next_blocker)
144     : input_objects_(input_objects), symtab_(symtab), layout_(layout),
145       dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
146       input_argument_(input_argument),
147       object_(object), sd_(sd), this_blocker_(this_blocker),
148       next_blocker_(next_blocker)
149   { }
150
151   ~Add_symbols();
152
153   // The standard Task methods.
154
155   Task_token*
156   is_runnable();
157
158   void
159   locks(Task_locker*);
160
161   void
162   run(Workqueue*);
163
164   std::string
165   get_name() const
166   { return "Add_symbols " + this->object_->name(); }
167
168 private:
169   Input_objects* input_objects_;
170   Symbol_table* symtab_;
171   Layout* layout_;
172   Dirsearch* dirpath_;
173   int dirindex_;
174   Mapfile* mapfile_;
175   const Input_argument* input_argument_;
176   Object* object_;
177   Read_symbols_data* sd_;
178   Task_token* this_blocker_;
179   Task_token* next_blocker_;
180 };
181
182 // This class is used to track the archives in a group.
183
184 class Input_group
185 {
186  public:
187   typedef std::vector<Archive*> Archives;
188   typedef Archives::const_iterator const_iterator;
189
190   Input_group()
191     : archives_()
192   { }
193
194   // Add an archive to the group.
195   void
196   add_archive(Archive* arch)
197   { this->archives_.push_back(arch); }
198
199   // Loop over the archives in the group.
200
201   const_iterator
202   begin() const
203   { return this->archives_.begin(); }
204
205   const_iterator
206   end() const
207   { return this->archives_.end(); }
208
209  private:
210   Archives archives_;
211 };
212
213 // This class starts the handling of a group.  It exists only to pick
214 // up the number of undefined symbols at that point, so that we only
215 // run back through the group if we saw a new undefined symbol.
216
217 class Start_group : public Task
218 {
219  public:
220   Start_group(Symbol_table* symtab, Finish_group* finish_group,
221               Task_token* this_blocker, Task_token* next_blocker)
222     : symtab_(symtab), finish_group_(finish_group),
223       this_blocker_(this_blocker), next_blocker_(next_blocker)
224   { }
225
226   ~Start_group();
227
228   // The standard Task methods.
229
230   Task_token*
231   is_runnable();
232
233   void
234   locks(Task_locker*);
235
236   void
237   run(Workqueue*);
238
239   std::string
240   get_name() const
241   { return "Start_group"; }
242
243  private:
244   Symbol_table* symtab_;
245   Finish_group* finish_group_;
246   Task_token* this_blocker_;
247   Task_token* next_blocker_;
248 };
249
250 // This class is used to finish up handling a group.  It is just a
251 // closure.
252
253 class Finish_group : public Task
254 {
255  public:
256   Finish_group(Input_objects* input_objects, Symbol_table* symtab,
257                Layout* layout, Mapfile* mapfile, Input_group* input_group,
258                Task_token* next_blocker)
259     : input_objects_(input_objects), symtab_(symtab),
260       layout_(layout), mapfile_(mapfile), input_group_(input_group),
261       saw_undefined_(0), this_blocker_(NULL), next_blocker_(next_blocker)
262   { }
263
264   ~Finish_group();
265
266   // Set the number of undefined symbols when we start processing the
267   // group.  This is called by the Start_group task.
268   void
269   set_saw_undefined(size_t saw_undefined)
270   { this->saw_undefined_ = saw_undefined; }
271
272   // Set the blocker to use for this task.
273   void
274   set_blocker(Task_token* this_blocker)
275   {
276     gold_assert(this->this_blocker_ == NULL);
277     this->this_blocker_ = this_blocker;
278   }
279
280   // The standard Task methods.
281
282   Task_token*
283   is_runnable();
284
285   void
286   locks(Task_locker*);
287
288   void
289   run(Workqueue*);
290
291   std::string
292   get_name() const
293   { return "Finish_group"; }
294
295  private:
296   Input_objects* input_objects_;
297   Symbol_table* symtab_;
298   Layout* layout_;
299   Mapfile* mapfile_;
300   Input_group* input_group_;
301   size_t saw_undefined_;
302   Task_token* this_blocker_;
303   Task_token* next_blocker_;
304 };
305
306 // This class is used to read a file which was not recognized as an
307 // object or archive.  It tries to read it as a linker script, using
308 // the tokens to serialize with the calls to Add_symbols.
309
310 class Read_script : public Task
311 {
312  public:
313   Read_script(Symbol_table* symtab, Layout* layout, Dirsearch* dirpath,
314               int dirindex, Input_objects* input_objects, Mapfile* mapfile,
315               Input_group* input_group, const Input_argument* input_argument,
316               Input_file* input_file, Task_token* this_blocker,
317               Task_token* next_blocker)
318     : symtab_(symtab), layout_(layout), dirpath_(dirpath), dirindex_(dirindex),
319       input_objects_(input_objects), mapfile_(mapfile),
320       input_group_(input_group), input_argument_(input_argument),
321       input_file_(input_file), this_blocker_(this_blocker),
322       next_blocker_(next_blocker)
323   { }
324
325   ~Read_script();
326
327   // The standard Task methods.
328
329   Task_token*
330   is_runnable();
331
332   void
333   locks(Task_locker*);
334
335   void
336   run(Workqueue*);
337
338   std::string
339   get_name() const;
340
341  private:
342   Symbol_table* symtab_;
343   Layout* layout_;
344   Dirsearch* dirpath_;
345   int dirindex_;
346   Input_objects* input_objects_;
347   Mapfile* mapfile_;
348   Input_group* input_group_;
349   const Input_argument* input_argument_;
350   Input_file* input_file_;
351   Task_token* this_blocker_;
352   Task_token* next_blocker_;
353 };
354
355 } // end namespace gold
356
357 #endif // !defined(GOLD_READSYMS_H)