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