| 1 | // output.cc -- manage the output file for gold |
| 2 | |
| 3 | #include "gold.h" |
| 4 | |
| 5 | #include <cstdlib> |
| 6 | #include <cerrno> |
| 7 | #include <fcntl.h> |
| 8 | #include <unistd.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <algorithm> |
| 11 | |
| 12 | #include "object.h" |
| 13 | #include "symtab.h" |
| 14 | #include "reloc.h" |
| 15 | #include "merge.h" |
| 16 | #include "output.h" |
| 17 | |
| 18 | namespace gold |
| 19 | { |
| 20 | |
| 21 | // Output_data variables. |
| 22 | |
| 23 | bool Output_data::sizes_are_fixed; |
| 24 | |
| 25 | // Output_data methods. |
| 26 | |
| 27 | Output_data::~Output_data() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | // Set the address and offset. |
| 32 | |
| 33 | void |
| 34 | Output_data::set_address(uint64_t addr, off_t off) |
| 35 | { |
| 36 | this->address_ = addr; |
| 37 | this->offset_ = off; |
| 38 | |
| 39 | // Let the child class know. |
| 40 | this->do_set_address(addr, off); |
| 41 | } |
| 42 | |
| 43 | // Return the default alignment for a size--32 or 64. |
| 44 | |
| 45 | uint64_t |
| 46 | Output_data::default_alignment(int size) |
| 47 | { |
| 48 | if (size == 32) |
| 49 | return 4; |
| 50 | else if (size == 64) |
| 51 | return 8; |
| 52 | else |
| 53 | gold_unreachable(); |
| 54 | } |
| 55 | |
| 56 | // Output_section_header methods. This currently assumes that the |
| 57 | // segment and section lists are complete at construction time. |
| 58 | |
| 59 | Output_section_headers::Output_section_headers( |
| 60 | int size, |
| 61 | bool big_endian, |
| 62 | const Layout* layout, |
| 63 | const Layout::Segment_list* segment_list, |
| 64 | const Layout::Section_list* unattached_section_list, |
| 65 | const Stringpool* secnamepool) |
| 66 | : size_(size), |
| 67 | big_endian_(big_endian), |
| 68 | layout_(layout), |
| 69 | segment_list_(segment_list), |
| 70 | unattached_section_list_(unattached_section_list), |
| 71 | secnamepool_(secnamepool) |
| 72 | { |
| 73 | // Count all the sections. Start with 1 for the null section. |
| 74 | off_t count = 1; |
| 75 | for (Layout::Segment_list::const_iterator p = segment_list->begin(); |
| 76 | p != segment_list->end(); |
| 77 | ++p) |
| 78 | if ((*p)->type() == elfcpp::PT_LOAD) |
| 79 | count += (*p)->output_section_count(); |
| 80 | count += unattached_section_list->size(); |
| 81 | |
| 82 | int shdr_size; |
| 83 | if (size == 32) |
| 84 | shdr_size = elfcpp::Elf_sizes<32>::shdr_size; |
| 85 | else if (size == 64) |
| 86 | shdr_size = elfcpp::Elf_sizes<64>::shdr_size; |
| 87 | else |
| 88 | gold_unreachable(); |
| 89 | |
| 90 | this->set_data_size(count * shdr_size); |
| 91 | } |
| 92 | |
| 93 | // Write out the section headers. |
| 94 | |
| 95 | void |
| 96 | Output_section_headers::do_write(Output_file* of) |
| 97 | { |
| 98 | if (this->size_ == 32) |
| 99 | { |
| 100 | if (this->big_endian_) |
| 101 | this->do_sized_write<32, true>(of); |
| 102 | else |
| 103 | this->do_sized_write<32, false>(of); |
| 104 | } |
| 105 | else if (this->size_ == 64) |
| 106 | { |
| 107 | if (this->big_endian_) |
| 108 | this->do_sized_write<64, true>(of); |
| 109 | else |
| 110 | this->do_sized_write<64, false>(of); |
| 111 | } |
| 112 | else |
| 113 | gold_unreachable(); |
| 114 | } |
| 115 | |
| 116 | template<int size, bool big_endian> |
| 117 | void |
| 118 | Output_section_headers::do_sized_write(Output_file* of) |
| 119 | { |
| 120 | off_t all_shdrs_size = this->data_size(); |
| 121 | unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size); |
| 122 | |
| 123 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; |
| 124 | unsigned char* v = view; |
| 125 | |
| 126 | { |
| 127 | typename elfcpp::Shdr_write<size, big_endian> oshdr(v); |
| 128 | oshdr.put_sh_name(0); |
| 129 | oshdr.put_sh_type(elfcpp::SHT_NULL); |
| 130 | oshdr.put_sh_flags(0); |
| 131 | oshdr.put_sh_addr(0); |
| 132 | oshdr.put_sh_offset(0); |
| 133 | oshdr.put_sh_size(0); |
| 134 | oshdr.put_sh_link(0); |
| 135 | oshdr.put_sh_info(0); |
| 136 | oshdr.put_sh_addralign(0); |
| 137 | oshdr.put_sh_entsize(0); |
| 138 | } |
| 139 | |
| 140 | v += shdr_size; |
| 141 | |
| 142 | unsigned shndx = 1; |
| 143 | for (Layout::Segment_list::const_iterator p = this->segment_list_->begin(); |
| 144 | p != this->segment_list_->end(); |
| 145 | ++p) |
| 146 | v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 147 | this->layout_, this->secnamepool_, v, &shndx |
| 148 | SELECT_SIZE_ENDIAN(size, big_endian)); |
| 149 | for (Layout::Section_list::const_iterator p = |
| 150 | this->unattached_section_list_->begin(); |
| 151 | p != this->unattached_section_list_->end(); |
| 152 | ++p) |
| 153 | { |
| 154 | gold_assert(shndx == (*p)->out_shndx()); |
| 155 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
| 156 | (*p)->write_header(this->layout_, this->secnamepool_, &oshdr); |
| 157 | v += shdr_size; |
| 158 | ++shndx; |
| 159 | } |
| 160 | |
| 161 | of->write_output_view(this->offset(), all_shdrs_size, view); |
| 162 | } |
| 163 | |
| 164 | // Output_segment_header methods. |
| 165 | |
| 166 | Output_segment_headers::Output_segment_headers( |
| 167 | int size, |
| 168 | bool big_endian, |
| 169 | const Layout::Segment_list& segment_list) |
| 170 | : size_(size), big_endian_(big_endian), segment_list_(segment_list) |
| 171 | { |
| 172 | int phdr_size; |
| 173 | if (size == 32) |
| 174 | phdr_size = elfcpp::Elf_sizes<32>::phdr_size; |
| 175 | else if (size == 64) |
| 176 | phdr_size = elfcpp::Elf_sizes<64>::phdr_size; |
| 177 | else |
| 178 | gold_unreachable(); |
| 179 | |
| 180 | this->set_data_size(segment_list.size() * phdr_size); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | Output_segment_headers::do_write(Output_file* of) |
| 185 | { |
| 186 | if (this->size_ == 32) |
| 187 | { |
| 188 | if (this->big_endian_) |
| 189 | this->do_sized_write<32, true>(of); |
| 190 | else |
| 191 | this->do_sized_write<32, false>(of); |
| 192 | } |
| 193 | else if (this->size_ == 64) |
| 194 | { |
| 195 | if (this->big_endian_) |
| 196 | this->do_sized_write<64, true>(of); |
| 197 | else |
| 198 | this->do_sized_write<64, false>(of); |
| 199 | } |
| 200 | else |
| 201 | gold_unreachable(); |
| 202 | } |
| 203 | |
| 204 | template<int size, bool big_endian> |
| 205 | void |
| 206 | Output_segment_headers::do_sized_write(Output_file* of) |
| 207 | { |
| 208 | const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size; |
| 209 | off_t all_phdrs_size = this->segment_list_.size() * phdr_size; |
| 210 | unsigned char* view = of->get_output_view(this->offset(), |
| 211 | all_phdrs_size); |
| 212 | unsigned char* v = view; |
| 213 | for (Layout::Segment_list::const_iterator p = this->segment_list_.begin(); |
| 214 | p != this->segment_list_.end(); |
| 215 | ++p) |
| 216 | { |
| 217 | elfcpp::Phdr_write<size, big_endian> ophdr(v); |
| 218 | (*p)->write_header(&ophdr); |
| 219 | v += phdr_size; |
| 220 | } |
| 221 | |
| 222 | of->write_output_view(this->offset(), all_phdrs_size, view); |
| 223 | } |
| 224 | |
| 225 | // Output_file_header methods. |
| 226 | |
| 227 | Output_file_header::Output_file_header(int size, |
| 228 | bool big_endian, |
| 229 | const General_options& options, |
| 230 | const Target* target, |
| 231 | const Symbol_table* symtab, |
| 232 | const Output_segment_headers* osh) |
| 233 | : size_(size), |
| 234 | big_endian_(big_endian), |
| 235 | options_(options), |
| 236 | target_(target), |
| 237 | symtab_(symtab), |
| 238 | segment_header_(osh), |
| 239 | section_header_(NULL), |
| 240 | shstrtab_(NULL) |
| 241 | { |
| 242 | int ehdr_size; |
| 243 | if (size == 32) |
| 244 | ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size; |
| 245 | else if (size == 64) |
| 246 | ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size; |
| 247 | else |
| 248 | gold_unreachable(); |
| 249 | |
| 250 | this->set_data_size(ehdr_size); |
| 251 | } |
| 252 | |
| 253 | // Set the section table information for a file header. |
| 254 | |
| 255 | void |
| 256 | Output_file_header::set_section_info(const Output_section_headers* shdrs, |
| 257 | const Output_section* shstrtab) |
| 258 | { |
| 259 | this->section_header_ = shdrs; |
| 260 | this->shstrtab_ = shstrtab; |
| 261 | } |
| 262 | |
| 263 | // Write out the file header. |
| 264 | |
| 265 | void |
| 266 | Output_file_header::do_write(Output_file* of) |
| 267 | { |
| 268 | if (this->size_ == 32) |
| 269 | { |
| 270 | if (this->big_endian_) |
| 271 | this->do_sized_write<32, true>(of); |
| 272 | else |
| 273 | this->do_sized_write<32, false>(of); |
| 274 | } |
| 275 | else if (this->size_ == 64) |
| 276 | { |
| 277 | if (this->big_endian_) |
| 278 | this->do_sized_write<64, true>(of); |
| 279 | else |
| 280 | this->do_sized_write<64, false>(of); |
| 281 | } |
| 282 | else |
| 283 | gold_unreachable(); |
| 284 | } |
| 285 | |
| 286 | // Write out the file header with appropriate size and endianess. |
| 287 | |
| 288 | template<int size, bool big_endian> |
| 289 | void |
| 290 | Output_file_header::do_sized_write(Output_file* of) |
| 291 | { |
| 292 | gold_assert(this->offset() == 0); |
| 293 | |
| 294 | int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; |
| 295 | unsigned char* view = of->get_output_view(0, ehdr_size); |
| 296 | elfcpp::Ehdr_write<size, big_endian> oehdr(view); |
| 297 | |
| 298 | unsigned char e_ident[elfcpp::EI_NIDENT]; |
| 299 | memset(e_ident, 0, elfcpp::EI_NIDENT); |
| 300 | e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; |
| 301 | e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; |
| 302 | e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; |
| 303 | e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; |
| 304 | if (size == 32) |
| 305 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; |
| 306 | else if (size == 64) |
| 307 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; |
| 308 | else |
| 309 | gold_unreachable(); |
| 310 | e_ident[elfcpp::EI_DATA] = (big_endian |
| 311 | ? elfcpp::ELFDATA2MSB |
| 312 | : elfcpp::ELFDATA2LSB); |
| 313 | e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; |
| 314 | // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION. |
| 315 | oehdr.put_e_ident(e_ident); |
| 316 | |
| 317 | elfcpp::ET e_type; |
| 318 | // FIXME: ET_DYN. |
| 319 | if (this->options_.is_relocatable()) |
| 320 | e_type = elfcpp::ET_REL; |
| 321 | else |
| 322 | e_type = elfcpp::ET_EXEC; |
| 323 | oehdr.put_e_type(e_type); |
| 324 | |
| 325 | oehdr.put_e_machine(this->target_->machine_code()); |
| 326 | oehdr.put_e_version(elfcpp::EV_CURRENT); |
| 327 | |
| 328 | // FIXME: Need to support -e, and target specific entry symbol. |
| 329 | Symbol* sym = this->symtab_->lookup("_start"); |
| 330 | typename Sized_symbol<size>::Value_type v; |
| 331 | if (sym == NULL) |
| 332 | v = 0; |
| 333 | else |
| 334 | { |
| 335 | Sized_symbol<size>* ssym; |
| 336 | ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) ( |
| 337 | sym SELECT_SIZE(size)); |
| 338 | v = ssym->value(); |
| 339 | } |
| 340 | oehdr.put_e_entry(v); |
| 341 | |
| 342 | oehdr.put_e_phoff(this->segment_header_->offset()); |
| 343 | oehdr.put_e_shoff(this->section_header_->offset()); |
| 344 | |
| 345 | // FIXME: The target needs to set the flags. |
| 346 | oehdr.put_e_flags(0); |
| 347 | |
| 348 | oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); |
| 349 | oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size); |
| 350 | oehdr.put_e_phnum(this->segment_header_->data_size() |
| 351 | / elfcpp::Elf_sizes<size>::phdr_size); |
| 352 | oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); |
| 353 | oehdr.put_e_shnum(this->section_header_->data_size() |
| 354 | / elfcpp::Elf_sizes<size>::shdr_size); |
| 355 | oehdr.put_e_shstrndx(this->shstrtab_->out_shndx()); |
| 356 | |
| 357 | of->write_output_view(0, ehdr_size, view); |
| 358 | } |
| 359 | |
| 360 | // Output_data_const methods. |
| 361 | |
| 362 | void |
| 363 | Output_data_const::do_write(Output_file* of) |
| 364 | { |
| 365 | of->write(this->offset(), this->data_.data(), this->data_.size()); |
| 366 | } |
| 367 | |
| 368 | // Output_data_const_buffer methods. |
| 369 | |
| 370 | void |
| 371 | Output_data_const_buffer::do_write(Output_file* of) |
| 372 | { |
| 373 | of->write(this->offset(), this->p_, this->data_size()); |
| 374 | } |
| 375 | |
| 376 | // Output_section_data methods. |
| 377 | |
| 378 | // Record the output section, and set the entry size and such. |
| 379 | |
| 380 | void |
| 381 | Output_section_data::set_output_section(Output_section* os) |
| 382 | { |
| 383 | gold_assert(this->output_section_ == NULL); |
| 384 | this->output_section_ = os; |
| 385 | this->do_adjust_output_section(os); |
| 386 | } |
| 387 | |
| 388 | // Return the section index of the output section. |
| 389 | |
| 390 | unsigned int |
| 391 | Output_section_data::do_out_shndx() const |
| 392 | { |
| 393 | gold_assert(this->output_section_ != NULL); |
| 394 | return this->output_section_->out_shndx(); |
| 395 | } |
| 396 | |
| 397 | // Output_data_strtab methods. |
| 398 | |
| 399 | // Set the address. We don't actually care about the address, but we |
| 400 | // do set our final size. |
| 401 | |
| 402 | void |
| 403 | Output_data_strtab::do_set_address(uint64_t, off_t) |
| 404 | { |
| 405 | this->strtab_->set_string_offsets(); |
| 406 | this->set_data_size(this->strtab_->get_strtab_size()); |
| 407 | } |
| 408 | |
| 409 | // Write out a string table. |
| 410 | |
| 411 | void |
| 412 | Output_data_strtab::do_write(Output_file* of) |
| 413 | { |
| 414 | this->strtab_->write(of, this->offset()); |
| 415 | } |
| 416 | |
| 417 | // Output_reloc methods. |
| 418 | |
| 419 | // Get the symbol index of a relocation. |
| 420 | |
| 421 | template<bool dynamic, int size, bool big_endian> |
| 422 | unsigned int |
| 423 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index() |
| 424 | const |
| 425 | { |
| 426 | unsigned int index; |
| 427 | switch (this->local_sym_index_) |
| 428 | { |
| 429 | case INVALID_CODE: |
| 430 | gold_unreachable(); |
| 431 | |
| 432 | case GSYM_CODE: |
| 433 | if (this->u1_.gsym == NULL) |
| 434 | index = 0; |
| 435 | else if (dynamic) |
| 436 | index = this->u1_.gsym->dynsym_index(); |
| 437 | else |
| 438 | index = this->u1_.gsym->symtab_index(); |
| 439 | break; |
| 440 | |
| 441 | case SECTION_CODE: |
| 442 | if (dynamic) |
| 443 | index = this->u1_.os->dynsym_index(); |
| 444 | else |
| 445 | index = this->u1_.os->symtab_index(); |
| 446 | break; |
| 447 | |
| 448 | default: |
| 449 | if (dynamic) |
| 450 | { |
| 451 | // FIXME: It seems that some targets may need to generate |
| 452 | // dynamic relocations against local symbols for some |
| 453 | // reasons. This will have to be addressed at some point. |
| 454 | gold_unreachable(); |
| 455 | } |
| 456 | else |
| 457 | index = this->u1_.relobj->symtab_index(this->local_sym_index_); |
| 458 | break; |
| 459 | } |
| 460 | gold_assert(index != -1U); |
| 461 | return index; |
| 462 | } |
| 463 | |
| 464 | // Write out the offset and info fields of a Rel or Rela relocation |
| 465 | // entry. |
| 466 | |
| 467 | template<bool dynamic, int size, bool big_endian> |
| 468 | template<typename Write_rel> |
| 469 | void |
| 470 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel( |
| 471 | Write_rel* wr) const |
| 472 | { |
| 473 | Address address = this->address_; |
| 474 | if (this->shndx_ != INVALID_CODE) |
| 475 | { |
| 476 | off_t off; |
| 477 | Output_section* os = this->u2_.relobj->output_section(this->shndx_, |
| 478 | &off); |
| 479 | gold_assert(os != NULL); |
| 480 | address += os->address() + off; |
| 481 | } |
| 482 | else if (this->u2_.od != NULL) |
| 483 | address += this->u2_.od->address(); |
| 484 | wr->put_r_offset(address); |
| 485 | wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(), |
| 486 | this->type_)); |
| 487 | } |
| 488 | |
| 489 | // Write out a Rel relocation. |
| 490 | |
| 491 | template<bool dynamic, int size, bool big_endian> |
| 492 | void |
| 493 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write( |
| 494 | unsigned char* pov) const |
| 495 | { |
| 496 | elfcpp::Rel_write<size, big_endian> orel(pov); |
| 497 | this->write_rel(&orel); |
| 498 | } |
| 499 | |
| 500 | // Write out a Rela relocation. |
| 501 | |
| 502 | template<bool dynamic, int size, bool big_endian> |
| 503 | void |
| 504 | Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write( |
| 505 | unsigned char* pov) const |
| 506 | { |
| 507 | elfcpp::Rela_write<size, big_endian> orel(pov); |
| 508 | this->rel_.write_rel(&orel); |
| 509 | orel.put_r_addend(this->addend_); |
| 510 | } |
| 511 | |
| 512 | // Output_data_reloc_base methods. |
| 513 | |
| 514 | // Adjust the output section. |
| 515 | |
| 516 | template<int sh_type, bool dynamic, int size, bool big_endian> |
| 517 | void |
| 518 | Output_data_reloc_base<sh_type, dynamic, size, big_endian> |
| 519 | ::do_adjust_output_section(Output_section* os) |
| 520 | { |
| 521 | if (sh_type == elfcpp::SHT_REL) |
| 522 | os->set_entsize(elfcpp::Elf_sizes<size>::rel_size); |
| 523 | else if (sh_type == elfcpp::SHT_RELA) |
| 524 | os->set_entsize(elfcpp::Elf_sizes<size>::rela_size); |
| 525 | else |
| 526 | gold_unreachable(); |
| 527 | if (dynamic) |
| 528 | os->set_should_link_to_dynsym(); |
| 529 | else |
| 530 | os->set_should_link_to_symtab(); |
| 531 | } |
| 532 | |
| 533 | // Write out relocation data. |
| 534 | |
| 535 | template<int sh_type, bool dynamic, int size, bool big_endian> |
| 536 | void |
| 537 | Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write( |
| 538 | Output_file* of) |
| 539 | { |
| 540 | const off_t off = this->offset(); |
| 541 | const off_t oview_size = this->data_size(); |
| 542 | unsigned char* const oview = of->get_output_view(off, oview_size); |
| 543 | |
| 544 | unsigned char* pov = oview; |
| 545 | for (typename Relocs::const_iterator p = this->relocs_.begin(); |
| 546 | p != this->relocs_.end(); |
| 547 | ++p) |
| 548 | { |
| 549 | p->write(pov); |
| 550 | pov += reloc_size; |
| 551 | } |
| 552 | |
| 553 | gold_assert(pov - oview == oview_size); |
| 554 | |
| 555 | of->write_output_view(off, oview_size, oview); |
| 556 | |
| 557 | // We no longer need the relocation entries. |
| 558 | this->relocs_.clear(); |
| 559 | } |
| 560 | |
| 561 | // Output_data_got::Got_entry methods. |
| 562 | |
| 563 | // Write out the entry. |
| 564 | |
| 565 | template<int size, bool big_endian> |
| 566 | void |
| 567 | Output_data_got<size, big_endian>::Got_entry::write( |
| 568 | const General_options* options, |
| 569 | unsigned char* pov) const |
| 570 | { |
| 571 | Valtype val = 0; |
| 572 | |
| 573 | switch (this->local_sym_index_) |
| 574 | { |
| 575 | case GSYM_CODE: |
| 576 | { |
| 577 | Symbol* gsym = this->u_.gsym; |
| 578 | |
| 579 | // If the symbol is resolved locally, we need to write out its |
| 580 | // value. Otherwise we just write zero. The target code is |
| 581 | // responsible for creating a relocation entry to fill in the |
| 582 | // value at runtime. |
| 583 | if (gsym->final_value_is_known(options)) |
| 584 | { |
| 585 | Sized_symbol<size>* sgsym; |
| 586 | // This cast is a bit ugly. We don't want to put a |
| 587 | // virtual method in Symbol, because we want Symbol to be |
| 588 | // as small as possible. |
| 589 | sgsym = static_cast<Sized_symbol<size>*>(gsym); |
| 590 | val = sgsym->value(); |
| 591 | } |
| 592 | } |
| 593 | break; |
| 594 | |
| 595 | case CONSTANT_CODE: |
| 596 | val = this->u_.constant; |
| 597 | break; |
| 598 | |
| 599 | default: |
| 600 | gold_unreachable(); |
| 601 | } |
| 602 | |
| 603 | elfcpp::Swap<size, big_endian>::writeval(pov, val); |
| 604 | } |
| 605 | |
| 606 | // Output_data_got methods. |
| 607 | |
| 608 | // Add an entry for a global symbol to the GOT. This returns true if |
| 609 | // this is a new GOT entry, false if the symbol already had a GOT |
| 610 | // entry. |
| 611 | |
| 612 | template<int size, bool big_endian> |
| 613 | bool |
| 614 | Output_data_got<size, big_endian>::add_global(Symbol* gsym) |
| 615 | { |
| 616 | if (gsym->has_got_offset()) |
| 617 | return false; |
| 618 | |
| 619 | this->entries_.push_back(Got_entry(gsym)); |
| 620 | this->set_got_size(); |
| 621 | gsym->set_got_offset(this->last_got_offset()); |
| 622 | return true; |
| 623 | } |
| 624 | |
| 625 | // Write out the GOT. |
| 626 | |
| 627 | template<int size, bool big_endian> |
| 628 | void |
| 629 | Output_data_got<size, big_endian>::do_write(Output_file* of) |
| 630 | { |
| 631 | const int add = size / 8; |
| 632 | |
| 633 | const off_t off = this->offset(); |
| 634 | const off_t oview_size = this->data_size(); |
| 635 | unsigned char* const oview = of->get_output_view(off, oview_size); |
| 636 | |
| 637 | unsigned char* pov = oview; |
| 638 | for (typename Got_entries::const_iterator p = this->entries_.begin(); |
| 639 | p != this->entries_.end(); |
| 640 | ++p) |
| 641 | { |
| 642 | p->write(this->options_, pov); |
| 643 | pov += add; |
| 644 | } |
| 645 | |
| 646 | gold_assert(pov - oview == oview_size); |
| 647 | |
| 648 | of->write_output_view(off, oview_size, oview); |
| 649 | |
| 650 | // We no longer need the GOT entries. |
| 651 | this->entries_.clear(); |
| 652 | } |
| 653 | |
| 654 | // Output_data_dynamic::Dynamic_entry methods. |
| 655 | |
| 656 | // Write out the entry. |
| 657 | |
| 658 | template<int size, bool big_endian> |
| 659 | void |
| 660 | Output_data_dynamic::Dynamic_entry::write( |
| 661 | unsigned char* pov, |
| 662 | const Stringpool* pool |
| 663 | ACCEPT_SIZE_ENDIAN) const |
| 664 | { |
| 665 | typename elfcpp::Elf_types<size>::Elf_WXword val; |
| 666 | switch (this->classification_) |
| 667 | { |
| 668 | case DYNAMIC_NUMBER: |
| 669 | val = this->u_.val; |
| 670 | break; |
| 671 | |
| 672 | case DYNAMIC_SECTION_ADDRESS: |
| 673 | val = this->u_.od->address(); |
| 674 | break; |
| 675 | |
| 676 | case DYNAMIC_SECTION_SIZE: |
| 677 | val = this->u_.od->data_size(); |
| 678 | break; |
| 679 | |
| 680 | case DYNAMIC_SYMBOL: |
| 681 | { |
| 682 | const Sized_symbol<size>* s = |
| 683 | static_cast<const Sized_symbol<size>*>(this->u_.sym); |
| 684 | val = s->value(); |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case DYNAMIC_STRING: |
| 689 | val = pool->get_offset(this->u_.str); |
| 690 | break; |
| 691 | |
| 692 | default: |
| 693 | gold_unreachable(); |
| 694 | } |
| 695 | |
| 696 | elfcpp::Dyn_write<size, big_endian> dw(pov); |
| 697 | dw.put_d_tag(this->tag_); |
| 698 | dw.put_d_val(val); |
| 699 | } |
| 700 | |
| 701 | // Output_data_dynamic methods. |
| 702 | |
| 703 | // Adjust the output section to set the entry size. |
| 704 | |
| 705 | void |
| 706 | Output_data_dynamic::do_adjust_output_section(Output_section* os) |
| 707 | { |
| 708 | if (this->target_->get_size() == 32) |
| 709 | os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size); |
| 710 | else if (this->target_->get_size() == 64) |
| 711 | os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size); |
| 712 | else |
| 713 | gold_unreachable(); |
| 714 | } |
| 715 | |
| 716 | // Set the final data size. |
| 717 | |
| 718 | void |
| 719 | Output_data_dynamic::do_set_address(uint64_t, off_t) |
| 720 | { |
| 721 | // Add the terminating entry. |
| 722 | this->add_constant(elfcpp::DT_NULL, 0); |
| 723 | |
| 724 | int dyn_size; |
| 725 | if (this->target_->get_size() == 32) |
| 726 | dyn_size = elfcpp::Elf_sizes<32>::dyn_size; |
| 727 | else if (this->target_->get_size() == 64) |
| 728 | dyn_size = elfcpp::Elf_sizes<64>::dyn_size; |
| 729 | else |
| 730 | gold_unreachable(); |
| 731 | this->set_data_size(this->entries_.size() * dyn_size); |
| 732 | } |
| 733 | |
| 734 | // Write out the dynamic entries. |
| 735 | |
| 736 | void |
| 737 | Output_data_dynamic::do_write(Output_file* of) |
| 738 | { |
| 739 | if (this->target_->get_size() == 32) |
| 740 | { |
| 741 | if (this->target_->is_big_endian()) |
| 742 | this->sized_write<32, true>(of); |
| 743 | else |
| 744 | this->sized_write<32, false>(of); |
| 745 | } |
| 746 | else if (this->target_->get_size() == 64) |
| 747 | { |
| 748 | if (this->target_->is_big_endian()) |
| 749 | this->sized_write<64, true>(of); |
| 750 | else |
| 751 | this->sized_write<64, false>(of); |
| 752 | } |
| 753 | else |
| 754 | gold_unreachable(); |
| 755 | } |
| 756 | |
| 757 | template<int size, bool big_endian> |
| 758 | void |
| 759 | Output_data_dynamic::sized_write(Output_file* of) |
| 760 | { |
| 761 | const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size; |
| 762 | |
| 763 | const off_t offset = this->offset(); |
| 764 | const off_t oview_size = this->data_size(); |
| 765 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
| 766 | |
| 767 | unsigned char* pov = oview; |
| 768 | for (typename Dynamic_entries::const_iterator p = this->entries_.begin(); |
| 769 | p != this->entries_.end(); |
| 770 | ++p) |
| 771 | { |
| 772 | p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)( |
| 773 | pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian)); |
| 774 | pov += dyn_size; |
| 775 | } |
| 776 | |
| 777 | gold_assert(pov - oview == oview_size); |
| 778 | |
| 779 | of->write_output_view(offset, oview_size, oview); |
| 780 | |
| 781 | // We no longer need the dynamic entries. |
| 782 | this->entries_.clear(); |
| 783 | } |
| 784 | |
| 785 | // Output_section::Input_section methods. |
| 786 | |
| 787 | // Return the data size. For an input section we store the size here. |
| 788 | // For an Output_section_data, we have to ask it for the size. |
| 789 | |
| 790 | off_t |
| 791 | Output_section::Input_section::data_size() const |
| 792 | { |
| 793 | if (this->is_input_section()) |
| 794 | return this->u1_.data_size; |
| 795 | else |
| 796 | return this->u2_.posd->data_size(); |
| 797 | } |
| 798 | |
| 799 | // Set the address and file offset. |
| 800 | |
| 801 | void |
| 802 | Output_section::Input_section::set_address(uint64_t addr, off_t off, |
| 803 | off_t secoff) |
| 804 | { |
| 805 | if (this->is_input_section()) |
| 806 | this->u2_.object->set_section_offset(this->shndx_, off - secoff); |
| 807 | else |
| 808 | this->u2_.posd->set_address(addr, off); |
| 809 | } |
| 810 | |
| 811 | // Try to turn an input address into an output address. |
| 812 | |
| 813 | bool |
| 814 | Output_section::Input_section::output_address(const Relobj* object, |
| 815 | unsigned int shndx, |
| 816 | off_t offset, |
| 817 | uint64_t output_section_address, |
| 818 | uint64_t *poutput) const |
| 819 | { |
| 820 | if (!this->is_input_section()) |
| 821 | return this->u2_.posd->output_address(object, shndx, offset, |
| 822 | output_section_address, poutput); |
| 823 | else |
| 824 | { |
| 825 | if (this->u2_.object != object) |
| 826 | return false; |
| 827 | off_t output_offset; |
| 828 | Output_section* os = object->output_section(shndx, &output_offset); |
| 829 | gold_assert(os != NULL); |
| 830 | *poutput = output_section_address + output_offset + offset; |
| 831 | return true; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // Write out the data. We don't have to do anything for an input |
| 836 | // section--they are handled via Object::relocate--but this is where |
| 837 | // we write out the data for an Output_section_data. |
| 838 | |
| 839 | void |
| 840 | Output_section::Input_section::write(Output_file* of) |
| 841 | { |
| 842 | if (!this->is_input_section()) |
| 843 | this->u2_.posd->write(of); |
| 844 | } |
| 845 | |
| 846 | // Output_section methods. |
| 847 | |
| 848 | // Construct an Output_section. NAME will point into a Stringpool. |
| 849 | |
| 850 | Output_section::Output_section(const char* name, elfcpp::Elf_Word type, |
| 851 | elfcpp::Elf_Xword flags) |
| 852 | : name_(name), |
| 853 | addralign_(0), |
| 854 | entsize_(0), |
| 855 | link_section_(NULL), |
| 856 | link_(0), |
| 857 | info_section_(NULL), |
| 858 | info_(0), |
| 859 | type_(type), |
| 860 | flags_(flags), |
| 861 | out_shndx_(0), |
| 862 | symtab_index_(0), |
| 863 | dynsym_index_(0), |
| 864 | input_sections_(), |
| 865 | first_input_offset_(0), |
| 866 | needs_symtab_index_(false), |
| 867 | needs_dynsym_index_(false), |
| 868 | should_link_to_symtab_(false), |
| 869 | should_link_to_dynsym_(false) |
| 870 | { |
| 871 | } |
| 872 | |
| 873 | Output_section::~Output_section() |
| 874 | { |
| 875 | } |
| 876 | |
| 877 | // Set the entry size. |
| 878 | |
| 879 | void |
| 880 | Output_section::set_entsize(uint64_t v) |
| 881 | { |
| 882 | if (this->entsize_ == 0) |
| 883 | this->entsize_ = v; |
| 884 | else |
| 885 | gold_assert(this->entsize_ == v); |
| 886 | } |
| 887 | |
| 888 | // Add the input section SHNDX, with header SHDR, named SECNAME, in |
| 889 | // OBJECT, to the Output_section. Return the offset of the input |
| 890 | // section within the output section. We don't always keep track of |
| 891 | // input sections for an Output_section. Instead, each Object keeps |
| 892 | // track of the Output_section for each of its input sections. |
| 893 | |
| 894 | template<int size, bool big_endian> |
| 895 | off_t |
| 896 | Output_section::add_input_section(Relobj* object, unsigned int shndx, |
| 897 | const char* secname, |
| 898 | const elfcpp::Shdr<size, big_endian>& shdr) |
| 899 | { |
| 900 | elfcpp::Elf_Xword addralign = shdr.get_sh_addralign(); |
| 901 | if ((addralign & (addralign - 1)) != 0) |
| 902 | { |
| 903 | fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"), |
| 904 | program_name, object->name().c_str(), |
| 905 | static_cast<unsigned long>(addralign), secname); |
| 906 | gold_exit(false); |
| 907 | } |
| 908 | |
| 909 | if (addralign > this->addralign_) |
| 910 | this->addralign_ = addralign; |
| 911 | |
| 912 | // If this is a SHF_MERGE section, we pass all the input sections to |
| 913 | // a Output_data_merge. |
| 914 | if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0) |
| 915 | { |
| 916 | if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(), |
| 917 | shdr.get_sh_entsize(), |
| 918 | addralign)) |
| 919 | { |
| 920 | // Tell the relocation routines that they need to call the |
| 921 | // output_address method to determine the final address. |
| 922 | return -1; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | off_t ssize = this->data_size(); |
| 927 | ssize = align_address(ssize, addralign); |
| 928 | this->set_data_size(ssize + shdr.get_sh_size()); |
| 929 | |
| 930 | // We need to keep track of this section if we are already keeping |
| 931 | // track of sections, or if we are relaxing. FIXME: Add test for |
| 932 | // relaxing. |
| 933 | if (! this->input_sections_.empty()) |
| 934 | this->input_sections_.push_back(Input_section(object, shndx, |
| 935 | shdr.get_sh_size(), |
| 936 | addralign)); |
| 937 | |
| 938 | return ssize; |
| 939 | } |
| 940 | |
| 941 | // Add arbitrary data to an output section. |
| 942 | |
| 943 | void |
| 944 | Output_section::add_output_section_data(Output_section_data* posd) |
| 945 | { |
| 946 | Input_section inp(posd); |
| 947 | this->add_output_section_data(&inp); |
| 948 | } |
| 949 | |
| 950 | // Add arbitrary data to an output section by Input_section. |
| 951 | |
| 952 | void |
| 953 | Output_section::add_output_section_data(Input_section* inp) |
| 954 | { |
| 955 | if (this->input_sections_.empty()) |
| 956 | this->first_input_offset_ = this->data_size(); |
| 957 | |
| 958 | this->input_sections_.push_back(*inp); |
| 959 | |
| 960 | uint64_t addralign = inp->addralign(); |
| 961 | if (addralign > this->addralign_) |
| 962 | this->addralign_ = addralign; |
| 963 | |
| 964 | inp->set_output_section(this); |
| 965 | } |
| 966 | |
| 967 | // Add a merge section to an output section. |
| 968 | |
| 969 | void |
| 970 | Output_section::add_output_merge_section(Output_section_data* posd, |
| 971 | bool is_string, uint64_t entsize) |
| 972 | { |
| 973 | Input_section inp(posd, is_string, entsize); |
| 974 | this->add_output_section_data(&inp); |
| 975 | } |
| 976 | |
| 977 | // Add an input section to a SHF_MERGE section. |
| 978 | |
| 979 | bool |
| 980 | Output_section::add_merge_input_section(Relobj* object, unsigned int shndx, |
| 981 | uint64_t flags, uint64_t entsize, |
| 982 | uint64_t addralign) |
| 983 | { |
| 984 | // We only merge constants if the alignment is not more than the |
| 985 | // entry size. This could be handled, but it's unusual. |
| 986 | if (addralign > entsize) |
| 987 | return false; |
| 988 | |
| 989 | bool is_string = (flags & elfcpp::SHF_STRINGS) != 0; |
| 990 | Input_section_list::iterator p; |
| 991 | for (p = this->input_sections_.begin(); |
| 992 | p != this->input_sections_.end(); |
| 993 | ++p) |
| 994 | if (p->is_merge_section(is_string, entsize)) |
| 995 | break; |
| 996 | |
| 997 | // We handle the actual constant merging in Output_merge_data or |
| 998 | // Output_merge_string_data. |
| 999 | if (p != this->input_sections_.end()) |
| 1000 | p->add_input_section(object, shndx); |
| 1001 | else |
| 1002 | { |
| 1003 | Output_section_data* posd; |
| 1004 | if (!is_string) |
| 1005 | posd = new Output_merge_data(entsize); |
| 1006 | else if (entsize == 1) |
| 1007 | posd = new Output_merge_string<char>(); |
| 1008 | else if (entsize == 2) |
| 1009 | posd = new Output_merge_string<uint16_t>(); |
| 1010 | else if (entsize == 4) |
| 1011 | posd = new Output_merge_string<uint32_t>(); |
| 1012 | else |
| 1013 | return false; |
| 1014 | |
| 1015 | this->add_output_merge_section(posd, is_string, entsize); |
| 1016 | posd->add_input_section(object, shndx); |
| 1017 | } |
| 1018 | |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
| 1022 | // Return the output virtual address of OFFSET relative to the start |
| 1023 | // of input section SHNDX in object OBJECT. |
| 1024 | |
| 1025 | uint64_t |
| 1026 | Output_section::output_address(const Relobj* object, unsigned int shndx, |
| 1027 | off_t offset) const |
| 1028 | { |
| 1029 | uint64_t addr = this->address() + this->first_input_offset_; |
| 1030 | for (Input_section_list::const_iterator p = this->input_sections_.begin(); |
| 1031 | p != this->input_sections_.end(); |
| 1032 | ++p) |
| 1033 | { |
| 1034 | addr = align_address(addr, p->addralign()); |
| 1035 | uint64_t output; |
| 1036 | if (p->output_address(object, shndx, offset, addr, &output)) |
| 1037 | return output; |
| 1038 | addr += p->data_size(); |
| 1039 | } |
| 1040 | |
| 1041 | // If we get here, it means that we don't know the mapping for this |
| 1042 | // input section. This might happen in principle if |
| 1043 | // add_input_section were called before add_output_section_data. |
| 1044 | // But it should never actually happen. |
| 1045 | |
| 1046 | gold_unreachable(); |
| 1047 | } |
| 1048 | |
| 1049 | // Set the address of an Output_section. This is where we handle |
| 1050 | // setting the addresses of any Output_section_data objects. |
| 1051 | |
| 1052 | void |
| 1053 | Output_section::do_set_address(uint64_t address, off_t startoff) |
| 1054 | { |
| 1055 | if (this->input_sections_.empty()) |
| 1056 | return; |
| 1057 | |
| 1058 | off_t off = startoff + this->first_input_offset_; |
| 1059 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
| 1060 | p != this->input_sections_.end(); |
| 1061 | ++p) |
| 1062 | { |
| 1063 | off = align_address(off, p->addralign()); |
| 1064 | p->set_address(address + (off - startoff), off, startoff); |
| 1065 | off += p->data_size(); |
| 1066 | } |
| 1067 | |
| 1068 | this->set_data_size(off - startoff); |
| 1069 | } |
| 1070 | |
| 1071 | // Write the section header to *OSHDR. |
| 1072 | |
| 1073 | template<int size, bool big_endian> |
| 1074 | void |
| 1075 | Output_section::write_header(const Layout* layout, |
| 1076 | const Stringpool* secnamepool, |
| 1077 | elfcpp::Shdr_write<size, big_endian>* oshdr) const |
| 1078 | { |
| 1079 | oshdr->put_sh_name(secnamepool->get_offset(this->name_)); |
| 1080 | oshdr->put_sh_type(this->type_); |
| 1081 | oshdr->put_sh_flags(this->flags_); |
| 1082 | oshdr->put_sh_addr(this->address()); |
| 1083 | oshdr->put_sh_offset(this->offset()); |
| 1084 | oshdr->put_sh_size(this->data_size()); |
| 1085 | if (this->link_section_ != NULL) |
| 1086 | oshdr->put_sh_link(this->link_section_->out_shndx()); |
| 1087 | else if (this->should_link_to_symtab_) |
| 1088 | oshdr->put_sh_link(layout->symtab_section()->out_shndx()); |
| 1089 | else if (this->should_link_to_dynsym_) |
| 1090 | oshdr->put_sh_link(layout->dynsym_section()->out_shndx()); |
| 1091 | else |
| 1092 | oshdr->put_sh_link(this->link_); |
| 1093 | if (this->info_section_ != NULL) |
| 1094 | oshdr->put_sh_info(this->info_section_->out_shndx()); |
| 1095 | else |
| 1096 | oshdr->put_sh_info(this->info_); |
| 1097 | oshdr->put_sh_addralign(this->addralign_); |
| 1098 | oshdr->put_sh_entsize(this->entsize_); |
| 1099 | } |
| 1100 | |
| 1101 | // Write out the data. For input sections the data is written out by |
| 1102 | // Object::relocate, but we have to handle Output_section_data objects |
| 1103 | // here. |
| 1104 | |
| 1105 | void |
| 1106 | Output_section::do_write(Output_file* of) |
| 1107 | { |
| 1108 | for (Input_section_list::iterator p = this->input_sections_.begin(); |
| 1109 | p != this->input_sections_.end(); |
| 1110 | ++p) |
| 1111 | p->write(of); |
| 1112 | } |
| 1113 | |
| 1114 | // Output segment methods. |
| 1115 | |
| 1116 | Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) |
| 1117 | : output_data_(), |
| 1118 | output_bss_(), |
| 1119 | vaddr_(0), |
| 1120 | paddr_(0), |
| 1121 | memsz_(0), |
| 1122 | align_(0), |
| 1123 | offset_(0), |
| 1124 | filesz_(0), |
| 1125 | type_(type), |
| 1126 | flags_(flags), |
| 1127 | is_align_known_(false) |
| 1128 | { |
| 1129 | } |
| 1130 | |
| 1131 | // Add an Output_section to an Output_segment. |
| 1132 | |
| 1133 | void |
| 1134 | Output_segment::add_output_section(Output_section* os, |
| 1135 | elfcpp::Elf_Word seg_flags, |
| 1136 | bool front) |
| 1137 | { |
| 1138 | gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); |
| 1139 | gold_assert(!this->is_align_known_); |
| 1140 | |
| 1141 | // Update the segment flags. |
| 1142 | this->flags_ |= seg_flags; |
| 1143 | |
| 1144 | Output_segment::Output_data_list* pdl; |
| 1145 | if (os->type() == elfcpp::SHT_NOBITS) |
| 1146 | pdl = &this->output_bss_; |
| 1147 | else |
| 1148 | pdl = &this->output_data_; |
| 1149 | |
| 1150 | // So that PT_NOTE segments will work correctly, we need to ensure |
| 1151 | // that all SHT_NOTE sections are adjacent. This will normally |
| 1152 | // happen automatically, because all the SHT_NOTE input sections |
| 1153 | // will wind up in the same output section. However, it is possible |
| 1154 | // for multiple SHT_NOTE input sections to have different section |
| 1155 | // flags, and thus be in different output sections, but for the |
| 1156 | // different section flags to map into the same segment flags and |
| 1157 | // thus the same output segment. |
| 1158 | |
| 1159 | // Note that while there may be many input sections in an output |
| 1160 | // section, there are normally only a few output sections in an |
| 1161 | // output segment. This loop is expected to be fast. |
| 1162 | |
| 1163 | if (os->type() == elfcpp::SHT_NOTE && !pdl->empty()) |
| 1164 | { |
| 1165 | Output_segment::Output_data_list::iterator p = pdl->end(); |
| 1166 | do |
| 1167 | { |
| 1168 | --p; |
| 1169 | if ((*p)->is_section_type(elfcpp::SHT_NOTE)) |
| 1170 | { |
| 1171 | // We don't worry about the FRONT parameter. |
| 1172 | ++p; |
| 1173 | pdl->insert(p, os); |
| 1174 | return; |
| 1175 | } |
| 1176 | } |
| 1177 | while (p != pdl->begin()); |
| 1178 | } |
| 1179 | |
| 1180 | // Similarly, so that PT_TLS segments will work, we need to group |
| 1181 | // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special |
| 1182 | // case: we group the SHF_TLS/SHT_NOBITS sections right after the |
| 1183 | // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS |
| 1184 | // correctly. |
| 1185 | if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty()) |
| 1186 | { |
| 1187 | pdl = &this->output_data_; |
| 1188 | bool nobits = os->type() == elfcpp::SHT_NOBITS; |
| 1189 | bool sawtls = false; |
| 1190 | Output_segment::Output_data_list::iterator p = pdl->end(); |
| 1191 | do |
| 1192 | { |
| 1193 | --p; |
| 1194 | bool insert; |
| 1195 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) |
| 1196 | { |
| 1197 | sawtls = true; |
| 1198 | // Put a NOBITS section after the first TLS section. |
| 1199 | // But a PROGBITS section after the first TLS/PROGBITS |
| 1200 | // section. |
| 1201 | insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS); |
| 1202 | } |
| 1203 | else |
| 1204 | { |
| 1205 | // If we've gone past the TLS sections, but we've seen a |
| 1206 | // TLS section, then we need to insert this section now. |
| 1207 | insert = sawtls; |
| 1208 | } |
| 1209 | |
| 1210 | if (insert) |
| 1211 | { |
| 1212 | // We don't worry about the FRONT parameter. |
| 1213 | ++p; |
| 1214 | pdl->insert(p, os); |
| 1215 | return; |
| 1216 | } |
| 1217 | } |
| 1218 | while (p != pdl->begin()); |
| 1219 | |
| 1220 | // There are no TLS sections yet; put this one at the requested |
| 1221 | // location in the section list. |
| 1222 | } |
| 1223 | |
| 1224 | if (front) |
| 1225 | pdl->push_front(os); |
| 1226 | else |
| 1227 | pdl->push_back(os); |
| 1228 | } |
| 1229 | |
| 1230 | // Add an Output_data (which is not an Output_section) to the start of |
| 1231 | // a segment. |
| 1232 | |
| 1233 | void |
| 1234 | Output_segment::add_initial_output_data(Output_data* od) |
| 1235 | { |
| 1236 | gold_assert(!this->is_align_known_); |
| 1237 | this->output_data_.push_front(od); |
| 1238 | } |
| 1239 | |
| 1240 | // Return the maximum alignment of the Output_data in Output_segment. |
| 1241 | // Once we compute this, we prohibit new sections from being added. |
| 1242 | |
| 1243 | uint64_t |
| 1244 | Output_segment::addralign() |
| 1245 | { |
| 1246 | if (!this->is_align_known_) |
| 1247 | { |
| 1248 | uint64_t addralign; |
| 1249 | |
| 1250 | addralign = Output_segment::maximum_alignment(&this->output_data_); |
| 1251 | if (addralign > this->align_) |
| 1252 | this->align_ = addralign; |
| 1253 | |
| 1254 | addralign = Output_segment::maximum_alignment(&this->output_bss_); |
| 1255 | if (addralign > this->align_) |
| 1256 | this->align_ = addralign; |
| 1257 | |
| 1258 | this->is_align_known_ = true; |
| 1259 | } |
| 1260 | |
| 1261 | return this->align_; |
| 1262 | } |
| 1263 | |
| 1264 | // Return the maximum alignment of a list of Output_data. |
| 1265 | |
| 1266 | uint64_t |
| 1267 | Output_segment::maximum_alignment(const Output_data_list* pdl) |
| 1268 | { |
| 1269 | uint64_t ret = 0; |
| 1270 | for (Output_data_list::const_iterator p = pdl->begin(); |
| 1271 | p != pdl->end(); |
| 1272 | ++p) |
| 1273 | { |
| 1274 | uint64_t addralign = (*p)->addralign(); |
| 1275 | if (addralign > ret) |
| 1276 | ret = addralign; |
| 1277 | } |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | // Set the section addresses for an Output_segment. ADDR is the |
| 1282 | // address and *POFF is the file offset. Set the section indexes |
| 1283 | // starting with *PSHNDX. Return the address of the immediately |
| 1284 | // following segment. Update *POFF and *PSHNDX. |
| 1285 | |
| 1286 | uint64_t |
| 1287 | Output_segment::set_section_addresses(uint64_t addr, off_t* poff, |
| 1288 | unsigned int* pshndx) |
| 1289 | { |
| 1290 | gold_assert(this->type_ == elfcpp::PT_LOAD); |
| 1291 | |
| 1292 | this->vaddr_ = addr; |
| 1293 | this->paddr_ = addr; |
| 1294 | |
| 1295 | off_t orig_off = *poff; |
| 1296 | this->offset_ = orig_off; |
| 1297 | |
| 1298 | *poff = align_address(*poff, this->addralign()); |
| 1299 | |
| 1300 | addr = this->set_section_list_addresses(&this->output_data_, addr, poff, |
| 1301 | pshndx); |
| 1302 | this->filesz_ = *poff - orig_off; |
| 1303 | |
| 1304 | off_t off = *poff; |
| 1305 | |
| 1306 | uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr, |
| 1307 | poff, pshndx); |
| 1308 | this->memsz_ = *poff - orig_off; |
| 1309 | |
| 1310 | // Ignore the file offset adjustments made by the BSS Output_data |
| 1311 | // objects. |
| 1312 | *poff = off; |
| 1313 | |
| 1314 | return ret; |
| 1315 | } |
| 1316 | |
| 1317 | // Set the addresses and file offsets in a list of Output_data |
| 1318 | // structures. |
| 1319 | |
| 1320 | uint64_t |
| 1321 | Output_segment::set_section_list_addresses(Output_data_list* pdl, |
| 1322 | uint64_t addr, off_t* poff, |
| 1323 | unsigned int* pshndx) |
| 1324 | { |
| 1325 | off_t startoff = *poff; |
| 1326 | |
| 1327 | off_t off = startoff; |
| 1328 | for (Output_data_list::iterator p = pdl->begin(); |
| 1329 | p != pdl->end(); |
| 1330 | ++p) |
| 1331 | { |
| 1332 | off = align_address(off, (*p)->addralign()); |
| 1333 | (*p)->set_address(addr + (off - startoff), off); |
| 1334 | |
| 1335 | // Unless this is a PT_TLS segment, we want to ignore the size |
| 1336 | // of a SHF_TLS/SHT_NOBITS section. Such a section does not |
| 1337 | // affect the size of a PT_LOAD segment. |
| 1338 | if (this->type_ == elfcpp::PT_TLS |
| 1339 | || !(*p)->is_section_flag_set(elfcpp::SHF_TLS) |
| 1340 | || !(*p)->is_section_type(elfcpp::SHT_NOBITS)) |
| 1341 | off += (*p)->data_size(); |
| 1342 | |
| 1343 | if ((*p)->is_section()) |
| 1344 | { |
| 1345 | (*p)->set_out_shndx(*pshndx); |
| 1346 | ++*pshndx; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | *poff = off; |
| 1351 | return addr + (off - startoff); |
| 1352 | } |
| 1353 | |
| 1354 | // For a non-PT_LOAD segment, set the offset from the sections, if |
| 1355 | // any. |
| 1356 | |
| 1357 | void |
| 1358 | Output_segment::set_offset() |
| 1359 | { |
| 1360 | gold_assert(this->type_ != elfcpp::PT_LOAD); |
| 1361 | |
| 1362 | if (this->output_data_.empty() && this->output_bss_.empty()) |
| 1363 | { |
| 1364 | this->vaddr_ = 0; |
| 1365 | this->paddr_ = 0; |
| 1366 | this->memsz_ = 0; |
| 1367 | this->align_ = 0; |
| 1368 | this->offset_ = 0; |
| 1369 | this->filesz_ = 0; |
| 1370 | return; |
| 1371 | } |
| 1372 | |
| 1373 | const Output_data* first; |
| 1374 | if (this->output_data_.empty()) |
| 1375 | first = this->output_bss_.front(); |
| 1376 | else |
| 1377 | first = this->output_data_.front(); |
| 1378 | this->vaddr_ = first->address(); |
| 1379 | this->paddr_ = this->vaddr_; |
| 1380 | this->offset_ = first->offset(); |
| 1381 | |
| 1382 | if (this->output_data_.empty()) |
| 1383 | this->filesz_ = 0; |
| 1384 | else |
| 1385 | { |
| 1386 | const Output_data* last_data = this->output_data_.back(); |
| 1387 | this->filesz_ = (last_data->address() |
| 1388 | + last_data->data_size() |
| 1389 | - this->vaddr_); |
| 1390 | } |
| 1391 | |
| 1392 | const Output_data* last; |
| 1393 | if (this->output_bss_.empty()) |
| 1394 | last = this->output_data_.back(); |
| 1395 | else |
| 1396 | last = this->output_bss_.back(); |
| 1397 | this->memsz_ = (last->address() |
| 1398 | + last->data_size() |
| 1399 | - this->vaddr_); |
| 1400 | } |
| 1401 | |
| 1402 | // Return the number of Output_sections in an Output_segment. |
| 1403 | |
| 1404 | unsigned int |
| 1405 | Output_segment::output_section_count() const |
| 1406 | { |
| 1407 | return (this->output_section_count_list(&this->output_data_) |
| 1408 | + this->output_section_count_list(&this->output_bss_)); |
| 1409 | } |
| 1410 | |
| 1411 | // Return the number of Output_sections in an Output_data_list. |
| 1412 | |
| 1413 | unsigned int |
| 1414 | Output_segment::output_section_count_list(const Output_data_list* pdl) const |
| 1415 | { |
| 1416 | unsigned int count = 0; |
| 1417 | for (Output_data_list::const_iterator p = pdl->begin(); |
| 1418 | p != pdl->end(); |
| 1419 | ++p) |
| 1420 | { |
| 1421 | if ((*p)->is_section()) |
| 1422 | ++count; |
| 1423 | } |
| 1424 | return count; |
| 1425 | } |
| 1426 | |
| 1427 | // Write the segment data into *OPHDR. |
| 1428 | |
| 1429 | template<int size, bool big_endian> |
| 1430 | void |
| 1431 | Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr) |
| 1432 | { |
| 1433 | ophdr->put_p_type(this->type_); |
| 1434 | ophdr->put_p_offset(this->offset_); |
| 1435 | ophdr->put_p_vaddr(this->vaddr_); |
| 1436 | ophdr->put_p_paddr(this->paddr_); |
| 1437 | ophdr->put_p_filesz(this->filesz_); |
| 1438 | ophdr->put_p_memsz(this->memsz_); |
| 1439 | ophdr->put_p_flags(this->flags_); |
| 1440 | ophdr->put_p_align(this->addralign()); |
| 1441 | } |
| 1442 | |
| 1443 | // Write the section headers into V. |
| 1444 | |
| 1445 | template<int size, bool big_endian> |
| 1446 | unsigned char* |
| 1447 | Output_segment::write_section_headers(const Layout* layout, |
| 1448 | const Stringpool* secnamepool, |
| 1449 | unsigned char* v, |
| 1450 | unsigned int *pshndx |
| 1451 | ACCEPT_SIZE_ENDIAN) const |
| 1452 | { |
| 1453 | // Every section that is attached to a segment must be attached to a |
| 1454 | // PT_LOAD segment, so we only write out section headers for PT_LOAD |
| 1455 | // segments. |
| 1456 | if (this->type_ != elfcpp::PT_LOAD) |
| 1457 | return v; |
| 1458 | |
| 1459 | v = this->write_section_headers_list |
| 1460 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 1461 | layout, secnamepool, &this->output_data_, v, pshndx |
| 1462 | SELECT_SIZE_ENDIAN(size, big_endian)); |
| 1463 | v = this->write_section_headers_list |
| 1464 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 1465 | layout, secnamepool, &this->output_bss_, v, pshndx |
| 1466 | SELECT_SIZE_ENDIAN(size, big_endian)); |
| 1467 | return v; |
| 1468 | } |
| 1469 | |
| 1470 | template<int size, bool big_endian> |
| 1471 | unsigned char* |
| 1472 | Output_segment::write_section_headers_list(const Layout* layout, |
| 1473 | const Stringpool* secnamepool, |
| 1474 | const Output_data_list* pdl, |
| 1475 | unsigned char* v, |
| 1476 | unsigned int* pshndx |
| 1477 | ACCEPT_SIZE_ENDIAN) const |
| 1478 | { |
| 1479 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; |
| 1480 | for (Output_data_list::const_iterator p = pdl->begin(); |
| 1481 | p != pdl->end(); |
| 1482 | ++p) |
| 1483 | { |
| 1484 | if ((*p)->is_section()) |
| 1485 | { |
| 1486 | const Output_section* ps = static_cast<const Output_section*>(*p); |
| 1487 | gold_assert(*pshndx == ps->out_shndx()); |
| 1488 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
| 1489 | ps->write_header(layout, secnamepool, &oshdr); |
| 1490 | v += shdr_size; |
| 1491 | ++*pshndx; |
| 1492 | } |
| 1493 | } |
| 1494 | return v; |
| 1495 | } |
| 1496 | |
| 1497 | // Output_file methods. |
| 1498 | |
| 1499 | Output_file::Output_file(const General_options& options) |
| 1500 | : options_(options), |
| 1501 | name_(options.output_file_name()), |
| 1502 | o_(-1), |
| 1503 | file_size_(0), |
| 1504 | base_(NULL) |
| 1505 | { |
| 1506 | } |
| 1507 | |
| 1508 | // Open the output file. |
| 1509 | |
| 1510 | void |
| 1511 | Output_file::open(off_t file_size) |
| 1512 | { |
| 1513 | this->file_size_ = file_size; |
| 1514 | |
| 1515 | int mode = this->options_.is_relocatable() ? 0666 : 0777; |
| 1516 | int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode); |
| 1517 | if (o < 0) |
| 1518 | { |
| 1519 | fprintf(stderr, _("%s: %s: open: %s\n"), |
| 1520 | program_name, this->name_, strerror(errno)); |
| 1521 | gold_exit(false); |
| 1522 | } |
| 1523 | this->o_ = o; |
| 1524 | |
| 1525 | // Write out one byte to make the file the right size. |
| 1526 | if (::lseek(o, file_size - 1, SEEK_SET) < 0) |
| 1527 | { |
| 1528 | fprintf(stderr, _("%s: %s: lseek: %s\n"), |
| 1529 | program_name, this->name_, strerror(errno)); |
| 1530 | gold_exit(false); |
| 1531 | } |
| 1532 | char b = 0; |
| 1533 | if (::write(o, &b, 1) != 1) |
| 1534 | { |
| 1535 | fprintf(stderr, _("%s: %s: write: %s\n"), |
| 1536 | program_name, this->name_, strerror(errno)); |
| 1537 | gold_exit(false); |
| 1538 | } |
| 1539 | |
| 1540 | // Map the file into memory. |
| 1541 | void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE, |
| 1542 | MAP_SHARED, o, 0); |
| 1543 | if (base == MAP_FAILED) |
| 1544 | { |
| 1545 | fprintf(stderr, _("%s: %s: mmap: %s\n"), |
| 1546 | program_name, this->name_, strerror(errno)); |
| 1547 | gold_exit(false); |
| 1548 | } |
| 1549 | this->base_ = static_cast<unsigned char*>(base); |
| 1550 | } |
| 1551 | |
| 1552 | // Close the output file. |
| 1553 | |
| 1554 | void |
| 1555 | Output_file::close() |
| 1556 | { |
| 1557 | if (::munmap(this->base_, this->file_size_) < 0) |
| 1558 | { |
| 1559 | fprintf(stderr, _("%s: %s: munmap: %s\n"), |
| 1560 | program_name, this->name_, strerror(errno)); |
| 1561 | gold_exit(false); |
| 1562 | } |
| 1563 | this->base_ = NULL; |
| 1564 | |
| 1565 | if (::close(this->o_) < 0) |
| 1566 | { |
| 1567 | fprintf(stderr, _("%s: %s: close: %s\n"), |
| 1568 | program_name, this->name_, strerror(errno)); |
| 1569 | gold_exit(false); |
| 1570 | } |
| 1571 | this->o_ = -1; |
| 1572 | } |
| 1573 | |
| 1574 | // Instantiate the templates we need. We could use the configure |
| 1575 | // script to restrict this to only the ones for implemented targets. |
| 1576 | |
| 1577 | #ifdef HAVE_TARGET_32_LITTLE |
| 1578 | template |
| 1579 | off_t |
| 1580 | Output_section::add_input_section<32, false>( |
| 1581 | Relobj* object, |
| 1582 | unsigned int shndx, |
| 1583 | const char* secname, |
| 1584 | const elfcpp::Shdr<32, false>& shdr); |
| 1585 | #endif |
| 1586 | |
| 1587 | #ifdef HAVE_TARGET_32_BIG |
| 1588 | template |
| 1589 | off_t |
| 1590 | Output_section::add_input_section<32, true>( |
| 1591 | Relobj* object, |
| 1592 | unsigned int shndx, |
| 1593 | const char* secname, |
| 1594 | const elfcpp::Shdr<32, true>& shdr); |
| 1595 | #endif |
| 1596 | |
| 1597 | #ifdef HAVE_TARGET_64_LITTLE |
| 1598 | template |
| 1599 | off_t |
| 1600 | Output_section::add_input_section<64, false>( |
| 1601 | Relobj* object, |
| 1602 | unsigned int shndx, |
| 1603 | const char* secname, |
| 1604 | const elfcpp::Shdr<64, false>& shdr); |
| 1605 | #endif |
| 1606 | |
| 1607 | #ifdef HAVE_TARGET_64_BIG |
| 1608 | template |
| 1609 | off_t |
| 1610 | Output_section::add_input_section<64, true>( |
| 1611 | Relobj* object, |
| 1612 | unsigned int shndx, |
| 1613 | const char* secname, |
| 1614 | const elfcpp::Shdr<64, true>& shdr); |
| 1615 | #endif |
| 1616 | |
| 1617 | #ifdef HAVE_TARGET_32_LITTLE |
| 1618 | template |
| 1619 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>; |
| 1620 | #endif |
| 1621 | |
| 1622 | #ifdef HAVE_TARGET_32_BIG |
| 1623 | template |
| 1624 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>; |
| 1625 | #endif |
| 1626 | |
| 1627 | #ifdef HAVE_TARGET_64_LITTLE |
| 1628 | template |
| 1629 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>; |
| 1630 | #endif |
| 1631 | |
| 1632 | #ifdef HAVE_TARGET_64_BIG |
| 1633 | template |
| 1634 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>; |
| 1635 | #endif |
| 1636 | |
| 1637 | #ifdef HAVE_TARGET_32_LITTLE |
| 1638 | template |
| 1639 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>; |
| 1640 | #endif |
| 1641 | |
| 1642 | #ifdef HAVE_TARGET_32_BIG |
| 1643 | template |
| 1644 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>; |
| 1645 | #endif |
| 1646 | |
| 1647 | #ifdef HAVE_TARGET_64_LITTLE |
| 1648 | template |
| 1649 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>; |
| 1650 | #endif |
| 1651 | |
| 1652 | #ifdef HAVE_TARGET_64_BIG |
| 1653 | template |
| 1654 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>; |
| 1655 | #endif |
| 1656 | |
| 1657 | #ifdef HAVE_TARGET_32_LITTLE |
| 1658 | template |
| 1659 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>; |
| 1660 | #endif |
| 1661 | |
| 1662 | #ifdef HAVE_TARGET_32_BIG |
| 1663 | template |
| 1664 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>; |
| 1665 | #endif |
| 1666 | |
| 1667 | #ifdef HAVE_TARGET_64_LITTLE |
| 1668 | template |
| 1669 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>; |
| 1670 | #endif |
| 1671 | |
| 1672 | #ifdef HAVE_TARGET_64_BIG |
| 1673 | template |
| 1674 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>; |
| 1675 | #endif |
| 1676 | |
| 1677 | #ifdef HAVE_TARGET_32_LITTLE |
| 1678 | template |
| 1679 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>; |
| 1680 | #endif |
| 1681 | |
| 1682 | #ifdef HAVE_TARGET_32_BIG |
| 1683 | template |
| 1684 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>; |
| 1685 | #endif |
| 1686 | |
| 1687 | #ifdef HAVE_TARGET_64_LITTLE |
| 1688 | template |
| 1689 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>; |
| 1690 | #endif |
| 1691 | |
| 1692 | #ifdef HAVE_TARGET_64_BIG |
| 1693 | template |
| 1694 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>; |
| 1695 | #endif |
| 1696 | |
| 1697 | #ifdef HAVE_TARGET_32_LITTLE |
| 1698 | template |
| 1699 | class Output_data_got<32, false>; |
| 1700 | #endif |
| 1701 | |
| 1702 | #ifdef HAVE_TARGET_32_BIG |
| 1703 | template |
| 1704 | class Output_data_got<32, true>; |
| 1705 | #endif |
| 1706 | |
| 1707 | #ifdef HAVE_TARGET_64_LITTLE |
| 1708 | template |
| 1709 | class Output_data_got<64, false>; |
| 1710 | #endif |
| 1711 | |
| 1712 | #ifdef HAVE_TARGET_64_BIG |
| 1713 | template |
| 1714 | class Output_data_got<64, true>; |
| 1715 | #endif |
| 1716 | |
| 1717 | } // End namespace gold. |