plugin.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /* Plugin control for the GNU linker.
  2. Copyright (C) 2010-2017 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  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,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "libiberty.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "bfdver.h"
  21. #include "ld.h"
  22. #include "ldmain.h"
  23. #include "ldmisc.h"
  24. #include "ldexp.h"
  25. #include "ldlang.h"
  26. #include "ldfile.h"
  27. #include "plugin-api.h"
  28. #include "../bfd/plugin.h"
  29. #include "plugin.h"
  30. #include "elf-bfd.h"
  31. #if HAVE_MMAP
  32. # include <sys/mman.h>
  33. # ifndef MAP_FAILED
  34. # define MAP_FAILED ((void *) -1)
  35. # endif
  36. # ifndef PROT_READ
  37. # define PROT_READ 0
  38. # endif
  39. # ifndef MAP_PRIVATE
  40. # define MAP_PRIVATE 0
  41. # endif
  42. #endif
  43. #include <errno.h>
  44. #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO))
  45. extern int errno;
  46. #endif
  47. #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
  48. #include <windows.h>
  49. #endif
  50. /* Report plugin symbols. */
  51. bfd_boolean report_plugin_symbols;
  52. /* The suffix to append to the name of the real (claimed) object file
  53. when generating a dummy BFD to hold the IR symbols sent from the
  54. plugin. For cosmetic use only; appears in maps, crefs etc. */
  55. #define IRONLY_SUFFIX " (symbol from plugin)"
  56. /* Stores a single argument passed to a plugin. */
  57. typedef struct plugin_arg
  58. {
  59. struct plugin_arg *next;
  60. const char *arg;
  61. } plugin_arg_t;
  62. /* Holds all details of a single plugin. */
  63. typedef struct plugin
  64. {
  65. /* Next on the list of plugins, or NULL at end of chain. */
  66. struct plugin *next;
  67. /* The argument string given to --plugin. */
  68. const char *name;
  69. /* The shared library handle returned by dlopen. */
  70. void *dlhandle;
  71. /* The list of argument string given to --plugin-opt. */
  72. plugin_arg_t *args;
  73. /* Number of args in the list, for convenience. */
  74. size_t n_args;
  75. /* The plugin's event handlers. */
  76. ld_plugin_claim_file_handler claim_file_handler;
  77. ld_plugin_all_symbols_read_handler all_symbols_read_handler;
  78. ld_plugin_cleanup_handler cleanup_handler;
  79. /* TRUE if the cleanup handlers have been called. */
  80. bfd_boolean cleanup_done;
  81. } plugin_t;
  82. typedef struct view_buffer
  83. {
  84. char *addr;
  85. size_t filesize;
  86. off_t offset;
  87. } view_buffer_t;
  88. /* The internal version of struct ld_plugin_input_file with a BFD
  89. pointer. */
  90. typedef struct plugin_input_file
  91. {
  92. bfd *abfd;
  93. view_buffer_t view_buffer;
  94. char *name;
  95. int fd;
  96. bfd_boolean use_mmap;
  97. off_t offset;
  98. off_t filesize;
  99. } plugin_input_file_t;
  100. /* The master list of all plugins. */
  101. static plugin_t *plugins_list = NULL;
  102. /* We keep a tail pointer for easy linking on the end. */
  103. static plugin_t **plugins_tail_chain_ptr = &plugins_list;
  104. /* The last plugin added to the list, for receiving args. */
  105. static plugin_t *last_plugin = NULL;
  106. /* The tail of the arg chain of the last plugin added to the list. */
  107. static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL;
  108. /* The plugin which is currently having a callback executed. */
  109. static plugin_t *called_plugin = NULL;
  110. /* Last plugin to cause an error, if any. */
  111. static const char *error_plugin = NULL;
  112. /* State of linker "notice" interface before we poked at it. */
  113. static bfd_boolean orig_notice_all;
  114. /* Original linker callbacks, and the plugin version. */
  115. static const struct bfd_link_callbacks *orig_callbacks;
  116. static struct bfd_link_callbacks plugin_callbacks;
  117. /* Set at all symbols read time, to avoid recursively offering the plugin
  118. its own newly-added input files and libs to claim. */
  119. bfd_boolean no_more_claiming = FALSE;
  120. #if HAVE_MMAP && HAVE_GETPAGESIZE
  121. /* Page size used by mmap. */
  122. static off_t plugin_pagesize;
  123. #endif
  124. /* List of tags to set in the constant leading part of the tv array. */
  125. static const enum ld_plugin_tag tv_header_tags[] =
  126. {
  127. LDPT_MESSAGE,
  128. LDPT_API_VERSION,
  129. LDPT_GNU_LD_VERSION,
  130. LDPT_LINKER_OUTPUT,
  131. LDPT_OUTPUT_NAME,
  132. LDPT_REGISTER_CLAIM_FILE_HOOK,
  133. LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
  134. LDPT_REGISTER_CLEANUP_HOOK,
  135. LDPT_ADD_SYMBOLS,
  136. LDPT_GET_INPUT_FILE,
  137. LDPT_GET_VIEW,
  138. LDPT_RELEASE_INPUT_FILE,
  139. LDPT_GET_SYMBOLS,
  140. LDPT_GET_SYMBOLS_V2,
  141. LDPT_ADD_INPUT_FILE,
  142. LDPT_ADD_INPUT_LIBRARY,
  143. LDPT_SET_EXTRA_LIBRARY_PATH
  144. };
  145. /* How many entries in the constant leading part of the tv array. */
  146. static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags);
  147. /* Forward references. */
  148. static bfd_boolean plugin_notice (struct bfd_link_info *,
  149. struct bfd_link_hash_entry *,
  150. struct bfd_link_hash_entry *,
  151. bfd *, asection *, bfd_vma, flagword);
  152. static const bfd_target * plugin_object_p (bfd *);
  153. #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
  154. #define RTLD_NOW 0 /* Dummy value. */
  155. static void *
  156. dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
  157. {
  158. return LoadLibrary (file);
  159. }
  160. static void *
  161. dlsym (void *handle, const char *name)
  162. {
  163. return GetProcAddress (handle, name);
  164. }
  165. static int
  166. dlclose (void *handle)
  167. {
  168. FreeLibrary (handle);
  169. return 0;
  170. }
  171. #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
  172. #ifndef HAVE_DLFCN_H
  173. static const char *
  174. dlerror (void)
  175. {
  176. return "";
  177. }
  178. #endif
  179. /* Helper function for exiting with error status. */
  180. static int
  181. set_plugin_error (const char *plugin)
  182. {
  183. error_plugin = plugin;
  184. return -1;
  185. }
  186. /* Test if an error occurred. */
  187. static bfd_boolean
  188. plugin_error_p (void)
  189. {
  190. return error_plugin != NULL;
  191. }
  192. /* Return name of plugin which caused an error if any. */
  193. const char *
  194. plugin_error_plugin (void)
  195. {
  196. return error_plugin ? error_plugin : _("<no plugin>");
  197. }
  198. /* Handle -plugin arg: find and load plugin, or return error. */
  199. void
  200. plugin_opt_plugin (const char *plugin)
  201. {
  202. plugin_t *newplug;
  203. plugin_t *curplug = plugins_list;
  204. newplug = xmalloc (sizeof *newplug);
  205. memset (newplug, 0, sizeof *newplug);
  206. newplug->name = plugin;
  207. newplug->dlhandle = dlopen (plugin, RTLD_NOW);
  208. if (!newplug->dlhandle)
  209. einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ());
  210. /* Check if plugin has been loaded already. */
  211. while (curplug)
  212. {
  213. if (newplug->dlhandle == curplug->dlhandle)
  214. {
  215. einfo (_("%P: %s: duplicated plugin\n"), plugin);
  216. free (newplug);
  217. return;
  218. }
  219. curplug = curplug->next;
  220. }
  221. /* Chain on end, so when we run list it is in command-line order. */
  222. *plugins_tail_chain_ptr = newplug;
  223. plugins_tail_chain_ptr = &newplug->next;
  224. /* Record it as current plugin for receiving args. */
  225. last_plugin = newplug;
  226. last_plugin_args_tail_chain_ptr = &newplug->args;
  227. }
  228. /* Accumulate option arguments for last-loaded plugin, or return
  229. error if none. */
  230. int
  231. plugin_opt_plugin_arg (const char *arg)
  232. {
  233. plugin_arg_t *newarg;
  234. if (!last_plugin)
  235. return set_plugin_error (_("<no plugin>"));
  236. /* Ignore -pass-through= from GCC driver. */
  237. if (*arg == '-')
  238. {
  239. const char *p = arg + 1;
  240. if (*p == '-')
  241. ++p;
  242. if (strncmp (p, "pass-through=", 13) == 0)
  243. return 0;
  244. }
  245. newarg = xmalloc (sizeof *newarg);
  246. newarg->arg = arg;
  247. newarg->next = NULL;
  248. /* Chain on end to preserve command-line order. */
  249. *last_plugin_args_tail_chain_ptr = newarg;
  250. last_plugin_args_tail_chain_ptr = &newarg->next;
  251. last_plugin->n_args++;
  252. return 0;
  253. }
  254. /* Generate a dummy BFD to represent an IR file, for any callers of
  255. plugin_call_claim_file to use as the handle in the ld_plugin_input_file
  256. struct that they build to pass in. The BFD is initially writable, so
  257. that symbols can be added to it; it must be made readable after the
  258. add_symbols hook has been called so that it can be read when linking. */
  259. static bfd *
  260. plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
  261. {
  262. bfd *abfd;
  263. bfd_boolean bfd_plugin_target;
  264. bfd_use_reserved_id = 1;
  265. bfd_plugin_target = bfd_plugin_target_p (srctemplate->xvec);
  266. abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
  267. bfd_plugin_target ? link_info.output_bfd : srctemplate);
  268. if (abfd != NULL)
  269. {
  270. abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
  271. if (!bfd_make_writable (abfd))
  272. goto report_error;
  273. if (!bfd_plugin_target)
  274. {
  275. bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
  276. bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
  277. if (!bfd_copy_private_bfd_data (srctemplate, abfd))
  278. goto report_error;
  279. }
  280. {
  281. flagword flags;
  282. /* Create section to own the symbols. */
  283. flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
  284. | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE);
  285. if (bfd_make_section_anyway_with_flags (abfd, ".text", flags))
  286. return abfd;
  287. }
  288. }
  289. report_error:
  290. einfo (_("could not create dummy IR bfd: %F%E\n"));
  291. return NULL;
  292. }
  293. /* Check if the BFD passed in is an IR dummy object file. */
  294. static inline bfd_boolean
  295. is_ir_dummy_bfd (const bfd *abfd)
  296. {
  297. /* ABFD can sometimes legitimately be NULL, e.g. when called from one
  298. of the linker callbacks for a symbol in the *ABS* or *UND* sections. */
  299. return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0;
  300. }
  301. /* Helpers to convert between BFD and GOLD symbol formats. */
  302. static enum ld_plugin_status
  303. asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym,
  304. const struct ld_plugin_symbol *ldsym)
  305. {
  306. flagword flags = BSF_NO_FLAGS;
  307. struct bfd_section *section;
  308. asym->the_bfd = abfd;
  309. asym->name = (ldsym->version
  310. ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL)
  311. : ldsym->name);
  312. asym->value = 0;
  313. switch (ldsym->def)
  314. {
  315. case LDPK_WEAKDEF:
  316. flags = BSF_WEAK;
  317. /* FALLTHRU */
  318. case LDPK_DEF:
  319. flags |= BSF_GLOBAL;
  320. if (ldsym->comdat_key)
  321. {
  322. char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key,
  323. (const char *) NULL);
  324. section = bfd_get_section_by_name (abfd, name);
  325. if (section != NULL)
  326. free (name);
  327. else
  328. {
  329. flagword sflags;
  330. sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY
  331. | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE
  332. | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD);
  333. section = bfd_make_section_anyway_with_flags (abfd, name, sflags);
  334. if (section == NULL)
  335. return LDPS_ERR;
  336. }
  337. }
  338. else
  339. section = bfd_get_section_by_name (abfd, ".text");
  340. break;
  341. case LDPK_WEAKUNDEF:
  342. flags = BSF_WEAK;
  343. /* FALLTHRU */
  344. case LDPK_UNDEF:
  345. section = bfd_und_section_ptr;
  346. break;
  347. case LDPK_COMMON:
  348. flags = BSF_GLOBAL;
  349. section = bfd_com_section_ptr;
  350. asym->value = ldsym->size;
  351. /* For ELF targets, set alignment of common symbol to 1. */
  352. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  353. {
  354. ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON;
  355. ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1;
  356. }
  357. break;
  358. default:
  359. return LDPS_ERR;
  360. }
  361. asym->flags = flags;
  362. asym->section = section;
  363. /* Visibility only applies on ELF targets. */
  364. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  365. {
  366. elf_symbol_type *elfsym = elf_symbol_from (abfd, asym);
  367. unsigned char visibility;
  368. if (!elfsym)
  369. einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name);
  370. switch (ldsym->visibility)
  371. {
  372. default:
  373. einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"),
  374. ldsym->visibility);
  375. return LDPS_ERR;
  376. case LDPV_DEFAULT:
  377. visibility = STV_DEFAULT;
  378. break;
  379. case LDPV_PROTECTED:
  380. visibility = STV_PROTECTED;
  381. break;
  382. case LDPV_INTERNAL:
  383. visibility = STV_INTERNAL;
  384. break;
  385. case LDPV_HIDDEN:
  386. visibility = STV_HIDDEN;
  387. break;
  388. }
  389. elfsym->internal_elf_sym.st_other
  390. = (visibility | (elfsym->internal_elf_sym.st_other
  391. & ~ELF_ST_VISIBILITY (-1)));
  392. }
  393. return LDPS_OK;
  394. }
  395. /* Register a claim-file handler. */
  396. static enum ld_plugin_status
  397. register_claim_file (ld_plugin_claim_file_handler handler)
  398. {
  399. ASSERT (called_plugin);
  400. called_plugin->claim_file_handler = handler;
  401. return LDPS_OK;
  402. }
  403. /* Register an all-symbols-read handler. */
  404. static enum ld_plugin_status
  405. register_all_symbols_read (ld_plugin_all_symbols_read_handler handler)
  406. {
  407. ASSERT (called_plugin);
  408. called_plugin->all_symbols_read_handler = handler;
  409. return LDPS_OK;
  410. }
  411. /* Register a cleanup handler. */
  412. static enum ld_plugin_status
  413. register_cleanup (ld_plugin_cleanup_handler handler)
  414. {
  415. ASSERT (called_plugin);
  416. called_plugin->cleanup_handler = handler;
  417. return LDPS_OK;
  418. }
  419. /* Add symbols from a plugin-claimed input file. */
  420. static enum ld_plugin_status
  421. add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms)
  422. {
  423. asymbol **symptrs;
  424. plugin_input_file_t *input = handle;
  425. bfd *abfd = input->abfd;
  426. int n;
  427. ASSERT (called_plugin);
  428. symptrs = xmalloc (nsyms * sizeof *symptrs);
  429. for (n = 0; n < nsyms; n++)
  430. {
  431. enum ld_plugin_status rv;
  432. asymbol *bfdsym;
  433. bfdsym = bfd_make_empty_symbol (abfd);
  434. symptrs[n] = bfdsym;
  435. rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n);
  436. if (rv != LDPS_OK)
  437. return rv;
  438. }
  439. bfd_set_symtab (abfd, symptrs, nsyms);
  440. return LDPS_OK;
  441. }
  442. /* Get the input file information with an open (possibly re-opened)
  443. file descriptor. */
  444. static enum ld_plugin_status
  445. get_input_file (const void *handle, struct ld_plugin_input_file *file)
  446. {
  447. const plugin_input_file_t *input = handle;
  448. ASSERT (called_plugin);
  449. file->name = input->name;
  450. file->offset = input->offset;
  451. file->filesize = input->filesize;
  452. file->handle = (void *) handle;
  453. return LDPS_OK;
  454. }
  455. /* Get view of the input file. */
  456. static enum ld_plugin_status
  457. get_view (const void *handle, const void **viewp)
  458. {
  459. plugin_input_file_t *input = (plugin_input_file_t *) handle;
  460. char *buffer;
  461. size_t size = input->filesize;
  462. off_t offset = input->offset;
  463. #if HAVE_MMAP && HAVE_GETPAGESIZE
  464. off_t bias;
  465. #endif
  466. ASSERT (called_plugin);
  467. /* FIXME: einfo should support %lld. */
  468. if ((off_t) size != input->filesize)
  469. einfo (_("%P%F: unsupported input file size: %s (%ld bytes)\n"),
  470. input->name, (long) input->filesize);
  471. /* Check the cached view buffer. */
  472. if (input->view_buffer.addr != NULL
  473. && input->view_buffer.filesize == size
  474. && input->view_buffer.offset == offset)
  475. {
  476. *viewp = input->view_buffer.addr;
  477. return LDPS_OK;
  478. }
  479. input->view_buffer.filesize = size;
  480. input->view_buffer.offset = offset;
  481. #if HAVE_MMAP
  482. # if HAVE_GETPAGESIZE
  483. bias = offset % plugin_pagesize;
  484. offset -= bias;
  485. size += bias;
  486. # endif
  487. buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd, offset);
  488. if (buffer != MAP_FAILED)
  489. {
  490. input->use_mmap = TRUE;
  491. # if HAVE_GETPAGESIZE
  492. buffer += bias;
  493. # endif
  494. }
  495. else
  496. #endif
  497. {
  498. char *p;
  499. input->use_mmap = FALSE;
  500. if (lseek (input->fd, offset, SEEK_SET) < 0)
  501. return LDPS_ERR;
  502. buffer = bfd_alloc (input->abfd, size);
  503. if (buffer == NULL)
  504. return LDPS_ERR;
  505. p = buffer;
  506. do
  507. {
  508. ssize_t got = read (input->fd, p, size);
  509. if (got == 0)
  510. break;
  511. else if (got > 0)
  512. {
  513. p += got;
  514. size -= got;
  515. }
  516. else if (errno != EINTR)
  517. return LDPS_ERR;
  518. }
  519. while (size > 0);
  520. }
  521. input->view_buffer.addr = buffer;
  522. *viewp = buffer;
  523. return LDPS_OK;
  524. }
  525. /* Release the input file. */
  526. static enum ld_plugin_status
  527. release_input_file (const void *handle)
  528. {
  529. plugin_input_file_t *input = (plugin_input_file_t *) handle;
  530. ASSERT (called_plugin);
  531. if (input->fd != -1)
  532. {
  533. close (input->fd);
  534. input->fd = -1;
  535. }
  536. return LDPS_OK;
  537. }
  538. /* Return TRUE if a defined symbol might be reachable from outside the
  539. universe of claimed objects. */
  540. static inline bfd_boolean
  541. is_visible_from_outside (struct ld_plugin_symbol *lsym,
  542. struct bfd_link_hash_entry *blhe)
  543. {
  544. struct bfd_sym_chain *sym;
  545. if (bfd_link_relocatable (&link_info))
  546. return TRUE;
  547. if (blhe->non_ir_ref_dynamic
  548. || link_info.export_dynamic
  549. || bfd_link_dll (&link_info))
  550. {
  551. /* Check if symbol is hidden by version script. */
  552. if (bfd_hide_sym_by_version (link_info.version_info,
  553. blhe->root.string))
  554. return FALSE;
  555. /* Only ELF symbols really have visibility. */
  556. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
  557. {
  558. struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe;
  559. int vis = ELF_ST_VISIBILITY (el->other);
  560. return vis == STV_DEFAULT || vis == STV_PROTECTED;
  561. }
  562. /* On non-ELF targets, we can safely make inferences by considering
  563. what visibility the plugin would have liked to apply when it first
  564. sent us the symbol. During ELF symbol processing, visibility only
  565. ever becomes more restrictive, not less, when symbols are merged,
  566. so this is a conservative estimate; it may give false positives,
  567. declaring something visible from outside when it in fact would
  568. not have been, but this will only lead to missed optimisation
  569. opportunities during LTRANS at worst; it will not give false
  570. negatives, which can lead to the disastrous conclusion that the
  571. related symbol is IRONLY. (See GCC PR46319 for an example.) */
  572. return (lsym->visibility == LDPV_DEFAULT
  573. || lsym->visibility == LDPV_PROTECTED);
  574. }
  575. for (sym = &entry_symbol; sym != NULL; sym = sym->next)
  576. if (sym->name
  577. && strcmp (sym->name, blhe->root.string) == 0)
  578. return TRUE;
  579. return FALSE;
  580. }
  581. /* Get the symbol resolution info for a plugin-claimed input file. */
  582. static enum ld_plugin_status
  583. get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms,
  584. int def_ironly_exp)
  585. {
  586. const plugin_input_file_t *input = handle;
  587. const bfd *abfd = (const bfd *) input->abfd;
  588. int n;
  589. ASSERT (called_plugin);
  590. for (n = 0; n < nsyms; n++)
  591. {
  592. struct bfd_link_hash_entry *blhe;
  593. asection *owner_sec;
  594. int res;
  595. if (syms[n].def != LDPK_UNDEF)
  596. blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name,
  597. FALSE, FALSE, TRUE);
  598. else
  599. blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info,
  600. syms[n].name, FALSE, FALSE, TRUE);
  601. if (!blhe)
  602. {
  603. /* The plugin is called to claim symbols in an archive element
  604. from plugin_object_p. But those symbols aren't needed to
  605. create output. They are defined and referenced only within
  606. IR. */
  607. switch (syms[n].def)
  608. {
  609. default:
  610. abort ();
  611. case LDPK_UNDEF:
  612. case LDPK_WEAKUNDEF:
  613. res = LDPR_UNDEF;
  614. break;
  615. case LDPK_DEF:
  616. case LDPK_WEAKDEF:
  617. case LDPK_COMMON:
  618. res = LDPR_PREVAILING_DEF_IRONLY;
  619. break;
  620. }
  621. goto report_symbol;
  622. }
  623. /* Determine resolution from blhe type and symbol's original type. */
  624. if (blhe->type == bfd_link_hash_undefined
  625. || blhe->type == bfd_link_hash_undefweak)
  626. {
  627. res = LDPR_UNDEF;
  628. goto report_symbol;
  629. }
  630. if (blhe->type != bfd_link_hash_defined
  631. && blhe->type != bfd_link_hash_defweak
  632. && blhe->type != bfd_link_hash_common)
  633. {
  634. /* We should not have a new, indirect or warning symbol here. */
  635. einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n",
  636. called_plugin->name, blhe->type);
  637. }
  638. /* Find out which section owns the symbol. Since it's not undef,
  639. it must have an owner; if it's not a common symbol, both defs
  640. and weakdefs keep it in the same place. */
  641. owner_sec = (blhe->type == bfd_link_hash_common
  642. ? blhe->u.c.p->section
  643. : blhe->u.def.section);
  644. /* If it was originally undefined or common, then it has been
  645. resolved; determine how. */
  646. if (syms[n].def == LDPK_UNDEF
  647. || syms[n].def == LDPK_WEAKUNDEF
  648. || syms[n].def == LDPK_COMMON)
  649. {
  650. if (owner_sec->owner == link_info.output_bfd)
  651. res = LDPR_RESOLVED_EXEC;
  652. else if (owner_sec->owner == abfd)
  653. res = LDPR_PREVAILING_DEF_IRONLY;
  654. else if (is_ir_dummy_bfd (owner_sec->owner))
  655. res = LDPR_RESOLVED_IR;
  656. else if (owner_sec->owner != NULL
  657. && (owner_sec->owner->flags & DYNAMIC) != 0)
  658. res = LDPR_RESOLVED_DYN;
  659. else
  660. res = LDPR_RESOLVED_EXEC;
  661. }
  662. /* Was originally def, or weakdef. Does it prevail? If the
  663. owner is the original dummy bfd that supplied it, then this
  664. is the definition that has prevailed. */
  665. else if (owner_sec->owner == link_info.output_bfd)
  666. res = LDPR_PREEMPTED_REG;
  667. else if (owner_sec->owner == abfd)
  668. res = LDPR_PREVAILING_DEF_IRONLY;
  669. /* Was originally def, weakdef, or common, but has been pre-empted. */
  670. else if (is_ir_dummy_bfd (owner_sec->owner))
  671. res = LDPR_PREEMPTED_IR;
  672. else
  673. res = LDPR_PREEMPTED_REG;
  674. if (res == LDPR_PREVAILING_DEF_IRONLY)
  675. {
  676. /* We need to know if the sym is referenced from non-IR files. Or
  677. even potentially-referenced, perhaps in a future final link if
  678. this is a partial one, perhaps dynamically at load-time if the
  679. symbol is externally visible. */
  680. if (blhe->non_ir_ref_regular)
  681. res = LDPR_PREVAILING_DEF;
  682. else if (is_visible_from_outside (&syms[n], blhe))
  683. res = def_ironly_exp;
  684. }
  685. report_symbol:
  686. syms[n].resolution = res;
  687. if (report_plugin_symbols)
  688. einfo (_("%P: %B: symbol `%s' "
  689. "definition: %d, visibility: %d, resolution: %d\n"),
  690. abfd, syms[n].name,
  691. syms[n].def, syms[n].visibility, res);
  692. }
  693. return LDPS_OK;
  694. }
  695. static enum ld_plugin_status
  696. get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
  697. {
  698. return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF);
  699. }
  700. static enum ld_plugin_status
  701. get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms)
  702. {
  703. return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP);
  704. }
  705. /* Add a new (real) input file generated by a plugin. */
  706. static enum ld_plugin_status
  707. add_input_file (const char *pathname)
  708. {
  709. lang_input_statement_type *is;
  710. ASSERT (called_plugin);
  711. is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum,
  712. NULL);
  713. if (!is)
  714. return LDPS_ERR;
  715. is->flags.lto_output = 1;
  716. return LDPS_OK;
  717. }
  718. /* Add a new (real) library required by a plugin. */
  719. static enum ld_plugin_status
  720. add_input_library (const char *pathname)
  721. {
  722. lang_input_statement_type *is;
  723. ASSERT (called_plugin);
  724. is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum,
  725. NULL);
  726. if (!is)
  727. return LDPS_ERR;
  728. is->flags.lto_output = 1;
  729. return LDPS_OK;
  730. }
  731. /* Set the extra library path to be used by libraries added via
  732. add_input_library. */
  733. static enum ld_plugin_status
  734. set_extra_library_path (const char *path)
  735. {
  736. ASSERT (called_plugin);
  737. ldfile_add_library_path (xstrdup (path), FALSE);
  738. return LDPS_OK;
  739. }
  740. /* Issue a diagnostic message from a plugin. */
  741. static enum ld_plugin_status
  742. message (int level, const char *format, ...)
  743. {
  744. va_list args;
  745. va_start (args, format);
  746. switch (level)
  747. {
  748. case LDPL_INFO:
  749. vfinfo (stdout, format, args, FALSE);
  750. putchar ('\n');
  751. break;
  752. case LDPL_WARNING:
  753. {
  754. char *newfmt = concat ("%P: warning: ", format, "\n",
  755. (const char *) NULL);
  756. vfinfo (stdout, newfmt, args, TRUE);
  757. free (newfmt);
  758. }
  759. break;
  760. case LDPL_FATAL:
  761. case LDPL_ERROR:
  762. default:
  763. {
  764. char *newfmt = concat (level == LDPL_FATAL ? "%P%F" : "%P%X",
  765. ": error: ", format, "\n",
  766. (const char *) NULL);
  767. fflush (stdout);
  768. vfinfo (stderr, newfmt, args, TRUE);
  769. fflush (stderr);
  770. free (newfmt);
  771. }
  772. break;
  773. }
  774. va_end (args);
  775. return LDPS_OK;
  776. }
  777. /* Helper to size leading part of tv array and set it up. */
  778. static void
  779. set_tv_header (struct ld_plugin_tv *tv)
  780. {
  781. size_t i;
  782. /* Version info. */
  783. static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL);
  784. static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100;
  785. for (i = 0; i < tv_header_size; i++)
  786. {
  787. tv[i].tv_tag = tv_header_tags[i];
  788. #define TVU(x) tv[i].tv_u.tv_ ## x
  789. switch (tv[i].tv_tag)
  790. {
  791. case LDPT_MESSAGE:
  792. TVU(message) = message;
  793. break;
  794. case LDPT_API_VERSION:
  795. TVU(val) = LD_PLUGIN_API_VERSION;
  796. break;
  797. case LDPT_GNU_LD_VERSION:
  798. TVU(val) = major * 100 + minor;
  799. break;
  800. case LDPT_LINKER_OUTPUT:
  801. TVU(val) = (bfd_link_relocatable (&link_info) ? LDPO_REL
  802. : bfd_link_pde (&link_info) ? LDPO_EXEC
  803. : bfd_link_pie (&link_info) ? LDPO_PIE
  804. : LDPO_DYN);
  805. break;
  806. case LDPT_OUTPUT_NAME:
  807. TVU(string) = output_filename;
  808. break;
  809. case LDPT_REGISTER_CLAIM_FILE_HOOK:
  810. TVU(register_claim_file) = register_claim_file;
  811. break;
  812. case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
  813. TVU(register_all_symbols_read) = register_all_symbols_read;
  814. break;
  815. case LDPT_REGISTER_CLEANUP_HOOK:
  816. TVU(register_cleanup) = register_cleanup;
  817. break;
  818. case LDPT_ADD_SYMBOLS:
  819. TVU(add_symbols) = add_symbols;
  820. break;
  821. case LDPT_GET_INPUT_FILE:
  822. TVU(get_input_file) = get_input_file;
  823. break;
  824. case LDPT_GET_VIEW:
  825. TVU(get_view) = get_view;
  826. break;
  827. case LDPT_RELEASE_INPUT_FILE:
  828. TVU(release_input_file) = release_input_file;
  829. break;
  830. case LDPT_GET_SYMBOLS:
  831. TVU(get_symbols) = get_symbols_v1;
  832. break;
  833. case LDPT_GET_SYMBOLS_V2:
  834. TVU(get_symbols) = get_symbols_v2;
  835. break;
  836. case LDPT_ADD_INPUT_FILE:
  837. TVU(add_input_file) = add_input_file;
  838. break;
  839. case LDPT_ADD_INPUT_LIBRARY:
  840. TVU(add_input_library) = add_input_library;
  841. break;
  842. case LDPT_SET_EXTRA_LIBRARY_PATH:
  843. TVU(set_extra_library_path) = set_extra_library_path;
  844. break;
  845. default:
  846. /* Added a new entry to the array without adding
  847. a new case to set up its value is a bug. */
  848. FAIL ();
  849. }
  850. #undef TVU
  851. }
  852. }
  853. /* Append the per-plugin args list and trailing LDPT_NULL to tv. */
  854. static void
  855. set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv)
  856. {
  857. plugin_arg_t *arg = plugin->args;
  858. while (arg)
  859. {
  860. tv->tv_tag = LDPT_OPTION;
  861. tv->tv_u.tv_string = arg->arg;
  862. arg = arg->next;
  863. tv++;
  864. }
  865. tv->tv_tag = LDPT_NULL;
  866. tv->tv_u.tv_val = 0;
  867. }
  868. /* Load up and initialise all plugins after argument parsing. */
  869. void
  870. plugin_load_plugins (void)
  871. {
  872. struct ld_plugin_tv *my_tv;
  873. unsigned int max_args = 0;
  874. plugin_t *curplug = plugins_list;
  875. /* If there are no plugins, we need do nothing this run. */
  876. if (!curplug)
  877. return;
  878. /* First pass over plugins to find max # args needed so that we
  879. can size and allocate the tv array. */
  880. while (curplug)
  881. {
  882. if (curplug->n_args > max_args)
  883. max_args = curplug->n_args;
  884. curplug = curplug->next;
  885. }
  886. /* Allocate tv array and initialise constant part. */
  887. my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv);
  888. set_tv_header (my_tv);
  889. /* Pass over plugins again, activating them. */
  890. curplug = plugins_list;
  891. while (curplug)
  892. {
  893. enum ld_plugin_status rv;
  894. ld_plugin_onload onloadfn;
  895. onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload");
  896. if (!onloadfn)
  897. onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload");
  898. if (!onloadfn)
  899. einfo (_("%P%F: %s: error loading plugin: %s\n"),
  900. curplug->name, dlerror ());
  901. set_tv_plugin_args (curplug, &my_tv[tv_header_size]);
  902. called_plugin = curplug;
  903. rv = (*onloadfn) (my_tv);
  904. called_plugin = NULL;
  905. if (rv != LDPS_OK)
  906. einfo (_("%P%F: %s: plugin error: %d\n"), curplug->name, rv);
  907. curplug = curplug->next;
  908. }
  909. /* Since plugin(s) inited ok, assume they're going to want symbol
  910. resolutions, which needs us to track which symbols are referenced
  911. by non-IR files using the linker's notice callback. */
  912. orig_notice_all = link_info.notice_all;
  913. orig_callbacks = link_info.callbacks;
  914. plugin_callbacks = *orig_callbacks;
  915. plugin_callbacks.notice = &plugin_notice;
  916. link_info.notice_all = TRUE;
  917. link_info.lto_plugin_active = TRUE;
  918. link_info.callbacks = &plugin_callbacks;
  919. register_ld_plugin_object_p (plugin_object_p);
  920. #if HAVE_MMAP && HAVE_GETPAGESIZE
  921. plugin_pagesize = getpagesize ();
  922. #endif
  923. }
  924. /* Call 'claim file' hook for all plugins. */
  925. static int
  926. plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed)
  927. {
  928. plugin_t *curplug = plugins_list;
  929. *claimed = FALSE;
  930. while (curplug && !*claimed)
  931. {
  932. if (curplug->claim_file_handler)
  933. {
  934. off_t cur_offset;
  935. enum ld_plugin_status rv;
  936. called_plugin = curplug;
  937. cur_offset = lseek (file->fd, 0, SEEK_CUR);
  938. rv = (*curplug->claim_file_handler) (file, claimed);
  939. if (!*claimed)
  940. lseek (file->fd, cur_offset, SEEK_SET);
  941. called_plugin = NULL;
  942. if (rv != LDPS_OK)
  943. set_plugin_error (curplug->name);
  944. }
  945. curplug = curplug->next;
  946. }
  947. return plugin_error_p () ? -1 : 0;
  948. }
  949. /* Duplicates a character string with memory attached to ABFD. */
  950. static char *
  951. plugin_strdup (bfd *abfd, const char *str)
  952. {
  953. size_t strlength;
  954. char *copy;
  955. strlength = strlen (str) + 1;
  956. copy = bfd_alloc (abfd, strlength);
  957. if (copy == NULL)
  958. einfo (_("%P%F: plugin_strdup failed to allocate memory: %s\n"),
  959. bfd_get_error ());
  960. memcpy (copy, str, strlength);
  961. return copy;
  962. }
  963. static const bfd_target *
  964. plugin_object_p (bfd *ibfd)
  965. {
  966. int claimed;
  967. plugin_input_file_t *input;
  968. struct ld_plugin_input_file file;
  969. bfd *abfd;
  970. /* Don't try the dummy object file. */
  971. if ((ibfd->flags & BFD_PLUGIN) != 0)
  972. return NULL;
  973. if (ibfd->plugin_format != bfd_plugin_unknown)
  974. {
  975. if (ibfd->plugin_format == bfd_plugin_yes)
  976. return ibfd->plugin_dummy_bfd->xvec;
  977. else
  978. return NULL;
  979. }
  980. /* We create a dummy BFD, initially empty, to house whatever symbols
  981. the plugin may want to add. */
  982. abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
  983. input = bfd_alloc (abfd, sizeof (*input));
  984. if (input == NULL)
  985. einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
  986. bfd_get_error ());
  987. if (!bfd_plugin_open_input (ibfd, &file))
  988. return NULL;
  989. if (file.name == ibfd->filename)
  990. {
  991. /* We must copy filename attached to ibfd if it is not an archive
  992. member since it may be freed by bfd_close below. */
  993. file.name = plugin_strdup (abfd, file.name);
  994. }
  995. file.handle = input;
  996. /* The plugin API expects that the file descriptor won't be closed
  997. and reused as done by the bfd file cache. So dup one. */
  998. file.fd = dup (file.fd);
  999. if (file.fd < 0)
  1000. return NULL;
  1001. input->abfd = abfd;
  1002. input->view_buffer.addr = NULL;
  1003. input->view_buffer.filesize = 0;
  1004. input->view_buffer.offset = 0;
  1005. input->fd = file.fd;
  1006. input->use_mmap = FALSE;
  1007. input->offset = file.offset;
  1008. input->filesize = file.filesize;
  1009. input->name = plugin_strdup (abfd, ibfd->filename);
  1010. claimed = 0;
  1011. if (plugin_call_claim_file (&file, &claimed))
  1012. einfo (_("%P%F: %s: plugin reported error claiming file\n"),
  1013. plugin_error_plugin ());
  1014. if (input->fd != -1 && !bfd_plugin_target_p (ibfd->xvec))
  1015. {
  1016. /* FIXME: fd belongs to us, not the plugin. GCC plugin, which
  1017. doesn't need fd after plugin_call_claim_file, doesn't use
  1018. BFD plugin target vector. Since GCC plugin doesn't call
  1019. release_input_file, we close it here. LLVM plugin, which
  1020. needs fd after plugin_call_claim_file and calls
  1021. release_input_file after it is done, uses BFD plugin target
  1022. vector. This scheme doesn't work when a plugin needs fd and
  1023. doesn't use BFD plugin target vector neither. */
  1024. close (input->fd);
  1025. input->fd = -1;
  1026. }
  1027. if (claimed)
  1028. {
  1029. ibfd->plugin_format = bfd_plugin_yes;
  1030. ibfd->plugin_dummy_bfd = abfd;
  1031. bfd_make_readable (abfd);
  1032. return abfd->xvec;
  1033. }
  1034. else
  1035. {
  1036. #if HAVE_MMAP
  1037. if (input->use_mmap)
  1038. {
  1039. /* If plugin didn't claim the file, unmap the buffer. */
  1040. char *addr = input->view_buffer.addr;
  1041. off_t size = input->view_buffer.filesize;
  1042. # if HAVE_GETPAGESIZE
  1043. off_t bias = input->view_buffer.offset % plugin_pagesize;
  1044. size += bias;
  1045. addr -= bias;
  1046. # endif
  1047. munmap (addr, size);
  1048. }
  1049. #endif
  1050. /* If plugin didn't claim the file, we don't need the dummy bfd.
  1051. Can't avoid speculatively creating it, alas. */
  1052. ibfd->plugin_format = bfd_plugin_no;
  1053. bfd_close_all_done (abfd);
  1054. return NULL;
  1055. }
  1056. }
  1057. void
  1058. plugin_maybe_claim (lang_input_statement_type *entry)
  1059. {
  1060. ASSERT (entry->header.type == lang_input_statement_enum);
  1061. if (plugin_object_p (entry->the_bfd))
  1062. {
  1063. bfd *abfd = entry->the_bfd->plugin_dummy_bfd;
  1064. /* Discard the real file's BFD and substitute the dummy one. */
  1065. /* We can't call bfd_close on archives. BFD archive handling
  1066. caches elements, and add_archive_element keeps pointers to
  1067. the_bfd and the_bfd->filename in a lang_input_statement_type
  1068. linker script statement. */
  1069. if (entry->the_bfd->my_archive == NULL)
  1070. bfd_close (entry->the_bfd);
  1071. entry->the_bfd = abfd;
  1072. entry->flags.claimed = 1;
  1073. }
  1074. }
  1075. /* Call 'all symbols read' hook for all plugins. */
  1076. int
  1077. plugin_call_all_symbols_read (void)
  1078. {
  1079. plugin_t *curplug = plugins_list;
  1080. /* Disable any further file-claiming. */
  1081. no_more_claiming = TRUE;
  1082. while (curplug)
  1083. {
  1084. if (curplug->all_symbols_read_handler)
  1085. {
  1086. enum ld_plugin_status rv;
  1087. called_plugin = curplug;
  1088. rv = (*curplug->all_symbols_read_handler) ();
  1089. called_plugin = NULL;
  1090. if (rv != LDPS_OK)
  1091. set_plugin_error (curplug->name);
  1092. }
  1093. curplug = curplug->next;
  1094. }
  1095. return plugin_error_p () ? -1 : 0;
  1096. }
  1097. /* Call 'cleanup' hook for all plugins at exit. */
  1098. void
  1099. plugin_call_cleanup (void)
  1100. {
  1101. plugin_t *curplug = plugins_list;
  1102. while (curplug)
  1103. {
  1104. if (curplug->cleanup_handler && !curplug->cleanup_done)
  1105. {
  1106. enum ld_plugin_status rv;
  1107. curplug->cleanup_done = TRUE;
  1108. called_plugin = curplug;
  1109. rv = (*curplug->cleanup_handler) ();
  1110. called_plugin = NULL;
  1111. if (rv != LDPS_OK)
  1112. info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
  1113. curplug->name, rv);
  1114. dlclose (curplug->dlhandle);
  1115. }
  1116. curplug = curplug->next;
  1117. }
  1118. }
  1119. /* To determine which symbols should be resolved LDPR_PREVAILING_DEF
  1120. and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as
  1121. the linker adds them to the linker hash table. Mark those
  1122. referenced from a non-IR file with non_ir_ref_regular or
  1123. non_ir_ref_dynamic as appropriate. We have to notice_all symbols,
  1124. because we won't necessarily know until later which ones will be
  1125. contributed by IR files. */
  1126. static bfd_boolean
  1127. plugin_notice (struct bfd_link_info *info,
  1128. struct bfd_link_hash_entry *h,
  1129. struct bfd_link_hash_entry *inh,
  1130. bfd *abfd,
  1131. asection *section,
  1132. bfd_vma value,
  1133. flagword flags)
  1134. {
  1135. struct bfd_link_hash_entry *orig_h = h;
  1136. if (h != NULL)
  1137. {
  1138. bfd *sym_bfd;
  1139. bfd_boolean ref = FALSE;
  1140. if (h->type == bfd_link_hash_warning)
  1141. h = h->u.i.link;
  1142. /* Nothing to do here if this def/ref is from an IR dummy BFD. */
  1143. if (is_ir_dummy_bfd (abfd))
  1144. ;
  1145. /* Making an indirect symbol counts as a reference unless this
  1146. is a brand new symbol. */
  1147. else if (bfd_is_ind_section (section)
  1148. || (flags & BSF_INDIRECT) != 0)
  1149. {
  1150. /* ??? Some of this is questionable. See comments in
  1151. _bfd_generic_link_add_one_symbol for case IND. */
  1152. if (h->type != bfd_link_hash_new
  1153. || inh->type == bfd_link_hash_new)
  1154. {
  1155. if ((abfd->flags & DYNAMIC) == 0)
  1156. inh->non_ir_ref_regular = TRUE;
  1157. else
  1158. inh->non_ir_ref_dynamic = TRUE;
  1159. }
  1160. if (h->type != bfd_link_hash_new)
  1161. ref = TRUE;
  1162. }
  1163. /* Nothing to do here for warning symbols. */
  1164. else if ((flags & BSF_WARNING) != 0)
  1165. ;
  1166. /* Nothing to do here for constructor symbols. */
  1167. else if ((flags & BSF_CONSTRUCTOR) != 0)
  1168. ;
  1169. /* If this is a ref, set non_ir_ref. */
  1170. else if (bfd_is_und_section (section))
  1171. {
  1172. /* Replace the undefined dummy bfd with the real one. */
  1173. if ((h->type == bfd_link_hash_undefined
  1174. || h->type == bfd_link_hash_undefweak)
  1175. && (h->u.undef.abfd == NULL
  1176. || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
  1177. h->u.undef.abfd = abfd;
  1178. ref = TRUE;
  1179. }
  1180. /* Otherwise, it must be a new def. */
  1181. else
  1182. {
  1183. /* Ensure any symbol defined in an IR dummy BFD takes on a
  1184. new value from a real BFD. Weak symbols are not normally
  1185. overridden by a new weak definition, and strong symbols
  1186. will normally cause multiple definition errors. Avoid
  1187. this by making the symbol appear to be undefined. */
  1188. if (((h->type == bfd_link_hash_defweak
  1189. || h->type == bfd_link_hash_defined)
  1190. && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner))
  1191. || (h->type == bfd_link_hash_common
  1192. && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner)))
  1193. {
  1194. h->type = bfd_link_hash_undefweak;
  1195. h->u.undef.abfd = sym_bfd;
  1196. }
  1197. /* A common symbol should be merged with other commons or
  1198. defs with the same name. In particular, a common ought
  1199. to be overridden by a def in a -flto object. In that
  1200. sense a common is also a ref. */
  1201. if (bfd_is_com_section (section))
  1202. ref = TRUE;
  1203. }
  1204. if (ref)
  1205. {
  1206. if ((abfd->flags & DYNAMIC) == 0)
  1207. h->non_ir_ref_regular = TRUE;
  1208. else
  1209. h->non_ir_ref_dynamic = TRUE;
  1210. }
  1211. }
  1212. /* Continue with cref/nocrossref/trace-sym processing. */
  1213. if (orig_h == NULL
  1214. || orig_notice_all
  1215. || (info->notice_hash != NULL
  1216. && bfd_hash_lookup (info->notice_hash, orig_h->root.string,
  1217. FALSE, FALSE) != NULL))
  1218. return (*orig_callbacks->notice) (info, orig_h, inh,
  1219. abfd, section, value, flags);
  1220. return TRUE;
  1221. }