Commit | Line | Data |
---|---|---|
46bed679 L |
1 | /* ELF program property support. |
2 | Copyright (C) 2017 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 (_("%B: 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 | bfd_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 | { | |
89 | bad_size: | |
90 | _bfd_error_handler | |
e4097f5e | 91 | (_("warning: %B: corrupt GNU_PROPERTY_TYPE (%ld) size: %#lx"), |
46bed679 L |
92 | abfd, note->type, note->descsz); |
93 | return FALSE; | |
94 | } | |
95 | ||
96 | while (1) | |
97 | { | |
98 | unsigned int type = bfd_h_get_32 (abfd, ptr); | |
99 | unsigned int datasz = bfd_h_get_32 (abfd, ptr + 4); | |
100 | elf_property *prop; | |
101 | ||
102 | ptr += 8; | |
103 | ||
104 | if ((ptr + datasz) > ptr_end) | |
105 | { | |
106 | _bfd_error_handler | |
e4097f5e | 107 | (_("warning: %B: corrupt GNU_PROPERTY_TYPE (%ld) type (0x%x) datasz: 0x%x"), |
46bed679 L |
108 | abfd, note->type, type, datasz); |
109 | /* Clear all properties. */ | |
110 | elf_properties (abfd) = NULL; | |
111 | return FALSE; | |
112 | } | |
113 | ||
114 | if (type >= GNU_PROPERTY_LOPROC) | |
115 | { | |
537616aa L |
116 | if (bed->elf_machine_code == EM_NONE) |
117 | { | |
118 | /* Ignore processor-specific properties with generic ELF | |
119 | target vector. They should be handled by the matching | |
120 | ELF target vector. */ | |
121 | goto next; | |
122 | } | |
123 | else if (type < GNU_PROPERTY_LOUSER | |
124 | && bed->parse_gnu_properties) | |
46bed679 L |
125 | { |
126 | enum elf_property_kind kind | |
127 | = bed->parse_gnu_properties (abfd, type, ptr, datasz); | |
128 | if (kind == property_corrupt) | |
129 | { | |
130 | /* Clear all properties. */ | |
131 | elf_properties (abfd) = NULL; | |
132 | return FALSE; | |
133 | } | |
134 | else if (kind != property_ignored) | |
135 | goto next; | |
136 | } | |
137 | } | |
138 | else | |
139 | { | |
140 | switch (type) | |
141 | { | |
142 | case GNU_PROPERTY_STACK_SIZE: | |
143 | if (datasz != align_size) | |
144 | { | |
145 | _bfd_error_handler | |
e4097f5e | 146 | (_("warning: %B: corrupt stack size: 0x%x"), |
46bed679 L |
147 | abfd, datasz); |
148 | /* Clear all properties. */ | |
149 | elf_properties (abfd) = NULL; | |
150 | return FALSE; | |
151 | } | |
152 | prop = _bfd_elf_get_property (abfd, type, datasz); | |
153 | if (datasz == 8) | |
154 | prop->u.number = bfd_h_get_64 (abfd, ptr); | |
155 | else | |
156 | prop->u.number = bfd_h_get_32 (abfd, ptr); | |
157 | prop->pr_kind = property_number; | |
158 | goto next; | |
159 | ||
160 | case GNU_PROPERTY_NO_COPY_ON_PROTECTED: | |
161 | if (datasz != 0) | |
162 | { | |
163 | _bfd_error_handler | |
e4097f5e | 164 | (_("warning: %B: corrupt no copy on protected size: 0x%x"), |
46bed679 L |
165 | abfd, datasz); |
166 | /* Clear all properties. */ | |
167 | elf_properties (abfd) = NULL; | |
168 | return FALSE; | |
169 | } | |
170 | prop = _bfd_elf_get_property (abfd, type, datasz); | |
a5b4ee94 | 171 | elf_has_no_copy_on_protected (abfd) = TRUE; |
46bed679 L |
172 | prop->pr_kind = property_number; |
173 | goto next; | |
174 | ||
175 | default: | |
176 | break; | |
177 | } | |
178 | } | |
179 | ||
180 | _bfd_error_handler | |
e4097f5e | 181 | (_("warning: %B: unsupported GNU_PROPERTY_TYPE (%ld) type: 0x%x"), |
46bed679 L |
182 | abfd, note->type, type); |
183 | ||
184 | next: | |
185 | ptr += (datasz + (align_size - 1)) & ~ (align_size - 1); | |
186 | if (ptr == ptr_end) | |
187 | break; | |
188 | ||
189 | if (ptr > (ptr_end - 8)) | |
190 | goto bad_size; | |
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 | ||
200 | static bfd_boolean | |
5c3ce2bc L |
201 | elf_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 | ||
242 | static elf_property * | |
243 | elf_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 | ||
266 | static void | |
5c3ce2bc L |
267 | elf_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 | ||
fba37edd L |
307 | /* Set up GNU properties. Return the first relocatable ELF input with |
308 | GNU properties if found. Otherwise, return NULL. */ | |
46bed679 | 309 | |
fba37edd | 310 | bfd * |
46bed679 L |
311 | _bfd_elf_link_setup_gnu_properties (struct bfd_link_info *info) |
312 | { | |
313 | bfd *abfd, *first_pbfd = NULL; | |
314 | elf_property_list *list; | |
315 | asection *sec; | |
316 | bfd_boolean has_properties = FALSE; | |
317 | const struct elf_backend_data *bed | |
318 | = get_elf_backend_data (info->output_bfd); | |
319 | unsigned int elfclass = bed->s->elfclass; | |
320 | int elf_machine_code = bed->elf_machine_code; | |
321 | ||
322 | /* Find the first relocatable ELF input with GNU properties. */ | |
323 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) | |
324 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
abfa390d | 325 | && (abfd->flags & DYNAMIC) == 0 |
46bed679 L |
326 | && elf_properties (abfd) != NULL) |
327 | { | |
328 | has_properties = TRUE; | |
329 | ||
330 | /* Ignore GNU properties from ELF objects with different machine | |
331 | code or class. */ | |
332 | if ((elf_machine_code | |
333 | == get_elf_backend_data (abfd)->elf_machine_code) | |
334 | && (elfclass | |
335 | == get_elf_backend_data (abfd)->s->elfclass)) | |
336 | { | |
337 | /* Keep .note.gnu.property section in FIRST_PBFD. */ | |
338 | first_pbfd = abfd; | |
339 | break; | |
340 | } | |
341 | } | |
342 | ||
343 | /* Do nothing if there is no .note.gnu.property section. */ | |
344 | if (!has_properties) | |
fba37edd | 345 | return NULL; |
46bed679 L |
346 | |
347 | /* Merge .note.gnu.property sections. */ | |
348 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) | |
abfa390d | 349 | if (abfd != first_pbfd && (abfd->flags & DYNAMIC) == 0) |
46bed679 L |
350 | { |
351 | elf_property_list *null_ptr = NULL; | |
352 | elf_property_list **listp = &null_ptr; | |
353 | ||
354 | /* Merge .note.gnu.property section in relocatable ELF input. */ | |
355 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) | |
356 | { | |
357 | list = elf_properties (abfd); | |
358 | ||
359 | /* Ignore GNU properties from ELF objects with different | |
360 | machine code. */ | |
361 | if (list != NULL | |
362 | && (elf_machine_code | |
363 | == get_elf_backend_data (abfd)->elf_machine_code)) | |
364 | listp = &elf_properties (abfd); | |
365 | } | |
366 | else | |
367 | list = NULL; | |
368 | ||
369 | /* Merge properties with FIRST_PBFD. FIRST_PBFD can be NULL | |
370 | when all properties are from ELF objects with different | |
371 | machine code or class. */ | |
372 | if (first_pbfd != NULL) | |
5c3ce2bc | 373 | elf_merge_gnu_property_list (info, first_pbfd, listp); |
46bed679 L |
374 | |
375 | if (list != NULL) | |
376 | { | |
377 | /* Discard .note.gnu.property section in the rest inputs. */ | |
378 | sec = bfd_get_section_by_name (abfd, | |
379 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
380 | sec->output_section = bfd_abs_section_ptr; | |
381 | } | |
382 | } | |
383 | ||
384 | /* Rewrite .note.gnu.property section so that GNU properties are | |
385 | always sorted by type even if input GNU properties aren't sorted. */ | |
386 | if (first_pbfd != NULL) | |
387 | { | |
388 | unsigned int size; | |
389 | unsigned int descsz; | |
390 | bfd_byte *contents; | |
391 | Elf_External_Note *e_note; | |
392 | unsigned int align_size = bed->s->elfclass == ELFCLASS64 ? 8 : 4; | |
393 | ||
394 | sec = bfd_get_section_by_name (first_pbfd, | |
395 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
396 | ||
397 | /* Update stack size in .note.gnu.property with -z stack-size=N | |
398 | if N > 0. */ | |
399 | if (info->stacksize > 0) | |
400 | { | |
401 | elf_property *p; | |
402 | bfd_vma stacksize = info->stacksize; | |
403 | ||
404 | p = _bfd_elf_get_property (first_pbfd, GNU_PROPERTY_STACK_SIZE, | |
405 | align_size); | |
406 | if (p->pr_kind == property_unknown) | |
407 | { | |
408 | /* Create GNU_PROPERTY_STACK_SIZE. */ | |
409 | p->u.number = stacksize; | |
410 | p->pr_kind = property_number; | |
411 | } | |
412 | else if (stacksize > p->u.number) | |
413 | p->u.number = stacksize; | |
414 | } | |
415 | else if (elf_properties (first_pbfd) == NULL) | |
416 | { | |
417 | /* Discard .note.gnu.property section if all properties have | |
418 | been removed. */ | |
419 | sec->output_section = bfd_abs_section_ptr; | |
fba37edd | 420 | return NULL; |
46bed679 L |
421 | } |
422 | ||
423 | /* Compute the section size. */ | |
424 | descsz = offsetof (Elf_External_Note, name[sizeof "GNU"]); | |
425 | descsz = (descsz + 3) & -(unsigned int) 4; | |
426 | size = descsz; | |
427 | for (list = elf_properties (first_pbfd); | |
428 | list != NULL; | |
429 | list = list->next) | |
430 | { | |
431 | /* There are 4 byte type + 4 byte datasz for each property. */ | |
432 | size += 4 + 4 + list->property.pr_datasz; | |
433 | /* Align each property. */ | |
434 | size = (size + (align_size - 1)) & ~(align_size - 1); | |
435 | } | |
436 | ||
437 | /* Update .note.gnu.property section now. */ | |
438 | sec->size = size; | |
439 | contents = (bfd_byte *) bfd_zalloc (first_pbfd, size); | |
440 | ||
441 | e_note = (Elf_External_Note *) contents; | |
442 | bfd_h_put_32 (first_pbfd, sizeof "GNU", &e_note->namesz); | |
443 | bfd_h_put_32 (first_pbfd, size - descsz, &e_note->descsz); | |
444 | bfd_h_put_32 (first_pbfd, NT_GNU_PROPERTY_TYPE_0, &e_note->type); | |
445 | memcpy (e_note->name, "GNU", sizeof "GNU"); | |
446 | ||
447 | size = descsz; | |
448 | for (list = elf_properties (first_pbfd); | |
449 | list != NULL; | |
450 | list = list->next) | |
451 | { | |
452 | /* There are 4 byte type + 4 byte datasz for each property. */ | |
453 | bfd_h_put_32 (first_pbfd, list->property.pr_type, | |
454 | contents + size); | |
455 | bfd_h_put_32 (first_pbfd, list->property.pr_datasz, | |
456 | contents + size + 4); | |
457 | size += 4 + 4; | |
458 | ||
459 | /* Write out property value. */ | |
460 | switch (list->property.pr_kind) | |
461 | { | |
462 | case property_number: | |
463 | switch (list->property.pr_datasz) | |
464 | { | |
465 | default: | |
466 | /* Never should happen. */ | |
467 | abort (); | |
468 | ||
469 | case 0: | |
470 | break; | |
471 | ||
472 | case 4: | |
473 | bfd_h_put_32 (first_pbfd, list->property.u.number, | |
474 | contents + size); | |
475 | break; | |
476 | ||
477 | case 8: | |
478 | bfd_h_put_64 (first_pbfd, list->property.u.number, | |
479 | contents + size); | |
480 | break; | |
481 | } | |
482 | break; | |
483 | ||
484 | default: | |
485 | /* Never should happen. */ | |
486 | abort (); | |
487 | } | |
488 | size += list->property.pr_datasz; | |
489 | ||
490 | /* Align each property. */ | |
491 | size = (size + (align_size - 1)) & ~ (align_size - 1); | |
492 | } | |
493 | ||
494 | /* Cache the section contents for elf_link_input_bfd. */ | |
495 | elf_section_data (sec)->this_hdr.contents = contents; | |
a5b4ee94 L |
496 | |
497 | /* If GNU_PROPERTY_NO_COPY_ON_PROTECTED is set, protected data | |
498 | symbol is defined in the shared object. */ | |
499 | if (elf_has_no_copy_on_protected (first_pbfd)) | |
500 | info->extern_protected_data = FALSE; | |
46bed679 | 501 | } |
fba37edd L |
502 | |
503 | return first_pbfd; | |
46bed679 | 504 | } |