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