aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/lto-plugin/lto-symtab.c
blob: fc42a9eb8167c802c2577cf0576a0a58d26158af (plain)
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
/* Program to read the IL symbol table.
   Copyright (C) 2008 Free Software Foundation, Inc.
   Contributed by Rafael Avila de Espindola (espindola@google.com).

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.  */

#include <fcntl.h>
#include <assert.h>
#include <dlfcn.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>

#include "plugin-api.h"
#include "../gcc/lto/common.h"

/* The presence of gelf.h is checked by the toplevel configure script.  */
# include <gelf.h>

static ld_plugin_claim_file_handler claim_file_handler;
static ld_plugin_all_symbols_read_handler all_symbols_read_handler;
static ld_plugin_cleanup_handler cleanup_handler;
static void *plugin_handle;

struct file_handle {
  unsigned nsyms;
  struct ld_plugin_symbol *syms;
};

static struct file_handle **all_file_handles = NULL;
static unsigned int num_file_handles;

/* Write NSYMS symbols from file HANDLE in SYMS. */

static enum ld_plugin_status
get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
{
  unsigned i;
  struct file_handle *h = (struct file_handle *) handle;
  assert (h->nsyms == nsyms);

  for (i = 0; i < nsyms; i++)
    syms[i] = h->syms[i];

  return LDPS_OK;
}

/* Register HANDLER as the callback for notifying the plugin that all symbols
   have been read. */

static enum ld_plugin_status
register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
{
  all_symbols_read_handler = handler;
  return LDPS_OK;
}

/* Register HANDLER as the callback for claiming a file. */

static enum ld_plugin_status
register_claim_file(ld_plugin_claim_file_handler handler)
{
  claim_file_handler = handler;
  return LDPS_OK;
}

/* Register HANDLER as the callback to removing temporary files. */

static enum ld_plugin_status
register_cleanup (ld_plugin_cleanup_handler handler)
{
  cleanup_handler = handler;
  return LDPS_OK;
}

/* For a file identified by HANDLE, add NSYMS symbols from SYMS. */

static enum ld_plugin_status
add_symbols (void *handle, int nsyms,
	     const struct ld_plugin_symbol *syms)
{
  int i;
  struct file_handle *h = (struct file_handle *) handle;
  h->nsyms = nsyms;
  h->syms = calloc (nsyms, sizeof (struct ld_plugin_symbol));
  assert (h->syms);

  for (i = 0; i < nsyms; i++)
    {
      h->syms[i] = syms[i];
      h->syms[i].name = strdup (h->syms[i].name);
      if (h->syms[i].version)
	h->syms[i].version = strdup (h->syms[i].version);
      if (h->syms[i].comdat_key)
	h->syms[i].comdat_key = strdup (h->syms[i].comdat_key);
    }

  return LDPS_OK;
}

struct ld_plugin_tv tv[] = {
  {LDPT_REGISTER_CLAIM_FILE_HOOK,
   {.tv_register_claim_file = register_claim_file}
  },
  {LDPT_ADD_SYMBOLS,
   {.tv_add_symbols = add_symbols}
  },

  {LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
   {.tv_register_all_symbols_read = register_all_symbols_read}
  },
  {LDPT_GET_SYMBOLS,
   {.tv_get_symbols = get_symbols}
  },
  {LDPT_REGISTER_CLEANUP_HOOK,
   {.tv_register_cleanup = register_cleanup}
  },
  {0, {0}}
};

/* Load a plugin from a file named NAME. */

static void
load_plugin (const char *name)
{
  ld_plugin_onload onload;
  plugin_handle = dlopen (name, RTLD_LAZY);

  assert (plugin_handle != NULL);
  onload = dlsym (plugin_handle, "onload");
  assert (onload);
  onload (tv);
  assert (claim_file_handler);
}

/* Send object to the plugin. The file (archive or object) name is NAME.
   FD is an open file descriptor. The object data starts at OFFSET and is
   FILESIZE bytes long. */

static void
register_object (const char *name, int fd, off_t offset, off_t filesize)
{
  int claimed;
  struct ld_plugin_input_file file;
  void *handle;

  num_file_handles++;
  all_file_handles = realloc (all_file_handles, num_file_handles
			      * sizeof (struct file_handle *));
  assert (all_file_handles);

  all_file_handles[num_file_handles - 1] = calloc (1,
						   sizeof (struct file_handle));
  handle = all_file_handles[num_file_handles - 1];
  assert (handle);

  file.name = (char *) name;
  file.fd = fd;
  file.offset = offset;
  file.filesize = filesize;

  file.handle = handle;

  claim_file_handler (&file, &claimed);
}

/* Send file named NAME to the plugin. */

static void
register_file (const char *name)
{
 int fd = open (name, O_RDONLY);
 Elf *elf;

 assert (fd >= 0);

 elf = elf_begin (fd, ELF_C_READ, NULL);
 assert (elf);

 Elf_Kind kind = elf_kind (elf);

 assert (kind == ELF_K_ELF || kind == ELF_K_AR);

 if (kind == ELF_K_AR)
   {
     Elf *member = elf_begin (fd, ELF_C_READ, elf);
     while (member)
       {
	 Elf_Arhdr *h = elf_getarhdr (member);
	 assert (h);

	 if (h->ar_name[0] != '/')
	   {
	     off_t offset = elf_getbase (member);
	     register_object (name, fd, offset, h->ar_size);
	   }

	 Elf_Cmd cmd = elf_next (member);
	 elf_end (member);
	 member = elf_begin (fd, cmd, elf);
       }
   }
 else /* Single File */
   register_object (name, fd, 0, 0);

 elf_end (elf);
}

/* Fake symbol resolution for testing. */

static void
resolve (void)
{
  unsigned j;
  for (j = 0; j < num_file_handles; j++)
    {
      struct file_handle *handle = all_file_handles[j];
      unsigned int nsyms = handle->nsyms;
      struct ld_plugin_symbol *syms = handle->syms;
      unsigned i;
      for (i = 0; i < nsyms; i++)
	{
	  switch (syms[i].def)
	    {
	    case LDPK_DEF:
	    case LDPK_WEAKDEF:
	    case LDPK_COMMON:
	      syms[i].resolution =  LDPR_PREVAILING_DEF;
	      break;
	    case LDPK_UNDEF:
	    case LDPK_WEAKUNDEF:
	      syms[i].resolution =  LDPR_RESOLVED_IR;
	      break;
	    }
	}
    }
}

/* Print all symbol information. */

static void
print (void)
{
  unsigned j;
  for (j = 0; j < num_file_handles; j++)
    {
      struct file_handle *handle = all_file_handles[j];
      unsigned int nsyms = handle->nsyms;
      struct ld_plugin_symbol *syms = handle->syms;
      unsigned i;
      for (i = 0; i < nsyms; i++)
	{
	  printf("name: %s; ", syms[i].name);
	  if (syms[i].version)
	     printf("version: %s;", syms[i].version);
	  else
	    printf("not versioned; ");
	  printf("kind: %s; ", lto_kind_str[syms[i].def]);
	  printf("visibility: %s; ", lto_visibility_str[syms[i].visibility]);
	  printf("size: %" PRId64 "; ", syms[i].size);
	  if (syms[i].comdat_key)
	    printf("comdat_key: %s; ", syms[i].comdat_key);
	  else
	    printf("no comdat_key; ");
	  printf ("resolution: %s\n", lto_resolution_str[syms[i].resolution]);
	}
    }
}

/* Unload the plugin. */

static void
unload_plugin (void)
{
  unsigned err = dlclose (plugin_handle);
  assert (err == 0);
  claim_file_handler = 0;
  all_symbols_read_handler = 0;
}

/* Free all memory allocated by us that hasn't been freed yet. */

static void
free_all (void)
{
  unsigned j;
  for (j = 0; j < num_file_handles; j++)
    {
      struct file_handle *handle = all_file_handles[j];
      unsigned int nsyms = handle->nsyms;
      struct ld_plugin_symbol *syms = handle->syms;
      unsigned i;
      for (i = 0; i < nsyms; i++)
	{
	  free (syms[i].name);
	  syms[i].name = 0;
	  if (syms[i].version)
	    {
	      free (syms[i].version);
	      syms[i].version = 0;
	    }
	  if (syms[i].comdat_key)
	    {
	      free (syms[i].comdat_key);
	      syms[i].comdat_key = 0;
	    }
	}
      free (syms);
      handle->syms = NULL;
      handle->nsyms = 0;
      free (all_file_handles[j]);
      all_file_handles[j] = NULL;
    }

  free (all_file_handles);
  all_file_handles = NULL;
  num_file_handles = 0;
}

int
main(int argc, char *argv[])
{
  const char *plugin;
  unsigned int i;
  assert (argc >= 3);
  plugin = argv[1];

  load_plugin (plugin);

  for (i = 2; i < argc; i++)
    register_file (argv[i]);

  resolve ();

  print ();

  all_symbols_read_handler ();

  free_all ();

  cleanup_handler ();

  unload_plugin ();

  return 0;
}