1 /* test_plugin.c -- simple linker plugin test
3 Copyright (C) 2008-2018 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_get_symbols get_symbols_v3
= NULL
;
61 static ld_plugin_add_input_file add_input_file
= NULL
;
62 static ld_plugin_message message
= NULL
;
63 static ld_plugin_get_input_file get_input_file
= NULL
;
64 static ld_plugin_release_input_file release_input_file
= NULL
;
65 static ld_plugin_get_input_section_count get_input_section_count
= NULL
;
66 static ld_plugin_get_input_section_type get_input_section_type
= NULL
;
67 static ld_plugin_get_input_section_name get_input_section_name
= NULL
;
68 static ld_plugin_get_input_section_contents get_input_section_contents
= NULL
;
69 static ld_plugin_update_section_order update_section_order
= NULL
;
70 static ld_plugin_allow_section_ordering allow_section_ordering
= NULL
;
71 static ld_plugin_get_wrap_symbols get_wrap_symbols
= NULL
;
75 static const char *opts
[MAXOPTS
];
78 enum ld_plugin_status
onload(struct ld_plugin_tv
*tv
);
79 enum ld_plugin_status
claim_file_hook(const struct ld_plugin_input_file
*file
,
81 enum ld_plugin_status
all_symbols_read_hook(void);
82 enum ld_plugin_status
cleanup_hook(void);
84 static void parse_readelf_line(char*, struct sym_info
*);
87 onload(struct ld_plugin_tv
*tv
)
89 struct ld_plugin_tv
*entry
;
94 for (entry
= tv
; entry
->tv_tag
!= LDPT_NULL
; ++entry
)
96 switch (entry
->tv_tag
)
98 case LDPT_API_VERSION
:
99 api_version
= entry
->tv_u
.tv_val
;
101 case LDPT_GOLD_VERSION
:
102 gold_version
= entry
->tv_u
.tv_val
;
104 case LDPT_LINKER_OUTPUT
:
108 opts
[nopts
++] = entry
->tv_u
.tv_string
;
110 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
111 register_claim_file_hook
= entry
->tv_u
.tv_register_claim_file
;
113 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
114 register_all_symbols_read_hook
=
115 entry
->tv_u
.tv_register_all_symbols_read
;
117 case LDPT_REGISTER_CLEANUP_HOOK
:
118 register_cleanup_hook
= entry
->tv_u
.tv_register_cleanup
;
120 case LDPT_ADD_SYMBOLS
:
121 add_symbols
= entry
->tv_u
.tv_add_symbols
;
123 case LDPT_GET_SYMBOLS
:
124 get_symbols
= entry
->tv_u
.tv_get_symbols
;
126 case LDPT_GET_SYMBOLS_V2
:
127 get_symbols_v2
= entry
->tv_u
.tv_get_symbols
;
129 case LDPT_GET_SYMBOLS_V3
:
130 get_symbols_v3
= entry
->tv_u
.tv_get_symbols
;
132 case LDPT_ADD_INPUT_FILE
:
133 add_input_file
= entry
->tv_u
.tv_add_input_file
;
136 message
= entry
->tv_u
.tv_message
;
138 case LDPT_GET_INPUT_FILE
:
139 get_input_file
= entry
->tv_u
.tv_get_input_file
;
141 case LDPT_RELEASE_INPUT_FILE
:
142 release_input_file
= entry
->tv_u
.tv_release_input_file
;
144 case LDPT_GET_INPUT_SECTION_COUNT
:
145 get_input_section_count
= *entry
->tv_u
.tv_get_input_section_count
;
147 case LDPT_GET_INPUT_SECTION_TYPE
:
148 get_input_section_type
= *entry
->tv_u
.tv_get_input_section_type
;
150 case LDPT_GET_INPUT_SECTION_NAME
:
151 get_input_section_name
= *entry
->tv_u
.tv_get_input_section_name
;
153 case LDPT_GET_INPUT_SECTION_CONTENTS
:
154 get_input_section_contents
= *entry
->tv_u
.tv_get_input_section_contents
;
156 case LDPT_UPDATE_SECTION_ORDER
:
157 update_section_order
= *entry
->tv_u
.tv_update_section_order
;
159 case LDPT_ALLOW_SECTION_ORDERING
:
160 allow_section_ordering
= *entry
->tv_u
.tv_allow_section_ordering
;
162 case LDPT_GET_WRAP_SYMBOLS
:
163 get_wrap_symbols
= *entry
->tv_u
.tv_get_wrap_symbols
;
172 fprintf(stderr
, "tv_message interface missing\n");
176 if (register_claim_file_hook
== NULL
)
178 fprintf(stderr
, "tv_register_claim_file_hook interface missing\n");
182 if (register_all_symbols_read_hook
== NULL
)
184 fprintf(stderr
, "tv_register_all_symbols_read_hook interface missing\n");
188 if (register_cleanup_hook
== NULL
)
190 fprintf(stderr
, "tv_register_cleanup_hook interface missing\n");
194 (*message
)(LDPL_INFO
, "API version: %d", api_version
);
195 (*message
)(LDPL_INFO
, "gold version: %d", gold_version
);
197 for (i
= 0; i
< nopts
; ++i
)
198 (*message
)(LDPL_INFO
, "option: %s", opts
[i
]);
200 if ((*register_claim_file_hook
)(claim_file_hook
) != LDPS_OK
)
202 (*message
)(LDPL_ERROR
, "error registering claim file hook");
206 if ((*register_all_symbols_read_hook
)(all_symbols_read_hook
) != LDPS_OK
)
208 (*message
)(LDPL_ERROR
, "error registering all symbols read hook");
212 if ((*register_cleanup_hook
)(cleanup_hook
) != LDPS_OK
)
214 (*message
)(LDPL_ERROR
, "error registering cleanup hook");
218 if (get_input_section_count
== NULL
)
220 fprintf(stderr
, "tv_get_input_section_count interface missing\n");
224 if (get_input_section_type
== NULL
)
226 fprintf(stderr
, "tv_get_input_section_type interface missing\n");
230 if (get_input_section_name
== NULL
)
232 fprintf(stderr
, "tv_get_input_section_name interface missing\n");
236 if (get_input_section_contents
== NULL
)
238 fprintf(stderr
, "tv_get_input_section_contents interface missing\n");
242 if (update_section_order
== NULL
)
244 fprintf(stderr
, "tv_update_section_order interface missing\n");
248 if (allow_section_ordering
== NULL
)
250 fprintf(stderr
, "tv_allow_section_ordering interface missing\n");
254 if (get_wrap_symbols
== NULL
)
256 fprintf(stderr
, "tv_get_wrap_symbols interface missing\n");
261 const char **wrap_symbols
;
263 if (get_wrap_symbols(&count
, &wrap_symbols
) == LDPS_OK
)
265 (*message
)(LDPL_INFO
, "Number of wrap symbols = %lu", count
);
266 for (; count
> 0; --count
)
267 (*message
)(LDPL_INFO
, "Wrap symbol %s", wrap_symbols
[count
- 1]);
271 fprintf(stderr
, "tv_get_wrap_symbols interface call failed\n");
279 enum ld_plugin_status
280 claim_file_hook (const struct ld_plugin_input_file
* file
, int* claimed
)
285 struct claimed_file
* claimed_file
;
286 struct ld_plugin_symbol
* syms
;
290 struct sym_info info
;
296 int irfile_was_opened
= 0;
299 (*message
)(LDPL_INFO
,
300 "%s: claim file hook called (offset = %ld, size = %ld)",
301 file
->name
, (long)file
->offset
, (long)file
->filesize
);
303 /* Look for matching syms file for an archive member. */
304 if (file
->offset
== 0)
305 snprintf(syms_name
, sizeof(syms_name
), "%s.syms", file
->name
);
307 snprintf(syms_name
, sizeof(syms_name
), "%s-%d.syms",
308 file
->name
, (int)file
->offset
);
309 irfile
= fopen(syms_name
, "r");
312 irfile_was_opened
= 1;
313 end_offset
= 1 << 20;
316 /* Otherwise, see if the file itself is a syms file. */
317 if (!irfile_was_opened
)
319 irfile
= fdopen(file
->fd
, "r");
320 (void)fseek(irfile
, file
->offset
, SEEK_SET
);
321 end_offset
= file
->offset
+ file
->filesize
;
324 /* Look for the beginning of output from readelf -s. */
325 len
= fread(buf
, 1, 13, irfile
);
326 if (len
< 13 || strncmp(buf
, "\nSymbol table", 13) != 0)
329 /* Skip the two header lines. */
330 (void) fgets(buf
, sizeof(buf
), irfile
);
331 (void) fgets(buf
, sizeof(buf
), irfile
);
333 if (add_symbols
== NULL
)
335 fprintf(stderr
, "tv_add_symbols interface missing\n");
339 /* Parse the output from readelf. The columns are:
340 Index Value Size Type Binding Visibility Section Name. */
341 syms
= (struct ld_plugin_symbol
*)malloc(sizeof(struct ld_plugin_symbol
) * 8);
345 while (ftell(irfile
) < end_offset
346 && fgets(buf
, sizeof(buf
), irfile
) != NULL
)
348 parse_readelf_line(buf
, &info
);
350 /* Ignore local symbols. */
351 if (strncmp(info
.bind
, "LOCAL", 5) == 0)
354 weak
= strncmp(info
.bind
, "WEAK", 4) == 0;
355 if (strncmp(info
.sect
, "UND", 3) == 0)
356 def
= weak
? LDPK_WEAKUNDEF
: LDPK_UNDEF
;
357 else if (strncmp(info
.sect
, "COM", 3) == 0)
360 def
= weak
? LDPK_WEAKDEF
: LDPK_DEF
;
362 if (strncmp(info
.vis
, "INTERNAL", 8) == 0)
364 else if (strncmp(info
.vis
, "HIDDEN", 6) == 0)
366 else if (strncmp(info
.vis
, "PROTECTED", 9) == 0)
367 vis
= LDPV_PROTECTED
;
371 /* If the symbol is listed in the options list, special-case
372 it as a comdat symbol. */
374 for (i
= 0; i
< nopts
; ++i
)
376 if (info
.name
!= NULL
&& strcmp(info
.name
, opts
[i
]) == 0)
383 if (nsyms
>= maxsyms
)
385 syms
= (struct ld_plugin_symbol
*)
386 realloc(syms
, sizeof(struct ld_plugin_symbol
) * maxsyms
* 2);
392 if (info
.name
== NULL
)
393 syms
[nsyms
].name
= NULL
;
396 len
= strlen(info
.name
);
397 syms
[nsyms
].name
= malloc(len
+ 1);
398 strncpy(syms
[nsyms
].name
, info
.name
, len
+ 1);
400 syms
[nsyms
].version
= NULL
;
401 syms
[nsyms
].def
= def
;
402 syms
[nsyms
].visibility
= vis
;
403 syms
[nsyms
].size
= info
.size
;
404 syms
[nsyms
].comdat_key
= is_comdat
? syms
[nsyms
].name
: NULL
;
405 syms
[nsyms
].resolution
= LDPR_UNKNOWN
;
409 claimed_file
= (struct claimed_file
*) malloc(sizeof(struct claimed_file
));
410 if (claimed_file
== NULL
)
413 claimed_file
->name
= file
->name
;
414 claimed_file
->handle
= file
->handle
;
415 claimed_file
->nsyms
= nsyms
;
416 claimed_file
->syms
= syms
;
417 claimed_file
->next
= NULL
;
418 if (last_claimed_file
== NULL
)
419 first_claimed_file
= claimed_file
;
421 last_claimed_file
->next
= claimed_file
;
422 last_claimed_file
= claimed_file
;
424 (*message
)(LDPL_INFO
, "%s: claiming file, adding %d symbols",
428 (*add_symbols
)(file
->handle
, nsyms
, syms
);
431 if (irfile_was_opened
)
436 enum ld_plugin_status
437 all_symbols_read_hook(void)
441 struct claimed_file
* claimed_file
;
442 struct ld_plugin_input_file file
;
445 struct sym_info info
;
449 const char* filename
;
451 (*message
)(LDPL_INFO
, "all symbols read hook called");
453 if (get_symbols_v3
== NULL
)
455 fprintf(stderr
, "tv_get_symbols (v3) interface missing\n");
459 for (claimed_file
= first_claimed_file
;
460 claimed_file
!= NULL
;
461 claimed_file
= claimed_file
->next
)
463 enum ld_plugin_status status
= (*get_symbols_v3
)(
464 claimed_file
->handle
, claimed_file
->nsyms
, claimed_file
->syms
);
465 if (status
== LDPS_NO_SYMS
)
467 (*message
)(LDPL_INFO
, "%s: no symbols", claimed_file
->name
);
471 for (i
= 0; i
< claimed_file
->nsyms
; ++i
)
473 switch (claimed_file
->syms
[i
].resolution
)
481 case LDPR_PREVAILING_DEF
:
482 res
= "PREVAILING_DEF_REG";
484 case LDPR_PREVAILING_DEF_IRONLY
:
485 res
= "PREVAILING_DEF_IRONLY";
487 case LDPR_PREVAILING_DEF_IRONLY_EXP
:
488 res
= "PREVAILING_DEF_IRONLY_EXP";
490 case LDPR_PREEMPTED_REG
:
491 res
= "PREEMPTED_REG";
493 case LDPR_PREEMPTED_IR
:
494 res
= "PREEMPTED_IR";
496 case LDPR_RESOLVED_IR
:
499 case LDPR_RESOLVED_EXEC
:
500 res
= "RESOLVED_EXEC";
502 case LDPR_RESOLVED_DYN
:
503 res
= "RESOLVED_DYN";
509 (*message
)(LDPL_INFO
, "%s: %s: %s", claimed_file
->name
,
510 claimed_file
->syms
[i
].name
, res
);
514 if (add_input_file
== NULL
)
516 fprintf(stderr
, "tv_add_input_file interface missing\n");
519 if (get_input_file
== NULL
)
521 fprintf(stderr
, "tv_get_input_file interface missing\n");
524 if (release_input_file
== NULL
)
526 fprintf(stderr
, "tv_release_input_file interface missing\n");
530 for (claimed_file
= first_claimed_file
;
531 claimed_file
!= NULL
;
532 claimed_file
= claimed_file
->next
)
534 int irfile_was_opened
= 0;
537 (*get_input_file
) (claimed_file
->handle
, &file
);
539 if (file
.offset
== 0)
540 snprintf(syms_name
, sizeof(syms_name
), "%s.syms", file
.name
);
542 snprintf(syms_name
, sizeof(syms_name
), "%s-%d.syms",
543 file
.name
, (int)file
.offset
);
544 irfile
= fopen(syms_name
, "r");
547 irfile_was_opened
= 1;
548 end_offset
= 1 << 20;
551 if (!irfile_was_opened
)
553 irfile
= fdopen(file
.fd
, "r");
554 (void)fseek(irfile
, file
.offset
, SEEK_SET
);
555 end_offset
= file
.offset
+ file
.filesize
;
558 /* Look for the beginning of output from readelf -s. */
559 len
= fread(buf
, 1, 13, irfile
);
560 if (len
< 13 || strncmp(buf
, "\nSymbol table", 13) != 0)
562 fprintf(stderr
, "%s: can't re-read original input file\n",
567 /* Skip the two header lines. */
568 (void) fgets(buf
, sizeof(buf
), irfile
);
569 (void) fgets(buf
, sizeof(buf
), irfile
);
572 while (ftell(irfile
) < end_offset
573 && fgets(buf
, sizeof(buf
), irfile
) != NULL
)
575 parse_readelf_line(buf
, &info
);
577 /* Look for file name. */
578 if (strncmp(info
.type
, "FILE", 4) == 0)
580 len
= strlen(info
.name
);
582 strncpy(p
, info
.name
, len
+ 1);
588 if (irfile_was_opened
)
591 (*release_input_file
) (claimed_file
->handle
);
593 if (filename
== NULL
)
594 filename
= claimed_file
->name
;
596 if (claimed_file
->nsyms
== 0)
599 if (strlen(filename
) >= sizeof(buf
))
601 (*message
)(LDPL_FATAL
, "%s: filename too long", filename
);
604 strcpy(buf
, filename
);
605 p
= strrchr(buf
, '.');
607 || (strcmp(p
, ".syms") != 0
608 && strcmp(p
, ".c") != 0
609 && strcmp(p
, ".cc") != 0))
611 (*message
)(LDPL_FATAL
, "%s: filename has unknown suffix",
617 (*message
)(LDPL_INFO
, "%s: adding new input file", buf
);
618 (*add_input_file
)(buf
);
624 enum ld_plugin_status
627 (*message
)(LDPL_INFO
, "cleanup hook called");
632 parse_readelf_line(char* p
, struct sym_info
* info
)
639 p
+= strcspn(p
, " ");
643 p
+= strcspn(p
, " ");
647 info
->size
= atoi(p
);
648 p
+= strcspn(p
, " ");
653 p
+= strcspn(p
, " ");
658 p
+= strcspn(p
, " ");
661 /* Visibility field. */
663 p
+= strcspn(p
, " ");
669 p
+= strcspn(p
, "]");
670 p
+= strspn(p
, "] ");
675 p
+= strcspn(p
, " ");
679 /* FIXME: Look for version. */
683 else if (p
[len
-1] == '\n')