OSDN Git Service

Another snapshot of the current state of the sources. Gets to the
[pf3gnuchains/pf3gnuchains3x.git] / gold / readsyms.cc
1 // readsyms.cc -- read input file symbols for gold
2
3 #include "gold.h"
4
5 #include <cstring>
6
7 #include "elfcpp.h"
8 #include "options.h"
9 #include "dirsearch.h"
10 #include "readsyms.h"
11
12 namespace gold
13 {
14
15 // Class read_symbols.
16
17 Read_symbols::~Read_symbols()
18 {
19   // The this_blocker_ and next_blocker_ pointers are passed on to the
20   // Add_symbols task.
21 }
22
23 // Return whether a Read_symbols task is runnable.  We need write
24 // access to the symbol table.  We can read an ordinary input file
25 // immediately.  For an archive specified using -l, we have to wait
26 // until the search path is complete.
27
28 Task::Is_runnable_type
29 Read_symbols::is_runnable(Workqueue*)
30 {
31   if (this->input_.is_lib() && this->dirpath_.token().is_blocked())
32     return IS_BLOCKED;
33
34   return IS_RUNNABLE;
35 }
36
37 // Return a Task_locker for a Read_symbols task.  We don't need any
38 // locks here.
39
40 Task_locker*
41 Read_symbols::locks(Workqueue*)
42 {
43   return NULL;
44 }
45
46 // Run a Read_symbols task.  This is where we actually read the
47 // symbols and relocations.
48
49 void
50 Read_symbols::run(Workqueue* workqueue)
51 {
52   // We don't keep track of Input_file objects, so this is a memory
53   // leak.
54   Input_file* input_file = new Input_file(this->input_);
55   input_file->open(this->options_, this->dirpath_);
56
57   // Read enough of the file to pick up the entire ELF header.
58
59   int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
60   off_t bytes;
61   const unsigned char* p = input_file->file().get_view(0, ehdr_size, &bytes);
62   if (bytes >= 4)
63     {
64       static unsigned char elfmagic[4] =
65         {
66           elfcpp::ELFMAG0, elfcpp::ELFMAG1,
67           elfcpp::ELFMAG2, elfcpp::ELFMAG3
68         };
69       if (memcmp(p, elfmagic, 4) == 0)
70         {
71           // This is an ELF object.
72           Object* obj = make_elf_object(this->input_.name(), input_file, 0,
73                                         p, bytes);
74                                         
75           Read_symbols_data sd = obj->read_symbols();
76           workqueue->queue(new Add_symbols(this->symtab_, obj, sd,
77                                            this->this_blocker_,
78                                            this->next_blocker_));
79
80           // Opening the file locked it, so now we need to unlock it.
81           input_file->file().unlock();
82
83           return;
84         }
85     }
86
87   // Here we have to handle archives and any other input file
88   // types we need.
89   gold_fatal("only objects are currently supported", false);
90 }
91
92 // Class Add_symbols.
93
94 Add_symbols::~Add_symbols()
95 {
96   if (this->this_blocker_ != NULL)
97     delete this->this_blocker_;
98   // next_blocker_ is deleted by the task associated with the next
99   // input file.
100 }
101
102 // We are blocked by this_blocker_.  We block next_blocker_.
103
104 Task::Is_runnable_type
105 Add_symbols::is_runnable(Workqueue*)
106 {
107   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
108     return IS_BLOCKED;
109   return IS_RUNNABLE;
110 }
111
112 Task_locker*
113 Add_symbols::locks(Workqueue* workqueue)
114 {
115   return new Task_locker_block(*this->next_blocker_, workqueue);
116 }
117
118 void
119 Add_symbols::run(Workqueue*)
120 {
121   this->object_->add_symbols(this->symtab_, this->sd_);
122 }
123
124 } // End namespace gold.