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
31 /* Get a property, allocate a new one if needed. */
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. */
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
;
58 else if (type
< p
->property
.pr_type
)
62 p
= (elf_property_list
*) bfd_alloc (abfd
, sizeof (*p
));
65 _bfd_error_handler (_("%pB: out of memory in _bfd_elf_get_property"),
69 memset (p
, 0, sizeof (*p
));
70 p
->property
.pr_type
= type
;
71 p
->property
.pr_datasz
= datasz
;
77 /* Parse GNU properties. */
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)
91 (_("warning: %pB: corrupt GNU_PROPERTY_TYPE (%ld) size: %#lx"),
92 abfd
, note
->type
, note
->descsz
);
96 while (ptr
!= ptr_end
)
102 if ((size_t) (ptr_end
- ptr
) < 8)
105 type
= bfd_h_get_32 (abfd
, ptr
);
106 datasz
= bfd_h_get_32 (abfd
, ptr
+ 4);
109 if (datasz
> (size_t) (ptr_end
- ptr
))
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
;
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. */
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
;
139 else if (kind
!= property_ignored
)
147 case GNU_PROPERTY_STACK_SIZE
:
148 if (datasz
!= align_size
)
151 (_("warning: %pB: corrupt stack size: 0x%x"),
153 /* Clear all properties. */
154 elf_properties (abfd
) = NULL
;
157 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
159 prop
->u
.number
= bfd_h_get_64 (abfd
, ptr
);
161 prop
->u
.number
= bfd_h_get_32 (abfd
, ptr
);
162 prop
->pr_kind
= property_number
;
165 case GNU_PROPERTY_NO_COPY_ON_PROTECTED
:
169 (_("warning: %pB: corrupt no copy on protected size: 0x%x"),
171 /* Clear all properties. */
172 elf_properties (abfd
) = NULL
;
175 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
176 elf_has_no_copy_on_protected (abfd
) = true;
177 prop
->pr_kind
= property_number
;
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
))
189 (_("error: %pB: <corrupt property (0x%x) size: 0x%x>"),
191 /* Clear all properties. */
192 elf_properties (abfd
) = NULL
;
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
;
205 (_("warning: %pB: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
206 abfd
, note
->type
, type
);
209 ptr
+= (datasz
+ (align_size
- 1)) & ~ (align_size
- 1);
215 /* Merge GNU property BPROP with APROP. If APROP isn't NULL, return TRUE
216 if APROP is updated. Otherwise, return TRUE if BPROP should be merged
220 elf_merge_gnu_properties (struct bfd_link_info
*info
, bfd
*abfd
, bfd
*bbfd
,
221 elf_property
*aprop
, elf_property
*bprop
)
223 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
224 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
228 if (bed
->merge_gnu_properties
!= NULL
229 && pr_type
>= GNU_PROPERTY_LOPROC
230 && pr_type
< GNU_PROPERTY_LOUSER
)
231 return bed
->merge_gnu_properties (info
, abfd
, bbfd
, aprop
, bprop
);
235 case GNU_PROPERTY_STACK_SIZE
:
236 if (aprop
!= NULL
&& bprop
!= NULL
)
238 if (bprop
->u
.number
> aprop
->u
.number
)
240 aprop
->u
.number
= bprop
->u
.number
;
247 case GNU_PROPERTY_NO_COPY_ON_PROTECTED
:
248 /* Return TRUE if APROP is NULL to indicate that BPROP should
250 return aprop
== NULL
;
254 if (pr_type
>= GNU_PROPERTY_UINT32_OR_LO
255 && pr_type
<= GNU_PROPERTY_UINT32_OR_HI
)
257 if (aprop
!= NULL
&& bprop
!= NULL
)
259 number
= aprop
->u
.number
;
260 aprop
->u
.number
= number
| bprop
->u
.number
;
261 /* Remove the property if all bits are empty. */
262 if (aprop
->u
.number
== 0)
264 aprop
->pr_kind
= property_remove
;
268 updated
= number
!= (unsigned int) aprop
->u
.number
;
272 /* Only one of APROP and BPROP can be NULL. */
275 if (aprop
->u
.number
== 0)
277 /* Remove APROP if all bits are empty. */
278 aprop
->pr_kind
= property_remove
;
284 /* Return TRUE if APROP is NULL and all bits of BPROP
285 aren't empty to indicate that BPROP should be added
287 updated
= bprop
->u
.number
!= 0;
292 else if (pr_type
>= GNU_PROPERTY_UINT32_AND_LO
293 && pr_type
<= GNU_PROPERTY_UINT32_AND_HI
)
295 /* Only one of APROP and BPROP can be NULL:
296 1. APROP & BPROP when both APROP and BPROP aren't NULL.
297 2. If APROP is NULL, remove x86 feature.
298 3. Otherwise, do nothing.
300 if (aprop
!= NULL
&& bprop
!= NULL
)
302 number
= aprop
->u
.number
;
303 aprop
->u
.number
= number
& bprop
->u
.number
;
304 updated
= number
!= (unsigned int) aprop
->u
.number
;
305 /* Remove the property if all feature bits are cleared. */
306 if (aprop
->u
.number
== 0)
307 aprop
->pr_kind
= property_remove
;
311 /* There should be no AND properties since some input
312 doesn't have them. */
315 aprop
->pr_kind
= property_remove
;
322 /* Never should happen. */
329 /* Return the property of TYPE on *LISTP and remove it from *LISTP if RM is
330 true. Return NULL if not found. */
332 static elf_property
*
333 elf_find_and_remove_property (elf_property_list
**listp
,
334 unsigned int type
, bool rm
)
336 elf_property_list
*list
;
338 for (list
= *listp
; list
; list
= list
->next
)
340 if (type
== list
->property
.pr_type
)
342 /* Remove this property. */
345 return &list
->property
;
347 else if (type
< list
->property
.pr_type
)
355 /* Merge GNU property list *LISTP in ABFD with FIRST_PBFD. */
358 elf_merge_gnu_property_list (struct bfd_link_info
*info
, bfd
*first_pbfd
,
359 bfd
*abfd
, elf_property_list
**listp
)
361 elf_property_list
*p
, **lastp
;
366 /* Merge each GNU property in FIRST_PBFD with the one on *LISTP. */
367 lastp
= &elf_properties (first_pbfd
);
368 for (p
= *lastp
; p
; p
= p
->next
)
369 if (p
->property
.pr_kind
!= property_remove
)
371 if (p
->property
.pr_kind
== property_number
)
374 number
= p
->property
.u
.number
;
378 pr
= elf_find_and_remove_property (listp
, p
->property
.pr_type
,
380 /* Pass NULL to elf_merge_gnu_properties for the property which
382 elf_merge_gnu_properties (info
, first_pbfd
, abfd
, &p
->property
, pr
);
383 if (p
->property
.pr_kind
== property_remove
)
385 if (info
->has_map_file
)
390 info
->callbacks
->minfo
391 (_("Removed property %W to merge %pB (0x%v) "
393 (bfd_vma
) p
->property
.pr_type
, first_pbfd
,
394 number
, abfd
, pr
->u
.number
);
396 info
->callbacks
->minfo
397 (_("Removed property %W to merge %pB (0x%v) "
398 "and %pB (not found)\n"),
399 (bfd_vma
) p
->property
.pr_type
, first_pbfd
,
405 info
->callbacks
->minfo
406 (_("Removed property %W to merge %pB and %pB\n"),
407 (bfd_vma
) p
->property
.pr_type
, first_pbfd
, abfd
);
409 info
->callbacks
->minfo
410 (_("Removed property %W to merge %pB and %pB "
412 (bfd_vma
) p
->property
.pr_type
, first_pbfd
, abfd
);
416 /* Remove this property. */
424 if (p
->property
.u
.number
!= number
425 || p
->property
.u
.number
!= pr
->u
.number
)
426 info
->callbacks
->minfo
427 (_("Updated property %W (0x%v) to merge %pB (0x%v) "
429 (bfd_vma
) p
->property
.pr_type
, p
->property
.u
.number
,
430 first_pbfd
, number
, abfd
, pr
->u
.number
);
434 if (p
->property
.u
.number
!= number
)
435 info
->callbacks
->minfo
436 (_("Updated property %W (%v) to merge %pB (0x%v) "
437 "and %pB (not found)\n"),
438 (bfd_vma
) p
->property
.pr_type
, p
->property
.u
.number
,
439 first_pbfd
, number
, abfd
);
445 /* Merge the remaining properties on *LISTP with FIRST_PBFD. */
446 for (p
= *listp
; p
!= NULL
; p
= p
->next
)
448 if (p
->property
.pr_kind
== property_number
)
451 number
= p
->property
.u
.number
;
456 if (elf_merge_gnu_properties (info
, first_pbfd
, abfd
, NULL
, &p
->property
))
458 if (p
->property
.pr_type
== GNU_PROPERTY_NO_COPY_ON_PROTECTED
)
459 elf_has_no_copy_on_protected (first_pbfd
) = true;
461 pr
= _bfd_elf_get_property (first_pbfd
, p
->property
.pr_type
,
462 p
->property
.pr_datasz
);
463 /* It must be a new property. */
464 if (pr
->pr_kind
!= property_unknown
)
466 /* Add a new property. */
471 pr
= elf_find_and_remove_property (&elf_properties (first_pbfd
),
477 info
->callbacks
->minfo
478 (_("Removed property %W to merge %pB (not found) and "
480 (bfd_vma
) p
->property
.pr_type
, first_pbfd
, abfd
,
483 info
->callbacks
->minfo
484 (_("Removed property %W to merge %pB and %pB\n"),
485 (bfd_vma
) p
->property
.pr_type
, first_pbfd
, abfd
);
487 else if (pr
->pr_kind
!= property_remove
)
493 /* Get GNU property section size. */
496 elf_get_gnu_property_section_size (elf_property_list
*list
,
497 unsigned int align_size
)
502 /* Compute the output section size. */
503 descsz
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
504 descsz
= (descsz
+ 3) & -(unsigned int) 4;
506 for (; list
!= NULL
; list
= list
->next
)
509 /* Check if this property should be skipped. */
510 if (list
->property
.pr_kind
== property_remove
)
512 /* There are 4 byte type + 4 byte datasz for each property. */
513 if (list
->property
.pr_type
== GNU_PROPERTY_STACK_SIZE
)
516 datasz
= list
->property
.pr_datasz
;
517 size
+= 4 + 4 + datasz
;
518 /* Align each property. */
519 size
= (size
+ (align_size
- 1)) & ~(align_size
- 1);
525 /* Write GNU properties. */
528 elf_write_gnu_properties (bfd
*abfd
, bfd_byte
*contents
,
529 elf_property_list
*list
, unsigned int size
,
530 unsigned int align_size
)
534 Elf_External_Note
*e_note
;
536 e_note
= (Elf_External_Note
*) contents
;
537 descsz
= offsetof (Elf_External_Note
, name
[sizeof "GNU"]);
538 descsz
= (descsz
+ 3) & -(unsigned int) 4;
539 bfd_h_put_32 (abfd
, sizeof "GNU", &e_note
->namesz
);
540 bfd_h_put_32 (abfd
, size
- descsz
, &e_note
->descsz
);
541 bfd_h_put_32 (abfd
, NT_GNU_PROPERTY_TYPE_0
, &e_note
->type
);
542 memcpy (e_note
->name
, "GNU", sizeof "GNU");
545 for (; list
!= NULL
; list
= list
->next
)
547 /* Check if this property should be skipped. */
548 if (list
->property
.pr_kind
== property_remove
)
550 /* There are 4 byte type + 4 byte datasz for each property. */
551 if (list
->property
.pr_type
== GNU_PROPERTY_STACK_SIZE
)
554 datasz
= list
->property
.pr_datasz
;
555 bfd_h_put_32 (abfd
, list
->property
.pr_type
, contents
+ size
);
556 bfd_h_put_32 (abfd
, datasz
, contents
+ size
+ 4);
559 /* Write out property value. */
560 switch (list
->property
.pr_kind
)
562 case property_number
:
566 /* Never should happen. */
573 bfd_h_put_32 (abfd
, list
->property
.u
.number
,
578 bfd_h_put_64 (abfd
, list
->property
.u
.number
,
585 /* Never should happen. */
590 /* Align each property. */
591 size
= (size
+ (align_size
- 1)) & ~ (align_size
- 1);
595 /* Set up GNU properties. Return the first relocatable ELF input with
596 GNU properties if found. Otherwise, return NULL. */
599 _bfd_elf_link_setup_gnu_properties (struct bfd_link_info
*info
)
601 bfd
*abfd
, *first_pbfd
= NULL
;
602 elf_property_list
*list
;
604 bool has_properties
= false;
605 const struct elf_backend_data
*bed
606 = get_elf_backend_data (info
->output_bfd
);
607 unsigned int elfclass
= bed
->s
->elfclass
;
608 int elf_machine_code
= bed
->elf_machine_code
;
610 /* Find the first relocatable ELF input with GNU properties. */
611 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
612 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
613 && (abfd
->flags
& DYNAMIC
) == 0
614 && elf_properties (abfd
) != NULL
)
616 has_properties
= true;
618 /* Ignore GNU properties from ELF objects with different machine
619 code or class. Also skip objects without a GNU_PROPERTY note
621 if ((elf_machine_code
622 == get_elf_backend_data (abfd
)->elf_machine_code
)
624 == get_elf_backend_data (abfd
)->s
->elfclass
)
625 && bfd_get_section_by_name (abfd
,
626 NOTE_GNU_PROPERTY_SECTION_NAME
) != NULL
629 /* Keep .note.gnu.property section in FIRST_PBFD. */
635 /* Do nothing if there is no .note.gnu.property section. */
639 /* Merge .note.gnu.property sections. */
640 info
->callbacks
->minfo (_("\n"));
641 info
->callbacks
->minfo (_("Merging program properties\n"));
642 info
->callbacks
->minfo (_("\n"));
644 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
645 if (abfd
!= first_pbfd
646 && (abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
| BFD_LINKER_CREATED
)) == 0)
648 elf_property_list
*null_ptr
= NULL
;
649 elf_property_list
**listp
= &null_ptr
;
651 /* Merge .note.gnu.property section in relocatable ELF input. */
652 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
654 list
= elf_properties (abfd
);
656 /* Ignore GNU properties from ELF objects with different
660 == get_elf_backend_data (abfd
)->elf_machine_code
))
661 listp
= &elf_properties (abfd
);
666 /* Merge properties with FIRST_PBFD. FIRST_PBFD can be NULL
667 when all properties are from ELF objects with different
668 machine code or class. */
669 if (first_pbfd
!= NULL
)
670 elf_merge_gnu_property_list (info
, first_pbfd
, abfd
, listp
);
674 /* Discard the .note.gnu.property section in this bfd. */
675 sec
= bfd_get_section_by_name (abfd
,
676 NOTE_GNU_PROPERTY_SECTION_NAME
);
678 sec
->output_section
= bfd_abs_section_ptr
;
682 /* Rewrite .note.gnu.property section so that GNU properties are
683 always sorted by type even if input GNU properties aren't sorted. */
684 if (first_pbfd
!= NULL
)
688 unsigned int align_size
= elfclass
== ELFCLASS64
? 8 : 4;
690 sec
= bfd_get_section_by_name (first_pbfd
,
691 NOTE_GNU_PROPERTY_SECTION_NAME
);
692 BFD_ASSERT (sec
!= NULL
);
694 /* Update stack size in .note.gnu.property with -z stack-size=N
696 if (info
->stacksize
> 0)
699 bfd_vma stacksize
= info
->stacksize
;
701 p
= _bfd_elf_get_property (first_pbfd
, GNU_PROPERTY_STACK_SIZE
,
703 if (p
->pr_kind
== property_unknown
)
705 /* Create GNU_PROPERTY_STACK_SIZE. */
706 p
->u
.number
= stacksize
;
707 p
->pr_kind
= property_number
;
709 else if (stacksize
> p
->u
.number
)
710 p
->u
.number
= stacksize
;
712 else if (elf_properties (first_pbfd
) == NULL
)
714 /* Discard .note.gnu.property section if all properties have
716 sec
->output_section
= bfd_abs_section_ptr
;
720 /* Fix up GNU properties. */
721 if (bed
->fixup_gnu_properties
)
722 bed
->fixup_gnu_properties (info
, &elf_properties (first_pbfd
));
724 if (elf_properties (first_pbfd
) == NULL
)
726 /* Discard .note.gnu.property section if all properties have
728 sec
->output_section
= bfd_abs_section_ptr
;
732 /* Compute the section size. */
733 list
= elf_properties (first_pbfd
);
734 size
= elf_get_gnu_property_section_size (list
, align_size
);
736 /* Update .note.gnu.property section now. */
738 contents
= (bfd_byte
*) bfd_zalloc (first_pbfd
, size
);
740 elf_write_gnu_properties (first_pbfd
, contents
, list
, size
,
743 /* Cache the section contents for elf_link_input_bfd. */
744 elf_section_data (sec
)->this_hdr
.contents
= contents
;
746 /* If GNU_PROPERTY_NO_COPY_ON_PROTECTED is set, protected data
747 symbol is defined in the shared object. */
748 if (elf_has_no_copy_on_protected (first_pbfd
))
749 info
->extern_protected_data
= false;
755 /* Convert GNU property size. */
758 _bfd_elf_convert_gnu_property_size (bfd
*ibfd
, bfd
*obfd
)
760 unsigned int align_size
;
761 const struct elf_backend_data
*bed
;
762 elf_property_list
*list
= elf_properties (ibfd
);
764 bed
= get_elf_backend_data (obfd
);
765 align_size
= bed
->s
->elfclass
== ELFCLASS64
? 8 : 4;
767 /* Get the output .note.gnu.property section size. */
768 return elf_get_gnu_property_section_size (list
, align_size
);
771 /* Convert GNU properties. */
774 _bfd_elf_convert_gnu_properties (bfd
*ibfd
, asection
*isec
,
775 bfd
*obfd
, bfd_byte
**ptr
,
776 bfd_size_type
*ptr_size
)
780 unsigned int align_shift
;
781 const struct elf_backend_data
*bed
;
782 elf_property_list
*list
= elf_properties (ibfd
);
784 bed
= get_elf_backend_data (obfd
);
785 align_shift
= bed
->s
->elfclass
== ELFCLASS64
? 3 : 2;
787 /* Get the output .note.gnu.property section size. */
788 size
= bfd_section_size (isec
->output_section
);
790 /* Update the output .note.gnu.property section alignment. */
791 bfd_set_section_alignment (isec
->output_section
, align_shift
);
793 if (size
> bfd_section_size (isec
))
795 contents
= (bfd_byte
*) bfd_malloc (size
);
796 if (contents
== NULL
)
806 /* Generate the output .note.gnu.property section. */
807 elf_write_gnu_properties (ibfd
, contents
, list
, size
, 1 << align_shift
);