1 /* ELF program property support.
2 Copyright (C) 2017 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 (_("%B: 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: %B: corrupt GNU_PROPERTY_TYPE (%ld) size: %#lx"),
92 abfd, note->type, note->descsz);
98 unsigned int type = bfd_h_get_32 (abfd, ptr);
99 unsigned int datasz = bfd_h_get_32 (abfd, ptr + 4);
104 if ((ptr + datasz) > ptr_end)
107 (_("warning: %B: corrupt GNU_PROPERTY_TYPE (%ld) type (0x%x) datasz: 0x%x"),
108 abfd, note->type, type, datasz);
109 /* Clear all properties. */
110 elf_properties (abfd) = NULL;
114 if (type >= GNU_PROPERTY_LOPROC)
116 if (bed->elf_machine_code == EM_NONE)
118 /* Ignore processor-specific properties with generic ELF
119 target vector. They should be handled by the matching
120 ELF target vector. */
123 else if (type < GNU_PROPERTY_LOUSER
124 && bed->parse_gnu_properties)
126 enum elf_property_kind kind
127 = bed->parse_gnu_properties (abfd, type, ptr, datasz);
128 if (kind == property_corrupt)
130 /* Clear all properties. */
131 elf_properties (abfd) = NULL;
134 else if (kind != property_ignored)
142 case GNU_PROPERTY_STACK_SIZE:
143 if (datasz != align_size)
146 (_("warning: %B: corrupt stack size: 0x%x"),
148 /* Clear all properties. */
149 elf_properties (abfd) = NULL;
152 prop = _bfd_elf_get_property (abfd, type, datasz);
154 prop->u.number = bfd_h_get_64 (abfd, ptr);
156 prop->u.number = bfd_h_get_32 (abfd, ptr);
157 prop->pr_kind = property_number;
160 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
164 (_("warning: %B: corrupt no copy on protected size: 0x%x"),
166 /* Clear all properties. */
167 elf_properties (abfd) = NULL;
170 prop = _bfd_elf_get_property (abfd, type, datasz);
171 prop->pr_kind = property_number;
180 (_("warning: %B: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"),
181 abfd, note->type, type);
184 ptr += (datasz + (align_size - 1)) & ~ (align_size - 1);
188 if (ptr > (ptr_end - 8))
195 /* Merge GNU property BPROP with APROP. If APROP isn't NULL, return TRUE
196 if APROP is updated. Otherwise, return TRUE if BPROP should be merged
200 elf_merge_gnu_properties (struct bfd_link_info *info, bfd *abfd,
201 elf_property *aprop, elf_property *bprop)
203 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
204 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
206 if (bed->merge_gnu_properties != NULL
207 && pr_type >= GNU_PROPERTY_LOPROC
208 && pr_type < GNU_PROPERTY_LOUSER)
209 return bed->merge_gnu_properties (info, abfd, aprop, bprop);
213 case GNU_PROPERTY_STACK_SIZE:
214 if (aprop != NULL && bprop != NULL)
216 if (bprop->u.number > aprop->u.number)
218 aprop->u.number = bprop->u.number;
225 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
226 /* Return TRUE if APROP is NULL to indicate that BPROP should
228 return aprop == NULL;
231 /* Never should happen. */
238 /* Return the property of TYPE on *LISTP and remove it from *LISTP.
239 Return NULL if not found. */
241 static elf_property *
242 elf_find_and_remove_property (elf_property_list **listp,
245 elf_property_list *list;
247 for (list = *listp; list; list = list->next)
249 if (type == list->property.pr_type)
251 /* Remove this property. */
253 return &list->property;
255 else if (type < list->property.pr_type)
263 /* Merge GNU property list *LISTP with ABFD. */
266 elf_merge_gnu_property_list (struct bfd_link_info *info, bfd *abfd,
267 elf_property_list **listp)
269 elf_property_list *p, **lastp;
272 /* Merge each GNU property in ABFD with the one on *LISTP. */
273 lastp = &elf_properties (abfd);
274 for (p = *lastp; p; p = p->next)
276 pr = elf_find_and_remove_property (listp, p->property.pr_type);
277 /* Pass NULL to elf_merge_gnu_properties for the property which
279 elf_merge_gnu_properties (info, abfd, &p->property, pr);
280 if (p->property.pr_kind == property_remove)
282 /* Remove this property. */
289 /* Merge the remaining properties on *LISTP with ABFD. */
290 for (p = *listp; p != NULL; p = p->next)
291 if (elf_merge_gnu_properties (info, abfd, NULL, &p->property))
293 pr = _bfd_elf_get_property (abfd, p->property.pr_type,
294 p->property.pr_datasz);
295 /* It must be a new property. */
296 if (pr->pr_kind != property_unknown)
298 /* Add a new property. */
303 /* Set up GNU properties. Return the first relocatable ELF input with
304 GNU properties if found. Otherwise, return NULL. */
307 _bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info)
309 bfd *abfd, *first_pbfd = NULL;
310 elf_property_list *list;
312 bfd_boolean has_properties = FALSE;
313 const struct elf_backend_data *bed
314 = get_elf_backend_data (info->output_bfd);
315 unsigned int elfclass = bed->s->elfclass;
316 int elf_machine_code = bed->elf_machine_code;
318 /* Find the first relocatable ELF input with GNU properties. */
319 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
320 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
321 && (abfd->flags & DYNAMIC) == 0
322 && elf_properties (abfd) != NULL)
324 has_properties = TRUE;
326 /* Ignore GNU properties from ELF objects with different machine
328 if ((elf_machine_code
329 == get_elf_backend_data (abfd)->elf_machine_code)
331 == get_elf_backend_data (abfd)->s->elfclass))
333 /* Keep .note.gnu.property section in FIRST_PBFD. */
339 /* Do nothing if there is no .note.gnu.property section. */
343 /* Merge .note.gnu.property sections. */
344 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
345 if (abfd != first_pbfd && (abfd->flags & DYNAMIC) == 0)
347 elf_property_list *null_ptr = NULL;
348 elf_property_list **listp = &null_ptr;
350 /* Merge .note.gnu.property section in relocatable ELF input. */
351 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
353 list = elf_properties (abfd);
355 /* Ignore GNU properties from ELF objects with different
359 == get_elf_backend_data (abfd)->elf_machine_code))
360 listp = &elf_properties (abfd);
365 /* Merge properties with FIRST_PBFD. FIRST_PBFD can be NULL
366 when all properties are from ELF objects with different
367 machine code or class. */
368 if (first_pbfd != NULL)
369 elf_merge_gnu_property_list (info, first_pbfd, listp);
373 /* Discard .note.gnu.property section in the rest inputs. */
374 sec = bfd_get_section_by_name (abfd,
375 NOTE_GNU_PROPERTY_SECTION_NAME);
376 sec->output_section = bfd_abs_section_ptr;
380 /* Rewrite .note.gnu.property section so that GNU properties are
381 always sorted by type even if input GNU properties aren't sorted. */
382 if (first_pbfd != NULL)
387 Elf_External_Note *e_note;
388 unsigned int align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4;
390 sec = bfd_get_section_by_name (first_pbfd,
391 NOTE_GNU_PROPERTY_SECTION_NAME);
393 /* Update stack size in .note.gnu.property with -z stack-size=N
395 if (info->stacksize > 0)
398 bfd_vma stacksize = info->stacksize;
400 p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE,
402 if (p->pr_kind == property_unknown)
404 /* Create GNU_PROPERTY_STACK_SIZE. */
405 p->u.number = stacksize;
406 p->pr_kind = property_number;
408 else if (stacksize > p->u.number)
409 p->u.number = stacksize;
411 else if (elf_properties (first_pbfd) == NULL)
413 /* Discard .note.gnu.property section if all properties have
415 sec->output_section = bfd_abs_section_ptr;
419 /* Compute the section size. */
420 descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]);
421 descsz = (descsz + 3) & -(unsigned int) 4;
423 for (list = elf_properties (first_pbfd);
427 /* There are 4 byte type + 4 byte datasz for each property. */
428 size += 4 + 4 + list->property.pr_datasz;
429 /* Align each property. */
430 size = (size + (align_size - 1)) & ~(align_size - 1);
433 /* Update .note.gnu.property section now. */
435 contents = (bfd_byte *) bfd_zalloc (first_pbfd, size);
437 e_note = (Elf_External_Note *) contents;
438 bfd_h_put_32 (first_pbfd, sizeof "GNU", &e_note->namesz);
439 bfd_h_put_32 (first_pbfd, size - descsz, &e_note->descsz);
440 bfd_h_put_32 (first_pbfd, NT_GNU_PROPERTY_TYPE_0, &e_note->type);
441 memcpy (e_note->name, "GNU", sizeof "GNU");
444 for (list = elf_properties (first_pbfd);
448 /* There are 4 byte type + 4 byte datasz for each property. */
449 bfd_h_put_32 (first_pbfd, list->property.pr_type,
451 bfd_h_put_32 (first_pbfd, list->property.pr_datasz,
452 contents + size + 4);
455 /* Write out property value. */
456 switch (list->property.pr_kind)
458 case property_number:
459 switch (list->property.pr_datasz)
462 /* Never should happen. */
469 bfd_h_put_32 (first_pbfd, list->property.u.number,
474 bfd_h_put_64 (first_pbfd, list->property.u.number,
481 /* Never should happen. */
484 size += list->property.pr_datasz;
486 /* Align each property. */
487 size = (size + (align_size - 1)) & ~ (align_size - 1);
490 /* Cache the section contents for elf_link_input_bfd. */
491 elf_section_data (sec)->this_hdr.contents = contents;