objdump: Fix check for corrupt reloc information, to allow for the fact that PDP11...
[deliverable/binutils-gdb.git] / bfd / elf-properties.c
CommitLineData
46bed679 1/* ELF program property support.
219d1afa 2 Copyright (C) 2017-2018 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
79bfd_boolean
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 {
89bad_size:
90 _bfd_error_handler
871b3ab2 91 (_("warning: %pB: corrupt GNU_PROPERTY_TYPE (%ld) size: %#lx"),
46bed679
L
92 abfd, note->type, note->descsz);
93 return FALSE;
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;
116 return FALSE;
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;
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
871b3ab2 151 (_("warning: %pB: corrupt stack size: 0x%x"),
46bed679
L
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
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;
173 return FALSE;
174 }
175 prop = _bfd_elf_get_property (abfd, type, datasz);
a5b4ee94 176 elf_has_no_copy_on_protected (abfd) = TRUE;
46bed679
L
177 prop->pr_kind = property_number;
178 goto next;
179
180 default:
181 break;
182 }
183 }
184
185 _bfd_error_handler
871b3ab2 186 (_("warning: %pB: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
46bed679
L
187 abfd, note->type, type);
188
189next:
190 ptr += (datasz + (align_size - 1)) & ~ (align_size - 1);
46bed679
L
191 }
192
193 return TRUE;
194}
195
196/* Merge GNU property BPROP with APROP. If APROP isn't NULL, return TRUE
197 if APROP is updated. Otherwise, return TRUE if BPROP should be merged
198 with ABFD. */
199
200static bfd_boolean
5c3ce2bc
L
201elf_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd,
202 elf_property *aprop, elf_property *bprop)
46bed679
L
203{
204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
205 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
206
207 if (bed->merge_gnu_properties != NULL
208 && pr_type >= GNU_PROPERTY_LOPROC
209 && pr_type < GNU_PROPERTY_LOUSER)
5c3ce2bc 210 return bed->merge_gnu_properties (info, abfd, aprop, bprop);
46bed679
L
211
212 switch (pr_type)
213 {
214 case GNU_PROPERTY_STACK_SIZE:
215 if (aprop != NULL && bprop != NULL)
216 {
217 if (bprop->u.number > aprop->u.number)
218 {
219 aprop->u.number = bprop->u.number;
220 return TRUE;
221 }
222 break;
223 }
224 /* FALLTHROUGH */
225
226 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
227 /* Return TRUE if APROP is NULL to indicate that BPROP should
228 be added to ABFD. */
229 return aprop == NULL;
230
231 default:
232 /* Never should happen. */
233 abort ();
234 }
235
236 return FALSE;
237}
238
239/* Return the property of TYPE on *LISTP and remove it from *LISTP.
240 Return NULL if not found. */
241
242static elf_property *
243elf_find_and_remove_property (elf_property_list **listp,
244 unsigned int type)
245{
246 elf_property_list *list;
247
248 for (list = *listp; list; list = list->next)
249 {
250 if (type == list->property.pr_type)
251 {
252 /* Remove this property. */
253 *listp = list->next;
254 return &list->property;
255 }
256 else if (type < list->property.pr_type)
257 break;
258 listp = &list->next;
259 }
260
261 return NULL;
262}
263
264/* Merge GNU property list *LISTP with ABFD. */
265
266static void
5c3ce2bc
L
267elf_merge_gnu_property_list (struct bfd_link_info *info, bfd *abfd,
268 elf_property_list **listp)
46bed679
L
269{
270 elf_property_list *p, **lastp;
271 elf_property *pr;
272
273 /* Merge each GNU property in ABFD with the one on *LISTP. */
274 lastp = &elf_properties (abfd);
275 for (p = *lastp; p; p = p->next)
276 {
277 pr = elf_find_and_remove_property (listp, p->property.pr_type);
278 /* Pass NULL to elf_merge_gnu_properties for the property which
279 isn't on *LISTP. */
5c3ce2bc 280 elf_merge_gnu_properties (info, abfd, &p->property, pr);
46bed679
L
281 if (p->property.pr_kind == property_remove)
282 {
283 /* Remove this property. */
284 *lastp = p->next;
285 continue;
286 }
287 lastp = &p->next;
288 }
289
290 /* Merge the remaining properties on *LISTP with ABFD. */
291 for (p = *listp; p != NULL; p = p->next)
5c3ce2bc 292 if (elf_merge_gnu_properties (info, abfd, NULL, &p->property))
46bed679 293 {
a5b4ee94
L
294 if (p->property.pr_type == GNU_PROPERTY_NO_COPY_ON_PROTECTED)
295 elf_has_no_copy_on_protected (abfd) = TRUE;
296
46bed679
L
297 pr = _bfd_elf_get_property (abfd, p->property.pr_type,
298 p->property.pr_datasz);
299 /* It must be a new property. */
300 if (pr->pr_kind != property_unknown)
301 abort ();
302 /* Add a new property. */
303 *pr = p->property;
304 }
305}
306
6404ab99
L
307/* Get GNU property section size. */
308
309static bfd_size_type
310elf_get_gnu_property_section_size (elf_property_list *list,
311 unsigned int align_size)
312{
313 bfd_size_type size;
314 unsigned int descsz;
315
316 /* Compute the output section size. */
317 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
318 descsz = (descsz + 3) & -(unsigned int) 4;
319 size = descsz;
320 for (; list != NULL; list = list->next)
321 {
322 /* There are 4 byte type + 4 byte datasz for each property. */
9c973a29
L
323 unsigned int datasz;
324 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
325 datasz = align_size;
326 else
327 datasz = list->property.pr_datasz;
328 size += 4 + 4 + datasz;
6404ab99
L
329 /* Align each property. */
330 size = (size + (align_size - 1)) & ~(align_size - 1);
331 }
332
333 return size;
334}
335
336/* Write GNU properties. */
337
338static void
339elf_write_gnu_properties (bfd *abfd, bfd_byte *contents,
340 elf_property_list *list, unsigned int size,
341 unsigned int align_size)
342{
343 unsigned int descsz;
9c973a29 344 unsigned int datasz;
6404ab99
L
345 Elf_External_Note *e_note;
346
347 e_note = (Elf_External_Note *) contents;
348 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
349 descsz = (descsz + 3) & -(unsigned int) 4;
350 bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
351 bfd_h_put_32 (abfd, size - descsz, &e_note->descsz);
352 bfd_h_put_32 (abfd, NT_GNU_PROPERTY_TYPE_0, &e_note->type);
353 memcpy (e_note->name, "GNU", sizeof "GNU");
354
355 size = descsz;
356 for (; list != NULL; list = list->next)
357 {
358 /* There are 4 byte type + 4 byte datasz for each property. */
9c973a29
L
359 if (list->property.pr_type == GNU_PROPERTY_STACK_SIZE)
360 datasz = align_size;
361 else
362 datasz = list->property.pr_datasz;
363 bfd_h_put_32 (abfd, list->property.pr_type, contents + size);
364 bfd_h_put_32 (abfd, datasz, contents + size + 4);
6404ab99
L
365 size += 4 + 4;
366
367 /* Write out property value. */
368 switch (list->property.pr_kind)
369 {
370 case property_number:
9c973a29 371 switch (datasz)
6404ab99
L
372 {
373 default:
374 /* Never should happen. */
375 abort ();
376
377 case 0:
378 break;
379
380 case 4:
381 bfd_h_put_32 (abfd, list->property.u.number,
382 contents + size);
383 break;
384
385 case 8:
386 bfd_h_put_64 (abfd, list->property.u.number,
387 contents + size);
388 break;
389 }
390 break;
391
392 default:
393 /* Never should happen. */
394 abort ();
395 }
9c973a29 396 size += datasz;
6404ab99
L
397
398 /* Align each property. */
399 size = (size + (align_size - 1)) & ~ (align_size - 1);
400 }
401}
402
fba37edd
L
403/* Set up GNU properties. Return the first relocatable ELF input with
404 GNU properties if found. Otherwise, return NULL. */
46bed679 405
fba37edd 406bfd *
46bed679
L
407_bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
408{
409 bfd *abfd, *first_pbfd = NULL;
410 elf_property_list *list;
411 asection *sec;
412 bfd_boolean has_properties = FALSE;
413 const struct elf_backend_data *bed
414 = get_elf_backend_data (info->output_bfd);
415 unsigned int elfclass = bed->s->elfclass;
416 int elf_machine_code = bed->elf_machine_code;
417
418 /* Find the first relocatable ELF input with GNU properties. */
419 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
abfa390d 421 && (abfd->flags & DYNAMIC) == 0
46bed679
L
422 && elf_properties (abfd) != NULL)
423 {
424 has_properties = TRUE;
425
426 /* Ignore GNU properties from ELF objects with different machine
b77db948
NC
427 code or class. Also skip objects without a GNU_PROPERTY note
428 section. */
46bed679
L
429 if ((elf_machine_code
430 == get_elf_backend_data (abfd)->elf_machine_code)
431 && (elfclass
b77db948
NC
432 == get_elf_backend_data (abfd)->s->elfclass)
433 && bfd_get_section_by_name (abfd,
434 NOTE_GNU_PROPERTY_SECTION_NAME) != NULL
435 )
46bed679
L
436 {
437 /* Keep .note.gnu.property section in FIRST_PBFD. */
438 first_pbfd = abfd;
439 break;
440 }
441 }
442
443 /* Do nothing if there is no .note.gnu.property section. */
444 if (!has_properties)
fba37edd 445 return NULL;
46bed679
L
446
447 /* Merge .note.gnu.property sections. */
448 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
abfa390d 449 if (abfd != first_pbfd && (abfd->flags & DYNAMIC) == 0)
46bed679
L
450 {
451 elf_property_list *null_ptr = NULL;
452 elf_property_list **listp = &null_ptr;
453
454 /* Merge .note.gnu.property section in relocatable ELF input. */
455 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
456 {
457 list = elf_properties (abfd);
458
459 /* Ignore GNU properties from ELF objects with different
460 machine code. */
461 if (list != NULL
462 && (elf_machine_code
463 == get_elf_backend_data (abfd)->elf_machine_code))
464 listp = &elf_properties (abfd);
465 }
466 else
467 list = NULL;
468
469 /* Merge properties with FIRST_PBFD. FIRST_PBFD can be NULL
470 when all properties are from ELF objects with different
471 machine code or class. */
472 if (first_pbfd != NULL)
5c3ce2bc 473 elf_merge_gnu_property_list (info, first_pbfd, listp);
46bed679
L
474
475 if (list != NULL)
476 {
b77db948 477 /* Discard the .note.gnu.property section in this bfd. */
46bed679
L
478 sec = bfd_get_section_by_name (abfd,
479 NOTE_GNU_PROPERTY_SECTION_NAME);
b77db948
NC
480 if (sec != NULL)
481 sec->output_section = bfd_abs_section_ptr;
46bed679
L
482 }
483 }
484
485 /* Rewrite .note.gnu.property section so that GNU properties are
486 always sorted by type even if input GNU properties aren't sorted. */
487 if (first_pbfd != NULL)
488 {
6404ab99 489 bfd_size_type size;
46bed679 490 bfd_byte *contents;
79c1bf3c 491 unsigned int align_size = elfclass == ELFCLASS64 ? 8 : 4;
46bed679
L
492
493 sec = bfd_get_section_by_name (first_pbfd,
494 NOTE_GNU_PROPERTY_SECTION_NAME);
b77db948 495 BFD_ASSERT (sec != NULL);
46bed679
L
496
497 /* Update stack size in .note.gnu.property with -z stack-size=N
498 if N > 0. */
499 if (info->stacksize > 0)
500 {
501 elf_property *p;
502 bfd_vma stacksize = info->stacksize;
503
504 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE,
505 align_size);
506 if (p->pr_kind == property_unknown)
507 {
508 /* Create GNU_PROPERTY_STACK_SIZE. */
509 p->u.number = stacksize;
510 p->pr_kind = property_number;
511 }
512 else if (stacksize > p->u.number)
513 p->u.number = stacksize;
514 }
515 else if (elf_properties (first_pbfd) == NULL)
516 {
517 /* Discard .note.gnu.property section if all properties have
518 been removed. */
519 sec->output_section = bfd_abs_section_ptr;
fba37edd 520 return NULL;
46bed679
L
521 }
522
bfb1e8c1
L
523 /* Fix up GNU properties. */
524 if (bed->fixup_gnu_properties)
525 bed->fixup_gnu_properties (info, &elf_properties (first_pbfd));
526
527 if (elf_properties (first_pbfd) == NULL)
528 {
529 /* Discard .note.gnu.property section if all properties have
530 been removed. */
531 sec->output_section = bfd_abs_section_ptr;
532 return NULL;
533 }
534
46bed679 535 /* Compute the section size. */
6404ab99
L
536 list = elf_properties (first_pbfd);
537 size = elf_get_gnu_property_section_size (list, align_size);
46bed679
L
538
539 /* Update .note.gnu.property section now. */
540 sec->size = size;
541 contents = (bfd_byte *) bfd_zalloc (first_pbfd, size);
542
6404ab99
L
543 elf_write_gnu_properties (first_pbfd, contents, list, size,
544 align_size);
46bed679
L
545
546 /* Cache the section contents for elf_link_input_bfd. */
547 elf_section_data (sec)->this_hdr.contents = contents;
a5b4ee94
L
548
549 /* If GNU_PROPERTY_NO_COPY_ON_PROTECTED is set, protected data
550 symbol is defined in the shared object. */
551 if (elf_has_no_copy_on_protected (first_pbfd))
552 info->extern_protected_data = FALSE;
46bed679 553 }
fba37edd
L
554
555 return first_pbfd;
46bed679 556}
6404ab99
L
557
558/* Convert GNU property size. */
559
560bfd_size_type
561_bfd_elf_convert_gnu_property_size (bfd *ibfd, bfd *obfd)
562{
563 unsigned int align_size;
564 const struct elf_backend_data *bed;
565 elf_property_list *list = elf_properties (ibfd);
566
567 bed = get_elf_backend_data (obfd);
568 align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
569
570 /* Get the output .note.gnu.property section size. */
571 return elf_get_gnu_property_section_size (list, align_size);
572}
573
574/* Convert GNU properties. */
575
576bfd_boolean
577_bfd_elf_convert_gnu_properties (bfd *ibfd, asection *isec,
578 bfd *obfd, bfd_byte **ptr,
579 bfd_size_type *ptr_size)
580{
581 unsigned int size;
582 bfd_byte *contents;
583 unsigned int align_shift;
584 const struct elf_backend_data *bed;
585 elf_property_list *list = elf_properties (ibfd);
586
587 bed = get_elf_backend_data (obfd);
588 align_shift = bed->s->elfclass == ELFCLASS64 ? 3 : 2;
589
590 /* Get the output .note.gnu.property section size. */
591 size = bfd_get_section_size (isec->output_section);
592
593 /* Update the output .note.gnu.property section alignment. */
594 bfd_set_section_alignment (obfd, isec->output_section, align_shift);
595
596 if (size > bfd_get_section_size (isec))
597 {
598 contents = (bfd_byte *) bfd_malloc (size);
599 free (*ptr);
600 *ptr = contents;
601 }
602 else
603 contents = *ptr;
604
605 *ptr_size = size;
606
607 /* Generate the output .note.gnu.property section. */
608 elf_write_gnu_properties (ibfd, contents, list, size, 1 << align_shift);
609
610 return TRUE;
611}
This page took 0.171102 seconds and 4 git commands to generate.