1 /* test_plugin.c -- simple linker plugin test
3 Copyright (C) 2008-2014 Free Software Foundation, Inc.
4 Written by Cary Coutant <ccoutant@google.com>.
6 This file is part of gold.
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.
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.
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. */
30 #include "plugin-api.h"
37 struct ld_plugin_symbol* syms;
38 struct claimed_file* next;
51 static struct claimed_file* first_claimed_file = NULL;
52 static struct claimed_file* last_claimed_file = NULL;
54 static ld_plugin_register_claim_file register_claim_file_hook = NULL;
55 static ld_plugin_register_all_symbols_read register_all_symbols_read_hook = NULL;
56 static ld_plugin_register_cleanup register_cleanup_hook = NULL;
57 static ld_plugin_add_symbols add_symbols = NULL;
58 static ld_plugin_get_symbols get_symbols = NULL;
59 static ld_plugin_get_symbols get_symbols_v2 = NULL;
60 static ld_plugin_add_input_file add_input_file = NULL;
61 static ld_plugin_message message = NULL;
62 static ld_plugin_get_input_file get_input_file = NULL;
63 static ld_plugin_release_input_file release_input_file = NULL;
64 static ld_plugin_get_input_section_count get_input_section_count = NULL;
65 static ld_plugin_get_input_section_type get_input_section_type = NULL;
66 static ld_plugin_get_input_section_name get_input_section_name = NULL;
67 static ld_plugin_get_input_section_contents get_input_section_contents = NULL;
68 static ld_plugin_update_section_order update_section_order = NULL;
69 static ld_plugin_allow_section_ordering allow_section_ordering = NULL;
73 static const char *opts[MAXOPTS];
76 enum ld_plugin_status onload(struct ld_plugin_tv *tv);
77 enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file,
79 enum ld_plugin_status all_symbols_read_hook(void);
80 enum ld_plugin_status cleanup_hook(void);
82 static void parse_readelf_line(char*, struct sym_info*);
85 onload(struct ld_plugin_tv *tv)
87 struct ld_plugin_tv *entry;
92 for (entry = tv; entry->tv_tag != LDPT_NULL; ++entry)
94 switch (entry->tv_tag)
96 case LDPT_API_VERSION:
97 api_version = entry->tv_u.tv_val;
99 case LDPT_GOLD_VERSION:
100 gold_version = entry->tv_u.tv_val;
102 case LDPT_LINKER_OUTPUT:
106 opts[nopts++] = entry->tv_u.tv_string;
108 case LDPT_REGISTER_CLAIM_FILE_HOOK:
109 register_claim_file_hook = entry->tv_u.tv_register_claim_file;
111 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
112 register_all_symbols_read_hook =
113 entry->tv_u.tv_register_all_symbols_read;
115 case LDPT_REGISTER_CLEANUP_HOOK:
116 register_cleanup_hook = entry->tv_u.tv_register_cleanup;
118 case LDPT_ADD_SYMBOLS:
119 add_symbols = entry->tv_u.tv_add_symbols;
121 case LDPT_GET_SYMBOLS:
122 get_symbols = entry->tv_u.tv_get_symbols;
124 case LDPT_GET_SYMBOLS_V2:
125 get_symbols_v2 = entry->tv_u.tv_get_symbols;
127 case LDPT_ADD_INPUT_FILE:
128 add_input_file = entry->tv_u.tv_add_input_file;
131 message = entry->tv_u.tv_message;
133 case LDPT_GET_INPUT_FILE:
134 get_input_file = entry->tv_u.tv_get_input_file;
136 case LDPT_RELEASE_INPUT_FILE:
137 release_input_file = entry->tv_u.tv_release_input_file;
139 case LDPT_GET_INPUT_SECTION_COUNT:
140 get_input_section_count = *entry->tv_u.tv_get_input_section_count;
142 case LDPT_GET_INPUT_SECTION_TYPE:
143 get_input_section_type = *entry->tv_u.tv_get_input_section_type;
145 case LDPT_GET_INPUT_SECTION_NAME:
146 get_input_section_name = *entry->tv_u.tv_get_input_section_name;
148 case LDPT_GET_INPUT_SECTION_CONTENTS:
149 get_input_section_contents = *entry->tv_u.tv_get_input_section_contents;
151 case LDPT_UPDATE_SECTION_ORDER:
152 update_section_order = *entry->tv_u.tv_update_section_order;
154 case LDPT_ALLOW_SECTION_ORDERING:
155 allow_section_ordering = *entry->tv_u.tv_allow_section_ordering;
164 fprintf(stderr, "tv_message interface missing\n");
168 if (register_claim_file_hook == NULL)
170 fprintf(stderr, "tv_register_claim_file_hook interface missing\n");
174 if (register_all_symbols_read_hook == NULL)
176 fprintf(stderr, "tv_register_all_symbols_read_hook interface missing\n");
180 if (register_cleanup_hook == NULL)
182 fprintf(stderr, "tv_register_cleanup_hook interface missing\n");
186 (*message)(LDPL_INFO, "API version: %d", api_version);
187 (*message)(LDPL_INFO, "gold version: %d", gold_version);
189 for (i = 0; i < nopts; ++i)
190 (*message)(LDPL_INFO, "option: %s", opts[i]);
192 if ((*register_claim_file_hook)(claim_file_hook) != LDPS_OK)
194 (*message)(LDPL_ERROR, "error registering claim file hook");
198 if ((*register_all_symbols_read_hook)(all_symbols_read_hook) != LDPS_OK)
200 (*message)(LDPL_ERROR, "error registering all symbols read hook");
204 if ((*register_cleanup_hook)(cleanup_hook) != LDPS_OK)
206 (*message)(LDPL_ERROR, "error registering cleanup hook");
210 if (get_input_section_count == NULL)
212 fprintf(stderr, "tv_get_input_section_count interface missing\n");
216 if (get_input_section_type == NULL)
218 fprintf(stderr, "tv_get_input_section_type interface missing\n");
222 if (get_input_section_name == NULL)
224 fprintf(stderr, "tv_get_input_section_name interface missing\n");
228 if (get_input_section_contents == NULL)
230 fprintf(stderr, "tv_get_input_section_contents interface missing\n");
234 if (update_section_order == NULL)
236 fprintf(stderr, "tv_update_section_order interface missing\n");
240 if (allow_section_ordering == NULL)
242 fprintf(stderr, "tv_allow_section_ordering interface missing\n");
249 enum ld_plugin_status
250 claim_file_hook (const struct ld_plugin_input_file* file, int* claimed)
255 struct claimed_file* claimed_file;
256 struct ld_plugin_symbol* syms;
260 struct sym_info info;
267 (*message)(LDPL_INFO,
268 "%s: claim file hook called (offset = %ld, size = %ld)",
269 file->name, (long)file->offset, (long)file->filesize);
271 /* Look for the beginning of output from readelf -s. */
272 irfile = fdopen(file->fd, "r");
273 (void)fseek(irfile, file->offset, SEEK_SET);
274 end_offset = file->offset + file->filesize;
275 len = fread(buf, 1, 13, irfile);
276 if (len < 13 || strncmp(buf, "\nSymbol table", 13) != 0)
279 /* Skip the two header lines. */
280 (void) fgets(buf, sizeof(buf), irfile);
281 (void) fgets(buf, sizeof(buf), irfile);
283 if (add_symbols == NULL)
285 fprintf(stderr, "tv_add_symbols interface missing\n");
289 /* Parse the output from readelf. The columns are:
290 Index Value Size Type Binding Visibility Section Name. */
291 syms = (struct ld_plugin_symbol*)malloc(sizeof(struct ld_plugin_symbol) * 8);
295 while (ftell(irfile) < end_offset
296 && fgets(buf, sizeof(buf), irfile) != NULL)
298 parse_readelf_line(buf, &info);
300 /* Ignore local symbols. */
301 if (strncmp(info.bind, "LOCAL", 5) == 0)
304 weak = strncmp(info.bind, "WEAK", 4) == 0;
305 if (strncmp(info.sect, "UND", 3) == 0)
306 def = weak ? LDPK_WEAKUNDEF : LDPK_UNDEF;
307 else if (strncmp(info.sect, "COM", 3) == 0)
310 def = weak ? LDPK_WEAKDEF : LDPK_DEF;
312 if (strncmp(info.vis, "INTERNAL", 8) == 0)
314 else if (strncmp(info.vis, "HIDDEN", 6) == 0)
316 else if (strncmp(info.vis, "PROTECTED", 9) == 0)
317 vis = LDPV_PROTECTED;
321 /* If the symbol is listed in the options list, special-case
322 it as a comdat symbol. */
324 for (i = 0; i < nopts; ++i)
326 if (info.name != NULL && strcmp(info.name, opts[i]) == 0)
333 if (nsyms >= maxsyms)
335 syms = (struct ld_plugin_symbol*)
336 realloc(syms, sizeof(struct ld_plugin_symbol) * maxsyms * 2);
342 if (info.name == NULL)
343 syms[nsyms].name = NULL;
346 len = strlen(info.name);
347 syms[nsyms].name = malloc(len + 1);
348 strncpy(syms[nsyms].name, info.name, len + 1);
350 syms[nsyms].version = NULL;
351 syms[nsyms].def = def;
352 syms[nsyms].visibility = vis;
353 syms[nsyms].size = info.size;
354 syms[nsyms].comdat_key = is_comdat ? syms[nsyms].name : NULL;
355 syms[nsyms].resolution = LDPR_UNKNOWN;
359 claimed_file = (struct claimed_file*) malloc(sizeof(struct claimed_file));
360 if (claimed_file == NULL)
363 claimed_file->name = file->name;
364 claimed_file->handle = file->handle;
365 claimed_file->nsyms = nsyms;
366 claimed_file->syms = syms;
367 claimed_file->next = NULL;
368 if (last_claimed_file == NULL)
369 first_claimed_file = claimed_file;
371 last_claimed_file->next = claimed_file;
372 last_claimed_file = claimed_file;
374 (*message)(LDPL_INFO, "%s: claiming file, adding %d symbols",
378 (*add_symbols)(file->handle, nsyms, syms);
384 enum ld_plugin_status
385 all_symbols_read_hook(void)
389 struct claimed_file* claimed_file;
390 struct ld_plugin_input_file file;
393 struct sym_info info;
397 const char* filename;
399 (*message)(LDPL_INFO, "all symbols read hook called");
401 if (get_symbols_v2 == NULL)
403 fprintf(stderr, "tv_get_symbols (v2) interface missing\n");
407 for (claimed_file = first_claimed_file;
408 claimed_file != NULL;
409 claimed_file = claimed_file->next)
411 (*get_symbols_v2)(claimed_file->handle, claimed_file->nsyms,
414 for (i = 0; i < claimed_file->nsyms; ++i)
416 switch (claimed_file->syms[i].resolution)
424 case LDPR_PREVAILING_DEF:
425 res = "PREVAILING_DEF_REG";
427 case LDPR_PREVAILING_DEF_IRONLY:
428 res = "PREVAILING_DEF_IRONLY";
430 case LDPR_PREVAILING_DEF_IRONLY_EXP:
431 res = "PREVAILING_DEF_IRONLY_EXP";
433 case LDPR_PREEMPTED_REG:
434 res = "PREEMPTED_REG";
436 case LDPR_PREEMPTED_IR:
437 res = "PREEMPTED_IR";
439 case LDPR_RESOLVED_IR:
442 case LDPR_RESOLVED_EXEC:
443 res = "RESOLVED_EXEC";
445 case LDPR_RESOLVED_DYN:
446 res = "RESOLVED_DYN";
452 (*message)(LDPL_INFO, "%s: %s: %s", claimed_file->name,
453 claimed_file->syms[i].name, res);
457 if (add_input_file == NULL)
459 fprintf(stderr, "tv_add_input_file interface missing\n");
462 if (get_input_file == NULL)
464 fprintf(stderr, "tv_get_input_file interface missing\n");
467 if (release_input_file == NULL)
469 fprintf(stderr, "tv_release_input_file interface missing\n");
473 for (claimed_file = first_claimed_file;
474 claimed_file != NULL;
475 claimed_file = claimed_file->next)
477 (*get_input_file) (claimed_file->handle, &file);
479 /* Look for the beginning of output from readelf -s. */
480 irfile = fdopen(file.fd, "r");
481 (void)fseek(irfile, file.offset, SEEK_SET);
482 end_offset = file.offset + file.filesize;
483 len = fread(buf, 1, 13, irfile);
484 if (len < 13 || strncmp(buf, "\nSymbol table", 13) != 0)
486 fprintf(stderr, "%s: can't re-read original input file\n",
491 /* Skip the two header lines. */
492 (void) fgets(buf, sizeof(buf), irfile);
493 (void) fgets(buf, sizeof(buf), irfile);
496 while (ftell(irfile) < end_offset
497 && fgets(buf, sizeof(buf), irfile) != NULL)
499 parse_readelf_line(buf, &info);
501 /* Look for file name. */
502 if (strncmp(info.type, "FILE", 4) == 0)
504 len = strlen(info.name);
506 strncpy(p, info.name, len + 1);
512 (*release_input_file) (claimed_file->handle);
514 if (filename == NULL)
515 filename = claimed_file->name;
517 if (claimed_file->nsyms == 0)
520 if (strlen(filename) >= sizeof(buf))
522 (*message)(LDPL_FATAL, "%s: filename too long", filename);
525 strcpy(buf, filename);
526 p = strrchr(buf, '.');
528 || (strcmp(p, ".syms") != 0
529 && strcmp(p, ".c") != 0
530 && strcmp(p, ".cc") != 0))
532 (*message)(LDPL_FATAL, "%s: filename has unknown suffix",
538 (*message)(LDPL_INFO, "%s: adding new input file", buf);
539 (*add_input_file)(buf);
545 enum ld_plugin_status
548 (*message)(LDPL_INFO, "cleanup hook called");
553 parse_readelf_line(char* p, struct sym_info* info)
560 p += strcspn(p, " ");
564 p += strcspn(p, " ");
568 info->size = atoi(p);
569 p += strcspn(p, " ");
574 p += strcspn(p, " ");
579 p += strcspn(p, " ");
582 /* Visibility field. */
584 p += strcspn(p, " ");
590 p += strcspn(p, "]");
591 p += strspn(p, "] ");
596 p += strcspn(p, " ");
600 /* FIXME: Look for version. */
604 else if (p[len-1] == '\n')