plugin.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* Plugin support for BFD.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  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 "bfd.h"
  18. #if BFD_SUPPORTS_PLUGINS
  19. #include <assert.h>
  20. #ifdef HAVE_DLFCN_H
  21. #include <dlfcn.h>
  22. #elif defined (HAVE_WINDOWS_H)
  23. #include <windows.h>
  24. #else
  25. #error Unknown how to handle dynamic-load-libraries.
  26. #endif
  27. #include <stdarg.h>
  28. #include "plugin-api.h"
  29. #include "plugin.h"
  30. #include "libbfd.h"
  31. #include "libiberty.h"
  32. #include <dirent.h>
  33. #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
  34. #define RTLD_NOW 0 /* Dummy value. */
  35. static void *
  36. dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
  37. {
  38. return LoadLibrary (file);
  39. }
  40. static void *
  41. dlsym (void *handle, const char *name)
  42. {
  43. return GetProcAddress (handle, name);
  44. }
  45. static int ATTRIBUTE_UNUSED
  46. dlclose (void *handle)
  47. {
  48. FreeLibrary (handle);
  49. return 0;
  50. }
  51. static const char *
  52. dlerror (void)
  53. {
  54. return "Unable to load DLL.";
  55. }
  56. #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
  57. #define bfd_plugin_close_and_cleanup _bfd_generic_close_and_cleanup
  58. #define bfd_plugin_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  59. #define bfd_plugin_new_section_hook _bfd_generic_new_section_hook
  60. #define bfd_plugin_get_section_contents _bfd_generic_get_section_contents
  61. #define bfd_plugin_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  62. #define bfd_plugin_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
  63. #define bfd_plugin_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  64. #define bfd_plugin_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
  65. #define bfd_plugin_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
  66. #define bfd_plugin_core_file_matches_executable_p generic_core_file_matches_executable_p
  67. #define bfd_plugin_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
  68. #define bfd_plugin_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  69. #define bfd_plugin_get_lineno _bfd_nosymbols_get_lineno
  70. #define bfd_plugin_find_nearest_line _bfd_nosymbols_find_nearest_line
  71. #define bfd_plugin_find_line _bfd_nosymbols_find_line
  72. #define bfd_plugin_find_inliner_info _bfd_nosymbols_find_inliner_info
  73. #define bfd_plugin_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  74. #define bfd_plugin_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  75. #define bfd_plugin_read_minisymbols _bfd_generic_read_minisymbols
  76. #define bfd_plugin_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  77. #define bfd_plugin_set_arch_mach bfd_default_set_arch_mach
  78. #define bfd_plugin_set_section_contents _bfd_generic_set_section_contents
  79. #define bfd_plugin_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  80. #define bfd_plugin_bfd_relax_section bfd_generic_relax_section
  81. #define bfd_plugin_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  82. #define bfd_plugin_bfd_link_add_symbols _bfd_generic_link_add_symbols
  83. #define bfd_plugin_bfd_link_just_syms _bfd_generic_link_just_syms
  84. #define bfd_plugin_bfd_final_link _bfd_generic_final_link
  85. #define bfd_plugin_bfd_link_split_section _bfd_generic_link_split_section
  86. #define bfd_plugin_bfd_gc_sections bfd_generic_gc_sections
  87. #define bfd_plugin_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  88. #define bfd_plugin_bfd_merge_sections bfd_generic_merge_sections
  89. #define bfd_plugin_bfd_is_group_section bfd_generic_is_group_section
  90. #define bfd_plugin_bfd_group_name bfd_generic_group_name
  91. #define bfd_plugin_bfd_discard_group bfd_generic_discard_group
  92. #define bfd_plugin_section_already_linked _bfd_generic_section_already_linked
  93. #define bfd_plugin_bfd_define_common_symbol bfd_generic_define_common_symbol
  94. #define bfd_plugin_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  95. #define bfd_plugin_bfd_define_start_stop bfd_generic_define_start_stop
  96. #define bfd_plugin_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
  97. #define bfd_plugin_bfd_link_check_relocs _bfd_generic_link_check_relocs
  98. static enum ld_plugin_status
  99. message (int level ATTRIBUTE_UNUSED,
  100. const char * format, ...)
  101. {
  102. va_list args;
  103. va_start (args, format);
  104. printf ("bfd plugin: ");
  105. vprintf (format, args);
  106. putchar ('\n');
  107. va_end (args);
  108. return LDPS_OK;
  109. }
  110. struct plugin_list_entry
  111. {
  112. /* These must be initialized for each IR object with LTO wrapper. */
  113. ld_plugin_claim_file_handler claim_file;
  114. ld_plugin_all_symbols_read_handler all_symbols_read;
  115. ld_plugin_all_symbols_read_handler cleanup_handler;
  116. bool has_symbol_type;
  117. struct plugin_list_entry *next;
  118. /* These can be reused for all IR objects. */
  119. const char *plugin_name;
  120. };
  121. static const char *plugin_program_name;
  122. void
  123. bfd_plugin_set_program_name (const char *program_name)
  124. {
  125. plugin_program_name = program_name;
  126. }
  127. static struct plugin_list_entry *plugin_list = NULL;
  128. static struct plugin_list_entry *current_plugin = NULL;
  129. /* Register a claim-file handler. */
  130. static enum ld_plugin_status
  131. register_claim_file (ld_plugin_claim_file_handler handler)
  132. {
  133. current_plugin->claim_file = handler;
  134. return LDPS_OK;
  135. }
  136. static enum ld_plugin_status
  137. add_symbols (void * handle,
  138. int nsyms,
  139. const struct ld_plugin_symbol * syms)
  140. {
  141. bfd *abfd = handle;
  142. struct plugin_data_struct *plugin_data =
  143. bfd_alloc (abfd, sizeof (plugin_data_struct));
  144. if (!plugin_data)
  145. return LDPS_ERR;
  146. plugin_data->nsyms = nsyms;
  147. plugin_data->syms = syms;
  148. if (nsyms != 0)
  149. abfd->flags |= HAS_SYMS;
  150. abfd->tdata.plugin_data = plugin_data;
  151. return LDPS_OK;
  152. }
  153. static enum ld_plugin_status
  154. add_symbols_v2 (void *handle, int nsyms,
  155. const struct ld_plugin_symbol *syms)
  156. {
  157. current_plugin->has_symbol_type = true;
  158. return add_symbols (handle, nsyms, syms);
  159. }
  160. int
  161. bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
  162. {
  163. bfd *iobfd;
  164. int fd;
  165. iobfd = ibfd;
  166. while (iobfd->my_archive
  167. && !bfd_is_thin_archive (iobfd->my_archive))
  168. iobfd = iobfd->my_archive;
  169. file->name = bfd_get_filename (iobfd);
  170. if (!iobfd->iostream && !bfd_open_file (iobfd))
  171. return 0;
  172. /* Reuse the archive plugin file descriptor. */
  173. if (iobfd != ibfd)
  174. fd = iobfd->archive_plugin_fd;
  175. else
  176. fd = -1;
  177. if (fd < 0)
  178. {
  179. /* The plugin API expects that the file descriptor won't be closed
  180. and reused as done by the bfd file cache. So open it again.
  181. dup isn't good enough. plugin IO uses lseek/read while BFD uses
  182. fseek/fread. It isn't wise to mix the unistd and stdio calls on
  183. the same underlying file descriptor. */
  184. fd = open (file->name, O_RDONLY | O_BINARY);
  185. if (fd < 0)
  186. {
  187. #ifndef EMFILE
  188. return 0;
  189. #else
  190. if (errno != EMFILE)
  191. return 0;
  192. #ifdef HAVE_GETRLIMIT
  193. struct rlimit lim;
  194. /* Complicated links involving lots of files and/or large
  195. archives can exhaust the number of file descriptors
  196. available to us. If possible, try to allocate more
  197. descriptors. */
  198. if (getrlimit (RLIMIT_NOFILE, & lim) == 0
  199. && lim.rlim_cur < lim.rlim_max)
  200. {
  201. lim.rlim_cur = lim.rlim_max;
  202. if (setrlimit (RLIMIT_NOFILE, &lim) == 0)
  203. fd = open (file->name, O_RDONLY | O_BINARY);
  204. }
  205. if (fd < 0)
  206. #endif
  207. {
  208. _bfd_error_handler (_("plugin framework: out of file descriptors. Try using fewer objects/archives\n"));
  209. return 0;
  210. }
  211. #endif
  212. }
  213. }
  214. if (iobfd == ibfd)
  215. {
  216. struct stat stat_buf;
  217. if (fstat (fd, &stat_buf))
  218. {
  219. close (fd);
  220. return 0;
  221. }
  222. file->offset = 0;
  223. file->filesize = stat_buf.st_size;
  224. }
  225. else
  226. {
  227. /* Cache the archive plugin file descriptor. */
  228. iobfd->archive_plugin_fd = fd;
  229. iobfd->archive_plugin_fd_open_count++;
  230. file->offset = ibfd->origin;
  231. file->filesize = arelt_size (ibfd);
  232. }
  233. file->fd = fd;
  234. return 1;
  235. }
  236. /* Close the plugin file descriptor FD. If ABFD isn't NULL, it is an
  237. archive member. */
  238. void
  239. bfd_plugin_close_file_descriptor (bfd *abfd, int fd)
  240. {
  241. if (abfd == NULL)
  242. close (fd);
  243. else
  244. {
  245. while (abfd->my_archive
  246. && !bfd_is_thin_archive (abfd->my_archive))
  247. abfd = abfd->my_archive;
  248. /* Close the file descriptor if there is no archive plugin file
  249. descriptor. */
  250. if (abfd->archive_plugin_fd == -1)
  251. {
  252. close (fd);
  253. return;
  254. }
  255. abfd->archive_plugin_fd_open_count--;
  256. /* Dup the archive plugin file descriptor for later use, which
  257. will be closed by _bfd_archive_close_and_cleanup. */
  258. if (abfd->archive_plugin_fd_open_count == 0)
  259. {
  260. abfd->archive_plugin_fd = dup (fd);
  261. close (fd);
  262. }
  263. }
  264. }
  265. static int
  266. try_claim (bfd *abfd)
  267. {
  268. int claimed = 0;
  269. struct ld_plugin_input_file file;
  270. file.handle = abfd;
  271. if (bfd_plugin_open_input (abfd, &file)
  272. && current_plugin->claim_file)
  273. {
  274. current_plugin->claim_file (&file, &claimed);
  275. bfd_plugin_close_file_descriptor ((abfd->my_archive != NULL
  276. ? abfd : NULL),
  277. file.fd);
  278. }
  279. return claimed;
  280. }
  281. static bool
  282. try_load_plugin (const char *pname,
  283. struct plugin_list_entry *plugin_list_iter,
  284. bfd *abfd,
  285. bool build_list_p)
  286. {
  287. void *plugin_handle;
  288. struct ld_plugin_tv tv[5];
  289. int i;
  290. ld_plugin_onload onload;
  291. enum ld_plugin_status status;
  292. bool result = false;
  293. /* NB: Each object is independent. Reuse the previous plugin from
  294. the last run will lead to wrong result. */
  295. if (current_plugin)
  296. memset (current_plugin, 0,
  297. offsetof (struct plugin_list_entry, next));
  298. if (plugin_list_iter)
  299. pname = plugin_list_iter->plugin_name;
  300. plugin_handle = dlopen (pname, RTLD_NOW);
  301. if (!plugin_handle)
  302. {
  303. /* If we are building a list of viable plugins, then
  304. we do not bother the user with the details of any
  305. plugins that cannot be loaded. */
  306. if (! build_list_p)
  307. _bfd_error_handler ("Failed to load plugin '%s', reason: %s\n",
  308. pname, dlerror ());
  309. return false;
  310. }
  311. if (plugin_list_iter == NULL)
  312. {
  313. size_t length_plugin_name = strlen (pname) + 1;
  314. char *plugin_name = bfd_malloc (length_plugin_name);
  315. if (plugin_name == NULL)
  316. goto short_circuit;
  317. plugin_list_iter = bfd_malloc (sizeof *plugin_list_iter);
  318. if (plugin_list_iter == NULL)
  319. {
  320. free (plugin_name);
  321. goto short_circuit;
  322. }
  323. /* Make a copy of PNAME since PNAME from load_plugin () will be
  324. freed. */
  325. memcpy (plugin_name, pname, length_plugin_name);
  326. memset (plugin_list_iter, 0, sizeof (*plugin_list_iter));
  327. plugin_list_iter->plugin_name = plugin_name;
  328. plugin_list_iter->next = plugin_list;
  329. plugin_list = plugin_list_iter;
  330. }
  331. current_plugin = plugin_list_iter;
  332. if (build_list_p)
  333. goto short_circuit;
  334. onload = dlsym (plugin_handle, "onload");
  335. if (!onload)
  336. goto short_circuit;
  337. i = 0;
  338. tv[i].tv_tag = LDPT_MESSAGE;
  339. tv[i].tv_u.tv_message = message;
  340. ++i;
  341. tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
  342. tv[i].tv_u.tv_register_claim_file = register_claim_file;
  343. ++i;
  344. tv[i].tv_tag = LDPT_ADD_SYMBOLS;
  345. tv[i].tv_u.tv_add_symbols = add_symbols;
  346. ++i;
  347. tv[i].tv_tag = LDPT_ADD_SYMBOLS_V2;
  348. tv[i].tv_u.tv_add_symbols = add_symbols_v2;
  349. ++i;
  350. tv[i].tv_tag = LDPT_NULL;
  351. tv[i].tv_u.tv_val = 0;
  352. /* LTO plugin will call handler hooks to set up plugin handlers. */
  353. status = (*onload)(tv);
  354. if (status != LDPS_OK)
  355. goto short_circuit;
  356. abfd->plugin_format = bfd_plugin_no;
  357. if (!current_plugin->claim_file)
  358. goto short_circuit;
  359. if (!try_claim (abfd))
  360. goto short_circuit;
  361. abfd->plugin_format = bfd_plugin_yes;
  362. result = true;
  363. short_circuit:
  364. dlclose (plugin_handle);
  365. return result;
  366. }
  367. /* There may be plugin libraries in lib/bfd-plugins. */
  368. static int has_plugin_list = -1;
  369. static bfd_cleanup (*ld_plugin_object_p) (bfd *);
  370. static const char *plugin_name;
  371. void
  372. bfd_plugin_set_plugin (const char *p)
  373. {
  374. plugin_name = p;
  375. }
  376. /* Return TRUE if a plugin library is used. */
  377. bool
  378. bfd_plugin_specified_p (void)
  379. {
  380. return plugin_list != NULL;
  381. }
  382. /* Return TRUE if ABFD can be claimed by linker LTO plugin. */
  383. bool
  384. bfd_link_plugin_object_p (bfd *abfd)
  385. {
  386. if (ld_plugin_object_p)
  387. return ld_plugin_object_p (abfd) != NULL;
  388. return false;
  389. }
  390. extern const bfd_target plugin_vec;
  391. /* Return TRUE if TARGET is a pointer to plugin_vec. */
  392. bool
  393. bfd_plugin_target_p (const bfd_target *target)
  394. {
  395. return target == &plugin_vec;
  396. }
  397. /* Register OBJECT_P to be used by bfd_plugin_object_p. */
  398. void
  399. register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *))
  400. {
  401. ld_plugin_object_p = object_p;
  402. }
  403. static void
  404. build_plugin_list (bfd *abfd)
  405. {
  406. /* The intent was to search ${libdir}/bfd-plugins for plugins, but
  407. unfortunately the original implementation wasn't precisely that
  408. when configuring binutils using --libdir. Search in the proper
  409. path first, then the old one for backwards compatibility. */
  410. static const char *path[]
  411. = { LIBDIR "/bfd-plugins", BINDIR "/../lib/bfd-plugins" };
  412. struct stat last_st;
  413. unsigned int i;
  414. if (has_plugin_list >= 0)
  415. return;
  416. /* Try not to search the same dir twice, by looking at st_dev and
  417. st_ino for the dir. If we are on a file system that always sets
  418. st_ino to zero or the actual st_ino is zero we might waste some
  419. time, but that doesn't matter too much. */
  420. last_st.st_dev = 0;
  421. last_st.st_ino = 0;
  422. for (i = 0; i < sizeof (path) / sizeof (path[0]); i++)
  423. {
  424. char *plugin_dir = make_relative_prefix (plugin_program_name,
  425. BINDIR,
  426. path[i]);
  427. if (plugin_dir)
  428. {
  429. struct stat st;
  430. DIR *d;
  431. if (stat (plugin_dir, &st) == 0
  432. && S_ISDIR (st.st_mode)
  433. && !(last_st.st_dev == st.st_dev
  434. && last_st.st_ino == st.st_ino
  435. && st.st_ino != 0)
  436. && (d = opendir (plugin_dir)) != NULL)
  437. {
  438. struct dirent *ent;
  439. last_st.st_dev = st.st_dev;
  440. last_st.st_ino = st.st_ino;
  441. while ((ent = readdir (d)) != NULL)
  442. {
  443. char *full_name;
  444. full_name = concat (plugin_dir, "/", ent->d_name, NULL);
  445. if (stat (full_name, &st) == 0 && S_ISREG (st.st_mode))
  446. (void) try_load_plugin (full_name, NULL, abfd, true);
  447. free (full_name);
  448. }
  449. closedir (d);
  450. }
  451. free (plugin_dir);
  452. }
  453. }
  454. has_plugin_list = plugin_list != NULL;
  455. }
  456. static bool
  457. load_plugin (bfd *abfd)
  458. {
  459. struct plugin_list_entry *plugin_list_iter;
  460. if (plugin_name)
  461. return try_load_plugin (plugin_name, plugin_list, abfd, false);
  462. if (plugin_program_name == NULL)
  463. return false;
  464. build_plugin_list (abfd);
  465. for (plugin_list_iter = plugin_list;
  466. plugin_list_iter;
  467. plugin_list_iter = plugin_list_iter->next)
  468. if (try_load_plugin (NULL, plugin_list_iter, abfd, false))
  469. return true;
  470. return false;
  471. }
  472. static bfd_cleanup
  473. bfd_plugin_object_p (bfd *abfd)
  474. {
  475. if (ld_plugin_object_p)
  476. return ld_plugin_object_p (abfd);
  477. if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd))
  478. return NULL;
  479. return abfd->plugin_format == bfd_plugin_yes ? _bfd_no_cleanup : NULL;
  480. }
  481. /* Copy any private info we understand from the input bfd
  482. to the output bfd. */
  483. static bool
  484. bfd_plugin_bfd_copy_private_bfd_data (bfd *ibfd ATTRIBUTE_UNUSED,
  485. bfd *obfd ATTRIBUTE_UNUSED)
  486. {
  487. BFD_ASSERT (0);
  488. return true;
  489. }
  490. /* Copy any private info we understand from the input section
  491. to the output section. */
  492. static bool
  493. bfd_plugin_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
  494. asection *isection ATTRIBUTE_UNUSED,
  495. bfd *obfd ATTRIBUTE_UNUSED,
  496. asection *osection ATTRIBUTE_UNUSED)
  497. {
  498. BFD_ASSERT (0);
  499. return true;
  500. }
  501. /* Copy any private info we understand from the input symbol
  502. to the output symbol. */
  503. static bool
  504. bfd_plugin_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
  505. asymbol *isymbol ATTRIBUTE_UNUSED,
  506. bfd *obfd ATTRIBUTE_UNUSED,
  507. asymbol *osymbol ATTRIBUTE_UNUSED)
  508. {
  509. BFD_ASSERT (0);
  510. return true;
  511. }
  512. static bool
  513. bfd_plugin_bfd_print_private_bfd_data (bfd *abfd ATTRIBUTE_UNUSED, PTR ptr ATTRIBUTE_UNUSED)
  514. {
  515. BFD_ASSERT (0);
  516. return true;
  517. }
  518. static char *
  519. bfd_plugin_core_file_failing_command (bfd *abfd ATTRIBUTE_UNUSED)
  520. {
  521. BFD_ASSERT (0);
  522. return NULL;
  523. }
  524. static int
  525. bfd_plugin_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
  526. {
  527. BFD_ASSERT (0);
  528. return 0;
  529. }
  530. static int
  531. bfd_plugin_core_file_pid (bfd *abfd ATTRIBUTE_UNUSED)
  532. {
  533. BFD_ASSERT (0);
  534. return 0;
  535. }
  536. static long
  537. bfd_plugin_get_symtab_upper_bound (bfd *abfd)
  538. {
  539. struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
  540. long nsyms = plugin_data->nsyms;
  541. BFD_ASSERT (nsyms >= 0);
  542. return ((nsyms + 1) * sizeof (asymbol *));
  543. }
  544. static flagword
  545. convert_flags (const struct ld_plugin_symbol *sym)
  546. {
  547. switch (sym->def)
  548. {
  549. case LDPK_DEF:
  550. case LDPK_COMMON:
  551. case LDPK_UNDEF:
  552. return BSF_GLOBAL;
  553. case LDPK_WEAKUNDEF:
  554. case LDPK_WEAKDEF:
  555. return BSF_GLOBAL | BSF_WEAK;
  556. default:
  557. BFD_ASSERT (0);
  558. return 0;
  559. }
  560. }
  561. static long
  562. bfd_plugin_canonicalize_symtab (bfd *abfd,
  563. asymbol **alocation)
  564. {
  565. struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
  566. long nsyms = plugin_data->nsyms;
  567. const struct ld_plugin_symbol *syms = plugin_data->syms;
  568. static asection fake_text_section
  569. = BFD_FAKE_SECTION (fake_text_section, NULL, "plug", 0,
  570. SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS);
  571. static asection fake_data_section
  572. = BFD_FAKE_SECTION (fake_data_section, NULL, "plug", 0,
  573. SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS);
  574. static asection fake_bss_section
  575. = BFD_FAKE_SECTION (fake_bss_section, NULL, "plug", 0,
  576. SEC_ALLOC);
  577. static asection fake_common_section
  578. = BFD_FAKE_SECTION (fake_common_section, NULL, "plug", 0, SEC_IS_COMMON);
  579. int i;
  580. for (i = 0; i < nsyms; i++)
  581. {
  582. asymbol *s = bfd_alloc (abfd, sizeof (asymbol));
  583. BFD_ASSERT (s);
  584. alocation[i] = s;
  585. s->the_bfd = abfd;
  586. s->name = syms[i].name;
  587. s->value = 0;
  588. s->flags = convert_flags (&syms[i]);
  589. switch (syms[i].def)
  590. {
  591. case LDPK_COMMON:
  592. s->section = &fake_common_section;
  593. break;
  594. case LDPK_UNDEF:
  595. case LDPK_WEAKUNDEF:
  596. s->section = bfd_und_section_ptr;
  597. break;
  598. case LDPK_DEF:
  599. case LDPK_WEAKDEF:
  600. if (current_plugin->has_symbol_type)
  601. switch (syms[i].symbol_type)
  602. {
  603. default:
  604. /* FIXME: Should we issue an error here ? */
  605. case LDST_UNKNOWN:
  606. /* What is the best fake section for LDST_UNKNOWN? */
  607. case LDST_FUNCTION:
  608. s->section = &fake_text_section;
  609. break;
  610. case LDST_VARIABLE:
  611. if (syms[i].section_kind == LDSSK_BSS)
  612. s->section = &fake_bss_section;
  613. else
  614. s->section = &fake_data_section;
  615. break;
  616. }
  617. else
  618. s->section = &fake_text_section;
  619. break;
  620. default:
  621. BFD_ASSERT (0);
  622. }
  623. s->udata.p = (void *) &syms[i];
  624. }
  625. return nsyms;
  626. }
  627. static void
  628. bfd_plugin_print_symbol (bfd *abfd ATTRIBUTE_UNUSED,
  629. PTR afile ATTRIBUTE_UNUSED,
  630. asymbol *symbol ATTRIBUTE_UNUSED,
  631. bfd_print_symbol_type how ATTRIBUTE_UNUSED)
  632. {
  633. BFD_ASSERT (0);
  634. }
  635. static void
  636. bfd_plugin_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
  637. asymbol *symbol,
  638. symbol_info *ret)
  639. {
  640. bfd_symbol_info (symbol, ret);
  641. }
  642. /* Make an empty symbol. */
  643. static asymbol *
  644. bfd_plugin_make_empty_symbol (bfd *abfd)
  645. {
  646. asymbol *new_symbol = bfd_zalloc (abfd, sizeof (asymbol));
  647. if (new_symbol == NULL)
  648. return new_symbol;
  649. new_symbol->the_bfd = abfd;
  650. return new_symbol;
  651. }
  652. static int
  653. bfd_plugin_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
  654. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  655. {
  656. BFD_ASSERT (0);
  657. return 0;
  658. }
  659. const bfd_target plugin_vec =
  660. {
  661. "plugin", /* Name. */
  662. bfd_target_unknown_flavour,
  663. BFD_ENDIAN_LITTLE, /* Target byte order. */
  664. BFD_ENDIAN_LITTLE, /* Target headers byte order. */
  665. (HAS_RELOC | EXEC_P | /* Object flags. */
  666. HAS_LINENO | HAS_DEBUG |
  667. HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  668. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  669. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  670. 0, /* symbol_leading_char. */
  671. '/', /* ar_pad_char. */
  672. 15, /* ar_max_namelen. */
  673. 255, /* match priority. */
  674. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  675. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  676. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  677. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  678. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  679. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  680. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  681. { /* bfd_check_format. */
  682. _bfd_dummy_target,
  683. bfd_plugin_object_p,
  684. bfd_generic_archive_p,
  685. _bfd_dummy_target
  686. },
  687. { /* bfd_set_format. */
  688. _bfd_bool_bfd_false_error,
  689. _bfd_bool_bfd_false_error,
  690. _bfd_generic_mkarchive,
  691. _bfd_bool_bfd_false_error,
  692. },
  693. { /* bfd_write_contents. */
  694. _bfd_bool_bfd_false_error,
  695. _bfd_bool_bfd_false_error,
  696. _bfd_write_archive_contents,
  697. _bfd_bool_bfd_false_error,
  698. },
  699. BFD_JUMP_TABLE_GENERIC (bfd_plugin),
  700. BFD_JUMP_TABLE_COPY (bfd_plugin),
  701. BFD_JUMP_TABLE_CORE (bfd_plugin),
  702. #ifdef USE_64_BIT_ARCHIVE
  703. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_64_bit),
  704. #else
  705. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  706. #endif
  707. BFD_JUMP_TABLE_SYMBOLS (bfd_plugin),
  708. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  709. BFD_JUMP_TABLE_WRITE (bfd_plugin),
  710. BFD_JUMP_TABLE_LINK (bfd_plugin),
  711. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  712. NULL,
  713. NULL /* backend_data. */
  714. };
  715. #endif /* BFD_SUPPORTS_PLUGINS */