| 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_SIZE 8 |
| 28 | |
| 29 | /* If *ITER hasn't reached END yet, read the next byte into *RESULT and |
| 30 | move onto the next byte. Return true on success. */ |
| 31 | |
| 32 | static inline bfd_boolean |
| 33 | read_byte (bfd_byte **iter, bfd_byte *end, unsigned char *result) |
| 34 | { |
| 35 | if (*iter >= end) |
| 36 | return FALSE; |
| 37 | *result = *((*iter)++); |
| 38 | return TRUE; |
| 39 | } |
| 40 | |
| 41 | /* Move *ITER over LENGTH bytes, or up to END, whichever is closer. |
| 42 | Return true it was possible to move LENGTH bytes. */ |
| 43 | |
| 44 | static inline bfd_boolean |
| 45 | skip_bytes (bfd_byte **iter, bfd_byte *end, bfd_size_type length) |
| 46 | { |
| 47 | if ((bfd_size_type) (end - *iter) < length) |
| 48 | { |
| 49 | *iter = end; |
| 50 | return FALSE; |
| 51 | } |
| 52 | *iter += length; |
| 53 | return TRUE; |
| 54 | } |
| 55 | |
| 56 | /* Move *ITER over an leb128, stopping at END. Return true if the end |
| 57 | of the leb128 was found. */ |
| 58 | |
| 59 | static bfd_boolean |
| 60 | skip_leb128 (bfd_byte **iter, bfd_byte *end) |
| 61 | { |
| 62 | unsigned char byte; |
| 63 | do |
| 64 | if (!read_byte (iter, end, &byte)) |
| 65 | return FALSE; |
| 66 | while (byte & 0x80); |
| 67 | return TRUE; |
| 68 | } |
| 69 | |
| 70 | /* Like skip_leb128, but treat the leb128 as an unsigned value and |
| 71 | store it in *VALUE. */ |
| 72 | |
| 73 | static bfd_boolean |
| 74 | read_uleb128 (bfd_byte **iter, bfd_byte *end, bfd_vma *value) |
| 75 | { |
| 76 | bfd_byte *start, *p; |
| 77 | |
| 78 | start = *iter; |
| 79 | if (!skip_leb128 (iter, end)) |
| 80 | return FALSE; |
| 81 | |
| 82 | p = *iter; |
| 83 | *value = *--p; |
| 84 | while (p > start) |
| 85 | *value = (*value << 7) | (*--p & 0x7f); |
| 86 | |
| 87 | return TRUE; |
| 88 | } |
| 89 | |
| 90 | /* Like read_uleb128, but for signed values. */ |
| 91 | |
| 92 | static bfd_boolean |
| 93 | read_sleb128 (bfd_byte **iter, bfd_byte *end, bfd_signed_vma *value) |
| 94 | { |
| 95 | bfd_byte *start, *p; |
| 96 | |
| 97 | start = *iter; |
| 98 | if (!skip_leb128 (iter, end)) |
| 99 | return FALSE; |
| 100 | |
| 101 | p = *iter; |
| 102 | *value = ((*--p & 0x7f) ^ 0x40) - 0x40; |
| 103 | while (p > start) |
| 104 | *value = (*value << 7) | (*--p & 0x7f); |
| 105 | |
| 106 | return TRUE; |
| 107 | } |
| 108 | |
| 109 | /* Return 0 if either encoding is variable width, or not yet known to bfd. */ |
| 110 | |
| 111 | static |
| 112 | int get_DW_EH_PE_width (int encoding, int ptr_size) |
| 113 | { |
| 114 | /* DW_EH_PE_ values of 0x60 and 0x70 weren't defined at the time .eh_frame |
| 115 | was added to bfd. */ |
| 116 | if ((encoding & 0x60) == 0x60) |
| 117 | return 0; |
| 118 | |
| 119 | switch (encoding & 7) |
| 120 | { |
| 121 | case DW_EH_PE_udata2: return 2; |
| 122 | case DW_EH_PE_udata4: return 4; |
| 123 | case DW_EH_PE_udata8: return 8; |
| 124 | case DW_EH_PE_absptr: return ptr_size; |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | #define get_DW_EH_PE_signed(encoding) (((encoding) & DW_EH_PE_signed) != 0) |
| 133 | |
| 134 | /* Read a width sized value from memory. */ |
| 135 | |
| 136 | static bfd_vma |
| 137 | read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed) |
| 138 | { |
| 139 | bfd_vma value; |
| 140 | |
| 141 | switch (width) |
| 142 | { |
| 143 | case 2: |
| 144 | if (is_signed) |
| 145 | value = bfd_get_signed_16 (abfd, buf); |
| 146 | else |
| 147 | value = bfd_get_16 (abfd, buf); |
| 148 | break; |
| 149 | case 4: |
| 150 | if (is_signed) |
| 151 | value = bfd_get_signed_32 (abfd, buf); |
| 152 | else |
| 153 | value = bfd_get_32 (abfd, buf); |
| 154 | break; |
| 155 | case 8: |
| 156 | if (is_signed) |
| 157 | value = bfd_get_signed_64 (abfd, buf); |
| 158 | else |
| 159 | value = bfd_get_64 (abfd, buf); |
| 160 | break; |
| 161 | default: |
| 162 | BFD_FAIL (); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | return value; |
| 167 | } |
| 168 | |
| 169 | /* Store a width sized value to memory. */ |
| 170 | |
| 171 | static void |
| 172 | write_value (bfd *abfd, bfd_byte *buf, bfd_vma value, int width) |
| 173 | { |
| 174 | switch (width) |
| 175 | { |
| 176 | case 2: bfd_put_16 (abfd, value, buf); break; |
| 177 | case 4: bfd_put_32 (abfd, value, buf); break; |
| 178 | case 8: bfd_put_64 (abfd, value, buf); break; |
| 179 | default: BFD_FAIL (); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* Return zero if C1 and C2 CIEs can be merged. */ |
| 184 | |
| 185 | static |
| 186 | int cie_compare (struct cie *c1, struct cie *c2) |
| 187 | { |
| 188 | if (c1->hdr.length == c2->hdr.length |
| 189 | && c1->version == c2->version |
| 190 | && strcmp (c1->augmentation, c2->augmentation) == 0 |
| 191 | && strcmp (c1->augmentation, "eh") != 0 |
| 192 | && c1->code_align == c2->code_align |
| 193 | && c1->data_align == c2->data_align |
| 194 | && c1->ra_column == c2->ra_column |
| 195 | && c1->augmentation_size == c2->augmentation_size |
| 196 | && c1->personality == c2->personality |
| 197 | && c1->per_encoding == c2->per_encoding |
| 198 | && c1->lsda_encoding == c2->lsda_encoding |
| 199 | && c1->fde_encoding == c2->fde_encoding |
| 200 | && c1->initial_insn_length == c2->initial_insn_length |
| 201 | && memcmp (c1->initial_instructions, |
| 202 | c2->initial_instructions, |
| 203 | c1->initial_insn_length) == 0) |
| 204 | return 0; |
| 205 | |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | /* Return the number of extra bytes that we'll be inserting into |
| 210 | ENTRY's augmentation string. */ |
| 211 | |
| 212 | static INLINE unsigned int |
| 213 | extra_augmentation_string_bytes (struct eh_cie_fde *entry) |
| 214 | { |
| 215 | unsigned int size = 0; |
| 216 | if (entry->cie) |
| 217 | { |
| 218 | if (entry->add_augmentation_size) |
| 219 | size++; |
| 220 | if (entry->add_fde_encoding) |
| 221 | size++; |
| 222 | } |
| 223 | return size; |
| 224 | } |
| 225 | |
| 226 | /* Likewise ENTRY's augmentation data. */ |
| 227 | |
| 228 | static INLINE unsigned int |
| 229 | extra_augmentation_data_bytes (struct eh_cie_fde *entry) |
| 230 | { |
| 231 | unsigned int size = 0; |
| 232 | if (entry->cie) |
| 233 | { |
| 234 | if (entry->add_augmentation_size) |
| 235 | size++; |
| 236 | if (entry->add_fde_encoding) |
| 237 | size++; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | if (entry->cie_inf->add_augmentation_size) |
| 242 | size++; |
| 243 | } |
| 244 | return size; |
| 245 | } |
| 246 | |
| 247 | /* Return the size that ENTRY will have in the output. ALIGNMENT is the |
| 248 | required alignment of ENTRY in bytes. */ |
| 249 | |
| 250 | static unsigned int |
| 251 | size_of_output_cie_fde (struct eh_cie_fde *entry, unsigned int alignment) |
| 252 | { |
| 253 | if (entry->removed) |
| 254 | return 0; |
| 255 | if (entry->size == 4) |
| 256 | return 4; |
| 257 | return (entry->size |
| 258 | + extra_augmentation_string_bytes (entry) |
| 259 | + extra_augmentation_data_bytes (entry) |
| 260 | + alignment - 1) & -alignment; |
| 261 | } |
| 262 | |
| 263 | /* Assume that the bytes between *ITER and END are CFA instructions. |
| 264 | Try to move *ITER past the first instruction and return true on |
| 265 | success. ENCODED_PTR_WIDTH gives the width of pointer entries. */ |
| 266 | |
| 267 | static bfd_boolean |
| 268 | skip_cfa_op (bfd_byte **iter, bfd_byte *end, unsigned int encoded_ptr_width) |
| 269 | { |
| 270 | bfd_byte op; |
| 271 | bfd_vma length; |
| 272 | |
| 273 | if (!read_byte (iter, end, &op)) |
| 274 | return FALSE; |
| 275 | |
| 276 | switch (op & 0x80 ? op & 0xc0 : op) |
| 277 | { |
| 278 | case DW_CFA_nop: |
| 279 | case DW_CFA_advance_loc: |
| 280 | case DW_CFA_restore: |
| 281 | /* No arguments. */ |
| 282 | return TRUE; |
| 283 | |
| 284 | case DW_CFA_offset: |
| 285 | case DW_CFA_restore_extended: |
| 286 | case DW_CFA_undefined: |
| 287 | case DW_CFA_same_value: |
| 288 | case DW_CFA_def_cfa_register: |
| 289 | case DW_CFA_def_cfa_offset: |
| 290 | case DW_CFA_def_cfa_offset_sf: |
| 291 | case DW_CFA_GNU_args_size: |
| 292 | /* One leb128 argument. */ |
| 293 | return skip_leb128 (iter, end); |
| 294 | |
| 295 | case DW_CFA_offset_extended: |
| 296 | case DW_CFA_register: |
| 297 | case DW_CFA_def_cfa: |
| 298 | case DW_CFA_offset_extended_sf: |
| 299 | case DW_CFA_GNU_negative_offset_extended: |
| 300 | case DW_CFA_def_cfa_sf: |
| 301 | /* Two leb128 arguments. */ |
| 302 | return (skip_leb128 (iter, end) |
| 303 | && skip_leb128 (iter, end)); |
| 304 | |
| 305 | case DW_CFA_def_cfa_expression: |
| 306 | /* A variable-length argument. */ |
| 307 | return (read_uleb128 (iter, end, &length) |
| 308 | && skip_bytes (iter, end, length)); |
| 309 | |
| 310 | case DW_CFA_expression: |
| 311 | /* A leb128 followed by a variable-length argument. */ |
| 312 | return (skip_leb128 (iter, end) |
| 313 | && read_uleb128 (iter, end, &length) |
| 314 | && skip_bytes (iter, end, length)); |
| 315 | |
| 316 | case DW_CFA_set_loc: |
| 317 | return skip_bytes (iter, end, encoded_ptr_width); |
| 318 | |
| 319 | case DW_CFA_advance_loc1: |
| 320 | return skip_bytes (iter, end, 1); |
| 321 | |
| 322 | case DW_CFA_advance_loc2: |
| 323 | return skip_bytes (iter, end, 2); |
| 324 | |
| 325 | case DW_CFA_advance_loc4: |
| 326 | return skip_bytes (iter, end, 4); |
| 327 | |
| 328 | case DW_CFA_MIPS_advance_loc8: |
| 329 | return skip_bytes (iter, end, 8); |
| 330 | |
| 331 | default: |
| 332 | return FALSE; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* Try to interpret the bytes between BUF and END as CFA instructions. |
| 337 | If every byte makes sense, return a pointer to the first DW_CFA_nop |
| 338 | padding byte, or END if there is no padding. Return null otherwise. |
| 339 | ENCODED_PTR_WIDTH is as for skip_cfa_op. */ |
| 340 | |
| 341 | static bfd_byte * |
| 342 | skip_non_nops (bfd_byte *buf, bfd_byte *end, unsigned int encoded_ptr_width) |
| 343 | { |
| 344 | bfd_byte *last; |
| 345 | |
| 346 | last = buf; |
| 347 | while (buf < end) |
| 348 | if (*buf == DW_CFA_nop) |
| 349 | buf++; |
| 350 | else |
| 351 | { |
| 352 | if (!skip_cfa_op (&buf, end, encoded_ptr_width)) |
| 353 | return 0; |
| 354 | last = buf; |
| 355 | } |
| 356 | return last; |
| 357 | } |
| 358 | |
| 359 | /* This function is called for each input file before the .eh_frame |
| 360 | section is relocated. It discards duplicate CIEs and FDEs for discarded |
| 361 | functions. The function returns TRUE iff any entries have been |
| 362 | deleted. */ |
| 363 | |
| 364 | bfd_boolean |
| 365 | _bfd_elf_discard_section_eh_frame |
| 366 | (bfd *abfd, struct bfd_link_info *info, asection *sec, |
| 367 | bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *), |
| 368 | struct elf_reloc_cookie *cookie) |
| 369 | { |
| 370 | #define REQUIRE(COND) \ |
| 371 | do \ |
| 372 | if (!(COND)) \ |
| 373 | goto free_no_table; \ |
| 374 | while (0) |
| 375 | |
| 376 | bfd_byte *ehbuf = NULL, *buf; |
| 377 | bfd_byte *last_cie, *last_fde; |
| 378 | struct eh_cie_fde *ent, *last_cie_inf, *this_inf; |
| 379 | struct cie_header hdr; |
| 380 | struct cie cie; |
| 381 | struct elf_link_hash_table *htab; |
| 382 | struct eh_frame_hdr_info *hdr_info; |
| 383 | struct eh_frame_sec_info *sec_info = NULL; |
| 384 | unsigned int cie_usage_count, offset; |
| 385 | unsigned int ptr_size; |
| 386 | |
| 387 | if (sec->size == 0) |
| 388 | { |
| 389 | /* This file does not contain .eh_frame information. */ |
| 390 | return FALSE; |
| 391 | } |
| 392 | |
| 393 | if (bfd_is_abs_section (sec->output_section)) |
| 394 | { |
| 395 | /* At least one of the sections is being discarded from the |
| 396 | link, so we should just ignore them. */ |
| 397 | return FALSE; |
| 398 | } |
| 399 | |
| 400 | htab = elf_hash_table (info); |
| 401 | hdr_info = &htab->eh_info; |
| 402 | |
| 403 | /* Read the frame unwind information from abfd. */ |
| 404 | |
| 405 | REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf)); |
| 406 | |
| 407 | if (sec->size >= 4 |
| 408 | && bfd_get_32 (abfd, ehbuf) == 0 |
| 409 | && cookie->rel == cookie->relend) |
| 410 | { |
| 411 | /* Empty .eh_frame section. */ |
| 412 | free (ehbuf); |
| 413 | return FALSE; |
| 414 | } |
| 415 | |
| 416 | /* If .eh_frame section size doesn't fit into int, we cannot handle |
| 417 | it (it would need to use 64-bit .eh_frame format anyway). */ |
| 418 | REQUIRE (sec->size == (unsigned int) sec->size); |
| 419 | |
| 420 | ptr_size = (get_elf_backend_data (abfd) |
| 421 | ->elf_backend_eh_frame_address_size (abfd, sec)); |
| 422 | REQUIRE (ptr_size != 0); |
| 423 | |
| 424 | buf = ehbuf; |
| 425 | last_cie = NULL; |
| 426 | last_cie_inf = NULL; |
| 427 | memset (&cie, 0, sizeof (cie)); |
| 428 | cie_usage_count = 0; |
| 429 | sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info) |
| 430 | + 99 * sizeof (struct eh_cie_fde)); |
| 431 | REQUIRE (sec_info); |
| 432 | |
| 433 | sec_info->alloced = 100; |
| 434 | |
| 435 | #define ENSURE_NO_RELOCS(buf) \ |
| 436 | REQUIRE (!(cookie->rel < cookie->relend \ |
| 437 | && (cookie->rel->r_offset \ |
| 438 | < (bfd_size_type) ((buf) - ehbuf)) \ |
| 439 | && cookie->rel->r_info != 0)) |
| 440 | |
| 441 | #define SKIP_RELOCS(buf) \ |
| 442 | while (cookie->rel < cookie->relend \ |
| 443 | && (cookie->rel->r_offset \ |
| 444 | < (bfd_size_type) ((buf) - ehbuf))) \ |
| 445 | cookie->rel++ |
| 446 | |
| 447 | #define GET_RELOC(buf) \ |
| 448 | ((cookie->rel < cookie->relend \ |
| 449 | && (cookie->rel->r_offset \ |
| 450 | == (bfd_size_type) ((buf) - ehbuf))) \ |
| 451 | ? cookie->rel : NULL) |
| 452 | |
| 453 | for (;;) |
| 454 | { |
| 455 | char *aug; |
| 456 | bfd_byte *start, *end, *insns; |
| 457 | bfd_size_type length; |
| 458 | |
| 459 | if (sec_info->count == sec_info->alloced) |
| 460 | { |
| 461 | struct eh_cie_fde *old_entry = sec_info->entry; |
| 462 | sec_info = bfd_realloc (sec_info, |
| 463 | sizeof (struct eh_frame_sec_info) |
| 464 | + ((sec_info->alloced + 99) |
| 465 | * sizeof (struct eh_cie_fde))); |
| 466 | REQUIRE (sec_info); |
| 467 | |
| 468 | memset (&sec_info->entry[sec_info->alloced], 0, |
| 469 | 100 * sizeof (struct eh_cie_fde)); |
| 470 | sec_info->alloced += 100; |
| 471 | |
| 472 | /* Now fix any pointers into the array. */ |
| 473 | if (last_cie_inf >= old_entry |
| 474 | && last_cie_inf < old_entry + sec_info->count) |
| 475 | last_cie_inf = sec_info->entry + (last_cie_inf - old_entry); |
| 476 | } |
| 477 | |
| 478 | this_inf = sec_info->entry + sec_info->count; |
| 479 | last_fde = buf; |
| 480 | /* If we are at the end of the section, we still need to decide |
| 481 | on whether to output or discard last encountered CIE (if any). */ |
| 482 | if ((bfd_size_type) (buf - ehbuf) == sec->size) |
| 483 | { |
| 484 | hdr.length = 0; |
| 485 | hdr.id = (unsigned int) -1; |
| 486 | end = buf; |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | /* Read the length of the entry. */ |
| 491 | REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4)); |
| 492 | hdr.length = bfd_get_32 (abfd, buf - 4); |
| 493 | |
| 494 | /* 64-bit .eh_frame is not supported. */ |
| 495 | REQUIRE (hdr.length != 0xffffffff); |
| 496 | |
| 497 | /* The CIE/FDE must be fully contained in this input section. */ |
| 498 | REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr.length <= sec->size); |
| 499 | end = buf + hdr.length; |
| 500 | |
| 501 | this_inf->offset = last_fde - ehbuf; |
| 502 | this_inf->size = 4 + hdr.length; |
| 503 | |
| 504 | if (hdr.length == 0) |
| 505 | { |
| 506 | /* A zero-length CIE should only be found at the end of |
| 507 | the section. */ |
| 508 | REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size); |
| 509 | ENSURE_NO_RELOCS (buf); |
| 510 | sec_info->count++; |
| 511 | /* Now just finish last encountered CIE processing and break |
| 512 | the loop. */ |
| 513 | hdr.id = (unsigned int) -1; |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | REQUIRE (skip_bytes (&buf, end, 4)); |
| 518 | hdr.id = bfd_get_32 (abfd, buf - 4); |
| 519 | REQUIRE (hdr.id != (unsigned int) -1); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (hdr.id == 0 || hdr.id == (unsigned int) -1) |
| 524 | { |
| 525 | unsigned int initial_insn_length; |
| 526 | |
| 527 | /* CIE */ |
| 528 | if (last_cie != NULL) |
| 529 | { |
| 530 | /* Now check if this CIE is identical to the last CIE, |
| 531 | in which case we can remove it provided we adjust |
| 532 | all FDEs. Also, it can be removed if we have removed |
| 533 | all FDEs using it. */ |
| 534 | if ((!info->relocatable |
| 535 | && hdr_info->last_cie_sec |
| 536 | && (sec->output_section |
| 537 | == hdr_info->last_cie_sec->output_section) |
| 538 | && cie_compare (&cie, &hdr_info->last_cie) == 0) |
| 539 | || cie_usage_count == 0) |
| 540 | last_cie_inf->removed = 1; |
| 541 | else |
| 542 | { |
| 543 | hdr_info->last_cie = cie; |
| 544 | hdr_info->last_cie_sec = sec; |
| 545 | last_cie_inf->make_relative = cie.make_relative; |
| 546 | last_cie_inf->make_lsda_relative = cie.make_lsda_relative; |
| 547 | last_cie_inf->per_encoding_relative |
| 548 | = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (hdr.id == (unsigned int) -1) |
| 553 | break; |
| 554 | |
| 555 | last_cie_inf = this_inf; |
| 556 | this_inf->cie = 1; |
| 557 | |
| 558 | cie_usage_count = 0; |
| 559 | memset (&cie, 0, sizeof (cie)); |
| 560 | cie.hdr = hdr; |
| 561 | REQUIRE (read_byte (&buf, end, &cie.version)); |
| 562 | |
| 563 | /* Cannot handle unknown versions. */ |
| 564 | REQUIRE (cie.version == 1 || cie.version == 3); |
| 565 | REQUIRE (strlen ((char *) buf) < sizeof (cie.augmentation)); |
| 566 | |
| 567 | strcpy (cie.augmentation, (char *) buf); |
| 568 | buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1; |
| 569 | ENSURE_NO_RELOCS (buf); |
| 570 | if (buf[0] == 'e' && buf[1] == 'h') |
| 571 | { |
| 572 | /* GCC < 3.0 .eh_frame CIE */ |
| 573 | /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__ |
| 574 | is private to each CIE, so we don't need it for anything. |
| 575 | Just skip it. */ |
| 576 | REQUIRE (skip_bytes (&buf, end, ptr_size)); |
| 577 | SKIP_RELOCS (buf); |
| 578 | } |
| 579 | REQUIRE (read_uleb128 (&buf, end, &cie.code_align)); |
| 580 | REQUIRE (read_sleb128 (&buf, end, &cie.data_align)); |
| 581 | if (cie.version == 1) |
| 582 | { |
| 583 | REQUIRE (buf < end); |
| 584 | cie.ra_column = *buf++; |
| 585 | } |
| 586 | else |
| 587 | REQUIRE (read_uleb128 (&buf, end, &cie.ra_column)); |
| 588 | ENSURE_NO_RELOCS (buf); |
| 589 | cie.lsda_encoding = DW_EH_PE_omit; |
| 590 | cie.fde_encoding = DW_EH_PE_omit; |
| 591 | cie.per_encoding = DW_EH_PE_omit; |
| 592 | aug = cie.augmentation; |
| 593 | if (aug[0] != 'e' || aug[1] != 'h') |
| 594 | { |
| 595 | if (*aug == 'z') |
| 596 | { |
| 597 | aug++; |
| 598 | REQUIRE (read_uleb128 (&buf, end, &cie.augmentation_size)); |
| 599 | ENSURE_NO_RELOCS (buf); |
| 600 | } |
| 601 | |
| 602 | while (*aug != '\0') |
| 603 | switch (*aug++) |
| 604 | { |
| 605 | case 'L': |
| 606 | REQUIRE (read_byte (&buf, end, &cie.lsda_encoding)); |
| 607 | ENSURE_NO_RELOCS (buf); |
| 608 | REQUIRE (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size)); |
| 609 | break; |
| 610 | case 'R': |
| 611 | REQUIRE (read_byte (&buf, end, &cie.fde_encoding)); |
| 612 | ENSURE_NO_RELOCS (buf); |
| 613 | REQUIRE (get_DW_EH_PE_width (cie.fde_encoding, ptr_size)); |
| 614 | break; |
| 615 | case 'S': |
| 616 | break; |
| 617 | case 'P': |
| 618 | { |
| 619 | int per_width; |
| 620 | |
| 621 | REQUIRE (read_byte (&buf, end, &cie.per_encoding)); |
| 622 | per_width = get_DW_EH_PE_width (cie.per_encoding, |
| 623 | ptr_size); |
| 624 | REQUIRE (per_width); |
| 625 | if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned) |
| 626 | { |
| 627 | length = -(buf - ehbuf) & (per_width - 1); |
| 628 | REQUIRE (skip_bytes (&buf, end, length)); |
| 629 | } |
| 630 | ENSURE_NO_RELOCS (buf); |
| 631 | /* Ensure we have a reloc here, against |
| 632 | a global symbol. */ |
| 633 | if (GET_RELOC (buf) != NULL) |
| 634 | { |
| 635 | unsigned long r_symndx; |
| 636 | |
| 637 | #ifdef BFD64 |
| 638 | if (ptr_size == 8) |
| 639 | r_symndx = ELF64_R_SYM (cookie->rel->r_info); |
| 640 | else |
| 641 | #endif |
| 642 | r_symndx = ELF32_R_SYM (cookie->rel->r_info); |
| 643 | if (r_symndx >= cookie->locsymcount) |
| 644 | { |
| 645 | struct elf_link_hash_entry *h; |
| 646 | |
| 647 | r_symndx -= cookie->extsymoff; |
| 648 | h = cookie->sym_hashes[r_symndx]; |
| 649 | |
| 650 | while (h->root.type == bfd_link_hash_indirect |
| 651 | || h->root.type == bfd_link_hash_warning) |
| 652 | h = (struct elf_link_hash_entry *) |
| 653 | h->root.u.i.link; |
| 654 | |
| 655 | cie.personality = h; |
| 656 | } |
| 657 | /* Cope with MIPS-style composite relocations. */ |
| 658 | do |
| 659 | cookie->rel++; |
| 660 | while (GET_RELOC (buf) != NULL); |
| 661 | } |
| 662 | REQUIRE (skip_bytes (&buf, end, per_width)); |
| 663 | } |
| 664 | break; |
| 665 | default: |
| 666 | /* Unrecognized augmentation. Better bail out. */ |
| 667 | goto free_no_table; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /* For shared libraries, try to get rid of as many RELATIVE relocs |
| 672 | as possible. */ |
| 673 | if (info->shared |
| 674 | && (get_elf_backend_data (abfd) |
| 675 | ->elf_backend_can_make_relative_eh_frame |
| 676 | (abfd, info, sec))) |
| 677 | { |
| 678 | if ((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr) |
| 679 | cie.make_relative = 1; |
| 680 | /* If the CIE doesn't already have an 'R' entry, it's fairly |
| 681 | easy to add one, provided that there's no aligned data |
| 682 | after the augmentation string. */ |
| 683 | else if (cie.fde_encoding == DW_EH_PE_omit |
| 684 | && (cie.per_encoding & 0xf0) != DW_EH_PE_aligned) |
| 685 | { |
| 686 | if (*cie.augmentation == 0) |
| 687 | this_inf->add_augmentation_size = 1; |
| 688 | this_inf->add_fde_encoding = 1; |
| 689 | cie.make_relative = 1; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (info->shared |
| 694 | && (get_elf_backend_data (abfd) |
| 695 | ->elf_backend_can_make_lsda_relative_eh_frame |
| 696 | (abfd, info, sec)) |
| 697 | && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr) |
| 698 | cie.make_lsda_relative = 1; |
| 699 | |
| 700 | /* If FDE encoding was not specified, it defaults to |
| 701 | DW_EH_absptr. */ |
| 702 | if (cie.fde_encoding == DW_EH_PE_omit) |
| 703 | cie.fde_encoding = DW_EH_PE_absptr; |
| 704 | |
| 705 | initial_insn_length = end - buf; |
| 706 | if (initial_insn_length <= 50) |
| 707 | { |
| 708 | cie.initial_insn_length = initial_insn_length; |
| 709 | memcpy (cie.initial_instructions, buf, initial_insn_length); |
| 710 | } |
| 711 | insns = buf; |
| 712 | buf += initial_insn_length; |
| 713 | ENSURE_NO_RELOCS (buf); |
| 714 | last_cie = last_fde; |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | /* Ensure this FDE uses the last CIE encountered. */ |
| 719 | REQUIRE (last_cie); |
| 720 | REQUIRE (hdr.id == (unsigned int) (buf - 4 - last_cie)); |
| 721 | |
| 722 | ENSURE_NO_RELOCS (buf); |
| 723 | REQUIRE (GET_RELOC (buf)); |
| 724 | |
| 725 | if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie)) |
| 726 | /* This is a FDE against a discarded section. It should |
| 727 | be deleted. */ |
| 728 | this_inf->removed = 1; |
| 729 | else |
| 730 | { |
| 731 | if (info->shared |
| 732 | && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr |
| 733 | && cie.make_relative == 0) |
| 734 | || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned)) |
| 735 | { |
| 736 | /* If a shared library uses absolute pointers |
| 737 | which we cannot turn into PC relative, |
| 738 | don't create the binary search table, |
| 739 | since it is affected by runtime relocations. */ |
| 740 | hdr_info->table = FALSE; |
| 741 | } |
| 742 | cie_usage_count++; |
| 743 | hdr_info->fde_count++; |
| 744 | } |
| 745 | /* Skip the initial location and address range. */ |
| 746 | start = buf; |
| 747 | length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size); |
| 748 | REQUIRE (skip_bytes (&buf, end, 2 * length)); |
| 749 | |
| 750 | /* Skip the augmentation size, if present. */ |
| 751 | if (cie.augmentation[0] == 'z') |
| 752 | REQUIRE (read_uleb128 (&buf, end, &length)); |
| 753 | else |
| 754 | length = 0; |
| 755 | |
| 756 | /* Of the supported augmentation characters above, only 'L' |
| 757 | adds augmentation data to the FDE. This code would need to |
| 758 | be adjusted if any future augmentations do the same thing. */ |
| 759 | if (cie.lsda_encoding != DW_EH_PE_omit) |
| 760 | { |
| 761 | this_inf->lsda_offset = buf - start; |
| 762 | /* If there's no 'z' augmentation, we don't know where the |
| 763 | CFA insns begin. Assume no padding. */ |
| 764 | if (cie.augmentation[0] != 'z') |
| 765 | length = end - buf; |
| 766 | } |
| 767 | |
| 768 | /* Skip over the augmentation data. */ |
| 769 | REQUIRE (skip_bytes (&buf, end, length)); |
| 770 | insns = buf; |
| 771 | |
| 772 | buf = last_fde + 4 + hdr.length; |
| 773 | SKIP_RELOCS (buf); |
| 774 | } |
| 775 | |
| 776 | /* Try to interpret the CFA instructions and find the first |
| 777 | padding nop. Shrink this_inf's size so that it doesn't |
| 778 | including the padding. */ |
| 779 | length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size); |
| 780 | insns = skip_non_nops (insns, end, length); |
| 781 | if (insns != 0) |
| 782 | this_inf->size -= end - insns; |
| 783 | |
| 784 | this_inf->fde_encoding = cie.fde_encoding; |
| 785 | this_inf->lsda_encoding = cie.lsda_encoding; |
| 786 | sec_info->count++; |
| 787 | } |
| 788 | |
| 789 | elf_section_data (sec)->sec_info = sec_info; |
| 790 | sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME; |
| 791 | |
| 792 | /* Ok, now we can assign new offsets. */ |
| 793 | offset = 0; |
| 794 | last_cie_inf = hdr_info->last_cie_inf; |
| 795 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) |
| 796 | if (!ent->removed) |
| 797 | { |
| 798 | if (ent->cie) |
| 799 | last_cie_inf = ent; |
| 800 | else |
| 801 | ent->cie_inf = last_cie_inf; |
| 802 | ent->new_offset = offset; |
| 803 | offset += size_of_output_cie_fde (ent, ptr_size); |
| 804 | } |
| 805 | hdr_info->last_cie_inf = last_cie_inf; |
| 806 | |
| 807 | /* Resize the sec as needed. */ |
| 808 | sec->rawsize = sec->size; |
| 809 | sec->size = offset; |
| 810 | if (sec->size == 0) |
| 811 | sec->flags |= SEC_EXCLUDE; |
| 812 | |
| 813 | free (ehbuf); |
| 814 | return offset != sec->rawsize; |
| 815 | |
| 816 | free_no_table: |
| 817 | if (ehbuf) |
| 818 | free (ehbuf); |
| 819 | if (sec_info) |
| 820 | free (sec_info); |
| 821 | hdr_info->table = FALSE; |
| 822 | hdr_info->last_cie.hdr.length = 0; |
| 823 | return FALSE; |
| 824 | |
| 825 | #undef REQUIRE |
| 826 | } |
| 827 | |
| 828 | /* This function is called for .eh_frame_hdr section after |
| 829 | _bfd_elf_discard_section_eh_frame has been called on all .eh_frame |
| 830 | input sections. It finalizes the size of .eh_frame_hdr section. */ |
| 831 | |
| 832 | bfd_boolean |
| 833 | _bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info) |
| 834 | { |
| 835 | struct elf_link_hash_table *htab; |
| 836 | struct eh_frame_hdr_info *hdr_info; |
| 837 | asection *sec; |
| 838 | |
| 839 | htab = elf_hash_table (info); |
| 840 | hdr_info = &htab->eh_info; |
| 841 | sec = hdr_info->hdr_sec; |
| 842 | if (sec == NULL) |
| 843 | return FALSE; |
| 844 | |
| 845 | sec->size = EH_FRAME_HDR_SIZE; |
| 846 | if (hdr_info->table) |
| 847 | sec->size += 4 + hdr_info->fde_count * 8; |
| 848 | |
| 849 | /* Request program headers to be recalculated. */ |
| 850 | elf_tdata (abfd)->program_header_size = 0; |
| 851 | elf_tdata (abfd)->eh_frame_hdr = sec; |
| 852 | return TRUE; |
| 853 | } |
| 854 | |
| 855 | /* This function is called from size_dynamic_sections. |
| 856 | It needs to decide whether .eh_frame_hdr should be output or not, |
| 857 | because when the dynamic symbol table has been sized it is too late |
| 858 | to strip sections. */ |
| 859 | |
| 860 | bfd_boolean |
| 861 | _bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info) |
| 862 | { |
| 863 | asection *o; |
| 864 | bfd *abfd; |
| 865 | struct elf_link_hash_table *htab; |
| 866 | struct eh_frame_hdr_info *hdr_info; |
| 867 | |
| 868 | htab = elf_hash_table (info); |
| 869 | hdr_info = &htab->eh_info; |
| 870 | if (hdr_info->hdr_sec == NULL) |
| 871 | return TRUE; |
| 872 | |
| 873 | if (bfd_is_abs_section (hdr_info->hdr_sec->output_section)) |
| 874 | { |
| 875 | hdr_info->hdr_sec = NULL; |
| 876 | return TRUE; |
| 877 | } |
| 878 | |
| 879 | abfd = NULL; |
| 880 | if (info->eh_frame_hdr) |
| 881 | for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) |
| 882 | { |
| 883 | /* Count only sections which have at least a single CIE or FDE. |
| 884 | There cannot be any CIE or FDE <= 8 bytes. */ |
| 885 | o = bfd_get_section_by_name (abfd, ".eh_frame"); |
| 886 | if (o && o->size > 8 && !bfd_is_abs_section (o->output_section)) |
| 887 | break; |
| 888 | } |
| 889 | |
| 890 | if (abfd == NULL) |
| 891 | { |
| 892 | hdr_info->hdr_sec->flags |= SEC_EXCLUDE; |
| 893 | hdr_info->hdr_sec = NULL; |
| 894 | return TRUE; |
| 895 | } |
| 896 | |
| 897 | hdr_info->table = TRUE; |
| 898 | return TRUE; |
| 899 | } |
| 900 | |
| 901 | /* Adjust an address in the .eh_frame section. Given OFFSET within |
| 902 | SEC, this returns the new offset in the adjusted .eh_frame section, |
| 903 | or -1 if the address refers to a CIE/FDE which has been removed |
| 904 | or to offset with dynamic relocation which is no longer needed. */ |
| 905 | |
| 906 | bfd_vma |
| 907 | _bfd_elf_eh_frame_section_offset (bfd *output_bfd ATTRIBUTE_UNUSED, |
| 908 | struct bfd_link_info *info, |
| 909 | asection *sec, |
| 910 | bfd_vma offset) |
| 911 | { |
| 912 | struct eh_frame_sec_info *sec_info; |
| 913 | struct elf_link_hash_table *htab; |
| 914 | struct eh_frame_hdr_info *hdr_info; |
| 915 | unsigned int lo, hi, mid; |
| 916 | |
| 917 | if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME) |
| 918 | return offset; |
| 919 | sec_info = elf_section_data (sec)->sec_info; |
| 920 | |
| 921 | if (offset >= sec->rawsize) |
| 922 | return offset - sec->rawsize + sec->size; |
| 923 | |
| 924 | htab = elf_hash_table (info); |
| 925 | hdr_info = &htab->eh_info; |
| 926 | if (hdr_info->offsets_adjusted) |
| 927 | offset += sec->output_offset; |
| 928 | |
| 929 | lo = 0; |
| 930 | hi = sec_info->count; |
| 931 | mid = 0; |
| 932 | while (lo < hi) |
| 933 | { |
| 934 | mid = (lo + hi) / 2; |
| 935 | if (offset < sec_info->entry[mid].offset) |
| 936 | hi = mid; |
| 937 | else if (offset |
| 938 | >= sec_info->entry[mid].offset + sec_info->entry[mid].size) |
| 939 | lo = mid + 1; |
| 940 | else |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | BFD_ASSERT (lo < hi); |
| 945 | |
| 946 | /* FDE or CIE was removed. */ |
| 947 | if (sec_info->entry[mid].removed) |
| 948 | return (bfd_vma) -1; |
| 949 | |
| 950 | /* If converting to DW_EH_PE_pcrel, there will be no need for run-time |
| 951 | relocation against FDE's initial_location field. */ |
| 952 | if (!sec_info->entry[mid].cie |
| 953 | && sec_info->entry[mid].cie_inf->make_relative |
| 954 | && offset == sec_info->entry[mid].offset + 8) |
| 955 | return (bfd_vma) -2; |
| 956 | |
| 957 | /* If converting LSDA pointers to DW_EH_PE_pcrel, there will be no need |
| 958 | for run-time relocation against LSDA field. */ |
| 959 | if (!sec_info->entry[mid].cie |
| 960 | && sec_info->entry[mid].cie_inf->make_lsda_relative |
| 961 | && (offset == (sec_info->entry[mid].offset + 8 |
| 962 | + sec_info->entry[mid].lsda_offset)) |
| 963 | && (sec_info->entry[mid].cie_inf->need_lsda_relative |
| 964 | || !hdr_info->offsets_adjusted)) |
| 965 | { |
| 966 | sec_info->entry[mid].cie_inf->need_lsda_relative = 1; |
| 967 | return (bfd_vma) -2; |
| 968 | } |
| 969 | |
| 970 | if (hdr_info->offsets_adjusted) |
| 971 | offset -= sec->output_offset; |
| 972 | /* Any new augmentation bytes go before the first relocation. */ |
| 973 | return (offset + sec_info->entry[mid].new_offset |
| 974 | - sec_info->entry[mid].offset |
| 975 | + extra_augmentation_string_bytes (sec_info->entry + mid) |
| 976 | + extra_augmentation_data_bytes (sec_info->entry + mid)); |
| 977 | } |
| 978 | |
| 979 | /* Write out .eh_frame section. This is called with the relocated |
| 980 | contents. */ |
| 981 | |
| 982 | bfd_boolean |
| 983 | _bfd_elf_write_section_eh_frame (bfd *abfd, |
| 984 | struct bfd_link_info *info, |
| 985 | asection *sec, |
| 986 | bfd_byte *contents) |
| 987 | { |
| 988 | struct eh_frame_sec_info *sec_info; |
| 989 | struct elf_link_hash_table *htab; |
| 990 | struct eh_frame_hdr_info *hdr_info; |
| 991 | unsigned int ptr_size; |
| 992 | struct eh_cie_fde *ent; |
| 993 | |
| 994 | if (sec->sec_info_type != ELF_INFO_TYPE_EH_FRAME) |
| 995 | return bfd_set_section_contents (abfd, sec->output_section, contents, |
| 996 | sec->output_offset, sec->size); |
| 997 | |
| 998 | ptr_size = (get_elf_backend_data (abfd) |
| 999 | ->elf_backend_eh_frame_address_size (abfd, sec)); |
| 1000 | BFD_ASSERT (ptr_size != 0); |
| 1001 | |
| 1002 | sec_info = elf_section_data (sec)->sec_info; |
| 1003 | htab = elf_hash_table (info); |
| 1004 | hdr_info = &htab->eh_info; |
| 1005 | |
| 1006 | /* First convert all offsets to output section offsets, so that a |
| 1007 | CIE offset is valid if the CIE is used by a FDE from some other |
| 1008 | section. This can happen when duplicate CIEs are deleted in |
| 1009 | _bfd_elf_discard_section_eh_frame. We do all sections here because |
| 1010 | this function might not be called on sections in the same order as |
| 1011 | _bfd_elf_discard_section_eh_frame. */ |
| 1012 | if (!hdr_info->offsets_adjusted) |
| 1013 | { |
| 1014 | bfd *ibfd; |
| 1015 | asection *eh; |
| 1016 | struct eh_frame_sec_info *eh_inf; |
| 1017 | |
| 1018 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) |
| 1019 | { |
| 1020 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
| 1021 | || (ibfd->flags & DYNAMIC) != 0) |
| 1022 | continue; |
| 1023 | |
| 1024 | eh = bfd_get_section_by_name (ibfd, ".eh_frame"); |
| 1025 | if (eh == NULL || eh->sec_info_type != ELF_INFO_TYPE_EH_FRAME) |
| 1026 | continue; |
| 1027 | |
| 1028 | eh_inf = elf_section_data (eh)->sec_info; |
| 1029 | for (ent = eh_inf->entry; ent < eh_inf->entry + eh_inf->count; ++ent) |
| 1030 | { |
| 1031 | ent->offset += eh->output_offset; |
| 1032 | ent->new_offset += eh->output_offset; |
| 1033 | } |
| 1034 | } |
| 1035 | hdr_info->offsets_adjusted = TRUE; |
| 1036 | } |
| 1037 | |
| 1038 | if (hdr_info->table && hdr_info->array == NULL) |
| 1039 | hdr_info->array |
| 1040 | = bfd_malloc (hdr_info->fde_count * sizeof(*hdr_info->array)); |
| 1041 | if (hdr_info->array == NULL) |
| 1042 | hdr_info = NULL; |
| 1043 | |
| 1044 | /* The new offsets can be bigger or smaller than the original offsets. |
| 1045 | We therefore need to make two passes over the section: one backward |
| 1046 | pass to move entries up and one forward pass to move entries down. |
| 1047 | The two passes won't interfere with each other because entries are |
| 1048 | not reordered */ |
| 1049 | for (ent = sec_info->entry + sec_info->count; ent-- != sec_info->entry;) |
| 1050 | if (!ent->removed && ent->new_offset > ent->offset) |
| 1051 | memmove (contents + ent->new_offset - sec->output_offset, |
| 1052 | contents + ent->offset - sec->output_offset, ent->size); |
| 1053 | |
| 1054 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) |
| 1055 | if (!ent->removed && ent->new_offset < ent->offset) |
| 1056 | memmove (contents + ent->new_offset - sec->output_offset, |
| 1057 | contents + ent->offset - sec->output_offset, ent->size); |
| 1058 | |
| 1059 | for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) |
| 1060 | { |
| 1061 | unsigned char *buf, *end; |
| 1062 | unsigned int new_size; |
| 1063 | |
| 1064 | if (ent->removed) |
| 1065 | continue; |
| 1066 | |
| 1067 | if (ent->size == 4) |
| 1068 | { |
| 1069 | /* Any terminating FDE must be at the end of the section. */ |
| 1070 | BFD_ASSERT (ent == sec_info->entry + sec_info->count - 1); |
| 1071 | continue; |
| 1072 | } |
| 1073 | |
| 1074 | buf = contents + ent->new_offset - sec->output_offset; |
| 1075 | end = buf + ent->size; |
| 1076 | new_size = size_of_output_cie_fde (ent, ptr_size); |
| 1077 | |
| 1078 | /* Install the new size, filling the extra bytes with DW_CFA_nops. */ |
| 1079 | if (new_size != ent->size) |
| 1080 | { |
| 1081 | memset (end, 0, new_size - ent->size); |
| 1082 | bfd_put_32 (abfd, new_size - 4, buf); |
| 1083 | } |
| 1084 | |
| 1085 | if (ent->cie) |
| 1086 | { |
| 1087 | /* CIE */ |
| 1088 | if (ent->make_relative |
| 1089 | || ent->need_lsda_relative |
| 1090 | || ent->per_encoding_relative) |
| 1091 | { |
| 1092 | char *aug; |
| 1093 | unsigned int action, extra_string, extra_data; |
| 1094 | unsigned int per_width, per_encoding; |
| 1095 | |
| 1096 | /* Need to find 'R' or 'L' augmentation's argument and modify |
| 1097 | DW_EH_PE_* value. */ |
| 1098 | action = ((ent->make_relative ? 1 : 0) |
| 1099 | | (ent->need_lsda_relative ? 2 : 0) |
| 1100 | | (ent->per_encoding_relative ? 4 : 0)); |
| 1101 | extra_string = extra_augmentation_string_bytes (ent); |
| 1102 | extra_data = extra_augmentation_data_bytes (ent); |
| 1103 | |
| 1104 | /* Skip length, id and version. */ |
| 1105 | buf += 9; |
| 1106 | aug = (char *) buf; |
| 1107 | buf += strlen (aug) + 1; |
| 1108 | skip_leb128 (&buf, end); |
| 1109 | skip_leb128 (&buf, end); |
| 1110 | skip_leb128 (&buf, end); |
| 1111 | if (*aug == 'z') |
| 1112 | { |
| 1113 | /* The uleb128 will always be a single byte for the kind |
| 1114 | of augmentation strings that we're prepared to handle. */ |
| 1115 | *buf++ += extra_data; |
| 1116 | aug++; |
| 1117 | } |
| 1118 | |
| 1119 | /* Make room for the new augmentation string and data bytes. */ |
| 1120 | memmove (buf + extra_string + extra_data, buf, end - buf); |
| 1121 | memmove (aug + extra_string, aug, buf - (bfd_byte *) aug); |
| 1122 | buf += extra_string; |
| 1123 | end += extra_string + extra_data; |
| 1124 | |
| 1125 | if (ent->add_augmentation_size) |
| 1126 | { |
| 1127 | *aug++ = 'z'; |
| 1128 | *buf++ = extra_data - 1; |
| 1129 | } |
| 1130 | if (ent->add_fde_encoding) |
| 1131 | { |
| 1132 | BFD_ASSERT (action & 1); |
| 1133 | *aug++ = 'R'; |
| 1134 | *buf++ = DW_EH_PE_pcrel; |
| 1135 | action &= ~1; |
| 1136 | } |
| 1137 | |
| 1138 | while (action) |
| 1139 | switch (*aug++) |
| 1140 | { |
| 1141 | case 'L': |
| 1142 | if (action & 2) |
| 1143 | { |
| 1144 | BFD_ASSERT (*buf == ent->lsda_encoding); |
| 1145 | *buf |= DW_EH_PE_pcrel; |
| 1146 | action &= ~2; |
| 1147 | } |
| 1148 | buf++; |
| 1149 | break; |
| 1150 | case 'P': |
| 1151 | per_encoding = *buf++; |
| 1152 | per_width = get_DW_EH_PE_width (per_encoding, ptr_size); |
| 1153 | BFD_ASSERT (per_width != 0); |
| 1154 | BFD_ASSERT (((per_encoding & 0x70) == DW_EH_PE_pcrel) |
| 1155 | == ent->per_encoding_relative); |
| 1156 | if ((per_encoding & 0xf0) == DW_EH_PE_aligned) |
| 1157 | buf = (contents |
| 1158 | + ((buf - contents + per_width - 1) |
| 1159 | & ~((bfd_size_type) per_width - 1))); |
| 1160 | if (action & 4) |
| 1161 | { |
| 1162 | bfd_vma val; |
| 1163 | |
| 1164 | val = read_value (abfd, buf, per_width, |
| 1165 | get_DW_EH_PE_signed (per_encoding)); |
| 1166 | val += ent->offset - ent->new_offset; |
| 1167 | val -= extra_string + extra_data; |
| 1168 | write_value (abfd, buf, val, per_width); |
| 1169 | action &= ~4; |
| 1170 | } |
| 1171 | buf += per_width; |
| 1172 | break; |
| 1173 | case 'R': |
| 1174 | if (action & 1) |
| 1175 | { |
| 1176 | BFD_ASSERT (*buf == ent->fde_encoding); |
| 1177 | *buf |= DW_EH_PE_pcrel; |
| 1178 | action &= ~1; |
| 1179 | } |
| 1180 | buf++; |
| 1181 | break; |
| 1182 | case 'S': |
| 1183 | break; |
| 1184 | default: |
| 1185 | BFD_FAIL (); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | /* FDE */ |
| 1192 | bfd_vma value, address; |
| 1193 | unsigned int width; |
| 1194 | |
| 1195 | /* Skip length. */ |
| 1196 | buf += 4; |
| 1197 | value = ent->new_offset + 4 - ent->cie_inf->new_offset; |
| 1198 | bfd_put_32 (abfd, value, buf); |
| 1199 | buf += 4; |
| 1200 | width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size); |
| 1201 | value = read_value (abfd, buf, width, |
| 1202 | get_DW_EH_PE_signed (ent->fde_encoding)); |
| 1203 | address = value; |
| 1204 | if (value) |
| 1205 | { |
| 1206 | switch (ent->fde_encoding & 0xf0) |
| 1207 | { |
| 1208 | case DW_EH_PE_indirect: |
| 1209 | case DW_EH_PE_textrel: |
| 1210 | BFD_ASSERT (hdr_info == NULL); |
| 1211 | break; |
| 1212 | case DW_EH_PE_datarel: |
| 1213 | { |
| 1214 | asection *got = bfd_get_section_by_name (abfd, ".got"); |
| 1215 | |
| 1216 | BFD_ASSERT (got != NULL); |
| 1217 | address += got->vma; |
| 1218 | } |
| 1219 | break; |
| 1220 | case DW_EH_PE_pcrel: |
| 1221 | value += ent->offset - ent->new_offset; |
| 1222 | address += sec->output_section->vma + ent->offset + 8; |
| 1223 | break; |
| 1224 | } |
| 1225 | if (ent->cie_inf->make_relative) |
| 1226 | value -= sec->output_section->vma + ent->new_offset + 8; |
| 1227 | write_value (abfd, buf, value, width); |
| 1228 | } |
| 1229 | |
| 1230 | if (hdr_info) |
| 1231 | { |
| 1232 | hdr_info->array[hdr_info->array_count].initial_loc = address; |
| 1233 | hdr_info->array[hdr_info->array_count++].fde |
| 1234 | = sec->output_section->vma + ent->new_offset; |
| 1235 | } |
| 1236 | |
| 1237 | if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel |
| 1238 | || ent->cie_inf->need_lsda_relative) |
| 1239 | { |
| 1240 | buf += ent->lsda_offset; |
| 1241 | width = get_DW_EH_PE_width (ent->lsda_encoding, ptr_size); |
| 1242 | value = read_value (abfd, buf, width, |
| 1243 | get_DW_EH_PE_signed (ent->lsda_encoding)); |
| 1244 | if (value) |
| 1245 | { |
| 1246 | if ((ent->lsda_encoding & 0xf0) == DW_EH_PE_pcrel) |
| 1247 | value += ent->offset - ent->new_offset; |
| 1248 | else if (ent->cie_inf->need_lsda_relative) |
| 1249 | value -= (sec->output_section->vma + ent->new_offset + 8 |
| 1250 | + ent->lsda_offset); |
| 1251 | write_value (abfd, buf, value, width); |
| 1252 | } |
| 1253 | } |
| 1254 | else if (ent->cie_inf->add_augmentation_size) |
| 1255 | { |
| 1256 | /* Skip the PC and length and insert a zero byte for the |
| 1257 | augmentation size. */ |
| 1258 | buf += width * 2; |
| 1259 | memmove (buf + 1, buf, end - buf); |
| 1260 | *buf = 0; |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | { |
| 1266 | unsigned int alignment = 1 << sec->alignment_power; |
| 1267 | unsigned int pad = sec->size % alignment; |
| 1268 | |
| 1269 | /* Don't pad beyond the raw size of the output section. It |
| 1270 | can happen at the last input section. */ |
| 1271 | if (pad |
| 1272 | && ((sec->output_offset + sec->size + pad) |
| 1273 | <= sec->output_section->size)) |
| 1274 | { |
| 1275 | bfd_byte *buf; |
| 1276 | unsigned int new_size; |
| 1277 | |
| 1278 | /* Find the last CIE/FDE. */ |
| 1279 | ent = sec_info->entry + sec_info->count; |
| 1280 | while (--ent != sec_info->entry) |
| 1281 | if (!ent->removed) |
| 1282 | break; |
| 1283 | |
| 1284 | /* The size of the last CIE/FDE must be at least 4. */ |
| 1285 | if (ent->removed || ent->size < 4) |
| 1286 | abort (); |
| 1287 | |
| 1288 | pad = alignment - pad; |
| 1289 | buf = contents + ent->new_offset - sec->output_offset; |
| 1290 | new_size = size_of_output_cie_fde (ent, ptr_size); |
| 1291 | |
| 1292 | /* Pad it with DW_CFA_nop */ |
| 1293 | memset (buf + new_size, 0, pad); |
| 1294 | bfd_put_32 (abfd, new_size + pad - 4, buf); |
| 1295 | |
| 1296 | sec->size += pad; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | return bfd_set_section_contents (abfd, sec->output_section, |
| 1301 | contents, (file_ptr) sec->output_offset, |
| 1302 | sec->size); |
| 1303 | } |
| 1304 | |
| 1305 | /* Helper function used to sort .eh_frame_hdr search table by increasing |
| 1306 | VMA of FDE initial location. */ |
| 1307 | |
| 1308 | static int |
| 1309 | vma_compare (const void *a, const void *b) |
| 1310 | { |
| 1311 | const struct eh_frame_array_ent *p = a; |
| 1312 | const struct eh_frame_array_ent *q = b; |
| 1313 | if (p->initial_loc > q->initial_loc) |
| 1314 | return 1; |
| 1315 | if (p->initial_loc < q->initial_loc) |
| 1316 | return -1; |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | /* Write out .eh_frame_hdr section. This must be called after |
| 1321 | _bfd_elf_write_section_eh_frame has been called on all input |
| 1322 | .eh_frame sections. |
| 1323 | .eh_frame_hdr format: |
| 1324 | ubyte version (currently 1) |
| 1325 | ubyte eh_frame_ptr_enc (DW_EH_PE_* encoding of pointer to start of |
| 1326 | .eh_frame section) |
| 1327 | ubyte fde_count_enc (DW_EH_PE_* encoding of total FDE count |
| 1328 | number (or DW_EH_PE_omit if there is no |
| 1329 | binary search table computed)) |
| 1330 | ubyte table_enc (DW_EH_PE_* encoding of binary search table, |
| 1331 | or DW_EH_PE_omit if not present. |
| 1332 | DW_EH_PE_datarel is using address of |
| 1333 | .eh_frame_hdr section start as base) |
| 1334 | [encoded] eh_frame_ptr (pointer to start of .eh_frame section) |
| 1335 | optionally followed by: |
| 1336 | [encoded] fde_count (total number of FDEs in .eh_frame section) |
| 1337 | fde_count x [encoded] initial_loc, fde |
| 1338 | (array of encoded pairs containing |
| 1339 | FDE initial_location field and FDE address, |
| 1340 | sorted by increasing initial_loc). */ |
| 1341 | |
| 1342 | bfd_boolean |
| 1343 | _bfd_elf_write_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info) |
| 1344 | { |
| 1345 | struct elf_link_hash_table *htab; |
| 1346 | struct eh_frame_hdr_info *hdr_info; |
| 1347 | asection *sec; |
| 1348 | bfd_byte *contents; |
| 1349 | asection *eh_frame_sec; |
| 1350 | bfd_size_type size; |
| 1351 | bfd_boolean retval; |
| 1352 | bfd_vma encoded_eh_frame; |
| 1353 | |
| 1354 | htab = elf_hash_table (info); |
| 1355 | hdr_info = &htab->eh_info; |
| 1356 | sec = hdr_info->hdr_sec; |
| 1357 | if (sec == NULL) |
| 1358 | return TRUE; |
| 1359 | |
| 1360 | size = EH_FRAME_HDR_SIZE; |
| 1361 | if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count) |
| 1362 | size += 4 + hdr_info->fde_count * 8; |
| 1363 | contents = bfd_malloc (size); |
| 1364 | if (contents == NULL) |
| 1365 | return FALSE; |
| 1366 | |
| 1367 | eh_frame_sec = bfd_get_section_by_name (abfd, ".eh_frame"); |
| 1368 | if (eh_frame_sec == NULL) |
| 1369 | { |
| 1370 | free (contents); |
| 1371 | return FALSE; |
| 1372 | } |
| 1373 | |
| 1374 | memset (contents, 0, EH_FRAME_HDR_SIZE); |
| 1375 | contents[0] = 1; /* Version. */ |
| 1376 | contents[1] = get_elf_backend_data (abfd)->elf_backend_encode_eh_address |
| 1377 | (abfd, info, eh_frame_sec, 0, sec, 4, |
| 1378 | &encoded_eh_frame); /* .eh_frame offset. */ |
| 1379 | |
| 1380 | if (hdr_info->array && hdr_info->array_count == hdr_info->fde_count) |
| 1381 | { |
| 1382 | contents[2] = DW_EH_PE_udata4; /* FDE count encoding. */ |
| 1383 | contents[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; /* Search table enc. */ |
| 1384 | } |
| 1385 | else |
| 1386 | { |
| 1387 | contents[2] = DW_EH_PE_omit; |
| 1388 | contents[3] = DW_EH_PE_omit; |
| 1389 | } |
| 1390 | bfd_put_32 (abfd, encoded_eh_frame, contents + 4); |
| 1391 | |
| 1392 | if (contents[2] != DW_EH_PE_omit) |
| 1393 | { |
| 1394 | unsigned int i; |
| 1395 | |
| 1396 | bfd_put_32 (abfd, hdr_info->fde_count, contents + EH_FRAME_HDR_SIZE); |
| 1397 | qsort (hdr_info->array, hdr_info->fde_count, sizeof (*hdr_info->array), |
| 1398 | vma_compare); |
| 1399 | for (i = 0; i < hdr_info->fde_count; i++) |
| 1400 | { |
| 1401 | bfd_put_32 (abfd, |
| 1402 | hdr_info->array[i].initial_loc |
| 1403 | - sec->output_section->vma, |
| 1404 | contents + EH_FRAME_HDR_SIZE + i * 8 + 4); |
| 1405 | bfd_put_32 (abfd, |
| 1406 | hdr_info->array[i].fde - sec->output_section->vma, |
| 1407 | contents + EH_FRAME_HDR_SIZE + i * 8 + 8); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | retval = bfd_set_section_contents (abfd, sec->output_section, |
| 1412 | contents, (file_ptr) sec->output_offset, |
| 1413 | sec->size); |
| 1414 | free (contents); |
| 1415 | return retval; |
| 1416 | } |
| 1417 | |
| 1418 | /* Return the width of FDE addresses. This is the default implementation. */ |
| 1419 | |
| 1420 | unsigned int |
| 1421 | _bfd_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED) |
| 1422 | { |
| 1423 | return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4; |
| 1424 | } |
| 1425 | |
| 1426 | /* Decide whether we can use a PC-relative encoding within the given |
| 1427 | EH frame section. This is the default implementation. */ |
| 1428 | |
| 1429 | bfd_boolean |
| 1430 | _bfd_elf_can_make_relative (bfd *input_bfd ATTRIBUTE_UNUSED, |
| 1431 | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1432 | asection *eh_frame_section ATTRIBUTE_UNUSED) |
| 1433 | { |
| 1434 | return TRUE; |
| 1435 | } |
| 1436 | |
| 1437 | /* Select an encoding for the given address. Preference is given to |
| 1438 | PC-relative addressing modes. */ |
| 1439 | |
| 1440 | bfd_byte |
| 1441 | _bfd_elf_encode_eh_address (bfd *abfd ATTRIBUTE_UNUSED, |
| 1442 | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 1443 | asection *osec, bfd_vma offset, |
| 1444 | asection *loc_sec, bfd_vma loc_offset, |
| 1445 | bfd_vma *encoded) |
| 1446 | { |
| 1447 | *encoded = osec->vma + offset - |
| 1448 | (loc_sec->output_section->vma + loc_sec->output_offset + loc_offset); |
| 1449 | return DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 1450 | } |