2010-03-15 Rafael Espindola <espindola@google.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, Task_token* this_blocker,
62                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       this_blocker_(this_blocker), next_blocker_(next_blocker)
67   { }
68
69   ~Read_symbols();
70
71   // If appropriate, issue a warning about skipping an incompatible
72   // object.
73   static void
74   incompatible_warning(const Input_argument*, const Input_file*);
75
76   // Requeue a Read_symbols task to search for the next object with
77   // the same name.
78   static void
79   requeue(Workqueue*, Input_objects*, Symbol_table*, Layout*, Dirsearch*,
80           int dirindex, Mapfile*, const Input_argument*, Input_group*,
81           Task_token* next_blocker);
82
83   // The standard Task methods.
84
85   Task_token*
86   is_runnable();
87
88   void
89   locks(Task_locker*);
90
91   void
92   run(Workqueue*);
93
94   std::string
95   get_name() const;
96
97  private:
98   // Handle an archive group.
99   void
100   do_group(Workqueue*);
101
102   // Open and identify the file.
103   bool
104   do_read_symbols(Workqueue*);
105
106   Input_objects* input_objects_;
107   Symbol_table* symtab_;
108   Layout* layout_;
109   Dirsearch* dirpath_;
110   int dirindex_;
111   Mapfile* mapfile_;
112   const Input_argument* input_argument_;
113   Input_group* input_group_;
114   Task_token* this_blocker_;
115   Task_token* next_blocker_;
116 };
117
118 // This Task handles adding the symbols to the symbol table.  These
119 // tasks must be run in the same order as the arguments appear on the
120 // command line.
121
122 class Add_symbols : public Task
123 {
124  public:
125   // THIS_BLOCKER is used to prevent this task from running before the
126   // one for the previous input file.  NEXT_BLOCKER is used to prevent
127   // the next task from running.
128   Add_symbols(Input_objects* input_objects, Symbol_table* symtab,
129               Layout* layout, Dirsearch* dirpath, int dirindex,
130               Mapfile* mapfile, const Input_argument* input_argument,
131               Object* object,
132               Read_symbols_data* sd, Task_token* this_blocker,
133               Task_token* next_blocker)
134     : input_objects_(input_objects), symtab_(symtab), layout_(layout),
135       dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
136       input_argument_(input_argument),
137       object_(object), sd_(sd), this_blocker_(this_blocker),
138       next_blocker_(next_blocker)
139   { }
140
141   ~Add_symbols();
142
143   // The standard Task methods.
144
145   Task_token*
146   is_runnable();
147
148   void
149   locks(Task_locker*);
150
151   void
152   run(Workqueue*);
153
154   std::string
155   get_name() const
156   { return "Add_symbols " + this->object_->name(); }
157
158 private:
159   Input_objects* input_objects_;
160   Symbol_table* symtab_;
161   Layout* layout_;
162   Dirsearch* dirpath_;
163   int dirindex_;
164   Mapfile* mapfile_;
165   const Input_argument* input_argument_;
166   Object* object_;
167   Read_symbols_data* sd_;
168   Task_token* this_blocker_;
169   Task_token* next_blocker_;
170 };
171
172 // This class is used to track the archives in a group.
173
174 class Input_group
175 {
176  public:
177   typedef std::vector<Archive*> Archives;
178   typedef Archives::const_iterator const_iterator;
179
180   Input_group()
181     : archives_()
182   { }
183
184   // Add an archive to the group.
185   void
186   add_archive(Archive* arch)
187   { this->archives_.push_back(arch); }
188
189   // Loop over the archives in the group.
190
191   const_iterator
192   begin() const
193   { return this->archives_.begin(); }
194
195   const_iterator
196   end() const
197   { return this->archives_.end(); }
198
199  private:
200   Archives archives_;
201 };
202
203 // This class starts the handling of a group.  It exists only to pick
204 // up the number of undefined symbols at that point, so that we only
205 // run back through the group if we saw a new undefined symbol.
206
207 class Start_group : public Task
208 {
209  public:
210   Start_group(Symbol_table* symtab, Finish_group* finish_group,
211               Task_token* this_blocker, Task_token* next_blocker)
212     : symtab_(symtab), finish_group_(finish_group),
213       this_blocker_(this_blocker), next_blocker_(next_blocker)
214   { }
215
216   ~Start_group();
217
218   // The standard Task methods.
219
220   Task_token*
221   is_runnable();
222
223   void
224   locks(Task_locker*);
225
226   void
227   run(Workqueue*);
228
229   std::string
230   get_name() const
231   { return "Start_group"; }
232
233  private:
234   Symbol_table* symtab_;
235   Finish_group* finish_group_;
236   Task_token* this_blocker_;
237   Task_token* next_blocker_;
238 };
239
240 // This class is used to finish up handling a group.  It is just a
241 // closure.
242
243 class Finish_group : public Task
244 {
245  public:
246   Finish_group(Input_objects* input_objects, Symbol_table* symtab,
247                Layout* layout, Mapfile* mapfile, Input_group* input_group,
248                Task_token* next_blocker)
249     : input_objects_(input_objects), symtab_(symtab),
250       layout_(layout), mapfile_(mapfile), input_group_(input_group),
251       saw_undefined_(0), this_blocker_(NULL), next_blocker_(next_blocker)
252   { }
253
254   ~Finish_group();
255
256   // Set the number of undefined symbols when we start processing the
257   // group.  This is called by the Start_group task.
258   void
259   set_saw_undefined(size_t saw_undefined)
260   { this->saw_undefined_ = saw_undefined; }
261
262   // Set the blocker to use for this task.
263   void
264   set_blocker(Task_token* this_blocker)
265   {
266     gold_assert(this->this_blocker_ == NULL);
267     this->this_blocker_ = this_blocker;
268   }
269
270   // The standard Task methods.
271
272   Task_token*
273   is_runnable();
274
275   void
276   locks(Task_locker*);
277
278   void
279   run(Workqueue*);
280
281   std::string
282   get_name() const
283   { return "Finish_group"; }
284
285  private:
286   Input_objects* input_objects_;
287   Symbol_table* symtab_;
288   Layout* layout_;
289   Mapfile* mapfile_;
290   Input_group* input_group_;
291   size_t saw_undefined_;
292   Task_token* this_blocker_;
293   Task_token* next_blocker_;
294 };
295
296 // This class is used to read a file which was not recognized as an
297 // object or archive.  It tries to read it as a linker script, using
298 // the tokens to serialize with the calls to Add_symbols.
299
300 class Read_script : public Task
301 {
302  public:
303   Read_script(Symbol_table* symtab, Layout* layout, Dirsearch* dirpath,
304               int dirindex, Input_objects* input_objects, Mapfile* mapfile,
305               Input_group* input_group, const Input_argument* input_argument,
306               Input_file* input_file, Task_token* this_blocker,
307               Task_token* next_blocker)
308     : symtab_(symtab), layout_(layout), dirpath_(dirpath), dirindex_(dirindex),
309       input_objects_(input_objects), mapfile_(mapfile),
310       input_group_(input_group), input_argument_(input_argument),
311       input_file_(input_file), this_blocker_(this_blocker),
312       next_blocker_(next_blocker)
313   { }
314
315   ~Read_script();
316
317   // The standard Task methods.
318
319   Task_token*
320   is_runnable();
321
322   void
323   locks(Task_locker*);
324
325   void
326   run(Workqueue*);
327
328   std::string
329   get_name() const;
330
331  private:
332   Symbol_table* symtab_;
333   Layout* layout_;
334   Dirsearch* dirpath_;
335   int dirindex_;
336   Input_objects* input_objects_;
337   Mapfile* mapfile_;
338   Input_group* input_group_;
339   const Input_argument* input_argument_;
340   Input_file* input_file_;
341   Task_token* this_blocker_;
342   Task_token* next_blocker_;
343 };
344
345 } // end namespace gold
346
347 #endif // !defined(GOLD_READSYMS_H)