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