Commit | Line | Data |
---|---|---|
6d03d481 | 1 | // plugin.cc -- plugin manager for gold -*- C++ -*- |
89fc3421 | 2 | |
0f7c0701 | 3 | // Copyright 2008, 2009 Free Software Foundation, Inc. |
89fc3421 CC |
4 | // Written by Cary Coutant <ccoutant@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 <cstdio> | |
24 | #include <cstdarg> | |
25 | #include <cstring> | |
26 | #include <string> | |
27 | #include <vector> | |
bc06c745 ILT |
28 | |
29 | #ifdef ENABLE_PLUGINS | |
89fc3421 | 30 | #include <dlfcn.h> |
bc06c745 | 31 | #endif |
89fc3421 CC |
32 | |
33 | #include "gold.h" | |
34 | #include "parameters.h" | |
35 | #include "errors.h" | |
36 | #include "fileread.h" | |
37 | #include "layout.h" | |
38 | #include "options.h" | |
39 | #include "plugin.h" | |
40 | #include "target.h" | |
41 | #include "readsyms.h" | |
42 | #include "symtab.h" | |
43 | #include "elfcpp.h" | |
44 | ||
45 | namespace gold | |
46 | { | |
47 | ||
48 | #ifdef ENABLE_PLUGINS | |
49 | ||
50 | // The linker's exported interfaces. | |
51 | ||
52 | extern "C" | |
53 | { | |
54 | ||
55 | static enum ld_plugin_status | |
56 | register_claim_file(ld_plugin_claim_file_handler handler); | |
57 | ||
58 | static enum ld_plugin_status | |
59 | register_all_symbols_read(ld_plugin_all_symbols_read_handler handler); | |
60 | ||
61 | static enum ld_plugin_status | |
62 | register_cleanup(ld_plugin_cleanup_handler handler); | |
63 | ||
64 | static enum ld_plugin_status | |
65 | add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms); | |
66 | ||
0f7c0701 CC |
67 | static enum ld_plugin_status |
68 | get_input_file(const void *handle, struct ld_plugin_input_file *file); | |
69 | ||
70 | static enum ld_plugin_status | |
71 | release_input_file(const void *handle); | |
72 | ||
89fc3421 CC |
73 | static enum ld_plugin_status |
74 | get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms); | |
75 | ||
76 | static enum ld_plugin_status | |
77 | add_input_file(char *pathname); | |
78 | ||
e99daf92 ILT |
79 | static enum ld_plugin_status |
80 | add_input_library(char *pathname); | |
81 | ||
89fc3421 | 82 | static enum ld_plugin_status |
6c52134c | 83 | message(int level, const char *format, ...); |
89fc3421 CC |
84 | |
85 | }; | |
86 | ||
87 | #endif // ENABLE_PLUGINS | |
88 | ||
89 | static Pluginobj* make_sized_plugin_object(Input_file* input_file, | |
0f7c0701 | 90 | off_t offset, off_t filesize); |
89fc3421 CC |
91 | |
92 | // Plugin methods. | |
93 | ||
94 | // Load one plugin library. | |
95 | ||
96 | void | |
97 | Plugin::load() | |
98 | { | |
99 | #ifdef ENABLE_PLUGINS | |
89fc3421 CC |
100 | // Load the plugin library. |
101 | // FIXME: Look for the library in standard locations. | |
4674ecfc | 102 | this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW); |
89fc3421 CC |
103 | if (this->handle_ == NULL) |
104 | { | |
4674ecfc CC |
105 | gold_error(_("%s: could not load plugin library"), |
106 | this->filename_.c_str()); | |
89fc3421 CC |
107 | return; |
108 | } | |
109 | ||
110 | // Find the plugin's onload entry point. | |
111 | ld_plugin_onload onload = reinterpret_cast<ld_plugin_onload> | |
112 | (dlsym(this->handle_, "onload")); | |
113 | if (onload == NULL) | |
114 | { | |
4674ecfc CC |
115 | gold_error(_("%s: could not find onload entry point"), |
116 | this->filename_.c_str()); | |
89fc3421 CC |
117 | return; |
118 | } | |
119 | ||
120 | // Get the linker's version number. | |
121 | const char* ver = get_version_string(); | |
122 | int major = 0; | |
123 | int minor = 0; | |
124 | sscanf(ver, "%d.%d", &major, &minor); | |
125 | ||
126 | // Allocate and populate a transfer vector. | |
e99daf92 | 127 | const int tv_fixed_size = 14; |
4674ecfc | 128 | int tv_size = this->args_.size() + tv_fixed_size; |
89fc3421 CC |
129 | ld_plugin_tv *tv = new ld_plugin_tv[tv_size]; |
130 | ||
abc8dcba CC |
131 | // Put LDPT_MESSAGE at the front of the list so the plugin can use it |
132 | // while processing subsequent entries. | |
89fc3421 | 133 | int i = 0; |
abc8dcba CC |
134 | tv[i].tv_tag = LDPT_MESSAGE; |
135 | tv[i].tv_u.tv_message = message; | |
136 | ||
137 | ++i; | |
89fc3421 CC |
138 | tv[i].tv_tag = LDPT_API_VERSION; |
139 | tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION; | |
140 | ||
141 | ++i; | |
142 | tv[i].tv_tag = LDPT_GOLD_VERSION; | |
143 | tv[i].tv_u.tv_val = major * 100 + minor; | |
144 | ||
145 | ++i; | |
146 | tv[i].tv_tag = LDPT_LINKER_OUTPUT; | |
147 | if (parameters->options().relocatable()) | |
148 | tv[i].tv_u.tv_val = LDPO_REL; | |
149 | else if (parameters->options().shared()) | |
150 | tv[i].tv_u.tv_val = LDPO_DYN; | |
151 | else | |
152 | tv[i].tv_u.tv_val = LDPO_EXEC; | |
153 | ||
4674ecfc | 154 | for (unsigned int j = 0; j < this->args_.size(); ++j) |
89fc3421 CC |
155 | { |
156 | ++i; | |
157 | tv[i].tv_tag = LDPT_OPTION; | |
4674ecfc | 158 | tv[i].tv_u.tv_string = this->args_[j].c_str(); |
89fc3421 CC |
159 | } |
160 | ||
161 | ++i; | |
162 | tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK; | |
163 | tv[i].tv_u.tv_register_claim_file = register_claim_file; | |
164 | ||
165 | ++i; | |
166 | tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK; | |
167 | tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read; | |
168 | ||
169 | ++i; | |
170 | tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK; | |
171 | tv[i].tv_u.tv_register_cleanup = register_cleanup; | |
172 | ||
173 | ++i; | |
174 | tv[i].tv_tag = LDPT_ADD_SYMBOLS; | |
175 | tv[i].tv_u.tv_add_symbols = add_symbols; | |
176 | ||
0f7c0701 CC |
177 | ++i; |
178 | tv[i].tv_tag = LDPT_GET_INPUT_FILE; | |
179 | tv[i].tv_u.tv_get_input_file = get_input_file; | |
180 | ||
181 | ++i; | |
182 | tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE; | |
183 | tv[i].tv_u.tv_release_input_file = release_input_file; | |
184 | ||
89fc3421 CC |
185 | ++i; |
186 | tv[i].tv_tag = LDPT_GET_SYMBOLS; | |
187 | tv[i].tv_u.tv_get_symbols = get_symbols; | |
188 | ||
189 | ++i; | |
190 | tv[i].tv_tag = LDPT_ADD_INPUT_FILE; | |
191 | tv[i].tv_u.tv_add_input_file = add_input_file; | |
192 | ||
e99daf92 ILT |
193 | ++i; |
194 | tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY; | |
195 | tv[i].tv_u.tv_add_input_library = add_input_library; | |
196 | ||
89fc3421 CC |
197 | ++i; |
198 | tv[i].tv_tag = LDPT_NULL; | |
199 | tv[i].tv_u.tv_val = 0; | |
200 | ||
201 | gold_assert(i == tv_size - 1); | |
202 | ||
203 | // Call the onload entry point. | |
204 | (*onload)(tv); | |
205 | ||
6c52134c | 206 | delete[] tv; |
89fc3421 CC |
207 | #endif // ENABLE_PLUGINS |
208 | } | |
209 | ||
210 | // Call the plugin claim-file handler. | |
211 | ||
212 | inline bool | |
213 | Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file) | |
214 | { | |
215 | int claimed = 0; | |
216 | ||
217 | if (this->claim_file_handler_ != NULL) | |
218 | { | |
219 | (*this->claim_file_handler_)(plugin_input_file, &claimed); | |
220 | if (claimed) | |
221 | return true; | |
222 | } | |
223 | return false; | |
224 | } | |
225 | ||
226 | // Call the all-symbols-read handler. | |
227 | ||
228 | inline void | |
229 | Plugin::all_symbols_read() | |
230 | { | |
231 | if (this->all_symbols_read_handler_ != NULL) | |
232 | (*this->all_symbols_read_handler_)(); | |
233 | } | |
234 | ||
235 | // Call the cleanup handler. | |
236 | ||
237 | inline void | |
238 | Plugin::cleanup() | |
239 | { | |
240 | if (this->cleanup_handler_ != NULL) | |
241 | (*this->cleanup_handler_)(); | |
242 | } | |
243 | ||
244 | // Plugin_manager methods. | |
245 | ||
246 | Plugin_manager::~Plugin_manager() | |
247 | { | |
248 | for (Plugin_list::iterator p = this->plugins_.begin(); | |
249 | p != this->plugins_.end(); | |
250 | ++p) | |
251 | delete *p; | |
252 | this->plugins_.clear(); | |
253 | for (Object_list::iterator obj = this->objects_.begin(); | |
254 | obj != this->objects_.end(); | |
255 | ++obj) | |
256 | delete *obj; | |
257 | this->objects_.clear(); | |
258 | } | |
259 | ||
260 | // Load all plugin libraries. | |
261 | ||
262 | void | |
263 | Plugin_manager::load_plugins() | |
264 | { | |
265 | for (this->current_ = this->plugins_.begin(); | |
266 | this->current_ != this->plugins_.end(); | |
267 | ++this->current_) | |
268 | (*this->current_)->load(); | |
269 | } | |
270 | ||
271 | // Call the plugin claim-file handlers in turn to see if any claim the file. | |
272 | ||
273 | Pluginobj* | |
274 | Plugin_manager::claim_file(Input_file* input_file, off_t offset, | |
275 | off_t filesize) | |
276 | { | |
277 | if (this->in_replacement_phase_) | |
278 | return NULL; | |
279 | ||
280 | unsigned int handle = this->objects_.size(); | |
281 | this->input_file_ = input_file; | |
282 | this->plugin_input_file_.name = input_file->filename().c_str(); | |
283 | this->plugin_input_file_.fd = input_file->file().descriptor(); | |
284 | this->plugin_input_file_.offset = offset; | |
285 | this->plugin_input_file_.filesize = filesize; | |
286 | this->plugin_input_file_.handle = reinterpret_cast<void*>(handle); | |
287 | ||
288 | for (this->current_ = this->plugins_.begin(); | |
289 | this->current_ != this->plugins_.end(); | |
290 | ++this->current_) | |
291 | { | |
292 | if ((*this->current_)->claim_file(&this->plugin_input_file_)) | |
293 | { | |
abc8dcba CC |
294 | if (this->objects_.size() > handle) |
295 | return this->objects_[handle]; | |
296 | ||
297 | // If the plugin claimed the file but did not call the | |
298 | // add_symbols callback, we need to create the Pluginobj now. | |
299 | Pluginobj* obj = this->make_plugin_object(handle); | |
300 | return obj; | |
89fc3421 CC |
301 | } |
302 | } | |
303 | ||
304 | return NULL; | |
305 | } | |
306 | ||
307 | // Call the all-symbols-read handlers. | |
308 | ||
309 | void | |
0f7c0701 | 310 | Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task, |
89fc3421 CC |
311 | Input_objects* input_objects, |
312 | Symbol_table* symtab, Layout* layout, | |
313 | Dirsearch* dirpath, Mapfile* mapfile, | |
314 | Task_token** last_blocker) | |
315 | { | |
316 | this->in_replacement_phase_ = true; | |
317 | this->workqueue_ = workqueue; | |
0f7c0701 | 318 | this->task_ = task; |
89fc3421 CC |
319 | this->input_objects_ = input_objects; |
320 | this->symtab_ = symtab; | |
321 | this->layout_ = layout; | |
322 | this->dirpath_ = dirpath; | |
323 | this->mapfile_ = mapfile; | |
324 | this->this_blocker_ = NULL; | |
325 | ||
326 | for (this->current_ = this->plugins_.begin(); | |
327 | this->current_ != this->plugins_.end(); | |
328 | ++this->current_) | |
329 | (*this->current_)->all_symbols_read(); | |
330 | ||
331 | *last_blocker = this->this_blocker_; | |
332 | } | |
333 | ||
483620e8 | 334 | // Layout deferred objects. |
89fc3421 CC |
335 | |
336 | void | |
483620e8 | 337 | Plugin_manager::layout_deferred_objects() |
89fc3421 | 338 | { |
5995b570 CC |
339 | Deferred_layout_list::iterator obj; |
340 | ||
341 | for (obj = this->deferred_layout_objects_.begin(); | |
342 | obj != this->deferred_layout_objects_.end(); | |
343 | ++obj) | |
344 | (*obj)->layout_deferred_sections(this->layout_); | |
483620e8 CC |
345 | } |
346 | ||
347 | // Call the cleanup handlers. | |
5995b570 | 348 | |
483620e8 CC |
349 | void |
350 | Plugin_manager::cleanup() | |
351 | { | |
352 | if (this->cleanup_done_) | |
353 | return; | |
89fc3421 CC |
354 | for (this->current_ = this->plugins_.begin(); |
355 | this->current_ != this->plugins_.end(); | |
356 | ++this->current_) | |
357 | (*this->current_)->cleanup(); | |
483620e8 | 358 | this->cleanup_done_ = true; |
89fc3421 CC |
359 | } |
360 | ||
361 | // Make a new Pluginobj object. This is called when the plugin calls | |
362 | // the add_symbols API. | |
363 | ||
364 | Pluginobj* | |
365 | Plugin_manager::make_plugin_object(unsigned int handle) | |
366 | { | |
367 | // Make sure we aren't asked to make an object for the same handle twice. | |
368 | if (this->objects_.size() != handle) | |
369 | return NULL; | |
370 | ||
371 | Pluginobj* obj = make_sized_plugin_object(this->input_file_, | |
0f7c0701 CC |
372 | this->plugin_input_file_.offset, |
373 | this->plugin_input_file_.filesize); | |
89fc3421 CC |
374 | this->objects_.push_back(obj); |
375 | return obj; | |
376 | } | |
377 | ||
0f7c0701 CC |
378 | // Get the input file information with an open (possibly re-opened) |
379 | // file descriptor. | |
380 | ||
381 | ld_plugin_status | |
382 | Plugin_manager::get_input_file(unsigned int handle, | |
383 | struct ld_plugin_input_file *file) | |
384 | { | |
385 | Pluginobj* obj = this->object(handle); | |
386 | if (obj == NULL) | |
387 | return LDPS_BAD_HANDLE; | |
388 | ||
389 | obj->lock(this->task_); | |
390 | file->name = obj->filename().c_str(); | |
391 | file->fd = obj->descriptor(); | |
392 | file->offset = obj->offset(); | |
393 | file->filesize = obj->filesize(); | |
394 | file->handle = reinterpret_cast<void*>(handle); | |
395 | return LDPS_OK; | |
396 | } | |
397 | ||
398 | // Release the input file. | |
399 | ||
400 | ld_plugin_status | |
401 | Plugin_manager::release_input_file(unsigned int handle) | |
402 | { | |
403 | Pluginobj* obj = this->object(handle); | |
404 | if (obj == NULL) | |
405 | return LDPS_BAD_HANDLE; | |
406 | ||
407 | obj->unlock(this->task_); | |
408 | return LDPS_OK; | |
409 | } | |
410 | ||
89fc3421 CC |
411 | // Add a new input file. |
412 | ||
413 | ld_plugin_status | |
e99daf92 | 414 | Plugin_manager::add_input_file(char *pathname, bool is_lib) |
89fc3421 | 415 | { |
e99daf92 | 416 | Input_file_argument file(pathname, is_lib, "", false, this->options_); |
89fc3421 CC |
417 | Input_argument* input_argument = new Input_argument(file); |
418 | Task_token* next_blocker = new Task_token(true); | |
419 | next_blocker->add_blocker(); | |
072fe7ce ILT |
420 | if (this->layout_->incremental_inputs()) |
421 | gold_error(_("Input files added by plug-ins in --incremental mode not " | |
422 | "supported yet.\n")); | |
f1ed28fb | 423 | this->workqueue_->queue_soon(new Read_symbols(this->input_objects_, |
89fc3421 CC |
424 | this->symtab_, |
425 | this->layout_, | |
426 | this->dirpath_, | |
15f8229b | 427 | 0, |
89fc3421 CC |
428 | this->mapfile_, |
429 | input_argument, | |
430 | NULL, | |
431 | this->this_blocker_, | |
432 | next_blocker)); | |
433 | this->this_blocker_ = next_blocker; | |
434 | return LDPS_OK; | |
435 | } | |
436 | ||
437 | // Class Pluginobj. | |
438 | ||
439 | Pluginobj::Pluginobj(const std::string& name, Input_file* input_file, | |
0f7c0701 | 440 | off_t offset, off_t filesize) |
89fc3421 | 441 | : Object(name, input_file, false, offset), |
0f7c0701 | 442 | nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_() |
89fc3421 CC |
443 | { |
444 | } | |
445 | ||
d66a9eb3 CC |
446 | // Return TRUE if a defined symbol might be reachable from outside the |
447 | // universe of claimed objects. | |
448 | ||
449 | static inline bool | |
450 | is_visible_from_outside(Symbol* lsym) | |
451 | { | |
452 | if (lsym->in_real_elf()) | |
453 | return true; | |
454 | if (parameters->options().relocatable()) | |
455 | return true; | |
456 | if (parameters->options().export_dynamic() || parameters->options().shared()) | |
457 | return lsym->is_externally_visible(); | |
458 | return false; | |
459 | } | |
460 | ||
89fc3421 CC |
461 | // Get symbol resolution info. |
462 | ||
463 | ld_plugin_status | |
464 | Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const | |
465 | { | |
abc8dcba | 466 | if (nsyms > this->nsyms_) |
89fc3421 CC |
467 | return LDPS_NO_SYMS; |
468 | for (int i = 0; i < nsyms; i++) | |
469 | { | |
470 | ld_plugin_symbol* isym = &syms[i]; | |
471 | Symbol* lsym = this->symbols_[i]; | |
472 | ld_plugin_symbol_resolution res = LDPR_UNKNOWN; | |
473 | ||
474 | if (lsym->is_undefined()) | |
475 | // The symbol remains undefined. | |
476 | res = LDPR_UNDEF; | |
477 | else if (isym->def == LDPK_UNDEF | |
478 | || isym->def == LDPK_WEAKUNDEF | |
479 | || isym->def == LDPK_COMMON) | |
480 | { | |
481 | // The original symbol was undefined or common. | |
482 | if (lsym->source() != Symbol::FROM_OBJECT) | |
483 | res = LDPR_RESOLVED_EXEC; | |
484 | else if (lsym->object()->pluginobj() != NULL) | |
485 | res = LDPR_RESOLVED_IR; | |
486 | else if (lsym->object()->is_dynamic()) | |
487 | res = LDPR_RESOLVED_DYN; | |
488 | else | |
489 | res = LDPR_RESOLVED_EXEC; | |
490 | } | |
491 | else | |
492 | { | |
493 | // The original symbol was a definition. | |
494 | if (lsym->source() != Symbol::FROM_OBJECT) | |
495 | res = LDPR_PREEMPTED_REG; | |
496 | else if (lsym->object() == static_cast<const Object*>(this)) | |
d66a9eb3 | 497 | res = (is_visible_from_outside(lsym) |
89fc3421 CC |
498 | ? LDPR_PREVAILING_DEF |
499 | : LDPR_PREVAILING_DEF_IRONLY); | |
500 | else | |
501 | res = (lsym->object()->pluginobj() != NULL | |
502 | ? LDPR_PREEMPTED_IR | |
503 | : LDPR_PREEMPTED_REG); | |
504 | } | |
505 | isym->resolution = res; | |
506 | } | |
507 | return LDPS_OK; | |
508 | } | |
509 | ||
510 | // Return TRUE if the comdat group with key COMDAT_KEY from this object | |
511 | // should be kept. | |
512 | ||
513 | bool | |
514 | Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout) | |
515 | { | |
516 | std::pair<Comdat_map::iterator, bool> ins = | |
517 | this->comdat_map_.insert(std::make_pair(comdat_key, false)); | |
518 | ||
519 | // If this is the first time we've seen this comdat key, ask the | |
520 | // layout object whether it should be included. | |
521 | if (ins.second) | |
1ef4d87f ILT |
522 | ins.first->second = layout->find_or_add_kept_section(comdat_key, |
523 | NULL, 0, true, | |
524 | true, NULL); | |
89fc3421 CC |
525 | |
526 | return ins.first->second; | |
527 | } | |
528 | ||
529 | // Class Sized_pluginobj. | |
530 | ||
531 | template<int size, bool big_endian> | |
532 | Sized_pluginobj<size, big_endian>::Sized_pluginobj( | |
533 | const std::string& name, | |
534 | Input_file* input_file, | |
0f7c0701 CC |
535 | off_t offset, |
536 | off_t filesize) | |
537 | : Pluginobj(name, input_file, offset, filesize) | |
89fc3421 CC |
538 | { |
539 | } | |
540 | ||
541 | // Read the symbols. Not used for plugin objects. | |
542 | ||
543 | template<int size, bool big_endian> | |
544 | void | |
545 | Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*) | |
546 | { | |
547 | gold_unreachable(); | |
548 | } | |
549 | ||
550 | // Lay out the input sections. Not used for plugin objects. | |
551 | ||
552 | template<int size, bool big_endian> | |
553 | void | |
554 | Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*, | |
555 | Read_symbols_data*) | |
556 | { | |
557 | gold_unreachable(); | |
558 | } | |
559 | ||
560 | // Add the symbols to the symbol table. | |
561 | ||
89fc3421 CC |
562 | template<int size, bool big_endian> |
563 | void | |
564 | Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab, | |
f488e4b0 | 565 | Read_symbols_data*, |
89fc3421 CC |
566 | Layout* layout) |
567 | { | |
568 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
569 | unsigned char symbuf[sym_size]; | |
570 | elfcpp::Sym<size, big_endian> sym(symbuf); | |
571 | elfcpp::Sym_write<size, big_endian> osym(symbuf); | |
572 | ||
573 | typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type; | |
574 | ||
575 | this->symbols_.resize(this->nsyms_); | |
576 | ||
577 | for (int i = 0; i < this->nsyms_; ++i) | |
578 | { | |
579 | const struct ld_plugin_symbol *isym = &this->syms_[i]; | |
580 | const char* name = isym->name; | |
581 | const char* ver = isym->version; | |
582 | elfcpp::Elf_Half shndx; | |
583 | elfcpp::STB bind; | |
584 | elfcpp::STV vis; | |
585 | ||
586 | if (name != NULL && name[0] == '\0') | |
587 | name = NULL; | |
588 | if (ver != NULL && ver[0] == '\0') | |
589 | ver = NULL; | |
590 | ||
591 | switch (isym->def) | |
592 | { | |
593 | case LDPK_WEAKDEF: | |
594 | case LDPK_WEAKUNDEF: | |
595 | bind = elfcpp::STB_WEAK; | |
596 | break; | |
597 | case LDPK_DEF: | |
598 | case LDPK_UNDEF: | |
599 | case LDPK_COMMON: | |
600 | default: | |
601 | bind = elfcpp::STB_GLOBAL; | |
602 | break; | |
603 | } | |
604 | ||
605 | switch (isym->def) | |
606 | { | |
607 | case LDPK_DEF: | |
608 | case LDPK_WEAKDEF: | |
609 | shndx = elfcpp::SHN_ABS; | |
610 | break; | |
611 | case LDPK_COMMON: | |
612 | shndx = elfcpp::SHN_COMMON; | |
613 | break; | |
614 | case LDPK_UNDEF: | |
615 | case LDPK_WEAKUNDEF: | |
616 | default: | |
617 | shndx = elfcpp::SHN_UNDEF; | |
618 | break; | |
619 | } | |
620 | ||
621 | switch (isym->visibility) | |
622 | { | |
623 | case LDPV_PROTECTED: | |
624 | vis = elfcpp::STV_DEFAULT; | |
625 | break; | |
626 | case LDPV_INTERNAL: | |
627 | vis = elfcpp::STV_DEFAULT; | |
628 | break; | |
629 | case LDPV_HIDDEN: | |
630 | vis = elfcpp::STV_DEFAULT; | |
631 | break; | |
632 | case LDPV_DEFAULT: | |
633 | default: | |
634 | vis = elfcpp::STV_DEFAULT; | |
635 | break; | |
636 | } | |
637 | ||
638 | if (isym->comdat_key != NULL | |
639 | && isym->comdat_key[0] != '\0' | |
640 | && !this->include_comdat_group(isym->comdat_key, layout)) | |
641 | shndx = elfcpp::SHN_UNDEF; | |
642 | ||
643 | osym.put_st_name(0); | |
644 | osym.put_st_value(0); | |
645 | osym.put_st_size(static_cast<Elf_size_type>(isym->size)); | |
646 | osym.put_st_info(bind, elfcpp::STT_NOTYPE); | |
647 | osym.put_st_other(vis, 0); | |
648 | osym.put_st_shndx(shndx); | |
649 | ||
650 | this->symbols_[i] = | |
651 | symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym); | |
652 | } | |
653 | } | |
654 | ||
655 | // Get the size of a section. Not used for plugin objects. | |
656 | ||
657 | template<int size, bool big_endian> | |
658 | uint64_t | |
659 | Sized_pluginobj<size, big_endian>::do_section_size(unsigned int) | |
660 | { | |
661 | gold_unreachable(); | |
662 | return 0; | |
663 | } | |
664 | ||
665 | // Get the name of a section. Not used for plugin objects. | |
666 | ||
667 | template<int size, bool big_endian> | |
668 | std::string | |
669 | Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) | |
670 | { | |
671 | gold_unreachable(); | |
672 | return std::string(); | |
673 | } | |
674 | ||
675 | // Return a view of the contents of a section. Not used for plugin objects. | |
676 | ||
677 | template<int size, bool big_endian> | |
678 | Object::Location | |
679 | Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int) | |
680 | { | |
681 | Location loc(0, 0); | |
682 | ||
683 | gold_unreachable(); | |
684 | return loc; | |
685 | } | |
686 | ||
687 | // Return section flags. Not used for plugin objects. | |
688 | ||
689 | template<int size, bool big_endian> | |
690 | uint64_t | |
691 | Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int) | |
692 | { | |
693 | gold_unreachable(); | |
694 | return 0; | |
695 | } | |
696 | ||
ef15dade ST |
697 | // Return section entsize. Not used for plugin objects. |
698 | ||
699 | template<int size, bool big_endian> | |
700 | uint64_t | |
701 | Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int) | |
702 | { | |
703 | gold_unreachable(); | |
704 | return 0; | |
705 | } | |
706 | ||
89fc3421 CC |
707 | // Return section address. Not used for plugin objects. |
708 | ||
709 | template<int size, bool big_endian> | |
710 | uint64_t | |
711 | Sized_pluginobj<size, big_endian>::do_section_address(unsigned int) | |
712 | { | |
713 | gold_unreachable(); | |
714 | return 0; | |
715 | } | |
716 | ||
717 | // Return section type. Not used for plugin objects. | |
718 | ||
719 | template<int size, bool big_endian> | |
720 | unsigned int | |
721 | Sized_pluginobj<size, big_endian>::do_section_type(unsigned int) | |
722 | { | |
723 | gold_unreachable(); | |
724 | return 0; | |
725 | } | |
726 | ||
727 | // Return the section link field. Not used for plugin objects. | |
728 | ||
729 | template<int size, bool big_endian> | |
730 | unsigned int | |
731 | Sized_pluginobj<size, big_endian>::do_section_link(unsigned int) | |
732 | { | |
733 | gold_unreachable(); | |
734 | return 0; | |
735 | } | |
736 | ||
737 | // Return the section link field. Not used for plugin objects. | |
738 | ||
739 | template<int size, bool big_endian> | |
740 | unsigned int | |
741 | Sized_pluginobj<size, big_endian>::do_section_info(unsigned int) | |
742 | { | |
743 | gold_unreachable(); | |
744 | return 0; | |
745 | } | |
746 | ||
747 | // Return the section alignment. Not used for plugin objects. | |
748 | ||
749 | template<int size, bool big_endian> | |
750 | uint64_t | |
751 | Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int) | |
752 | { | |
753 | gold_unreachable(); | |
754 | return 0; | |
755 | } | |
756 | ||
757 | // Return the Xindex structure to use. Not used for plugin objects. | |
758 | ||
759 | template<int size, bool big_endian> | |
760 | Xindex* | |
761 | Sized_pluginobj<size, big_endian>::do_initialize_xindex() | |
762 | { | |
763 | gold_unreachable(); | |
764 | return NULL; | |
765 | } | |
766 | ||
767 | // Get symbol counts. Not used for plugin objects. | |
768 | ||
769 | template<int size, bool big_endian> | |
770 | void | |
771 | Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*, | |
772 | size_t*, size_t*) const | |
773 | { | |
774 | gold_unreachable(); | |
775 | } | |
776 | ||
5995b570 | 777 | // Class Plugin_finish. This task runs after all replacement files have |
6d03d481 | 778 | // been added. It calls each plugin's cleanup handler. |
89fc3421 | 779 | |
5995b570 | 780 | class Plugin_finish : public Task |
89fc3421 CC |
781 | { |
782 | public: | |
5995b570 | 783 | Plugin_finish(Task_token* this_blocker, Task_token* next_blocker) |
89fc3421 CC |
784 | : this_blocker_(this_blocker), next_blocker_(next_blocker) |
785 | { } | |
786 | ||
5995b570 | 787 | ~Plugin_finish() |
89fc3421 CC |
788 | { |
789 | if (this->this_blocker_ != NULL) | |
790 | delete this->this_blocker_; | |
791 | } | |
792 | ||
793 | Task_token* | |
794 | is_runnable() | |
795 | { | |
796 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
797 | return this->this_blocker_; | |
798 | return NULL; | |
799 | } | |
800 | ||
801 | void | |
802 | locks(Task_locker* tl) | |
803 | { tl->add(this, this->next_blocker_); } | |
804 | ||
805 | void | |
806 | run(Workqueue*) | |
483620e8 CC |
807 | { |
808 | Plugin_manager* plugins = parameters->options().plugins(); | |
809 | gold_assert(plugins != NULL); | |
483620e8 CC |
810 | plugins->cleanup(); |
811 | } | |
89fc3421 CC |
812 | |
813 | std::string | |
814 | get_name() const | |
5995b570 | 815 | { return "Plugin_finish"; } |
89fc3421 CC |
816 | |
817 | private: | |
818 | Task_token* this_blocker_; | |
819 | Task_token* next_blocker_; | |
820 | }; | |
821 | ||
822 | // Class Plugin_hook. | |
823 | ||
824 | Plugin_hook::~Plugin_hook() | |
825 | { | |
826 | } | |
827 | ||
828 | // Return whether a Plugin_hook task is runnable. | |
829 | ||
830 | Task_token* | |
831 | Plugin_hook::is_runnable() | |
832 | { | |
833 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
834 | return this->this_blocker_; | |
835 | return NULL; | |
836 | } | |
837 | ||
838 | // Return a Task_locker for a Plugin_hook task. We don't need any | |
839 | // locks here. | |
840 | ||
841 | void | |
842 | Plugin_hook::locks(Task_locker*) | |
843 | { | |
844 | } | |
845 | ||
89fc3421 CC |
846 | // Run the "all symbols read" plugin hook. |
847 | ||
848 | void | |
5995b570 | 849 | Plugin_hook::run(Workqueue* workqueue) |
89fc3421 CC |
850 | { |
851 | gold_assert(this->options_.has_plugins()); | |
852 | this->options_.plugins()->all_symbols_read(workqueue, | |
0f7c0701 | 853 | this, |
89fc3421 CC |
854 | this->input_objects_, |
855 | this->symtab_, | |
856 | this->layout_, | |
857 | this->dirpath_, | |
858 | this->mapfile_, | |
859 | &this->this_blocker_); | |
5995b570 CC |
860 | workqueue->queue_soon(new Plugin_finish(this->this_blocker_, |
861 | this->next_blocker_)); | |
89fc3421 CC |
862 | } |
863 | ||
864 | // The C interface routines called by the plugins. | |
865 | ||
866 | #ifdef ENABLE_PLUGINS | |
867 | ||
868 | // Register a claim-file handler. | |
869 | ||
870 | static enum ld_plugin_status | |
871 | register_claim_file(ld_plugin_claim_file_handler handler) | |
872 | { | |
873 | gold_assert(parameters->options().has_plugins()); | |
874 | parameters->options().plugins()->set_claim_file_handler(handler); | |
875 | return LDPS_OK; | |
876 | } | |
877 | ||
878 | // Register an all-symbols-read handler. | |
879 | ||
880 | static enum ld_plugin_status | |
881 | register_all_symbols_read(ld_plugin_all_symbols_read_handler handler) | |
882 | { | |
883 | gold_assert(parameters->options().has_plugins()); | |
884 | parameters->options().plugins()->set_all_symbols_read_handler(handler); | |
885 | return LDPS_OK; | |
886 | } | |
887 | ||
888 | // Register a cleanup handler. | |
889 | ||
890 | static enum ld_plugin_status | |
891 | register_cleanup(ld_plugin_cleanup_handler handler) | |
892 | { | |
893 | gold_assert(parameters->options().has_plugins()); | |
894 | parameters->options().plugins()->set_cleanup_handler(handler); | |
895 | return LDPS_OK; | |
896 | } | |
897 | ||
898 | // Add symbols from a plugin-claimed input file. | |
899 | ||
900 | static enum ld_plugin_status | |
901 | add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms) | |
902 | { | |
903 | gold_assert(parameters->options().has_plugins()); | |
904 | Pluginobj* obj = parameters->options().plugins()->make_plugin_object( | |
905 | static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle))); | |
906 | if (obj == NULL) | |
907 | return LDPS_ERR; | |
908 | obj->store_incoming_symbols(nsyms, syms); | |
909 | return LDPS_OK; | |
910 | } | |
911 | ||
0f7c0701 CC |
912 | // Get the input file information with an open (possibly re-opened) |
913 | // file descriptor. | |
914 | ||
915 | static enum ld_plugin_status | |
916 | get_input_file(const void *handle, struct ld_plugin_input_file *file) | |
917 | { | |
918 | gold_assert(parameters->options().has_plugins()); | |
919 | unsigned int obj_index = | |
920 | static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)); | |
921 | return parameters->options().plugins()->get_input_file(obj_index, file); | |
922 | } | |
923 | ||
924 | // Release the input file. | |
925 | ||
926 | static enum ld_plugin_status | |
927 | release_input_file(const void *handle) | |
928 | { | |
929 | gold_assert(parameters->options().has_plugins()); | |
930 | unsigned int obj_index = | |
931 | static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)); | |
932 | return parameters->options().plugins()->release_input_file(obj_index); | |
933 | } | |
934 | ||
89fc3421 CC |
935 | // Get the symbol resolution info for a plugin-claimed input file. |
936 | ||
937 | static enum ld_plugin_status | |
938 | get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms) | |
939 | { | |
940 | gold_assert(parameters->options().has_plugins()); | |
941 | Pluginobj* obj = parameters->options().plugins()->object( | |
942 | static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle))); | |
943 | if (obj == NULL) | |
944 | return LDPS_ERR; | |
945 | return obj->get_symbol_resolution_info(nsyms, syms); | |
946 | } | |
947 | ||
948 | // Add a new (real) input file generated by a plugin. | |
949 | ||
950 | static enum ld_plugin_status | |
951 | add_input_file(char *pathname) | |
952 | { | |
953 | gold_assert(parameters->options().has_plugins()); | |
e99daf92 ILT |
954 | return parameters->options().plugins()->add_input_file(pathname, false); |
955 | } | |
956 | ||
957 | // Add a new (real) library required by a plugin. | |
958 | ||
959 | static enum ld_plugin_status | |
960 | add_input_library(char *pathname) | |
961 | { | |
962 | gold_assert(parameters->options().has_plugins()); | |
963 | return parameters->options().plugins()->add_input_file(pathname, true); | |
89fc3421 CC |
964 | } |
965 | ||
966 | // Issue a diagnostic message from a plugin. | |
967 | ||
968 | static enum ld_plugin_status | |
6c52134c | 969 | message(int level, const char * format, ...) |
89fc3421 CC |
970 | { |
971 | va_list args; | |
972 | va_start(args, format); | |
973 | ||
974 | switch (level) | |
975 | { | |
976 | case LDPL_INFO: | |
977 | parameters->errors()->info(format, args); | |
978 | break; | |
979 | case LDPL_WARNING: | |
980 | parameters->errors()->warning(format, args); | |
981 | break; | |
982 | case LDPL_ERROR: | |
983 | default: | |
984 | parameters->errors()->error(format, args); | |
985 | break; | |
986 | case LDPL_FATAL: | |
987 | parameters->errors()->fatal(format, args); | |
988 | break; | |
989 | } | |
990 | ||
991 | va_end(args); | |
992 | return LDPS_OK; | |
993 | } | |
994 | ||
995 | #endif // ENABLE_PLUGINS | |
996 | ||
997 | // Allocate a Pluginobj object of the appropriate size and endianness. | |
998 | ||
999 | static Pluginobj* | |
0f7c0701 | 1000 | make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize) |
89fc3421 | 1001 | { |
89fc3421 CC |
1002 | Pluginobj* obj = NULL; |
1003 | ||
029ba973 ILT |
1004 | parameters_force_valid_target(); |
1005 | const Target& target(parameters->target()); | |
89fc3421 | 1006 | |
029ba973 | 1007 | if (target.get_size() == 32) |
89fc3421 | 1008 | { |
029ba973 | 1009 | if (target.is_big_endian()) |
92f03fcb | 1010 | #ifdef HAVE_TARGET_32_BIG |
89fc3421 | 1011 | obj = new Sized_pluginobj<32, true>(input_file->filename(), |
0f7c0701 | 1012 | input_file, offset, filesize); |
92f03fcb CC |
1013 | #else |
1014 | gold_error(_("%s: not configured to support " | |
1015 | "32-bit big-endian object"), | |
1016 | input_file->filename().c_str()); | |
89fc3421 | 1017 | #endif |
89fc3421 | 1018 | else |
92f03fcb | 1019 | #ifdef HAVE_TARGET_32_LITTLE |
89fc3421 | 1020 | obj = new Sized_pluginobj<32, false>(input_file->filename(), |
0f7c0701 | 1021 | input_file, offset, filesize); |
92f03fcb CC |
1022 | #else |
1023 | gold_error(_("%s: not configured to support " | |
1024 | "32-bit little-endian object"), | |
1025 | input_file->filename().c_str()); | |
89fc3421 CC |
1026 | #endif |
1027 | } | |
029ba973 | 1028 | else if (target.get_size() == 64) |
89fc3421 | 1029 | { |
029ba973 | 1030 | if (target.is_big_endian()) |
92f03fcb | 1031 | #ifdef HAVE_TARGET_64_BIG |
89fc3421 | 1032 | obj = new Sized_pluginobj<64, true>(input_file->filename(), |
0f7c0701 | 1033 | input_file, offset, filesize); |
92f03fcb CC |
1034 | #else |
1035 | gold_error(_("%s: not configured to support " | |
1036 | "64-bit big-endian object"), | |
1037 | input_file->filename().c_str()); | |
89fc3421 | 1038 | #endif |
89fc3421 | 1039 | else |
92f03fcb | 1040 | #ifdef HAVE_TARGET_64_LITTLE |
89fc3421 | 1041 | obj = new Sized_pluginobj<64, false>(input_file->filename(), |
0f7c0701 | 1042 | input_file, offset, filesize); |
92f03fcb CC |
1043 | #else |
1044 | gold_error(_("%s: not configured to support " | |
1045 | "64-bit little-endian object"), | |
1046 | input_file->filename().c_str()); | |
89fc3421 CC |
1047 | #endif |
1048 | } | |
1049 | ||
1050 | gold_assert(obj != NULL); | |
89fc3421 CC |
1051 | return obj; |
1052 | } | |
1053 | ||
1054 | } // End namespace gold. |