format.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* Generic BFD support for file formats.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  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. /*
  18. SECTION
  19. File formats
  20. A format is a BFD concept of high level file contents type. The
  21. formats supported by BFD are:
  22. o <<bfd_object>>
  23. The BFD may contain data, symbols, relocations and debug info.
  24. o <<bfd_archive>>
  25. The BFD contains other BFDs and an optional index.
  26. o <<bfd_core>>
  27. The BFD contains the result of an executable core dump.
  28. SUBSECTION
  29. File format functions
  30. */
  31. #include "sysdep.h"
  32. #include "bfd.h"
  33. #include "libbfd.h"
  34. /* IMPORT from targets.c. */
  35. extern const size_t _bfd_target_vector_entries;
  36. /*
  37. FUNCTION
  38. bfd_check_format
  39. SYNOPSIS
  40. bool bfd_check_format (bfd *abfd, bfd_format format);
  41. DESCRIPTION
  42. Verify if the file attached to the BFD @var{abfd} is compatible
  43. with the format @var{format} (i.e., one of <<bfd_object>>,
  44. <<bfd_archive>> or <<bfd_core>>).
  45. If the BFD has been set to a specific target before the
  46. call, only the named target and format combination is
  47. checked. If the target has not been set, or has been set to
  48. <<default>>, then all the known target backends is
  49. interrogated to determine a match. If the default target
  50. matches, it is used. If not, exactly one target must recognize
  51. the file, or an error results.
  52. The function returns <<TRUE>> on success, otherwise <<FALSE>>
  53. with one of the following error codes:
  54. o <<bfd_error_invalid_operation>> -
  55. if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
  56. <<bfd_core>>.
  57. o <<bfd_error_system_call>> -
  58. if an error occured during a read - even some file mismatches
  59. can cause bfd_error_system_calls.
  60. o <<file_not_recognised>> -
  61. none of the backends recognised the file format.
  62. o <<bfd_error_file_ambiguously_recognized>> -
  63. more than one backend recognised the file format.
  64. */
  65. bool
  66. bfd_check_format (bfd *abfd, bfd_format format)
  67. {
  68. return bfd_check_format_matches (abfd, format, NULL);
  69. }
  70. struct bfd_preserve
  71. {
  72. void *marker;
  73. void *tdata;
  74. flagword flags;
  75. const struct bfd_arch_info *arch_info;
  76. struct bfd_section *sections;
  77. struct bfd_section *section_last;
  78. unsigned int section_count;
  79. unsigned int section_id;
  80. struct bfd_hash_table section_htab;
  81. const struct bfd_build_id *build_id;
  82. bfd_cleanup cleanup;
  83. };
  84. /* When testing an object for compatibility with a particular target
  85. back-end, the back-end object_p function needs to set up certain
  86. fields in the bfd on successfully recognizing the object. This
  87. typically happens in a piecemeal fashion, with failures possible at
  88. many points. On failure, the bfd is supposed to be restored to its
  89. initial state, which is virtually impossible. However, restoring a
  90. subset of the bfd state works in practice. This function stores
  91. the subset. */
  92. static bool
  93. bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
  94. bfd_cleanup cleanup)
  95. {
  96. preserve->tdata = abfd->tdata.any;
  97. preserve->arch_info = abfd->arch_info;
  98. preserve->flags = abfd->flags;
  99. preserve->sections = abfd->sections;
  100. preserve->section_last = abfd->section_last;
  101. preserve->section_count = abfd->section_count;
  102. preserve->section_id = _bfd_section_id;
  103. preserve->section_htab = abfd->section_htab;
  104. preserve->marker = bfd_alloc (abfd, 1);
  105. preserve->build_id = abfd->build_id;
  106. preserve->cleanup = cleanup;
  107. if (preserve->marker == NULL)
  108. return false;
  109. return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
  110. sizeof (struct section_hash_entry));
  111. }
  112. /* Clear out a subset of BFD state. */
  113. static void
  114. bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup)
  115. {
  116. _bfd_section_id = section_id;
  117. if (cleanup)
  118. cleanup (abfd);
  119. abfd->tdata.any = NULL;
  120. abfd->arch_info = &bfd_default_arch_struct;
  121. abfd->flags &= BFD_FLAGS_SAVED;
  122. abfd->build_id = NULL;
  123. bfd_section_list_clear (abfd);
  124. }
  125. /* Restores bfd state saved by bfd_preserve_save. */
  126. static bfd_cleanup
  127. bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
  128. {
  129. bfd_hash_table_free (&abfd->section_htab);
  130. abfd->tdata.any = preserve->tdata;
  131. abfd->arch_info = preserve->arch_info;
  132. abfd->flags = preserve->flags;
  133. abfd->section_htab = preserve->section_htab;
  134. abfd->sections = preserve->sections;
  135. abfd->section_last = preserve->section_last;
  136. abfd->section_count = preserve->section_count;
  137. _bfd_section_id = preserve->section_id;
  138. abfd->build_id = preserve->build_id;
  139. /* bfd_release frees all memory more recently bfd_alloc'd than
  140. its arg, as well as its arg. */
  141. bfd_release (abfd, preserve->marker);
  142. preserve->marker = NULL;
  143. return preserve->cleanup;
  144. }
  145. /* Called when the bfd state saved by bfd_preserve_save is no longer
  146. needed. */
  147. static void
  148. bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
  149. {
  150. if (preserve->cleanup)
  151. {
  152. /* Run the cleanup, assuming that all it will need is the
  153. tdata at the time the cleanup was returned. */
  154. void *tdata = abfd->tdata.any;
  155. abfd->tdata.any = preserve->tdata;
  156. preserve->cleanup (abfd);
  157. abfd->tdata.any = tdata;
  158. }
  159. /* It would be nice to be able to free more memory here, eg. old
  160. tdata, but that's not possible since these blocks are sitting
  161. inside bfd_alloc'd memory. The section hash is on a separate
  162. objalloc. */
  163. bfd_hash_table_free (&preserve->section_htab);
  164. preserve->marker = NULL;
  165. }
  166. /*
  167. FUNCTION
  168. bfd_check_format_matches
  169. SYNOPSIS
  170. bool bfd_check_format_matches
  171. (bfd *abfd, bfd_format format, char ***matching);
  172. DESCRIPTION
  173. Like <<bfd_check_format>>, except when it returns FALSE with
  174. <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
  175. case, if @var{matching} is not NULL, it will be filled in with
  176. a NULL-terminated list of the names of the formats that matched,
  177. allocated with <<malloc>>.
  178. Then the user may choose a format and try again.
  179. When done with the list that @var{matching} points to, the caller
  180. should free it.
  181. */
  182. bool
  183. bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
  184. {
  185. extern const bfd_target binary_vec;
  186. #if BFD_SUPPORTS_PLUGINS
  187. extern const bfd_target plugin_vec;
  188. #endif
  189. const bfd_target * const *target;
  190. const bfd_target **matching_vector = NULL;
  191. const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
  192. int match_count, best_count, best_match;
  193. int ar_match_index;
  194. unsigned int initial_section_id = _bfd_section_id;
  195. struct bfd_preserve preserve, preserve_match;
  196. bfd_cleanup cleanup = NULL;
  197. if (matching != NULL)
  198. *matching = NULL;
  199. if (!bfd_read_p (abfd)
  200. || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
  201. {
  202. bfd_set_error (bfd_error_invalid_operation);
  203. return false;
  204. }
  205. if (abfd->format != bfd_unknown)
  206. return abfd->format == format;
  207. if (matching != NULL || *bfd_associated_vector != NULL)
  208. {
  209. size_t amt;
  210. amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
  211. matching_vector = (const bfd_target **) bfd_malloc (amt);
  212. if (!matching_vector)
  213. return false;
  214. }
  215. /* Presume the answer is yes. */
  216. abfd->format = format;
  217. save_targ = abfd->xvec;
  218. preserve_match.marker = NULL;
  219. if (!bfd_preserve_save (abfd, &preserve, NULL))
  220. goto err_ret;
  221. /* If the target type was explicitly specified, just check that target. */
  222. if (!abfd->target_defaulted)
  223. {
  224. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
  225. goto err_ret;
  226. cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  227. if (cleanup)
  228. goto ok_ret;
  229. /* For a long time the code has dropped through to check all
  230. targets if the specified target was wrong. I don't know why,
  231. and I'm reluctant to change it. However, in the case of an
  232. archive, it can cause problems. If the specified target does
  233. not permit archives (e.g., the binary target), then we should
  234. not allow some other target to recognize it as an archive, but
  235. should instead allow the specified target to recognize it as an
  236. object. When I first made this change, it broke the PE target,
  237. because the specified pei-i386 target did not recognize the
  238. actual pe-i386 archive. Since there may be other problems of
  239. this sort, I changed this test to check only for the binary
  240. target. */
  241. if (format == bfd_archive && save_targ == &binary_vec)
  242. goto err_unrecog;
  243. }
  244. /* Since the target type was defaulted, check them all in the hope
  245. that one will be uniquely recognized. */
  246. right_targ = NULL;
  247. ar_right_targ = NULL;
  248. match_targ = NULL;
  249. best_match = 256;
  250. best_count = 0;
  251. match_count = 0;
  252. ar_match_index = _bfd_target_vector_entries;
  253. for (target = bfd_target_vector; *target != NULL; target++)
  254. {
  255. void **high_water;
  256. /* The binary target matches anything, so don't return it when
  257. searching. Don't match the plugin target if we have another
  258. alternative since we want to properly set the input format
  259. before allowing a plugin to claim the file. Also, don't
  260. check the default target twice. */
  261. if (*target == &binary_vec
  262. #if BFD_SUPPORTS_PLUGINS
  263. || (match_count != 0 && *target == &plugin_vec)
  264. #endif
  265. || (!abfd->target_defaulted && *target == save_targ))
  266. continue;
  267. /* If we already tried a match, the bfd is modified and may
  268. have sections attached, which will confuse the next
  269. _bfd_check_format call. */
  270. bfd_reinit (abfd, initial_section_id, cleanup);
  271. /* Free bfd_alloc memory too. If we have matched and preserved
  272. a target then the high water mark is that much higher. */
  273. if (preserve_match.marker)
  274. high_water = &preserve_match.marker;
  275. else
  276. high_water = &preserve.marker;
  277. bfd_release (abfd, *high_water);
  278. *high_water = bfd_alloc (abfd, 1);
  279. /* Change BFD's target temporarily. */
  280. abfd->xvec = *target;
  281. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  282. goto err_ret;
  283. cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  284. if (cleanup)
  285. {
  286. int match_priority = abfd->xvec->match_priority;
  287. #if BFD_SUPPORTS_PLUGINS
  288. /* If this object can be handled by a plugin, give that the
  289. lowest priority; objects both handled by a plugin and
  290. with an underlying object format will be claimed
  291. separately by the plugin. */
  292. if (*target == &plugin_vec)
  293. match_priority = (*target)->match_priority;
  294. #endif
  295. if (abfd->format != bfd_archive
  296. || (bfd_has_map (abfd)
  297. && bfd_get_error () != bfd_error_wrong_object_format))
  298. {
  299. /* If this is the default target, accept it, even if
  300. other targets might match. People who want those
  301. other targets have to set the GNUTARGET variable. */
  302. if (abfd->xvec == bfd_default_vector[0])
  303. goto ok_ret;
  304. if (matching_vector)
  305. matching_vector[match_count] = abfd->xvec;
  306. match_count++;
  307. if (match_priority < best_match)
  308. {
  309. best_match = match_priority;
  310. best_count = 0;
  311. }
  312. if (match_priority <= best_match)
  313. {
  314. /* This format checks out as ok! */
  315. right_targ = abfd->xvec;
  316. best_count++;
  317. }
  318. }
  319. else
  320. {
  321. /* An archive with no armap or objects of the wrong
  322. type. We want this target to match if we get no
  323. better matches. */
  324. if (ar_right_targ != bfd_default_vector[0])
  325. ar_right_targ = *target;
  326. if (matching_vector)
  327. matching_vector[ar_match_index] = *target;
  328. ar_match_index++;
  329. }
  330. if (preserve_match.marker == NULL)
  331. {
  332. match_targ = abfd->xvec;
  333. if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
  334. goto err_ret;
  335. cleanup = NULL;
  336. }
  337. }
  338. }
  339. if (best_count == 1)
  340. match_count = 1;
  341. if (match_count == 0)
  342. {
  343. /* Try partial matches. */
  344. right_targ = ar_right_targ;
  345. if (right_targ == bfd_default_vector[0])
  346. {
  347. match_count = 1;
  348. }
  349. else
  350. {
  351. match_count = ar_match_index - _bfd_target_vector_entries;
  352. if (matching_vector && match_count > 1)
  353. memcpy (matching_vector,
  354. matching_vector + _bfd_target_vector_entries,
  355. sizeof (*matching_vector) * match_count);
  356. }
  357. }
  358. /* We have more than one equally good match. If any of the best
  359. matches is a target in config.bfd targ_defvec or targ_selvecs,
  360. choose it. */
  361. if (match_count > 1)
  362. {
  363. const bfd_target * const *assoc = bfd_associated_vector;
  364. while ((right_targ = *assoc++) != NULL)
  365. {
  366. int i = match_count;
  367. while (--i >= 0)
  368. if (matching_vector[i] == right_targ
  369. && right_targ->match_priority <= best_match)
  370. break;
  371. if (i >= 0)
  372. {
  373. match_count = 1;
  374. break;
  375. }
  376. }
  377. }
  378. /* We still have more than one equally good match, and at least some
  379. of the targets support match priority. Choose the first of the
  380. best matches. */
  381. if (matching_vector && match_count > 1 && best_count != match_count)
  382. {
  383. int i;
  384. for (i = 0; i < match_count; i++)
  385. {
  386. right_targ = matching_vector[i];
  387. if (right_targ->match_priority <= best_match)
  388. break;
  389. }
  390. match_count = 1;
  391. }
  392. /* There is way too much undoing of half-known state here. We
  393. really shouldn't iterate on live bfd's. Note that saving the
  394. whole bfd and restoring it would be even worse; the first thing
  395. you notice is that the cached bfd file position gets out of sync. */
  396. if (preserve_match.marker != NULL)
  397. cleanup = bfd_preserve_restore (abfd, &preserve_match);
  398. if (match_count == 1)
  399. {
  400. abfd->xvec = right_targ;
  401. /* If we come out of the loop knowing that the last target that
  402. matched is the one we want, then ABFD should still be in a usable
  403. state (except possibly for XVEC). This is not just an
  404. optimisation. In the case of plugins a match against the
  405. plugin target can result in the bfd being changed such that
  406. it no longer matches the plugin target, nor will it match
  407. RIGHT_TARG again. */
  408. if (match_targ != right_targ)
  409. {
  410. bfd_reinit (abfd, initial_section_id, cleanup);
  411. bfd_release (abfd, preserve.marker);
  412. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  413. goto err_ret;
  414. cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  415. BFD_ASSERT (cleanup != NULL);
  416. }
  417. ok_ret:
  418. /* If the file was opened for update, then `output_has_begun'
  419. some time ago when the file was created. Do not recompute
  420. sections sizes or alignments in _bfd_set_section_contents.
  421. We can not set this flag until after checking the format,
  422. because it will interfere with creation of BFD sections. */
  423. if (abfd->direction == both_direction)
  424. abfd->output_has_begun = true;
  425. free (matching_vector);
  426. if (preserve_match.marker != NULL)
  427. bfd_preserve_finish (abfd, &preserve_match);
  428. bfd_preserve_finish (abfd, &preserve);
  429. /* File position has moved, BTW. */
  430. return true;
  431. }
  432. if (match_count == 0)
  433. {
  434. err_unrecog:
  435. bfd_set_error (bfd_error_file_not_recognized);
  436. err_ret:
  437. if (cleanup)
  438. cleanup (abfd);
  439. abfd->xvec = save_targ;
  440. abfd->format = bfd_unknown;
  441. free (matching_vector);
  442. if (preserve_match.marker != NULL)
  443. bfd_preserve_finish (abfd, &preserve_match);
  444. bfd_preserve_restore (abfd, &preserve);
  445. return false;
  446. }
  447. /* Restore original target type and format. */
  448. abfd->xvec = save_targ;
  449. abfd->format = bfd_unknown;
  450. bfd_set_error (bfd_error_file_ambiguously_recognized);
  451. if (matching)
  452. {
  453. *matching = (char **) matching_vector;
  454. matching_vector[match_count] = NULL;
  455. /* Return target names. This is a little nasty. Maybe we
  456. should do another bfd_malloc? */
  457. while (--match_count >= 0)
  458. {
  459. const char *name = matching_vector[match_count]->name;
  460. *(const char **) &matching_vector[match_count] = name;
  461. }
  462. }
  463. else
  464. free (matching_vector);
  465. if (cleanup)
  466. cleanup (abfd);
  467. if (preserve_match.marker != NULL)
  468. bfd_preserve_finish (abfd, &preserve_match);
  469. bfd_preserve_restore (abfd, &preserve);
  470. return false;
  471. }
  472. /*
  473. FUNCTION
  474. bfd_set_format
  475. SYNOPSIS
  476. bool bfd_set_format (bfd *abfd, bfd_format format);
  477. DESCRIPTION
  478. This function sets the file format of the BFD @var{abfd} to the
  479. format @var{format}. If the target set in the BFD does not
  480. support the format requested, the format is invalid, or the BFD
  481. is not open for writing, then an error occurs.
  482. */
  483. bool
  484. bfd_set_format (bfd *abfd, bfd_format format)
  485. {
  486. if (bfd_read_p (abfd)
  487. || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
  488. {
  489. bfd_set_error (bfd_error_invalid_operation);
  490. return false;
  491. }
  492. if (abfd->format != bfd_unknown)
  493. return abfd->format == format;
  494. /* Presume the answer is yes. */
  495. abfd->format = format;
  496. if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
  497. {
  498. abfd->format = bfd_unknown;
  499. return false;
  500. }
  501. return true;
  502. }
  503. /*
  504. FUNCTION
  505. bfd_format_string
  506. SYNOPSIS
  507. const char *bfd_format_string (bfd_format format);
  508. DESCRIPTION
  509. Return a pointer to a const string
  510. <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
  511. depending upon the value of @var{format}.
  512. */
  513. const char *
  514. bfd_format_string (bfd_format format)
  515. {
  516. if (((int) format < (int) bfd_unknown)
  517. || ((int) format >= (int) bfd_type_end))
  518. return "invalid";
  519. switch (format)
  520. {
  521. case bfd_object:
  522. return "object"; /* Linker/assembler/compiler output. */
  523. case bfd_archive:
  524. return "archive"; /* Object archive file. */
  525. case bfd_core:
  526. return "core"; /* Core dump. */
  527. default:
  528. return "unknown";
  529. }
  530. }