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