]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf-properties.c
elf: Add GNU_PROPERTY_1_NEEDED check
[thirdparty/binutils-gdb.git] / bfd / elf-properties.c
1 /* ELF program property support.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
20
21 /* GNU program property draft is at:
22
23 https://github.com/hjl-tools/linux-abi/wiki/property-draft.pdf
24 */
25
26 #include "sysdep.h"
27 #include "bfd.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30
31 /* Get a property, allocate a new one if needed. */
32
33 elf_property *
34 _bfd_elf_get_property (bfd *abfd, unsigned int type, unsigned int datasz)
35 {
36 elf_property_list *p, **lastp;
37
38 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
39 {
40 /* Never should happen. */
41 abort ();
42 }
43
44 /* Keep the property list in order of type. */
45 lastp = &elf_properties (abfd);
46 for (p = *lastp; p; p = p->next)
47 {
48 /* Reuse the existing entry. */
49 if (type == p->property.pr_type)
50 {
51 if (datasz > p->property.pr_datasz)
52 {
53 /* This can happen when mixing 32-bit and 64-bit objects. */
54 p->property.pr_datasz = datasz;
55 }
56 return &p->property;
57 }
58 else if (type < p->property.pr_type)
59 break;
60 lastp = &p->next;
61 }
62 p = (elf_property_list *) bfd_alloc (abfd, sizeof (*p));
63 if (p == NULL)
64 {
65 _bfd_error_handler (_("%pB: out of memory in _bfd_elf_get_property"),
66 abfd);
67 _exit (EXIT_FAILURE);
68 }
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;
75 }
76
77 /* Parse GNU properties. */
78
79 bool
80 _bfd_elf_parse_gnu_properties (bfd *abfd, Elf_Internal_Note *note)
81 {
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;
86
87 if (note->descsz < 8 || (note->descsz % align_size) != 0)
88 {
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;
94 }
95
96 while (ptr != ptr_end)
97 {
98 unsigned int type;
99 unsigned int datasz;
100 elf_property *prop;
101
102 if ((size_t) (ptr_end - ptr) < 8)
103 goto bad_size;
104
105 type = bfd_h_get_32 (abfd, ptr);
106 datasz = bfd_h_get_32 (abfd, ptr + 4);
107 ptr += 8;
108
109 if (datasz > (size_t) (ptr_end - ptr))
110 {
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;
117 }
118
119 if (type >= GNU_PROPERTY_LOPROC)
120 {
121 if (bed->elf_machine_code == EM_NONE)
122 {
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;
127 }
128 else if (type < GNU_PROPERTY_LOUSER
129 && bed->parse_gnu_properties)
130 {
131 enum elf_property_kind kind
132 = bed->parse_gnu_properties (abfd, type, ptr, datasz);
133 if (kind == property_corrupt)
134 {
135 /* Clear all properties. */
136 elf_properties (abfd) = NULL;
137 return false;
138 }
139 else if (kind != property_ignored)
140 goto next;
141 }
142 }
143 else
144 {
145 switch (type)
146 {
147 case GNU_PROPERTY_STACK_SIZE:
148 if (datasz != align_size)
149 {
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;
156 }
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;
164
165 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
166 if (datasz != 0)
167 {
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;
174 }
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;
179
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))
185 {
186 if (datasz != 4)
187 {
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;
194 }
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))
203 {
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;
209 }
210 goto next;
211 }
212 break;
213 }
214 }
215
216 _bfd_error_handler
217 (_("warning: %pB: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
218 abfd, note->type, type);
219
220 next:
221 ptr += (datasz + (align_size - 1)) & ~ (align_size - 1);
222 }
223
224 return true;
225 }
226
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. */
230
231 static bool
232 elf_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd, bfd *bbfd,
233 elf_property *aprop, elf_property *bprop)
234 {
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;
239
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);
244
245 switch (pr_type)
246 {
247 case GNU_PROPERTY_STACK_SIZE:
248 if (aprop != NULL && bprop != NULL)
249 {
250 if (bprop->u.number > aprop->u.number)
251 {
252 aprop->u.number = bprop->u.number;
253 return true;
254 }
255 break;
256 }
257 /* FALLTHROUGH */
258
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;
263
264 default:
265 updated = false;
266 if (pr_type >= GNU_PROPERTY_UINT32_OR_LO
267 && pr_type <= GNU_PROPERTY_UINT32_OR_HI)
268 {
269 if (aprop != NULL && bprop != NULL)
270 {
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)
275 {
276 aprop->pr_kind = property_remove;
277 updated = true;
278 }
279 else
280 updated = number != (unsigned int) aprop->u.number;
281 }
282 else
283 {
284 /* Only one of APROP and BPROP can be NULL. */
285 if (aprop != NULL)
286 {
287 if (aprop->u.number == 0)
288 {
289 /* Remove APROP if all bits are empty. */
290 aprop->pr_kind = property_remove;
291 updated = true;
292 }
293 }
294 else
295 {
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;
300 }
301 }
302 return updated;
303 }
304 else if (pr_type >= GNU_PROPERTY_UINT32_AND_LO
305 && pr_type <= GNU_PROPERTY_UINT32_AND_HI)
306 {
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.
311 */
312 if (aprop != NULL && bprop != NULL)
313 {
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;
320 }
321 else
322 {
323 /* There should be no AND properties since some input
324 doesn't have them. */
325 if (aprop != NULL)
326 {
327 aprop->pr_kind = property_remove;
328 updated = true;
329 }
330 }
331 return updated;
332 }
333
334 /* Never should happen. */
335 abort ();
336 }
337
338 return false;
339 }
340
341 /* Return the property of TYPE on *LISTP and remove it from *LISTP if RM is
342 true. Return NULL if not found. */
343
344 static elf_property *
345 elf_find_and_remove_property (elf_property_list **listp,
346 unsigned int type, bool rm)
347 {
348 elf_property_list *list;
349
350 for (list = *listp; list; list = list->next)
351 {
352 if (type == list->property.pr_type)
353 {
354 /* Remove this property. */
355 if (rm)
356 *listp = list->next;
357 return &list->property;
358 }
359 else if (type < list->property.pr_type)
360 break;
361 listp = &list->next;
362 }
363
364 return NULL;
365 }
366
367 /* Merge GNU property list *LISTP in ABFD with FIRST_PBFD. */
368
369 static void
370 elf_merge_gnu_property_list (struct bfd_link_info *info, bfd *first_pbfd,
371 bfd *abfd, elf_property_list **listp)
372 {
373 elf_property_list *p, **lastp;
374 elf_property *pr;
375 bool number_p;
376 bfd_vma number = 0;
377
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)
382 {
383 if (p->property.pr_kind == property_number)
384 {
385 number_p = true;
386 number = p->property.u.number;
387 }
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)
396 {
397 if (info->has_map_file)
398 {
399 if (number_p)
400 {
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);
413 }
414 else
415 {
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);
425 }
426 }
427
428 /* Remove this property. */
429 *lastp = p->next;
430 continue;
431 }
432 else if (number_p)
433 {
434 if (pr != NULL)
435 {
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);
443 }
444 else
445 {
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);
452 }
453 }
454 lastp = &p->next;
455 }
456
457 /* Merge the remaining properties on *LISTP with FIRST_PBFD. */
458 for (p = *listp; p != NULL; p = p->next)
459 {
460 if (p->property.pr_kind == property_number)
461 {
462 number_p = true;
463 number = p->property.u.number;
464 }
465 else
466 number_p = false;
467
468 if (elf_merge_gnu_properties (info, first_pbfd, abfd, NULL, &p->property))
469 {
470 if (p->property.pr_type == GNU_PROPERTY_NO_COPY_ON_PROTECTED)
471 elf_has_no_copy_on_protected (first_pbfd) = true;
472
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;
480 }
481 else
482 {
483 pr = elf_find_and_remove_property (&elf_properties (first_pbfd),
484 p->property.pr_type,
485 false);
486 if (pr == NULL)
487 {
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);
498 }
499 else if (pr->pr_kind != property_remove)
500 abort ();
501 }
502 }
503 }
504
505 /* Get GNU property section size. */
506
507 static bfd_size_type
508 elf_get_gnu_property_section_size (elf_property_list *list,
509 unsigned int align_size)
510 {
511 bfd_size_type size;
512 unsigned int descsz;
513
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)
519 {
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);
532 }
533
534 return size;
535 }
536
537 /* Write GNU properties. */
538
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)
544 {
545 unsigned int descsz;
546 unsigned int datasz;
547 Elf_External_Note *e_note;
548
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");
556
557 size = descsz;
558 for (; list != NULL; list = list->next)
559 {
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;
571
572 /* Write out property value. */
573 switch (list->property.pr_kind)
574 {
575 case property_number:
576 switch (datasz)
577 {
578 default:
579 /* Never should happen. */
580 abort ();
581
582 case 0:
583 break;
584
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;
594
595 case 8:
596 bfd_h_put_64 (abfd, list->property.u.number,
597 contents + size);
598 break;
599 }
600 break;
601
602 default:
603 /* Never should happen. */
604 abort ();
605 }
606 size += datasz;
607
608 /* Align each property. */
609 size = (size + (align_size - 1)) & ~ (align_size - 1);
610 }
611 }
612
613 /* Set up GNU properties. Return the first relocatable ELF input with
614 GNU properties if found. Otherwise, return NULL. */
615
616 bfd *
617 _bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
618 {
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;
628
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))
636 {
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;
641
642 if (elf_properties (abfd) != NULL)
643 {
644 has_properties = true;
645
646 if (bfd_get_section_by_name (abfd,
647 NOTE_GNU_PROPERTY_SECTION_NAME)
648 != NULL)
649 {
650 /* Keep .note.gnu.property section in FIRST_PBFD. */
651 first_pbfd = abfd;
652 break;
653 }
654 }
655 }
656
657 if (info->indirect_extern_access > 0 && elf_bfd != NULL)
658 {
659 /* Support -z indirect-extern-access. */
660 if (first_pbfd == NULL)
661 {
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"));
672
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);
677
678 elf_section_type (sec) = SHT_NOTE;
679 first_pbfd = elf_bfd;
680 has_properties = true;
681 }
682
683 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_1_NEEDED, 4);
684 if (p->pr_kind == property_unknown)
685 {
686 /* Create GNU_PROPERTY_1_NEEDED. */
687 p->u.number
688 = GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
689 p->pr_kind = property_number;
690 }
691 else
692 p->u.number
693 |= GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
694 }
695
696 /* Do nothing if there is no .note.gnu.property section. */
697 if (!has_properties)
698 return NULL;
699
700 /* Merge .note.gnu.property sections. */
701 info->callbacks->minfo (_("\n"));
702 info->callbacks->minfo (_("Merging program properties\n"));
703 info->callbacks->minfo (_("\n"));
704
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)
708 {
709 elf_property_list *null_ptr = NULL;
710 elf_property_list **listp = &null_ptr;
711
712 /* Merge .note.gnu.property section in relocatable ELF input. */
713 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
714 {
715 list = elf_properties (abfd);
716
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);
723 }
724 else
725 list = NULL;
726
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);
732
733 if (list != NULL)
734 {
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;
740 }
741 }
742
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)
746 {
747 bfd_size_type size;
748 bfd_byte *contents;
749 unsigned int align_size = elfclass == ELFCLASS64 ? 8 : 4;
750
751 sec = bfd_get_section_by_name (first_pbfd,
752 NOTE_GNU_PROPERTY_SECTION_NAME);
753 BFD_ASSERT (sec != NULL);
754
755 /* Update stack size in .note.gnu.property with -z stack-size=N
756 if N > 0. */
757 if (info->stacksize > 0)
758 {
759 bfd_vma stacksize = info->stacksize;
760
761 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE,
762 align_size);
763 if (p->pr_kind == property_unknown)
764 {
765 /* Create GNU_PROPERTY_STACK_SIZE. */
766 p->u.number = stacksize;
767 p->pr_kind = property_number;
768 }
769 else if (stacksize > p->u.number)
770 p->u.number = stacksize;
771 }
772 else if (elf_properties (first_pbfd) == NULL)
773 {
774 /* Discard .note.gnu.property section if all properties have
775 been removed. */
776 sec->output_section = bfd_abs_section_ptr;
777 return NULL;
778 }
779
780 /* Fix up GNU properties. */
781 if (bed->fixup_gnu_properties)
782 bed->fixup_gnu_properties (info, &elf_properties (first_pbfd));
783
784 if (elf_properties (first_pbfd) == NULL)
785 {
786 /* Discard .note.gnu.property section if all properties have
787 been removed. */
788 sec->output_section = bfd_abs_section_ptr;
789 return NULL;
790 }
791
792 /* Compute the section size. */
793 list = elf_properties (first_pbfd);
794 size = elf_get_gnu_property_section_size (list, align_size);
795
796 /* Update .note.gnu.property section now. */
797 sec->size = size;
798 contents = (bfd_byte *) bfd_zalloc (first_pbfd, size);
799
800 if (info->indirect_extern_access <= 0)
801 {
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)
806 {
807 if (info->indirect_extern_access < 0)
808 {
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;
815 }
816 else
817 /* Turn off indirect external access. */
818 p->u.number
819 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
820 }
821 }
822
823 elf_write_gnu_properties (info, first_pbfd, contents, list, size,
824 align_size);
825
826 /* Cache the section contents for elf_link_input_bfd. */
827 elf_section_data (sec)->this_hdr.contents = contents;
828
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;
833
834 if (info->indirect_extern_access > 0)
835 {
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;
841 }
842 }
843
844 return first_pbfd;
845 }
846
847 /* Convert GNU property size. */
848
849 bfd_size_type
850 _bfd_elf_convert_gnu_property_size (bfd *ibfd, bfd *obfd)
851 {
852 unsigned int align_size;
853 const struct elf_backend_data *bed;
854 elf_property_list *list = elf_properties (ibfd);
855
856 bed = get_elf_backend_data (obfd);
857 align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
858
859 /* Get the output .note.gnu.property section size. */
860 return elf_get_gnu_property_section_size (list, align_size);
861 }
862
863 /* Convert GNU properties. */
864
865 bool
866 _bfd_elf_convert_gnu_properties (bfd *ibfd, asection *isec,
867 bfd *obfd, bfd_byte **ptr,
868 bfd_size_type *ptr_size)
869 {
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);
875
876 bed = get_elf_backend_data (obfd);
877 align_shift = bed->s->elfclass == ELFCLASS64 ? 3 : 2;
878
879 /* Get the output .note.gnu.property section size. */
880 size = bfd_section_size (isec->output_section);
881
882 /* Update the output .note.gnu.property section alignment. */
883 bfd_set_section_alignment (isec->output_section, align_shift);
884
885 if (size > bfd_section_size (isec))
886 {
887 contents = (bfd_byte *) bfd_malloc (size);
888 if (contents == NULL)
889 return false;
890 free (*ptr);
891 *ptr = contents;
892 }
893 else
894 contents = *ptr;
895
896 *ptr_size = size;
897
898 /* Generate the output .note.gnu.property section. */
899 elf_write_gnu_properties (NULL, ibfd, contents, list, size,
900 1 << align_shift);
901
902 return true;
903 }