| 1 | // output.h -- manage the output file for gold -*- C++ -*- |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008, 2009, 2010 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 | #ifndef GOLD_OUTPUT_H |
| 24 | #define GOLD_OUTPUT_H |
| 25 | |
| 26 | #include <list> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "elfcpp.h" |
| 30 | #include "mapfile.h" |
| 31 | #include "layout.h" |
| 32 | #include "reloc-types.h" |
| 33 | |
| 34 | namespace gold |
| 35 | { |
| 36 | |
| 37 | class General_options; |
| 38 | class Object; |
| 39 | class Symbol; |
| 40 | class Output_file; |
| 41 | class Output_merge_base; |
| 42 | class Output_section; |
| 43 | class Relocatable_relocs; |
| 44 | class Target; |
| 45 | template<int size, bool big_endian> |
| 46 | class Sized_target; |
| 47 | template<int size, bool big_endian> |
| 48 | class Sized_relobj; |
| 49 | |
| 50 | // An abtract class for data which has to go into the output file. |
| 51 | |
| 52 | class Output_data |
| 53 | { |
| 54 | public: |
| 55 | explicit Output_data() |
| 56 | : address_(0), data_size_(0), offset_(-1), |
| 57 | is_address_valid_(false), is_data_size_valid_(false), |
| 58 | is_offset_valid_(false), is_data_size_fixed_(false), |
| 59 | has_dynamic_reloc_(false) |
| 60 | { } |
| 61 | |
| 62 | virtual |
| 63 | ~Output_data(); |
| 64 | |
| 65 | // Return the address. For allocated sections, this is only valid |
| 66 | // after Layout::finalize is finished. |
| 67 | uint64_t |
| 68 | address() const |
| 69 | { |
| 70 | gold_assert(this->is_address_valid_); |
| 71 | return this->address_; |
| 72 | } |
| 73 | |
| 74 | // Return the size of the data. For allocated sections, this must |
| 75 | // be valid after Layout::finalize calls set_address, but need not |
| 76 | // be valid before then. |
| 77 | off_t |
| 78 | data_size() const |
| 79 | { |
| 80 | gold_assert(this->is_data_size_valid_); |
| 81 | return this->data_size_; |
| 82 | } |
| 83 | |
| 84 | // Return true if data size is fixed. |
| 85 | bool |
| 86 | is_data_size_fixed() const |
| 87 | { return this->is_data_size_fixed_; } |
| 88 | |
| 89 | // Return the file offset. This is only valid after |
| 90 | // Layout::finalize is finished. For some non-allocated sections, |
| 91 | // it may not be valid until near the end of the link. |
| 92 | off_t |
| 93 | offset() const |
| 94 | { |
| 95 | gold_assert(this->is_offset_valid_); |
| 96 | return this->offset_; |
| 97 | } |
| 98 | |
| 99 | // Reset the address and file offset. This essentially disables the |
| 100 | // sanity testing about duplicate and unknown settings. |
| 101 | void |
| 102 | reset_address_and_file_offset() |
| 103 | { |
| 104 | this->is_address_valid_ = false; |
| 105 | this->is_offset_valid_ = false; |
| 106 | if (!this->is_data_size_fixed_) |
| 107 | this->is_data_size_valid_ = false; |
| 108 | this->do_reset_address_and_file_offset(); |
| 109 | } |
| 110 | |
| 111 | // Return true if address and file offset already have reset values. In |
| 112 | // other words, calling reset_address_and_file_offset will not change them. |
| 113 | bool |
| 114 | address_and_file_offset_have_reset_values() const |
| 115 | { return this->do_address_and_file_offset_have_reset_values(); } |
| 116 | |
| 117 | // Return the required alignment. |
| 118 | uint64_t |
| 119 | addralign() const |
| 120 | { return this->do_addralign(); } |
| 121 | |
| 122 | // Return whether this has a load address. |
| 123 | bool |
| 124 | has_load_address() const |
| 125 | { return this->do_has_load_address(); } |
| 126 | |
| 127 | // Return the load address. |
| 128 | uint64_t |
| 129 | load_address() const |
| 130 | { return this->do_load_address(); } |
| 131 | |
| 132 | // Return whether this is an Output_section. |
| 133 | bool |
| 134 | is_section() const |
| 135 | { return this->do_is_section(); } |
| 136 | |
| 137 | // Return whether this is an Output_section of the specified type. |
| 138 | bool |
| 139 | is_section_type(elfcpp::Elf_Word stt) const |
| 140 | { return this->do_is_section_type(stt); } |
| 141 | |
| 142 | // Return whether this is an Output_section with the specified flag |
| 143 | // set. |
| 144 | bool |
| 145 | is_section_flag_set(elfcpp::Elf_Xword shf) const |
| 146 | { return this->do_is_section_flag_set(shf); } |
| 147 | |
| 148 | // Return the output section that this goes in, if there is one. |
| 149 | Output_section* |
| 150 | output_section() |
| 151 | { return this->do_output_section(); } |
| 152 | |
| 153 | const Output_section* |
| 154 | output_section() const |
| 155 | { return this->do_output_section(); } |
| 156 | |
| 157 | // Return the output section index, if there is an output section. |
| 158 | unsigned int |
| 159 | out_shndx() const |
| 160 | { return this->do_out_shndx(); } |
| 161 | |
| 162 | // Set the output section index, if this is an output section. |
| 163 | void |
| 164 | set_out_shndx(unsigned int shndx) |
| 165 | { this->do_set_out_shndx(shndx); } |
| 166 | |
| 167 | // Set the address and file offset of this data, and finalize the |
| 168 | // size of the data. This is called during Layout::finalize for |
| 169 | // allocated sections. |
| 170 | void |
| 171 | set_address_and_file_offset(uint64_t addr, off_t off) |
| 172 | { |
| 173 | this->set_address(addr); |
| 174 | this->set_file_offset(off); |
| 175 | this->finalize_data_size(); |
| 176 | } |
| 177 | |
| 178 | // Set the address. |
| 179 | void |
| 180 | set_address(uint64_t addr) |
| 181 | { |
| 182 | gold_assert(!this->is_address_valid_); |
| 183 | this->address_ = addr; |
| 184 | this->is_address_valid_ = true; |
| 185 | } |
| 186 | |
| 187 | // Set the file offset. |
| 188 | void |
| 189 | set_file_offset(off_t off) |
| 190 | { |
| 191 | gold_assert(!this->is_offset_valid_); |
| 192 | this->offset_ = off; |
| 193 | this->is_offset_valid_ = true; |
| 194 | } |
| 195 | |
| 196 | // Finalize the data size. |
| 197 | void |
| 198 | finalize_data_size() |
| 199 | { |
| 200 | if (!this->is_data_size_valid_) |
| 201 | { |
| 202 | // Tell the child class to set the data size. |
| 203 | this->set_final_data_size(); |
| 204 | gold_assert(this->is_data_size_valid_); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Set the TLS offset. Called only for SHT_TLS sections. |
| 209 | void |
| 210 | set_tls_offset(uint64_t tls_base) |
| 211 | { this->do_set_tls_offset(tls_base); } |
| 212 | |
| 213 | // Return the TLS offset, relative to the base of the TLS segment. |
| 214 | // Valid only for SHT_TLS sections. |
| 215 | uint64_t |
| 216 | tls_offset() const |
| 217 | { return this->do_tls_offset(); } |
| 218 | |
| 219 | // Write the data to the output file. This is called after |
| 220 | // Layout::finalize is complete. |
| 221 | void |
| 222 | write(Output_file* file) |
| 223 | { this->do_write(file); } |
| 224 | |
| 225 | // This is called by Layout::finalize to note that the sizes of |
| 226 | // allocated sections must now be fixed. |
| 227 | static void |
| 228 | layout_complete() |
| 229 | { Output_data::allocated_sizes_are_fixed = true; } |
| 230 | |
| 231 | // Used to check that layout has been done. |
| 232 | static bool |
| 233 | is_layout_complete() |
| 234 | { return Output_data::allocated_sizes_are_fixed; } |
| 235 | |
| 236 | // Note that a dynamic reloc has been applied to this data. |
| 237 | void |
| 238 | add_dynamic_reloc() |
| 239 | { this->has_dynamic_reloc_ = true; } |
| 240 | |
| 241 | // Return whether a dynamic reloc has been applied. |
| 242 | bool |
| 243 | has_dynamic_reloc() const |
| 244 | { return this->has_dynamic_reloc_; } |
| 245 | |
| 246 | // Whether the address is valid. |
| 247 | bool |
| 248 | is_address_valid() const |
| 249 | { return this->is_address_valid_; } |
| 250 | |
| 251 | // Whether the file offset is valid. |
| 252 | bool |
| 253 | is_offset_valid() const |
| 254 | { return this->is_offset_valid_; } |
| 255 | |
| 256 | // Whether the data size is valid. |
| 257 | bool |
| 258 | is_data_size_valid() const |
| 259 | { return this->is_data_size_valid_; } |
| 260 | |
| 261 | // Print information to the map file. |
| 262 | void |
| 263 | print_to_mapfile(Mapfile* mapfile) const |
| 264 | { return this->do_print_to_mapfile(mapfile); } |
| 265 | |
| 266 | protected: |
| 267 | // Functions that child classes may or in some cases must implement. |
| 268 | |
| 269 | // Write the data to the output file. |
| 270 | virtual void |
| 271 | do_write(Output_file*) = 0; |
| 272 | |
| 273 | // Return the required alignment. |
| 274 | virtual uint64_t |
| 275 | do_addralign() const = 0; |
| 276 | |
| 277 | // Return whether this has a load address. |
| 278 | virtual bool |
| 279 | do_has_load_address() const |
| 280 | { return false; } |
| 281 | |
| 282 | // Return the load address. |
| 283 | virtual uint64_t |
| 284 | do_load_address() const |
| 285 | { gold_unreachable(); } |
| 286 | |
| 287 | // Return whether this is an Output_section. |
| 288 | virtual bool |
| 289 | do_is_section() const |
| 290 | { return false; } |
| 291 | |
| 292 | // Return whether this is an Output_section of the specified type. |
| 293 | // This only needs to be implement by Output_section. |
| 294 | virtual bool |
| 295 | do_is_section_type(elfcpp::Elf_Word) const |
| 296 | { return false; } |
| 297 | |
| 298 | // Return whether this is an Output_section with the specific flag |
| 299 | // set. This only needs to be implemented by Output_section. |
| 300 | virtual bool |
| 301 | do_is_section_flag_set(elfcpp::Elf_Xword) const |
| 302 | { return false; } |
| 303 | |
| 304 | // Return the output section, if there is one. |
| 305 | virtual Output_section* |
| 306 | do_output_section() |
| 307 | { return NULL; } |
| 308 | |
| 309 | virtual const Output_section* |
| 310 | do_output_section() const |
| 311 | { return NULL; } |
| 312 | |
| 313 | // Return the output section index, if there is an output section. |
| 314 | virtual unsigned int |
| 315 | do_out_shndx() const |
| 316 | { gold_unreachable(); } |
| 317 | |
| 318 | // Set the output section index, if this is an output section. |
| 319 | virtual void |
| 320 | do_set_out_shndx(unsigned int) |
| 321 | { gold_unreachable(); } |
| 322 | |
| 323 | // This is a hook for derived classes to set the data size. This is |
| 324 | // called by finalize_data_size, normally called during |
| 325 | // Layout::finalize, when the section address is set. |
| 326 | virtual void |
| 327 | set_final_data_size() |
| 328 | { gold_unreachable(); } |
| 329 | |
| 330 | // A hook for resetting the address and file offset. |
| 331 | virtual void |
| 332 | do_reset_address_and_file_offset() |
| 333 | { } |
| 334 | |
| 335 | // Return true if address and file offset already have reset values. In |
| 336 | // other words, calling reset_address_and_file_offset will not change them. |
| 337 | // A child class overriding do_reset_address_and_file_offset may need to |
| 338 | // also override this. |
| 339 | virtual bool |
| 340 | do_address_and_file_offset_have_reset_values() const |
| 341 | { return !this->is_address_valid_ && !this->is_offset_valid_; } |
| 342 | |
| 343 | // Set the TLS offset. Called only for SHT_TLS sections. |
| 344 | virtual void |
| 345 | do_set_tls_offset(uint64_t) |
| 346 | { gold_unreachable(); } |
| 347 | |
| 348 | // Return the TLS offset, relative to the base of the TLS segment. |
| 349 | // Valid only for SHT_TLS sections. |
| 350 | virtual uint64_t |
| 351 | do_tls_offset() const |
| 352 | { gold_unreachable(); } |
| 353 | |
| 354 | // Print to the map file. This only needs to be implemented by |
| 355 | // classes which may appear in a PT_LOAD segment. |
| 356 | virtual void |
| 357 | do_print_to_mapfile(Mapfile*) const |
| 358 | { gold_unreachable(); } |
| 359 | |
| 360 | // Functions that child classes may call. |
| 361 | |
| 362 | // Reset the address. The Output_section class needs this when an |
| 363 | // SHF_ALLOC input section is added to an output section which was |
| 364 | // formerly not SHF_ALLOC. |
| 365 | void |
| 366 | mark_address_invalid() |
| 367 | { this->is_address_valid_ = false; } |
| 368 | |
| 369 | // Set the size of the data. |
| 370 | void |
| 371 | set_data_size(off_t data_size) |
| 372 | { |
| 373 | gold_assert(!this->is_data_size_valid_ |
| 374 | && !this->is_data_size_fixed_); |
| 375 | this->data_size_ = data_size; |
| 376 | this->is_data_size_valid_ = true; |
| 377 | } |
| 378 | |
| 379 | // Fix the data size. Once it is fixed, it cannot be changed |
| 380 | // and the data size remains always valid. |
| 381 | void |
| 382 | fix_data_size() |
| 383 | { |
| 384 | gold_assert(this->is_data_size_valid_); |
| 385 | this->is_data_size_fixed_ = true; |
| 386 | } |
| 387 | |
| 388 | // Get the current data size--this is for the convenience of |
| 389 | // sections which build up their size over time. |
| 390 | off_t |
| 391 | current_data_size_for_child() const |
| 392 | { return this->data_size_; } |
| 393 | |
| 394 | // Set the current data size--this is for the convenience of |
| 395 | // sections which build up their size over time. |
| 396 | void |
| 397 | set_current_data_size_for_child(off_t data_size) |
| 398 | { |
| 399 | gold_assert(!this->is_data_size_valid_); |
| 400 | this->data_size_ = data_size; |
| 401 | } |
| 402 | |
| 403 | // Return default alignment for the target size. |
| 404 | static uint64_t |
| 405 | default_alignment(); |
| 406 | |
| 407 | // Return default alignment for a specified size--32 or 64. |
| 408 | static uint64_t |
| 409 | default_alignment_for_size(int size); |
| 410 | |
| 411 | private: |
| 412 | Output_data(const Output_data&); |
| 413 | Output_data& operator=(const Output_data&); |
| 414 | |
| 415 | // This is used for verification, to make sure that we don't try to |
| 416 | // change any sizes of allocated sections after we set the section |
| 417 | // addresses. |
| 418 | static bool allocated_sizes_are_fixed; |
| 419 | |
| 420 | // Memory address in output file. |
| 421 | uint64_t address_; |
| 422 | // Size of data in output file. |
| 423 | off_t data_size_; |
| 424 | // File offset of contents in output file. |
| 425 | off_t offset_; |
| 426 | // Whether address_ is valid. |
| 427 | bool is_address_valid_ : 1; |
| 428 | // Whether data_size_ is valid. |
| 429 | bool is_data_size_valid_ : 1; |
| 430 | // Whether offset_ is valid. |
| 431 | bool is_offset_valid_ : 1; |
| 432 | // Whether data size is fixed. |
| 433 | bool is_data_size_fixed_ : 1; |
| 434 | // Whether any dynamic relocs have been applied to this section. |
| 435 | bool has_dynamic_reloc_ : 1; |
| 436 | }; |
| 437 | |
| 438 | // Output the section headers. |
| 439 | |
| 440 | class Output_section_headers : public Output_data |
| 441 | { |
| 442 | public: |
| 443 | Output_section_headers(const Layout*, |
| 444 | const Layout::Segment_list*, |
| 445 | const Layout::Section_list*, |
| 446 | const Layout::Section_list*, |
| 447 | const Stringpool*, |
| 448 | const Output_section*); |
| 449 | |
| 450 | protected: |
| 451 | // Write the data to the file. |
| 452 | void |
| 453 | do_write(Output_file*); |
| 454 | |
| 455 | // Return the required alignment. |
| 456 | uint64_t |
| 457 | do_addralign() const |
| 458 | { return Output_data::default_alignment(); } |
| 459 | |
| 460 | // Write to a map file. |
| 461 | void |
| 462 | do_print_to_mapfile(Mapfile* mapfile) const |
| 463 | { mapfile->print_output_data(this, _("** section headers")); } |
| 464 | |
| 465 | // Set final data size. |
| 466 | void |
| 467 | set_final_data_size() |
| 468 | { this->set_data_size(this->do_size()); } |
| 469 | |
| 470 | private: |
| 471 | // Write the data to the file with the right size and endianness. |
| 472 | template<int size, bool big_endian> |
| 473 | void |
| 474 | do_sized_write(Output_file*); |
| 475 | |
| 476 | // Compute data size. |
| 477 | off_t |
| 478 | do_size() const; |
| 479 | |
| 480 | const Layout* layout_; |
| 481 | const Layout::Segment_list* segment_list_; |
| 482 | const Layout::Section_list* section_list_; |
| 483 | const Layout::Section_list* unattached_section_list_; |
| 484 | const Stringpool* secnamepool_; |
| 485 | const Output_section* shstrtab_section_; |
| 486 | }; |
| 487 | |
| 488 | // Output the segment headers. |
| 489 | |
| 490 | class Output_segment_headers : public Output_data |
| 491 | { |
| 492 | public: |
| 493 | Output_segment_headers(const Layout::Segment_list& segment_list); |
| 494 | |
| 495 | protected: |
| 496 | // Write the data to the file. |
| 497 | void |
| 498 | do_write(Output_file*); |
| 499 | |
| 500 | // Return the required alignment. |
| 501 | uint64_t |
| 502 | do_addralign() const |
| 503 | { return Output_data::default_alignment(); } |
| 504 | |
| 505 | // Write to a map file. |
| 506 | void |
| 507 | do_print_to_mapfile(Mapfile* mapfile) const |
| 508 | { mapfile->print_output_data(this, _("** segment headers")); } |
| 509 | |
| 510 | // Set final data size. |
| 511 | void |
| 512 | set_final_data_size() |
| 513 | { this->set_data_size(this->do_size()); } |
| 514 | |
| 515 | private: |
| 516 | // Write the data to the file with the right size and endianness. |
| 517 | template<int size, bool big_endian> |
| 518 | void |
| 519 | do_sized_write(Output_file*); |
| 520 | |
| 521 | // Compute the current size. |
| 522 | off_t |
| 523 | do_size() const; |
| 524 | |
| 525 | const Layout::Segment_list& segment_list_; |
| 526 | }; |
| 527 | |
| 528 | // Output the ELF file header. |
| 529 | |
| 530 | class Output_file_header : public Output_data |
| 531 | { |
| 532 | public: |
| 533 | Output_file_header(const Target*, |
| 534 | const Symbol_table*, |
| 535 | const Output_segment_headers*, |
| 536 | const char* entry); |
| 537 | |
| 538 | // Add information about the section headers. We lay out the ELF |
| 539 | // file header before we create the section headers. |
| 540 | void set_section_info(const Output_section_headers*, |
| 541 | const Output_section* shstrtab); |
| 542 | |
| 543 | protected: |
| 544 | // Write the data to the file. |
| 545 | void |
| 546 | do_write(Output_file*); |
| 547 | |
| 548 | // Return the required alignment. |
| 549 | uint64_t |
| 550 | do_addralign() const |
| 551 | { return Output_data::default_alignment(); } |
| 552 | |
| 553 | // Write to a map file. |
| 554 | void |
| 555 | do_print_to_mapfile(Mapfile* mapfile) const |
| 556 | { mapfile->print_output_data(this, _("** file header")); } |
| 557 | |
| 558 | // Set final data size. |
| 559 | void |
| 560 | set_final_data_size(void) |
| 561 | { this->set_data_size(this->do_size()); } |
| 562 | |
| 563 | private: |
| 564 | // Write the data to the file with the right size and endianness. |
| 565 | template<int size, bool big_endian> |
| 566 | void |
| 567 | do_sized_write(Output_file*); |
| 568 | |
| 569 | // Return the value to use for the entry address. |
| 570 | template<int size> |
| 571 | typename elfcpp::Elf_types<size>::Elf_Addr |
| 572 | entry(); |
| 573 | |
| 574 | // Compute the current data size. |
| 575 | off_t |
| 576 | do_size() const; |
| 577 | |
| 578 | const Target* target_; |
| 579 | const Symbol_table* symtab_; |
| 580 | const Output_segment_headers* segment_header_; |
| 581 | const Output_section_headers* section_header_; |
| 582 | const Output_section* shstrtab_; |
| 583 | const char* entry_; |
| 584 | }; |
| 585 | |
| 586 | // Output sections are mainly comprised of input sections. However, |
| 587 | // there are cases where we have data to write out which is not in an |
| 588 | // input section. Output_section_data is used in such cases. This is |
| 589 | // an abstract base class. |
| 590 | |
| 591 | class Output_section_data : public Output_data |
| 592 | { |
| 593 | public: |
| 594 | Output_section_data(off_t data_size, uint64_t addralign, |
| 595 | bool is_data_size_fixed) |
| 596 | : Output_data(), output_section_(NULL), addralign_(addralign) |
| 597 | { |
| 598 | this->set_data_size(data_size); |
| 599 | if (is_data_size_fixed) |
| 600 | this->fix_data_size(); |
| 601 | } |
| 602 | |
| 603 | Output_section_data(uint64_t addralign) |
| 604 | : Output_data(), output_section_(NULL), addralign_(addralign) |
| 605 | { } |
| 606 | |
| 607 | // Return the output section. |
| 608 | Output_section* |
| 609 | output_section() |
| 610 | { return this->output_section_; } |
| 611 | |
| 612 | const Output_section* |
| 613 | output_section() const |
| 614 | { return this->output_section_; } |
| 615 | |
| 616 | // Record the output section. |
| 617 | void |
| 618 | set_output_section(Output_section* os); |
| 619 | |
| 620 | // Add an input section, for SHF_MERGE sections. This returns true |
| 621 | // if the section was handled. |
| 622 | bool |
| 623 | add_input_section(Relobj* object, unsigned int shndx) |
| 624 | { return this->do_add_input_section(object, shndx); } |
| 625 | |
| 626 | // Given an input OBJECT, an input section index SHNDX within that |
| 627 | // object, and an OFFSET relative to the start of that input |
| 628 | // section, return whether or not the corresponding offset within |
| 629 | // the output section is known. If this function returns true, it |
| 630 | // sets *POUTPUT to the output offset. The value -1 indicates that |
| 631 | // this input offset is being discarded. |
| 632 | bool |
| 633 | output_offset(const Relobj* object, unsigned int shndx, |
| 634 | section_offset_type offset, |
| 635 | section_offset_type* poutput) const |
| 636 | { return this->do_output_offset(object, shndx, offset, poutput); } |
| 637 | |
| 638 | // Return whether this is the merge section for the input section |
| 639 | // SHNDX in OBJECT. This should return true when output_offset |
| 640 | // would return true for some values of OFFSET. |
| 641 | bool |
| 642 | is_merge_section_for(const Relobj* object, unsigned int shndx) const |
| 643 | { return this->do_is_merge_section_for(object, shndx); } |
| 644 | |
| 645 | // Write the contents to a buffer. This is used for sections which |
| 646 | // require postprocessing, such as compression. |
| 647 | void |
| 648 | write_to_buffer(unsigned char* buffer) |
| 649 | { this->do_write_to_buffer(buffer); } |
| 650 | |
| 651 | // Print merge stats to stderr. This should only be called for |
| 652 | // SHF_MERGE sections. |
| 653 | void |
| 654 | print_merge_stats(const char* section_name) |
| 655 | { this->do_print_merge_stats(section_name); } |
| 656 | |
| 657 | protected: |
| 658 | // The child class must implement do_write. |
| 659 | |
| 660 | // The child class may implement specific adjustments to the output |
| 661 | // section. |
| 662 | virtual void |
| 663 | do_adjust_output_section(Output_section*) |
| 664 | { } |
| 665 | |
| 666 | // May be implemented by child class. Return true if the section |
| 667 | // was handled. |
| 668 | virtual bool |
| 669 | do_add_input_section(Relobj*, unsigned int) |
| 670 | { gold_unreachable(); } |
| 671 | |
| 672 | // The child class may implement output_offset. |
| 673 | virtual bool |
| 674 | do_output_offset(const Relobj*, unsigned int, section_offset_type, |
| 675 | section_offset_type*) const |
| 676 | { return false; } |
| 677 | |
| 678 | // The child class may implement is_merge_section_for. |
| 679 | virtual bool |
| 680 | do_is_merge_section_for(const Relobj*, unsigned int) const |
| 681 | { return false; } |
| 682 | |
| 683 | // The child class may implement write_to_buffer. Most child |
| 684 | // classes can not appear in a compressed section, and they do not |
| 685 | // implement this. |
| 686 | virtual void |
| 687 | do_write_to_buffer(unsigned char*) |
| 688 | { gold_unreachable(); } |
| 689 | |
| 690 | // Print merge statistics. |
| 691 | virtual void |
| 692 | do_print_merge_stats(const char*) |
| 693 | { gold_unreachable(); } |
| 694 | |
| 695 | // Return the required alignment. |
| 696 | uint64_t |
| 697 | do_addralign() const |
| 698 | { return this->addralign_; } |
| 699 | |
| 700 | // Return the output section. |
| 701 | Output_section* |
| 702 | do_output_section() |
| 703 | { return this->output_section_; } |
| 704 | |
| 705 | const Output_section* |
| 706 | do_output_section() const |
| 707 | { return this->output_section_; } |
| 708 | |
| 709 | // Return the section index of the output section. |
| 710 | unsigned int |
| 711 | do_out_shndx() const; |
| 712 | |
| 713 | // Set the alignment. |
| 714 | void |
| 715 | set_addralign(uint64_t addralign); |
| 716 | |
| 717 | private: |
| 718 | // The output section for this section. |
| 719 | Output_section* output_section_; |
| 720 | // The required alignment. |
| 721 | uint64_t addralign_; |
| 722 | }; |
| 723 | |
| 724 | // Some Output_section_data classes build up their data step by step, |
| 725 | // rather than all at once. This class provides an interface for |
| 726 | // them. |
| 727 | |
| 728 | class Output_section_data_build : public Output_section_data |
| 729 | { |
| 730 | public: |
| 731 | Output_section_data_build(uint64_t addralign) |
| 732 | : Output_section_data(addralign) |
| 733 | { } |
| 734 | |
| 735 | // Get the current data size. |
| 736 | off_t |
| 737 | current_data_size() const |
| 738 | { return this->current_data_size_for_child(); } |
| 739 | |
| 740 | // Set the current data size. |
| 741 | void |
| 742 | set_current_data_size(off_t data_size) |
| 743 | { this->set_current_data_size_for_child(data_size); } |
| 744 | |
| 745 | protected: |
| 746 | // Set the final data size. |
| 747 | virtual void |
| 748 | set_final_data_size() |
| 749 | { this->set_data_size(this->current_data_size_for_child()); } |
| 750 | }; |
| 751 | |
| 752 | // A simple case of Output_data in which we have constant data to |
| 753 | // output. |
| 754 | |
| 755 | class Output_data_const : public Output_section_data |
| 756 | { |
| 757 | public: |
| 758 | Output_data_const(const std::string& data, uint64_t addralign) |
| 759 | : Output_section_data(data.size(), addralign, true), data_(data) |
| 760 | { } |
| 761 | |
| 762 | Output_data_const(const char* p, off_t len, uint64_t addralign) |
| 763 | : Output_section_data(len, addralign, true), data_(p, len) |
| 764 | { } |
| 765 | |
| 766 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) |
| 767 | : Output_section_data(len, addralign, true), |
| 768 | data_(reinterpret_cast<const char*>(p), len) |
| 769 | { } |
| 770 | |
| 771 | protected: |
| 772 | // Write the data to the output file. |
| 773 | void |
| 774 | do_write(Output_file*); |
| 775 | |
| 776 | // Write the data to a buffer. |
| 777 | void |
| 778 | do_write_to_buffer(unsigned char* buffer) |
| 779 | { memcpy(buffer, this->data_.data(), this->data_.size()); } |
| 780 | |
| 781 | // Write to a map file. |
| 782 | void |
| 783 | do_print_to_mapfile(Mapfile* mapfile) const |
| 784 | { mapfile->print_output_data(this, _("** fill")); } |
| 785 | |
| 786 | private: |
| 787 | std::string data_; |
| 788 | }; |
| 789 | |
| 790 | // Another version of Output_data with constant data, in which the |
| 791 | // buffer is allocated by the caller. |
| 792 | |
| 793 | class Output_data_const_buffer : public Output_section_data |
| 794 | { |
| 795 | public: |
| 796 | Output_data_const_buffer(const unsigned char* p, off_t len, |
| 797 | uint64_t addralign, const char* map_name) |
| 798 | : Output_section_data(len, addralign, true), |
| 799 | p_(p), map_name_(map_name) |
| 800 | { } |
| 801 | |
| 802 | protected: |
| 803 | // Write the data the output file. |
| 804 | void |
| 805 | do_write(Output_file*); |
| 806 | |
| 807 | // Write the data to a buffer. |
| 808 | void |
| 809 | do_write_to_buffer(unsigned char* buffer) |
| 810 | { memcpy(buffer, this->p_, this->data_size()); } |
| 811 | |
| 812 | // Write to a map file. |
| 813 | void |
| 814 | do_print_to_mapfile(Mapfile* mapfile) const |
| 815 | { mapfile->print_output_data(this, _(this->map_name_)); } |
| 816 | |
| 817 | private: |
| 818 | // The data to output. |
| 819 | const unsigned char* p_; |
| 820 | // Name to use in a map file. Maps are a rarely used feature, but |
| 821 | // the space usage is minor as aren't very many of these objects. |
| 822 | const char* map_name_; |
| 823 | }; |
| 824 | |
| 825 | // A place holder for a fixed amount of data written out via some |
| 826 | // other mechanism. |
| 827 | |
| 828 | class Output_data_fixed_space : public Output_section_data |
| 829 | { |
| 830 | public: |
| 831 | Output_data_fixed_space(off_t data_size, uint64_t addralign, |
| 832 | const char* map_name) |
| 833 | : Output_section_data(data_size, addralign, true), |
| 834 | map_name_(map_name) |
| 835 | { } |
| 836 | |
| 837 | protected: |
| 838 | // Write out the data--the actual data must be written out |
| 839 | // elsewhere. |
| 840 | void |
| 841 | do_write(Output_file*) |
| 842 | { } |
| 843 | |
| 844 | // Write to a map file. |
| 845 | void |
| 846 | do_print_to_mapfile(Mapfile* mapfile) const |
| 847 | { mapfile->print_output_data(this, _(this->map_name_)); } |
| 848 | |
| 849 | private: |
| 850 | // Name to use in a map file. Maps are a rarely used feature, but |
| 851 | // the space usage is minor as aren't very many of these objects. |
| 852 | const char* map_name_; |
| 853 | }; |
| 854 | |
| 855 | // A place holder for variable sized data written out via some other |
| 856 | // mechanism. |
| 857 | |
| 858 | class Output_data_space : public Output_section_data_build |
| 859 | { |
| 860 | public: |
| 861 | explicit Output_data_space(uint64_t addralign, const char* map_name) |
| 862 | : Output_section_data_build(addralign), |
| 863 | map_name_(map_name) |
| 864 | { } |
| 865 | |
| 866 | // Set the alignment. |
| 867 | void |
| 868 | set_space_alignment(uint64_t align) |
| 869 | { this->set_addralign(align); } |
| 870 | |
| 871 | protected: |
| 872 | // Write out the data--the actual data must be written out |
| 873 | // elsewhere. |
| 874 | void |
| 875 | do_write(Output_file*) |
| 876 | { } |
| 877 | |
| 878 | // Write to a map file. |
| 879 | void |
| 880 | do_print_to_mapfile(Mapfile* mapfile) const |
| 881 | { mapfile->print_output_data(this, _(this->map_name_)); } |
| 882 | |
| 883 | private: |
| 884 | // Name to use in a map file. Maps are a rarely used feature, but |
| 885 | // the space usage is minor as aren't very many of these objects. |
| 886 | const char* map_name_; |
| 887 | }; |
| 888 | |
| 889 | // Fill fixed space with zeroes. This is just like |
| 890 | // Output_data_fixed_space, except that the map name is known. |
| 891 | |
| 892 | class Output_data_zero_fill : public Output_section_data |
| 893 | { |
| 894 | public: |
| 895 | Output_data_zero_fill(off_t data_size, uint64_t addralign) |
| 896 | : Output_section_data(data_size, addralign, true) |
| 897 | { } |
| 898 | |
| 899 | protected: |
| 900 | // There is no data to write out. |
| 901 | void |
| 902 | do_write(Output_file*) |
| 903 | { } |
| 904 | |
| 905 | // Write to a map file. |
| 906 | void |
| 907 | do_print_to_mapfile(Mapfile* mapfile) const |
| 908 | { mapfile->print_output_data(this, "** zero fill"); } |
| 909 | }; |
| 910 | |
| 911 | // A string table which goes into an output section. |
| 912 | |
| 913 | class Output_data_strtab : public Output_section_data |
| 914 | { |
| 915 | public: |
| 916 | Output_data_strtab(Stringpool* strtab) |
| 917 | : Output_section_data(1), strtab_(strtab) |
| 918 | { } |
| 919 | |
| 920 | protected: |
| 921 | // This is called to set the address and file offset. Here we make |
| 922 | // sure that the Stringpool is finalized. |
| 923 | void |
| 924 | set_final_data_size(); |
| 925 | |
| 926 | // Write out the data. |
| 927 | void |
| 928 | do_write(Output_file*); |
| 929 | |
| 930 | // Write the data to a buffer. |
| 931 | void |
| 932 | do_write_to_buffer(unsigned char* buffer) |
| 933 | { this->strtab_->write_to_buffer(buffer, this->data_size()); } |
| 934 | |
| 935 | // Write to a map file. |
| 936 | void |
| 937 | do_print_to_mapfile(Mapfile* mapfile) const |
| 938 | { mapfile->print_output_data(this, _("** string table")); } |
| 939 | |
| 940 | private: |
| 941 | Stringpool* strtab_; |
| 942 | }; |
| 943 | |
| 944 | // This POD class is used to represent a single reloc in the output |
| 945 | // file. This could be a private class within Output_data_reloc, but |
| 946 | // the templatization is complex enough that I broke it out into a |
| 947 | // separate class. The class is templatized on either elfcpp::SHT_REL |
| 948 | // or elfcpp::SHT_RELA, and also on whether this is a dynamic |
| 949 | // relocation or an ordinary relocation. |
| 950 | |
| 951 | // A relocation can be against a global symbol, a local symbol, a |
| 952 | // local section symbol, an output section, or the undefined symbol at |
| 953 | // index 0. We represent the latter by using a NULL global symbol. |
| 954 | |
| 955 | template<int sh_type, bool dynamic, int size, bool big_endian> |
| 956 | class Output_reloc; |
| 957 | |
| 958 | template<bool dynamic, int size, bool big_endian> |
| 959 | class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> |
| 960 | { |
| 961 | public: |
| 962 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
| 963 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; |
| 964 | |
| 965 | static const Address invalid_address = static_cast<Address>(0) - 1; |
| 966 | |
| 967 | // An uninitialized entry. We need this because we want to put |
| 968 | // instances of this class into an STL container. |
| 969 | Output_reloc() |
| 970 | : local_sym_index_(INVALID_CODE) |
| 971 | { } |
| 972 | |
| 973 | // We have a bunch of different constructors. They come in pairs |
| 974 | // depending on how the address of the relocation is specified. It |
| 975 | // can either be an offset in an Output_data or an offset in an |
| 976 | // input section. |
| 977 | |
| 978 | // A reloc against a global symbol. |
| 979 | |
| 980 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
| 981 | Address address, bool is_relative, bool is_symbolless); |
| 982 | |
| 983 | Output_reloc(Symbol* gsym, unsigned int type, |
| 984 | Sized_relobj<size, big_endian>* relobj, |
| 985 | unsigned int shndx, Address address, bool is_relative, |
| 986 | bool is_symbolless); |
| 987 | |
| 988 | // A reloc against a local symbol or local section symbol. |
| 989 | |
| 990 | Output_reloc(Sized_relobj<size, big_endian>* relobj, |
| 991 | unsigned int local_sym_index, unsigned int type, |
| 992 | Output_data* od, Address address, bool is_relative, |
| 993 | bool is_symbolless, bool is_section_symbol); |
| 994 | |
| 995 | Output_reloc(Sized_relobj<size, big_endian>* relobj, |
| 996 | unsigned int local_sym_index, unsigned int type, |
| 997 | unsigned int shndx, Address address, bool is_relative, |
| 998 | bool is_symbolless, bool is_section_symbol); |
| 999 | |
| 1000 | // A reloc against the STT_SECTION symbol of an output section. |
| 1001 | |
| 1002 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
| 1003 | Address address); |
| 1004 | |
| 1005 | Output_reloc(Output_section* os, unsigned int type, |
| 1006 | Sized_relobj<size, big_endian>* relobj, |
| 1007 | unsigned int shndx, Address address); |
| 1008 | |
| 1009 | // An absolute relocation with no symbol. |
| 1010 | |
| 1011 | Output_reloc(unsigned int type, Output_data* od, Address address); |
| 1012 | |
| 1013 | Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj, |
| 1014 | unsigned int shndx, Address address); |
| 1015 | |
| 1016 | // A target specific relocation. The target will be called to get |
| 1017 | // the symbol index, passing ARG. The type and offset will be set |
| 1018 | // as for other relocation types. |
| 1019 | |
| 1020 | Output_reloc(unsigned int type, void* arg, Output_data* od, |
| 1021 | Address address); |
| 1022 | |
| 1023 | Output_reloc(unsigned int type, void* arg, |
| 1024 | Sized_relobj<size, big_endian>* relobj, |
| 1025 | unsigned int shndx, Address address); |
| 1026 | |
| 1027 | // Return the reloc type. |
| 1028 | unsigned int |
| 1029 | type() const |
| 1030 | { return this->type_; } |
| 1031 | |
| 1032 | // Return whether this is a RELATIVE relocation. |
| 1033 | bool |
| 1034 | is_relative() const |
| 1035 | { return this->is_relative_; } |
| 1036 | |
| 1037 | // Return whether this is a relocation which should not use |
| 1038 | // a symbol, but which obtains its addend from a symbol. |
| 1039 | bool |
| 1040 | is_symbolless() const |
| 1041 | { return this->is_symbolless_; } |
| 1042 | |
| 1043 | // Return whether this is against a local section symbol. |
| 1044 | bool |
| 1045 | is_local_section_symbol() const |
| 1046 | { |
| 1047 | return (this->local_sym_index_ != GSYM_CODE |
| 1048 | && this->local_sym_index_ != SECTION_CODE |
| 1049 | && this->local_sym_index_ != INVALID_CODE |
| 1050 | && this->local_sym_index_ != TARGET_CODE |
| 1051 | && this->is_section_symbol_); |
| 1052 | } |
| 1053 | |
| 1054 | // Return whether this is a target specific relocation. |
| 1055 | bool |
| 1056 | is_target_specific() const |
| 1057 | { return this->local_sym_index_ == TARGET_CODE; } |
| 1058 | |
| 1059 | // Return the argument to pass to the target for a target specific |
| 1060 | // relocation. |
| 1061 | void* |
| 1062 | target_arg() const |
| 1063 | { |
| 1064 | gold_assert(this->local_sym_index_ == TARGET_CODE); |
| 1065 | return this->u1_.arg; |
| 1066 | } |
| 1067 | |
| 1068 | // For a local section symbol, return the offset of the input |
| 1069 | // section within the output section. ADDEND is the addend being |
| 1070 | // applied to the input section. |
| 1071 | Address |
| 1072 | local_section_offset(Addend addend) const; |
| 1073 | |
| 1074 | // Get the value of the symbol referred to by a Rel relocation when |
| 1075 | // we are adding the given ADDEND. |
| 1076 | Address |
| 1077 | symbol_value(Addend addend) const; |
| 1078 | |
| 1079 | // Write the reloc entry to an output view. |
| 1080 | void |
| 1081 | write(unsigned char* pov) const; |
| 1082 | |
| 1083 | // Write the offset and info fields to Write_rel. |
| 1084 | template<typename Write_rel> |
| 1085 | void write_rel(Write_rel*) const; |
| 1086 | |
| 1087 | // This is used when sorting dynamic relocs. Return -1 to sort this |
| 1088 | // reloc before R2, 0 to sort the same as R2, 1 to sort after R2. |
| 1089 | int |
| 1090 | compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2) |
| 1091 | const; |
| 1092 | |
| 1093 | // Return whether this reloc should be sorted before the argument |
| 1094 | // when sorting dynamic relocs. |
| 1095 | bool |
| 1096 | sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& |
| 1097 | r2) const |
| 1098 | { return this->compare(r2) < 0; } |
| 1099 | |
| 1100 | private: |
| 1101 | // Record that we need a dynamic symbol index. |
| 1102 | void |
| 1103 | set_needs_dynsym_index(); |
| 1104 | |
| 1105 | // Return the symbol index. |
| 1106 | unsigned int |
| 1107 | get_symbol_index() const; |
| 1108 | |
| 1109 | // Return the output address. |
| 1110 | Address |
| 1111 | get_address() const; |
| 1112 | |
| 1113 | // Codes for local_sym_index_. |
| 1114 | enum |
| 1115 | { |
| 1116 | // Global symbol. |
| 1117 | GSYM_CODE = -1U, |
| 1118 | // Output section. |
| 1119 | SECTION_CODE = -2U, |
| 1120 | // Target specific. |
| 1121 | TARGET_CODE = -3U, |
| 1122 | // Invalid uninitialized entry. |
| 1123 | INVALID_CODE = -4U |
| 1124 | }; |
| 1125 | |
| 1126 | union |
| 1127 | { |
| 1128 | // For a local symbol or local section symbol |
| 1129 | // (this->local_sym_index_ >= 0), the object. We will never |
| 1130 | // generate a relocation against a local symbol in a dynamic |
| 1131 | // object; that doesn't make sense. And our callers will always |
| 1132 | // be templatized, so we use Sized_relobj here. |
| 1133 | Sized_relobj<size, big_endian>* relobj; |
| 1134 | // For a global symbol (this->local_sym_index_ == GSYM_CODE, the |
| 1135 | // symbol. If this is NULL, it indicates a relocation against the |
| 1136 | // undefined 0 symbol. |
| 1137 | Symbol* gsym; |
| 1138 | // For a relocation against an output section |
| 1139 | // (this->local_sym_index_ == SECTION_CODE), the output section. |
| 1140 | Output_section* os; |
| 1141 | // For a target specific relocation, an argument to pass to the |
| 1142 | // target. |
| 1143 | void* arg; |
| 1144 | } u1_; |
| 1145 | union |
| 1146 | { |
| 1147 | // If this->shndx_ is not INVALID CODE, the object which holds the |
| 1148 | // input section being used to specify the reloc address. |
| 1149 | Sized_relobj<size, big_endian>* relobj; |
| 1150 | // If this->shndx_ is INVALID_CODE, the output data being used to |
| 1151 | // specify the reloc address. This may be NULL if the reloc |
| 1152 | // address is absolute. |
| 1153 | Output_data* od; |
| 1154 | } u2_; |
| 1155 | // The address offset within the input section or the Output_data. |
| 1156 | Address address_; |
| 1157 | // This is GSYM_CODE for a global symbol, or SECTION_CODE for a |
| 1158 | // relocation against an output section, or TARGET_CODE for a target |
| 1159 | // specific relocation, or INVALID_CODE for an uninitialized value. |
| 1160 | // Otherwise, for a local symbol (this->is_section_symbol_ is |
| 1161 | // false), the local symbol index. For a local section symbol |
| 1162 | // (this->is_section_symbol_ is true), the section index in the |
| 1163 | // input file. |
| 1164 | unsigned int local_sym_index_; |
| 1165 | // The reloc type--a processor specific code. |
| 1166 | unsigned int type_ : 29; |
| 1167 | // True if the relocation is a RELATIVE relocation. |
| 1168 | bool is_relative_ : 1; |
| 1169 | // True if the relocation is one which should not use |
| 1170 | // a symbol, but which obtains its addend from a symbol. |
| 1171 | bool is_symbolless_ : 1; |
| 1172 | // True if the relocation is against a section symbol. |
| 1173 | bool is_section_symbol_ : 1; |
| 1174 | // If the reloc address is an input section in an object, the |
| 1175 | // section index. This is INVALID_CODE if the reloc address is |
| 1176 | // specified in some other way. |
| 1177 | unsigned int shndx_; |
| 1178 | }; |
| 1179 | |
| 1180 | // The SHT_RELA version of Output_reloc<>. This is just derived from |
| 1181 | // the SHT_REL version of Output_reloc, but it adds an addend. |
| 1182 | |
| 1183 | template<bool dynamic, int size, bool big_endian> |
| 1184 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> |
| 1185 | { |
| 1186 | public: |
| 1187 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
| 1188 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; |
| 1189 | |
| 1190 | // An uninitialized entry. |
| 1191 | Output_reloc() |
| 1192 | : rel_() |
| 1193 | { } |
| 1194 | |
| 1195 | // A reloc against a global symbol. |
| 1196 | |
| 1197 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
| 1198 | Address address, Addend addend, bool is_relative, |
| 1199 | bool is_symbolless) |
| 1200 | : rel_(gsym, type, od, address, is_relative, is_symbolless), |
| 1201 | addend_(addend) |
| 1202 | { } |
| 1203 | |
| 1204 | Output_reloc(Symbol* gsym, unsigned int type, |
| 1205 | Sized_relobj<size, big_endian>* relobj, |
| 1206 | unsigned int shndx, Address address, Addend addend, |
| 1207 | bool is_relative, bool is_symbolless) |
| 1208 | : rel_(gsym, type, relobj, shndx, address, is_relative, |
| 1209 | is_symbolless), addend_(addend) |
| 1210 | { } |
| 1211 | |
| 1212 | // A reloc against a local symbol. |
| 1213 | |
| 1214 | Output_reloc(Sized_relobj<size, big_endian>* relobj, |
| 1215 | unsigned int local_sym_index, unsigned int type, |
| 1216 | Output_data* od, Address address, |
| 1217 | Addend addend, bool is_relative, |
| 1218 | bool is_symbolless, bool is_section_symbol) |
| 1219 | : rel_(relobj, local_sym_index, type, od, address, is_relative, |
| 1220 | is_symbolless, is_section_symbol), |
| 1221 | addend_(addend) |
| 1222 | { } |
| 1223 | |
| 1224 | Output_reloc(Sized_relobj<size, big_endian>* relobj, |
| 1225 | unsigned int local_sym_index, unsigned int type, |
| 1226 | unsigned int shndx, Address address, |
| 1227 | Addend addend, bool is_relative, |
| 1228 | bool is_symbolless, bool is_section_symbol) |
| 1229 | : rel_(relobj, local_sym_index, type, shndx, address, is_relative, |
| 1230 | is_symbolless, is_section_symbol), |
| 1231 | addend_(addend) |
| 1232 | { } |
| 1233 | |
| 1234 | // A reloc against the STT_SECTION symbol of an output section. |
| 1235 | |
| 1236 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
| 1237 | Address address, Addend addend) |
| 1238 | : rel_(os, type, od, address), addend_(addend) |
| 1239 | { } |
| 1240 | |
| 1241 | Output_reloc(Output_section* os, unsigned int type, |
| 1242 | Sized_relobj<size, big_endian>* relobj, |
| 1243 | unsigned int shndx, Address address, Addend addend) |
| 1244 | : rel_(os, type, relobj, shndx, address), addend_(addend) |
| 1245 | { } |
| 1246 | |
| 1247 | // An absolute relocation with no symbol. |
| 1248 | |
| 1249 | Output_reloc(unsigned int type, Output_data* od, Address address, |
| 1250 | Addend addend) |
| 1251 | : rel_(type, od, address), addend_(addend) |
| 1252 | { } |
| 1253 | |
| 1254 | Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj, |
| 1255 | unsigned int shndx, Address address, Addend addend) |
| 1256 | : rel_(type, relobj, shndx, address), addend_(addend) |
| 1257 | { } |
| 1258 | |
| 1259 | // A target specific relocation. The target will be called to get |
| 1260 | // the symbol index and the addend, passing ARG. The type and |
| 1261 | // offset will be set as for other relocation types. |
| 1262 | |
| 1263 | Output_reloc(unsigned int type, void* arg, Output_data* od, |
| 1264 | Address address, Addend addend) |
| 1265 | : rel_(type, arg, od, address), addend_(addend) |
| 1266 | { } |
| 1267 | |
| 1268 | Output_reloc(unsigned int type, void* arg, |
| 1269 | Sized_relobj<size, big_endian>* relobj, |
| 1270 | unsigned int shndx, Address address, Addend addend) |
| 1271 | : rel_(type, arg, relobj, shndx, address), addend_(addend) |
| 1272 | { } |
| 1273 | |
| 1274 | // Return whether this is a RELATIVE relocation. |
| 1275 | bool |
| 1276 | is_relative() const |
| 1277 | { return this->rel_.is_relative(); } |
| 1278 | |
| 1279 | // Return whether this is a relocation which should not use |
| 1280 | // a symbol, but which obtains its addend from a symbol. |
| 1281 | bool |
| 1282 | is_symbolless() const |
| 1283 | { return this->rel_.is_symbolless(); } |
| 1284 | |
| 1285 | // Write the reloc entry to an output view. |
| 1286 | void |
| 1287 | write(unsigned char* pov) const; |
| 1288 | |
| 1289 | // Return whether this reloc should be sorted before the argument |
| 1290 | // when sorting dynamic relocs. |
| 1291 | bool |
| 1292 | sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>& |
| 1293 | r2) const |
| 1294 | { |
| 1295 | int i = this->rel_.compare(r2.rel_); |
| 1296 | if (i < 0) |
| 1297 | return true; |
| 1298 | else if (i > 0) |
| 1299 | return false; |
| 1300 | else |
| 1301 | return this->addend_ < r2.addend_; |
| 1302 | } |
| 1303 | |
| 1304 | private: |
| 1305 | // The basic reloc. |
| 1306 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; |
| 1307 | // The addend. |
| 1308 | Addend addend_; |
| 1309 | }; |
| 1310 | |
| 1311 | // Output_data_reloc_generic is a non-template base class for |
| 1312 | // Output_data_reloc_base. This gives the generic code a way to hold |
| 1313 | // a pointer to a reloc section. |
| 1314 | |
| 1315 | class Output_data_reloc_generic : public Output_section_data_build |
| 1316 | { |
| 1317 | public: |
| 1318 | Output_data_reloc_generic(int size, bool sort_relocs) |
| 1319 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), |
| 1320 | relative_reloc_count_(0), sort_relocs_(sort_relocs) |
| 1321 | { } |
| 1322 | |
| 1323 | // Return the number of relative relocs in this section. |
| 1324 | size_t |
| 1325 | relative_reloc_count() const |
| 1326 | { return this->relative_reloc_count_; } |
| 1327 | |
| 1328 | // Whether we should sort the relocs. |
| 1329 | bool |
| 1330 | sort_relocs() const |
| 1331 | { return this->sort_relocs_; } |
| 1332 | |
| 1333 | protected: |
| 1334 | // Note that we've added another relative reloc. |
| 1335 | void |
| 1336 | bump_relative_reloc_count() |
| 1337 | { ++this->relative_reloc_count_; } |
| 1338 | |
| 1339 | private: |
| 1340 | // The number of relative relocs added to this section. This is to |
| 1341 | // support DT_RELCOUNT. |
| 1342 | size_t relative_reloc_count_; |
| 1343 | // Whether to sort the relocations when writing them out, to make |
| 1344 | // the dynamic linker more efficient. |
| 1345 | bool sort_relocs_; |
| 1346 | }; |
| 1347 | |
| 1348 | // Output_data_reloc is used to manage a section containing relocs. |
| 1349 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC |
| 1350 | // indicates whether this is a dynamic relocation or a normal |
| 1351 | // relocation. Output_data_reloc_base is a base class. |
| 1352 | // Output_data_reloc is the real class, which we specialize based on |
| 1353 | // the reloc type. |
| 1354 | |
| 1355 | template<int sh_type, bool dynamic, int size, bool big_endian> |
| 1356 | class Output_data_reloc_base : public Output_data_reloc_generic |
| 1357 | { |
| 1358 | public: |
| 1359 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; |
| 1360 | typedef typename Output_reloc_type::Address Address; |
| 1361 | static const int reloc_size = |
| 1362 | Reloc_types<sh_type, size, big_endian>::reloc_size; |
| 1363 | |
| 1364 | // Construct the section. |
| 1365 | Output_data_reloc_base(bool sort_relocs) |
| 1366 | : Output_data_reloc_generic(size, sort_relocs) |
| 1367 | { } |
| 1368 | |
| 1369 | protected: |
| 1370 | // Write out the data. |
| 1371 | void |
| 1372 | do_write(Output_file*); |
| 1373 | |
| 1374 | // Set the entry size and the link. |
| 1375 | void |
| 1376 | do_adjust_output_section(Output_section* os); |
| 1377 | |
| 1378 | // Write to a map file. |
| 1379 | void |
| 1380 | do_print_to_mapfile(Mapfile* mapfile) const |
| 1381 | { |
| 1382 | mapfile->print_output_data(this, |
| 1383 | (dynamic |
| 1384 | ? _("** dynamic relocs") |
| 1385 | : _("** relocs"))); |
| 1386 | } |
| 1387 | |
| 1388 | // Add a relocation entry. |
| 1389 | void |
| 1390 | add(Output_data* od, const Output_reloc_type& reloc) |
| 1391 | { |
| 1392 | this->relocs_.push_back(reloc); |
| 1393 | this->set_current_data_size(this->relocs_.size() * reloc_size); |
| 1394 | od->add_dynamic_reloc(); |
| 1395 | if (reloc.is_relative()) |
| 1396 | this->bump_relative_reloc_count(); |
| 1397 | } |
| 1398 | |
| 1399 | private: |
| 1400 | typedef std::vector<Output_reloc_type> Relocs; |
| 1401 | |
| 1402 | // The class used to sort the relocations. |
| 1403 | struct Sort_relocs_comparison |
| 1404 | { |
| 1405 | bool |
| 1406 | operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const |
| 1407 | { return r1.sort_before(r2); } |
| 1408 | }; |
| 1409 | |
| 1410 | // The relocations in this section. |
| 1411 | Relocs relocs_; |
| 1412 | }; |
| 1413 | |
| 1414 | // The class which callers actually create. |
| 1415 | |
| 1416 | template<int sh_type, bool dynamic, int size, bool big_endian> |
| 1417 | class Output_data_reloc; |
| 1418 | |
| 1419 | // The SHT_REL version of Output_data_reloc. |
| 1420 | |
| 1421 | template<bool dynamic, int size, bool big_endian> |
| 1422 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> |
| 1423 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> |
| 1424 | { |
| 1425 | private: |
| 1426 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, |
| 1427 | big_endian> Base; |
| 1428 | |
| 1429 | public: |
| 1430 | typedef typename Base::Output_reloc_type Output_reloc_type; |
| 1431 | typedef typename Output_reloc_type::Address Address; |
| 1432 | |
| 1433 | Output_data_reloc(bool sr) |
| 1434 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr) |
| 1435 | { } |
| 1436 | |
| 1437 | // Add a reloc against a global symbol. |
| 1438 | |
| 1439 | void |
| 1440 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
| 1441 | { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); } |
| 1442 | |
| 1443 | void |
| 1444 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
| 1445 | Sized_relobj<size, big_endian>* relobj, |
| 1446 | unsigned int shndx, Address address) |
| 1447 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
| 1448 | false, false)); } |
| 1449 | |
| 1450 | // These are to simplify the Copy_relocs class. |
| 1451 | |
| 1452 | void |
| 1453 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address, |
| 1454 | Address addend) |
| 1455 | { |
| 1456 | gold_assert(addend == 0); |
| 1457 | this->add_global(gsym, type, od, address); |
| 1458 | } |
| 1459 | |
| 1460 | void |
| 1461 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
| 1462 | Sized_relobj<size, big_endian>* relobj, |
| 1463 | unsigned int shndx, Address address, Address addend) |
| 1464 | { |
| 1465 | gold_assert(addend == 0); |
| 1466 | this->add_global(gsym, type, od, relobj, shndx, address); |
| 1467 | } |
| 1468 | |
| 1469 | // Add a RELATIVE reloc against a global symbol. The final relocation |
| 1470 | // will not reference the symbol. |
| 1471 | |
| 1472 | void |
| 1473 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, |
| 1474 | Address address) |
| 1475 | { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); } |
| 1476 | |
| 1477 | void |
| 1478 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, |
| 1479 | Sized_relobj<size, big_endian>* relobj, |
| 1480 | unsigned int shndx, Address address) |
| 1481 | { |
| 1482 | this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
| 1483 | true, true)); |
| 1484 | } |
| 1485 | |
| 1486 | // Add a global relocation which does not use a symbol for the relocation, |
| 1487 | // but which gets its addend from a symbol. |
| 1488 | |
| 1489 | void |
| 1490 | add_symbolless_global_addend(Symbol* gsym, unsigned int type, |
| 1491 | Output_data* od, Address address) |
| 1492 | { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); } |
| 1493 | |
| 1494 | void |
| 1495 | add_symbolless_global_addend(Symbol* gsym, unsigned int type, |
| 1496 | Output_data* od, |
| 1497 | Sized_relobj<size, big_endian>* relobj, |
| 1498 | unsigned int shndx, Address address) |
| 1499 | { |
| 1500 | this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
| 1501 | false, true)); |
| 1502 | } |
| 1503 | |
| 1504 | // Add a reloc against a local symbol. |
| 1505 | |
| 1506 | void |
| 1507 | add_local(Sized_relobj<size, big_endian>* relobj, |
| 1508 | unsigned int local_sym_index, unsigned int type, |
| 1509 | Output_data* od, Address address) |
| 1510 | { |
| 1511 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, |
| 1512 | address, false, false, false)); |
| 1513 | } |
| 1514 | |
| 1515 | void |
| 1516 | add_local(Sized_relobj<size, big_endian>* relobj, |
| 1517 | unsigned int local_sym_index, unsigned int type, |
| 1518 | Output_data* od, unsigned int shndx, Address address) |
| 1519 | { |
| 1520 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
| 1521 | address, false, false, false)); |
| 1522 | } |
| 1523 | |
| 1524 | // Add a RELATIVE reloc against a local symbol. |
| 1525 | |
| 1526 | void |
| 1527 | add_local_relative(Sized_relobj<size, big_endian>* relobj, |
| 1528 | unsigned int local_sym_index, unsigned int type, |
| 1529 | Output_data* od, Address address) |
| 1530 | { |
| 1531 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, |
| 1532 | address, true, true, false)); |
| 1533 | } |
| 1534 | |
| 1535 | void |
| 1536 | add_local_relative(Sized_relobj<size, big_endian>* relobj, |
| 1537 | unsigned int local_sym_index, unsigned int type, |
| 1538 | Output_data* od, unsigned int shndx, Address address) |
| 1539 | { |
| 1540 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
| 1541 | address, true, true, false)); |
| 1542 | } |
| 1543 | |
| 1544 | // Add a local relocation which does not use a symbol for the relocation, |
| 1545 | // but which gets its addend from a symbol. |
| 1546 | |
| 1547 | void |
| 1548 | add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj, |
| 1549 | unsigned int local_sym_index, unsigned int type, |
| 1550 | Output_data* od, Address address) |
| 1551 | { |
| 1552 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, |
| 1553 | address, false, true, false)); |
| 1554 | } |
| 1555 | |
| 1556 | void |
| 1557 | add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj, |
| 1558 | unsigned int local_sym_index, unsigned int type, |
| 1559 | Output_data* od, unsigned int shndx, |
| 1560 | Address address) |
| 1561 | { |
| 1562 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
| 1563 | address, false, true, false)); |
| 1564 | } |
| 1565 | |
| 1566 | // Add a reloc against a local section symbol. This will be |
| 1567 | // converted into a reloc against the STT_SECTION symbol of the |
| 1568 | // output section. |
| 1569 | |
| 1570 | void |
| 1571 | add_local_section(Sized_relobj<size, big_endian>* relobj, |
| 1572 | unsigned int input_shndx, unsigned int type, |
| 1573 | Output_data* od, Address address) |
| 1574 | { |
| 1575 | this->add(od, Output_reloc_type(relobj, input_shndx, type, od, |
| 1576 | address, false, false, true)); |
| 1577 | } |
| 1578 | |
| 1579 | void |
| 1580 | add_local_section(Sized_relobj<size, big_endian>* relobj, |
| 1581 | unsigned int input_shndx, unsigned int type, |
| 1582 | Output_data* od, unsigned int shndx, Address address) |
| 1583 | { |
| 1584 | this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx, |
| 1585 | address, false, false, true)); |
| 1586 | } |
| 1587 | |
| 1588 | // A reloc against the STT_SECTION symbol of an output section. |
| 1589 | // OS is the Output_section that the relocation refers to; OD is |
| 1590 | // the Output_data object being relocated. |
| 1591 | |
| 1592 | void |
| 1593 | add_output_section(Output_section* os, unsigned int type, |
| 1594 | Output_data* od, Address address) |
| 1595 | { this->add(od, Output_reloc_type(os, type, od, address)); } |
| 1596 | |
| 1597 | void |
| 1598 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
| 1599 | Sized_relobj<size, big_endian>* relobj, |
| 1600 | unsigned int shndx, Address address) |
| 1601 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); } |
| 1602 | |
| 1603 | // Add an absolute relocation. |
| 1604 | |
| 1605 | void |
| 1606 | add_absolute(unsigned int type, Output_data* od, Address address) |
| 1607 | { this->add(od, Output_reloc_type(type, od, address)); } |
| 1608 | |
| 1609 | void |
| 1610 | add_absolute(unsigned int type, Output_data* od, |
| 1611 | Sized_relobj<size, big_endian>* relobj, |
| 1612 | unsigned int shndx, Address address) |
| 1613 | { this->add(od, Output_reloc_type(type, relobj, shndx, address)); } |
| 1614 | |
| 1615 | // Add a target specific relocation. A target which calls this must |
| 1616 | // define the reloc_symbol_index and reloc_addend virtual functions. |
| 1617 | |
| 1618 | void |
| 1619 | add_target_specific(unsigned int type, void* arg, Output_data* od, |
| 1620 | Address address) |
| 1621 | { this->add(od, Output_reloc_type(type, arg, od, address)); } |
| 1622 | |
| 1623 | void |
| 1624 | add_target_specific(unsigned int type, void* arg, Output_data* od, |
| 1625 | Sized_relobj<size, big_endian>* relobj, |
| 1626 | unsigned int shndx, Address address) |
| 1627 | { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); } |
| 1628 | }; |
| 1629 | |
| 1630 | // The SHT_RELA version of Output_data_reloc. |
| 1631 | |
| 1632 | template<bool dynamic, int size, bool big_endian> |
| 1633 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> |
| 1634 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> |
| 1635 | { |
| 1636 | private: |
| 1637 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, |
| 1638 | big_endian> Base; |
| 1639 | |
| 1640 | public: |
| 1641 | typedef typename Base::Output_reloc_type Output_reloc_type; |
| 1642 | typedef typename Output_reloc_type::Address Address; |
| 1643 | typedef typename Output_reloc_type::Addend Addend; |
| 1644 | |
| 1645 | Output_data_reloc(bool sr) |
| 1646 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr) |
| 1647 | { } |
| 1648 | |
| 1649 | // Add a reloc against a global symbol. |
| 1650 | |
| 1651 | void |
| 1652 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
| 1653 | Address address, Addend addend) |
| 1654 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, |
| 1655 | false, false)); } |
| 1656 | |
| 1657 | void |
| 1658 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
| 1659 | Sized_relobj<size, big_endian>* relobj, |
| 1660 | unsigned int shndx, Address address, |
| 1661 | Addend addend) |
| 1662 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
| 1663 | addend, false, false)); } |
| 1664 | |
| 1665 | // Add a RELATIVE reloc against a global symbol. The final output |
| 1666 | // relocation will not reference the symbol, but we must keep the symbol |
| 1667 | // information long enough to set the addend of the relocation correctly |
| 1668 | // when it is written. |
| 1669 | |
| 1670 | void |
| 1671 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, |
| 1672 | Address address, Addend addend) |
| 1673 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true, |
| 1674 | true)); } |
| 1675 | |
| 1676 | void |
| 1677 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, |
| 1678 | Sized_relobj<size, big_endian>* relobj, |
| 1679 | unsigned int shndx, Address address, Addend addend) |
| 1680 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
| 1681 | addend, true, true)); } |
| 1682 | |
| 1683 | // Add a global relocation which does not use a symbol for the relocation, |
| 1684 | // but which gets its addend from a symbol. |
| 1685 | |
| 1686 | void |
| 1687 | add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od, |
| 1688 | Address address, Addend addend) |
| 1689 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, |
| 1690 | false, true)); } |
| 1691 | |
| 1692 | void |
| 1693 | add_symbolless_global_addend(Symbol* gsym, unsigned int type, |
| 1694 | Output_data* od, |
| 1695 | Sized_relobj<size, big_endian>* relobj, |
| 1696 | unsigned int shndx, Address address, Addend addend) |
| 1697 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
| 1698 | addend, false, true)); } |
| 1699 | |
| 1700 | // Add a reloc against a local symbol. |
| 1701 | |
| 1702 | void |
| 1703 | add_local(Sized_relobj<size, big_endian>* relobj, |
| 1704 | unsigned int local_sym_index, unsigned int type, |
| 1705 | Output_data* od, Address address, Addend addend) |
| 1706 | { |
| 1707 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
| 1708 | addend, false, false, false)); |
| 1709 | } |
| 1710 | |
| 1711 | void |
| 1712 | add_local(Sized_relobj<size, big_endian>* relobj, |
| 1713 | unsigned int local_sym_index, unsigned int type, |
| 1714 | Output_data* od, unsigned int shndx, Address address, |
| 1715 | Addend addend) |
| 1716 | { |
| 1717 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
| 1718 | address, addend, false, false, false)); |
| 1719 | } |
| 1720 | |
| 1721 | // Add a RELATIVE reloc against a local symbol. |
| 1722 | |
| 1723 | void |
| 1724 | add_local_relative(Sized_relobj<size, big_endian>* relobj, |
| 1725 | unsigned int local_sym_index, unsigned int type, |
| 1726 | Output_data* od, Address address, Addend addend) |
| 1727 | { |
| 1728 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
| 1729 | addend, true, true, false)); |
| 1730 | } |
| 1731 | |
| 1732 | void |
| 1733 | add_local_relative(Sized_relobj<size, big_endian>* relobj, |
| 1734 | unsigned int local_sym_index, unsigned int type, |
| 1735 | Output_data* od, unsigned int shndx, Address address, |
| 1736 | Addend addend) |
| 1737 | { |
| 1738 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
| 1739 | address, addend, true, true, false)); |
| 1740 | } |
| 1741 | |
| 1742 | // Add a local relocation which does not use a symbol for the relocation, |
| 1743 | // but which gets it's addend from a symbol. |
| 1744 | |
| 1745 | void |
| 1746 | add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj, |
| 1747 | unsigned int local_sym_index, unsigned int type, |
| 1748 | Output_data* od, Address address, Addend addend) |
| 1749 | { |
| 1750 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
| 1751 | addend, false, true, false)); |
| 1752 | } |
| 1753 | |
| 1754 | void |
| 1755 | add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj, |
| 1756 | unsigned int local_sym_index, unsigned int type, |
| 1757 | Output_data* od, unsigned int shndx, |
| 1758 | Address address, Addend addend) |
| 1759 | { |
| 1760 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
| 1761 | address, addend, false, true, false)); |
| 1762 | } |
| 1763 | |
| 1764 | // Add a reloc against a local section symbol. This will be |
| 1765 | // converted into a reloc against the STT_SECTION symbol of the |
| 1766 | // output section. |
| 1767 | |
| 1768 | void |
| 1769 | add_local_section(Sized_relobj<size, big_endian>* relobj, |
| 1770 | unsigned int input_shndx, unsigned int type, |
| 1771 | Output_data* od, Address address, Addend addend) |
| 1772 | { |
| 1773 | this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address, |
| 1774 | addend, false, false, true)); |
| 1775 | } |
| 1776 | |
| 1777 | void |
| 1778 | add_local_section(Sized_relobj<size, big_endian>* relobj, |
| 1779 | unsigned int input_shndx, unsigned int type, |
| 1780 | Output_data* od, unsigned int shndx, Address address, |
| 1781 | Addend addend) |
| 1782 | { |
| 1783 | this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx, |
| 1784 | address, addend, false, false, true)); |
| 1785 | } |
| 1786 | |
| 1787 | // A reloc against the STT_SECTION symbol of an output section. |
| 1788 | |
| 1789 | void |
| 1790 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
| 1791 | Address address, Addend addend) |
| 1792 | { this->add(od, Output_reloc_type(os, type, od, address, addend)); } |
| 1793 | |
| 1794 | void |
| 1795 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
| 1796 | Sized_relobj<size, big_endian>* relobj, |
| 1797 | unsigned int shndx, Address address, Addend addend) |
| 1798 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, |
| 1799 | addend)); } |
| 1800 | |
| 1801 | // Add an absolute relocation. |
| 1802 | |
| 1803 | void |
| 1804 | add_absolute(unsigned int type, Output_data* od, Address address, |
| 1805 | Addend addend) |
| 1806 | { this->add(od, Output_reloc_type(type, od, address, addend)); } |
| 1807 | |
| 1808 | void |
| 1809 | add_absolute(unsigned int type, Output_data* od, |
| 1810 | Sized_relobj<size, big_endian>* relobj, |
| 1811 | unsigned int shndx, Address address, Addend addend) |
| 1812 | { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); } |
| 1813 | |
| 1814 | // Add a target specific relocation. A target which calls this must |
| 1815 | // define the reloc_symbol_index and reloc_addend virtual functions. |
| 1816 | |
| 1817 | void |
| 1818 | add_target_specific(unsigned int type, void* arg, Output_data* od, |
| 1819 | Address address, Addend addend) |
| 1820 | { this->add(od, Output_reloc_type(type, arg, od, address, addend)); } |
| 1821 | |
| 1822 | void |
| 1823 | add_target_specific(unsigned int type, void* arg, Output_data* od, |
| 1824 | Sized_relobj<size, big_endian>* relobj, |
| 1825 | unsigned int shndx, Address address, Addend addend) |
| 1826 | { |
| 1827 | this->add(od, Output_reloc_type(type, arg, relobj, shndx, address, |
| 1828 | addend)); |
| 1829 | } |
| 1830 | }; |
| 1831 | |
| 1832 | // Output_relocatable_relocs represents a relocation section in a |
| 1833 | // relocatable link. The actual data is written out in the target |
| 1834 | // hook relocate_for_relocatable. This just saves space for it. |
| 1835 | |
| 1836 | template<int sh_type, int size, bool big_endian> |
| 1837 | class Output_relocatable_relocs : public Output_section_data |
| 1838 | { |
| 1839 | public: |
| 1840 | Output_relocatable_relocs(Relocatable_relocs* rr) |
| 1841 | : Output_section_data(Output_data::default_alignment_for_size(size)), |
| 1842 | rr_(rr) |
| 1843 | { } |
| 1844 | |
| 1845 | void |
| 1846 | set_final_data_size(); |
| 1847 | |
| 1848 | // Write out the data. There is nothing to do here. |
| 1849 | void |
| 1850 | do_write(Output_file*) |
| 1851 | { } |
| 1852 | |
| 1853 | // Write to a map file. |
| 1854 | void |
| 1855 | do_print_to_mapfile(Mapfile* mapfile) const |
| 1856 | { mapfile->print_output_data(this, _("** relocs")); } |
| 1857 | |
| 1858 | private: |
| 1859 | // The relocs associated with this input section. |
| 1860 | Relocatable_relocs* rr_; |
| 1861 | }; |
| 1862 | |
| 1863 | // Handle a GROUP section. |
| 1864 | |
| 1865 | template<int size, bool big_endian> |
| 1866 | class Output_data_group : public Output_section_data |
| 1867 | { |
| 1868 | public: |
| 1869 | // The constructor clears *INPUT_SHNDXES. |
| 1870 | Output_data_group(Sized_relobj<size, big_endian>* relobj, |
| 1871 | section_size_type entry_count, |
| 1872 | elfcpp::Elf_Word flags, |
| 1873 | std::vector<unsigned int>* input_shndxes); |
| 1874 | |
| 1875 | void |
| 1876 | do_write(Output_file*); |
| 1877 | |
| 1878 | // Write to a map file. |
| 1879 | void |
| 1880 | do_print_to_mapfile(Mapfile* mapfile) const |
| 1881 | { mapfile->print_output_data(this, _("** group")); } |
| 1882 | |
| 1883 | // Set final data size. |
| 1884 | void |
| 1885 | set_final_data_size() |
| 1886 | { this->set_data_size((this->input_shndxes_.size() + 1) * 4); } |
| 1887 | |
| 1888 | private: |
| 1889 | // The input object. |
| 1890 | Sized_relobj<size, big_endian>* relobj_; |
| 1891 | // The group flag word. |
| 1892 | elfcpp::Elf_Word flags_; |
| 1893 | // The section indexes of the input sections in this group. |
| 1894 | std::vector<unsigned int> input_shndxes_; |
| 1895 | }; |
| 1896 | |
| 1897 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
| 1898 | // for one symbol--either a global symbol or a local symbol in an |
| 1899 | // object. The target specific code adds entries to the GOT as |
| 1900 | // needed. |
| 1901 | |
| 1902 | template<int size, bool big_endian> |
| 1903 | class Output_data_got : public Output_section_data_build |
| 1904 | { |
| 1905 | public: |
| 1906 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; |
| 1907 | typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn; |
| 1908 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn; |
| 1909 | |
| 1910 | Output_data_got() |
| 1911 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), |
| 1912 | entries_() |
| 1913 | { } |
| 1914 | |
| 1915 | // Add an entry for a global symbol to the GOT. Return true if this |
| 1916 | // is a new GOT entry, false if the symbol was already in the GOT. |
| 1917 | bool |
| 1918 | add_global(Symbol* gsym, unsigned int got_type); |
| 1919 | |
| 1920 | // Like add_global, but use the PLT offset of the global symbol if |
| 1921 | // it has one. |
| 1922 | bool |
| 1923 | add_global_plt(Symbol* gsym, unsigned int got_type); |
| 1924 | |
| 1925 | // Add an entry for a global symbol to the GOT, and add a dynamic |
| 1926 | // relocation of type R_TYPE for the GOT entry. |
| 1927 | void |
| 1928 | add_global_with_rel(Symbol* gsym, unsigned int got_type, |
| 1929 | Rel_dyn* rel_dyn, unsigned int r_type); |
| 1930 | |
| 1931 | void |
| 1932 | add_global_with_rela(Symbol* gsym, unsigned int got_type, |
| 1933 | Rela_dyn* rela_dyn, unsigned int r_type); |
| 1934 | |
| 1935 | // Add a pair of entries for a global symbol to the GOT, and add |
| 1936 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. |
| 1937 | void |
| 1938 | add_global_pair_with_rel(Symbol* gsym, unsigned int got_type, |
| 1939 | Rel_dyn* rel_dyn, unsigned int r_type_1, |
| 1940 | unsigned int r_type_2); |
| 1941 | |
| 1942 | void |
| 1943 | add_global_pair_with_rela(Symbol* gsym, unsigned int got_type, |
| 1944 | Rela_dyn* rela_dyn, unsigned int r_type_1, |
| 1945 | unsigned int r_type_2); |
| 1946 | |
| 1947 | // Add an entry for a local symbol to the GOT. This returns true if |
| 1948 | // this is a new GOT entry, false if the symbol already has a GOT |
| 1949 | // entry. |
| 1950 | bool |
| 1951 | add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index, |
| 1952 | unsigned int got_type); |
| 1953 | |
| 1954 | // Like add_local, but use the PLT offset of the local symbol if it |
| 1955 | // has one. |
| 1956 | bool |
| 1957 | add_local_plt(Sized_relobj<size, big_endian>* object, unsigned int sym_index, |
| 1958 | unsigned int got_type); |
| 1959 | |
| 1960 | // Add an entry for a local symbol to the GOT, and add a dynamic |
| 1961 | // relocation of type R_TYPE for the GOT entry. |
| 1962 | void |
| 1963 | add_local_with_rel(Sized_relobj<size, big_endian>* object, |
| 1964 | unsigned int sym_index, unsigned int got_type, |
| 1965 | Rel_dyn* rel_dyn, unsigned int r_type); |
| 1966 | |
| 1967 | void |
| 1968 | add_local_with_rela(Sized_relobj<size, big_endian>* object, |
| 1969 | unsigned int sym_index, unsigned int got_type, |
| 1970 | Rela_dyn* rela_dyn, unsigned int r_type); |
| 1971 | |
| 1972 | // Add a pair of entries for a local symbol to the GOT, and add |
| 1973 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. |
| 1974 | void |
| 1975 | add_local_pair_with_rel(Sized_relobj<size, big_endian>* object, |
| 1976 | unsigned int sym_index, unsigned int shndx, |
| 1977 | unsigned int got_type, Rel_dyn* rel_dyn, |
| 1978 | unsigned int r_type_1, unsigned int r_type_2); |
| 1979 | |
| 1980 | void |
| 1981 | add_local_pair_with_rela(Sized_relobj<size, big_endian>* object, |
| 1982 | unsigned int sym_index, unsigned int shndx, |
| 1983 | unsigned int got_type, Rela_dyn* rela_dyn, |
| 1984 | unsigned int r_type_1, unsigned int r_type_2); |
| 1985 | |
| 1986 | // Add a constant to the GOT. This returns the offset of the new |
| 1987 | // entry from the start of the GOT. |
| 1988 | unsigned int |
| 1989 | add_constant(Valtype constant) |
| 1990 | { |
| 1991 | this->entries_.push_back(Got_entry(constant)); |
| 1992 | this->set_got_size(); |
| 1993 | return this->last_got_offset(); |
| 1994 | } |
| 1995 | |
| 1996 | protected: |
| 1997 | // Write out the GOT table. |
| 1998 | void |
| 1999 | do_write(Output_file*); |
| 2000 | |
| 2001 | // Write to a map file. |
| 2002 | void |
| 2003 | do_print_to_mapfile(Mapfile* mapfile) const |
| 2004 | { mapfile->print_output_data(this, _("** GOT")); } |
| 2005 | |
| 2006 | private: |
| 2007 | // This POD class holds a single GOT entry. |
| 2008 | class Got_entry |
| 2009 | { |
| 2010 | public: |
| 2011 | // Create a zero entry. |
| 2012 | Got_entry() |
| 2013 | : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false) |
| 2014 | { this->u_.constant = 0; } |
| 2015 | |
| 2016 | // Create a global symbol entry. |
| 2017 | Got_entry(Symbol* gsym, bool use_plt_offset) |
| 2018 | : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset) |
| 2019 | { this->u_.gsym = gsym; } |
| 2020 | |
| 2021 | // Create a local symbol entry. |
| 2022 | Got_entry(Sized_relobj<size, big_endian>* object, |
| 2023 | unsigned int local_sym_index, bool use_plt_offset) |
| 2024 | : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset) |
| 2025 | { |
| 2026 | gold_assert(local_sym_index != GSYM_CODE |
| 2027 | && local_sym_index != CONSTANT_CODE |
| 2028 | && local_sym_index == this->local_sym_index_); |
| 2029 | this->u_.object = object; |
| 2030 | } |
| 2031 | |
| 2032 | // Create a constant entry. The constant is a host value--it will |
| 2033 | // be swapped, if necessary, when it is written out. |
| 2034 | explicit Got_entry(Valtype constant) |
| 2035 | : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false) |
| 2036 | { this->u_.constant = constant; } |
| 2037 | |
| 2038 | // Write the GOT entry to an output view. |
| 2039 | void |
| 2040 | write(unsigned char* pov) const; |
| 2041 | |
| 2042 | private: |
| 2043 | enum |
| 2044 | { |
| 2045 | GSYM_CODE = 0x7fffffff, |
| 2046 | CONSTANT_CODE = 0x7ffffffe |
| 2047 | }; |
| 2048 | |
| 2049 | union |
| 2050 | { |
| 2051 | // For a local symbol, the object. |
| 2052 | Sized_relobj<size, big_endian>* object; |
| 2053 | // For a global symbol, the symbol. |
| 2054 | Symbol* gsym; |
| 2055 | // For a constant, the constant. |
| 2056 | Valtype constant; |
| 2057 | } u_; |
| 2058 | // For a local symbol, the local symbol index. This is GSYM_CODE |
| 2059 | // for a global symbol, or CONSTANT_CODE for a constant. |
| 2060 | unsigned int local_sym_index_ : 31; |
| 2061 | // Whether to use the PLT offset of the symbol if it has one. |
| 2062 | bool use_plt_offset_ : 1; |
| 2063 | }; |
| 2064 | |
| 2065 | typedef std::vector<Got_entry> Got_entries; |
| 2066 | |
| 2067 | // Return the offset into the GOT of GOT entry I. |
| 2068 | unsigned int |
| 2069 | got_offset(unsigned int i) const |
| 2070 | { return i * (size / 8); } |
| 2071 | |
| 2072 | // Return the offset into the GOT of the last entry added. |
| 2073 | unsigned int |
| 2074 | last_got_offset() const |
| 2075 | { return this->got_offset(this->entries_.size() - 1); } |
| 2076 | |
| 2077 | // Set the size of the section. |
| 2078 | void |
| 2079 | set_got_size() |
| 2080 | { this->set_current_data_size(this->got_offset(this->entries_.size())); } |
| 2081 | |
| 2082 | // The list of GOT entries. |
| 2083 | Got_entries entries_; |
| 2084 | }; |
| 2085 | |
| 2086 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
| 2087 | // section. |
| 2088 | |
| 2089 | class Output_data_dynamic : public Output_section_data |
| 2090 | { |
| 2091 | public: |
| 2092 | Output_data_dynamic(Stringpool* pool) |
| 2093 | : Output_section_data(Output_data::default_alignment()), |
| 2094 | entries_(), pool_(pool) |
| 2095 | { } |
| 2096 | |
| 2097 | // Add a new dynamic entry with a fixed numeric value. |
| 2098 | void |
| 2099 | add_constant(elfcpp::DT tag, unsigned int val) |
| 2100 | { this->add_entry(Dynamic_entry(tag, val)); } |
| 2101 | |
| 2102 | // Add a new dynamic entry with the address of output data. |
| 2103 | void |
| 2104 | add_section_address(elfcpp::DT tag, const Output_data* od) |
| 2105 | { this->add_entry(Dynamic_entry(tag, od, false)); } |
| 2106 | |
| 2107 | // Add a new dynamic entry with the address of output data |
| 2108 | // plus a constant offset. |
| 2109 | void |
| 2110 | add_section_plus_offset(elfcpp::DT tag, const Output_data* od, |
| 2111 | unsigned int offset) |
| 2112 | { this->add_entry(Dynamic_entry(tag, od, offset)); } |
| 2113 | |
| 2114 | // Add a new dynamic entry with the size of output data. |
| 2115 | void |
| 2116 | add_section_size(elfcpp::DT tag, const Output_data* od) |
| 2117 | { this->add_entry(Dynamic_entry(tag, od, true)); } |
| 2118 | |
| 2119 | // Add a new dynamic entry with the total size of two output datas. |
| 2120 | void |
| 2121 | add_section_size(elfcpp::DT tag, const Output_data* od, |
| 2122 | const Output_data* od2) |
| 2123 | { this->add_entry(Dynamic_entry(tag, od, od2)); } |
| 2124 | |
| 2125 | // Add a new dynamic entry with the address of a symbol. |
| 2126 | void |
| 2127 | add_symbol(elfcpp::DT tag, const Symbol* sym) |
| 2128 | { this->add_entry(Dynamic_entry(tag, sym)); } |
| 2129 | |
| 2130 | // Add a new dynamic entry with a string. |
| 2131 | void |
| 2132 | add_string(elfcpp::DT tag, const char* str) |
| 2133 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); } |
| 2134 | |
| 2135 | void |
| 2136 | add_string(elfcpp::DT tag, const std::string& str) |
| 2137 | { this->add_string(tag, str.c_str()); } |
| 2138 | |
| 2139 | protected: |
| 2140 | // Adjust the output section to set the entry size. |
| 2141 | void |
| 2142 | do_adjust_output_section(Output_section*); |
| 2143 | |
| 2144 | // Set the final data size. |
| 2145 | void |
| 2146 | set_final_data_size(); |
| 2147 | |
| 2148 | // Write out the dynamic entries. |
| 2149 | void |
| 2150 | do_write(Output_file*); |
| 2151 | |
| 2152 | // Write to a map file. |
| 2153 | void |
| 2154 | do_print_to_mapfile(Mapfile* mapfile) const |
| 2155 | { mapfile->print_output_data(this, _("** dynamic")); } |
| 2156 | |
| 2157 | private: |
| 2158 | // This POD class holds a single dynamic entry. |
| 2159 | class Dynamic_entry |
| 2160 | { |
| 2161 | public: |
| 2162 | // Create an entry with a fixed numeric value. |
| 2163 | Dynamic_entry(elfcpp::DT tag, unsigned int val) |
| 2164 | : tag_(tag), offset_(DYNAMIC_NUMBER) |
| 2165 | { this->u_.val = val; } |
| 2166 | |
| 2167 | // Create an entry with the size or address of a section. |
| 2168 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size) |
| 2169 | : tag_(tag), |
| 2170 | offset_(section_size |
| 2171 | ? DYNAMIC_SECTION_SIZE |
| 2172 | : DYNAMIC_SECTION_ADDRESS) |
| 2173 | { |
| 2174 | this->u_.od = od; |
| 2175 | this->od2 = NULL; |
| 2176 | } |
| 2177 | |
| 2178 | // Create an entry with the size of two sections. |
| 2179 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2) |
| 2180 | : tag_(tag), |
| 2181 | offset_(DYNAMIC_SECTION_SIZE) |
| 2182 | { |
| 2183 | this->u_.od = od; |
| 2184 | this->od2 = od2; |
| 2185 | } |
| 2186 | |
| 2187 | // Create an entry with the address of a section plus a constant offset. |
| 2188 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset) |
| 2189 | : tag_(tag), |
| 2190 | offset_(offset) |
| 2191 | { this->u_.od = od; } |
| 2192 | |
| 2193 | // Create an entry with the address of a symbol. |
| 2194 | Dynamic_entry(elfcpp::DT tag, const Symbol* sym) |
| 2195 | : tag_(tag), offset_(DYNAMIC_SYMBOL) |
| 2196 | { this->u_.sym = sym; } |
| 2197 | |
| 2198 | // Create an entry with a string. |
| 2199 | Dynamic_entry(elfcpp::DT tag, const char* str) |
| 2200 | : tag_(tag), offset_(DYNAMIC_STRING) |
| 2201 | { this->u_.str = str; } |
| 2202 | |
| 2203 | // Return the tag of this entry. |
| 2204 | elfcpp::DT |
| 2205 | tag() const |
| 2206 | { return this->tag_; } |
| 2207 | |
| 2208 | // Write the dynamic entry to an output view. |
| 2209 | template<int size, bool big_endian> |
| 2210 | void |
| 2211 | write(unsigned char* pov, const Stringpool*) const; |
| 2212 | |
| 2213 | private: |
| 2214 | // Classification is encoded in the OFFSET field. |
| 2215 | enum Classification |
| 2216 | { |
| 2217 | // Section address. |
| 2218 | DYNAMIC_SECTION_ADDRESS = 0, |
| 2219 | // Number. |
| 2220 | DYNAMIC_NUMBER = -1U, |
| 2221 | // Section size. |
| 2222 | DYNAMIC_SECTION_SIZE = -2U, |
| 2223 | // Symbol adress. |
| 2224 | DYNAMIC_SYMBOL = -3U, |
| 2225 | // String. |
| 2226 | DYNAMIC_STRING = -4U |
| 2227 | // Any other value indicates a section address plus OFFSET. |
| 2228 | }; |
| 2229 | |
| 2230 | union |
| 2231 | { |
| 2232 | // For DYNAMIC_NUMBER. |
| 2233 | unsigned int val; |
| 2234 | // For DYNAMIC_SECTION_SIZE and section address plus OFFSET. |
| 2235 | const Output_data* od; |
| 2236 | // For DYNAMIC_SYMBOL. |
| 2237 | const Symbol* sym; |
| 2238 | // For DYNAMIC_STRING. |
| 2239 | const char* str; |
| 2240 | } u_; |
| 2241 | // For DYNAMIC_SYMBOL with two sections. |
| 2242 | const Output_data* od2; |
| 2243 | // The dynamic tag. |
| 2244 | elfcpp::DT tag_; |
| 2245 | // The type of entry (Classification) or offset within a section. |
| 2246 | unsigned int offset_; |
| 2247 | }; |
| 2248 | |
| 2249 | // Add an entry to the list. |
| 2250 | void |
| 2251 | add_entry(const Dynamic_entry& entry) |
| 2252 | { this->entries_.push_back(entry); } |
| 2253 | |
| 2254 | // Sized version of write function. |
| 2255 | template<int size, bool big_endian> |
| 2256 | void |
| 2257 | sized_write(Output_file* of); |
| 2258 | |
| 2259 | // The type of the list of entries. |
| 2260 | typedef std::vector<Dynamic_entry> Dynamic_entries; |
| 2261 | |
| 2262 | // The entries. |
| 2263 | Dynamic_entries entries_; |
| 2264 | // The pool used for strings. |
| 2265 | Stringpool* pool_; |
| 2266 | }; |
| 2267 | |
| 2268 | // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections, |
| 2269 | // which may be required if the object file has more than |
| 2270 | // SHN_LORESERVE sections. |
| 2271 | |
| 2272 | class Output_symtab_xindex : public Output_section_data |
| 2273 | { |
| 2274 | public: |
| 2275 | Output_symtab_xindex(size_t symcount) |
| 2276 | : Output_section_data(symcount * 4, 4, true), |
| 2277 | entries_() |
| 2278 | { } |
| 2279 | |
| 2280 | // Add an entry: symbol number SYMNDX has section SHNDX. |
| 2281 | void |
| 2282 | add(unsigned int symndx, unsigned int shndx) |
| 2283 | { this->entries_.push_back(std::make_pair(symndx, shndx)); } |
| 2284 | |
| 2285 | protected: |
| 2286 | void |
| 2287 | do_write(Output_file*); |
| 2288 | |
| 2289 | // Write to a map file. |
| 2290 | void |
| 2291 | do_print_to_mapfile(Mapfile* mapfile) const |
| 2292 | { mapfile->print_output_data(this, _("** symtab xindex")); } |
| 2293 | |
| 2294 | private: |
| 2295 | template<bool big_endian> |
| 2296 | void |
| 2297 | endian_do_write(unsigned char*); |
| 2298 | |
| 2299 | // It is likely that most symbols will not require entries. Rather |
| 2300 | // than keep a vector for all symbols, we keep pairs of symbol index |
| 2301 | // and section index. |
| 2302 | typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries; |
| 2303 | |
| 2304 | // The entries we need. |
| 2305 | Xindex_entries entries_; |
| 2306 | }; |
| 2307 | |
| 2308 | // A relaxed input section. |
| 2309 | class Output_relaxed_input_section : public Output_section_data_build |
| 2310 | { |
| 2311 | public: |
| 2312 | // We would like to call relobj->section_addralign(shndx) to get the |
| 2313 | // alignment but we do not want the constructor to fail. So callers |
| 2314 | // are repsonsible for ensuring that. |
| 2315 | Output_relaxed_input_section(Relobj* relobj, unsigned int shndx, |
| 2316 | uint64_t addralign) |
| 2317 | : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx) |
| 2318 | { } |
| 2319 | |
| 2320 | // Return the Relobj of this relaxed input section. |
| 2321 | Relobj* |
| 2322 | relobj() const |
| 2323 | { return this->relobj_; } |
| 2324 | |
| 2325 | // Return the section index of this relaxed input section. |
| 2326 | unsigned int |
| 2327 | shndx() const |
| 2328 | { return this->shndx_; } |
| 2329 | |
| 2330 | private: |
| 2331 | Relobj* relobj_; |
| 2332 | unsigned int shndx_; |
| 2333 | }; |
| 2334 | |
| 2335 | // This class describes properties of merge data sections. It is used |
| 2336 | // as a key type for maps. |
| 2337 | class Merge_section_properties |
| 2338 | { |
| 2339 | public: |
| 2340 | Merge_section_properties(bool is_string, uint64_t entsize, |
| 2341 | uint64_t addralign) |
| 2342 | : is_string_(is_string), entsize_(entsize), addralign_(addralign) |
| 2343 | { } |
| 2344 | |
| 2345 | // Whether this equals to another Merge_section_properties MSP. |
| 2346 | bool |
| 2347 | eq(const Merge_section_properties& msp) const |
| 2348 | { |
| 2349 | return ((this->is_string_ == msp.is_string_) |
| 2350 | && (this->entsize_ == msp.entsize_) |
| 2351 | && (this->addralign_ == msp.addralign_)); |
| 2352 | } |
| 2353 | |
| 2354 | // Compute a hash value for this using 64-bit FNV-1a hash. |
| 2355 | size_t |
| 2356 | hash_value() const |
| 2357 | { |
| 2358 | uint64_t h = 14695981039346656037ULL; // FNV offset basis. |
| 2359 | uint64_t prime = 1099511628211ULL; |
| 2360 | h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime; |
| 2361 | h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime; |
| 2362 | h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime; |
| 2363 | return h; |
| 2364 | } |
| 2365 | |
| 2366 | // Functors for associative containers. |
| 2367 | struct equal_to |
| 2368 | { |
| 2369 | bool |
| 2370 | operator()(const Merge_section_properties& msp1, |
| 2371 | const Merge_section_properties& msp2) const |
| 2372 | { return msp1.eq(msp2); } |
| 2373 | }; |
| 2374 | |
| 2375 | struct hash |
| 2376 | { |
| 2377 | size_t |
| 2378 | operator()(const Merge_section_properties& msp) const |
| 2379 | { return msp.hash_value(); } |
| 2380 | }; |
| 2381 | |
| 2382 | private: |
| 2383 | // Whether this merge data section is for strings. |
| 2384 | bool is_string_; |
| 2385 | // Entsize of this merge data section. |
| 2386 | uint64_t entsize_; |
| 2387 | // Address alignment. |
| 2388 | uint64_t addralign_; |
| 2389 | }; |
| 2390 | |
| 2391 | // This class is used to speed up look up of special input sections in an |
| 2392 | // Output_section. |
| 2393 | |
| 2394 | class Output_section_lookup_maps |
| 2395 | { |
| 2396 | public: |
| 2397 | Output_section_lookup_maps() |
| 2398 | : is_valid_(true), merge_sections_by_properties_(), |
| 2399 | merge_sections_by_id_(), relaxed_input_sections_by_id_() |
| 2400 | { } |
| 2401 | |
| 2402 | // Whether the maps are valid. |
| 2403 | bool |
| 2404 | is_valid() const |
| 2405 | { return this->is_valid_; } |
| 2406 | |
| 2407 | // Invalidate the maps. |
| 2408 | void |
| 2409 | invalidate() |
| 2410 | { this->is_valid_ = false; } |
| 2411 | |
| 2412 | // Clear the maps. |
| 2413 | void |
| 2414 | clear() |
| 2415 | { |
| 2416 | this->merge_sections_by_properties_.clear(); |
| 2417 | this->merge_sections_by_id_.clear(); |
| 2418 | this->relaxed_input_sections_by_id_.clear(); |
| 2419 | // A cleared map is valid. |
| 2420 | this->is_valid_ = true; |
| 2421 | } |
| 2422 | |
| 2423 | // Find a merge section by merge section properties. Return NULL if none |
| 2424 | // is found. |
| 2425 | Output_merge_base* |
| 2426 | find_merge_section(const Merge_section_properties& msp) const |
| 2427 | { |
| 2428 | gold_assert(this->is_valid_); |
| 2429 | Merge_sections_by_properties::const_iterator p = |
| 2430 | this->merge_sections_by_properties_.find(msp); |
| 2431 | return p != this->merge_sections_by_properties_.end() ? p->second : NULL; |
| 2432 | } |
| 2433 | |
| 2434 | // Find a merge section by section ID of a merge input section. Return NULL |
| 2435 | // if none is found. |
| 2436 | Output_merge_base* |
| 2437 | find_merge_section(const Object* object, unsigned int shndx) const |
| 2438 | { |
| 2439 | gold_assert(this->is_valid_); |
| 2440 | Merge_sections_by_id::const_iterator p = |
| 2441 | this->merge_sections_by_id_.find(Const_section_id(object, shndx)); |
| 2442 | return p != this->merge_sections_by_id_.end() ? p->second : NULL; |
| 2443 | } |
| 2444 | |
| 2445 | // Add a merge section pointed by POMB with properties MSP. |
| 2446 | void |
| 2447 | add_merge_section(const Merge_section_properties& msp, |
| 2448 | Output_merge_base* pomb) |
| 2449 | { |
| 2450 | std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb); |
| 2451 | std::pair<Merge_sections_by_properties::iterator, bool> result = |
| 2452 | this->merge_sections_by_properties_.insert(value); |
| 2453 | gold_assert(result.second); |
| 2454 | } |
| 2455 | |
| 2456 | // Add a mapping from a merged input section in OBJECT with index SHNDX |
| 2457 | // to a merge output section pointed by POMB. |
| 2458 | void |
| 2459 | add_merge_input_section(const Object* object, unsigned int shndx, |
| 2460 | Output_merge_base* pomb) |
| 2461 | { |
| 2462 | Const_section_id csid(object, shndx); |
| 2463 | std::pair<Const_section_id, Output_merge_base*> value(csid, pomb); |
| 2464 | std::pair<Merge_sections_by_id::iterator, bool> result = |
| 2465 | this->merge_sections_by_id_.insert(value); |
| 2466 | gold_assert(result.second); |
| 2467 | } |
| 2468 | |
| 2469 | // Find a relaxed input section of OBJECT with index SHNDX. |
| 2470 | Output_relaxed_input_section* |
| 2471 | find_relaxed_input_section(const Object* object, unsigned int shndx) const |
| 2472 | { |
| 2473 | gold_assert(this->is_valid_); |
| 2474 | Relaxed_input_sections_by_id::const_iterator p = |
| 2475 | this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx)); |
| 2476 | return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL; |
| 2477 | } |
| 2478 | |
| 2479 | // Add a relaxed input section pointed by POMB and whose original input |
| 2480 | // section is in OBJECT with index SHNDX. |
| 2481 | void |
| 2482 | add_relaxed_input_section(const Relobj* relobj, unsigned int shndx, |
| 2483 | Output_relaxed_input_section* poris) |
| 2484 | { |
| 2485 | Const_section_id csid(relobj, shndx); |
| 2486 | std::pair<Const_section_id, Output_relaxed_input_section*> |
| 2487 | value(csid, poris); |
| 2488 | std::pair<Relaxed_input_sections_by_id::iterator, bool> result = |
| 2489 | this->relaxed_input_sections_by_id_.insert(value); |
| 2490 | gold_assert(result.second); |
| 2491 | } |
| 2492 | |
| 2493 | private: |
| 2494 | typedef Unordered_map<Const_section_id, Output_merge_base*, |
| 2495 | Const_section_id_hash> |
| 2496 | Merge_sections_by_id; |
| 2497 | |
| 2498 | typedef Unordered_map<Merge_section_properties, Output_merge_base*, |
| 2499 | Merge_section_properties::hash, |
| 2500 | Merge_section_properties::equal_to> |
| 2501 | Merge_sections_by_properties; |
| 2502 | |
| 2503 | typedef Unordered_map<Const_section_id, Output_relaxed_input_section*, |
| 2504 | Const_section_id_hash> |
| 2505 | Relaxed_input_sections_by_id; |
| 2506 | |
| 2507 | // Whether this is valid |
| 2508 | bool is_valid_; |
| 2509 | // Merge sections by merge section properties. |
| 2510 | Merge_sections_by_properties merge_sections_by_properties_; |
| 2511 | // Merge sections by section IDs. |
| 2512 | Merge_sections_by_id merge_sections_by_id_; |
| 2513 | // Relaxed sections by section IDs. |
| 2514 | Relaxed_input_sections_by_id relaxed_input_sections_by_id_; |
| 2515 | }; |
| 2516 | |
| 2517 | // An output section. We don't expect to have too many output |
| 2518 | // sections, so we don't bother to do a template on the size. |
| 2519 | |
| 2520 | class Output_section : public Output_data |
| 2521 | { |
| 2522 | public: |
| 2523 | // Create an output section, giving the name, type, and flags. |
| 2524 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); |
| 2525 | virtual ~Output_section(); |
| 2526 | |
| 2527 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
| 2528 | // object OBJECT. RELOC_SHNDX is the index of a relocation section |
| 2529 | // which applies to this section, or 0 if none, or -1 if more than |
| 2530 | // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause |
| 2531 | // in a linker script; in that case we need to keep track of input |
| 2532 | // sections associated with an output section. Return the offset |
| 2533 | // within the output section. |
| 2534 | template<int size, bool big_endian> |
| 2535 | off_t |
| 2536 | add_input_section(Layout* layout, Sized_relobj<size, big_endian>* object, |
| 2537 | unsigned int shndx, const char* name, |
| 2538 | const elfcpp::Shdr<size, big_endian>& shdr, |
| 2539 | unsigned int reloc_shndx, bool have_sections_script); |
| 2540 | |
| 2541 | // Add generated data POSD to this output section. |
| 2542 | void |
| 2543 | add_output_section_data(Output_section_data* posd); |
| 2544 | |
| 2545 | // Add a relaxed input section PORIS called NAME to this output section |
| 2546 | // with LAYOUT. |
| 2547 | void |
| 2548 | add_relaxed_input_section(Layout* layout, |
| 2549 | Output_relaxed_input_section* poris, |
| 2550 | const std::string& name); |
| 2551 | |
| 2552 | // Return the section name. |
| 2553 | const char* |
| 2554 | name() const |
| 2555 | { return this->name_; } |
| 2556 | |
| 2557 | // Return the section type. |
| 2558 | elfcpp::Elf_Word |
| 2559 | type() const |
| 2560 | { return this->type_; } |
| 2561 | |
| 2562 | // Return the section flags. |
| 2563 | elfcpp::Elf_Xword |
| 2564 | flags() const |
| 2565 | { return this->flags_; } |
| 2566 | |
| 2567 | // Update the output section flags based on input section flags. |
| 2568 | void |
| 2569 | update_flags_for_input_section(elfcpp::Elf_Xword flags); |
| 2570 | |
| 2571 | // Return the entsize field. |
| 2572 | uint64_t |
| 2573 | entsize() const |
| 2574 | { return this->entsize_; } |
| 2575 | |
| 2576 | // Set the entsize field. |
| 2577 | void |
| 2578 | set_entsize(uint64_t v); |
| 2579 | |
| 2580 | // Set the load address. |
| 2581 | void |
| 2582 | set_load_address(uint64_t load_address) |
| 2583 | { |
| 2584 | this->load_address_ = load_address; |
| 2585 | this->has_load_address_ = true; |
| 2586 | } |
| 2587 | |
| 2588 | // Set the link field to the output section index of a section. |
| 2589 | void |
| 2590 | set_link_section(const Output_data* od) |
| 2591 | { |
| 2592 | gold_assert(this->link_ == 0 |
| 2593 | && !this->should_link_to_symtab_ |
| 2594 | && !this->should_link_to_dynsym_); |
| 2595 | this->link_section_ = od; |
| 2596 | } |
| 2597 | |
| 2598 | // Set the link field to a constant. |
| 2599 | void |
| 2600 | set_link(unsigned int v) |
| 2601 | { |
| 2602 | gold_assert(this->link_section_ == NULL |
| 2603 | && !this->should_link_to_symtab_ |
| 2604 | && !this->should_link_to_dynsym_); |
| 2605 | this->link_ = v; |
| 2606 | } |
| 2607 | |
| 2608 | // Record that this section should link to the normal symbol table. |
| 2609 | void |
| 2610 | set_should_link_to_symtab() |
| 2611 | { |
| 2612 | gold_assert(this->link_section_ == NULL |
| 2613 | && this->link_ == 0 |
| 2614 | && !this->should_link_to_dynsym_); |
| 2615 | this->should_link_to_symtab_ = true; |
| 2616 | } |
| 2617 | |
| 2618 | // Record that this section should link to the dynamic symbol table. |
| 2619 | void |
| 2620 | set_should_link_to_dynsym() |
| 2621 | { |
| 2622 | gold_assert(this->link_section_ == NULL |
| 2623 | && this->link_ == 0 |
| 2624 | && !this->should_link_to_symtab_); |
| 2625 | this->should_link_to_dynsym_ = true; |
| 2626 | } |
| 2627 | |
| 2628 | // Return the info field. |
| 2629 | unsigned int |
| 2630 | info() const |
| 2631 | { |
| 2632 | gold_assert(this->info_section_ == NULL |
| 2633 | && this->info_symndx_ == NULL); |
| 2634 | return this->info_; |
| 2635 | } |
| 2636 | |
| 2637 | // Set the info field to the output section index of a section. |
| 2638 | void |
| 2639 | set_info_section(const Output_section* os) |
| 2640 | { |
| 2641 | gold_assert((this->info_section_ == NULL |
| 2642 | || (this->info_section_ == os |
| 2643 | && this->info_uses_section_index_)) |
| 2644 | && this->info_symndx_ == NULL |
| 2645 | && this->info_ == 0); |
| 2646 | this->info_section_ = os; |
| 2647 | this->info_uses_section_index_= true; |
| 2648 | } |
| 2649 | |
| 2650 | // Set the info field to the symbol table index of a symbol. |
| 2651 | void |
| 2652 | set_info_symndx(const Symbol* sym) |
| 2653 | { |
| 2654 | gold_assert(this->info_section_ == NULL |
| 2655 | && (this->info_symndx_ == NULL |
| 2656 | || this->info_symndx_ == sym) |
| 2657 | && this->info_ == 0); |
| 2658 | this->info_symndx_ = sym; |
| 2659 | } |
| 2660 | |
| 2661 | // Set the info field to the symbol table index of a section symbol. |
| 2662 | void |
| 2663 | set_info_section_symndx(const Output_section* os) |
| 2664 | { |
| 2665 | gold_assert((this->info_section_ == NULL |
| 2666 | || (this->info_section_ == os |
| 2667 | && !this->info_uses_section_index_)) |
| 2668 | && this->info_symndx_ == NULL |
| 2669 | && this->info_ == 0); |
| 2670 | this->info_section_ = os; |
| 2671 | this->info_uses_section_index_ = false; |
| 2672 | } |
| 2673 | |
| 2674 | // Set the info field to a constant. |
| 2675 | void |
| 2676 | set_info(unsigned int v) |
| 2677 | { |
| 2678 | gold_assert(this->info_section_ == NULL |
| 2679 | && this->info_symndx_ == NULL |
| 2680 | && (this->info_ == 0 |
| 2681 | || this->info_ == v)); |
| 2682 | this->info_ = v; |
| 2683 | } |
| 2684 | |
| 2685 | // Set the addralign field. |
| 2686 | void |
| 2687 | set_addralign(uint64_t v) |
| 2688 | { this->addralign_ = v; } |
| 2689 | |
| 2690 | // Whether the output section index has been set. |
| 2691 | bool |
| 2692 | has_out_shndx() const |
| 2693 | { return this->out_shndx_ != -1U; } |
| 2694 | |
| 2695 | // Indicate that we need a symtab index. |
| 2696 | void |
| 2697 | set_needs_symtab_index() |
| 2698 | { this->needs_symtab_index_ = true; } |
| 2699 | |
| 2700 | // Return whether we need a symtab index. |
| 2701 | bool |
| 2702 | needs_symtab_index() const |
| 2703 | { return this->needs_symtab_index_; } |
| 2704 | |
| 2705 | // Get the symtab index. |
| 2706 | unsigned int |
| 2707 | symtab_index() const |
| 2708 | { |
| 2709 | gold_assert(this->symtab_index_ != 0); |
| 2710 | return this->symtab_index_; |
| 2711 | } |
| 2712 | |
| 2713 | // Set the symtab index. |
| 2714 | void |
| 2715 | set_symtab_index(unsigned int index) |
| 2716 | { |
| 2717 | gold_assert(index != 0); |
| 2718 | this->symtab_index_ = index; |
| 2719 | } |
| 2720 | |
| 2721 | // Indicate that we need a dynsym index. |
| 2722 | void |
| 2723 | set_needs_dynsym_index() |
| 2724 | { this->needs_dynsym_index_ = true; } |
| 2725 | |
| 2726 | // Return whether we need a dynsym index. |
| 2727 | bool |
| 2728 | needs_dynsym_index() const |
| 2729 | { return this->needs_dynsym_index_; } |
| 2730 | |
| 2731 | // Get the dynsym index. |
| 2732 | unsigned int |
| 2733 | dynsym_index() const |
| 2734 | { |
| 2735 | gold_assert(this->dynsym_index_ != 0); |
| 2736 | return this->dynsym_index_; |
| 2737 | } |
| 2738 | |
| 2739 | // Set the dynsym index. |
| 2740 | void |
| 2741 | set_dynsym_index(unsigned int index) |
| 2742 | { |
| 2743 | gold_assert(index != 0); |
| 2744 | this->dynsym_index_ = index; |
| 2745 | } |
| 2746 | |
| 2747 | // Return whether the input sections sections attachd to this output |
| 2748 | // section may require sorting. This is used to handle constructor |
| 2749 | // priorities compatibly with GNU ld. |
| 2750 | bool |
| 2751 | may_sort_attached_input_sections() const |
| 2752 | { return this->may_sort_attached_input_sections_; } |
| 2753 | |
| 2754 | // Record that the input sections attached to this output section |
| 2755 | // may require sorting. |
| 2756 | void |
| 2757 | set_may_sort_attached_input_sections() |
| 2758 | { this->may_sort_attached_input_sections_ = true; } |
| 2759 | |
| 2760 | // Returns true if input sections must be sorted according to the |
| 2761 | // order in which their name appear in the --section-ordering-file. |
| 2762 | bool |
| 2763 | input_section_order_specified() |
| 2764 | { return this->input_section_order_specified_; } |
| 2765 | |
| 2766 | // Record that input sections must be sorted as some of their names |
| 2767 | // match the patterns specified through --section-ordering-file. |
| 2768 | void |
| 2769 | set_input_section_order_specified() |
| 2770 | { this->input_section_order_specified_ = true; } |
| 2771 | |
| 2772 | // Return whether the input sections attached to this output section |
| 2773 | // require sorting. This is used to handle constructor priorities |
| 2774 | // compatibly with GNU ld. |
| 2775 | bool |
| 2776 | must_sort_attached_input_sections() const |
| 2777 | { return this->must_sort_attached_input_sections_; } |
| 2778 | |
| 2779 | // Record that the input sections attached to this output section |
| 2780 | // require sorting. |
| 2781 | void |
| 2782 | set_must_sort_attached_input_sections() |
| 2783 | { this->must_sort_attached_input_sections_ = true; } |
| 2784 | |
| 2785 | // Get the order in which this section appears in the PT_LOAD output |
| 2786 | // segment. |
| 2787 | Output_section_order |
| 2788 | order() const |
| 2789 | { return this->order_; } |
| 2790 | |
| 2791 | // Set the order for this section. |
| 2792 | void |
| 2793 | set_order(Output_section_order order) |
| 2794 | { this->order_ = order; } |
| 2795 | |
| 2796 | // Return whether this section holds relro data--data which has |
| 2797 | // dynamic relocations but which may be marked read-only after the |
| 2798 | // dynamic relocations have been completed. |
| 2799 | bool |
| 2800 | is_relro() const |
| 2801 | { return this->is_relro_; } |
| 2802 | |
| 2803 | // Record that this section holds relro data. |
| 2804 | void |
| 2805 | set_is_relro() |
| 2806 | { this->is_relro_ = true; } |
| 2807 | |
| 2808 | // Record that this section does not hold relro data. |
| 2809 | void |
| 2810 | clear_is_relro() |
| 2811 | { this->is_relro_ = false; } |
| 2812 | |
| 2813 | // True if this is a small section: a section which holds small |
| 2814 | // variables. |
| 2815 | bool |
| 2816 | is_small_section() const |
| 2817 | { return this->is_small_section_; } |
| 2818 | |
| 2819 | // Record that this is a small section. |
| 2820 | void |
| 2821 | set_is_small_section() |
| 2822 | { this->is_small_section_ = true; } |
| 2823 | |
| 2824 | // True if this is a large section: a section which holds large |
| 2825 | // variables. |
| 2826 | bool |
| 2827 | is_large_section() const |
| 2828 | { return this->is_large_section_; } |
| 2829 | |
| 2830 | // Record that this is a large section. |
| 2831 | void |
| 2832 | set_is_large_section() |
| 2833 | { this->is_large_section_ = true; } |
| 2834 | |
| 2835 | // True if this is a large data (not BSS) section. |
| 2836 | bool |
| 2837 | is_large_data_section() |
| 2838 | { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; } |
| 2839 | |
| 2840 | // Return whether this section should be written after all the input |
| 2841 | // sections are complete. |
| 2842 | bool |
| 2843 | after_input_sections() const |
| 2844 | { return this->after_input_sections_; } |
| 2845 | |
| 2846 | // Record that this section should be written after all the input |
| 2847 | // sections are complete. |
| 2848 | void |
| 2849 | set_after_input_sections() |
| 2850 | { this->after_input_sections_ = true; } |
| 2851 | |
| 2852 | // Return whether this section requires postprocessing after all |
| 2853 | // relocations have been applied. |
| 2854 | bool |
| 2855 | requires_postprocessing() const |
| 2856 | { return this->requires_postprocessing_; } |
| 2857 | |
| 2858 | // If a section requires postprocessing, return the buffer to use. |
| 2859 | unsigned char* |
| 2860 | postprocessing_buffer() const |
| 2861 | { |
| 2862 | gold_assert(this->postprocessing_buffer_ != NULL); |
| 2863 | return this->postprocessing_buffer_; |
| 2864 | } |
| 2865 | |
| 2866 | // If a section requires postprocessing, create the buffer to use. |
| 2867 | void |
| 2868 | create_postprocessing_buffer(); |
| 2869 | |
| 2870 | // If a section requires postprocessing, this is the size of the |
| 2871 | // buffer to which relocations should be applied. |
| 2872 | off_t |
| 2873 | postprocessing_buffer_size() const |
| 2874 | { return this->current_data_size_for_child(); } |
| 2875 | |
| 2876 | // Modify the section name. This is only permitted for an |
| 2877 | // unallocated section, and only before the size has been finalized. |
| 2878 | // Otherwise the name will not get into Layout::namepool_. |
| 2879 | void |
| 2880 | set_name(const char* newname) |
| 2881 | { |
| 2882 | gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0); |
| 2883 | gold_assert(!this->is_data_size_valid()); |
| 2884 | this->name_ = newname; |
| 2885 | } |
| 2886 | |
| 2887 | // Return whether the offset OFFSET in the input section SHNDX in |
| 2888 | // object OBJECT is being included in the link. |
| 2889 | bool |
| 2890 | is_input_address_mapped(const Relobj* object, unsigned int shndx, |
| 2891 | off_t offset) const; |
| 2892 | |
| 2893 | // Return the offset within the output section of OFFSET relative to |
| 2894 | // the start of input section SHNDX in object OBJECT. |
| 2895 | section_offset_type |
| 2896 | output_offset(const Relobj* object, unsigned int shndx, |
| 2897 | section_offset_type offset) const; |
| 2898 | |
| 2899 | // Return the output virtual address of OFFSET relative to the start |
| 2900 | // of input section SHNDX in object OBJECT. |
| 2901 | uint64_t |
| 2902 | output_address(const Relobj* object, unsigned int shndx, |
| 2903 | off_t offset) const; |
| 2904 | |
| 2905 | // Look for the merged section for input section SHNDX in object |
| 2906 | // OBJECT. If found, return true, and set *ADDR to the address of |
| 2907 | // the start of the merged section. This is not necessary the |
| 2908 | // output offset corresponding to input offset 0 in the section, |
| 2909 | // since the section may be mapped arbitrarily. |
| 2910 | bool |
| 2911 | find_starting_output_address(const Relobj* object, unsigned int shndx, |
| 2912 | uint64_t* addr) const; |
| 2913 | |
| 2914 | // Record that this output section was found in the SECTIONS clause |
| 2915 | // of a linker script. |
| 2916 | void |
| 2917 | set_found_in_sections_clause() |
| 2918 | { this->found_in_sections_clause_ = true; } |
| 2919 | |
| 2920 | // Return whether this output section was found in the SECTIONS |
| 2921 | // clause of a linker script. |
| 2922 | bool |
| 2923 | found_in_sections_clause() const |
| 2924 | { return this->found_in_sections_clause_; } |
| 2925 | |
| 2926 | // Write the section header into *OPHDR. |
| 2927 | template<int size, bool big_endian> |
| 2928 | void |
| 2929 | write_header(const Layout*, const Stringpool*, |
| 2930 | elfcpp::Shdr_write<size, big_endian>*) const; |
| 2931 | |
| 2932 | // The next few calls are for linker script support. |
| 2933 | |
| 2934 | // In some cases we need to keep a list of the input sections |
| 2935 | // associated with this output section. We only need the list if we |
| 2936 | // might have to change the offsets of the input section within the |
| 2937 | // output section after we add the input section. The ordinary |
| 2938 | // input sections will be written out when we process the object |
| 2939 | // file, and as such we don't need to track them here. We do need |
| 2940 | // to track Output_section_data objects here. We store instances of |
| 2941 | // this structure in a std::vector, so it must be a POD. There can |
| 2942 | // be many instances of this structure, so we use a union to save |
| 2943 | // some space. |
| 2944 | class Input_section |
| 2945 | { |
| 2946 | public: |
| 2947 | Input_section() |
| 2948 | : shndx_(0), p2align_(0) |
| 2949 | { |
| 2950 | this->u1_.data_size = 0; |
| 2951 | this->u2_.object = NULL; |
| 2952 | } |
| 2953 | |
| 2954 | // For an ordinary input section. |
| 2955 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
| 2956 | uint64_t addralign) |
| 2957 | : shndx_(shndx), |
| 2958 | p2align_(ffsll(static_cast<long long>(addralign))), |
| 2959 | section_order_index_(0) |
| 2960 | { |
| 2961 | gold_assert(shndx != OUTPUT_SECTION_CODE |
| 2962 | && shndx != MERGE_DATA_SECTION_CODE |
| 2963 | && shndx != MERGE_STRING_SECTION_CODE |
| 2964 | && shndx != RELAXED_INPUT_SECTION_CODE); |
| 2965 | this->u1_.data_size = data_size; |
| 2966 | this->u2_.object = object; |
| 2967 | } |
| 2968 | |
| 2969 | // For a non-merge output section. |
| 2970 | Input_section(Output_section_data* posd) |
| 2971 | : shndx_(OUTPUT_SECTION_CODE), p2align_(0), |
| 2972 | section_order_index_(0) |
| 2973 | { |
| 2974 | this->u1_.data_size = 0; |
| 2975 | this->u2_.posd = posd; |
| 2976 | } |
| 2977 | |
| 2978 | // For a merge section. |
| 2979 | Input_section(Output_section_data* posd, bool is_string, uint64_t entsize) |
| 2980 | : shndx_(is_string |
| 2981 | ? MERGE_STRING_SECTION_CODE |
| 2982 | : MERGE_DATA_SECTION_CODE), |
| 2983 | p2align_(0), |
| 2984 | section_order_index_(0) |
| 2985 | { |
| 2986 | this->u1_.entsize = entsize; |
| 2987 | this->u2_.posd = posd; |
| 2988 | } |
| 2989 | |
| 2990 | // For a relaxed input section. |
| 2991 | Input_section(Output_relaxed_input_section* psection) |
| 2992 | : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0), |
| 2993 | section_order_index_(0) |
| 2994 | { |
| 2995 | this->u1_.data_size = 0; |
| 2996 | this->u2_.poris = psection; |
| 2997 | } |
| 2998 | |
| 2999 | unsigned int |
| 3000 | section_order_index() const |
| 3001 | { |
| 3002 | return this->section_order_index_; |
| 3003 | } |
| 3004 | |
| 3005 | void |
| 3006 | set_section_order_index(unsigned int number) |
| 3007 | { |
| 3008 | this->section_order_index_ = number; |
| 3009 | } |
| 3010 | |
| 3011 | // The required alignment. |
| 3012 | uint64_t |
| 3013 | addralign() const |
| 3014 | { |
| 3015 | if (this->p2align_ != 0) |
| 3016 | return static_cast<uint64_t>(1) << (this->p2align_ - 1); |
| 3017 | else if (!this->is_input_section()) |
| 3018 | return this->u2_.posd->addralign(); |
| 3019 | else |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | // Set the required alignment, which must be either 0 or a power of 2. |
| 3024 | // For input sections that are sub-classes of Output_section_data, a |
| 3025 | // alignment of zero means asking the underlying object for alignment. |
| 3026 | void |
| 3027 | set_addralign(uint64_t addralign) |
| 3028 | { |
| 3029 | if (addralign == 0) |
| 3030 | this->p2align_ = 0; |
| 3031 | else |
| 3032 | { |
| 3033 | gold_assert((addralign & (addralign - 1)) == 0); |
| 3034 | this->p2align_ = ffsll(static_cast<long long>(addralign)); |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | // Return the required size. |
| 3039 | off_t |
| 3040 | data_size() const; |
| 3041 | |
| 3042 | // Whether this is an input section. |
| 3043 | bool |
| 3044 | is_input_section() const |
| 3045 | { |
| 3046 | return (this->shndx_ != OUTPUT_SECTION_CODE |
| 3047 | && this->shndx_ != MERGE_DATA_SECTION_CODE |
| 3048 | && this->shndx_ != MERGE_STRING_SECTION_CODE |
| 3049 | && this->shndx_ != RELAXED_INPUT_SECTION_CODE); |
| 3050 | } |
| 3051 | |
| 3052 | // Return whether this is a merge section which matches the |
| 3053 | // parameters. |
| 3054 | bool |
| 3055 | is_merge_section(bool is_string, uint64_t entsize, |
| 3056 | uint64_t addralign) const |
| 3057 | { |
| 3058 | return (this->shndx_ == (is_string |
| 3059 | ? MERGE_STRING_SECTION_CODE |
| 3060 | : MERGE_DATA_SECTION_CODE) |
| 3061 | && this->u1_.entsize == entsize |
| 3062 | && this->addralign() == addralign); |
| 3063 | } |
| 3064 | |
| 3065 | // Return whether this is a merge section for some input section. |
| 3066 | bool |
| 3067 | is_merge_section() const |
| 3068 | { |
| 3069 | return (this->shndx_ == MERGE_DATA_SECTION_CODE |
| 3070 | || this->shndx_ == MERGE_STRING_SECTION_CODE); |
| 3071 | } |
| 3072 | |
| 3073 | // Return whether this is a relaxed input section. |
| 3074 | bool |
| 3075 | is_relaxed_input_section() const |
| 3076 | { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; } |
| 3077 | |
| 3078 | // Return whether this is a generic Output_section_data. |
| 3079 | bool |
| 3080 | is_output_section_data() const |
| 3081 | { |
| 3082 | return this->shndx_ == OUTPUT_SECTION_CODE; |
| 3083 | } |
| 3084 | |
| 3085 | // Return the object for an input section. |
| 3086 | Relobj* |
| 3087 | relobj() const; |
| 3088 | |
| 3089 | // Return the input section index for an input section. |
| 3090 | unsigned int |
| 3091 | shndx() const; |
| 3092 | |
| 3093 | // For non-input-sections, return the associated Output_section_data |
| 3094 | // object. |
| 3095 | Output_section_data* |
| 3096 | output_section_data() const |
| 3097 | { |
| 3098 | gold_assert(!this->is_input_section()); |
| 3099 | return this->u2_.posd; |
| 3100 | } |
| 3101 | |
| 3102 | // For a merge section, return the Output_merge_base pointer. |
| 3103 | Output_merge_base* |
| 3104 | output_merge_base() const |
| 3105 | { |
| 3106 | gold_assert(this->is_merge_section()); |
| 3107 | return this->u2_.pomb; |
| 3108 | } |
| 3109 | |
| 3110 | // Return the Output_relaxed_input_section object. |
| 3111 | Output_relaxed_input_section* |
| 3112 | relaxed_input_section() const |
| 3113 | { |
| 3114 | gold_assert(this->is_relaxed_input_section()); |
| 3115 | return this->u2_.poris; |
| 3116 | } |
| 3117 | |
| 3118 | // Set the output section. |
| 3119 | void |
| 3120 | set_output_section(Output_section* os) |
| 3121 | { |
| 3122 | gold_assert(!this->is_input_section()); |
| 3123 | Output_section_data* posd = |
| 3124 | this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd; |
| 3125 | posd->set_output_section(os); |
| 3126 | } |
| 3127 | |
| 3128 | // Set the address and file offset. This is called during |
| 3129 | // Layout::finalize. SECTION_FILE_OFFSET is the file offset of |
| 3130 | // the enclosing section. |
| 3131 | void |
| 3132 | set_address_and_file_offset(uint64_t address, off_t file_offset, |
| 3133 | off_t section_file_offset); |
| 3134 | |
| 3135 | // Reset the address and file offset. |
| 3136 | void |
| 3137 | reset_address_and_file_offset(); |
| 3138 | |
| 3139 | // Finalize the data size. |
| 3140 | void |
| 3141 | finalize_data_size(); |
| 3142 | |
| 3143 | // Add an input section, for SHF_MERGE sections. |
| 3144 | bool |
| 3145 | add_input_section(Relobj* object, unsigned int shndx) |
| 3146 | { |
| 3147 | gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE |
| 3148 | || this->shndx_ == MERGE_STRING_SECTION_CODE); |
| 3149 | return this->u2_.posd->add_input_section(object, shndx); |
| 3150 | } |
| 3151 | |
| 3152 | // Given an input OBJECT, an input section index SHNDX within that |
| 3153 | // object, and an OFFSET relative to the start of that input |
| 3154 | // section, return whether or not the output offset is known. If |
| 3155 | // this function returns true, it sets *POUTPUT to the offset in |
| 3156 | // the output section, relative to the start of the input section |
| 3157 | // in the output section. *POUTPUT may be different from OFFSET |
| 3158 | // for a merged section. |
| 3159 | bool |
| 3160 | output_offset(const Relobj* object, unsigned int shndx, |
| 3161 | section_offset_type offset, |
| 3162 | section_offset_type* poutput) const; |
| 3163 | |
| 3164 | // Return whether this is the merge section for the input section |
| 3165 | // SHNDX in OBJECT. |
| 3166 | bool |
| 3167 | is_merge_section_for(const Relobj* object, unsigned int shndx) const; |
| 3168 | |
| 3169 | // Write out the data. This does nothing for an input section. |
| 3170 | void |
| 3171 | write(Output_file*); |
| 3172 | |
| 3173 | // Write the data to a buffer. This does nothing for an input |
| 3174 | // section. |
| 3175 | void |
| 3176 | write_to_buffer(unsigned char*); |
| 3177 | |
| 3178 | // Print to a map file. |
| 3179 | void |
| 3180 | print_to_mapfile(Mapfile*) const; |
| 3181 | |
| 3182 | // Print statistics about merge sections to stderr. |
| 3183 | void |
| 3184 | print_merge_stats(const char* section_name) |
| 3185 | { |
| 3186 | if (this->shndx_ == MERGE_DATA_SECTION_CODE |
| 3187 | || this->shndx_ == MERGE_STRING_SECTION_CODE) |
| 3188 | this->u2_.posd->print_merge_stats(section_name); |
| 3189 | } |
| 3190 | |
| 3191 | private: |
| 3192 | // Code values which appear in shndx_. If the value is not one of |
| 3193 | // these codes, it is the input section index in the object file. |
| 3194 | enum |
| 3195 | { |
| 3196 | // An Output_section_data. |
| 3197 | OUTPUT_SECTION_CODE = -1U, |
| 3198 | // An Output_section_data for an SHF_MERGE section with |
| 3199 | // SHF_STRINGS not set. |
| 3200 | MERGE_DATA_SECTION_CODE = -2U, |
| 3201 | // An Output_section_data for an SHF_MERGE section with |
| 3202 | // SHF_STRINGS set. |
| 3203 | MERGE_STRING_SECTION_CODE = -3U, |
| 3204 | // An Output_section_data for a relaxed input section. |
| 3205 | RELAXED_INPUT_SECTION_CODE = -4U |
| 3206 | }; |
| 3207 | |
| 3208 | // For an ordinary input section, this is the section index in the |
| 3209 | // input file. For an Output_section_data, this is |
| 3210 | // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
| 3211 | // MERGE_STRING_SECTION_CODE. |
| 3212 | unsigned int shndx_; |
| 3213 | // The required alignment, stored as a power of 2. |
| 3214 | unsigned int p2align_; |
| 3215 | union |
| 3216 | { |
| 3217 | // For an ordinary input section, the section size. |
| 3218 | off_t data_size; |
| 3219 | // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not |
| 3220 | // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the |
| 3221 | // entity size. |
| 3222 | uint64_t entsize; |
| 3223 | } u1_; |
| 3224 | union |
| 3225 | { |
| 3226 | // For an ordinary input section, the object which holds the |
| 3227 | // input section. |
| 3228 | Relobj* object; |
| 3229 | // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
| 3230 | // MERGE_STRING_SECTION_CODE, the data. |
| 3231 | Output_section_data* posd; |
| 3232 | Output_merge_base* pomb; |
| 3233 | // For RELAXED_INPUT_SECTION_CODE, the data. |
| 3234 | Output_relaxed_input_section* poris; |
| 3235 | } u2_; |
| 3236 | // The line number of the pattern it matches in the --section-ordering-file |
| 3237 | // file. It is 0 if does not match any pattern. |
| 3238 | unsigned int section_order_index_; |
| 3239 | }; |
| 3240 | |
| 3241 | // Store the list of input sections for this Output_section into the |
| 3242 | // list passed in. This removes the input sections, leaving only |
| 3243 | // any Output_section_data elements. This returns the size of those |
| 3244 | // Output_section_data elements. ADDRESS is the address of this |
| 3245 | // output section. FILL is the fill value to use, in case there are |
| 3246 | // any spaces between the remaining Output_section_data elements. |
| 3247 | uint64_t |
| 3248 | get_input_sections(uint64_t address, const std::string& fill, |
| 3249 | std::list<Input_section>*); |
| 3250 | |
| 3251 | // Add a script input section. A script input section can either be |
| 3252 | // a plain input section or a sub-class of Output_section_data. |
| 3253 | void |
| 3254 | add_script_input_section(const Input_section& input_section); |
| 3255 | |
| 3256 | // Set the current size of the output section. |
| 3257 | void |
| 3258 | set_current_data_size(off_t size) |
| 3259 | { this->set_current_data_size_for_child(size); } |
| 3260 | |
| 3261 | // Get the current size of the output section. |
| 3262 | off_t |
| 3263 | current_data_size() const |
| 3264 | { return this->current_data_size_for_child(); } |
| 3265 | |
| 3266 | // End of linker script support. |
| 3267 | |
| 3268 | // Save states before doing section layout. |
| 3269 | // This is used for relaxation. |
| 3270 | void |
| 3271 | save_states(); |
| 3272 | |
| 3273 | // Restore states prior to section layout. |
| 3274 | void |
| 3275 | restore_states(); |
| 3276 | |
| 3277 | // Discard states. |
| 3278 | void |
| 3279 | discard_states(); |
| 3280 | |
| 3281 | // Convert existing input sections to relaxed input sections. |
| 3282 | void |
| 3283 | convert_input_sections_to_relaxed_sections( |
| 3284 | const std::vector<Output_relaxed_input_section*>& sections); |
| 3285 | |
| 3286 | // Find a relaxed input section to an input section in OBJECT |
| 3287 | // with index SHNDX. Return NULL if none is found. |
| 3288 | const Output_relaxed_input_section* |
| 3289 | find_relaxed_input_section(const Relobj* object, unsigned int shndx) const; |
| 3290 | |
| 3291 | // Whether section offsets need adjustment due to relaxation. |
| 3292 | bool |
| 3293 | section_offsets_need_adjustment() const |
| 3294 | { return this->section_offsets_need_adjustment_; } |
| 3295 | |
| 3296 | // Set section_offsets_need_adjustment to be true. |
| 3297 | void |
| 3298 | set_section_offsets_need_adjustment() |
| 3299 | { this->section_offsets_need_adjustment_ = true; } |
| 3300 | |
| 3301 | // Adjust section offsets of input sections in this. This is |
| 3302 | // requires if relaxation caused some input sections to change sizes. |
| 3303 | void |
| 3304 | adjust_section_offsets(); |
| 3305 | |
| 3306 | // Whether this is a NOLOAD section. |
| 3307 | bool |
| 3308 | is_noload() const |
| 3309 | { return this->is_noload_; } |
| 3310 | |
| 3311 | // Set NOLOAD flag. |
| 3312 | void |
| 3313 | set_is_noload() |
| 3314 | { this->is_noload_ = true; } |
| 3315 | |
| 3316 | // Print merge statistics to stderr. |
| 3317 | void |
| 3318 | print_merge_stats(); |
| 3319 | |
| 3320 | protected: |
| 3321 | // Return the output section--i.e., the object itself. |
| 3322 | Output_section* |
| 3323 | do_output_section() |
| 3324 | { return this; } |
| 3325 | |
| 3326 | const Output_section* |
| 3327 | do_output_section() const |
| 3328 | { return this; } |
| 3329 | |
| 3330 | // Return the section index in the output file. |
| 3331 | unsigned int |
| 3332 | do_out_shndx() const |
| 3333 | { |
| 3334 | gold_assert(this->out_shndx_ != -1U); |
| 3335 | return this->out_shndx_; |
| 3336 | } |
| 3337 | |
| 3338 | // Set the output section index. |
| 3339 | void |
| 3340 | do_set_out_shndx(unsigned int shndx) |
| 3341 | { |
| 3342 | gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx); |
| 3343 | this->out_shndx_ = shndx; |
| 3344 | } |
| 3345 | |
| 3346 | // Set the final data size of the Output_section. For a typical |
| 3347 | // Output_section, there is nothing to do, but if there are any |
| 3348 | // Output_section_data objects we need to set their final addresses |
| 3349 | // here. |
| 3350 | virtual void |
| 3351 | set_final_data_size(); |
| 3352 | |
| 3353 | // Reset the address and file offset. |
| 3354 | void |
| 3355 | do_reset_address_and_file_offset(); |
| 3356 | |
| 3357 | // Return true if address and file offset already have reset values. In |
| 3358 | // other words, calling reset_address_and_file_offset will not change them. |
| 3359 | bool |
| 3360 | do_address_and_file_offset_have_reset_values() const; |
| 3361 | |
| 3362 | // Write the data to the file. For a typical Output_section, this |
| 3363 | // does nothing: the data is written out by calling Object::Relocate |
| 3364 | // on each input object. But if there are any Output_section_data |
| 3365 | // objects we do need to write them out here. |
| 3366 | virtual void |
| 3367 | do_write(Output_file*); |
| 3368 | |
| 3369 | // Return the address alignment--function required by parent class. |
| 3370 | uint64_t |
| 3371 | do_addralign() const |
| 3372 | { return this->addralign_; } |
| 3373 | |
| 3374 | // Return whether there is a load address. |
| 3375 | bool |
| 3376 | do_has_load_address() const |
| 3377 | { return this->has_load_address_; } |
| 3378 | |
| 3379 | // Return the load address. |
| 3380 | uint64_t |
| 3381 | do_load_address() const |
| 3382 | { |
| 3383 | gold_assert(this->has_load_address_); |
| 3384 | return this->load_address_; |
| 3385 | } |
| 3386 | |
| 3387 | // Return whether this is an Output_section. |
| 3388 | bool |
| 3389 | do_is_section() const |
| 3390 | { return true; } |
| 3391 | |
| 3392 | // Return whether this is a section of the specified type. |
| 3393 | bool |
| 3394 | do_is_section_type(elfcpp::Elf_Word type) const |
| 3395 | { return this->type_ == type; } |
| 3396 | |
| 3397 | // Return whether the specified section flag is set. |
| 3398 | bool |
| 3399 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
| 3400 | { return (this->flags_ & flag) != 0; } |
| 3401 | |
| 3402 | // Set the TLS offset. Called only for SHT_TLS sections. |
| 3403 | void |
| 3404 | do_set_tls_offset(uint64_t tls_base); |
| 3405 | |
| 3406 | // Return the TLS offset, relative to the base of the TLS segment. |
| 3407 | // Valid only for SHT_TLS sections. |
| 3408 | uint64_t |
| 3409 | do_tls_offset() const |
| 3410 | { return this->tls_offset_; } |
| 3411 | |
| 3412 | // This may be implemented by a child class. |
| 3413 | virtual void |
| 3414 | do_finalize_name(Layout*) |
| 3415 | { } |
| 3416 | |
| 3417 | // Print to the map file. |
| 3418 | virtual void |
| 3419 | do_print_to_mapfile(Mapfile*) const; |
| 3420 | |
| 3421 | // Record that this section requires postprocessing after all |
| 3422 | // relocations have been applied. This is called by a child class. |
| 3423 | void |
| 3424 | set_requires_postprocessing() |
| 3425 | { |
| 3426 | this->requires_postprocessing_ = true; |
| 3427 | this->after_input_sections_ = true; |
| 3428 | } |
| 3429 | |
| 3430 | // Write all the data of an Output_section into the postprocessing |
| 3431 | // buffer. |
| 3432 | void |
| 3433 | write_to_postprocessing_buffer(); |
| 3434 | |
| 3435 | typedef std::vector<Input_section> Input_section_list; |
| 3436 | |
| 3437 | // Allow a child class to access the input sections. |
| 3438 | const Input_section_list& |
| 3439 | input_sections() const |
| 3440 | { return this->input_sections_; } |
| 3441 | |
| 3442 | // Whether this always keeps an input section list |
| 3443 | bool |
| 3444 | always_keeps_input_sections() const |
| 3445 | { return this->always_keeps_input_sections_; } |
| 3446 | |
| 3447 | // Always keep an input section list. |
| 3448 | void |
| 3449 | set_always_keeps_input_sections() |
| 3450 | { |
| 3451 | gold_assert(this->current_data_size_for_child() == 0); |
| 3452 | this->always_keeps_input_sections_ = true; |
| 3453 | } |
| 3454 | |
| 3455 | private: |
| 3456 | // We only save enough information to undo the effects of section layout. |
| 3457 | class Checkpoint_output_section |
| 3458 | { |
| 3459 | public: |
| 3460 | Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags, |
| 3461 | const Input_section_list& input_sections, |
| 3462 | off_t first_input_offset, |
| 3463 | bool attached_input_sections_are_sorted) |
| 3464 | : addralign_(addralign), flags_(flags), |
| 3465 | input_sections_(input_sections), |
| 3466 | input_sections_size_(input_sections_.size()), |
| 3467 | input_sections_copy_(), first_input_offset_(first_input_offset), |
| 3468 | attached_input_sections_are_sorted_(attached_input_sections_are_sorted) |
| 3469 | { } |
| 3470 | |
| 3471 | virtual |
| 3472 | ~Checkpoint_output_section() |
| 3473 | { } |
| 3474 | |
| 3475 | // Return the address alignment. |
| 3476 | uint64_t |
| 3477 | addralign() const |
| 3478 | { return this->addralign_; } |
| 3479 | |
| 3480 | // Return the section flags. |
| 3481 | elfcpp::Elf_Xword |
| 3482 | flags() const |
| 3483 | { return this->flags_; } |
| 3484 | |
| 3485 | // Return a reference to the input section list copy. |
| 3486 | Input_section_list* |
| 3487 | input_sections() |
| 3488 | { return &this->input_sections_copy_; } |
| 3489 | |
| 3490 | // Return the size of input_sections at the time when checkpoint is |
| 3491 | // taken. |
| 3492 | size_t |
| 3493 | input_sections_size() const |
| 3494 | { return this->input_sections_size_; } |
| 3495 | |
| 3496 | // Whether input sections are copied. |
| 3497 | bool |
| 3498 | input_sections_saved() const |
| 3499 | { return this->input_sections_copy_.size() == this->input_sections_size_; } |
| 3500 | |
| 3501 | off_t |
| 3502 | first_input_offset() const |
| 3503 | { return this->first_input_offset_; } |
| 3504 | |
| 3505 | bool |
| 3506 | attached_input_sections_are_sorted() const |
| 3507 | { return this->attached_input_sections_are_sorted_; } |
| 3508 | |
| 3509 | // Save input sections. |
| 3510 | void |
| 3511 | save_input_sections() |
| 3512 | { |
| 3513 | this->input_sections_copy_.reserve(this->input_sections_size_); |
| 3514 | this->input_sections_copy_.clear(); |
| 3515 | Input_section_list::const_iterator p = this->input_sections_.begin(); |
| 3516 | gold_assert(this->input_sections_size_ >= this->input_sections_.size()); |
| 3517 | for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p) |
| 3518 | this->input_sections_copy_.push_back(*p); |
| 3519 | } |
| 3520 | |
| 3521 | private: |
| 3522 | // The section alignment. |
| 3523 | uint64_t addralign_; |
| 3524 | // The section flags. |
| 3525 | elfcpp::Elf_Xword flags_; |
| 3526 | // Reference to the input sections to be checkpointed. |
| 3527 | const Input_section_list& input_sections_; |
| 3528 | // Size of the checkpointed portion of input_sections_; |
| 3529 | size_t input_sections_size_; |
| 3530 | // Copy of input sections. |
| 3531 | Input_section_list input_sections_copy_; |
| 3532 | // The offset of the first entry in input_sections_. |
| 3533 | off_t first_input_offset_; |
| 3534 | // True if the input sections attached to this output section have |
| 3535 | // already been sorted. |
| 3536 | bool attached_input_sections_are_sorted_; |
| 3537 | }; |
| 3538 | |
| 3539 | // This class is used to sort the input sections. |
| 3540 | class Input_section_sort_entry; |
| 3541 | |
| 3542 | // This is the sort comparison function for ctors and dtors. |
| 3543 | struct Input_section_sort_compare |
| 3544 | { |
| 3545 | bool |
| 3546 | operator()(const Input_section_sort_entry&, |
| 3547 | const Input_section_sort_entry&) const; |
| 3548 | }; |
| 3549 | |
| 3550 | // This is the sort comparison function for .init_array and .fini_array. |
| 3551 | struct Input_section_sort_init_fini_compare |
| 3552 | { |
| 3553 | bool |
| 3554 | operator()(const Input_section_sort_entry&, |
| 3555 | const Input_section_sort_entry&) const; |
| 3556 | }; |
| 3557 | |
| 3558 | // This is the sort comparison function when a section order is specified |
| 3559 | // from an input file. |
| 3560 | struct Input_section_sort_section_order_index_compare |
| 3561 | { |
| 3562 | bool |
| 3563 | operator()(const Input_section_sort_entry&, |
| 3564 | const Input_section_sort_entry&) const; |
| 3565 | }; |
| 3566 | |
| 3567 | // Fill data. This is used to fill in data between input sections. |
| 3568 | // It is also used for data statements (BYTE, WORD, etc.) in linker |
| 3569 | // scripts. When we have to keep track of the input sections, we |
| 3570 | // can use an Output_data_const, but we don't want to have to keep |
| 3571 | // track of input sections just to implement fills. |
| 3572 | class Fill |
| 3573 | { |
| 3574 | public: |
| 3575 | Fill(off_t section_offset, off_t length) |
| 3576 | : section_offset_(section_offset), |
| 3577 | length_(convert_to_section_size_type(length)) |
| 3578 | { } |
| 3579 | |
| 3580 | // Return section offset. |
| 3581 | off_t |
| 3582 | section_offset() const |
| 3583 | { return this->section_offset_; } |
| 3584 | |
| 3585 | // Return fill length. |
| 3586 | section_size_type |
| 3587 | length() const |
| 3588 | { return this->length_; } |
| 3589 | |
| 3590 | private: |
| 3591 | // The offset within the output section. |
| 3592 | off_t section_offset_; |
| 3593 | // The length of the space to fill. |
| 3594 | section_size_type length_; |
| 3595 | }; |
| 3596 | |
| 3597 | typedef std::vector<Fill> Fill_list; |
| 3598 | |
| 3599 | // Map used during relaxation of existing sections. This map |
| 3600 | // a section id an input section list index. We assume that |
| 3601 | // Input_section_list is a vector. |
| 3602 | typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map; |
| 3603 | |
| 3604 | // Add a new output section by Input_section. |
| 3605 | void |
| 3606 | add_output_section_data(Input_section*); |
| 3607 | |
| 3608 | // Add an SHF_MERGE input section. Returns true if the section was |
| 3609 | // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section |
| 3610 | // stores information about the merged input sections. |
| 3611 | bool |
| 3612 | add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags, |
| 3613 | uint64_t entsize, uint64_t addralign, |
| 3614 | bool keeps_input_sections); |
| 3615 | |
| 3616 | // Add an output SHF_MERGE section POSD to this output section. |
| 3617 | // IS_STRING indicates whether it is a SHF_STRINGS section, and |
| 3618 | // ENTSIZE is the entity size. This returns the entry added to |
| 3619 | // input_sections_. |
| 3620 | void |
| 3621 | add_output_merge_section(Output_section_data* posd, bool is_string, |
| 3622 | uint64_t entsize); |
| 3623 | |
| 3624 | // Sort the attached input sections. |
| 3625 | void |
| 3626 | sort_attached_input_sections(); |
| 3627 | |
| 3628 | // Find the merge section into which an input section with index SHNDX in |
| 3629 | // OBJECT has been added. Return NULL if none found. |
| 3630 | Output_section_data* |
| 3631 | find_merge_section(const Relobj* object, unsigned int shndx) const; |
| 3632 | |
| 3633 | // Build a relaxation map. |
| 3634 | void |
| 3635 | build_relaxation_map( |
| 3636 | const Input_section_list& input_sections, |
| 3637 | size_t limit, |
| 3638 | Relaxation_map* map) const; |
| 3639 | |
| 3640 | // Convert input sections in an input section list into relaxed sections. |
| 3641 | void |
| 3642 | convert_input_sections_in_list_to_relaxed_sections( |
| 3643 | const std::vector<Output_relaxed_input_section*>& relaxed_sections, |
| 3644 | const Relaxation_map& map, |
| 3645 | Input_section_list* input_sections); |
| 3646 | |
| 3647 | // Build the lookup maps for merge and relaxed input sections. |
| 3648 | void |
| 3649 | build_lookup_maps() const; |
| 3650 | |
| 3651 | // Most of these fields are only valid after layout. |
| 3652 | |
| 3653 | // The name of the section. This will point into a Stringpool. |
| 3654 | const char* name_; |
| 3655 | // The section address is in the parent class. |
| 3656 | // The section alignment. |
| 3657 | uint64_t addralign_; |
| 3658 | // The section entry size. |
| 3659 | uint64_t entsize_; |
| 3660 | // The load address. This is only used when using a linker script |
| 3661 | // with a SECTIONS clause. The has_load_address_ field indicates |
| 3662 | // whether this field is valid. |
| 3663 | uint64_t load_address_; |
| 3664 | // The file offset is in the parent class. |
| 3665 | // Set the section link field to the index of this section. |
| 3666 | const Output_data* link_section_; |
| 3667 | // If link_section_ is NULL, this is the link field. |
| 3668 | unsigned int link_; |
| 3669 | // Set the section info field to the index of this section. |
| 3670 | const Output_section* info_section_; |
| 3671 | // If info_section_ is NULL, set the info field to the symbol table |
| 3672 | // index of this symbol. |
| 3673 | const Symbol* info_symndx_; |
| 3674 | // If info_section_ and info_symndx_ are NULL, this is the section |
| 3675 | // info field. |
| 3676 | unsigned int info_; |
| 3677 | // The section type. |
| 3678 | const elfcpp::Elf_Word type_; |
| 3679 | // The section flags. |
| 3680 | elfcpp::Elf_Xword flags_; |
| 3681 | // The order of this section in the output segment. |
| 3682 | Output_section_order order_; |
| 3683 | // The section index. |
| 3684 | unsigned int out_shndx_; |
| 3685 | // If there is a STT_SECTION for this output section in the normal |
| 3686 | // symbol table, this is the symbol index. This starts out as zero. |
| 3687 | // It is initialized in Layout::finalize() to be the index, or -1U |
| 3688 | // if there isn't one. |
| 3689 | unsigned int symtab_index_; |
| 3690 | // If there is a STT_SECTION for this output section in the dynamic |
| 3691 | // symbol table, this is the symbol index. This starts out as zero. |
| 3692 | // It is initialized in Layout::finalize() to be the index, or -1U |
| 3693 | // if there isn't one. |
| 3694 | unsigned int dynsym_index_; |
| 3695 | // The input sections. This will be empty in cases where we don't |
| 3696 | // need to keep track of them. |
| 3697 | Input_section_list input_sections_; |
| 3698 | // The offset of the first entry in input_sections_. |
| 3699 | off_t first_input_offset_; |
| 3700 | // The fill data. This is separate from input_sections_ because we |
| 3701 | // often will need fill sections without needing to keep track of |
| 3702 | // input sections. |
| 3703 | Fill_list fills_; |
| 3704 | // If the section requires postprocessing, this buffer holds the |
| 3705 | // section contents during relocation. |
| 3706 | unsigned char* postprocessing_buffer_; |
| 3707 | // Whether this output section needs a STT_SECTION symbol in the |
| 3708 | // normal symbol table. This will be true if there is a relocation |
| 3709 | // which needs it. |
| 3710 | bool needs_symtab_index_ : 1; |
| 3711 | // Whether this output section needs a STT_SECTION symbol in the |
| 3712 | // dynamic symbol table. This will be true if there is a dynamic |
| 3713 | // relocation which needs it. |
| 3714 | bool needs_dynsym_index_ : 1; |
| 3715 | // Whether the link field of this output section should point to the |
| 3716 | // normal symbol table. |
| 3717 | bool should_link_to_symtab_ : 1; |
| 3718 | // Whether the link field of this output section should point to the |
| 3719 | // dynamic symbol table. |
| 3720 | bool should_link_to_dynsym_ : 1; |
| 3721 | // Whether this section should be written after all the input |
| 3722 | // sections are complete. |
| 3723 | bool after_input_sections_ : 1; |
| 3724 | // Whether this section requires post processing after all |
| 3725 | // relocations have been applied. |
| 3726 | bool requires_postprocessing_ : 1; |
| 3727 | // Whether an input section was mapped to this output section |
| 3728 | // because of a SECTIONS clause in a linker script. |
| 3729 | bool found_in_sections_clause_ : 1; |
| 3730 | // Whether this section has an explicitly specified load address. |
| 3731 | bool has_load_address_ : 1; |
| 3732 | // True if the info_section_ field means the section index of the |
| 3733 | // section, false if it means the symbol index of the corresponding |
| 3734 | // section symbol. |
| 3735 | bool info_uses_section_index_ : 1; |
| 3736 | // True if input sections attached to this output section have to be |
| 3737 | // sorted according to a specified order. |
| 3738 | bool input_section_order_specified_ : 1; |
| 3739 | // True if the input sections attached to this output section may |
| 3740 | // need sorting. |
| 3741 | bool may_sort_attached_input_sections_ : 1; |
| 3742 | // True if the input sections attached to this output section must |
| 3743 | // be sorted. |
| 3744 | bool must_sort_attached_input_sections_ : 1; |
| 3745 | // True if the input sections attached to this output section have |
| 3746 | // already been sorted. |
| 3747 | bool attached_input_sections_are_sorted_ : 1; |
| 3748 | // True if this section holds relro data. |
| 3749 | bool is_relro_ : 1; |
| 3750 | // True if this is a small section. |
| 3751 | bool is_small_section_ : 1; |
| 3752 | // True if this is a large section. |
| 3753 | bool is_large_section_ : 1; |
| 3754 | // Whether code-fills are generated at write. |
| 3755 | bool generate_code_fills_at_write_ : 1; |
| 3756 | // Whether the entry size field should be zero. |
| 3757 | bool is_entsize_zero_ : 1; |
| 3758 | // Whether section offsets need adjustment due to relaxation. |
| 3759 | bool section_offsets_need_adjustment_ : 1; |
| 3760 | // Whether this is a NOLOAD section. |
| 3761 | bool is_noload_ : 1; |
| 3762 | // Whether this always keeps input section. |
| 3763 | bool always_keeps_input_sections_ : 1; |
| 3764 | // For SHT_TLS sections, the offset of this section relative to the base |
| 3765 | // of the TLS segment. |
| 3766 | uint64_t tls_offset_; |
| 3767 | // Saved checkpoint. |
| 3768 | Checkpoint_output_section* checkpoint_; |
| 3769 | // Fast lookup maps for merged and relaxed input sections. |
| 3770 | Output_section_lookup_maps* lookup_maps_; |
| 3771 | }; |
| 3772 | |
| 3773 | // An output segment. PT_LOAD segments are built from collections of |
| 3774 | // output sections. Other segments typically point within PT_LOAD |
| 3775 | // segments, and are built directly as needed. |
| 3776 | // |
| 3777 | // NOTE: We want to use the copy constructor for this class. During |
| 3778 | // relaxation, we may try built the segments multiple times. We do |
| 3779 | // that by copying the original segment list before lay-out, doing |
| 3780 | // a trial lay-out and roll-back to the saved copied if we need to |
| 3781 | // to the lay-out again. |
| 3782 | |
| 3783 | class Output_segment |
| 3784 | { |
| 3785 | public: |
| 3786 | // Create an output segment, specifying the type and flags. |
| 3787 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); |
| 3788 | |
| 3789 | // Return the virtual address. |
| 3790 | uint64_t |
| 3791 | vaddr() const |
| 3792 | { return this->vaddr_; } |
| 3793 | |
| 3794 | // Return the physical address. |
| 3795 | uint64_t |
| 3796 | paddr() const |
| 3797 | { return this->paddr_; } |
| 3798 | |
| 3799 | // Return the segment type. |
| 3800 | elfcpp::Elf_Word |
| 3801 | type() const |
| 3802 | { return this->type_; } |
| 3803 | |
| 3804 | // Return the segment flags. |
| 3805 | elfcpp::Elf_Word |
| 3806 | flags() const |
| 3807 | { return this->flags_; } |
| 3808 | |
| 3809 | // Return the memory size. |
| 3810 | uint64_t |
| 3811 | memsz() const |
| 3812 | { return this->memsz_; } |
| 3813 | |
| 3814 | // Return the file size. |
| 3815 | off_t |
| 3816 | filesz() const |
| 3817 | { return this->filesz_; } |
| 3818 | |
| 3819 | // Return the file offset. |
| 3820 | off_t |
| 3821 | offset() const |
| 3822 | { return this->offset_; } |
| 3823 | |
| 3824 | // Whether this is a segment created to hold large data sections. |
| 3825 | bool |
| 3826 | is_large_data_segment() const |
| 3827 | { return this->is_large_data_segment_; } |
| 3828 | |
| 3829 | // Record that this is a segment created to hold large data |
| 3830 | // sections. |
| 3831 | void |
| 3832 | set_is_large_data_segment() |
| 3833 | { this->is_large_data_segment_ = true; } |
| 3834 | |
| 3835 | // Return the maximum alignment of the Output_data. |
| 3836 | uint64_t |
| 3837 | maximum_alignment(); |
| 3838 | |
| 3839 | // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is |
| 3840 | // the segment flags to use. |
| 3841 | void |
| 3842 | add_output_section_to_load(Layout* layout, Output_section* os, |
| 3843 | elfcpp::Elf_Word seg_flags); |
| 3844 | |
| 3845 | // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS |
| 3846 | // is the segment flags to use. |
| 3847 | void |
| 3848 | add_output_section_to_nonload(Output_section* os, |
| 3849 | elfcpp::Elf_Word seg_flags); |
| 3850 | |
| 3851 | // Remove an Output_section from this segment. It is an error if it |
| 3852 | // is not present. |
| 3853 | void |
| 3854 | remove_output_section(Output_section* os); |
| 3855 | |
| 3856 | // Add an Output_data (which need not be an Output_section) to the |
| 3857 | // start of this segment. |
| 3858 | void |
| 3859 | add_initial_output_data(Output_data*); |
| 3860 | |
| 3861 | // Return true if this segment has any sections which hold actual |
| 3862 | // data, rather than being a BSS section. |
| 3863 | bool |
| 3864 | has_any_data_sections() const; |
| 3865 | |
| 3866 | // Whether this segment has a dynamic relocs. |
| 3867 | bool |
| 3868 | has_dynamic_reloc() const; |
| 3869 | |
| 3870 | // Return the address of the first section. |
| 3871 | uint64_t |
| 3872 | first_section_load_address() const; |
| 3873 | |
| 3874 | // Return whether the addresses have been set already. |
| 3875 | bool |
| 3876 | are_addresses_set() const |
| 3877 | { return this->are_addresses_set_; } |
| 3878 | |
| 3879 | // Set the addresses. |
| 3880 | void |
| 3881 | set_addresses(uint64_t vaddr, uint64_t paddr) |
| 3882 | { |
| 3883 | this->vaddr_ = vaddr; |
| 3884 | this->paddr_ = paddr; |
| 3885 | this->are_addresses_set_ = true; |
| 3886 | } |
| 3887 | |
| 3888 | // Update the flags for the flags of an output section added to this |
| 3889 | // segment. |
| 3890 | void |
| 3891 | update_flags_for_output_section(elfcpp::Elf_Xword flags) |
| 3892 | { |
| 3893 | // The ELF ABI specifies that a PT_TLS segment should always have |
| 3894 | // PF_R as the flags. |
| 3895 | if (this->type() != elfcpp::PT_TLS) |
| 3896 | this->flags_ |= flags; |
| 3897 | } |
| 3898 | |
| 3899 | // Set the segment flags. This is only used if we have a PHDRS |
| 3900 | // clause which explicitly specifies the flags. |
| 3901 | void |
| 3902 | set_flags(elfcpp::Elf_Word flags) |
| 3903 | { this->flags_ = flags; } |
| 3904 | |
| 3905 | // Set the address of the segment to ADDR and the offset to *POFF |
| 3906 | // and set the addresses and offsets of all contained output |
| 3907 | // sections accordingly. Set the section indexes of all contained |
| 3908 | // output sections starting with *PSHNDX. If RESET is true, first |
| 3909 | // reset the addresses of the contained sections. Return the |
| 3910 | // address of the immediately following segment. Update *POFF and |
| 3911 | // *PSHNDX. This should only be called for a PT_LOAD segment. |
| 3912 | uint64_t |
| 3913 | set_section_addresses(const Layout*, bool reset, uint64_t addr, |
| 3914 | unsigned int* increase_relro, bool* has_relro, |
| 3915 | off_t* poff, unsigned int* pshndx); |
| 3916 | |
| 3917 | // Set the minimum alignment of this segment. This may be adjusted |
| 3918 | // upward based on the section alignments. |
| 3919 | void |
| 3920 | set_minimum_p_align(uint64_t align) |
| 3921 | { |
| 3922 | if (align > this->min_p_align_) |
| 3923 | this->min_p_align_ = align; |
| 3924 | } |
| 3925 | |
| 3926 | // Set the offset of this segment based on the section. This should |
| 3927 | // only be called for a non-PT_LOAD segment. |
| 3928 | void |
| 3929 | set_offset(unsigned int increase); |
| 3930 | |
| 3931 | // Set the TLS offsets of the sections contained in the PT_TLS segment. |
| 3932 | void |
| 3933 | set_tls_offsets(); |
| 3934 | |
| 3935 | // Return the number of output sections. |
| 3936 | unsigned int |
| 3937 | output_section_count() const; |
| 3938 | |
| 3939 | // Return the section attached to the list segment with the lowest |
| 3940 | // load address. This is used when handling a PHDRS clause in a |
| 3941 | // linker script. |
| 3942 | Output_section* |
| 3943 | section_with_lowest_load_address() const; |
| 3944 | |
| 3945 | // Write the segment header into *OPHDR. |
| 3946 | template<int size, bool big_endian> |
| 3947 | void |
| 3948 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
| 3949 | |
| 3950 | // Write the section headers of associated sections into V. |
| 3951 | template<int size, bool big_endian> |
| 3952 | unsigned char* |
| 3953 | write_section_headers(const Layout*, const Stringpool*, unsigned char* v, |
| 3954 | unsigned int* pshndx) const; |
| 3955 | |
| 3956 | // Print the output sections in the map file. |
| 3957 | void |
| 3958 | print_sections_to_mapfile(Mapfile*) const; |
| 3959 | |
| 3960 | private: |
| 3961 | typedef std::vector<Output_data*> Output_data_list; |
| 3962 | |
| 3963 | // Find the maximum alignment in an Output_data_list. |
| 3964 | static uint64_t |
| 3965 | maximum_alignment_list(const Output_data_list*); |
| 3966 | |
| 3967 | // Return whether the first data section is a relro section. |
| 3968 | bool |
| 3969 | is_first_section_relro() const; |
| 3970 | |
| 3971 | // Set the section addresses in an Output_data_list. |
| 3972 | uint64_t |
| 3973 | set_section_list_addresses(const Layout*, bool reset, Output_data_list*, |
| 3974 | uint64_t addr, off_t* poff, unsigned int* pshndx, |
| 3975 | bool* in_tls); |
| 3976 | |
| 3977 | // Return the number of Output_sections in an Output_data_list. |
| 3978 | unsigned int |
| 3979 | output_section_count_list(const Output_data_list*) const; |
| 3980 | |
| 3981 | // Return whether an Output_data_list has a dynamic reloc. |
| 3982 | bool |
| 3983 | has_dynamic_reloc_list(const Output_data_list*) const; |
| 3984 | |
| 3985 | // Find the section with the lowest load address in an |
| 3986 | // Output_data_list. |
| 3987 | void |
| 3988 | lowest_load_address_in_list(const Output_data_list* pdl, |
| 3989 | Output_section** found, |
| 3990 | uint64_t* found_lma) const; |
| 3991 | |
| 3992 | // Find the first and last entries by address. |
| 3993 | void |
| 3994 | find_first_and_last_list(const Output_data_list* pdl, |
| 3995 | const Output_data** pfirst, |
| 3996 | const Output_data** plast) const; |
| 3997 | |
| 3998 | // Write the section headers in the list into V. |
| 3999 | template<int size, bool big_endian> |
| 4000 | unsigned char* |
| 4001 | write_section_headers_list(const Layout*, const Stringpool*, |
| 4002 | const Output_data_list*, unsigned char* v, |
| 4003 | unsigned int* pshdx) const; |
| 4004 | |
| 4005 | // Print a section list to the mapfile. |
| 4006 | void |
| 4007 | print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const; |
| 4008 | |
| 4009 | // NOTE: We want to use the copy constructor. Currently, shallow copy |
| 4010 | // works for us so we do not need to write our own copy constructor. |
| 4011 | |
| 4012 | // The list of output data attached to this segment. |
| 4013 | Output_data_list output_lists_[ORDER_MAX]; |
| 4014 | // The segment virtual address. |
| 4015 | uint64_t vaddr_; |
| 4016 | // The segment physical address. |
| 4017 | uint64_t paddr_; |
| 4018 | // The size of the segment in memory. |
| 4019 | uint64_t memsz_; |
| 4020 | // The maximum section alignment. The is_max_align_known_ field |
| 4021 | // indicates whether this has been finalized. |
| 4022 | uint64_t max_align_; |
| 4023 | // The required minimum value for the p_align field. This is used |
| 4024 | // for PT_LOAD segments. Note that this does not mean that |
| 4025 | // addresses should be aligned to this value; it means the p_paddr |
| 4026 | // and p_vaddr fields must be congruent modulo this value. For |
| 4027 | // non-PT_LOAD segments, the dynamic linker works more efficiently |
| 4028 | // if the p_align field has the more conventional value, although it |
| 4029 | // can align as needed. |
| 4030 | uint64_t min_p_align_; |
| 4031 | // The offset of the segment data within the file. |
| 4032 | off_t offset_; |
| 4033 | // The size of the segment data in the file. |
| 4034 | off_t filesz_; |
| 4035 | // The segment type; |
| 4036 | elfcpp::Elf_Word type_; |
| 4037 | // The segment flags. |
| 4038 | elfcpp::Elf_Word flags_; |
| 4039 | // Whether we have finalized max_align_. |
| 4040 | bool is_max_align_known_ : 1; |
| 4041 | // Whether vaddr and paddr were set by a linker script. |
| 4042 | bool are_addresses_set_ : 1; |
| 4043 | // Whether this segment holds large data sections. |
| 4044 | bool is_large_data_segment_ : 1; |
| 4045 | }; |
| 4046 | |
| 4047 | // This class represents the output file. |
| 4048 | |
| 4049 | class Output_file |
| 4050 | { |
| 4051 | public: |
| 4052 | Output_file(const char* name); |
| 4053 | |
| 4054 | // Indicate that this is a temporary file which should not be |
| 4055 | // output. |
| 4056 | void |
| 4057 | set_is_temporary() |
| 4058 | { this->is_temporary_ = true; } |
| 4059 | |
| 4060 | // Try to open an existing file. Returns false if the file doesn't |
| 4061 | // exist, has a size of 0 or can't be mmaped. This method is |
| 4062 | // thread-unsafe. |
| 4063 | bool |
| 4064 | open_for_modification(); |
| 4065 | |
| 4066 | // Open the output file. FILE_SIZE is the final size of the file. |
| 4067 | // If the file already exists, it is deleted/truncated. This method |
| 4068 | // is thread-unsafe. |
| 4069 | void |
| 4070 | open(off_t file_size); |
| 4071 | |
| 4072 | // Resize the output file. This method is thread-unsafe. |
| 4073 | void |
| 4074 | resize(off_t file_size); |
| 4075 | |
| 4076 | // Close the output file (flushing all buffered data) and make sure |
| 4077 | // there are no errors. This method is thread-unsafe. |
| 4078 | void |
| 4079 | close(); |
| 4080 | |
| 4081 | // Return the size of this file. |
| 4082 | off_t |
| 4083 | filesize() |
| 4084 | { return this->file_size_; } |
| 4085 | |
| 4086 | // Return the name of this file. |
| 4087 | const char* |
| 4088 | filename() |
| 4089 | { return this->name_; } |
| 4090 | |
| 4091 | // We currently always use mmap which makes the view handling quite |
| 4092 | // simple. In the future we may support other approaches. |
| 4093 | |
| 4094 | // Write data to the output file. |
| 4095 | void |
| 4096 | write(off_t offset, const void* data, size_t len) |
| 4097 | { memcpy(this->base_ + offset, data, len); } |
| 4098 | |
| 4099 | // Get a buffer to use to write to the file, given the offset into |
| 4100 | // the file and the size. |
| 4101 | unsigned char* |
| 4102 | get_output_view(off_t start, size_t size) |
| 4103 | { |
| 4104 | gold_assert(start >= 0 |
| 4105 | && start + static_cast<off_t>(size) <= this->file_size_); |
| 4106 | return this->base_ + start; |
| 4107 | } |
| 4108 | |
| 4109 | // VIEW must have been returned by get_output_view. Write the |
| 4110 | // buffer to the file, passing in the offset and the size. |
| 4111 | void |
| 4112 | write_output_view(off_t, size_t, unsigned char*) |
| 4113 | { } |
| 4114 | |
| 4115 | // Get a read/write buffer. This is used when we want to write part |
| 4116 | // of the file, read it in, and write it again. |
| 4117 | unsigned char* |
| 4118 | get_input_output_view(off_t start, size_t size) |
| 4119 | { return this->get_output_view(start, size); } |
| 4120 | |
| 4121 | // Write a read/write buffer back to the file. |
| 4122 | void |
| 4123 | write_input_output_view(off_t, size_t, unsigned char*) |
| 4124 | { } |
| 4125 | |
| 4126 | // Get a read buffer. This is used when we just want to read part |
| 4127 | // of the file back it in. |
| 4128 | const unsigned char* |
| 4129 | get_input_view(off_t start, size_t size) |
| 4130 | { return this->get_output_view(start, size); } |
| 4131 | |
| 4132 | // Release a read bfufer. |
| 4133 | void |
| 4134 | free_input_view(off_t, size_t, const unsigned char*) |
| 4135 | { } |
| 4136 | |
| 4137 | private: |
| 4138 | // Map the file into memory or, if that fails, allocate anonymous |
| 4139 | // memory. |
| 4140 | void |
| 4141 | map(); |
| 4142 | |
| 4143 | // Allocate anonymous memory for the file. |
| 4144 | bool |
| 4145 | map_anonymous(); |
| 4146 | |
| 4147 | // Map the file into memory. |
| 4148 | bool |
| 4149 | map_no_anonymous(); |
| 4150 | |
| 4151 | // Unmap the file from memory (and flush to disk buffers). |
| 4152 | void |
| 4153 | unmap(); |
| 4154 | |
| 4155 | // File name. |
| 4156 | const char* name_; |
| 4157 | // File descriptor. |
| 4158 | int o_; |
| 4159 | // File size. |
| 4160 | off_t file_size_; |
| 4161 | // Base of file mapped into memory. |
| 4162 | unsigned char* base_; |
| 4163 | // True iff base_ points to a memory buffer rather than an output file. |
| 4164 | bool map_is_anonymous_; |
| 4165 | // True if this is a temporary file which should not be output. |
| 4166 | bool is_temporary_; |
| 4167 | }; |
| 4168 | |
| 4169 | } // End namespace gold. |
| 4170 | |
| 4171 | #endif // !defined(GOLD_OUTPUT_H) |