Commit | Line | Data |
---|---|---|
bae7f79e ILT |
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" | |
f6ce93d6 | 10 | #include "symtab.h" |
a2fb1b05 | 11 | #include "object.h" |
61ba1cf9 | 12 | #include "archive.h" |
dbe717ef | 13 | #include "script.h" |
61ba1cf9 | 14 | #include "readsyms.h" |
bae7f79e ILT |
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 | ||
ead1e424 ILT |
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. | |
bae7f79e ILT |
30 | |
31 | Task::Is_runnable_type | |
32 | Read_symbols::is_runnable(Workqueue*) | |
33 | { | |
dbe717ef ILT |
34 | if (this->input_argument_->is_file() |
35 | && this->input_argument_->file().is_lib() | |
ead1e424 | 36 | && this->dirpath_.token().is_blocked()) |
bae7f79e ILT |
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 | { | |
dbe717ef | 57 | if (this->input_argument_->is_group()) |
ead1e424 | 58 | { |
a3ad94ed | 59 | gold_assert(this->input_group_ == NULL); |
ead1e424 ILT |
60 | this->do_group(workqueue); |
61 | return; | |
62 | } | |
63 | ||
5a6f7e2d | 64 | Input_file* input_file = new Input_file(&this->input_argument_->file()); |
bae7f79e ILT |
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. | |
a2fb1b05 | 82 | |
dbe717ef ILT |
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()) | |
ead1e424 ILT |
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 | ||
12e14209 ILT |
98 | Read_symbols_data* sd = new Read_symbols_data; |
99 | obj->read_symbols(sd); | |
f6ce93d6 ILT |
100 | workqueue->queue_front(new Add_symbols(this->options_, |
101 | this->input_objects_, | |
ead1e424 | 102 | this->symtab_, this->layout_, |
92e059d8 ILT |
103 | obj, sd, |
104 | this->this_blocker_, | |
105 | this->next_blocker_)); | |
bae7f79e ILT |
106 | |
107 | // Opening the file locked it, so now we need to unlock it. | |
108 | input_file->file().unlock(); | |
109 | ||
110 | return; | |
111 | } | |
112 | } | |
113 | ||
61ba1cf9 ILT |
114 | if (bytes >= Archive::sarmag) |
115 | { | |
116 | if (memcmp(p, Archive::armag, Archive::sarmag) == 0) | |
117 | { | |
118 | // This is an archive. | |
dbe717ef ILT |
119 | Archive* arch = new Archive(this->input_argument_->file().name(), |
120 | input_file); | |
61ba1cf9 | 121 | arch->setup(); |
f6ce93d6 ILT |
122 | workqueue->queue(new Add_archive_symbols(this->options_, |
123 | this->symtab_, | |
12e14209 | 124 | this->layout_, |
61ba1cf9 ILT |
125 | this->input_objects_, |
126 | arch, | |
ead1e424 | 127 | this->input_group_, |
61ba1cf9 ILT |
128 | this->this_blocker_, |
129 | this->next_blocker_)); | |
130 | return; | |
131 | } | |
132 | } | |
133 | ||
dbe717ef ILT |
134 | if (bytes == 0) |
135 | { | |
136 | fprintf(stderr, _("%s: %s: file is empty\n"), | |
137 | program_name, input_file->file().filename().c_str()); | |
138 | gold_exit(false); | |
139 | } | |
140 | ||
141 | // Try to parse this file as a script. | |
142 | if (read_input_script(workqueue, this->options_, this->symtab_, | |
143 | this->layout_, this->dirpath_, this->input_objects_, | |
144 | this->input_group_, this->input_argument_, input_file, | |
145 | p, bytes, this->this_blocker_, this->next_blocker_)) | |
146 | return; | |
147 | ||
92e059d8 | 148 | // Here we have to handle any other input file types we need. |
61ba1cf9 ILT |
149 | fprintf(stderr, _("%s: %s: not an object or archive\n"), |
150 | program_name, input_file->file().filename().c_str()); | |
151 | gold_exit(false); | |
bae7f79e ILT |
152 | } |
153 | ||
ead1e424 ILT |
154 | // Handle a group. We need to walk through the arguments over and |
155 | // over until we don't see any new undefined symbols. We do this by | |
156 | // setting off Read_symbols Tasks as usual, but recording the archive | |
157 | // entries instead of deleting them. We also start a Finish_group | |
158 | // Task which runs after we've read all the symbols. In that task we | |
159 | // process the archives in a loop until we are done. | |
160 | ||
161 | void | |
162 | Read_symbols::do_group(Workqueue* workqueue) | |
163 | { | |
164 | Input_group* input_group = new Input_group(); | |
165 | ||
dbe717ef | 166 | const Input_file_group* group = this->input_argument_->group(); |
ead1e424 ILT |
167 | Task_token* this_blocker = this->this_blocker_; |
168 | for (Input_file_group::const_iterator p = group->begin(); | |
169 | p != group->end(); | |
170 | ++p) | |
171 | { | |
dbe717ef | 172 | const Input_argument* arg = &*p; |
a3ad94ed | 173 | gold_assert(arg->is_file()); |
ead1e424 ILT |
174 | |
175 | Task_token* next_blocker = new Task_token(); | |
176 | next_blocker->add_blocker(); | |
177 | workqueue->queue(new Read_symbols(this->options_, this->input_objects_, | |
178 | this->symtab_, this->layout_, | |
179 | this->dirpath_, arg, input_group, | |
180 | this_blocker, next_blocker)); | |
181 | this_blocker = next_blocker; | |
182 | } | |
183 | ||
184 | const int saw_undefined = this->symtab_->saw_undefined(); | |
f6ce93d6 ILT |
185 | workqueue->queue(new Finish_group(this->options_, |
186 | this->input_objects_, | |
ead1e424 ILT |
187 | this->symtab_, |
188 | this->layout_, | |
189 | input_group, | |
190 | saw_undefined, | |
191 | this_blocker, | |
192 | this->next_blocker_)); | |
193 | } | |
194 | ||
bae7f79e ILT |
195 | // Class Add_symbols. |
196 | ||
197 | Add_symbols::~Add_symbols() | |
198 | { | |
199 | if (this->this_blocker_ != NULL) | |
200 | delete this->this_blocker_; | |
201 | // next_blocker_ is deleted by the task associated with the next | |
202 | // input file. | |
203 | } | |
204 | ||
a2fb1b05 ILT |
205 | // We are blocked by this_blocker_. We block next_blocker_. We also |
206 | // lock the file. | |
bae7f79e ILT |
207 | |
208 | Task::Is_runnable_type | |
209 | Add_symbols::is_runnable(Workqueue*) | |
210 | { | |
211 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
212 | return IS_BLOCKED; | |
a2fb1b05 ILT |
213 | if (this->object_->is_locked()) |
214 | return IS_LOCKED; | |
bae7f79e ILT |
215 | return IS_RUNNABLE; |
216 | } | |
217 | ||
a2fb1b05 ILT |
218 | class Add_symbols::Add_symbols_locker : public Task_locker |
219 | { | |
220 | public: | |
221 | Add_symbols_locker(Task_token& token, Workqueue* workqueue, | |
222 | Object* object) | |
223 | : blocker_(token, workqueue), objlock_(*object) | |
224 | { } | |
225 | ||
226 | private: | |
227 | Task_locker_block blocker_; | |
228 | Task_locker_obj<Object> objlock_; | |
229 | }; | |
230 | ||
bae7f79e ILT |
231 | Task_locker* |
232 | Add_symbols::locks(Workqueue* workqueue) | |
233 | { | |
a2fb1b05 ILT |
234 | return new Add_symbols_locker(*this->next_blocker_, workqueue, |
235 | this->object_); | |
bae7f79e ILT |
236 | } |
237 | ||
ead1e424 ILT |
238 | // Add the symbols in the object to the symbol table. |
239 | ||
bae7f79e ILT |
240 | void |
241 | Add_symbols::run(Workqueue*) | |
242 | { | |
008db82e ILT |
243 | if (!this->input_objects_->add_object(this->object_)) |
244 | { | |
245 | // FIXME: We need to close the descriptor here. | |
246 | delete this->object_; | |
247 | } | |
248 | else | |
249 | { | |
250 | this->object_->layout(this->options_, this->symtab_, this->layout_, | |
251 | this->sd_); | |
252 | this->object_->add_symbols(this->symtab_, this->sd_); | |
253 | } | |
12e14209 ILT |
254 | delete this->sd_; |
255 | this->sd_ = NULL; | |
bae7f79e ILT |
256 | } |
257 | ||
ead1e424 ILT |
258 | // Class Finish_group. |
259 | ||
260 | Finish_group::~Finish_group() | |
261 | { | |
262 | if (this->this_blocker_ != NULL) | |
263 | delete this->this_blocker_; | |
264 | // next_blocker_ is deleted by the task associated with the next | |
265 | // input file following the group. | |
266 | } | |
267 | ||
268 | // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_. | |
269 | ||
270 | Task::Is_runnable_type | |
271 | Finish_group::is_runnable(Workqueue*) | |
272 | { | |
273 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
274 | return IS_BLOCKED; | |
275 | return IS_RUNNABLE; | |
276 | } | |
277 | ||
278 | Task_locker* | |
279 | Finish_group::locks(Workqueue* workqueue) | |
280 | { | |
281 | return new Task_locker_block(*this->next_blocker_, workqueue); | |
282 | } | |
283 | ||
284 | // Loop over the archives until there are no new undefined symbols. | |
285 | ||
286 | void | |
287 | Finish_group::run(Workqueue*) | |
288 | { | |
289 | int saw_undefined = this->saw_undefined_; | |
290 | while (saw_undefined != this->symtab_->saw_undefined()) | |
291 | { | |
292 | saw_undefined = this->symtab_->saw_undefined(); | |
293 | ||
294 | for (Input_group::const_iterator p = this->input_group_->begin(); | |
295 | p != this->input_group_->end(); | |
296 | ++p) | |
297 | { | |
298 | Task_lock_obj<Archive> tl(**p); | |
299 | ||
f6ce93d6 | 300 | (*p)->add_symbols(this->options_, this->symtab_, this->layout_, |
ead1e424 ILT |
301 | this->input_objects_); |
302 | } | |
303 | } | |
304 | ||
305 | // Delete all the archives now that we no longer need them. | |
306 | for (Input_group::const_iterator p = this->input_group_->begin(); | |
307 | p != this->input_group_->end(); | |
308 | ++p) | |
309 | delete *p; | |
310 | delete this->input_group_; | |
311 | } | |
312 | ||
bae7f79e | 313 | } // End namespace gold. |