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