1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/* test_plugin.c -- simple linker plugin test
Copyright (C) 2008-2018 Free Software Foundation, Inc.
Written by Cary Coutant <ccoutant@google.com>.
This file is part of gold.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "plugin-api.h"
struct claimed_file
{
const char* name;
void* handle;
int nsyms;
struct ld_plugin_symbol* syms;
struct claimed_file* next;
};
struct sym_info
{
int size;
char* type;
char* bind;
char* vis;
char* sect;
char* name;
};
static struct claimed_file* first_claimed_file = NULL;
static struct claimed_file* last_claimed_file = NULL;
static ld_plugin_register_claim_file register_claim_file_hook = NULL;
static ld_plugin_register_all_symbols_read register_all_symbols_read_hook = NULL;
static ld_plugin_register_cleanup register_cleanup_hook = NULL;
static ld_plugin_add_symbols add_symbols = NULL;
static ld_plugin_get_symbols get_symbols = NULL;
static ld_plugin_get_symbols get_symbols_v2 = NULL;
static ld_plugin_get_symbols get_symbols_v3 = NULL;
static ld_plugin_add_input_file add_input_file = NULL;
static ld_plugin_message message = NULL;
static ld_plugin_get_input_file get_input_file = NULL;
static ld_plugin_release_input_file release_input_file = NULL;
static ld_plugin_get_input_section_count get_input_section_count = NULL;
static ld_plugin_get_input_section_type get_input_section_type = NULL;
static ld_plugin_get_input_section_name get_input_section_name = NULL;
static ld_plugin_get_input_section_contents get_input_section_contents = NULL;
static ld_plugin_update_section_order update_section_order = NULL;
static ld_plugin_allow_section_ordering allow_section_ordering = NULL;
static ld_plugin_get_wrap_symbols get_wrap_symbols = NULL;
#define MAXOPTS 10
static const char *opts[MAXOPTS];
static int nopts = 0;
enum ld_plugin_status onload(struct ld_plugin_tv *tv);
enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file,
int *claimed);
enum ld_plugin_status all_symbols_read_hook(void);
enum ld_plugin_status cleanup_hook(void);
static void parse_readelf_line(char*, struct sym_info*);
enum ld_plugin_status
onload(struct ld_plugin_tv *tv)
{
struct ld_plugin_tv *entry;
int api_version = 0;
int gold_version = 0;
int i;
for (entry = tv; entry->tv_tag != LDPT_NULL; ++entry)
{
switch (entry->tv_tag)
{
case LDPT_API_VERSION:
api_version = entry->tv_u.tv_val;
break;
case LDPT_GOLD_VERSION:
gold_version = entry->tv_u.tv_val;
break;
case LDPT_LINKER_OUTPUT:
break;
case LDPT_OPTION:
if (nopts < MAXOPTS)
opts[nopts++] = entry->tv_u.tv_string;
break;
case LDPT_REGISTER_CLAIM_FILE_HOOK:
register_claim_file_hook = entry->tv_u.tv_register_claim_file;
break;
case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
register_all_symbols_read_hook =
entry->tv_u.tv_register_all_symbols_read;
break;
case LDPT_REGISTER_CLEANUP_HOOK:
register_cleanup_hook = entry->tv_u.tv_register_cleanup;
break;
case LDPT_ADD_SYMBOLS:
add_symbols = entry->tv_u.tv_add_symbols;
break;
case LDPT_GET_SYMBOLS:
get_symbols = entry->tv_u.tv_get_symbols;
break;
case LDPT_GET_SYMBOLS_V2:
get_symbols_v2 = entry->tv_u.tv_get_symbols;
break;
case LDPT_GET_SYMBOLS_V3:
get_symbols_v3 = entry->tv_u.tv_get_symbols;
break;
case LDPT_ADD_INPUT_FILE:
add_input_file = entry->tv_u.tv_add_input_file;
break;
case LDPT_MESSAGE:
message = entry->tv_u.tv_message;
break;
case LDPT_GET_INPUT_FILE:
get_input_file = entry->tv_u.tv_get_input_file;
break;
case LDPT_RELEASE_INPUT_FILE:
release_input_file = entry->tv_u.tv_release_input_file;
break;
case LDPT_GET_INPUT_SECTION_COUNT:
get_input_section_count = *entry->tv_u.tv_get_input_section_count;
break;
case LDPT_GET_INPUT_SECTION_TYPE:
get_input_section_type = *entry->tv_u.tv_get_input_section_type;
break;
case LDPT_GET_INPUT_SECTION_NAME:
get_input_section_name = *entry->tv_u.tv_get_input_section_name;
break;
case LDPT_GET_INPUT_SECTION_CONTENTS:
get_input_section_contents = *entry->tv_u.tv_get_input_section_contents;
break;
case LDPT_UPDATE_SECTION_ORDER:
update_section_order = *entry->tv_u.tv_update_section_order;
break;
case LDPT_ALLOW_SECTION_ORDERING:
allow_section_ordering = *entry->tv_u.tv_allow_section_ordering;
break;
case LDPT_GET_WRAP_SYMBOLS:
get_wrap_symbols = *entry->tv_u.tv_get_wrap_symbols;
break;
default:
break;
}
}
if (message == NULL)
{
fprintf(stderr, "tv_message interface missing\n");
return LDPS_ERR;
}
if (register_claim_file_hook == NULL)
{
fprintf(stderr, "tv_register_claim_file_hook interface missing\n");
return LDPS_ERR;
}
if (register_all_symbols_read_hook == NULL)
{
fprintf(stderr, "tv_register_all_symbols_read_hook interface missing\n");
return LDPS_ERR;
}
if (register_cleanup_hook == NULL)
{
fprintf(stderr, "tv_register_cleanup_hook interface missing\n");
return LDPS_ERR;
}
(*message)(LDPL_INFO, "API version: %d", api_version);
(*message)(LDPL_INFO, "gold version: %d", gold_version);
for (i = 0; i < nopts; ++i)
(*message)(LDPL_INFO, "option: %s", opts[i]);
if ((*register_claim_file_hook)(claim_file_hook) != LDPS_OK)
{
(*message)(LDPL_ERROR, "error registering claim file hook");
return LDPS_ERR;
}
if ((*register_all_symbols_read_hook)(all_symbols_read_hook) != LDPS_OK)
{
(*message)(LDPL_ERROR, "error registering all symbols read hook");
return LDPS_ERR;
}
if ((*register_cleanup_hook)(cleanup_hook) != LDPS_OK)
{
(*message)(LDPL_ERROR, "error registering cleanup hook");
return LDPS_ERR;
}
if (get_input_section_count == NULL)
{
fprintf(stderr, "tv_get_input_section_count interface missing\n");
return LDPS_ERR;
}
if (get_input_section_type == NULL)
{
fprintf(stderr, "tv_get_input_section_type interface missing\n");
return LDPS_ERR;
}
if (get_input_section_name == NULL)
{
fprintf(stderr, "tv_get_input_section_name interface missing\n");
return LDPS_ERR;
}
if (get_input_section_contents == NULL)
{
fprintf(stderr, "tv_get_input_section_contents interface missing\n");
return LDPS_ERR;
}
if (update_section_order == NULL)
{
fprintf(stderr, "tv_update_section_order interface missing\n");
return LDPS_ERR;
}
if (allow_section_ordering == NULL)
{
fprintf(stderr, "tv_allow_section_ordering interface missing\n");
return LDPS_ERR;
}
if (get_wrap_symbols == NULL)
{
fprintf(stderr, "tv_get_wrap_symbols interface missing\n");
return LDPS_ERR;
}
else
{
const char **wrap_symbols;
uint64_t count = 0;
if (get_wrap_symbols(&count, &wrap_symbols) == LDPS_OK)
{
(*message)(LDPL_INFO, "Number of wrap symbols = %lu", count);
for (; count > 0; --count)
(*message)(LDPL_INFO, "Wrap symbol %s", wrap_symbols[count - 1]);
}
else
{
fprintf(stderr, "tv_get_wrap_symbols interface call failed\n");
return LDPS_ERR;
}
}
return LDPS_OK;
}
enum ld_plugin_status
claim_file_hook (const struct ld_plugin_input_file* file, int* claimed)
{
int len;
off_t end_offset;
char buf[160];
struct claimed_file* claimed_file;
struct ld_plugin_symbol* syms;
int nsyms = 0;
int maxsyms = 0;
FILE* irfile;
struct sym_info info;
int weak;
int def;
int vis;
int is_comdat;
int i;
int irfile_was_opened = 0;
char syms_name[80];
(*message)(LDPL_INFO,
"%s: claim file hook called (offset = %ld, size = %ld)",
file->name, (long)file->offset, (long)file->filesize);
/* Look for matching syms file for an archive member. */
if (file->offset == 0)
snprintf(syms_name, sizeof(syms_name), "%s.syms", file->name);
else
snprintf(syms_name, sizeof(syms_name), "%s-%d.syms",
file->name, (int)file->offset);
irfile = fopen(syms_name, "r");
if (irfile != NULL)
{
irfile_was_opened = 1;
end_offset = 1 << 20;
}
/* Otherwise, see if the file itself is a syms file. */
if (!irfile_was_opened)
{
irfile = fdopen(file->fd, "r");
(void)fseek(irfile, file->offset, SEEK_SET);
end_offset = file->offset + file->filesize;
}
/* Look for the beginning of output from readelf -s. */
len = fread(buf, 1, 13, irfile);
if (len < 13 || strncmp(buf, "\nSymbol table", 13) != 0)
return LDPS_OK;
/* Skip the two header lines. */
(void) fgets(buf, sizeof(buf), irfile);
(void) fgets(buf, sizeof(buf), irfile);
if (add_symbols == NULL)
{
fprintf(stderr, "tv_add_symbols interface missing\n");
return LDPS_ERR;
}
/* Parse the output from readelf. The columns are:
Index Value Size Type Binding Visibility Section Name. */
syms = (struct ld_plugin_symbol*)malloc(sizeof(struct ld_plugin_symbol) * 8);
if (syms == NULL)
return LDPS_ERR;
maxsyms = 8;
while (ftell(irfile) < end_offset
&& fgets(buf, sizeof(buf), irfile) != NULL)
{
parse_readelf_line(buf, &info);
/* Ignore local symbols. */
if (strncmp(info.bind, "LOCAL", 5) == 0)
continue;
weak = strncmp(info.bind, "WEAK", 4) == 0;
if (strncmp(info.sect, "UND", 3) == 0)
def = weak ? LDPK_WEAKUNDEF : LDPK_UNDEF;
else if (strncmp(info.sect, "COM", 3) == 0)
def = LDPK_COMMON;
else
def = weak ? LDPK_WEAKDEF : LDPK_DEF;
if (strncmp(info.vis, "INTERNAL", 8) == 0)
vis = LDPV_INTERNAL;
else if (strncmp(info.vis, "HIDDEN", 6) == 0)
vis = LDPV_HIDDEN;
else if (strncmp(info.vis, "PROTECTED", 9) == 0)
vis = LDPV_PROTECTED;
else
vis = LDPV_DEFAULT;
/* If the symbol is listed in the options list, special-case
it as a comdat symbol. */
is_comdat = 0;
for (i = 0; i < nopts; ++i)
{
if (info.name != NULL && strcmp(info.name, opts[i]) == 0)
{
is_comdat = 1;
break;
}
}
if (nsyms >= maxsyms)
{
syms = (struct ld_plugin_symbol*)
realloc(syms, sizeof(struct ld_plugin_symbol) * maxsyms * 2);
if (syms == NULL)
return LDPS_ERR;
maxsyms *= 2;
}
if (info.name == NULL)
syms[nsyms].name = NULL;
else
{
len = strlen(info.name);
syms[nsyms].name = malloc(len + 1);
strncpy(syms[nsyms].name, info.name, len + 1);
}
syms[nsyms].version = NULL;
syms[nsyms].def = def;
syms[nsyms].visibility = vis;
syms[nsyms].size = info.size;
syms[nsyms].comdat_key = is_comdat ? syms[nsyms].name : NULL;
syms[nsyms].resolution = LDPR_UNKNOWN;
++nsyms;
}
claimed_file = (struct claimed_file*) malloc(sizeof(struct claimed_file));
if (claimed_file == NULL)
return LDPS_ERR;
claimed_file->name = file->name;
claimed_file->handle = file->handle;
claimed_file->nsyms = nsyms;
claimed_file->syms = syms;
claimed_file->next = NULL;
if (last_claimed_file == NULL)
first_claimed_file = claimed_file;
else
last_claimed_file->next = claimed_file;
last_claimed_file = claimed_file;
(*message)(LDPL_INFO, "%s: claiming file, adding %d symbols",
file->name, nsyms);
if (nsyms > 0)
(*add_symbols)(file->handle, nsyms, syms);
*claimed = 1;
if (irfile_was_opened)
fclose(irfile);
return LDPS_OK;
}
enum ld_plugin_status
all_symbols_read_hook(void)
{
int i;
const char* res;
struct claimed_file* claimed_file;
struct ld_plugin_input_file file;
FILE* irfile;
off_t end_offset;
struct sym_info info;
int len;
char buf[160];
char* p;
const char* filename;
(*message)(LDPL_INFO, "all symbols read hook called");
if (get_symbols_v3 == NULL)
{
fprintf(stderr, "tv_get_symbols (v3) interface missing\n");
return LDPS_ERR;
}
for (claimed_file = first_claimed_file;
claimed_file != NULL;
claimed_file = claimed_file->next)
{
enum ld_plugin_status status = (*get_symbols_v3)(
claimed_file->handle, claimed_file->nsyms, claimed_file->syms);
if (status == LDPS_NO_SYMS)
{
(*message)(LDPL_INFO, "%s: no symbols", claimed_file->name);
continue;
}
for (i = 0; i < claimed_file->nsyms; ++i)
{
switch (claimed_file->syms[i].resolution)
{
case LDPR_UNKNOWN:
res = "UNKNOWN";
break;
case LDPR_UNDEF:
res = "UNDEF";
break;
case LDPR_PREVAILING_DEF:
res = "PREVAILING_DEF_REG";
break;
case LDPR_PREVAILING_DEF_IRONLY:
res = "PREVAILING_DEF_IRONLY";
break;
case LDPR_PREVAILING_DEF_IRONLY_EXP:
res = "PREVAILING_DEF_IRONLY_EXP";
break;
case LDPR_PREEMPTED_REG:
res = "PREEMPTED_REG";
break;
case LDPR_PREEMPTED_IR:
res = "PREEMPTED_IR";
break;
case LDPR_RESOLVED_IR:
res = "RESOLVED_IR";
break;
case LDPR_RESOLVED_EXEC:
res = "RESOLVED_EXEC";
break;
case LDPR_RESOLVED_DYN:
res = "RESOLVED_DYN";
break;
default:
res = "?";
break;
}
(*message)(LDPL_INFO, "%s: %s: %s", claimed_file->name,
claimed_file->syms[i].name, res);
}
}
if (add_input_file == NULL)
{
fprintf(stderr, "tv_add_input_file interface missing\n");
return LDPS_ERR;
}
if (get_input_file == NULL)
{
fprintf(stderr, "tv_get_input_file interface missing\n");
return LDPS_ERR;
}
if (release_input_file == NULL)
{
fprintf(stderr, "tv_release_input_file interface missing\n");
return LDPS_ERR;
}
for (claimed_file = first_claimed_file;
claimed_file != NULL;
claimed_file = claimed_file->next)
{
int irfile_was_opened = 0;
char syms_name[80];
(*get_input_file) (claimed_file->handle, &file);
if (file.offset == 0)
snprintf(syms_name, sizeof(syms_name), "%s.syms", file.name);
else
snprintf(syms_name, sizeof(syms_name), "%s-%d.syms",
file.name, (int)file.offset);
irfile = fopen(syms_name, "r");
if (irfile != NULL)
{
irfile_was_opened = 1;
end_offset = 1 << 20;
}
if (!irfile_was_opened)
{
irfile = fdopen(file.fd, "r");
(void)fseek(irfile, file.offset, SEEK_SET);
end_offset = file.offset + file.filesize;
}
/* Look for the beginning of output from readelf -s. */
len = fread(buf, 1, 13, irfile);
if (len < 13 || strncmp(buf, "\nSymbol table", 13) != 0)
{
fprintf(stderr, "%s: can't re-read original input file\n",
claimed_file->name);
return LDPS_ERR;
}
/* Skip the two header lines. */
(void) fgets(buf, sizeof(buf), irfile);
(void) fgets(buf, sizeof(buf), irfile);
filename = NULL;
while (ftell(irfile) < end_offset
&& fgets(buf, sizeof(buf), irfile) != NULL)
{
parse_readelf_line(buf, &info);
/* Look for file name. */
if (strncmp(info.type, "FILE", 4) == 0)
{
len = strlen(info.name);
p = malloc(len + 1);
strncpy(p, info.name, len + 1);
filename = p;
break;
}
}
if (irfile_was_opened)
fclose(irfile);
(*release_input_file) (claimed_file->handle);
if (filename == NULL)
filename = claimed_file->name;
if (claimed_file->nsyms == 0)
continue;
if (strlen(filename) >= sizeof(buf))
{
(*message)(LDPL_FATAL, "%s: filename too long", filename);
return LDPS_ERR;
}
strcpy(buf, filename);
p = strrchr(buf, '.');
if (p == NULL
|| (strcmp(p, ".syms") != 0
&& strcmp(p, ".c") != 0
&& strcmp(p, ".cc") != 0))
{
(*message)(LDPL_FATAL, "%s: filename has unknown suffix",
filename);
return LDPS_ERR;
}
p[1] = 'o';
p[2] = '\0';
(*message)(LDPL_INFO, "%s: adding new input file", buf);
(*add_input_file)(buf);
}
return LDPS_OK;
}
enum ld_plugin_status
cleanup_hook(void)
{
(*message)(LDPL_INFO, "cleanup hook called");
return LDPS_OK;
}
static void
parse_readelf_line(char* p, struct sym_info* info)
{
int len;
p += strspn(p, " ");
/* Index field. */
p += strcspn(p, " ");
p += strspn(p, " ");
/* Value field. */
p += strcspn(p, " ");
p += strspn(p, " ");
/* Size field. */
info->size = atoi(p);
p += strcspn(p, " ");
p += strspn(p, " ");
/* Type field. */
info->type = p;
p += strcspn(p, " ");
p += strspn(p, " ");
/* Binding field. */
info->bind = p;
p += strcspn(p, " ");
p += strspn(p, " ");
/* Visibility field. */
info->vis = p;
p += strcspn(p, " ");
p += strspn(p, " ");
if (*p == '[')
{
/* Skip st_other. */
p += strcspn(p, "]");
p += strspn(p, "] ");
}
/* Section field. */
info->sect = p;
p += strcspn(p, " ");
p += strspn(p, " ");
/* Name field. */
/* FIXME: Look for version. */
len = strlen(p);
if (len == 0)
p = NULL;
else if (p[len-1] == '\n')
p[--len] = '\0';
info->name = p;
}
|