lto-symtab.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* Program to read the IL symbol table.
  2. Copyright (C) 2008 Free Software Foundation, Inc.
  3. Contributed by Rafael Avila de Espindola (espindola@google.com).
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <fcntl.h>
  16. #include <assert.h>
  17. #include <dlfcn.h>
  18. #include <stdio.h>
  19. #include <inttypes.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "plugin-api.h"
  23. #include "../gcc/lto/common.h"
  24. /* The presence of gelf.h is checked by the toplevel configure script. */
  25. # include <gelf.h>
  26. static ld_plugin_claim_file_handler claim_file_handler;
  27. static ld_plugin_all_symbols_read_handler all_symbols_read_handler;
  28. static ld_plugin_cleanup_handler cleanup_handler;
  29. static void *plugin_handle;
  30. struct file_handle {
  31. unsigned nsyms;
  32. struct ld_plugin_symbol *syms;
  33. };
  34. static struct file_handle **all_file_handles = NULL;
  35. static unsigned int num_file_handles;
  36. /* Write NSYMS symbols from file HANDLE in SYMS. */
  37. static enum ld_plugin_status
  38. get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
  39. {
  40. unsigned i;
  41. struct file_handle *h = (struct file_handle *) handle;
  42. assert (h->nsyms == nsyms);
  43. for (i = 0; i < nsyms; i++)
  44. syms[i] = h->syms[i];
  45. return LDPS_OK;
  46. }
  47. /* Register HANDLER as the callback for notifying the plugin that all symbols
  48. have been read. */
  49. static enum ld_plugin_status
  50. register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
  51. {
  52. all_symbols_read_handler = handler;
  53. return LDPS_OK;
  54. }
  55. /* Register HANDLER as the callback for claiming a file. */
  56. static enum ld_plugin_status
  57. register_claim_file(ld_plugin_claim_file_handler handler)
  58. {
  59. claim_file_handler = handler;
  60. return LDPS_OK;
  61. }
  62. /* Register HANDLER as the callback to removing temporary files. */
  63. static enum ld_plugin_status
  64. register_cleanup (ld_plugin_cleanup_handler handler)
  65. {
  66. cleanup_handler = handler;
  67. return LDPS_OK;
  68. }
  69. /* For a file identified by HANDLE, add NSYMS symbols from SYMS. */
  70. static enum ld_plugin_status
  71. add_symbols (void *handle, int nsyms,
  72. const struct ld_plugin_symbol *syms)
  73. {
  74. int i;
  75. struct file_handle *h = (struct file_handle *) handle;
  76. h->nsyms = nsyms;
  77. h->syms = calloc (nsyms, sizeof (struct ld_plugin_symbol));
  78. assert (h->syms);
  79. for (i = 0; i < nsyms; i++)
  80. {
  81. h->syms[i] = syms[i];
  82. h->syms[i].name = strdup (h->syms[i].name);
  83. if (h->syms[i].version)
  84. h->syms[i].version = strdup (h->syms[i].version);
  85. if (h->syms[i].comdat_key)
  86. h->syms[i].comdat_key = strdup (h->syms[i].comdat_key);
  87. }
  88. return LDPS_OK;
  89. }
  90. struct ld_plugin_tv tv[] = {
  91. {LDPT_REGISTER_CLAIM_FILE_HOOK,
  92. {.tv_register_claim_file = register_claim_file}
  93. },
  94. {LDPT_ADD_SYMBOLS,
  95. {.tv_add_symbols = add_symbols}
  96. },
  97. {LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
  98. {.tv_register_all_symbols_read = register_all_symbols_read}
  99. },
  100. {LDPT_GET_SYMBOLS,
  101. {.tv_get_symbols = get_symbols}
  102. },
  103. {LDPT_REGISTER_CLEANUP_HOOK,
  104. {.tv_register_cleanup = register_cleanup}
  105. },
  106. {0, {0}}
  107. };
  108. /* Load a plugin from a file named NAME. */
  109. static void
  110. load_plugin (const char *name)
  111. {
  112. ld_plugin_onload onload;
  113. plugin_handle = dlopen (name, RTLD_LAZY);
  114. assert (plugin_handle != NULL);
  115. onload = dlsym (plugin_handle, "onload");
  116. assert (onload);
  117. onload (tv);
  118. assert (claim_file_handler);
  119. }
  120. /* Send object to the plugin. The file (archive or object) name is NAME.
  121. FD is an open file descriptor. The object data starts at OFFSET and is
  122. FILESIZE bytes long. */
  123. static void
  124. register_object (const char *name, int fd, off_t offset, off_t filesize)
  125. {
  126. int claimed;
  127. struct ld_plugin_input_file file;
  128. void *handle;
  129. num_file_handles++;
  130. all_file_handles = realloc (all_file_handles, num_file_handles
  131. * sizeof (struct file_handle *));
  132. assert (all_file_handles);
  133. all_file_handles[num_file_handles - 1] = calloc (1,
  134. sizeof (struct file_handle));
  135. handle = all_file_handles[num_file_handles - 1];
  136. assert (handle);
  137. file.name = (char *) name;
  138. file.fd = fd;
  139. file.offset = offset;
  140. file.filesize = filesize;
  141. file.handle = handle;
  142. claim_file_handler (&file, &claimed);
  143. }
  144. /* Send file named NAME to the plugin. */
  145. static void
  146. register_file (const char *name)
  147. {
  148. int fd = open (name, O_RDONLY);
  149. Elf *elf;
  150. assert (fd >= 0);
  151. elf = elf_begin (fd, ELF_C_READ, NULL);
  152. assert (elf);
  153. Elf_Kind kind = elf_kind (elf);
  154. assert (kind == ELF_K_ELF || kind == ELF_K_AR);
  155. if (kind == ELF_K_AR)
  156. {
  157. Elf *member = elf_begin (fd, ELF_C_READ, elf);
  158. while (member)
  159. {
  160. Elf_Arhdr *h = elf_getarhdr (member);
  161. assert (h);
  162. if (h->ar_name[0] != '/')
  163. {
  164. off_t offset = elf_getbase (member);
  165. register_object (name, fd, offset, h->ar_size);
  166. }
  167. Elf_Cmd cmd = elf_next (member);
  168. elf_end (member);
  169. member = elf_begin (fd, cmd, elf);
  170. }
  171. }
  172. else /* Single File */
  173. register_object (name, fd, 0, 0);
  174. elf_end (elf);
  175. }
  176. /* Fake symbol resolution for testing. */
  177. static void
  178. resolve (void)
  179. {
  180. unsigned j;
  181. for (j = 0; j < num_file_handles; j++)
  182. {
  183. struct file_handle *handle = all_file_handles[j];
  184. unsigned int nsyms = handle->nsyms;
  185. struct ld_plugin_symbol *syms = handle->syms;
  186. unsigned i;
  187. for (i = 0; i < nsyms; i++)
  188. {
  189. switch (syms[i].def)
  190. {
  191. case LDPK_DEF:
  192. case LDPK_WEAKDEF:
  193. case LDPK_COMMON:
  194. syms[i].resolution = LDPR_PREVAILING_DEF;
  195. break;
  196. case LDPK_UNDEF:
  197. case LDPK_WEAKUNDEF:
  198. syms[i].resolution = LDPR_RESOLVED_IR;
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. /* Print all symbol information. */
  205. static void
  206. print (void)
  207. {
  208. unsigned j;
  209. for (j = 0; j < num_file_handles; j++)
  210. {
  211. struct file_handle *handle = all_file_handles[j];
  212. unsigned int nsyms = handle->nsyms;
  213. struct ld_plugin_symbol *syms = handle->syms;
  214. unsigned i;
  215. for (i = 0; i < nsyms; i++)
  216. {
  217. printf("name: %s; ", syms[i].name);
  218. if (syms[i].version)
  219. printf("version: %s;", syms[i].version);
  220. else
  221. printf("not versioned; ");
  222. printf("kind: %s; ", lto_kind_str[syms[i].def]);
  223. printf("visibility: %s; ", lto_visibility_str[syms[i].visibility]);
  224. printf("size: %" PRId64 "; ", syms[i].size);
  225. if (syms[i].comdat_key)
  226. printf("comdat_key: %s; ", syms[i].comdat_key);
  227. else
  228. printf("no comdat_key; ");
  229. printf ("resolution: %s\n", lto_resolution_str[syms[i].resolution]);
  230. }
  231. }
  232. }
  233. /* Unload the plugin. */
  234. static void
  235. unload_plugin (void)
  236. {
  237. unsigned err = dlclose (plugin_handle);
  238. assert (err == 0);
  239. claim_file_handler = 0;
  240. all_symbols_read_handler = 0;
  241. }
  242. /* Free all memory allocated by us that hasn't been freed yet. */
  243. static void
  244. free_all (void)
  245. {
  246. unsigned j;
  247. for (j = 0; j < num_file_handles; j++)
  248. {
  249. struct file_handle *handle = all_file_handles[j];
  250. unsigned int nsyms = handle->nsyms;
  251. struct ld_plugin_symbol *syms = handle->syms;
  252. unsigned i;
  253. for (i = 0; i < nsyms; i++)
  254. {
  255. free (syms[i].name);
  256. syms[i].name = 0;
  257. if (syms[i].version)
  258. {
  259. free (syms[i].version);
  260. syms[i].version = 0;
  261. }
  262. if (syms[i].comdat_key)
  263. {
  264. free (syms[i].comdat_key);
  265. syms[i].comdat_key = 0;
  266. }
  267. }
  268. free (syms);
  269. handle->syms = NULL;
  270. handle->nsyms = 0;
  271. free (all_file_handles[j]);
  272. all_file_handles[j] = NULL;
  273. }
  274. free (all_file_handles);
  275. all_file_handles = NULL;
  276. num_file_handles = 0;
  277. }
  278. int
  279. main(int argc, char *argv[])
  280. {
  281. const char *plugin;
  282. unsigned int i;
  283. assert (argc >= 3);
  284. plugin = argv[1];
  285. load_plugin (plugin);
  286. for (i = 2; i < argc; i++)
  287. register_file (argv[i]);
  288. resolve ();
  289. print ();
  290. all_symbols_read_handler ();
  291. free_all ();
  292. cleanup_handler ();
  293. unload_plugin ();
  294. return 0;
  295. }