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