Remove struct keyword from section_addr_info
[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:
1427 shndx = elfcpp::SHN_ABS;
1428 break;
1429 case LDPK_COMMON:
1430 shndx = elfcpp::SHN_COMMON;
1431 break;
1432 case LDPK_UNDEF:
1433 case LDPK_WEAKUNDEF:
1434 default:
1435 shndx = elfcpp::SHN_UNDEF;
1436 break;
1437 }
1438
1439 switch (isym->visibility)
1440 {
1441 case LDPV_PROTECTED:
105b6afd 1442 vis = elfcpp::STV_PROTECTED;
89fc3421
CC
1443 break;
1444 case LDPV_INTERNAL:
105b6afd 1445 vis = elfcpp::STV_INTERNAL;
89fc3421
CC
1446 break;
1447 case LDPV_HIDDEN:
105b6afd 1448 vis = elfcpp::STV_HIDDEN;
89fc3421
CC
1449 break;
1450 case LDPV_DEFAULT:
1451 default:
1452 vis = elfcpp::STV_DEFAULT;
1453 break;
1454 }
1455
1456 if (isym->comdat_key != NULL
1457 && isym->comdat_key[0] != '\0'
2ea97941 1458 && !this->include_comdat_group(isym->comdat_key, layout))
89fc3421
CC
1459 shndx = elfcpp::SHN_UNDEF;
1460
1461 osym.put_st_name(0);
1462 osym.put_st_value(0);
6168c2a1 1463 osym.put_st_size(0);
89fc3421
CC
1464 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1465 osym.put_st_other(vis, 0);
1466 osym.put_st_shndx(shndx);
1467
1468 this->symbols_[i] =
2ea97941 1469 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
89fc3421
CC
1470 }
1471}
1472
b0193076
RÁE
1473template<int size, bool big_endian>
1474Archive::Should_include
1475Sized_pluginobj<size, big_endian>::do_should_include_member(
88a4108b
ILT
1476 Symbol_table* symtab,
1477 Layout* layout,
1478 Read_symbols_data*,
1479 std::string* why)
b0193076
RÁE
1480{
1481 char* tmpbuf = NULL;
1482 size_t tmpbuflen = 0;
1483
88a4108b
ILT
1484 for (int i = 0; i < this->nsyms_; ++i)
1485 {
1486 const struct ld_plugin_symbol& sym = this->syms_[i];
a06ed37d
RÁE
1487 if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
1488 continue;
88a4108b
ILT
1489 const char* name = sym.name;
1490 Symbol* symbol;
1491 Archive::Should_include t = Archive::should_include_member(symtab,
1492 layout,
1493 name,
1494 &symbol, why,
1495 &tmpbuf,
1496 &tmpbuflen);
b0193076
RÁE
1497 if (t == Archive::SHOULD_INCLUDE_YES)
1498 {
1499 if (tmpbuf != NULL)
1500 free(tmpbuf);
1501 return t;
1502 }
88a4108b 1503 }
b0193076
RÁE
1504 if (tmpbuf != NULL)
1505 free(tmpbuf);
1506 return Archive::SHOULD_INCLUDE_UNKNOWN;
1507}
1508
e0c52780
CC
1509// Iterate over global symbols, calling a visitor class V for each.
1510
1511template<int size, bool big_endian>
1512void
1513Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1514 Read_symbols_data*,
1515 Library_base::Symbol_visitor_base* v)
1516{
1517 for (int i = 0; i < this->nsyms_; ++i)
1518 {
1519 const struct ld_plugin_symbol& sym = this->syms_[i];
1520 if (sym.def != LDPK_UNDEF)
1521 v->visit(sym.name);
1522 }
1523}
1524
cdc29364
CC
1525// Iterate over local symbols, calling a visitor class V for each GOT offset
1526// associated with a local symbol.
1527template<int size, bool big_endian>
1528void
1529Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1530 Got_offset_list::Visitor*) const
1531{
1532 gold_unreachable();
1533}
1534
89fc3421
CC
1535// Get the size of a section. Not used for plugin objects.
1536
1537template<int size, bool big_endian>
1538uint64_t
1539Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1540{
1541 gold_unreachable();
1542 return 0;
1543}
1544
1545// Get the name of a section. Not used for plugin objects.
1546
1547template<int size, bool big_endian>
1548std::string
54674d38 1549Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
89fc3421
CC
1550{
1551 gold_unreachable();
1552 return std::string();
1553}
1554
1555// Return a view of the contents of a section. Not used for plugin objects.
1556
1557template<int size, bool big_endian>
c1027032
CC
1558const unsigned char*
1559Sized_pluginobj<size, big_endian>::do_section_contents(
1560 unsigned int,
1561 section_size_type*,
1562 bool)
89fc3421 1563{
89fc3421 1564 gold_unreachable();
c1027032 1565 return NULL;
89fc3421
CC
1566}
1567
1568// Return section flags. Not used for plugin objects.
1569
1570template<int size, bool big_endian>
1571uint64_t
1572Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1573{
1574 gold_unreachable();
1575 return 0;
1576}
1577
ef15dade
ST
1578// Return section entsize. Not used for plugin objects.
1579
1580template<int size, bool big_endian>
1581uint64_t
1582Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1583{
1584 gold_unreachable();
1585 return 0;
1586}
1587
89fc3421
CC
1588// Return section address. Not used for plugin objects.
1589
1590template<int size, bool big_endian>
1591uint64_t
1592Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1593{
1594 gold_unreachable();
1595 return 0;
1596}
1597
1598// Return section type. Not used for plugin objects.
1599
1600template<int size, bool big_endian>
1601unsigned int
1602Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1603{
1604 gold_unreachable();
1605 return 0;
1606}
1607
1608// Return the section link field. Not used for plugin objects.
1609
1610template<int size, bool big_endian>
1611unsigned int
1612Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1613{
1614 gold_unreachable();
1615 return 0;
1616}
1617
1618// Return the section link field. Not used for plugin objects.
1619
1620template<int size, bool big_endian>
1621unsigned int
1622Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1623{
1624 gold_unreachable();
1625 return 0;
1626}
1627
1628// Return the section alignment. Not used for plugin objects.
1629
1630template<int size, bool big_endian>
1631uint64_t
1632Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1633{
1634 gold_unreachable();
1635 return 0;
1636}
1637
1638// Return the Xindex structure to use. Not used for plugin objects.
1639
1640template<int size, bool big_endian>
1641Xindex*
1642Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1643{
1644 gold_unreachable();
1645 return NULL;
1646}
1647
53bbcc1b
CC
1648// Get symbol counts. Don't count plugin objects; the replacement
1649// files will provide the counts.
89fc3421
CC
1650
1651template<int size, bool big_endian>
1652void
53bbcc1b
CC
1653Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1654 const Symbol_table*,
1655 size_t* defined,
1656 size_t* used) const
89fc3421 1657{
53bbcc1b
CC
1658 *defined = 0;
1659 *used = 0;
89fc3421
CC
1660}
1661
dde3f402
ILT
1662// Get symbols. Not used for plugin objects.
1663
1664template<int size, bool big_endian>
1665const Object::Symbols*
1666Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1667{
1668 gold_unreachable();
1669}
1670
5995b570 1671// Class Plugin_finish. This task runs after all replacement files have
78384e8f
CC
1672// been added. For now, it's a placeholder for a possible plugin API
1673// to allow the plugin to release most of its resources. The cleanup
1674// handlers must be called later, because they can remove the temporary
1675// object files that are needed until the end of the link.
89fc3421 1676
5995b570 1677class Plugin_finish : public Task
89fc3421
CC
1678{
1679 public:
5995b570 1680 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
89fc3421
CC
1681 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1682 { }
1683
5995b570 1684 ~Plugin_finish()
89fc3421
CC
1685 {
1686 if (this->this_blocker_ != NULL)
1687 delete this->this_blocker_;
1688 }
1689
1690 Task_token*
1691 is_runnable()
1692 {
1693 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1694 return this->this_blocker_;
1695 return NULL;
1696 }
1697
1698 void
1699 locks(Task_locker* tl)
1700 { tl->add(this, this->next_blocker_); }
1701
1702 void
1703 run(Workqueue*)
483620e8 1704 {
291158a3
CC
1705 Plugin_manager* plugins = parameters->options().plugins();
1706 gold_assert(plugins != NULL);
78384e8f 1707 // We could call early cleanup handlers here.
291158a3
CC
1708 if (plugins->recorder())
1709 plugins->recorder()->finish();
483620e8 1710 }
89fc3421
CC
1711
1712 std::string
1713 get_name() const
5995b570 1714 { return "Plugin_finish"; }
89fc3421
CC
1715
1716 private:
1717 Task_token* this_blocker_;
1718 Task_token* next_blocker_;
1719};
1720
1721// Class Plugin_hook.
1722
1723Plugin_hook::~Plugin_hook()
1724{
1725}
1726
1727// Return whether a Plugin_hook task is runnable.
1728
1729Task_token*
1730Plugin_hook::is_runnable()
1731{
1732 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1733 return this->this_blocker_;
1734 return NULL;
1735}
1736
1737// Return a Task_locker for a Plugin_hook task. We don't need any
1738// locks here.
1739
1740void
1741Plugin_hook::locks(Task_locker*)
1742{
1743}
1744
89fc3421
CC
1745// Run the "all symbols read" plugin hook.
1746
1747void
5995b570 1748Plugin_hook::run(Workqueue* workqueue)
89fc3421
CC
1749{
1750 gold_assert(this->options_.has_plugins());
a10ae760 1751 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
91ff43fe
RÁE
1752 if (start_sym != NULL)
1753 start_sym->set_in_real_elf();
1754
89fc3421 1755 this->options_.plugins()->all_symbols_read(workqueue,
0f7c0701 1756 this,
89fc3421
CC
1757 this->input_objects_,
1758 this->symtab_,
89fc3421
CC
1759 this->dirpath_,
1760 this->mapfile_,
1761 &this->this_blocker_);
5995b570
CC
1762 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1763 this->next_blocker_));
89fc3421
CC
1764}
1765
1766// The C interface routines called by the plugins.
1767
1768#ifdef ENABLE_PLUGINS
1769
1770// Register a claim-file handler.
1771
1772static enum ld_plugin_status
1773register_claim_file(ld_plugin_claim_file_handler handler)
1774{
1775 gold_assert(parameters->options().has_plugins());
1776 parameters->options().plugins()->set_claim_file_handler(handler);
1777 return LDPS_OK;
1778}
1779
1780// Register an all-symbols-read handler.
1781
1782static enum ld_plugin_status
1783register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1784{
1785 gold_assert(parameters->options().has_plugins());
1786 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1787 return LDPS_OK;
1788}
1789
1790// Register a cleanup handler.
1791
1792static enum ld_plugin_status
1793register_cleanup(ld_plugin_cleanup_handler handler)
1794{
1795 gold_assert(parameters->options().has_plugins());
1796 parameters->options().plugins()->set_cleanup_handler(handler);
1797 return LDPS_OK;
1798}
1799
1800// Add symbols from a plugin-claimed input file.
1801
1802static enum ld_plugin_status
ca09d69a 1803add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
89fc3421
CC
1804{
1805 gold_assert(parameters->options().has_plugins());
1806 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1807 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1808 if (obj == NULL)
1809 return LDPS_ERR;
1810 obj->store_incoming_symbols(nsyms, syms);
1811 return LDPS_OK;
1812}
1813
0f7c0701
CC
1814// Get the input file information with an open (possibly re-opened)
1815// file descriptor.
1816
1817static enum ld_plugin_status
ca09d69a 1818get_input_file(const void* handle, struct ld_plugin_input_file* file)
0f7c0701
CC
1819{
1820 gold_assert(parameters->options().has_plugins());
1821 unsigned int obj_index =
1822 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1823 return parameters->options().plugins()->get_input_file(obj_index, file);
1824}
1825
1826// Release the input file.
1827
1828static enum ld_plugin_status
ca09d69a 1829release_input_file(const void* handle)
0f7c0701
CC
1830{
1831 gold_assert(parameters->options().has_plugins());
1832 unsigned int obj_index =
1833 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1834 return parameters->options().plugins()->release_input_file(obj_index);
1835}
1836
9c793f14
RÁE
1837static enum ld_plugin_status
1838get_view(const void *handle, const void **viewp)
1839{
1840 gold_assert(parameters->options().has_plugins());
1841 unsigned int obj_index =
1842 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1843 return parameters->options().plugins()->get_view(obj_index, viewp);
1844}
1845
89fc3421
CC
1846// Get the symbol resolution info for a plugin-claimed input file.
1847
1848static enum ld_plugin_status
ca09d69a 1849get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
89fc3421
CC
1850{
1851 gold_assert(parameters->options().has_plugins());
3c537f7f
PC
1852 Plugin_manager* plugins = parameters->options().plugins();
1853 Object* obj = plugins->object(
e9552f7e 1854 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
89fc3421
CC
1855 if (obj == NULL)
1856 return LDPS_ERR;
e9552f7e
ST
1857 Pluginobj* plugin_obj = obj->pluginobj();
1858 if (plugin_obj == NULL)
1859 return LDPS_ERR;
3c537f7f
PC
1860 Symbol_table* symtab = plugins->symtab();
1861 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
235061c2
CC
1862}
1863
1864// Version 2 of the above. The only difference is that this version
1865// is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1866
1867static enum ld_plugin_status
1868get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1869{
1870 gold_assert(parameters->options().has_plugins());
3c537f7f
PC
1871 Plugin_manager* plugins = parameters->options().plugins();
1872 Object* obj = plugins->object(
235061c2
CC
1873 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1874 if (obj == NULL)
1875 return LDPS_ERR;
1876 Pluginobj* plugin_obj = obj->pluginobj();
1877 if (plugin_obj == NULL)
1878 return LDPS_ERR;
3c537f7f
PC
1879 Symbol_table* symtab = plugins->symtab();
1880 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
89fc3421
CC
1881}
1882
95ecdfbf
ES
1883// Version 3 of the above. The only difference from v2 is that it
1884// returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1885// decided to include.
1886
1887static enum ld_plugin_status
1888get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
1889{
1890 gold_assert(parameters->options().has_plugins());
1891 Plugin_manager* plugins = parameters->options().plugins();
1892 Object* obj = plugins->object(
1893 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1894 if (obj == NULL)
1895 return LDPS_ERR;
1896 Pluginobj* plugin_obj = obj->pluginobj();
1897 if (plugin_obj == NULL)
1898 return LDPS_ERR;
1899 Symbol_table* symtab = plugins->symtab();
1900 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
1901}
1902
89fc3421
CC
1903// Add a new (real) input file generated by a plugin.
1904
1905static enum ld_plugin_status
ca09d69a 1906add_input_file(const char* pathname)
89fc3421
CC
1907{
1908 gold_assert(parameters->options().has_plugins());
e99daf92
ILT
1909 return parameters->options().plugins()->add_input_file(pathname, false);
1910}
1911
1912// Add a new (real) library required by a plugin.
1913
1914static enum ld_plugin_status
ca09d69a 1915add_input_library(const char* pathname)
e99daf92
ILT
1916{
1917 gold_assert(parameters->options().has_plugins());
1918 return parameters->options().plugins()->add_input_file(pathname, true);
89fc3421
CC
1919}
1920
42218b9f
RÁE
1921// Set the extra library path to be used by libraries added via
1922// add_input_library
1923
1924static enum ld_plugin_status
ca09d69a 1925set_extra_library_path(const char* path)
42218b9f
RÁE
1926{
1927 gold_assert(parameters->options().has_plugins());
1928 return parameters->options().plugins()->set_extra_library_path(path);
1929}
1930
89fc3421
CC
1931// Issue a diagnostic message from a plugin.
1932
1933static enum ld_plugin_status
ca09d69a 1934message(int level, const char* format, ...)
89fc3421
CC
1935{
1936 va_list args;
1937 va_start(args, format);
1938
1939 switch (level)
1940 {
1941 case LDPL_INFO:
1942 parameters->errors()->info(format, args);
1943 break;
1944 case LDPL_WARNING:
1945 parameters->errors()->warning(format, args);
1946 break;
1947 case LDPL_ERROR:
1948 default:
1949 parameters->errors()->error(format, args);
1950 break;
1951 case LDPL_FATAL:
1952 parameters->errors()->fatal(format, args);
1953 break;
1954 }
1955
1956 va_end(args);
1957 return LDPS_OK;
1958}
1959
e9552f7e
ST
1960// Get the section count of the object corresponding to the handle. This
1961// plugin interface can only be called in the claim_file handler of the plugin.
1962
1963static enum ld_plugin_status
1964get_input_section_count(const void* handle, unsigned int* count)
1965{
1966 gold_assert(parameters->options().has_plugins());
1967
1968 if (!parameters->options().plugins()->in_claim_file_handler())
1969 return LDPS_ERR;
1970
1971 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1972
1973 if (obj == NULL)
1974 return LDPS_ERR;
1975
1976 *count = obj->shnum();
1977 return LDPS_OK;
1978}
1979
1980// Get the type of the specified section in the object corresponding
1981// to the handle. This plugin interface can only be called in the
1982// claim_file handler of the plugin.
1983
1984static enum ld_plugin_status
1985get_input_section_type(const struct ld_plugin_section section,
1986 unsigned int* type)
1987{
1988 gold_assert(parameters->options().has_plugins());
1989
1990 if (!parameters->options().plugins()->in_claim_file_handler())
1991 return LDPS_ERR;
1992
1993 Object* obj
1994 = parameters->options().plugins()->get_elf_object(section.handle);
1995
1996 if (obj == NULL)
1997 return LDPS_BAD_HANDLE;
1998
1999 *type = obj->section_type(section.shndx);
2000 return LDPS_OK;
2001}
2002
2003// Get the name of the specified section in the object corresponding
2004// to the handle. This plugin interface can only be called in the
2005// claim_file handler of the plugin.
2006
2007static enum ld_plugin_status
2008get_input_section_name(const struct ld_plugin_section section,
2009 char** section_name_ptr)
2010{
2011 gold_assert(parameters->options().has_plugins());
2012
2013 if (!parameters->options().plugins()->in_claim_file_handler())
2014 return LDPS_ERR;
2015
2016 Object* obj
2017 = parameters->options().plugins()->get_elf_object(section.handle);
2018
2019 if (obj == NULL)
2020 return LDPS_BAD_HANDLE;
2021
2022 // Check if the object is locked before getting the section name.
2023 gold_assert(obj->is_locked());
2024
2025 const std::string section_name = obj->section_name(section.shndx);
2026 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
2027 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
2028 return LDPS_OK;
2029}
2030
2031// Get the contents of the specified section in the object corresponding
2032// to the handle. This plugin interface can only be called in the
2033// claim_file handler of the plugin.
2034
2035static enum ld_plugin_status
2036get_input_section_contents(const struct ld_plugin_section section,
2037 const unsigned char** section_contents_ptr,
2038 size_t* len)
2039{
2040 gold_assert(parameters->options().has_plugins());
2041
2042 if (!parameters->options().plugins()->in_claim_file_handler())
2043 return LDPS_ERR;
2044
2045 Object* obj
2046 = parameters->options().plugins()->get_elf_object(section.handle);
2047
2048 if (obj == NULL)
2049 return LDPS_BAD_HANDLE;
2050
2051 // Check if the object is locked before getting the section contents.
2052 gold_assert(obj->is_locked());
2053
2054 section_size_type plen;
2055 *section_contents_ptr
2056 = obj->section_contents(section.shndx, &plen, false);
2057 *len = plen;
2058 return LDPS_OK;
2059}
2060
82838bd6
CC
2061// Get the alignment of the specified section in the object corresponding
2062// to the handle. This plugin interface can only be called in the
2063// claim_file handler of the plugin.
2064
2065static enum ld_plugin_status
2066get_input_section_alignment(const struct ld_plugin_section section,
2067 unsigned int* addralign)
2068{
2069 gold_assert(parameters->options().has_plugins());
2070
2071 if (!parameters->options().plugins()->in_claim_file_handler())
2072 return LDPS_ERR;
2073
2074 Object* obj
2075 = parameters->options().plugins()->get_elf_object(section.handle);
2076
2077 if (obj == NULL)
2078 return LDPS_BAD_HANDLE;
2079
2080 *addralign = obj->section_addralign(section.shndx);
2081 return LDPS_OK;
2082}
2083
2084// Get the size of the specified section in the object corresponding
2085// to the handle. This plugin interface can only be called in the
2086// claim_file handler of the plugin.
2087
2088static enum ld_plugin_status
2089get_input_section_size(const struct ld_plugin_section section,
2090 uint64_t* secsize)
2091{
2092 gold_assert(parameters->options().has_plugins());
2093
2094 if (!parameters->options().plugins()->in_claim_file_handler())
2095 return LDPS_ERR;
2096
2097 Object* obj
2098 = parameters->options().plugins()->get_elf_object(section.handle);
2099
2100 if (obj == NULL)
2101 return LDPS_BAD_HANDLE;
2102
2103 *secsize = obj->section_size(section.shndx);
2104 return LDPS_OK;
2105}
2106
0b65c07b
ST
2107static enum ld_plugin_status
2108get_wrap_symbols(uint64_t *count, const char ***wrap_symbols)
2109{
2110 gold_assert(parameters->options().has_plugins());
2111 *count = parameters->options().wrap_size();
2112
2113 if (*count == 0)
2114 return LDPS_OK;
2115
2116 *wrap_symbols = new const char *[*count];
2117 int i = 0;
2118 for (options::String_set::const_iterator
2119 it = parameters->options().wrap_begin();
2120 it != parameters->options().wrap_end(); ++it, ++i) {
2121 (*wrap_symbols)[i] = it->c_str();
2122 }
2123 return LDPS_OK;
2124}
2125
82838bd6 2126
e9552f7e
ST
2127// Specify the ordering of sections in the final layout. The sections are
2128// specified as (handle,shndx) pairs in the two arrays in the order in
2129// which they should appear in the final layout.
2130
2131static enum ld_plugin_status
f0558624 2132update_section_order(const struct ld_plugin_section* section_list,
e9552f7e
ST
2133 unsigned int num_sections)
2134{
2135 gold_assert(parameters->options().has_plugins());
2136
2137 if (num_sections == 0)
2138 return LDPS_OK;
2139
2140 if (section_list == NULL)
2141 return LDPS_ERR;
2142
f0558624
ST
2143 Layout* layout = parameters->options().plugins()->layout();
2144 gold_assert (layout != NULL);
e9552f7e 2145
f0558624
ST
2146 std::map<Section_id, unsigned int>* order_map
2147 = layout->get_section_order_map();
2148
2149 /* Store the mapping from Section_id to section position in layout's
2150 order_map to consult after output sections are added. */
e9552f7e
ST
2151 for (unsigned int i = 0; i < num_sections; ++i)
2152 {
2153 Object* obj = parameters->options().plugins()->get_elf_object(
2154 section_list[i].handle);
efc6fa12 2155 if (obj == NULL || obj->is_dynamic())
e9552f7e
ST
2156 return LDPS_BAD_HANDLE;
2157 unsigned int shndx = section_list[i].shndx;
efc6fa12 2158 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
f0558624 2159 (*order_map)[secn_id] = i + 1;
e9552f7e
ST
2160 }
2161
e9552f7e
ST
2162 return LDPS_OK;
2163}
2164
2165// Let the linker know that the sections could be reordered.
2166
2167static enum ld_plugin_status
2168allow_section_ordering()
2169{
2170 gold_assert(parameters->options().has_plugins());
2171 Layout* layout = parameters->options().plugins()->layout();
2172 layout->set_section_ordering_specified();
2173 return LDPS_OK;
2174}
2175
16164a6b
ST
2176// Let the linker know that a subset of sections could be mapped
2177// to a unique segment.
2178
2179static enum ld_plugin_status
2180allow_unique_segment_for_sections()
2181{
2182 gold_assert(parameters->options().has_plugins());
2183 Layout* layout = parameters->options().plugins()->layout();
2184 layout->set_unique_segment_for_sections_specified();
2185 return LDPS_OK;
2186}
2187
2188// This function should map the list of sections specified in the
2189// SECTION_LIST to a unique segment. ELF segments do not have names
2190// and the NAME is used to identify Output Section which should contain
2191// the list of sections. This Output Section will then be mapped to
2192// a unique segment. FLAGS is used to specify if any additional segment
2193// flags need to be set. For instance, a specific segment flag can be
2194// set to identify this segment. Unsetting segment flags is not possible.
2195// ALIGN specifies the alignment of the segment.
2196
2197static enum ld_plugin_status
2198unique_segment_for_sections(const char* segment_name,
2199 uint64_t flags,
2200 uint64_t align,
2201 const struct ld_plugin_section* section_list,
2202 unsigned int num_sections)
2203{
2204 gold_assert(parameters->options().has_plugins());
2205
2206 if (num_sections == 0)
2207 return LDPS_OK;
2208
2209 if (section_list == NULL)
2210 return LDPS_ERR;
2211
2212 Layout* layout = parameters->options().plugins()->layout();
2213 gold_assert (layout != NULL);
2214
2215 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
2216 s->name = segment_name;
2217 s->flags = flags;
2218 s->align = align;
2219
2220 for (unsigned int i = 0; i < num_sections; ++i)
2221 {
2222 Object* obj = parameters->options().plugins()->get_elf_object(
2223 section_list[i].handle);
efc6fa12 2224 if (obj == NULL || obj->is_dynamic())
16164a6b
ST
2225 return LDPS_BAD_HANDLE;
2226 unsigned int shndx = section_list[i].shndx;
efc6fa12 2227 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
16164a6b
ST
2228 layout->insert_section_segment_map(secn_id, s);
2229 }
2230
2231 return LDPS_OK;
2232}
2233
c4e64843
SC
2234// Register a new_input handler.
2235
2236static enum ld_plugin_status
2237register_new_input(ld_plugin_new_input_handler handler)
2238{
2239 gold_assert(parameters->options().has_plugins());
2240 parameters->options().plugins()->set_new_input_handler(handler);
2241 return LDPS_OK;
2242}
2243
89fc3421
CC
2244#endif // ENABLE_PLUGINS
2245
2246// Allocate a Pluginobj object of the appropriate size and endianness.
2247
2248static Pluginobj*
291158a3
CC
2249make_sized_plugin_object(const std::string& filename,
2250 Input_file* input_file, off_t offset, off_t filesize)
89fc3421 2251{
89fc3421
CC
2252 Pluginobj* obj = NULL;
2253
029ba973
ILT
2254 parameters_force_valid_target();
2255 const Target& target(parameters->target());
89fc3421 2256
029ba973 2257 if (target.get_size() == 32)
89fc3421 2258 {
029ba973 2259 if (target.is_big_endian())
92f03fcb 2260#ifdef HAVE_TARGET_32_BIG
291158a3
CC
2261 obj = new Sized_pluginobj<32, true>(filename, input_file,
2262 offset, filesize);
92f03fcb
CC
2263#else
2264 gold_error(_("%s: not configured to support "
2265 "32-bit big-endian object"),
291158a3 2266 filename.c_str());
89fc3421 2267#endif
89fc3421 2268 else
92f03fcb 2269#ifdef HAVE_TARGET_32_LITTLE
291158a3
CC
2270 obj = new Sized_pluginobj<32, false>(filename, input_file,
2271 offset, filesize);
92f03fcb
CC
2272#else
2273 gold_error(_("%s: not configured to support "
2274 "32-bit little-endian object"),
291158a3 2275 filename.c_str());
89fc3421
CC
2276#endif
2277 }
029ba973 2278 else if (target.get_size() == 64)
89fc3421 2279 {
029ba973 2280 if (target.is_big_endian())
92f03fcb 2281#ifdef HAVE_TARGET_64_BIG
291158a3
CC
2282 obj = new Sized_pluginobj<64, true>(filename, input_file,
2283 offset, filesize);
92f03fcb
CC
2284#else
2285 gold_error(_("%s: not configured to support "
2286 "64-bit big-endian object"),
291158a3 2287 filename.c_str());
89fc3421 2288#endif
89fc3421 2289 else
92f03fcb 2290#ifdef HAVE_TARGET_64_LITTLE
291158a3
CC
2291 obj = new Sized_pluginobj<64, false>(filename, input_file,
2292 offset, filesize);
92f03fcb
CC
2293#else
2294 gold_error(_("%s: not configured to support "
2295 "64-bit little-endian object"),
291158a3 2296 filename.c_str());
89fc3421
CC
2297#endif
2298 }
2299
2300 gold_assert(obj != NULL);
89fc3421
CC
2301 return obj;
2302}
2303
2304} // End namespace gold.
This page took 0.56458 seconds and 4 git commands to generate.