Use failif on ld-elf/exclude3 tests
[deliverable/binutils-gdb.git] / gold / plugin.cc
CommitLineData
6d03d481 1// plugin.cc -- plugin manager for gold -*- C++ -*-
89fc3421 2
0f3b89d8 3// Copyright 2008, 2009, 2010, 2011 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
1c7814ed
ILT
23#include "gold.h"
24
89fc3421
CC
25#include <cstdio>
26#include <cstdarg>
27#include <cstring>
28#include <string>
29#include <vector>
bc06c745
ILT
30
31#ifdef ENABLE_PLUGINS
89fc3421 32#include <dlfcn.h>
bc06c745 33#endif
89fc3421 34
89fc3421
CC
35#include "parameters.h"
36#include "errors.h"
37#include "fileread.h"
38#include "layout.h"
39#include "options.h"
40#include "plugin.h"
41#include "target.h"
42#include "readsyms.h"
43#include "symtab.h"
44#include "elfcpp.h"
45
46namespace gold
47{
48
49#ifdef ENABLE_PLUGINS
50
51// The linker's exported interfaces.
52
53extern "C"
54{
55
56static enum ld_plugin_status
57register_claim_file(ld_plugin_claim_file_handler handler);
58
59static enum ld_plugin_status
60register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
61
62static enum ld_plugin_status
63register_cleanup(ld_plugin_cleanup_handler handler);
64
65static enum ld_plugin_status
66add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
67
0f7c0701
CC
68static enum ld_plugin_status
69get_input_file(const void *handle, struct ld_plugin_input_file *file);
70
9c793f14
RÁE
71static enum ld_plugin_status
72get_view(const void *handle, const void **viewp);
73
0f7c0701
CC
74static enum ld_plugin_status
75release_input_file(const void *handle);
76
89fc3421
CC
77static enum ld_plugin_status
78get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
79
235061c2
CC
80static enum ld_plugin_status
81get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
82
89fc3421 83static enum ld_plugin_status
6508b958 84add_input_file(const char *pathname);
89fc3421 85
e99daf92 86static enum ld_plugin_status
6508b958 87add_input_library(const char *pathname);
e99daf92 88
42218b9f
RÁE
89static enum ld_plugin_status
90set_extra_library_path(const char *path);
91
89fc3421 92static enum ld_plugin_status
6c52134c 93message(int level, const char *format, ...);
89fc3421 94
e9552f7e
ST
95static enum ld_plugin_status
96get_input_section_count(const void* handle, unsigned int* count);
97
98static enum ld_plugin_status
99get_input_section_type(const struct ld_plugin_section section,
100 unsigned int* type);
101
102static enum ld_plugin_status
103get_input_section_name(const struct ld_plugin_section section,
104 char** section_name_ptr);
105
106static enum ld_plugin_status
107get_input_section_contents(const struct ld_plugin_section section,
108 const unsigned char** section_contents,
109 size_t* len);
110
111static enum ld_plugin_status
112update_section_order(const struct ld_plugin_section *section_list,
113 unsigned int num_sections);
114
115static enum ld_plugin_status
116allow_section_ordering();
117
89fc3421
CC
118};
119
120#endif // ENABLE_PLUGINS
121
122static Pluginobj* make_sized_plugin_object(Input_file* input_file,
0f7c0701 123 off_t offset, off_t filesize);
89fc3421
CC
124
125// Plugin methods.
126
127// Load one plugin library.
128
129void
130Plugin::load()
131{
132#ifdef ENABLE_PLUGINS
89fc3421
CC
133 // Load the plugin library.
134 // FIXME: Look for the library in standard locations.
4674ecfc 135 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
89fc3421
CC
136 if (this->handle_ == NULL)
137 {
4802450a
RÁE
138 gold_error(_("%s: could not load plugin library: %s"),
139 this->filename_.c_str(), dlerror());
89fc3421
CC
140 return;
141 }
142
143 // Find the plugin's onload entry point.
b59befec
ILT
144 void* ptr = dlsym(this->handle_, "onload");
145 if (ptr == NULL)
89fc3421 146 {
4674ecfc
CC
147 gold_error(_("%s: could not find onload entry point"),
148 this->filename_.c_str());
89fc3421
CC
149 return;
150 }
b59befec
ILT
151 ld_plugin_onload onload;
152 gold_assert(sizeof(onload) == sizeof(ptr));
153 memcpy(&onload, &ptr, sizeof(ptr));
89fc3421
CC
154
155 // Get the linker's version number.
156 const char* ver = get_version_string();
157 int major = 0;
158 int minor = 0;
159 sscanf(ver, "%d.%d", &major, &minor);
160
161 // Allocate and populate a transfer vector.
235061c2 162 const int tv_fixed_size = 24;
e9552f7e 163
4674ecfc 164 int tv_size = this->args_.size() + tv_fixed_size;
ca09d69a 165 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
89fc3421 166
abc8dcba
CC
167 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
168 // while processing subsequent entries.
89fc3421 169 int i = 0;
abc8dcba
CC
170 tv[i].tv_tag = LDPT_MESSAGE;
171 tv[i].tv_u.tv_message = message;
172
173 ++i;
89fc3421
CC
174 tv[i].tv_tag = LDPT_API_VERSION;
175 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
176
177 ++i;
178 tv[i].tv_tag = LDPT_GOLD_VERSION;
179 tv[i].tv_u.tv_val = major * 100 + minor;
180
181 ++i;
182 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
183 if (parameters->options().relocatable())
184 tv[i].tv_u.tv_val = LDPO_REL;
185 else if (parameters->options().shared())
186 tv[i].tv_u.tv_val = LDPO_DYN;
370e30b6
RÁE
187 else if (parameters->options().pie())
188 tv[i].tv_u.tv_val = LDPO_PIE;
89fc3421
CC
189 else
190 tv[i].tv_u.tv_val = LDPO_EXEC;
191
3537c84b
RÁE
192 ++i;
193 tv[i].tv_tag = LDPT_OUTPUT_NAME;
194 tv[i].tv_u.tv_string = parameters->options().output();
195
4674ecfc 196 for (unsigned int j = 0; j < this->args_.size(); ++j)
89fc3421
CC
197 {
198 ++i;
199 tv[i].tv_tag = LDPT_OPTION;
4674ecfc 200 tv[i].tv_u.tv_string = this->args_[j].c_str();
89fc3421
CC
201 }
202
203 ++i;
204 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
205 tv[i].tv_u.tv_register_claim_file = register_claim_file;
206
207 ++i;
208 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
209 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
210
211 ++i;
212 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
213 tv[i].tv_u.tv_register_cleanup = register_cleanup;
214
215 ++i;
216 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
217 tv[i].tv_u.tv_add_symbols = add_symbols;
218
0f7c0701
CC
219 ++i;
220 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
221 tv[i].tv_u.tv_get_input_file = get_input_file;
222
9c793f14
RÁE
223 ++i;
224 tv[i].tv_tag = LDPT_GET_VIEW;
225 tv[i].tv_u.tv_get_view = get_view;
226
0f7c0701
CC
227 ++i;
228 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
229 tv[i].tv_u.tv_release_input_file = release_input_file;
230
89fc3421
CC
231 ++i;
232 tv[i].tv_tag = LDPT_GET_SYMBOLS;
233 tv[i].tv_u.tv_get_symbols = get_symbols;
234
235061c2
CC
235 ++i;
236 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
237 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
238
89fc3421
CC
239 ++i;
240 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
241 tv[i].tv_u.tv_add_input_file = add_input_file;
242
e99daf92
ILT
243 ++i;
244 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
245 tv[i].tv_u.tv_add_input_library = add_input_library;
246
42218b9f
RÁE
247 ++i;
248 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
249 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
250
e9552f7e
ST
251 ++i;
252 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
253 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
254
255 ++i;
256 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
257 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
258
259 ++i;
260 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
261 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
262
263 ++i;
264 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
265 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
266
267 ++i;
268 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
269 tv[i].tv_u.tv_update_section_order = update_section_order;
270
271 ++i;
272 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
273 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
274
89fc3421
CC
275 ++i;
276 tv[i].tv_tag = LDPT_NULL;
277 tv[i].tv_u.tv_val = 0;
278
279 gold_assert(i == tv_size - 1);
280
281 // Call the onload entry point.
282 (*onload)(tv);
283
6c52134c 284 delete[] tv;
89fc3421
CC
285#endif // ENABLE_PLUGINS
286}
287
288// Call the plugin claim-file handler.
289
290inline bool
ca09d69a 291Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
89fc3421
CC
292{
293 int claimed = 0;
294
295 if (this->claim_file_handler_ != NULL)
296 {
297 (*this->claim_file_handler_)(plugin_input_file, &claimed);
298 if (claimed)
299 return true;
300 }
301 return false;
302}
303
304// Call the all-symbols-read handler.
305
306inline void
307Plugin::all_symbols_read()
308{
309 if (this->all_symbols_read_handler_ != NULL)
310 (*this->all_symbols_read_handler_)();
311}
312
313// Call the cleanup handler.
314
315inline void
316Plugin::cleanup()
317{
40f36857
CC
318 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
319 {
320 // Set this flag before calling to prevent a recursive plunge
321 // in the event that a plugin's cleanup handler issues a
322 // fatal error.
323 this->cleanup_done_ = true;
324 (*this->cleanup_handler_)();
325 }
89fc3421
CC
326}
327
0f3b89d8
ILT
328// This task is used to rescan archives as needed.
329
330class Plugin_rescan : public Task
331{
332 public:
333 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
334 : this_blocker_(this_blocker), next_blocker_(next_blocker)
335 { }
336
337 ~Plugin_rescan()
338 {
339 delete this->this_blocker_;
340 }
341
342 Task_token*
343 is_runnable()
344 {
345 if (this->this_blocker_->is_blocked())
346 return this->this_blocker_;
347 return NULL;
348 }
349
350 void
351 locks(Task_locker* tl)
352 { tl->add(this, this->next_blocker_); }
353
354 void
355 run(Workqueue*)
356 { parameters->options().plugins()->rescan(this); }
357
358 std::string
359 get_name() const
360 { return "Plugin_rescan"; }
361
362 private:
363 Task_token* this_blocker_;
364 Task_token* next_blocker_;
365};
366
89fc3421
CC
367// Plugin_manager methods.
368
369Plugin_manager::~Plugin_manager()
370{
371 for (Plugin_list::iterator p = this->plugins_.begin();
372 p != this->plugins_.end();
373 ++p)
374 delete *p;
375 this->plugins_.clear();
376 for (Object_list::iterator obj = this->objects_.begin();
377 obj != this->objects_.end();
378 ++obj)
379 delete *obj;
380 this->objects_.clear();
381}
382
383// Load all plugin libraries.
384
385void
e9552f7e 386Plugin_manager::load_plugins(Layout* layout)
89fc3421 387{
e9552f7e 388 this->layout_ = layout;
89fc3421
CC
389 for (this->current_ = this->plugins_.begin();
390 this->current_ != this->plugins_.end();
391 ++this->current_)
392 (*this->current_)->load();
393}
394
395// Call the plugin claim-file handlers in turn to see if any claim the file.
396
397Pluginobj*
398Plugin_manager::claim_file(Input_file* input_file, off_t offset,
e9552f7e 399 off_t filesize, Object* elf_object)
89fc3421
CC
400{
401 if (this->in_replacement_phase_)
402 return NULL;
403
404 unsigned int handle = this->objects_.size();
405 this->input_file_ = input_file;
406 this->plugin_input_file_.name = input_file->filename().c_str();
407 this->plugin_input_file_.fd = input_file->file().descriptor();
408 this->plugin_input_file_.offset = offset;
409 this->plugin_input_file_.filesize = filesize;
410 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
e9552f7e
ST
411 if (elf_object != NULL)
412 this->objects_.push_back(elf_object);
413 this->in_claim_file_handler_ = true;
89fc3421
CC
414
415 for (this->current_ = this->plugins_.begin();
416 this->current_ != this->plugins_.end();
417 ++this->current_)
418 {
419 if ((*this->current_)->claim_file(&this->plugin_input_file_))
420 {
0f3b89d8 421 this->any_claimed_ = true;
e9552f7e 422 this->in_claim_file_handler_ = false;
0f3b89d8 423
e9552f7e
ST
424 if (this->objects_.size() > handle
425 && this->objects_[handle]->pluginobj() != NULL)
426 return this->objects_[handle]->pluginobj();
abc8dcba
CC
427
428 // If the plugin claimed the file but did not call the
429 // add_symbols callback, we need to create the Pluginobj now.
430 Pluginobj* obj = this->make_plugin_object(handle);
431 return obj;
89fc3421
CC
432 }
433 }
434
e9552f7e 435 this->in_claim_file_handler_ = false;
89fc3421
CC
436 return NULL;
437}
438
0f3b89d8
ILT
439// Save an archive. This is used so that a plugin can add a file
440// which refers to a symbol which was not previously referenced. In
441// that case we want to pretend that the symbol was referenced before,
442// and pull in the archive object.
443
444void
445Plugin_manager::save_archive(Archive* archive)
446{
447 if (this->in_replacement_phase_ || !this->any_claimed_)
448 delete archive;
449 else
450 this->rescannable_.push_back(Rescannable(archive));
451}
452
453// Save an Input_group. This is like save_archive.
454
455void
456Plugin_manager::save_input_group(Input_group* input_group)
457{
458 if (this->in_replacement_phase_ || !this->any_claimed_)
459 delete input_group;
460 else
461 this->rescannable_.push_back(Rescannable(input_group));
462}
463
89fc3421
CC
464// Call the all-symbols-read handlers.
465
466void
0f7c0701 467Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
89fc3421 468 Input_objects* input_objects,
e9552f7e 469 Symbol_table* symtab,
89fc3421
CC
470 Dirsearch* dirpath, Mapfile* mapfile,
471 Task_token** last_blocker)
472{
473 this->in_replacement_phase_ = true;
474 this->workqueue_ = workqueue;
0f7c0701 475 this->task_ = task;
89fc3421
CC
476 this->input_objects_ = input_objects;
477 this->symtab_ = symtab;
89fc3421
CC
478 this->dirpath_ = dirpath;
479 this->mapfile_ = mapfile;
480 this->this_blocker_ = NULL;
481
482 for (this->current_ = this->plugins_.begin();
483 this->current_ != this->plugins_.end();
484 ++this->current_)
485 (*this->current_)->all_symbols_read();
486
0f3b89d8
ILT
487 if (this->any_added_)
488 {
489 Task_token* next_blocker = new Task_token(true);
490 next_blocker->add_blocker();
491 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
492 this->this_blocker_ = next_blocker;
493 }
494
89fc3421
CC
495 *last_blocker = this->this_blocker_;
496}
497
0f3b89d8
ILT
498// This is called when we see a new undefined symbol. If we are in
499// the replacement phase, this means that we may need to rescan some
500// archives we have previously seen.
501
502void
503Plugin_manager::new_undefined_symbol(Symbol* sym)
504{
505 if (this->in_replacement_phase_)
506 this->undefined_symbols_.push_back(sym);
507}
508
509// Rescan archives as needed. This handles the case where a new
510// object file added by a plugin has an undefined reference to some
511// symbol defined in an archive.
512
513void
514Plugin_manager::rescan(Task* task)
515{
516 size_t rescan_pos = 0;
517 size_t rescan_size = this->rescannable_.size();
518 while (!this->undefined_symbols_.empty())
519 {
520 if (rescan_pos >= rescan_size)
521 {
522 this->undefined_symbols_.clear();
523 return;
524 }
525
526 Undefined_symbol_list undefs;
527 undefs.reserve(this->undefined_symbols_.size());
528 this->undefined_symbols_.swap(undefs);
529
530 size_t min_rescan_pos = rescan_size;
531
532 for (Undefined_symbol_list::const_iterator p = undefs.begin();
533 p != undefs.end();
534 ++p)
535 {
536 if (!(*p)->is_undefined())
537 continue;
538
539 this->undefined_symbols_.push_back(*p);
540
541 // Find the first rescan archive which defines this symbol,
542 // starting at the current rescan position. The rescan position
543 // exists so that given -la -lb -lc we don't look for undefined
544 // symbols in -lb back in -la, but instead get the definition
545 // from -lc. Don't bother to look past the current minimum
546 // rescan position.
547 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
548 {
549 if (this->rescannable_defines(i, *p))
550 {
551 min_rescan_pos = i;
552 break;
553 }
554 }
555 }
556
557 if (min_rescan_pos >= rescan_size)
558 {
559 // We didn't find any rescannable archives which define any
560 // undefined symbols.
561 return;
562 }
563
564 const Rescannable& r(this->rescannable_[min_rescan_pos]);
565 if (r.is_archive)
566 {
567 Task_lock_obj<Archive> tl(task, r.u.archive);
568 r.u.archive->add_symbols(this->symtab_, this->layout_,
569 this->input_objects_, this->mapfile_);
570 }
571 else
572 {
573 size_t next_saw_undefined = this->symtab_->saw_undefined();
574 size_t saw_undefined;
575 do
576 {
577 saw_undefined = next_saw_undefined;
578
579 for (Input_group::const_iterator p = r.u.input_group->begin();
580 p != r.u.input_group->end();
581 ++p)
582 {
583 Task_lock_obj<Archive> tl(task, *p);
584
585 (*p)->add_symbols(this->symtab_, this->layout_,
586 this->input_objects_, this->mapfile_);
587 }
588
589 next_saw_undefined = this->symtab_->saw_undefined();
590 }
591 while (saw_undefined != next_saw_undefined);
592 }
593
594 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
595 {
596 if (this->rescannable_[i].is_archive)
597 delete this->rescannable_[i].u.archive;
598 else
599 delete this->rescannable_[i].u.input_group;
600 }
601
602 rescan_pos = min_rescan_pos + 1;
603 }
604}
605
606// Return whether the rescannable at index I defines SYM.
607
608bool
609Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
610{
611 const Rescannable& r(this->rescannable_[i]);
612 if (r.is_archive)
613 return r.u.archive->defines_symbol(sym);
614 else
615 {
616 for (Input_group::const_iterator p = r.u.input_group->begin();
617 p != r.u.input_group->end();
618 ++p)
619 {
620 if ((*p)->defines_symbol(sym))
621 return true;
622 }
623 return false;
624 }
625}
626
483620e8 627// Layout deferred objects.
89fc3421
CC
628
629void
483620e8 630Plugin_manager::layout_deferred_objects()
89fc3421 631{
5995b570
CC
632 Deferred_layout_list::iterator obj;
633
634 for (obj = this->deferred_layout_objects_.begin();
635 obj != this->deferred_layout_objects_.end();
636 ++obj)
5f9bcf58
CC
637 {
638 // Lock the object so we can read from it. This is only called
639 // single-threaded from queue_middle_tasks, so it is OK to lock.
640 // Unfortunately we have no way to pass in a Task token.
641 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
642 Task_lock_obj<Object> tl(dummy_task, *obj);
643 (*obj)->layout_deferred_sections(this->layout_);
644 }
483620e8
CC
645}
646
647// Call the cleanup handlers.
5995b570 648
483620e8
CC
649void
650Plugin_manager::cleanup()
651{
89fc3421
CC
652 for (this->current_ = this->plugins_.begin();
653 this->current_ != this->plugins_.end();
654 ++this->current_)
655 (*this->current_)->cleanup();
656}
657
658// Make a new Pluginobj object. This is called when the plugin calls
659// the add_symbols API.
660
661Pluginobj*
662Plugin_manager::make_plugin_object(unsigned int handle)
663{
664 // Make sure we aren't asked to make an object for the same handle twice.
e9552f7e
ST
665 if (this->objects_.size() != handle
666 && this->objects_[handle]->pluginobj() != NULL)
89fc3421
CC
667 return NULL;
668
669 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
0f7c0701
CC
670 this->plugin_input_file_.offset,
671 this->plugin_input_file_.filesize);
e9552f7e
ST
672
673
674 // If the elf object for this file was pushed into the objects_ vector, delete
675 // it to make room for the Pluginobj as this file is claimed.
676 if (this->objects_.size() != handle)
677 this->objects_.pop_back();
678
89fc3421
CC
679 this->objects_.push_back(obj);
680 return obj;
681}
682
0f7c0701
CC
683// Get the input file information with an open (possibly re-opened)
684// file descriptor.
685
686ld_plugin_status
687Plugin_manager::get_input_file(unsigned int handle,
ca09d69a 688 struct ld_plugin_input_file* file)
0f7c0701 689{
e9552f7e 690 Pluginobj* obj = this->object(handle)->pluginobj();
0f7c0701
CC
691 if (obj == NULL)
692 return LDPS_BAD_HANDLE;
693
694 obj->lock(this->task_);
695 file->name = obj->filename().c_str();
696 file->fd = obj->descriptor();
697 file->offset = obj->offset();
698 file->filesize = obj->filesize();
699 file->handle = reinterpret_cast<void*>(handle);
700 return LDPS_OK;
701}
702
703// Release the input file.
704
705ld_plugin_status
706Plugin_manager::release_input_file(unsigned int handle)
707{
e9552f7e
ST
708 if (this->object(handle) == NULL)
709 return LDPS_BAD_HANDLE;
710
711 Pluginobj* obj = this->object(handle)->pluginobj();
712
0f7c0701
CC
713 if (obj == NULL)
714 return LDPS_BAD_HANDLE;
715
716 obj->unlock(this->task_);
717 return LDPS_OK;
718}
719
e9552f7e
ST
720// Get the elf object corresponding to the handle. Return NULL if we
721// found a Pluginobj instead.
722
723Object*
724Plugin_manager::get_elf_object(const void* handle)
725{
726 Object* obj = this->object(
727 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
728
729 // The object should not be a Pluginobj.
730 if (obj == NULL
731 || obj->pluginobj() != NULL)
732 return NULL;
733
734 return obj;
735}
736
9c793f14
RÁE
737ld_plugin_status
738Plugin_manager::get_view(unsigned int handle, const void **viewp)
739{
740 off_t offset;
741 size_t filesize;
742 Input_file *input_file;
e9552f7e 743 if (this->in_claim_file_handler_)
9c793f14
RÁE
744 {
745 // We are being called from the claim_file hook.
746 const struct ld_plugin_input_file &f = this->plugin_input_file_;
747 offset = f.offset;
748 filesize = f.filesize;
749 input_file = this->input_file_;
750 }
751 else
752 {
753 // An already claimed file.
e9552f7e
ST
754 if (this->object(handle) == NULL)
755 return LDPS_BAD_HANDLE;
756 Pluginobj* obj = this->object(handle)->pluginobj();
9c793f14
RÁE
757 if (obj == NULL)
758 return LDPS_BAD_HANDLE;
759 offset = obj->offset();
760 filesize = obj->filesize();
761 input_file = obj->input_file();
762 }
763 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
764 false);
765 return LDPS_OK;
766}
767
42218b9f
RÁE
768// Add a new library path.
769
770ld_plugin_status
ca09d69a 771Plugin_manager::set_extra_library_path(const char* path)
42218b9f
RÁE
772{
773 this->extra_search_path_ = std::string(path);
774 return LDPS_OK;
775}
776
89fc3421
CC
777// Add a new input file.
778
779ld_plugin_status
ca09d69a 780Plugin_manager::add_input_file(const char* pathname, bool is_lib)
89fc3421 781{
ae3b5189
CD
782 Input_file_argument file(pathname,
783 (is_lib
784 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
785 : Input_file_argument::INPUT_FILE_TYPE_FILE),
42218b9f
RÁE
786 (is_lib
787 ? this->extra_search_path_.c_str()
788 : ""),
789 false,
790 this->options_);
89fc3421
CC
791 Input_argument* input_argument = new Input_argument(file);
792 Task_token* next_blocker = new Task_token(true);
793 next_blocker->add_blocker();
8c21d9d3 794 if (parameters->incremental())
40f36857
CC
795 gold_error(_("input files added by plug-ins in --incremental mode not "
796 "supported yet"));
f1ed28fb 797 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
89fc3421
CC
798 this->symtab_,
799 this->layout_,
800 this->dirpath_,
15f8229b 801 0,
89fc3421
CC
802 this->mapfile_,
803 input_argument,
804 NULL,
b0193076 805 NULL,
89fc3421
CC
806 this->this_blocker_,
807 next_blocker));
808 this->this_blocker_ = next_blocker;
0f3b89d8 809 this->any_added_ = true;
89fc3421
CC
810 return LDPS_OK;
811}
812
813// Class Pluginobj.
814
2ea97941
ILT
815Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
816 off_t offset, off_t filesize)
817 : Object(name, input_file, false, offset),
818 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
89fc3421
CC
819{
820}
821
235061c2 822// Return TRUE if a defined symbol is referenced from outside the
f7c5b166
CC
823// universe of claimed objects. Only references from relocatable,
824// non-IR (unclaimed) objects count as a reference. References from
825// dynamic objects count only as "visible".
d66a9eb3
CC
826
827static inline bool
235061c2 828is_referenced_from_outside(Symbol* lsym)
d66a9eb3
CC
829{
830 if (lsym->in_real_elf())
831 return true;
832 if (parameters->options().relocatable())
833 return true;
84ced98a
RÁE
834 if (parameters->options().is_undefined(lsym->name()))
835 return true;
235061c2
CC
836 return false;
837}
838
839// Return TRUE if a defined symbol might be reachable from outside the
840// load module.
841
842static inline bool
843is_visible_from_outside(Symbol* lsym)
844{
f7c5b166
CC
845 if (lsym->in_dyn())
846 return true;
d66a9eb3
CC
847 if (parameters->options().export_dynamic() || parameters->options().shared())
848 return lsym->is_externally_visible();
849 return false;
850}
851
89fc3421
CC
852// Get symbol resolution info.
853
854ld_plugin_status
235061c2
CC
855Pluginobj::get_symbol_resolution_info(int nsyms,
856 ld_plugin_symbol* syms,
857 int version) const
858{
859 // For version 1 of this interface, we cannot use
860 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
861 // instead.
862 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
863 = (version > 1
864 ? LDPR_PREVAILING_DEF_IRONLY_EXP
865 : LDPR_PREVAILING_DEF);
866
abc8dcba 867 if (nsyms > this->nsyms_)
89fc3421 868 return LDPS_NO_SYMS;
b0193076
RÁE
869
870 if (static_cast<size_t>(nsyms) > this->symbols_.size())
871 {
872 // We never decided to include this object. We mark all symbols as
873 // preempted.
ca09d69a 874 gold_assert(this->symbols_.size() == 0);
b0193076
RÁE
875 for (int i = 0; i < nsyms; i++)
876 syms[i].resolution = LDPR_PREEMPTED_REG;
877 return LDPS_OK;
878 }
879
89fc3421
CC
880 for (int i = 0; i < nsyms; i++)
881 {
882 ld_plugin_symbol* isym = &syms[i];
883 Symbol* lsym = this->symbols_[i];
884 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
885
886 if (lsym->is_undefined())
887 // The symbol remains undefined.
888 res = LDPR_UNDEF;
889 else if (isym->def == LDPK_UNDEF
890 || isym->def == LDPK_WEAKUNDEF
891 || isym->def == LDPK_COMMON)
892 {
893 // The original symbol was undefined or common.
894 if (lsym->source() != Symbol::FROM_OBJECT)
895 res = LDPR_RESOLVED_EXEC;
be234d88 896 else if (lsym->object()->pluginobj() == this)
235061c2
CC
897 {
898 if (is_referenced_from_outside(lsym))
899 res = LDPR_PREVAILING_DEF;
900 else if (is_visible_from_outside(lsym))
901 res = ldpr_prevailing_def_ironly_exp;
902 else
903 res = LDPR_PREVAILING_DEF_IRONLY;
904 }
89fc3421
CC
905 else if (lsym->object()->pluginobj() != NULL)
906 res = LDPR_RESOLVED_IR;
907 else if (lsym->object()->is_dynamic())
908 res = LDPR_RESOLVED_DYN;
909 else
910 res = LDPR_RESOLVED_EXEC;
911 }
912 else
913 {
914 // The original symbol was a definition.
915 if (lsym->source() != Symbol::FROM_OBJECT)
916 res = LDPR_PREEMPTED_REG;
917 else if (lsym->object() == static_cast<const Object*>(this))
235061c2
CC
918 {
919 if (is_referenced_from_outside(lsym))
920 res = LDPR_PREVAILING_DEF;
921 else if (is_visible_from_outside(lsym))
922 res = ldpr_prevailing_def_ironly_exp;
923 else
924 res = LDPR_PREVAILING_DEF_IRONLY;
925 }
89fc3421
CC
926 else
927 res = (lsym->object()->pluginobj() != NULL
928 ? LDPR_PREEMPTED_IR
929 : LDPR_PREEMPTED_REG);
930 }
931 isym->resolution = res;
932 }
933 return LDPS_OK;
934}
935
936// Return TRUE if the comdat group with key COMDAT_KEY from this object
937// should be kept.
938
939bool
2ea97941 940Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
89fc3421
CC
941{
942 std::pair<Comdat_map::iterator, bool> ins =
943 this->comdat_map_.insert(std::make_pair(comdat_key, false));
944
945 // If this is the first time we've seen this comdat key, ask the
946 // layout object whether it should be included.
947 if (ins.second)
2ea97941
ILT
948 ins.first->second = layout->find_or_add_kept_section(comdat_key,
949 NULL, 0, true,
950 true, NULL);
89fc3421
CC
951
952 return ins.first->second;
953}
954
955// Class Sized_pluginobj.
956
957template<int size, bool big_endian>
958Sized_pluginobj<size, big_endian>::Sized_pluginobj(
2ea97941
ILT
959 const std::string& name,
960 Input_file* input_file,
961 off_t offset,
962 off_t filesize)
963 : Pluginobj(name, input_file, offset, filesize)
89fc3421
CC
964{
965}
966
967// Read the symbols. Not used for plugin objects.
968
969template<int size, bool big_endian>
970void
971Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
972{
973 gold_unreachable();
974}
975
976// Lay out the input sections. Not used for plugin objects.
977
978template<int size, bool big_endian>
979void
980Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
981 Read_symbols_data*)
982{
983 gold_unreachable();
984}
985
986// Add the symbols to the symbol table.
987
89fc3421
CC
988template<int size, bool big_endian>
989void
990Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
f488e4b0 991 Read_symbols_data*,
2ea97941 992 Layout* layout)
89fc3421
CC
993{
994 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
995 unsigned char symbuf[sym_size];
996 elfcpp::Sym<size, big_endian> sym(symbuf);
997 elfcpp::Sym_write<size, big_endian> osym(symbuf);
998
999 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
1000
1001 this->symbols_.resize(this->nsyms_);
1002
1003 for (int i = 0; i < this->nsyms_; ++i)
1004 {
ca09d69a 1005 const struct ld_plugin_symbol* isym = &this->syms_[i];
2ea97941 1006 const char* name = isym->name;
89fc3421
CC
1007 const char* ver = isym->version;
1008 elfcpp::Elf_Half shndx;
1009 elfcpp::STB bind;
1010 elfcpp::STV vis;
1011
2ea97941
ILT
1012 if (name != NULL && name[0] == '\0')
1013 name = NULL;
89fc3421
CC
1014 if (ver != NULL && ver[0] == '\0')
1015 ver = NULL;
1016
1017 switch (isym->def)
1018 {
1019 case LDPK_WEAKDEF:
1020 case LDPK_WEAKUNDEF:
1021 bind = elfcpp::STB_WEAK;
1022 break;
1023 case LDPK_DEF:
1024 case LDPK_UNDEF:
1025 case LDPK_COMMON:
1026 default:
1027 bind = elfcpp::STB_GLOBAL;
1028 break;
1029 }
1030
1031 switch (isym->def)
1032 {
1033 case LDPK_DEF:
1034 case LDPK_WEAKDEF:
1035 shndx = elfcpp::SHN_ABS;
1036 break;
1037 case LDPK_COMMON:
1038 shndx = elfcpp::SHN_COMMON;
1039 break;
1040 case LDPK_UNDEF:
1041 case LDPK_WEAKUNDEF:
1042 default:
1043 shndx = elfcpp::SHN_UNDEF;
1044 break;
1045 }
1046
1047 switch (isym->visibility)
1048 {
1049 case LDPV_PROTECTED:
105b6afd 1050 vis = elfcpp::STV_PROTECTED;
89fc3421
CC
1051 break;
1052 case LDPV_INTERNAL:
105b6afd 1053 vis = elfcpp::STV_INTERNAL;
89fc3421
CC
1054 break;
1055 case LDPV_HIDDEN:
105b6afd 1056 vis = elfcpp::STV_HIDDEN;
89fc3421
CC
1057 break;
1058 case LDPV_DEFAULT:
1059 default:
1060 vis = elfcpp::STV_DEFAULT;
1061 break;
1062 }
1063
1064 if (isym->comdat_key != NULL
1065 && isym->comdat_key[0] != '\0'
2ea97941 1066 && !this->include_comdat_group(isym->comdat_key, layout))
89fc3421
CC
1067 shndx = elfcpp::SHN_UNDEF;
1068
1069 osym.put_st_name(0);
1070 osym.put_st_value(0);
1071 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
1072 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1073 osym.put_st_other(vis, 0);
1074 osym.put_st_shndx(shndx);
1075
1076 this->symbols_[i] =
2ea97941 1077 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
89fc3421
CC
1078 }
1079}
1080
b0193076
RÁE
1081template<int size, bool big_endian>
1082Archive::Should_include
1083Sized_pluginobj<size, big_endian>::do_should_include_member(
88a4108b
ILT
1084 Symbol_table* symtab,
1085 Layout* layout,
1086 Read_symbols_data*,
1087 std::string* why)
b0193076
RÁE
1088{
1089 char* tmpbuf = NULL;
1090 size_t tmpbuflen = 0;
1091
88a4108b
ILT
1092 for (int i = 0; i < this->nsyms_; ++i)
1093 {
1094 const struct ld_plugin_symbol& sym = this->syms_[i];
1095 const char* name = sym.name;
1096 Symbol* symbol;
1097 Archive::Should_include t = Archive::should_include_member(symtab,
1098 layout,
1099 name,
1100 &symbol, why,
1101 &tmpbuf,
1102 &tmpbuflen);
b0193076
RÁE
1103 if (t == Archive::SHOULD_INCLUDE_YES)
1104 {
1105 if (tmpbuf != NULL)
1106 free(tmpbuf);
1107 return t;
1108 }
88a4108b 1109 }
b0193076
RÁE
1110 if (tmpbuf != NULL)
1111 free(tmpbuf);
1112 return Archive::SHOULD_INCLUDE_UNKNOWN;
1113}
1114
e0c52780
CC
1115// Iterate over global symbols, calling a visitor class V for each.
1116
1117template<int size, bool big_endian>
1118void
1119Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1120 Read_symbols_data*,
1121 Library_base::Symbol_visitor_base* v)
1122{
1123 for (int i = 0; i < this->nsyms_; ++i)
1124 {
1125 const struct ld_plugin_symbol& sym = this->syms_[i];
1126 if (sym.def != LDPK_UNDEF)
1127 v->visit(sym.name);
1128 }
1129}
1130
cdc29364
CC
1131// Iterate over local symbols, calling a visitor class V for each GOT offset
1132// associated with a local symbol.
1133template<int size, bool big_endian>
1134void
1135Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1136 Got_offset_list::Visitor*) const
1137{
1138 gold_unreachable();
1139}
1140
89fc3421
CC
1141// Get the size of a section. Not used for plugin objects.
1142
1143template<int size, bool big_endian>
1144uint64_t
1145Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1146{
1147 gold_unreachable();
1148 return 0;
1149}
1150
1151// Get the name of a section. Not used for plugin objects.
1152
1153template<int size, bool big_endian>
1154std::string
1155Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
1156{
1157 gold_unreachable();
1158 return std::string();
1159}
1160
1161// Return a view of the contents of a section. Not used for plugin objects.
1162
1163template<int size, bool big_endian>
c1027032
CC
1164const unsigned char*
1165Sized_pluginobj<size, big_endian>::do_section_contents(
1166 unsigned int,
1167 section_size_type*,
1168 bool)
89fc3421 1169{
89fc3421 1170 gold_unreachable();
c1027032 1171 return NULL;
89fc3421
CC
1172}
1173
1174// Return section flags. Not used for plugin objects.
1175
1176template<int size, bool big_endian>
1177uint64_t
1178Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1179{
1180 gold_unreachable();
1181 return 0;
1182}
1183
ef15dade
ST
1184// Return section entsize. Not used for plugin objects.
1185
1186template<int size, bool big_endian>
1187uint64_t
1188Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1189{
1190 gold_unreachable();
1191 return 0;
1192}
1193
89fc3421
CC
1194// Return section address. Not used for plugin objects.
1195
1196template<int size, bool big_endian>
1197uint64_t
1198Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1199{
1200 gold_unreachable();
1201 return 0;
1202}
1203
1204// Return section type. Not used for plugin objects.
1205
1206template<int size, bool big_endian>
1207unsigned int
1208Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1209{
1210 gold_unreachable();
1211 return 0;
1212}
1213
1214// Return the section link field. Not used for plugin objects.
1215
1216template<int size, bool big_endian>
1217unsigned int
1218Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1219{
1220 gold_unreachable();
1221 return 0;
1222}
1223
1224// Return the section link field. Not used for plugin objects.
1225
1226template<int size, bool big_endian>
1227unsigned int
1228Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1229{
1230 gold_unreachable();
1231 return 0;
1232}
1233
1234// Return the section alignment. Not used for plugin objects.
1235
1236template<int size, bool big_endian>
1237uint64_t
1238Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1239{
1240 gold_unreachable();
1241 return 0;
1242}
1243
1244// Return the Xindex structure to use. Not used for plugin objects.
1245
1246template<int size, bool big_endian>
1247Xindex*
1248Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1249{
1250 gold_unreachable();
1251 return NULL;
1252}
1253
53bbcc1b
CC
1254// Get symbol counts. Don't count plugin objects; the replacement
1255// files will provide the counts.
89fc3421
CC
1256
1257template<int size, bool big_endian>
1258void
53bbcc1b
CC
1259Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1260 const Symbol_table*,
1261 size_t* defined,
1262 size_t* used) const
89fc3421 1263{
53bbcc1b
CC
1264 *defined = 0;
1265 *used = 0;
89fc3421
CC
1266}
1267
dde3f402
ILT
1268// Get symbols. Not used for plugin objects.
1269
1270template<int size, bool big_endian>
1271const Object::Symbols*
1272Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1273{
1274 gold_unreachable();
1275}
1276
5995b570 1277// Class Plugin_finish. This task runs after all replacement files have
78384e8f
CC
1278// been added. For now, it's a placeholder for a possible plugin API
1279// to allow the plugin to release most of its resources. The cleanup
1280// handlers must be called later, because they can remove the temporary
1281// object files that are needed until the end of the link.
89fc3421 1282
5995b570 1283class Plugin_finish : public Task
89fc3421
CC
1284{
1285 public:
5995b570 1286 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
89fc3421
CC
1287 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1288 { }
1289
5995b570 1290 ~Plugin_finish()
89fc3421
CC
1291 {
1292 if (this->this_blocker_ != NULL)
1293 delete this->this_blocker_;
1294 }
1295
1296 Task_token*
1297 is_runnable()
1298 {
1299 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1300 return this->this_blocker_;
1301 return NULL;
1302 }
1303
1304 void
1305 locks(Task_locker* tl)
1306 { tl->add(this, this->next_blocker_); }
1307
1308 void
1309 run(Workqueue*)
483620e8 1310 {
78384e8f 1311 // We could call early cleanup handlers here.
483620e8 1312 }
89fc3421
CC
1313
1314 std::string
1315 get_name() const
5995b570 1316 { return "Plugin_finish"; }
89fc3421
CC
1317
1318 private:
1319 Task_token* this_blocker_;
1320 Task_token* next_blocker_;
1321};
1322
1323// Class Plugin_hook.
1324
1325Plugin_hook::~Plugin_hook()
1326{
1327}
1328
1329// Return whether a Plugin_hook task is runnable.
1330
1331Task_token*
1332Plugin_hook::is_runnable()
1333{
1334 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1335 return this->this_blocker_;
1336 return NULL;
1337}
1338
1339// Return a Task_locker for a Plugin_hook task. We don't need any
1340// locks here.
1341
1342void
1343Plugin_hook::locks(Task_locker*)
1344{
1345}
1346
89fc3421
CC
1347// Run the "all symbols read" plugin hook.
1348
1349void
5995b570 1350Plugin_hook::run(Workqueue* workqueue)
89fc3421
CC
1351{
1352 gold_assert(this->options_.has_plugins());
a10ae760 1353 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
91ff43fe
RÁE
1354 if (start_sym != NULL)
1355 start_sym->set_in_real_elf();
1356
89fc3421 1357 this->options_.plugins()->all_symbols_read(workqueue,
0f7c0701 1358 this,
89fc3421
CC
1359 this->input_objects_,
1360 this->symtab_,
89fc3421
CC
1361 this->dirpath_,
1362 this->mapfile_,
1363 &this->this_blocker_);
5995b570
CC
1364 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1365 this->next_blocker_));
89fc3421
CC
1366}
1367
1368// The C interface routines called by the plugins.
1369
1370#ifdef ENABLE_PLUGINS
1371
1372// Register a claim-file handler.
1373
1374static enum ld_plugin_status
1375register_claim_file(ld_plugin_claim_file_handler handler)
1376{
1377 gold_assert(parameters->options().has_plugins());
1378 parameters->options().plugins()->set_claim_file_handler(handler);
1379 return LDPS_OK;
1380}
1381
1382// Register an all-symbols-read handler.
1383
1384static enum ld_plugin_status
1385register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1386{
1387 gold_assert(parameters->options().has_plugins());
1388 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1389 return LDPS_OK;
1390}
1391
1392// Register a cleanup handler.
1393
1394static enum ld_plugin_status
1395register_cleanup(ld_plugin_cleanup_handler handler)
1396{
1397 gold_assert(parameters->options().has_plugins());
1398 parameters->options().plugins()->set_cleanup_handler(handler);
1399 return LDPS_OK;
1400}
1401
1402// Add symbols from a plugin-claimed input file.
1403
1404static enum ld_plugin_status
ca09d69a 1405add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
89fc3421
CC
1406{
1407 gold_assert(parameters->options().has_plugins());
1408 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1409 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1410 if (obj == NULL)
1411 return LDPS_ERR;
1412 obj->store_incoming_symbols(nsyms, syms);
1413 return LDPS_OK;
1414}
1415
0f7c0701
CC
1416// Get the input file information with an open (possibly re-opened)
1417// file descriptor.
1418
1419static enum ld_plugin_status
ca09d69a 1420get_input_file(const void* handle, struct ld_plugin_input_file* file)
0f7c0701
CC
1421{
1422 gold_assert(parameters->options().has_plugins());
1423 unsigned int obj_index =
1424 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1425 return parameters->options().plugins()->get_input_file(obj_index, file);
1426}
1427
1428// Release the input file.
1429
1430static enum ld_plugin_status
ca09d69a 1431release_input_file(const void* handle)
0f7c0701
CC
1432{
1433 gold_assert(parameters->options().has_plugins());
1434 unsigned int obj_index =
1435 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1436 return parameters->options().plugins()->release_input_file(obj_index);
1437}
1438
9c793f14
RÁE
1439static enum ld_plugin_status
1440get_view(const void *handle, const void **viewp)
1441{
1442 gold_assert(parameters->options().has_plugins());
1443 unsigned int obj_index =
1444 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1445 return parameters->options().plugins()->get_view(obj_index, viewp);
1446}
1447
89fc3421
CC
1448// Get the symbol resolution info for a plugin-claimed input file.
1449
1450static enum ld_plugin_status
ca09d69a 1451get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
89fc3421
CC
1452{
1453 gold_assert(parameters->options().has_plugins());
e9552f7e
ST
1454 Object* obj = parameters->options().plugins()->object(
1455 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
89fc3421
CC
1456 if (obj == NULL)
1457 return LDPS_ERR;
e9552f7e
ST
1458 Pluginobj* plugin_obj = obj->pluginobj();
1459 if (plugin_obj == NULL)
1460 return LDPS_ERR;
235061c2
CC
1461 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 1);
1462}
1463
1464// Version 2 of the above. The only difference is that this version
1465// is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1466
1467static enum ld_plugin_status
1468get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1469{
1470 gold_assert(parameters->options().has_plugins());
1471 Object* obj = parameters->options().plugins()->object(
1472 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1473 if (obj == NULL)
1474 return LDPS_ERR;
1475 Pluginobj* plugin_obj = obj->pluginobj();
1476 if (plugin_obj == NULL)
1477 return LDPS_ERR;
1478 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 2);
89fc3421
CC
1479}
1480
1481// Add a new (real) input file generated by a plugin.
1482
1483static enum ld_plugin_status
ca09d69a 1484add_input_file(const char* pathname)
89fc3421
CC
1485{
1486 gold_assert(parameters->options().has_plugins());
e99daf92
ILT
1487 return parameters->options().plugins()->add_input_file(pathname, false);
1488}
1489
1490// Add a new (real) library required by a plugin.
1491
1492static enum ld_plugin_status
ca09d69a 1493add_input_library(const char* pathname)
e99daf92
ILT
1494{
1495 gold_assert(parameters->options().has_plugins());
1496 return parameters->options().plugins()->add_input_file(pathname, true);
89fc3421
CC
1497}
1498
42218b9f
RÁE
1499// Set the extra library path to be used by libraries added via
1500// add_input_library
1501
1502static enum ld_plugin_status
ca09d69a 1503set_extra_library_path(const char* path)
42218b9f
RÁE
1504{
1505 gold_assert(parameters->options().has_plugins());
1506 return parameters->options().plugins()->set_extra_library_path(path);
1507}
1508
89fc3421
CC
1509// Issue a diagnostic message from a plugin.
1510
1511static enum ld_plugin_status
ca09d69a 1512message(int level, const char* format, ...)
89fc3421
CC
1513{
1514 va_list args;
1515 va_start(args, format);
1516
1517 switch (level)
1518 {
1519 case LDPL_INFO:
1520 parameters->errors()->info(format, args);
1521 break;
1522 case LDPL_WARNING:
1523 parameters->errors()->warning(format, args);
1524 break;
1525 case LDPL_ERROR:
1526 default:
1527 parameters->errors()->error(format, args);
1528 break;
1529 case LDPL_FATAL:
1530 parameters->errors()->fatal(format, args);
1531 break;
1532 }
1533
1534 va_end(args);
1535 return LDPS_OK;
1536}
1537
e9552f7e
ST
1538// Get the section count of the object corresponding to the handle. This
1539// plugin interface can only be called in the claim_file handler of the plugin.
1540
1541static enum ld_plugin_status
1542get_input_section_count(const void* handle, unsigned int* count)
1543{
1544 gold_assert(parameters->options().has_plugins());
1545
1546 if (!parameters->options().plugins()->in_claim_file_handler())
1547 return LDPS_ERR;
1548
1549 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1550
1551 if (obj == NULL)
1552 return LDPS_ERR;
1553
1554 *count = obj->shnum();
1555 return LDPS_OK;
1556}
1557
1558// Get the type of the specified section in the object corresponding
1559// to the handle. This plugin interface can only be called in the
1560// claim_file handler of the plugin.
1561
1562static enum ld_plugin_status
1563get_input_section_type(const struct ld_plugin_section section,
1564 unsigned int* type)
1565{
1566 gold_assert(parameters->options().has_plugins());
1567
1568 if (!parameters->options().plugins()->in_claim_file_handler())
1569 return LDPS_ERR;
1570
1571 Object* obj
1572 = parameters->options().plugins()->get_elf_object(section.handle);
1573
1574 if (obj == NULL)
1575 return LDPS_BAD_HANDLE;
1576
1577 *type = obj->section_type(section.shndx);
1578 return LDPS_OK;
1579}
1580
1581// Get the name of the specified section in the object corresponding
1582// to the handle. This plugin interface can only be called in the
1583// claim_file handler of the plugin.
1584
1585static enum ld_plugin_status
1586get_input_section_name(const struct ld_plugin_section section,
1587 char** section_name_ptr)
1588{
1589 gold_assert(parameters->options().has_plugins());
1590
1591 if (!parameters->options().plugins()->in_claim_file_handler())
1592 return LDPS_ERR;
1593
1594 Object* obj
1595 = parameters->options().plugins()->get_elf_object(section.handle);
1596
1597 if (obj == NULL)
1598 return LDPS_BAD_HANDLE;
1599
1600 // Check if the object is locked before getting the section name.
1601 gold_assert(obj->is_locked());
1602
1603 const std::string section_name = obj->section_name(section.shndx);
1604 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1605 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1606 return LDPS_OK;
1607}
1608
1609// Get the contents of the specified section in the object corresponding
1610// to the handle. This plugin interface can only be called in the
1611// claim_file handler of the plugin.
1612
1613static enum ld_plugin_status
1614get_input_section_contents(const struct ld_plugin_section section,
1615 const unsigned char** section_contents_ptr,
1616 size_t* len)
1617{
1618 gold_assert(parameters->options().has_plugins());
1619
1620 if (!parameters->options().plugins()->in_claim_file_handler())
1621 return LDPS_ERR;
1622
1623 Object* obj
1624 = parameters->options().plugins()->get_elf_object(section.handle);
1625
1626 if (obj == NULL)
1627 return LDPS_BAD_HANDLE;
1628
1629 // Check if the object is locked before getting the section contents.
1630 gold_assert(obj->is_locked());
1631
1632 section_size_type plen;
1633 *section_contents_ptr
1634 = obj->section_contents(section.shndx, &plen, false);
1635 *len = plen;
1636 return LDPS_OK;
1637}
1638
1639// Specify the ordering of sections in the final layout. The sections are
1640// specified as (handle,shndx) pairs in the two arrays in the order in
1641// which they should appear in the final layout.
1642
1643static enum ld_plugin_status
f0558624 1644update_section_order(const struct ld_plugin_section* section_list,
e9552f7e
ST
1645 unsigned int num_sections)
1646{
1647 gold_assert(parameters->options().has_plugins());
1648
1649 if (num_sections == 0)
1650 return LDPS_OK;
1651
1652 if (section_list == NULL)
1653 return LDPS_ERR;
1654
f0558624
ST
1655 Layout* layout = parameters->options().plugins()->layout();
1656 gold_assert (layout != NULL);
e9552f7e 1657
f0558624
ST
1658 std::map<Section_id, unsigned int>* order_map
1659 = layout->get_section_order_map();
1660
1661 /* Store the mapping from Section_id to section position in layout's
1662 order_map to consult after output sections are added. */
e9552f7e
ST
1663 for (unsigned int i = 0; i < num_sections; ++i)
1664 {
1665 Object* obj = parameters->options().plugins()->get_elf_object(
1666 section_list[i].handle);
1667 if (obj == NULL)
1668 return LDPS_BAD_HANDLE;
1669 unsigned int shndx = section_list[i].shndx;
1670 Section_id secn_id(obj, shndx);
f0558624 1671 (*order_map)[secn_id] = i + 1;
e9552f7e
ST
1672 }
1673
e9552f7e
ST
1674 return LDPS_OK;
1675}
1676
1677// Let the linker know that the sections could be reordered.
1678
1679static enum ld_plugin_status
1680allow_section_ordering()
1681{
1682 gold_assert(parameters->options().has_plugins());
1683 Layout* layout = parameters->options().plugins()->layout();
1684 layout->set_section_ordering_specified();
1685 return LDPS_OK;
1686}
1687
89fc3421
CC
1688#endif // ENABLE_PLUGINS
1689
1690// Allocate a Pluginobj object of the appropriate size and endianness.
1691
1692static Pluginobj*
0f7c0701 1693make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
89fc3421 1694{
89fc3421
CC
1695 Pluginobj* obj = NULL;
1696
029ba973
ILT
1697 parameters_force_valid_target();
1698 const Target& target(parameters->target());
89fc3421 1699
029ba973 1700 if (target.get_size() == 32)
89fc3421 1701 {
029ba973 1702 if (target.is_big_endian())
92f03fcb 1703#ifdef HAVE_TARGET_32_BIG
89fc3421 1704 obj = new Sized_pluginobj<32, true>(input_file->filename(),
0f7c0701 1705 input_file, offset, filesize);
92f03fcb
CC
1706#else
1707 gold_error(_("%s: not configured to support "
1708 "32-bit big-endian object"),
1709 input_file->filename().c_str());
89fc3421 1710#endif
89fc3421 1711 else
92f03fcb 1712#ifdef HAVE_TARGET_32_LITTLE
89fc3421 1713 obj = new Sized_pluginobj<32, false>(input_file->filename(),
0f7c0701 1714 input_file, offset, filesize);
92f03fcb
CC
1715#else
1716 gold_error(_("%s: not configured to support "
1717 "32-bit little-endian object"),
1718 input_file->filename().c_str());
89fc3421
CC
1719#endif
1720 }
029ba973 1721 else if (target.get_size() == 64)
89fc3421 1722 {
029ba973 1723 if (target.is_big_endian())
92f03fcb 1724#ifdef HAVE_TARGET_64_BIG
89fc3421 1725 obj = new Sized_pluginobj<64, true>(input_file->filename(),
0f7c0701 1726 input_file, offset, filesize);
92f03fcb
CC
1727#else
1728 gold_error(_("%s: not configured to support "
1729 "64-bit big-endian object"),
1730 input_file->filename().c_str());
89fc3421 1731#endif
89fc3421 1732 else
92f03fcb 1733#ifdef HAVE_TARGET_64_LITTLE
89fc3421 1734 obj = new Sized_pluginobj<64, false>(input_file->filename(),
0f7c0701 1735 input_file, offset, filesize);
92f03fcb
CC
1736#else
1737 gold_error(_("%s: not configured to support "
1738 "64-bit little-endian object"),
1739 input_file->filename().c_str());
89fc3421
CC
1740#endif
1741 }
1742
1743 gold_assert(obj != NULL);
89fc3421
CC
1744 return obj;
1745}
1746
1747} // End namespace gold.
This page took 0.286263 seconds and 4 git commands to generate.