Add --debug=plugin option to record plugin actions.
[external/binutils.git] / gold / plugin.cc
1 // plugin.cc -- plugin manager for gold      -*- C++ -*-
2
3 // Copyright (C) 2008-2018 Free Software Foundation, Inc.
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
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstdio>
27 #include <cstdarg>
28 #include <cstring>
29 #include <string>
30 #include <vector>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include "libiberty.h"
34
35 #ifdef ENABLE_PLUGINS
36 #ifdef HAVE_DLFCN_H
37 #include <dlfcn.h>
38 #elif defined (HAVE_WINDOWS_H)
39 #include <windows.h>
40 #else
41 #error Unknown how to handle dynamic-load-libraries.
42 #endif
43
44 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
45
46 #define RTLD_NOW 0      /* Dummy value.  */
47 static void *
48 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
49 {
50   return LoadLibrary(file);
51 }
52
53 static void *
54 dlsym(void *handle, const char *name)
55 {
56   return reinterpret_cast<void *>(
57      GetProcAddress(static_cast<HMODULE>(handle),name));
58 }
59
60 static const char *
61 dlerror(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
69 #include "parameters.h"
70 #include "debug.h"
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"
79 #include "descriptors.h"
80 #include "elfcpp.h"
81
82 namespace gold
83 {
84
85 #ifdef ENABLE_PLUGINS
86
87 // The linker's exported interfaces.
88
89 extern "C"
90 {
91
92 static enum ld_plugin_status
93 register_claim_file(ld_plugin_claim_file_handler handler);
94
95 static enum ld_plugin_status
96 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
97
98 static enum ld_plugin_status
99 register_cleanup(ld_plugin_cleanup_handler handler);
100
101 static enum ld_plugin_status
102 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
103
104 static enum ld_plugin_status
105 get_input_file(const void *handle, struct ld_plugin_input_file *file);
106
107 static enum ld_plugin_status
108 get_view(const void *handle, const void **viewp);
109
110 static enum ld_plugin_status
111 release_input_file(const void *handle);
112
113 static enum ld_plugin_status
114 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
115
116 static enum ld_plugin_status
117 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
118
119 static enum ld_plugin_status
120 get_symbols_v3(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
121
122 static enum ld_plugin_status
123 add_input_file(const char *pathname);
124
125 static enum ld_plugin_status
126 add_input_library(const char *pathname);
127
128 static enum ld_plugin_status
129 set_extra_library_path(const char *path);
130
131 static enum ld_plugin_status
132 message(int level, const char *format, ...);
133
134 static enum ld_plugin_status
135 get_input_section_count(const void* handle, unsigned int* count);
136
137 static enum ld_plugin_status
138 get_input_section_type(const struct ld_plugin_section section,
139                        unsigned int* type);
140
141 static enum ld_plugin_status
142 get_input_section_name(const struct ld_plugin_section section,
143                        char** section_name_ptr);
144
145 static enum ld_plugin_status
146 get_input_section_contents(const struct ld_plugin_section section,
147                            const unsigned char** section_contents,
148                            size_t* len);
149
150 static enum ld_plugin_status
151 update_section_order(const struct ld_plugin_section *section_list,
152                      unsigned int num_sections);
153
154 static enum ld_plugin_status
155 allow_section_ordering();
156
157 static enum ld_plugin_status
158 allow_unique_segment_for_sections();
159
160 static enum ld_plugin_status
161 unique_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);
166
167 static enum ld_plugin_status
168 get_input_section_alignment(const struct ld_plugin_section section,
169                             unsigned int* addralign);
170
171 static enum ld_plugin_status
172 get_input_section_size(const struct ld_plugin_section section,
173                        uint64_t* secsize);
174
175 static enum ld_plugin_status
176 register_new_input(ld_plugin_new_input_handler handler);
177
178 static enum ld_plugin_status
179 get_wrap_symbols(uint64_t *num_symbols, const char ***wrap_symbol_list);
180
181 };
182
183 #endif // ENABLE_PLUGINS
184
185 static Pluginobj* make_sized_plugin_object(const std::string& filename,
186                                            Input_file* input_file,
187                                            off_t offset, off_t filesize);
188
189 // Plugin methods.
190
191 // Load one plugin library.
192
193 void
194 Plugin::load()
195 {
196 #ifdef ENABLE_PLUGINS
197   // Load the plugin library.
198   // FIXME: Look for the library in standard locations.
199   this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
200   if (this->handle_ == NULL)
201     {
202       gold_error(_("%s: could not load plugin library: %s"),
203                  this->filename_.c_str(), dlerror());
204       return;
205     }
206
207   // Find the plugin's onload entry point.
208   void* ptr = dlsym(this->handle_, "onload");
209   if (ptr == NULL)
210     {
211       gold_error(_("%s: could not find onload entry point"),
212                  this->filename_.c_str());
213       return;
214     }
215   ld_plugin_onload onload;
216   gold_assert(sizeof(onload) == sizeof(ptr));
217   memcpy(&onload, &ptr, sizeof(ptr));
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.
226   const int tv_fixed_size = 31;
227
228   int tv_size = this->args_.size() + tv_fixed_size;
229   ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
230
231   // Put LDPT_MESSAGE at the front of the list so the plugin can use it
232   // while processing subsequent entries.
233   int i = 0;
234   tv[i].tv_tag = LDPT_MESSAGE;
235   tv[i].tv_u.tv_message = message;
236
237   ++i;
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;
251   else if (parameters->options().pie())
252     tv[i].tv_u.tv_val = LDPO_PIE;
253   else
254     tv[i].tv_u.tv_val = LDPO_EXEC;
255
256   ++i;
257   tv[i].tv_tag = LDPT_OUTPUT_NAME;
258   tv[i].tv_u.tv_string = parameters->options().output();
259
260   for (unsigned int j = 0; j < this->args_.size(); ++j)
261     {
262       ++i;
263       tv[i].tv_tag = LDPT_OPTION;
264       tv[i].tv_u.tv_string = this->args_[j].c_str();
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
283   ++i;
284   tv[i].tv_tag = LDPT_GET_INPUT_FILE;
285   tv[i].tv_u.tv_get_input_file = get_input_file;
286
287   ++i;
288   tv[i].tv_tag = LDPT_GET_VIEW;
289   tv[i].tv_u.tv_get_view = get_view;
290
291   ++i;
292   tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
293   tv[i].tv_u.tv_release_input_file = release_input_file;
294
295   ++i;
296   tv[i].tv_tag = LDPT_GET_SYMBOLS;
297   tv[i].tv_u.tv_get_symbols = get_symbols;
298
299   ++i;
300   tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
301   tv[i].tv_u.tv_get_symbols = get_symbols_v2;
302
303   ++i;
304   tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
305   tv[i].tv_u.tv_get_symbols = get_symbols_v3;
306
307   ++i;
308   tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
309   tv[i].tv_u.tv_add_input_file = add_input_file;
310
311   ++i;
312   tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
313   tv[i].tv_u.tv_add_input_library = add_input_library;
314
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
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
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
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
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
364   ++i;
365   tv[i].tv_tag = LDPT_GET_WRAP_SYMBOLS;
366   tv[i].tv_u.tv_get_wrap_symbols = get_wrap_symbols;
367
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
377   delete[] tv;
378 #endif // ENABLE_PLUGINS
379 }
380
381 // Call the plugin claim-file handler.
382
383 inline bool
384 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
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
399 inline void
400 Plugin::all_symbols_read()
401 {
402   if (this->all_symbols_read_handler_ != NULL)
403     (*this->all_symbols_read_handler_)();
404 }
405
406 // Call the new_input handler.
407
408 inline void
409 Plugin::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
415 // Call the cleanup handler.
416
417 inline void
418 Plugin::cleanup()
419 {
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     }
428 }
429
430 // This task is used to rescan archives as needed.
431
432 class 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
469 // Plugin_recorder logs plugin actions and saves intermediate files
470 // for later replay.
471
472 class 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
505 bool
506 Plugin_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
533 void
534 Plugin_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
546 void
547 Plugin_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
560 static bool
561 link_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
589 void
590 Plugin_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
612 void
613 Plugin_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
678 // Plugin_manager methods.
679
680 Plugin_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();
692   delete this->lock_;
693   delete this->recorder_;
694 }
695
696 // Load all plugin libraries.
697
698 void
699 Plugin_manager::load_plugins(Layout* layout)
700 {
701   this->layout_ = layout;
702
703   if (is_debugging_enabled(DEBUG_PLUGIN))
704     {
705       this->recorder_ = new Plugin_recorder();
706       this->recorder_->init();
707     }
708
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
717 Pluginobj*
718 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
719                            off_t filesize, Object* elf_object)
720 {
721   bool lock_initialized = this->initialize_lock_.initialize();
722
723   gold_assert(lock_initialized);
724   Hold_lock hl(*this->lock_);
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);
733   if (elf_object != NULL)
734     this->objects_.push_back(elf_object);
735   this->in_claim_file_handler_ = true;
736
737   for (this->current_ = this->plugins_.begin();
738        this->current_ != this->plugins_.end();
739        ++this->current_)
740     {
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_)
744         {
745           if ((*this->current_)->claim_file(&this->plugin_input_file_))
746             {
747               this->any_claimed_ = true;
748               this->in_claim_file_handler_ = false;
749
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
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_);
773         }
774     }
775
776   this->in_claim_file_handler_ = false;
777
778   if (this->recorder_ != NULL)
779     this->recorder_->unclaimed_file(input_file->filename(), offset, filesize);
780
781   return NULL;
782 }
783
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
789 void
790 Plugin_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
800 void
801 Plugin_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
809 // Call the all-symbols-read handlers.
810
811 void
812 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
813                                  Input_objects* input_objects,
814                                  Symbol_table* symtab,
815                                  Dirsearch* dirpath, Mapfile* mapfile,
816                                  Task_token** last_blocker)
817 {
818   this->in_replacement_phase_ = true;
819   this->workqueue_ = workqueue;
820   this->task_ = task;
821   this->input_objects_ = input_objects;
822   this->symtab_ = symtab;
823   this->dirpath_ = dirpath;
824   this->mapfile_ = mapfile;
825   this->this_blocker_ = NULL;
826
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
832   for (this->current_ = this->plugins_.begin();
833        this->current_ != this->plugins_.end();
834        ++this->current_)
835     (*this->current_)->all_symbols_read();
836
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
845   *last_blocker = this->this_blocker_;
846 }
847
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
852 void
853 Plugin_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
863 void
864 Plugin_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
958 bool
959 Plugin_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
977 // Layout deferred objects.
978
979 void
980 Plugin_manager::layout_deferred_objects()
981 {
982   Deferred_layout_list::iterator obj;
983
984   for (obj = this->deferred_layout_objects_.begin();
985        obj != this->deferred_layout_objects_.end();
986        ++obj)
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     }
995 }
996
997 // Call the cleanup handlers.
998
999 void
1000 Plugin_manager::cleanup()
1001 {
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
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
1019 Pluginobj*
1020 Plugin_manager::make_plugin_object(unsigned int handle)
1021 {
1022   // Make sure we aren't asked to make an object for the same handle twice.
1023   if (this->objects_.size() != handle
1024       && this->objects_[handle]->pluginobj() != NULL)
1025     return NULL;
1026
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_,
1040                                             this->plugin_input_file_.offset,
1041                                             this->plugin_input_file_.filesize);
1042
1043
1044
1045   this->objects_.push_back(obj);
1046   return obj;
1047 }
1048
1049 // Get the input file information with an open (possibly re-opened)
1050 // file descriptor.
1051
1052 ld_plugin_status
1053 Plugin_manager::get_input_file(unsigned int handle,
1054                                struct ld_plugin_input_file* file)
1055 {
1056   Pluginobj* obj = this->object(handle)->pluginobj();
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
1071 ld_plugin_status
1072 Plugin_manager::release_input_file(unsigned int handle)
1073 {
1074   if (this->object(handle) == NULL)
1075     return LDPS_BAD_HANDLE;
1076
1077   Pluginobj* obj = this->object(handle)->pluginobj();
1078
1079   if (obj == NULL)
1080     return LDPS_BAD_HANDLE;
1081
1082   obj->unlock(this->task_);
1083   return LDPS_OK;
1084 }
1085
1086 // Get the elf object corresponding to the handle. Return NULL if we
1087 // found a Pluginobj instead.
1088
1089 Object*
1090 Plugin_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
1103 ld_plugin_status
1104 Plugin_manager::get_view(unsigned int handle, const void **viewp)
1105 {
1106   off_t offset;
1107   size_t filesize;
1108   Input_file *input_file;
1109   if (this->in_claim_file_handler_)
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.
1120       if (this->object(handle) == NULL)
1121         return LDPS_BAD_HANDLE;
1122       Pluginobj* obj = this->object(handle)->pluginobj();
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
1134 // Add a new library path.
1135
1136 ld_plugin_status
1137 Plugin_manager::set_extra_library_path(const char* path)
1138 {
1139   this->extra_search_path_ = std::string(path);
1140   return LDPS_OK;
1141 }
1142
1143 // Add a new input file.
1144
1145 ld_plugin_status
1146 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
1147 {
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),
1152                            (is_lib
1153                             ? this->extra_search_path_.c_str()
1154                             : ""),
1155                            false,
1156                            this->options_);
1157   Input_argument* input_argument = new Input_argument(file);
1158   Task_token* next_blocker = new Task_token(true);
1159   next_blocker->add_blocker();
1160   if (parameters->incremental())
1161     gold_error(_("input files added by plug-ins in --incremental mode not "
1162                  "supported yet"));
1163
1164   if (this->recorder_ != NULL)
1165     this->recorder_->replacement_file(pathname, is_lib);
1166
1167   this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
1168                                                 this->symtab_,
1169                                                 this->layout_,
1170                                                 this->dirpath_,
1171                                                 0,
1172                                                 this->mapfile_,
1173                                                 input_argument,
1174                                                 NULL,
1175                                                 NULL,
1176                                                 this->this_blocker_,
1177                                                 next_blocker));
1178   this->this_blocker_ = next_blocker;
1179   this->any_added_ = true;
1180   return LDPS_OK;
1181 }
1182
1183 // Class Pluginobj.
1184
1185 Pluginobj::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_()
1189 {
1190 }
1191
1192 // Return TRUE if a defined symbol is referenced from outside the
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".
1196
1197 static inline bool
1198 is_referenced_from_outside(Symbol* lsym)
1199 {
1200   if (lsym->in_real_elf())
1201     return true;
1202   if (parameters->options().relocatable())
1203     return true;
1204   if (parameters->options().is_undefined(lsym->name()))
1205     return true;
1206   return false;
1207 }
1208
1209 // Return TRUE if a defined symbol might be reachable from outside the
1210 // load module.
1211
1212 static inline bool
1213 is_visible_from_outside(Symbol* lsym)
1214 {
1215   if (lsym->in_dyn())
1216     return true;
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()))
1220     return lsym->is_externally_visible();
1221   return false;
1222 }
1223
1224 // Get symbol resolution info.
1225
1226 ld_plugin_status
1227 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
1228                                       int nsyms,
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
1240   if (nsyms > this->nsyms_)
1241     return LDPS_NO_SYMS;
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.
1247       gold_assert(this->symbols_.size() == 0);
1248       for (int i = 0; i < nsyms; i++)
1249         syms[i].resolution = LDPR_PREEMPTED_REG;
1250       return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
1251     }
1252
1253   Plugin_manager* plugins = parameters->options().plugins();
1254   for (int i = 0; i < nsyms; i++)
1255     {
1256       ld_plugin_symbol* isym = &syms[i];
1257       Symbol* lsym = this->symbols_[i];
1258       if (lsym->is_forwarder())
1259         lsym = symtab->resolve_forwards(lsym);
1260       ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
1261
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         }
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;
1279           else if (lsym->object()->pluginobj() == this)
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             }
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))
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             }
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
1322 bool
1323 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
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)
1331     ins.first->second = layout->find_or_add_kept_section(comdat_key,
1332                                                          NULL, 0, true,
1333                                                          true, NULL);
1334
1335   return ins.first->second;
1336 }
1337
1338 // Class Sized_pluginobj.
1339
1340 template<int size, bool big_endian>
1341 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
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)
1347 {
1348 }
1349
1350 // Read the symbols.  Not used for plugin objects.
1351
1352 template<int size, bool big_endian>
1353 void
1354 Sized_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
1361 template<int size, bool big_endian>
1362 void
1363 Sized_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
1371 template<int size, bool big_endian>
1372 void
1373 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1374                                                   Read_symbols_data*,
1375                                                   Layout* layout)
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
1382   Plugin_recorder* recorder = parameters->options().plugins()->recorder();
1383   if (recorder != NULL)
1384     recorder->record_symbols(this, this->nsyms_, this->syms_);
1385
1386   this->symbols_.resize(this->nsyms_);
1387
1388   for (int i = 0; i < this->nsyms_; ++i)
1389     {
1390       const struct ld_plugin_symbol* isym = &this->syms_[i];
1391       const char* name = isym->name;
1392       const char* ver = isym->version;
1393       elfcpp::Elf_Half shndx;
1394       elfcpp::STB bind;
1395       elfcpp::STV vis;
1396
1397       if (name != NULL && name[0] == '\0')
1398         name = NULL;
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:
1435           vis = elfcpp::STV_PROTECTED;
1436           break;
1437         case LDPV_INTERNAL:
1438           vis = elfcpp::STV_INTERNAL;
1439           break;
1440         case LDPV_HIDDEN:
1441           vis = elfcpp::STV_HIDDEN;
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'
1451           && !this->include_comdat_group(isym->comdat_key, layout))
1452         shndx = elfcpp::SHN_UNDEF;
1453
1454       osym.put_st_name(0);
1455       osym.put_st_value(0);
1456       osym.put_st_size(0);
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] =
1462         symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1463     }
1464 }
1465
1466 template<int size, bool big_endian>
1467 Archive::Should_include
1468 Sized_pluginobj<size, big_endian>::do_should_include_member(
1469     Symbol_table* symtab,
1470     Layout* layout,
1471     Read_symbols_data*,
1472     std::string* why)
1473 {
1474   char* tmpbuf = NULL;
1475   size_t tmpbuflen = 0;
1476
1477   for (int i = 0; i < this->nsyms_; ++i)
1478     {
1479       const struct ld_plugin_symbol& sym = this->syms_[i];
1480       if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
1481         continue;
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);
1490       if (t == Archive::SHOULD_INCLUDE_YES)
1491         {
1492           if (tmpbuf != NULL)
1493             free(tmpbuf);
1494           return t;
1495         }
1496     }
1497   if (tmpbuf != NULL)
1498     free(tmpbuf);
1499   return Archive::SHOULD_INCLUDE_UNKNOWN;
1500 }
1501
1502 // Iterate over global symbols, calling a visitor class V for each.
1503
1504 template<int size, bool big_endian>
1505 void
1506 Sized_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
1518 // Iterate over local symbols, calling a visitor class V for each GOT offset
1519 // associated with a local symbol.
1520 template<int size, bool big_endian>
1521 void
1522 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1523     Got_offset_list::Visitor*) const
1524 {
1525   gold_unreachable();
1526 }
1527
1528 // Get the size of a section.  Not used for plugin objects.
1529
1530 template<int size, bool big_endian>
1531 uint64_t
1532 Sized_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
1540 template<int size, bool big_endian>
1541 std::string
1542 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
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
1550 template<int size, bool big_endian>
1551 const unsigned char*
1552 Sized_pluginobj<size, big_endian>::do_section_contents(
1553     unsigned int,
1554     section_size_type*,
1555     bool)
1556 {
1557   gold_unreachable();
1558   return NULL;
1559 }
1560
1561 // Return section flags.  Not used for plugin objects.
1562
1563 template<int size, bool big_endian>
1564 uint64_t
1565 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1566 {
1567   gold_unreachable();
1568   return 0;
1569 }
1570
1571 // Return section entsize.  Not used for plugin objects.
1572
1573 template<int size, bool big_endian>
1574 uint64_t
1575 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1576 {
1577   gold_unreachable();
1578   return 0;
1579 }
1580
1581 // Return section address.  Not used for plugin objects.
1582
1583 template<int size, bool big_endian>
1584 uint64_t
1585 Sized_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
1593 template<int size, bool big_endian>
1594 unsigned int
1595 Sized_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
1603 template<int size, bool big_endian>
1604 unsigned int
1605 Sized_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
1613 template<int size, bool big_endian>
1614 unsigned int
1615 Sized_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
1623 template<int size, bool big_endian>
1624 uint64_t
1625 Sized_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
1633 template<int size, bool big_endian>
1634 Xindex*
1635 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1636 {
1637   gold_unreachable();
1638   return NULL;
1639 }
1640
1641 // Get symbol counts.  Don't count plugin objects; the replacement
1642 // files will provide the counts.
1643
1644 template<int size, bool big_endian>
1645 void
1646 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1647     const Symbol_table*,
1648     size_t* defined,
1649     size_t* used) const
1650 {
1651   *defined = 0;
1652   *used = 0;
1653 }
1654
1655 // Get symbols.  Not used for plugin objects.
1656
1657 template<int size, bool big_endian>
1658 const Object::Symbols*
1659 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1660 {
1661   gold_unreachable();
1662 }
1663
1664 // Class Plugin_finish.  This task runs after all replacement files have
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.
1669
1670 class Plugin_finish : public Task
1671 {
1672  public:
1673   Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1674     : this_blocker_(this_blocker), next_blocker_(next_blocker)
1675   { }
1676
1677   ~Plugin_finish()
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*)
1697   {
1698     Plugin_manager* plugins = parameters->options().plugins();
1699     gold_assert(plugins != NULL);
1700     // We could call early cleanup handlers here.
1701     if (plugins->recorder())
1702       plugins->recorder()->finish();
1703   }
1704
1705   std::string
1706   get_name() const
1707   { return "Plugin_finish"; }
1708
1709  private:
1710   Task_token* this_blocker_;
1711   Task_token* next_blocker_;
1712 };
1713
1714 // Class Plugin_hook.
1715
1716 Plugin_hook::~Plugin_hook()
1717 {
1718 }
1719
1720 // Return whether a Plugin_hook task is runnable.
1721
1722 Task_token*
1723 Plugin_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
1733 void
1734 Plugin_hook::locks(Task_locker*)
1735 {
1736 }
1737
1738 // Run the "all symbols read" plugin hook.
1739
1740 void
1741 Plugin_hook::run(Workqueue* workqueue)
1742 {
1743   gold_assert(this->options_.has_plugins());
1744   Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1745   if (start_sym != NULL)
1746     start_sym->set_in_real_elf();
1747
1748   this->options_.plugins()->all_symbols_read(workqueue,
1749                                              this,
1750                                              this->input_objects_,
1751                                              this->symtab_,
1752                                              this->dirpath_,
1753                                              this->mapfile_,
1754                                              &this->this_blocker_);
1755   workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1756                                           this->next_blocker_));
1757 }
1758
1759 // The C interface routines called by the plugins.
1760
1761 #ifdef ENABLE_PLUGINS
1762
1763 // Register a claim-file handler.
1764
1765 static enum ld_plugin_status
1766 register_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
1775 static enum ld_plugin_status
1776 register_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
1785 static enum ld_plugin_status
1786 register_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
1795 static enum ld_plugin_status
1796 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
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
1807 // Get the input file information with an open (possibly re-opened)
1808 // file descriptor.
1809
1810 static enum ld_plugin_status
1811 get_input_file(const void* handle, struct ld_plugin_input_file* file)
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
1821 static enum ld_plugin_status
1822 release_input_file(const void* handle)
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
1830 static enum ld_plugin_status
1831 get_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
1839 // Get the symbol resolution info for a plugin-claimed input file.
1840
1841 static enum ld_plugin_status
1842 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1843 {
1844   gold_assert(parameters->options().has_plugins());
1845   Plugin_manager* plugins = parameters->options().plugins();
1846   Object* obj = plugins->object(
1847     static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1848   if (obj == NULL)
1849     return LDPS_ERR;
1850   Pluginobj* plugin_obj = obj->pluginobj();
1851   if (plugin_obj == NULL)
1852     return LDPS_ERR;
1853   Symbol_table* symtab = plugins->symtab();
1854   return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
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
1860 static enum ld_plugin_status
1861 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1862 {
1863   gold_assert(parameters->options().has_plugins());
1864   Plugin_manager* plugins = parameters->options().plugins();
1865   Object* obj = plugins->object(
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;
1872   Symbol_table* symtab = plugins->symtab();
1873   return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
1874 }
1875
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
1880 static enum ld_plugin_status
1881 get_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
1896 // Add a new (real) input file generated by a plugin.
1897
1898 static enum ld_plugin_status
1899 add_input_file(const char* pathname)
1900 {
1901   gold_assert(parameters->options().has_plugins());
1902   return parameters->options().plugins()->add_input_file(pathname, false);
1903 }
1904
1905 // Add a new (real) library required by a plugin.
1906
1907 static enum ld_plugin_status
1908 add_input_library(const char* pathname)
1909 {
1910   gold_assert(parameters->options().has_plugins());
1911   return parameters->options().plugins()->add_input_file(pathname, true);
1912 }
1913
1914 // Set the extra library path to be used by libraries added via
1915 // add_input_library
1916
1917 static enum ld_plugin_status
1918 set_extra_library_path(const char* path)
1919 {
1920   gold_assert(parameters->options().has_plugins());
1921   return parameters->options().plugins()->set_extra_library_path(path);
1922 }
1923
1924 // Issue a diagnostic message from a plugin.
1925
1926 static enum ld_plugin_status
1927 message(int level, const char* format, ...)
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
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
1956 static enum ld_plugin_status
1957 get_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
1977 static enum ld_plugin_status
1978 get_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
2000 static enum ld_plugin_status
2001 get_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
2028 static enum ld_plugin_status
2029 get_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
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
2058 static enum ld_plugin_status
2059 get_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
2081 static enum ld_plugin_status
2082 get_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
2100 static enum ld_plugin_status
2101 get_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
2119
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
2124 static enum ld_plugin_status
2125 update_section_order(const struct ld_plugin_section* section_list,
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
2136   Layout* layout = parameters->options().plugins()->layout();
2137   gold_assert (layout != NULL);
2138
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.  */
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);
2148       if (obj == NULL || obj->is_dynamic())
2149         return LDPS_BAD_HANDLE;
2150       unsigned int shndx = section_list[i].shndx;
2151       Section_id secn_id(static_cast<Relobj*>(obj), shndx);
2152       (*order_map)[secn_id] = i + 1;
2153     }
2154
2155   return LDPS_OK;
2156 }
2157
2158 // Let the linker know that the sections could be reordered.
2159
2160 static enum ld_plugin_status
2161 allow_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
2169 // Let the linker know that a subset of sections could be mapped
2170 // to a unique segment.
2171
2172 static enum ld_plugin_status
2173 allow_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
2190 static enum ld_plugin_status
2191 unique_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);
2217       if (obj == NULL || obj->is_dynamic())
2218         return LDPS_BAD_HANDLE;
2219       unsigned int shndx = section_list[i].shndx;
2220       Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
2221       layout->insert_section_segment_map(secn_id, s);
2222     }
2223
2224   return LDPS_OK;
2225 }
2226
2227 // Register a new_input handler.
2228
2229 static enum ld_plugin_status
2230 register_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
2237 #endif // ENABLE_PLUGINS
2238
2239 // Allocate a Pluginobj object of the appropriate size and endianness.
2240
2241 static Pluginobj*
2242 make_sized_plugin_object(const std::string& filename,
2243                          Input_file* input_file, off_t offset, off_t filesize)
2244 {
2245   Pluginobj* obj = NULL;
2246
2247   parameters_force_valid_target();
2248   const Target& target(parameters->target());
2249
2250   if (target.get_size() == 32)
2251     {
2252       if (target.is_big_endian())
2253 #ifdef HAVE_TARGET_32_BIG
2254         obj = new Sized_pluginobj<32, true>(filename, input_file,
2255                                             offset, filesize);
2256 #else
2257         gold_error(_("%s: not configured to support "
2258                      "32-bit big-endian object"),
2259                    filename.c_str());
2260 #endif
2261       else
2262 #ifdef HAVE_TARGET_32_LITTLE
2263         obj = new Sized_pluginobj<32, false>(filename, input_file,
2264                                              offset, filesize);
2265 #else
2266         gold_error(_("%s: not configured to support "
2267                      "32-bit little-endian object"),
2268                    filename.c_str());
2269 #endif
2270     }
2271   else if (target.get_size() == 64)
2272     {
2273       if (target.is_big_endian())
2274 #ifdef HAVE_TARGET_64_BIG
2275         obj = new Sized_pluginobj<64, true>(filename, input_file,
2276                                             offset, filesize);
2277 #else
2278         gold_error(_("%s: not configured to support "
2279                      "64-bit big-endian object"),
2280                    filename.c_str());
2281 #endif
2282       else
2283 #ifdef HAVE_TARGET_64_LITTLE
2284         obj = new Sized_pluginobj<64, false>(filename, input_file,
2285                                              offset, filesize);
2286 #else
2287         gold_error(_("%s: not configured to support "
2288                      "64-bit little-endian object"),
2289                    filename.c_str());
2290 #endif
2291     }
2292
2293   gold_assert(obj != NULL);
2294   return obj;
2295 }
2296
2297 } // End namespace gold.