readelf: Support RELR in -S and -d and output
[binutils-gdb.git] / bfd / elf-properties.c
blobaae02885c15bb7be21516749dd914ac2a5c7ec70
1 /* ELF program property support.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* GNU program property draft is at:
23 https://github.com/hjl-tools/linux-abi/wiki/property-draft.pdf
26 #include "sysdep.h"
27 #include "bfd.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
31 /* Get a property, allocate a new one if needed. */
33 elf_property *
34 _bfd_elf_get_property (bfd *abfd, unsigned int type, unsigned int datasz)
36 elf_property_list *p, **lastp;
38 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
40 /* Never should happen. */
41 abort ();
44 /* Keep the property list in order of type. */
45 lastp = &elf_properties (abfd);
46 for (p = *lastp; p; p = p->next)
48 /* Reuse the existing entry. */
49 if (type == p->property.pr_type)
51 if (datasz > p->property.pr_datasz)
53 /* This can happen when mixing 32-bit and 64-bit objects. */
54 p->property.pr_datasz = datasz;
56 return &p->property;
58 else if (type < p->property.pr_type)
59 break;
60 lastp = &p->next;
62 p = (elf_property_list *) bfd_alloc (abfd, sizeof (*p));
63 if (p == NULL)
65 _bfd_error_handler (_("%pB: out of memory in _bfd_elf_get_property"),
66 abfd);
67 _exit (EXIT_FAILURE);
69 memset (p, 0, sizeof (*p));
70 p->property.pr_type = type;
71 p->property.pr_datasz = datasz;
72 p->next = *lastp;
73 *lastp = p;
74 return &p->property;
77 /* Parse GNU properties. */
79 bool
80 _bfd_elf_parse_gnu_properties (bfd *abfd, Elf_Internal_Note *note)
82 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
83 unsigned int align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
84 bfd_byte *ptr = (bfd_byte *) note->descdata;
85 bfd_byte *ptr_end = ptr + note->descsz;
87 if (note->descsz < 8 || (note->descsz % align_size) != 0)
89 bad_size:
90 _bfd_error_handler
91 (_("warning: %pB: corrupt GNU_PROPERTY_TYPE (%ld) size: %#lx"),
92 abfd, note->type, note->descsz);
93 return false;
96 while (ptr != ptr_end)
98 unsigned int type;
99 unsigned int datasz;
100 elf_property *prop;
102 if ((size_t) (ptr_end - ptr) < 8)
103 goto bad_size;
105 type = bfd_h_get_32 (abfd, ptr);
106 datasz = bfd_h_get_32 (abfd, ptr + 4);
107 ptr += 8;
109 if (datasz > (size_t) (ptr_end - ptr))
111 _bfd_error_handler
112 (_("warning: %pB: corrupt GNU_PROPERTY_TYPE (%ld) type (0x%x) datasz: 0x%x"),
113 abfd, note->type, type, datasz);
114 /* Clear all properties. */
115 elf_properties (abfd) = NULL;
116 return false;
119 if (type >= GNU_PROPERTY_LOPROC)
121 if (bed->elf_machine_code == EM_NONE)
123 /* Ignore processor-specific properties with generic ELF
124 target vector. They should be handled by the matching
125 ELF target vector. */
126 goto next;
128 else if (type < GNU_PROPERTY_LOUSER
129 && bed->parse_gnu_properties)
131 enum elf_property_kind kind
132 = bed->parse_gnu_properties (abfd, type, ptr, datasz);
133 if (kind == property_corrupt)
135 /* Clear all properties. */
136 elf_properties (abfd) = NULL;
137 return false;
139 else if (kind != property_ignored)
140 goto next;
143 else
145 switch (type)
147 case GNU_PROPERTY_STACK_SIZE:
148 if (datasz != align_size)
150 _bfd_error_handler
151 (_("warning: %pB: corrupt stack size: 0x%x"),
152 abfd, datasz);
153 /* Clear all properties. */
154 elf_properties (abfd) = NULL;
155 return false;
157 prop = _bfd_elf_get_property (abfd, type, datasz);
158 if (datasz == 8)
159 prop->u.number = bfd_h_get_64 (abfd, ptr);
160 else
161 prop->u.number = bfd_h_get_32 (abfd, ptr);
162 prop->pr_kind = property_number;
163 goto next;
165 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
166 if (datasz != 0)
168 _bfd_error_handler
169 (_("warning: %pB: corrupt no copy on protected size: 0x%x"),
170 abfd, datasz);
171 /* Clear all properties. */
172 elf_properties (abfd) = NULL;
173 return false;
175 prop = _bfd_elf_get_property (abfd, type, datasz);
176 elf_has_no_copy_on_protected (abfd) = true;
177 prop->pr_kind = property_number;
178 goto next;
180 default:
181 if ((type >= GNU_PROPERTY_UINT32_AND_LO
182 && type <= GNU_PROPERTY_UINT32_AND_HI)
183 || (type >= GNU_PROPERTY_UINT32_OR_LO
184 && type <= GNU_PROPERTY_UINT32_OR_HI))
186 if (datasz != 4)
188 _bfd_error_handler
189 (_("error: %pB: <corrupt property (0x%x) size: 0x%x>"),
190 abfd, type, datasz);
191 /* Clear all properties. */
192 elf_properties (abfd) = NULL;
193 return false;
195 prop = _bfd_elf_get_property (abfd, type, datasz);
196 prop->u.number |= bfd_h_get_32 (abfd, ptr);
197 prop->pr_kind = property_number;
198 if ((abfd->flags & DYNAMIC) == 0
199 && type == GNU_PROPERTY_1_NEEDED
200 && ((prop->u.number
201 & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
202 != 0))
204 /* NB: Skip the shared library since it may not be
205 the same at run-time. */
206 elf_has_indirect_extern_access (abfd) = true;
207 /* GNU_PROPERTY_NO_COPY_ON_PROTECTED is implied. */
208 elf_has_no_copy_on_protected (abfd) = true;
210 goto next;
212 break;
216 _bfd_error_handler
217 (_("warning: %pB: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
218 abfd, note->type, type);
220 next:
221 ptr += (datasz + (align_size - 1)) & ~ (align_size - 1);
224 return true;
227 /* Merge GNU property BPROP with APROP. If APROP isn't NULL, return TRUE
228 if APROP is updated. Otherwise, return TRUE if BPROP should be merged
229 with ABFD. */
231 static bool
232 elf_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd, bfd *bbfd,
233 elf_property *aprop, elf_property *bprop)
235 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
236 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
237 unsigned int number;
238 bool updated;
240 if (bed->merge_gnu_properties != NULL
241 && pr_type >= GNU_PROPERTY_LOPROC
242 && pr_type < GNU_PROPERTY_LOUSER)
243 return bed->merge_gnu_properties (info, abfd, bbfd, aprop, bprop);
245 switch (pr_type)
247 case GNU_PROPERTY_STACK_SIZE:
248 if (aprop != NULL && bprop != NULL)
250 if (bprop->u.number > aprop->u.number)
252 aprop->u.number = bprop->u.number;
253 return true;
255 break;
257 /* FALLTHROUGH */
259 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
260 /* Return TRUE if APROP is NULL to indicate that BPROP should
261 be added to ABFD. */
262 return aprop == NULL;
264 default:
265 updated = false;
266 if (pr_type >= GNU_PROPERTY_UINT32_OR_LO
267 && pr_type <= GNU_PROPERTY_UINT32_OR_HI)
269 if (aprop != NULL && bprop != NULL)
271 number = aprop->u.number;
272 aprop->u.number = number | bprop->u.number;
273 /* Remove the property if all bits are empty. */
274 if (aprop->u.number == 0)
276 aprop->pr_kind = property_remove;
277 updated = true;
279 else
280 updated = number != (unsigned int) aprop->u.number;
282 else
284 /* Only one of APROP and BPROP can be NULL. */
285 if (aprop != NULL)
287 if (aprop->u.number == 0)
289 /* Remove APROP if all bits are empty. */
290 aprop->pr_kind = property_remove;
291 updated = true;
294 else
296 /* Return TRUE if APROP is NULL and all bits of BPROP
297 aren't empty to indicate that BPROP should be added
298 to ABFD. */
299 updated = bprop->u.number != 0;
302 return updated;
304 else if (pr_type >= GNU_PROPERTY_UINT32_AND_LO
305 && pr_type <= GNU_PROPERTY_UINT32_AND_HI)
307 /* Only one of APROP and BPROP can be NULL:
308 1. APROP & BPROP when both APROP and BPROP aren't NULL.
309 2. If APROP is NULL, remove x86 feature.
310 3. Otherwise, do nothing.
312 if (aprop != NULL && bprop != NULL)
314 number = aprop->u.number;
315 aprop->u.number = number & bprop->u.number;
316 updated = number != (unsigned int) aprop->u.number;
317 /* Remove the property if all feature bits are cleared. */
318 if (aprop->u.number == 0)
319 aprop->pr_kind = property_remove;
321 else
323 /* There should be no AND properties since some input
324 doesn't have them. */
325 if (aprop != NULL)
327 aprop->pr_kind = property_remove;
328 updated = true;
331 return updated;
334 /* Never should happen. */
335 abort ();
338 return false;
341 /* Return the property of TYPE on *LISTP and remove it from *LISTP if RM is
342 true. Return NULL if not found. */
344 static elf_property *
345 elf_find_and_remove_property (elf_property_list **listp,
346 unsigned int type, bool rm)
348 elf_property_list *list;
350 for (list = *listp; list; list = list->next)
352 if (type == list->property.pr_type)
354 /* Remove this property. */
355 if (rm)
356 *listp = list->next;
357 return &list->property;
359 else if (type < list->property.pr_type)
360 break;
361 listp = &list->next;
364 return NULL;
367 /* Merge GNU property list *LISTP in ABFD with FIRST_PBFD. */
369 static void
370 elf_merge_gnu_property_list (struct bfd_link_info *info, bfd *first_pbfd,
371 bfd *abfd, elf_property_list **listp)
373 elf_property_list *p, **lastp;
374 elf_property *pr;
375 bool number_p;
376 bfd_vma number = 0;
378 /* Merge each GNU property in FIRST_PBFD with the one on *LISTP. */
379 lastp = &elf_properties (first_pbfd);
380 for (p = *lastp; p; p = p->next)
381 if (p->property.pr_kind != property_remove)
383 if (p->property.pr_kind == property_number)
385 number_p = true;
386 number = p->property.u.number;
388 else
389 number_p = false;
390 pr = elf_find_and_remove_property (listp, p->property.pr_type,
391 true);
392 /* Pass NULL to elf_merge_gnu_properties for the property which
393 isn't on *LISTP. */
394 elf_merge_gnu_properties (info, first_pbfd, abfd, &p->property, pr);
395 if (p->property.pr_kind == property_remove)
397 if (info->has_map_file)
399 if (number_p)
401 if (pr != NULL)
402 info->callbacks->minfo
403 (_("Removed property %W to merge %pB (0x%v) "
404 "and %pB (0x%v)\n"),
405 (bfd_vma) p->property.pr_type, first_pbfd,
406 number, abfd, pr->u.number);
407 else
408 info->callbacks->minfo
409 (_("Removed property %W to merge %pB (0x%v) "
410 "and %pB (not found)\n"),
411 (bfd_vma) p->property.pr_type, first_pbfd,
412 number, abfd);
414 else
416 if (pr != NULL)
417 info->callbacks->minfo
418 (_("Removed property %W to merge %pB and %pB\n"),
419 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
420 else
421 info->callbacks->minfo
422 (_("Removed property %W to merge %pB and %pB "
423 "(not found)\n"),
424 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
428 /* Remove this property. */
429 *lastp = p->next;
430 continue;
432 else if (number_p)
434 if (pr != NULL)
436 if (p->property.u.number != number
437 || p->property.u.number != pr->u.number)
438 info->callbacks->minfo
439 (_("Updated property %W (0x%v) to merge %pB (0x%v) "
440 "and %pB (0x%v)\n"),
441 (bfd_vma) p->property.pr_type, p->property.u.number,
442 first_pbfd, number, abfd, pr->u.number);
444 else
446 if (p->property.u.number != number)
447 info->callbacks->minfo
448 (_("Updated property %W (%v) to merge %pB (0x%v) "
449 "and %pB (not found)\n"),
450 (bfd_vma) p->property.pr_type, p->property.u.number,
451 first_pbfd, number, abfd);
454 lastp = &p->next;
457 /* Merge the remaining properties on *LISTP with FIRST_PBFD. */
458 for (p = *listp; p != NULL; p = p->next)
460 if (p->property.pr_kind == property_number)
462 number_p = true;
463 number = p->property.u.number;
465 else
466 number_p = false;
468 if (elf_merge_gnu_properties (info, first_pbfd, abfd, NULL, &p->property))
470 if (p->property.pr_type == GNU_PROPERTY_NO_COPY_ON_PROTECTED)
471 elf_has_no_copy_on_protected (first_pbfd) = true;
473 pr = _bfd_elf_get_property (first_pbfd, p->property.pr_type,
474 p->property.pr_datasz);
475 /* It must be a new property. */
476 if (pr->pr_kind != property_unknown)
477 abort ();
478 /* Add a new property. */
479 *pr = p->property;
481 else
483 pr = elf_find_and_remove_property (&elf_properties (first_pbfd),
484 p->property.pr_type,
485 false);
486 if (pr == NULL)
488 if (number_p)
489 info->callbacks->minfo
490 (_("Removed property %W to merge %pB (not found) and "
491 "%pB (0x%v)\n"),
492 (bfd_vma) p->property.pr_type, first_pbfd, abfd,
493 number);
494 else
495 info->callbacks->minfo
496 (_("Removed property %W to merge %pB and %pB\n"),
497 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
499 else if (pr->pr_kind != property_remove)
500 abort ();
505 /* Get GNU property section size. */
507 static bfd_size_type
508 elf_get_gnu_property_section_size (elf_property_list *list,
509 unsigned int align_size)
511 bfd_size_type size;
512 unsigned int descsz;
514 /* Compute the output section size. */
515 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
516 descsz = (descsz + 3) & -(unsigned int) 4;
517 size = descsz;
518 for (; list != NULL; list = list->next)
520 unsigned int datasz;
521 /* Check if this property should be skipped. */
522 if (list->property.pr_kind == property_remove)
523 continue;
524 /* There are 4 byte type + 4 byte datasz for each property. */
525 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
526 datasz = align_size;
527 else
528 datasz = list->property.pr_datasz;
529 size += 4 + 4 + datasz;
530 /* Align each property. */
531 size = (size + (align_size - 1)) & ~(align_size - 1);
534 return size;
537 /* Write GNU properties. */
539 static void
540 elf_write_gnu_properties (struct bfd_link_info *info,
541 bfd *abfd, bfd_byte *contents,
542 elf_property_list *list, unsigned int size,
543 unsigned int align_size)
545 unsigned int descsz;
546 unsigned int datasz;
547 Elf_External_Note *e_note;
549 e_note = (Elf_External_Note *) contents;
550 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
551 descsz = (descsz + 3) & -(unsigned int) 4;
552 bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
553 bfd_h_put_32 (abfd, size - descsz, &e_note->descsz);
554 bfd_h_put_32 (abfd, NT_GNU_PROPERTY_TYPE_0, &e_note->type);
555 memcpy (e_note->name, "GNU", sizeof "GNU");
557 size = descsz;
558 for (; list != NULL; list = list->next)
560 /* Check if this property should be skipped. */
561 if (list->property.pr_kind == property_remove)
562 continue;
563 /* There are 4 byte type + 4 byte datasz for each property. */
564 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
565 datasz = align_size;
566 else
567 datasz = list->property.pr_datasz;
568 bfd_h_put_32 (abfd, list->property.pr_type, contents + size);
569 bfd_h_put_32 (abfd, datasz, contents + size + 4);
570 size += 4 + 4;
572 /* Write out property value. */
573 switch (list->property.pr_kind)
575 case property_number:
576 switch (datasz)
578 default:
579 /* Never should happen. */
580 abort ();
582 case 0:
583 break;
585 case 4:
586 /* Save the pointer to GNU_PROPERTY_1_NEEDED so that it
587 can be updated later if needed. */
588 if (info != NULL
589 && list->property.pr_type == GNU_PROPERTY_1_NEEDED)
590 info->needed_1_p = contents + size;
591 bfd_h_put_32 (abfd, list->property.u.number,
592 contents + size);
593 break;
595 case 8:
596 bfd_h_put_64 (abfd, list->property.u.number,
597 contents + size);
598 break;
600 break;
602 default:
603 /* Never should happen. */
604 abort ();
606 size += datasz;
608 /* Align each property. */
609 size = (size + (align_size - 1)) & ~ (align_size - 1);
613 /* Set up GNU properties. Return the first relocatable ELF input with
614 GNU properties if found. Otherwise, return NULL. */
616 bfd *
617 _bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
619 bfd *abfd, *first_pbfd = NULL, *elf_bfd = NULL;
620 elf_property_list *list;
621 asection *sec;
622 bool has_properties = false;
623 const struct elf_backend_data *bed
624 = get_elf_backend_data (info->output_bfd);
625 unsigned int elfclass = bed->s->elfclass;
626 int elf_machine_code = bed->elf_machine_code;
627 elf_property *p;
629 /* Find the first relocatable ELF input with GNU properties. */
630 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
631 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
632 && (abfd->flags & DYNAMIC) == 0
633 && (elf_machine_code
634 == get_elf_backend_data (abfd)->elf_machine_code)
635 && (elfclass == get_elf_backend_data (abfd)->s->elfclass))
637 /* Ignore GNU properties from ELF objects with different machine
638 code or class. Also skip objects without a GNU_PROPERTY note
639 section. */
640 elf_bfd = abfd;
642 if (elf_properties (abfd) != NULL)
644 has_properties = true;
646 if (bfd_get_section_by_name (abfd,
647 NOTE_GNU_PROPERTY_SECTION_NAME)
648 != NULL)
650 /* Keep .note.gnu.property section in FIRST_PBFD. */
651 first_pbfd = abfd;
652 break;
657 if (info->indirect_extern_access > 0 && elf_bfd != NULL)
659 /* Support -z indirect-extern-access. */
660 if (first_pbfd == NULL)
662 sec = bfd_make_section_with_flags (elf_bfd,
663 NOTE_GNU_PROPERTY_SECTION_NAME,
664 (SEC_ALLOC
665 | SEC_LOAD
666 | SEC_IN_MEMORY
667 | SEC_READONLY
668 | SEC_HAS_CONTENTS
669 | SEC_DATA));
670 if (sec == NULL)
671 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
673 if (!bfd_set_section_alignment (sec,
674 elfclass == ELFCLASS64 ? 3 : 2))
675 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
676 sec);
678 elf_section_type (sec) = SHT_NOTE;
679 first_pbfd = elf_bfd;
680 has_properties = true;
683 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_1_NEEDED, 4);
684 if (p->pr_kind == property_unknown)
686 /* Create GNU_PROPERTY_1_NEEDED. */
687 p->u.number
688 = GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
689 p->pr_kind = property_number;
691 else
692 p->u.number
693 |= GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
696 /* Do nothing if there is no .note.gnu.property section. */
697 if (!has_properties)
698 return NULL;
700 /* Merge .note.gnu.property sections. */
701 info->callbacks->minfo (_("\n"));
702 info->callbacks->minfo (_("Merging program properties\n"));
703 info->callbacks->minfo (_("\n"));
705 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
706 if (abfd != first_pbfd
707 && (abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED)) == 0)
709 elf_property_list *null_ptr = NULL;
710 elf_property_list **listp = &null_ptr;
712 /* Merge .note.gnu.property section in relocatable ELF input. */
713 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
715 list = elf_properties (abfd);
717 /* Ignore GNU properties from ELF objects with different
718 machine code. */
719 if (list != NULL
720 && (elf_machine_code
721 == get_elf_backend_data (abfd)->elf_machine_code))
722 listp = &elf_properties (abfd);
724 else
725 list = NULL;
727 /* Merge properties with FIRST_PBFD. FIRST_PBFD can be NULL
728 when all properties are from ELF objects with different
729 machine code or class. */
730 if (first_pbfd != NULL)
731 elf_merge_gnu_property_list (info, first_pbfd, abfd, listp);
733 if (list != NULL)
735 /* Discard the .note.gnu.property section in this bfd. */
736 sec = bfd_get_section_by_name (abfd,
737 NOTE_GNU_PROPERTY_SECTION_NAME);
738 if (sec != NULL)
739 sec->output_section = bfd_abs_section_ptr;
743 /* Rewrite .note.gnu.property section so that GNU properties are
744 always sorted by type even if input GNU properties aren't sorted. */
745 if (first_pbfd != NULL)
747 bfd_size_type size;
748 bfd_byte *contents;
749 unsigned int align_size = elfclass == ELFCLASS64 ? 8 : 4;
751 sec = bfd_get_section_by_name (first_pbfd,
752 NOTE_GNU_PROPERTY_SECTION_NAME);
753 BFD_ASSERT (sec != NULL);
755 /* Update stack size in .note.gnu.property with -z stack-size=N
756 if N > 0. */
757 if (info->stacksize > 0)
759 bfd_vma stacksize = info->stacksize;
761 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE,
762 align_size);
763 if (p->pr_kind == property_unknown)
765 /* Create GNU_PROPERTY_STACK_SIZE. */
766 p->u.number = stacksize;
767 p->pr_kind = property_number;
769 else if (stacksize > p->u.number)
770 p->u.number = stacksize;
772 else if (elf_properties (first_pbfd) == NULL)
774 /* Discard .note.gnu.property section if all properties have
775 been removed. */
776 sec->output_section = bfd_abs_section_ptr;
777 return NULL;
780 /* Fix up GNU properties. */
781 if (bed->fixup_gnu_properties)
782 bed->fixup_gnu_properties (info, &elf_properties (first_pbfd));
784 if (elf_properties (first_pbfd) == NULL)
786 /* Discard .note.gnu.property section if all properties have
787 been removed. */
788 sec->output_section = bfd_abs_section_ptr;
789 return NULL;
792 /* Compute the section size. */
793 list = elf_properties (first_pbfd);
794 size = elf_get_gnu_property_section_size (list, align_size);
796 /* Update .note.gnu.property section now. */
797 sec->size = size;
798 contents = (bfd_byte *) bfd_zalloc (first_pbfd, size);
800 if (info->indirect_extern_access <= 0)
802 /* Get GNU_PROPERTY_1_NEEDED properties. */
803 p = elf_find_and_remove_property (&elf_properties (first_pbfd),
804 GNU_PROPERTY_1_NEEDED, false);
805 if (p != NULL)
807 if (info->indirect_extern_access < 0)
809 /* Set indirect_extern_access to 1 to indicate that
810 it is turned on by input properties. */
811 if ((p->u.number
812 & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
813 != 0)
814 info->indirect_extern_access = 1;
816 else
817 /* Turn off indirect external access. */
818 p->u.number
819 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
823 elf_write_gnu_properties (info, first_pbfd, contents, list, size,
824 align_size);
826 /* Cache the section contents for elf_link_input_bfd. */
827 elf_section_data (sec)->this_hdr.contents = contents;
829 /* If GNU_PROPERTY_NO_COPY_ON_PROTECTED is set, protected data
830 symbol is defined in the shared object. */
831 if (elf_has_no_copy_on_protected (first_pbfd))
832 info->extern_protected_data = false;
834 if (info->indirect_extern_access > 0)
836 /* For indirect external access, don't generate copy
837 relocations. NB: Set to nocopyreloc to 2 to indicate
838 that it is implied by indirect_extern_access. */
839 info->nocopyreloc = 2;
840 info->extern_protected_data = false;
844 return first_pbfd;
847 /* Convert GNU property size. */
849 bfd_size_type
850 _bfd_elf_convert_gnu_property_size (bfd *ibfd, bfd *obfd)
852 unsigned int align_size;
853 const struct elf_backend_data *bed;
854 elf_property_list *list = elf_properties (ibfd);
856 bed = get_elf_backend_data (obfd);
857 align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
859 /* Get the output .note.gnu.property section size. */
860 return elf_get_gnu_property_section_size (list, align_size);
863 /* Convert GNU properties. */
865 bool
866 _bfd_elf_convert_gnu_properties (bfd *ibfd, asection *isec,
867 bfd *obfd, bfd_byte **ptr,
868 bfd_size_type *ptr_size)
870 unsigned int size;
871 bfd_byte *contents;
872 unsigned int align_shift;
873 const struct elf_backend_data *bed;
874 elf_property_list *list = elf_properties (ibfd);
876 bed = get_elf_backend_data (obfd);
877 align_shift = bed->s->elfclass == ELFCLASS64 ? 3 : 2;
879 /* Get the output .note.gnu.property section size. */
880 size = bfd_section_size (isec->output_section);
882 /* Update the output .note.gnu.property section alignment. */
883 bfd_set_section_alignment (isec->output_section, align_shift);
885 if (size > bfd_section_size (isec))
887 contents = (bfd_byte *) bfd_malloc (size);
888 if (contents == NULL)
889 return false;
890 free (*ptr);
891 *ptr = contents;
893 else
894 contents = *ptr;
896 *ptr_size = size;
898 /* Generate the output .note.gnu.property section. */
899 elf_write_gnu_properties (NULL, ibfd, contents, list, size,
900 1 << align_shift);
902 return true;