]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf-properties.c
elf: Add GNU_PROPERTY_UINT32_AND_XXX/GNU_PROPERTY_UINT32_OR_XXX
[thirdparty/binutils-gdb.git] / bfd / elf-properties.c
CommitLineData
46bed679 1/* ELF program property support.
250d07de 2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
46bed679
L
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
33elf_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 {
871b3ab2 65 _bfd_error_handler (_("%pB: out of memory in _bfd_elf_get_property"),
46bed679
L
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
0a1b45a2 79bool
46bed679
L
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 {
dc1e8a47 89 bad_size:
46bed679 90 _bfd_error_handler
871b3ab2 91 (_("warning: %pB: corrupt GNU_PROPERTY_TYPE (%ld) size: %#lx"),
46bed679 92 abfd, note->type, note->descsz);
0a1b45a2 93 return false;
46bed679
L
94 }
95
cf54ebff 96 while (ptr != ptr_end)
46bed679 97 {
cf54ebff
AM
98 unsigned int type;
99 unsigned int datasz;
46bed679
L
100 elf_property *prop;
101
cf54ebff
AM
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);
46bed679
L
107 ptr += 8;
108
cf54ebff 109 if (datasz > (size_t) (ptr_end - ptr))
46bed679
L
110 {
111 _bfd_error_handler
871b3ab2 112 (_("warning: %pB: corrupt GNU_PROPERTY_TYPE (%ld) type (0x%x) datasz: 0x%x"),
46bed679
L
113 abfd, note->type, type, datasz);
114 /* Clear all properties. */
115 elf_properties (abfd) = NULL;
0a1b45a2 116 return false;
46bed679
L
117 }
118
119 if (type >= GNU_PROPERTY_LOPROC)
120 {
537616aa
L
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)
46bed679
L
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;
0a1b45a2 137 return false;
46bed679
L
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
871b3ab2 151 (_("warning: %pB: corrupt stack size: 0x%x"),
46bed679
L
152 abfd, datasz);
153 /* Clear all properties. */
154 elf_properties (abfd) = NULL;
0a1b45a2 155 return false;
46bed679
L
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
871b3ab2 169 (_("warning: %pB: corrupt no copy on protected size: 0x%x"),
46bed679
L
170 abfd, datasz);
171 /* Clear all properties. */
172 elf_properties (abfd) = NULL;
0a1b45a2 173 return false;
46bed679
L
174 }
175 prop = _bfd_elf_get_property (abfd, type, datasz);
0a1b45a2 176 elf_has_no_copy_on_protected (abfd) = true;
46bed679
L
177 prop->pr_kind = property_number;
178 goto next;
179
180 default:
5a767724
L
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 goto next;
199 }
46bed679
L
200 break;
201 }
202 }
203
204 _bfd_error_handler
871b3ab2 205 (_("warning: %pB: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
46bed679
L
206 abfd, note->type, type);
207
dc1e8a47 208 next:
46bed679 209 ptr += (datasz + (align_size - 1)) & ~ (align_size - 1);
46bed679
L
210 }
211
0a1b45a2 212 return true;
46bed679
L
213}
214
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
217 with ABFD. */
218
0a1b45a2 219static bool
4e539114 220elf_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd, bfd *bbfd,
5c3ce2bc 221 elf_property *aprop, elf_property *bprop)
46bed679
L
222{
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;
5a767724
L
225 unsigned int number;
226 bool updated;
46bed679
L
227
228 if (bed->merge_gnu_properties != NULL
229 && pr_type >= GNU_PROPERTY_LOPROC
230 && pr_type < GNU_PROPERTY_LOUSER)
4e539114 231 return bed->merge_gnu_properties (info, abfd, bbfd, aprop, bprop);
46bed679
L
232
233 switch (pr_type)
234 {
235 case GNU_PROPERTY_STACK_SIZE:
236 if (aprop != NULL && bprop != NULL)
237 {
238 if (bprop->u.number > aprop->u.number)
239 {
240 aprop->u.number = bprop->u.number;
0a1b45a2 241 return true;
46bed679
L
242 }
243 break;
244 }
245 /* FALLTHROUGH */
246
247 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
248 /* Return TRUE if APROP is NULL to indicate that BPROP should
249 be added to ABFD. */
250 return aprop == NULL;
251
252 default:
5a767724
L
253 updated = false;
254 if (pr_type >= GNU_PROPERTY_UINT32_OR_LO
255 && pr_type <= GNU_PROPERTY_UINT32_OR_HI)
256 {
257 if (aprop != NULL && bprop != NULL)
258 {
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)
263 {
264 aprop->pr_kind = property_remove;
265 updated = true;
266 }
267 else
268 updated = number != (unsigned int) aprop->u.number;
269 }
270 else
271 {
272 /* Only one of APROP and BPROP can be NULL. */
273 if (aprop != NULL)
274 {
275 if (aprop->u.number == 0)
276 {
277 /* Remove APROP if all bits are empty. */
278 aprop->pr_kind = property_remove;
279 updated = true;
280 }
281 }
282 else
283 {
284 /* Return TRUE if APROP is NULL and all bits of BPROP
285 aren't empty to indicate that BPROP should be added
286 to ABFD. */
287 updated = bprop->u.number != 0;
288 }
289 }
290 return updated;
291 }
292 else if (pr_type >= GNU_PROPERTY_UINT32_AND_LO
293 && pr_type <= GNU_PROPERTY_UINT32_AND_HI)
294 {
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.
299 */
300 if (aprop != NULL && bprop != NULL)
301 {
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;
308 }
309 else
310 {
311 /* There should be no AND properties since some input
312 doesn't have them. */
313 if (aprop != NULL)
314 {
315 aprop->pr_kind = property_remove;
316 updated = true;
317 }
318 }
319 return updated;
320 }
321
46bed679
L
322 /* Never should happen. */
323 abort ();
324 }
325
0a1b45a2 326 return false;
46bed679
L
327}
328
ab9f654c
JB
329/* Return the property of TYPE on *LISTP and remove it from *LISTP if RM is
330 true. Return NULL if not found. */
46bed679
L
331
332static elf_property *
333elf_find_and_remove_property (elf_property_list **listp,
0a1b45a2 334 unsigned int type, bool rm)
46bed679
L
335{
336 elf_property_list *list;
337
338 for (list = *listp; list; list = list->next)
339 {
340 if (type == list->property.pr_type)
341 {
342 /* Remove this property. */
ab9f654c 343 if (rm)
d2ef37eb 344 *listp = list->next;
46bed679
L
345 return &list->property;
346 }
347 else if (type < list->property.pr_type)
348 break;
349 listp = &list->next;
350 }
351
352 return NULL;
353}
354
d2ef37eb 355/* Merge GNU property list *LISTP in ABFD with FIRST_PBFD. */
46bed679
L
356
357static void
d2ef37eb
L
358elf_merge_gnu_property_list (struct bfd_link_info *info, bfd *first_pbfd,
359 bfd *abfd, elf_property_list **listp)
46bed679
L
360{
361 elf_property_list *p, **lastp;
362 elf_property *pr;
0a1b45a2 363 bool number_p;
d2ef37eb 364 bfd_vma number = 0;
46bed679 365
d2ef37eb
L
366 /* Merge each GNU property in FIRST_PBFD with the one on *LISTP. */
367 lastp = &elf_properties (first_pbfd);
46bed679 368 for (p = *lastp; p; p = p->next)
d2ef37eb
L
369 if (p->property.pr_kind != property_remove)
370 {
371 if (p->property.pr_kind == property_number)
372 {
0a1b45a2 373 number_p = true;
d2ef37eb
L
374 number = p->property.u.number;
375 }
376 else
0a1b45a2 377 number_p = false;
d2ef37eb 378 pr = elf_find_and_remove_property (listp, p->property.pr_type,
0a1b45a2 379 true);
d2ef37eb
L
380 /* Pass NULL to elf_merge_gnu_properties for the property which
381 isn't on *LISTP. */
4e539114 382 elf_merge_gnu_properties (info, first_pbfd, abfd, &p->property, pr);
d2ef37eb
L
383 if (p->property.pr_kind == property_remove)
384 {
385 if (info->has_map_file)
386 {
387 if (number_p)
388 {
389 if (pr != NULL)
390 info->callbacks->minfo
391 (_("Removed property %W to merge %pB (0x%v) "
392 "and %pB (0x%v)\n"),
393 (bfd_vma) p->property.pr_type, first_pbfd,
394 number, abfd, pr->u.number);
395 else
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,
400 number, abfd);
401 }
402 else
403 {
404 if (pr != NULL)
405 info->callbacks->minfo
406 (_("Removed property %W to merge %pB and %pB\n"),
407 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
408 else
409 info->callbacks->minfo
410 (_("Removed property %W to merge %pB and %pB "
411 "(not found)\n"),
412 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
413 }
414 }
f93ab3a0
L
415
416 /* Remove this property. */
417 *lastp = p->next;
418 continue;
d2ef37eb
L
419 }
420 else if (number_p)
421 {
422 if (pr != NULL)
423 {
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) "
428 "and %pB (0x%v)\n"),
429 (bfd_vma) p->property.pr_type, p->property.u.number,
430 first_pbfd, number, abfd, pr->u.number);
431 }
432 else
433 {
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);
440 }
441 }
442 lastp = &p->next;
443 }
444
445 /* Merge the remaining properties on *LISTP with FIRST_PBFD. */
446 for (p = *listp; p != NULL; p = p->next)
46bed679 447 {
d2ef37eb 448 if (p->property.pr_kind == property_number)
46bed679 449 {
0a1b45a2 450 number_p = true;
d2ef37eb 451 number = p->property.u.number;
46bed679 452 }
d2ef37eb 453 else
0a1b45a2 454 number_p = false;
a5b4ee94 455
4e539114 456 if (elf_merge_gnu_properties (info, first_pbfd, abfd, NULL, &p->property))
d2ef37eb
L
457 {
458 if (p->property.pr_type == GNU_PROPERTY_NO_COPY_ON_PROTECTED)
0a1b45a2 459 elf_has_no_copy_on_protected (first_pbfd) = true;
d2ef37eb
L
460
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)
465 abort ();
466 /* Add a new property. */
467 *pr = p->property;
468 }
469 else
470 {
471 pr = elf_find_and_remove_property (&elf_properties (first_pbfd),
472 p->property.pr_type,
0a1b45a2 473 false);
d2ef37eb
L
474 if (pr == NULL)
475 {
476 if (number_p)
477 info->callbacks->minfo
478 (_("Removed property %W to merge %pB (not found) and "
479 "%pB (0x%v)\n"),
480 (bfd_vma) p->property.pr_type, first_pbfd, abfd,
481 number);
482 else
483 info->callbacks->minfo
484 (_("Removed property %W to merge %pB and %pB\n"),
485 (bfd_vma) p->property.pr_type, first_pbfd, abfd);
486 }
487 else if (pr->pr_kind != property_remove)
488 abort ();
489 }
490 }
46bed679
L
491}
492
6404ab99
L
493/* Get GNU property section size. */
494
495static bfd_size_type
496elf_get_gnu_property_section_size (elf_property_list *list,
497 unsigned int align_size)
498{
499 bfd_size_type size;
500 unsigned int descsz;
501
502 /* Compute the output section size. */
503 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
504 descsz = (descsz + 3) & -(unsigned int) 4;
505 size = descsz;
506 for (; list != NULL; list = list->next)
507 {
9c973a29 508 unsigned int datasz;
d2ef37eb
L
509 /* Check if this property should be skipped. */
510 if (list->property.pr_kind == property_remove)
511 continue;
512 /* There are 4 byte type + 4 byte datasz for each property. */
9c973a29
L
513 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
514 datasz = align_size;
515 else
516 datasz = list->property.pr_datasz;
517 size += 4 + 4 + datasz;
6404ab99
L
518 /* Align each property. */
519 size = (size + (align_size - 1)) & ~(align_size - 1);
520 }
521
522 return size;
523}
524
525/* Write GNU properties. */
526
527static void
528elf_write_gnu_properties (bfd *abfd, bfd_byte *contents,
529 elf_property_list *list, unsigned int size,
530 unsigned int align_size)
531{
532 unsigned int descsz;
9c973a29 533 unsigned int datasz;
6404ab99
L
534 Elf_External_Note *e_note;
535
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");
543
544 size = descsz;
545 for (; list != NULL; list = list->next)
546 {
d2ef37eb
L
547 /* Check if this property should be skipped. */
548 if (list->property.pr_kind == property_remove)
549 continue;
6404ab99 550 /* There are 4 byte type + 4 byte datasz for each property. */
9c973a29
L
551 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
552 datasz = align_size;
553 else
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);
6404ab99
L
557 size += 4 + 4;
558
559 /* Write out property value. */
560 switch (list->property.pr_kind)
561 {
562 case property_number:
9c973a29 563 switch (datasz)
6404ab99
L
564 {
565 default:
566 /* Never should happen. */
567 abort ();
568
569 case 0:
570 break;
571
572 case 4:
573 bfd_h_put_32 (abfd, list->property.u.number,
574 contents + size);
575 break;
576
577 case 8:
578 bfd_h_put_64 (abfd, list->property.u.number,
579 contents + size);
580 break;
581 }
582 break;
583
584 default:
585 /* Never should happen. */
586 abort ();
587 }
9c973a29 588 size += datasz;
6404ab99
L
589
590 /* Align each property. */
591 size = (size + (align_size - 1)) & ~ (align_size - 1);
592 }
593}
594
fba37edd
L
595/* Set up GNU properties. Return the first relocatable ELF input with
596 GNU properties if found. Otherwise, return NULL. */
46bed679 597
fba37edd 598bfd *
46bed679
L
599_bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
600{
601 bfd *abfd, *first_pbfd = NULL;
602 elf_property_list *list;
603 asection *sec;
0a1b45a2 604 bool has_properties = false;
46bed679
L
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;
609
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
abfa390d 613 && (abfd->flags & DYNAMIC) == 0
46bed679
L
614 && elf_properties (abfd) != NULL)
615 {
0a1b45a2 616 has_properties = true;
46bed679
L
617
618 /* Ignore GNU properties from ELF objects with different machine
b77db948
NC
619 code or class. Also skip objects without a GNU_PROPERTY note
620 section. */
46bed679
L
621 if ((elf_machine_code
622 == get_elf_backend_data (abfd)->elf_machine_code)
623 && (elfclass
b77db948
NC
624 == get_elf_backend_data (abfd)->s->elfclass)
625 && bfd_get_section_by_name (abfd,
626 NOTE_GNU_PROPERTY_SECTION_NAME) != NULL
627 )
46bed679
L
628 {
629 /* Keep .note.gnu.property section in FIRST_PBFD. */
630 first_pbfd = abfd;
631 break;
632 }
633 }
634
635 /* Do nothing if there is no .note.gnu.property section. */
636 if (!has_properties)
fba37edd 637 return NULL;
46bed679
L
638
639 /* Merge .note.gnu.property sections. */
d2ef37eb
L
640 info->callbacks->minfo (_("\n"));
641 info->callbacks->minfo (_("Merging program properties\n"));
642 info->callbacks->minfo (_("\n"));
643
46bed679 644 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
ffd9e4d0 645 if (abfd != first_pbfd
cd702818 646 && (abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED)) == 0)
46bed679
L
647 {
648 elf_property_list *null_ptr = NULL;
649 elf_property_list **listp = &null_ptr;
650
651 /* Merge .note.gnu.property section in relocatable ELF input. */
652 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
653 {
654 list = elf_properties (abfd);
655
656 /* Ignore GNU properties from ELF objects with different
657 machine code. */
658 if (list != NULL
659 && (elf_machine_code
660 == get_elf_backend_data (abfd)->elf_machine_code))
661 listp = &elf_properties (abfd);
662 }
663 else
664 list = NULL;
665
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)
d2ef37eb 670 elf_merge_gnu_property_list (info, first_pbfd, abfd, listp);
46bed679
L
671
672 if (list != NULL)
673 {
b77db948 674 /* Discard the .note.gnu.property section in this bfd. */
46bed679
L
675 sec = bfd_get_section_by_name (abfd,
676 NOTE_GNU_PROPERTY_SECTION_NAME);
b77db948
NC
677 if (sec != NULL)
678 sec->output_section = bfd_abs_section_ptr;
46bed679
L
679 }
680 }
681
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)
685 {
6404ab99 686 bfd_size_type size;
46bed679 687 bfd_byte *contents;
79c1bf3c 688 unsigned int align_size = elfclass == ELFCLASS64 ? 8 : 4;
46bed679
L
689
690 sec = bfd_get_section_by_name (first_pbfd,
691 NOTE_GNU_PROPERTY_SECTION_NAME);
b77db948 692 BFD_ASSERT (sec != NULL);
46bed679
L
693
694 /* Update stack size in .note.gnu.property with -z stack-size=N
695 if N > 0. */
696 if (info->stacksize > 0)
697 {
698 elf_property *p;
699 bfd_vma stacksize = info->stacksize;
700
701 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE,
702 align_size);
703 if (p->pr_kind == property_unknown)
704 {
705 /* Create GNU_PROPERTY_STACK_SIZE. */
706 p->u.number = stacksize;
707 p->pr_kind = property_number;
708 }
709 else if (stacksize > p->u.number)
710 p->u.number = stacksize;
711 }
712 else if (elf_properties (first_pbfd) == NULL)
713 {
714 /* Discard .note.gnu.property section if all properties have
715 been removed. */
716 sec->output_section = bfd_abs_section_ptr;
fba37edd 717 return NULL;
46bed679
L
718 }
719
bfb1e8c1
L
720 /* Fix up GNU properties. */
721 if (bed->fixup_gnu_properties)
722 bed->fixup_gnu_properties (info, &elf_properties (first_pbfd));
723
724 if (elf_properties (first_pbfd) == NULL)
725 {
726 /* Discard .note.gnu.property section if all properties have
727 been removed. */
728 sec->output_section = bfd_abs_section_ptr;
729 return NULL;
730 }
731
46bed679 732 /* Compute the section size. */
6404ab99
L
733 list = elf_properties (first_pbfd);
734 size = elf_get_gnu_property_section_size (list, align_size);
46bed679
L
735
736 /* Update .note.gnu.property section now. */
737 sec->size = size;
738 contents = (bfd_byte *) bfd_zalloc (first_pbfd, size);
739
6404ab99
L
740 elf_write_gnu_properties (first_pbfd, contents, list, size,
741 align_size);
46bed679
L
742
743 /* Cache the section contents for elf_link_input_bfd. */
744 elf_section_data (sec)->this_hdr.contents = contents;
a5b4ee94
L
745
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))
0a1b45a2 749 info->extern_protected_data = false;
46bed679 750 }
fba37edd
L
751
752 return first_pbfd;
46bed679 753}
6404ab99
L
754
755/* Convert GNU property size. */
756
757bfd_size_type
758_bfd_elf_convert_gnu_property_size (bfd *ibfd, bfd *obfd)
759{
760 unsigned int align_size;
761 const struct elf_backend_data *bed;
762 elf_property_list *list = elf_properties (ibfd);
763
764 bed = get_elf_backend_data (obfd);
765 align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
766
767 /* Get the output .note.gnu.property section size. */
768 return elf_get_gnu_property_section_size (list, align_size);
769}
770
771/* Convert GNU properties. */
772
0a1b45a2 773bool
6404ab99
L
774_bfd_elf_convert_gnu_properties (bfd *ibfd, asection *isec,
775 bfd *obfd, bfd_byte **ptr,
776 bfd_size_type *ptr_size)
777{
778 unsigned int size;
779 bfd_byte *contents;
780 unsigned int align_shift;
781 const struct elf_backend_data *bed;
782 elf_property_list *list = elf_properties (ibfd);
783
784 bed = get_elf_backend_data (obfd);
785 align_shift = bed->s->elfclass == ELFCLASS64 ? 3 : 2;
786
787 /* Get the output .note.gnu.property section size. */
fd361982 788 size = bfd_section_size (isec->output_section);
6404ab99
L
789
790 /* Update the output .note.gnu.property section alignment. */
fd361982 791 bfd_set_section_alignment (isec->output_section, align_shift);
6404ab99 792
fd361982 793 if (size > bfd_section_size (isec))
6404ab99
L
794 {
795 contents = (bfd_byte *) bfd_malloc (size);
7a0fb7be 796 if (contents == NULL)
0a1b45a2 797 return false;
6404ab99
L
798 free (*ptr);
799 *ptr = contents;
800 }
801 else
802 contents = *ptr;
803
804 *ptr_size = size;
805
806 /* Generate the output .note.gnu.property section. */
807 elf_write_gnu_properties (ibfd, contents, list, size, 1 << align_shift);
808
0a1b45a2 809 return true;
6404ab99 810}