Add global parameters.
[deliverable/binutils-gdb.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 "symtab.h"
11 #include "object.h"
12 #include "archive.h"
13 #include "script.h"
14 #include "readsyms.h"
15
16 namespace gold
17 {
18
19 // Class read_symbols.
20
21 Read_symbols::~Read_symbols()
22 {
23 // The this_blocker_ and next_blocker_ pointers are passed on to the
24 // Add_symbols task.
25 }
26
27 // Return whether a Read_symbols task is runnable. We can read an
28 // ordinary input file immediately. For an archive specified using
29 // -l, we have to wait until the search path is complete.
30
31 Task::Is_runnable_type
32 Read_symbols::is_runnable(Workqueue*)
33 {
34 if (this->input_argument_->is_file()
35 && this->input_argument_->file().is_lib()
36 && this->dirpath_.token().is_blocked())
37 return IS_BLOCKED;
38
39 return IS_RUNNABLE;
40 }
41
42 // Return a Task_locker for a Read_symbols task. We don't need any
43 // locks here.
44
45 Task_locker*
46 Read_symbols::locks(Workqueue*)
47 {
48 return NULL;
49 }
50
51 // Run a Read_symbols task. This is where we actually read the
52 // symbols and relocations.
53
54 void
55 Read_symbols::run(Workqueue* workqueue)
56 {
57 if (this->input_argument_->is_group())
58 {
59 gold_assert(this->input_group_ == NULL);
60 this->do_group(workqueue);
61 return;
62 }
63
64 Input_file* input_file = new Input_file(&this->input_argument_->file());
65 input_file->open(this->options_, this->dirpath_);
66
67 // Read enough of the file to pick up the entire ELF header.
68
69 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
70 off_t bytes;
71 const unsigned char* p = input_file->file().get_view(0, ehdr_size, &bytes);
72 if (bytes >= 4)
73 {
74 static unsigned char elfmagic[4] =
75 {
76 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
77 elfcpp::ELFMAG2, elfcpp::ELFMAG3
78 };
79 if (memcmp(p, elfmagic, 4) == 0)
80 {
81 // This is an ELF object.
82
83 Object* obj = make_elf_object(input_file->filename(),
84 input_file, 0, p, bytes);
85
86 // We don't have a way to record a non-archive in an input
87 // group. If this is an ordinary object file, we can't
88 // include it more than once anyhow. If this is a dynamic
89 // object, then including it a second time changes nothing.
90 if (this->input_group_ != NULL && !obj->is_dynamic())
91 {
92 fprintf(stderr,
93 _("%s: %s: ordinary object found in input group\n"),
94 program_name, input_file->name());
95 gold_exit(false);
96 }
97
98 Read_symbols_data* sd = new Read_symbols_data;
99 obj->read_symbols(sd);
100 workqueue->queue_front(new Add_symbols(this->input_objects_,
101 this->symtab_, this->layout_,
102 obj, sd,
103 this->this_blocker_,
104 this->next_blocker_));
105
106 // Opening the file locked it, so now we need to unlock it.
107 input_file->file().unlock();
108
109 return;
110 }
111 }
112
113 if (bytes >= Archive::sarmag)
114 {
115 if (memcmp(p, Archive::armag, Archive::sarmag) == 0)
116 {
117 // This is an archive.
118 Archive* arch = new Archive(this->input_argument_->file().name(),
119 input_file);
120 arch->setup();
121 workqueue->queue(new Add_archive_symbols(this->symtab_,
122 this->layout_,
123 this->input_objects_,
124 arch,
125 this->input_group_,
126 this->this_blocker_,
127 this->next_blocker_));
128 return;
129 }
130 }
131
132 if (bytes == 0)
133 {
134 fprintf(stderr, _("%s: %s: file is empty\n"),
135 program_name, input_file->file().filename().c_str());
136 gold_exit(false);
137 }
138
139 // Try to parse this file as a script.
140 if (read_input_script(workqueue, this->options_, this->symtab_,
141 this->layout_, this->dirpath_, this->input_objects_,
142 this->input_group_, this->input_argument_, input_file,
143 p, bytes, this->this_blocker_, this->next_blocker_))
144 return;
145
146 // Here we have to handle any other input file types we need.
147 fprintf(stderr, _("%s: %s: not an object or archive\n"),
148 program_name, input_file->file().filename().c_str());
149 gold_exit(false);
150 }
151
152 // Handle a group. We need to walk through the arguments over and
153 // over until we don't see any new undefined symbols. We do this by
154 // setting off Read_symbols Tasks as usual, but recording the archive
155 // entries instead of deleting them. We also start a Finish_group
156 // Task which runs after we've read all the symbols. In that task we
157 // process the archives in a loop until we are done.
158
159 void
160 Read_symbols::do_group(Workqueue* workqueue)
161 {
162 Input_group* input_group = new Input_group();
163
164 const Input_file_group* group = this->input_argument_->group();
165 Task_token* this_blocker = this->this_blocker_;
166 for (Input_file_group::const_iterator p = group->begin();
167 p != group->end();
168 ++p)
169 {
170 const Input_argument* arg = &*p;
171 gold_assert(arg->is_file());
172
173 Task_token* next_blocker = new Task_token();
174 next_blocker->add_blocker();
175 workqueue->queue(new Read_symbols(this->options_, this->input_objects_,
176 this->symtab_, this->layout_,
177 this->dirpath_, arg, input_group,
178 this_blocker, next_blocker));
179 this_blocker = next_blocker;
180 }
181
182 const int saw_undefined = this->symtab_->saw_undefined();
183 workqueue->queue(new Finish_group(this->input_objects_,
184 this->symtab_,
185 this->layout_,
186 input_group,
187 saw_undefined,
188 this_blocker,
189 this->next_blocker_));
190 }
191
192 // Class Add_symbols.
193
194 Add_symbols::~Add_symbols()
195 {
196 if (this->this_blocker_ != NULL)
197 delete this->this_blocker_;
198 // next_blocker_ is deleted by the task associated with the next
199 // input file.
200 }
201
202 // We are blocked by this_blocker_. We block next_blocker_. We also
203 // lock the file.
204
205 Task::Is_runnable_type
206 Add_symbols::is_runnable(Workqueue*)
207 {
208 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
209 return IS_BLOCKED;
210 if (this->object_->is_locked())
211 return IS_LOCKED;
212 return IS_RUNNABLE;
213 }
214
215 class Add_symbols::Add_symbols_locker : public Task_locker
216 {
217 public:
218 Add_symbols_locker(Task_token& token, Workqueue* workqueue,
219 Object* object)
220 : blocker_(token, workqueue), objlock_(*object)
221 { }
222
223 private:
224 Task_locker_block blocker_;
225 Task_locker_obj<Object> objlock_;
226 };
227
228 Task_locker*
229 Add_symbols::locks(Workqueue* workqueue)
230 {
231 return new Add_symbols_locker(*this->next_blocker_, workqueue,
232 this->object_);
233 }
234
235 // Add the symbols in the object to the symbol table.
236
237 void
238 Add_symbols::run(Workqueue*)
239 {
240 if (!this->input_objects_->add_object(this->object_))
241 {
242 // FIXME: We need to close the descriptor here.
243 delete this->object_;
244 }
245 else
246 {
247 this->object_->layout(this->symtab_, this->layout_, this->sd_);
248 this->object_->add_symbols(this->symtab_, this->sd_);
249 }
250 delete this->sd_;
251 this->sd_ = NULL;
252 }
253
254 // Class Finish_group.
255
256 Finish_group::~Finish_group()
257 {
258 if (this->this_blocker_ != NULL)
259 delete this->this_blocker_;
260 // next_blocker_ is deleted by the task associated with the next
261 // input file following the group.
262 }
263
264 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
265
266 Task::Is_runnable_type
267 Finish_group::is_runnable(Workqueue*)
268 {
269 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
270 return IS_BLOCKED;
271 return IS_RUNNABLE;
272 }
273
274 Task_locker*
275 Finish_group::locks(Workqueue* workqueue)
276 {
277 return new Task_locker_block(*this->next_blocker_, workqueue);
278 }
279
280 // Loop over the archives until there are no new undefined symbols.
281
282 void
283 Finish_group::run(Workqueue*)
284 {
285 int saw_undefined = this->saw_undefined_;
286 while (saw_undefined != this->symtab_->saw_undefined())
287 {
288 saw_undefined = this->symtab_->saw_undefined();
289
290 for (Input_group::const_iterator p = this->input_group_->begin();
291 p != this->input_group_->end();
292 ++p)
293 {
294 Task_lock_obj<Archive> tl(**p);
295
296 (*p)->add_symbols(this->symtab_, this->layout_,
297 this->input_objects_);
298 }
299 }
300
301 // Delete all the archives now that we no longer need them.
302 for (Input_group::const_iterator p = this->input_group_->begin();
303 p != this->input_group_->end();
304 ++p)
305 delete *p;
306 delete this->input_group_;
307 }
308
309 } // End namespace gold.
This page took 0.036602 seconds and 5 git commands to generate.