1 /* Test plugin for the GNU linker.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "plugin-api.h"
24 /* For ARRAY_SIZE macro only - we don't link the library itself. */
25 #include "libiberty.h"
27 extern enum ld_plugin_status onload (struct ld_plugin_tv *tv);
28 static enum ld_plugin_status onclaim_file (const struct ld_plugin_input_file *file,
30 static enum ld_plugin_status onall_symbols_read (void);
31 static enum ld_plugin_status oncleanup (void);
33 /* Helper for calling plugin api message function. */
34 #define TV_MESSAGE if (tv_message) (*tv_message)
36 /* Struct for recording files to claim / files claimed. */
37 typedef struct claim_file
39 struct claim_file *next;
40 struct ld_plugin_input_file file;
42 struct ld_plugin_symbol *symbols;
47 /* Types of things that can be added at all symbols read time. */
48 typedef enum addfile_enum
55 /* Struct for recording files to add to final link. */
56 typedef struct add_file
58 struct add_file *next;
63 /* Helper macro for defining array of transfer vector tags and names. */
64 #define ADDENTRY(tag) { tag, #tag }
66 /* Struct for looking up human-readable versions of tag names. */
67 typedef struct tag_name
69 enum ld_plugin_tag tag;
73 /* Array of all known tags and their names. */
74 static const tag_name_t tag_names[] =
77 ADDENTRY(LDPT_API_VERSION),
78 ADDENTRY(LDPT_GOLD_VERSION),
79 ADDENTRY(LDPT_LINKER_OUTPUT),
80 ADDENTRY(LDPT_OPTION),
81 ADDENTRY(LDPT_REGISTER_CLAIM_FILE_HOOK),
82 ADDENTRY(LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK),
83 ADDENTRY(LDPT_REGISTER_CLEANUP_HOOK),
84 ADDENTRY(LDPT_ADD_SYMBOLS),
85 ADDENTRY(LDPT_GET_SYMBOLS),
86 ADDENTRY(LDPT_GET_SYMBOLS_V2),
87 ADDENTRY(LDPT_ADD_INPUT_FILE),
88 ADDENTRY(LDPT_MESSAGE),
89 ADDENTRY(LDPT_GET_INPUT_FILE),
90 ADDENTRY(LDPT_RELEASE_INPUT_FILE),
91 ADDENTRY(LDPT_ADD_INPUT_LIBRARY),
92 ADDENTRY(LDPT_OUTPUT_NAME),
93 ADDENTRY(LDPT_SET_EXTRA_LIBRARY_PATH),
94 ADDENTRY(LDPT_GNU_LD_VERSION)
97 /* Function pointers to cache hooks passed at onload time. */
98 static ld_plugin_register_claim_file tv_register_claim_file = 0;
99 static ld_plugin_register_all_symbols_read tv_register_all_symbols_read = 0;
100 static ld_plugin_register_cleanup tv_register_cleanup = 0;
101 static ld_plugin_add_symbols tv_add_symbols = 0;
102 static ld_plugin_get_symbols tv_get_symbols = 0;
103 static ld_plugin_get_symbols tv_get_symbols_v2 = 0;
104 static ld_plugin_add_input_file tv_add_input_file = 0;
105 static ld_plugin_message tv_message = 0;
106 static ld_plugin_get_input_file tv_get_input_file = 0;
107 static ld_plugin_release_input_file tv_release_input_file = 0;
108 static ld_plugin_add_input_library tv_add_input_library = 0;
109 static ld_plugin_set_extra_library_path tv_set_extra_library_path = 0;
111 /* Other cached info from the transfer vector. */
112 static enum ld_plugin_output_file_type linker_output;
113 static const char *output_name;
115 /* Behaviour control flags set by plugin options. */
116 static enum ld_plugin_status onload_ret = LDPS_OK;
117 static enum ld_plugin_status claim_file_ret = LDPS_OK;
118 static enum ld_plugin_status all_symbols_read_ret = LDPS_OK;
119 static enum ld_plugin_status cleanup_ret = LDPS_OK;
120 static bfd_boolean register_claimfile_hook = FALSE;
121 static bfd_boolean register_allsymbolsread_hook = FALSE;
122 static bfd_boolean register_cleanup_hook = FALSE;
123 static bfd_boolean dumpresolutions = FALSE;
125 /* The master list of all claimable/claimed files. */
126 static claim_file_t *claimfiles_list = NULL;
128 /* We keep a tail pointer for easy linking on the end. */
129 static claim_file_t **claimfiles_tail_chain_ptr = &claimfiles_list;
131 /* The last claimed file added to the list, for receiving syms. */
132 static claim_file_t *last_claimfile = NULL;
134 /* The master list of all files to add to the final link. */
135 static add_file_t *addfiles_list = NULL;
137 /* We keep a tail pointer for easy linking on the end. */
138 static add_file_t **addfiles_tail_chain_ptr = &addfiles_list;
140 /* Add a new claimfile on the end of the chain. */
141 static enum ld_plugin_status
142 record_claim_file (const char *file)
144 claim_file_t *newfile;
146 newfile = malloc (sizeof *newfile);
149 memset (newfile, 0, sizeof *newfile);
150 /* Only setup for now is remembering the name to look for. */
151 newfile->file.name = file;
152 /* Chain it on the end of the list. */
153 *claimfiles_tail_chain_ptr = newfile;
154 claimfiles_tail_chain_ptr = &newfile->next;
155 /* Record it as active for receiving symbols to register. */
156 last_claimfile = newfile;
160 /* Add a new addfile on the end of the chain. */
161 static enum ld_plugin_status
162 record_add_file (const char *file, addfile_enum_t type)
166 newfile = malloc (sizeof *newfile);
169 newfile->next = NULL;
170 newfile->name = file;
171 newfile->type = type;
172 /* Chain it on the end of the list. */
173 *addfiles_tail_chain_ptr = newfile;
174 addfiles_tail_chain_ptr = &newfile->next;
178 /* Parse a command-line argument string into a symbol definition.
179 Symbol-strings follow the colon-separated format:
180 NAME:VERSION:def:vis:size:COMDATKEY
181 where the fields in capitals are strings and those in lower
182 case are integers. We don't allow to specify a resolution as
183 doing so is not meaningful when calling the add symbols hook. */
184 static enum ld_plugin_status
185 parse_symdefstr (const char *str, struct ld_plugin_symbol *sym)
189 const char *colon1, *colon2, *colon5;
191 /* Locate the colons separating the first two strings. */
192 colon1 = strchr (str, ':');
195 colon2 = strchr (colon1+1, ':');
198 /* Name must not be empty (version may be). */
202 /* The fifth colon and trailing comdat key string are optional,
203 but the intermediate ones must all be present. */
204 colon5 = strchr (colon2+1, ':'); /* Actually only third so far. */
207 colon5 = strchr (colon5+1, ':'); /* Hopefully fourth now. */
210 colon5 = strchr (colon5+1, ':'); /* Optional fifth now. */
212 /* Finally we'll use sscanf to parse the numeric fields, then
213 we'll split out the strings which we need to allocate separate
214 storage for anyway so that we can add nul termination. */
215 n = sscanf (colon2 + 1, "%i:%i:%lli", &sym->def, &sym->visibility, &size);
219 /* Parsed successfully, so allocate strings and fill out fields. */
221 sym->resolution = LDPR_UNKNOWN;
222 sym->name = malloc (colon1 - str + 1);
225 memcpy (sym->name, str, colon1 - str);
226 sym->name[colon1 - str] = '\0';
227 if (colon2 > (colon1 + 1))
229 sym->version = malloc (colon2 - colon1);
232 memcpy (sym->version, colon1 + 1, colon2 - (colon1 + 1));
233 sym->version[colon2 - (colon1 + 1)] = '\0';
237 if (colon5 && colon5[1])
239 sym->comdat_key = malloc (strlen (colon5 + 1) + 1);
240 if (!sym->comdat_key)
242 strcpy (sym->comdat_key, colon5 + 1);
249 /* Record a symbol to be added for the last-added claimfile. */
250 static enum ld_plugin_status
251 record_claimed_file_symbol (const char *symdefstr)
253 struct ld_plugin_symbol sym;
255 /* Can't add symbols except as belonging to claimed files. */
259 /* If string doesn't parse correctly, give an error. */
260 if (parse_symdefstr (symdefstr, &sym) != LDPS_OK)
263 /* Check for enough space, resize array if needed, and add it. */
264 if (last_claimfile->n_syms_allocated == last_claimfile->n_syms_used)
266 int new_n_syms = last_claimfile->n_syms_allocated
267 ? 2 * last_claimfile->n_syms_allocated
269 last_claimfile->symbols = realloc (last_claimfile->symbols,
270 new_n_syms * sizeof *last_claimfile->symbols);
271 if (!last_claimfile->symbols)
273 last_claimfile->n_syms_allocated = new_n_syms;
275 last_claimfile->symbols[last_claimfile->n_syms_used++] = sym;
280 /* Records the status to return from one of the registered hooks. */
281 static enum ld_plugin_status
282 set_ret_val (const char *whichval, enum ld_plugin_status retval)
284 if (!strcmp ("onload", whichval))
286 else if (!strcmp ("claimfile", whichval))
287 claim_file_ret = retval;
288 else if (!strcmp ("allsymbolsread", whichval))
289 all_symbols_read_ret = retval;
290 else if (!strcmp ("cleanup", whichval))
291 cleanup_ret = retval;
297 /* Records hooks which should be registered. */
298 static enum ld_plugin_status
299 set_register_hook (const char *whichhook, bfd_boolean yesno)
301 if (!strcmp ("claimfile", whichhook))
302 register_claimfile_hook = yesno;
303 else if (!strcmp ("allsymbolsread", whichhook))
304 register_allsymbolsread_hook = yesno;
305 else if (!strcmp ("cleanup", whichhook))
306 register_cleanup_hook = yesno;
312 /* Determine type of plugin option and pass to individual parsers. */
313 static enum ld_plugin_status
314 parse_option (const char *opt)
316 if (!strncmp ("fail", opt, 4))
317 return set_ret_val (opt + 4, LDPS_ERR);
318 else if (!strncmp ("pass", opt, 4))
319 return set_ret_val (opt + 4, LDPS_OK);
320 else if (!strncmp ("register", opt, 8))
321 return set_register_hook (opt + 8, TRUE);
322 else if (!strncmp ("noregister", opt, 10))
323 return set_register_hook (opt + 10, FALSE);
324 else if (!strncmp ("claim:", opt, 6))
325 return record_claim_file (opt + 6);
326 else if (!strncmp ("sym:", opt, 4))
327 return record_claimed_file_symbol (opt + 4);
328 else if (!strncmp ("add:", opt, 4))
329 return record_add_file (opt + 4, ADD_FILE);
330 else if (!strncmp ("lib:", opt, 4))
331 return record_add_file (opt + 4, ADD_LIB);
332 else if (!strncmp ("dir:", opt, 4))
333 return record_add_file (opt + 4, ADD_DIR);
334 else if (!strcmp ("dumpresolutions", opt))
335 dumpresolutions = TRUE;
341 /* Output contents of transfer vector array entry in human-readable form. */
343 dump_tv_tag (size_t n, struct ld_plugin_tv *tv)
349 for (tag = 0; tag < ARRAY_SIZE (tag_names); tag++)
350 if (tag_names[tag].tag == tv->tv_tag)
352 sprintf (unknownbuf, "unknown tag #%d", tv->tv_tag);
353 name = (tag < ARRAY_SIZE (tag_names)) ? tag_names[tag].name : unknownbuf;
357 case LDPT_OUTPUT_NAME:
358 TV_MESSAGE (LDPL_INFO, "tv[%d]: %s '%s'", n, name,
361 case LDPT_REGISTER_CLAIM_FILE_HOOK:
362 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
363 case LDPT_REGISTER_CLEANUP_HOOK:
364 case LDPT_ADD_SYMBOLS:
365 case LDPT_GET_SYMBOLS:
366 case LDPT_GET_SYMBOLS_V2:
367 case LDPT_ADD_INPUT_FILE:
369 case LDPT_GET_INPUT_FILE:
370 case LDPT_RELEASE_INPUT_FILE:
371 case LDPT_ADD_INPUT_LIBRARY:
372 case LDPT_SET_EXTRA_LIBRARY_PATH:
373 TV_MESSAGE (LDPL_INFO, "tv[%d]: %s func@0x%p", n, name,
374 (void *)(tv->tv_u.tv_message));
377 case LDPT_API_VERSION:
378 case LDPT_GOLD_VERSION:
379 case LDPT_LINKER_OUTPUT:
380 case LDPT_GNU_LD_VERSION:
382 TV_MESSAGE (LDPL_INFO, "tv[%d]: %s value %W (%d)", n, name,
383 (bfd_vma)tv->tv_u.tv_val, tv->tv_u.tv_val);
388 /* Handle/record information received in a transfer vector entry. */
389 static enum ld_plugin_status
390 parse_tv_tag (struct ld_plugin_tv *tv)
392 #define SETVAR(x) x = tv->tv_u.x
396 return parse_option (tv->tv_u.tv_string);
398 case LDPT_GOLD_VERSION:
399 case LDPT_GNU_LD_VERSION:
400 case LDPT_API_VERSION:
403 case LDPT_OUTPUT_NAME:
404 output_name = tv->tv_u.tv_string;
406 case LDPT_LINKER_OUTPUT:
407 linker_output = tv->tv_u.tv_val;
409 case LDPT_REGISTER_CLAIM_FILE_HOOK:
410 SETVAR(tv_register_claim_file);
412 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
413 SETVAR(tv_register_all_symbols_read);
415 case LDPT_REGISTER_CLEANUP_HOOK:
416 SETVAR(tv_register_cleanup);
418 case LDPT_ADD_SYMBOLS:
419 SETVAR(tv_add_symbols);
421 case LDPT_GET_SYMBOLS:
422 SETVAR(tv_get_symbols);
424 case LDPT_GET_SYMBOLS_V2:
425 tv_get_symbols_v2 = tv->tv_u.tv_get_symbols;
427 case LDPT_ADD_INPUT_FILE:
428 SETVAR(tv_add_input_file);
433 case LDPT_GET_INPUT_FILE:
434 SETVAR(tv_get_input_file);
436 case LDPT_RELEASE_INPUT_FILE:
437 SETVAR(tv_release_input_file);
439 case LDPT_ADD_INPUT_LIBRARY:
440 SETVAR(tv_add_input_library);
442 case LDPT_SET_EXTRA_LIBRARY_PATH:
443 SETVAR(tv_set_extra_library_path);
450 /* Record any useful information in transfer vector entry and display
451 it in human-readable form using the plugin API message() callback. */
452 enum ld_plugin_status
453 parse_and_dump_tv_tag (size_t n, struct ld_plugin_tv *tv)
455 enum ld_plugin_status rv = parse_tv_tag (tv);
460 /* Standard plugin API entry point. */
461 enum ld_plugin_status
462 onload (struct ld_plugin_tv *tv)
465 enum ld_plugin_status rv;
467 /* This plugin does nothing but dump the tv array. It would
468 be an error if this function was called without one. */
472 /* First entry should always be LDPT_MESSAGE, letting us get
473 hold of it easily so we can send output straight away. */
474 if (tv[0].tv_tag == LDPT_MESSAGE)
475 tv_message = tv[0].tv_u.tv_message;
478 TV_MESSAGE (LDPL_INFO, "Hello from testplugin.");
481 if ((rv = parse_and_dump_tv_tag (n++, tv)) != LDPS_OK)
483 while ((tv++)->tv_tag != LDPT_NULL);
485 /* Register hooks only if instructed by options. */
486 if (register_claimfile_hook)
488 if (!tv_register_claim_file)
490 TV_MESSAGE (LDPL_FATAL, "No register_claim_file hook");
494 (*tv_register_claim_file) (onclaim_file);
496 if (register_allsymbolsread_hook)
498 if (!tv_register_all_symbols_read)
500 TV_MESSAGE (LDPL_FATAL, "No register_all_symbols_read hook");
504 (*tv_register_all_symbols_read) (onall_symbols_read);
506 if (register_cleanup_hook)
508 if (!tv_register_cleanup)
510 TV_MESSAGE (LDPL_FATAL, "No register_cleanup hook");
514 (*tv_register_cleanup) (oncleanup);
520 /* Standard plugin API registerable hook. */
521 static enum ld_plugin_status
522 onclaim_file (const struct ld_plugin_input_file *file, int *claimed)
524 /* Let's see if we want to claim this file. */
525 claim_file_t *claimfile = claimfiles_list;
528 if (!strcmp (file->name, claimfile->file.name))
530 claimfile = claimfile->next;
533 /* Inform the user/testsuite. */
534 TV_MESSAGE (LDPL_INFO, "hook called: claim_file %s [@%ld/%ld] %s",
535 file->name, (long)file->offset, (long)file->filesize,
536 claimfile ? "CLAIMED" : "not claimed");
539 /* If we decided to claim it, record that fact, and add any symbols
540 that were defined for it by plugin options. */
541 *claimed = (claimfile != 0);
544 claimfile->claimed = TRUE;
545 claimfile->file = *file;
546 if (claimfile->n_syms_used && !tv_add_symbols)
548 else if (claimfile->n_syms_used)
549 return (*tv_add_symbols) (claimfile->file.handle,
550 claimfile->n_syms_used, claimfile->symbols);
553 return claim_file_ret;
556 /* Standard plugin API registerable hook. */
557 static enum ld_plugin_status
558 onall_symbols_read (void)
560 static const char *resolutions[] =
564 "LDPR_PREVAILING_DEF",
565 "LDPR_PREVAILING_DEF_IRONLY",
566 "LDPR_PREEMPTED_REG",
569 "LDPR_RESOLVED_EXEC",
571 "LDPR_PREVAILING_DEF_IRONLY_EXP",
573 claim_file_t *claimfile = dumpresolutions ? claimfiles_list : NULL;
574 add_file_t *addfile = addfiles_list;
575 TV_MESSAGE (LDPL_INFO, "hook called: all symbols read.");
576 for ( ; claimfile; claimfile = claimfile->next)
578 enum ld_plugin_status rv;
580 if (claimfile->n_syms_used && !tv_get_symbols_v2)
582 else if (!claimfile->n_syms_used)
584 rv = tv_get_symbols_v2 (claimfile->file.handle, claimfile->n_syms_used,
588 for (n = 0; n < claimfile->n_syms_used; n++)
589 TV_MESSAGE (LDPL_INFO, "Sym: '%s%s%s' Resolution: %s",
590 claimfile->symbols[n].name,
591 claimfile->symbols[n].version ? "@" : "",
592 (claimfile->symbols[n].version
593 ? claimfile->symbols[n].version : ""),
594 resolutions[claimfile->symbols[n].resolution]);
596 for ( ; addfile ; addfile = addfile->next)
598 enum ld_plugin_status rv;
599 if (addfile->type == ADD_LIB && tv_add_input_library)
600 rv = (*tv_add_input_library) (addfile->name);
601 else if (addfile->type == ADD_FILE && tv_add_input_file)
602 rv = (*tv_add_input_file) (addfile->name);
603 else if (addfile->type == ADD_DIR && tv_set_extra_library_path)
604 rv = (*tv_set_extra_library_path) (addfile->name);
611 return all_symbols_read_ret;
614 /* Standard plugin API registerable hook. */
615 static enum ld_plugin_status
618 TV_MESSAGE (LDPL_INFO, "hook called: cleanup.");