| 1 | // layout.cc -- lay out output file sections for gold |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
| 4 | // Written by Ian Lance Taylor <iant@google.com>. |
| 5 | |
| 6 | // This file is part of gold. |
| 7 | |
| 8 | // This program is free software; you can redistribute it and/or modify |
| 9 | // it under the terms of the GNU General Public License as published by |
| 10 | // the Free Software Foundation; either version 3 of the License, or |
| 11 | // (at your option) any later version. |
| 12 | |
| 13 | // This program is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 21 | // MA 02110-1301, USA. |
| 22 | |
| 23 | #include "gold.h" |
| 24 | |
| 25 | #include <cstring> |
| 26 | #include <algorithm> |
| 27 | #include <iostream> |
| 28 | #include <utility> |
| 29 | |
| 30 | #include "parameters.h" |
| 31 | #include "options.h" |
| 32 | #include "script.h" |
| 33 | #include "script-sections.h" |
| 34 | #include "output.h" |
| 35 | #include "symtab.h" |
| 36 | #include "dynobj.h" |
| 37 | #include "ehframe.h" |
| 38 | #include "compressed_output.h" |
| 39 | #include "reloc.h" |
| 40 | #include "layout.h" |
| 41 | |
| 42 | namespace gold |
| 43 | { |
| 44 | |
| 45 | // Layout_task_runner methods. |
| 46 | |
| 47 | // Lay out the sections. This is called after all the input objects |
| 48 | // have been read. |
| 49 | |
| 50 | void |
| 51 | Layout_task_runner::run(Workqueue* workqueue, const Task* task) |
| 52 | { |
| 53 | off_t file_size = this->layout_->finalize(this->input_objects_, |
| 54 | this->symtab_, |
| 55 | this->target_, |
| 56 | task); |
| 57 | |
| 58 | // Now we know the final size of the output file and we know where |
| 59 | // each piece of information goes. |
| 60 | Output_file* of = new Output_file(parameters->options().output_file_name()); |
| 61 | if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
| 62 | of->set_is_temporary(); |
| 63 | of->open(file_size); |
| 64 | |
| 65 | // Queue up the final set of tasks. |
| 66 | gold::queue_final_tasks(this->options_, this->input_objects_, |
| 67 | this->symtab_, this->layout_, workqueue, of); |
| 68 | } |
| 69 | |
| 70 | // Layout methods. |
| 71 | |
| 72 | Layout::Layout(const General_options& options, Script_options* script_options) |
| 73 | : options_(options), script_options_(script_options), namepool_(), |
| 74 | sympool_(), dynpool_(), signatures_(), |
| 75 | section_name_map_(), segment_list_(), section_list_(), |
| 76 | unattached_section_list_(), special_output_list_(), |
| 77 | section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL), |
| 78 | dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL), |
| 79 | eh_frame_section_(NULL), group_signatures_(), output_file_size_(-1), |
| 80 | input_requires_executable_stack_(false), |
| 81 | input_with_gnu_stack_note_(false), |
| 82 | input_without_gnu_stack_note_(false), |
| 83 | has_static_tls_(false), |
| 84 | any_postprocessing_sections_(false) |
| 85 | { |
| 86 | // Make space for more than enough segments for a typical file. |
| 87 | // This is just for efficiency--it's OK if we wind up needing more. |
| 88 | this->segment_list_.reserve(12); |
| 89 | |
| 90 | // We expect two unattached Output_data objects: the file header and |
| 91 | // the segment headers. |
| 92 | this->special_output_list_.reserve(2); |
| 93 | } |
| 94 | |
| 95 | // Hash a key we use to look up an output section mapping. |
| 96 | |
| 97 | size_t |
| 98 | Layout::Hash_key::operator()(const Layout::Key& k) const |
| 99 | { |
| 100 | return k.first + k.second.first + k.second.second; |
| 101 | } |
| 102 | |
| 103 | // Return whether PREFIX is a prefix of STR. |
| 104 | |
| 105 | static inline bool |
| 106 | is_prefix_of(const char* prefix, const char* str) |
| 107 | { |
| 108 | return strncmp(prefix, str, strlen(prefix)) == 0; |
| 109 | } |
| 110 | |
| 111 | // Returns whether the given section is in the list of |
| 112 | // debug-sections-used-by-some-version-of-gdb. Currently, |
| 113 | // we've checked versions of gdb up to and including 6.7.1. |
| 114 | |
| 115 | static const char* gdb_sections[] = |
| 116 | { ".debug_abbrev", |
| 117 | // ".debug_aranges", // not used by gdb as of 6.7.1 |
| 118 | ".debug_frame", |
| 119 | ".debug_info", |
| 120 | ".debug_line", |
| 121 | ".debug_loc", |
| 122 | ".debug_macinfo", |
| 123 | // ".debug_pubnames", // not used by gdb as of 6.7.1 |
| 124 | ".debug_ranges", |
| 125 | ".debug_str", |
| 126 | }; |
| 127 | |
| 128 | static inline bool |
| 129 | is_gdb_debug_section(const char* str) |
| 130 | { |
| 131 | // We can do this faster: binary search or a hashtable. But why bother? |
| 132 | for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i) |
| 133 | if (strcmp(str, gdb_sections[i]) == 0) |
| 134 | return true; |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // Whether to include this section in the link. |
| 139 | |
| 140 | template<int size, bool big_endian> |
| 141 | bool |
| 142 | Layout::include_section(Sized_relobj<size, big_endian>*, const char* name, |
| 143 | const elfcpp::Shdr<size, big_endian>& shdr) |
| 144 | { |
| 145 | switch (shdr.get_sh_type()) |
| 146 | { |
| 147 | case elfcpp::SHT_NULL: |
| 148 | case elfcpp::SHT_SYMTAB: |
| 149 | case elfcpp::SHT_DYNSYM: |
| 150 | case elfcpp::SHT_STRTAB: |
| 151 | case elfcpp::SHT_HASH: |
| 152 | case elfcpp::SHT_DYNAMIC: |
| 153 | case elfcpp::SHT_SYMTAB_SHNDX: |
| 154 | return false; |
| 155 | |
| 156 | case elfcpp::SHT_RELA: |
| 157 | case elfcpp::SHT_REL: |
| 158 | case elfcpp::SHT_GROUP: |
| 159 | // If we are emitting relocations these should be handled |
| 160 | // elsewhere. |
| 161 | gold_assert(!parameters->options().relocatable() |
| 162 | && !parameters->options().emit_relocs()); |
| 163 | return false; |
| 164 | |
| 165 | case elfcpp::SHT_PROGBITS: |
| 166 | if (parameters->options().strip_debug() |
| 167 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) |
| 168 | { |
| 169 | // Debugging sections can only be recognized by name. |
| 170 | if (is_prefix_of(".debug", name) |
| 171 | || is_prefix_of(".gnu.linkonce.wi.", name) |
| 172 | || is_prefix_of(".line", name) |
| 173 | || is_prefix_of(".stab", name)) |
| 174 | return false; |
| 175 | } |
| 176 | if (parameters->options().strip_debug_gdb() |
| 177 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) |
| 178 | { |
| 179 | // Debugging sections can only be recognized by name. |
| 180 | if (is_prefix_of(".debug", name) |
| 181 | && !is_gdb_debug_section(name)) |
| 182 | return false; |
| 183 | } |
| 184 | return true; |
| 185 | |
| 186 | default: |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Return an output section named NAME, or NULL if there is none. |
| 192 | |
| 193 | Output_section* |
| 194 | Layout::find_output_section(const char* name) const |
| 195 | { |
| 196 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 197 | p != this->section_list_.end(); |
| 198 | ++p) |
| 199 | if (strcmp((*p)->name(), name) == 0) |
| 200 | return *p; |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | // Return an output segment of type TYPE, with segment flags SET set |
| 205 | // and segment flags CLEAR clear. Return NULL if there is none. |
| 206 | |
| 207 | Output_segment* |
| 208 | Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, |
| 209 | elfcpp::Elf_Word clear) const |
| 210 | { |
| 211 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
| 212 | p != this->segment_list_.end(); |
| 213 | ++p) |
| 214 | if (static_cast<elfcpp::PT>((*p)->type()) == type |
| 215 | && ((*p)->flags() & set) == set |
| 216 | && ((*p)->flags() & clear) == 0) |
| 217 | return *p; |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | // Return the output section to use for section NAME with type TYPE |
| 222 | // and section flags FLAGS. NAME must be canonicalized in the string |
| 223 | // pool, and NAME_KEY is the key. |
| 224 | |
| 225 | Output_section* |
| 226 | Layout::get_output_section(const char* name, Stringpool::Key name_key, |
| 227 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags) |
| 228 | { |
| 229 | const Key key(name_key, std::make_pair(type, flags)); |
| 230 | const std::pair<Key, Output_section*> v(key, NULL); |
| 231 | std::pair<Section_name_map::iterator, bool> ins( |
| 232 | this->section_name_map_.insert(v)); |
| 233 | |
| 234 | if (!ins.second) |
| 235 | return ins.first->second; |
| 236 | else |
| 237 | { |
| 238 | // This is the first time we've seen this name/type/flags |
| 239 | // combination. For compatibility with the GNU linker, we |
| 240 | // combine sections with contents and zero flags with sections |
| 241 | // with non-zero flags. This is a workaround for cases where |
| 242 | // assembler code forgets to set section flags. FIXME: Perhaps |
| 243 | // there should be an option to control this. |
| 244 | Output_section* os = NULL; |
| 245 | |
| 246 | if (type == elfcpp::SHT_PROGBITS) |
| 247 | { |
| 248 | if (flags == 0) |
| 249 | { |
| 250 | Output_section* same_name = this->find_output_section(name); |
| 251 | if (same_name != NULL |
| 252 | && same_name->type() == elfcpp::SHT_PROGBITS |
| 253 | && (same_name->flags() & elfcpp::SHF_TLS) == 0) |
| 254 | os = same_name; |
| 255 | } |
| 256 | else if ((flags & elfcpp::SHF_TLS) == 0) |
| 257 | { |
| 258 | elfcpp::Elf_Xword zero_flags = 0; |
| 259 | const Key zero_key(name_key, std::make_pair(type, zero_flags)); |
| 260 | Section_name_map::iterator p = |
| 261 | this->section_name_map_.find(zero_key); |
| 262 | if (p != this->section_name_map_.end()) |
| 263 | { |
| 264 | os = p->second; |
| 265 | if ((flags & elfcpp::SHF_ALLOC) != 0) |
| 266 | this->allocate_output_section(os, flags); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (os == NULL) |
| 272 | os = this->make_output_section(name, type, flags); |
| 273 | ins.first->second = os; |
| 274 | return os; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Pick the output section to use for section NAME, in input file |
| 279 | // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a |
| 280 | // linker created section. ADJUST_NAME is true if we should apply the |
| 281 | // standard name mappings in Layout::output_section_name. This will |
| 282 | // return NULL if the input section should be discarded. |
| 283 | |
| 284 | Output_section* |
| 285 | Layout::choose_output_section(const Relobj* relobj, const char* name, |
| 286 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags, |
| 287 | bool adjust_name) |
| 288 | { |
| 289 | // We should ignore some flags. FIXME: This will need some |
| 290 | // adjustment for ld -r. |
| 291 | flags &= ~ (elfcpp::SHF_INFO_LINK |
| 292 | | elfcpp::SHF_LINK_ORDER |
| 293 | | elfcpp::SHF_GROUP |
| 294 | | elfcpp::SHF_MERGE |
| 295 | | elfcpp::SHF_STRINGS); |
| 296 | |
| 297 | if (this->script_options_->saw_sections_clause()) |
| 298 | { |
| 299 | // We are using a SECTIONS clause, so the output section is |
| 300 | // chosen based only on the name. |
| 301 | |
| 302 | Script_sections* ss = this->script_options_->script_sections(); |
| 303 | const char* file_name = relobj == NULL ? NULL : relobj->name().c_str(); |
| 304 | Output_section** output_section_slot; |
| 305 | name = ss->output_section_name(file_name, name, &output_section_slot); |
| 306 | if (name == NULL) |
| 307 | { |
| 308 | // The SECTIONS clause says to discard this input section. |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | // If this is an orphan section--one not mentioned in the linker |
| 313 | // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the |
| 314 | // default processing below. |
| 315 | |
| 316 | if (output_section_slot != NULL) |
| 317 | { |
| 318 | if (*output_section_slot != NULL) |
| 319 | { |
| 320 | // If the output section was created unallocated, and we |
| 321 | // are now allocating it, then we need to clear the |
| 322 | // address set in the constructor and remove it from the |
| 323 | // unattached section list. |
| 324 | if (((*output_section_slot)->flags() & elfcpp::SHF_ALLOC) == 0 |
| 325 | && (flags & elfcpp::SHF_ALLOC) != 0) |
| 326 | this->allocate_output_section(*output_section_slot, flags); |
| 327 | |
| 328 | return *output_section_slot; |
| 329 | } |
| 330 | |
| 331 | // We don't put sections found in the linker script into |
| 332 | // SECTION_NAME_MAP_. That keeps us from getting confused |
| 333 | // if an orphan section is mapped to a section with the same |
| 334 | // name as one in the linker script. |
| 335 | |
| 336 | name = this->namepool_.add(name, false, NULL); |
| 337 | |
| 338 | Output_section* os = this->make_output_section(name, type, flags); |
| 339 | os->set_found_in_sections_clause(); |
| 340 | *output_section_slot = os; |
| 341 | return os; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // FIXME: Handle SHF_OS_NONCONFORMING somewhere. |
| 346 | |
| 347 | // Turn NAME from the name of the input section into the name of the |
| 348 | // output section. |
| 349 | |
| 350 | size_t len = strlen(name); |
| 351 | if (adjust_name && !parameters->options().relocatable()) |
| 352 | name = Layout::output_section_name(name, &len); |
| 353 | |
| 354 | Stringpool::Key name_key; |
| 355 | name = this->namepool_.add_with_length(name, len, true, &name_key); |
| 356 | |
| 357 | // Find or make the output section. The output section is selected |
| 358 | // based on the section name, type, and flags. |
| 359 | return this->get_output_section(name, name_key, type, flags); |
| 360 | } |
| 361 | |
| 362 | // Return the output section to use for input section SHNDX, with name |
| 363 | // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the |
| 364 | // index of a relocation section which applies to this section, or 0 |
| 365 | // if none, or -1U if more than one. RELOC_TYPE is the type of the |
| 366 | // relocation section if there is one. Set *OFF to the offset of this |
| 367 | // input section without the output section. Return NULL if the |
| 368 | // section should be discarded. Set *OFF to -1 if the section |
| 369 | // contents should not be written directly to the output file, but |
| 370 | // will instead receive special handling. |
| 371 | |
| 372 | template<int size, bool big_endian> |
| 373 | Output_section* |
| 374 | Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
| 375 | const char* name, const elfcpp::Shdr<size, big_endian>& shdr, |
| 376 | unsigned int reloc_shndx, unsigned int, off_t* off) |
| 377 | { |
| 378 | if (!this->include_section(object, name, shdr)) |
| 379 | return NULL; |
| 380 | |
| 381 | Output_section* os; |
| 382 | |
| 383 | // In a relocatable link a grouped section must not be combined with |
| 384 | // any other sections. |
| 385 | if (parameters->options().relocatable() |
| 386 | && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0) |
| 387 | { |
| 388 | name = this->namepool_.add(name, true, NULL); |
| 389 | os = this->make_output_section(name, shdr.get_sh_type(), |
| 390 | shdr.get_sh_flags()); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | os = this->choose_output_section(object, name, shdr.get_sh_type(), |
| 395 | shdr.get_sh_flags(), true); |
| 396 | if (os == NULL) |
| 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | // FIXME: Handle SHF_LINK_ORDER somewhere. |
| 401 | |
| 402 | *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx, |
| 403 | this->script_options_->saw_sections_clause()); |
| 404 | |
| 405 | return os; |
| 406 | } |
| 407 | |
| 408 | // Handle a relocation section when doing a relocatable link. |
| 409 | |
| 410 | template<int size, bool big_endian> |
| 411 | Output_section* |
| 412 | Layout::layout_reloc(Sized_relobj<size, big_endian>* object, |
| 413 | unsigned int, |
| 414 | const elfcpp::Shdr<size, big_endian>& shdr, |
| 415 | Output_section* data_section, |
| 416 | Relocatable_relocs* rr) |
| 417 | { |
| 418 | gold_assert(parameters->options().relocatable() |
| 419 | || parameters->options().emit_relocs()); |
| 420 | |
| 421 | int sh_type = shdr.get_sh_type(); |
| 422 | |
| 423 | std::string name; |
| 424 | if (sh_type == elfcpp::SHT_REL) |
| 425 | name = ".rel"; |
| 426 | else if (sh_type == elfcpp::SHT_RELA) |
| 427 | name = ".rela"; |
| 428 | else |
| 429 | gold_unreachable(); |
| 430 | name += data_section->name(); |
| 431 | |
| 432 | Output_section* os = this->choose_output_section(object, name.c_str(), |
| 433 | sh_type, |
| 434 | shdr.get_sh_flags(), |
| 435 | false); |
| 436 | |
| 437 | os->set_should_link_to_symtab(); |
| 438 | os->set_info_section(data_section); |
| 439 | |
| 440 | Output_section_data* posd; |
| 441 | if (sh_type == elfcpp::SHT_REL) |
| 442 | { |
| 443 | os->set_entsize(elfcpp::Elf_sizes<size>::rel_size); |
| 444 | posd = new Output_relocatable_relocs<elfcpp::SHT_REL, |
| 445 | size, |
| 446 | big_endian>(rr); |
| 447 | } |
| 448 | else if (sh_type == elfcpp::SHT_RELA) |
| 449 | { |
| 450 | os->set_entsize(elfcpp::Elf_sizes<size>::rela_size); |
| 451 | posd = new Output_relocatable_relocs<elfcpp::SHT_RELA, |
| 452 | size, |
| 453 | big_endian>(rr); |
| 454 | } |
| 455 | else |
| 456 | gold_unreachable(); |
| 457 | |
| 458 | os->add_output_section_data(posd); |
| 459 | rr->set_output_data(posd); |
| 460 | |
| 461 | return os; |
| 462 | } |
| 463 | |
| 464 | // Handle a group section when doing a relocatable link. |
| 465 | |
| 466 | template<int size, bool big_endian> |
| 467 | void |
| 468 | Layout::layout_group(Symbol_table* symtab, |
| 469 | Sized_relobj<size, big_endian>* object, |
| 470 | unsigned int, |
| 471 | const char* group_section_name, |
| 472 | const char* signature, |
| 473 | const elfcpp::Shdr<size, big_endian>& shdr, |
| 474 | const elfcpp::Elf_Word* contents) |
| 475 | { |
| 476 | gold_assert(parameters->options().relocatable()); |
| 477 | gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP); |
| 478 | group_section_name = this->namepool_.add(group_section_name, true, NULL); |
| 479 | Output_section* os = this->make_output_section(group_section_name, |
| 480 | elfcpp::SHT_GROUP, |
| 481 | shdr.get_sh_flags()); |
| 482 | |
| 483 | // We need to find a symbol with the signature in the symbol table. |
| 484 | // If we don't find one now, we need to look again later. |
| 485 | Symbol* sym = symtab->lookup(signature, NULL); |
| 486 | if (sym != NULL) |
| 487 | os->set_info_symndx(sym); |
| 488 | else |
| 489 | { |
| 490 | // We will wind up using a symbol whose name is the signature. |
| 491 | // So just put the signature in the symbol name pool to save it. |
| 492 | signature = symtab->canonicalize_name(signature); |
| 493 | this->group_signatures_.push_back(Group_signature(os, signature)); |
| 494 | } |
| 495 | |
| 496 | os->set_should_link_to_symtab(); |
| 497 | os->set_entsize(4); |
| 498 | |
| 499 | section_size_type entry_count = |
| 500 | convert_to_section_size_type(shdr.get_sh_size() / 4); |
| 501 | Output_section_data* posd = |
| 502 | new Output_data_group<size, big_endian>(object, entry_count, contents); |
| 503 | os->add_output_section_data(posd); |
| 504 | } |
| 505 | |
| 506 | // Special GNU handling of sections name .eh_frame. They will |
| 507 | // normally hold exception frame data as defined by the C++ ABI |
| 508 | // (http://codesourcery.com/cxx-abi/). |
| 509 | |
| 510 | template<int size, bool big_endian> |
| 511 | Output_section* |
| 512 | Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object, |
| 513 | const unsigned char* symbols, |
| 514 | off_t symbols_size, |
| 515 | const unsigned char* symbol_names, |
| 516 | off_t symbol_names_size, |
| 517 | unsigned int shndx, |
| 518 | const elfcpp::Shdr<size, big_endian>& shdr, |
| 519 | unsigned int reloc_shndx, unsigned int reloc_type, |
| 520 | off_t* off) |
| 521 | { |
| 522 | gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS); |
| 523 | gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0); |
| 524 | |
| 525 | const char* const name = ".eh_frame"; |
| 526 | Output_section* os = this->choose_output_section(object, |
| 527 | name, |
| 528 | elfcpp::SHT_PROGBITS, |
| 529 | elfcpp::SHF_ALLOC, |
| 530 | false); |
| 531 | if (os == NULL) |
| 532 | return NULL; |
| 533 | |
| 534 | // On some targets gcc assumes that a read-only .eh_frame section |
| 535 | // will be merged with a read-write .eh_frame section. |
| 536 | if ((shdr.get_sh_flags() & elfcpp::SHF_WRITE) != 0 |
| 537 | && (os->flags() & elfcpp::SHF_WRITE) == 0) |
| 538 | { |
| 539 | elfcpp::Elf_Xword new_flags = os->flags() | elfcpp::SHF_WRITE; |
| 540 | this->write_enable_output_section(os, new_flags); |
| 541 | os->set_flags(new_flags); |
| 542 | } |
| 543 | |
| 544 | if (this->eh_frame_section_ == NULL) |
| 545 | { |
| 546 | this->eh_frame_section_ = os; |
| 547 | this->eh_frame_data_ = new Eh_frame(); |
| 548 | os->add_output_section_data(this->eh_frame_data_); |
| 549 | |
| 550 | if (this->options_.eh_frame_hdr()) |
| 551 | { |
| 552 | Output_section* hdr_os = |
| 553 | this->choose_output_section(NULL, |
| 554 | ".eh_frame_hdr", |
| 555 | elfcpp::SHT_PROGBITS, |
| 556 | elfcpp::SHF_ALLOC, |
| 557 | false); |
| 558 | |
| 559 | if (hdr_os != NULL) |
| 560 | { |
| 561 | Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os, |
| 562 | this->eh_frame_data_); |
| 563 | hdr_os->add_output_section_data(hdr_posd); |
| 564 | |
| 565 | hdr_os->set_after_input_sections(); |
| 566 | |
| 567 | if (!this->script_options_->saw_phdrs_clause()) |
| 568 | { |
| 569 | Output_segment* hdr_oseg; |
| 570 | hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME, |
| 571 | elfcpp::PF_R); |
| 572 | hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R); |
| 573 | } |
| 574 | |
| 575 | this->eh_frame_data_->set_eh_frame_hdr(hdr_posd); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | gold_assert(this->eh_frame_section_ == os); |
| 581 | |
| 582 | if (this->eh_frame_data_->add_ehframe_input_section(object, |
| 583 | symbols, |
| 584 | symbols_size, |
| 585 | symbol_names, |
| 586 | symbol_names_size, |
| 587 | shndx, |
| 588 | reloc_shndx, |
| 589 | reloc_type)) |
| 590 | *off = -1; |
| 591 | else |
| 592 | { |
| 593 | // We couldn't handle this .eh_frame section for some reason. |
| 594 | // Add it as a normal section. |
| 595 | bool saw_sections_clause = this->script_options_->saw_sections_clause(); |
| 596 | *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx, |
| 597 | saw_sections_clause); |
| 598 | } |
| 599 | |
| 600 | return os; |
| 601 | } |
| 602 | |
| 603 | // Add POSD to an output section using NAME, TYPE, and FLAGS. |
| 604 | |
| 605 | void |
| 606 | Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type, |
| 607 | elfcpp::Elf_Xword flags, |
| 608 | Output_section_data* posd) |
| 609 | { |
| 610 | Output_section* os = this->choose_output_section(NULL, name, type, flags, |
| 611 | false); |
| 612 | if (os != NULL) |
| 613 | os->add_output_section_data(posd); |
| 614 | } |
| 615 | |
| 616 | // Map section flags to segment flags. |
| 617 | |
| 618 | elfcpp::Elf_Word |
| 619 | Layout::section_flags_to_segment(elfcpp::Elf_Xword flags) |
| 620 | { |
| 621 | elfcpp::Elf_Word ret = elfcpp::PF_R; |
| 622 | if ((flags & elfcpp::SHF_WRITE) != 0) |
| 623 | ret |= elfcpp::PF_W; |
| 624 | if ((flags & elfcpp::SHF_EXECINSTR) != 0) |
| 625 | ret |= elfcpp::PF_X; |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | // Sometimes we compress sections. This is typically done for |
| 630 | // sections that are not part of normal program execution (such as |
| 631 | // .debug_* sections), and where the readers of these sections know |
| 632 | // how to deal with compressed sections. (To make it easier for them, |
| 633 | // we will rename the ouput section in such cases from .foo to |
| 634 | // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine |
| 635 | // doesn't say for certain whether we'll compress -- it depends on |
| 636 | // commandline options as well -- just whether this section is a |
| 637 | // candidate for compression. |
| 638 | |
| 639 | static bool |
| 640 | is_compressible_debug_section(const char* secname) |
| 641 | { |
| 642 | return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0); |
| 643 | } |
| 644 | |
| 645 | // Make a new Output_section, and attach it to segments as |
| 646 | // appropriate. |
| 647 | |
| 648 | Output_section* |
| 649 | Layout::make_output_section(const char* name, elfcpp::Elf_Word type, |
| 650 | elfcpp::Elf_Xword flags) |
| 651 | { |
| 652 | Output_section* os; |
| 653 | if ((flags & elfcpp::SHF_ALLOC) == 0 |
| 654 | && strcmp(this->options_.compress_debug_sections(), "none") != 0 |
| 655 | && is_compressible_debug_section(name)) |
| 656 | os = new Output_compressed_section(&this->options_, name, type, flags); |
| 657 | else |
| 658 | os = new Output_section(name, type, flags); |
| 659 | |
| 660 | this->section_list_.push_back(os); |
| 661 | |
| 662 | if ((flags & elfcpp::SHF_ALLOC) == 0) |
| 663 | this->unattached_section_list_.push_back(os); |
| 664 | else |
| 665 | this->attach_to_segment(os, flags); |
| 666 | |
| 667 | return os; |
| 668 | } |
| 669 | |
| 670 | // Attach an allocated output section to a segment. |
| 671 | |
| 672 | void |
| 673 | Layout::attach_to_segment(Output_section* os, elfcpp::Elf_Xword flags) |
| 674 | { |
| 675 | gold_assert((flags & elfcpp::SHF_ALLOC) != 0); |
| 676 | |
| 677 | if (parameters->options().relocatable()) |
| 678 | return; |
| 679 | |
| 680 | // If we have a SECTIONS clause, we can't handle the attachment to |
| 681 | // segments until after we've seen all the sections. |
| 682 | if (this->script_options_->saw_sections_clause()) |
| 683 | return; |
| 684 | |
| 685 | gold_assert(!this->script_options_->saw_phdrs_clause()); |
| 686 | |
| 687 | // This output section goes into a PT_LOAD segment. |
| 688 | |
| 689 | elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags); |
| 690 | |
| 691 | // In general the only thing we really care about for PT_LOAD |
| 692 | // segments is whether or not they are writable, so that is how we |
| 693 | // search for them. People who need segments sorted on some other |
| 694 | // basis will have to use a linker script. |
| 695 | |
| 696 | Segment_list::const_iterator p; |
| 697 | for (p = this->segment_list_.begin(); |
| 698 | p != this->segment_list_.end(); |
| 699 | ++p) |
| 700 | { |
| 701 | if ((*p)->type() == elfcpp::PT_LOAD |
| 702 | && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W)) |
| 703 | { |
| 704 | // If -Tbss was specified, we need to separate the data |
| 705 | // and BSS segments. |
| 706 | if (this->options_.user_set_Tbss()) |
| 707 | { |
| 708 | if ((os->type() == elfcpp::SHT_NOBITS) |
| 709 | == (*p)->has_any_data_sections()) |
| 710 | continue; |
| 711 | } |
| 712 | |
| 713 | (*p)->add_output_section(os, seg_flags); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (p == this->segment_list_.end()) |
| 719 | { |
| 720 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD, |
| 721 | seg_flags); |
| 722 | oseg->add_output_section(os, seg_flags); |
| 723 | } |
| 724 | |
| 725 | // If we see a loadable SHT_NOTE section, we create a PT_NOTE |
| 726 | // segment. |
| 727 | if (os->type() == elfcpp::SHT_NOTE) |
| 728 | { |
| 729 | // See if we already have an equivalent PT_NOTE segment. |
| 730 | for (p = this->segment_list_.begin(); |
| 731 | p != segment_list_.end(); |
| 732 | ++p) |
| 733 | { |
| 734 | if ((*p)->type() == elfcpp::PT_NOTE |
| 735 | && (((*p)->flags() & elfcpp::PF_W) |
| 736 | == (seg_flags & elfcpp::PF_W))) |
| 737 | { |
| 738 | (*p)->add_output_section(os, seg_flags); |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (p == this->segment_list_.end()) |
| 744 | { |
| 745 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE, |
| 746 | seg_flags); |
| 747 | oseg->add_output_section(os, seg_flags); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // If we see a loadable SHF_TLS section, we create a PT_TLS |
| 752 | // segment. There can only be one such segment. |
| 753 | if ((flags & elfcpp::SHF_TLS) != 0) |
| 754 | { |
| 755 | if (this->tls_segment_ == NULL) |
| 756 | this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS, |
| 757 | seg_flags); |
| 758 | this->tls_segment_->add_output_section(os, seg_flags); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | // Make an output section for a script. |
| 763 | |
| 764 | Output_section* |
| 765 | Layout::make_output_section_for_script(const char* name) |
| 766 | { |
| 767 | name = this->namepool_.add(name, false, NULL); |
| 768 | Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS, |
| 769 | elfcpp::SHF_ALLOC); |
| 770 | os->set_found_in_sections_clause(); |
| 771 | return os; |
| 772 | } |
| 773 | |
| 774 | // We have to move an existing output section from the unallocated |
| 775 | // list to the allocated list. |
| 776 | |
| 777 | void |
| 778 | Layout::allocate_output_section(Output_section* os, elfcpp::Elf_Xword flags) |
| 779 | { |
| 780 | os->reset_address_and_file_offset(); |
| 781 | |
| 782 | Section_list::iterator p = std::find(this->unattached_section_list_.begin(), |
| 783 | this->unattached_section_list_.end(), |
| 784 | os); |
| 785 | gold_assert(p != this->unattached_section_list_.end()); |
| 786 | this->unattached_section_list_.erase(p); |
| 787 | |
| 788 | this->attach_to_segment(os, flags); |
| 789 | } |
| 790 | |
| 791 | // We have to move an existing output section from the read-only |
| 792 | // segment to the writable segment. |
| 793 | |
| 794 | void |
| 795 | Layout::write_enable_output_section(Output_section* os, |
| 796 | elfcpp::Elf_Xword flags) |
| 797 | { |
| 798 | gold_assert((os->flags() & elfcpp::SHF_WRITE) == 0); |
| 799 | gold_assert(os->type() == elfcpp::SHT_PROGBITS); |
| 800 | gold_assert((flags & elfcpp::SHF_WRITE) != 0); |
| 801 | gold_assert((flags & elfcpp::SHF_ALLOC) != 0); |
| 802 | |
| 803 | if (parameters->options().relocatable()) |
| 804 | return; |
| 805 | |
| 806 | if (this->script_options_->saw_sections_clause()) |
| 807 | return; |
| 808 | |
| 809 | Segment_list::iterator p; |
| 810 | for (p = this->segment_list_.begin(); |
| 811 | p != this->segment_list_.end(); |
| 812 | ++p) |
| 813 | { |
| 814 | if ((*p)->type() == elfcpp::PT_LOAD |
| 815 | && ((*p)->flags() & elfcpp::PF_W) == 0) |
| 816 | { |
| 817 | (*p)->remove_output_section(os); |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | gold_assert(p != this->segment_list_.end()); |
| 822 | |
| 823 | this->attach_to_segment(os, flags); |
| 824 | } |
| 825 | |
| 826 | // Return the number of segments we expect to see. |
| 827 | |
| 828 | size_t |
| 829 | Layout::expected_segment_count() const |
| 830 | { |
| 831 | size_t ret = this->segment_list_.size(); |
| 832 | |
| 833 | // If we didn't see a SECTIONS clause in a linker script, we should |
| 834 | // already have the complete list of segments. Otherwise we ask the |
| 835 | // SECTIONS clause how many segments it expects, and add in the ones |
| 836 | // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.) |
| 837 | |
| 838 | if (!this->script_options_->saw_sections_clause()) |
| 839 | return ret; |
| 840 | else |
| 841 | { |
| 842 | const Script_sections* ss = this->script_options_->script_sections(); |
| 843 | return ret + ss->expected_segment_count(this); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK |
| 848 | // is whether we saw a .note.GNU-stack section in the object file. |
| 849 | // GNU_STACK_FLAGS is the section flags. The flags give the |
| 850 | // protection required for stack memory. We record this in an |
| 851 | // executable as a PT_GNU_STACK segment. If an object file does not |
| 852 | // have a .note.GNU-stack segment, we must assume that it is an old |
| 853 | // object. On some targets that will force an executable stack. |
| 854 | |
| 855 | void |
| 856 | Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags) |
| 857 | { |
| 858 | if (!seen_gnu_stack) |
| 859 | this->input_without_gnu_stack_note_ = true; |
| 860 | else |
| 861 | { |
| 862 | this->input_with_gnu_stack_note_ = true; |
| 863 | if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0) |
| 864 | this->input_requires_executable_stack_ = true; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | // Create the dynamic sections which are needed before we read the |
| 869 | // relocs. |
| 870 | |
| 871 | void |
| 872 | Layout::create_initial_dynamic_sections(Symbol_table* symtab) |
| 873 | { |
| 874 | if (parameters->doing_static_link()) |
| 875 | return; |
| 876 | |
| 877 | this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic", |
| 878 | elfcpp::SHT_DYNAMIC, |
| 879 | (elfcpp::SHF_ALLOC |
| 880 | | elfcpp::SHF_WRITE), |
| 881 | false); |
| 882 | |
| 883 | symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0, |
| 884 | elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, |
| 885 | elfcpp::STV_HIDDEN, 0, false, false); |
| 886 | |
| 887 | this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_); |
| 888 | |
| 889 | this->dynamic_section_->add_output_section_data(this->dynamic_data_); |
| 890 | } |
| 891 | |
| 892 | // For each output section whose name can be represented as C symbol, |
| 893 | // define __start and __stop symbols for the section. This is a GNU |
| 894 | // extension. |
| 895 | |
| 896 | void |
| 897 | Layout::define_section_symbols(Symbol_table* symtab) |
| 898 | { |
| 899 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 900 | p != this->section_list_.end(); |
| 901 | ++p) |
| 902 | { |
| 903 | const char* const name = (*p)->name(); |
| 904 | if (name[strspn(name, |
| 905 | ("0123456789" |
| 906 | "ABCDEFGHIJKLMNOPWRSTUVWXYZ" |
| 907 | "abcdefghijklmnopqrstuvwxyz" |
| 908 | "_"))] |
| 909 | == '\0') |
| 910 | { |
| 911 | const std::string name_string(name); |
| 912 | const std::string start_name("__start_" + name_string); |
| 913 | const std::string stop_name("__stop_" + name_string); |
| 914 | |
| 915 | symtab->define_in_output_data(start_name.c_str(), |
| 916 | NULL, // version |
| 917 | *p, |
| 918 | 0, // value |
| 919 | 0, // symsize |
| 920 | elfcpp::STT_NOTYPE, |
| 921 | elfcpp::STB_GLOBAL, |
| 922 | elfcpp::STV_DEFAULT, |
| 923 | 0, // nonvis |
| 924 | false, // offset_is_from_end |
| 925 | true); // only_if_ref |
| 926 | |
| 927 | symtab->define_in_output_data(stop_name.c_str(), |
| 928 | NULL, // version |
| 929 | *p, |
| 930 | 0, // value |
| 931 | 0, // symsize |
| 932 | elfcpp::STT_NOTYPE, |
| 933 | elfcpp::STB_GLOBAL, |
| 934 | elfcpp::STV_DEFAULT, |
| 935 | 0, // nonvis |
| 936 | true, // offset_is_from_end |
| 937 | true); // only_if_ref |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | // Define symbols for group signatures. |
| 943 | |
| 944 | void |
| 945 | Layout::define_group_signatures(Symbol_table* symtab) |
| 946 | { |
| 947 | for (Group_signatures::iterator p = this->group_signatures_.begin(); |
| 948 | p != this->group_signatures_.end(); |
| 949 | ++p) |
| 950 | { |
| 951 | Symbol* sym = symtab->lookup(p->signature, NULL); |
| 952 | if (sym != NULL) |
| 953 | p->section->set_info_symndx(sym); |
| 954 | else |
| 955 | { |
| 956 | // Force the name of the group section to the group |
| 957 | // signature, and use the group's section symbol as the |
| 958 | // signature symbol. |
| 959 | if (strcmp(p->section->name(), p->signature) != 0) |
| 960 | { |
| 961 | const char* name = this->namepool_.add(p->signature, |
| 962 | true, NULL); |
| 963 | p->section->set_name(name); |
| 964 | } |
| 965 | p->section->set_needs_symtab_index(); |
| 966 | p->section->set_info_section_symndx(p->section); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | this->group_signatures_.clear(); |
| 971 | } |
| 972 | |
| 973 | // Find the first read-only PT_LOAD segment, creating one if |
| 974 | // necessary. |
| 975 | |
| 976 | Output_segment* |
| 977 | Layout::find_first_load_seg() |
| 978 | { |
| 979 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
| 980 | p != this->segment_list_.end(); |
| 981 | ++p) |
| 982 | { |
| 983 | if ((*p)->type() == elfcpp::PT_LOAD |
| 984 | && ((*p)->flags() & elfcpp::PF_R) != 0 |
| 985 | && ((*p)->flags() & elfcpp::PF_W) == 0) |
| 986 | return *p; |
| 987 | } |
| 988 | |
| 989 | gold_assert(!this->script_options_->saw_phdrs_clause()); |
| 990 | |
| 991 | Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD, |
| 992 | elfcpp::PF_R); |
| 993 | return load_seg; |
| 994 | } |
| 995 | |
| 996 | // Finalize the layout. When this is called, we have created all the |
| 997 | // output sections and all the output segments which are based on |
| 998 | // input sections. We have several things to do, and we have to do |
| 999 | // them in the right order, so that we get the right results correctly |
| 1000 | // and efficiently. |
| 1001 | |
| 1002 | // 1) Finalize the list of output segments and create the segment |
| 1003 | // table header. |
| 1004 | |
| 1005 | // 2) Finalize the dynamic symbol table and associated sections. |
| 1006 | |
| 1007 | // 3) Determine the final file offset of all the output segments. |
| 1008 | |
| 1009 | // 4) Determine the final file offset of all the SHF_ALLOC output |
| 1010 | // sections. |
| 1011 | |
| 1012 | // 5) Create the symbol table sections and the section name table |
| 1013 | // section. |
| 1014 | |
| 1015 | // 6) Finalize the symbol table: set symbol values to their final |
| 1016 | // value and make a final determination of which symbols are going |
| 1017 | // into the output symbol table. |
| 1018 | |
| 1019 | // 7) Create the section table header. |
| 1020 | |
| 1021 | // 8) Determine the final file offset of all the output sections which |
| 1022 | // are not SHF_ALLOC, including the section table header. |
| 1023 | |
| 1024 | // 9) Finalize the ELF file header. |
| 1025 | |
| 1026 | // This function returns the size of the output file. |
| 1027 | |
| 1028 | off_t |
| 1029 | Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab, |
| 1030 | Target* target, const Task* task) |
| 1031 | { |
| 1032 | target->finalize_sections(this); |
| 1033 | |
| 1034 | this->count_local_symbols(task, input_objects); |
| 1035 | |
| 1036 | this->create_gold_note(); |
| 1037 | this->create_executable_stack_info(target); |
| 1038 | |
| 1039 | Output_segment* phdr_seg = NULL; |
| 1040 | if (!parameters->options().relocatable() && !parameters->doing_static_link()) |
| 1041 | { |
| 1042 | // There was a dynamic object in the link. We need to create |
| 1043 | // some information for the dynamic linker. |
| 1044 | |
| 1045 | // Create the PT_PHDR segment which will hold the program |
| 1046 | // headers. |
| 1047 | if (!this->script_options_->saw_phdrs_clause()) |
| 1048 | phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R); |
| 1049 | |
| 1050 | // Create the dynamic symbol table, including the hash table. |
| 1051 | Output_section* dynstr; |
| 1052 | std::vector<Symbol*> dynamic_symbols; |
| 1053 | unsigned int local_dynamic_count; |
| 1054 | Versions versions(*this->script_options()->version_script_info(), |
| 1055 | &this->dynpool_); |
| 1056 | this->create_dynamic_symtab(input_objects, symtab, &dynstr, |
| 1057 | &local_dynamic_count, &dynamic_symbols, |
| 1058 | &versions); |
| 1059 | |
| 1060 | // Create the .interp section to hold the name of the |
| 1061 | // interpreter, and put it in a PT_INTERP segment. |
| 1062 | if (!parameters->options().shared()) |
| 1063 | this->create_interp(target); |
| 1064 | |
| 1065 | // Finish the .dynamic section to hold the dynamic data, and put |
| 1066 | // it in a PT_DYNAMIC segment. |
| 1067 | this->finish_dynamic_section(input_objects, symtab); |
| 1068 | |
| 1069 | // We should have added everything we need to the dynamic string |
| 1070 | // table. |
| 1071 | this->dynpool_.set_string_offsets(); |
| 1072 | |
| 1073 | // Create the version sections. We can't do this until the |
| 1074 | // dynamic string table is complete. |
| 1075 | this->create_version_sections(&versions, symtab, local_dynamic_count, |
| 1076 | dynamic_symbols, dynstr); |
| 1077 | } |
| 1078 | |
| 1079 | // If there is a SECTIONS clause, put all the input sections into |
| 1080 | // the required order. |
| 1081 | Output_segment* load_seg; |
| 1082 | if (this->script_options_->saw_sections_clause()) |
| 1083 | load_seg = this->set_section_addresses_from_script(symtab); |
| 1084 | else if (parameters->options().relocatable()) |
| 1085 | load_seg = NULL; |
| 1086 | else |
| 1087 | load_seg = this->find_first_load_seg(); |
| 1088 | |
| 1089 | if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
| 1090 | load_seg = NULL; |
| 1091 | |
| 1092 | gold_assert(phdr_seg == NULL || load_seg != NULL); |
| 1093 | |
| 1094 | // Lay out the segment headers. |
| 1095 | Output_segment_headers* segment_headers; |
| 1096 | if (parameters->options().relocatable()) |
| 1097 | segment_headers = NULL; |
| 1098 | else |
| 1099 | { |
| 1100 | segment_headers = new Output_segment_headers(this->segment_list_); |
| 1101 | if (load_seg != NULL) |
| 1102 | load_seg->add_initial_output_data(segment_headers); |
| 1103 | if (phdr_seg != NULL) |
| 1104 | phdr_seg->add_initial_output_data(segment_headers); |
| 1105 | } |
| 1106 | |
| 1107 | // Lay out the file header. |
| 1108 | Output_file_header* file_header; |
| 1109 | file_header = new Output_file_header(target, symtab, segment_headers, |
| 1110 | this->options_.entry()); |
| 1111 | if (load_seg != NULL) |
| 1112 | load_seg->add_initial_output_data(file_header); |
| 1113 | |
| 1114 | this->special_output_list_.push_back(file_header); |
| 1115 | if (segment_headers != NULL) |
| 1116 | this->special_output_list_.push_back(segment_headers); |
| 1117 | |
| 1118 | if (this->script_options_->saw_phdrs_clause() |
| 1119 | && !parameters->options().relocatable()) |
| 1120 | { |
| 1121 | // Support use of FILEHDRS and PHDRS attachments in a PHDRS |
| 1122 | // clause in a linker script. |
| 1123 | Script_sections* ss = this->script_options_->script_sections(); |
| 1124 | ss->put_headers_in_phdrs(file_header, segment_headers); |
| 1125 | } |
| 1126 | |
| 1127 | // We set the output section indexes in set_segment_offsets and |
| 1128 | // set_section_indexes. |
| 1129 | unsigned int shndx = 1; |
| 1130 | |
| 1131 | // Set the file offsets of all the segments, and all the sections |
| 1132 | // they contain. |
| 1133 | off_t off; |
| 1134 | if (!parameters->options().relocatable()) |
| 1135 | off = this->set_segment_offsets(target, load_seg, &shndx); |
| 1136 | else |
| 1137 | off = this->set_relocatable_section_offsets(file_header, &shndx); |
| 1138 | |
| 1139 | // Set the file offsets of all the non-data sections we've seen so |
| 1140 | // far which don't have to wait for the input sections. We need |
| 1141 | // this in order to finalize local symbols in non-allocated |
| 1142 | // sections. |
| 1143 | off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS); |
| 1144 | |
| 1145 | // Create the symbol table sections. |
| 1146 | this->create_symtab_sections(input_objects, symtab, &off); |
| 1147 | if (!parameters->doing_static_link()) |
| 1148 | this->assign_local_dynsym_offsets(input_objects); |
| 1149 | |
| 1150 | // Process any symbol assignments from a linker script. This must |
| 1151 | // be called after the symbol table has been finalized. |
| 1152 | this->script_options_->finalize_symbols(symtab, this); |
| 1153 | |
| 1154 | // Create the .shstrtab section. |
| 1155 | Output_section* shstrtab_section = this->create_shstrtab(); |
| 1156 | |
| 1157 | // Set the file offsets of the rest of the non-data sections which |
| 1158 | // don't have to wait for the input sections. |
| 1159 | off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS); |
| 1160 | |
| 1161 | // Now that all sections have been created, set the section indexes. |
| 1162 | shndx = this->set_section_indexes(shndx); |
| 1163 | |
| 1164 | // Create the section table header. |
| 1165 | this->create_shdrs(&off); |
| 1166 | |
| 1167 | // If there are no sections which require postprocessing, we can |
| 1168 | // handle the section names now, and avoid a resize later. |
| 1169 | if (!this->any_postprocessing_sections_) |
| 1170 | off = this->set_section_offsets(off, |
| 1171 | STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS); |
| 1172 | |
| 1173 | file_header->set_section_info(this->section_headers_, shstrtab_section); |
| 1174 | |
| 1175 | // Now we know exactly where everything goes in the output file |
| 1176 | // (except for non-allocated sections which require postprocessing). |
| 1177 | Output_data::layout_complete(); |
| 1178 | |
| 1179 | this->output_file_size_ = off; |
| 1180 | |
| 1181 | return off; |
| 1182 | } |
| 1183 | |
| 1184 | // Create a .note section for an executable or shared library. This |
| 1185 | // records the version of gold used to create the binary. |
| 1186 | |
| 1187 | void |
| 1188 | Layout::create_gold_note() |
| 1189 | { |
| 1190 | if (parameters->options().relocatable()) |
| 1191 | return; |
| 1192 | |
| 1193 | // Authorities all agree that the values in a .note field should |
| 1194 | // be aligned on 4-byte boundaries for 32-bit binaries. However, |
| 1195 | // they differ on what the alignment is for 64-bit binaries. |
| 1196 | // The GABI says unambiguously they take 8-byte alignment: |
| 1197 | // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section |
| 1198 | // Other documentation says alignment should always be 4 bytes: |
| 1199 | // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format |
| 1200 | // GNU ld and GNU readelf both support the latter (at least as of |
| 1201 | // version 2.16.91), and glibc always generates the latter for |
| 1202 | // .note.ABI-tag (as of version 1.6), so that's the one we go with |
| 1203 | // here. |
| 1204 | #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default. |
| 1205 | const int size = parameters->target().get_size(); |
| 1206 | #else |
| 1207 | const int size = 32; |
| 1208 | #endif |
| 1209 | |
| 1210 | // The contents of the .note section. |
| 1211 | const char* name = "GNU"; |
| 1212 | std::string desc(std::string("gold ") + gold::get_version_string()); |
| 1213 | size_t namesz = strlen(name) + 1; |
| 1214 | size_t aligned_namesz = align_address(namesz, size / 8); |
| 1215 | size_t descsz = desc.length() + 1; |
| 1216 | size_t aligned_descsz = align_address(descsz, size / 8); |
| 1217 | const int note_type = 4; |
| 1218 | |
| 1219 | size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz; |
| 1220 | |
| 1221 | unsigned char buffer[128]; |
| 1222 | gold_assert(sizeof buffer >= notesz); |
| 1223 | memset(buffer, 0, notesz); |
| 1224 | |
| 1225 | bool is_big_endian = parameters->target().is_big_endian(); |
| 1226 | |
| 1227 | if (size == 32) |
| 1228 | { |
| 1229 | if (!is_big_endian) |
| 1230 | { |
| 1231 | elfcpp::Swap<32, false>::writeval(buffer, namesz); |
| 1232 | elfcpp::Swap<32, false>::writeval(buffer + 4, descsz); |
| 1233 | elfcpp::Swap<32, false>::writeval(buffer + 8, note_type); |
| 1234 | } |
| 1235 | else |
| 1236 | { |
| 1237 | elfcpp::Swap<32, true>::writeval(buffer, namesz); |
| 1238 | elfcpp::Swap<32, true>::writeval(buffer + 4, descsz); |
| 1239 | elfcpp::Swap<32, true>::writeval(buffer + 8, note_type); |
| 1240 | } |
| 1241 | } |
| 1242 | else if (size == 64) |
| 1243 | { |
| 1244 | if (!is_big_endian) |
| 1245 | { |
| 1246 | elfcpp::Swap<64, false>::writeval(buffer, namesz); |
| 1247 | elfcpp::Swap<64, false>::writeval(buffer + 8, descsz); |
| 1248 | elfcpp::Swap<64, false>::writeval(buffer + 16, note_type); |
| 1249 | } |
| 1250 | else |
| 1251 | { |
| 1252 | elfcpp::Swap<64, true>::writeval(buffer, namesz); |
| 1253 | elfcpp::Swap<64, true>::writeval(buffer + 8, descsz); |
| 1254 | elfcpp::Swap<64, true>::writeval(buffer + 16, note_type); |
| 1255 | } |
| 1256 | } |
| 1257 | else |
| 1258 | gold_unreachable(); |
| 1259 | |
| 1260 | memcpy(buffer + 3 * (size / 8), name, namesz); |
| 1261 | memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz); |
| 1262 | |
| 1263 | const char* note_name = this->namepool_.add(".note", false, NULL); |
| 1264 | Output_section* os = this->make_output_section(note_name, |
| 1265 | elfcpp::SHT_NOTE, |
| 1266 | 0); |
| 1267 | Output_section_data* posd = new Output_data_const(buffer, notesz, |
| 1268 | size / 8); |
| 1269 | os->add_output_section_data(posd); |
| 1270 | } |
| 1271 | |
| 1272 | // Record whether the stack should be executable. This can be set |
| 1273 | // from the command line using the -z execstack or -z noexecstack |
| 1274 | // options. Otherwise, if any input file has a .note.GNU-stack |
| 1275 | // section with the SHF_EXECINSTR flag set, the stack should be |
| 1276 | // executable. Otherwise, if at least one input file a |
| 1277 | // .note.GNU-stack section, and some input file has no .note.GNU-stack |
| 1278 | // section, we use the target default for whether the stack should be |
| 1279 | // executable. Otherwise, we don't generate a stack note. When |
| 1280 | // generating a object file, we create a .note.GNU-stack section with |
| 1281 | // the appropriate marking. When generating an executable or shared |
| 1282 | // library, we create a PT_GNU_STACK segment. |
| 1283 | |
| 1284 | void |
| 1285 | Layout::create_executable_stack_info(const Target* target) |
| 1286 | { |
| 1287 | bool is_stack_executable; |
| 1288 | if (this->options_.is_execstack_set()) |
| 1289 | is_stack_executable = this->options_.is_stack_executable(); |
| 1290 | else if (!this->input_with_gnu_stack_note_) |
| 1291 | return; |
| 1292 | else |
| 1293 | { |
| 1294 | if (this->input_requires_executable_stack_) |
| 1295 | is_stack_executable = true; |
| 1296 | else if (this->input_without_gnu_stack_note_) |
| 1297 | is_stack_executable = target->is_default_stack_executable(); |
| 1298 | else |
| 1299 | is_stack_executable = false; |
| 1300 | } |
| 1301 | |
| 1302 | if (parameters->options().relocatable()) |
| 1303 | { |
| 1304 | const char* name = this->namepool_.add(".note.GNU-stack", false, NULL); |
| 1305 | elfcpp::Elf_Xword flags = 0; |
| 1306 | if (is_stack_executable) |
| 1307 | flags |= elfcpp::SHF_EXECINSTR; |
| 1308 | this->make_output_section(name, elfcpp::SHT_PROGBITS, flags); |
| 1309 | } |
| 1310 | else |
| 1311 | { |
| 1312 | if (this->script_options_->saw_phdrs_clause()) |
| 1313 | return; |
| 1314 | int flags = elfcpp::PF_R | elfcpp::PF_W; |
| 1315 | if (is_stack_executable) |
| 1316 | flags |= elfcpp::PF_X; |
| 1317 | this->make_output_segment(elfcpp::PT_GNU_STACK, flags); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | // Return whether SEG1 should be before SEG2 in the output file. This |
| 1322 | // is based entirely on the segment type and flags. When this is |
| 1323 | // called the segment addresses has normally not yet been set. |
| 1324 | |
| 1325 | bool |
| 1326 | Layout::segment_precedes(const Output_segment* seg1, |
| 1327 | const Output_segment* seg2) |
| 1328 | { |
| 1329 | elfcpp::Elf_Word type1 = seg1->type(); |
| 1330 | elfcpp::Elf_Word type2 = seg2->type(); |
| 1331 | |
| 1332 | // The single PT_PHDR segment is required to precede any loadable |
| 1333 | // segment. We simply make it always first. |
| 1334 | if (type1 == elfcpp::PT_PHDR) |
| 1335 | { |
| 1336 | gold_assert(type2 != elfcpp::PT_PHDR); |
| 1337 | return true; |
| 1338 | } |
| 1339 | if (type2 == elfcpp::PT_PHDR) |
| 1340 | return false; |
| 1341 | |
| 1342 | // The single PT_INTERP segment is required to precede any loadable |
| 1343 | // segment. We simply make it always second. |
| 1344 | if (type1 == elfcpp::PT_INTERP) |
| 1345 | { |
| 1346 | gold_assert(type2 != elfcpp::PT_INTERP); |
| 1347 | return true; |
| 1348 | } |
| 1349 | if (type2 == elfcpp::PT_INTERP) |
| 1350 | return false; |
| 1351 | |
| 1352 | // We then put PT_LOAD segments before any other segments. |
| 1353 | if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD) |
| 1354 | return true; |
| 1355 | if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD) |
| 1356 | return false; |
| 1357 | |
| 1358 | // We put the PT_TLS segment last, because that is where the dynamic |
| 1359 | // linker expects to find it (this is just for efficiency; other |
| 1360 | // positions would also work correctly). |
| 1361 | if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS) |
| 1362 | return false; |
| 1363 | if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS) |
| 1364 | return true; |
| 1365 | |
| 1366 | const elfcpp::Elf_Word flags1 = seg1->flags(); |
| 1367 | const elfcpp::Elf_Word flags2 = seg2->flags(); |
| 1368 | |
| 1369 | // The order of non-PT_LOAD segments is unimportant. We simply sort |
| 1370 | // by the numeric segment type and flags values. There should not |
| 1371 | // be more than one segment with the same type and flags. |
| 1372 | if (type1 != elfcpp::PT_LOAD) |
| 1373 | { |
| 1374 | if (type1 != type2) |
| 1375 | return type1 < type2; |
| 1376 | gold_assert(flags1 != flags2); |
| 1377 | return flags1 < flags2; |
| 1378 | } |
| 1379 | |
| 1380 | // If the addresses are set already, sort by load address. |
| 1381 | if (seg1->are_addresses_set()) |
| 1382 | { |
| 1383 | if (!seg2->are_addresses_set()) |
| 1384 | return true; |
| 1385 | |
| 1386 | unsigned int section_count1 = seg1->output_section_count(); |
| 1387 | unsigned int section_count2 = seg2->output_section_count(); |
| 1388 | if (section_count1 == 0 && section_count2 > 0) |
| 1389 | return true; |
| 1390 | if (section_count1 > 0 && section_count2 == 0) |
| 1391 | return false; |
| 1392 | |
| 1393 | uint64_t paddr1 = seg1->first_section_load_address(); |
| 1394 | uint64_t paddr2 = seg2->first_section_load_address(); |
| 1395 | if (paddr1 != paddr2) |
| 1396 | return paddr1 < paddr2; |
| 1397 | } |
| 1398 | else if (seg2->are_addresses_set()) |
| 1399 | return false; |
| 1400 | |
| 1401 | // We sort PT_LOAD segments based on the flags. Readonly segments |
| 1402 | // come before writable segments. Then writable segments with data |
| 1403 | // come before writable segments without data. Then executable |
| 1404 | // segments come before non-executable segments. Then the unlikely |
| 1405 | // case of a non-readable segment comes before the normal case of a |
| 1406 | // readable segment. If there are multiple segments with the same |
| 1407 | // type and flags, we require that the address be set, and we sort |
| 1408 | // by virtual address and then physical address. |
| 1409 | if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W)) |
| 1410 | return (flags1 & elfcpp::PF_W) == 0; |
| 1411 | if ((flags1 & elfcpp::PF_W) != 0 |
| 1412 | && seg1->has_any_data_sections() != seg2->has_any_data_sections()) |
| 1413 | return seg1->has_any_data_sections(); |
| 1414 | if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X)) |
| 1415 | return (flags1 & elfcpp::PF_X) != 0; |
| 1416 | if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R)) |
| 1417 | return (flags1 & elfcpp::PF_R) == 0; |
| 1418 | |
| 1419 | // We shouldn't get here--we shouldn't create segments which we |
| 1420 | // can't distinguish. |
| 1421 | gold_unreachable(); |
| 1422 | } |
| 1423 | |
| 1424 | // Set the file offsets of all the segments, and all the sections they |
| 1425 | // contain. They have all been created. LOAD_SEG must be be laid out |
| 1426 | // first. Return the offset of the data to follow. |
| 1427 | |
| 1428 | off_t |
| 1429 | Layout::set_segment_offsets(const Target* target, Output_segment* load_seg, |
| 1430 | unsigned int *pshndx) |
| 1431 | { |
| 1432 | // Sort them into the final order. |
| 1433 | std::sort(this->segment_list_.begin(), this->segment_list_.end(), |
| 1434 | Layout::Compare_segments()); |
| 1435 | |
| 1436 | // Find the PT_LOAD segments, and set their addresses and offsets |
| 1437 | // and their section's addresses and offsets. |
| 1438 | uint64_t addr; |
| 1439 | if (this->options_.user_set_Ttext()) |
| 1440 | addr = this->options_.Ttext(); |
| 1441 | else if (parameters->options().shared()) |
| 1442 | addr = 0; |
| 1443 | else |
| 1444 | addr = target->default_text_segment_address(); |
| 1445 | off_t off = 0; |
| 1446 | |
| 1447 | // If LOAD_SEG is NULL, then the file header and segment headers |
| 1448 | // will not be loadable. But they still need to be at offset 0 in |
| 1449 | // the file. Set their offsets now. |
| 1450 | if (load_seg == NULL) |
| 1451 | { |
| 1452 | for (Data_list::iterator p = this->special_output_list_.begin(); |
| 1453 | p != this->special_output_list_.end(); |
| 1454 | ++p) |
| 1455 | { |
| 1456 | off = align_address(off, (*p)->addralign()); |
| 1457 | (*p)->set_address_and_file_offset(0, off); |
| 1458 | off += (*p)->data_size(); |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | bool was_readonly = false; |
| 1463 | for (Segment_list::iterator p = this->segment_list_.begin(); |
| 1464 | p != this->segment_list_.end(); |
| 1465 | ++p) |
| 1466 | { |
| 1467 | if ((*p)->type() == elfcpp::PT_LOAD) |
| 1468 | { |
| 1469 | if (load_seg != NULL && load_seg != *p) |
| 1470 | gold_unreachable(); |
| 1471 | load_seg = NULL; |
| 1472 | |
| 1473 | bool are_addresses_set = (*p)->are_addresses_set(); |
| 1474 | if (are_addresses_set) |
| 1475 | { |
| 1476 | // When it comes to setting file offsets, we care about |
| 1477 | // the physical address. |
| 1478 | addr = (*p)->paddr(); |
| 1479 | } |
| 1480 | else if (this->options_.user_set_Tdata() |
| 1481 | && ((*p)->flags() & elfcpp::PF_W) != 0 |
| 1482 | && (!this->options_.user_set_Tbss() |
| 1483 | || (*p)->has_any_data_sections())) |
| 1484 | { |
| 1485 | addr = this->options_.Tdata(); |
| 1486 | are_addresses_set = true; |
| 1487 | } |
| 1488 | else if (this->options_.user_set_Tbss() |
| 1489 | && ((*p)->flags() & elfcpp::PF_W) != 0 |
| 1490 | && !(*p)->has_any_data_sections()) |
| 1491 | { |
| 1492 | addr = this->options_.Tbss(); |
| 1493 | are_addresses_set = true; |
| 1494 | } |
| 1495 | |
| 1496 | uint64_t orig_addr = addr; |
| 1497 | uint64_t orig_off = off; |
| 1498 | |
| 1499 | uint64_t aligned_addr = 0; |
| 1500 | uint64_t abi_pagesize = target->abi_pagesize(); |
| 1501 | |
| 1502 | // FIXME: This should depend on the -n and -N options. |
| 1503 | (*p)->set_minimum_p_align(target->common_pagesize()); |
| 1504 | |
| 1505 | if (are_addresses_set) |
| 1506 | { |
| 1507 | // Adjust the file offset to the same address modulo the |
| 1508 | // page size. |
| 1509 | uint64_t unsigned_off = off; |
| 1510 | uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1)) |
| 1511 | | (addr & (abi_pagesize - 1))); |
| 1512 | if (aligned_off < unsigned_off) |
| 1513 | aligned_off += abi_pagesize; |
| 1514 | off = aligned_off; |
| 1515 | } |
| 1516 | else |
| 1517 | { |
| 1518 | // If the last segment was readonly, and this one is |
| 1519 | // not, then skip the address forward one page, |
| 1520 | // maintaining the same position within the page. This |
| 1521 | // lets us store both segments overlapping on a single |
| 1522 | // page in the file, but the loader will put them on |
| 1523 | // different pages in memory. |
| 1524 | |
| 1525 | addr = align_address(addr, (*p)->maximum_alignment()); |
| 1526 | aligned_addr = addr; |
| 1527 | |
| 1528 | if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0) |
| 1529 | { |
| 1530 | if ((addr & (abi_pagesize - 1)) != 0) |
| 1531 | addr = addr + abi_pagesize; |
| 1532 | } |
| 1533 | |
| 1534 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
| 1535 | } |
| 1536 | |
| 1537 | unsigned int shndx_hold = *pshndx; |
| 1538 | uint64_t new_addr = (*p)->set_section_addresses(this, false, addr, |
| 1539 | &off, pshndx); |
| 1540 | |
| 1541 | // Now that we know the size of this segment, we may be able |
| 1542 | // to save a page in memory, at the cost of wasting some |
| 1543 | // file space, by instead aligning to the start of a new |
| 1544 | // page. Here we use the real machine page size rather than |
| 1545 | // the ABI mandated page size. |
| 1546 | |
| 1547 | if (!are_addresses_set && aligned_addr != addr) |
| 1548 | { |
| 1549 | uint64_t common_pagesize = target->common_pagesize(); |
| 1550 | uint64_t first_off = (common_pagesize |
| 1551 | - (aligned_addr |
| 1552 | & (common_pagesize - 1))); |
| 1553 | uint64_t last_off = new_addr & (common_pagesize - 1); |
| 1554 | if (first_off > 0 |
| 1555 | && last_off > 0 |
| 1556 | && ((aligned_addr & ~ (common_pagesize - 1)) |
| 1557 | != (new_addr & ~ (common_pagesize - 1))) |
| 1558 | && first_off + last_off <= common_pagesize) |
| 1559 | { |
| 1560 | *pshndx = shndx_hold; |
| 1561 | addr = align_address(aligned_addr, common_pagesize); |
| 1562 | addr = align_address(addr, (*p)->maximum_alignment()); |
| 1563 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
| 1564 | new_addr = (*p)->set_section_addresses(this, true, addr, |
| 1565 | &off, pshndx); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | addr = new_addr; |
| 1570 | |
| 1571 | if (((*p)->flags() & elfcpp::PF_W) == 0) |
| 1572 | was_readonly = true; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | // Handle the non-PT_LOAD segments, setting their offsets from their |
| 1577 | // section's offsets. |
| 1578 | for (Segment_list::iterator p = this->segment_list_.begin(); |
| 1579 | p != this->segment_list_.end(); |
| 1580 | ++p) |
| 1581 | { |
| 1582 | if ((*p)->type() != elfcpp::PT_LOAD) |
| 1583 | (*p)->set_offset(); |
| 1584 | } |
| 1585 | |
| 1586 | // Set the TLS offsets for each section in the PT_TLS segment. |
| 1587 | if (this->tls_segment_ != NULL) |
| 1588 | this->tls_segment_->set_tls_offsets(); |
| 1589 | |
| 1590 | return off; |
| 1591 | } |
| 1592 | |
| 1593 | // Set the offsets of all the allocated sections when doing a |
| 1594 | // relocatable link. This does the same jobs as set_segment_offsets, |
| 1595 | // only for a relocatable link. |
| 1596 | |
| 1597 | off_t |
| 1598 | Layout::set_relocatable_section_offsets(Output_data* file_header, |
| 1599 | unsigned int *pshndx) |
| 1600 | { |
| 1601 | off_t off = 0; |
| 1602 | |
| 1603 | file_header->set_address_and_file_offset(0, 0); |
| 1604 | off += file_header->data_size(); |
| 1605 | |
| 1606 | for (Section_list::iterator p = this->section_list_.begin(); |
| 1607 | p != this->section_list_.end(); |
| 1608 | ++p) |
| 1609 | { |
| 1610 | // We skip unallocated sections here, except that group sections |
| 1611 | // have to come first. |
| 1612 | if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0 |
| 1613 | && (*p)->type() != elfcpp::SHT_GROUP) |
| 1614 | continue; |
| 1615 | |
| 1616 | off = align_address(off, (*p)->addralign()); |
| 1617 | |
| 1618 | // The linker script might have set the address. |
| 1619 | if (!(*p)->is_address_valid()) |
| 1620 | (*p)->set_address(0); |
| 1621 | (*p)->set_file_offset(off); |
| 1622 | (*p)->finalize_data_size(); |
| 1623 | off += (*p)->data_size(); |
| 1624 | |
| 1625 | (*p)->set_out_shndx(*pshndx); |
| 1626 | ++*pshndx; |
| 1627 | } |
| 1628 | |
| 1629 | return off; |
| 1630 | } |
| 1631 | |
| 1632 | // Set the file offset of all the sections not associated with a |
| 1633 | // segment. |
| 1634 | |
| 1635 | off_t |
| 1636 | Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass) |
| 1637 | { |
| 1638 | for (Section_list::iterator p = this->unattached_section_list_.begin(); |
| 1639 | p != this->unattached_section_list_.end(); |
| 1640 | ++p) |
| 1641 | { |
| 1642 | // The symtab section is handled in create_symtab_sections. |
| 1643 | if (*p == this->symtab_section_) |
| 1644 | continue; |
| 1645 | |
| 1646 | // If we've already set the data size, don't set it again. |
| 1647 | if ((*p)->is_offset_valid() && (*p)->is_data_size_valid()) |
| 1648 | continue; |
| 1649 | |
| 1650 | if (pass == BEFORE_INPUT_SECTIONS_PASS |
| 1651 | && (*p)->requires_postprocessing()) |
| 1652 | { |
| 1653 | (*p)->create_postprocessing_buffer(); |
| 1654 | this->any_postprocessing_sections_ = true; |
| 1655 | } |
| 1656 | |
| 1657 | if (pass == BEFORE_INPUT_SECTIONS_PASS |
| 1658 | && (*p)->after_input_sections()) |
| 1659 | continue; |
| 1660 | else if (pass == POSTPROCESSING_SECTIONS_PASS |
| 1661 | && (!(*p)->after_input_sections() |
| 1662 | || (*p)->type() == elfcpp::SHT_STRTAB)) |
| 1663 | continue; |
| 1664 | else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS |
| 1665 | && (!(*p)->after_input_sections() |
| 1666 | || (*p)->type() != elfcpp::SHT_STRTAB)) |
| 1667 | continue; |
| 1668 | |
| 1669 | off = align_address(off, (*p)->addralign()); |
| 1670 | (*p)->set_file_offset(off); |
| 1671 | (*p)->finalize_data_size(); |
| 1672 | off += (*p)->data_size(); |
| 1673 | |
| 1674 | // At this point the name must be set. |
| 1675 | if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS) |
| 1676 | this->namepool_.add((*p)->name(), false, NULL); |
| 1677 | } |
| 1678 | return off; |
| 1679 | } |
| 1680 | |
| 1681 | // Set the section indexes of all the sections not associated with a |
| 1682 | // segment. |
| 1683 | |
| 1684 | unsigned int |
| 1685 | Layout::set_section_indexes(unsigned int shndx) |
| 1686 | { |
| 1687 | const bool output_is_object = parameters->options().relocatable(); |
| 1688 | for (Section_list::iterator p = this->unattached_section_list_.begin(); |
| 1689 | p != this->unattached_section_list_.end(); |
| 1690 | ++p) |
| 1691 | { |
| 1692 | // In a relocatable link, we already did group sections. |
| 1693 | if (output_is_object |
| 1694 | && (*p)->type() == elfcpp::SHT_GROUP) |
| 1695 | continue; |
| 1696 | |
| 1697 | (*p)->set_out_shndx(shndx); |
| 1698 | ++shndx; |
| 1699 | } |
| 1700 | return shndx; |
| 1701 | } |
| 1702 | |
| 1703 | // Set the section addresses according to the linker script. This is |
| 1704 | // only called when we see a SECTIONS clause. This returns the |
| 1705 | // program segment which should hold the file header and segment |
| 1706 | // headers, if any. It will return NULL if they should not be in a |
| 1707 | // segment. |
| 1708 | |
| 1709 | Output_segment* |
| 1710 | Layout::set_section_addresses_from_script(Symbol_table* symtab) |
| 1711 | { |
| 1712 | Script_sections* ss = this->script_options_->script_sections(); |
| 1713 | gold_assert(ss->saw_sections_clause()); |
| 1714 | |
| 1715 | // Place each orphaned output section in the script. |
| 1716 | for (Section_list::iterator p = this->section_list_.begin(); |
| 1717 | p != this->section_list_.end(); |
| 1718 | ++p) |
| 1719 | { |
| 1720 | if (!(*p)->found_in_sections_clause()) |
| 1721 | ss->place_orphan(*p); |
| 1722 | } |
| 1723 | |
| 1724 | return this->script_options_->set_section_addresses(symtab, this); |
| 1725 | } |
| 1726 | |
| 1727 | // Count the local symbols in the regular symbol table and the dynamic |
| 1728 | // symbol table, and build the respective string pools. |
| 1729 | |
| 1730 | void |
| 1731 | Layout::count_local_symbols(const Task* task, |
| 1732 | const Input_objects* input_objects) |
| 1733 | { |
| 1734 | // First, figure out an upper bound on the number of symbols we'll |
| 1735 | // be inserting into each pool. This helps us create the pools with |
| 1736 | // the right size, to avoid unnecessary hashtable resizing. |
| 1737 | unsigned int symbol_count = 0; |
| 1738 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
| 1739 | p != input_objects->relobj_end(); |
| 1740 | ++p) |
| 1741 | symbol_count += (*p)->local_symbol_count(); |
| 1742 | |
| 1743 | // Go from "upper bound" to "estimate." We overcount for two |
| 1744 | // reasons: we double-count symbols that occur in more than one |
| 1745 | // object file, and we count symbols that are dropped from the |
| 1746 | // output. Add it all together and assume we overcount by 100%. |
| 1747 | symbol_count /= 2; |
| 1748 | |
| 1749 | // We assume all symbols will go into both the sympool and dynpool. |
| 1750 | this->sympool_.reserve(symbol_count); |
| 1751 | this->dynpool_.reserve(symbol_count); |
| 1752 | |
| 1753 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
| 1754 | p != input_objects->relobj_end(); |
| 1755 | ++p) |
| 1756 | { |
| 1757 | Task_lock_obj<Object> tlo(task, *p); |
| 1758 | (*p)->count_local_symbols(&this->sympool_, &this->dynpool_); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | // Create the symbol table sections. Here we also set the final |
| 1763 | // values of the symbols. At this point all the loadable sections are |
| 1764 | // fully laid out. |
| 1765 | |
| 1766 | void |
| 1767 | Layout::create_symtab_sections(const Input_objects* input_objects, |
| 1768 | Symbol_table* symtab, |
| 1769 | off_t* poff) |
| 1770 | { |
| 1771 | int symsize; |
| 1772 | unsigned int align; |
| 1773 | if (parameters->target().get_size() == 32) |
| 1774 | { |
| 1775 | symsize = elfcpp::Elf_sizes<32>::sym_size; |
| 1776 | align = 4; |
| 1777 | } |
| 1778 | else if (parameters->target().get_size() == 64) |
| 1779 | { |
| 1780 | symsize = elfcpp::Elf_sizes<64>::sym_size; |
| 1781 | align = 8; |
| 1782 | } |
| 1783 | else |
| 1784 | gold_unreachable(); |
| 1785 | |
| 1786 | off_t off = *poff; |
| 1787 | off = align_address(off, align); |
| 1788 | off_t startoff = off; |
| 1789 | |
| 1790 | // Save space for the dummy symbol at the start of the section. We |
| 1791 | // never bother to write this out--it will just be left as zero. |
| 1792 | off += symsize; |
| 1793 | unsigned int local_symbol_index = 1; |
| 1794 | |
| 1795 | // Add STT_SECTION symbols for each Output section which needs one. |
| 1796 | for (Section_list::iterator p = this->section_list_.begin(); |
| 1797 | p != this->section_list_.end(); |
| 1798 | ++p) |
| 1799 | { |
| 1800 | if (!(*p)->needs_symtab_index()) |
| 1801 | (*p)->set_symtab_index(-1U); |
| 1802 | else |
| 1803 | { |
| 1804 | (*p)->set_symtab_index(local_symbol_index); |
| 1805 | ++local_symbol_index; |
| 1806 | off += symsize; |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
| 1811 | p != input_objects->relobj_end(); |
| 1812 | ++p) |
| 1813 | { |
| 1814 | unsigned int index = (*p)->finalize_local_symbols(local_symbol_index, |
| 1815 | off); |
| 1816 | off += (index - local_symbol_index) * symsize; |
| 1817 | local_symbol_index = index; |
| 1818 | } |
| 1819 | |
| 1820 | unsigned int local_symcount = local_symbol_index; |
| 1821 | gold_assert(local_symcount * symsize == off - startoff); |
| 1822 | |
| 1823 | off_t dynoff; |
| 1824 | size_t dyn_global_index; |
| 1825 | size_t dyncount; |
| 1826 | if (this->dynsym_section_ == NULL) |
| 1827 | { |
| 1828 | dynoff = 0; |
| 1829 | dyn_global_index = 0; |
| 1830 | dyncount = 0; |
| 1831 | } |
| 1832 | else |
| 1833 | { |
| 1834 | dyn_global_index = this->dynsym_section_->info(); |
| 1835 | off_t locsize = dyn_global_index * this->dynsym_section_->entsize(); |
| 1836 | dynoff = this->dynsym_section_->offset() + locsize; |
| 1837 | dyncount = (this->dynsym_section_->data_size() - locsize) / symsize; |
| 1838 | gold_assert(static_cast<off_t>(dyncount * symsize) |
| 1839 | == this->dynsym_section_->data_size() - locsize); |
| 1840 | } |
| 1841 | |
| 1842 | off = symtab->finalize(off, dynoff, dyn_global_index, dyncount, |
| 1843 | &this->sympool_, &local_symcount); |
| 1844 | |
| 1845 | if (!parameters->options().strip_all()) |
| 1846 | { |
| 1847 | this->sympool_.set_string_offsets(); |
| 1848 | |
| 1849 | const char* symtab_name = this->namepool_.add(".symtab", false, NULL); |
| 1850 | Output_section* osymtab = this->make_output_section(symtab_name, |
| 1851 | elfcpp::SHT_SYMTAB, |
| 1852 | 0); |
| 1853 | this->symtab_section_ = osymtab; |
| 1854 | |
| 1855 | Output_section_data* pos = new Output_data_fixed_space(off - startoff, |
| 1856 | align); |
| 1857 | osymtab->add_output_section_data(pos); |
| 1858 | |
| 1859 | const char* strtab_name = this->namepool_.add(".strtab", false, NULL); |
| 1860 | Output_section* ostrtab = this->make_output_section(strtab_name, |
| 1861 | elfcpp::SHT_STRTAB, |
| 1862 | 0); |
| 1863 | |
| 1864 | Output_section_data* pstr = new Output_data_strtab(&this->sympool_); |
| 1865 | ostrtab->add_output_section_data(pstr); |
| 1866 | |
| 1867 | osymtab->set_file_offset(startoff); |
| 1868 | osymtab->finalize_data_size(); |
| 1869 | osymtab->set_link_section(ostrtab); |
| 1870 | osymtab->set_info(local_symcount); |
| 1871 | osymtab->set_entsize(symsize); |
| 1872 | |
| 1873 | *poff = off; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | // Create the .shstrtab section, which holds the names of the |
| 1878 | // sections. At the time this is called, we have created all the |
| 1879 | // output sections except .shstrtab itself. |
| 1880 | |
| 1881 | Output_section* |
| 1882 | Layout::create_shstrtab() |
| 1883 | { |
| 1884 | // FIXME: We don't need to create a .shstrtab section if we are |
| 1885 | // stripping everything. |
| 1886 | |
| 1887 | const char* name = this->namepool_.add(".shstrtab", false, NULL); |
| 1888 | |
| 1889 | Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0); |
| 1890 | |
| 1891 | // We can't write out this section until we've set all the section |
| 1892 | // names, and we don't set the names of compressed output sections |
| 1893 | // until relocations are complete. |
| 1894 | os->set_after_input_sections(); |
| 1895 | |
| 1896 | Output_section_data* posd = new Output_data_strtab(&this->namepool_); |
| 1897 | os->add_output_section_data(posd); |
| 1898 | |
| 1899 | return os; |
| 1900 | } |
| 1901 | |
| 1902 | // Create the section headers. SIZE is 32 or 64. OFF is the file |
| 1903 | // offset. |
| 1904 | |
| 1905 | void |
| 1906 | Layout::create_shdrs(off_t* poff) |
| 1907 | { |
| 1908 | Output_section_headers* oshdrs; |
| 1909 | oshdrs = new Output_section_headers(this, |
| 1910 | &this->segment_list_, |
| 1911 | &this->section_list_, |
| 1912 | &this->unattached_section_list_, |
| 1913 | &this->namepool_); |
| 1914 | off_t off = align_address(*poff, oshdrs->addralign()); |
| 1915 | oshdrs->set_address_and_file_offset(0, off); |
| 1916 | off += oshdrs->data_size(); |
| 1917 | *poff = off; |
| 1918 | this->section_headers_ = oshdrs; |
| 1919 | } |
| 1920 | |
| 1921 | // Create the dynamic symbol table. |
| 1922 | |
| 1923 | void |
| 1924 | Layout::create_dynamic_symtab(const Input_objects* input_objects, |
| 1925 | Symbol_table* symtab, |
| 1926 | Output_section **pdynstr, |
| 1927 | unsigned int* plocal_dynamic_count, |
| 1928 | std::vector<Symbol*>* pdynamic_symbols, |
| 1929 | Versions* pversions) |
| 1930 | { |
| 1931 | // Count all the symbols in the dynamic symbol table, and set the |
| 1932 | // dynamic symbol indexes. |
| 1933 | |
| 1934 | // Skip symbol 0, which is always all zeroes. |
| 1935 | unsigned int index = 1; |
| 1936 | |
| 1937 | // Add STT_SECTION symbols for each Output section which needs one. |
| 1938 | for (Section_list::iterator p = this->section_list_.begin(); |
| 1939 | p != this->section_list_.end(); |
| 1940 | ++p) |
| 1941 | { |
| 1942 | if (!(*p)->needs_dynsym_index()) |
| 1943 | (*p)->set_dynsym_index(-1U); |
| 1944 | else |
| 1945 | { |
| 1946 | (*p)->set_dynsym_index(index); |
| 1947 | ++index; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | // Count the local symbols that need to go in the dynamic symbol table, |
| 1952 | // and set the dynamic symbol indexes. |
| 1953 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
| 1954 | p != input_objects->relobj_end(); |
| 1955 | ++p) |
| 1956 | { |
| 1957 | unsigned int new_index = (*p)->set_local_dynsym_indexes(index); |
| 1958 | index = new_index; |
| 1959 | } |
| 1960 | |
| 1961 | unsigned int local_symcount = index; |
| 1962 | *plocal_dynamic_count = local_symcount; |
| 1963 | |
| 1964 | // FIXME: We have to tell set_dynsym_indexes whether the |
| 1965 | // -E/--export-dynamic option was used. |
| 1966 | index = symtab->set_dynsym_indexes(index, pdynamic_symbols, |
| 1967 | &this->dynpool_, pversions); |
| 1968 | |
| 1969 | int symsize; |
| 1970 | unsigned int align; |
| 1971 | const int size = parameters->target().get_size(); |
| 1972 | if (size == 32) |
| 1973 | { |
| 1974 | symsize = elfcpp::Elf_sizes<32>::sym_size; |
| 1975 | align = 4; |
| 1976 | } |
| 1977 | else if (size == 64) |
| 1978 | { |
| 1979 | symsize = elfcpp::Elf_sizes<64>::sym_size; |
| 1980 | align = 8; |
| 1981 | } |
| 1982 | else |
| 1983 | gold_unreachable(); |
| 1984 | |
| 1985 | // Create the dynamic symbol table section. |
| 1986 | |
| 1987 | Output_section* dynsym = this->choose_output_section(NULL, ".dynsym", |
| 1988 | elfcpp::SHT_DYNSYM, |
| 1989 | elfcpp::SHF_ALLOC, |
| 1990 | false); |
| 1991 | |
| 1992 | Output_section_data* odata = new Output_data_fixed_space(index * symsize, |
| 1993 | align); |
| 1994 | dynsym->add_output_section_data(odata); |
| 1995 | |
| 1996 | dynsym->set_info(local_symcount); |
| 1997 | dynsym->set_entsize(symsize); |
| 1998 | dynsym->set_addralign(align); |
| 1999 | |
| 2000 | this->dynsym_section_ = dynsym; |
| 2001 | |
| 2002 | Output_data_dynamic* const odyn = this->dynamic_data_; |
| 2003 | odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym); |
| 2004 | odyn->add_constant(elfcpp::DT_SYMENT, symsize); |
| 2005 | |
| 2006 | // Create the dynamic string table section. |
| 2007 | |
| 2008 | Output_section* dynstr = this->choose_output_section(NULL, ".dynstr", |
| 2009 | elfcpp::SHT_STRTAB, |
| 2010 | elfcpp::SHF_ALLOC, |
| 2011 | false); |
| 2012 | |
| 2013 | Output_section_data* strdata = new Output_data_strtab(&this->dynpool_); |
| 2014 | dynstr->add_output_section_data(strdata); |
| 2015 | |
| 2016 | dynsym->set_link_section(dynstr); |
| 2017 | this->dynamic_section_->set_link_section(dynstr); |
| 2018 | |
| 2019 | odyn->add_section_address(elfcpp::DT_STRTAB, dynstr); |
| 2020 | odyn->add_section_size(elfcpp::DT_STRSZ, dynstr); |
| 2021 | |
| 2022 | *pdynstr = dynstr; |
| 2023 | |
| 2024 | // Create the hash tables. |
| 2025 | |
| 2026 | if (strcmp(parameters->options().hash_style(), "sysv") == 0 |
| 2027 | || strcmp(parameters->options().hash_style(), "both") == 0) |
| 2028 | { |
| 2029 | unsigned char* phash; |
| 2030 | unsigned int hashlen; |
| 2031 | Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount, |
| 2032 | &phash, &hashlen); |
| 2033 | |
| 2034 | Output_section* hashsec = this->choose_output_section(NULL, ".hash", |
| 2035 | elfcpp::SHT_HASH, |
| 2036 | elfcpp::SHF_ALLOC, |
| 2037 | false); |
| 2038 | |
| 2039 | Output_section_data* hashdata = new Output_data_const_buffer(phash, |
| 2040 | hashlen, |
| 2041 | align); |
| 2042 | hashsec->add_output_section_data(hashdata); |
| 2043 | |
| 2044 | hashsec->set_link_section(dynsym); |
| 2045 | hashsec->set_entsize(4); |
| 2046 | |
| 2047 | odyn->add_section_address(elfcpp::DT_HASH, hashsec); |
| 2048 | } |
| 2049 | |
| 2050 | if (strcmp(parameters->options().hash_style(), "gnu") == 0 |
| 2051 | || strcmp(parameters->options().hash_style(), "both") == 0) |
| 2052 | { |
| 2053 | unsigned char* phash; |
| 2054 | unsigned int hashlen; |
| 2055 | Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount, |
| 2056 | &phash, &hashlen); |
| 2057 | |
| 2058 | Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash", |
| 2059 | elfcpp::SHT_GNU_HASH, |
| 2060 | elfcpp::SHF_ALLOC, |
| 2061 | false); |
| 2062 | |
| 2063 | Output_section_data* hashdata = new Output_data_const_buffer(phash, |
| 2064 | hashlen, |
| 2065 | align); |
| 2066 | hashsec->add_output_section_data(hashdata); |
| 2067 | |
| 2068 | hashsec->set_link_section(dynsym); |
| 2069 | hashsec->set_entsize(4); |
| 2070 | |
| 2071 | odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec); |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | // Assign offsets to each local portion of the dynamic symbol table. |
| 2076 | |
| 2077 | void |
| 2078 | Layout::assign_local_dynsym_offsets(const Input_objects* input_objects) |
| 2079 | { |
| 2080 | Output_section* dynsym = this->dynsym_section_; |
| 2081 | gold_assert(dynsym != NULL); |
| 2082 | |
| 2083 | off_t off = dynsym->offset(); |
| 2084 | |
| 2085 | // Skip the dummy symbol at the start of the section. |
| 2086 | off += dynsym->entsize(); |
| 2087 | |
| 2088 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
| 2089 | p != input_objects->relobj_end(); |
| 2090 | ++p) |
| 2091 | { |
| 2092 | unsigned int count = (*p)->set_local_dynsym_offset(off); |
| 2093 | off += count * dynsym->entsize(); |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | // Create the version sections. |
| 2098 | |
| 2099 | void |
| 2100 | Layout::create_version_sections(const Versions* versions, |
| 2101 | const Symbol_table* symtab, |
| 2102 | unsigned int local_symcount, |
| 2103 | const std::vector<Symbol*>& dynamic_symbols, |
| 2104 | const Output_section* dynstr) |
| 2105 | { |
| 2106 | if (!versions->any_defs() && !versions->any_needs()) |
| 2107 | return; |
| 2108 | |
| 2109 | switch (parameters->size_and_endianness()) |
| 2110 | { |
| 2111 | #ifdef HAVE_TARGET_32_LITTLE |
| 2112 | case Parameters::TARGET_32_LITTLE: |
| 2113 | this->sized_create_version_sections<32, false>(versions, symtab, |
| 2114 | local_symcount, |
| 2115 | dynamic_symbols, dynstr); |
| 2116 | break; |
| 2117 | #endif |
| 2118 | #ifdef HAVE_TARGET_32_BIG |
| 2119 | case Parameters::TARGET_32_BIG: |
| 2120 | this->sized_create_version_sections<32, true>(versions, symtab, |
| 2121 | local_symcount, |
| 2122 | dynamic_symbols, dynstr); |
| 2123 | break; |
| 2124 | #endif |
| 2125 | #ifdef HAVE_TARGET_64_LITTLE |
| 2126 | case Parameters::TARGET_64_LITTLE: |
| 2127 | this->sized_create_version_sections<64, false>(versions, symtab, |
| 2128 | local_symcount, |
| 2129 | dynamic_symbols, dynstr); |
| 2130 | break; |
| 2131 | #endif |
| 2132 | #ifdef HAVE_TARGET_64_BIG |
| 2133 | case Parameters::TARGET_64_BIG: |
| 2134 | this->sized_create_version_sections<64, true>(versions, symtab, |
| 2135 | local_symcount, |
| 2136 | dynamic_symbols, dynstr); |
| 2137 | break; |
| 2138 | #endif |
| 2139 | default: |
| 2140 | gold_unreachable(); |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | // Create the version sections, sized version. |
| 2145 | |
| 2146 | template<int size, bool big_endian> |
| 2147 | void |
| 2148 | Layout::sized_create_version_sections( |
| 2149 | const Versions* versions, |
| 2150 | const Symbol_table* symtab, |
| 2151 | unsigned int local_symcount, |
| 2152 | const std::vector<Symbol*>& dynamic_symbols, |
| 2153 | const Output_section* dynstr) |
| 2154 | { |
| 2155 | Output_section* vsec = this->choose_output_section(NULL, ".gnu.version", |
| 2156 | elfcpp::SHT_GNU_versym, |
| 2157 | elfcpp::SHF_ALLOC, |
| 2158 | false); |
| 2159 | |
| 2160 | unsigned char* vbuf; |
| 2161 | unsigned int vsize; |
| 2162 | versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_, |
| 2163 | local_symcount, |
| 2164 | dynamic_symbols, |
| 2165 | &vbuf, &vsize); |
| 2166 | |
| 2167 | Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2); |
| 2168 | |
| 2169 | vsec->add_output_section_data(vdata); |
| 2170 | vsec->set_entsize(2); |
| 2171 | vsec->set_link_section(this->dynsym_section_); |
| 2172 | |
| 2173 | Output_data_dynamic* const odyn = this->dynamic_data_; |
| 2174 | odyn->add_section_address(elfcpp::DT_VERSYM, vsec); |
| 2175 | |
| 2176 | if (versions->any_defs()) |
| 2177 | { |
| 2178 | Output_section* vdsec; |
| 2179 | vdsec= this->choose_output_section(NULL, ".gnu.version_d", |
| 2180 | elfcpp::SHT_GNU_verdef, |
| 2181 | elfcpp::SHF_ALLOC, |
| 2182 | false); |
| 2183 | |
| 2184 | unsigned char* vdbuf; |
| 2185 | unsigned int vdsize; |
| 2186 | unsigned int vdentries; |
| 2187 | versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf, |
| 2188 | &vdsize, &vdentries); |
| 2189 | |
| 2190 | Output_section_data* vddata = new Output_data_const_buffer(vdbuf, |
| 2191 | vdsize, |
| 2192 | 4); |
| 2193 | |
| 2194 | vdsec->add_output_section_data(vddata); |
| 2195 | vdsec->set_link_section(dynstr); |
| 2196 | vdsec->set_info(vdentries); |
| 2197 | |
| 2198 | odyn->add_section_address(elfcpp::DT_VERDEF, vdsec); |
| 2199 | odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries); |
| 2200 | } |
| 2201 | |
| 2202 | if (versions->any_needs()) |
| 2203 | { |
| 2204 | Output_section* vnsec; |
| 2205 | vnsec = this->choose_output_section(NULL, ".gnu.version_r", |
| 2206 | elfcpp::SHT_GNU_verneed, |
| 2207 | elfcpp::SHF_ALLOC, |
| 2208 | false); |
| 2209 | |
| 2210 | unsigned char* vnbuf; |
| 2211 | unsigned int vnsize; |
| 2212 | unsigned int vnentries; |
| 2213 | versions->need_section_contents<size, big_endian>(&this->dynpool_, |
| 2214 | &vnbuf, &vnsize, |
| 2215 | &vnentries); |
| 2216 | |
| 2217 | Output_section_data* vndata = new Output_data_const_buffer(vnbuf, |
| 2218 | vnsize, |
| 2219 | 4); |
| 2220 | |
| 2221 | vnsec->add_output_section_data(vndata); |
| 2222 | vnsec->set_link_section(dynstr); |
| 2223 | vnsec->set_info(vnentries); |
| 2224 | |
| 2225 | odyn->add_section_address(elfcpp::DT_VERNEED, vnsec); |
| 2226 | odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries); |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | // Create the .interp section and PT_INTERP segment. |
| 2231 | |
| 2232 | void |
| 2233 | Layout::create_interp(const Target* target) |
| 2234 | { |
| 2235 | const char* interp = this->options_.dynamic_linker(); |
| 2236 | if (interp == NULL) |
| 2237 | { |
| 2238 | interp = target->dynamic_linker(); |
| 2239 | gold_assert(interp != NULL); |
| 2240 | } |
| 2241 | |
| 2242 | size_t len = strlen(interp) + 1; |
| 2243 | |
| 2244 | Output_section_data* odata = new Output_data_const(interp, len, 1); |
| 2245 | |
| 2246 | Output_section* osec = this->choose_output_section(NULL, ".interp", |
| 2247 | elfcpp::SHT_PROGBITS, |
| 2248 | elfcpp::SHF_ALLOC, |
| 2249 | false); |
| 2250 | osec->add_output_section_data(odata); |
| 2251 | |
| 2252 | if (!this->script_options_->saw_phdrs_clause()) |
| 2253 | { |
| 2254 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP, |
| 2255 | elfcpp::PF_R); |
| 2256 | oseg->add_initial_output_section(osec, elfcpp::PF_R); |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | // Finish the .dynamic section and PT_DYNAMIC segment. |
| 2261 | |
| 2262 | void |
| 2263 | Layout::finish_dynamic_section(const Input_objects* input_objects, |
| 2264 | const Symbol_table* symtab) |
| 2265 | { |
| 2266 | if (!this->script_options_->saw_phdrs_clause()) |
| 2267 | { |
| 2268 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC, |
| 2269 | (elfcpp::PF_R |
| 2270 | | elfcpp::PF_W)); |
| 2271 | oseg->add_initial_output_section(this->dynamic_section_, |
| 2272 | elfcpp::PF_R | elfcpp::PF_W); |
| 2273 | } |
| 2274 | |
| 2275 | Output_data_dynamic* const odyn = this->dynamic_data_; |
| 2276 | |
| 2277 | for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); |
| 2278 | p != input_objects->dynobj_end(); |
| 2279 | ++p) |
| 2280 | { |
| 2281 | // FIXME: Handle --as-needed. |
| 2282 | odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname()); |
| 2283 | } |
| 2284 | |
| 2285 | if (parameters->options().shared()) |
| 2286 | { |
| 2287 | const char* soname = this->options_.soname(); |
| 2288 | if (soname != NULL) |
| 2289 | odyn->add_string(elfcpp::DT_SONAME, soname); |
| 2290 | } |
| 2291 | |
| 2292 | // FIXME: Support --init and --fini. |
| 2293 | Symbol* sym = symtab->lookup("_init"); |
| 2294 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
| 2295 | odyn->add_symbol(elfcpp::DT_INIT, sym); |
| 2296 | |
| 2297 | sym = symtab->lookup("_fini"); |
| 2298 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
| 2299 | odyn->add_symbol(elfcpp::DT_FINI, sym); |
| 2300 | |
| 2301 | // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY. |
| 2302 | |
| 2303 | // Add a DT_RPATH entry if needed. |
| 2304 | const General_options::Dir_list& rpath(this->options_.rpath()); |
| 2305 | if (!rpath.empty()) |
| 2306 | { |
| 2307 | std::string rpath_val; |
| 2308 | for (General_options::Dir_list::const_iterator p = rpath.begin(); |
| 2309 | p != rpath.end(); |
| 2310 | ++p) |
| 2311 | { |
| 2312 | if (rpath_val.empty()) |
| 2313 | rpath_val = p->name(); |
| 2314 | else |
| 2315 | { |
| 2316 | // Eliminate duplicates. |
| 2317 | General_options::Dir_list::const_iterator q; |
| 2318 | for (q = rpath.begin(); q != p; ++q) |
| 2319 | if (q->name() == p->name()) |
| 2320 | break; |
| 2321 | if (q == p) |
| 2322 | { |
| 2323 | rpath_val += ':'; |
| 2324 | rpath_val += p->name(); |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | odyn->add_string(elfcpp::DT_RPATH, rpath_val); |
| 2330 | } |
| 2331 | |
| 2332 | // Look for text segments that have dynamic relocations. |
| 2333 | bool have_textrel = false; |
| 2334 | if (!this->script_options_->saw_sections_clause()) |
| 2335 | { |
| 2336 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
| 2337 | p != this->segment_list_.end(); |
| 2338 | ++p) |
| 2339 | { |
| 2340 | if (((*p)->flags() & elfcpp::PF_W) == 0 |
| 2341 | && (*p)->dynamic_reloc_count() > 0) |
| 2342 | { |
| 2343 | have_textrel = true; |
| 2344 | break; |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | else |
| 2349 | { |
| 2350 | // We don't know the section -> segment mapping, so we are |
| 2351 | // conservative and just look for readonly sections with |
| 2352 | // relocations. If those sections wind up in writable segments, |
| 2353 | // then we have created an unnecessary DT_TEXTREL entry. |
| 2354 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2355 | p != this->section_list_.end(); |
| 2356 | ++p) |
| 2357 | { |
| 2358 | if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0 |
| 2359 | && ((*p)->flags() & elfcpp::SHF_WRITE) == 0 |
| 2360 | && ((*p)->dynamic_reloc_count() > 0)) |
| 2361 | { |
| 2362 | have_textrel = true; |
| 2363 | break; |
| 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | // Add a DT_FLAGS entry. We add it even if no flags are set so that |
| 2369 | // post-link tools can easily modify these flags if desired. |
| 2370 | unsigned int flags = 0; |
| 2371 | if (have_textrel) |
| 2372 | { |
| 2373 | // Add a DT_TEXTREL for compatibility with older loaders. |
| 2374 | odyn->add_constant(elfcpp::DT_TEXTREL, 0); |
| 2375 | flags |= elfcpp::DF_TEXTREL; |
| 2376 | } |
| 2377 | if (parameters->options().shared() && this->has_static_tls()) |
| 2378 | flags |= elfcpp::DF_STATIC_TLS; |
| 2379 | odyn->add_constant(elfcpp::DT_FLAGS, flags); |
| 2380 | } |
| 2381 | |
| 2382 | // The mapping of .gnu.linkonce section names to real section names. |
| 2383 | |
| 2384 | #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 } |
| 2385 | const Layout::Linkonce_mapping Layout::linkonce_mapping[] = |
| 2386 | { |
| 2387 | MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d". |
| 2388 | MAPPING_INIT("t", ".text"), |
| 2389 | MAPPING_INIT("r", ".rodata"), |
| 2390 | MAPPING_INIT("d", ".data"), |
| 2391 | MAPPING_INIT("b", ".bss"), |
| 2392 | MAPPING_INIT("s", ".sdata"), |
| 2393 | MAPPING_INIT("sb", ".sbss"), |
| 2394 | MAPPING_INIT("s2", ".sdata2"), |
| 2395 | MAPPING_INIT("sb2", ".sbss2"), |
| 2396 | MAPPING_INIT("wi", ".debug_info"), |
| 2397 | MAPPING_INIT("td", ".tdata"), |
| 2398 | MAPPING_INIT("tb", ".tbss"), |
| 2399 | MAPPING_INIT("lr", ".lrodata"), |
| 2400 | MAPPING_INIT("l", ".ldata"), |
| 2401 | MAPPING_INIT("lb", ".lbss"), |
| 2402 | }; |
| 2403 | #undef MAPPING_INIT |
| 2404 | |
| 2405 | const int Layout::linkonce_mapping_count = |
| 2406 | sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]); |
| 2407 | |
| 2408 | // Return the name of the output section to use for a .gnu.linkonce |
| 2409 | // section. This is based on the default ELF linker script of the old |
| 2410 | // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo" |
| 2411 | // to ".text". Set *PLEN to the length of the name. *PLEN is |
| 2412 | // initialized to the length of NAME. |
| 2413 | |
| 2414 | const char* |
| 2415 | Layout::linkonce_output_name(const char* name, size_t *plen) |
| 2416 | { |
| 2417 | const char* s = name + sizeof(".gnu.linkonce") - 1; |
| 2418 | if (*s != '.') |
| 2419 | return name; |
| 2420 | ++s; |
| 2421 | const Linkonce_mapping* plm = linkonce_mapping; |
| 2422 | for (int i = 0; i < linkonce_mapping_count; ++i, ++plm) |
| 2423 | { |
| 2424 | if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.') |
| 2425 | { |
| 2426 | *plen = plm->tolen; |
| 2427 | return plm->to; |
| 2428 | } |
| 2429 | } |
| 2430 | return name; |
| 2431 | } |
| 2432 | |
| 2433 | // Choose the output section name to use given an input section name. |
| 2434 | // Set *PLEN to the length of the name. *PLEN is initialized to the |
| 2435 | // length of NAME. |
| 2436 | |
| 2437 | const char* |
| 2438 | Layout::output_section_name(const char* name, size_t* plen) |
| 2439 | { |
| 2440 | if (Layout::is_linkonce(name)) |
| 2441 | { |
| 2442 | // .gnu.linkonce sections are laid out as though they were named |
| 2443 | // for the sections are placed into. |
| 2444 | return Layout::linkonce_output_name(name, plen); |
| 2445 | } |
| 2446 | |
| 2447 | // gcc 4.3 generates the following sorts of section names when it |
| 2448 | // needs a section name specific to a function: |
| 2449 | // .text.FN |
| 2450 | // .rodata.FN |
| 2451 | // .sdata2.FN |
| 2452 | // .data.FN |
| 2453 | // .data.rel.FN |
| 2454 | // .data.rel.local.FN |
| 2455 | // .data.rel.ro.FN |
| 2456 | // .data.rel.ro.local.FN |
| 2457 | // .sdata.FN |
| 2458 | // .bss.FN |
| 2459 | // .sbss.FN |
| 2460 | // .tdata.FN |
| 2461 | // .tbss.FN |
| 2462 | |
| 2463 | // The GNU linker maps all of those to the part before the .FN, |
| 2464 | // except that .data.rel.local.FN is mapped to .data, and |
| 2465 | // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections |
| 2466 | // beginning with .data.rel.ro.local are grouped together. |
| 2467 | |
| 2468 | // For an anonymous namespace, the string FN can contain a '.'. |
| 2469 | |
| 2470 | // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the |
| 2471 | // GNU linker maps to .rodata. |
| 2472 | |
| 2473 | // The .data.rel.ro sections enable a security feature triggered by |
| 2474 | // the -z relro option. Section which need to be relocated at |
| 2475 | // program startup time but which may be readonly after startup are |
| 2476 | // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO |
| 2477 | // segment. The dynamic linker will make that segment writable, |
| 2478 | // perform relocations, and then make it read-only. FIXME: We do |
| 2479 | // not yet implement this optimization. |
| 2480 | |
| 2481 | // It is hard to handle this in a principled way. |
| 2482 | |
| 2483 | // These are the rules we follow: |
| 2484 | |
| 2485 | // If the section name has no initial '.', or no dot other than an |
| 2486 | // initial '.', we use the name unchanged (i.e., "mysection" and |
| 2487 | // ".text" are unchanged). |
| 2488 | |
| 2489 | // If the name starts with ".data.rel.ro" we use ".data.rel.ro". |
| 2490 | |
| 2491 | // Otherwise, we drop the second '.' and everything that comes after |
| 2492 | // it (i.e., ".text.XXX" becomes ".text"). |
| 2493 | |
| 2494 | const char* s = name; |
| 2495 | if (*s != '.') |
| 2496 | return name; |
| 2497 | ++s; |
| 2498 | const char* sdot = strchr(s, '.'); |
| 2499 | if (sdot == NULL) |
| 2500 | return name; |
| 2501 | |
| 2502 | const char* const data_rel_ro = ".data.rel.ro"; |
| 2503 | if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0) |
| 2504 | { |
| 2505 | *plen = strlen(data_rel_ro); |
| 2506 | return data_rel_ro; |
| 2507 | } |
| 2508 | |
| 2509 | *plen = sdot - name; |
| 2510 | return name; |
| 2511 | } |
| 2512 | |
| 2513 | // Record the signature of a comdat section, and return whether to |
| 2514 | // include it in the link. If GROUP is true, this is a regular |
| 2515 | // section group. If GROUP is false, this is a group signature |
| 2516 | // derived from the name of a linkonce section. We want linkonce |
| 2517 | // signatures and group signatures to block each other, but we don't |
| 2518 | // want a linkonce signature to block another linkonce signature. |
| 2519 | |
| 2520 | bool |
| 2521 | Layout::add_comdat(const char* signature, bool group) |
| 2522 | { |
| 2523 | std::string sig(signature); |
| 2524 | std::pair<Signatures::iterator, bool> ins( |
| 2525 | this->signatures_.insert(std::make_pair(sig, group))); |
| 2526 | |
| 2527 | if (ins.second) |
| 2528 | { |
| 2529 | // This is the first time we've seen this signature. |
| 2530 | return true; |
| 2531 | } |
| 2532 | |
| 2533 | if (ins.first->second) |
| 2534 | { |
| 2535 | // We've already seen a real section group with this signature. |
| 2536 | return false; |
| 2537 | } |
| 2538 | else if (group) |
| 2539 | { |
| 2540 | // This is a real section group, and we've already seen a |
| 2541 | // linkonce section with this signature. Record that we've seen |
| 2542 | // a section group, and don't include this section group. |
| 2543 | ins.first->second = true; |
| 2544 | return false; |
| 2545 | } |
| 2546 | else |
| 2547 | { |
| 2548 | // We've already seen a linkonce section and this is a linkonce |
| 2549 | // section. These don't block each other--this may be the same |
| 2550 | // symbol name with different section types. |
| 2551 | return true; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | // Store the allocated sections into the section list. |
| 2556 | |
| 2557 | void |
| 2558 | Layout::get_allocated_sections(Section_list* section_list) const |
| 2559 | { |
| 2560 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2561 | p != this->section_list_.end(); |
| 2562 | ++p) |
| 2563 | if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0) |
| 2564 | section_list->push_back(*p); |
| 2565 | } |
| 2566 | |
| 2567 | // Create an output segment. |
| 2568 | |
| 2569 | Output_segment* |
| 2570 | Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) |
| 2571 | { |
| 2572 | gold_assert(!parameters->options().relocatable()); |
| 2573 | Output_segment* oseg = new Output_segment(type, flags); |
| 2574 | this->segment_list_.push_back(oseg); |
| 2575 | return oseg; |
| 2576 | } |
| 2577 | |
| 2578 | // Write out the Output_sections. Most won't have anything to write, |
| 2579 | // since most of the data will come from input sections which are |
| 2580 | // handled elsewhere. But some Output_sections do have Output_data. |
| 2581 | |
| 2582 | void |
| 2583 | Layout::write_output_sections(Output_file* of) const |
| 2584 | { |
| 2585 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2586 | p != this->section_list_.end(); |
| 2587 | ++p) |
| 2588 | { |
| 2589 | if (!(*p)->after_input_sections()) |
| 2590 | (*p)->write(of); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | // Write out data not associated with a section or the symbol table. |
| 2595 | |
| 2596 | void |
| 2597 | Layout::write_data(const Symbol_table* symtab, Output_file* of) const |
| 2598 | { |
| 2599 | if (!parameters->options().strip_all()) |
| 2600 | { |
| 2601 | const Output_section* symtab_section = this->symtab_section_; |
| 2602 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2603 | p != this->section_list_.end(); |
| 2604 | ++p) |
| 2605 | { |
| 2606 | if ((*p)->needs_symtab_index()) |
| 2607 | { |
| 2608 | gold_assert(symtab_section != NULL); |
| 2609 | unsigned int index = (*p)->symtab_index(); |
| 2610 | gold_assert(index > 0 && index != -1U); |
| 2611 | off_t off = (symtab_section->offset() |
| 2612 | + index * symtab_section->entsize()); |
| 2613 | symtab->write_section_symbol(*p, of, off); |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | const Output_section* dynsym_section = this->dynsym_section_; |
| 2619 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2620 | p != this->section_list_.end(); |
| 2621 | ++p) |
| 2622 | { |
| 2623 | if ((*p)->needs_dynsym_index()) |
| 2624 | { |
| 2625 | gold_assert(dynsym_section != NULL); |
| 2626 | unsigned int index = (*p)->dynsym_index(); |
| 2627 | gold_assert(index > 0 && index != -1U); |
| 2628 | off_t off = (dynsym_section->offset() |
| 2629 | + index * dynsym_section->entsize()); |
| 2630 | symtab->write_section_symbol(*p, of, off); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | // Write out the Output_data which are not in an Output_section. |
| 2635 | for (Data_list::const_iterator p = this->special_output_list_.begin(); |
| 2636 | p != this->special_output_list_.end(); |
| 2637 | ++p) |
| 2638 | (*p)->write(of); |
| 2639 | } |
| 2640 | |
| 2641 | // Write out the Output_sections which can only be written after the |
| 2642 | // input sections are complete. |
| 2643 | |
| 2644 | void |
| 2645 | Layout::write_sections_after_input_sections(Output_file* of) |
| 2646 | { |
| 2647 | // Determine the final section offsets, and thus the final output |
| 2648 | // file size. Note we finalize the .shstrab last, to allow the |
| 2649 | // after_input_section sections to modify their section-names before |
| 2650 | // writing. |
| 2651 | if (this->any_postprocessing_sections_) |
| 2652 | { |
| 2653 | off_t off = this->output_file_size_; |
| 2654 | off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS); |
| 2655 | |
| 2656 | // Now that we've finalized the names, we can finalize the shstrab. |
| 2657 | off = |
| 2658 | this->set_section_offsets(off, |
| 2659 | STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS); |
| 2660 | |
| 2661 | if (off > this->output_file_size_) |
| 2662 | { |
| 2663 | of->resize(off); |
| 2664 | this->output_file_size_ = off; |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2669 | p != this->section_list_.end(); |
| 2670 | ++p) |
| 2671 | { |
| 2672 | if ((*p)->after_input_sections()) |
| 2673 | (*p)->write(of); |
| 2674 | } |
| 2675 | |
| 2676 | this->section_headers_->write(of); |
| 2677 | } |
| 2678 | |
| 2679 | // Write out a binary file. This is called after the link is |
| 2680 | // complete. IN is the temporary output file we used to generate the |
| 2681 | // ELF code. We simply walk through the segments, read them from |
| 2682 | // their file offset in IN, and write them to their load address in |
| 2683 | // the output file. FIXME: with a bit more work, we could support |
| 2684 | // S-records and/or Intel hex format here. |
| 2685 | |
| 2686 | void |
| 2687 | Layout::write_binary(Output_file* in) const |
| 2688 | { |
| 2689 | gold_assert(this->options_.oformat_enum() |
| 2690 | == General_options::OBJECT_FORMAT_BINARY); |
| 2691 | |
| 2692 | // Get the size of the binary file. |
| 2693 | uint64_t max_load_address = 0; |
| 2694 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
| 2695 | p != this->segment_list_.end(); |
| 2696 | ++p) |
| 2697 | { |
| 2698 | if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0) |
| 2699 | { |
| 2700 | uint64_t max_paddr = (*p)->paddr() + (*p)->filesz(); |
| 2701 | if (max_paddr > max_load_address) |
| 2702 | max_load_address = max_paddr; |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | Output_file out(parameters->options().output_file_name()); |
| 2707 | out.open(max_load_address); |
| 2708 | |
| 2709 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
| 2710 | p != this->segment_list_.end(); |
| 2711 | ++p) |
| 2712 | { |
| 2713 | if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0) |
| 2714 | { |
| 2715 | const unsigned char* vin = in->get_input_view((*p)->offset(), |
| 2716 | (*p)->filesz()); |
| 2717 | unsigned char* vout = out.get_output_view((*p)->paddr(), |
| 2718 | (*p)->filesz()); |
| 2719 | memcpy(vout, vin, (*p)->filesz()); |
| 2720 | out.write_output_view((*p)->paddr(), (*p)->filesz(), vout); |
| 2721 | in->free_input_view((*p)->offset(), (*p)->filesz(), vin); |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | out.close(); |
| 2726 | } |
| 2727 | |
| 2728 | // Print statistical information to stderr. This is used for --stats. |
| 2729 | |
| 2730 | void |
| 2731 | Layout::print_stats() const |
| 2732 | { |
| 2733 | this->namepool_.print_stats("section name pool"); |
| 2734 | this->sympool_.print_stats("output symbol name pool"); |
| 2735 | this->dynpool_.print_stats("dynamic name pool"); |
| 2736 | |
| 2737 | for (Section_list::const_iterator p = this->section_list_.begin(); |
| 2738 | p != this->section_list_.end(); |
| 2739 | ++p) |
| 2740 | (*p)->print_merge_stats(); |
| 2741 | } |
| 2742 | |
| 2743 | // Write_sections_task methods. |
| 2744 | |
| 2745 | // We can always run this task. |
| 2746 | |
| 2747 | Task_token* |
| 2748 | Write_sections_task::is_runnable() |
| 2749 | { |
| 2750 | return NULL; |
| 2751 | } |
| 2752 | |
| 2753 | // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER |
| 2754 | // when finished. |
| 2755 | |
| 2756 | void |
| 2757 | Write_sections_task::locks(Task_locker* tl) |
| 2758 | { |
| 2759 | tl->add(this, this->output_sections_blocker_); |
| 2760 | tl->add(this, this->final_blocker_); |
| 2761 | } |
| 2762 | |
| 2763 | // Run the task--write out the data. |
| 2764 | |
| 2765 | void |
| 2766 | Write_sections_task::run(Workqueue*) |
| 2767 | { |
| 2768 | this->layout_->write_output_sections(this->of_); |
| 2769 | } |
| 2770 | |
| 2771 | // Write_data_task methods. |
| 2772 | |
| 2773 | // We can always run this task. |
| 2774 | |
| 2775 | Task_token* |
| 2776 | Write_data_task::is_runnable() |
| 2777 | { |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
| 2781 | // We need to unlock FINAL_BLOCKER when finished. |
| 2782 | |
| 2783 | void |
| 2784 | Write_data_task::locks(Task_locker* tl) |
| 2785 | { |
| 2786 | tl->add(this, this->final_blocker_); |
| 2787 | } |
| 2788 | |
| 2789 | // Run the task--write out the data. |
| 2790 | |
| 2791 | void |
| 2792 | Write_data_task::run(Workqueue*) |
| 2793 | { |
| 2794 | this->layout_->write_data(this->symtab_, this->of_); |
| 2795 | } |
| 2796 | |
| 2797 | // Write_symbols_task methods. |
| 2798 | |
| 2799 | // We can always run this task. |
| 2800 | |
| 2801 | Task_token* |
| 2802 | Write_symbols_task::is_runnable() |
| 2803 | { |
| 2804 | return NULL; |
| 2805 | } |
| 2806 | |
| 2807 | // We need to unlock FINAL_BLOCKER when finished. |
| 2808 | |
| 2809 | void |
| 2810 | Write_symbols_task::locks(Task_locker* tl) |
| 2811 | { |
| 2812 | tl->add(this, this->final_blocker_); |
| 2813 | } |
| 2814 | |
| 2815 | // Run the task--write out the symbols. |
| 2816 | |
| 2817 | void |
| 2818 | Write_symbols_task::run(Workqueue*) |
| 2819 | { |
| 2820 | this->symtab_->write_globals(this->input_objects_, this->sympool_, |
| 2821 | this->dynpool_, this->of_); |
| 2822 | } |
| 2823 | |
| 2824 | // Write_after_input_sections_task methods. |
| 2825 | |
| 2826 | // We can only run this task after the input sections have completed. |
| 2827 | |
| 2828 | Task_token* |
| 2829 | Write_after_input_sections_task::is_runnable() |
| 2830 | { |
| 2831 | if (this->input_sections_blocker_->is_blocked()) |
| 2832 | return this->input_sections_blocker_; |
| 2833 | return NULL; |
| 2834 | } |
| 2835 | |
| 2836 | // We need to unlock FINAL_BLOCKER when finished. |
| 2837 | |
| 2838 | void |
| 2839 | Write_after_input_sections_task::locks(Task_locker* tl) |
| 2840 | { |
| 2841 | tl->add(this, this->final_blocker_); |
| 2842 | } |
| 2843 | |
| 2844 | // Run the task. |
| 2845 | |
| 2846 | void |
| 2847 | Write_after_input_sections_task::run(Workqueue*) |
| 2848 | { |
| 2849 | this->layout_->write_sections_after_input_sections(this->of_); |
| 2850 | } |
| 2851 | |
| 2852 | // Close_task_runner methods. |
| 2853 | |
| 2854 | // Run the task--close the file. |
| 2855 | |
| 2856 | void |
| 2857 | Close_task_runner::run(Workqueue*, const Task*) |
| 2858 | { |
| 2859 | // If we've been asked to create a binary file, we do so here. |
| 2860 | if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
| 2861 | this->layout_->write_binary(this->of_); |
| 2862 | |
| 2863 | this->of_->close(); |
| 2864 | } |
| 2865 | |
| 2866 | // Instantiate the templates we need. We could use the configure |
| 2867 | // script to restrict this to only the ones for implemented targets. |
| 2868 | |
| 2869 | #ifdef HAVE_TARGET_32_LITTLE |
| 2870 | template |
| 2871 | Output_section* |
| 2872 | Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx, |
| 2873 | const char* name, |
| 2874 | const elfcpp::Shdr<32, false>& shdr, |
| 2875 | unsigned int, unsigned int, off_t*); |
| 2876 | #endif |
| 2877 | |
| 2878 | #ifdef HAVE_TARGET_32_BIG |
| 2879 | template |
| 2880 | Output_section* |
| 2881 | Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx, |
| 2882 | const char* name, |
| 2883 | const elfcpp::Shdr<32, true>& shdr, |
| 2884 | unsigned int, unsigned int, off_t*); |
| 2885 | #endif |
| 2886 | |
| 2887 | #ifdef HAVE_TARGET_64_LITTLE |
| 2888 | template |
| 2889 | Output_section* |
| 2890 | Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx, |
| 2891 | const char* name, |
| 2892 | const elfcpp::Shdr<64, false>& shdr, |
| 2893 | unsigned int, unsigned int, off_t*); |
| 2894 | #endif |
| 2895 | |
| 2896 | #ifdef HAVE_TARGET_64_BIG |
| 2897 | template |
| 2898 | Output_section* |
| 2899 | Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx, |
| 2900 | const char* name, |
| 2901 | const elfcpp::Shdr<64, true>& shdr, |
| 2902 | unsigned int, unsigned int, off_t*); |
| 2903 | #endif |
| 2904 | |
| 2905 | #ifdef HAVE_TARGET_32_LITTLE |
| 2906 | template |
| 2907 | Output_section* |
| 2908 | Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object, |
| 2909 | unsigned int reloc_shndx, |
| 2910 | const elfcpp::Shdr<32, false>& shdr, |
| 2911 | Output_section* data_section, |
| 2912 | Relocatable_relocs* rr); |
| 2913 | #endif |
| 2914 | |
| 2915 | #ifdef HAVE_TARGET_32_BIG |
| 2916 | template |
| 2917 | Output_section* |
| 2918 | Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object, |
| 2919 | unsigned int reloc_shndx, |
| 2920 | const elfcpp::Shdr<32, true>& shdr, |
| 2921 | Output_section* data_section, |
| 2922 | Relocatable_relocs* rr); |
| 2923 | #endif |
| 2924 | |
| 2925 | #ifdef HAVE_TARGET_64_LITTLE |
| 2926 | template |
| 2927 | Output_section* |
| 2928 | Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object, |
| 2929 | unsigned int reloc_shndx, |
| 2930 | const elfcpp::Shdr<64, false>& shdr, |
| 2931 | Output_section* data_section, |
| 2932 | Relocatable_relocs* rr); |
| 2933 | #endif |
| 2934 | |
| 2935 | #ifdef HAVE_TARGET_64_BIG |
| 2936 | template |
| 2937 | Output_section* |
| 2938 | Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object, |
| 2939 | unsigned int reloc_shndx, |
| 2940 | const elfcpp::Shdr<64, true>& shdr, |
| 2941 | Output_section* data_section, |
| 2942 | Relocatable_relocs* rr); |
| 2943 | #endif |
| 2944 | |
| 2945 | #ifdef HAVE_TARGET_32_LITTLE |
| 2946 | template |
| 2947 | void |
| 2948 | Layout::layout_group<32, false>(Symbol_table* symtab, |
| 2949 | Sized_relobj<32, false>* object, |
| 2950 | unsigned int, |
| 2951 | const char* group_section_name, |
| 2952 | const char* signature, |
| 2953 | const elfcpp::Shdr<32, false>& shdr, |
| 2954 | const elfcpp::Elf_Word* contents); |
| 2955 | #endif |
| 2956 | |
| 2957 | #ifdef HAVE_TARGET_32_BIG |
| 2958 | template |
| 2959 | void |
| 2960 | Layout::layout_group<32, true>(Symbol_table* symtab, |
| 2961 | Sized_relobj<32, true>* object, |
| 2962 | unsigned int, |
| 2963 | const char* group_section_name, |
| 2964 | const char* signature, |
| 2965 | const elfcpp::Shdr<32, true>& shdr, |
| 2966 | const elfcpp::Elf_Word* contents); |
| 2967 | #endif |
| 2968 | |
| 2969 | #ifdef HAVE_TARGET_64_LITTLE |
| 2970 | template |
| 2971 | void |
| 2972 | Layout::layout_group<64, false>(Symbol_table* symtab, |
| 2973 | Sized_relobj<64, false>* object, |
| 2974 | unsigned int, |
| 2975 | const char* group_section_name, |
| 2976 | const char* signature, |
| 2977 | const elfcpp::Shdr<64, false>& shdr, |
| 2978 | const elfcpp::Elf_Word* contents); |
| 2979 | #endif |
| 2980 | |
| 2981 | #ifdef HAVE_TARGET_64_BIG |
| 2982 | template |
| 2983 | void |
| 2984 | Layout::layout_group<64, true>(Symbol_table* symtab, |
| 2985 | Sized_relobj<64, true>* object, |
| 2986 | unsigned int, |
| 2987 | const char* group_section_name, |
| 2988 | const char* signature, |
| 2989 | const elfcpp::Shdr<64, true>& shdr, |
| 2990 | const elfcpp::Elf_Word* contents); |
| 2991 | #endif |
| 2992 | |
| 2993 | #ifdef HAVE_TARGET_32_LITTLE |
| 2994 | template |
| 2995 | Output_section* |
| 2996 | Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object, |
| 2997 | const unsigned char* symbols, |
| 2998 | off_t symbols_size, |
| 2999 | const unsigned char* symbol_names, |
| 3000 | off_t symbol_names_size, |
| 3001 | unsigned int shndx, |
| 3002 | const elfcpp::Shdr<32, false>& shdr, |
| 3003 | unsigned int reloc_shndx, |
| 3004 | unsigned int reloc_type, |
| 3005 | off_t* off); |
| 3006 | #endif |
| 3007 | |
| 3008 | #ifdef HAVE_TARGET_32_BIG |
| 3009 | template |
| 3010 | Output_section* |
| 3011 | Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object, |
| 3012 | const unsigned char* symbols, |
| 3013 | off_t symbols_size, |
| 3014 | const unsigned char* symbol_names, |
| 3015 | off_t symbol_names_size, |
| 3016 | unsigned int shndx, |
| 3017 | const elfcpp::Shdr<32, true>& shdr, |
| 3018 | unsigned int reloc_shndx, |
| 3019 | unsigned int reloc_type, |
| 3020 | off_t* off); |
| 3021 | #endif |
| 3022 | |
| 3023 | #ifdef HAVE_TARGET_64_LITTLE |
| 3024 | template |
| 3025 | Output_section* |
| 3026 | Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object, |
| 3027 | const unsigned char* symbols, |
| 3028 | off_t symbols_size, |
| 3029 | const unsigned char* symbol_names, |
| 3030 | off_t symbol_names_size, |
| 3031 | unsigned int shndx, |
| 3032 | const elfcpp::Shdr<64, false>& shdr, |
| 3033 | unsigned int reloc_shndx, |
| 3034 | unsigned int reloc_type, |
| 3035 | off_t* off); |
| 3036 | #endif |
| 3037 | |
| 3038 | #ifdef HAVE_TARGET_64_BIG |
| 3039 | template |
| 3040 | Output_section* |
| 3041 | Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object, |
| 3042 | const unsigned char* symbols, |
| 3043 | off_t symbols_size, |
| 3044 | const unsigned char* symbol_names, |
| 3045 | off_t symbol_names_size, |
| 3046 | unsigned int shndx, |
| 3047 | const elfcpp::Shdr<64, true>& shdr, |
| 3048 | unsigned int reloc_shndx, |
| 3049 | unsigned int reloc_type, |
| 3050 | off_t* off); |
| 3051 | #endif |
| 3052 | |
| 3053 | } // End namespace gold. |