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