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