plugin.c 39 KB

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