OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [plugin.cc] - Blame information for rev 318

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 khays
// plugin.cc -- plugin manager for gold      -*- C++ -*-
2
 
3
// Copyright 2008, 2009, 2010, 2011 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 <cstdio>
26
#include <cstdarg>
27
#include <cstring>
28
#include <string>
29
#include <vector>
30
 
31
#ifdef ENABLE_PLUGINS
32
#include <dlfcn.h>
33
#endif
34
 
35
#include "parameters.h"
36
#include "errors.h"
37
#include "fileread.h"
38
#include "layout.h"
39
#include "options.h"
40
#include "plugin.h"
41
#include "target.h"
42
#include "readsyms.h"
43
#include "symtab.h"
44
#include "elfcpp.h"
45
 
46
namespace gold
47
{
48
 
49
#ifdef ENABLE_PLUGINS
50
 
51
// The linker's exported interfaces.
52
 
53
extern "C"
54
{
55
 
56
static enum ld_plugin_status
57
register_claim_file(ld_plugin_claim_file_handler handler);
58
 
59
static enum ld_plugin_status
60
register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
61
 
62
static enum ld_plugin_status
63
register_cleanup(ld_plugin_cleanup_handler handler);
64
 
65
static enum ld_plugin_status
66
add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
67
 
68
static enum ld_plugin_status
69
get_input_file(const void *handle, struct ld_plugin_input_file *file);
70
 
71
static enum ld_plugin_status
72
get_view(const void *handle, const void **viewp);
73
 
74
static enum ld_plugin_status
75
release_input_file(const void *handle);
76
 
77
static enum ld_plugin_status
78
get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
79
 
80
static enum ld_plugin_status
81 163 khays
get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
82
 
83
static enum ld_plugin_status
84 27 khays
add_input_file(const char *pathname);
85
 
86
static enum ld_plugin_status
87
add_input_library(const char *pathname);
88
 
89
static enum ld_plugin_status
90
set_extra_library_path(const char *path);
91
 
92
static enum ld_plugin_status
93
message(int level, const char *format, ...);
94
 
95 159 khays
static enum ld_plugin_status
96
get_input_section_count(const void* handle, unsigned int* count);
97
 
98
static enum ld_plugin_status
99
get_input_section_type(const struct ld_plugin_section section,
100
                       unsigned int* type);
101
 
102
static enum ld_plugin_status
103
get_input_section_name(const struct ld_plugin_section section,
104
                       char** section_name_ptr);
105
 
106
static enum ld_plugin_status
107
get_input_section_contents(const struct ld_plugin_section section,
108
                           const unsigned char** section_contents,
109
                           size_t* len);
110
 
111
static enum ld_plugin_status
112
update_section_order(const struct ld_plugin_section *section_list,
113
                     unsigned int num_sections);
114
 
115
static enum ld_plugin_status
116
allow_section_ordering();
117
 
118 27 khays
};
119
 
120
#endif // ENABLE_PLUGINS
121
 
122
static Pluginobj* make_sized_plugin_object(Input_file* input_file,
123
                                           off_t offset, off_t filesize);
124
 
125
// Plugin methods.
126
 
127
// Load one plugin library.
128
 
129
void
130
Plugin::load()
131
{
132
#ifdef ENABLE_PLUGINS
133
  // Load the plugin library.
134
  // FIXME: Look for the library in standard locations.
135
  this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
136
  if (this->handle_ == NULL)
137
    {
138
      gold_error(_("%s: could not load plugin library: %s"),
139
                 this->filename_.c_str(), dlerror());
140
      return;
141
    }
142
 
143
  // Find the plugin's onload entry point.
144
  void* ptr = dlsym(this->handle_, "onload");
145
  if (ptr == NULL)
146
    {
147
      gold_error(_("%s: could not find onload entry point"),
148
                 this->filename_.c_str());
149
      return;
150
    }
151
  ld_plugin_onload onload;
152
  gold_assert(sizeof(onload) == sizeof(ptr));
153
  memcpy(&onload, &ptr, sizeof(ptr));
154
 
155
  // Get the linker's version number.
156
  const char* ver = get_version_string();
157
  int major = 0;
158
  int minor = 0;
159
  sscanf(ver, "%d.%d", &major, &minor);
160
 
161
  // Allocate and populate a transfer vector.
162 163 khays
  const int tv_fixed_size = 24;
163 159 khays
 
164 27 khays
  int tv_size = this->args_.size() + tv_fixed_size;
165
  ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
166
 
167
  // Put LDPT_MESSAGE at the front of the list so the plugin can use it
168
  // while processing subsequent entries.
169
  int i = 0;
170
  tv[i].tv_tag = LDPT_MESSAGE;
171
  tv[i].tv_u.tv_message = message;
172
 
173
  ++i;
174
  tv[i].tv_tag = LDPT_API_VERSION;
175
  tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
176
 
177
  ++i;
178
  tv[i].tv_tag = LDPT_GOLD_VERSION;
179
  tv[i].tv_u.tv_val = major * 100 + minor;
180
 
181
  ++i;
182
  tv[i].tv_tag = LDPT_LINKER_OUTPUT;
183
  if (parameters->options().relocatable())
184
    tv[i].tv_u.tv_val = LDPO_REL;
185
  else if (parameters->options().shared())
186
    tv[i].tv_u.tv_val = LDPO_DYN;
187
  else
188
    tv[i].tv_u.tv_val = LDPO_EXEC;
189
 
190
  ++i;
191
  tv[i].tv_tag = LDPT_OUTPUT_NAME;
192
  tv[i].tv_u.tv_string = parameters->options().output();
193
 
194
  for (unsigned int j = 0; j < this->args_.size(); ++j)
195
    {
196
      ++i;
197
      tv[i].tv_tag = LDPT_OPTION;
198
      tv[i].tv_u.tv_string = this->args_[j].c_str();
199
    }
200
 
201
  ++i;
202
  tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
203
  tv[i].tv_u.tv_register_claim_file = register_claim_file;
204
 
205
  ++i;
206
  tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
207
  tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
208
 
209
  ++i;
210
  tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
211
  tv[i].tv_u.tv_register_cleanup = register_cleanup;
212
 
213
  ++i;
214
  tv[i].tv_tag = LDPT_ADD_SYMBOLS;
215
  tv[i].tv_u.tv_add_symbols = add_symbols;
216
 
217
  ++i;
218
  tv[i].tv_tag = LDPT_GET_INPUT_FILE;
219
  tv[i].tv_u.tv_get_input_file = get_input_file;
220
 
221
  ++i;
222
  tv[i].tv_tag = LDPT_GET_VIEW;
223
  tv[i].tv_u.tv_get_view = get_view;
224
 
225
  ++i;
226
  tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
227
  tv[i].tv_u.tv_release_input_file = release_input_file;
228
 
229
  ++i;
230
  tv[i].tv_tag = LDPT_GET_SYMBOLS;
231
  tv[i].tv_u.tv_get_symbols = get_symbols;
232
 
233
  ++i;
234 163 khays
  tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
235
  tv[i].tv_u.tv_get_symbols = get_symbols_v2;
236
 
237
  ++i;
238 27 khays
  tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
239
  tv[i].tv_u.tv_add_input_file = add_input_file;
240
 
241
  ++i;
242
  tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
243
  tv[i].tv_u.tv_add_input_library = add_input_library;
244
 
245
  ++i;
246
  tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
247
  tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
248
 
249
  ++i;
250 159 khays
  tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
251
  tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
252
 
253
  ++i;
254
  tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
255
  tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
256
 
257
  ++i;
258
  tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
259
  tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
260
 
261
  ++i;
262
  tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
263
  tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
264
 
265
  ++i;
266
  tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
267
  tv[i].tv_u.tv_update_section_order = update_section_order;
268
 
269
  ++i;
270
  tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
271
  tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
272
 
273
  ++i;
274 27 khays
  tv[i].tv_tag = LDPT_NULL;
275
  tv[i].tv_u.tv_val = 0;
276
 
277
  gold_assert(i == tv_size - 1);
278
 
279
  // Call the onload entry point.
280
  (*onload)(tv);
281
 
282
  delete[] tv;
283
#endif // ENABLE_PLUGINS
284
}
285
 
286
// Call the plugin claim-file handler.
287
 
288
inline bool
289
Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
290
{
291
  int claimed = 0;
292
 
293
  if (this->claim_file_handler_ != NULL)
294
    {
295
      (*this->claim_file_handler_)(plugin_input_file, &claimed);
296
      if (claimed)
297
        return true;
298
    }
299
  return false;
300
}
301
 
302
// Call the all-symbols-read handler.
303
 
304
inline void
305
Plugin::all_symbols_read()
306
{
307
  if (this->all_symbols_read_handler_ != NULL)
308
    (*this->all_symbols_read_handler_)();
309
}
310
 
311
// Call the cleanup handler.
312
 
313
inline void
314
Plugin::cleanup()
315
{
316
  if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
317
    {
318
      // Set this flag before calling to prevent a recursive plunge
319
      // in the event that a plugin's cleanup handler issues a
320
      // fatal error.
321
      this->cleanup_done_ = true;
322
      (*this->cleanup_handler_)();
323
    }
324
}
325
 
326
// This task is used to rescan archives as needed.
327
 
328
class Plugin_rescan : public Task
329
{
330
 public:
331
  Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
332
    : this_blocker_(this_blocker), next_blocker_(next_blocker)
333
  { }
334
 
335
  ~Plugin_rescan()
336
  {
337
    delete this->this_blocker_;
338
  }
339
 
340
  Task_token*
341
  is_runnable()
342
  {
343
    if (this->this_blocker_->is_blocked())
344
      return this->this_blocker_;
345
    return NULL;
346
  }
347
 
348
  void
349
  locks(Task_locker* tl)
350
  { tl->add(this, this->next_blocker_); }
351
 
352
  void
353
  run(Workqueue*)
354
  { parameters->options().plugins()->rescan(this); }
355
 
356
  std::string
357
  get_name() const
358
  { return "Plugin_rescan"; }
359
 
360
 private:
361
  Task_token* this_blocker_;
362
  Task_token* next_blocker_;
363
};
364
 
365
// Plugin_manager methods.
366
 
367
Plugin_manager::~Plugin_manager()
368
{
369
  for (Plugin_list::iterator p = this->plugins_.begin();
370
       p != this->plugins_.end();
371
       ++p)
372
    delete *p;
373
  this->plugins_.clear();
374
  for (Object_list::iterator obj = this->objects_.begin();
375
       obj != this->objects_.end();
376
       ++obj)
377
    delete *obj;
378
  this->objects_.clear();
379
}
380
 
381
// Load all plugin libraries.
382
 
383
void
384 159 khays
Plugin_manager::load_plugins(Layout* layout)
385 27 khays
{
386 159 khays
  this->layout_ = layout;
387 27 khays
  for (this->current_ = this->plugins_.begin();
388
       this->current_ != this->plugins_.end();
389
       ++this->current_)
390
    (*this->current_)->load();
391
}
392
 
393
// Call the plugin claim-file handlers in turn to see if any claim the file.
394
 
395
Pluginobj*
396
Plugin_manager::claim_file(Input_file* input_file, off_t offset,
397 159 khays
                           off_t filesize, Object* elf_object)
398 27 khays
{
399
  if (this->in_replacement_phase_)
400
    return NULL;
401
 
402
  unsigned int handle = this->objects_.size();
403
  this->input_file_ = input_file;
404
  this->plugin_input_file_.name = input_file->filename().c_str();
405
  this->plugin_input_file_.fd = input_file->file().descriptor();
406
  this->plugin_input_file_.offset = offset;
407
  this->plugin_input_file_.filesize = filesize;
408
  this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
409 159 khays
  if (elf_object != NULL)
410
    this->objects_.push_back(elf_object);
411
  this->in_claim_file_handler_ = true;
412 27 khays
 
413
  for (this->current_ = this->plugins_.begin();
414
       this->current_ != this->plugins_.end();
415
       ++this->current_)
416
    {
417
      if ((*this->current_)->claim_file(&this->plugin_input_file_))
418
        {
419
          this->any_claimed_ = true;
420 159 khays
          this->in_claim_file_handler_ = false;
421 27 khays
 
422 159 khays
          if (this->objects_.size() > handle
423
              && this->objects_[handle]->pluginobj() != NULL)
424
            return this->objects_[handle]->pluginobj();
425 27 khays
 
426
          // If the plugin claimed the file but did not call the
427
          // add_symbols callback, we need to create the Pluginobj now.
428
          Pluginobj* obj = this->make_plugin_object(handle);
429
          return obj;
430
        }
431
    }
432
 
433 159 khays
  this->in_claim_file_handler_ = false;
434 27 khays
  return NULL;
435
}
436
 
437
// Save an archive.  This is used so that a plugin can add a file
438
// which refers to a symbol which was not previously referenced.  In
439
// that case we want to pretend that the symbol was referenced before,
440
// and pull in the archive object.
441
 
442
void
443
Plugin_manager::save_archive(Archive* archive)
444
{
445
  if (this->in_replacement_phase_ || !this->any_claimed_)
446
    delete archive;
447
  else
448
    this->rescannable_.push_back(Rescannable(archive));
449
}
450
 
451
// Save an Input_group.  This is like save_archive.
452
 
453
void
454
Plugin_manager::save_input_group(Input_group* input_group)
455
{
456
  if (this->in_replacement_phase_ || !this->any_claimed_)
457
    delete input_group;
458
  else
459
    this->rescannable_.push_back(Rescannable(input_group));
460
}
461
 
462
// Call the all-symbols-read handlers.
463
 
464
void
465
Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
466
                                 Input_objects* input_objects,
467 159 khays
                                 Symbol_table* symtab,
468 27 khays
                                 Dirsearch* dirpath, Mapfile* mapfile,
469
                                 Task_token** last_blocker)
470
{
471
  this->in_replacement_phase_ = true;
472
  this->workqueue_ = workqueue;
473
  this->task_ = task;
474
  this->input_objects_ = input_objects;
475
  this->symtab_ = symtab;
476
  this->dirpath_ = dirpath;
477
  this->mapfile_ = mapfile;
478
  this->this_blocker_ = NULL;
479
 
480
  for (this->current_ = this->plugins_.begin();
481
       this->current_ != this->plugins_.end();
482
       ++this->current_)
483
    (*this->current_)->all_symbols_read();
484
 
485
  if (this->any_added_)
486
    {
487
      Task_token* next_blocker = new Task_token(true);
488
      next_blocker->add_blocker();
489
      workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
490
      this->this_blocker_ = next_blocker;
491
    }
492
 
493
  *last_blocker = this->this_blocker_;
494
}
495
 
496
// This is called when we see a new undefined symbol.  If we are in
497
// the replacement phase, this means that we may need to rescan some
498
// archives we have previously seen.
499
 
500
void
501
Plugin_manager::new_undefined_symbol(Symbol* sym)
502
{
503
  if (this->in_replacement_phase_)
504
    this->undefined_symbols_.push_back(sym);
505
}
506
 
507
// Rescan archives as needed.  This handles the case where a new
508
// object file added by a plugin has an undefined reference to some
509
// symbol defined in an archive.
510
 
511
void
512
Plugin_manager::rescan(Task* task)
513
{
514
  size_t rescan_pos = 0;
515
  size_t rescan_size = this->rescannable_.size();
516
  while (!this->undefined_symbols_.empty())
517
    {
518
      if (rescan_pos >= rescan_size)
519
        {
520
          this->undefined_symbols_.clear();
521
          return;
522
        }
523
 
524
      Undefined_symbol_list undefs;
525
      undefs.reserve(this->undefined_symbols_.size());
526
      this->undefined_symbols_.swap(undefs);
527
 
528
      size_t min_rescan_pos = rescan_size;
529
 
530
      for (Undefined_symbol_list::const_iterator p = undefs.begin();
531
           p != undefs.end();
532
           ++p)
533
        {
534
          if (!(*p)->is_undefined())
535
            continue;
536
 
537
          this->undefined_symbols_.push_back(*p);
538
 
539
          // Find the first rescan archive which defines this symbol,
540
          // starting at the current rescan position.  The rescan position
541
          // exists so that given -la -lb -lc we don't look for undefined
542
          // symbols in -lb back in -la, but instead get the definition
543
          // from -lc.  Don't bother to look past the current minimum
544
          // rescan position.
545
          for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
546
            {
547
              if (this->rescannable_defines(i, *p))
548
                {
549
                  min_rescan_pos = i;
550
                  break;
551
                }
552
            }
553
        }
554
 
555
      if (min_rescan_pos >= rescan_size)
556
        {
557
          // We didn't find any rescannable archives which define any
558
          // undefined symbols.
559
          return;
560
        }
561
 
562
      const Rescannable& r(this->rescannable_[min_rescan_pos]);
563
      if (r.is_archive)
564
        {
565
          Task_lock_obj<Archive> tl(task, r.u.archive);
566
          r.u.archive->add_symbols(this->symtab_, this->layout_,
567
                                   this->input_objects_, this->mapfile_);
568
        }
569
      else
570
        {
571
          size_t next_saw_undefined = this->symtab_->saw_undefined();
572
          size_t saw_undefined;
573
          do
574
            {
575
              saw_undefined = next_saw_undefined;
576
 
577
              for (Input_group::const_iterator p = r.u.input_group->begin();
578
                   p != r.u.input_group->end();
579
                   ++p)
580
                {
581
                  Task_lock_obj<Archive> tl(task, *p);
582
 
583
                  (*p)->add_symbols(this->symtab_, this->layout_,
584
                                    this->input_objects_, this->mapfile_);
585
                }
586
 
587
              next_saw_undefined = this->symtab_->saw_undefined();
588
            }
589
          while (saw_undefined != next_saw_undefined);
590
        }
591
 
592
      for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
593
        {
594
          if (this->rescannable_[i].is_archive)
595
            delete this->rescannable_[i].u.archive;
596
          else
597
            delete this->rescannable_[i].u.input_group;
598
        }
599
 
600
      rescan_pos = min_rescan_pos + 1;
601
    }
602
}
603
 
604
// Return whether the rescannable at index I defines SYM.
605
 
606
bool
607
Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
608
{
609
  const Rescannable& r(this->rescannable_[i]);
610
  if (r.is_archive)
611
    return r.u.archive->defines_symbol(sym);
612
  else
613
    {
614
      for (Input_group::const_iterator p = r.u.input_group->begin();
615
           p != r.u.input_group->end();
616
           ++p)
617
        {
618
          if ((*p)->defines_symbol(sym))
619
            return true;
620
        }
621
      return false;
622
    }
623
}
624
 
625
// Layout deferred objects.
626
 
627
void
628
Plugin_manager::layout_deferred_objects()
629
{
630
  Deferred_layout_list::iterator obj;
631
 
632
  for (obj = this->deferred_layout_objects_.begin();
633
       obj != this->deferred_layout_objects_.end();
634
       ++obj)
635
    {
636
      // Lock the object so we can read from it.  This is only called
637
      // single-threaded from queue_middle_tasks, so it is OK to lock.
638
      // Unfortunately we have no way to pass in a Task token.
639
      const Task* dummy_task = reinterpret_cast<const Task*>(-1);
640
      Task_lock_obj<Object> tl(dummy_task, *obj);
641
      (*obj)->layout_deferred_sections(this->layout_);
642
    }
643
}
644
 
645
// Call the cleanup handlers.
646
 
647
void
648
Plugin_manager::cleanup()
649
{
650
  for (this->current_ = this->plugins_.begin();
651
       this->current_ != this->plugins_.end();
652
       ++this->current_)
653
    (*this->current_)->cleanup();
654
}
655
 
656
// Make a new Pluginobj object.  This is called when the plugin calls
657
// the add_symbols API.
658
 
659
Pluginobj*
660
Plugin_manager::make_plugin_object(unsigned int handle)
661
{
662
  // Make sure we aren't asked to make an object for the same handle twice.
663 159 khays
  if (this->objects_.size() != handle
664
      && this->objects_[handle]->pluginobj() != NULL)
665 27 khays
    return NULL;
666
 
667
  Pluginobj* obj = make_sized_plugin_object(this->input_file_,
668
                                            this->plugin_input_file_.offset,
669
                                            this->plugin_input_file_.filesize);
670 159 khays
 
671
 
672
  // If the elf object for this file was pushed into the objects_ vector, delete
673
  // it to make room for the Pluginobj as this file is claimed.
674
  if (this->objects_.size() != handle)
675
    this->objects_.pop_back();
676
 
677 27 khays
  this->objects_.push_back(obj);
678
  return obj;
679
}
680
 
681
// Get the input file information with an open (possibly re-opened)
682
// file descriptor.
683
 
684
ld_plugin_status
685
Plugin_manager::get_input_file(unsigned int handle,
686
                               struct ld_plugin_input_file* file)
687
{
688 159 khays
  Pluginobj* obj = this->object(handle)->pluginobj();
689 27 khays
  if (obj == NULL)
690
    return LDPS_BAD_HANDLE;
691
 
692
  obj->lock(this->task_);
693
  file->name = obj->filename().c_str();
694
  file->fd = obj->descriptor();
695
  file->offset = obj->offset();
696
  file->filesize = obj->filesize();
697
  file->handle = reinterpret_cast<void*>(handle);
698
  return LDPS_OK;
699
}
700
 
701
// Release the input file.
702
 
703
ld_plugin_status
704
Plugin_manager::release_input_file(unsigned int handle)
705
{
706 159 khays
  if (this->object(handle) == NULL)
707
    return LDPS_BAD_HANDLE;
708
 
709
  Pluginobj* obj = this->object(handle)->pluginobj();
710
 
711 27 khays
  if (obj == NULL)
712
    return LDPS_BAD_HANDLE;
713
 
714
  obj->unlock(this->task_);
715
  return LDPS_OK;
716
}
717
 
718 159 khays
// Get the elf object corresponding to the handle. Return NULL if we
719
// found a Pluginobj instead.
720
 
721
Object*
722
Plugin_manager::get_elf_object(const void* handle)
723
{
724
  Object* obj = this->object(
725
      static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
726
 
727
  // The object should not be a Pluginobj.
728
  if (obj == NULL
729
      || obj->pluginobj() != NULL)
730
    return NULL;
731
 
732
  return obj;
733
}
734
 
735 27 khays
ld_plugin_status
736
Plugin_manager::get_view(unsigned int handle, const void **viewp)
737
{
738
  off_t offset;
739
  size_t filesize;
740
  Input_file *input_file;
741 159 khays
  if (this->in_claim_file_handler_)
742 27 khays
    {
743
      // We are being called from the claim_file hook.
744
      const struct ld_plugin_input_file &f = this->plugin_input_file_;
745
      offset = f.offset;
746
      filesize = f.filesize;
747
      input_file = this->input_file_;
748
    }
749
  else
750
    {
751
      // An already claimed file.
752 159 khays
      if (this->object(handle) == NULL)
753
        return LDPS_BAD_HANDLE;
754
      Pluginobj* obj = this->object(handle)->pluginobj();
755 27 khays
      if (obj == NULL)
756
        return LDPS_BAD_HANDLE;
757
      offset = obj->offset();
758
      filesize = obj->filesize();
759
      input_file = obj->input_file();
760
    }
761
  *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
762
                                               false);
763
  return LDPS_OK;
764
}
765
 
766
// Add a new library path.
767
 
768
ld_plugin_status
769
Plugin_manager::set_extra_library_path(const char* path)
770
{
771
  this->extra_search_path_ = std::string(path);
772
  return LDPS_OK;
773
}
774
 
775
// Add a new input file.
776
 
777
ld_plugin_status
778
Plugin_manager::add_input_file(const char* pathname, bool is_lib)
779
{
780
  Input_file_argument file(pathname,
781
                           (is_lib
782
                            ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
783
                            : Input_file_argument::INPUT_FILE_TYPE_FILE),
784
                           (is_lib
785
                            ? this->extra_search_path_.c_str()
786
                            : ""),
787
                           false,
788
                           this->options_);
789
  Input_argument* input_argument = new Input_argument(file);
790
  Task_token* next_blocker = new Task_token(true);
791
  next_blocker->add_blocker();
792
  if (parameters->incremental())
793
    gold_error(_("input files added by plug-ins in --incremental mode not "
794
                 "supported yet"));
795
  this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
796
                                                this->symtab_,
797
                                                this->layout_,
798
                                                this->dirpath_,
799
                                                0,
800
                                                this->mapfile_,
801
                                                input_argument,
802
                                                NULL,
803
                                                NULL,
804
                                                this->this_blocker_,
805
                                                next_blocker));
806
  this->this_blocker_ = next_blocker;
807
  this->any_added_ = true;
808
  return LDPS_OK;
809
}
810
 
811
// Class Pluginobj.
812
 
813
Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
814
                     off_t offset, off_t filesize)
815
  : Object(name, input_file, false, offset),
816
    nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
817
{
818
}
819
 
820 163 khays
// Return TRUE if a defined symbol is referenced from outside the
821
// universe of claimed objects.  Only references from relocatable,
822
// non-IR (unclaimed) objects count as a reference.  References from
823
// dynamic objects count only as "visible".
824 27 khays
 
825
static inline bool
826 163 khays
is_referenced_from_outside(Symbol* lsym)
827 27 khays
{
828
  if (lsym->in_real_elf())
829
    return true;
830
  if (parameters->options().relocatable())
831
    return true;
832
  if (parameters->options().is_undefined(lsym->name()))
833
    return true;
834 163 khays
  return false;
835
}
836
 
837
// Return TRUE if a defined symbol might be reachable from outside the
838
// load module.
839
 
840
static inline bool
841
is_visible_from_outside(Symbol* lsym)
842
{
843
  if (lsym->in_dyn())
844
    return true;
845 27 khays
  if (parameters->options().export_dynamic() || parameters->options().shared())
846
    return lsym->is_externally_visible();
847
  return false;
848
}
849
 
850
// Get symbol resolution info.
851
 
852
ld_plugin_status
853 163 khays
Pluginobj::get_symbol_resolution_info(int nsyms,
854
                                      ld_plugin_symbol* syms,
855
                                      int version) const
856 27 khays
{
857 163 khays
  // For version 1 of this interface, we cannot use
858
  // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
859
  // instead.
860
  const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
861
      = (version > 1
862
         ? LDPR_PREVAILING_DEF_IRONLY_EXP
863
         : LDPR_PREVAILING_DEF);
864
 
865 27 khays
  if (nsyms > this->nsyms_)
866
    return LDPS_NO_SYMS;
867
 
868
  if (static_cast<size_t>(nsyms) > this->symbols_.size())
869
    {
870
      // We never decided to include this object. We mark all symbols as
871
      // preempted.
872
      gold_assert(this->symbols_.size() == 0);
873
      for (int i = 0; i < nsyms; i++)
874
        syms[i].resolution = LDPR_PREEMPTED_REG;
875
      return LDPS_OK;
876
    }
877
 
878
  for (int i = 0; i < nsyms; i++)
879
    {
880
      ld_plugin_symbol* isym = &syms[i];
881
      Symbol* lsym = this->symbols_[i];
882
      ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
883
 
884
      if (lsym->is_undefined())
885
        // The symbol remains undefined.
886
        res = LDPR_UNDEF;
887
      else if (isym->def == LDPK_UNDEF
888
               || isym->def == LDPK_WEAKUNDEF
889
               || isym->def == LDPK_COMMON)
890
        {
891
          // The original symbol was undefined or common.
892
          if (lsym->source() != Symbol::FROM_OBJECT)
893
            res = LDPR_RESOLVED_EXEC;
894
          else if (lsym->object()->pluginobj() == this)
895 163 khays
            {
896
              if (is_referenced_from_outside(lsym))
897
                res = LDPR_PREVAILING_DEF;
898
              else if (is_visible_from_outside(lsym))
899
                res = ldpr_prevailing_def_ironly_exp;
900
              else
901
                res = LDPR_PREVAILING_DEF_IRONLY;
902
            }
903 27 khays
          else if (lsym->object()->pluginobj() != NULL)
904
            res = LDPR_RESOLVED_IR;
905
          else if (lsym->object()->is_dynamic())
906
            res = LDPR_RESOLVED_DYN;
907
          else
908
            res = LDPR_RESOLVED_EXEC;
909
        }
910
      else
911
        {
912
          // The original symbol was a definition.
913
          if (lsym->source() != Symbol::FROM_OBJECT)
914
            res = LDPR_PREEMPTED_REG;
915
          else if (lsym->object() == static_cast<const Object*>(this))
916 163 khays
            {
917
              if (is_referenced_from_outside(lsym))
918
                res = LDPR_PREVAILING_DEF;
919
              else if (is_visible_from_outside(lsym))
920
                res = ldpr_prevailing_def_ironly_exp;
921
              else
922
                res = LDPR_PREVAILING_DEF_IRONLY;
923
            }
924 27 khays
          else
925
            res = (lsym->object()->pluginobj() != NULL
926
                   ? LDPR_PREEMPTED_IR
927
                   : LDPR_PREEMPTED_REG);
928
        }
929
      isym->resolution = res;
930
    }
931
  return LDPS_OK;
932
}
933
 
934
// Return TRUE if the comdat group with key COMDAT_KEY from this object
935
// should be kept.
936
 
937
bool
938
Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
939
{
940
  std::pair<Comdat_map::iterator, bool> ins =
941
    this->comdat_map_.insert(std::make_pair(comdat_key, false));
942
 
943
  // If this is the first time we've seen this comdat key, ask the
944
  // layout object whether it should be included.
945
  if (ins.second)
946
    ins.first->second = layout->find_or_add_kept_section(comdat_key,
947
                                                         NULL, 0, true,
948
                                                         true, NULL);
949
 
950
  return ins.first->second;
951
}
952
 
953
// Class Sized_pluginobj.
954
 
955
template<int size, bool big_endian>
956
Sized_pluginobj<size, big_endian>::Sized_pluginobj(
957
    const std::string& name,
958
    Input_file* input_file,
959
    off_t offset,
960
    off_t filesize)
961
  : Pluginobj(name, input_file, offset, filesize)
962
{
963
}
964
 
965
// Read the symbols.  Not used for plugin objects.
966
 
967
template<int size, bool big_endian>
968
void
969
Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
970
{
971
  gold_unreachable();
972
}
973
 
974
// Lay out the input sections.  Not used for plugin objects.
975
 
976
template<int size, bool big_endian>
977
void
978
Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
979
                                             Read_symbols_data*)
980
{
981
  gold_unreachable();
982
}
983
 
984
// Add the symbols to the symbol table.
985
 
986
template<int size, bool big_endian>
987
void
988
Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
989
                                                  Read_symbols_data*,
990
                                                  Layout* layout)
991
{
992
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
993
  unsigned char symbuf[sym_size];
994
  elfcpp::Sym<size, big_endian> sym(symbuf);
995
  elfcpp::Sym_write<size, big_endian> osym(symbuf);
996
 
997
  typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
998
 
999
  this->symbols_.resize(this->nsyms_);
1000
 
1001
  for (int i = 0; i < this->nsyms_; ++i)
1002
    {
1003
      const struct ld_plugin_symbol* isym = &this->syms_[i];
1004
      const char* name = isym->name;
1005
      const char* ver = isym->version;
1006
      elfcpp::Elf_Half shndx;
1007
      elfcpp::STB bind;
1008
      elfcpp::STV vis;
1009
 
1010
      if (name != NULL && name[0] == '\0')
1011
        name = NULL;
1012
      if (ver != NULL && ver[0] == '\0')
1013
        ver = NULL;
1014
 
1015
      switch (isym->def)
1016
        {
1017
        case LDPK_WEAKDEF:
1018
        case LDPK_WEAKUNDEF:
1019
          bind = elfcpp::STB_WEAK;
1020
          break;
1021
        case LDPK_DEF:
1022
        case LDPK_UNDEF:
1023
        case LDPK_COMMON:
1024
        default:
1025
          bind = elfcpp::STB_GLOBAL;
1026
          break;
1027
        }
1028
 
1029
      switch (isym->def)
1030
        {
1031
        case LDPK_DEF:
1032
        case LDPK_WEAKDEF:
1033
          shndx = elfcpp::SHN_ABS;
1034
          break;
1035
        case LDPK_COMMON:
1036
          shndx = elfcpp::SHN_COMMON;
1037
          break;
1038
        case LDPK_UNDEF:
1039
        case LDPK_WEAKUNDEF:
1040
        default:
1041
          shndx = elfcpp::SHN_UNDEF;
1042
          break;
1043
        }
1044
 
1045
      switch (isym->visibility)
1046
        {
1047
        case LDPV_PROTECTED:
1048
          vis = elfcpp::STV_PROTECTED;
1049
          break;
1050
        case LDPV_INTERNAL:
1051
          vis = elfcpp::STV_INTERNAL;
1052
          break;
1053
        case LDPV_HIDDEN:
1054
          vis = elfcpp::STV_HIDDEN;
1055
          break;
1056
        case LDPV_DEFAULT:
1057
        default:
1058
          vis = elfcpp::STV_DEFAULT;
1059
          break;
1060
        }
1061
 
1062
      if (isym->comdat_key != NULL
1063
          && isym->comdat_key[0] != '\0'
1064
          && !this->include_comdat_group(isym->comdat_key, layout))
1065
        shndx = elfcpp::SHN_UNDEF;
1066
 
1067
      osym.put_st_name(0);
1068
      osym.put_st_value(0);
1069
      osym.put_st_size(static_cast<Elf_size_type>(isym->size));
1070
      osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1071
      osym.put_st_other(vis, 0);
1072
      osym.put_st_shndx(shndx);
1073
 
1074
      this->symbols_[i] =
1075
        symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1076
    }
1077
}
1078
 
1079
template<int size, bool big_endian>
1080
Archive::Should_include
1081
Sized_pluginobj<size, big_endian>::do_should_include_member(
1082
    Symbol_table* symtab,
1083
    Layout* layout,
1084
    Read_symbols_data*,
1085
    std::string* why)
1086
{
1087
  char* tmpbuf = NULL;
1088
  size_t tmpbuflen = 0;
1089
 
1090
  for (int i = 0; i < this->nsyms_; ++i)
1091
    {
1092
      const struct ld_plugin_symbol& sym = this->syms_[i];
1093
      const char* name = sym.name;
1094
      Symbol* symbol;
1095
      Archive::Should_include t = Archive::should_include_member(symtab,
1096
                                                                 layout,
1097
                                                                 name,
1098
                                                                 &symbol, why,
1099
                                                                 &tmpbuf,
1100
                                                                 &tmpbuflen);
1101
      if (t == Archive::SHOULD_INCLUDE_YES)
1102
        {
1103
          if (tmpbuf != NULL)
1104
            free(tmpbuf);
1105
          return t;
1106
        }
1107
    }
1108
  if (tmpbuf != NULL)
1109
    free(tmpbuf);
1110
  return Archive::SHOULD_INCLUDE_UNKNOWN;
1111
}
1112
 
1113
// Iterate over global symbols, calling a visitor class V for each.
1114
 
1115
template<int size, bool big_endian>
1116
void
1117
Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1118
    Read_symbols_data*,
1119
    Library_base::Symbol_visitor_base* v)
1120
{
1121
  for (int i = 0; i < this->nsyms_; ++i)
1122
    {
1123
      const struct ld_plugin_symbol& sym = this->syms_[i];
1124
      if (sym.def != LDPK_UNDEF)
1125
        v->visit(sym.name);
1126
    }
1127
}
1128
 
1129
// Iterate over local symbols, calling a visitor class V for each GOT offset
1130
// associated with a local symbol.
1131
template<int size, bool big_endian>
1132
void
1133
Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1134
    Got_offset_list::Visitor*) const
1135
{
1136
  gold_unreachable();
1137
}
1138
 
1139
// Get the size of a section.  Not used for plugin objects.
1140
 
1141
template<int size, bool big_endian>
1142
uint64_t
1143
Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1144
{
1145
  gold_unreachable();
1146
  return 0;
1147
}
1148
 
1149
// Get the name of a section.  Not used for plugin objects.
1150
 
1151
template<int size, bool big_endian>
1152
std::string
1153
Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
1154
{
1155
  gold_unreachable();
1156
  return std::string();
1157
}
1158
 
1159
// Return a view of the contents of a section.  Not used for plugin objects.
1160
 
1161
template<int size, bool big_endian>
1162
Object::Location
1163
Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
1164
{
1165
  Location loc(0, 0);
1166
 
1167
  gold_unreachable();
1168
  return loc;
1169
}
1170
 
1171
// Return section flags.  Not used for plugin objects.
1172
 
1173
template<int size, bool big_endian>
1174
uint64_t
1175
Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1176
{
1177
  gold_unreachable();
1178
  return 0;
1179
}
1180
 
1181
// Return section entsize.  Not used for plugin objects.
1182
 
1183
template<int size, bool big_endian>
1184
uint64_t
1185
Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1186
{
1187
  gold_unreachable();
1188
  return 0;
1189
}
1190
 
1191
// Return section address.  Not used for plugin objects.
1192
 
1193
template<int size, bool big_endian>
1194
uint64_t
1195
Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1196
{
1197
  gold_unreachable();
1198
  return 0;
1199
}
1200
 
1201
// Return section type.  Not used for plugin objects.
1202
 
1203
template<int size, bool big_endian>
1204
unsigned int
1205
Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1206
{
1207
  gold_unreachable();
1208
  return 0;
1209
}
1210
 
1211
// Return the section link field.  Not used for plugin objects.
1212
 
1213
template<int size, bool big_endian>
1214
unsigned int
1215
Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1216
{
1217
  gold_unreachable();
1218
  return 0;
1219
}
1220
 
1221
// Return the section link field.  Not used for plugin objects.
1222
 
1223
template<int size, bool big_endian>
1224
unsigned int
1225
Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1226
{
1227
  gold_unreachable();
1228
  return 0;
1229
}
1230
 
1231
// Return the section alignment.  Not used for plugin objects.
1232
 
1233
template<int size, bool big_endian>
1234
uint64_t
1235
Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1236
{
1237
  gold_unreachable();
1238
  return 0;
1239
}
1240
 
1241
// Return the Xindex structure to use.  Not used for plugin objects.
1242
 
1243
template<int size, bool big_endian>
1244
Xindex*
1245
Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1246
{
1247
  gold_unreachable();
1248
  return NULL;
1249
}
1250
 
1251 163 khays
// Get symbol counts.  Don't count plugin objects; the replacement
1252
// files will provide the counts.
1253 27 khays
 
1254
template<int size, bool big_endian>
1255
void
1256 163 khays
Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1257
    const Symbol_table*,
1258
    size_t* defined,
1259
    size_t* used) const
1260 27 khays
{
1261 163 khays
  *defined = 0;
1262
  *used = 0;
1263 27 khays
}
1264
 
1265
// Get symbols.  Not used for plugin objects.
1266
 
1267
template<int size, bool big_endian>
1268
const Object::Symbols*
1269
Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1270
{
1271
  gold_unreachable();
1272
}
1273
 
1274
// Class Plugin_finish.  This task runs after all replacement files have
1275
// been added.  For now, it's a placeholder for a possible plugin API
1276
// to allow the plugin to release most of its resources.  The cleanup
1277
// handlers must be called later, because they can remove the temporary
1278
// object files that are needed until the end of the link.
1279
 
1280
class Plugin_finish : public Task
1281
{
1282
 public:
1283
  Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1284
    : this_blocker_(this_blocker), next_blocker_(next_blocker)
1285
  { }
1286
 
1287
  ~Plugin_finish()
1288
  {
1289
    if (this->this_blocker_ != NULL)
1290
      delete this->this_blocker_;
1291
  }
1292
 
1293
  Task_token*
1294
  is_runnable()
1295
  {
1296
    if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1297
      return this->this_blocker_;
1298
    return NULL;
1299
  }
1300
 
1301
  void
1302
  locks(Task_locker* tl)
1303
  { tl->add(this, this->next_blocker_); }
1304
 
1305
  void
1306
  run(Workqueue*)
1307
  {
1308
    // We could call early cleanup handlers here.
1309
  }
1310
 
1311
  std::string
1312
  get_name() const
1313
  { return "Plugin_finish"; }
1314
 
1315
 private:
1316
  Task_token* this_blocker_;
1317
  Task_token* next_blocker_;
1318
};
1319
 
1320
// Class Plugin_hook.
1321
 
1322
Plugin_hook::~Plugin_hook()
1323
{
1324
}
1325
 
1326
// Return whether a Plugin_hook task is runnable.
1327
 
1328
Task_token*
1329
Plugin_hook::is_runnable()
1330
{
1331
  if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1332
    return this->this_blocker_;
1333
  return NULL;
1334
}
1335
 
1336
// Return a Task_locker for a Plugin_hook task.  We don't need any
1337
// locks here.
1338
 
1339
void
1340
Plugin_hook::locks(Task_locker*)
1341
{
1342
}
1343
 
1344
// Run the "all symbols read" plugin hook.
1345
 
1346
void
1347
Plugin_hook::run(Workqueue* workqueue)
1348
{
1349
  gold_assert(this->options_.has_plugins());
1350
  Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1351
  if (start_sym != NULL)
1352
    start_sym->set_in_real_elf();
1353
 
1354
  this->options_.plugins()->all_symbols_read(workqueue,
1355
                                             this,
1356
                                             this->input_objects_,
1357
                                             this->symtab_,
1358
                                             this->dirpath_,
1359
                                             this->mapfile_,
1360
                                             &this->this_blocker_);
1361
  workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1362
                                          this->next_blocker_));
1363
}
1364
 
1365
// The C interface routines called by the plugins.
1366
 
1367
#ifdef ENABLE_PLUGINS
1368
 
1369
// Register a claim-file handler.
1370
 
1371
static enum ld_plugin_status
1372
register_claim_file(ld_plugin_claim_file_handler handler)
1373
{
1374
  gold_assert(parameters->options().has_plugins());
1375
  parameters->options().plugins()->set_claim_file_handler(handler);
1376
  return LDPS_OK;
1377
}
1378
 
1379
// Register an all-symbols-read handler.
1380
 
1381
static enum ld_plugin_status
1382
register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1383
{
1384
  gold_assert(parameters->options().has_plugins());
1385
  parameters->options().plugins()->set_all_symbols_read_handler(handler);
1386
  return LDPS_OK;
1387
}
1388
 
1389
// Register a cleanup handler.
1390
 
1391
static enum ld_plugin_status
1392
register_cleanup(ld_plugin_cleanup_handler handler)
1393
{
1394
  gold_assert(parameters->options().has_plugins());
1395
  parameters->options().plugins()->set_cleanup_handler(handler);
1396
  return LDPS_OK;
1397
}
1398
 
1399
// Add symbols from a plugin-claimed input file.
1400
 
1401
static enum ld_plugin_status
1402
add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1403
{
1404
  gold_assert(parameters->options().has_plugins());
1405
  Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1406
      static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1407
  if (obj == NULL)
1408
    return LDPS_ERR;
1409
  obj->store_incoming_symbols(nsyms, syms);
1410
  return LDPS_OK;
1411
}
1412
 
1413
// Get the input file information with an open (possibly re-opened)
1414
// file descriptor.
1415
 
1416
static enum ld_plugin_status
1417
get_input_file(const void* handle, struct ld_plugin_input_file* file)
1418
{
1419
  gold_assert(parameters->options().has_plugins());
1420
  unsigned int obj_index =
1421
      static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1422
  return parameters->options().plugins()->get_input_file(obj_index, file);
1423
}
1424
 
1425
// Release the input file.
1426
 
1427
static enum ld_plugin_status
1428
release_input_file(const void* handle)
1429
{
1430
  gold_assert(parameters->options().has_plugins());
1431
  unsigned int obj_index =
1432
      static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1433
  return parameters->options().plugins()->release_input_file(obj_index);
1434
}
1435
 
1436
static enum ld_plugin_status
1437
get_view(const void *handle, const void **viewp)
1438
{
1439
  gold_assert(parameters->options().has_plugins());
1440
  unsigned int obj_index =
1441
      static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1442
  return parameters->options().plugins()->get_view(obj_index, viewp);
1443
}
1444
 
1445
// Get the symbol resolution info for a plugin-claimed input file.
1446
 
1447
static enum ld_plugin_status
1448
get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1449
{
1450
  gold_assert(parameters->options().has_plugins());
1451 159 khays
  Object* obj = parameters->options().plugins()->object(
1452
    static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1453 27 khays
  if (obj == NULL)
1454
    return LDPS_ERR;
1455 159 khays
  Pluginobj* plugin_obj = obj->pluginobj();
1456
  if (plugin_obj == NULL)
1457
    return LDPS_ERR;
1458 163 khays
  return plugin_obj->get_symbol_resolution_info(nsyms, syms, 1);
1459 27 khays
}
1460
 
1461 163 khays
// Version 2 of the above.  The only difference is that this version
1462
// is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1463
 
1464
static enum ld_plugin_status
1465
get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1466
{
1467
  gold_assert(parameters->options().has_plugins());
1468
  Object* obj = parameters->options().plugins()->object(
1469
    static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1470
  if (obj == NULL)
1471
    return LDPS_ERR;
1472
  Pluginobj* plugin_obj = obj->pluginobj();
1473
  if (plugin_obj == NULL)
1474
    return LDPS_ERR;
1475
  return plugin_obj->get_symbol_resolution_info(nsyms, syms, 2);
1476
}
1477
 
1478 27 khays
// Add a new (real) input file generated by a plugin.
1479
 
1480
static enum ld_plugin_status
1481
add_input_file(const char* pathname)
1482
{
1483
  gold_assert(parameters->options().has_plugins());
1484
  return parameters->options().plugins()->add_input_file(pathname, false);
1485
}
1486
 
1487
// Add a new (real) library required by a plugin.
1488
 
1489
static enum ld_plugin_status
1490
add_input_library(const char* pathname)
1491
{
1492
  gold_assert(parameters->options().has_plugins());
1493
  return parameters->options().plugins()->add_input_file(pathname, true);
1494
}
1495
 
1496
// Set the extra library path to be used by libraries added via
1497
// add_input_library
1498
 
1499
static enum ld_plugin_status
1500
set_extra_library_path(const char* path)
1501
{
1502
  gold_assert(parameters->options().has_plugins());
1503
  return parameters->options().plugins()->set_extra_library_path(path);
1504
}
1505
 
1506
// Issue a diagnostic message from a plugin.
1507
 
1508
static enum ld_plugin_status
1509
message(int level, const char* format, ...)
1510
{
1511
  va_list args;
1512
  va_start(args, format);
1513
 
1514
  switch (level)
1515
    {
1516
    case LDPL_INFO:
1517
      parameters->errors()->info(format, args);
1518
      break;
1519
    case LDPL_WARNING:
1520
      parameters->errors()->warning(format, args);
1521
      break;
1522
    case LDPL_ERROR:
1523
    default:
1524
      parameters->errors()->error(format, args);
1525
      break;
1526
    case LDPL_FATAL:
1527
      parameters->errors()->fatal(format, args);
1528
      break;
1529
    }
1530
 
1531
  va_end(args);
1532
  return LDPS_OK;
1533
}
1534
 
1535 159 khays
// Get the section count of the object corresponding to the handle.  This
1536
// plugin interface can only be called in the claim_file handler of the plugin.
1537
 
1538
static enum ld_plugin_status
1539
get_input_section_count(const void* handle, unsigned int* count)
1540
{
1541
  gold_assert(parameters->options().has_plugins());
1542
 
1543
  if (!parameters->options().plugins()->in_claim_file_handler())
1544
    return LDPS_ERR;
1545
 
1546
  Object* obj = parameters->options().plugins()->get_elf_object(handle);
1547
 
1548
  if (obj == NULL)
1549
    return LDPS_ERR;
1550
 
1551
  *count = obj->shnum();
1552
  return LDPS_OK;
1553
}
1554
 
1555
// Get the type of the specified section in the object corresponding
1556
// to the handle.  This plugin interface can only be called in the
1557
// claim_file handler of the plugin.
1558
 
1559
static enum ld_plugin_status
1560
get_input_section_type(const struct ld_plugin_section section,
1561
                       unsigned int* type)
1562
{
1563
  gold_assert(parameters->options().has_plugins());
1564
 
1565
  if (!parameters->options().plugins()->in_claim_file_handler())
1566
    return LDPS_ERR;
1567
 
1568
  Object* obj
1569
    = parameters->options().plugins()->get_elf_object(section.handle);
1570
 
1571
  if (obj == NULL)
1572
    return LDPS_BAD_HANDLE;
1573
 
1574
  *type = obj->section_type(section.shndx);
1575
  return LDPS_OK;
1576
}
1577
 
1578
// Get the name of the specified section in the object corresponding
1579
// to the handle.  This plugin interface can only be called in the
1580
// claim_file handler of the plugin.
1581
 
1582
static enum ld_plugin_status
1583
get_input_section_name(const struct ld_plugin_section section,
1584
                       char** section_name_ptr)
1585
{
1586
  gold_assert(parameters->options().has_plugins());
1587
 
1588
  if (!parameters->options().plugins()->in_claim_file_handler())
1589
    return LDPS_ERR;
1590
 
1591
  Object* obj
1592
    = parameters->options().plugins()->get_elf_object(section.handle);
1593
 
1594
  if (obj == NULL)
1595
    return LDPS_BAD_HANDLE;
1596
 
1597
  // Check if the object is locked before getting the section name.
1598
  gold_assert(obj->is_locked());
1599
 
1600
  const std::string section_name = obj->section_name(section.shndx);
1601
  *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1602
  memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1603
  return LDPS_OK;
1604
}
1605
 
1606
// Get the contents of the specified section in the object corresponding
1607
// to the handle.  This plugin interface can only be called in the
1608
// claim_file handler of the plugin.
1609
 
1610
static enum ld_plugin_status
1611
get_input_section_contents(const struct ld_plugin_section section,
1612
                           const unsigned char** section_contents_ptr,
1613
                           size_t* len)
1614
{
1615
  gold_assert(parameters->options().has_plugins());
1616
 
1617
  if (!parameters->options().plugins()->in_claim_file_handler())
1618
    return LDPS_ERR;
1619
 
1620
  Object* obj
1621
    = parameters->options().plugins()->get_elf_object(section.handle);
1622
 
1623
  if (obj == NULL)
1624
    return LDPS_BAD_HANDLE;
1625
 
1626
  // Check if the object is locked before getting the section contents.
1627
  gold_assert(obj->is_locked());
1628
 
1629
  section_size_type plen;
1630
  *section_contents_ptr
1631
      = obj->section_contents(section.shndx, &plen, false);
1632
  *len = plen;
1633
  return LDPS_OK;
1634
}
1635
 
1636
// Specify the ordering of sections in the final layout. The sections are
1637
// specified as (handle,shndx) pairs in the two arrays in the order in
1638
// which they should appear in the final layout.
1639
 
1640
static enum ld_plugin_status
1641 163 khays
update_section_order(const struct ld_plugin_section* section_list,
1642 159 khays
                     unsigned int num_sections)
1643
{
1644
  gold_assert(parameters->options().has_plugins());
1645
 
1646
  if (num_sections == 0)
1647
    return LDPS_OK;
1648
 
1649
  if (section_list == NULL)
1650
    return LDPS_ERR;
1651
 
1652 163 khays
  Layout* layout = parameters->options().plugins()->layout();
1653
  gold_assert (layout != NULL);
1654 159 khays
 
1655 163 khays
  std::map<Section_id, unsigned int>* order_map
1656
    = layout->get_section_order_map();
1657
 
1658
  /* Store the mapping from Section_id to section position in layout's
1659
     order_map to consult after output sections are added.  */
1660 159 khays
  for (unsigned int i = 0; i < num_sections; ++i)
1661
    {
1662
      Object* obj = parameters->options().plugins()->get_elf_object(
1663
          section_list[i].handle);
1664
      if (obj == NULL)
1665
        return LDPS_BAD_HANDLE;
1666
      unsigned int shndx = section_list[i].shndx;
1667
      Section_id secn_id(obj, shndx);
1668 163 khays
      (*order_map)[secn_id] = i + 1;
1669 159 khays
    }
1670
 
1671
  return LDPS_OK;
1672
}
1673
 
1674
// Let the linker know that the sections could be reordered.
1675
 
1676
static enum ld_plugin_status
1677
allow_section_ordering()
1678
{
1679
  gold_assert(parameters->options().has_plugins());
1680
  Layout* layout = parameters->options().plugins()->layout();
1681
  layout->set_section_ordering_specified();
1682
  return LDPS_OK;
1683
}
1684
 
1685 27 khays
#endif // ENABLE_PLUGINS
1686
 
1687
// Allocate a Pluginobj object of the appropriate size and endianness.
1688
 
1689
static Pluginobj*
1690
make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1691
{
1692
  Pluginobj* obj = NULL;
1693
 
1694
  parameters_force_valid_target();
1695
  const Target& target(parameters->target());
1696
 
1697
  if (target.get_size() == 32)
1698
    {
1699
      if (target.is_big_endian())
1700
#ifdef HAVE_TARGET_32_BIG
1701
        obj = new Sized_pluginobj<32, true>(input_file->filename(),
1702
                                            input_file, offset, filesize);
1703
#else
1704
        gold_error(_("%s: not configured to support "
1705
                     "32-bit big-endian object"),
1706
                   input_file->filename().c_str());
1707
#endif
1708
      else
1709
#ifdef HAVE_TARGET_32_LITTLE
1710
        obj = new Sized_pluginobj<32, false>(input_file->filename(),
1711
                                             input_file, offset, filesize);
1712
#else
1713
        gold_error(_("%s: not configured to support "
1714
                     "32-bit little-endian object"),
1715
                   input_file->filename().c_str());
1716
#endif
1717
    }
1718
  else if (target.get_size() == 64)
1719
    {
1720
      if (target.is_big_endian())
1721
#ifdef HAVE_TARGET_64_BIG
1722
        obj = new Sized_pluginobj<64, true>(input_file->filename(),
1723
                                            input_file, offset, filesize);
1724
#else
1725
        gold_error(_("%s: not configured to support "
1726
                     "64-bit big-endian object"),
1727
                   input_file->filename().c_str());
1728
#endif
1729
      else
1730
#ifdef HAVE_TARGET_64_LITTLE
1731
        obj = new Sized_pluginobj<64, false>(input_file->filename(),
1732
                                             input_file, offset, filesize);
1733
#else
1734
        gold_error(_("%s: not configured to support "
1735
                     "64-bit little-endian object"),
1736
                   input_file->filename().c_str());
1737
#endif
1738
    }
1739
 
1740
  gold_assert(obj != NULL);
1741
  return obj;
1742
}
1743
 
1744
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.