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