1 /* test_plugin.c -- simple linker plugin test
3 Copyright 2008, 2009 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_add_input_file add_input_file
= NULL
;
60 static ld_plugin_message message
= NULL
;
61 static ld_plugin_get_input_file get_input_file
= NULL
;
62 static ld_plugin_release_input_file release_input_file
= NULL
;
63 static ld_plugin_get_input_section_count get_input_section_count
= NULL
;
64 static ld_plugin_get_input_section_type get_input_section_type
= NULL
;
65 static ld_plugin_get_input_section_name get_input_section_name
= NULL
;
66 static ld_plugin_get_input_section_contents get_input_section_contents
= NULL
;
67 static ld_plugin_update_section_order update_section_order
= NULL
;
68 static ld_plugin_allow_section_ordering allow_section_ordering
= NULL
;
72 static const char *opts
[MAXOPTS
];
75 enum ld_plugin_status
onload(struct ld_plugin_tv
*tv
);
76 enum ld_plugin_status
claim_file_hook(const struct ld_plugin_input_file
*file
,
78 enum ld_plugin_status
all_symbols_read_hook(void);
79 enum ld_plugin_status
cleanup_hook(void);
81 static void parse_readelf_line(char*, struct sym_info
*);
84 onload(struct ld_plugin_tv
*tv
)
86 struct ld_plugin_tv
*entry
;
91 for (entry
= tv
; entry
->tv_tag
!= LDPT_NULL
; ++entry
)
93 switch (entry
->tv_tag
)
95 case LDPT_API_VERSION
:
96 api_version
= entry
->tv_u
.tv_val
;
98 case LDPT_GOLD_VERSION
:
99 gold_version
= entry
->tv_u
.tv_val
;
101 case LDPT_LINKER_OUTPUT
:
105 opts
[nopts
++] = entry
->tv_u
.tv_string
;
107 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
108 register_claim_file_hook
= entry
->tv_u
.tv_register_claim_file
;
110 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
111 register_all_symbols_read_hook
=
112 entry
->tv_u
.tv_register_all_symbols_read
;
114 case LDPT_REGISTER_CLEANUP_HOOK
:
115 register_cleanup_hook
= entry
->tv_u
.tv_register_cleanup
;
117 case LDPT_ADD_SYMBOLS
:
118 add_symbols
= entry
->tv_u
.tv_add_symbols
;
120 case LDPT_GET_SYMBOLS
:
121 get_symbols
= entry
->tv_u
.tv_get_symbols
;
123 case LDPT_ADD_INPUT_FILE
:
124 add_input_file
= entry
->tv_u
.tv_add_input_file
;
127 message
= entry
->tv_u
.tv_message
;
129 case LDPT_GET_INPUT_FILE
:
130 get_input_file
= entry
->tv_u
.tv_get_input_file
;
132 case LDPT_RELEASE_INPUT_FILE
:
133 release_input_file
= entry
->tv_u
.tv_release_input_file
;
135 case LDPT_GET_INPUT_SECTION_COUNT
:
136 get_input_section_count
= *entry
->tv_u
.tv_get_input_section_count
;
138 case LDPT_GET_INPUT_SECTION_TYPE
:
139 get_input_section_type
= *entry
->tv_u
.tv_get_input_section_type
;
141 case LDPT_GET_INPUT_SECTION_NAME
:
142 get_input_section_name
= *entry
->tv_u
.tv_get_input_section_name
;
144 case LDPT_GET_INPUT_SECTION_CONTENTS
:
145 get_input_section_contents
= *entry
->tv_u
.tv_get_input_section_contents
;
147 case LDPT_UPDATE_SECTION_ORDER
:
148 update_section_order
= *entry
->tv_u
.tv_update_section_order
;
150 case LDPT_ALLOW_SECTION_ORDERING
:
151 allow_section_ordering
= *entry
->tv_u
.tv_allow_section_ordering
;
160 fprintf(stderr
, "tv_message interface missing\n");
164 if (register_claim_file_hook
== NULL
)
166 fprintf(stderr
, "tv_register_claim_file_hook interface missing\n");
170 if (register_all_symbols_read_hook
== NULL
)
172 fprintf(stderr
, "tv_register_all_symbols_read_hook interface missing\n");
176 if (register_cleanup_hook
== NULL
)
178 fprintf(stderr
, "tv_register_cleanup_hook interface missing\n");
182 (*message
)(LDPL_INFO
, "API version: %d", api_version
);
183 (*message
)(LDPL_INFO
, "gold version: %d", gold_version
);
185 for (i
= 0; i
< nopts
; ++i
)
186 (*message
)(LDPL_INFO
, "option: %s", opts
[i
]);
188 if ((*register_claim_file_hook
)(claim_file_hook
) != LDPS_OK
)
190 (*message
)(LDPL_ERROR
, "error registering claim file hook");
194 if ((*register_all_symbols_read_hook
)(all_symbols_read_hook
) != LDPS_OK
)
196 (*message
)(LDPL_ERROR
, "error registering all symbols read hook");
200 if ((*register_cleanup_hook
)(cleanup_hook
) != LDPS_OK
)
202 (*message
)(LDPL_ERROR
, "error registering cleanup hook");
206 if (get_input_section_count
== NULL
)
208 fprintf(stderr
, "tv_get_input_section_count interface missing\n");
212 if (get_input_section_type
== NULL
)
214 fprintf(stderr
, "tv_get_input_section_type interface missing\n");
218 if (get_input_section_name
== NULL
)
220 fprintf(stderr
, "tv_get_input_section_name interface missing\n");
224 if (get_input_section_contents
== NULL
)
226 fprintf(stderr
, "tv_get_input_section_contents interface missing\n");
230 if (update_section_order
== NULL
)
232 fprintf(stderr
, "tv_update_section_order interface missing\n");
236 if (allow_section_ordering
== NULL
)
238 fprintf(stderr
, "tv_allow_section_ordering interface missing\n");
245 enum ld_plugin_status
246 claim_file_hook (const struct ld_plugin_input_file
* file
, int* claimed
)
251 struct claimed_file
* claimed_file
;
252 struct ld_plugin_symbol
* syms
;
256 struct sym_info info
;
263 (*message
)(LDPL_INFO
,
264 "%s: claim file hook called (offset = %ld, size = %ld)",
265 file
->name
, (long)file
->offset
, (long)file
->filesize
);
267 /* Look for the beginning of output from readelf -s. */
268 irfile
= fdopen(file
->fd
, "r");
269 (void)fseek(irfile
, file
->offset
, SEEK_SET
);
270 end_offset
= file
->offset
+ file
->filesize
;
271 len
= fread(buf
, 1, 13, irfile
);
272 if (len
< 13 || strncmp(buf
, "\nSymbol table", 13) != 0)
275 /* Skip the two header lines. */
276 (void) fgets(buf
, sizeof(buf
), irfile
);
277 (void) fgets(buf
, sizeof(buf
), irfile
);
279 if (add_symbols
== NULL
)
281 fprintf(stderr
, "tv_add_symbols interface missing\n");
285 /* Parse the output from readelf. The columns are:
286 Index Value Size Type Binding Visibility Section Name. */
287 syms
= (struct ld_plugin_symbol
*)malloc(sizeof(struct ld_plugin_symbol
) * 8);
291 while (ftell(irfile
) < end_offset
292 && fgets(buf
, sizeof(buf
), irfile
) != NULL
)
294 parse_readelf_line(buf
, &info
);
296 /* Ignore local symbols. */
297 if (strncmp(info
.bind
, "LOCAL", 5) == 0)
300 weak
= strncmp(info
.bind
, "WEAK", 4) == 0;
301 if (strncmp(info
.sect
, "UND", 3) == 0)
302 def
= weak
? LDPK_WEAKUNDEF
: LDPK_UNDEF
;
303 else if (strncmp(info
.sect
, "COM", 3) == 0)
306 def
= weak
? LDPK_WEAKDEF
: LDPK_DEF
;
308 if (strncmp(info
.vis
, "INTERNAL", 8) == 0)
310 else if (strncmp(info
.vis
, "HIDDEN", 6) == 0)
312 else if (strncmp(info
.vis
, "PROTECTED", 9) == 0)
313 vis
= LDPV_PROTECTED
;
317 /* If the symbol is listed in the options list, special-case
318 it as a comdat symbol. */
320 for (i
= 0; i
< nopts
; ++i
)
322 if (info
.name
!= NULL
&& strcmp(info
.name
, opts
[i
]) == 0)
329 if (nsyms
>= maxsyms
)
331 syms
= (struct ld_plugin_symbol
*)
332 realloc(syms
, sizeof(struct ld_plugin_symbol
) * maxsyms
* 2);
338 if (info
.name
== NULL
)
339 syms
[nsyms
].name
= NULL
;
342 len
= strlen(info
.name
);
343 syms
[nsyms
].name
= malloc(len
+ 1);
344 strncpy(syms
[nsyms
].name
, info
.name
, len
+ 1);
346 syms
[nsyms
].version
= NULL
;
347 syms
[nsyms
].def
= def
;
348 syms
[nsyms
].visibility
= vis
;
349 syms
[nsyms
].size
= info
.size
;
350 syms
[nsyms
].comdat_key
= is_comdat
? syms
[nsyms
].name
: NULL
;
351 syms
[nsyms
].resolution
= LDPR_UNKNOWN
;
355 claimed_file
= (struct claimed_file
*) malloc(sizeof(struct claimed_file
));
356 if (claimed_file
== NULL
)
359 claimed_file
->name
= file
->name
;
360 claimed_file
->handle
= file
->handle
;
361 claimed_file
->nsyms
= nsyms
;
362 claimed_file
->syms
= syms
;
363 claimed_file
->next
= NULL
;
364 if (last_claimed_file
== NULL
)
365 first_claimed_file
= claimed_file
;
367 last_claimed_file
->next
= claimed_file
;
368 last_claimed_file
= claimed_file
;
370 (*message
)(LDPL_INFO
, "%s: claiming file, adding %d symbols",
374 (*add_symbols
)(file
->handle
, nsyms
, syms
);
380 enum ld_plugin_status
381 all_symbols_read_hook(void)
385 struct claimed_file
* claimed_file
;
386 struct ld_plugin_input_file file
;
389 struct sym_info info
;
393 const char* filename
;
395 (*message
)(LDPL_INFO
, "all symbols read hook called");
397 if (get_symbols
== NULL
)
399 fprintf(stderr
, "tv_get_symbols interface missing\n");
403 for (claimed_file
= first_claimed_file
;
404 claimed_file
!= NULL
;
405 claimed_file
= claimed_file
->next
)
407 (*get_symbols
)(claimed_file
->handle
, claimed_file
->nsyms
,
410 for (i
= 0; i
< claimed_file
->nsyms
; ++i
)
412 switch (claimed_file
->syms
[i
].resolution
)
420 case LDPR_PREVAILING_DEF
:
421 res
= "PREVAILING_DEF_REG";
423 case LDPR_PREVAILING_DEF_IRONLY
:
424 res
= "PREVAILING_DEF_IRONLY";
426 case LDPR_PREEMPTED_REG
:
427 res
= "PREEMPTED_REG";
429 case LDPR_PREEMPTED_IR
:
430 res
= "PREEMPTED_IR";
432 case LDPR_RESOLVED_IR
:
435 case LDPR_RESOLVED_EXEC
:
436 res
= "RESOLVED_EXEC";
438 case LDPR_RESOLVED_DYN
:
439 res
= "RESOLVED_DYN";
445 (*message
)(LDPL_INFO
, "%s: %s: %s", claimed_file
->name
,
446 claimed_file
->syms
[i
].name
, res
);
450 if (add_input_file
== NULL
)
452 fprintf(stderr
, "tv_add_input_file interface missing\n");
455 if (get_input_file
== NULL
)
457 fprintf(stderr
, "tv_get_input_file interface missing\n");
460 if (release_input_file
== NULL
)
462 fprintf(stderr
, "tv_release_input_file interface missing\n");
466 for (claimed_file
= first_claimed_file
;
467 claimed_file
!= NULL
;
468 claimed_file
= claimed_file
->next
)
470 (*get_input_file
) (claimed_file
->handle
, &file
);
472 /* Look for the beginning of output from readelf -s. */
473 irfile
= fdopen(file
.fd
, "r");
474 (void)fseek(irfile
, file
.offset
, SEEK_SET
);
475 end_offset
= file
.offset
+ file
.filesize
;
476 len
= fread(buf
, 1, 13, irfile
);
477 if (len
< 13 || strncmp(buf
, "\nSymbol table", 13) != 0)
479 fprintf(stderr
, "%s: can't re-read original input file\n",
484 /* Skip the two header lines. */
485 (void) fgets(buf
, sizeof(buf
), irfile
);
486 (void) fgets(buf
, sizeof(buf
), irfile
);
489 while (ftell(irfile
) < end_offset
490 && fgets(buf
, sizeof(buf
), irfile
) != NULL
)
492 parse_readelf_line(buf
, &info
);
494 /* Look for file name. */
495 if (strncmp(info
.type
, "FILE", 4) == 0)
497 len
= strlen(info
.name
);
499 strncpy(p
, info
.name
, len
+ 1);
505 (*release_input_file
) (claimed_file
->handle
);
507 if (filename
== NULL
)
508 filename
= claimed_file
->name
;
510 if (claimed_file
->nsyms
== 0)
513 if (strlen(filename
) >= sizeof(buf
))
515 (*message
)(LDPL_FATAL
, "%s: filename too long", filename
);
518 strcpy(buf
, filename
);
519 p
= strrchr(buf
, '.');
521 || (strcmp(p
, ".syms") != 0
522 && strcmp(p
, ".c") != 0
523 && strcmp(p
, ".cc") != 0))
525 (*message
)(LDPL_FATAL
, "%s: filename has unknown suffix",
531 (*message
)(LDPL_INFO
, "%s: adding new input file", buf
);
532 (*add_input_file
)(buf
);
538 enum ld_plugin_status
541 (*message
)(LDPL_INFO
, "cleanup hook called");
546 parse_readelf_line(char* p
, struct sym_info
* info
)
553 p
+= strcspn(p
, " ");
557 p
+= strcspn(p
, " ");
561 info
->size
= atoi(p
);
562 p
+= strcspn(p
, " ");
567 p
+= strcspn(p
, " ");
572 p
+= strcspn(p
, " ");
575 /* Visibility field. */
577 p
+= strcspn(p
, " ");
582 p
+= strcspn(p
, " ");
586 /* FIXME: Look for version. */
590 else if (p
[len
-1] == '\n')