1 /* test_plugin.c -- simple linker plugin test
3 Copyright (C) 2008-2016 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
;
74 static const char *opts
[MAXOPTS
];
77 enum ld_plugin_status
onload(struct ld_plugin_tv
*tv
);
78 enum ld_plugin_status
claim_file_hook(const struct ld_plugin_input_file
*file
,
80 enum ld_plugin_status
all_symbols_read_hook(void);
81 enum ld_plugin_status
cleanup_hook(void);
83 static void parse_readelf_line(char*, struct sym_info
*);
86 onload(struct ld_plugin_tv
*tv
)
88 struct ld_plugin_tv
*entry
;
93 for (entry
= tv
; entry
->tv_tag
!= LDPT_NULL
; ++entry
)
95 switch (entry
->tv_tag
)
97 case LDPT_API_VERSION
:
98 api_version
= entry
->tv_u
.tv_val
;
100 case LDPT_GOLD_VERSION
:
101 gold_version
= entry
->tv_u
.tv_val
;
103 case LDPT_LINKER_OUTPUT
:
107 opts
[nopts
++] = entry
->tv_u
.tv_string
;
109 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
110 register_claim_file_hook
= entry
->tv_u
.tv_register_claim_file
;
112 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
113 register_all_symbols_read_hook
=
114 entry
->tv_u
.tv_register_all_symbols_read
;
116 case LDPT_REGISTER_CLEANUP_HOOK
:
117 register_cleanup_hook
= entry
->tv_u
.tv_register_cleanup
;
119 case LDPT_ADD_SYMBOLS
:
120 add_symbols
= entry
->tv_u
.tv_add_symbols
;
122 case LDPT_GET_SYMBOLS
:
123 get_symbols
= entry
->tv_u
.tv_get_symbols
;
125 case LDPT_GET_SYMBOLS_V2
:
126 get_symbols_v2
= entry
->tv_u
.tv_get_symbols
;
128 case LDPT_GET_SYMBOLS_V3
:
129 get_symbols_v3
= entry
->tv_u
.tv_get_symbols
;
131 case LDPT_ADD_INPUT_FILE
:
132 add_input_file
= entry
->tv_u
.tv_add_input_file
;
135 message
= entry
->tv_u
.tv_message
;
137 case LDPT_GET_INPUT_FILE
:
138 get_input_file
= entry
->tv_u
.tv_get_input_file
;
140 case LDPT_RELEASE_INPUT_FILE
:
141 release_input_file
= entry
->tv_u
.tv_release_input_file
;
143 case LDPT_GET_INPUT_SECTION_COUNT
:
144 get_input_section_count
= *entry
->tv_u
.tv_get_input_section_count
;
146 case LDPT_GET_INPUT_SECTION_TYPE
:
147 get_input_section_type
= *entry
->tv_u
.tv_get_input_section_type
;
149 case LDPT_GET_INPUT_SECTION_NAME
:
150 get_input_section_name
= *entry
->tv_u
.tv_get_input_section_name
;
152 case LDPT_GET_INPUT_SECTION_CONTENTS
:
153 get_input_section_contents
= *entry
->tv_u
.tv_get_input_section_contents
;
155 case LDPT_UPDATE_SECTION_ORDER
:
156 update_section_order
= *entry
->tv_u
.tv_update_section_order
;
158 case LDPT_ALLOW_SECTION_ORDERING
:
159 allow_section_ordering
= *entry
->tv_u
.tv_allow_section_ordering
;
168 fprintf(stderr
, "tv_message interface missing\n");
172 if (register_claim_file_hook
== NULL
)
174 fprintf(stderr
, "tv_register_claim_file_hook interface missing\n");
178 if (register_all_symbols_read_hook
== NULL
)
180 fprintf(stderr
, "tv_register_all_symbols_read_hook interface missing\n");
184 if (register_cleanup_hook
== NULL
)
186 fprintf(stderr
, "tv_register_cleanup_hook interface missing\n");
190 (*message
)(LDPL_INFO
, "API version: %d", api_version
);
191 (*message
)(LDPL_INFO
, "gold version: %d", gold_version
);
193 for (i
= 0; i
< nopts
; ++i
)
194 (*message
)(LDPL_INFO
, "option: %s", opts
[i
]);
196 if ((*register_claim_file_hook
)(claim_file_hook
) != LDPS_OK
)
198 (*message
)(LDPL_ERROR
, "error registering claim file hook");
202 if ((*register_all_symbols_read_hook
)(all_symbols_read_hook
) != LDPS_OK
)
204 (*message
)(LDPL_ERROR
, "error registering all symbols read hook");
208 if ((*register_cleanup_hook
)(cleanup_hook
) != LDPS_OK
)
210 (*message
)(LDPL_ERROR
, "error registering cleanup hook");
214 if (get_input_section_count
== NULL
)
216 fprintf(stderr
, "tv_get_input_section_count interface missing\n");
220 if (get_input_section_type
== NULL
)
222 fprintf(stderr
, "tv_get_input_section_type interface missing\n");
226 if (get_input_section_name
== NULL
)
228 fprintf(stderr
, "tv_get_input_section_name interface missing\n");
232 if (get_input_section_contents
== NULL
)
234 fprintf(stderr
, "tv_get_input_section_contents interface missing\n");
238 if (update_section_order
== NULL
)
240 fprintf(stderr
, "tv_update_section_order interface missing\n");
244 if (allow_section_ordering
== NULL
)
246 fprintf(stderr
, "tv_allow_section_ordering interface missing\n");
253 enum ld_plugin_status
254 claim_file_hook (const struct ld_plugin_input_file
* file
, int* claimed
)
259 struct claimed_file
* claimed_file
;
260 struct ld_plugin_symbol
* syms
;
264 struct sym_info info
;
270 int irfile_was_opened
= 0;
273 (*message
)(LDPL_INFO
,
274 "%s: claim file hook called (offset = %ld, size = %ld)",
275 file
->name
, (long)file
->offset
, (long)file
->filesize
);
277 /* Look for matching syms file for an archive member. */
278 if (file
->offset
== 0)
279 snprintf(syms_name
, sizeof(syms_name
), "%s.syms", file
->name
);
281 snprintf(syms_name
, sizeof(syms_name
), "%s-%d.syms",
282 file
->name
, (int)file
->offset
);
283 irfile
= fopen(syms_name
, "r");
286 irfile_was_opened
= 1;
287 end_offset
= 1 << 20;
290 /* Otherwise, see if the file itself is a syms file. */
291 if (!irfile_was_opened
)
293 irfile
= fdopen(file
->fd
, "r");
294 (void)fseek(irfile
, file
->offset
, SEEK_SET
);
295 end_offset
= file
->offset
+ file
->filesize
;
298 /* Look for the beginning of output from readelf -s. */
299 len
= fread(buf
, 1, 13, irfile
);
300 if (len
< 13 || strncmp(buf
, "\nSymbol table", 13) != 0)
303 /* Skip the two header lines. */
304 (void) fgets(buf
, sizeof(buf
), irfile
);
305 (void) fgets(buf
, sizeof(buf
), irfile
);
307 if (add_symbols
== NULL
)
309 fprintf(stderr
, "tv_add_symbols interface missing\n");
313 /* Parse the output from readelf. The columns are:
314 Index Value Size Type Binding Visibility Section Name. */
315 syms
= (struct ld_plugin_symbol
*)malloc(sizeof(struct ld_plugin_symbol
) * 8);
319 while (ftell(irfile
) < end_offset
320 && fgets(buf
, sizeof(buf
), irfile
) != NULL
)
322 parse_readelf_line(buf
, &info
);
324 /* Ignore local symbols. */
325 if (strncmp(info
.bind
, "LOCAL", 5) == 0)
328 weak
= strncmp(info
.bind
, "WEAK", 4) == 0;
329 if (strncmp(info
.sect
, "UND", 3) == 0)
330 def
= weak
? LDPK_WEAKUNDEF
: LDPK_UNDEF
;
331 else if (strncmp(info
.sect
, "COM", 3) == 0)
334 def
= weak
? LDPK_WEAKDEF
: LDPK_DEF
;
336 if (strncmp(info
.vis
, "INTERNAL", 8) == 0)
338 else if (strncmp(info
.vis
, "HIDDEN", 6) == 0)
340 else if (strncmp(info
.vis
, "PROTECTED", 9) == 0)
341 vis
= LDPV_PROTECTED
;
345 /* If the symbol is listed in the options list, special-case
346 it as a comdat symbol. */
348 for (i
= 0; i
< nopts
; ++i
)
350 if (info
.name
!= NULL
&& strcmp(info
.name
, opts
[i
]) == 0)
357 if (nsyms
>= maxsyms
)
359 syms
= (struct ld_plugin_symbol
*)
360 realloc(syms
, sizeof(struct ld_plugin_symbol
) * maxsyms
* 2);
366 if (info
.name
== NULL
)
367 syms
[nsyms
].name
= NULL
;
370 len
= strlen(info
.name
);
371 syms
[nsyms
].name
= malloc(len
+ 1);
372 strncpy(syms
[nsyms
].name
, info
.name
, len
+ 1);
374 syms
[nsyms
].version
= NULL
;
375 syms
[nsyms
].def
= def
;
376 syms
[nsyms
].visibility
= vis
;
377 syms
[nsyms
].size
= info
.size
;
378 syms
[nsyms
].comdat_key
= is_comdat
? syms
[nsyms
].name
: NULL
;
379 syms
[nsyms
].resolution
= LDPR_UNKNOWN
;
383 claimed_file
= (struct claimed_file
*) malloc(sizeof(struct claimed_file
));
384 if (claimed_file
== NULL
)
387 claimed_file
->name
= file
->name
;
388 claimed_file
->handle
= file
->handle
;
389 claimed_file
->nsyms
= nsyms
;
390 claimed_file
->syms
= syms
;
391 claimed_file
->next
= NULL
;
392 if (last_claimed_file
== NULL
)
393 first_claimed_file
= claimed_file
;
395 last_claimed_file
->next
= claimed_file
;
396 last_claimed_file
= claimed_file
;
398 (*message
)(LDPL_INFO
, "%s: claiming file, adding %d symbols",
402 (*add_symbols
)(file
->handle
, nsyms
, syms
);
405 if (irfile_was_opened
)
410 enum ld_plugin_status
411 all_symbols_read_hook(void)
415 struct claimed_file
* claimed_file
;
416 struct ld_plugin_input_file file
;
419 struct sym_info info
;
423 const char* filename
;
425 (*message
)(LDPL_INFO
, "all symbols read hook called");
427 if (get_symbols_v3
== NULL
)
429 fprintf(stderr
, "tv_get_symbols (v3) interface missing\n");
433 for (claimed_file
= first_claimed_file
;
434 claimed_file
!= NULL
;
435 claimed_file
= claimed_file
->next
)
437 enum ld_plugin_status status
= (*get_symbols_v3
)(
438 claimed_file
->handle
, claimed_file
->nsyms
, claimed_file
->syms
);
439 if (status
== LDPS_NO_SYMS
)
441 (*message
)(LDPL_INFO
, "%s: no symbols", claimed_file
->name
);
445 for (i
= 0; i
< claimed_file
->nsyms
; ++i
)
447 switch (claimed_file
->syms
[i
].resolution
)
455 case LDPR_PREVAILING_DEF
:
456 res
= "PREVAILING_DEF_REG";
458 case LDPR_PREVAILING_DEF_IRONLY
:
459 res
= "PREVAILING_DEF_IRONLY";
461 case LDPR_PREVAILING_DEF_IRONLY_EXP
:
462 res
= "PREVAILING_DEF_IRONLY_EXP";
464 case LDPR_PREEMPTED_REG
:
465 res
= "PREEMPTED_REG";
467 case LDPR_PREEMPTED_IR
:
468 res
= "PREEMPTED_IR";
470 case LDPR_RESOLVED_IR
:
473 case LDPR_RESOLVED_EXEC
:
474 res
= "RESOLVED_EXEC";
476 case LDPR_RESOLVED_DYN
:
477 res
= "RESOLVED_DYN";
483 (*message
)(LDPL_INFO
, "%s: %s: %s", claimed_file
->name
,
484 claimed_file
->syms
[i
].name
, res
);
488 if (add_input_file
== NULL
)
490 fprintf(stderr
, "tv_add_input_file interface missing\n");
493 if (get_input_file
== NULL
)
495 fprintf(stderr
, "tv_get_input_file interface missing\n");
498 if (release_input_file
== NULL
)
500 fprintf(stderr
, "tv_release_input_file interface missing\n");
504 for (claimed_file
= first_claimed_file
;
505 claimed_file
!= NULL
;
506 claimed_file
= claimed_file
->next
)
508 int irfile_was_opened
= 0;
511 (*get_input_file
) (claimed_file
->handle
, &file
);
513 if (file
.offset
== 0)
514 snprintf(syms_name
, sizeof(syms_name
), "%s.syms", file
.name
);
516 snprintf(syms_name
, sizeof(syms_name
), "%s-%d.syms",
517 file
.name
, (int)file
.offset
);
518 irfile
= fopen(syms_name
, "r");
521 irfile_was_opened
= 1;
522 end_offset
= 1 << 20;
525 if (!irfile_was_opened
)
527 irfile
= fdopen(file
.fd
, "r");
528 (void)fseek(irfile
, file
.offset
, SEEK_SET
);
529 end_offset
= file
.offset
+ file
.filesize
;
532 /* Look for the beginning of output from readelf -s. */
533 len
= fread(buf
, 1, 13, irfile
);
534 if (len
< 13 || strncmp(buf
, "\nSymbol table", 13) != 0)
536 fprintf(stderr
, "%s: can't re-read original input file\n",
541 /* Skip the two header lines. */
542 (void) fgets(buf
, sizeof(buf
), irfile
);
543 (void) fgets(buf
, sizeof(buf
), irfile
);
546 while (ftell(irfile
) < end_offset
547 && fgets(buf
, sizeof(buf
), irfile
) != NULL
)
549 parse_readelf_line(buf
, &info
);
551 /* Look for file name. */
552 if (strncmp(info
.type
, "FILE", 4) == 0)
554 len
= strlen(info
.name
);
556 strncpy(p
, info
.name
, len
+ 1);
562 if (irfile_was_opened
)
565 (*release_input_file
) (claimed_file
->handle
);
567 if (filename
== NULL
)
568 filename
= claimed_file
->name
;
570 if (claimed_file
->nsyms
== 0)
573 if (strlen(filename
) >= sizeof(buf
))
575 (*message
)(LDPL_FATAL
, "%s: filename too long", filename
);
578 strcpy(buf
, filename
);
579 p
= strrchr(buf
, '.');
581 || (strcmp(p
, ".syms") != 0
582 && strcmp(p
, ".c") != 0
583 && strcmp(p
, ".cc") != 0))
585 (*message
)(LDPL_FATAL
, "%s: filename has unknown suffix",
591 (*message
)(LDPL_INFO
, "%s: adding new input file", buf
);
592 (*add_input_file
)(buf
);
598 enum ld_plugin_status
601 (*message
)(LDPL_INFO
, "cleanup hook called");
606 parse_readelf_line(char* p
, struct sym_info
* info
)
613 p
+= strcspn(p
, " ");
617 p
+= strcspn(p
, " ");
621 info
->size
= atoi(p
);
622 p
+= strcspn(p
, " ");
627 p
+= strcspn(p
, " ");
632 p
+= strcspn(p
, " ");
635 /* Visibility field. */
637 p
+= strcspn(p
, " ");
643 p
+= strcspn(p
, "]");
644 p
+= strspn(p
, "] ");
649 p
+= strcspn(p
, " ");
653 /* FIXME: Look for version. */
657 else if (p
[len
-1] == '\n')