testplug2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* Test plugin for the GNU linker. Check non-object IR file as well as
  2. get_input_file, get_view and release_input_file interfaces.
  3. Copyright (C) 2015-2022 Free Software Foundation, Inc.
  4. This file is part of the GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #if BFD_SUPPORTS_PLUGINS
  20. #include "plugin-api.h"
  21. #include "filenames.h"
  22. /* For ARRAY_SIZE macro only - we don't link the library itself. */
  23. #include "libiberty.h"
  24. extern enum ld_plugin_status onload (struct ld_plugin_tv *tv);
  25. static enum ld_plugin_status onclaim_file (const struct ld_plugin_input_file *file,
  26. int *claimed);
  27. static enum ld_plugin_status onall_symbols_read (void);
  28. static enum ld_plugin_status oncleanup (void);
  29. /* Helper for calling plugin api message function. */
  30. #define TV_MESSAGE if (tv_message) (*tv_message)
  31. /* Struct for recording files to claim / files claimed. */
  32. typedef struct claim_file
  33. {
  34. struct claim_file *next;
  35. struct ld_plugin_input_file file;
  36. bool claimed;
  37. struct ld_plugin_symbol *symbols;
  38. int n_syms_allocated;
  39. int n_syms_used;
  40. } claim_file_t;
  41. /* Types of things that can be added at all symbols read time. */
  42. typedef enum addfile_enum
  43. {
  44. ADD_FILE,
  45. ADD_LIB,
  46. ADD_DIR
  47. } addfile_enum_t;
  48. /* Struct for recording files to add to final link. */
  49. typedef struct add_file
  50. {
  51. struct add_file *next;
  52. const char *name;
  53. addfile_enum_t type;
  54. } add_file_t;
  55. /* Helper macro for defining array of transfer vector tags and names. */
  56. #define ADDENTRY(tag) { tag, #tag }
  57. /* Struct for looking up human-readable versions of tag names. */
  58. typedef struct tag_name
  59. {
  60. enum ld_plugin_tag tag;
  61. const char *name;
  62. } tag_name_t;
  63. /* Array of all known tags and their names. */
  64. static const tag_name_t tag_names[] =
  65. {
  66. ADDENTRY(LDPT_NULL),
  67. ADDENTRY(LDPT_API_VERSION),
  68. ADDENTRY(LDPT_GOLD_VERSION),
  69. ADDENTRY(LDPT_LINKER_OUTPUT),
  70. ADDENTRY(LDPT_OPTION),
  71. ADDENTRY(LDPT_REGISTER_CLAIM_FILE_HOOK),
  72. ADDENTRY(LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK),
  73. ADDENTRY(LDPT_REGISTER_CLEANUP_HOOK),
  74. ADDENTRY(LDPT_ADD_SYMBOLS),
  75. ADDENTRY(LDPT_GET_SYMBOLS),
  76. ADDENTRY(LDPT_GET_SYMBOLS_V2),
  77. ADDENTRY(LDPT_ADD_INPUT_FILE),
  78. ADDENTRY(LDPT_MESSAGE),
  79. ADDENTRY(LDPT_GET_INPUT_FILE),
  80. ADDENTRY(LDPT_GET_VIEW),
  81. ADDENTRY(LDPT_RELEASE_INPUT_FILE),
  82. ADDENTRY(LDPT_ADD_INPUT_LIBRARY),
  83. ADDENTRY(LDPT_OUTPUT_NAME),
  84. ADDENTRY(LDPT_SET_EXTRA_LIBRARY_PATH),
  85. ADDENTRY(LDPT_GNU_LD_VERSION)
  86. };
  87. /* Function pointers to cache hooks passed at onload time. */
  88. static ld_plugin_register_claim_file tv_register_claim_file = 0;
  89. static ld_plugin_register_all_symbols_read tv_register_all_symbols_read = 0;
  90. static ld_plugin_register_cleanup tv_register_cleanup = 0;
  91. static ld_plugin_add_symbols tv_add_symbols = 0;
  92. static ld_plugin_get_symbols tv_get_symbols = 0;
  93. static ld_plugin_get_symbols tv_get_symbols_v2 = 0;
  94. static ld_plugin_add_input_file tv_add_input_file = 0;
  95. static ld_plugin_message tv_message = 0;
  96. static ld_plugin_get_input_file tv_get_input_file = 0;
  97. static ld_plugin_get_view tv_get_view = 0;
  98. static ld_plugin_release_input_file tv_release_input_file = 0;
  99. static ld_plugin_add_input_library tv_add_input_library = 0;
  100. static ld_plugin_set_extra_library_path tv_set_extra_library_path = 0;
  101. /* Other cached info from the transfer vector. */
  102. static enum ld_plugin_output_file_type linker_output;
  103. static const char *output_name;
  104. /* Behaviour control flags set by plugin options. */
  105. static enum ld_plugin_status onload_ret = LDPS_OK;
  106. static enum ld_plugin_status claim_file_ret = LDPS_OK;
  107. static enum ld_plugin_status all_symbols_read_ret = LDPS_OK;
  108. static enum ld_plugin_status cleanup_ret = LDPS_OK;
  109. static bool register_claimfile_hook = true;
  110. static bool register_allsymbolsread_hook = false;
  111. static bool register_cleanup_hook = false;
  112. static bool dumpresolutions = false;
  113. static bool allsymbolsread_silent = false;
  114. /* The master list of all claimable/claimed files. */
  115. static claim_file_t *claimfiles_list = NULL;
  116. /* We keep a tail pointer for easy linking on the end. */
  117. static claim_file_t **claimfiles_tail_chain_ptr = &claimfiles_list;
  118. /* The last claimed file added to the list, for receiving syms. */
  119. static claim_file_t *last_claimfile = NULL;
  120. /* The master list of all files to add to the final link. */
  121. static add_file_t *addfiles_list = NULL;
  122. /* We keep a tail pointer for easy linking on the end. */
  123. static add_file_t **addfiles_tail_chain_ptr = &addfiles_list;
  124. /* Add a new claimfile on the end of the chain. */
  125. static enum ld_plugin_status
  126. record_claim_file (const char *file, off_t filesize)
  127. {
  128. claim_file_t *newfile;
  129. newfile = malloc (sizeof *newfile);
  130. if (!newfile)
  131. return LDPS_ERR;
  132. memset (newfile, 0, sizeof *newfile);
  133. /* Only setup for now is remembering the name to look for. */
  134. newfile->file.name = file;
  135. newfile->file.filesize = filesize;
  136. /* Chain it on the end of the list. */
  137. *claimfiles_tail_chain_ptr = newfile;
  138. claimfiles_tail_chain_ptr = &newfile->next;
  139. /* Record it as active for receiving symbols to register. */
  140. last_claimfile = newfile;
  141. return LDPS_OK;
  142. }
  143. /* Add a new addfile on the end of the chain. */
  144. static enum ld_plugin_status
  145. record_add_file (const char *file, addfile_enum_t type)
  146. {
  147. add_file_t *newfile;
  148. newfile = malloc (sizeof *newfile);
  149. if (!newfile)
  150. return LDPS_ERR;
  151. newfile->next = NULL;
  152. newfile->name = file;
  153. newfile->type = type;
  154. /* Chain it on the end of the list. */
  155. *addfiles_tail_chain_ptr = newfile;
  156. addfiles_tail_chain_ptr = &newfile->next;
  157. return LDPS_OK;
  158. }
  159. /* Parse a command-line argument string into a symbol definition.
  160. Symbol-strings follow the colon-separated format:
  161. NAME:VERSION:def:vis:size:COMDATKEY
  162. where the fields in capitals are strings and those in lower
  163. case are integers. We don't allow to specify a resolution as
  164. doing so is not meaningful when calling the add symbols hook. */
  165. static enum ld_plugin_status
  166. parse_symdefstr (const char *str, struct ld_plugin_symbol *sym)
  167. {
  168. int n;
  169. long long size;
  170. const char *colon1, *colon2, *colon5;
  171. /* Locate the colons separating the first two strings. */
  172. colon1 = strchr (str, ':');
  173. if (!colon1)
  174. return LDPS_ERR;
  175. colon2 = strchr (colon1+1, ':');
  176. if (!colon2)
  177. return LDPS_ERR;
  178. /* Name must not be empty (version may be). */
  179. if (colon1 == str)
  180. return LDPS_ERR;
  181. /* The fifth colon and trailing comdat key string are optional,
  182. but the intermediate ones must all be present. */
  183. colon5 = strchr (colon2+1, ':'); /* Actually only third so far. */
  184. if (!colon5)
  185. return LDPS_ERR;
  186. colon5 = strchr (colon5+1, ':'); /* Hopefully fourth now. */
  187. if (!colon5)
  188. return LDPS_ERR;
  189. colon5 = strchr (colon5+1, ':'); /* Optional fifth now. */
  190. /* Finally we'll use sscanf to parse the numeric fields, then
  191. we'll split out the strings which we need to allocate separate
  192. storage for anyway so that we can add nul termination. */
  193. n = sscanf (colon2 + 1, "%hhi:%i:%lli", &sym->def, &sym->visibility, &size);
  194. if (n != 3)
  195. return LDPS_ERR;
  196. /* Parsed successfully, so allocate strings and fill out fields. */
  197. sym->size = size;
  198. sym->unused = 0;
  199. sym->section_kind = 0;
  200. sym->symbol_type = 0;
  201. sym->resolution = LDPR_UNKNOWN;
  202. sym->name = malloc (colon1 - str + 1);
  203. if (!sym->name)
  204. return LDPS_ERR;
  205. memcpy (sym->name, str, colon1 - str);
  206. sym->name[colon1 - str] = '\0';
  207. if (colon2 > (colon1 + 1))
  208. {
  209. sym->version = malloc (colon2 - colon1);
  210. if (!sym->version)
  211. return LDPS_ERR;
  212. memcpy (sym->version, colon1 + 1, colon2 - (colon1 + 1));
  213. sym->version[colon2 - (colon1 + 1)] = '\0';
  214. }
  215. else
  216. sym->version = NULL;
  217. if (colon5 && colon5[1])
  218. {
  219. sym->comdat_key = malloc (strlen (colon5 + 1) + 1);
  220. if (!sym->comdat_key)
  221. return LDPS_ERR;
  222. strcpy (sym->comdat_key, colon5 + 1);
  223. }
  224. else
  225. sym->comdat_key = 0;
  226. return LDPS_OK;
  227. }
  228. /* Record a symbol to be added for the last-added claimfile. */
  229. static enum ld_plugin_status
  230. record_claimed_file_symbol (const char *symdefstr)
  231. {
  232. struct ld_plugin_symbol sym;
  233. /* Can't add symbols except as belonging to claimed files. */
  234. if (!last_claimfile)
  235. return LDPS_ERR;
  236. /* If string doesn't parse correctly, give an error. */
  237. if (parse_symdefstr (symdefstr, &sym) != LDPS_OK)
  238. return LDPS_ERR;
  239. /* Check for enough space, resize array if needed, and add it. */
  240. if (last_claimfile->n_syms_allocated == last_claimfile->n_syms_used)
  241. {
  242. int new_n_syms = last_claimfile->n_syms_allocated
  243. ? 2 * last_claimfile->n_syms_allocated
  244. : 10;
  245. last_claimfile->symbols = realloc (last_claimfile->symbols,
  246. new_n_syms * sizeof *last_claimfile->symbols);
  247. if (!last_claimfile->symbols)
  248. return LDPS_ERR;
  249. last_claimfile->n_syms_allocated = new_n_syms;
  250. }
  251. last_claimfile->symbols[last_claimfile->n_syms_used++] = sym;
  252. return LDPS_OK;
  253. }
  254. /* Records the status to return from one of the registered hooks. */
  255. static enum ld_plugin_status
  256. set_ret_val (const char *whichval, enum ld_plugin_status retval)
  257. {
  258. if (!strcmp ("onload", whichval))
  259. onload_ret = retval;
  260. else if (!strcmp ("claimfile", whichval))
  261. claim_file_ret = retval;
  262. else if (!strcmp ("allsymbolsread", whichval))
  263. all_symbols_read_ret = retval;
  264. else if (!strcmp ("cleanup", whichval))
  265. cleanup_ret = retval;
  266. else
  267. return LDPS_ERR;
  268. return LDPS_OK;
  269. }
  270. /* Records hooks which should be registered. */
  271. static enum ld_plugin_status
  272. set_register_hook (const char *whichhook, bool yesno)
  273. {
  274. if (!strcmp ("claimfile", whichhook))
  275. register_claimfile_hook = yesno;
  276. else if (!strcmp ("allsymbolsread", whichhook))
  277. register_allsymbolsread_hook = yesno;
  278. else if (!strcmp ("allsymbolsreadsilent", whichhook))
  279. {
  280. register_allsymbolsread_hook = yesno;
  281. allsymbolsread_silent = true;
  282. }
  283. else if (!strcmp ("cleanup", whichhook))
  284. register_cleanup_hook = yesno;
  285. else
  286. return LDPS_ERR;
  287. return LDPS_OK;
  288. }
  289. /* Determine type of plugin option and pass to individual parsers. */
  290. static enum ld_plugin_status
  291. parse_option (const char *opt)
  292. {
  293. if (!strncmp ("fatal", opt, 5))
  294. {
  295. TV_MESSAGE (LDPL_FATAL, "Fatal error");
  296. fflush (NULL);
  297. }
  298. else if (!strncmp ("error", opt, 5))
  299. {
  300. TV_MESSAGE (LDPL_ERROR, "Error");
  301. fflush (NULL);
  302. }
  303. else if (!strncmp ("warning", opt, 7))
  304. {
  305. TV_MESSAGE (LDPL_WARNING, "Warning");
  306. fflush (NULL);
  307. }
  308. else if (!strncmp ("fail", opt, 4))
  309. return set_ret_val (opt + 4, LDPS_ERR);
  310. else if (!strncmp ("pass", opt, 4))
  311. return set_ret_val (opt + 4, LDPS_OK);
  312. else if (!strncmp ("register", opt, 8))
  313. return set_register_hook (opt + 8, true);
  314. else if (!strncmp ("noregister", opt, 10))
  315. return set_register_hook (opt + 10, false);
  316. else if (!strncmp ("claim:", opt, 6))
  317. return record_claim_file (opt + 6, 0);
  318. else if (!strncmp ("sym:", opt, 4))
  319. return record_claimed_file_symbol (opt + 4);
  320. else if (!strncmp ("add:", opt, 4))
  321. return record_add_file (opt + 4, ADD_FILE);
  322. else if (!strncmp ("lib:", opt, 4))
  323. return record_add_file (opt + 4, ADD_LIB);
  324. else if (!strncmp ("dir:", opt, 4))
  325. return record_add_file (opt + 4, ADD_DIR);
  326. else if (!strcmp ("dumpresolutions", opt))
  327. dumpresolutions = true;
  328. else
  329. return LDPS_ERR;
  330. return LDPS_OK;
  331. }
  332. /* Handle/record information received in a transfer vector entry. */
  333. static enum ld_plugin_status
  334. parse_tv_tag (struct ld_plugin_tv *tv)
  335. {
  336. #define SETVAR(x) x = tv->tv_u.x
  337. switch (tv->tv_tag)
  338. {
  339. case LDPT_OPTION:
  340. return parse_option (tv->tv_u.tv_string);
  341. case LDPT_NULL:
  342. case LDPT_GOLD_VERSION:
  343. case LDPT_GNU_LD_VERSION:
  344. case LDPT_API_VERSION:
  345. default:
  346. break;
  347. case LDPT_OUTPUT_NAME:
  348. output_name = tv->tv_u.tv_string;
  349. break;
  350. case LDPT_LINKER_OUTPUT:
  351. linker_output = tv->tv_u.tv_val;
  352. break;
  353. case LDPT_REGISTER_CLAIM_FILE_HOOK:
  354. SETVAR(tv_register_claim_file);
  355. break;
  356. case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
  357. SETVAR(tv_register_all_symbols_read);
  358. break;
  359. case LDPT_REGISTER_CLEANUP_HOOK:
  360. SETVAR(tv_register_cleanup);
  361. break;
  362. case LDPT_ADD_SYMBOLS:
  363. SETVAR(tv_add_symbols);
  364. break;
  365. case LDPT_GET_SYMBOLS:
  366. SETVAR(tv_get_symbols);
  367. break;
  368. case LDPT_GET_SYMBOLS_V2:
  369. tv_get_symbols_v2 = tv->tv_u.tv_get_symbols;
  370. break;
  371. case LDPT_ADD_INPUT_FILE:
  372. SETVAR(tv_add_input_file);
  373. break;
  374. case LDPT_MESSAGE:
  375. SETVAR(tv_message);
  376. break;
  377. case LDPT_GET_INPUT_FILE:
  378. SETVAR(tv_get_input_file);
  379. break;
  380. case LDPT_GET_VIEW:
  381. SETVAR(tv_get_view);
  382. break;
  383. case LDPT_RELEASE_INPUT_FILE:
  384. SETVAR(tv_release_input_file);
  385. break;
  386. case LDPT_ADD_INPUT_LIBRARY:
  387. SETVAR(tv_add_input_library);
  388. break;
  389. case LDPT_SET_EXTRA_LIBRARY_PATH:
  390. SETVAR(tv_set_extra_library_path);
  391. break;
  392. }
  393. #undef SETVAR
  394. return LDPS_OK;
  395. }
  396. /* Standard plugin API entry point. */
  397. enum ld_plugin_status
  398. onload (struct ld_plugin_tv *tv)
  399. {
  400. enum ld_plugin_status rv;
  401. /* This plugin does nothing but dump the tv array. It would
  402. be an error if this function was called without one. */
  403. if (!tv)
  404. return LDPS_ERR;
  405. /* First entry should always be LDPT_MESSAGE, letting us get
  406. hold of it easily so we can send output straight away. */
  407. if (tv[0].tv_tag == LDPT_MESSAGE)
  408. tv_message = tv[0].tv_u.tv_message;
  409. do
  410. if ((rv = parse_tv_tag (tv)) != LDPS_OK)
  411. return rv;
  412. while ((tv++)->tv_tag != LDPT_NULL);
  413. /* Register hooks only if instructed by options. */
  414. if (register_claimfile_hook)
  415. {
  416. if (!tv_register_claim_file)
  417. {
  418. TV_MESSAGE (LDPL_FATAL, "No register_claim_file hook");
  419. fflush (NULL);
  420. return LDPS_ERR;
  421. }
  422. (*tv_register_claim_file) (onclaim_file);
  423. }
  424. if (register_allsymbolsread_hook)
  425. {
  426. if (!tv_register_all_symbols_read)
  427. {
  428. TV_MESSAGE (LDPL_FATAL, "No register_all_symbols_read hook");
  429. fflush (NULL);
  430. return LDPS_ERR;
  431. }
  432. (*tv_register_all_symbols_read) (onall_symbols_read);
  433. }
  434. if (register_cleanup_hook)
  435. {
  436. if (!tv_register_cleanup)
  437. {
  438. TV_MESSAGE (LDPL_FATAL, "No register_cleanup hook");
  439. fflush (NULL);
  440. return LDPS_ERR;
  441. }
  442. (*tv_register_cleanup) (oncleanup);
  443. }
  444. /* Claim testsuite/ld-plugin/func.c, standalone or in a library. Its
  445. size must be SIZE_OF_FUNC_C bytes. */
  446. #define SIZE_OF_FUNC_C 248
  447. if (onload_ret == LDPS_OK
  448. && (record_claim_file ("func.c", SIZE_OF_FUNC_C) != LDPS_OK
  449. || record_claimed_file_symbol ("func::0:0:0") != LDPS_OK
  450. || record_claimed_file_symbol ("_func::0:0:0") != LDPS_OK
  451. || record_claim_file ("libfunc.a", SIZE_OF_FUNC_C) != LDPS_OK
  452. || record_claimed_file_symbol ("func::0:0:0") != LDPS_OK
  453. || record_claimed_file_symbol ("_func::0:0:0") != LDPS_OK))
  454. onload_ret = LDPS_ERR;
  455. return onload_ret;
  456. }
  457. char *
  458. xstrdup (const char *s)
  459. {
  460. size_t len = strlen (s) + 1;
  461. char *ret = malloc (len + 1);
  462. return (char *) memcpy (ret, s, len);
  463. }
  464. /* Standard plugin API registerable hook. */
  465. static enum ld_plugin_status
  466. onclaim_file (const struct ld_plugin_input_file *file, int *claimed)
  467. {
  468. /* Let's see if we want to claim this file. */
  469. claim_file_t *claimfile = claimfiles_list;
  470. size_t len = strlen (file->name);
  471. char *name = xstrdup (file->name);
  472. char *p = name + len;
  473. bool islib;
  474. /* Only match the file name without the directory part. */
  475. islib = *p == 'a' && *(p - 1) == '.';
  476. for (; p != name; p--)
  477. if (IS_DIR_SEPARATOR (*p))
  478. {
  479. p++;
  480. break;
  481. }
  482. while (claimfile)
  483. {
  484. /* Claim the file only if the file name and size match and don't
  485. match the whole library. */
  486. if (!strcmp (p, claimfile->file.name)
  487. && claimfile->file.filesize == file->filesize
  488. && (!islib || file->offset != 0))
  489. break;
  490. claimfile = claimfile->next;
  491. }
  492. free (name);
  493. /* If we decided to claim it, record that fact, and add any symbols
  494. that were defined for it by plugin options. */
  495. *claimed = (claimfile != 0);
  496. if (claimfile)
  497. {
  498. claimfile->claimed = true;
  499. claimfile->file = *file;
  500. if (claimfile->n_syms_used && !tv_add_symbols)
  501. return LDPS_ERR;
  502. else if (claimfile->n_syms_used)
  503. return (*tv_add_symbols) (claimfile->file.handle,
  504. claimfile->n_syms_used, claimfile->symbols);
  505. }
  506. return claim_file_ret;
  507. }
  508. /* Standard plugin API registerable hook. */
  509. static enum ld_plugin_status
  510. onall_symbols_read (void)
  511. {
  512. static const char *resolutions[] =
  513. {
  514. "LDPR_UNKNOWN",
  515. "LDPR_UNDEF",
  516. "LDPR_PREVAILING_DEF",
  517. "LDPR_PREVAILING_DEF_IRONLY",
  518. "LDPR_PREEMPTED_REG",
  519. "LDPR_PREEMPTED_IR",
  520. "LDPR_RESOLVED_IR",
  521. "LDPR_RESOLVED_EXEC",
  522. "LDPR_RESOLVED_DYN",
  523. "LDPR_PREVAILING_DEF_IRONLY_EXP",
  524. };
  525. claim_file_t *claimfile = dumpresolutions ? claimfiles_list : NULL;
  526. add_file_t *addfile = addfiles_list;
  527. struct ld_plugin_input_file file;
  528. const void *view;
  529. char buffer[30];
  530. int fd;
  531. char *filename;
  532. if (! allsymbolsread_silent)
  533. TV_MESSAGE (LDPL_INFO, "hook called: all symbols read.");
  534. for ( ; claimfile; claimfile = claimfile->next)
  535. {
  536. enum ld_plugin_status rv;
  537. int n;
  538. if (claimfile->n_syms_used && !tv_get_symbols_v2)
  539. return LDPS_ERR;
  540. else if (!claimfile->n_syms_used)
  541. continue;
  542. else if (!claimfile->file.handle)
  543. continue;
  544. rv = tv_get_input_file (claimfile->file.handle, &file);
  545. if (rv != LDPS_OK)
  546. return rv;
  547. TV_MESSAGE (LDPL_INFO, "Input: %s (%s)", file.name,
  548. claimfile->file.name);
  549. rv = tv_get_view (claimfile->file.handle, &view);
  550. if (rv != LDPS_OK)
  551. return rv;
  552. #define EXPECTED_VIEW "/* The first line of this file must match the expectation of"
  553. #define EXPECTED_VIEW_LENGTH (sizeof (EXPECTED_VIEW) - 1)
  554. if (file.filesize != SIZE_OF_FUNC_C
  555. || SIZE_OF_FUNC_C < EXPECTED_VIEW_LENGTH
  556. || memcmp (view, EXPECTED_VIEW, EXPECTED_VIEW_LENGTH) != 0)
  557. {
  558. char result[EXPECTED_VIEW_LENGTH + 1];
  559. memcpy (result, view, sizeof (result));
  560. result[EXPECTED_VIEW_LENGTH] = '\0';
  561. TV_MESSAGE (LDPL_INFO, "Incorrect view:");
  562. TV_MESSAGE (LDPL_INFO, " Expect: " EXPECTED_VIEW);
  563. TV_MESSAGE (LDPL_INFO, " Result: %s", result);
  564. }
  565. rv = tv_get_symbols_v2 (claimfile->file.handle, claimfile->n_syms_used,
  566. claimfile->symbols);
  567. if (rv != LDPS_OK)
  568. return rv;
  569. for (n = 0; n < claimfile->n_syms_used; n++)
  570. TV_MESSAGE (LDPL_INFO, "Sym: '%s%s%s' Resolution: %s",
  571. claimfile->symbols[n].name,
  572. claimfile->symbols[n].version ? "@" : "",
  573. (claimfile->symbols[n].version
  574. ? claimfile->symbols[n].version : ""),
  575. resolutions[claimfile->symbols[n].resolution]);
  576. fd = claimfile->file.fd;
  577. filename = xstrdup (claimfile->file.name);
  578. rv = tv_release_input_file (claimfile->file.handle);
  579. if (rv != LDPS_OK)
  580. {
  581. free (filename);
  582. return rv;
  583. }
  584. if (read (fd, buffer, sizeof (buffer)) >= 0)
  585. {
  586. TV_MESSAGE (LDPL_FATAL, "Unreleased file descriptor on: %s",
  587. claimfile->file.name);
  588. free (filename);
  589. return LDPS_ERR;
  590. }
  591. free (filename);
  592. }
  593. for ( ; addfile ; addfile = addfile->next)
  594. {
  595. enum ld_plugin_status rv;
  596. if (addfile->type == ADD_LIB && tv_add_input_library)
  597. rv = (*tv_add_input_library) (addfile->name);
  598. else if (addfile->type == ADD_FILE && tv_add_input_file)
  599. rv = (*tv_add_input_file) (addfile->name);
  600. else if (addfile->type == ADD_DIR && tv_set_extra_library_path)
  601. rv = (*tv_set_extra_library_path) (addfile->name);
  602. else
  603. rv = LDPS_ERR;
  604. if (rv != LDPS_OK)
  605. return rv;
  606. }
  607. fflush (NULL);
  608. return all_symbols_read_ret;
  609. }
  610. /* Standard plugin API registerable hook. */
  611. static enum ld_plugin_status
  612. oncleanup (void)
  613. {
  614. TV_MESSAGE (LDPL_INFO, "hook called: cleanup.");
  615. fflush (NULL);
  616. return cleanup_ret;
  617. }
  618. #endif /* BFD_SUPPORTS_PLUGINS */