Add missing ChangeLog entry for last commit.
[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 {
571 gold_warning(_("%s: can't open (%s)\n"), inname, strerror(errno));
572 return false;
573 }
574 int out = ::open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0600);
575 if (out < 0)
576 {
577 gold_warning(_("%s: can't create (%s)\n"), outname, strerror(errno));
578 ::close(in);
579 return false;
580 }
581 ssize_t len;
582 while ((len = ::read(in, buf, sizeof(buf))) > 0)
583 static_cast<void>(::write(out, buf, len));
584 ::close(in);
585 ::close(out);
586 return true;
587}
588
589void
590Plugin_recorder::replacement_file(const char* name, bool is_lib)
591{
592 fprintf(this->logfile_, "REPLACEMENT: %s", name);
593 if (is_lib)
594 fprintf(this->logfile_, "(lib)");
595 else
596 {
597 char counter[10];
598 const char* basename = lbasename(name);
599 snprintf(counter, sizeof(counter), "%05d", this->file_count_);
600 ++this->file_count_;
601 std::string outname(this->tempdir_);
602 outname.append("/");
603 outname.append(counter);
604 outname.append("-");
605 outname.append(basename);
606 if (link_or_copy_file(name, outname.c_str()))
607 fprintf(this->logfile_, " -> %s", outname.c_str());
608 }
609 fprintf(this->logfile_, "\n");
610}
611
612void
613Plugin_recorder::record_symbols(const Object* obj, int nsyms,
614 const struct ld_plugin_symbol* syms)
615{
616 fprintf(this->logfile_, "SYMBOLS: %d %s\n", nsyms, obj->name().c_str());
617 for (int i = 0; i < nsyms; ++i)
618 {
619 const struct ld_plugin_symbol* isym = &syms[i];
620
621 const char* def;
622 switch (isym->def)
623 {
624 case LDPK_DEF:
625 def = "D";
626 break;
627 case LDPK_WEAKDEF:
628 def = "WD";
629 break;
630 case LDPK_UNDEF:
631 def = "U";
632 break;
633 case LDPK_WEAKUNDEF:
634 def = "WU";
635 break;
636 case LDPK_COMMON:
637 def = "C";
638 break;
639 default:
640 def = "?";
641 break;
642 }
643
644 char vis;
645 switch (isym->visibility)
646 {
647 case LDPV_PROTECTED:
648 vis = 'P';
649 break;
650 case LDPV_INTERNAL:
651 vis = 'I';
652 break;
653 case LDPV_HIDDEN:
654 vis = 'H';
655 break;
656 case LDPV_DEFAULT:
657 vis = 'D';
658 break;
659 default:
660 vis = '?';
661 break;
662 }
663
664 fprintf(this->logfile_, " %5d: %-2s %c %s", i, def, vis, isym->name);
665 if (isym->version != NULL && isym->version[0] != '\0')
666 fprintf(this->logfile_, "@%s", isym->version);
667 if (isym->comdat_key != NULL && isym->comdat_key[0] != '\0')
668 {
669 if (strcmp(isym->name, isym->comdat_key) == 0)
670 fprintf(this->logfile_, " [comdat]");
671 else
672 fprintf(this->logfile_, " [comdat: %s]", isym->comdat_key);
673 }
674 fprintf(this->logfile_, "\n");
675 }
676}
677
89fc3421
CC
678// Plugin_manager methods.
679
680Plugin_manager::~Plugin_manager()
681{
682 for (Plugin_list::iterator p = this->plugins_.begin();
683 p != this->plugins_.end();
684 ++p)
685 delete *p;
686 this->plugins_.clear();
687 for (Object_list::iterator obj = this->objects_.begin();
688 obj != this->objects_.end();
689 ++obj)
690 delete *obj;
691 this->objects_.clear();
d37ffe25 692 delete this->lock_;
291158a3 693 delete this->recorder_;
89fc3421
CC
694}
695
696// Load all plugin libraries.
697
698void
e9552f7e 699Plugin_manager::load_plugins(Layout* layout)
89fc3421 700{
e9552f7e 701 this->layout_ = layout;
291158a3
CC
702
703 if (is_debugging_enabled(DEBUG_PLUGIN))
704 {
705 this->recorder_ = new Plugin_recorder();
706 this->recorder_->init();
707 }
708
89fc3421
CC
709 for (this->current_ = this->plugins_.begin();
710 this->current_ != this->plugins_.end();
711 ++this->current_)
712 (*this->current_)->load();
713}
714
715// Call the plugin claim-file handlers in turn to see if any claim the file.
716
717Pluginobj*
718Plugin_manager::claim_file(Input_file* input_file, off_t offset,
e9552f7e 719 off_t filesize, Object* elf_object)
89fc3421 720{
d37ffe25
ED
721 bool lock_initialized = this->initialize_lock_.initialize();
722
723 gold_assert(lock_initialized);
724 Hold_lock hl(*this->lock_);
89fc3421
CC
725
726 unsigned int handle = this->objects_.size();
727 this->input_file_ = input_file;
728 this->plugin_input_file_.name = input_file->filename().c_str();
729 this->plugin_input_file_.fd = input_file->file().descriptor();
730 this->plugin_input_file_.offset = offset;
731 this->plugin_input_file_.filesize = filesize;
732 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
e9552f7e
ST
733 if (elf_object != NULL)
734 this->objects_.push_back(elf_object);
735 this->in_claim_file_handler_ = true;
89fc3421
CC
736
737 for (this->current_ = this->plugins_.begin();
738 this->current_ != this->plugins_.end();
739 ++this->current_)
740 {
c4e64843
SC
741 // If we aren't yet in replacement phase, allow plugins to claim input
742 // files, otherwise notify the plugin of the new input file, if needed.
743 if (!this->in_replacement_phase_)
89fc3421 744 {
c4e64843
SC
745 if ((*this->current_)->claim_file(&this->plugin_input_file_))
746 {
747 this->any_claimed_ = true;
748 this->in_claim_file_handler_ = false;
749
291158a3
CC
750 if (this->recorder_ != NULL)
751 {
752 const std::string& objname = (elf_object == NULL
753 ? input_file->filename()
754 : elf_object->name());
755 this->recorder_->claimed_file(objname,
756 offset, filesize,
757 (*this->current_)->filename());
758 }
759
c4e64843
SC
760 if (this->objects_.size() > handle
761 && this->objects_[handle]->pluginobj() != NULL)
762 return this->objects_[handle]->pluginobj();
763
764 // If the plugin claimed the file but did not call the
765 // add_symbols callback, we need to create the Pluginobj now.
766 Pluginobj* obj = this->make_plugin_object(handle);
767 return obj;
768 }
769 }
770 else
771 {
772 (*this->current_)->new_input(&this->plugin_input_file_);
89fc3421
CC
773 }
774 }
775
e9552f7e 776 this->in_claim_file_handler_ = false;
291158a3
CC
777
778 if (this->recorder_ != NULL)
779 this->recorder_->unclaimed_file(input_file->filename(), offset, filesize);
780
89fc3421
CC
781 return NULL;
782}
783
0f3b89d8
ILT
784// Save an archive. This is used so that a plugin can add a file
785// which refers to a symbol which was not previously referenced. In
786// that case we want to pretend that the symbol was referenced before,
787// and pull in the archive object.
788
789void
790Plugin_manager::save_archive(Archive* archive)
791{
792 if (this->in_replacement_phase_ || !this->any_claimed_)
793 delete archive;
794 else
795 this->rescannable_.push_back(Rescannable(archive));
796}
797
798// Save an Input_group. This is like save_archive.
799
800void
801Plugin_manager::save_input_group(Input_group* input_group)
802{
803 if (this->in_replacement_phase_ || !this->any_claimed_)
804 delete input_group;
805 else
806 this->rescannable_.push_back(Rescannable(input_group));
807}
808
89fc3421
CC
809// Call the all-symbols-read handlers.
810
811void
0f7c0701 812Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
89fc3421 813 Input_objects* input_objects,
e9552f7e 814 Symbol_table* symtab,
89fc3421
CC
815 Dirsearch* dirpath, Mapfile* mapfile,
816 Task_token** last_blocker)
817{
818 this->in_replacement_phase_ = true;
819 this->workqueue_ = workqueue;
0f7c0701 820 this->task_ = task;
89fc3421
CC
821 this->input_objects_ = input_objects;
822 this->symtab_ = symtab;
89fc3421
CC
823 this->dirpath_ = dirpath;
824 this->mapfile_ = mapfile;
825 this->this_blocker_ = NULL;
826
3281b315
ST
827 // Set symbols used in defsym expressions as seen in real ELF.
828 Layout *layout = parameters->options().plugins()->layout();
829 layout->script_options()->set_defsym_uses_in_real_elf(symtab);
830 layout->script_options()->find_defsym_defs(this->defsym_defines_set_);
831
89fc3421
CC
832 for (this->current_ = this->plugins_.begin();
833 this->current_ != this->plugins_.end();
834 ++this->current_)
835 (*this->current_)->all_symbols_read();
836
0f3b89d8
ILT
837 if (this->any_added_)
838 {
839 Task_token* next_blocker = new Task_token(true);
840 next_blocker->add_blocker();
841 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
842 this->this_blocker_ = next_blocker;
843 }
844
89fc3421
CC
845 *last_blocker = this->this_blocker_;
846}
847
0f3b89d8
ILT
848// This is called when we see a new undefined symbol. If we are in
849// the replacement phase, this means that we may need to rescan some
850// archives we have previously seen.
851
852void
853Plugin_manager::new_undefined_symbol(Symbol* sym)
854{
855 if (this->in_replacement_phase_)
856 this->undefined_symbols_.push_back(sym);
857}
858
859// Rescan archives as needed. This handles the case where a new
860// object file added by a plugin has an undefined reference to some
861// symbol defined in an archive.
862
863void
864Plugin_manager::rescan(Task* task)
865{
866 size_t rescan_pos = 0;
867 size_t rescan_size = this->rescannable_.size();
868 while (!this->undefined_symbols_.empty())
869 {
870 if (rescan_pos >= rescan_size)
871 {
872 this->undefined_symbols_.clear();
873 return;
874 }
875
876 Undefined_symbol_list undefs;
877 undefs.reserve(this->undefined_symbols_.size());
878 this->undefined_symbols_.swap(undefs);
879
880 size_t min_rescan_pos = rescan_size;
881
882 for (Undefined_symbol_list::const_iterator p = undefs.begin();
883 p != undefs.end();
884 ++p)
885 {
886 if (!(*p)->is_undefined())
887 continue;
888
889 this->undefined_symbols_.push_back(*p);
890
891 // Find the first rescan archive which defines this symbol,
892 // starting at the current rescan position. The rescan position
893 // exists so that given -la -lb -lc we don't look for undefined
894 // symbols in -lb back in -la, but instead get the definition
895 // from -lc. Don't bother to look past the current minimum
896 // rescan position.
897 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
898 {
899 if (this->rescannable_defines(i, *p))
900 {
901 min_rescan_pos = i;
902 break;
903 }
904 }
905 }
906
907 if (min_rescan_pos >= rescan_size)
908 {
909 // We didn't find any rescannable archives which define any
910 // undefined symbols.
911 return;
912 }
913
914 const Rescannable& r(this->rescannable_[min_rescan_pos]);
915 if (r.is_archive)
916 {
917 Task_lock_obj<Archive> tl(task, r.u.archive);
918 r.u.archive->add_symbols(this->symtab_, this->layout_,
919 this->input_objects_, this->mapfile_);
920 }
921 else
922 {
923 size_t next_saw_undefined = this->symtab_->saw_undefined();
924 size_t saw_undefined;
925 do
926 {
927 saw_undefined = next_saw_undefined;
928
929 for (Input_group::const_iterator p = r.u.input_group->begin();
930 p != r.u.input_group->end();
931 ++p)
932 {
933 Task_lock_obj<Archive> tl(task, *p);
934
935 (*p)->add_symbols(this->symtab_, this->layout_,
936 this->input_objects_, this->mapfile_);
937 }
938
939 next_saw_undefined = this->symtab_->saw_undefined();
940 }
941 while (saw_undefined != next_saw_undefined);
942 }
943
944 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
945 {
946 if (this->rescannable_[i].is_archive)
947 delete this->rescannable_[i].u.archive;
948 else
949 delete this->rescannable_[i].u.input_group;
950 }
951
952 rescan_pos = min_rescan_pos + 1;
953 }
954}
955
956// Return whether the rescannable at index I defines SYM.
957
958bool
959Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
960{
961 const Rescannable& r(this->rescannable_[i]);
962 if (r.is_archive)
963 return r.u.archive->defines_symbol(sym);
964 else
965 {
966 for (Input_group::const_iterator p = r.u.input_group->begin();
967 p != r.u.input_group->end();
968 ++p)
969 {
970 if ((*p)->defines_symbol(sym))
971 return true;
972 }
973 return false;
974 }
975}
976
483620e8 977// Layout deferred objects.
89fc3421
CC
978
979void
483620e8 980Plugin_manager::layout_deferred_objects()
89fc3421 981{
5995b570
CC
982 Deferred_layout_list::iterator obj;
983
984 for (obj = this->deferred_layout_objects_.begin();
985 obj != this->deferred_layout_objects_.end();
986 ++obj)
5f9bcf58
CC
987 {
988 // Lock the object so we can read from it. This is only called
989 // single-threaded from queue_middle_tasks, so it is OK to lock.
990 // Unfortunately we have no way to pass in a Task token.
991 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
992 Task_lock_obj<Object> tl(dummy_task, *obj);
993 (*obj)->layout_deferred_sections(this->layout_);
994 }
483620e8
CC
995}
996
997// Call the cleanup handlers.
5995b570 998
483620e8
CC
999void
1000Plugin_manager::cleanup()
1001{
20e2a8aa
ILT
1002 if (this->any_added_)
1003 {
1004 // If any input files were added, close all the input files.
1005 // This is because the plugin may want to remove them, and on
1006 // Windows you are not allowed to remove an open file.
1007 close_all_descriptors();
1008 }
1009
89fc3421
CC
1010 for (this->current_ = this->plugins_.begin();
1011 this->current_ != this->plugins_.end();
1012 ++this->current_)
1013 (*this->current_)->cleanup();
1014}
1015
1016// Make a new Pluginobj object. This is called when the plugin calls
1017// the add_symbols API.
1018
1019Pluginobj*
1020Plugin_manager::make_plugin_object(unsigned int handle)
1021{
1022 // Make sure we aren't asked to make an object for the same handle twice.
e9552f7e
ST
1023 if (this->objects_.size() != handle
1024 && this->objects_[handle]->pluginobj() != NULL)
89fc3421
CC
1025 return NULL;
1026
291158a3
CC
1027 const std::string* filename = &this->input_file_->filename();
1028
1029 // If the elf object for this file was pushed into the objects_ vector,
1030 // use its filename, then delete it to make room for the Pluginobj as
1031 // this file is claimed.
1032 if (this->objects_.size() != handle)
1033 {
1034 filename = &this->objects_.back()->name();
1035 this->objects_.pop_back();
1036 }
1037
1038 Pluginobj* obj = make_sized_plugin_object(*filename,
1039 this->input_file_,
0f7c0701
CC
1040 this->plugin_input_file_.offset,
1041 this->plugin_input_file_.filesize);
e9552f7e
ST
1042
1043
e9552f7e 1044
89fc3421
CC
1045 this->objects_.push_back(obj);
1046 return obj;
1047}
1048
0f7c0701
CC
1049// Get the input file information with an open (possibly re-opened)
1050// file descriptor.
1051
1052ld_plugin_status
1053Plugin_manager::get_input_file(unsigned int handle,
ca09d69a 1054 struct ld_plugin_input_file* file)
0f7c0701 1055{
e9552f7e 1056 Pluginobj* obj = this->object(handle)->pluginobj();
0f7c0701
CC
1057 if (obj == NULL)
1058 return LDPS_BAD_HANDLE;
1059
1060 obj->lock(this->task_);
1061 file->name = obj->filename().c_str();
1062 file->fd = obj->descriptor();
1063 file->offset = obj->offset();
1064 file->filesize = obj->filesize();
1065 file->handle = reinterpret_cast<void*>(handle);
1066 return LDPS_OK;
1067}
1068
1069// Release the input file.
1070
1071ld_plugin_status
1072Plugin_manager::release_input_file(unsigned int handle)
1073{
e9552f7e
ST
1074 if (this->object(handle) == NULL)
1075 return LDPS_BAD_HANDLE;
1076
1077 Pluginobj* obj = this->object(handle)->pluginobj();
1078
0f7c0701
CC
1079 if (obj == NULL)
1080 return LDPS_BAD_HANDLE;
1081
1082 obj->unlock(this->task_);
1083 return LDPS_OK;
1084}
1085
e9552f7e
ST
1086// Get the elf object corresponding to the handle. Return NULL if we
1087// found a Pluginobj instead.
1088
1089Object*
1090Plugin_manager::get_elf_object(const void* handle)
1091{
1092 Object* obj = this->object(
1093 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1094
1095 // The object should not be a Pluginobj.
1096 if (obj == NULL
1097 || obj->pluginobj() != NULL)
1098 return NULL;
1099
1100 return obj;
1101}
1102
9c793f14
RÁE
1103ld_plugin_status
1104Plugin_manager::get_view(unsigned int handle, const void **viewp)
1105{
1106 off_t offset;
1107 size_t filesize;
1108 Input_file *input_file;
e9552f7e 1109 if (this->in_claim_file_handler_)
9c793f14
RÁE
1110 {
1111 // We are being called from the claim_file hook.
1112 const struct ld_plugin_input_file &f = this->plugin_input_file_;
1113 offset = f.offset;
1114 filesize = f.filesize;
1115 input_file = this->input_file_;
1116 }
1117 else
1118 {
1119 // An already claimed file.
e9552f7e
ST
1120 if (this->object(handle) == NULL)
1121 return LDPS_BAD_HANDLE;
1122 Pluginobj* obj = this->object(handle)->pluginobj();
9c793f14
RÁE
1123 if (obj == NULL)
1124 return LDPS_BAD_HANDLE;
1125 offset = obj->offset();
1126 filesize = obj->filesize();
1127 input_file = obj->input_file();
1128 }
1129 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
1130 false);
1131 return LDPS_OK;
1132}
1133
42218b9f
RÁE
1134// Add a new library path.
1135
1136ld_plugin_status
ca09d69a 1137Plugin_manager::set_extra_library_path(const char* path)
42218b9f
RÁE
1138{
1139 this->extra_search_path_ = std::string(path);
1140 return LDPS_OK;
1141}
1142
89fc3421
CC
1143// Add a new input file.
1144
1145ld_plugin_status
ca09d69a 1146Plugin_manager::add_input_file(const char* pathname, bool is_lib)
89fc3421 1147{
ae3b5189
CD
1148 Input_file_argument file(pathname,
1149 (is_lib
1150 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
1151 : Input_file_argument::INPUT_FILE_TYPE_FILE),
42218b9f
RÁE
1152 (is_lib
1153 ? this->extra_search_path_.c_str()
1154 : ""),
1155 false,
1156 this->options_);
89fc3421
CC
1157 Input_argument* input_argument = new Input_argument(file);
1158 Task_token* next_blocker = new Task_token(true);
1159 next_blocker->add_blocker();
8c21d9d3 1160 if (parameters->incremental())
40f36857
CC
1161 gold_error(_("input files added by plug-ins in --incremental mode not "
1162 "supported yet"));
291158a3
CC
1163
1164 if (this->recorder_ != NULL)
1165 this->recorder_->replacement_file(pathname, is_lib);
1166
f1ed28fb 1167 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
89fc3421
CC
1168 this->symtab_,
1169 this->layout_,
1170 this->dirpath_,
15f8229b 1171 0,
89fc3421
CC
1172 this->mapfile_,
1173 input_argument,
1174 NULL,
b0193076 1175 NULL,
89fc3421
CC
1176 this->this_blocker_,
1177 next_blocker));
1178 this->this_blocker_ = next_blocker;
0f3b89d8 1179 this->any_added_ = true;
89fc3421
CC
1180 return LDPS_OK;
1181}
1182
1183// Class Pluginobj.
1184
2ea97941
ILT
1185Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
1186 off_t offset, off_t filesize)
1187 : Object(name, input_file, false, offset),
1188 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
89fc3421
CC
1189{
1190}
1191
235061c2 1192// Return TRUE if a defined symbol is referenced from outside the
f7c5b166
CC
1193// universe of claimed objects. Only references from relocatable,
1194// non-IR (unclaimed) objects count as a reference. References from
1195// dynamic objects count only as "visible".
d66a9eb3
CC
1196
1197static inline bool
235061c2 1198is_referenced_from_outside(Symbol* lsym)
d66a9eb3
CC
1199{
1200 if (lsym->in_real_elf())
1201 return true;
1202 if (parameters->options().relocatable())
1203 return true;
84ced98a
RÁE
1204 if (parameters->options().is_undefined(lsym->name()))
1205 return true;
235061c2
CC
1206 return false;
1207}
1208
1209// Return TRUE if a defined symbol might be reachable from outside the
1210// load module.
1211
1212static inline bool
1213is_visible_from_outside(Symbol* lsym)
1214{
f7c5b166
CC
1215 if (lsym->in_dyn())
1216 return true;
ca464aac
TJ
1217 if (parameters->options().export_dynamic() || parameters->options().shared()
1218 || parameters->options().in_dynamic_list(lsym->name())
1219 || parameters->options().is_export_dynamic_symbol(lsym->name()))
d66a9eb3
CC
1220 return lsym->is_externally_visible();
1221 return false;
1222}
1223
89fc3421
CC
1224// Get symbol resolution info.
1225
1226ld_plugin_status
3c537f7f
PC
1227Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
1228 int nsyms,
235061c2
CC
1229 ld_plugin_symbol* syms,
1230 int version) const
1231{
1232 // For version 1 of this interface, we cannot use
1233 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
1234 // instead.
1235 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
1236 = (version > 1
1237 ? LDPR_PREVAILING_DEF_IRONLY_EXP
1238 : LDPR_PREVAILING_DEF);
1239
abc8dcba 1240 if (nsyms > this->nsyms_)
89fc3421 1241 return LDPS_NO_SYMS;
b0193076
RÁE
1242
1243 if (static_cast<size_t>(nsyms) > this->symbols_.size())
1244 {
1245 // We never decided to include this object. We mark all symbols as
1246 // preempted.
ca09d69a 1247 gold_assert(this->symbols_.size() == 0);
b0193076
RÁE
1248 for (int i = 0; i < nsyms; i++)
1249 syms[i].resolution = LDPR_PREEMPTED_REG;
95ecdfbf 1250 return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
b0193076
RÁE
1251 }
1252
3281b315 1253 Plugin_manager* plugins = parameters->options().plugins();
89fc3421
CC
1254 for (int i = 0; i < nsyms; i++)
1255 {
1256 ld_plugin_symbol* isym = &syms[i];
1257 Symbol* lsym = this->symbols_[i];
3c537f7f
PC
1258 if (lsym->is_forwarder())
1259 lsym = symtab->resolve_forwards(lsym);
89fc3421
CC
1260 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
1261
3281b315
ST
1262 if (plugins->is_defsym_def(lsym->name()))
1263 {
1264 // The symbol is redefined via defsym.
1265 res = LDPR_PREEMPTED_REG;
1266 }
1267 else if (lsym->is_undefined())
1268 {
1269 // The symbol remains undefined.
1270 res = LDPR_UNDEF;
1271 }
89fc3421
CC
1272 else if (isym->def == LDPK_UNDEF
1273 || isym->def == LDPK_WEAKUNDEF
1274 || isym->def == LDPK_COMMON)
1275 {
1276 // The original symbol was undefined or common.
1277 if (lsym->source() != Symbol::FROM_OBJECT)
1278 res = LDPR_RESOLVED_EXEC;
be234d88 1279 else if (lsym->object()->pluginobj() == this)
235061c2
CC
1280 {
1281 if (is_referenced_from_outside(lsym))
1282 res = LDPR_PREVAILING_DEF;
1283 else if (is_visible_from_outside(lsym))
1284 res = ldpr_prevailing_def_ironly_exp;
1285 else
1286 res = LDPR_PREVAILING_DEF_IRONLY;
1287 }
89fc3421
CC
1288 else if (lsym->object()->pluginobj() != NULL)
1289 res = LDPR_RESOLVED_IR;
1290 else if (lsym->object()->is_dynamic())
1291 res = LDPR_RESOLVED_DYN;
1292 else
1293 res = LDPR_RESOLVED_EXEC;
1294 }
1295 else
1296 {
1297 // The original symbol was a definition.
1298 if (lsym->source() != Symbol::FROM_OBJECT)
1299 res = LDPR_PREEMPTED_REG;
1300 else if (lsym->object() == static_cast<const Object*>(this))
235061c2
CC
1301 {
1302 if (is_referenced_from_outside(lsym))
1303 res = LDPR_PREVAILING_DEF;
1304 else if (is_visible_from_outside(lsym))
1305 res = ldpr_prevailing_def_ironly_exp;
1306 else
1307 res = LDPR_PREVAILING_DEF_IRONLY;
1308 }
89fc3421
CC
1309 else
1310 res = (lsym->object()->pluginobj() != NULL
1311 ? LDPR_PREEMPTED_IR
1312 : LDPR_PREEMPTED_REG);
1313 }
1314 isym->resolution = res;
1315 }
1316 return LDPS_OK;
1317}
1318
1319// Return TRUE if the comdat group with key COMDAT_KEY from this object
1320// should be kept.
1321
1322bool
2ea97941 1323Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
89fc3421
CC
1324{
1325 std::pair<Comdat_map::iterator, bool> ins =
1326 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1327
1328 // If this is the first time we've seen this comdat key, ask the
1329 // layout object whether it should be included.
1330 if (ins.second)
2ea97941
ILT
1331 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1332 NULL, 0, true,
1333 true, NULL);
89fc3421
CC
1334
1335 return ins.first->second;
1336}
1337
1338// Class Sized_pluginobj.
1339
1340template<int size, bool big_endian>
1341Sized_pluginobj<size, big_endian>::Sized_pluginobj(
2ea97941
ILT
1342 const std::string& name,
1343 Input_file* input_file,
1344 off_t offset,
1345 off_t filesize)
1346 : Pluginobj(name, input_file, offset, filesize)
89fc3421
CC
1347{
1348}
1349
1350// Read the symbols. Not used for plugin objects.
1351
1352template<int size, bool big_endian>
1353void
1354Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1355{
1356 gold_unreachable();
1357}
1358
1359// Lay out the input sections. Not used for plugin objects.
1360
1361template<int size, bool big_endian>
1362void
1363Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1364 Read_symbols_data*)
1365{
1366 gold_unreachable();
1367}
1368
1369// Add the symbols to the symbol table.
1370
89fc3421
CC
1371template<int size, bool big_endian>
1372void
1373Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
f488e4b0 1374 Read_symbols_data*,
2ea97941 1375 Layout* layout)
89fc3421
CC
1376{
1377 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1378 unsigned char symbuf[sym_size];
1379 elfcpp::Sym<size, big_endian> sym(symbuf);
1380 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1381
291158a3
CC
1382 Plugin_recorder* recorder = parameters->options().plugins()->recorder();
1383 if (recorder != NULL)
1384 recorder->record_symbols(this, this->nsyms_, this->syms_);
1385
89fc3421
CC
1386 this->symbols_.resize(this->nsyms_);
1387
1388 for (int i = 0; i < this->nsyms_; ++i)
1389 {
ca09d69a 1390 const struct ld_plugin_symbol* isym = &this->syms_[i];
2ea97941 1391 const char* name = isym->name;
89fc3421
CC
1392 const char* ver = isym->version;
1393 elfcpp::Elf_Half shndx;
1394 elfcpp::STB bind;
1395 elfcpp::STV vis;
1396
2ea97941
ILT
1397 if (name != NULL && name[0] == '\0')
1398 name = NULL;
89fc3421
CC
1399 if (ver != NULL && ver[0] == '\0')
1400 ver = NULL;
1401
1402 switch (isym->def)
1403 {
1404 case LDPK_WEAKDEF:
1405 case LDPK_WEAKUNDEF:
1406 bind = elfcpp::STB_WEAK;
1407 break;
1408 case LDPK_DEF:
1409 case LDPK_UNDEF:
1410 case LDPK_COMMON:
1411 default:
1412 bind = elfcpp::STB_GLOBAL;
1413 break;
1414 }
1415
1416 switch (isym->def)
1417 {
1418 case LDPK_DEF:
1419 case LDPK_WEAKDEF:
1420 shndx = elfcpp::SHN_ABS;
1421 break;
1422 case LDPK_COMMON:
1423 shndx = elfcpp::SHN_COMMON;
1424 break;
1425 case LDPK_UNDEF:
1426 case LDPK_WEAKUNDEF:
1427 default:
1428 shndx = elfcpp::SHN_UNDEF;
1429 break;
1430 }
1431
1432 switch (isym->visibility)
1433 {
1434 case LDPV_PROTECTED:
105b6afd 1435 vis = elfcpp::STV_PROTECTED;
89fc3421
CC
1436 break;
1437 case LDPV_INTERNAL:
105b6afd 1438 vis = elfcpp::STV_INTERNAL;
89fc3421
CC
1439 break;
1440 case LDPV_HIDDEN:
105b6afd 1441 vis = elfcpp::STV_HIDDEN;
89fc3421
CC
1442 break;
1443 case LDPV_DEFAULT:
1444 default:
1445 vis = elfcpp::STV_DEFAULT;
1446 break;
1447 }
1448
1449 if (isym->comdat_key != NULL
1450 && isym->comdat_key[0] != '\0'
2ea97941 1451 && !this->include_comdat_group(isym->comdat_key, layout))
89fc3421
CC
1452 shndx = elfcpp::SHN_UNDEF;
1453
1454 osym.put_st_name(0);
1455 osym.put_st_value(0);
6168c2a1 1456 osym.put_st_size(0);
89fc3421
CC
1457 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1458 osym.put_st_other(vis, 0);
1459 osym.put_st_shndx(shndx);
1460
1461 this->symbols_[i] =
2ea97941 1462 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
89fc3421
CC
1463 }
1464}
1465
b0193076
RÁE
1466template<int size, bool big_endian>
1467Archive::Should_include
1468Sized_pluginobj<size, big_endian>::do_should_include_member(
88a4108b
ILT
1469 Symbol_table* symtab,
1470 Layout* layout,
1471 Read_symbols_data*,
1472 std::string* why)
b0193076
RÁE
1473{
1474 char* tmpbuf = NULL;
1475 size_t tmpbuflen = 0;
1476
88a4108b
ILT
1477 for (int i = 0; i < this->nsyms_; ++i)
1478 {
1479 const struct ld_plugin_symbol& sym = this->syms_[i];
a06ed37d
RÁE
1480 if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
1481 continue;
88a4108b
ILT
1482 const char* name = sym.name;
1483 Symbol* symbol;
1484 Archive::Should_include t = Archive::should_include_member(symtab,
1485 layout,
1486 name,
1487 &symbol, why,
1488 &tmpbuf,
1489 &tmpbuflen);
b0193076
RÁE
1490 if (t == Archive::SHOULD_INCLUDE_YES)
1491 {
1492 if (tmpbuf != NULL)
1493 free(tmpbuf);
1494 return t;
1495 }
88a4108b 1496 }
b0193076
RÁE
1497 if (tmpbuf != NULL)
1498 free(tmpbuf);
1499 return Archive::SHOULD_INCLUDE_UNKNOWN;
1500}
1501
e0c52780
CC
1502// Iterate over global symbols, calling a visitor class V for each.
1503
1504template<int size, bool big_endian>
1505void
1506Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1507 Read_symbols_data*,
1508 Library_base::Symbol_visitor_base* v)
1509{
1510 for (int i = 0; i < this->nsyms_; ++i)
1511 {
1512 const struct ld_plugin_symbol& sym = this->syms_[i];
1513 if (sym.def != LDPK_UNDEF)
1514 v->visit(sym.name);
1515 }
1516}
1517
cdc29364
CC
1518// Iterate over local symbols, calling a visitor class V for each GOT offset
1519// associated with a local symbol.
1520template<int size, bool big_endian>
1521void
1522Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1523 Got_offset_list::Visitor*) const
1524{
1525 gold_unreachable();
1526}
1527
89fc3421
CC
1528// Get the size of a section. Not used for plugin objects.
1529
1530template<int size, bool big_endian>
1531uint64_t
1532Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1533{
1534 gold_unreachable();
1535 return 0;
1536}
1537
1538// Get the name of a section. Not used for plugin objects.
1539
1540template<int size, bool big_endian>
1541std::string
54674d38 1542Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
89fc3421
CC
1543{
1544 gold_unreachable();
1545 return std::string();
1546}
1547
1548// Return a view of the contents of a section. Not used for plugin objects.
1549
1550template<int size, bool big_endian>
c1027032
CC
1551const unsigned char*
1552Sized_pluginobj<size, big_endian>::do_section_contents(
1553 unsigned int,
1554 section_size_type*,
1555 bool)
89fc3421 1556{
89fc3421 1557 gold_unreachable();
c1027032 1558 return NULL;
89fc3421
CC
1559}
1560
1561// Return section flags. Not used for plugin objects.
1562
1563template<int size, bool big_endian>
1564uint64_t
1565Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1566{
1567 gold_unreachable();
1568 return 0;
1569}
1570
ef15dade
ST
1571// Return section entsize. Not used for plugin objects.
1572
1573template<int size, bool big_endian>
1574uint64_t
1575Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1576{
1577 gold_unreachable();
1578 return 0;
1579}
1580
89fc3421
CC
1581// Return section address. Not used for plugin objects.
1582
1583template<int size, bool big_endian>
1584uint64_t
1585Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1586{
1587 gold_unreachable();
1588 return 0;
1589}
1590
1591// Return section type. Not used for plugin objects.
1592
1593template<int size, bool big_endian>
1594unsigned int
1595Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1596{
1597 gold_unreachable();
1598 return 0;
1599}
1600
1601// Return the section link field. Not used for plugin objects.
1602
1603template<int size, bool big_endian>
1604unsigned int
1605Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1606{
1607 gold_unreachable();
1608 return 0;
1609}
1610
1611// Return the section link field. Not used for plugin objects.
1612
1613template<int size, bool big_endian>
1614unsigned int
1615Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1616{
1617 gold_unreachable();
1618 return 0;
1619}
1620
1621// Return the section alignment. Not used for plugin objects.
1622
1623template<int size, bool big_endian>
1624uint64_t
1625Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1626{
1627 gold_unreachable();
1628 return 0;
1629}
1630
1631// Return the Xindex structure to use. Not used for plugin objects.
1632
1633template<int size, bool big_endian>
1634Xindex*
1635Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1636{
1637 gold_unreachable();
1638 return NULL;
1639}
1640
53bbcc1b
CC
1641// Get symbol counts. Don't count plugin objects; the replacement
1642// files will provide the counts.
89fc3421
CC
1643
1644template<int size, bool big_endian>
1645void
53bbcc1b
CC
1646Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1647 const Symbol_table*,
1648 size_t* defined,
1649 size_t* used) const
89fc3421 1650{
53bbcc1b
CC
1651 *defined = 0;
1652 *used = 0;
89fc3421
CC
1653}
1654
dde3f402
ILT
1655// Get symbols. Not used for plugin objects.
1656
1657template<int size, bool big_endian>
1658const Object::Symbols*
1659Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1660{
1661 gold_unreachable();
1662}
1663
5995b570 1664// Class Plugin_finish. This task runs after all replacement files have
78384e8f
CC
1665// been added. For now, it's a placeholder for a possible plugin API
1666// to allow the plugin to release most of its resources. The cleanup
1667// handlers must be called later, because they can remove the temporary
1668// object files that are needed until the end of the link.
89fc3421 1669
5995b570 1670class Plugin_finish : public Task
89fc3421
CC
1671{
1672 public:
5995b570 1673 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
89fc3421
CC
1674 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1675 { }
1676
5995b570 1677 ~Plugin_finish()
89fc3421
CC
1678 {
1679 if (this->this_blocker_ != NULL)
1680 delete this->this_blocker_;
1681 }
1682
1683 Task_token*
1684 is_runnable()
1685 {
1686 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1687 return this->this_blocker_;
1688 return NULL;
1689 }
1690
1691 void
1692 locks(Task_locker* tl)
1693 { tl->add(this, this->next_blocker_); }
1694
1695 void
1696 run(Workqueue*)
483620e8 1697 {
291158a3
CC
1698 Plugin_manager* plugins = parameters->options().plugins();
1699 gold_assert(plugins != NULL);
78384e8f 1700 // We could call early cleanup handlers here.
291158a3
CC
1701 if (plugins->recorder())
1702 plugins->recorder()->finish();
483620e8 1703 }
89fc3421
CC
1704
1705 std::string
1706 get_name() const
5995b570 1707 { return "Plugin_finish"; }
89fc3421
CC
1708
1709 private:
1710 Task_token* this_blocker_;
1711 Task_token* next_blocker_;
1712};
1713
1714// Class Plugin_hook.
1715
1716Plugin_hook::~Plugin_hook()
1717{
1718}
1719
1720// Return whether a Plugin_hook task is runnable.
1721
1722Task_token*
1723Plugin_hook::is_runnable()
1724{
1725 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1726 return this->this_blocker_;
1727 return NULL;
1728}
1729
1730// Return a Task_locker for a Plugin_hook task. We don't need any
1731// locks here.
1732
1733void
1734Plugin_hook::locks(Task_locker*)
1735{
1736}
1737
89fc3421
CC
1738// Run the "all symbols read" plugin hook.
1739
1740void
5995b570 1741Plugin_hook::run(Workqueue* workqueue)
89fc3421
CC
1742{
1743 gold_assert(this->options_.has_plugins());
a10ae760 1744 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
91ff43fe
RÁE
1745 if (start_sym != NULL)
1746 start_sym->set_in_real_elf();
1747
89fc3421 1748 this->options_.plugins()->all_symbols_read(workqueue,
0f7c0701 1749 this,
89fc3421
CC
1750 this->input_objects_,
1751 this->symtab_,
89fc3421
CC
1752 this->dirpath_,
1753 this->mapfile_,
1754 &this->this_blocker_);
5995b570
CC
1755 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1756 this->next_blocker_));
89fc3421
CC
1757}
1758
1759// The C interface routines called by the plugins.
1760
1761#ifdef ENABLE_PLUGINS
1762
1763// Register a claim-file handler.
1764
1765static enum ld_plugin_status
1766register_claim_file(ld_plugin_claim_file_handler handler)
1767{
1768 gold_assert(parameters->options().has_plugins());
1769 parameters->options().plugins()->set_claim_file_handler(handler);
1770 return LDPS_OK;
1771}
1772
1773// Register an all-symbols-read handler.
1774
1775static enum ld_plugin_status
1776register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1777{
1778 gold_assert(parameters->options().has_plugins());
1779 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1780 return LDPS_OK;
1781}
1782
1783// Register a cleanup handler.
1784
1785static enum ld_plugin_status
1786register_cleanup(ld_plugin_cleanup_handler handler)
1787{
1788 gold_assert(parameters->options().has_plugins());
1789 parameters->options().plugins()->set_cleanup_handler(handler);
1790 return LDPS_OK;
1791}
1792
1793// Add symbols from a plugin-claimed input file.
1794
1795static enum ld_plugin_status
ca09d69a 1796add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
89fc3421
CC
1797{
1798 gold_assert(parameters->options().has_plugins());
1799 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1800 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1801 if (obj == NULL)
1802 return LDPS_ERR;
1803 obj->store_incoming_symbols(nsyms, syms);
1804 return LDPS_OK;
1805}
1806
0f7c0701
CC
1807// Get the input file information with an open (possibly re-opened)
1808// file descriptor.
1809
1810static enum ld_plugin_status
ca09d69a 1811get_input_file(const void* handle, struct ld_plugin_input_file* file)
0f7c0701
CC
1812{
1813 gold_assert(parameters->options().has_plugins());
1814 unsigned int obj_index =
1815 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1816 return parameters->options().plugins()->get_input_file(obj_index, file);
1817}
1818
1819// Release the input file.
1820
1821static enum ld_plugin_status
ca09d69a 1822release_input_file(const void* handle)
0f7c0701
CC
1823{
1824 gold_assert(parameters->options().has_plugins());
1825 unsigned int obj_index =
1826 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1827 return parameters->options().plugins()->release_input_file(obj_index);
1828}
1829
9c793f14
RÁE
1830static enum ld_plugin_status
1831get_view(const void *handle, const void **viewp)
1832{
1833 gold_assert(parameters->options().has_plugins());
1834 unsigned int obj_index =
1835 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1836 return parameters->options().plugins()->get_view(obj_index, viewp);
1837}
1838
89fc3421
CC
1839// Get the symbol resolution info for a plugin-claimed input file.
1840
1841static enum ld_plugin_status
ca09d69a 1842get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
89fc3421
CC
1843{
1844 gold_assert(parameters->options().has_plugins());
3c537f7f
PC
1845 Plugin_manager* plugins = parameters->options().plugins();
1846 Object* obj = plugins->object(
e9552f7e 1847 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
89fc3421
CC
1848 if (obj == NULL)
1849 return LDPS_ERR;
e9552f7e
ST
1850 Pluginobj* plugin_obj = obj->pluginobj();
1851 if (plugin_obj == NULL)
1852 return LDPS_ERR;
3c537f7f
PC
1853 Symbol_table* symtab = plugins->symtab();
1854 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
235061c2
CC
1855}
1856
1857// Version 2 of the above. The only difference is that this version
1858// is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1859
1860static enum ld_plugin_status
1861get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1862{
1863 gold_assert(parameters->options().has_plugins());
3c537f7f
PC
1864 Plugin_manager* plugins = parameters->options().plugins();
1865 Object* obj = plugins->object(
235061c2
CC
1866 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1867 if (obj == NULL)
1868 return LDPS_ERR;
1869 Pluginobj* plugin_obj = obj->pluginobj();
1870 if (plugin_obj == NULL)
1871 return LDPS_ERR;
3c537f7f
PC
1872 Symbol_table* symtab = plugins->symtab();
1873 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
89fc3421
CC
1874}
1875
95ecdfbf
ES
1876// Version 3 of the above. The only difference from v2 is that it
1877// returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1878// decided to include.
1879
1880static enum ld_plugin_status
1881get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
1882{
1883 gold_assert(parameters->options().has_plugins());
1884 Plugin_manager* plugins = parameters->options().plugins();
1885 Object* obj = plugins->object(
1886 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1887 if (obj == NULL)
1888 return LDPS_ERR;
1889 Pluginobj* plugin_obj = obj->pluginobj();
1890 if (plugin_obj == NULL)
1891 return LDPS_ERR;
1892 Symbol_table* symtab = plugins->symtab();
1893 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
1894}
1895
89fc3421
CC
1896// Add a new (real) input file generated by a plugin.
1897
1898static enum ld_plugin_status
ca09d69a 1899add_input_file(const char* pathname)
89fc3421
CC
1900{
1901 gold_assert(parameters->options().has_plugins());
e99daf92
ILT
1902 return parameters->options().plugins()->add_input_file(pathname, false);
1903}
1904
1905// Add a new (real) library required by a plugin.
1906
1907static enum ld_plugin_status
ca09d69a 1908add_input_library(const char* pathname)
e99daf92
ILT
1909{
1910 gold_assert(parameters->options().has_plugins());
1911 return parameters->options().plugins()->add_input_file(pathname, true);
89fc3421
CC
1912}
1913
42218b9f
RÁE
1914// Set the extra library path to be used by libraries added via
1915// add_input_library
1916
1917static enum ld_plugin_status
ca09d69a 1918set_extra_library_path(const char* path)
42218b9f
RÁE
1919{
1920 gold_assert(parameters->options().has_plugins());
1921 return parameters->options().plugins()->set_extra_library_path(path);
1922}
1923
89fc3421
CC
1924// Issue a diagnostic message from a plugin.
1925
1926static enum ld_plugin_status
ca09d69a 1927message(int level, const char* format, ...)
89fc3421
CC
1928{
1929 va_list args;
1930 va_start(args, format);
1931
1932 switch (level)
1933 {
1934 case LDPL_INFO:
1935 parameters->errors()->info(format, args);
1936 break;
1937 case LDPL_WARNING:
1938 parameters->errors()->warning(format, args);
1939 break;
1940 case LDPL_ERROR:
1941 default:
1942 parameters->errors()->error(format, args);
1943 break;
1944 case LDPL_FATAL:
1945 parameters->errors()->fatal(format, args);
1946 break;
1947 }
1948
1949 va_end(args);
1950 return LDPS_OK;
1951}
1952
e9552f7e
ST
1953// Get the section count of the object corresponding to the handle. This
1954// plugin interface can only be called in the claim_file handler of the plugin.
1955
1956static enum ld_plugin_status
1957get_input_section_count(const void* handle, unsigned int* count)
1958{
1959 gold_assert(parameters->options().has_plugins());
1960
1961 if (!parameters->options().plugins()->in_claim_file_handler())
1962 return LDPS_ERR;
1963
1964 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1965
1966 if (obj == NULL)
1967 return LDPS_ERR;
1968
1969 *count = obj->shnum();
1970 return LDPS_OK;
1971}
1972
1973// Get the type of the specified section in the object corresponding
1974// to the handle. This plugin interface can only be called in the
1975// claim_file handler of the plugin.
1976
1977static enum ld_plugin_status
1978get_input_section_type(const struct ld_plugin_section section,
1979 unsigned int* type)
1980{
1981 gold_assert(parameters->options().has_plugins());
1982
1983 if (!parameters->options().plugins()->in_claim_file_handler())
1984 return LDPS_ERR;
1985
1986 Object* obj
1987 = parameters->options().plugins()->get_elf_object(section.handle);
1988
1989 if (obj == NULL)
1990 return LDPS_BAD_HANDLE;
1991
1992 *type = obj->section_type(section.shndx);
1993 return LDPS_OK;
1994}
1995
1996// Get the name of the specified section in the object corresponding
1997// to the handle. This plugin interface can only be called in the
1998// claim_file handler of the plugin.
1999
2000static enum ld_plugin_status
2001get_input_section_name(const struct ld_plugin_section section,
2002 char** section_name_ptr)
2003{
2004 gold_assert(parameters->options().has_plugins());
2005
2006 if (!parameters->options().plugins()->in_claim_file_handler())
2007 return LDPS_ERR;
2008
2009 Object* obj
2010 = parameters->options().plugins()->get_elf_object(section.handle);
2011
2012 if (obj == NULL)
2013 return LDPS_BAD_HANDLE;
2014
2015 // Check if the object is locked before getting the section name.
2016 gold_assert(obj->is_locked());
2017
2018 const std::string section_name = obj->section_name(section.shndx);
2019 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
2020 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
2021 return LDPS_OK;
2022}
2023
2024// Get the contents of the specified section in the object corresponding
2025// to the handle. This plugin interface can only be called in the
2026// claim_file handler of the plugin.
2027
2028static enum ld_plugin_status
2029get_input_section_contents(const struct ld_plugin_section section,
2030 const unsigned char** section_contents_ptr,
2031 size_t* len)
2032{
2033 gold_assert(parameters->options().has_plugins());
2034
2035 if (!parameters->options().plugins()->in_claim_file_handler())
2036 return LDPS_ERR;
2037
2038 Object* obj
2039 = parameters->options().plugins()->get_elf_object(section.handle);
2040
2041 if (obj == NULL)
2042 return LDPS_BAD_HANDLE;
2043
2044 // Check if the object is locked before getting the section contents.
2045 gold_assert(obj->is_locked());
2046
2047 section_size_type plen;
2048 *section_contents_ptr
2049 = obj->section_contents(section.shndx, &plen, false);
2050 *len = plen;
2051 return LDPS_OK;
2052}
2053
82838bd6
CC
2054// Get the alignment of the specified section in the object corresponding
2055// to the handle. This plugin interface can only be called in the
2056// claim_file handler of the plugin.
2057
2058static enum ld_plugin_status
2059get_input_section_alignment(const struct ld_plugin_section section,
2060 unsigned int* addralign)
2061{
2062 gold_assert(parameters->options().has_plugins());
2063
2064 if (!parameters->options().plugins()->in_claim_file_handler())
2065 return LDPS_ERR;
2066
2067 Object* obj
2068 = parameters->options().plugins()->get_elf_object(section.handle);
2069
2070 if (obj == NULL)
2071 return LDPS_BAD_HANDLE;
2072
2073 *addralign = obj->section_addralign(section.shndx);
2074 return LDPS_OK;
2075}
2076
2077// Get the size of the specified section in the object corresponding
2078// to the handle. This plugin interface can only be called in the
2079// claim_file handler of the plugin.
2080
2081static enum ld_plugin_status
2082get_input_section_size(const struct ld_plugin_section section,
2083 uint64_t* secsize)
2084{
2085 gold_assert(parameters->options().has_plugins());
2086
2087 if (!parameters->options().plugins()->in_claim_file_handler())
2088 return LDPS_ERR;
2089
2090 Object* obj
2091 = parameters->options().plugins()->get_elf_object(section.handle);
2092
2093 if (obj == NULL)
2094 return LDPS_BAD_HANDLE;
2095
2096 *secsize = obj->section_size(section.shndx);
2097 return LDPS_OK;
2098}
2099
0b65c07b
ST
2100static enum ld_plugin_status
2101get_wrap_symbols(uint64_t *count, const char ***wrap_symbols)
2102{
2103 gold_assert(parameters->options().has_plugins());
2104 *count = parameters->options().wrap_size();
2105
2106 if (*count == 0)
2107 return LDPS_OK;
2108
2109 *wrap_symbols = new const char *[*count];
2110 int i = 0;
2111 for (options::String_set::const_iterator
2112 it = parameters->options().wrap_begin();
2113 it != parameters->options().wrap_end(); ++it, ++i) {
2114 (*wrap_symbols)[i] = it->c_str();
2115 }
2116 return LDPS_OK;
2117}
2118
82838bd6 2119
e9552f7e
ST
2120// Specify the ordering of sections in the final layout. The sections are
2121// specified as (handle,shndx) pairs in the two arrays in the order in
2122// which they should appear in the final layout.
2123
2124static enum ld_plugin_status
f0558624 2125update_section_order(const struct ld_plugin_section* section_list,
e9552f7e
ST
2126 unsigned int num_sections)
2127{
2128 gold_assert(parameters->options().has_plugins());
2129
2130 if (num_sections == 0)
2131 return LDPS_OK;
2132
2133 if (section_list == NULL)
2134 return LDPS_ERR;
2135
f0558624
ST
2136 Layout* layout = parameters->options().plugins()->layout();
2137 gold_assert (layout != NULL);
e9552f7e 2138
f0558624
ST
2139 std::map<Section_id, unsigned int>* order_map
2140 = layout->get_section_order_map();
2141
2142 /* Store the mapping from Section_id to section position in layout's
2143 order_map to consult after output sections are added. */
e9552f7e
ST
2144 for (unsigned int i = 0; i < num_sections; ++i)
2145 {
2146 Object* obj = parameters->options().plugins()->get_elf_object(
2147 section_list[i].handle);
efc6fa12 2148 if (obj == NULL || obj->is_dynamic())
e9552f7e
ST
2149 return LDPS_BAD_HANDLE;
2150 unsigned int shndx = section_list[i].shndx;
efc6fa12 2151 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
f0558624 2152 (*order_map)[secn_id] = i + 1;
e9552f7e
ST
2153 }
2154
e9552f7e
ST
2155 return LDPS_OK;
2156}
2157
2158// Let the linker know that the sections could be reordered.
2159
2160static enum ld_plugin_status
2161allow_section_ordering()
2162{
2163 gold_assert(parameters->options().has_plugins());
2164 Layout* layout = parameters->options().plugins()->layout();
2165 layout->set_section_ordering_specified();
2166 return LDPS_OK;
2167}
2168
16164a6b
ST
2169// Let the linker know that a subset of sections could be mapped
2170// to a unique segment.
2171
2172static enum ld_plugin_status
2173allow_unique_segment_for_sections()
2174{
2175 gold_assert(parameters->options().has_plugins());
2176 Layout* layout = parameters->options().plugins()->layout();
2177 layout->set_unique_segment_for_sections_specified();
2178 return LDPS_OK;
2179}
2180
2181// This function should map the list of sections specified in the
2182// SECTION_LIST to a unique segment. ELF segments do not have names
2183// and the NAME is used to identify Output Section which should contain
2184// the list of sections. This Output Section will then be mapped to
2185// a unique segment. FLAGS is used to specify if any additional segment
2186// flags need to be set. For instance, a specific segment flag can be
2187// set to identify this segment. Unsetting segment flags is not possible.
2188// ALIGN specifies the alignment of the segment.
2189
2190static enum ld_plugin_status
2191unique_segment_for_sections(const char* segment_name,
2192 uint64_t flags,
2193 uint64_t align,
2194 const struct ld_plugin_section* section_list,
2195 unsigned int num_sections)
2196{
2197 gold_assert(parameters->options().has_plugins());
2198
2199 if (num_sections == 0)
2200 return LDPS_OK;
2201
2202 if (section_list == NULL)
2203 return LDPS_ERR;
2204
2205 Layout* layout = parameters->options().plugins()->layout();
2206 gold_assert (layout != NULL);
2207
2208 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
2209 s->name = segment_name;
2210 s->flags = flags;
2211 s->align = align;
2212
2213 for (unsigned int i = 0; i < num_sections; ++i)
2214 {
2215 Object* obj = parameters->options().plugins()->get_elf_object(
2216 section_list[i].handle);
efc6fa12 2217 if (obj == NULL || obj->is_dynamic())
16164a6b
ST
2218 return LDPS_BAD_HANDLE;
2219 unsigned int shndx = section_list[i].shndx;
efc6fa12 2220 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
16164a6b
ST
2221 layout->insert_section_segment_map(secn_id, s);
2222 }
2223
2224 return LDPS_OK;
2225}
2226
c4e64843
SC
2227// Register a new_input handler.
2228
2229static enum ld_plugin_status
2230register_new_input(ld_plugin_new_input_handler handler)
2231{
2232 gold_assert(parameters->options().has_plugins());
2233 parameters->options().plugins()->set_new_input_handler(handler);
2234 return LDPS_OK;
2235}
2236
89fc3421
CC
2237#endif // ENABLE_PLUGINS
2238
2239// Allocate a Pluginobj object of the appropriate size and endianness.
2240
2241static Pluginobj*
291158a3
CC
2242make_sized_plugin_object(const std::string& filename,
2243 Input_file* input_file, off_t offset, off_t filesize)
89fc3421 2244{
89fc3421
CC
2245 Pluginobj* obj = NULL;
2246
029ba973
ILT
2247 parameters_force_valid_target();
2248 const Target& target(parameters->target());
89fc3421 2249
029ba973 2250 if (target.get_size() == 32)
89fc3421 2251 {
029ba973 2252 if (target.is_big_endian())
92f03fcb 2253#ifdef HAVE_TARGET_32_BIG
291158a3
CC
2254 obj = new Sized_pluginobj<32, true>(filename, input_file,
2255 offset, filesize);
92f03fcb
CC
2256#else
2257 gold_error(_("%s: not configured to support "
2258 "32-bit big-endian object"),
291158a3 2259 filename.c_str());
89fc3421 2260#endif
89fc3421 2261 else
92f03fcb 2262#ifdef HAVE_TARGET_32_LITTLE
291158a3
CC
2263 obj = new Sized_pluginobj<32, false>(filename, input_file,
2264 offset, filesize);
92f03fcb
CC
2265#else
2266 gold_error(_("%s: not configured to support "
2267 "32-bit little-endian object"),
291158a3 2268 filename.c_str());
89fc3421
CC
2269#endif
2270 }
029ba973 2271 else if (target.get_size() == 64)
89fc3421 2272 {
029ba973 2273 if (target.is_big_endian())
92f03fcb 2274#ifdef HAVE_TARGET_64_BIG
291158a3
CC
2275 obj = new Sized_pluginobj<64, true>(filename, input_file,
2276 offset, filesize);
92f03fcb
CC
2277#else
2278 gold_error(_("%s: not configured to support "
2279 "64-bit big-endian object"),
291158a3 2280 filename.c_str());
89fc3421 2281#endif
89fc3421 2282 else
92f03fcb 2283#ifdef HAVE_TARGET_64_LITTLE
291158a3
CC
2284 obj = new Sized_pluginobj<64, false>(filename, input_file,
2285 offset, filesize);
92f03fcb
CC
2286#else
2287 gold_error(_("%s: not configured to support "
2288 "64-bit little-endian object"),
291158a3 2289 filename.c_str());
89fc3421
CC
2290#endif
2291 }
2292
2293 gold_assert(obj != NULL);
89fc3421
CC
2294 return obj;
2295}
2296
2297} // End namespace gold.
This page took 0.520005 seconds and 4 git commands to generate.