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