elf-attrs.c 20 KB

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