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