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