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