| 1 | // ehframe.cc -- handle exception frame sections for gold |
| 2 | |
| 3 | // Copyright (C) 2006-2018 Free Software Foundation, Inc. |
| 4 | // Written by Ian Lance Taylor <iant@google.com>. |
| 5 | |
| 6 | // This file is part of gold. |
| 7 | |
| 8 | // This program is free software; you can redistribute it and/or modify |
| 9 | // it under the terms of the GNU General Public License as published by |
| 10 | // the Free Software Foundation; either version 3 of the License, or |
| 11 | // (at your option) any later version. |
| 12 | |
| 13 | // This program is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 21 | // MA 02110-1301, USA. |
| 22 | |
| 23 | #include "gold.h" |
| 24 | |
| 25 | #include <cstring> |
| 26 | #include <algorithm> |
| 27 | |
| 28 | #include "elfcpp.h" |
| 29 | #include "dwarf.h" |
| 30 | #include "symtab.h" |
| 31 | #include "reloc.h" |
| 32 | #include "ehframe.h" |
| 33 | |
| 34 | namespace gold |
| 35 | { |
| 36 | |
| 37 | // This file handles generation of the exception frame header that |
| 38 | // gcc's runtime support libraries use to find unwind information at |
| 39 | // runtime. This file also handles discarding duplicate exception |
| 40 | // frame information. |
| 41 | |
| 42 | // The exception frame header starts with four bytes: |
| 43 | |
| 44 | // 0: The version number, currently 1. |
| 45 | |
| 46 | // 1: The encoding of the pointer to the exception frames. This can |
| 47 | // be any DWARF unwind encoding (DW_EH_PE_*). It is normally a 4 |
| 48 | // byte PC relative offset (DW_EH_PE_pcrel | DW_EH_PE_sdata4). |
| 49 | |
| 50 | // 2: The encoding of the count of the number of FDE pointers in the |
| 51 | // lookup table. This can be any DWARF unwind encoding, and in |
| 52 | // particular can be DW_EH_PE_omit if the count is omitted. It is |
| 53 | // normally a 4 byte unsigned count (DW_EH_PE_udata4). |
| 54 | |
| 55 | // 3: The encoding of the lookup table entries. Currently gcc's |
| 56 | // libraries will only support DW_EH_PE_datarel | DW_EH_PE_sdata4, |
| 57 | // which means that the values are 4 byte offsets from the start of |
| 58 | // the table. |
| 59 | |
| 60 | // The exception frame header is followed by a pointer to the contents |
| 61 | // of the exception frame section (.eh_frame). This pointer is |
| 62 | // encoded as specified in the byte at offset 1 of the header (i.e., |
| 63 | // it is normally a 4 byte PC relative offset). |
| 64 | |
| 65 | // If there is a lookup table, this is followed by the count of the |
| 66 | // number of FDE pointers, encoded as specified in the byte at offset |
| 67 | // 2 of the header (i.e., normally a 4 byte unsigned integer). |
| 68 | |
| 69 | // This is followed by the table, which should start at an 4-byte |
| 70 | // aligned address in memory. Each entry in the table is 8 bytes. |
| 71 | // Each entry represents an FDE. The first four bytes of each entry |
| 72 | // are an offset to the starting PC for the FDE. The last four bytes |
| 73 | // of each entry are an offset to the FDE data. The offsets are from |
| 74 | // the start of the exception frame header information. The entries |
| 75 | // are in sorted order by starting PC. |
| 76 | |
| 77 | const int eh_frame_hdr_size = 4; |
| 78 | |
| 79 | // Construct the exception frame header. |
| 80 | |
| 81 | Eh_frame_hdr::Eh_frame_hdr(Output_section* eh_frame_section, |
| 82 | const Eh_frame* eh_frame_data) |
| 83 | : Output_section_data(4), |
| 84 | eh_frame_section_(eh_frame_section), |
| 85 | eh_frame_data_(eh_frame_data), |
| 86 | fde_offsets_(), |
| 87 | any_unrecognized_eh_frame_sections_(false) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | // Set the size of the exception frame header. |
| 92 | |
| 93 | void |
| 94 | Eh_frame_hdr::set_final_data_size() |
| 95 | { |
| 96 | unsigned int data_size = eh_frame_hdr_size + 4; |
| 97 | if (!this->any_unrecognized_eh_frame_sections_) |
| 98 | { |
| 99 | unsigned int fde_count = this->eh_frame_data_->fde_count(); |
| 100 | if (fde_count != 0) |
| 101 | data_size += 4 + 8 * fde_count; |
| 102 | this->fde_offsets_.reserve(fde_count); |
| 103 | } |
| 104 | this->set_data_size(data_size); |
| 105 | } |
| 106 | |
| 107 | // Write the data to the file. |
| 108 | |
| 109 | void |
| 110 | Eh_frame_hdr::do_write(Output_file* of) |
| 111 | { |
| 112 | switch (parameters->size_and_endianness()) |
| 113 | { |
| 114 | #ifdef HAVE_TARGET_32_LITTLE |
| 115 | case Parameters::TARGET_32_LITTLE: |
| 116 | this->do_sized_write<32, false>(of); |
| 117 | break; |
| 118 | #endif |
| 119 | #ifdef HAVE_TARGET_32_BIG |
| 120 | case Parameters::TARGET_32_BIG: |
| 121 | this->do_sized_write<32, true>(of); |
| 122 | break; |
| 123 | #endif |
| 124 | #ifdef HAVE_TARGET_64_LITTLE |
| 125 | case Parameters::TARGET_64_LITTLE: |
| 126 | this->do_sized_write<64, false>(of); |
| 127 | break; |
| 128 | #endif |
| 129 | #ifdef HAVE_TARGET_64_BIG |
| 130 | case Parameters::TARGET_64_BIG: |
| 131 | this->do_sized_write<64, true>(of); |
| 132 | break; |
| 133 | #endif |
| 134 | default: |
| 135 | gold_unreachable(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Write the data to the file with the right endianness. |
| 140 | |
| 141 | template<int size, bool big_endian> |
| 142 | void |
| 143 | Eh_frame_hdr::do_sized_write(Output_file* of) |
| 144 | { |
| 145 | const off_t off = this->offset(); |
| 146 | const off_t oview_size = this->data_size(); |
| 147 | unsigned char* const oview = of->get_output_view(off, oview_size); |
| 148 | |
| 149 | // Version number. |
| 150 | oview[0] = 1; |
| 151 | |
| 152 | // Write out a 4 byte PC relative offset to the address of the |
| 153 | // .eh_frame section. |
| 154 | oview[1] = elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4; |
| 155 | uint64_t eh_frame_address = this->eh_frame_section_->address(); |
| 156 | uint64_t eh_frame_hdr_address = this->address(); |
| 157 | uint64_t eh_frame_offset = (eh_frame_address - |
| 158 | (eh_frame_hdr_address + 4)); |
| 159 | elfcpp::Swap<32, big_endian>::writeval(oview + 4, eh_frame_offset); |
| 160 | |
| 161 | if (this->any_unrecognized_eh_frame_sections_ |
| 162 | || this->fde_offsets_.empty()) |
| 163 | { |
| 164 | // There are no FDEs, or we didn't recognize the format of the |
| 165 | // some of the .eh_frame sections, so we can't write out the |
| 166 | // sorted table. |
| 167 | oview[2] = elfcpp::DW_EH_PE_omit; |
| 168 | oview[3] = elfcpp::DW_EH_PE_omit; |
| 169 | |
| 170 | gold_assert(oview_size == 8); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | oview[2] = elfcpp::DW_EH_PE_udata4; |
| 175 | oview[3] = elfcpp::DW_EH_PE_datarel | elfcpp::DW_EH_PE_sdata4; |
| 176 | |
| 177 | elfcpp::Swap<32, big_endian>::writeval(oview + 8, |
| 178 | this->fde_offsets_.size()); |
| 179 | |
| 180 | // We have the offsets of the FDEs in the .eh_frame section. We |
| 181 | // couldn't easily get the PC values before, as they depend on |
| 182 | // relocations which are, of course, target specific. This code |
| 183 | // is run after all those relocations have been applied to the |
| 184 | // output file. Here we read the output file again to find the |
| 185 | // PC values. Then we sort the list and write it out. |
| 186 | |
| 187 | Fde_addresses<size> fde_addresses(this->fde_offsets_.size()); |
| 188 | this->get_fde_addresses<size, big_endian>(of, &this->fde_offsets_, |
| 189 | &fde_addresses); |
| 190 | |
| 191 | std::sort(fde_addresses.begin(), fde_addresses.end(), |
| 192 | Fde_address_compare<size>()); |
| 193 | |
| 194 | typename elfcpp::Elf_types<size>::Elf_Addr output_address; |
| 195 | output_address = this->address(); |
| 196 | |
| 197 | unsigned char* pfde = oview + 12; |
| 198 | for (typename Fde_addresses<size>::iterator p = fde_addresses.begin(); |
| 199 | p != fde_addresses.end(); |
| 200 | ++p) |
| 201 | { |
| 202 | elfcpp::Swap<32, big_endian>::writeval(pfde, |
| 203 | p->first - output_address); |
| 204 | elfcpp::Swap<32, big_endian>::writeval(pfde + 4, |
| 205 | p->second - output_address); |
| 206 | pfde += 8; |
| 207 | } |
| 208 | |
| 209 | gold_assert(pfde - oview == oview_size); |
| 210 | } |
| 211 | |
| 212 | of->write_output_view(off, oview_size, oview); |
| 213 | } |
| 214 | |
| 215 | // Given the offset FDE_OFFSET of an FDE in the .eh_frame section, and |
| 216 | // the contents of the .eh_frame section EH_FRAME_CONTENTS, where the |
| 217 | // FDE's encoding is FDE_ENCODING, return the output address of the |
| 218 | // FDE's PC. |
| 219 | |
| 220 | template<int size, bool big_endian> |
| 221 | typename elfcpp::Elf_types<size>::Elf_Addr |
| 222 | Eh_frame_hdr::get_fde_pc( |
| 223 | typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address, |
| 224 | const unsigned char* eh_frame_contents, |
| 225 | section_offset_type fde_offset, |
| 226 | unsigned char fde_encoding) |
| 227 | { |
| 228 | // The FDE starts with a 4 byte length and a 4 byte offset to the |
| 229 | // CIE. The PC follows. |
| 230 | const unsigned char* p = eh_frame_contents + fde_offset + 8; |
| 231 | |
| 232 | typename elfcpp::Elf_types<size>::Elf_Addr pc; |
| 233 | bool is_signed = (fde_encoding & elfcpp::DW_EH_PE_signed) != 0; |
| 234 | int pc_size = fde_encoding & 7; |
| 235 | if (pc_size == elfcpp::DW_EH_PE_absptr) |
| 236 | { |
| 237 | if (size == 32) |
| 238 | pc_size = elfcpp::DW_EH_PE_udata4; |
| 239 | else if (size == 64) |
| 240 | pc_size = elfcpp::DW_EH_PE_udata8; |
| 241 | else |
| 242 | gold_unreachable(); |
| 243 | } |
| 244 | |
| 245 | switch (pc_size) |
| 246 | { |
| 247 | case elfcpp::DW_EH_PE_udata2: |
| 248 | pc = elfcpp::Swap<16, big_endian>::readval(p); |
| 249 | if (is_signed) |
| 250 | pc = (pc ^ 0x8000) - 0x8000; |
| 251 | break; |
| 252 | |
| 253 | case elfcpp::DW_EH_PE_udata4: |
| 254 | pc = elfcpp::Swap<32, big_endian>::readval(p); |
| 255 | if (size > 32 && is_signed) |
| 256 | pc = (pc ^ 0x80000000) - 0x80000000; |
| 257 | break; |
| 258 | |
| 259 | case elfcpp::DW_EH_PE_udata8: |
| 260 | gold_assert(size == 64); |
| 261 | pc = elfcpp::Swap_unaligned<64, big_endian>::readval(p); |
| 262 | break; |
| 263 | |
| 264 | default: |
| 265 | // All other cases were rejected in Eh_frame::read_cie. |
| 266 | gold_unreachable(); |
| 267 | } |
| 268 | |
| 269 | switch (fde_encoding & 0x70) |
| 270 | { |
| 271 | case 0: |
| 272 | break; |
| 273 | |
| 274 | case elfcpp::DW_EH_PE_pcrel: |
| 275 | pc += eh_frame_address + fde_offset + 8; |
| 276 | break; |
| 277 | |
| 278 | case elfcpp::DW_EH_PE_datarel: |
| 279 | pc += parameters->target().ehframe_datarel_base(); |
| 280 | break; |
| 281 | |
| 282 | default: |
| 283 | // If other cases arise, then we have to handle them, or we have |
| 284 | // to reject them by returning false in Eh_frame::read_cie. |
| 285 | gold_unreachable(); |
| 286 | } |
| 287 | |
| 288 | gold_assert((fde_encoding & elfcpp::DW_EH_PE_indirect) == 0); |
| 289 | |
| 290 | return pc; |
| 291 | } |
| 292 | |
| 293 | // Given an array of FDE offsets in the .eh_frame section, return an |
| 294 | // array of offsets from the exception frame header to the FDE's |
| 295 | // output PC and to the output address of the FDE itself. We get the |
| 296 | // FDE's PC by actually looking in the .eh_frame section we just wrote |
| 297 | // to the output file. |
| 298 | |
| 299 | template<int size, bool big_endian> |
| 300 | void |
| 301 | Eh_frame_hdr::get_fde_addresses(Output_file* of, |
| 302 | const Fde_offsets* fde_offsets, |
| 303 | Fde_addresses<size>* fde_addresses) |
| 304 | { |
| 305 | typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address; |
| 306 | eh_frame_address = this->eh_frame_section_->address(); |
| 307 | off_t eh_frame_offset = this->eh_frame_section_->offset(); |
| 308 | off_t eh_frame_size = this->eh_frame_section_->data_size(); |
| 309 | const unsigned char* eh_frame_contents = of->get_input_view(eh_frame_offset, |
| 310 | eh_frame_size); |
| 311 | |
| 312 | for (Fde_offsets::const_iterator p = fde_offsets->begin(); |
| 313 | p != fde_offsets->end(); |
| 314 | ++p) |
| 315 | { |
| 316 | typename elfcpp::Elf_types<size>::Elf_Addr fde_pc; |
| 317 | fde_pc = this->get_fde_pc<size, big_endian>(eh_frame_address, |
| 318 | eh_frame_contents, |
| 319 | p->first, p->second); |
| 320 | fde_addresses->push_back(fde_pc, eh_frame_address + p->first); |
| 321 | } |
| 322 | |
| 323 | of->free_input_view(eh_frame_offset, eh_frame_size, eh_frame_contents); |
| 324 | } |
| 325 | |
| 326 | // Class Fde. |
| 327 | |
| 328 | bool |
| 329 | Fde::operator==(const Fde& that) const |
| 330 | { |
| 331 | if (this->object_ != that.object_ |
| 332 | || this->contents_ != that.contents_) |
| 333 | return false; |
| 334 | if (this->object_ == NULL) |
| 335 | return (this->u_.from_linker.plt == that.u_.from_linker.plt |
| 336 | && this->u_.from_linker.post_map == that.u_.from_linker.post_map); |
| 337 | else |
| 338 | return (this->u_.from_object.shndx == that.u_.from_object.shndx |
| 339 | && (this->u_.from_object.input_offset |
| 340 | == that.u_.from_object.input_offset)); |
| 341 | } |
| 342 | |
| 343 | // Write the FDE to OVIEW starting at OFFSET. CIE_OFFSET is the |
| 344 | // offset of the CIE in OVIEW. OUTPUT_OFFSET is the offset of the |
| 345 | // Eh_frame section within the output section. FDE_ENCODING is the |
| 346 | // encoding, from the CIE. ADDRALIGN is the required alignment. |
| 347 | // ADDRESS is the virtual address of OVIEW. Record the FDE pc for |
| 348 | // EH_FRAME_HDR. Return the new offset. |
| 349 | |
| 350 | template<int size, bool big_endian> |
| 351 | section_offset_type |
| 352 | Fde::write(unsigned char* oview, section_offset_type output_offset, |
| 353 | section_offset_type offset, uint64_t address, unsigned int addralign, |
| 354 | section_offset_type cie_offset, unsigned char fde_encoding, |
| 355 | Eh_frame_hdr* eh_frame_hdr) |
| 356 | { |
| 357 | gold_assert((offset & (addralign - 1)) == 0); |
| 358 | |
| 359 | size_t length = this->contents_.length(); |
| 360 | |
| 361 | // We add 8 when getting the aligned length to account for the |
| 362 | // length word and the CIE offset. |
| 363 | size_t aligned_full_length = align_address(length + 8, addralign); |
| 364 | |
| 365 | // Write the length of the FDE as a 32-bit word. The length word |
| 366 | // does not include the four bytes of the length word itself, but it |
| 367 | // does include the offset to the CIE. |
| 368 | elfcpp::Swap<32, big_endian>::writeval(oview + offset, |
| 369 | aligned_full_length - 4); |
| 370 | |
| 371 | // Write the offset to the CIE as a 32-bit word. This is the |
| 372 | // difference between the address of the offset word itself and the |
| 373 | // CIE address. |
| 374 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, |
| 375 | offset + 4 - cie_offset); |
| 376 | |
| 377 | // Copy the rest of the FDE. Note that this is run before |
| 378 | // relocation processing is done on this section, so the relocations |
| 379 | // will later be applied to the FDE data. |
| 380 | memcpy(oview + offset + 8, this->contents_.data(), length); |
| 381 | |
| 382 | // If this FDE is associated with a PLT, fill in the PLT's address |
| 383 | // and size. |
| 384 | if (this->object_ == NULL) |
| 385 | { |
| 386 | gold_assert(memcmp(oview + offset + 8, "\0\0\0\0\0\0\0\0", 8) == 0); |
| 387 | uint64_t paddress; |
| 388 | off_t psize; |
| 389 | parameters->target().plt_fde_location(this->u_.from_linker.plt, |
| 390 | oview + offset + 8, |
| 391 | &paddress, &psize); |
| 392 | uint64_t poffset = paddress - (address + offset + 8); |
| 393 | int32_t spoffset = static_cast<int32_t>(poffset); |
| 394 | uint32_t upsize = static_cast<uint32_t>(psize); |
| 395 | if (static_cast<uint64_t>(static_cast<int64_t>(spoffset)) != poffset |
| 396 | || static_cast<off_t>(upsize) != psize) |
| 397 | gold_warning(_("overflow in PLT unwind data; " |
| 398 | "unwinding through PLT may fail")); |
| 399 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 8, spoffset); |
| 400 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 12, upsize); |
| 401 | } |
| 402 | |
| 403 | if (aligned_full_length > length + 8) |
| 404 | memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8)); |
| 405 | |
| 406 | // Tell the exception frame header about this FDE. |
| 407 | if (eh_frame_hdr != NULL) |
| 408 | eh_frame_hdr->record_fde(output_offset + offset, fde_encoding); |
| 409 | |
| 410 | return offset + aligned_full_length; |
| 411 | } |
| 412 | |
| 413 | // Class Cie. |
| 414 | |
| 415 | // Destructor. |
| 416 | |
| 417 | Cie::~Cie() |
| 418 | { |
| 419 | for (std::vector<Fde*>::iterator p = this->fdes_.begin(); |
| 420 | p != this->fdes_.end(); |
| 421 | ++p) |
| 422 | delete *p; |
| 423 | } |
| 424 | |
| 425 | // Set the output offset of a CIE. Return the new output offset. |
| 426 | |
| 427 | section_offset_type |
| 428 | Cie::set_output_offset(section_offset_type output_offset, |
| 429 | unsigned int addralign, |
| 430 | Output_section_data *output_data) |
| 431 | { |
| 432 | size_t length = this->contents_.length(); |
| 433 | |
| 434 | // Add 4 for length and 4 for zero CIE identifier tag. |
| 435 | length += 8; |
| 436 | |
| 437 | if (this->object_ != NULL) |
| 438 | { |
| 439 | // Add a mapping so that relocations are applied correctly. |
| 440 | this->object_->add_merge_mapping(output_data, this->shndx_, |
| 441 | this->input_offset_, length, |
| 442 | output_offset); |
| 443 | } |
| 444 | |
| 445 | length = align_address(length, addralign); |
| 446 | |
| 447 | for (std::vector<Fde*>::const_iterator p = this->fdes_.begin(); |
| 448 | p != this->fdes_.end(); |
| 449 | ++p) |
| 450 | { |
| 451 | (*p)->add_mapping(output_offset + length, output_data); |
| 452 | |
| 453 | size_t fde_length = (*p)->length(); |
| 454 | fde_length = align_address(fde_length, addralign); |
| 455 | length += fde_length; |
| 456 | } |
| 457 | |
| 458 | return output_offset + length; |
| 459 | } |
| 460 | |
| 461 | // Remove FDE. Only the last FDE using this CIE may be removed. |
| 462 | |
| 463 | void |
| 464 | Cie::remove_fde(const Fde* fde) |
| 465 | { |
| 466 | gold_assert(*fde == *this->fdes_.back()); |
| 467 | this->fdes_.pop_back(); |
| 468 | } |
| 469 | |
| 470 | // Write the CIE to OVIEW starting at OFFSET. OUTPUT_OFFSET is the |
| 471 | // offset of the Eh_frame section within the output section. Round up |
| 472 | // the bytes to ADDRALIGN. ADDRESS is the virtual address of OVIEW. |
| 473 | // EH_FRAME_HDR is the exception frame header for FDE recording. |
| 474 | // POST_FDES stashes FDEs created after mappings were done, for later |
| 475 | // writing. Return the new offset. |
| 476 | |
| 477 | template<int size, bool big_endian> |
| 478 | section_offset_type |
| 479 | Cie::write(unsigned char* oview, section_offset_type output_offset, |
| 480 | section_offset_type offset, uint64_t address, |
| 481 | unsigned int addralign, Eh_frame_hdr* eh_frame_hdr, |
| 482 | Post_fdes* post_fdes) |
| 483 | { |
| 484 | gold_assert((offset & (addralign - 1)) == 0); |
| 485 | |
| 486 | section_offset_type cie_offset = offset; |
| 487 | |
| 488 | size_t length = this->contents_.length(); |
| 489 | |
| 490 | // We add 8 when getting the aligned length to account for the |
| 491 | // length word and the CIE tag. |
| 492 | size_t aligned_full_length = align_address(length + 8, addralign); |
| 493 | |
| 494 | // Write the length of the CIE as a 32-bit word. The length word |
| 495 | // does not include the four bytes of the length word itself. |
| 496 | elfcpp::Swap<32, big_endian>::writeval(oview + offset, |
| 497 | aligned_full_length - 4); |
| 498 | |
| 499 | // Write the tag which marks this as a CIE: a 32-bit zero. |
| 500 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, 0); |
| 501 | |
| 502 | // Write out the CIE data. |
| 503 | memcpy(oview + offset + 8, this->contents_.data(), length); |
| 504 | |
| 505 | if (aligned_full_length > length + 8) |
| 506 | memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8)); |
| 507 | |
| 508 | offset += aligned_full_length; |
| 509 | |
| 510 | // Write out the associated FDEs. |
| 511 | unsigned char fde_encoding = this->fde_encoding_; |
| 512 | for (std::vector<Fde*>::const_iterator p = this->fdes_.begin(); |
| 513 | p != this->fdes_.end(); |
| 514 | ++p) |
| 515 | { |
| 516 | if ((*p)->post_map()) |
| 517 | post_fdes->push_back(Post_fde(*p, cie_offset, fde_encoding)); |
| 518 | else |
| 519 | offset = (*p)->write<size, big_endian>(oview, output_offset, offset, |
| 520 | address, addralign, cie_offset, |
| 521 | fde_encoding, eh_frame_hdr); |
| 522 | } |
| 523 | |
| 524 | return offset; |
| 525 | } |
| 526 | |
| 527 | // We track all the CIEs we see, and merge them when possible. This |
| 528 | // works because each FDE holds an offset to the relevant CIE: we |
| 529 | // rewrite the FDEs to point to the merged CIE. This is worthwhile |
| 530 | // because in a typical C++ program many FDEs in many different object |
| 531 | // files will use the same CIE. |
| 532 | |
| 533 | // An equality operator for Cie. |
| 534 | |
| 535 | bool |
| 536 | operator==(const Cie& cie1, const Cie& cie2) |
| 537 | { |
| 538 | return (cie1.personality_name_ == cie2.personality_name_ |
| 539 | && cie1.contents_ == cie2.contents_); |
| 540 | } |
| 541 | |
| 542 | // A less-than operator for Cie. |
| 543 | |
| 544 | bool |
| 545 | operator<(const Cie& cie1, const Cie& cie2) |
| 546 | { |
| 547 | if (cie1.personality_name_ != cie2.personality_name_) |
| 548 | return cie1.personality_name_ < cie2.personality_name_; |
| 549 | return cie1.contents_ < cie2.contents_; |
| 550 | } |
| 551 | |
| 552 | // Class Eh_frame. |
| 553 | |
| 554 | Eh_frame::Eh_frame() |
| 555 | : Output_section_data(Output_data::default_alignment()), |
| 556 | eh_frame_hdr_(NULL), |
| 557 | cie_offsets_(), |
| 558 | unmergeable_cie_offsets_(), |
| 559 | mappings_are_done_(false), |
| 560 | final_data_size_(0) |
| 561 | { |
| 562 | } |
| 563 | |
| 564 | // Skip an LEB128, updating *PP to point to the next character. |
| 565 | // Return false if we ran off the end of the string. |
| 566 | |
| 567 | bool |
| 568 | Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend) |
| 569 | { |
| 570 | const unsigned char* p; |
| 571 | for (p = *pp; p < pend; ++p) |
| 572 | { |
| 573 | if ((*p & 0x80) == 0) |
| 574 | { |
| 575 | *pp = p + 1; |
| 576 | return true; |
| 577 | } |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | // Add input section SHNDX in OBJECT to an exception frame section. |
| 583 | // SYMBOLS is the contents of the symbol table section (size |
| 584 | // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size |
| 585 | // SYMBOL_NAMES_SIZE). RELOC_SHNDX is the index of a relocation |
| 586 | // section applying to SHNDX, or 0 if none, or -1U if more than one. |
| 587 | // RELOC_TYPE is the type of the reloc section if there is one, either |
| 588 | // SHT_REL or SHT_RELA. We try to parse the input exception frame |
| 589 | // data into our data structures. If we can't do it, we return false |
| 590 | // to mean that the section should be handled as a normal input |
| 591 | // section. |
| 592 | |
| 593 | template<int size, bool big_endian> |
| 594 | Eh_frame::Eh_frame_section_disposition |
| 595 | Eh_frame::add_ehframe_input_section( |
| 596 | Sized_relobj_file<size, big_endian>* object, |
| 597 | const unsigned char* symbols, |
| 598 | section_size_type symbols_size, |
| 599 | const unsigned char* symbol_names, |
| 600 | section_size_type symbol_names_size, |
| 601 | unsigned int shndx, |
| 602 | unsigned int reloc_shndx, |
| 603 | unsigned int reloc_type) |
| 604 | { |
| 605 | // Get the section contents. |
| 606 | section_size_type contents_len; |
| 607 | const unsigned char* pcontents = object->section_contents(shndx, |
| 608 | &contents_len, |
| 609 | false); |
| 610 | if (contents_len == 0) |
| 611 | return EH_EMPTY_SECTION; |
| 612 | |
| 613 | // If this is the marker section for the end of the data, then |
| 614 | // return false to force it to be handled as an ordinary input |
| 615 | // section. If we don't do this, we won't correctly handle the case |
| 616 | // of unrecognized .eh_frame sections. |
| 617 | if (contents_len == 4 |
| 618 | && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0) |
| 619 | return EH_END_MARKER_SECTION; |
| 620 | |
| 621 | New_cies new_cies; |
| 622 | if (!this->do_add_ehframe_input_section(object, symbols, symbols_size, |
| 623 | symbol_names, symbol_names_size, |
| 624 | shndx, reloc_shndx, |
| 625 | reloc_type, pcontents, |
| 626 | contents_len, &new_cies)) |
| 627 | { |
| 628 | if (this->eh_frame_hdr_ != NULL) |
| 629 | this->eh_frame_hdr_->found_unrecognized_eh_frame_section(); |
| 630 | |
| 631 | for (New_cies::iterator p = new_cies.begin(); |
| 632 | p != new_cies.end(); |
| 633 | ++p) |
| 634 | delete p->first; |
| 635 | |
| 636 | return EH_UNRECOGNIZED_SECTION; |
| 637 | } |
| 638 | |
| 639 | // Now that we know we are using this section, record any new CIEs |
| 640 | // that we found. |
| 641 | for (New_cies::const_iterator p = new_cies.begin(); |
| 642 | p != new_cies.end(); |
| 643 | ++p) |
| 644 | { |
| 645 | if (p->second) |
| 646 | this->cie_offsets_.insert(p->first); |
| 647 | else |
| 648 | this->unmergeable_cie_offsets_.push_back(p->first); |
| 649 | } |
| 650 | |
| 651 | return EH_OPTIMIZABLE_SECTION; |
| 652 | } |
| 653 | |
| 654 | // The bulk of the implementation of add_ehframe_input_section. |
| 655 | |
| 656 | template<int size, bool big_endian> |
| 657 | bool |
| 658 | Eh_frame::do_add_ehframe_input_section( |
| 659 | Sized_relobj_file<size, big_endian>* object, |
| 660 | const unsigned char* symbols, |
| 661 | section_size_type symbols_size, |
| 662 | const unsigned char* symbol_names, |
| 663 | section_size_type symbol_names_size, |
| 664 | unsigned int shndx, |
| 665 | unsigned int reloc_shndx, |
| 666 | unsigned int reloc_type, |
| 667 | const unsigned char* pcontents, |
| 668 | section_size_type contents_len, |
| 669 | New_cies* new_cies) |
| 670 | { |
| 671 | Track_relocs<size, big_endian> relocs; |
| 672 | |
| 673 | const unsigned char* p = pcontents; |
| 674 | const unsigned char* pend = p + contents_len; |
| 675 | |
| 676 | // Get the contents of the reloc section if any. |
| 677 | if (!relocs.initialize(object, reloc_shndx, reloc_type)) |
| 678 | return false; |
| 679 | |
| 680 | // Keep track of which CIEs are at which offsets. |
| 681 | Offsets_to_cie cies; |
| 682 | |
| 683 | while (p < pend) |
| 684 | { |
| 685 | if (pend - p < 4) |
| 686 | return false; |
| 687 | |
| 688 | // There shouldn't be any relocations here. |
| 689 | if (relocs.advance(p + 4 - pcontents) > 0) |
| 690 | return false; |
| 691 | |
| 692 | unsigned int len = elfcpp::Swap<32, big_endian>::readval(p); |
| 693 | p += 4; |
| 694 | if (len == 0) |
| 695 | { |
| 696 | // We should only find a zero-length entry at the end of the |
| 697 | // section. |
| 698 | if (p < pend) |
| 699 | return false; |
| 700 | break; |
| 701 | } |
| 702 | // We don't support a 64-bit .eh_frame. |
| 703 | if (len == 0xffffffff) |
| 704 | return false; |
| 705 | if (static_cast<unsigned int>(pend - p) < len) |
| 706 | return false; |
| 707 | |
| 708 | const unsigned char* const pentend = p + len; |
| 709 | |
| 710 | if (pend - p < 4) |
| 711 | return false; |
| 712 | if (relocs.advance(p + 4 - pcontents) > 0) |
| 713 | return false; |
| 714 | |
| 715 | unsigned int id = elfcpp::Swap<32, big_endian>::readval(p); |
| 716 | p += 4; |
| 717 | |
| 718 | if (id == 0) |
| 719 | { |
| 720 | // CIE. |
| 721 | if (!this->read_cie(object, shndx, symbols, symbols_size, |
| 722 | symbol_names, symbol_names_size, |
| 723 | pcontents, p, pentend, &relocs, &cies, |
| 724 | new_cies)) |
| 725 | return false; |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | // FDE. |
| 730 | if (!this->read_fde(object, shndx, symbols, symbols_size, |
| 731 | pcontents, id, p, pentend, &relocs, &cies)) |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | p = pentend; |
| 736 | } |
| 737 | |
| 738 | return true; |
| 739 | } |
| 740 | |
| 741 | // Read a CIE. Return false if we can't parse the information. |
| 742 | |
| 743 | template<int size, bool big_endian> |
| 744 | bool |
| 745 | Eh_frame::read_cie(Sized_relobj_file<size, big_endian>* object, |
| 746 | unsigned int shndx, |
| 747 | const unsigned char* symbols, |
| 748 | section_size_type symbols_size, |
| 749 | const unsigned char* symbol_names, |
| 750 | section_size_type symbol_names_size, |
| 751 | const unsigned char* pcontents, |
| 752 | const unsigned char* pcie, |
| 753 | const unsigned char* pcieend, |
| 754 | Track_relocs<size, big_endian>* relocs, |
| 755 | Offsets_to_cie* cies, |
| 756 | New_cies* new_cies) |
| 757 | { |
| 758 | bool mergeable = true; |
| 759 | |
| 760 | // We need to find the personality routine if there is one, since we |
| 761 | // can only merge CIEs which use the same routine. We also need to |
| 762 | // find the FDE encoding if there is one, so that we can read the PC |
| 763 | // from the FDE. |
| 764 | |
| 765 | const unsigned char* p = pcie; |
| 766 | |
| 767 | if (pcieend - p < 1) |
| 768 | return false; |
| 769 | unsigned char version = *p++; |
| 770 | if (version != 1 && version != 3) |
| 771 | return false; |
| 772 | |
| 773 | const unsigned char* paug = p; |
| 774 | const void* paugendv = memchr(p, '\0', pcieend - p); |
| 775 | const unsigned char* paugend = static_cast<const unsigned char*>(paugendv); |
| 776 | if (paugend == NULL) |
| 777 | return false; |
| 778 | p = paugend + 1; |
| 779 | |
| 780 | if (paug[0] == 'e' && paug[1] == 'h') |
| 781 | { |
| 782 | // This is a CIE from gcc before version 3.0. We can't merge |
| 783 | // these. We can still read the FDEs. |
| 784 | mergeable = false; |
| 785 | paug += 2; |
| 786 | if (*paug != '\0') |
| 787 | return false; |
| 788 | if (pcieend - p < size / 8) |
| 789 | return false; |
| 790 | p += size / 8; |
| 791 | } |
| 792 | |
| 793 | // Skip the code alignment. |
| 794 | if (!skip_leb128(&p, pcieend)) |
| 795 | return false; |
| 796 | |
| 797 | // Skip the data alignment. |
| 798 | if (!skip_leb128(&p, pcieend)) |
| 799 | return false; |
| 800 | |
| 801 | // Skip the return column. |
| 802 | if (version == 1) |
| 803 | { |
| 804 | if (pcieend - p < 1) |
| 805 | return false; |
| 806 | ++p; |
| 807 | } |
| 808 | else |
| 809 | { |
| 810 | if (!skip_leb128(&p, pcieend)) |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | if (*paug == 'z') |
| 815 | { |
| 816 | ++paug; |
| 817 | // Skip the augmentation size. |
| 818 | if (!skip_leb128(&p, pcieend)) |
| 819 | return false; |
| 820 | } |
| 821 | |
| 822 | unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr; |
| 823 | int per_offset = -1; |
| 824 | while (*paug != '\0') |
| 825 | { |
| 826 | switch (*paug) |
| 827 | { |
| 828 | case 'L': // LSDA encoding. |
| 829 | if (pcieend - p < 1) |
| 830 | return false; |
| 831 | ++p; |
| 832 | break; |
| 833 | |
| 834 | case 'R': // FDE encoding. |
| 835 | if (pcieend - p < 1) |
| 836 | return false; |
| 837 | fde_encoding = *p; |
| 838 | switch (fde_encoding & 7) |
| 839 | { |
| 840 | case elfcpp::DW_EH_PE_absptr: |
| 841 | case elfcpp::DW_EH_PE_udata2: |
| 842 | case elfcpp::DW_EH_PE_udata4: |
| 843 | case elfcpp::DW_EH_PE_udata8: |
| 844 | break; |
| 845 | default: |
| 846 | // We don't expect to see any other cases here, and |
| 847 | // we're not prepared to handle them. |
| 848 | return false; |
| 849 | } |
| 850 | ++p; |
| 851 | break; |
| 852 | |
| 853 | case 'S': |
| 854 | break; |
| 855 | |
| 856 | case 'P': |
| 857 | // Personality encoding. |
| 858 | { |
| 859 | if (pcieend - p < 1) |
| 860 | return false; |
| 861 | unsigned char per_encoding = *p; |
| 862 | ++p; |
| 863 | |
| 864 | if ((per_encoding & 0x60) == 0x60) |
| 865 | return false; |
| 866 | unsigned int per_width; |
| 867 | switch (per_encoding & 7) |
| 868 | { |
| 869 | case elfcpp::DW_EH_PE_udata2: |
| 870 | per_width = 2; |
| 871 | break; |
| 872 | case elfcpp::DW_EH_PE_udata4: |
| 873 | per_width = 4; |
| 874 | break; |
| 875 | case elfcpp::DW_EH_PE_udata8: |
| 876 | per_width = 8; |
| 877 | break; |
| 878 | case elfcpp::DW_EH_PE_absptr: |
| 879 | per_width = size / 8; |
| 880 | break; |
| 881 | default: |
| 882 | return false; |
| 883 | } |
| 884 | |
| 885 | if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned) |
| 886 | { |
| 887 | unsigned int len = p - pcie; |
| 888 | len += per_width - 1; |
| 889 | len &= ~ (per_width - 1); |
| 890 | if (static_cast<unsigned int>(pcieend - p) < len) |
| 891 | return false; |
| 892 | p += len; |
| 893 | } |
| 894 | |
| 895 | per_offset = p - pcontents; |
| 896 | |
| 897 | if (static_cast<unsigned int>(pcieend - p) < per_width) |
| 898 | return false; |
| 899 | p += per_width; |
| 900 | } |
| 901 | break; |
| 902 | |
| 903 | default: |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | ++paug; |
| 908 | } |
| 909 | |
| 910 | const char* personality_name = ""; |
| 911 | if (per_offset != -1) |
| 912 | { |
| 913 | if (relocs->advance(per_offset) > 0) |
| 914 | return false; |
| 915 | if (relocs->next_offset() != per_offset) |
| 916 | return false; |
| 917 | |
| 918 | unsigned int personality_symndx = relocs->next_symndx(); |
| 919 | if (personality_symndx == -1U) |
| 920 | return false; |
| 921 | |
| 922 | if (personality_symndx < object->local_symbol_count()) |
| 923 | { |
| 924 | // We can only merge this CIE if the personality routine is |
| 925 | // a global symbol. We can still read the FDEs. |
| 926 | mergeable = false; |
| 927 | } |
| 928 | else |
| 929 | { |
| 930 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 931 | if (personality_symndx >= symbols_size / sym_size) |
| 932 | return false; |
| 933 | elfcpp::Sym<size, big_endian> sym(symbols |
| 934 | + (personality_symndx * sym_size)); |
| 935 | unsigned int name_offset = sym.get_st_name(); |
| 936 | if (name_offset >= symbol_names_size) |
| 937 | return false; |
| 938 | personality_name = (reinterpret_cast<const char*>(symbol_names) |
| 939 | + name_offset); |
| 940 | } |
| 941 | |
| 942 | int r = relocs->advance(per_offset + 1); |
| 943 | gold_assert(r == 1); |
| 944 | } |
| 945 | |
| 946 | if (relocs->advance(pcieend - pcontents) > 0) |
| 947 | return false; |
| 948 | |
| 949 | Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding, |
| 950 | personality_name, pcie, pcieend - pcie); |
| 951 | Cie* cie_pointer = NULL; |
| 952 | if (mergeable) |
| 953 | { |
| 954 | Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie); |
| 955 | if (find_cie != this->cie_offsets_.end()) |
| 956 | cie_pointer = *find_cie; |
| 957 | else |
| 958 | { |
| 959 | // See if we already saw this CIE in this object file. |
| 960 | for (New_cies::const_iterator pc = new_cies->begin(); |
| 961 | pc != new_cies->end(); |
| 962 | ++pc) |
| 963 | { |
| 964 | if (*(pc->first) == cie) |
| 965 | { |
| 966 | cie_pointer = pc->first; |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | if (cie_pointer == NULL) |
| 974 | { |
| 975 | cie_pointer = new Cie(cie); |
| 976 | new_cies->push_back(std::make_pair(cie_pointer, mergeable)); |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | // We are deleting this CIE. Record that in our mapping from |
| 981 | // input sections to the output section. At this point we don't |
| 982 | // know for sure that we are doing a special mapping for this |
| 983 | // input section, but that's OK--if we don't do a special |
| 984 | // mapping, nobody will ever ask for the mapping we add here. |
| 985 | object->add_merge_mapping(this, shndx, (pcie - 8) - pcontents, |
| 986 | pcieend - (pcie - 8), -1); |
| 987 | } |
| 988 | |
| 989 | // Record this CIE plus the offset in the input section. |
| 990 | cies->insert(std::make_pair(pcie - pcontents, cie_pointer)); |
| 991 | |
| 992 | return true; |
| 993 | } |
| 994 | |
| 995 | // Read an FDE. Return false if we can't parse the information. |
| 996 | |
| 997 | template<int size, bool big_endian> |
| 998 | bool |
| 999 | Eh_frame::read_fde(Sized_relobj_file<size, big_endian>* object, |
| 1000 | unsigned int shndx, |
| 1001 | const unsigned char* symbols, |
| 1002 | section_size_type symbols_size, |
| 1003 | const unsigned char* pcontents, |
| 1004 | unsigned int offset, |
| 1005 | const unsigned char* pfde, |
| 1006 | const unsigned char* pfdeend, |
| 1007 | Track_relocs<size, big_endian>* relocs, |
| 1008 | Offsets_to_cie* cies) |
| 1009 | { |
| 1010 | // OFFSET is the distance between the 4 bytes before PFDE to the |
| 1011 | // start of the CIE. The offset we recorded for the CIE is 8 bytes |
| 1012 | // after the start of the CIE--after the length and the zero tag. |
| 1013 | unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8; |
| 1014 | Offsets_to_cie::const_iterator pcie = cies->find(cie_offset); |
| 1015 | if (pcie == cies->end()) |
| 1016 | return false; |
| 1017 | Cie* cie = pcie->second; |
| 1018 | |
| 1019 | int pc_size = 0; |
| 1020 | switch (cie->fde_encoding() & 7) |
| 1021 | { |
| 1022 | case elfcpp::DW_EH_PE_udata2: |
| 1023 | pc_size = 2; |
| 1024 | break; |
| 1025 | case elfcpp::DW_EH_PE_udata4: |
| 1026 | pc_size = 4; |
| 1027 | break; |
| 1028 | case elfcpp::DW_EH_PE_udata8: |
| 1029 | gold_assert(size == 64); |
| 1030 | pc_size = 8; |
| 1031 | break; |
| 1032 | case elfcpp::DW_EH_PE_absptr: |
| 1033 | pc_size = size == 32 ? 4 : 8; |
| 1034 | break; |
| 1035 | default: |
| 1036 | // All other cases were rejected in Eh_frame::read_cie. |
| 1037 | gold_unreachable(); |
| 1038 | } |
| 1039 | |
| 1040 | // The FDE should start with a reloc to the start of the code which |
| 1041 | // it describes. |
| 1042 | if (relocs->advance(pfde - pcontents) > 0) |
| 1043 | return false; |
| 1044 | if (relocs->next_offset() != pfde - pcontents) |
| 1045 | { |
| 1046 | // In an object produced by a relocatable link, gold may have |
| 1047 | // discarded a COMDAT group in the previous link, but not the |
| 1048 | // corresponding FDEs. In that case, gold will have discarded |
| 1049 | // the relocations, so the FDE will have a non-relocatable zero |
| 1050 | // (regardless of whether the PC encoding is absolute, pc-relative, |
| 1051 | // or data-relative) instead of a pointer to the start of the code. |
| 1052 | |
| 1053 | uint64_t pc_value = 0; |
| 1054 | switch (pc_size) |
| 1055 | { |
| 1056 | case 2: |
| 1057 | pc_value = elfcpp::Swap<16, big_endian>::readval(pfde); |
| 1058 | break; |
| 1059 | case 4: |
| 1060 | pc_value = elfcpp::Swap<32, big_endian>::readval(pfde); |
| 1061 | break; |
| 1062 | case 8: |
| 1063 | pc_value = elfcpp::Swap_unaligned<64, big_endian>::readval(pfde); |
| 1064 | break; |
| 1065 | default: |
| 1066 | gold_unreachable(); |
| 1067 | } |
| 1068 | |
| 1069 | if (pc_value == 0) |
| 1070 | { |
| 1071 | // This FDE applies to a discarded function. We |
| 1072 | // can discard this FDE. |
| 1073 | object->add_merge_mapping(this, shndx, (pfde - 8) - pcontents, |
| 1074 | pfdeend - (pfde - 8), -1); |
| 1075 | return true; |
| 1076 | } |
| 1077 | |
| 1078 | // Otherwise, reject the FDE. |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | unsigned int symndx = relocs->next_symndx(); |
| 1083 | if (symndx == -1U) |
| 1084 | return false; |
| 1085 | |
| 1086 | // There can be another reloc in the FDE, if the CIE specifies an |
| 1087 | // LSDA (language specific data area). We currently don't care. We |
| 1088 | // will care later if we want to optimize the LSDA from an absolute |
| 1089 | // pointer to a PC relative offset when generating a shared library. |
| 1090 | relocs->advance(pfdeend - pcontents); |
| 1091 | |
| 1092 | // Find the section index for code that this FDE describes. |
| 1093 | // If we have discarded the section, we can also discard the FDE. |
| 1094 | unsigned int fde_shndx; |
| 1095 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 1096 | if (symndx >= symbols_size / sym_size) |
| 1097 | return false; |
| 1098 | elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size); |
| 1099 | bool is_ordinary; |
| 1100 | fde_shndx = object->adjust_sym_shndx(symndx, sym.get_st_shndx(), |
| 1101 | &is_ordinary); |
| 1102 | bool is_discarded = (is_ordinary |
| 1103 | && fde_shndx != elfcpp::SHN_UNDEF |
| 1104 | && fde_shndx < object->shnum() |
| 1105 | && !object->is_section_included(fde_shndx)); |
| 1106 | |
| 1107 | // Fetch the address range field from the FDE. The offset and size |
| 1108 | // of the field depends on the PC encoding given in the CIE, but |
| 1109 | // it is always an absolute value. If the address range is 0, this |
| 1110 | // FDE corresponds to a function that was discarded during optimization |
| 1111 | // (too late to discard the corresponding FDE). |
| 1112 | uint64_t address_range = 0; |
| 1113 | switch (pc_size) |
| 1114 | { |
| 1115 | case 2: |
| 1116 | address_range = elfcpp::Swap<16, big_endian>::readval(pfde + 2); |
| 1117 | break; |
| 1118 | case 4: |
| 1119 | address_range = elfcpp::Swap<32, big_endian>::readval(pfde + 4); |
| 1120 | break; |
| 1121 | case 8: |
| 1122 | address_range = elfcpp::Swap_unaligned<64, big_endian>::readval(pfde + 8); |
| 1123 | break; |
| 1124 | default: |
| 1125 | gold_unreachable(); |
| 1126 | } |
| 1127 | |
| 1128 | if (is_discarded || address_range == 0) |
| 1129 | { |
| 1130 | // This FDE applies to a discarded function. We |
| 1131 | // can discard this FDE. |
| 1132 | object->add_merge_mapping(this, shndx, (pfde - 8) - pcontents, |
| 1133 | pfdeend - (pfde - 8), -1); |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
| 1137 | cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents, |
| 1138 | pfde, pfdeend - pfde)); |
| 1139 | |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
| 1143 | // Add unwind information for a PLT. |
| 1144 | |
| 1145 | void |
| 1146 | Eh_frame::add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data, |
| 1147 | size_t cie_length, const unsigned char* fde_data, |
| 1148 | size_t fde_length) |
| 1149 | { |
| 1150 | Cie cie(NULL, 0, 0, elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4, "", |
| 1151 | cie_data, cie_length); |
| 1152 | Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie); |
| 1153 | Cie* pcie; |
| 1154 | if (find_cie != this->cie_offsets_.end()) |
| 1155 | pcie = *find_cie; |
| 1156 | else |
| 1157 | { |
| 1158 | gold_assert(!this->mappings_are_done_); |
| 1159 | pcie = new Cie(cie); |
| 1160 | this->cie_offsets_.insert(pcie); |
| 1161 | } |
| 1162 | |
| 1163 | Fde* fde = new Fde(plt, fde_data, fde_length, this->mappings_are_done_); |
| 1164 | pcie->add_fde(fde); |
| 1165 | |
| 1166 | if (this->mappings_are_done_) |
| 1167 | this->final_data_size_ += align_address(fde_length + 8, this->addralign()); |
| 1168 | } |
| 1169 | |
| 1170 | // Remove unwind information for a PLT. Only the last FDE added may be removed. |
| 1171 | |
| 1172 | void |
| 1173 | Eh_frame::remove_ehframe_for_plt(Output_data* plt, |
| 1174 | const unsigned char* cie_data, |
| 1175 | size_t cie_length, |
| 1176 | const unsigned char* fde_data, |
| 1177 | size_t fde_length) |
| 1178 | { |
| 1179 | Cie cie(NULL, 0, 0, elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4, "", |
| 1180 | cie_data, cie_length); |
| 1181 | Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie); |
| 1182 | gold_assert (find_cie != this->cie_offsets_.end()); |
| 1183 | Cie* pcie = *find_cie; |
| 1184 | |
| 1185 | Fde* fde = new Fde(plt, fde_data, fde_length, this->mappings_are_done_); |
| 1186 | pcie->remove_fde(fde); |
| 1187 | |
| 1188 | if (this->mappings_are_done_) |
| 1189 | this->final_data_size_ -= align_address(fde_length + 8, this->addralign()); |
| 1190 | } |
| 1191 | |
| 1192 | // Return the number of FDEs. |
| 1193 | |
| 1194 | unsigned int |
| 1195 | Eh_frame::fde_count() const |
| 1196 | { |
| 1197 | unsigned int ret = 0; |
| 1198 | for (Unmergeable_cie_offsets::const_iterator p = |
| 1199 | this->unmergeable_cie_offsets_.begin(); |
| 1200 | p != this->unmergeable_cie_offsets_.end(); |
| 1201 | ++p) |
| 1202 | ret += (*p)->fde_count(); |
| 1203 | for (Cie_offsets::const_iterator p = this->cie_offsets_.begin(); |
| 1204 | p != this->cie_offsets_.end(); |
| 1205 | ++p) |
| 1206 | ret += (*p)->fde_count(); |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | // Set the final data size. |
| 1211 | |
| 1212 | void |
| 1213 | Eh_frame::set_final_data_size() |
| 1214 | { |
| 1215 | // We can be called more than once if Layout::set_segment_offsets |
| 1216 | // finds a better mapping. We don't want to add all the mappings |
| 1217 | // again. |
| 1218 | if (this->mappings_are_done_) |
| 1219 | { |
| 1220 | this->set_data_size(this->final_data_size_); |
| 1221 | return; |
| 1222 | } |
| 1223 | |
| 1224 | section_offset_type output_start = 0; |
| 1225 | if (this->is_offset_valid()) |
| 1226 | output_start = this->offset() - this->output_section()->offset(); |
| 1227 | section_offset_type output_offset = output_start; |
| 1228 | |
| 1229 | for (Unmergeable_cie_offsets::iterator p = |
| 1230 | this->unmergeable_cie_offsets_.begin(); |
| 1231 | p != this->unmergeable_cie_offsets_.end(); |
| 1232 | ++p) |
| 1233 | output_offset = (*p)->set_output_offset(output_offset, |
| 1234 | this->addralign(), |
| 1235 | this); |
| 1236 | |
| 1237 | for (Cie_offsets::iterator p = this->cie_offsets_.begin(); |
| 1238 | p != this->cie_offsets_.end(); |
| 1239 | ++p) |
| 1240 | output_offset = (*p)->set_output_offset(output_offset, |
| 1241 | this->addralign(), |
| 1242 | this); |
| 1243 | |
| 1244 | this->mappings_are_done_ = true; |
| 1245 | this->final_data_size_ = output_offset - output_start; |
| 1246 | |
| 1247 | gold_assert((output_offset & (this->addralign() - 1)) == 0); |
| 1248 | this->set_data_size(this->final_data_size_); |
| 1249 | } |
| 1250 | |
| 1251 | // Return an output offset for an input offset. |
| 1252 | |
| 1253 | bool |
| 1254 | Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx, |
| 1255 | section_offset_type offset, |
| 1256 | section_offset_type* poutput) const |
| 1257 | { |
| 1258 | return object->merge_output_offset(shndx, offset, poutput); |
| 1259 | } |
| 1260 | |
| 1261 | // Write the data to the output file. |
| 1262 | |
| 1263 | void |
| 1264 | Eh_frame::do_write(Output_file* of) |
| 1265 | { |
| 1266 | const off_t offset = this->offset(); |
| 1267 | const off_t oview_size = this->data_size(); |
| 1268 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
| 1269 | |
| 1270 | switch (parameters->size_and_endianness()) |
| 1271 | { |
| 1272 | #ifdef HAVE_TARGET_32_LITTLE |
| 1273 | case Parameters::TARGET_32_LITTLE: |
| 1274 | this->do_sized_write<32, false>(oview); |
| 1275 | break; |
| 1276 | #endif |
| 1277 | #ifdef HAVE_TARGET_32_BIG |
| 1278 | case Parameters::TARGET_32_BIG: |
| 1279 | this->do_sized_write<32, true>(oview); |
| 1280 | break; |
| 1281 | #endif |
| 1282 | #ifdef HAVE_TARGET_64_LITTLE |
| 1283 | case Parameters::TARGET_64_LITTLE: |
| 1284 | this->do_sized_write<64, false>(oview); |
| 1285 | break; |
| 1286 | #endif |
| 1287 | #ifdef HAVE_TARGET_64_BIG |
| 1288 | case Parameters::TARGET_64_BIG: |
| 1289 | this->do_sized_write<64, true>(oview); |
| 1290 | break; |
| 1291 | #endif |
| 1292 | default: |
| 1293 | gold_unreachable(); |
| 1294 | } |
| 1295 | |
| 1296 | of->write_output_view(offset, oview_size, oview); |
| 1297 | } |
| 1298 | |
| 1299 | // Write the data to the output file--template version. |
| 1300 | |
| 1301 | template<int size, bool big_endian> |
| 1302 | void |
| 1303 | Eh_frame::do_sized_write(unsigned char* oview) |
| 1304 | { |
| 1305 | uint64_t address = this->address(); |
| 1306 | unsigned int addralign = this->addralign(); |
| 1307 | section_offset_type o = 0; |
| 1308 | const off_t output_offset = this->offset() - this->output_section()->offset(); |
| 1309 | Post_fdes post_fdes; |
| 1310 | for (Unmergeable_cie_offsets::iterator p = |
| 1311 | this->unmergeable_cie_offsets_.begin(); |
| 1312 | p != this->unmergeable_cie_offsets_.end(); |
| 1313 | ++p) |
| 1314 | o = (*p)->write<size, big_endian>(oview, output_offset, o, address, |
| 1315 | addralign, this->eh_frame_hdr_, |
| 1316 | &post_fdes); |
| 1317 | for (Cie_offsets::iterator p = this->cie_offsets_.begin(); |
| 1318 | p != this->cie_offsets_.end(); |
| 1319 | ++p) |
| 1320 | o = (*p)->write<size, big_endian>(oview, output_offset, o, address, |
| 1321 | addralign, this->eh_frame_hdr_, |
| 1322 | &post_fdes); |
| 1323 | for (Post_fdes::iterator p = post_fdes.begin(); |
| 1324 | p != post_fdes.end(); |
| 1325 | ++p) |
| 1326 | o = (*p).fde->write<size, big_endian>(oview, output_offset, o, address, |
| 1327 | addralign, (*p).cie_offset, |
| 1328 | (*p).fde_encoding, |
| 1329 | this->eh_frame_hdr_); |
| 1330 | } |
| 1331 | |
| 1332 | #ifdef HAVE_TARGET_32_LITTLE |
| 1333 | template |
| 1334 | Eh_frame::Eh_frame_section_disposition |
| 1335 | Eh_frame::add_ehframe_input_section<32, false>( |
| 1336 | Sized_relobj_file<32, false>* object, |
| 1337 | const unsigned char* symbols, |
| 1338 | section_size_type symbols_size, |
| 1339 | const unsigned char* symbol_names, |
| 1340 | section_size_type symbol_names_size, |
| 1341 | unsigned int shndx, |
| 1342 | unsigned int reloc_shndx, |
| 1343 | unsigned int reloc_type); |
| 1344 | #endif |
| 1345 | |
| 1346 | #ifdef HAVE_TARGET_32_BIG |
| 1347 | template |
| 1348 | Eh_frame::Eh_frame_section_disposition |
| 1349 | Eh_frame::add_ehframe_input_section<32, true>( |
| 1350 | Sized_relobj_file<32, true>* object, |
| 1351 | const unsigned char* symbols, |
| 1352 | section_size_type symbols_size, |
| 1353 | const unsigned char* symbol_names, |
| 1354 | section_size_type symbol_names_size, |
| 1355 | unsigned int shndx, |
| 1356 | unsigned int reloc_shndx, |
| 1357 | unsigned int reloc_type); |
| 1358 | #endif |
| 1359 | |
| 1360 | #ifdef HAVE_TARGET_64_LITTLE |
| 1361 | template |
| 1362 | Eh_frame::Eh_frame_section_disposition |
| 1363 | Eh_frame::add_ehframe_input_section<64, false>( |
| 1364 | Sized_relobj_file<64, false>* object, |
| 1365 | const unsigned char* symbols, |
| 1366 | section_size_type symbols_size, |
| 1367 | const unsigned char* symbol_names, |
| 1368 | section_size_type symbol_names_size, |
| 1369 | unsigned int shndx, |
| 1370 | unsigned int reloc_shndx, |
| 1371 | unsigned int reloc_type); |
| 1372 | #endif |
| 1373 | |
| 1374 | #ifdef HAVE_TARGET_64_BIG |
| 1375 | template |
| 1376 | Eh_frame::Eh_frame_section_disposition |
| 1377 | Eh_frame::add_ehframe_input_section<64, true>( |
| 1378 | Sized_relobj_file<64, true>* object, |
| 1379 | const unsigned char* symbols, |
| 1380 | section_size_type symbols_size, |
| 1381 | const unsigned char* symbol_names, |
| 1382 | section_size_type symbol_names_size, |
| 1383 | unsigned int shndx, |
| 1384 | unsigned int reloc_shndx, |
| 1385 | unsigned int reloc_type); |
| 1386 | #endif |
| 1387 | |
| 1388 | } // End namespace gold. |