elf-attrs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /* ELF attributes support (based on ARM EABI attributes).
  2. Copyright (C) 2005-2015 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. #include "libiberty.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. /* Return the number of bytes needed by I in uleb128 format. */
  22. static int
  23. uleb128_size (unsigned int i)
  24. {
  25. int size;
  26. size = 1;
  27. while (i >= 0x80)
  28. {
  29. i >>= 7;
  30. size++;
  31. }
  32. return size;
  33. }
  34. /* Return TRUE if the attribute has the default value (0/""). */
  35. static bfd_boolean
  36. is_default_attr (obj_attribute *attr)
  37. {
  38. if (ATTR_TYPE_HAS_INT_VAL (attr->type) && attr->i != 0)
  39. return FALSE;
  40. if (ATTR_TYPE_HAS_STR_VAL (attr->type) && attr->s && *attr->s)
  41. return FALSE;
  42. if (ATTR_TYPE_HAS_NO_DEFAULT (attr->type))
  43. return FALSE;
  44. return TRUE;
  45. }
  46. /* Return the size of a single attribute. */
  47. static bfd_vma
  48. obj_attr_size (unsigned int tag, obj_attribute *attr)
  49. {
  50. bfd_vma size;
  51. if (is_default_attr (attr))
  52. return 0;
  53. size = uleb128_size (tag);
  54. if (ATTR_TYPE_HAS_INT_VAL (attr->type))
  55. size += uleb128_size (attr->i);
  56. if (ATTR_TYPE_HAS_STR_VAL (attr->type))
  57. size += strlen ((char *)attr->s) + 1;
  58. return size;
  59. }
  60. /* Return the vendor name for a given object attributes section. */
  61. static const char *
  62. vendor_obj_attr_name (bfd *abfd, int vendor)
  63. {
  64. return (vendor == OBJ_ATTR_PROC
  65. ? get_elf_backend_data (abfd)->obj_attrs_vendor
  66. : "gnu");
  67. }
  68. /* Return the size of the object attributes section for VENDOR
  69. (OBJ_ATTR_PROC or OBJ_ATTR_GNU), or 0 if there are no attributes
  70. for that vendor to record and the vendor is OBJ_ATTR_GNU. */
  71. static bfd_vma
  72. vendor_obj_attr_size (bfd *abfd, int vendor)
  73. {
  74. bfd_vma size;
  75. obj_attribute *attr;
  76. obj_attribute_list *list;
  77. int i;
  78. const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
  79. if (!vendor_name)
  80. return 0;
  81. attr = elf_known_obj_attributes (abfd)[vendor];
  82. size = 0;
  83. for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
  84. size += obj_attr_size (i, &attr[i]);
  85. for (list = elf_other_obj_attributes (abfd)[vendor];
  86. list;
  87. list = list->next)
  88. size += obj_attr_size (list->tag, &list->attr);
  89. /* <size> <vendor_name> NUL 0x1 <size> */
  90. return ((size || vendor == OBJ_ATTR_PROC)
  91. ? size + 10 + strlen (vendor_name)
  92. : 0);
  93. }
  94. /* Return the size of the object attributes section. */
  95. bfd_vma
  96. bfd_elf_obj_attr_size (bfd *abfd)
  97. {
  98. bfd_vma size;
  99. size = vendor_obj_attr_size (abfd, OBJ_ATTR_PROC);
  100. size += vendor_obj_attr_size (abfd, OBJ_ATTR_GNU);
  101. /* 'A' <sections for each vendor> */
  102. return (size ? size + 1 : 0);
  103. }
  104. /* Write VAL in uleb128 format to P, returning a pointer to the
  105. following byte. */
  106. static bfd_byte *
  107. write_uleb128 (bfd_byte *p, unsigned int val)
  108. {
  109. bfd_byte c;
  110. do
  111. {
  112. c = val & 0x7f;
  113. val >>= 7;
  114. if (val)
  115. c |= 0x80;
  116. *(p++) = c;
  117. }
  118. while (val);
  119. return p;
  120. }
  121. /* Write attribute ATTR to butter P, and return a pointer to the following
  122. byte. */
  123. static bfd_byte *
  124. write_obj_attribute (bfd_byte *p, unsigned int tag, obj_attribute *attr)
  125. {
  126. /* Suppress default entries. */
  127. if (is_default_attr (attr))
  128. return p;
  129. p = write_uleb128 (p, tag);
  130. if (ATTR_TYPE_HAS_INT_VAL (attr->type))
  131. p = write_uleb128 (p, attr->i);
  132. if (ATTR_TYPE_HAS_STR_VAL (attr->type))
  133. {
  134. int len;
  135. len = strlen (attr->s) + 1;
  136. memcpy (p, attr->s, len);
  137. p += len;
  138. }
  139. return p;
  140. }
  141. /* Write the contents of the object attributes section (length SIZE)
  142. for VENDOR to CONTENTS. */
  143. static void
  144. vendor_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size,
  145. int vendor)
  146. {
  147. bfd_byte *p;
  148. obj_attribute *attr;
  149. obj_attribute_list *list;
  150. int i;
  151. const char *vendor_name = vendor_obj_attr_name (abfd, vendor);
  152. size_t vendor_length = strlen (vendor_name) + 1;
  153. p = contents;
  154. bfd_put_32 (abfd, size, p);
  155. p += 4;
  156. memcpy (p, vendor_name, vendor_length);
  157. p += vendor_length;
  158. *(p++) = Tag_File;
  159. bfd_put_32 (abfd, size - 4 - vendor_length, p);
  160. p += 4;
  161. attr = elf_known_obj_attributes (abfd)[vendor];
  162. for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
  163. {
  164. unsigned int tag = i;
  165. if (get_elf_backend_data (abfd)->obj_attrs_order)
  166. tag = get_elf_backend_data (abfd)->obj_attrs_order (i);
  167. p = write_obj_attribute (p, tag, &attr[tag]);
  168. }
  169. for (list = elf_other_obj_attributes (abfd)[vendor];
  170. list;
  171. list = list->next)
  172. p = write_obj_attribute (p, list->tag, &list->attr);
  173. }
  174. /* Write the contents of the object attributes section to CONTENTS. */
  175. void
  176. bfd_elf_set_obj_attr_contents (bfd *abfd, bfd_byte *contents, bfd_vma size)
  177. {
  178. bfd_byte *p;
  179. int vendor;
  180. bfd_vma my_size;
  181. p = contents;
  182. *(p++) = 'A';
  183. my_size = 1;
  184. for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
  185. {
  186. bfd_vma vendor_size = vendor_obj_attr_size (abfd, vendor);
  187. if (vendor_size)
  188. vendor_set_obj_attr_contents (abfd, p, vendor_size, vendor);
  189. p += vendor_size;
  190. my_size += vendor_size;
  191. }
  192. if (size != my_size)
  193. abort ();
  194. }
  195. /* Allocate/find an object attribute. */
  196. static obj_attribute *
  197. elf_new_obj_attr (bfd *abfd, int vendor, unsigned int tag)
  198. {
  199. obj_attribute *attr;
  200. obj_attribute_list *list;
  201. obj_attribute_list *p;
  202. obj_attribute_list **lastp;
  203. if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
  204. {
  205. /* Known tags are preallocated. */
  206. attr = &elf_known_obj_attributes (abfd)[vendor][tag];
  207. }
  208. else
  209. {
  210. /* Create a new tag. */
  211. list = (obj_attribute_list *)
  212. bfd_alloc (abfd, sizeof (obj_attribute_list));
  213. memset (list, 0, sizeof (obj_attribute_list));
  214. list->tag = tag;
  215. /* Keep the tag list in order. */
  216. lastp = &elf_other_obj_attributes (abfd)[vendor];
  217. for (p = *lastp; p; p = p->next)
  218. {
  219. if (tag < p->tag)
  220. break;
  221. lastp = &p->next;
  222. }
  223. list->next = *lastp;
  224. *lastp = list;
  225. attr = &list->attr;
  226. }
  227. return attr;
  228. }
  229. /* Return the value of an integer object attribute. */
  230. int
  231. bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, unsigned int tag)
  232. {
  233. obj_attribute_list *p;
  234. if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
  235. {
  236. /* Known tags are preallocated. */
  237. return elf_known_obj_attributes (abfd)[vendor][tag].i;
  238. }
  239. else
  240. {
  241. for (p = elf_other_obj_attributes (abfd)[vendor];
  242. p;
  243. p = p->next)
  244. {
  245. if (tag == p->tag)
  246. return p->attr.i;
  247. if (tag < p->tag)
  248. break;
  249. }
  250. return 0;
  251. }
  252. }
  253. /* Add an integer object attribute. */
  254. void
  255. bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i)
  256. {
  257. obj_attribute *attr;
  258. attr = elf_new_obj_attr (abfd, vendor, tag);
  259. attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
  260. attr->i = i;
  261. }
  262. /* Duplicate an object attribute string value. */
  263. char *
  264. _bfd_elf_attr_strdup (bfd *abfd, const char * s)
  265. {
  266. char * p;
  267. int len;
  268. len = strlen (s) + 1;
  269. p = (char *) bfd_alloc (abfd, len);
  270. return (char *) memcpy (p, s, len);
  271. }
  272. /* Add a string object attribute. */
  273. void
  274. bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag, const char *s)
  275. {
  276. obj_attribute *attr;
  277. attr = elf_new_obj_attr (abfd, vendor, tag);
  278. attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
  279. attr->s = _bfd_elf_attr_strdup (abfd, s);
  280. }
  281. /* Add a int+string object attribute. */
  282. void
  283. bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor,
  284. unsigned int tag,
  285. unsigned int i, const char *s)
  286. {
  287. obj_attribute *attr;
  288. attr = elf_new_obj_attr (abfd, vendor, tag);
  289. attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
  290. attr->i = i;
  291. attr->s = _bfd_elf_attr_strdup (abfd, s);
  292. }
  293. /* Copy the object attributes from IBFD to OBFD. */
  294. void
  295. _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
  296. {
  297. obj_attribute *in_attr;
  298. obj_attribute *out_attr;
  299. obj_attribute_list *list;
  300. int i;
  301. int vendor;
  302. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  303. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  304. return;
  305. for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
  306. {
  307. in_attr
  308. = &elf_known_obj_attributes (ibfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
  309. out_attr
  310. = &elf_known_obj_attributes (obfd)[vendor][LEAST_KNOWN_OBJ_ATTRIBUTE];
  311. for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
  312. {
  313. out_attr->type = in_attr->type;
  314. out_attr->i = in_attr->i;
  315. if (in_attr->s && *in_attr->s)
  316. out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
  317. in_attr++;
  318. out_attr++;
  319. }
  320. for (list = elf_other_obj_attributes (ibfd)[vendor];
  321. list;
  322. list = list->next)
  323. {
  324. in_attr = &list->attr;
  325. switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
  326. {
  327. case ATTR_TYPE_FLAG_INT_VAL:
  328. bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i);
  329. break;
  330. case ATTR_TYPE_FLAG_STR_VAL:
  331. bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
  332. in_attr->s);
  333. break;
  334. case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
  335. bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag,
  336. in_attr->i, in_attr->s);
  337. break;
  338. default:
  339. abort ();
  340. }
  341. }
  342. }
  343. }
  344. /* Determine whether a GNU object attribute tag takes an integer, a
  345. string or both. */
  346. static int
  347. gnu_obj_attrs_arg_type (unsigned int tag)
  348. {
  349. /* Except for Tag_compatibility, for GNU attributes we follow the
  350. same rule ARM ones > 32 follow: odd-numbered tags take strings
  351. and even-numbered tags take integers. In addition, tag & 2 is
  352. nonzero for architecture-independent tags and zero for
  353. architecture-dependent ones. */
  354. if (tag == Tag_compatibility)
  355. return 3;
  356. else
  357. return (tag & 1) != 0 ? 2 : 1;
  358. }
  359. /* Determine what arguments an attribute tag takes. */
  360. int
  361. _bfd_elf_obj_attrs_arg_type (bfd *abfd, int vendor, unsigned int tag)
  362. {
  363. switch (vendor)
  364. {
  365. case OBJ_ATTR_PROC:
  366. return get_elf_backend_data (abfd)->obj_attrs_arg_type (tag);
  367. break;
  368. case OBJ_ATTR_GNU:
  369. return gnu_obj_attrs_arg_type (tag);
  370. break;
  371. default:
  372. abort ();
  373. }
  374. }
  375. /* Parse an object attributes section. */
  376. void
  377. _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
  378. {
  379. bfd_byte *contents;
  380. bfd_byte *p;
  381. bfd_byte *p_end;
  382. bfd_vma len;
  383. const char *std_sec;
  384. /* PR 17512: file: 2844a11d. */
  385. if (hdr->sh_size == 0)
  386. return;
  387. contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
  388. if (!contents)
  389. return;
  390. if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
  391. hdr->sh_size))
  392. {
  393. free (contents);
  394. return;
  395. }
  396. p = contents;
  397. p_end = p + hdr->sh_size;
  398. std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor;
  399. if (*(p++) == 'A')
  400. {
  401. len = hdr->sh_size - 1;
  402. while (len > 0 && p < p_end - 4)
  403. {
  404. unsigned namelen;
  405. bfd_vma section_len;
  406. int vendor;
  407. section_len = bfd_get_32 (abfd, p);
  408. p += 4;
  409. if (section_len == 0)
  410. break;
  411. if (section_len > len)
  412. section_len = len;
  413. len -= section_len;
  414. section_len -= 4;
  415. namelen = strnlen ((char *) p, section_len) + 1;
  416. if (namelen == 0 || namelen >= section_len)
  417. break;
  418. section_len -= namelen;
  419. if (std_sec && strcmp ((char *) p, std_sec) == 0)
  420. vendor = OBJ_ATTR_PROC;
  421. else if (strcmp ((char *) p, "gnu") == 0)
  422. vendor = OBJ_ATTR_GNU;
  423. else
  424. {
  425. /* Other vendor section. Ignore it. */
  426. p += namelen + section_len;
  427. continue;
  428. }
  429. p += namelen;
  430. while (section_len > 0 && p < p_end)
  431. {
  432. unsigned int tag;
  433. unsigned int n;
  434. unsigned int val;
  435. bfd_vma subsection_len;
  436. bfd_byte *end;
  437. tag = safe_read_leb128 (abfd, p, &n, FALSE, p_end);
  438. p += n;
  439. if (p < p_end - 4)
  440. subsection_len = bfd_get_32 (abfd, p);
  441. else
  442. subsection_len = 0;
  443. p += 4;
  444. if (subsection_len == 0)
  445. break;
  446. if (subsection_len > section_len)
  447. subsection_len = section_len;
  448. section_len -= subsection_len;
  449. subsection_len -= n + 4;
  450. end = p + subsection_len;
  451. /* PR 17512: file: 0e8c0c90. */
  452. if (end > p_end)
  453. end = p_end;
  454. switch (tag)
  455. {
  456. case Tag_File:
  457. while (p < end)
  458. {
  459. int type;
  460. tag = safe_read_leb128 (abfd, p, &n, FALSE, end);
  461. p += n;
  462. type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
  463. switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
  464. {
  465. case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
  466. val = safe_read_leb128 (abfd, p, &n, FALSE, end);
  467. p += n;
  468. bfd_elf_add_obj_attr_int_string (abfd, vendor, tag,
  469. val, (char *) p);
  470. p += strlen ((char *)p) + 1;
  471. break;
  472. case ATTR_TYPE_FLAG_STR_VAL:
  473. bfd_elf_add_obj_attr_string (abfd, vendor, tag,
  474. (char *) p);
  475. p += strlen ((char *)p) + 1;
  476. break;
  477. case ATTR_TYPE_FLAG_INT_VAL:
  478. val = safe_read_leb128 (abfd, p, &n, FALSE, end);
  479. p += n;
  480. bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
  481. break;
  482. default:
  483. abort ();
  484. }
  485. }
  486. break;
  487. case Tag_Section:
  488. case Tag_Symbol:
  489. /* Don't have anywhere convenient to attach these.
  490. Fall through for now. */
  491. default:
  492. /* Ignore things we don't kow about. */
  493. p += subsection_len;
  494. subsection_len = 0;
  495. break;
  496. }
  497. }
  498. }
  499. }
  500. free (contents);
  501. }
  502. /* Merge common object attributes from IBFD into OBFD. Raise an error
  503. if there are conflicting attributes. Any processor-specific
  504. attributes have already been merged. This must be called from the
  505. bfd_elfNN_bfd_merge_private_bfd_data hook for each individual
  506. target, along with any target-specific merging. Because there are
  507. no common attributes other than Tag_compatibility at present, and
  508. non-"gnu" Tag_compatibility is not expected in "gnu" sections, this
  509. is not presently called for targets without their own
  510. attributes. */
  511. bfd_boolean
  512. _bfd_elf_merge_object_attributes (bfd *ibfd, bfd *obfd)
  513. {
  514. obj_attribute *in_attr;
  515. obj_attribute *out_attr;
  516. int vendor;
  517. /* The only common attribute is currently Tag_compatibility,
  518. accepted in both processor and "gnu" sections. */
  519. for (vendor = OBJ_ATTR_FIRST; vendor <= OBJ_ATTR_LAST; vendor++)
  520. {
  521. /* Handle Tag_compatibility. The tags are only compatible if the flags
  522. are identical and, if the flags are '1', the strings are identical.
  523. If the flags are non-zero, then we can only use the string "gnu". */
  524. in_attr = &elf_known_obj_attributes (ibfd)[vendor][Tag_compatibility];
  525. out_attr = &elf_known_obj_attributes (obfd)[vendor][Tag_compatibility];
  526. if (in_attr->i > 0 && strcmp (in_attr->s, "gnu") != 0)
  527. {
  528. _bfd_error_handler
  529. (_("error: %B: Object has vendor-specific contents that "
  530. "must be processed by the '%s' toolchain"),
  531. ibfd, in_attr->s);
  532. return FALSE;
  533. }
  534. if (in_attr->i != out_attr->i
  535. || (in_attr->i != 0 && strcmp (in_attr->s, out_attr->s) != 0))
  536. {
  537. _bfd_error_handler (_("error: %B: Object tag '%d, %s' is "
  538. "incompatible with tag '%d, %s'"),
  539. ibfd,
  540. in_attr->i, in_attr->s ? in_attr->s : "",
  541. out_attr->i, out_attr->s ? out_attr->s : "");
  542. return FALSE;
  543. }
  544. }
  545. return TRUE;
  546. }
  547. /* Merge an unknown processor-specific attribute TAG, within the range
  548. of known attributes, from IBFD into OBFD; return TRUE if the link
  549. is OK, FALSE if it must fail. */
  550. bfd_boolean
  551. _bfd_elf_merge_unknown_attribute_low (bfd *ibfd, bfd *obfd, int tag)
  552. {
  553. obj_attribute *in_attr;
  554. obj_attribute *out_attr;
  555. bfd *err_bfd = NULL;
  556. bfd_boolean result = TRUE;
  557. in_attr = elf_known_obj_attributes_proc (ibfd);
  558. out_attr = elf_known_obj_attributes_proc (obfd);
  559. if (out_attr[tag].i != 0 || out_attr[tag].s != NULL)
  560. err_bfd = obfd;
  561. else if (in_attr[tag].i != 0 || in_attr[tag].s != NULL)
  562. err_bfd = ibfd;
  563. if (err_bfd != NULL)
  564. result
  565. = get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag);
  566. /* Only pass on attributes that match in both inputs. */
  567. if (in_attr[tag].i != out_attr[tag].i
  568. || (in_attr[tag].s == NULL) != (out_attr[tag].s == NULL)
  569. || (in_attr[tag].s != NULL && out_attr[tag].s != NULL
  570. && strcmp (in_attr[tag].s, out_attr[tag].s) != 0))
  571. {
  572. out_attr[tag].i = 0;
  573. out_attr[tag].s = NULL;
  574. }
  575. return result;
  576. }
  577. /* Merge the lists of unknown processor-specific attributes, outside
  578. the known range, from IBFD into OBFD; return TRUE if the link is
  579. OK, FALSE if it must fail. */
  580. bfd_boolean
  581. _bfd_elf_merge_unknown_attribute_list (bfd *ibfd, bfd *obfd)
  582. {
  583. obj_attribute_list *in_list;
  584. obj_attribute_list *out_list;
  585. obj_attribute_list **out_listp;
  586. bfd_boolean result = TRUE;
  587. in_list = elf_other_obj_attributes_proc (ibfd);
  588. out_listp = &elf_other_obj_attributes_proc (obfd);
  589. out_list = *out_listp;
  590. for (; in_list || out_list; )
  591. {
  592. bfd *err_bfd = NULL;
  593. unsigned int err_tag = 0;
  594. /* The tags for each list are in numerical order. */
  595. /* If the tags are equal, then merge. */
  596. if (out_list && (!in_list || in_list->tag > out_list->tag))
  597. {
  598. /* This attribute only exists in obfd. We can't merge, and we don't
  599. know what the tag means, so delete it. */
  600. err_bfd = obfd;
  601. err_tag = out_list->tag;
  602. *out_listp = out_list->next;
  603. out_list = *out_listp;
  604. }
  605. else if (in_list && (!out_list || in_list->tag < out_list->tag))
  606. {
  607. /* This attribute only exists in ibfd. We can't merge, and we don't
  608. know what the tag means, so ignore it. */
  609. err_bfd = ibfd;
  610. err_tag = in_list->tag;
  611. in_list = in_list->next;
  612. }
  613. else /* The tags are equal. */
  614. {
  615. /* As present, all attributes in the list are unknown, and
  616. therefore can't be merged meaningfully. */
  617. err_bfd = obfd;
  618. err_tag = out_list->tag;
  619. /* Only pass on attributes that match in both inputs. */
  620. if (in_list->attr.i != out_list->attr.i
  621. || (in_list->attr.s == NULL) != (out_list->attr.s == NULL)
  622. || (in_list->attr.s && out_list->attr.s
  623. && strcmp (in_list->attr.s, out_list->attr.s) != 0))
  624. {
  625. /* No match. Delete the attribute. */
  626. *out_listp = out_list->next;
  627. out_list = *out_listp;
  628. }
  629. else
  630. {
  631. /* Matched. Keep the attribute and move to the next. */
  632. out_list = out_list->next;
  633. in_list = in_list->next;
  634. }
  635. }
  636. if (err_bfd)
  637. result = result
  638. && get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd,
  639. err_tag);
  640. }
  641. return result;
  642. }