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