Add translations for various sub-directories
[binutils-gdb.git] / bfd / elf-properties.c
blobf150ab4899f3e086448243ede228ac1d363ff15c
1 /* ELF program property support.
2 Copyright (C) 2017-2025 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 case GNU_PROPERTY_MEMORY_SEAL:
181 if (datasz != 0)
183 _bfd_error_handler
184 (_("warning: %pB: corrupt memory sealing size: 0x%x"),
185 abfd, datasz);
186 /* Clear all properties. */
187 elf_properties (abfd) = NULL;
188 return false;
190 prop = _bfd_elf_get_property (abfd, type, datasz);
191 prop->pr_kind = property_number;
192 goto next;
194 default:
195 if ((type >= GNU_PROPERTY_UINT32_AND_LO
196 && type <= GNU_PROPERTY_UINT32_AND_HI)
197 || (type >= GNU_PROPERTY_UINT32_OR_LO
198 && type <= GNU_PROPERTY_UINT32_OR_HI))
200 if (datasz != 4)
202 _bfd_error_handler
203 (_("error: %pB: <corrupt property (0x%x) size: 0x%x>"),
204 abfd, type, datasz);
205 /* Clear all properties. */
206 elf_properties (abfd) = NULL;
207 return false;
209 prop = _bfd_elf_get_property (abfd, type, datasz);
210 prop->u.number |= bfd_h_get_32 (abfd, ptr);
211 prop->pr_kind = property_number;
212 if (type == GNU_PROPERTY_1_NEEDED
213 && ((prop->u.number
214 & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
215 != 0))
217 elf_has_indirect_extern_access (abfd) = true;
218 /* GNU_PROPERTY_NO_COPY_ON_PROTECTED is implied. */
219 elf_has_no_copy_on_protected (abfd) = true;
221 goto next;
223 break;
227 _bfd_error_handler
228 (_("warning: %pB: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
229 abfd, note->type, type);
231 next:
232 ptr += (datasz + (align_size - 1)) & ~ (align_size - 1);
235 return true;
238 /* Merge GNU property BPROP with APROP. If APROP isn't NULL, return TRUE
239 if APROP is updated. Otherwise, return TRUE if BPROP should be merged
240 with ABFD. */
242 static bool
243 elf_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd, bfd *bbfd,
244 elf_property *aprop, elf_property *bprop)
246 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
247 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
248 unsigned int number;
249 bool updated;
251 if (bed->merge_gnu_properties != NULL
252 && pr_type >= GNU_PROPERTY_LOPROC
253 && pr_type < GNU_PROPERTY_LOUSER)
254 return bed->merge_gnu_properties (info, abfd, bbfd, aprop, bprop);
256 switch (pr_type)
258 case GNU_PROPERTY_STACK_SIZE:
259 if (aprop != NULL && bprop != NULL)
261 if (bprop->u.number > aprop->u.number)
263 aprop->u.number = bprop->u.number;
264 return true;
266 break;
268 /* FALLTHROUGH */
270 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
271 case GNU_PROPERTY_MEMORY_SEAL:
272 /* Return TRUE if APROP is NULL to indicate that BPROP should
273 be added to ABFD. */
274 return aprop == NULL;
276 default:
277 updated = false;
278 if (pr_type >= GNU_PROPERTY_UINT32_OR_LO
279 && pr_type <= GNU_PROPERTY_UINT32_OR_HI)
281 if (aprop != NULL && bprop != NULL)
283 number = aprop->u.number;
284 aprop->u.number = number | bprop->u.number;
285 /* Remove the property if all bits are empty. */
286 if (aprop->u.number == 0)
288 aprop->pr_kind = property_remove;
289 updated = true;
291 else
292 updated = number != (unsigned int) aprop->u.number;
294 else
296 /* Only one of APROP and BPROP can be NULL. */
297 if (aprop != NULL)
299 if (aprop->u.number == 0)
301 /* Remove APROP if all bits are empty. */
302 aprop->pr_kind = property_remove;
303 updated = true;
306 else
308 /* Return TRUE if APROP is NULL and all bits of BPROP
309 aren't empty to indicate that BPROP should be added
310 to ABFD. */
311 updated = bprop->u.number != 0;
314 return updated;
316 else if (pr_type >= GNU_PROPERTY_UINT32_AND_LO
317 && pr_type <= GNU_PROPERTY_UINT32_AND_HI)
319 /* Only one of APROP and BPROP can be NULL:
320 1. APROP & BPROP when both APROP and BPROP aren't NULL.
321 2. If APROP is NULL, remove x86 feature.
322 3. Otherwise, do nothing.
324 if (aprop != NULL && bprop != NULL)
326 number = aprop->u.number;
327 aprop->u.number = number & bprop->u.number;
328 updated = number != (unsigned int) aprop->u.number;
329 /* Remove the property if all feature bits are cleared. */
330 if (aprop->u.number == 0)
331 aprop->pr_kind = property_remove;
333 else
335 /* There should be no AND properties since some input
336 doesn't have them. */
337 if (aprop != NULL)
339 aprop->pr_kind = property_remove;
340 updated = true;
343 return updated;
346 /* Never should happen. */
347 abort ();
350 return false;
353 /* Return the property of TYPE on *LISTP and remove it from *LISTP if RM is
354 true. Return NULL if not found. */
356 static elf_property *
357 elf_find_and_remove_property (elf_property_list **listp,
358 unsigned int type, bool rm)
360 elf_property_list *list;
362 for (list = *listp; list; list = list->next)
364 if (type == list->property.pr_type)
366 /* Remove this property. */
367 if (rm)
368 *listp = list->next;
369 return &list->property;
371 else if (type < list->property.pr_type)
372 break;
373 listp = &list->next;
376 return NULL;
379 /* Merge GNU property list *LISTP in ABFD with FIRST_PBFD. */
381 static void
382 elf_merge_gnu_property_list (struct bfd_link_info *info, bfd *first_pbfd,
383 bfd *abfd, elf_property_list **listp)
385 elf_property_list *p, **lastp;
386 elf_property *pr;
387 bool number_p;
388 bfd_vma number = 0;
390 /* Merge each GNU property in FIRST_PBFD with the one on *LISTP. */
391 lastp = &elf_properties (first_pbfd);
392 for (p = *lastp; p; p = p->next)
393 if (p->property.pr_kind != property_remove)
395 if (p->property.pr_kind == property_number)
397 number_p = true;
398 number = p->property.u.number;
400 else
401 number_p = false;
402 pr = elf_find_and_remove_property (listp, p->property.pr_type,
403 true);
404 /* Pass NULL to elf_merge_gnu_properties for the property which
405 isn't on *LISTP. */
406 elf_merge_gnu_properties (info, first_pbfd, abfd, &p->property, pr);
407 if (p->property.pr_kind == property_remove)
409 if (info->has_map_file)
411 if (number_p)
413 if (pr != NULL)
414 info->callbacks->minfo
415 (_("Removed property %W to merge %pB (0x%v) "
416 "and %pB (0x%v)\n"),
417 (bfd_vma) p->property.pr_type, first_pbfd,
418 number, abfd, pr->u.number);
419 else
420 info->callbacks->minfo
421 (_("Removed property %W to merge %pB (0x%v) "
422 "and %pB (not found)\n"),
423 (bfd_vma) p->property.pr_type, first_pbfd,
424 number, abfd);
426 else
428 if (pr != NULL)
429 info->callbacks->minfo
430 (_("Removed property %W to merge %pB and %pB\n"),
431 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
432 else
433 info->callbacks->minfo
434 (_("Removed property %W to merge %pB and %pB "
435 "(not found)\n"),
436 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
440 /* Remove this property. */
441 *lastp = p->next;
442 continue;
444 else if (number_p)
446 if (pr != NULL)
448 if (p->property.u.number != number
449 || p->property.u.number != pr->u.number)
450 info->callbacks->minfo
451 (_("Updated property %W (0x%v) to merge %pB (0x%v) "
452 "and %pB (0x%v)\n"),
453 (bfd_vma) p->property.pr_type, p->property.u.number,
454 first_pbfd, number, abfd, pr->u.number);
456 else
458 if (p->property.u.number != number)
459 info->callbacks->minfo
460 (_("Updated property %W (%v) to merge %pB (0x%v) "
461 "and %pB (not found)\n"),
462 (bfd_vma) p->property.pr_type, p->property.u.number,
463 first_pbfd, number, abfd);
466 lastp = &p->next;
469 /* Merge the remaining properties on *LISTP with FIRST_PBFD. */
470 for (p = *listp; p != NULL; p = p->next)
472 if (p->property.pr_kind == property_number)
474 number_p = true;
475 number = p->property.u.number;
477 else
478 number_p = false;
480 if (elf_merge_gnu_properties (info, first_pbfd, abfd, NULL, &p->property))
482 if (p->property.pr_type == GNU_PROPERTY_NO_COPY_ON_PROTECTED)
483 elf_has_no_copy_on_protected (first_pbfd) = true;
485 pr = _bfd_elf_get_property (first_pbfd, p->property.pr_type,
486 p->property.pr_datasz);
487 /* It must be a new property. */
488 if (pr->pr_kind != property_unknown)
489 abort ();
490 /* Add a new property. */
491 *pr = p->property;
493 else
495 pr = elf_find_and_remove_property (&elf_properties (first_pbfd),
496 p->property.pr_type,
497 false);
498 if (pr == NULL)
500 if (number_p)
501 info->callbacks->minfo
502 (_("Removed property %W to merge %pB (not found) and "
503 "%pB (0x%v)\n"),
504 (bfd_vma) p->property.pr_type, first_pbfd, abfd,
505 number);
506 else
507 info->callbacks->minfo
508 (_("Removed property %W to merge %pB and %pB\n"),
509 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
511 else if (pr->pr_kind != property_remove)
512 abort ();
517 /* Get GNU property section size. */
519 static bfd_size_type
520 elf_get_gnu_property_section_size (elf_property_list *list,
521 unsigned int align_size)
523 bfd_size_type size;
524 unsigned int descsz;
526 /* Compute the output section size. */
527 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
528 descsz = (descsz + 3) & -(unsigned int) 4;
529 size = descsz;
530 for (; list != NULL; list = list->next)
532 unsigned int datasz;
533 /* Check if this property should be skipped. */
534 if (list->property.pr_kind == property_remove)
535 continue;
536 /* There are 4 byte type + 4 byte datasz for each property. */
537 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
538 datasz = align_size;
539 else
540 datasz = list->property.pr_datasz;
541 size += 4 + 4 + datasz;
542 /* Align each property. */
543 size = (size + (align_size - 1)) & ~(align_size - 1);
546 return size;
549 /* Write GNU properties. */
551 static void
552 elf_write_gnu_properties (struct bfd_link_info *info,
553 bfd *abfd, bfd_byte *contents,
554 elf_property_list *list, unsigned int size,
555 unsigned int align_size)
557 unsigned int descsz;
558 unsigned int datasz;
559 Elf_External_Note *e_note;
561 e_note = (Elf_External_Note *) contents;
562 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
563 descsz = (descsz + 3) & -(unsigned int) 4;
564 bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
565 bfd_h_put_32 (abfd, size - descsz, &e_note->descsz);
566 bfd_h_put_32 (abfd, NT_GNU_PROPERTY_TYPE_0, &e_note->type);
567 memcpy (e_note->name, "GNU", sizeof "GNU");
569 size = descsz;
570 for (; list != NULL; list = list->next)
572 /* Check if this property should be skipped. */
573 if (list->property.pr_kind == property_remove)
574 continue;
575 /* There are 4 byte type + 4 byte datasz for each property. */
576 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
577 datasz = align_size;
578 else
579 datasz = list->property.pr_datasz;
580 bfd_h_put_32 (abfd, list->property.pr_type, contents + size);
581 bfd_h_put_32 (abfd, datasz, contents + size + 4);
582 size += 4 + 4;
584 /* Write out property value. */
585 switch (list->property.pr_kind)
587 case property_number:
588 switch (datasz)
590 default:
591 /* Never should happen. */
592 abort ();
594 case 0:
595 break;
597 case 4:
598 /* Save the pointer to GNU_PROPERTY_1_NEEDED so that it
599 can be updated later if needed. */
600 if (info != NULL
601 && list->property.pr_type == GNU_PROPERTY_1_NEEDED)
602 info->needed_1_p = contents + size;
603 bfd_h_put_32 (abfd, list->property.u.number,
604 contents + size);
605 break;
607 case 8:
608 bfd_h_put_64 (abfd, list->property.u.number,
609 contents + size);
610 break;
612 break;
614 default:
615 /* Never should happen. */
616 abort ();
618 size += datasz;
620 /* Align each property. */
621 size = (size + (align_size - 1)) & ~ (align_size - 1);
625 static asection *
626 _bfd_elf_link_create_gnu_property_sec (struct bfd_link_info *info, bfd *elf_bfd,
627 unsigned int elfclass)
629 asection *sec;
631 sec = bfd_make_section_with_flags (elf_bfd,
632 NOTE_GNU_PROPERTY_SECTION_NAME,
633 (SEC_ALLOC
634 | SEC_LOAD
635 | SEC_IN_MEMORY
636 | SEC_READONLY
637 | SEC_HAS_CONTENTS
638 | SEC_DATA));
639 if (sec == NULL)
640 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
642 if (!bfd_set_section_alignment (sec,
643 elfclass == ELFCLASS64 ? 3 : 2))
644 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
645 sec);
647 elf_section_type (sec) = SHT_NOTE;
648 return sec;
652 /* Set up GNU properties. Return the first relocatable ELF input with
653 GNU properties if found. Otherwise, return NULL. */
655 bfd *
656 _bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
658 bfd *abfd, *first_pbfd = NULL, *elf_bfd = NULL;
659 elf_property_list *list;
660 asection *sec;
661 bool has_properties = false;
662 const struct elf_backend_data *bed
663 = get_elf_backend_data (info->output_bfd);
664 unsigned int elfclass = bed->s->elfclass;
665 int elf_machine_code = bed->elf_machine_code;
666 elf_property *p;
668 /* Find the first relocatable ELF input with GNU properties. */
669 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
670 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
671 && (abfd->flags & DYNAMIC) == 0
672 && (elf_machine_code
673 == get_elf_backend_data (abfd)->elf_machine_code)
674 && (elfclass == get_elf_backend_data (abfd)->s->elfclass))
676 /* Ignore GNU properties from ELF objects with different machine
677 code or class. Also skip objects without a GNU_PROPERTY note
678 section. */
679 elf_bfd = abfd;
681 if (elf_properties (abfd) != NULL)
683 has_properties = true;
685 if (bfd_get_section_by_name (abfd,
686 NOTE_GNU_PROPERTY_SECTION_NAME)
687 != NULL)
689 /* Keep .note.gnu.property section in FIRST_PBFD. */
690 first_pbfd = abfd;
691 break;
696 if (info->indirect_extern_access > 0 && elf_bfd != NULL)
698 /* Support -z indirect-extern-access. */
699 if (first_pbfd == NULL)
701 sec = _bfd_elf_link_create_gnu_property_sec (info, elf_bfd, elfclass);
702 first_pbfd = elf_bfd;
703 has_properties = true;
706 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_1_NEEDED, 4);
707 if (p->pr_kind == property_unknown)
709 /* Create GNU_PROPERTY_1_NEEDED. */
710 p->u.number
711 = GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
712 p->pr_kind = property_number;
714 else
715 p->u.number
716 |= GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
719 if (elf_bfd != NULL)
721 if (info->memory_seal)
723 /* Support -z no-memory-seal. */
724 if (first_pbfd == NULL)
726 sec = _bfd_elf_link_create_gnu_property_sec (info, elf_bfd, elfclass);
727 first_pbfd = elf_bfd;
728 has_properties = true;
731 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_MEMORY_SEAL, 0);
732 if (p->pr_kind == property_unknown)
734 /* Create GNU_PROPERTY_NO_MEMORY_SEAL. */
735 p->u.number = GNU_PROPERTY_MEMORY_SEAL;
736 p->pr_kind = property_number;
739 else
740 elf_find_and_remove_property (&elf_properties (elf_bfd),
741 GNU_PROPERTY_MEMORY_SEAL, true);
744 /* Do nothing if there is no .note.gnu.property section. */
745 if (!has_properties)
746 return NULL;
748 /* Merge .note.gnu.property sections. */
749 info->callbacks->minfo (_("\n"));
750 info->callbacks->minfo (_("Merging program properties\n"));
751 info->callbacks->minfo (_("\n"));
753 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
754 if (abfd != first_pbfd
755 && (abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED)) == 0)
757 elf_property_list *null_ptr = NULL;
758 elf_property_list **listp = &null_ptr;
760 /* Merge .note.gnu.property section in relocatable ELF input. */
761 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
763 list = elf_properties (abfd);
765 /* Ignore GNU properties from ELF objects with different
766 machine code. */
767 if (list != NULL
768 && (elf_machine_code
769 == get_elf_backend_data (abfd)->elf_machine_code))
770 listp = &elf_properties (abfd);
772 else
773 list = NULL;
775 /* Merge properties with FIRST_PBFD. FIRST_PBFD can be NULL
776 when all properties are from ELF objects with different
777 machine code or class. */
778 if (first_pbfd != NULL)
779 elf_merge_gnu_property_list (info, first_pbfd, abfd, listp);
781 if (list != NULL)
783 /* Discard the .note.gnu.property section in this bfd. */
784 sec = bfd_get_section_by_name (abfd,
785 NOTE_GNU_PROPERTY_SECTION_NAME);
786 if (sec != NULL)
787 sec->output_section = bfd_abs_section_ptr;
791 /* Rewrite .note.gnu.property section so that GNU properties are
792 always sorted by type even if input GNU properties aren't sorted. */
793 if (first_pbfd != NULL)
795 bfd_size_type size;
796 bfd_byte *contents;
797 unsigned int align_size = elfclass == ELFCLASS64 ? 8 : 4;
799 sec = bfd_get_section_by_name (first_pbfd,
800 NOTE_GNU_PROPERTY_SECTION_NAME);
801 BFD_ASSERT (sec != NULL);
803 /* Update stack size in .note.gnu.property with -z stack-size=N
804 if N > 0. */
805 if (info->stacksize > 0)
807 bfd_vma stacksize = info->stacksize;
809 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE,
810 align_size);
811 if (p->pr_kind == property_unknown)
813 /* Create GNU_PROPERTY_STACK_SIZE. */
814 p->u.number = stacksize;
815 p->pr_kind = property_number;
817 else if (stacksize > p->u.number)
818 p->u.number = stacksize;
820 else if (elf_properties (first_pbfd) == NULL)
822 /* Discard .note.gnu.property section if all properties have
823 been removed. */
824 sec->output_section = bfd_abs_section_ptr;
825 return NULL;
828 /* Fix up GNU properties. */
829 if (bed->fixup_gnu_properties)
830 bed->fixup_gnu_properties (info, &elf_properties (first_pbfd));
832 if (elf_properties (first_pbfd) == NULL)
834 /* Discard .note.gnu.property section if all properties have
835 been removed. */
836 sec->output_section = bfd_abs_section_ptr;
837 return NULL;
840 /* Compute the section size. */
841 list = elf_properties (first_pbfd);
842 size = elf_get_gnu_property_section_size (list, align_size);
844 /* Update .note.gnu.property section now. */
845 sec->size = size;
846 contents = (bfd_byte *) bfd_zalloc (first_pbfd, size);
848 if (info->indirect_extern_access <= 0)
850 /* Get GNU_PROPERTY_1_NEEDED properties. */
851 p = elf_find_and_remove_property (&elf_properties (first_pbfd),
852 GNU_PROPERTY_1_NEEDED, false);
853 if (p != NULL)
855 if (info->indirect_extern_access < 0)
857 /* Set indirect_extern_access to 1 to indicate that
858 it is turned on by input properties. */
859 if ((p->u.number
860 & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
861 != 0)
862 info->indirect_extern_access = 1;
864 else
865 /* Turn off indirect external access. */
866 p->u.number
867 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
871 elf_write_gnu_properties (info, first_pbfd, contents, list, size,
872 align_size);
874 /* Cache the section contents for elf_link_input_bfd. */
875 sec->alloced = 1;
876 elf_section_data (sec)->this_hdr.contents = contents;
878 /* If GNU_PROPERTY_NO_COPY_ON_PROTECTED is set, protected data
879 symbol is defined in the shared object. */
880 if (elf_has_no_copy_on_protected (first_pbfd))
881 info->extern_protected_data = false;
883 if (info->indirect_extern_access > 0)
885 /* For indirect external access, don't generate copy
886 relocations. NB: Set to nocopyreloc to 2 to indicate
887 that it is implied by indirect_extern_access. */
888 info->nocopyreloc = 2;
889 info->extern_protected_data = false;
893 return first_pbfd;
896 /* Convert GNU property size. */
898 bfd_size_type
899 _bfd_elf_convert_gnu_property_size (bfd *ibfd, bfd *obfd)
901 unsigned int align_size;
902 const struct elf_backend_data *bed;
903 elf_property_list *list = elf_properties (ibfd);
905 bed = get_elf_backend_data (obfd);
906 align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
908 /* Get the output .note.gnu.property section size. */
909 return elf_get_gnu_property_section_size (list, align_size);
912 /* Convert GNU properties. */
914 bool
915 _bfd_elf_convert_gnu_properties (bfd *ibfd, asection *isec,
916 bfd *obfd, bfd_byte **ptr,
917 bfd_size_type *ptr_size)
919 unsigned int size;
920 bfd_byte *contents;
921 unsigned int align_shift;
922 const struct elf_backend_data *bed;
923 elf_property_list *list = elf_properties (ibfd);
925 bed = get_elf_backend_data (obfd);
926 align_shift = bed->s->elfclass == ELFCLASS64 ? 3 : 2;
928 /* Get the output .note.gnu.property section size. */
929 size = bfd_section_size (isec->output_section);
931 /* Update the output .note.gnu.property section alignment. */
932 bfd_set_section_alignment (isec->output_section, align_shift);
934 if (size > bfd_section_size (isec))
936 contents = (bfd_byte *) bfd_malloc (size);
937 if (contents == NULL)
938 return false;
939 free (*ptr);
940 *ptr = contents;
942 else
943 contents = *ptr;
945 *ptr_size = size;
947 /* Generate the output .note.gnu.property section. */
948 elf_write_gnu_properties (NULL, ibfd, contents, list, size,
949 1 << align_shift);
951 return true;