format.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* Generic BFD support for file formats.
  2. Copyright (C) 1990-2015 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. bfd_boolean 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. bfd_boolean
  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. struct bfd_hash_table section_htab;
  80. };
  81. /* When testing an object for compatibility with a particular target
  82. back-end, the back-end object_p function needs to set up certain
  83. fields in the bfd on successfully recognizing the object. This
  84. typically happens in a piecemeal fashion, with failures possible at
  85. many points. On failure, the bfd is supposed to be restored to its
  86. initial state, which is virtually impossible. However, restoring a
  87. subset of the bfd state works in practice. This function stores
  88. the subset. */
  89. static bfd_boolean
  90. bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
  91. {
  92. preserve->tdata = abfd->tdata.any;
  93. preserve->arch_info = abfd->arch_info;
  94. preserve->flags = abfd->flags;
  95. preserve->sections = abfd->sections;
  96. preserve->section_last = abfd->section_last;
  97. preserve->section_count = abfd->section_count;
  98. preserve->section_htab = abfd->section_htab;
  99. preserve->marker = bfd_alloc (abfd, 1);
  100. if (preserve->marker == NULL)
  101. return FALSE;
  102. return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
  103. sizeof (struct section_hash_entry));
  104. }
  105. /* Clear out a subset of BFD state. */
  106. static void
  107. bfd_reinit (bfd *abfd)
  108. {
  109. abfd->tdata.any = NULL;
  110. abfd->arch_info = &bfd_default_arch_struct;
  111. abfd->flags &= BFD_FLAGS_SAVED;
  112. bfd_section_list_clear (abfd);
  113. }
  114. /* Restores bfd state saved by bfd_preserve_save. */
  115. static void
  116. bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
  117. {
  118. bfd_hash_table_free (&abfd->section_htab);
  119. abfd->tdata.any = preserve->tdata;
  120. abfd->arch_info = preserve->arch_info;
  121. abfd->flags = preserve->flags;
  122. abfd->section_htab = preserve->section_htab;
  123. abfd->sections = preserve->sections;
  124. abfd->section_last = preserve->section_last;
  125. abfd->section_count = preserve->section_count;
  126. /* bfd_release frees all memory more recently bfd_alloc'd than
  127. its arg, as well as its arg. */
  128. bfd_release (abfd, preserve->marker);
  129. preserve->marker = NULL;
  130. }
  131. /* Called when the bfd state saved by bfd_preserve_save is no longer
  132. needed. */
  133. static void
  134. bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
  135. {
  136. /* It would be nice to be able to free more memory here, eg. old
  137. tdata, but that's not possible since these blocks are sitting
  138. inside bfd_alloc'd memory. The section hash is on a separate
  139. objalloc. */
  140. bfd_hash_table_free (&preserve->section_htab);
  141. preserve->marker = NULL;
  142. }
  143. /*
  144. FUNCTION
  145. bfd_check_format_matches
  146. SYNOPSIS
  147. bfd_boolean bfd_check_format_matches
  148. (bfd *abfd, bfd_format format, char ***matching);
  149. DESCRIPTION
  150. Like <<bfd_check_format>>, except when it returns FALSE with
  151. <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
  152. case, if @var{matching} is not NULL, it will be filled in with
  153. a NULL-terminated list of the names of the formats that matched,
  154. allocated with <<malloc>>.
  155. Then the user may choose a format and try again.
  156. When done with the list that @var{matching} points to, the caller
  157. should free it.
  158. */
  159. bfd_boolean
  160. bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
  161. {
  162. extern const bfd_target binary_vec;
  163. const bfd_target * const *target;
  164. const bfd_target **matching_vector = NULL;
  165. const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
  166. int match_count, best_count, best_match;
  167. int ar_match_index;
  168. struct bfd_preserve preserve;
  169. if (matching != NULL)
  170. *matching = NULL;
  171. if (!bfd_read_p (abfd)
  172. || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
  173. {
  174. bfd_set_error (bfd_error_invalid_operation);
  175. return FALSE;
  176. }
  177. if (abfd->format != bfd_unknown)
  178. return abfd->format == format;
  179. if (matching != NULL || *bfd_associated_vector != NULL)
  180. {
  181. bfd_size_type amt;
  182. amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
  183. matching_vector = (const bfd_target **) bfd_malloc (amt);
  184. if (!matching_vector)
  185. return FALSE;
  186. }
  187. /* Presume the answer is yes. */
  188. abfd->format = format;
  189. save_targ = abfd->xvec;
  190. preserve.marker = NULL;
  191. /* If the target type was explicitly specified, just check that target. */
  192. if (!abfd->target_defaulted)
  193. {
  194. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) /* rewind! */
  195. goto err_ret;
  196. right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  197. if (right_targ)
  198. goto ok_ret;
  199. /* For a long time the code has dropped through to check all
  200. targets if the specified target was wrong. I don't know why,
  201. and I'm reluctant to change it. However, in the case of an
  202. archive, it can cause problems. If the specified target does
  203. not permit archives (e.g., the binary target), then we should
  204. not allow some other target to recognize it as an archive, but
  205. should instead allow the specified target to recognize it as an
  206. object. When I first made this change, it broke the PE target,
  207. because the specified pei-i386 target did not recognize the
  208. actual pe-i386 archive. Since there may be other problems of
  209. this sort, I changed this test to check only for the binary
  210. target. */
  211. if (format == bfd_archive && save_targ == &binary_vec)
  212. goto err_unrecog;
  213. }
  214. /* Since the target type was defaulted, check them all in the hope
  215. that one will be uniquely recognized. */
  216. right_targ = NULL;
  217. ar_right_targ = NULL;
  218. match_targ = NULL;
  219. best_match = 256;
  220. best_count = 0;
  221. match_count = 0;
  222. ar_match_index = _bfd_target_vector_entries;
  223. for (target = bfd_target_vector; *target != NULL; target++)
  224. {
  225. const bfd_target *temp;
  226. /* Don't check the default target twice. */
  227. if (*target == &binary_vec
  228. || (!abfd->target_defaulted && *target == save_targ)
  229. || (*target)->match_priority > best_match)
  230. continue;
  231. /* If we already tried a match, the bfd is modified and may
  232. have sections attached, which will confuse the next
  233. _bfd_check_format call. */
  234. bfd_reinit (abfd);
  235. /* Change BFD's target temporarily. */
  236. abfd->xvec = *target;
  237. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  238. goto err_ret;
  239. /* If _bfd_check_format neglects to set bfd_error, assume
  240. bfd_error_wrong_format. We didn't used to even pay any
  241. attention to bfd_error, so I suspect that some
  242. _bfd_check_format might have this problem. */
  243. bfd_set_error (bfd_error_wrong_format);
  244. temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  245. if (temp)
  246. {
  247. match_targ = temp;
  248. if (preserve.marker != NULL)
  249. bfd_preserve_finish (abfd, &preserve);
  250. if (abfd->format != bfd_archive
  251. || (bfd_has_map (abfd)
  252. && bfd_get_error () != bfd_error_wrong_object_format))
  253. {
  254. /* This format checks out as ok! */
  255. right_targ = temp;
  256. /* If this is the default target, accept it, even if
  257. other targets might match. People who want those
  258. other targets have to set the GNUTARGET variable. */
  259. if (temp == bfd_default_vector[0])
  260. goto ok_ret;
  261. if (matching_vector)
  262. matching_vector[match_count] = temp;
  263. match_count++;
  264. if (temp->match_priority < best_match)
  265. {
  266. best_match = temp->match_priority;
  267. best_count = 0;
  268. }
  269. best_count++;
  270. }
  271. else
  272. {
  273. /* An archive with no armap or objects of the wrong
  274. type. We want this target to match if we get no
  275. better matches. */
  276. if (ar_right_targ != bfd_default_vector[0])
  277. ar_right_targ = *target;
  278. if (matching_vector)
  279. matching_vector[ar_match_index] = *target;
  280. ar_match_index++;
  281. }
  282. if (!bfd_preserve_save (abfd, &preserve))
  283. goto err_ret;
  284. }
  285. else if (bfd_get_error () != bfd_error_wrong_format)
  286. goto err_ret;
  287. }
  288. if (best_count == 1)
  289. match_count = 1;
  290. if (match_count == 0)
  291. {
  292. /* Try partial matches. */
  293. right_targ = ar_right_targ;
  294. if (right_targ == bfd_default_vector[0])
  295. {
  296. match_count = 1;
  297. }
  298. else
  299. {
  300. match_count = ar_match_index - _bfd_target_vector_entries;
  301. if (matching_vector && match_count > 1)
  302. memcpy (matching_vector,
  303. matching_vector + _bfd_target_vector_entries,
  304. sizeof (*matching_vector) * match_count);
  305. }
  306. }
  307. /* We have more than one equally good match. If any of the best
  308. matches is a target in config.bfd targ_defvec or targ_selvecs,
  309. choose it. */
  310. if (match_count > 1)
  311. {
  312. const bfd_target * const *assoc = bfd_associated_vector;
  313. while ((right_targ = *assoc++) != NULL)
  314. {
  315. int i = match_count;
  316. while (--i >= 0)
  317. if (matching_vector[i] == right_targ
  318. && right_targ->match_priority <= best_match)
  319. break;
  320. if (i >= 0)
  321. {
  322. match_count = 1;
  323. break;
  324. }
  325. }
  326. }
  327. /* We still have more than one equally good match, and at least some
  328. of the targets support match priority. Choose the first of the
  329. best matches. */
  330. if (matching_vector && match_count > 1 && best_count != match_count)
  331. {
  332. int i;
  333. for (i = 0; i < match_count; i++)
  334. {
  335. right_targ = matching_vector[i];
  336. if (right_targ->match_priority <= best_match)
  337. break;
  338. }
  339. match_count = 1;
  340. }
  341. /* There is way too much undoing of half-known state here. We
  342. really shouldn't iterate on live bfd's. Note that saving the
  343. whole bfd and restoring it would be even worse; the first thing
  344. you notice is that the cached bfd file position gets out of sync. */
  345. if (preserve.marker != NULL)
  346. bfd_preserve_restore (abfd, &preserve);
  347. if (match_count == 1)
  348. {
  349. abfd->xvec = right_targ;
  350. /* If we come out of the loop knowing that the last target that
  351. matched is the one we want, then ABFD should still be in a usable
  352. state (except possibly for XVEC). */
  353. if (match_targ != right_targ)
  354. {
  355. bfd_reinit (abfd);
  356. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  357. goto err_ret;
  358. match_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  359. BFD_ASSERT (match_targ != NULL);
  360. }
  361. ok_ret:
  362. /* If the file was opened for update, then `output_has_begun'
  363. some time ago when the file was created. Do not recompute
  364. sections sizes or alignments in _bfd_set_section_contents.
  365. We can not set this flag until after checking the format,
  366. because it will interfere with creation of BFD sections. */
  367. if (abfd->direction == both_direction)
  368. abfd->output_has_begun = TRUE;
  369. if (matching_vector)
  370. free (matching_vector);
  371. /* File position has moved, BTW. */
  372. return TRUE;
  373. }
  374. if (match_count == 0)
  375. {
  376. err_unrecog:
  377. bfd_set_error (bfd_error_file_not_recognized);
  378. err_ret:
  379. abfd->xvec = save_targ;
  380. abfd->format = bfd_unknown;
  381. if (matching_vector)
  382. free (matching_vector);
  383. if (preserve.marker != NULL)
  384. bfd_preserve_restore (abfd, &preserve);
  385. return FALSE;
  386. }
  387. /* Restore original target type and format. */
  388. abfd->xvec = save_targ;
  389. abfd->format = bfd_unknown;
  390. bfd_set_error (bfd_error_file_ambiguously_recognized);
  391. if (matching)
  392. {
  393. *matching = (char **) matching_vector;
  394. matching_vector[match_count] = NULL;
  395. /* Return target names. This is a little nasty. Maybe we
  396. should do another bfd_malloc? */
  397. while (--match_count >= 0)
  398. {
  399. const char *name = matching_vector[match_count]->name;
  400. *(const char **) &matching_vector[match_count] = name;
  401. }
  402. }
  403. return FALSE;
  404. }
  405. /*
  406. FUNCTION
  407. bfd_set_format
  408. SYNOPSIS
  409. bfd_boolean bfd_set_format (bfd *abfd, bfd_format format);
  410. DESCRIPTION
  411. This function sets the file format of the BFD @var{abfd} to the
  412. format @var{format}. If the target set in the BFD does not
  413. support the format requested, the format is invalid, or the BFD
  414. is not open for writing, then an error occurs.
  415. */
  416. bfd_boolean
  417. bfd_set_format (bfd *abfd, bfd_format format)
  418. {
  419. if (bfd_read_p (abfd)
  420. || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
  421. {
  422. bfd_set_error (bfd_error_invalid_operation);
  423. return FALSE;
  424. }
  425. if (abfd->format != bfd_unknown)
  426. return abfd->format == format;
  427. /* Presume the answer is yes. */
  428. abfd->format = format;
  429. if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
  430. {
  431. abfd->format = bfd_unknown;
  432. return FALSE;
  433. }
  434. return TRUE;
  435. }
  436. /*
  437. FUNCTION
  438. bfd_format_string
  439. SYNOPSIS
  440. const char *bfd_format_string (bfd_format format);
  441. DESCRIPTION
  442. Return a pointer to a const string
  443. <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
  444. depending upon the value of @var{format}.
  445. */
  446. const char *
  447. bfd_format_string (bfd_format format)
  448. {
  449. if (((int) format < (int) bfd_unknown)
  450. || ((int) format >= (int) bfd_type_end))
  451. return "invalid";
  452. switch (format)
  453. {
  454. case bfd_object:
  455. return "object"; /* Linker/assembler/compiler output. */
  456. case bfd_archive:
  457. return "archive"; /* Object archive file. */
  458. case bfd_core:
  459. return "core"; /* Core dump. */
  460. default:
  461. return "unknown";
  462. }
  463. }