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