plugin.c 36 KB

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