Bug Summary

File:src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c
Warning:line 921, column 21
Dereference of null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name elf-eh-frame.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/binutils-2.17/obj/bfd -resource-dir /usr/local/lib/clang/13.0.0 -D HAVE_CONFIG_H -I . -I /usr/src/gnu/usr.bin/binutils-2.17/bfd -I . -D NETBSD_CORE -I . -I /usr/src/gnu/usr.bin/binutils-2.17/bfd -I /usr/src/gnu/usr.bin/binutils-2.17/bfd/../include -I /usr/src/gnu/usr.bin/binutils-2.17/bfd/../intl -I ../intl -D PIE_DEFAULT=1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/gnu/usr.bin/binutils-2.17/obj/bfd -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c
1/* .eh_frame section optimization.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "elf-bfd.h"
25#include "elf/dwarf2.h"
26
27#define EH_FRAME_HDR_SIZE8 8
28
29struct cie
30{
31 unsigned int length;
32 unsigned int hash;
33 unsigned char version;
34 char augmentation[20];
35 bfd_vma code_align;
36 bfd_signed_vma data_align;
37 bfd_vma ra_column;
38 bfd_vma augmentation_size;
39 struct elf_link_hash_entry *personality;
40 asection *output_sec;
41 struct eh_cie_fde *cie_inf;
42 unsigned char per_encoding;
43 unsigned char lsda_encoding;
44 unsigned char fde_encoding;
45 unsigned char initial_insn_length;
46 unsigned char make_relative;
47 unsigned char make_lsda_relative;
48 unsigned char initial_instructions[50];
49};
50
51
52
53/* If *ITER hasn't reached END yet, read the next byte into *RESULT and
54 move onto the next byte. Return true on success. */
55
56static inline bfd_boolean
57read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result)
58{
59 if (*iter >= end)
60 return FALSE0;
61 *result = *((*iter)++);
62 return TRUE1;
63}
64
65/* Move *ITER over LENGTH bytes, or up to END, whichever is closer.
66 Return true it was possible to move LENGTH bytes. */
67
68static inline bfd_boolean
69skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length)
70{
71 if ((bfd_size_type) (end - *iter) < length)
72 {
73 *iter = end;
74 return FALSE0;
75 }
76 *iter += length;
77 return TRUE1;
78}
79
80/* Move *ITER over an leb128, stopping at END. Return true if the end
81 of the leb128 was found. */
82
83static bfd_boolean
84skip_leb128 (bfd_byte **iter, bfd_byte *end)
85{
86 unsigned char byte;
87 do
88 if (!read_byte (iter, end, &byte))
89 return FALSE0;
90 while (byte & 0x80);
91 return TRUE1;
92}
93
94/* Like skip_leb128, but treat the leb128 as an unsigned value and
95 store it in *VALUE. */
96
97static bfd_boolean
98read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value)
99{
100 bfd_byte *start, *p;
101
102 start = *iter;
103 if (!skip_leb128 (iter, end))
104 return FALSE0;
105
106 p = *iter;
107 *value = *--p;
108 while (p > start)
109 *value = (*value << 7) | (*--p & 0x7f);
110
111 return TRUE1;
112}
113
114/* Like read_uleb128, but for signed values. */
115
116static bfd_boolean
117read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value)
118{
119 bfd_byte *start, *p;
120
121 start = *iter;
122 if (!skip_leb128 (iter, end))
123 return FALSE0;
124
125 p = *iter;
126 *value = ((*--p & 0x7f) ^ 0x40) - 0x40;
127 while (p > start)
128 *value = (*value << 7) | (*--p & 0x7f);
129
130 return TRUE1;
131}
132
133/* Return 0 if either encoding is variable width, or not yet known to bfd. */
134
135static
136int get_DW_EH_PE_width (int encoding, int ptr_size)
137{
138 /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame
139 was added to bfd. */
140 if ((encoding & 0x60) == 0x60)
141 return 0;
142
143 switch (encoding & 7)
144 {
145 case DW_EH_PE_udata20x02: return 2;
146 case DW_EH_PE_udata40x03: return 4;
147 case DW_EH_PE_udata80x04: return 8;
148 case DW_EH_PE_absptr0x00: return ptr_size;
149 default:
150 break;
151 }
152
153 return 0;
154}
155
156#define get_DW_EH_PE_signed(encoding)(((encoding) & 0x08) != 0) (((encoding) & DW_EH_PE_signed0x08) != 0)
157
158/* Read a width sized value from memory. */
159
160static bfd_vma
161read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed)
162{
163 bfd_vma value;
164
165 switch (width)
166 {
167 case 2:
168 if (is_signed)
169 value = bfd_get_signed_16 (abfd, buf)((*((abfd)->xvec->bfd_getx_signed_16)) (buf));
170 else
171 value = bfd_get_16 (abfd, buf)((*((abfd)->xvec->bfd_getx16)) (buf));
172 break;
173 case 4:
174 if (is_signed)
175 value = bfd_get_signed_32 (abfd, buf)((*((abfd)->xvec->bfd_getx_signed_32)) (buf));
176 else
177 value = bfd_get_32 (abfd, buf)((*((abfd)->xvec->bfd_getx32)) (buf));
178 break;
179 case 8:
180 if (is_signed)
181 value = bfd_get_signed_64 (abfd, buf)((*((abfd)->xvec->bfd_getx_signed_64)) (buf));
182 else
183 value = bfd_get_64 (abfd, buf)((*((abfd)->xvec->bfd_getx64)) (buf));
184 break;
185 default:
186 BFD_FAIL ()do { bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,186); } while (0)
;
187 return 0;
188 }
189
190 return value;
191}
192
193/* Store a width sized value to memory. */
194
195static void
196write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width)
197{
198 switch (width)
199 {
200 case 2: bfd_put_16 (abfd, value, buf)((*((abfd)->xvec->bfd_putx16)) ((value),(buf))); break;
201 case 4: bfd_put_32 (abfd, value, buf)((*((abfd)->xvec->bfd_putx32)) ((value),(buf))); break;
202 case 8: bfd_put_64 (abfd, value, buf)((*((abfd)->xvec->bfd_putx64)) ((value), (buf))); break;
203 default: BFD_FAIL ()do { bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,203); } while (0)
;
204 }
205}
206
207/* Return one if C1 and C2 CIEs can be merged. */
208
209static int
210cie_eq (const void *e1, const void *e2)
211{
212 const struct cie *c1 = e1;
213 const struct cie *c2 = e2;
214
215 if (c1->hash == c2->hash
216 && c1->length == c2->length
217 && c1->version == c2->version
218 && strcmp (c1->augmentation, c2->augmentation) == 0
219 && strcmp (c1->augmentation, "eh") != 0
220 && c1->code_align == c2->code_align
221 && c1->data_align == c2->data_align
222 && c1->ra_column == c2->ra_column
223 && c1->augmentation_size == c2->augmentation_size
224 && c1->personality == c2->personality
225 && c1->output_sec == c2->output_sec
226 && c1->per_encoding == c2->per_encoding
227 && c1->lsda_encoding == c2->lsda_encoding
228 && c1->fde_encoding == c2->fde_encoding
229 && c1->initial_insn_length == c2->initial_insn_length
230 && memcmp (c1->initial_instructions,
231 c2->initial_instructions,
232 c1->initial_insn_length) == 0)
233 return 1;
234
235 return 0;
236}
237
238static hashval_t
239cie_hash (const void *e)
240{
241 const struct cie *c = e;
242 return c->hash;
243}
244
245static hashval_t
246cie_compute_hash (struct cie *c)
247{
248 hashval_t h = 0;
249 h = iterative_hash_object (c->length, h)iterative_hash (&c->length, sizeof (c->length), h);
250 h = iterative_hash_object (c->version, h)iterative_hash (&c->version, sizeof (c->version), h
)
;
251 h = iterative_hash (c->augmentation, strlen (c->augmentation) + 1, h);
252 h = iterative_hash_object (c->code_align, h)iterative_hash (&c->code_align, sizeof (c->code_align
), h)
;
253 h = iterative_hash_object (c->data_align, h)iterative_hash (&c->data_align, sizeof (c->data_align
), h)
;
254 h = iterative_hash_object (c->ra_column, h)iterative_hash (&c->ra_column, sizeof (c->ra_column
), h)
;
255 h = iterative_hash_object (c->augmentation_size, h)iterative_hash (&c->augmentation_size, sizeof (c->augmentation_size
), h)
;
256 h = iterative_hash_object (c->personality, h)iterative_hash (&c->personality, sizeof (c->personality
), h)
;
257 h = iterative_hash_object (c->output_sec, h)iterative_hash (&c->output_sec, sizeof (c->output_sec
), h)
;
258 h = iterative_hash_object (c->per_encoding, h)iterative_hash (&c->per_encoding, sizeof (c->per_encoding
), h)
;
259 h = iterative_hash_object (c->lsda_encoding, h)iterative_hash (&c->lsda_encoding, sizeof (c->lsda_encoding
), h)
;
260 h = iterative_hash_object (c->fde_encoding, h)iterative_hash (&c->fde_encoding, sizeof (c->fde_encoding
), h)
;
261 h = iterative_hash_object (c->initial_insn_length, h)iterative_hash (&c->initial_insn_length, sizeof (c->
initial_insn_length), h)
;
262 h = iterative_hash (c->initial_instructions, c->initial_insn_length, h);
263 c->hash = h;
264 return h;
265}
266
267/* Return the number of extra bytes that we'll be inserting into
268 ENTRY's augmentation string. */
269
270static INLINE__inline__ unsigned int
271extra_augmentation_string_bytes (struct eh_cie_fde *entry)
272{
273 unsigned int size = 0;
274 if (entry->cie)
275 {
276 if (entry->add_augmentation_size)
277 size++;
278 if (entry->add_fde_encoding)
279 size++;
280 }
281 return size;
282}
283
284/* Likewise ENTRY's augmentation data. */
285
286static INLINE__inline__ unsigned int
287extra_augmentation_data_bytes (struct eh_cie_fde *entry)
288{
289 unsigned int size = 0;
290 if (entry->cie)
291 {
292 if (entry->add_augmentation_size)
293 size++;
294 if (entry->add_fde_encoding)
295 size++;
296 }
297 else
298 {
299 if (entry->cie_inf->add_augmentation_size)
300 size++;
301 }
302 return size;
303}
304
305/* Return the size that ENTRY will have in the output. ALIGNMENT is the
306 required alignment of ENTRY in bytes. */
307
308static unsigned int
309size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment)
310{
311 if (entry->removed)
312 return 0;
313 if (entry->size == 4)
314 return 4;
315 return (entry->size
316 + extra_augmentation_string_bytes (entry)
317 + extra_augmentation_data_bytes (entry)
318 + alignment - 1) & -alignment;
319}
320
321/* Assume that the bytes between *ITER and END are CFA instructions.
322 Try to move *ITER past the first instruction and return true on
323 success. ENCODED_PTR_WIDTH gives the width of pointer entries. */
324
325static bfd_boolean
326skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width)
327{
328 bfd_byte op;
329 bfd_vma length;
330
331 if (!read_byte (iter, end, &op))
332 return FALSE0;
333
334 switch (op & 0xc0 ? op & 0xc0 : op)
335 {
336 case DW_CFA_nop:
337 case DW_CFA_advance_loc:
338 case DW_CFA_restore:
339 case DW_CFA_remember_state:
340 case DW_CFA_restore_state:
341 case DW_CFA_GNU_window_save:
342 /* No arguments. */
343 return TRUE1;
344
345 case DW_CFA_offset:
346 case DW_CFA_restore_extended:
347 case DW_CFA_undefined:
348 case DW_CFA_same_value:
349 case DW_CFA_def_cfa_register:
350 case DW_CFA_def_cfa_offset:
351 case DW_CFA_def_cfa_offset_sf:
352 case DW_CFA_GNU_args_size:
353 /* One leb128 argument. */
354 return skip_leb128 (iter, end);
355
356 case DW_CFA_val_offset:
357 case DW_CFA_val_offset_sf:
358 case DW_CFA_offset_extended:
359 case DW_CFA_register:
360 case DW_CFA_def_cfa:
361 case DW_CFA_offset_extended_sf:
362 case DW_CFA_GNU_negative_offset_extended:
363 case DW_CFA_def_cfa_sf:
364 /* Two leb128 arguments. */
365 return (skip_leb128 (iter, end)
366 && skip_leb128 (iter, end));
367
368 case DW_CFA_def_cfa_expression:
369 /* A variable-length argument. */
370 return (read_uleb128 (iter, end, &length)
371 && skip_bytes (iter, end, length));
372
373 case DW_CFA_expression:
374 case DW_CFA_val_expression:
375 /* A leb128 followed by a variable-length argument. */
376 return (skip_leb128 (iter, end)
377 && read_uleb128 (iter, end, &length)
378 && skip_bytes (iter, end, length));
379
380 case DW_CFA_set_loc:
381 return skip_bytes (iter, end, encoded_ptr_width);
382
383 case DW_CFA_advance_loc1:
384 return skip_bytes (iter, end, 1);
385
386 case DW_CFA_advance_loc2:
387 return skip_bytes (iter, end, 2);
388
389 case DW_CFA_advance_loc4:
390 return skip_bytes (iter, end, 4);
391
392 case DW_CFA_MIPS_advance_loc8:
393 return skip_bytes (iter, end, 8);
394
395 default:
396 return FALSE0;
397 }
398}
399
400/* Try to interpret the bytes between BUF and END as CFA instructions.
401 If every byte makes sense, return a pointer to the first DW_CFA_nop
402 padding byte, or END if there is no padding. Return null otherwise.
403 ENCODED_PTR_WIDTH is as for skip_cfa_op. */
404
405static bfd_byte *
406skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width,
407 unsigned int *set_loc_count)
408{
409 bfd_byte *last;
410
411 last = buf;
412 while (buf < end)
413 if (*buf == DW_CFA_nop)
414 buf++;
415 else
416 {
417 if (*buf == DW_CFA_set_loc)
418 ++*set_loc_count;
419 if (!skip_cfa_op (&buf, end, encoded_ptr_width))
420 return 0;
421 last = buf;
422 }
423 return last;
424}
425
426/* This function is called for each input file before the .eh_frame
427 section is relocated. It discards duplicate CIEs and FDEs for discarded
428 functions. The function returns TRUE iff any entries have been
429 deleted. */
430
431bfd_boolean
432_bfd_elf_discard_section_eh_frame
433 (bfd *abfd, struct bfd_link_info *info, asection *sec,
434 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
435 struct elf_reloc_cookie *cookie)
436{
437#define REQUIRE(COND) \
438 do \
439 if (!(COND)) \
440 goto free_no_table; \
441 while (0)
442
443 bfd_byte *ehbuf = NULL((void*)0), *buf;
444 bfd_byte *last_fde;
445 struct eh_cie_fde *ent, *this_inf;
446 unsigned int hdr_length, hdr_id;
447 struct extended_cie
448 {
449 struct cie cie;
450 unsigned int offset;
451 unsigned int usage_count;
452 unsigned int entry;
453 } *ecies = NULL((void*)0), *ecie;
1
'ecies' initialized to a null pointer value
454 unsigned int ecie_count = 0, ecie_alloced = 0;
455 struct cie *cie;
456 struct elf_link_hash_table *htab;
457 struct eh_frame_hdr_info *hdr_info;
458 struct eh_frame_sec_info *sec_info = NULL((void*)0);
459 unsigned int offset;
460 unsigned int ptr_size;
461 unsigned int entry_alloced;
462
463 if (sec->size == 0)
2
Assuming field 'size' is not equal to 0
3
Taking false branch
464 {
465 /* This file does not contain .eh_frame information. */
466 return FALSE0;
467 }
468
469 if (bfd_is_abs_section (sec->output_section)((sec->output_section) == ((asection *) &bfd_abs_section
))
)
4
Assuming the condition is false
5
Taking false branch
470 {
471 /* At least one of the sections is being discarded from the
472 link, so we should just ignore them. */
473 return FALSE0;
474 }
475
476 htab = elf_hash_table (info)((struct elf_link_hash_table *) ((info)->hash));
477 hdr_info = &htab->eh_info;
478
479 if (hdr_info->cies == NULL((void*)0) && !info->relocatable)
6
Assuming field 'cies' is not equal to NULL
480 hdr_info->cies = htab_try_create (1, cie_hash, cie_eq, free);
481
482 /* Read the frame unwind information from abfd. */
483
484 REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf));
7
Assuming the condition is false
8
Taking false branch
9
Loop condition is false. Exiting loop
485
486 if (sec->size >= 4
10
Assuming field 'size' is < 4
487 && bfd_get_32 (abfd, ehbuf)((*((abfd)->xvec->bfd_getx32)) (ehbuf)) == 0
488 && cookie->rel == cookie->relend)
489 {
490 /* Empty .eh_frame section. */
491 free (ehbuf);
492 return FALSE0;
493 }
494
495 /* If .eh_frame section size doesn't fit into int, we cannot handle
496 it (it would need to use 64-bit .eh_frame format anyway). */
497 REQUIRE (sec->size == (unsigned int) sec->size);
11
Taking false branch
12
Loop condition is false. Exiting loop
498
499 ptr_size = (get_elf_backend_data (abfd)((const struct elf_backend_data *) (abfd)->xvec->backend_data
)
500 ->elf_backend_eh_frame_address_size (abfd, sec));
501 REQUIRE (ptr_size != 0);
13
Assuming 'ptr_size' is not equal to 0
14
Taking false branch
15
Loop condition is false. Exiting loop
502
503 buf = ehbuf;
504 sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info)
505 + 99 * sizeof (struct eh_cie_fde));
506 REQUIRE (sec_info);
16
Assuming 'sec_info' is non-null
17
Taking false branch
18
Loop condition is false. Exiting loop
507
508 entry_alloced = 100;
509
510#define ENSURE_NO_RELOCS(buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
\
511 REQUIRE (!(cookie->rel < cookie->relend \
512 && (cookie->rel->r_offset \
513 < (bfd_size_type) ((buf) - ehbuf)) \
514 && cookie->rel->r_info != 0))
515
516#define SKIP_RELOCS(buf)while (cookie->rel < cookie->relend && (cookie
->rel->r_offset < (bfd_size_type) ((buf) - ehbuf))) cookie
->rel++
\
517 while (cookie->rel < cookie->relend \
518 && (cookie->rel->r_offset \
519 < (bfd_size_type) ((buf) - ehbuf))) \
520 cookie->rel++
521
522#define GET_RELOC(buf)((cookie->rel < cookie->relend && (cookie->
rel->r_offset == (bfd_size_type) ((buf) - ehbuf))) ? cookie
->rel : ((void*)0))
\
523 ((cookie->rel < cookie->relend \
524 && (cookie->rel->r_offset \
525 == (bfd_size_type) ((buf) - ehbuf))) \
526 ? cookie->rel : NULL((void*)0))
527
528 for (;;)
19
Loop condition is true. Entering loop body
529 {
530 char *aug;
531 bfd_byte *start, *end, *insns, *insns_end;
532 bfd_size_type length;
533 unsigned int set_loc_count;
534
535 if (sec_info->count == entry_alloced)
20
Assuming 'entry_alloced' is not equal to field 'count'
21
Taking false branch
536 {
537 sec_info = bfd_realloc (sec_info,
538 sizeof (struct eh_frame_sec_info)
539 + ((entry_alloced + 99)
540 * sizeof (struct eh_cie_fde)));
541 REQUIRE (sec_info);
542
543 memset (&sec_info->entry[entry_alloced], 0,
544 100 * sizeof (struct eh_cie_fde));
545 entry_alloced += 100;
546 }
547
548 this_inf = sec_info->entry + sec_info->count;
549 last_fde = buf;
550
551 if ((bfd_size_type) (buf - ehbuf) == sec->size)
22
Assuming the condition is true
23
Taking true branch
552 break;
24
Execution continues on line 871
553
554 /* Read the length of the entry. */
555 REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4));
556 hdr_length = bfd_get_32 (abfd, buf - 4)((*((abfd)->xvec->bfd_getx32)) (buf - 4));
557
558 /* 64-bit .eh_frame is not supported. */
559 REQUIRE (hdr_length != 0xffffffff);
560
561 /* The CIE/FDE must be fully contained in this input section. */
562 REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size);
563 end = buf + hdr_length;
564
565 this_inf->offset = last_fde - ehbuf;
566 this_inf->size = 4 + hdr_length;
567
568 if (hdr_length == 0)
569 {
570 /* A zero-length CIE should only be found at the end of
571 the section. */
572 REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size);
573 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
574 sec_info->count++;
575 break;
576 }
577
578 REQUIRE (skip_bytes (&buf, end, 4));
579 hdr_id = bfd_get_32 (abfd, buf - 4)((*((abfd)->xvec->bfd_getx32)) (buf - 4));
580
581 if (hdr_id == 0)
582 {
583 unsigned int initial_insn_length;
584
585 /* CIE */
586 this_inf->cie = 1;
587
588 if (ecie_count == ecie_alloced)
589 {
590 ecies = bfd_realloc (ecies,
591 (ecie_alloced + 20) * sizeof (*ecies));
592 REQUIRE (ecies);
593 memset (&ecies[ecie_alloced], 0, 20 * sizeof (*ecies));
594 ecie_alloced += 20;
595 }
596
597 cie = &ecies[ecie_count].cie;
598 ecies[ecie_count].offset = this_inf->offset;
599 ecies[ecie_count++].entry = sec_info->count;
600 cie->length = hdr_length;
601 start = buf;
602 REQUIRE (read_byte (&buf, end, &cie->version));
603
604 /* Cannot handle unknown versions. */
605 REQUIRE (cie->version == 1 || cie->version == 3);
606 REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation));
607
608 strcpy (cie->augmentation, (char *) buf);
609 buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1;
610 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
611 if (buf[0] == 'e' && buf[1] == 'h')
612 {
613 /* GCC < 3.0 .eh_frame CIE */
614 /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__
615 is private to each CIE, so we don't need it for anything.
616 Just skip it. */
617 REQUIRE (skip_bytes (&buf, end, ptr_size));
618 SKIP_RELOCS (buf)while (cookie->rel < cookie->relend && (cookie
->rel->r_offset < (bfd_size_type) ((buf) - ehbuf))) cookie
->rel++
;
619 }
620 REQUIRE (read_uleb128 (&buf, end, &cie->code_align));
621 REQUIRE (read_sleb128 (&buf, end, &cie->data_align));
622 if (cie->version == 1)
623 {
624 REQUIRE (buf < end);
625 cie->ra_column = *buf++;
626 }
627 else
628 REQUIRE (read_uleb128 (&buf, end, &cie->ra_column));
629 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
630 cie->lsda_encoding = DW_EH_PE_omit0xff;
631 cie->fde_encoding = DW_EH_PE_omit0xff;
632 cie->per_encoding = DW_EH_PE_omit0xff;
633 aug = cie->augmentation;
634 if (aug[0] != 'e' || aug[1] != 'h')
635 {
636 if (*aug == 'z')
637 {
638 aug++;
639 REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size));
640 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
641 }
642
643 while (*aug != '\0')
644 switch (*aug++)
645 {
646 case 'L':
647 REQUIRE (read_byte (&buf, end, &cie->lsda_encoding));
648 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
649 REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size));
650 break;
651 case 'R':
652 REQUIRE (read_byte (&buf, end, &cie->fde_encoding));
653 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
654 REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size));
655 break;
656 case 'S':
657 break;
658 case 'P':
659 {
660 int per_width;
661
662 REQUIRE (read_byte (&buf, end, &cie->per_encoding));
663 per_width = get_DW_EH_PE_width (cie->per_encoding,
664 ptr_size);
665 REQUIRE (per_width);
666 if ((cie->per_encoding & 0xf0) == DW_EH_PE_aligned0x50)
667 {
668 length = -(buf - ehbuf) & (per_width - 1);
669 REQUIRE (skip_bytes (&buf, end, length));
670 }
671 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
672 /* Ensure we have a reloc here, against
673 a global symbol. */
674 if (GET_RELOC (buf)((cookie->rel < cookie->relend && (cookie->
rel->r_offset == (bfd_size_type) ((buf) - ehbuf))) ? cookie
->rel : ((void*)0))
!= NULL((void*)0))
675 {
676 unsigned long r_symndx;
677
678#ifdef BFD64
679 if (ptr_size == 8)
680 r_symndx = ELF64_R_SYM (cookie->rel->r_info)((cookie->rel->r_info) >> 32);
681 else
682#endif
683 r_symndx = ELF32_R_SYM (cookie->rel->r_info)((cookie->rel->r_info) >> 8);
684 if (r_symndx >= cookie->locsymcount)
685 {
686 struct elf_link_hash_entry *h;
687
688 r_symndx -= cookie->extsymoff;
689 h = cookie->sym_hashes[r_symndx];
690
691 while (h->root.type == bfd_link_hash_indirect
692 || h->root.type == bfd_link_hash_warning)
693 h = (struct elf_link_hash_entry *)
694 h->root.u.i.link;
695
696 cie->personality = h;
697 }
698 /* Cope with MIPS-style composite relocations. */
699 do
700 cookie->rel++;
701 while (GET_RELOC (buf)((cookie->rel < cookie->relend && (cookie->
rel->r_offset == (bfd_size_type) ((buf) - ehbuf))) ? cookie
->rel : ((void*)0))
!= NULL((void*)0));
702 }
703 REQUIRE (skip_bytes (&buf, end, per_width));
704 REQUIRE (cie->personality);
705 }
706 break;
707 default:
708 /* Unrecognized augmentation. Better bail out. */
709 goto free_no_table;
710 }
711 }
712
713 /* For shared libraries, try to get rid of as many RELATIVE relocs
714 as possible. */
715 if (info->shared
716 && (get_elf_backend_data (abfd)((const struct elf_backend_data *) (abfd)->xvec->backend_data
)
717 ->elf_backend_can_make_relative_eh_frame
718 (abfd, info, sec)))
719 {
720 if ((cie->fde_encoding & 0xf0) == DW_EH_PE_absptr0x00)
721 cie->make_relative = 1;
722 /* If the CIE doesn't already have an 'R' entry, it's fairly
723 easy to add one, provided that there's no aligned data
724 after the augmentation string. */
725 else if (cie->fde_encoding == DW_EH_PE_omit0xff
726 && (cie->per_encoding & 0xf0) != DW_EH_PE_aligned0x50)
727 {
728 if (*cie->augmentation == 0)
729 this_inf->add_augmentation_size = 1;
730 this_inf->add_fde_encoding = 1;
731 cie->make_relative = 1;
732 }
733 }
734
735 if (info->shared
736 && (get_elf_backend_data (abfd)((const struct elf_backend_data *) (abfd)->xvec->backend_data
)
737 ->elf_backend_can_make_lsda_relative_eh_frame
738 (abfd, info, sec))
739 && (cie->lsda_encoding & 0xf0) == DW_EH_PE_absptr0x00)
740 cie->make_lsda_relative = 1;
741
742 /* If FDE encoding was not specified, it defaults to
743 DW_EH_absptr. */
744 if (cie->fde_encoding == DW_EH_PE_omit0xff)
745 cie->fde_encoding = DW_EH_PE_absptr0x00;
746
747 initial_insn_length = end - buf;
748 if (initial_insn_length <= sizeof (cie->initial_instructions))
749 {
750 cie->initial_insn_length = initial_insn_length;
751 memcpy (cie->initial_instructions, buf, initial_insn_length);
752 }
753 insns = buf;
754 buf += initial_insn_length;
755 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
756 }
757 else
758 {
759 /* Find the corresponding CIE. */
760 unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
761 for (ecie = ecies; ecie < ecies + ecie_count; ++ecie)
762 if (cie_offset == ecie->offset)
763 break;
764
765 /* Ensure this FDE references one of the CIEs in this input
766 section. */
767 REQUIRE (ecie != ecies + ecie_count);
768 cie = &ecie->cie;
769
770 ENSURE_NO_RELOCS (buf)REQUIRE (!(cookie->rel < cookie->relend && (
cookie->rel->r_offset < (bfd_size_type) ((buf) - ehbuf
)) && cookie->rel->r_info != 0))
;
771 REQUIRE (GET_RELOC (buf)((cookie->rel < cookie->relend && (cookie->
rel->r_offset == (bfd_size_type) ((buf) - ehbuf))) ? cookie
->rel : ((void*)0))
);
772
773 if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie))
774 /* This is a FDE against a discarded section. It should
775 be deleted. */
776 this_inf->removed = 1;
777 else
778 {
779 if (info->shared
780 && (((cie->fde_encoding & 0xf0) == DW_EH_PE_absptr0x00
781 && cie->make_relative == 0)
782 || (cie->fde_encoding & 0xf0) == DW_EH_PE_aligned0x50))
783 {
784 /* If a shared library uses absolute pointers
785 which we cannot turn into PC relative,
786 don't create the binary search table,
787 since it is affected by runtime relocations. */
788 hdr_info->table = FALSE0;
789 }
790 ecie->usage_count++;
791 hdr_info->fde_count++;
792 this_inf->cie_inf = (void *) (ecie - ecies);
793 }
794
795 /* Skip the initial location and address range. */
796 start = buf;
797 length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
798 REQUIRE (skip_bytes (&buf, end, 2 * length));
799
800 /* Skip the augmentation size, if present. */
801 if (cie->augmentation[0] == 'z')
802 REQUIRE (read_uleb128 (&buf, end, &length));
803 else
804 length = 0;
805
806 /* Of the supported augmentation characters above, only 'L'
807 adds augmentation data to the FDE. This code would need to
808 be adjusted if any future augmentations do the same thing. */
809 if (cie->lsda_encoding != DW_EH_PE_omit0xff)
810 {
811 this_inf->lsda_offset = buf - start;
812 /* If there's no 'z' augmentation, we don't know where the
813 CFA insns begin. Assume no padding. */
814 if (cie->augmentation[0] != 'z')
815 length = end - buf;
816 }
817
818 /* Skip over the augmentation data. */
819 REQUIRE (skip_bytes (&buf, end, length));
820 insns = buf;
821
822 buf = last_fde + 4 + hdr_length;
823 SKIP_RELOCS (buf)while (cookie->rel < cookie->relend && (cookie
->rel->r_offset < (bfd_size_type) ((buf) - ehbuf))) cookie
->rel++
;
824 }
825
826 /* Try to interpret the CFA instructions and find the first
827 padding nop. Shrink this_inf's size so that it doesn't
828 include the padding. */
829 length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
830 set_loc_count = 0;
831 insns_end = skip_non_nops (insns, end, length, &set_loc_count);
832 /* If we don't understand the CFA instructions, we can't know
833 what needs to be adjusted there. */
834 if (insns_end == NULL((void*)0)
835 /* For the time being we don't support DW_CFA_set_loc in
836 CIE instructions. */
837 || (set_loc_count && this_inf->cie))
838 goto free_no_table;
839 this_inf->size -= end - insns_end;
840 if (insns_end != end && this_inf->cie)
841 {
842 cie->initial_insn_length -= end - insns_end;
843 cie->length -= end - insns_end;
844 }
845 if (set_loc_count
846 && ((cie->fde_encoding & 0xf0) == DW_EH_PE_pcrel0x10
847 || cie->make_relative))
848 {
849 unsigned int cnt;
850 bfd_byte *p;
851
852 this_inf->set_loc = bfd_malloc ((set_loc_count + 1)
853 * sizeof (unsigned int));
854 REQUIRE (this_inf->set_loc);
855 this_inf->set_loc[0] = set_loc_count;
856 p = insns;
857 cnt = 0;
858 while (p < end)
859 {
860 if (*p == DW_CFA_set_loc)
861 this_inf->set_loc[++cnt] = p + 1 - start;
862 REQUIRE (skip_cfa_op (&p, end, length));
863 }
864 }
865
866 this_inf->fde_encoding = cie->fde_encoding;
867 this_inf->lsda_encoding = cie->lsda_encoding;
868 sec_info->count++;
869 }
870
871 elf_section_data (sec)((struct bfd_elf_section_data*)(sec)->used_by_bfd)->sec_info = sec_info;
872 sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME3;
873
874 /* Look at all CIEs in this section and determine which can be
875 removed as unused, which can be merged with previous duplicate
876 CIEs and which need to be kept. */
877 for (ecie = ecies; ecie < ecies + ecie_count; ++ecie)
25
Loop condition is false. Execution continues on line 914
878 {
879 if (ecie->usage_count == 0)
880 {
881 sec_info->entry[ecie->entry].removed = 1;
882 continue;
883 }
884 ecie->cie.output_sec = sec->output_section;
885 ecie->cie.cie_inf = sec_info->entry + ecie->entry;
886 cie_compute_hash (&ecie->cie);
887 if (hdr_info->cies != NULL((void*)0))
888 {
889 void **loc = htab_find_slot_with_hash (hdr_info->cies, &ecie->cie,
890 ecie->cie.hash, INSERT);
891 if (loc != NULL((void*)0))
892 {
893 if (*loc != HTAB_EMPTY_ENTRY((void *) 0))
894 {
895 sec_info->entry[ecie->entry].removed = 1;
896 ecie->cie.cie_inf = ((struct cie *) *loc)->cie_inf;
897 continue;
898 }
899
900 *loc = malloc (sizeof (struct cie));
901 if (*loc == NULL((void*)0))
902 *loc = HTAB_DELETED_ENTRY((void *) 1);
903 else
904 memcpy (*loc, &ecie->cie, sizeof (struct cie));
905 }
906 }
907 ecie->cie.cie_inf->make_relative = ecie->cie.make_relative;
908 ecie->cie.cie_inf->make_lsda_relative = ecie->cie.make_lsda_relative;
909 ecie->cie.cie_inf->per_encoding_relative
910 = (ecie->cie.per_encoding & 0x70) == DW_EH_PE_pcrel0x10;
911 }
912
913 /* Ok, now we can assign new offsets. */
914 offset = 0;
915 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
26
Assuming the condition is true
27
Loop condition is true. Entering loop body
916 if (!ent->removed)
28
Assuming field 'removed' is 0
29
Taking true branch
917 {
918 if (!ent->cie)
30
Assuming field 'cie' is 0
31
Taking true branch
919 {
920 ecie = ecies + (unsigned long) ent->cie_inf;
32
Null pointer value stored to 'ecie'
921 ent->cie_inf = ecie->cie.cie_inf;
33
Dereference of null pointer
922 }
923 ent->new_offset = offset;
924 offset += size_of_output_cie_fde (ent, ptr_size);
925 }
926
927 /* Resize the sec as needed. */
928 sec->rawsize = sec->size;
929 sec->size = offset;
930 if (sec->size == 0)
931 sec->flags |= SEC_EXCLUDE0x8000;
932
933 free (ehbuf);
934 if (ecies)
935 free (ecies);
936 return offset != sec->rawsize;
937
938free_no_table:
939 if (ehbuf)
940 free (ehbuf);
941 if (sec_info)
942 free (sec_info);
943 if (ecies)
944 free (ecies);
945 hdr_info->table = FALSE0;
946 return FALSE0;
947
948#undef REQUIRE
949}
950
951/* This function is called for .eh_frame_hdr section after
952 _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
953 input sections. It finalizes the size of .eh_frame_hdr section. */
954
955bfd_boolean
956_bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
957{
958 struct elf_link_hash_table *htab;
959 struct eh_frame_hdr_info *hdr_info;
960 asection *sec;
961
962 htab = elf_hash_table (info)((struct elf_link_hash_table *) ((info)->hash));
963 hdr_info = &htab->eh_info;
964
965 if (hdr_info->cies != NULL((void*)0))
966 {
967 htab_delete (hdr_info->cies);
968 hdr_info->cies = NULL((void*)0);
969 }
970
971 sec = hdr_info->hdr_sec;
972 if (sec == NULL((void*)0))
973 return FALSE0;
974
975 sec->size = EH_FRAME_HDR_SIZE8;
976 if (hdr_info->table)
977 sec->size += 4 + hdr_info->fde_count * 8;
978
979 /* Request program headers to be recalculated. */
980 elf_tdata (abfd)((abfd) -> tdata.elf_obj_data)->program_header_size = 0;
981 elf_tdata (abfd)((abfd) -> tdata.elf_obj_data)->eh_frame_hdr = sec;
982 return TRUE1;
983}
984
985/* This function is called from size_dynamic_sections.
986 It needs to decide whether .eh_frame_hdr should be output or not,
987 because when the dynamic symbol table has been sized it is too late
988 to strip sections. */
989
990bfd_boolean
991_bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
992{
993 asection *o;
994 bfd *abfd;
995 struct elf_link_hash_table *htab;
996 struct eh_frame_hdr_info *hdr_info;
997
998 htab = elf_hash_table (info)((struct elf_link_hash_table *) ((info)->hash));
999 hdr_info = &htab->eh_info;
1000 if (hdr_info->hdr_sec == NULL((void*)0))
1001 return TRUE1;
1002
1003 if (bfd_is_abs_section (hdr_info->hdr_sec->output_section)((hdr_info->hdr_sec->output_section) == ((asection *) &
bfd_abs_section))
)
1004 {
1005 hdr_info->hdr_sec = NULL((void*)0);
1006 return TRUE1;
1007 }
1008
1009 abfd = NULL((void*)0);
1010 if (info->eh_frame_hdr)
1011 for (abfd = info->input_bfds; abfd != NULL((void*)0); abfd = abfd->link_next)
1012 {
1013 /* Count only sections which have at least a single CIE or FDE.
1014 There cannot be any CIE or FDE <= 8 bytes. */
1015 o = bfd_get_section_by_name (abfd, ".eh_frame");
1016 if (o && o->size > 8 && !bfd_is_abs_section (o->output_section)((o->output_section) == ((asection *) &bfd_abs_section
))
)
1017 break;
1018 }
1019
1020 if (abfd == NULL((void*)0))
1021 {
1022 hdr_info->hdr_sec->flags |= SEC_EXCLUDE0x8000;
1023 hdr_info->hdr_sec = NULL((void*)0);
1024 return TRUE1;
1025 }
1026
1027 hdr_info->table = TRUE1;
1028 return TRUE1;
1029}
1030
1031/* Adjust an address in the .eh_frame section. Given OFFSET within
1032 SEC, this returns the new offset in the adjusted .eh_frame section,
1033 or -1 if the address refers to a CIE/FDE which has been removed
1034 or to offset with dynamic relocation which is no longer needed. */
1035
1036bfd_vma
1037_bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1038 struct bfd_link_info *info,
1039 asection *sec,
1040 bfd_vma offset)
1041{
1042 struct eh_frame_sec_info *sec_info;
1043 struct elf_link_hash_table *htab;
1044 struct eh_frame_hdr_info *hdr_info;
1045 unsigned int lo, hi, mid;
1046
1047 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME3)
1048 return offset;
1049 sec_info = elf_section_data (sec)((struct bfd_elf_section_data*)(sec)->used_by_bfd)->sec_info;
1050
1051 if (offset >= sec->rawsize)
1052 return offset - sec->rawsize + sec->size;
1053
1054 htab = elf_hash_table (info)((struct elf_link_hash_table *) ((info)->hash));
1055 hdr_info = &htab->eh_info;
1056 if (hdr_info->offsets_adjusted)
1057 offset += sec->output_offset;
1058
1059 lo = 0;
1060 hi = sec_info->count;
1061 mid = 0;
1062 while (lo < hi)
1063 {
1064 mid = (lo + hi) / 2;
1065 if (offset < sec_info->entry[mid].offset)
1066 hi = mid;
1067 else if (offset
1068 >= sec_info->entry[mid].offset + sec_info->entry[mid].size)
1069 lo = mid + 1;
1070 else
1071 break;
1072 }
1073
1074 BFD_ASSERT (lo < hi)do { if (!(lo < hi)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1074); } while (0)
;
1075
1076 /* FDE or CIE was removed. */
1077 if (sec_info->entry[mid].removed)
1078 return (bfd_vma) -1;
1079
1080 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1081 relocation against FDE's initial_location field. */
1082 if (!sec_info->entry[mid].cie
1083 && sec_info->entry[mid].cie_inf->make_relative
1084 && offset == sec_info->entry[mid].offset + 8)
1085 return (bfd_vma) -2;
1086
1087 /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need
1088 for run-time relocation against LSDA field. */
1089 if (!sec_info->entry[mid].cie
1090 && sec_info->entry[mid].cie_inf->make_lsda_relative
1091 && (offset == (sec_info->entry[mid].offset + 8
1092 + sec_info->entry[mid].lsda_offset))
1093 && (sec_info->entry[mid].cie_inf->need_lsda_relative
1094 || !hdr_info->offsets_adjusted))
1095 {
1096 sec_info->entry[mid].cie_inf->need_lsda_relative = 1;
1097 return (bfd_vma) -2;
1098 }
1099
1100 /* If converting to DW_EH_PE_pcrel, there will be no need for run-time
1101 relocation against DW_CFA_set_loc's arguments. */
1102 if (sec_info->entry[mid].set_loc
1103 && (sec_info->entry[mid].cie
1104 ? sec_info->entry[mid].make_relative
1105 : sec_info->entry[mid].cie_inf->make_relative)
1106 && (offset >= sec_info->entry[mid].offset + 8
1107 + sec_info->entry[mid].set_loc[1]))
1108 {
1109 unsigned int cnt;
1110
1111 for (cnt = 1; cnt <= sec_info->entry[mid].set_loc[0]; cnt++)
1112 if (offset == sec_info->entry[mid].offset + 8
1113 + sec_info->entry[mid].set_loc[cnt])
1114 return (bfd_vma) -2;
1115 }
1116
1117 if (hdr_info->offsets_adjusted)
1118 offset -= sec->output_offset;
1119 /* Any new augmentation bytes go before the first relocation. */
1120 return (offset + sec_info->entry[mid].new_offset
1121 - sec_info->entry[mid].offset
1122 + extra_augmentation_string_bytes (sec_info->entry + mid)
1123 + extra_augmentation_data_bytes (sec_info->entry + mid));
1124}
1125
1126/* Write out .eh_frame section. This is called with the relocated
1127 contents. */
1128
1129bfd_boolean
1130_bfd_elf_write_section_eh_frame (bfd *abfd,
1131 struct bfd_link_info *info,
1132 asection *sec,
1133 bfd_byte *contents)
1134{
1135 struct eh_frame_sec_info *sec_info;
1136 struct elf_link_hash_table *htab;
1137 struct eh_frame_hdr_info *hdr_info;
1138 unsigned int ptr_size;
1139 struct eh_cie_fde *ent;
1140
1141 if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME3)
1142 return bfd_set_section_contents (abfd, sec->output_section, contents,
1143 sec->output_offset, sec->size);
1144
1145 ptr_size = (get_elf_backend_data (abfd)((const struct elf_backend_data *) (abfd)->xvec->backend_data
)
1146 ->elf_backend_eh_frame_address_size (abfd, sec));
1147 BFD_ASSERT (ptr_size != 0)do { if (!(ptr_size != 0)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1147); } while (0)
;
1148
1149 sec_info = elf_section_data (sec)((struct bfd_elf_section_data*)(sec)->used_by_bfd)->sec_info;
1150 htab = elf_hash_table (info)((struct elf_link_hash_table *) ((info)->hash));
1151 hdr_info = &htab->eh_info;
1152
1153 /* First convert all offsets to output section offsets, so that a
1154 CIE offset is valid if the CIE is used by a FDE from some other
1155 section. This can happen when duplicate CIEs are deleted in
1156 _bfd_elf_discard_section_eh_frame. We do all sections here because
1157 this function might not be called on sections in the same order as
1158 _bfd_elf_discard_section_eh_frame. */
1159 if (!hdr_info->offsets_adjusted)
1160 {
1161 bfd *ibfd;
1162 asection *eh;
1163 struct eh_frame_sec_info *eh_inf;
1164
1165 for (ibfd = info->input_bfds; ibfd != NULL((void*)0); ibfd = ibfd->link_next)
1166 {
1167 if (bfd_get_flavour (ibfd)((ibfd)->xvec->flavour) != bfd_target_elf_flavour
1168 || (ibfd->flags & DYNAMIC0x40) != 0)
1169 continue;
1170
1171 eh = bfd_get_section_by_name (ibfd, ".eh_frame");
1172 if (eh == NULL((void*)0) || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME3)
1173 continue;
1174
1175 eh_inf = elf_section_data (eh)((struct bfd_elf_section_data*)(eh)->used_by_bfd)->sec_info;
1176 for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent)
1177 {
1178 ent->offset += eh->output_offset;
1179 ent->new_offset += eh->output_offset;
1180 }
1181 }
1182 hdr_info->offsets_adjusted = TRUE1;
1183 }
1184
1185 if (hdr_info->table && hdr_info->array == NULL((void*)0))
1186 hdr_info->array
1187 = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array));
1188 if (hdr_info->array == NULL((void*)0))
1189 hdr_info = NULL((void*)0);
1190
1191 /* The new offsets can be bigger or smaller than the original offsets.
1192 We therefore need to make two passes over the section: one backward
1193 pass to move entries up and one forward pass to move entries down.
1194 The two passes won't interfere with each other because entries are
1195 not reordered */
1196 for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;)
1197 if (!ent->removed && ent->new_offset > ent->offset)
1198 memmove (contents + ent->new_offset - sec->output_offset,
1199 contents + ent->offset - sec->output_offset, ent->size);
1200
1201 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1202 if (!ent->removed && ent->new_offset < ent->offset)
1203 memmove (contents + ent->new_offset - sec->output_offset,
1204 contents + ent->offset - sec->output_offset, ent->size);
1205
1206 for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
1207 {
1208 unsigned char *buf, *end;
1209 unsigned int new_size;
1210
1211 if (ent->removed)
1212 continue;
1213
1214 if (ent->size == 4)
1215 {
1216 /* Any terminating FDE must be at the end of the section. */
1217 BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1)do { if (!(ent == sec_info->entry + sec_info->count - 1
)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1217); } while (0)
;
1218 continue;
1219 }
1220
1221 buf = contents + ent->new_offset - sec->output_offset;
1222 end = buf + ent->size;
1223 new_size = size_of_output_cie_fde (ent, ptr_size);
1224
1225 /* Update the size. It may be shrinked. */
1226 bfd_put_32 (abfd, new_size - 4, buf)((*((abfd)->xvec->bfd_putx32)) ((new_size - 4),(buf)));
1227
1228 /* Filling the extra bytes with DW_CFA_nops. */
1229 if (new_size != ent->size)
1230 memset (end, 0, new_size - ent->size);
1231
1232 if (ent->cie)
1233 {
1234 /* CIE */
1235 if (ent->make_relative
1236 || ent->need_lsda_relative
1237 || ent->per_encoding_relative)
1238 {
1239 char *aug;
1240 unsigned int action, extra_string, extra_data;
1241 unsigned int per_width, per_encoding;
1242
1243 /* Need to find 'R' or 'L' augmentation's argument and modify
1244 DW_EH_PE_* value. */
1245 action = ((ent->make_relative ? 1 : 0)
1246 | (ent->need_lsda_relative ? 2 : 0)
1247 | (ent->per_encoding_relative ? 4 : 0));
1248 extra_string = extra_augmentation_string_bytes (ent);
1249 extra_data = extra_augmentation_data_bytes (ent);
1250
1251 /* Skip length, id and version. */
1252 buf += 9;
1253 aug = (char *) buf;
1254 buf += strlen (aug) + 1;
1255 skip_leb128 (&buf, end);
1256 skip_leb128 (&buf, end);
1257 skip_leb128 (&buf, end);
1258 if (*aug == 'z')
1259 {
1260 /* The uleb128 will always be a single byte for the kind
1261 of augmentation strings that we're prepared to handle. */
1262 *buf++ += extra_data;
1263 aug++;
1264 }
1265
1266 /* Make room for the new augmentation string and data bytes. */
1267 memmove (buf + extra_string + extra_data, buf, end - buf);
1268 memmove (aug + extra_string, aug, buf - (bfd_byte *) aug);
1269 buf += extra_string;
1270 end += extra_string + extra_data;
1271
1272 if (ent->add_augmentation_size)
1273 {
1274 *aug++ = 'z';
1275 *buf++ = extra_data - 1;
1276 }
1277 if (ent->add_fde_encoding)
1278 {
1279 BFD_ASSERT (action & 1)do { if (!(action & 1)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1279); } while (0)
;
1280 *aug++ = 'R';
1281 *buf++ = DW_EH_PE_pcrel0x10;
1282 action &= ~1;
1283 }
1284
1285 while (action)
1286 switch (*aug++)
1287 {
1288 case 'L':
1289 if (action & 2)
1290 {
1291 BFD_ASSERT (*buf == ent->lsda_encoding)do { if (!(*buf == ent->lsda_encoding)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1291); } while (0)
;
1292 *buf |= DW_EH_PE_pcrel0x10;
1293 action &= ~2;
1294 }
1295 buf++;
1296 break;
1297 case 'P':
1298 per_encoding = *buf++;
1299 per_width = get_DW_EH_PE_width (per_encoding, ptr_size);
1300 BFD_ASSERT (per_width != 0)do { if (!(per_width != 0)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1300); } while (0)
;
1301 BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel)do { if (!(((per_encoding & 0x70) == 0x10) == ent->per_encoding_relative
)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1302); } while (0)
1302 == ent->per_encoding_relative)do { if (!(((per_encoding & 0x70) == 0x10) == ent->per_encoding_relative
)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1302); } while (0)
;
1303 if ((per_encoding & 0xf0) == DW_EH_PE_aligned0x50)
1304 buf = (contents
1305 + ((buf - contents + per_width - 1)
1306 & ~((bfd_size_type) per_width - 1)));
1307 if (action & 4)
1308 {
1309 bfd_vma val;
1310
1311 val = read_value (abfd, buf, per_width,
1312 get_DW_EH_PE_signed (per_encoding)(((per_encoding) & 0x08) != 0));
1313 val += ent->offset - ent->new_offset;
1314 val -= extra_string + extra_data;
1315 write_value (abfd, buf, val, per_width);
1316 action &= ~4;
1317 }
1318 buf += per_width;
1319 break;
1320 case 'R':
1321 if (action & 1)
1322 {
1323 BFD_ASSERT (*buf == ent->fde_encoding)do { if (!(*buf == ent->fde_encoding)) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1323); } while (0)
;
1324 *buf |= DW_EH_PE_pcrel0x10;
1325 action &= ~1;
1326 }
1327 buf++;
1328 break;
1329 case 'S':
1330 break;
1331 default:
1332 BFD_FAIL ()do { bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1332); } while (0)
;
1333 }
1334 }
1335 }
1336 else
1337 {
1338 /* FDE */
1339 bfd_vma value, address;
1340 unsigned int width;
1341 bfd_byte *start;
1342
1343 /* Skip length. */
1344 buf += 4;
1345 value = ent->new_offset + 4 - ent->cie_inf->new_offset;
1346 bfd_put_32 (abfd, value, buf)((*((abfd)->xvec->bfd_putx32)) ((value),(buf)));
1347 buf += 4;
1348 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1349 value = read_value (abfd, buf, width,
1350 get_DW_EH_PE_signed (ent->fde_encoding)(((ent->fde_encoding) & 0x08) != 0));
1351 address = value;
1352 if (value)
1353 {
1354 switch (ent->fde_encoding & 0xf0)
1355 {
1356 case DW_EH_PE_indirect0x80:
1357 case DW_EH_PE_textrel0x20:
1358 BFD_ASSERT (hdr_info == NULL)do { if (!(hdr_info == ((void*)0))) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1358); } while (0)
;
1359 break;
1360 case DW_EH_PE_datarel0x30:
1361 {
1362 asection *got = bfd_get_section_by_name (abfd, ".got");
1363
1364 BFD_ASSERT (got != NULL)do { if (!(got != ((void*)0))) bfd_assert("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
,1364); } while (0)
;
1365 address += got->vma;
1366 }
1367 break;
1368 case DW_EH_PE_pcrel0x10:
1369 value += ent->offset - ent->new_offset;
1370 address += sec->output_section->vma + ent->offset + 8;
1371 break;
1372 }
1373 if (ent->cie_inf->make_relative)
1374 value -= sec->output_section->vma + ent->new_offset + 8;
1375 write_value (abfd, buf, value, width);
1376 }
1377
1378 start = buf;
1379
1380 if (hdr_info)
1381 {
1382 hdr_info->array[hdr_info->array_count].initial_loc = address;
1383 hdr_info->array[hdr_info->array_count++].fde
1384 = sec->output_section->vma + ent->new_offset;
1385 }
1386
1387 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel0x10
1388 || ent->cie_inf->need_lsda_relative)
1389 {
1390 buf += ent->lsda_offset;
1391 width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size);
1392 value = read_value (abfd, buf, width,
1393 get_DW_EH_PE_signed (ent->lsda_encoding)(((ent->lsda_encoding) & 0x08) != 0));
1394 if (value)
1395 {
1396 if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel0x10)
1397 value += ent->offset - ent->new_offset;
1398 else if (ent->cie_inf->need_lsda_relative)
1399 value -= (sec->output_section->vma + ent->new_offset + 8
1400 + ent->lsda_offset);
1401 write_value (abfd, buf, value, width);
1402 }
1403 }
1404 else if (ent->cie_inf->add_augmentation_size)
1405 {
1406 /* Skip the PC and length and insert a zero byte for the
1407 augmentation size. */
1408 buf += width * 2;
1409 memmove (buf + 1, buf, end - buf);
1410 *buf = 0;
1411 }
1412
1413 if (ent->set_loc)
1414 {
1415 /* Adjust DW_CFA_set_loc. */
1416 unsigned int cnt, width;
1417 bfd_vma new_offset;
1418
1419 width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
1420 new_offset = ent->new_offset + 8
1421 + extra_augmentation_string_bytes (ent)
1422 + extra_augmentation_data_bytes (ent);
1423
1424 for (cnt = 1; cnt <= ent->set_loc[0]; cnt++)
1425 {
1426 bfd_vma value;
1427 buf = start + ent->set_loc[cnt];
1428
1429 value = read_value (abfd, buf, width,
1430 get_DW_EH_PE_signed (ent->fde_encoding)(((ent->fde_encoding) & 0x08) != 0));
1431 if (!value)
1432 continue;
1433
1434 if ((ent->fde_encoding & 0xf0) == DW_EH_PE_pcrel0x10)
1435 value += ent->offset + 8 - new_offset;
1436 if (ent->cie_inf->make_relative)
1437 value -= sec->output_section->vma + new_offset
1438 + ent->set_loc[cnt];
1439 write_value (abfd, buf, value, width);
1440 }
1441 }
1442 }
1443 }
1444
1445 /* We don't align the section to its section alignment since the
1446 runtime library only expects all CIE/FDE records aligned at
1447 the pointer size. _bfd_elf_discard_section_eh_frame should
1448 have padded CIE/FDE records to multiple of pointer size with
1449 size_of_output_cie_fde. */
1450 if ((sec->size % ptr_size) != 0)
1451 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils-2.17/bfd/elf-eh-frame.c"
, 1451, __PRETTY_FUNCTION__)
;
1452
1453 return bfd_set_section_contents (abfd, sec->output_section,
1454 contents, (file_ptr) sec->output_offset,
1455 sec->size);
1456}
1457
1458/* Helper function used to sort .eh_frame_hdr search table by increasing
1459 VMA of FDE initial location. */
1460
1461static int
1462vma_compare (const void *a, const void *b)
1463{
1464 const struct eh_frame_array_ent *p = a;
1465 const struct eh_frame_array_ent *q = b;
1466 if (p->initial_loc > q->initial_loc)
1467 return 1;
1468 if (p->initial_loc < q->initial_loc)
1469 return -1;
1470 return 0;
1471}
1472
1473/* Write out .eh_frame_hdr section. This must be called after
1474 _bfd_elf_write_section_eh_frame has been called on all input
1475 .eh_frame sections.
1476 .eh_frame_hdr format:
1477 ubyte version (currently 1)
1478 ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of
1479 .eh_frame section)
1480 ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count
1481 number (or DW_EH_PE_omit if there is no
1482 binary search table computed))
1483 ubyte table_enc (DW_EH_PE_* encoding of binary search table,
1484 or DW_EH_PE_omit if not present.
1485 DW_EH_PE_datarel is using address of
1486 .eh_frame_hdr section start as base)
1487 [encoded] eh_frame_ptr (pointer to start of .eh_frame section)
1488 optionally followed by:
1489 [encoded] fde_count (total number of FDEs in .eh_frame section)
1490 fde_count x [encoded] initial_loc, fde
1491 (array of encoded pairs containing
1492 FDE initial_location field and FDE address,
1493 sorted by increasing initial_loc). */
1494
1495bfd_boolean
1496_bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
1497{
1498 struct elf_link_hash_table *htab;
1499 struct eh_frame_hdr_info *hdr_info;
1500 asection *sec;
1501 bfd_byte *contents;
1502 asection *eh_frame_sec;
1503 bfd_size_type size;
1504 bfd_boolean retval;
1505 bfd_vma encoded_eh_frame;
1506
1507 htab = elf_hash_table (info)((struct elf_link_hash_table *) ((info)->hash));
1508 hdr_info = &htab->eh_info;
1509 sec = hdr_info->hdr_sec;
1510 if (sec == NULL((void*)0))
1511 return TRUE1;
1512
1513 size = EH_FRAME_HDR_SIZE8;
1514 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1515 size += 4 + hdr_info->fde_count * 8;
1516 contents = bfd_malloc (size);
1517 if (contents == NULL((void*)0))
1518 return FALSE0;
1519
1520 eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame");
1521 if (eh_frame_sec == NULL((void*)0))
1522 {
1523 free (contents);
1524 return FALSE0;
1525 }
1526
1527 memset (contents, 0, EH_FRAME_HDR_SIZE8);
1528 contents[0] = 1; /* Version. */
1529 contents[1] = get_elf_backend_data (abfd)((const struct elf_backend_data *) (abfd)->xvec->backend_data
)
->elf_backend_encode_eh_address
1530 (abfd, info, eh_frame_sec, 0, sec, 4,
1531 &encoded_eh_frame); /* .eh_frame offset. */
1532
1533 if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count)
1534 {
1535 contents[2] = DW_EH_PE_udata40x03; /* FDE count encoding. */
1536 contents[3] = DW_EH_PE_datarel0x30 | DW_EH_PE_sdata40x0B; /* Search table enc. */
1537 }
1538 else
1539 {
1540 contents[2] = DW_EH_PE_omit0xff;
1541 contents[3] = DW_EH_PE_omit0xff;
1542 }
1543 bfd_put_32 (abfd, encoded_eh_frame, contents + 4)((*((abfd)->xvec->bfd_putx32)) ((encoded_eh_frame),(contents
+ 4)))
;
1544
1545 if (contents[2] != DW_EH_PE_omit0xff)
1546 {
1547 unsigned int i;
1548
1549 bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE)((*((abfd)->xvec->bfd_putx32)) ((hdr_info->fde_count
),(contents + 8)))
;
1550 qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array),
1551 vma_compare);
1552 for (i = 0; i < hdr_info->fde_count; i++)
1553 {
1554 bfd_put_32 (abfd,((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
initial_loc - sec->output_section->vma),(contents + 8 +
i * 8 + 4)))
1555 hdr_info->array[i].initial_loc((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
initial_loc - sec->output_section->vma),(contents + 8 +
i * 8 + 4)))
1556 - sec->output_section->vma,((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
initial_loc - sec->output_section->vma),(contents + 8 +
i * 8 + 4)))
1557 contents + EH_FRAME_HDR_SIZE + i * 8 + 4)((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
initial_loc - sec->output_section->vma),(contents + 8 +
i * 8 + 4)))
;
1558 bfd_put_32 (abfd,((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
fde - sec->output_section->vma),(contents + 8 + i * 8 +
8)))
1559 hdr_info->array[i].fde - sec->output_section->vma,((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
fde - sec->output_section->vma),(contents + 8 + i * 8 +
8)))
1560 contents + EH_FRAME_HDR_SIZE + i * 8 + 8)((*((abfd)->xvec->bfd_putx32)) ((hdr_info->array[i].
fde - sec->output_section->vma),(contents + 8 + i * 8 +
8)))
;
1561 }
1562 }
1563
1564 retval = bfd_set_section_contents (abfd, sec->output_section,
1565 contents, (file_ptr) sec->output_offset,
1566 sec->size);
1567 free (contents);
1568 return retval;
1569}
1570
1571/* Return the width of FDE addresses. This is the default implementation. */
1572
1573unsigned int
1574_bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED__attribute__ ((__unused__)))
1575{
1576 return elf_elfheader (abfd)(((abfd) -> tdata.elf_obj_data) -> elf_header)->e_ident[EI_CLASS4] == ELFCLASS642 ? 8 : 4;
1577}
1578
1579/* Decide whether we can use a PC-relative encoding within the given
1580 EH frame section. This is the default implementation. */
1581
1582bfd_boolean
1583_bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1584 struct bfd_link_info *info ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1585 asection *eh_frame_section ATTRIBUTE_UNUSED__attribute__ ((__unused__)))
1586{
1587 return TRUE1;
1588}
1589
1590/* Select an encoding for the given address. Preference is given to
1591 PC-relative addressing modes. */
1592
1593bfd_byte
1594_bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1595 struct bfd_link_info *info ATTRIBUTE_UNUSED__attribute__ ((__unused__)),
1596 asection *osec, bfd_vma offset,
1597 asection *loc_sec, bfd_vma loc_offset,
1598 bfd_vma *encoded)
1599{
1600 *encoded = osec->vma + offset -
1601 (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset);
1602 return DW_EH_PE_pcrel0x10 | DW_EH_PE_sdata40x0B;
1603}