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