| 1 | // object.h -- support for an object file for linking in gold -*- C++ -*- |
| 2 | |
| 3 | // Copyright 2006, 2007 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_OBJECT_H |
| 24 | #define GOLD_OBJECT_H |
| 25 | |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "elfcpp.h" |
| 30 | #include "elfcpp_file.h" |
| 31 | #include "fileread.h" |
| 32 | #include "target.h" |
| 33 | |
| 34 | namespace gold |
| 35 | { |
| 36 | |
| 37 | class General_options; |
| 38 | class Task; |
| 39 | class Layout; |
| 40 | class Output_section; |
| 41 | class Output_file; |
| 42 | class Dynobj; |
| 43 | class Object_merge_map; |
| 44 | |
| 45 | template<typename Stringpool_char> |
| 46 | class Stringpool_template; |
| 47 | |
| 48 | // Data to pass from read_symbols() to add_symbols(). |
| 49 | |
| 50 | struct Read_symbols_data |
| 51 | { |
| 52 | // Section headers. |
| 53 | File_view* section_headers; |
| 54 | // Section names. |
| 55 | File_view* section_names; |
| 56 | // Size of section name data in bytes. |
| 57 | section_size_type section_names_size; |
| 58 | // Symbol data. |
| 59 | File_view* symbols; |
| 60 | // Size of symbol data in bytes. |
| 61 | section_size_type symbols_size; |
| 62 | // Offset of external symbols within symbol data. This structure |
| 63 | // sometimes contains only external symbols, in which case this will |
| 64 | // be zero. Sometimes it contains all symbols. |
| 65 | section_offset_type external_symbols_offset; |
| 66 | // Symbol names. |
| 67 | File_view* symbol_names; |
| 68 | // Size of symbol name data in bytes. |
| 69 | section_size_type symbol_names_size; |
| 70 | |
| 71 | // Version information. This is only used on dynamic objects. |
| 72 | // Version symbol data (from SHT_GNU_versym section). |
| 73 | File_view* versym; |
| 74 | section_size_type versym_size; |
| 75 | // Version definition data (from SHT_GNU_verdef section). |
| 76 | File_view* verdef; |
| 77 | section_size_type verdef_size; |
| 78 | unsigned int verdef_info; |
| 79 | // Needed version data (from SHT_GNU_verneed section). |
| 80 | File_view* verneed; |
| 81 | section_size_type verneed_size; |
| 82 | unsigned int verneed_info; |
| 83 | }; |
| 84 | |
| 85 | // Information used to print error messages. |
| 86 | |
| 87 | struct Symbol_location_info |
| 88 | { |
| 89 | std::string source_file; |
| 90 | std::string enclosing_symbol_name; |
| 91 | int line_number; |
| 92 | }; |
| 93 | |
| 94 | // Data about a single relocation section. This is read in |
| 95 | // read_relocs and processed in scan_relocs. |
| 96 | |
| 97 | struct Section_relocs |
| 98 | { |
| 99 | // Index of reloc section. |
| 100 | unsigned int reloc_shndx; |
| 101 | // Index of section that relocs apply to. |
| 102 | unsigned int data_shndx; |
| 103 | // Contents of reloc section. |
| 104 | File_view* contents; |
| 105 | // Reloc section type. |
| 106 | unsigned int sh_type; |
| 107 | // Number of reloc entries. |
| 108 | size_t reloc_count; |
| 109 | // Output section. |
| 110 | Output_section* output_section; |
| 111 | // Whether this section has special handling for offsets. |
| 112 | bool needs_special_offset_handling; |
| 113 | }; |
| 114 | |
| 115 | // Relocations in an object file. This is read in read_relocs and |
| 116 | // processed in scan_relocs. |
| 117 | |
| 118 | struct Read_relocs_data |
| 119 | { |
| 120 | typedef std::vector<Section_relocs> Relocs_list; |
| 121 | // The relocations. |
| 122 | Relocs_list relocs; |
| 123 | // The local symbols. |
| 124 | File_view* local_symbols; |
| 125 | }; |
| 126 | |
| 127 | // Object is an abstract base class which represents either a 32-bit |
| 128 | // or a 64-bit input object. This can be a regular object file |
| 129 | // (ET_REL) or a shared object (ET_DYN). |
| 130 | |
| 131 | class Object |
| 132 | { |
| 133 | public: |
| 134 | // NAME is the name of the object as we would report it to the user |
| 135 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is |
| 136 | // used to read the file. OFFSET is the offset within the input |
| 137 | // file--0 for a .o or .so file, something else for a .a file. |
| 138 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
| 139 | off_t offset = 0) |
| 140 | : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U), |
| 141 | is_dynamic_(is_dynamic), target_(NULL) |
| 142 | { input_file->file().add_object(); } |
| 143 | |
| 144 | virtual ~Object() |
| 145 | { this->input_file_->file().remove_object(); } |
| 146 | |
| 147 | // Return the name of the object as we would report it to the tuser. |
| 148 | const std::string& |
| 149 | name() const |
| 150 | { return this->name_; } |
| 151 | |
| 152 | // Get the offset into the file. |
| 153 | off_t |
| 154 | offset() const |
| 155 | { return this->offset_; } |
| 156 | |
| 157 | // Return whether this is a dynamic object. |
| 158 | bool |
| 159 | is_dynamic() const |
| 160 | { return this->is_dynamic_; } |
| 161 | |
| 162 | // Return the target structure associated with this object. |
| 163 | Target* |
| 164 | target() const |
| 165 | { return this->target_; } |
| 166 | |
| 167 | // Lock the underlying file. |
| 168 | void |
| 169 | lock(const Task* t) |
| 170 | { this->input_file()->file().lock(t); } |
| 171 | |
| 172 | // Unlock the underlying file. |
| 173 | void |
| 174 | unlock(const Task* t) |
| 175 | { this->input_file()->file().unlock(t); } |
| 176 | |
| 177 | // Return whether the underlying file is locked. |
| 178 | bool |
| 179 | is_locked() const |
| 180 | { return this->input_file()->file().is_locked(); } |
| 181 | |
| 182 | // Return the token, so that the task can be queued. |
| 183 | Task_token* |
| 184 | token() |
| 185 | { return this->input_file()->file().token(); } |
| 186 | |
| 187 | // Release the underlying file. |
| 188 | void |
| 189 | release() |
| 190 | { this->input_file_->file().release(); } |
| 191 | |
| 192 | // Return the sized target structure associated with this object. |
| 193 | // This is like the target method but it returns a pointer of |
| 194 | // appropriate checked type. |
| 195 | template<int size, bool big_endian> |
| 196 | Sized_target<size, big_endian>* |
| 197 | sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const; |
| 198 | |
| 199 | // Get the number of sections. |
| 200 | unsigned int |
| 201 | shnum() const |
| 202 | { return this->shnum_; } |
| 203 | |
| 204 | // Return a view of the contents of a section. Set *PLEN to the |
| 205 | // size. CACHE is a hint as in File_read::get_view. |
| 206 | const unsigned char* |
| 207 | section_contents(unsigned int shndx, section_size_type* plen, bool cache); |
| 208 | |
| 209 | // Return the name of a section given a section index. This is only |
| 210 | // used for error messages. |
| 211 | std::string |
| 212 | section_name(unsigned int shndx) |
| 213 | { return this->do_section_name(shndx); } |
| 214 | |
| 215 | // Return the section flags given a section index. |
| 216 | uint64_t |
| 217 | section_flags(unsigned int shndx) |
| 218 | { return this->do_section_flags(shndx); } |
| 219 | |
| 220 | // Return the section type given a section index. |
| 221 | unsigned int |
| 222 | section_type(unsigned int shndx) |
| 223 | { return this->do_section_type(shndx); } |
| 224 | |
| 225 | // Return the section link field given a section index. |
| 226 | unsigned int |
| 227 | section_link(unsigned int shndx) |
| 228 | { return this->do_section_link(shndx); } |
| 229 | |
| 230 | // Return the section info field given a section index. |
| 231 | unsigned int |
| 232 | section_info(unsigned int shndx) |
| 233 | { return this->do_section_info(shndx); } |
| 234 | |
| 235 | // Read the symbol information. |
| 236 | void |
| 237 | read_symbols(Read_symbols_data* sd) |
| 238 | { return this->do_read_symbols(sd); } |
| 239 | |
| 240 | // Pass sections which should be included in the link to the Layout |
| 241 | // object, and record where the sections go in the output file. |
| 242 | void |
| 243 | layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd) |
| 244 | { this->do_layout(symtab, layout, sd); } |
| 245 | |
| 246 | // Add symbol information to the global symbol table. |
| 247 | void |
| 248 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd) |
| 249 | { this->do_add_symbols(symtab, sd); } |
| 250 | |
| 251 | // Functions and types for the elfcpp::Elf_file interface. This |
| 252 | // permit us to use Object as the File template parameter for |
| 253 | // elfcpp::Elf_file. |
| 254 | |
| 255 | // The View class is returned by view. It must support a single |
| 256 | // method, data(). This is trivial, because get_view does what we |
| 257 | // need. |
| 258 | class View |
| 259 | { |
| 260 | public: |
| 261 | View(const unsigned char* p) |
| 262 | : p_(p) |
| 263 | { } |
| 264 | |
| 265 | const unsigned char* |
| 266 | data() const |
| 267 | { return this->p_; } |
| 268 | |
| 269 | private: |
| 270 | const unsigned char* p_; |
| 271 | }; |
| 272 | |
| 273 | // Return a View. |
| 274 | View |
| 275 | view(off_t file_offset, section_size_type data_size) |
| 276 | { return View(this->get_view(file_offset, data_size, true)); } |
| 277 | |
| 278 | // Report an error. |
| 279 | void |
| 280 | error(const char* format, ...) const ATTRIBUTE_PRINTF_2; |
| 281 | |
| 282 | // A location in the file. |
| 283 | struct Location |
| 284 | { |
| 285 | off_t file_offset; |
| 286 | off_t data_size; |
| 287 | |
| 288 | Location(off_t fo, section_size_type ds) |
| 289 | : file_offset(fo), data_size(ds) |
| 290 | { } |
| 291 | }; |
| 292 | |
| 293 | // Get a View given a Location. |
| 294 | View view(Location loc) |
| 295 | { return View(this->get_view(loc.file_offset, loc.data_size, true)); } |
| 296 | |
| 297 | // Get a view into the underlying file. |
| 298 | const unsigned char* |
| 299 | get_view(off_t start, section_size_type size, bool cache) |
| 300 | { |
| 301 | return this->input_file()->file().get_view(start + this->offset_, size, |
| 302 | cache); |
| 303 | } |
| 304 | |
| 305 | // Get a lasting view into the underlying file. |
| 306 | File_view* |
| 307 | get_lasting_view(off_t start, section_size_type size, bool cache) |
| 308 | { |
| 309 | return this->input_file()->file().get_lasting_view(start + this->offset_, |
| 310 | size, cache); |
| 311 | } |
| 312 | |
| 313 | // Read data from the underlying file. |
| 314 | void |
| 315 | read(off_t start, section_size_type size, void* p) const |
| 316 | { this->input_file()->file().read(start + this->offset_, size, p); } |
| 317 | |
| 318 | // Read multiple data from the underlying file. |
| 319 | void |
| 320 | read_multiple(const File_read::Read_multiple& rm) |
| 321 | { this->input_file()->file().read_multiple(this->offset_, rm); } |
| 322 | |
| 323 | // Stop caching views in the underlying file. |
| 324 | void |
| 325 | clear_view_cache_marks() |
| 326 | { this->input_file()->file().clear_view_cache_marks(); } |
| 327 | |
| 328 | protected: |
| 329 | // Read the symbols--implemented by child class. |
| 330 | virtual void |
| 331 | do_read_symbols(Read_symbols_data*) = 0; |
| 332 | |
| 333 | // Lay out sections--implemented by child class. |
| 334 | virtual void |
| 335 | do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0; |
| 336 | |
| 337 | // Add symbol information to the global symbol table--implemented by |
| 338 | // child class. |
| 339 | virtual void |
| 340 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; |
| 341 | |
| 342 | // Return the location of the contents of a section. Implemented by |
| 343 | // child class. |
| 344 | virtual Location |
| 345 | do_section_contents(unsigned int shndx) = 0; |
| 346 | |
| 347 | // Get the name of a section--implemented by child class. |
| 348 | virtual std::string |
| 349 | do_section_name(unsigned int shndx) = 0; |
| 350 | |
| 351 | // Get section flags--implemented by child class. |
| 352 | virtual uint64_t |
| 353 | do_section_flags(unsigned int shndx) = 0; |
| 354 | |
| 355 | // Get section type--implemented by child class. |
| 356 | virtual unsigned int |
| 357 | do_section_type(unsigned int shndx) = 0; |
| 358 | |
| 359 | // Get section link field--implemented by child class. |
| 360 | virtual unsigned int |
| 361 | do_section_link(unsigned int shndx) = 0; |
| 362 | |
| 363 | // Get section info field--implemented by child class. |
| 364 | virtual unsigned int |
| 365 | do_section_info(unsigned int shndx) = 0; |
| 366 | |
| 367 | // Get the file. We pass on const-ness. |
| 368 | Input_file* |
| 369 | input_file() |
| 370 | { return this->input_file_; } |
| 371 | |
| 372 | const Input_file* |
| 373 | input_file() const |
| 374 | { return this->input_file_; } |
| 375 | |
| 376 | // Set the target. |
| 377 | void |
| 378 | set_target(int machine, int size, bool big_endian, int osabi, |
| 379 | int abiversion); |
| 380 | |
| 381 | // Set the number of sections. |
| 382 | void |
| 383 | set_shnum(int shnum) |
| 384 | { this->shnum_ = shnum; } |
| 385 | |
| 386 | // Functions used by both Sized_relobj and Sized_dynobj. |
| 387 | |
| 388 | // Read the section data into a Read_symbols_data object. |
| 389 | template<int size, bool big_endian> |
| 390 | void |
| 391 | read_section_data(elfcpp::Elf_file<size, big_endian, Object>*, |
| 392 | Read_symbols_data*); |
| 393 | |
| 394 | // If NAME is the name of a special .gnu.warning section, arrange |
| 395 | // for the warning to be issued. SHNDX is the section index. |
| 396 | // Return whether it is a warning section. |
| 397 | bool |
| 398 | handle_gnu_warning_section(const char* name, unsigned int shndx, |
| 399 | Symbol_table*); |
| 400 | |
| 401 | private: |
| 402 | // This class may not be copied. |
| 403 | Object(const Object&); |
| 404 | Object& operator=(const Object&); |
| 405 | |
| 406 | // Name of object as printed to user. |
| 407 | std::string name_; |
| 408 | // For reading the file. |
| 409 | Input_file* input_file_; |
| 410 | // Offset within the file--0 for an object file, non-0 for an |
| 411 | // archive. |
| 412 | off_t offset_; |
| 413 | // Number of input sections. |
| 414 | unsigned int shnum_; |
| 415 | // Whether this is a dynamic object. |
| 416 | bool is_dynamic_; |
| 417 | // Target functions--may be NULL if the target is not known. |
| 418 | Target* target_; |
| 419 | }; |
| 420 | |
| 421 | // Implement sized_target inline for efficiency. This approach breaks |
| 422 | // static type checking, but is made safe using asserts. |
| 423 | |
| 424 | template<int size, bool big_endian> |
| 425 | inline Sized_target<size, big_endian>* |
| 426 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const |
| 427 | { |
| 428 | gold_assert(this->target_->get_size() == size); |
| 429 | gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian); |
| 430 | return static_cast<Sized_target<size, big_endian>*>(this->target_); |
| 431 | } |
| 432 | |
| 433 | // A regular object (ET_REL). This is an abstract base class itself. |
| 434 | // The implementation is the template class Sized_relobj. |
| 435 | |
| 436 | class Relobj : public Object |
| 437 | { |
| 438 | public: |
| 439 | Relobj(const std::string& name, Input_file* input_file, off_t offset = 0) |
| 440 | : Object(name, input_file, false, offset), |
| 441 | map_to_output_(), |
| 442 | object_merge_map_(NULL), |
| 443 | relocs_must_follow_section_writes_(false) |
| 444 | { } |
| 445 | |
| 446 | // Read the relocs. |
| 447 | void |
| 448 | read_relocs(Read_relocs_data* rd) |
| 449 | { return this->do_read_relocs(rd); } |
| 450 | |
| 451 | // Scan the relocs and adjust the symbol table. |
| 452 | void |
| 453 | scan_relocs(const General_options& options, Symbol_table* symtab, |
| 454 | Layout* layout, Read_relocs_data* rd) |
| 455 | { return this->do_scan_relocs(options, symtab, layout, rd); } |
| 456 | |
| 457 | // The number of local symbols in the input symbol table. |
| 458 | virtual unsigned int |
| 459 | local_symbol_count() const |
| 460 | { return this->do_local_symbol_count(); } |
| 461 | |
| 462 | // Initial local symbol processing: count the number of local symbols |
| 463 | // in the output symbol table and dynamic symbol table; add local symbol |
| 464 | // names to *POOL and *DYNPOOL. |
| 465 | void |
| 466 | count_local_symbols(Stringpool_template<char>* pool, |
| 467 | Stringpool_template<char>* dynpool) |
| 468 | { return this->do_count_local_symbols(pool, dynpool); } |
| 469 | |
| 470 | // Set the values of the local symbols, set the output symbol table |
| 471 | // indexes for the local variables, and set the offset where local |
| 472 | // symbol information will be stored. Returns the new local symbol index. |
| 473 | unsigned int |
| 474 | finalize_local_symbols(unsigned int index, off_t off) |
| 475 | { return this->do_finalize_local_symbols(index, off); } |
| 476 | |
| 477 | // Set the output dynamic symbol table indexes for the local variables. |
| 478 | unsigned int |
| 479 | set_local_dynsym_indexes(unsigned int index) |
| 480 | { return this->do_set_local_dynsym_indexes(index); } |
| 481 | |
| 482 | // Set the offset where local dynamic symbol information will be stored. |
| 483 | unsigned int |
| 484 | set_local_dynsym_offset(off_t off) |
| 485 | { return this->do_set_local_dynsym_offset(off); } |
| 486 | |
| 487 | // Relocate the input sections and write out the local symbols. |
| 488 | void |
| 489 | relocate(const General_options& options, const Symbol_table* symtab, |
| 490 | const Layout* layout, Output_file* of) |
| 491 | { return this->do_relocate(options, symtab, layout, of); } |
| 492 | |
| 493 | // Return whether an input section is being included in the link. |
| 494 | bool |
| 495 | is_section_included(unsigned int shndx) const |
| 496 | { |
| 497 | gold_assert(shndx < this->map_to_output_.size()); |
| 498 | return this->map_to_output_[shndx].output_section != NULL; |
| 499 | } |
| 500 | |
| 501 | // Return whether an input section requires special |
| 502 | // handling--whether it is not simply mapped from the input file to |
| 503 | // the output file. |
| 504 | bool |
| 505 | is_section_specially_mapped(unsigned int shndx) const |
| 506 | { |
| 507 | gold_assert(shndx < this->map_to_output_.size()); |
| 508 | return (this->map_to_output_[shndx].output_section != NULL |
| 509 | && this->map_to_output_[shndx].offset == -1); |
| 510 | } |
| 511 | |
| 512 | // Given a section index, return the corresponding Output_section |
| 513 | // (which will be NULL if the section is not included in the link) |
| 514 | // and set *POFF to the offset within that section. *POFF will be |
| 515 | // set to -1 if the section requires special handling. |
| 516 | inline Output_section* |
| 517 | output_section(unsigned int shndx, section_offset_type* poff) const; |
| 518 | |
| 519 | // Set the offset of an input section within its output section. |
| 520 | void |
| 521 | set_section_offset(unsigned int shndx, section_offset_type off) |
| 522 | { |
| 523 | gold_assert(shndx < this->map_to_output_.size()); |
| 524 | this->map_to_output_[shndx].offset = off; |
| 525 | } |
| 526 | |
| 527 | // Return true if we need to wait for output sections to be written |
| 528 | // before we can apply relocations. This is true if the object has |
| 529 | // any relocations for sections which require special handling, such |
| 530 | // as the exception frame section. |
| 531 | bool |
| 532 | relocs_must_follow_section_writes() const |
| 533 | { return this->relocs_must_follow_section_writes_; } |
| 534 | |
| 535 | // Return the object merge map. |
| 536 | Object_merge_map* |
| 537 | merge_map() const |
| 538 | { return this->object_merge_map_; } |
| 539 | |
| 540 | // Set the object merge map. |
| 541 | void |
| 542 | set_merge_map(Object_merge_map* object_merge_map) |
| 543 | { |
| 544 | gold_assert(this->object_merge_map_ == NULL); |
| 545 | this->object_merge_map_ = object_merge_map; |
| 546 | } |
| 547 | |
| 548 | protected: |
| 549 | // What we need to know to map an input section to an output |
| 550 | // section. We keep an array of these, one for each input section, |
| 551 | // indexed by the input section number. |
| 552 | struct Map_to_output |
| 553 | { |
| 554 | // The output section. This is NULL if the input section is to be |
| 555 | // discarded. |
| 556 | Output_section* output_section; |
| 557 | // The offset within the output section. This is -1 if the |
| 558 | // section requires special handling. |
| 559 | section_offset_type offset; |
| 560 | }; |
| 561 | |
| 562 | // Read the relocs--implemented by child class. |
| 563 | virtual void |
| 564 | do_read_relocs(Read_relocs_data*) = 0; |
| 565 | |
| 566 | // Scan the relocs--implemented by child class. |
| 567 | virtual void |
| 568 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
| 569 | Read_relocs_data*) = 0; |
| 570 | |
| 571 | // Return the number of local symbols--implemented by child class. |
| 572 | virtual unsigned int |
| 573 | do_local_symbol_count() const = 0; |
| 574 | |
| 575 | // Count local symbols--implemented by child class. |
| 576 | virtual void |
| 577 | do_count_local_symbols(Stringpool_template<char>*, |
| 578 | Stringpool_template<char>*) = 0; |
| 579 | |
| 580 | // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the |
| 581 | // offset where local symbol information will be stored. |
| 582 | virtual unsigned int |
| 583 | do_finalize_local_symbols(unsigned int, off_t) = 0; |
| 584 | |
| 585 | // Set the output dynamic symbol table indexes for the local variables. |
| 586 | virtual unsigned int |
| 587 | do_set_local_dynsym_indexes(unsigned int) = 0; |
| 588 | |
| 589 | // Set the offset where local dynamic symbol information will be stored. |
| 590 | virtual unsigned int |
| 591 | do_set_local_dynsym_offset(off_t) = 0; |
| 592 | |
| 593 | // Relocate the input sections and write out the local |
| 594 | // symbols--implemented by child class. |
| 595 | virtual void |
| 596 | do_relocate(const General_options& options, const Symbol_table* symtab, |
| 597 | const Layout*, Output_file* of) = 0; |
| 598 | |
| 599 | // Return the vector mapping input sections to output sections. |
| 600 | std::vector<Map_to_output>& |
| 601 | map_to_output() |
| 602 | { return this->map_to_output_; } |
| 603 | |
| 604 | const std::vector<Map_to_output>& |
| 605 | map_to_output() const |
| 606 | { return this->map_to_output_; } |
| 607 | |
| 608 | // Record that we must wait for the output sections to be written |
| 609 | // before applying relocations. |
| 610 | void |
| 611 | set_relocs_must_follow_section_writes() |
| 612 | { this->relocs_must_follow_section_writes_ = true; } |
| 613 | |
| 614 | private: |
| 615 | // Mapping from input sections to output section. |
| 616 | std::vector<Map_to_output> map_to_output_; |
| 617 | // Mappings for merge sections. This is managed by the code in the |
| 618 | // Merge_map class. |
| 619 | Object_merge_map* object_merge_map_; |
| 620 | // Whether we need to wait for output sections to be written before |
| 621 | // we can apply relocations. |
| 622 | bool relocs_must_follow_section_writes_; |
| 623 | }; |
| 624 | |
| 625 | // Implement Object::output_section inline for efficiency. |
| 626 | inline Output_section* |
| 627 | Relobj::output_section(unsigned int shndx, section_offset_type* poff) const |
| 628 | { |
| 629 | gold_assert(shndx < this->map_to_output_.size()); |
| 630 | const Map_to_output& mo(this->map_to_output_[shndx]); |
| 631 | *poff = mo.offset; |
| 632 | return mo.output_section; |
| 633 | } |
| 634 | |
| 635 | // This class is used to handle relocations against a section symbol |
| 636 | // in an SHF_MERGE section. For such a symbol, we need to know the |
| 637 | // addend of the relocation before we can determine the final value. |
| 638 | // The addend gives us the location in the input section, and we can |
| 639 | // determine how it is mapped to the output section. For a |
| 640 | // non-section symbol, we apply the addend to the final value of the |
| 641 | // symbol; that is done in finalize_local_symbols, and does not use |
| 642 | // this class. |
| 643 | |
| 644 | template<int size> |
| 645 | class Merged_symbol_value |
| 646 | { |
| 647 | public: |
| 648 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; |
| 649 | |
| 650 | // We use a hash table to map offsets in the input section to output |
| 651 | // addresses. |
| 652 | typedef Unordered_map<section_offset_type, Value> Output_addresses; |
| 653 | |
| 654 | Merged_symbol_value(Value input_value, Value output_start_address) |
| 655 | : input_value_(input_value), output_start_address_(output_start_address), |
| 656 | output_addresses_() |
| 657 | { } |
| 658 | |
| 659 | // Initialize the hash table. |
| 660 | void |
| 661 | initialize_input_to_output_map(const Relobj*, unsigned int input_shndx); |
| 662 | |
| 663 | // Release the hash table to save space. |
| 664 | void |
| 665 | free_input_to_output_map() |
| 666 | { this->output_addresses_.clear(); } |
| 667 | |
| 668 | // Get the output value corresponding to an addend. The object and |
| 669 | // input section index are passed in because the caller will have |
| 670 | // them; otherwise we could store them here. |
| 671 | Value |
| 672 | value(const Relobj* object, unsigned int input_shndx, Value addend) const |
| 673 | { |
| 674 | Value input_offset = this->input_value_ + addend; |
| 675 | typename Output_addresses::const_iterator p = |
| 676 | this->output_addresses_.find(input_offset); |
| 677 | if (p != this->output_addresses_.end()) |
| 678 | return p->second; |
| 679 | |
| 680 | return this->value_from_output_section(object, input_shndx, input_offset); |
| 681 | } |
| 682 | |
| 683 | private: |
| 684 | // Get the output value for an input offset if we couldn't find it |
| 685 | // in the hash table. |
| 686 | Value |
| 687 | value_from_output_section(const Relobj*, unsigned int input_shndx, |
| 688 | Value input_offset) const; |
| 689 | |
| 690 | // The value of the section symbol in the input file. This is |
| 691 | // normally zero, but could in principle be something else. |
| 692 | Value input_value_; |
| 693 | // The start address of this merged section in the output file. |
| 694 | Value output_start_address_; |
| 695 | // A hash table which maps offsets in the input section to output |
| 696 | // addresses. This only maps specific offsets, not all offsets. |
| 697 | Output_addresses output_addresses_; |
| 698 | }; |
| 699 | |
| 700 | // This POD class is holds the value of a symbol. This is used for |
| 701 | // local symbols, and for all symbols during relocation processing. |
| 702 | // For special sections, such as SHF_MERGE sections, this calls a |
| 703 | // function to get the final symbol value. |
| 704 | |
| 705 | template<int size> |
| 706 | class Symbol_value |
| 707 | { |
| 708 | public: |
| 709 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; |
| 710 | |
| 711 | Symbol_value() |
| 712 | : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0), |
| 713 | is_section_symbol_(false), is_tls_symbol_(false), |
| 714 | has_output_value_(true) |
| 715 | { this->u_.value = 0; } |
| 716 | |
| 717 | // Get the value of this symbol. OBJECT is the object in which this |
| 718 | // symbol is defined, and ADDEND is an addend to add to the value. |
| 719 | template<bool big_endian> |
| 720 | Value |
| 721 | value(const Sized_relobj<size, big_endian>* object, Value addend) const |
| 722 | { |
| 723 | if (this->has_output_value_) |
| 724 | return this->u_.value + addend; |
| 725 | else |
| 726 | return this->u_.merged_symbol_value->value(object, this->input_shndx_, |
| 727 | addend); |
| 728 | } |
| 729 | |
| 730 | // Set the value of this symbol in the output symbol table. |
| 731 | void |
| 732 | set_output_value(Value value) |
| 733 | { this->u_.value = value; } |
| 734 | |
| 735 | // For a section symbol in a merged section, we need more |
| 736 | // information. |
| 737 | void |
| 738 | set_merged_symbol_value(Merged_symbol_value<size>* msv) |
| 739 | { |
| 740 | gold_assert(this->is_section_symbol_); |
| 741 | this->has_output_value_ = false; |
| 742 | this->u_.merged_symbol_value = msv; |
| 743 | } |
| 744 | |
| 745 | // Initialize the input to output map for a section symbol in a |
| 746 | // merged section. We also initialize the value of a non-section |
| 747 | // symbol in a merged section. |
| 748 | void |
| 749 | initialize_input_to_output_map(const Relobj* object) |
| 750 | { |
| 751 | if (!this->has_output_value_) |
| 752 | { |
| 753 | gold_assert(this->is_section_symbol_); |
| 754 | Merged_symbol_value<size>* msv = this->u_.merged_symbol_value; |
| 755 | msv->initialize_input_to_output_map(object, this->input_shndx_); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Free the input to output map for a section symbol in a merged |
| 760 | // section. |
| 761 | void |
| 762 | free_input_to_output_map() |
| 763 | { |
| 764 | if (!this->has_output_value_) |
| 765 | this->u_.merged_symbol_value->free_input_to_output_map(); |
| 766 | } |
| 767 | |
| 768 | // Set the value of the symbol from the input file. This is only |
| 769 | // called by count_local_symbols, to communicate the value to |
| 770 | // finalize_local_symbols. |
| 771 | void |
| 772 | set_input_value(Value value) |
| 773 | { this->u_.value = value; } |
| 774 | |
| 775 | // Return the input value. This is only called by |
| 776 | // finalize_local_symbols. |
| 777 | Value |
| 778 | input_value() const |
| 779 | { return this->u_.value; } |
| 780 | |
| 781 | // Return whether this symbol should go into the output symbol |
| 782 | // table. |
| 783 | bool |
| 784 | needs_output_symtab_entry() const |
| 785 | { return this->output_symtab_index_ != -1U; } |
| 786 | |
| 787 | // Return the index in the output symbol table. |
| 788 | unsigned int |
| 789 | output_symtab_index() const |
| 790 | { |
| 791 | gold_assert(this->output_symtab_index_ != 0); |
| 792 | return this->output_symtab_index_; |
| 793 | } |
| 794 | |
| 795 | // Set the index in the output symbol table. |
| 796 | void |
| 797 | set_output_symtab_index(unsigned int i) |
| 798 | { |
| 799 | gold_assert(this->output_symtab_index_ == 0); |
| 800 | this->output_symtab_index_ = i; |
| 801 | } |
| 802 | |
| 803 | // Record that this symbol should not go into the output symbol |
| 804 | // table. |
| 805 | void |
| 806 | set_no_output_symtab_entry() |
| 807 | { |
| 808 | gold_assert(this->output_symtab_index_ == 0); |
| 809 | this->output_symtab_index_ = -1U; |
| 810 | } |
| 811 | |
| 812 | // Set the index in the output dynamic symbol table. |
| 813 | void |
| 814 | set_needs_output_dynsym_entry() |
| 815 | { |
| 816 | this->output_dynsym_index_ = 0; |
| 817 | } |
| 818 | |
| 819 | // Return whether this symbol should go into the output symbol |
| 820 | // table. |
| 821 | bool |
| 822 | needs_output_dynsym_entry() const |
| 823 | { |
| 824 | return this->output_dynsym_index_ != -1U; |
| 825 | } |
| 826 | |
| 827 | // Record that this symbol should go into the dynamic symbol table. |
| 828 | void |
| 829 | set_output_dynsym_index(unsigned int i) |
| 830 | { |
| 831 | gold_assert(this->output_dynsym_index_ == 0); |
| 832 | this->output_dynsym_index_ = i; |
| 833 | } |
| 834 | |
| 835 | // Return the index in the output dynamic symbol table. |
| 836 | unsigned int |
| 837 | output_dynsym_index() const |
| 838 | { |
| 839 | gold_assert(this->output_dynsym_index_ != 0); |
| 840 | return this->output_dynsym_index_; |
| 841 | } |
| 842 | |
| 843 | // Set the index of the input section in the input file. |
| 844 | void |
| 845 | set_input_shndx(unsigned int i) |
| 846 | { |
| 847 | this->input_shndx_ = i; |
| 848 | // input_shndx_ field is a bitfield, so make sure that the value |
| 849 | // fits. |
| 850 | gold_assert(this->input_shndx_ == i); |
| 851 | } |
| 852 | |
| 853 | // Return the index of the input section in the input file. |
| 854 | unsigned int |
| 855 | input_shndx() const |
| 856 | { return this->input_shndx_; } |
| 857 | |
| 858 | // Whether this is a section symbol. |
| 859 | bool |
| 860 | is_section_symbol() const |
| 861 | { return this->is_section_symbol_; } |
| 862 | |
| 863 | // Record that this is a section symbol. |
| 864 | void |
| 865 | set_is_section_symbol() |
| 866 | { this->is_section_symbol_ = true; } |
| 867 | |
| 868 | // Record that this is a TLS symbol. |
| 869 | void |
| 870 | set_is_tls_symbol() |
| 871 | { this->is_tls_symbol_ = true; } |
| 872 | |
| 873 | // Return TRUE if this is a TLS symbol. |
| 874 | bool |
| 875 | is_tls_symbol() const |
| 876 | { return this->is_tls_symbol_; } |
| 877 | |
| 878 | private: |
| 879 | // The index of this local symbol in the output symbol table. This |
| 880 | // will be -1 if the symbol should not go into the symbol table. |
| 881 | unsigned int output_symtab_index_; |
| 882 | // The index of this local symbol in the dynamic symbol table. This |
| 883 | // will be -1 if the symbol should not go into the symbol table. |
| 884 | unsigned int output_dynsym_index_; |
| 885 | // The section index in the input file in which this symbol is |
| 886 | // defined. |
| 887 | unsigned int input_shndx_ : 29; |
| 888 | // Whether this is a STT_SECTION symbol. |
| 889 | bool is_section_symbol_ : 1; |
| 890 | // Whether this is a STT_TLS symbol. |
| 891 | bool is_tls_symbol_ : 1; |
| 892 | // Whether this symbol has a value for the output file. This is |
| 893 | // normally set to true during Layout::finalize, by |
| 894 | // finalize_local_symbols. It will be false for a section symbol in |
| 895 | // a merge section, as for such symbols we can not determine the |
| 896 | // value to use in a relocation until we see the addend. |
| 897 | bool has_output_value_ : 1; |
| 898 | union |
| 899 | { |
| 900 | // This is used if has_output_value_ is true. Between |
| 901 | // count_local_symbols and finalize_local_symbols, this is the |
| 902 | // value in the input file. After finalize_local_symbols, it is |
| 903 | // the value in the output file. |
| 904 | Value value; |
| 905 | // This is used if has_output_value_ is false. It points to the |
| 906 | // information we need to get the value for a merge section. |
| 907 | Merged_symbol_value<size>* merged_symbol_value; |
| 908 | } u_; |
| 909 | }; |
| 910 | |
| 911 | // A regular object file. This is size and endian specific. |
| 912 | |
| 913 | template<int size, bool big_endian> |
| 914 | class Sized_relobj : public Relobj |
| 915 | { |
| 916 | public: |
| 917 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
| 918 | typedef std::vector<Symbol*> Symbols; |
| 919 | typedef std::vector<Symbol_value<size> > Local_values; |
| 920 | |
| 921 | Sized_relobj(const std::string& name, Input_file* input_file, off_t offset, |
| 922 | const typename elfcpp::Ehdr<size, big_endian>&); |
| 923 | |
| 924 | ~Sized_relobj(); |
| 925 | |
| 926 | // Set up the object file based on the ELF header. |
| 927 | void |
| 928 | setup(const typename elfcpp::Ehdr<size, big_endian>&); |
| 929 | |
| 930 | // If SYM is the index of a global symbol in the object file's |
| 931 | // symbol table, return the Symbol object. Otherwise, return NULL. |
| 932 | Symbol* |
| 933 | global_symbol(unsigned int sym) const |
| 934 | { |
| 935 | if (sym >= this->local_symbol_count_) |
| 936 | { |
| 937 | gold_assert(sym - this->local_symbol_count_ < this->symbols_.size()); |
| 938 | return this->symbols_[sym - this->local_symbol_count_]; |
| 939 | } |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | // Return the section index of symbol SYM. Set *VALUE to its value |
| 944 | // in the object file. Note that for a symbol which is not defined |
| 945 | // in this object file, this will set *VALUE to 0 and return |
| 946 | // SHN_UNDEF; it will not return the final value of the symbol in |
| 947 | // the link. |
| 948 | unsigned int |
| 949 | symbol_section_and_value(unsigned int sym, Address* value); |
| 950 | |
| 951 | // Return a pointer to the Symbol_value structure which holds the |
| 952 | // value of a local symbol. |
| 953 | const Symbol_value<size>* |
| 954 | local_symbol(unsigned int sym) const |
| 955 | { |
| 956 | gold_assert(sym < this->local_values_.size()); |
| 957 | return &this->local_values_[sym]; |
| 958 | } |
| 959 | |
| 960 | // Return the index of local symbol SYM in the ordinary symbol |
| 961 | // table. A value of -1U means that the symbol is not being output. |
| 962 | unsigned int |
| 963 | symtab_index(unsigned int sym) const |
| 964 | { |
| 965 | gold_assert(sym < this->local_values_.size()); |
| 966 | return this->local_values_[sym].output_symtab_index(); |
| 967 | } |
| 968 | |
| 969 | // Return the index of local symbol SYM in the dynamic symbol |
| 970 | // table. A value of -1U means that the symbol is not being output. |
| 971 | unsigned int |
| 972 | dynsym_index(unsigned int sym) const |
| 973 | { |
| 974 | gold_assert(sym < this->local_values_.size()); |
| 975 | return this->local_values_[sym].output_dynsym_index(); |
| 976 | } |
| 977 | |
| 978 | // Return the appropriate Sized_target structure. |
| 979 | Sized_target<size, big_endian>* |
| 980 | sized_target() |
| 981 | { |
| 982 | return this->Object::sized_target |
| 983 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 984 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); |
| 985 | } |
| 986 | |
| 987 | // Return the value of the local symbol symndx. |
| 988 | Address |
| 989 | local_symbol_value(unsigned int symndx) const; |
| 990 | |
| 991 | void |
| 992 | set_needs_output_dynsym_entry(unsigned int sym) |
| 993 | { |
| 994 | gold_assert(sym < this->local_values_.size()); |
| 995 | this->local_values_[sym].set_needs_output_dynsym_entry(); |
| 996 | } |
| 997 | |
| 998 | // Return whether the local symbol SYMNDX has a GOT offset. |
| 999 | // For TLS symbols, the GOT entry will hold its tp-relative offset. |
| 1000 | bool |
| 1001 | local_has_got_offset(unsigned int symndx) const |
| 1002 | { |
| 1003 | return (this->local_got_offsets_.find(symndx) |
| 1004 | != this->local_got_offsets_.end()); |
| 1005 | } |
| 1006 | |
| 1007 | // Return the GOT offset of the local symbol SYMNDX. |
| 1008 | unsigned int |
| 1009 | local_got_offset(unsigned int symndx) const |
| 1010 | { |
| 1011 | Local_got_offsets::const_iterator p = |
| 1012 | this->local_got_offsets_.find(symndx); |
| 1013 | gold_assert(p != this->local_got_offsets_.end()); |
| 1014 | return p->second; |
| 1015 | } |
| 1016 | |
| 1017 | // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET. |
| 1018 | void |
| 1019 | set_local_got_offset(unsigned int symndx, unsigned int got_offset) |
| 1020 | { |
| 1021 | std::pair<Local_got_offsets::iterator, bool> ins = |
| 1022 | this->local_got_offsets_.insert(std::make_pair(symndx, got_offset)); |
| 1023 | gold_assert(ins.second); |
| 1024 | } |
| 1025 | |
| 1026 | // Return whether the local TLS symbol SYMNDX has a GOT offset. |
| 1027 | // The GOT entry at this offset will contain a module index. If |
| 1028 | // NEED_PAIR is true, a second entry immediately following the first |
| 1029 | // will contain the dtv-relative offset. |
| 1030 | bool |
| 1031 | local_has_tls_got_offset(unsigned int symndx, bool need_pair) const |
| 1032 | { |
| 1033 | typename Local_tls_got_offsets::const_iterator p = |
| 1034 | this->local_tls_got_offsets_.find(symndx); |
| 1035 | if (p == this->local_tls_got_offsets_.end() |
| 1036 | || (need_pair && !p->second.have_pair_)) |
| 1037 | return false; |
| 1038 | return true; |
| 1039 | } |
| 1040 | |
| 1041 | // Return the offset of the GOT entry for the local TLS symbol SYMNDX. |
| 1042 | // If NEED_PAIR is true, we need the offset of a pair of GOT entries; |
| 1043 | // otherwise we need the offset of the GOT entry for the module index. |
| 1044 | unsigned int |
| 1045 | local_tls_got_offset(unsigned int symndx, bool need_pair) const |
| 1046 | { |
| 1047 | typename Local_tls_got_offsets::const_iterator p = |
| 1048 | this->local_tls_got_offsets_.find(symndx); |
| 1049 | gold_assert(p != this->local_tls_got_offsets_.end()); |
| 1050 | gold_assert(!need_pair || p->second.have_pair_); |
| 1051 | return p->second.got_offset_; |
| 1052 | } |
| 1053 | |
| 1054 | // Set the offset of the GOT entry for the local TLS symbol SYMNDX |
| 1055 | // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries; |
| 1056 | // otherwise, we have just a single entry for the module index. |
| 1057 | void |
| 1058 | set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset, |
| 1059 | bool have_pair) |
| 1060 | { |
| 1061 | typename Local_tls_got_offsets::iterator p = |
| 1062 | this->local_tls_got_offsets_.find(symndx); |
| 1063 | if (p != this->local_tls_got_offsets_.end()) |
| 1064 | { |
| 1065 | // An entry already existed for this symbol. This can happen |
| 1066 | // if we see a relocation asking for the module index before |
| 1067 | // a relocation asking for the pair. In that case, the original |
| 1068 | // GOT entry will remain, but won't get used by any further |
| 1069 | // relocations. |
| 1070 | p->second.got_offset_ = got_offset; |
| 1071 | gold_assert(have_pair); |
| 1072 | p->second.have_pair_ = true; |
| 1073 | } |
| 1074 | else |
| 1075 | { |
| 1076 | std::pair<typename Local_tls_got_offsets::iterator, bool> ins = |
| 1077 | this->local_tls_got_offsets_.insert( |
| 1078 | std::make_pair(symndx, Tls_got_entry(got_offset, have_pair))); |
| 1079 | gold_assert(ins.second); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | // Return the name of the symbol that spans the given offset in the |
| 1084 | // specified section in this object. This is used only for error |
| 1085 | // messages and is not particularly efficient. |
| 1086 | bool |
| 1087 | get_symbol_location_info(unsigned int shndx, off_t offset, |
| 1088 | Symbol_location_info* info); |
| 1089 | |
| 1090 | protected: |
| 1091 | // Read the symbols. |
| 1092 | void |
| 1093 | do_read_symbols(Read_symbols_data*); |
| 1094 | |
| 1095 | // Return the number of local symbols. |
| 1096 | unsigned int |
| 1097 | do_local_symbol_count() const |
| 1098 | { return this->local_symbol_count_; } |
| 1099 | |
| 1100 | // Lay out the input sections. |
| 1101 | void |
| 1102 | do_layout(Symbol_table*, Layout*, Read_symbols_data*); |
| 1103 | |
| 1104 | // Add the symbols to the symbol table. |
| 1105 | void |
| 1106 | do_add_symbols(Symbol_table*, Read_symbols_data*); |
| 1107 | |
| 1108 | // Read the relocs. |
| 1109 | void |
| 1110 | do_read_relocs(Read_relocs_data*); |
| 1111 | |
| 1112 | // Scan the relocs and adjust the symbol table. |
| 1113 | void |
| 1114 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
| 1115 | Read_relocs_data*); |
| 1116 | |
| 1117 | // Count the local symbols. |
| 1118 | void |
| 1119 | do_count_local_symbols(Stringpool_template<char>*, |
| 1120 | Stringpool_template<char>*); |
| 1121 | |
| 1122 | // Finalize the local symbols. |
| 1123 | unsigned int |
| 1124 | do_finalize_local_symbols(unsigned int, off_t); |
| 1125 | |
| 1126 | // Set the offset where local dynamic symbol information will be stored. |
| 1127 | unsigned int |
| 1128 | do_set_local_dynsym_indexes(unsigned int); |
| 1129 | |
| 1130 | // Set the offset where local dynamic symbol information will be stored. |
| 1131 | unsigned int |
| 1132 | do_set_local_dynsym_offset(off_t); |
| 1133 | |
| 1134 | // Relocate the input sections and write out the local symbols. |
| 1135 | void |
| 1136 | do_relocate(const General_options& options, const Symbol_table* symtab, |
| 1137 | const Layout*, Output_file* of); |
| 1138 | |
| 1139 | // Get the name of a section. |
| 1140 | std::string |
| 1141 | do_section_name(unsigned int shndx) |
| 1142 | { return this->elf_file_.section_name(shndx); } |
| 1143 | |
| 1144 | // Return the location of the contents of a section. |
| 1145 | Object::Location |
| 1146 | do_section_contents(unsigned int shndx) |
| 1147 | { return this->elf_file_.section_contents(shndx); } |
| 1148 | |
| 1149 | // Return section flags. |
| 1150 | uint64_t |
| 1151 | do_section_flags(unsigned int shndx) |
| 1152 | { return this->elf_file_.section_flags(shndx); } |
| 1153 | |
| 1154 | // Return section type. |
| 1155 | unsigned int |
| 1156 | do_section_type(unsigned int shndx) |
| 1157 | { return this->elf_file_.section_type(shndx); } |
| 1158 | |
| 1159 | // Return the section link field. |
| 1160 | unsigned int |
| 1161 | do_section_link(unsigned int shndx) |
| 1162 | { return this->elf_file_.section_link(shndx); } |
| 1163 | |
| 1164 | // Return the section info field. |
| 1165 | unsigned int |
| 1166 | do_section_info(unsigned int shndx) |
| 1167 | { return this->elf_file_.section_info(shndx); } |
| 1168 | |
| 1169 | private: |
| 1170 | // For convenience. |
| 1171 | typedef Sized_relobj<size, big_endian> This; |
| 1172 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; |
| 1173 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; |
| 1174 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 1175 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
| 1176 | |
| 1177 | // Find the SHT_SYMTAB section, given the section headers. |
| 1178 | void |
| 1179 | find_symtab(const unsigned char* pshdrs); |
| 1180 | |
| 1181 | // Return whether SHDR has the right flags for a GNU style exception |
| 1182 | // frame section. |
| 1183 | bool |
| 1184 | check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const; |
| 1185 | |
| 1186 | // Return whether there is a section named .eh_frame which might be |
| 1187 | // a GNU style exception frame section. |
| 1188 | bool |
| 1189 | find_eh_frame(const unsigned char* pshdrs, const char* names, |
| 1190 | section_size_type names_size) const; |
| 1191 | |
| 1192 | // Whether to include a section group in the link. |
| 1193 | bool |
| 1194 | include_section_group(Layout*, unsigned int, |
| 1195 | const elfcpp::Shdr<size, big_endian>&, |
| 1196 | std::vector<bool>*); |
| 1197 | |
| 1198 | // Whether to include a linkonce section in the link. |
| 1199 | bool |
| 1200 | include_linkonce_section(Layout*, const char*, |
| 1201 | const elfcpp::Shdr<size, big_endian>&); |
| 1202 | |
| 1203 | // Views and sizes when relocating. |
| 1204 | struct View_size |
| 1205 | { |
| 1206 | unsigned char* view; |
| 1207 | typename elfcpp::Elf_types<size>::Elf_Addr address; |
| 1208 | off_t offset; |
| 1209 | section_size_type view_size; |
| 1210 | bool is_input_output_view; |
| 1211 | bool is_postprocessing_view; |
| 1212 | }; |
| 1213 | |
| 1214 | typedef std::vector<View_size> Views; |
| 1215 | |
| 1216 | // Write section data to the output file. Record the views and |
| 1217 | // sizes in VIEWS for use when relocating. |
| 1218 | void |
| 1219 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); |
| 1220 | |
| 1221 | // Relocate the sections in the output file. |
| 1222 | void |
| 1223 | relocate_sections(const General_options& options, const Symbol_table*, |
| 1224 | const Layout*, const unsigned char* pshdrs, Views*); |
| 1225 | |
| 1226 | // Initialize input to output maps for section symbols in merged |
| 1227 | // sections. |
| 1228 | void |
| 1229 | initialize_input_to_output_maps(); |
| 1230 | |
| 1231 | // Free the input to output maps for section symbols in merged |
| 1232 | // sections. |
| 1233 | void |
| 1234 | free_input_to_output_maps(); |
| 1235 | |
| 1236 | // Write out the local symbols. |
| 1237 | void |
| 1238 | write_local_symbols(Output_file*, |
| 1239 | const Stringpool_template<char>*, |
| 1240 | const Stringpool_template<char>*); |
| 1241 | |
| 1242 | // Clear the local symbol information. |
| 1243 | void |
| 1244 | clear_local_symbols() |
| 1245 | { |
| 1246 | this->local_values_.clear(); |
| 1247 | this->local_got_offsets_.clear(); |
| 1248 | this->local_tls_got_offsets_.clear(); |
| 1249 | } |
| 1250 | |
| 1251 | // The GOT offsets of local symbols. This map also stores GOT offsets |
| 1252 | // for tp-relative offsets for TLS symbols. |
| 1253 | typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets; |
| 1254 | |
| 1255 | // The TLS GOT offsets of local symbols. The map stores the offsets |
| 1256 | // for either a single GOT entry that holds the module index of a TLS |
| 1257 | // symbol, or a pair of GOT entries containing the module index and |
| 1258 | // dtv-relative offset. |
| 1259 | struct Tls_got_entry |
| 1260 | { |
| 1261 | Tls_got_entry(int got_offset, bool have_pair) |
| 1262 | : got_offset_(got_offset), |
| 1263 | have_pair_(have_pair) |
| 1264 | { } |
| 1265 | int got_offset_; |
| 1266 | bool have_pair_; |
| 1267 | }; |
| 1268 | typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets; |
| 1269 | |
| 1270 | // General access to the ELF file. |
| 1271 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; |
| 1272 | // Index of SHT_SYMTAB section. |
| 1273 | unsigned int symtab_shndx_; |
| 1274 | // The number of local symbols. |
| 1275 | unsigned int local_symbol_count_; |
| 1276 | // The number of local symbols which go into the output file. |
| 1277 | unsigned int output_local_symbol_count_; |
| 1278 | // The number of local symbols which go into the output file's dynamic |
| 1279 | // symbol table. |
| 1280 | unsigned int output_local_dynsym_count_; |
| 1281 | // The entries in the symbol table for the external symbols. |
| 1282 | Symbols symbols_; |
| 1283 | // File offset for local symbols. |
| 1284 | off_t local_symbol_offset_; |
| 1285 | // File offset for local dynamic symbols. |
| 1286 | off_t local_dynsym_offset_; |
| 1287 | // Values of local symbols. |
| 1288 | Local_values local_values_; |
| 1289 | // GOT offsets for local non-TLS symbols, and tp-relative offsets |
| 1290 | // for TLS symbols, indexed by symbol number. |
| 1291 | Local_got_offsets local_got_offsets_; |
| 1292 | // GOT offsets for local TLS symbols, indexed by symbol number |
| 1293 | // and GOT entry type. |
| 1294 | Local_tls_got_offsets local_tls_got_offsets_; |
| 1295 | // Whether this object has a GNU style .eh_frame section. |
| 1296 | bool has_eh_frame_; |
| 1297 | }; |
| 1298 | |
| 1299 | // A class to manage the list of all objects. |
| 1300 | |
| 1301 | class Input_objects |
| 1302 | { |
| 1303 | public: |
| 1304 | Input_objects() |
| 1305 | : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(), |
| 1306 | system_library_directory_() |
| 1307 | { } |
| 1308 | |
| 1309 | // The type of the list of input relocateable objects. |
| 1310 | typedef std::vector<Relobj*> Relobj_list; |
| 1311 | typedef Relobj_list::const_iterator Relobj_iterator; |
| 1312 | |
| 1313 | // The type of the list of input dynamic objects. |
| 1314 | typedef std::vector<Dynobj*> Dynobj_list; |
| 1315 | typedef Dynobj_list::const_iterator Dynobj_iterator; |
| 1316 | |
| 1317 | // Add an object to the list. Return true if all is well, or false |
| 1318 | // if this object should be ignored. |
| 1319 | bool |
| 1320 | add_object(Object*); |
| 1321 | |
| 1322 | // Get the target we should use for the output file. |
| 1323 | Target* |
| 1324 | target() const |
| 1325 | { return this->target_; } |
| 1326 | |
| 1327 | // For each dynamic object, check whether we've seen all of its |
| 1328 | // explicit dependencies. |
| 1329 | void |
| 1330 | check_dynamic_dependencies() const; |
| 1331 | |
| 1332 | // Return whether an object was found in the system library |
| 1333 | // directory. |
| 1334 | bool |
| 1335 | found_in_system_library_directory(const Object*) const; |
| 1336 | |
| 1337 | // Iterate over all regular objects. |
| 1338 | |
| 1339 | Relobj_iterator |
| 1340 | relobj_begin() const |
| 1341 | { return this->relobj_list_.begin(); } |
| 1342 | |
| 1343 | Relobj_iterator |
| 1344 | relobj_end() const |
| 1345 | { return this->relobj_list_.end(); } |
| 1346 | |
| 1347 | // Iterate over all dynamic objects. |
| 1348 | |
| 1349 | Dynobj_iterator |
| 1350 | dynobj_begin() const |
| 1351 | { return this->dynobj_list_.begin(); } |
| 1352 | |
| 1353 | Dynobj_iterator |
| 1354 | dynobj_end() const |
| 1355 | { return this->dynobj_list_.end(); } |
| 1356 | |
| 1357 | // Return whether we have seen any dynamic objects. |
| 1358 | bool |
| 1359 | any_dynamic() const |
| 1360 | { return !this->dynobj_list_.empty(); } |
| 1361 | |
| 1362 | // Return the number of input objects. |
| 1363 | int |
| 1364 | number_of_input_objects() const |
| 1365 | { return this->relobj_list_.size() + this->dynobj_list_.size(); } |
| 1366 | |
| 1367 | private: |
| 1368 | Input_objects(const Input_objects&); |
| 1369 | Input_objects& operator=(const Input_objects&); |
| 1370 | |
| 1371 | // The list of ordinary objects included in the link. |
| 1372 | Relobj_list relobj_list_; |
| 1373 | // The list of dynamic objects included in the link. |
| 1374 | Dynobj_list dynobj_list_; |
| 1375 | // The target. |
| 1376 | Target* target_; |
| 1377 | // SONAMEs that we have seen. |
| 1378 | Unordered_set<std::string> sonames_; |
| 1379 | // The directory in which we find the libc.so. |
| 1380 | std::string system_library_directory_; |
| 1381 | }; |
| 1382 | |
| 1383 | // Some of the information we pass to the relocation routines. We |
| 1384 | // group this together to avoid passing a dozen different arguments. |
| 1385 | |
| 1386 | template<int size, bool big_endian> |
| 1387 | struct Relocate_info |
| 1388 | { |
| 1389 | // Command line options. |
| 1390 | const General_options* options; |
| 1391 | // Symbol table. |
| 1392 | const Symbol_table* symtab; |
| 1393 | // Layout. |
| 1394 | const Layout* layout; |
| 1395 | // Object being relocated. |
| 1396 | Sized_relobj<size, big_endian>* object; |
| 1397 | // Section index of relocation section. |
| 1398 | unsigned int reloc_shndx; |
| 1399 | // Section index of section being relocated. |
| 1400 | unsigned int data_shndx; |
| 1401 | |
| 1402 | // Return a string showing the location of a relocation. This is |
| 1403 | // only used for error messages. |
| 1404 | std::string |
| 1405 | location(size_t relnum, off_t reloffset) const; |
| 1406 | }; |
| 1407 | |
| 1408 | // Return an Object appropriate for the input file. P is BYTES long, |
| 1409 | // and holds the ELF header. |
| 1410 | |
| 1411 | extern Object* |
| 1412 | make_elf_object(const std::string& name, Input_file*, |
| 1413 | off_t offset, const unsigned char* p, |
| 1414 | section_offset_type bytes); |
| 1415 | |
| 1416 | } // end namespace gold |
| 1417 | |
| 1418 | #endif // !defined(GOLD_OBJECT_H) |