lto-plugin.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /* LTO plugin for gold and/or GNU ld.
  2. Copyright (C) 2009, 2010 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, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* The plugin has only one external function: onload. Gold passes it an array of
  16. function that the plugin uses to communicate back to gold.
  17. With the functions provided by gold, the plugin can be notified when
  18. gold first analyzes a file and pass a symbol table back to gold. The plugin
  19. is also notified when all symbols have been read and it is time to generate
  20. machine code for the necessary symbols.
  21. More information at http://gcc.gnu.org/wiki/whopr/driver.
  22. This plugin should be passed the lto-wrapper options and will forward them.
  23. It also has 2 options of its own:
  24. -debug: Print the command line used to run lto-wrapper.
  25. -nop: Instead of running lto-wrapper, pass the original to the plugin. This
  26. only works if the input files are hybrid. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #if HAVE_STDINT_H
  31. #include <stdint.h>
  32. #endif
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <inttypes.h>
  39. #include <sys/stat.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <sys/types.h>
  43. #ifdef HAVE_SYS_WAIT_H
  44. #include <sys/wait.h>
  45. #endif
  46. #ifndef WIFEXITED
  47. #define WIFEXITED(S) (((S) & 0xff) == 0)
  48. #endif
  49. #ifndef WEXITSTATUS
  50. #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
  51. #endif
  52. #include <libiberty.h>
  53. #include <hashtab.h>
  54. #include "../gcc/lto/common.h"
  55. #include "simple-object.h"
  56. #include "plugin-api.h"
  57. /* We need to use I64 instead of ll width-specifier on native Windows.
  58. The reason for this is that older MS-runtimes don't support the ll. */
  59. #ifdef __MINGW32__
  60. #define PRI_LL "I64"
  61. #else
  62. #define PRI_LL "ll"
  63. #endif
  64. /* Handle opening elf files on hosts, such as Windows, that may use
  65. text file handling that will break binary access. */
  66. #ifndef O_BINARY
  67. # define O_BINARY 0
  68. #endif
  69. /* Segment name for LTO sections. This is only used for Mach-O.
  70. FIXME: This needs to be kept in sync with darwin.c. */
  71. #define LTO_SEGMENT_NAME "__GNU_LTO"
  72. /* LTO magic section name. */
  73. #define LTO_SECTION_PREFIX ".gnu.lto_.symtab"
  74. #define LTO_SECTION_PREFIX_LEN (sizeof (LTO_SECTION_PREFIX) - 1)
  75. #define OFFLOAD_SECTION ".gnu.offload_lto_.opts"
  76. #define OFFLOAD_SECTION_LEN (sizeof (OFFLOAD_SECTION) - 1)
  77. /* The part of the symbol table the plugin has to keep track of. Note that we
  78. must keep SYMS until all_symbols_read is called to give the linker time to
  79. copy the symbol information.
  80. The id must be 64bit to minimze collisions. */
  81. struct sym_aux
  82. {
  83. uint32_t slot;
  84. unsigned long long id;
  85. unsigned next_conflict;
  86. };
  87. struct plugin_symtab
  88. {
  89. int nsyms;
  90. struct sym_aux *aux;
  91. struct ld_plugin_symbol *syms;
  92. unsigned long long id;
  93. };
  94. /* Encapsulates object file data during symbol scan. */
  95. struct plugin_objfile
  96. {
  97. int found;
  98. int offload;
  99. simple_object_read *objfile;
  100. struct plugin_symtab *out;
  101. const struct ld_plugin_input_file *file;
  102. };
  103. /* All that we have to remember about a file. */
  104. struct plugin_file_info
  105. {
  106. char *name;
  107. void *handle;
  108. struct plugin_symtab symtab;
  109. struct plugin_symtab conflicts;
  110. };
  111. /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
  112. stdio file streams, we do simple label translation here. */
  113. enum symbol_style
  114. {
  115. ss_none, /* No underscore prefix. */
  116. ss_win32, /* Underscore prefix any symbol not beginning with '@'. */
  117. ss_uscore, /* Underscore prefix all symbols. */
  118. };
  119. static char *arguments_file_name;
  120. static ld_plugin_register_claim_file register_claim_file;
  121. static ld_plugin_register_all_symbols_read register_all_symbols_read;
  122. static ld_plugin_get_symbols get_symbols, get_symbols_v2;
  123. static ld_plugin_register_cleanup register_cleanup;
  124. static ld_plugin_add_input_file add_input_file;
  125. static ld_plugin_add_input_library add_input_library;
  126. static ld_plugin_message message;
  127. static ld_plugin_add_symbols add_symbols;
  128. static struct plugin_file_info *claimed_files = NULL;
  129. static unsigned int num_claimed_files = 0;
  130. static struct plugin_file_info *offload_files = NULL;
  131. static unsigned int num_offload_files = 0;
  132. static char **output_files = NULL;
  133. static unsigned int num_output_files = 0;
  134. static char **lto_wrapper_argv;
  135. static int lto_wrapper_num_args;
  136. static char **pass_through_items = NULL;
  137. static unsigned int num_pass_through_items;
  138. static char debug;
  139. static char nop;
  140. static char *resolution_file = NULL;
  141. /* The version of gold being used, or -1 if not gold. The number is
  142. MAJOR * 100 + MINOR. */
  143. static int gold_version = -1;
  144. /* Not used by default, but can be overridden at runtime
  145. by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
  146. (in fact, only first letter of style arg is checked.) */
  147. static enum symbol_style sym_style = ss_none;
  148. static void
  149. check_1 (int gate, enum ld_plugin_level level, const char *text)
  150. {
  151. if (gate)
  152. return;
  153. if (message)
  154. message (level, text);
  155. else
  156. {
  157. /* If there is no nicer way to inform the user, fallback to stderr. */
  158. fprintf (stderr, "%s\n", text);
  159. if (level == LDPL_FATAL)
  160. abort ();
  161. }
  162. }
  163. /* This little wrapper allows check to be called with a non-integer
  164. first argument, such as a pointer that must be non-NULL. We can't
  165. use c99 bool type to coerce it into range, so we explicitly test. */
  166. #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
  167. /* Parse an entry of the IL symbol table. The data to be parsed is pointed
  168. by P and the result is written in ENTRY. The slot number is stored in SLOT.
  169. Returns the address of the next entry. */
  170. static char *
  171. parse_table_entry (char *p, struct ld_plugin_symbol *entry,
  172. struct sym_aux *aux)
  173. {
  174. unsigned char t;
  175. enum ld_plugin_symbol_kind translate_kind[] =
  176. {
  177. LDPK_DEF,
  178. LDPK_WEAKDEF,
  179. LDPK_UNDEF,
  180. LDPK_WEAKUNDEF,
  181. LDPK_COMMON
  182. };
  183. enum ld_plugin_symbol_visibility translate_visibility[] =
  184. {
  185. LDPV_DEFAULT,
  186. LDPV_PROTECTED,
  187. LDPV_INTERNAL,
  188. LDPV_HIDDEN
  189. };
  190. switch (sym_style)
  191. {
  192. case ss_win32:
  193. if (p[0] == '@')
  194. {
  195. /* cf. Duff's device. */
  196. case ss_none:
  197. entry->name = xstrdup (p);
  198. break;
  199. }
  200. /* FALL-THROUGH. */
  201. case ss_uscore:
  202. entry->name = concat ("_", p, NULL);
  203. break;
  204. default:
  205. check (0, LDPL_FATAL, "invalid symbol style requested");
  206. break;
  207. }
  208. while (*p)
  209. p++;
  210. p++;
  211. entry->version = NULL;
  212. entry->comdat_key = p;
  213. while (*p)
  214. p++;
  215. p++;
  216. if (strlen (entry->comdat_key) == 0)
  217. entry->comdat_key = NULL;
  218. else
  219. entry->comdat_key = xstrdup (entry->comdat_key);
  220. t = *p;
  221. check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
  222. entry->def = translate_kind[t];
  223. p++;
  224. t = *p;
  225. check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
  226. entry->visibility = translate_visibility[t];
  227. p++;
  228. memcpy (&entry->size, p, sizeof (uint64_t));
  229. p += 8;
  230. memcpy (&aux->slot, p, sizeof (uint32_t));
  231. p += 4;
  232. entry->resolution = LDPR_UNKNOWN;
  233. aux->next_conflict = -1;
  234. return p;
  235. }
  236. /* Translate the IL symbol table located between DATA and END. Append the
  237. slots and symbols to OUT. */
  238. static void
  239. translate (char *data, char *end, struct plugin_symtab *out)
  240. {
  241. struct sym_aux *aux;
  242. struct ld_plugin_symbol *syms = NULL;
  243. int n, len;
  244. /* This overestimates the output buffer sizes, but at least
  245. the algorithm is O(1) now. */
  246. len = (end - data)/8 + out->nsyms + 1;
  247. syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
  248. aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
  249. for (n = out->nsyms; data < end; n++)
  250. {
  251. aux[n].id = out->id;
  252. data = parse_table_entry (data, &syms[n], &aux[n]);
  253. }
  254. assert(n < len);
  255. out->nsyms = n;
  256. out->syms = syms;
  257. out->aux = aux;
  258. }
  259. /* Free all memory that is no longer needed after writing the symbol
  260. resolution. */
  261. static void
  262. free_1 (struct plugin_file_info *files, unsigned num_files)
  263. {
  264. unsigned int i;
  265. for (i = 0; i < num_files; i++)
  266. {
  267. struct plugin_file_info *info = &files[i];
  268. struct plugin_symtab *symtab = &info->symtab;
  269. unsigned int j;
  270. for (j = 0; j < symtab->nsyms; j++)
  271. {
  272. struct ld_plugin_symbol *s = &symtab->syms[j];
  273. free (s->name);
  274. free (s->comdat_key);
  275. }
  276. free (symtab->syms);
  277. symtab->syms = NULL;
  278. }
  279. }
  280. /* Free all remaining memory. */
  281. static void
  282. free_2 (void)
  283. {
  284. unsigned int i;
  285. for (i = 0; i < num_claimed_files; i++)
  286. {
  287. struct plugin_file_info *info = &claimed_files[i];
  288. struct plugin_symtab *symtab = &info->symtab;
  289. free (symtab->aux);
  290. free (info->name);
  291. }
  292. for (i = 0; i < num_offload_files; i++)
  293. {
  294. struct plugin_file_info *info = &offload_files[i];
  295. struct plugin_symtab *symtab = &info->symtab;
  296. free (symtab->aux);
  297. free (info->name);
  298. }
  299. for (i = 0; i < num_output_files; i++)
  300. free (output_files[i]);
  301. free (output_files);
  302. free (claimed_files);
  303. claimed_files = NULL;
  304. num_claimed_files = 0;
  305. free (offload_files);
  306. offload_files = NULL;
  307. num_offload_files = 0;
  308. free (arguments_file_name);
  309. arguments_file_name = NULL;
  310. }
  311. /* Dump SYMTAB to resolution file F. */
  312. static void
  313. dump_symtab (FILE *f, struct plugin_symtab *symtab)
  314. {
  315. unsigned j;
  316. for (j = 0; j < symtab->nsyms; j++)
  317. {
  318. uint32_t slot = symtab->aux[j].slot;
  319. unsigned int resolution = symtab->syms[j].resolution;
  320. assert (resolution != LDPR_UNKNOWN);
  321. fprintf (f, "%u %" PRI_LL "x %s %s\n",
  322. (unsigned int) slot, symtab->aux[j].id,
  323. lto_resolution_str[resolution],
  324. symtab->syms[j].name);
  325. }
  326. }
  327. /* Finish the conflicts' resolution information after the linker resolved
  328. the original symbols */
  329. static void
  330. finish_conflict_resolution (struct plugin_symtab *symtab,
  331. struct plugin_symtab *conflicts)
  332. {
  333. int i, j;
  334. if (conflicts->nsyms == 0)
  335. return;
  336. for (i = 0; i < symtab->nsyms; i++)
  337. {
  338. int resolution = LDPR_UNKNOWN;
  339. if (symtab->aux[i].next_conflict == -1)
  340. continue;
  341. switch (symtab->syms[i].def)
  342. {
  343. case LDPK_DEF:
  344. case LDPK_COMMON: /* ??? */
  345. resolution = LDPR_RESOLVED_IR;
  346. break;
  347. case LDPK_WEAKDEF:
  348. resolution = LDPR_PREEMPTED_IR;
  349. break;
  350. case LDPK_UNDEF:
  351. case LDPK_WEAKUNDEF:
  352. resolution = symtab->syms[i].resolution;
  353. break;
  354. default:
  355. assert (0);
  356. }
  357. assert (resolution != LDPR_UNKNOWN);
  358. for (j = symtab->aux[i].next_conflict;
  359. j != -1;
  360. j = conflicts->aux[j].next_conflict)
  361. conflicts->syms[j].resolution = resolution;
  362. }
  363. }
  364. /* Free symbol table SYMTAB. */
  365. static void
  366. free_symtab (struct plugin_symtab *symtab)
  367. {
  368. free (symtab->syms);
  369. symtab->syms = NULL;
  370. free (symtab->aux);
  371. symtab->aux = NULL;
  372. }
  373. /* Writes the relocations to disk. */
  374. static void
  375. write_resolution (void)
  376. {
  377. unsigned int i;
  378. FILE *f;
  379. check (resolution_file, LDPL_FATAL, "resolution file not specified");
  380. f = fopen (resolution_file, "w");
  381. check (f, LDPL_FATAL, "could not open file");
  382. fprintf (f, "%d\n", num_claimed_files);
  383. for (i = 0; i < num_claimed_files; i++)
  384. {
  385. struct plugin_file_info *info = &claimed_files[i];
  386. struct plugin_symtab *symtab = &info->symtab;
  387. struct ld_plugin_symbol *syms = symtab->syms;
  388. /* Version 2 of API supports IRONLY_EXP resolution that is
  389. accepted by GCC-4.7 and newer. */
  390. if (get_symbols_v2)
  391. get_symbols_v2 (info->handle, symtab->nsyms, syms);
  392. else
  393. get_symbols (info->handle, symtab->nsyms, syms);
  394. finish_conflict_resolution (symtab, &info->conflicts);
  395. fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
  396. dump_symtab (f, symtab);
  397. if (info->conflicts.nsyms)
  398. {
  399. dump_symtab (f, &info->conflicts);
  400. free_symtab (&info->conflicts);
  401. }
  402. }
  403. fclose (f);
  404. }
  405. /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
  406. stdout. */
  407. static void
  408. add_output_files (FILE *f)
  409. {
  410. for (;;)
  411. {
  412. const unsigned piece = 32;
  413. char *buf, *s = xmalloc (piece);
  414. size_t len;
  415. buf = s;
  416. cont:
  417. if (!fgets (buf, piece, f))
  418. {
  419. free (s);
  420. break;
  421. }
  422. len = strlen (s);
  423. if (s[len - 1] != '\n')
  424. {
  425. s = xrealloc (s, len + piece);
  426. buf = s + len;
  427. goto cont;
  428. }
  429. s[len - 1] = '\0';
  430. num_output_files++;
  431. output_files
  432. = xrealloc (output_files, num_output_files * sizeof (char *));
  433. output_files[num_output_files - 1] = s;
  434. add_input_file (output_files[num_output_files - 1]);
  435. }
  436. }
  437. /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
  438. argument list. */
  439. static void
  440. exec_lto_wrapper (char *argv[])
  441. {
  442. int t, i;
  443. int status;
  444. char *at_args;
  445. FILE *args;
  446. FILE *wrapper_output;
  447. char *new_argv[3];
  448. struct pex_obj *pex;
  449. const char *errmsg;
  450. /* Write argv to a file to avoid a command line that is too long. */
  451. arguments_file_name = make_temp_file ("");
  452. check (arguments_file_name, LDPL_FATAL,
  453. "Failed to generate a temorary file name");
  454. args = fopen (arguments_file_name, "w");
  455. check (args, LDPL_FATAL, "could not open arguments file");
  456. t = writeargv (&argv[1], args);
  457. check (t == 0, LDPL_FATAL, "could not write arguments");
  458. t = fclose (args);
  459. check (t == 0, LDPL_FATAL, "could not close arguments file");
  460. at_args = concat ("@", arguments_file_name, NULL);
  461. check (at_args, LDPL_FATAL, "could not allocate");
  462. for (i = 1; argv[i]; i++)
  463. {
  464. char *a = argv[i];
  465. if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
  466. {
  467. for (i = 0; argv[i]; i++)
  468. fprintf (stderr, "%s ", argv[i]);
  469. fprintf (stderr, "\n");
  470. break;
  471. }
  472. }
  473. new_argv[0] = argv[0];
  474. new_argv[1] = at_args;
  475. new_argv[2] = NULL;
  476. if (debug)
  477. {
  478. for (i = 0; new_argv[i]; i++)
  479. fprintf (stderr, "%s ", new_argv[i]);
  480. fprintf (stderr, "\n");
  481. }
  482. pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
  483. check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
  484. errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
  485. check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
  486. check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
  487. wrapper_output = pex_read_output (pex, 0);
  488. check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
  489. add_output_files (wrapper_output);
  490. t = pex_get_status (pex, 1, &status);
  491. check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
  492. check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
  493. "lto-wrapper failed");
  494. pex_free (pex);
  495. free (at_args);
  496. }
  497. /* Pass the original files back to the linker. */
  498. static void
  499. use_original_files (void)
  500. {
  501. unsigned i;
  502. for (i = 0; i < num_claimed_files; i++)
  503. {
  504. struct plugin_file_info *info = &claimed_files[i];
  505. add_input_file (info->name);
  506. }
  507. }
  508. /* Called by the linker once all symbols have been read. */
  509. static enum ld_plugin_status
  510. all_symbols_read_handler (void)
  511. {
  512. unsigned i;
  513. unsigned num_lto_args
  514. = num_claimed_files + num_offload_files + lto_wrapper_num_args + 1;
  515. char **lto_argv;
  516. const char **lto_arg_ptr;
  517. if (num_claimed_files + num_offload_files == 0)
  518. return LDPS_OK;
  519. if (nop)
  520. {
  521. use_original_files ();
  522. return LDPS_OK;
  523. }
  524. lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
  525. lto_arg_ptr = (const char **) lto_argv;
  526. assert (lto_wrapper_argv);
  527. write_resolution ();
  528. free_1 (claimed_files, num_claimed_files);
  529. free_1 (offload_files, num_offload_files);
  530. for (i = 0; i < lto_wrapper_num_args; i++)
  531. *lto_arg_ptr++ = lto_wrapper_argv[i];
  532. for (i = 0; i < num_claimed_files; i++)
  533. {
  534. struct plugin_file_info *info = &claimed_files[i];
  535. *lto_arg_ptr++ = info->name;
  536. }
  537. for (i = 0; i < num_offload_files; i++)
  538. {
  539. struct plugin_file_info *info = &offload_files[i];
  540. *lto_arg_ptr++ = info->name;
  541. }
  542. *lto_arg_ptr++ = NULL;
  543. exec_lto_wrapper (lto_argv);
  544. free (lto_argv);
  545. /* --pass-through is not needed when using gold 1.11 or later. */
  546. if (pass_through_items && gold_version < 111)
  547. {
  548. unsigned int i;
  549. for (i = 0; i < num_pass_through_items; i++)
  550. {
  551. if (strncmp (pass_through_items[i], "-l", 2) == 0)
  552. add_input_library (pass_through_items[i] + 2);
  553. else
  554. add_input_file (pass_through_items[i]);
  555. free (pass_through_items[i]);
  556. pass_through_items[i] = NULL;
  557. }
  558. free (pass_through_items);
  559. pass_through_items = NULL;
  560. }
  561. return LDPS_OK;
  562. }
  563. /* Remove temporary files at the end of the link. */
  564. static enum ld_plugin_status
  565. cleanup_handler (void)
  566. {
  567. unsigned int i;
  568. int t;
  569. if (debug)
  570. return LDPS_OK;
  571. if (arguments_file_name)
  572. {
  573. t = unlink (arguments_file_name);
  574. check (t == 0, LDPL_FATAL, "could not unlink arguments file");
  575. }
  576. for (i = 0; i < num_output_files; i++)
  577. {
  578. t = unlink (output_files[i]);
  579. check (t == 0, LDPL_FATAL, "could not unlink output file");
  580. }
  581. free_2 ();
  582. return LDPS_OK;
  583. }
  584. #define SWAP(type, a, b) \
  585. do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
  586. /* Compare two hash table entries */
  587. static int eq_sym (const void *a, const void *b)
  588. {
  589. const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
  590. const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
  591. return !strcmp (as->name, bs->name);
  592. }
  593. /* Hash a symbol */
  594. static hashval_t hash_sym (const void *a)
  595. {
  596. const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
  597. return htab_hash_string (as->name);
  598. }
  599. /* Determine how strong a symbol is */
  600. static int symbol_strength (struct ld_plugin_symbol *s)
  601. {
  602. switch (s->def)
  603. {
  604. case LDPK_UNDEF:
  605. case LDPK_WEAKUNDEF:
  606. return 0;
  607. case LDPK_WEAKDEF:
  608. return 1;
  609. default:
  610. return 2;
  611. }
  612. }
  613. /* In the ld -r case we can get dups in the LTO symbol tables, where
  614. the same symbol can have different resolutions (e.g. undefined and defined).
  615. We have to keep that in the LTO symbol tables, but the dups confuse
  616. gold and then finally gcc by supplying incorrect resolutions.
  617. Problem is that the main gold symbol table doesn't know about subids
  618. and does not distingush the same symbols in different states.
  619. So we drop duplicates from the linker visible symbol table
  620. and keep them in a private table. Then later do own symbol
  621. resolution for the duplicated based on the results for the
  622. originals.
  623. Then when writing out the resolution file readd the dropped symbols.
  624. XXX how to handle common? */
  625. static void
  626. resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
  627. {
  628. htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
  629. int i;
  630. int out;
  631. int outlen;
  632. outlen = t->nsyms;
  633. conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
  634. conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
  635. /* Move all duplicate symbols into the auxiliary conflicts table. */
  636. out = 0;
  637. for (i = 0; i < t->nsyms; i++)
  638. {
  639. struct ld_plugin_symbol *s = &t->syms[i];
  640. struct sym_aux *aux = &t->aux[i];
  641. void **slot;
  642. slot = htab_find_slot (symtab, s, INSERT);
  643. if (*slot != NULL)
  644. {
  645. int cnf;
  646. struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
  647. struct sym_aux *orig_aux = &t->aux[orig - t->syms];
  648. /* Always let the linker resolve the strongest symbol */
  649. if (symbol_strength (orig) < symbol_strength (s))
  650. {
  651. SWAP (struct ld_plugin_symbol, *orig, *s);
  652. SWAP (uint32_t, orig_aux->slot, aux->slot);
  653. SWAP (unsigned long long, orig_aux->id, aux->id);
  654. /* Don't swap conflict chain pointer */
  655. }
  656. /* Move current symbol into the conflicts table */
  657. cnf = conflicts->nsyms++;
  658. conflicts->syms[cnf] = *s;
  659. conflicts->aux[cnf] = *aux;
  660. aux = &conflicts->aux[cnf];
  661. /* Update conflicts chain of the original symbol */
  662. aux->next_conflict = orig_aux->next_conflict;
  663. orig_aux->next_conflict = cnf;
  664. continue;
  665. }
  666. /* Remove previous duplicates in the main table */
  667. if (out < i)
  668. {
  669. t->syms[out] = *s;
  670. t->aux[out] = *aux;
  671. }
  672. /* Put original into the hash table */
  673. *slot = &t->syms[out];
  674. out++;
  675. }
  676. assert (conflicts->nsyms <= outlen);
  677. assert (conflicts->nsyms + out == t->nsyms);
  678. t->nsyms = out;
  679. htab_delete (symtab);
  680. }
  681. /* Process one section of an object file. */
  682. static int
  683. process_symtab (void *data, const char *name, off_t offset, off_t length)
  684. {
  685. struct plugin_objfile *obj = (struct plugin_objfile *)data;
  686. char *s;
  687. char *secdatastart, *secdata;
  688. if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0)
  689. return 1;
  690. s = strrchr (name, '.');
  691. if (s)
  692. sscanf (s, ".%" PRI_LL "x", &obj->out->id);
  693. secdata = secdatastart = xmalloc (length);
  694. offset += obj->file->offset;
  695. if (offset != lseek (obj->file->fd, offset, SEEK_SET))
  696. goto err;
  697. do
  698. {
  699. ssize_t got = read (obj->file->fd, secdata, length);
  700. if (got == 0)
  701. break;
  702. else if (got > 0)
  703. {
  704. secdata += got;
  705. length -= got;
  706. }
  707. else if (errno != EINTR)
  708. goto err;
  709. }
  710. while (length > 0);
  711. if (length > 0)
  712. goto err;
  713. translate (secdatastart, secdata, obj->out);
  714. obj->found++;
  715. free (secdatastart);
  716. return 1;
  717. err:
  718. if (message)
  719. message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
  720. /* Force claim_file_handler to abandon this file. */
  721. obj->found = 0;
  722. free (secdatastart);
  723. return 0;
  724. }
  725. /* Find an offload section of an object file. */
  726. static int
  727. process_offload_section (void *data, const char *name, off_t offset, off_t len)
  728. {
  729. if (!strncmp (name, OFFLOAD_SECTION, OFFLOAD_SECTION_LEN))
  730. {
  731. struct plugin_objfile *obj = (struct plugin_objfile *) data;
  732. obj->offload = 1;
  733. return 0;
  734. }
  735. return 1;
  736. }
  737. /* Callback used by gold to check if the plugin will claim FILE. Writes
  738. the result in CLAIMED. */
  739. static enum ld_plugin_status
  740. claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
  741. {
  742. enum ld_plugin_status status;
  743. struct plugin_objfile obj;
  744. struct plugin_file_info lto_file;
  745. int err;
  746. const char *errmsg;
  747. memset (&lto_file, 0, sizeof (struct plugin_file_info));
  748. if (file->offset != 0)
  749. {
  750. char *objname;
  751. /* We pass the offset of the actual file, not the archive header.
  752. Can't use PRIx64, because that's C99, so we have to print the
  753. 64-bit hex int as two 32-bit ones. */
  754. int lo, hi, t;
  755. lo = file->offset & 0xffffffff;
  756. hi = ((int64_t)file->offset >> 32) & 0xffffffff;
  757. t = hi ? asprintf (&objname, "%s@0x%x%08x", file->name, lo, hi)
  758. : asprintf (&objname, "%s@0x%x", file->name, lo);
  759. check (t >= 0, LDPL_FATAL, "asprintf failed");
  760. lto_file.name = objname;
  761. }
  762. else
  763. {
  764. lto_file.name = xstrdup (file->name);
  765. }
  766. lto_file.handle = file->handle;
  767. *claimed = 0;
  768. obj.file = file;
  769. obj.found = 0;
  770. obj.offload = 0;
  771. obj.out = &lto_file.symtab;
  772. errmsg = NULL;
  773. obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
  774. &errmsg, &err);
  775. /* No file, but also no error code means unrecognized format; just skip it. */
  776. if (!obj.objfile && !err)
  777. goto err;
  778. if (obj.objfile)
  779. errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err);
  780. if (!obj.objfile || errmsg)
  781. {
  782. if (err && message)
  783. message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
  784. xstrerror (err));
  785. else if (message)
  786. message (LDPL_FATAL, "%s: %s", file->name, errmsg);
  787. goto err;
  788. }
  789. if (obj.objfile)
  790. simple_object_find_sections (obj.objfile, process_offload_section,
  791. &obj, &err);
  792. if (obj.found == 0 && obj.offload == 0)
  793. goto err;
  794. if (obj.found > 1)
  795. resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
  796. if (obj.found > 0)
  797. {
  798. status = add_symbols (file->handle, lto_file.symtab.nsyms,
  799. lto_file.symtab.syms);
  800. check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
  801. num_claimed_files++;
  802. claimed_files =
  803. xrealloc (claimed_files,
  804. num_claimed_files * sizeof (struct plugin_file_info));
  805. claimed_files[num_claimed_files - 1] = lto_file;
  806. }
  807. if (obj.found == 0 && obj.offload == 1)
  808. {
  809. num_offload_files++;
  810. offload_files =
  811. xrealloc (offload_files,
  812. num_offload_files * sizeof (struct plugin_file_info));
  813. offload_files[num_offload_files - 1] = lto_file;
  814. }
  815. *claimed = 1;
  816. goto cleanup;
  817. err:
  818. free (lto_file.name);
  819. cleanup:
  820. if (obj.objfile)
  821. simple_object_release_read (obj.objfile);
  822. return LDPS_OK;
  823. }
  824. /* Parse the plugin options. */
  825. static void
  826. process_option (const char *option)
  827. {
  828. if (strcmp (option, "-debug") == 0)
  829. debug = 1;
  830. else if (strcmp (option, "-nop") == 0)
  831. nop = 1;
  832. else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
  833. {
  834. num_pass_through_items++;
  835. pass_through_items = xrealloc (pass_through_items,
  836. num_pass_through_items * sizeof (char *));
  837. pass_through_items[num_pass_through_items - 1] =
  838. xstrdup (option + strlen ("-pass-through="));
  839. }
  840. else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
  841. {
  842. switch (option[sizeof ("-sym-style=") - 1])
  843. {
  844. case 'w':
  845. sym_style = ss_win32;
  846. break;
  847. case 'u':
  848. sym_style = ss_uscore;
  849. break;
  850. default:
  851. sym_style = ss_none;
  852. break;
  853. }
  854. }
  855. else
  856. {
  857. int size;
  858. char *opt = xstrdup (option);
  859. lto_wrapper_num_args += 1;
  860. size = lto_wrapper_num_args * sizeof (char *);
  861. lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
  862. lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
  863. if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
  864. resolution_file = opt + sizeof ("-fresolution=") - 1;
  865. }
  866. }
  867. /* Called by gold after loading the plugin. TV is the transfer vector. */
  868. enum ld_plugin_status
  869. onload (struct ld_plugin_tv *tv)
  870. {
  871. struct ld_plugin_tv *p;
  872. enum ld_plugin_status status;
  873. p = tv;
  874. while (p->tv_tag)
  875. {
  876. switch (p->tv_tag)
  877. {
  878. case LDPT_MESSAGE:
  879. message = p->tv_u.tv_message;
  880. break;
  881. case LDPT_REGISTER_CLAIM_FILE_HOOK:
  882. register_claim_file = p->tv_u.tv_register_claim_file;
  883. break;
  884. case LDPT_ADD_SYMBOLS:
  885. add_symbols = p->tv_u.tv_add_symbols;
  886. break;
  887. case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
  888. register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
  889. break;
  890. case LDPT_GET_SYMBOLS_V2:
  891. get_symbols_v2 = p->tv_u.tv_get_symbols;
  892. break;
  893. case LDPT_GET_SYMBOLS:
  894. get_symbols = p->tv_u.tv_get_symbols;
  895. break;
  896. case LDPT_REGISTER_CLEANUP_HOOK:
  897. register_cleanup = p->tv_u.tv_register_cleanup;
  898. break;
  899. case LDPT_ADD_INPUT_FILE:
  900. add_input_file = p->tv_u.tv_add_input_file;
  901. break;
  902. case LDPT_ADD_INPUT_LIBRARY:
  903. add_input_library = p->tv_u.tv_add_input_library;
  904. break;
  905. case LDPT_OPTION:
  906. process_option (p->tv_u.tv_string);
  907. break;
  908. case LDPT_GOLD_VERSION:
  909. gold_version = p->tv_u.tv_val;
  910. break;
  911. default:
  912. break;
  913. }
  914. p++;
  915. }
  916. check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
  917. check (add_symbols, LDPL_FATAL, "add_symbols not found");
  918. status = register_claim_file (claim_file_handler);
  919. check (status == LDPS_OK, LDPL_FATAL,
  920. "could not register the claim_file callback");
  921. if (register_cleanup)
  922. {
  923. status = register_cleanup (cleanup_handler);
  924. check (status == LDPS_OK, LDPL_FATAL,
  925. "could not register the cleanup callback");
  926. }
  927. if (register_all_symbols_read)
  928. {
  929. check (get_symbols, LDPL_FATAL, "get_symbols not found");
  930. status = register_all_symbols_read (all_symbols_read_handler);
  931. check (status == LDPS_OK, LDPL_FATAL,
  932. "could not register the all_symbols_read callback");
  933. }
  934. /* Support -fno-use-linker-plugin by failing to load the plugin
  935. for the case where it is auto-loaded by BFD. */
  936. char *collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
  937. if (collect_gcc_options
  938. && strstr (collect_gcc_options, "'-fno-use-linker-plugin'"))
  939. return LDPS_ERR;
  940. return LDPS_OK;
  941. }