Remove get_view_and_size.
[external/binutils.git] / gold / readsyms.cc
1 // readsyms.cc -- read input file symbols for gold
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 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "options.h"
29 #include "dirsearch.h"
30 #include "symtab.h"
31 #include "object.h"
32 #include "archive.h"
33 #include "script.h"
34 #include "readsyms.h"
35
36 namespace gold
37 {
38
39 // Class read_symbols.
40
41 Read_symbols::~Read_symbols()
42 {
43   // The this_blocker_ and next_blocker_ pointers are passed on to the
44   // Add_symbols task.
45 }
46
47 // Return whether a Read_symbols task is runnable.  We can read an
48 // ordinary input file immediately.  For an archive specified using
49 // -l, we have to wait until the search path is complete.
50
51 Task::Is_runnable_type
52 Read_symbols::is_runnable(Workqueue*)
53 {
54   if (this->input_argument_->is_file()
55       && this->input_argument_->file().is_lib()
56       && this->dirpath_.token().is_blocked())
57     return IS_BLOCKED;
58
59   return IS_RUNNABLE;
60 }
61
62 // Return a Task_locker for a Read_symbols task.  We don't need any
63 // locks here.
64
65 Task_locker*
66 Read_symbols::locks(Workqueue*)
67 {
68   return NULL;
69 }
70
71 // Run a Read_symbols task.  This is where we actually read the
72 // symbols and relocations.
73
74 void
75 Read_symbols::run(Workqueue* workqueue)
76 {
77   if (this->input_argument_->is_group())
78     {
79       gold_assert(this->input_group_ == NULL);
80       this->do_group(workqueue);
81       return;
82     }
83
84   Input_file* input_file = new Input_file(&this->input_argument_->file());
85   input_file->open(this->options_, this->dirpath_);
86
87   // Read enough of the file to pick up the entire ELF header.
88
89   const int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
90   unsigned char ehdr_buf[ehdr_size];
91   off_t bytes;
92   input_file->file().read_up_to(0, ehdr_size, ehdr_buf, &bytes);
93
94   if (bytes >= 4)
95     {
96       static unsigned char elfmagic[4] =
97         {
98           elfcpp::ELFMAG0, elfcpp::ELFMAG1,
99           elfcpp::ELFMAG2, elfcpp::ELFMAG3
100         };
101       if (memcmp(ehdr_buf, elfmagic, 4) == 0)
102         {
103           // This is an ELF object.
104
105           Object* obj = make_elf_object(input_file->filename(),
106                                         input_file, 0, ehdr_buf, bytes);
107
108           // We don't have a way to record a non-archive in an input
109           // group.  If this is an ordinary object file, we can't
110           // include it more than once anyhow.  If this is a dynamic
111           // object, then including it a second time changes nothing.
112           if (this->input_group_ != NULL && !obj->is_dynamic())
113             {
114               fprintf(stderr,
115                       _("%s: %s: ordinary object found in input group\n"),
116                       program_name, input_file->name());
117               gold_exit(false);
118             }
119
120           Read_symbols_data* sd = new Read_symbols_data;
121           obj->read_symbols(sd);
122           workqueue->queue_front(new Add_symbols(this->input_objects_,
123                                                  this->symtab_, this->layout_,
124                                                  obj, sd,
125                                                  this->this_blocker_,
126                                                  this->next_blocker_));
127
128           // Opening the file locked it, so now we need to unlock it.
129           input_file->file().unlock();
130
131           return;
132         }
133     }
134
135   if (bytes >= Archive::sarmag)
136     {
137       if (memcmp(ehdr_buf, Archive::armag, Archive::sarmag) == 0)
138         {
139           // This is an archive.
140           Archive* arch = new Archive(this->input_argument_->file().name(),
141                                       input_file);
142           arch->setup();
143           workqueue->queue(new Add_archive_symbols(this->symtab_,
144                                                    this->layout_,
145                                                    this->input_objects_,
146                                                    arch,
147                                                    this->input_group_,
148                                                    this->this_blocker_,
149                                                    this->next_blocker_));
150           return;
151         }
152     }
153
154   if (bytes == 0)
155     {
156       fprintf(stderr, _("%s: %s: file is empty\n"),
157               program_name, input_file->file().filename().c_str());
158       gold_exit(false);
159     }
160
161   // Try to parse this file as a script.
162   if (read_input_script(workqueue, this->options_, this->symtab_,
163                         this->layout_, this->dirpath_, this->input_objects_,
164                         this->input_group_, this->input_argument_, input_file,
165                         ehdr_buf, bytes, this->this_blocker_,
166                         this->next_blocker_))
167     return;
168
169   // Here we have to handle any other input file types we need.
170   fprintf(stderr, _("%s: %s: not an object or archive\n"),
171           program_name, input_file->file().filename().c_str());
172   gold_exit(false);
173 }
174
175 // Handle a group.  We need to walk through the arguments over and
176 // over until we don't see any new undefined symbols.  We do this by
177 // setting off Read_symbols Tasks as usual, but recording the archive
178 // entries instead of deleting them.  We also start a Finish_group
179 // Task which runs after we've read all the symbols.  In that task we
180 // process the archives in a loop until we are done.
181
182 void
183 Read_symbols::do_group(Workqueue* workqueue)
184 {
185   Input_group* input_group = new Input_group();
186
187   const Input_file_group* group = this->input_argument_->group();
188   Task_token* this_blocker = this->this_blocker_;
189   for (Input_file_group::const_iterator p = group->begin();
190        p != group->end();
191        ++p)
192     {
193       const Input_argument* arg = &*p;
194       gold_assert(arg->is_file());
195
196       Task_token* next_blocker = new Task_token();
197       next_blocker->add_blocker();
198       workqueue->queue(new Read_symbols(this->options_, this->input_objects_,
199                                         this->symtab_, this->layout_,
200                                         this->dirpath_, arg, input_group,
201                                         this_blocker, next_blocker));
202       this_blocker = next_blocker;
203     }
204
205   const int saw_undefined = this->symtab_->saw_undefined();
206   workqueue->queue(new Finish_group(this->input_objects_,
207                                     this->symtab_,
208                                     this->layout_,
209                                     input_group,
210                                     saw_undefined,
211                                     this_blocker,
212                                     this->next_blocker_));
213 }
214
215 // Class Add_symbols.
216
217 Add_symbols::~Add_symbols()
218 {
219   if (this->this_blocker_ != NULL)
220     delete this->this_blocker_;
221   // next_blocker_ is deleted by the task associated with the next
222   // input file.
223 }
224
225 // We are blocked by this_blocker_.  We block next_blocker_.  We also
226 // lock the file.
227
228 Task::Is_runnable_type
229 Add_symbols::is_runnable(Workqueue*)
230 {
231   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
232     return IS_BLOCKED;
233   if (this->object_->is_locked())
234     return IS_LOCKED;
235   return IS_RUNNABLE;
236 }
237
238 class Add_symbols::Add_symbols_locker : public Task_locker
239 {
240  public:
241   Add_symbols_locker(Task_token& token, Workqueue* workqueue,
242                      Object* object)
243     : blocker_(token, workqueue), objlock_(*object)
244   { }
245
246  private:
247   Task_locker_block blocker_;
248   Task_locker_obj<Object> objlock_;
249 };
250
251 Task_locker*
252 Add_symbols::locks(Workqueue* workqueue)
253 {
254   return new Add_symbols_locker(*this->next_blocker_, workqueue,
255                                 this->object_);
256 }
257
258 // Add the symbols in the object to the symbol table.
259
260 void
261 Add_symbols::run(Workqueue*)
262 {
263   if (!this->input_objects_->add_object(this->object_))
264     {
265       // FIXME: We need to close the descriptor here.
266       delete this->object_;
267     }
268   else
269     {
270       this->object_->layout(this->symtab_, this->layout_, this->sd_);
271       this->object_->add_symbols(this->symtab_, this->sd_);
272     }
273   delete this->sd_;
274   this->sd_ = NULL;
275 }
276
277 // Class Finish_group.
278
279 Finish_group::~Finish_group()
280 {
281   if (this->this_blocker_ != NULL)
282     delete this->this_blocker_;
283   // next_blocker_ is deleted by the task associated with the next
284   // input file following the group.
285 }
286
287 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
288
289 Task::Is_runnable_type
290 Finish_group::is_runnable(Workqueue*)
291 {
292   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
293     return IS_BLOCKED;
294   return IS_RUNNABLE;
295 }
296
297 Task_locker*
298 Finish_group::locks(Workqueue* workqueue)
299 {
300   return new Task_locker_block(*this->next_blocker_, workqueue);
301 }
302
303 // Loop over the archives until there are no new undefined symbols.
304
305 void
306 Finish_group::run(Workqueue*)
307 {
308   int saw_undefined = this->saw_undefined_;
309   while (saw_undefined != this->symtab_->saw_undefined())
310     {
311       saw_undefined = this->symtab_->saw_undefined();
312
313       for (Input_group::const_iterator p = this->input_group_->begin();
314            p != this->input_group_->end();
315            ++p)
316         {
317           Task_lock_obj<Archive> tl(**p);
318
319           (*p)->add_symbols(this->symtab_, this->layout_,
320                             this->input_objects_);
321         }
322     }
323
324   // Delete all the archives now that we no longer need them.
325   for (Input_group::const_iterator p = this->input_group_->begin();
326        p != this->input_group_->end();
327        ++p)
328     delete *p;
329   delete this->input_group_;
330 }
331
332 } // End namespace gold.