| 1 | // object.cc -- support for an object file for linking in gold |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008, 2009 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 <cerrno> |
| 26 | #include <cstring> |
| 27 | #include <cstdarg> |
| 28 | #include "demangle.h" |
| 29 | #include "libiberty.h" |
| 30 | |
| 31 | #include "gc.h" |
| 32 | #include "target-select.h" |
| 33 | #include "dwarf_reader.h" |
| 34 | #include "layout.h" |
| 35 | #include "output.h" |
| 36 | #include "symtab.h" |
| 37 | #include "cref.h" |
| 38 | #include "reloc.h" |
| 39 | #include "object.h" |
| 40 | #include "dynobj.h" |
| 41 | #include "plugin.h" |
| 42 | |
| 43 | namespace gold |
| 44 | { |
| 45 | |
| 46 | // Class Xindex. |
| 47 | |
| 48 | // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX |
| 49 | // section and read it in. SYMTAB_SHNDX is the index of the symbol |
| 50 | // table we care about. |
| 51 | |
| 52 | template<int size, bool big_endian> |
| 53 | void |
| 54 | Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx) |
| 55 | { |
| 56 | if (!this->symtab_xindex_.empty()) |
| 57 | return; |
| 58 | |
| 59 | gold_assert(symtab_shndx != 0); |
| 60 | |
| 61 | // Look through the sections in reverse order, on the theory that it |
| 62 | // is more likely to be near the end than the beginning. |
| 63 | unsigned int i = object->shnum(); |
| 64 | while (i > 0) |
| 65 | { |
| 66 | --i; |
| 67 | if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX |
| 68 | && this->adjust_shndx(object->section_link(i)) == symtab_shndx) |
| 69 | { |
| 70 | this->read_symtab_xindex<size, big_endian>(object, i, NULL); |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | object->error(_("missing SHT_SYMTAB_SHNDX section")); |
| 76 | } |
| 77 | |
| 78 | // Read in the symtab_xindex_ array, given the section index of the |
| 79 | // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the |
| 80 | // section headers. |
| 81 | |
| 82 | template<int size, bool big_endian> |
| 83 | void |
| 84 | Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx, |
| 85 | const unsigned char* pshdrs) |
| 86 | { |
| 87 | section_size_type bytecount; |
| 88 | const unsigned char* contents; |
| 89 | if (pshdrs == NULL) |
| 90 | contents = object->section_contents(xindex_shndx, &bytecount, false); |
| 91 | else |
| 92 | { |
| 93 | const unsigned char* p = (pshdrs |
| 94 | + (xindex_shndx |
| 95 | * elfcpp::Elf_sizes<size>::shdr_size)); |
| 96 | typename elfcpp::Shdr<size, big_endian> shdr(p); |
| 97 | bytecount = convert_to_section_size_type(shdr.get_sh_size()); |
| 98 | contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false); |
| 99 | } |
| 100 | |
| 101 | gold_assert(this->symtab_xindex_.empty()); |
| 102 | this->symtab_xindex_.reserve(bytecount / 4); |
| 103 | for (section_size_type i = 0; i < bytecount; i += 4) |
| 104 | { |
| 105 | unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i); |
| 106 | // We preadjust the section indexes we save. |
| 107 | this->symtab_xindex_.push_back(this->adjust_shndx(shndx)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Symbol symndx has a section of SHN_XINDEX; return the real section |
| 112 | // index. |
| 113 | |
| 114 | unsigned int |
| 115 | Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx) |
| 116 | { |
| 117 | if (symndx >= this->symtab_xindex_.size()) |
| 118 | { |
| 119 | object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"), |
| 120 | symndx); |
| 121 | return elfcpp::SHN_UNDEF; |
| 122 | } |
| 123 | unsigned int shndx = this->symtab_xindex_[symndx]; |
| 124 | if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum()) |
| 125 | { |
| 126 | object->error(_("extended index for symbol %u out of range: %u"), |
| 127 | symndx, shndx); |
| 128 | return elfcpp::SHN_UNDEF; |
| 129 | } |
| 130 | return shndx; |
| 131 | } |
| 132 | |
| 133 | // Class Object. |
| 134 | |
| 135 | // Report an error for this object file. This is used by the |
| 136 | // elfcpp::Elf_file interface, and also called by the Object code |
| 137 | // itself. |
| 138 | |
| 139 | void |
| 140 | Object::error(const char* format, ...) const |
| 141 | { |
| 142 | va_list args; |
| 143 | va_start(args, format); |
| 144 | char* buf = NULL; |
| 145 | if (vasprintf(&buf, format, args) < 0) |
| 146 | gold_nomem(); |
| 147 | va_end(args); |
| 148 | gold_error(_("%s: %s"), this->name().c_str(), buf); |
| 149 | free(buf); |
| 150 | } |
| 151 | |
| 152 | // Return a view of the contents of a section. |
| 153 | |
| 154 | const unsigned char* |
| 155 | Object::section_contents(unsigned int shndx, section_size_type* plen, |
| 156 | bool cache) |
| 157 | { |
| 158 | Location loc(this->do_section_contents(shndx)); |
| 159 | *plen = convert_to_section_size_type(loc.data_size); |
| 160 | if (*plen == 0) |
| 161 | { |
| 162 | static const unsigned char empty[1] = { '\0' }; |
| 163 | return empty; |
| 164 | } |
| 165 | return this->get_view(loc.file_offset, *plen, true, cache); |
| 166 | } |
| 167 | |
| 168 | // Read the section data into SD. This is code common to Sized_relobj |
| 169 | // and Sized_dynobj, so we put it into Object. |
| 170 | |
| 171 | template<int size, bool big_endian> |
| 172 | void |
| 173 | Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file, |
| 174 | Read_symbols_data* sd) |
| 175 | { |
| 176 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; |
| 177 | |
| 178 | // Read the section headers. |
| 179 | const off_t shoff = elf_file->shoff(); |
| 180 | const unsigned int shnum = this->shnum(); |
| 181 | sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, |
| 182 | true, true); |
| 183 | |
| 184 | // Read the section names. |
| 185 | const unsigned char* pshdrs = sd->section_headers->data(); |
| 186 | const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size; |
| 187 | typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames); |
| 188 | |
| 189 | if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB) |
| 190 | this->error(_("section name section has wrong type: %u"), |
| 191 | static_cast<unsigned int>(shdrnames.get_sh_type())); |
| 192 | |
| 193 | sd->section_names_size = |
| 194 | convert_to_section_size_type(shdrnames.get_sh_size()); |
| 195 | sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(), |
| 196 | sd->section_names_size, false, |
| 197 | false); |
| 198 | } |
| 199 | |
| 200 | // If NAME is the name of a special .gnu.warning section, arrange for |
| 201 | // the warning to be issued. SHNDX is the section index. Return |
| 202 | // whether it is a warning section. |
| 203 | |
| 204 | bool |
| 205 | Object::handle_gnu_warning_section(const char* name, unsigned int shndx, |
| 206 | Symbol_table* symtab) |
| 207 | { |
| 208 | const char warn_prefix[] = ".gnu.warning."; |
| 209 | const int warn_prefix_len = sizeof warn_prefix - 1; |
| 210 | if (strncmp(name, warn_prefix, warn_prefix_len) == 0) |
| 211 | { |
| 212 | // Read the section contents to get the warning text. It would |
| 213 | // be nicer if we only did this if we have to actually issue a |
| 214 | // warning. Unfortunately, warnings are issued as we relocate |
| 215 | // sections. That means that we can not lock the object then, |
| 216 | // as we might try to issue the same warning multiple times |
| 217 | // simultaneously. |
| 218 | section_size_type len; |
| 219 | const unsigned char* contents = this->section_contents(shndx, &len, |
| 220 | false); |
| 221 | if (len == 0) |
| 222 | { |
| 223 | const char* warning = name + warn_prefix_len; |
| 224 | contents = reinterpret_cast<const unsigned char*>(warning); |
| 225 | len = strlen(warning); |
| 226 | } |
| 227 | std::string warning(reinterpret_cast<const char*>(contents), len); |
| 228 | symtab->add_warning(name + warn_prefix_len, this, warning); |
| 229 | return true; |
| 230 | } |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // Class Relobj |
| 235 | |
| 236 | // To copy the symbols data read from the file to a local data structure. |
| 237 | // This function is called from do_layout only while doing garbage |
| 238 | // collection. |
| 239 | |
| 240 | void |
| 241 | Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd, |
| 242 | unsigned int section_header_size) |
| 243 | { |
| 244 | gc_sd->section_headers_data = |
| 245 | new unsigned char[(section_header_size)]; |
| 246 | memcpy(gc_sd->section_headers_data, sd->section_headers->data(), |
| 247 | section_header_size); |
| 248 | gc_sd->section_names_data = |
| 249 | new unsigned char[sd->section_names_size]; |
| 250 | memcpy(gc_sd->section_names_data, sd->section_names->data(), |
| 251 | sd->section_names_size); |
| 252 | gc_sd->section_names_size = sd->section_names_size; |
| 253 | if (sd->symbols != NULL) |
| 254 | { |
| 255 | gc_sd->symbols_data = |
| 256 | new unsigned char[sd->symbols_size]; |
| 257 | memcpy(gc_sd->symbols_data, sd->symbols->data(), |
| 258 | sd->symbols_size); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | gc_sd->symbols_data = NULL; |
| 263 | } |
| 264 | gc_sd->symbols_size = sd->symbols_size; |
| 265 | gc_sd->external_symbols_offset = sd->external_symbols_offset; |
| 266 | if (sd->symbol_names != NULL) |
| 267 | { |
| 268 | gc_sd->symbol_names_data = |
| 269 | new unsigned char[sd->symbol_names_size]; |
| 270 | memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(), |
| 271 | sd->symbol_names_size); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | gc_sd->symbol_names_data = NULL; |
| 276 | } |
| 277 | gc_sd->symbol_names_size = sd->symbol_names_size; |
| 278 | } |
| 279 | |
| 280 | // This function determines if a particular section name must be included |
| 281 | // in the link. This is used during garbage collection to determine the |
| 282 | // roots of the worklist. |
| 283 | |
| 284 | bool |
| 285 | Relobj::is_section_name_included(const char* name) |
| 286 | { |
| 287 | if (is_prefix_of(".ctors", name) |
| 288 | || is_prefix_of(".dtors", name) |
| 289 | || is_prefix_of(".note", name) |
| 290 | || is_prefix_of(".init", name) |
| 291 | || is_prefix_of(".fini", name) |
| 292 | || is_prefix_of(".gcc_except_table", name) |
| 293 | || is_prefix_of(".jcr", name) |
| 294 | || is_prefix_of(".preinit_array", name) |
| 295 | || (is_prefix_of(".text", name) |
| 296 | && strstr(name, "personality")) |
| 297 | || (is_prefix_of(".data", name) |
| 298 | && strstr(name, "personality")) |
| 299 | || (is_prefix_of(".gnu.linkonce.d", name) && |
| 300 | strstr(name, "personality"))) |
| 301 | { |
| 302 | return true; |
| 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | // Class Sized_relobj. |
| 308 | |
| 309 | template<int size, bool big_endian> |
| 310 | Sized_relobj<size, big_endian>::Sized_relobj( |
| 311 | const std::string& name, |
| 312 | Input_file* input_file, |
| 313 | off_t offset, |
| 314 | const elfcpp::Ehdr<size, big_endian>& ehdr) |
| 315 | : Relobj(name, input_file, offset), |
| 316 | elf_file_(this, ehdr), |
| 317 | symtab_shndx_(-1U), |
| 318 | local_symbol_count_(0), |
| 319 | output_local_symbol_count_(0), |
| 320 | output_local_dynsym_count_(0), |
| 321 | symbols_(), |
| 322 | defined_count_(0), |
| 323 | local_symbol_offset_(0), |
| 324 | local_dynsym_offset_(0), |
| 325 | local_values_(), |
| 326 | local_got_offsets_(), |
| 327 | kept_comdat_sections_(), |
| 328 | has_eh_frame_(false), |
| 329 | discarded_eh_frame_shndx_(-1U) |
| 330 | { |
| 331 | } |
| 332 | |
| 333 | template<int size, bool big_endian> |
| 334 | Sized_relobj<size, big_endian>::~Sized_relobj() |
| 335 | { |
| 336 | } |
| 337 | |
| 338 | // Set up an object file based on the file header. This sets up the |
| 339 | // target and reads the section information. |
| 340 | |
| 341 | template<int size, bool big_endian> |
| 342 | void |
| 343 | Sized_relobj<size, big_endian>::setup(Target *target) |
| 344 | { |
| 345 | this->set_target(target); |
| 346 | |
| 347 | const unsigned int shnum = this->elf_file_.shnum(); |
| 348 | this->set_shnum(shnum); |
| 349 | } |
| 350 | |
| 351 | // Find the SHT_SYMTAB section, given the section headers. The ELF |
| 352 | // standard says that maybe in the future there can be more than one |
| 353 | // SHT_SYMTAB section. Until somebody figures out how that could |
| 354 | // work, we assume there is only one. |
| 355 | |
| 356 | template<int size, bool big_endian> |
| 357 | void |
| 358 | Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs) |
| 359 | { |
| 360 | const unsigned int shnum = this->shnum(); |
| 361 | this->symtab_shndx_ = 0; |
| 362 | if (shnum > 0) |
| 363 | { |
| 364 | // Look through the sections in reverse order, since gas tends |
| 365 | // to put the symbol table at the end. |
| 366 | const unsigned char* p = pshdrs + shnum * This::shdr_size; |
| 367 | unsigned int i = shnum; |
| 368 | unsigned int xindex_shndx = 0; |
| 369 | unsigned int xindex_link = 0; |
| 370 | while (i > 0) |
| 371 | { |
| 372 | --i; |
| 373 | p -= This::shdr_size; |
| 374 | typename This::Shdr shdr(p); |
| 375 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) |
| 376 | { |
| 377 | this->symtab_shndx_ = i; |
| 378 | if (xindex_shndx > 0 && xindex_link == i) |
| 379 | { |
| 380 | Xindex* xindex = |
| 381 | new Xindex(this->elf_file_.large_shndx_offset()); |
| 382 | xindex->read_symtab_xindex<size, big_endian>(this, |
| 383 | xindex_shndx, |
| 384 | pshdrs); |
| 385 | this->set_xindex(xindex); |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | // Try to pick up the SHT_SYMTAB_SHNDX section, if there is |
| 391 | // one. This will work if it follows the SHT_SYMTAB |
| 392 | // section. |
| 393 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX) |
| 394 | { |
| 395 | xindex_shndx = i; |
| 396 | xindex_link = this->adjust_shndx(shdr.get_sh_link()); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // Return the Xindex structure to use for object with lots of |
| 403 | // sections. |
| 404 | |
| 405 | template<int size, bool big_endian> |
| 406 | Xindex* |
| 407 | Sized_relobj<size, big_endian>::do_initialize_xindex() |
| 408 | { |
| 409 | gold_assert(this->symtab_shndx_ != -1U); |
| 410 | Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset()); |
| 411 | xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_); |
| 412 | return xindex; |
| 413 | } |
| 414 | |
| 415 | // Return whether SHDR has the right type and flags to be a GNU |
| 416 | // .eh_frame section. |
| 417 | |
| 418 | template<int size, bool big_endian> |
| 419 | bool |
| 420 | Sized_relobj<size, big_endian>::check_eh_frame_flags( |
| 421 | const elfcpp::Shdr<size, big_endian>* shdr) const |
| 422 | { |
| 423 | return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS |
| 424 | && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0); |
| 425 | } |
| 426 | |
| 427 | // Return whether there is a GNU .eh_frame section, given the section |
| 428 | // headers and the section names. |
| 429 | |
| 430 | template<int size, bool big_endian> |
| 431 | bool |
| 432 | Sized_relobj<size, big_endian>::find_eh_frame( |
| 433 | const unsigned char* pshdrs, |
| 434 | const char* names, |
| 435 | section_size_type names_size) const |
| 436 | { |
| 437 | const unsigned int shnum = this->shnum(); |
| 438 | const unsigned char* p = pshdrs + This::shdr_size; |
| 439 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) |
| 440 | { |
| 441 | typename This::Shdr shdr(p); |
| 442 | if (this->check_eh_frame_flags(&shdr)) |
| 443 | { |
| 444 | if (shdr.get_sh_name() >= names_size) |
| 445 | { |
| 446 | this->error(_("bad section name offset for section %u: %lu"), |
| 447 | i, static_cast<unsigned long>(shdr.get_sh_name())); |
| 448 | continue; |
| 449 | } |
| 450 | |
| 451 | const char* name = names + shdr.get_sh_name(); |
| 452 | if (strcmp(name, ".eh_frame") == 0) |
| 453 | return true; |
| 454 | } |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // Read the sections and symbols from an object file. |
| 460 | |
| 461 | template<int size, bool big_endian> |
| 462 | void |
| 463 | Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) |
| 464 | { |
| 465 | this->read_section_data(&this->elf_file_, sd); |
| 466 | |
| 467 | const unsigned char* const pshdrs = sd->section_headers->data(); |
| 468 | |
| 469 | this->find_symtab(pshdrs); |
| 470 | |
| 471 | const unsigned char* namesu = sd->section_names->data(); |
| 472 | const char* names = reinterpret_cast<const char*>(namesu); |
| 473 | if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL) |
| 474 | { |
| 475 | if (this->find_eh_frame(pshdrs, names, sd->section_names_size)) |
| 476 | this->has_eh_frame_ = true; |
| 477 | } |
| 478 | |
| 479 | sd->symbols = NULL; |
| 480 | sd->symbols_size = 0; |
| 481 | sd->external_symbols_offset = 0; |
| 482 | sd->symbol_names = NULL; |
| 483 | sd->symbol_names_size = 0; |
| 484 | |
| 485 | if (this->symtab_shndx_ == 0) |
| 486 | { |
| 487 | // No symbol table. Weird but legal. |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | // Get the symbol table section header. |
| 492 | typename This::Shdr symtabshdr(pshdrs |
| 493 | + this->symtab_shndx_ * This::shdr_size); |
| 494 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
| 495 | |
| 496 | // If this object has a .eh_frame section, we need all the symbols. |
| 497 | // Otherwise we only need the external symbols. While it would be |
| 498 | // simpler to just always read all the symbols, I've seen object |
| 499 | // files with well over 2000 local symbols, which for a 64-bit |
| 500 | // object file format is over 5 pages that we don't need to read |
| 501 | // now. |
| 502 | |
| 503 | const int sym_size = This::sym_size; |
| 504 | const unsigned int loccount = symtabshdr.get_sh_info(); |
| 505 | this->local_symbol_count_ = loccount; |
| 506 | this->local_values_.resize(loccount); |
| 507 | section_offset_type locsize = loccount * sym_size; |
| 508 | off_t dataoff = symtabshdr.get_sh_offset(); |
| 509 | section_size_type datasize = |
| 510 | convert_to_section_size_type(symtabshdr.get_sh_size()); |
| 511 | off_t extoff = dataoff + locsize; |
| 512 | section_size_type extsize = datasize - locsize; |
| 513 | |
| 514 | off_t readoff = this->has_eh_frame_ ? dataoff : extoff; |
| 515 | section_size_type readsize = this->has_eh_frame_ ? datasize : extsize; |
| 516 | |
| 517 | if (readsize == 0) |
| 518 | { |
| 519 | // No external symbols. Also weird but also legal. |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false); |
| 524 | |
| 525 | // Read the section header for the symbol names. |
| 526 | unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link()); |
| 527 | if (strtab_shndx >= this->shnum()) |
| 528 | { |
| 529 | this->error(_("invalid symbol table name index: %u"), strtab_shndx); |
| 530 | return; |
| 531 | } |
| 532 | typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size); |
| 533 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) |
| 534 | { |
| 535 | this->error(_("symbol table name section has wrong type: %u"), |
| 536 | static_cast<unsigned int>(strtabshdr.get_sh_type())); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | // Read the symbol names. |
| 541 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), |
| 542 | strtabshdr.get_sh_size(), |
| 543 | false, true); |
| 544 | |
| 545 | sd->symbols = fvsymtab; |
| 546 | sd->symbols_size = readsize; |
| 547 | sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0; |
| 548 | sd->symbol_names = fvstrtab; |
| 549 | sd->symbol_names_size = |
| 550 | convert_to_section_size_type(strtabshdr.get_sh_size()); |
| 551 | } |
| 552 | |
| 553 | // Return the section index of symbol SYM. Set *VALUE to its value in |
| 554 | // the object file. Set *IS_ORDINARY if this is an ordinary section |
| 555 | // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE. |
| 556 | // Note that for a symbol which is not defined in this object file, |
| 557 | // this will set *VALUE to 0 and return SHN_UNDEF; it will not return |
| 558 | // the final value of the symbol in the link. |
| 559 | |
| 560 | template<int size, bool big_endian> |
| 561 | unsigned int |
| 562 | Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym, |
| 563 | Address* value, |
| 564 | bool* is_ordinary) |
| 565 | { |
| 566 | section_size_type symbols_size; |
| 567 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
| 568 | &symbols_size, |
| 569 | false); |
| 570 | |
| 571 | const size_t count = symbols_size / This::sym_size; |
| 572 | gold_assert(sym < count); |
| 573 | |
| 574 | elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size); |
| 575 | *value = elfsym.get_st_value(); |
| 576 | |
| 577 | return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary); |
| 578 | } |
| 579 | |
| 580 | // Return whether to include a section group in the link. LAYOUT is |
| 581 | // used to keep track of which section groups we have already seen. |
| 582 | // INDEX is the index of the section group and SHDR is the section |
| 583 | // header. If we do not want to include this group, we set bits in |
| 584 | // OMIT for each section which should be discarded. |
| 585 | |
| 586 | template<int size, bool big_endian> |
| 587 | bool |
| 588 | Sized_relobj<size, big_endian>::include_section_group( |
| 589 | Symbol_table* symtab, |
| 590 | Layout* layout, |
| 591 | unsigned int index, |
| 592 | const char* name, |
| 593 | const unsigned char* shdrs, |
| 594 | const char* section_names, |
| 595 | section_size_type section_names_size, |
| 596 | std::vector<bool>* omit) |
| 597 | { |
| 598 | // Read the section contents. |
| 599 | typename This::Shdr shdr(shdrs + index * This::shdr_size); |
| 600 | const unsigned char* pcon = this->get_view(shdr.get_sh_offset(), |
| 601 | shdr.get_sh_size(), true, false); |
| 602 | const elfcpp::Elf_Word* pword = |
| 603 | reinterpret_cast<const elfcpp::Elf_Word*>(pcon); |
| 604 | |
| 605 | // The first word contains flags. We only care about COMDAT section |
| 606 | // groups. Other section groups are always included in the link |
| 607 | // just like ordinary sections. |
| 608 | elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword); |
| 609 | |
| 610 | // Look up the group signature, which is the name of a symbol. This |
| 611 | // is a lot of effort to go to to read a string. Why didn't they |
| 612 | // just have the group signature point into the string table, rather |
| 613 | // than indirect through a symbol? |
| 614 | |
| 615 | // Get the appropriate symbol table header (this will normally be |
| 616 | // the single SHT_SYMTAB section, but in principle it need not be). |
| 617 | const unsigned int link = this->adjust_shndx(shdr.get_sh_link()); |
| 618 | typename This::Shdr symshdr(this, this->elf_file_.section_header(link)); |
| 619 | |
| 620 | // Read the symbol table entry. |
| 621 | unsigned int symndx = shdr.get_sh_info(); |
| 622 | if (symndx >= symshdr.get_sh_size() / This::sym_size) |
| 623 | { |
| 624 | this->error(_("section group %u info %u out of range"), |
| 625 | index, symndx); |
| 626 | return false; |
| 627 | } |
| 628 | off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size; |
| 629 | const unsigned char* psym = this->get_view(symoff, This::sym_size, true, |
| 630 | false); |
| 631 | elfcpp::Sym<size, big_endian> sym(psym); |
| 632 | |
| 633 | // Read the symbol table names. |
| 634 | section_size_type symnamelen; |
| 635 | const unsigned char* psymnamesu; |
| 636 | psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()), |
| 637 | &symnamelen, true); |
| 638 | const char* psymnames = reinterpret_cast<const char*>(psymnamesu); |
| 639 | |
| 640 | // Get the section group signature. |
| 641 | if (sym.get_st_name() >= symnamelen) |
| 642 | { |
| 643 | this->error(_("symbol %u name offset %u out of range"), |
| 644 | symndx, sym.get_st_name()); |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | std::string signature(psymnames + sym.get_st_name()); |
| 649 | |
| 650 | // It seems that some versions of gas will create a section group |
| 651 | // associated with a section symbol, and then fail to give a name to |
| 652 | // the section symbol. In such a case, use the name of the section. |
| 653 | if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION) |
| 654 | { |
| 655 | bool is_ordinary; |
| 656 | unsigned int sym_shndx = this->adjust_sym_shndx(symndx, |
| 657 | sym.get_st_shndx(), |
| 658 | &is_ordinary); |
| 659 | if (!is_ordinary || sym_shndx >= this->shnum()) |
| 660 | { |
| 661 | this->error(_("symbol %u invalid section index %u"), |
| 662 | symndx, sym_shndx); |
| 663 | return false; |
| 664 | } |
| 665 | typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size); |
| 666 | if (member_shdr.get_sh_name() < section_names_size) |
| 667 | signature = section_names + member_shdr.get_sh_name(); |
| 668 | } |
| 669 | |
| 670 | // Record this section group in the layout, and see whether we've already |
| 671 | // seen one with the same signature. |
| 672 | bool include_group; |
| 673 | bool is_comdat; |
| 674 | Kept_section* kept_section = NULL; |
| 675 | |
| 676 | if ((flags & elfcpp::GRP_COMDAT) == 0) |
| 677 | { |
| 678 | include_group = true; |
| 679 | is_comdat = false; |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | include_group = layout->find_or_add_kept_section(signature, |
| 684 | this, index, true, |
| 685 | true, &kept_section); |
| 686 | is_comdat = true; |
| 687 | } |
| 688 | |
| 689 | size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word); |
| 690 | |
| 691 | std::vector<unsigned int> shndxes; |
| 692 | bool relocate_group = include_group && parameters->options().relocatable(); |
| 693 | if (relocate_group) |
| 694 | shndxes.reserve(count - 1); |
| 695 | |
| 696 | for (size_t i = 1; i < count; ++i) |
| 697 | { |
| 698 | elfcpp::Elf_Word shndx = |
| 699 | this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i)); |
| 700 | |
| 701 | if (relocate_group) |
| 702 | shndxes.push_back(shndx); |
| 703 | |
| 704 | if (shndx >= this->shnum()) |
| 705 | { |
| 706 | this->error(_("section %u in section group %u out of range"), |
| 707 | shndx, index); |
| 708 | continue; |
| 709 | } |
| 710 | |
| 711 | // Check for an earlier section number, since we're going to get |
| 712 | // it wrong--we may have already decided to include the section. |
| 713 | if (shndx < index) |
| 714 | this->error(_("invalid section group %u refers to earlier section %u"), |
| 715 | index, shndx); |
| 716 | |
| 717 | // Get the name of the member section. |
| 718 | typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size); |
| 719 | if (member_shdr.get_sh_name() >= section_names_size) |
| 720 | { |
| 721 | // This is an error, but it will be diagnosed eventually |
| 722 | // in do_layout, so we don't need to do anything here but |
| 723 | // ignore it. |
| 724 | continue; |
| 725 | } |
| 726 | std::string mname(section_names + member_shdr.get_sh_name()); |
| 727 | |
| 728 | if (include_group) |
| 729 | { |
| 730 | if (is_comdat) |
| 731 | kept_section->add_comdat_section(mname, shndx, |
| 732 | member_shdr.get_sh_size()); |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | (*omit)[shndx] = true; |
| 737 | |
| 738 | if (is_comdat) |
| 739 | { |
| 740 | Relobj* kept_object = kept_section->object(); |
| 741 | if (kept_section->is_comdat()) |
| 742 | { |
| 743 | // Find the corresponding kept section, and store |
| 744 | // that info in the discarded section table. |
| 745 | unsigned int kept_shndx; |
| 746 | uint64_t kept_size; |
| 747 | if (kept_section->find_comdat_section(mname, &kept_shndx, |
| 748 | &kept_size)) |
| 749 | { |
| 750 | // We don't keep a mapping for this section if |
| 751 | // it has a different size. The mapping is only |
| 752 | // used for relocation processing, and we don't |
| 753 | // want to treat the sections as similar if the |
| 754 | // sizes are different. Checking the section |
| 755 | // size is the approach used by the GNU linker. |
| 756 | if (kept_size == member_shdr.get_sh_size()) |
| 757 | this->set_kept_comdat_section(shndx, kept_object, |
| 758 | kept_shndx); |
| 759 | } |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | // The existing section is a linkonce section. Add |
| 764 | // a mapping if there is exactly one section in the |
| 765 | // group (which is true when COUNT == 2) and if it |
| 766 | // is the same size. |
| 767 | if (count == 2 |
| 768 | && (kept_section->linkonce_size() |
| 769 | == member_shdr.get_sh_size())) |
| 770 | this->set_kept_comdat_section(shndx, kept_object, |
| 771 | kept_section->shndx()); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if (relocate_group) |
| 778 | layout->layout_group(symtab, this, index, name, signature.c_str(), |
| 779 | shdr, flags, &shndxes); |
| 780 | |
| 781 | return include_group; |
| 782 | } |
| 783 | |
| 784 | // Whether to include a linkonce section in the link. NAME is the |
| 785 | // name of the section and SHDR is the section header. |
| 786 | |
| 787 | // Linkonce sections are a GNU extension implemented in the original |
| 788 | // GNU linker before section groups were defined. The semantics are |
| 789 | // that we only include one linkonce section with a given name. The |
| 790 | // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME, |
| 791 | // where T is the type of section and SYMNAME is the name of a symbol. |
| 792 | // In an attempt to make linkonce sections interact well with section |
| 793 | // groups, we try to identify SYMNAME and use it like a section group |
| 794 | // signature. We want to block section groups with that signature, |
| 795 | // but not other linkonce sections with that signature. We also use |
| 796 | // the full name of the linkonce section as a normal section group |
| 797 | // signature. |
| 798 | |
| 799 | template<int size, bool big_endian> |
| 800 | bool |
| 801 | Sized_relobj<size, big_endian>::include_linkonce_section( |
| 802 | Layout* layout, |
| 803 | unsigned int index, |
| 804 | const char* name, |
| 805 | const elfcpp::Shdr<size, big_endian>& shdr) |
| 806 | { |
| 807 | typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size(); |
| 808 | // In general the symbol name we want will be the string following |
| 809 | // the last '.'. However, we have to handle the case of |
| 810 | // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by |
| 811 | // some versions of gcc. So we use a heuristic: if the name starts |
| 812 | // with ".gnu.linkonce.t.", we use everything after that. Otherwise |
| 813 | // we look for the last '.'. We can't always simply skip |
| 814 | // ".gnu.linkonce.X", because we have to deal with cases like |
| 815 | // ".gnu.linkonce.d.rel.ro.local". |
| 816 | const char* const linkonce_t = ".gnu.linkonce.t."; |
| 817 | const char* symname; |
| 818 | if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0) |
| 819 | symname = name + strlen(linkonce_t); |
| 820 | else |
| 821 | symname = strrchr(name, '.') + 1; |
| 822 | std::string sig1(symname); |
| 823 | std::string sig2(name); |
| 824 | Kept_section* kept1; |
| 825 | Kept_section* kept2; |
| 826 | bool include1 = layout->find_or_add_kept_section(sig1, this, index, false, |
| 827 | false, &kept1); |
| 828 | bool include2 = layout->find_or_add_kept_section(sig2, this, index, false, |
| 829 | true, &kept2); |
| 830 | |
| 831 | if (!include2) |
| 832 | { |
| 833 | // We are not including this section because we already saw the |
| 834 | // name of the section as a signature. This normally implies |
| 835 | // that the kept section is another linkonce section. If it is |
| 836 | // the same size, record it as the section which corresponds to |
| 837 | // this one. |
| 838 | if (kept2->object() != NULL |
| 839 | && !kept2->is_comdat() |
| 840 | && kept2->linkonce_size() == sh_size) |
| 841 | this->set_kept_comdat_section(index, kept2->object(), kept2->shndx()); |
| 842 | } |
| 843 | else if (!include1) |
| 844 | { |
| 845 | // The section is being discarded on the basis of its symbol |
| 846 | // name. This means that the corresponding kept section was |
| 847 | // part of a comdat group, and it will be difficult to identify |
| 848 | // the specific section within that group that corresponds to |
| 849 | // this linkonce section. We'll handle the simple case where |
| 850 | // the group has only one member section. Otherwise, it's not |
| 851 | // worth the effort. |
| 852 | unsigned int kept_shndx; |
| 853 | uint64_t kept_size; |
| 854 | if (kept1->object() != NULL |
| 855 | && kept1->is_comdat() |
| 856 | && kept1->find_single_comdat_section(&kept_shndx, &kept_size) |
| 857 | && kept_size == sh_size) |
| 858 | this->set_kept_comdat_section(index, kept1->object(), kept_shndx); |
| 859 | } |
| 860 | else |
| 861 | { |
| 862 | kept1->set_linkonce_size(sh_size); |
| 863 | kept2->set_linkonce_size(sh_size); |
| 864 | } |
| 865 | |
| 866 | return include1 && include2; |
| 867 | } |
| 868 | |
| 869 | // Layout an input section. |
| 870 | |
| 871 | template<int size, bool big_endian> |
| 872 | inline void |
| 873 | Sized_relobj<size, big_endian>::layout_section(Layout* layout, |
| 874 | unsigned int shndx, |
| 875 | const char* name, |
| 876 | typename This::Shdr& shdr, |
| 877 | unsigned int reloc_shndx, |
| 878 | unsigned int reloc_type) |
| 879 | { |
| 880 | off_t offset; |
| 881 | Output_section* os = layout->layout(this, shndx, name, shdr, |
| 882 | reloc_shndx, reloc_type, &offset); |
| 883 | |
| 884 | this->output_sections()[shndx] = os; |
| 885 | if (offset == -1) |
| 886 | this->section_offsets_[shndx] = invalid_address; |
| 887 | else |
| 888 | this->section_offsets_[shndx] = convert_types<Address, off_t>(offset); |
| 889 | |
| 890 | // If this section requires special handling, and if there are |
| 891 | // relocs that apply to it, then we must do the special handling |
| 892 | // before we apply the relocs. |
| 893 | if (offset == -1 && reloc_shndx != 0) |
| 894 | this->set_relocs_must_follow_section_writes(); |
| 895 | } |
| 896 | |
| 897 | // Lay out the input sections. We walk through the sections and check |
| 898 | // whether they should be included in the link. If they should, we |
| 899 | // pass them to the Layout object, which will return an output section |
| 900 | // and an offset. |
| 901 | // During garbage collection (--gc-sections) and identical code folding |
| 902 | // (--icf), this function is called twice. When it is called the first |
| 903 | // time, it is for setting up some sections as roots to a work-list for |
| 904 | // --gc-sections and to do comdat processing. Actual layout happens the |
| 905 | // second time around after all the relevant sections have been determined. |
| 906 | // The first time, is_worklist_ready or is_icf_ready is false. It is then |
| 907 | // set to true after the garbage collection worklist or identical code |
| 908 | // folding is processed and the relevant sections to be kept are |
| 909 | // determined. Then, this function is called again to layout the sections. |
| 910 | |
| 911 | template<int size, bool big_endian> |
| 912 | void |
| 913 | Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab, |
| 914 | Layout* layout, |
| 915 | Read_symbols_data* sd) |
| 916 | { |
| 917 | const unsigned int shnum = this->shnum(); |
| 918 | bool is_gc_pass_one = ((parameters->options().gc_sections() |
| 919 | && !symtab->gc()->is_worklist_ready()) |
| 920 | || (parameters->options().icf() |
| 921 | && !symtab->icf()->is_icf_ready())); |
| 922 | |
| 923 | bool is_gc_pass_two = ((parameters->options().gc_sections() |
| 924 | && symtab->gc()->is_worklist_ready()) |
| 925 | || (parameters->options().icf() |
| 926 | && symtab->icf()->is_icf_ready())); |
| 927 | |
| 928 | bool is_gc_or_icf = (parameters->options().gc_sections() |
| 929 | || parameters->options().icf()); |
| 930 | |
| 931 | // Both is_gc_pass_one and is_gc_pass_two should not be true. |
| 932 | gold_assert(!(is_gc_pass_one && is_gc_pass_two)); |
| 933 | |
| 934 | if (shnum == 0) |
| 935 | return; |
| 936 | Symbols_data* gc_sd = NULL; |
| 937 | if (is_gc_pass_one) |
| 938 | { |
| 939 | // During garbage collection save the symbols data to use it when |
| 940 | // re-entering this function. |
| 941 | gc_sd = new Symbols_data; |
| 942 | this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum); |
| 943 | this->set_symbols_data(gc_sd); |
| 944 | } |
| 945 | else if (is_gc_pass_two) |
| 946 | { |
| 947 | gc_sd = this->get_symbols_data(); |
| 948 | } |
| 949 | |
| 950 | const unsigned char* section_headers_data = NULL; |
| 951 | section_size_type section_names_size; |
| 952 | const unsigned char* symbols_data = NULL; |
| 953 | section_size_type symbols_size; |
| 954 | section_offset_type external_symbols_offset; |
| 955 | const unsigned char* symbol_names_data = NULL; |
| 956 | section_size_type symbol_names_size; |
| 957 | |
| 958 | if (is_gc_or_icf) |
| 959 | { |
| 960 | section_headers_data = gc_sd->section_headers_data; |
| 961 | section_names_size = gc_sd->section_names_size; |
| 962 | symbols_data = gc_sd->symbols_data; |
| 963 | symbols_size = gc_sd->symbols_size; |
| 964 | external_symbols_offset = gc_sd->external_symbols_offset; |
| 965 | symbol_names_data = gc_sd->symbol_names_data; |
| 966 | symbol_names_size = gc_sd->symbol_names_size; |
| 967 | } |
| 968 | else |
| 969 | { |
| 970 | section_headers_data = sd->section_headers->data(); |
| 971 | section_names_size = sd->section_names_size; |
| 972 | if (sd->symbols != NULL) |
| 973 | symbols_data = sd->symbols->data(); |
| 974 | symbols_size = sd->symbols_size; |
| 975 | external_symbols_offset = sd->external_symbols_offset; |
| 976 | if (sd->symbol_names != NULL) |
| 977 | symbol_names_data = sd->symbol_names->data(); |
| 978 | symbol_names_size = sd->symbol_names_size; |
| 979 | } |
| 980 | |
| 981 | // Get the section headers. |
| 982 | const unsigned char* shdrs = section_headers_data; |
| 983 | const unsigned char* pshdrs; |
| 984 | |
| 985 | // Get the section names. |
| 986 | const unsigned char* pnamesu = (is_gc_or_icf) |
| 987 | ? gc_sd->section_names_data |
| 988 | : sd->section_names->data(); |
| 989 | |
| 990 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
| 991 | |
| 992 | // If any input files have been claimed by plugins, we need to defer |
| 993 | // actual layout until the replacement files have arrived. |
| 994 | const bool should_defer_layout = |
| 995 | (parameters->options().has_plugins() |
| 996 | && parameters->options().plugins()->should_defer_layout()); |
| 997 | unsigned int num_sections_to_defer = 0; |
| 998 | |
| 999 | // For each section, record the index of the reloc section if any. |
| 1000 | // Use 0 to mean that there is no reloc section, -1U to mean that |
| 1001 | // there is more than one. |
| 1002 | std::vector<unsigned int> reloc_shndx(shnum, 0); |
| 1003 | std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL); |
| 1004 | // Skip the first, dummy, section. |
| 1005 | pshdrs = shdrs + This::shdr_size; |
| 1006 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
| 1007 | { |
| 1008 | typename This::Shdr shdr(pshdrs); |
| 1009 | |
| 1010 | // Count the number of sections whose layout will be deferred. |
| 1011 | if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) |
| 1012 | ++num_sections_to_defer; |
| 1013 | |
| 1014 | unsigned int sh_type = shdr.get_sh_type(); |
| 1015 | if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA) |
| 1016 | { |
| 1017 | unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info()); |
| 1018 | if (target_shndx == 0 || target_shndx >= shnum) |
| 1019 | { |
| 1020 | this->error(_("relocation section %u has bad info %u"), |
| 1021 | i, target_shndx); |
| 1022 | continue; |
| 1023 | } |
| 1024 | |
| 1025 | if (reloc_shndx[target_shndx] != 0) |
| 1026 | reloc_shndx[target_shndx] = -1U; |
| 1027 | else |
| 1028 | { |
| 1029 | reloc_shndx[target_shndx] = i; |
| 1030 | reloc_type[target_shndx] = sh_type; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | Output_sections& out_sections(this->output_sections()); |
| 1036 | std::vector<Address>& out_section_offsets(this->section_offsets_); |
| 1037 | |
| 1038 | if (!is_gc_pass_two) |
| 1039 | { |
| 1040 | out_sections.resize(shnum); |
| 1041 | out_section_offsets.resize(shnum); |
| 1042 | } |
| 1043 | |
| 1044 | // If we are only linking for symbols, then there is nothing else to |
| 1045 | // do here. |
| 1046 | if (this->input_file()->just_symbols()) |
| 1047 | { |
| 1048 | if (!is_gc_pass_two) |
| 1049 | { |
| 1050 | delete sd->section_headers; |
| 1051 | sd->section_headers = NULL; |
| 1052 | delete sd->section_names; |
| 1053 | sd->section_names = NULL; |
| 1054 | } |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | if (num_sections_to_defer > 0) |
| 1059 | { |
| 1060 | parameters->options().plugins()->add_deferred_layout_object(this); |
| 1061 | this->deferred_layout_.reserve(num_sections_to_defer); |
| 1062 | } |
| 1063 | |
| 1064 | // Whether we've seen a .note.GNU-stack section. |
| 1065 | bool seen_gnu_stack = false; |
| 1066 | // The flags of a .note.GNU-stack section. |
| 1067 | uint64_t gnu_stack_flags = 0; |
| 1068 | |
| 1069 | // Keep track of which sections to omit. |
| 1070 | std::vector<bool> omit(shnum, false); |
| 1071 | |
| 1072 | // Keep track of reloc sections when emitting relocations. |
| 1073 | const bool relocatable = parameters->options().relocatable(); |
| 1074 | const bool emit_relocs = (relocatable |
| 1075 | || parameters->options().emit_relocs()); |
| 1076 | std::vector<unsigned int> reloc_sections; |
| 1077 | |
| 1078 | // Keep track of .eh_frame sections. |
| 1079 | std::vector<unsigned int> eh_frame_sections; |
| 1080 | |
| 1081 | // Skip the first, dummy, section. |
| 1082 | pshdrs = shdrs + This::shdr_size; |
| 1083 | for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size) |
| 1084 | { |
| 1085 | typename This::Shdr shdr(pshdrs); |
| 1086 | |
| 1087 | if (shdr.get_sh_name() >= section_names_size) |
| 1088 | { |
| 1089 | this->error(_("bad section name offset for section %u: %lu"), |
| 1090 | i, static_cast<unsigned long>(shdr.get_sh_name())); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | const char* name = pnames + shdr.get_sh_name(); |
| 1095 | |
| 1096 | if (!is_gc_pass_two) |
| 1097 | { |
| 1098 | if (this->handle_gnu_warning_section(name, i, symtab)) |
| 1099 | { |
| 1100 | if (!relocatable) |
| 1101 | omit[i] = true; |
| 1102 | } |
| 1103 | |
| 1104 | // The .note.GNU-stack section is special. It gives the |
| 1105 | // protection flags that this object file requires for the stack |
| 1106 | // in memory. |
| 1107 | if (strcmp(name, ".note.GNU-stack") == 0) |
| 1108 | { |
| 1109 | seen_gnu_stack = true; |
| 1110 | gnu_stack_flags |= shdr.get_sh_flags(); |
| 1111 | omit[i] = true; |
| 1112 | } |
| 1113 | |
| 1114 | bool discard = omit[i]; |
| 1115 | if (!discard) |
| 1116 | { |
| 1117 | if (shdr.get_sh_type() == elfcpp::SHT_GROUP) |
| 1118 | { |
| 1119 | if (!this->include_section_group(symtab, layout, i, name, |
| 1120 | shdrs, pnames, |
| 1121 | section_names_size, |
| 1122 | &omit)) |
| 1123 | discard = true; |
| 1124 | } |
| 1125 | else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0 |
| 1126 | && Layout::is_linkonce(name)) |
| 1127 | { |
| 1128 | if (!this->include_linkonce_section(layout, i, name, shdr)) |
| 1129 | discard = true; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (discard) |
| 1134 | { |
| 1135 | // Do not include this section in the link. |
| 1136 | out_sections[i] = NULL; |
| 1137 | out_section_offsets[i] = invalid_address; |
| 1138 | continue; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | if (is_gc_pass_one && parameters->options().gc_sections()) |
| 1143 | { |
| 1144 | if (is_section_name_included(name) |
| 1145 | || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY |
| 1146 | || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY) |
| 1147 | { |
| 1148 | symtab->gc()->worklist().push(Section_id(this, i)); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | // When doing a relocatable link we are going to copy input |
| 1153 | // reloc sections into the output. We only want to copy the |
| 1154 | // ones associated with sections which are not being discarded. |
| 1155 | // However, we don't know that yet for all sections. So save |
| 1156 | // reloc sections and process them later. Garbage collection is |
| 1157 | // not triggered when relocatable code is desired. |
| 1158 | if (emit_relocs |
| 1159 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
| 1160 | || shdr.get_sh_type() == elfcpp::SHT_RELA)) |
| 1161 | { |
| 1162 | reloc_sections.push_back(i); |
| 1163 | continue; |
| 1164 | } |
| 1165 | |
| 1166 | if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP) |
| 1167 | continue; |
| 1168 | |
| 1169 | // The .eh_frame section is special. It holds exception frame |
| 1170 | // information that we need to read in order to generate the |
| 1171 | // exception frame header. We process these after all the other |
| 1172 | // sections so that the exception frame reader can reliably |
| 1173 | // determine which sections are being discarded, and discard the |
| 1174 | // corresponding information. |
| 1175 | if (!relocatable |
| 1176 | && strcmp(name, ".eh_frame") == 0 |
| 1177 | && this->check_eh_frame_flags(&shdr)) |
| 1178 | { |
| 1179 | if (is_gc_pass_one) |
| 1180 | { |
| 1181 | out_sections[i] = reinterpret_cast<Output_section*>(1); |
| 1182 | out_section_offsets[i] = invalid_address; |
| 1183 | } |
| 1184 | else |
| 1185 | eh_frame_sections.push_back(i); |
| 1186 | continue; |
| 1187 | } |
| 1188 | |
| 1189 | if (is_gc_pass_two && parameters->options().gc_sections()) |
| 1190 | { |
| 1191 | // This is executed during the second pass of garbage |
| 1192 | // collection. do_layout has been called before and some |
| 1193 | // sections have been already discarded. Simply ignore |
| 1194 | // such sections this time around. |
| 1195 | if (out_sections[i] == NULL) |
| 1196 | { |
| 1197 | gold_assert(out_section_offsets[i] == invalid_address); |
| 1198 | continue; |
| 1199 | } |
| 1200 | if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) |
| 1201 | && symtab->gc()->is_section_garbage(this, i)) |
| 1202 | { |
| 1203 | if (parameters->options().print_gc_sections()) |
| 1204 | gold_info(_("%s: removing unused section from '%s'" |
| 1205 | " in file '%s'"), |
| 1206 | program_name, this->section_name(i).c_str(), |
| 1207 | this->name().c_str()); |
| 1208 | out_sections[i] = NULL; |
| 1209 | out_section_offsets[i] = invalid_address; |
| 1210 | continue; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | if (is_gc_pass_two && parameters->options().icf()) |
| 1215 | { |
| 1216 | if (out_sections[i] == NULL) |
| 1217 | { |
| 1218 | gold_assert(out_section_offsets[i] == invalid_address); |
| 1219 | continue; |
| 1220 | } |
| 1221 | if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) |
| 1222 | && symtab->icf()->is_section_folded(this, i)) |
| 1223 | { |
| 1224 | if (parameters->options().print_icf_sections()) |
| 1225 | { |
| 1226 | Section_id folded = |
| 1227 | symtab->icf()->get_folded_section(this, i); |
| 1228 | Relobj* folded_obj = |
| 1229 | reinterpret_cast<Relobj*>(folded.first); |
| 1230 | gold_info(_("%s: ICF folding section '%s' in file '%s'" |
| 1231 | "into '%s' in file '%s'"), |
| 1232 | program_name, this->section_name(i).c_str(), |
| 1233 | this->name().c_str(), |
| 1234 | folded_obj->section_name(folded.second).c_str(), |
| 1235 | folded_obj->name().c_str()); |
| 1236 | } |
| 1237 | out_sections[i] = NULL; |
| 1238 | out_section_offsets[i] = invalid_address; |
| 1239 | continue; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | // Defer layout here if input files are claimed by plugins. When gc |
| 1244 | // is turned on this function is called twice. For the second call |
| 1245 | // should_defer_layout should be false. |
| 1246 | if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC)) |
| 1247 | { |
| 1248 | gold_assert(!is_gc_pass_two); |
| 1249 | this->deferred_layout_.push_back(Deferred_layout(i, name, |
| 1250 | pshdrs, |
| 1251 | reloc_shndx[i], |
| 1252 | reloc_type[i])); |
| 1253 | // Put dummy values here; real values will be supplied by |
| 1254 | // do_layout_deferred_sections. |
| 1255 | out_sections[i] = reinterpret_cast<Output_section*>(2); |
| 1256 | out_section_offsets[i] = invalid_address; |
| 1257 | continue; |
| 1258 | } |
| 1259 | |
| 1260 | // During gc_pass_two if a section that was previously deferred is |
| 1261 | // found, do not layout the section as layout_deferred_sections will |
| 1262 | // do it later from gold.cc. |
| 1263 | if (is_gc_pass_two |
| 1264 | && (out_sections[i] == reinterpret_cast<Output_section*>(2))) |
| 1265 | continue; |
| 1266 | |
| 1267 | if (is_gc_pass_one) |
| 1268 | { |
| 1269 | // This is during garbage collection. The out_sections are |
| 1270 | // assigned in the second call to this function. |
| 1271 | out_sections[i] = reinterpret_cast<Output_section*>(1); |
| 1272 | out_section_offsets[i] = invalid_address; |
| 1273 | } |
| 1274 | else |
| 1275 | { |
| 1276 | // When garbage collection is switched on the actual layout |
| 1277 | // only happens in the second call. |
| 1278 | this->layout_section(layout, i, name, shdr, reloc_shndx[i], |
| 1279 | reloc_type[i]); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | if (!is_gc_pass_one) |
| 1284 | layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags); |
| 1285 | |
| 1286 | // When doing a relocatable link handle the reloc sections at the |
| 1287 | // end. Garbage collection and Identical Code Folding is not |
| 1288 | // turned on for relocatable code. |
| 1289 | if (emit_relocs) |
| 1290 | this->size_relocatable_relocs(); |
| 1291 | |
| 1292 | gold_assert(!(is_gc_or_icf) || reloc_sections.empty()); |
| 1293 | |
| 1294 | for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin(); |
| 1295 | p != reloc_sections.end(); |
| 1296 | ++p) |
| 1297 | { |
| 1298 | unsigned int i = *p; |
| 1299 | const unsigned char* pshdr; |
| 1300 | pshdr = section_headers_data + i * This::shdr_size; |
| 1301 | typename This::Shdr shdr(pshdr); |
| 1302 | |
| 1303 | unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info()); |
| 1304 | if (data_shndx >= shnum) |
| 1305 | { |
| 1306 | // We already warned about this above. |
| 1307 | continue; |
| 1308 | } |
| 1309 | |
| 1310 | Output_section* data_section = out_sections[data_shndx]; |
| 1311 | if (data_section == NULL) |
| 1312 | { |
| 1313 | out_sections[i] = NULL; |
| 1314 | out_section_offsets[i] = invalid_address; |
| 1315 | continue; |
| 1316 | } |
| 1317 | |
| 1318 | Relocatable_relocs* rr = new Relocatable_relocs(); |
| 1319 | this->set_relocatable_relocs(i, rr); |
| 1320 | |
| 1321 | Output_section* os = layout->layout_reloc(this, i, shdr, data_section, |
| 1322 | rr); |
| 1323 | out_sections[i] = os; |
| 1324 | out_section_offsets[i] = invalid_address; |
| 1325 | } |
| 1326 | |
| 1327 | // Handle the .eh_frame sections at the end. |
| 1328 | gold_assert(!is_gc_pass_one || eh_frame_sections.empty()); |
| 1329 | for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin(); |
| 1330 | p != eh_frame_sections.end(); |
| 1331 | ++p) |
| 1332 | { |
| 1333 | gold_assert(this->has_eh_frame_); |
| 1334 | gold_assert(external_symbols_offset != 0); |
| 1335 | |
| 1336 | unsigned int i = *p; |
| 1337 | const unsigned char *pshdr; |
| 1338 | pshdr = section_headers_data + i * This::shdr_size; |
| 1339 | typename This::Shdr shdr(pshdr); |
| 1340 | |
| 1341 | off_t offset; |
| 1342 | Output_section* os = layout->layout_eh_frame(this, |
| 1343 | symbols_data, |
| 1344 | symbols_size, |
| 1345 | symbol_names_data, |
| 1346 | symbol_names_size, |
| 1347 | i, shdr, |
| 1348 | reloc_shndx[i], |
| 1349 | reloc_type[i], |
| 1350 | &offset); |
| 1351 | out_sections[i] = os; |
| 1352 | if (offset == -1) |
| 1353 | { |
| 1354 | // An object can contain at most one section holding exception |
| 1355 | // frame information. |
| 1356 | gold_assert(this->discarded_eh_frame_shndx_ == -1U); |
| 1357 | this->discarded_eh_frame_shndx_ = i; |
| 1358 | out_section_offsets[i] = invalid_address; |
| 1359 | } |
| 1360 | else |
| 1361 | out_section_offsets[i] = convert_types<Address, off_t>(offset); |
| 1362 | |
| 1363 | // If this section requires special handling, and if there are |
| 1364 | // relocs that apply to it, then we must do the special handling |
| 1365 | // before we apply the relocs. |
| 1366 | if (offset == -1 && reloc_shndx[i] != 0) |
| 1367 | this->set_relocs_must_follow_section_writes(); |
| 1368 | } |
| 1369 | |
| 1370 | if (is_gc_pass_two) |
| 1371 | { |
| 1372 | delete[] gc_sd->section_headers_data; |
| 1373 | delete[] gc_sd->section_names_data; |
| 1374 | delete[] gc_sd->symbols_data; |
| 1375 | delete[] gc_sd->symbol_names_data; |
| 1376 | this->set_symbols_data(NULL); |
| 1377 | } |
| 1378 | else |
| 1379 | { |
| 1380 | delete sd->section_headers; |
| 1381 | sd->section_headers = NULL; |
| 1382 | delete sd->section_names; |
| 1383 | sd->section_names = NULL; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // Layout sections whose layout was deferred while waiting for |
| 1388 | // input files from a plugin. |
| 1389 | |
| 1390 | template<int size, bool big_endian> |
| 1391 | void |
| 1392 | Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout) |
| 1393 | { |
| 1394 | typename std::vector<Deferred_layout>::iterator deferred; |
| 1395 | |
| 1396 | for (deferred = this->deferred_layout_.begin(); |
| 1397 | deferred != this->deferred_layout_.end(); |
| 1398 | ++deferred) |
| 1399 | { |
| 1400 | typename This::Shdr shdr(deferred->shdr_data_); |
| 1401 | this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(), |
| 1402 | shdr, deferred->reloc_shndx_, deferred->reloc_type_); |
| 1403 | } |
| 1404 | |
| 1405 | this->deferred_layout_.clear(); |
| 1406 | } |
| 1407 | |
| 1408 | // Add the symbols to the symbol table. |
| 1409 | |
| 1410 | template<int size, bool big_endian> |
| 1411 | void |
| 1412 | Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
| 1413 | Read_symbols_data* sd, |
| 1414 | Layout*) |
| 1415 | { |
| 1416 | if (sd->symbols == NULL) |
| 1417 | { |
| 1418 | gold_assert(sd->symbol_names == NULL); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | const int sym_size = This::sym_size; |
| 1423 | size_t symcount = ((sd->symbols_size - sd->external_symbols_offset) |
| 1424 | / sym_size); |
| 1425 | if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset) |
| 1426 | { |
| 1427 | this->error(_("size of symbols is not multiple of symbol size")); |
| 1428 | return; |
| 1429 | } |
| 1430 | |
| 1431 | this->symbols_.resize(symcount); |
| 1432 | |
| 1433 | const char* sym_names = |
| 1434 | reinterpret_cast<const char*>(sd->symbol_names->data()); |
| 1435 | symtab->add_from_relobj(this, |
| 1436 | sd->symbols->data() + sd->external_symbols_offset, |
| 1437 | symcount, this->local_symbol_count_, |
| 1438 | sym_names, sd->symbol_names_size, |
| 1439 | &this->symbols_, |
| 1440 | &this->defined_count_); |
| 1441 | |
| 1442 | delete sd->symbols; |
| 1443 | sd->symbols = NULL; |
| 1444 | delete sd->symbol_names; |
| 1445 | sd->symbol_names = NULL; |
| 1446 | } |
| 1447 | |
| 1448 | // First pass over the local symbols. Here we add their names to |
| 1449 | // *POOL and *DYNPOOL, and we store the symbol value in |
| 1450 | // THIS->LOCAL_VALUES_. This function is always called from a |
| 1451 | // singleton thread. This is followed by a call to |
| 1452 | // finalize_local_symbols. |
| 1453 | |
| 1454 | template<int size, bool big_endian> |
| 1455 | void |
| 1456 | Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool, |
| 1457 | Stringpool* dynpool) |
| 1458 | { |
| 1459 | gold_assert(this->symtab_shndx_ != -1U); |
| 1460 | if (this->symtab_shndx_ == 0) |
| 1461 | { |
| 1462 | // This object has no symbols. Weird but legal. |
| 1463 | return; |
| 1464 | } |
| 1465 | |
| 1466 | // Read the symbol table section header. |
| 1467 | const unsigned int symtab_shndx = this->symtab_shndx_; |
| 1468 | typename This::Shdr symtabshdr(this, |
| 1469 | this->elf_file_.section_header(symtab_shndx)); |
| 1470 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
| 1471 | |
| 1472 | // Read the local symbols. |
| 1473 | const int sym_size = This::sym_size; |
| 1474 | const unsigned int loccount = this->local_symbol_count_; |
| 1475 | gold_assert(loccount == symtabshdr.get_sh_info()); |
| 1476 | off_t locsize = loccount * sym_size; |
| 1477 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
| 1478 | locsize, true, true); |
| 1479 | |
| 1480 | // Read the symbol names. |
| 1481 | const unsigned int strtab_shndx = |
| 1482 | this->adjust_shndx(symtabshdr.get_sh_link()); |
| 1483 | section_size_type strtab_size; |
| 1484 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
| 1485 | &strtab_size, |
| 1486 | true); |
| 1487 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
| 1488 | |
| 1489 | // Loop over the local symbols. |
| 1490 | |
| 1491 | const Output_sections& out_sections(this->output_sections()); |
| 1492 | unsigned int shnum = this->shnum(); |
| 1493 | unsigned int count = 0; |
| 1494 | unsigned int dyncount = 0; |
| 1495 | // Skip the first, dummy, symbol. |
| 1496 | psyms += sym_size; |
| 1497 | bool discard_locals = parameters->options().discard_locals(); |
| 1498 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
| 1499 | { |
| 1500 | elfcpp::Sym<size, big_endian> sym(psyms); |
| 1501 | |
| 1502 | Symbol_value<size>& lv(this->local_values_[i]); |
| 1503 | |
| 1504 | bool is_ordinary; |
| 1505 | unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), |
| 1506 | &is_ordinary); |
| 1507 | lv.set_input_shndx(shndx, is_ordinary); |
| 1508 | |
| 1509 | if (sym.get_st_type() == elfcpp::STT_SECTION) |
| 1510 | lv.set_is_section_symbol(); |
| 1511 | else if (sym.get_st_type() == elfcpp::STT_TLS) |
| 1512 | lv.set_is_tls_symbol(); |
| 1513 | |
| 1514 | // Save the input symbol value for use in do_finalize_local_symbols(). |
| 1515 | lv.set_input_value(sym.get_st_value()); |
| 1516 | |
| 1517 | // Decide whether this symbol should go into the output file. |
| 1518 | |
| 1519 | if ((shndx < shnum && out_sections[shndx] == NULL) |
| 1520 | || (shndx == this->discarded_eh_frame_shndx_)) |
| 1521 | { |
| 1522 | lv.set_no_output_symtab_entry(); |
| 1523 | gold_assert(!lv.needs_output_dynsym_entry()); |
| 1524 | continue; |
| 1525 | } |
| 1526 | |
| 1527 | if (sym.get_st_type() == elfcpp::STT_SECTION) |
| 1528 | { |
| 1529 | lv.set_no_output_symtab_entry(); |
| 1530 | gold_assert(!lv.needs_output_dynsym_entry()); |
| 1531 | continue; |
| 1532 | } |
| 1533 | |
| 1534 | if (sym.get_st_name() >= strtab_size) |
| 1535 | { |
| 1536 | this->error(_("local symbol %u section name out of range: %u >= %u"), |
| 1537 | i, sym.get_st_name(), |
| 1538 | static_cast<unsigned int>(strtab_size)); |
| 1539 | lv.set_no_output_symtab_entry(); |
| 1540 | continue; |
| 1541 | } |
| 1542 | |
| 1543 | // If --discard-locals option is used, discard all temporary local |
| 1544 | // symbols. These symbols start with system-specific local label |
| 1545 | // prefixes, typically .L for ELF system. We want to be compatible |
| 1546 | // with GNU ld so here we essentially use the same check in |
| 1547 | // bfd_is_local_label(). The code is different because we already |
| 1548 | // know that: |
| 1549 | // |
| 1550 | // - the symbol is local and thus cannot have global or weak binding. |
| 1551 | // - the symbol is not a section symbol. |
| 1552 | // - the symbol has a name. |
| 1553 | // |
| 1554 | // We do not discard a symbol if it needs a dynamic symbol entry. |
| 1555 | const char* name = pnames + sym.get_st_name(); |
| 1556 | if (discard_locals |
| 1557 | && sym.get_st_type() != elfcpp::STT_FILE |
| 1558 | && !lv.needs_output_dynsym_entry() |
| 1559 | && parameters->target().is_local_label_name(name)) |
| 1560 | { |
| 1561 | lv.set_no_output_symtab_entry(); |
| 1562 | continue; |
| 1563 | } |
| 1564 | |
| 1565 | // Add the symbol to the symbol table string pool. |
| 1566 | pool->add(name, true, NULL); |
| 1567 | ++count; |
| 1568 | |
| 1569 | // If needed, add the symbol to the dynamic symbol table string pool. |
| 1570 | if (lv.needs_output_dynsym_entry()) |
| 1571 | { |
| 1572 | dynpool->add(name, true, NULL); |
| 1573 | ++dyncount; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | this->output_local_symbol_count_ = count; |
| 1578 | this->output_local_dynsym_count_ = dyncount; |
| 1579 | } |
| 1580 | |
| 1581 | // Finalize the local symbols. Here we set the final value in |
| 1582 | // THIS->LOCAL_VALUES_ and set their output symbol table indexes. |
| 1583 | // This function is always called from a singleton thread. The actual |
| 1584 | // output of the local symbols will occur in a separate task. |
| 1585 | |
| 1586 | template<int size, bool big_endian> |
| 1587 | unsigned int |
| 1588 | Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index, |
| 1589 | off_t off, |
| 1590 | Symbol_table* symtab) |
| 1591 | { |
| 1592 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); |
| 1593 | |
| 1594 | const unsigned int loccount = this->local_symbol_count_; |
| 1595 | this->local_symbol_offset_ = off; |
| 1596 | |
| 1597 | const bool relocatable = parameters->options().relocatable(); |
| 1598 | const Output_sections& out_sections(this->output_sections()); |
| 1599 | const std::vector<Address>& out_offsets(this->section_offsets_); |
| 1600 | unsigned int shnum = this->shnum(); |
| 1601 | |
| 1602 | for (unsigned int i = 1; i < loccount; ++i) |
| 1603 | { |
| 1604 | Symbol_value<size>& lv(this->local_values_[i]); |
| 1605 | |
| 1606 | bool is_ordinary; |
| 1607 | unsigned int shndx = lv.input_shndx(&is_ordinary); |
| 1608 | |
| 1609 | // Set the output symbol value. |
| 1610 | |
| 1611 | if (!is_ordinary) |
| 1612 | { |
| 1613 | if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx)) |
| 1614 | lv.set_output_value(lv.input_value()); |
| 1615 | else |
| 1616 | { |
| 1617 | this->error(_("unknown section index %u for local symbol %u"), |
| 1618 | shndx, i); |
| 1619 | lv.set_output_value(0); |
| 1620 | } |
| 1621 | } |
| 1622 | else |
| 1623 | { |
| 1624 | if (shndx >= shnum) |
| 1625 | { |
| 1626 | this->error(_("local symbol %u section index %u out of range"), |
| 1627 | i, shndx); |
| 1628 | shndx = 0; |
| 1629 | } |
| 1630 | |
| 1631 | Output_section* os = out_sections[shndx]; |
| 1632 | Address secoffset = out_offsets[shndx]; |
| 1633 | if (symtab->is_section_folded(this, shndx)) |
| 1634 | { |
| 1635 | gold_assert (os == NULL && secoffset == invalid_address); |
| 1636 | // Get the os of the section it is folded onto. |
| 1637 | Section_id folded = symtab->icf()->get_folded_section(this, |
| 1638 | shndx); |
| 1639 | gold_assert(folded.first != NULL); |
| 1640 | Sized_relobj<size, big_endian>* folded_obj = reinterpret_cast |
| 1641 | <Sized_relobj<size, big_endian>*>(folded.first); |
| 1642 | os = folded_obj->output_section(folded.second); |
| 1643 | gold_assert(os != NULL); |
| 1644 | secoffset = folded_obj->get_output_section_offset(folded.second); |
| 1645 | gold_assert(secoffset != invalid_address); |
| 1646 | } |
| 1647 | |
| 1648 | if (os == NULL) |
| 1649 | { |
| 1650 | // This local symbol belongs to a section we are discarding. |
| 1651 | // In some cases when applying relocations later, we will |
| 1652 | // attempt to match it to the corresponding kept section, |
| 1653 | // so we leave the input value unchanged here. |
| 1654 | continue; |
| 1655 | } |
| 1656 | else if (secoffset == invalid_address) |
| 1657 | { |
| 1658 | uint64_t start; |
| 1659 | |
| 1660 | // This is a SHF_MERGE section or one which otherwise |
| 1661 | // requires special handling. |
| 1662 | if (shndx == this->discarded_eh_frame_shndx_) |
| 1663 | { |
| 1664 | // This local symbol belongs to a discarded .eh_frame |
| 1665 | // section. Just treat it like the case in which |
| 1666 | // os == NULL above. |
| 1667 | gold_assert(this->has_eh_frame_); |
| 1668 | continue; |
| 1669 | } |
| 1670 | else if (!lv.is_section_symbol()) |
| 1671 | { |
| 1672 | // This is not a section symbol. We can determine |
| 1673 | // the final value now. |
| 1674 | lv.set_output_value(os->output_address(this, shndx, |
| 1675 | lv.input_value())); |
| 1676 | } |
| 1677 | else if (!os->find_starting_output_address(this, shndx, &start)) |
| 1678 | { |
| 1679 | // This is a section symbol, but apparently not one |
| 1680 | // in a merged section. Just use the start of the |
| 1681 | // output section. This happens with relocatable |
| 1682 | // links when the input object has section symbols |
| 1683 | // for arbitrary non-merge sections. |
| 1684 | lv.set_output_value(os->address()); |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | // We have to consider the addend to determine the |
| 1689 | // value to use in a relocation. START is the start |
| 1690 | // of this input section. |
| 1691 | Merged_symbol_value<size>* msv = |
| 1692 | new Merged_symbol_value<size>(lv.input_value(), start); |
| 1693 | lv.set_merged_symbol_value(msv); |
| 1694 | } |
| 1695 | } |
| 1696 | else if (lv.is_tls_symbol()) |
| 1697 | lv.set_output_value(os->tls_offset() |
| 1698 | + secoffset |
| 1699 | + lv.input_value()); |
| 1700 | else |
| 1701 | lv.set_output_value((relocatable ? 0 : os->address()) |
| 1702 | + secoffset |
| 1703 | + lv.input_value()); |
| 1704 | } |
| 1705 | |
| 1706 | if (lv.needs_output_symtab_entry()) |
| 1707 | { |
| 1708 | lv.set_output_symtab_index(index); |
| 1709 | ++index; |
| 1710 | } |
| 1711 | } |
| 1712 | return index; |
| 1713 | } |
| 1714 | |
| 1715 | // Set the output dynamic symbol table indexes for the local variables. |
| 1716 | |
| 1717 | template<int size, bool big_endian> |
| 1718 | unsigned int |
| 1719 | Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index) |
| 1720 | { |
| 1721 | const unsigned int loccount = this->local_symbol_count_; |
| 1722 | for (unsigned int i = 1; i < loccount; ++i) |
| 1723 | { |
| 1724 | Symbol_value<size>& lv(this->local_values_[i]); |
| 1725 | if (lv.needs_output_dynsym_entry()) |
| 1726 | { |
| 1727 | lv.set_output_dynsym_index(index); |
| 1728 | ++index; |
| 1729 | } |
| 1730 | } |
| 1731 | return index; |
| 1732 | } |
| 1733 | |
| 1734 | // Set the offset where local dynamic symbol information will be stored. |
| 1735 | // Returns the count of local symbols contributed to the symbol table by |
| 1736 | // this object. |
| 1737 | |
| 1738 | template<int size, bool big_endian> |
| 1739 | unsigned int |
| 1740 | Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off) |
| 1741 | { |
| 1742 | gold_assert(off == static_cast<off_t>(align_address(off, size >> 3))); |
| 1743 | this->local_dynsym_offset_ = off; |
| 1744 | return this->output_local_dynsym_count_; |
| 1745 | } |
| 1746 | |
| 1747 | // If Symbols_data is not NULL get the section flags from here otherwise |
| 1748 | // get it from the file. |
| 1749 | |
| 1750 | template<int size, bool big_endian> |
| 1751 | uint64_t |
| 1752 | Sized_relobj<size, big_endian>::do_section_flags(unsigned int shndx) |
| 1753 | { |
| 1754 | Symbols_data* sd = this->get_symbols_data(); |
| 1755 | if (sd != NULL) |
| 1756 | { |
| 1757 | const unsigned char* pshdrs = sd->section_headers_data |
| 1758 | + This::shdr_size * shndx; |
| 1759 | typename This::Shdr shdr(pshdrs); |
| 1760 | return shdr.get_sh_flags(); |
| 1761 | } |
| 1762 | // If sd is NULL, read the section header from the file. |
| 1763 | return this->elf_file_.section_flags(shndx); |
| 1764 | } |
| 1765 | |
| 1766 | // Get the section's ent size from Symbols_data. Called by get_section_contents |
| 1767 | // in icf.cc |
| 1768 | |
| 1769 | template<int size, bool big_endian> |
| 1770 | uint64_t |
| 1771 | Sized_relobj<size, big_endian>::do_section_entsize(unsigned int shndx) |
| 1772 | { |
| 1773 | Symbols_data* sd = this->get_symbols_data(); |
| 1774 | gold_assert (sd != NULL); |
| 1775 | |
| 1776 | const unsigned char* pshdrs = sd->section_headers_data |
| 1777 | + This::shdr_size * shndx; |
| 1778 | typename This::Shdr shdr(pshdrs); |
| 1779 | return shdr.get_sh_entsize(); |
| 1780 | } |
| 1781 | |
| 1782 | |
| 1783 | // Write out the local symbols. |
| 1784 | |
| 1785 | template<int size, bool big_endian> |
| 1786 | void |
| 1787 | Sized_relobj<size, big_endian>::write_local_symbols( |
| 1788 | Output_file* of, |
| 1789 | const Stringpool* sympool, |
| 1790 | const Stringpool* dynpool, |
| 1791 | Output_symtab_xindex* symtab_xindex, |
| 1792 | Output_symtab_xindex* dynsym_xindex) |
| 1793 | { |
| 1794 | const bool strip_all = parameters->options().strip_all(); |
| 1795 | if (strip_all) |
| 1796 | { |
| 1797 | if (this->output_local_dynsym_count_ == 0) |
| 1798 | return; |
| 1799 | this->output_local_symbol_count_ = 0; |
| 1800 | } |
| 1801 | |
| 1802 | gold_assert(this->symtab_shndx_ != -1U); |
| 1803 | if (this->symtab_shndx_ == 0) |
| 1804 | { |
| 1805 | // This object has no symbols. Weird but legal. |
| 1806 | return; |
| 1807 | } |
| 1808 | |
| 1809 | // Read the symbol table section header. |
| 1810 | const unsigned int symtab_shndx = this->symtab_shndx_; |
| 1811 | typename This::Shdr symtabshdr(this, |
| 1812 | this->elf_file_.section_header(symtab_shndx)); |
| 1813 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
| 1814 | const unsigned int loccount = this->local_symbol_count_; |
| 1815 | gold_assert(loccount == symtabshdr.get_sh_info()); |
| 1816 | |
| 1817 | // Read the local symbols. |
| 1818 | const int sym_size = This::sym_size; |
| 1819 | off_t locsize = loccount * sym_size; |
| 1820 | const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), |
| 1821 | locsize, true, false); |
| 1822 | |
| 1823 | // Read the symbol names. |
| 1824 | const unsigned int strtab_shndx = |
| 1825 | this->adjust_shndx(symtabshdr.get_sh_link()); |
| 1826 | section_size_type strtab_size; |
| 1827 | const unsigned char* pnamesu = this->section_contents(strtab_shndx, |
| 1828 | &strtab_size, |
| 1829 | false); |
| 1830 | const char* pnames = reinterpret_cast<const char*>(pnamesu); |
| 1831 | |
| 1832 | // Get views into the output file for the portions of the symbol table |
| 1833 | // and the dynamic symbol table that we will be writing. |
| 1834 | off_t output_size = this->output_local_symbol_count_ * sym_size; |
| 1835 | unsigned char* oview = NULL; |
| 1836 | if (output_size > 0) |
| 1837 | oview = of->get_output_view(this->local_symbol_offset_, output_size); |
| 1838 | |
| 1839 | off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size; |
| 1840 | unsigned char* dyn_oview = NULL; |
| 1841 | if (dyn_output_size > 0) |
| 1842 | dyn_oview = of->get_output_view(this->local_dynsym_offset_, |
| 1843 | dyn_output_size); |
| 1844 | |
| 1845 | const Output_sections out_sections(this->output_sections()); |
| 1846 | |
| 1847 | gold_assert(this->local_values_.size() == loccount); |
| 1848 | |
| 1849 | unsigned char* ov = oview; |
| 1850 | unsigned char* dyn_ov = dyn_oview; |
| 1851 | psyms += sym_size; |
| 1852 | for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) |
| 1853 | { |
| 1854 | elfcpp::Sym<size, big_endian> isym(psyms); |
| 1855 | |
| 1856 | Symbol_value<size>& lv(this->local_values_[i]); |
| 1857 | |
| 1858 | bool is_ordinary; |
| 1859 | unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), |
| 1860 | &is_ordinary); |
| 1861 | if (is_ordinary) |
| 1862 | { |
| 1863 | gold_assert(st_shndx < out_sections.size()); |
| 1864 | if (out_sections[st_shndx] == NULL) |
| 1865 | continue; |
| 1866 | st_shndx = out_sections[st_shndx]->out_shndx(); |
| 1867 | if (st_shndx >= elfcpp::SHN_LORESERVE) |
| 1868 | { |
| 1869 | if (lv.needs_output_symtab_entry() && !strip_all) |
| 1870 | symtab_xindex->add(lv.output_symtab_index(), st_shndx); |
| 1871 | if (lv.needs_output_dynsym_entry()) |
| 1872 | dynsym_xindex->add(lv.output_dynsym_index(), st_shndx); |
| 1873 | st_shndx = elfcpp::SHN_XINDEX; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | // Write the symbol to the output symbol table. |
| 1878 | if (!strip_all && lv.needs_output_symtab_entry()) |
| 1879 | { |
| 1880 | elfcpp::Sym_write<size, big_endian> osym(ov); |
| 1881 | |
| 1882 | gold_assert(isym.get_st_name() < strtab_size); |
| 1883 | const char* name = pnames + isym.get_st_name(); |
| 1884 | osym.put_st_name(sympool->get_offset(name)); |
| 1885 | osym.put_st_value(this->local_values_[i].value(this, 0)); |
| 1886 | osym.put_st_size(isym.get_st_size()); |
| 1887 | osym.put_st_info(isym.get_st_info()); |
| 1888 | osym.put_st_other(isym.get_st_other()); |
| 1889 | osym.put_st_shndx(st_shndx); |
| 1890 | |
| 1891 | ov += sym_size; |
| 1892 | } |
| 1893 | |
| 1894 | // Write the symbol to the output dynamic symbol table. |
| 1895 | if (lv.needs_output_dynsym_entry()) |
| 1896 | { |
| 1897 | gold_assert(dyn_ov < dyn_oview + dyn_output_size); |
| 1898 | elfcpp::Sym_write<size, big_endian> osym(dyn_ov); |
| 1899 | |
| 1900 | gold_assert(isym.get_st_name() < strtab_size); |
| 1901 | const char* name = pnames + isym.get_st_name(); |
| 1902 | osym.put_st_name(dynpool->get_offset(name)); |
| 1903 | osym.put_st_value(this->local_values_[i].value(this, 0)); |
| 1904 | osym.put_st_size(isym.get_st_size()); |
| 1905 | osym.put_st_info(isym.get_st_info()); |
| 1906 | osym.put_st_other(isym.get_st_other()); |
| 1907 | osym.put_st_shndx(st_shndx); |
| 1908 | |
| 1909 | dyn_ov += sym_size; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | if (output_size > 0) |
| 1915 | { |
| 1916 | gold_assert(ov - oview == output_size); |
| 1917 | of->write_output_view(this->local_symbol_offset_, output_size, oview); |
| 1918 | } |
| 1919 | |
| 1920 | if (dyn_output_size > 0) |
| 1921 | { |
| 1922 | gold_assert(dyn_ov - dyn_oview == dyn_output_size); |
| 1923 | of->write_output_view(this->local_dynsym_offset_, dyn_output_size, |
| 1924 | dyn_oview); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | // Set *INFO to symbolic information about the offset OFFSET in the |
| 1929 | // section SHNDX. Return true if we found something, false if we |
| 1930 | // found nothing. |
| 1931 | |
| 1932 | template<int size, bool big_endian> |
| 1933 | bool |
| 1934 | Sized_relobj<size, big_endian>::get_symbol_location_info( |
| 1935 | unsigned int shndx, |
| 1936 | off_t offset, |
| 1937 | Symbol_location_info* info) |
| 1938 | { |
| 1939 | if (this->symtab_shndx_ == 0) |
| 1940 | return false; |
| 1941 | |
| 1942 | section_size_type symbols_size; |
| 1943 | const unsigned char* symbols = this->section_contents(this->symtab_shndx_, |
| 1944 | &symbols_size, |
| 1945 | false); |
| 1946 | |
| 1947 | unsigned int symbol_names_shndx = |
| 1948 | this->adjust_shndx(this->section_link(this->symtab_shndx_)); |
| 1949 | section_size_type names_size; |
| 1950 | const unsigned char* symbol_names_u = |
| 1951 | this->section_contents(symbol_names_shndx, &names_size, false); |
| 1952 | const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u); |
| 1953 | |
| 1954 | const int sym_size = This::sym_size; |
| 1955 | const size_t count = symbols_size / sym_size; |
| 1956 | |
| 1957 | const unsigned char* p = symbols; |
| 1958 | for (size_t i = 0; i < count; ++i, p += sym_size) |
| 1959 | { |
| 1960 | elfcpp::Sym<size, big_endian> sym(p); |
| 1961 | |
| 1962 | if (sym.get_st_type() == elfcpp::STT_FILE) |
| 1963 | { |
| 1964 | if (sym.get_st_name() >= names_size) |
| 1965 | info->source_file = "(invalid)"; |
| 1966 | else |
| 1967 | info->source_file = symbol_names + sym.get_st_name(); |
| 1968 | continue; |
| 1969 | } |
| 1970 | |
| 1971 | bool is_ordinary; |
| 1972 | unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(), |
| 1973 | &is_ordinary); |
| 1974 | if (is_ordinary |
| 1975 | && st_shndx == shndx |
| 1976 | && static_cast<off_t>(sym.get_st_value()) <= offset |
| 1977 | && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size()) |
| 1978 | > offset)) |
| 1979 | { |
| 1980 | if (sym.get_st_name() > names_size) |
| 1981 | info->enclosing_symbol_name = "(invalid)"; |
| 1982 | else |
| 1983 | { |
| 1984 | info->enclosing_symbol_name = symbol_names + sym.get_st_name(); |
| 1985 | if (parameters->options().do_demangle()) |
| 1986 | { |
| 1987 | char* demangled_name = cplus_demangle( |
| 1988 | info->enclosing_symbol_name.c_str(), |
| 1989 | DMGL_ANSI | DMGL_PARAMS); |
| 1990 | if (demangled_name != NULL) |
| 1991 | { |
| 1992 | info->enclosing_symbol_name.assign(demangled_name); |
| 1993 | free(demangled_name); |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | return true; |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | return false; |
| 2002 | } |
| 2003 | |
| 2004 | // Look for a kept section corresponding to the given discarded section, |
| 2005 | // and return its output address. This is used only for relocations in |
| 2006 | // debugging sections. If we can't find the kept section, return 0. |
| 2007 | |
| 2008 | template<int size, bool big_endian> |
| 2009 | typename Sized_relobj<size, big_endian>::Address |
| 2010 | Sized_relobj<size, big_endian>::map_to_kept_section( |
| 2011 | unsigned int shndx, |
| 2012 | bool* found) const |
| 2013 | { |
| 2014 | Relobj* kept_object; |
| 2015 | unsigned int kept_shndx; |
| 2016 | if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx)) |
| 2017 | { |
| 2018 | Sized_relobj<size, big_endian>* kept_relobj = |
| 2019 | static_cast<Sized_relobj<size, big_endian>*>(kept_object); |
| 2020 | Output_section* os = kept_relobj->output_section(kept_shndx); |
| 2021 | Address offset = kept_relobj->get_output_section_offset(kept_shndx); |
| 2022 | if (os != NULL && offset != invalid_address) |
| 2023 | { |
| 2024 | *found = true; |
| 2025 | return os->address() + offset; |
| 2026 | } |
| 2027 | } |
| 2028 | *found = false; |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | // Get symbol counts. |
| 2033 | |
| 2034 | template<int size, bool big_endian> |
| 2035 | void |
| 2036 | Sized_relobj<size, big_endian>::do_get_global_symbol_counts( |
| 2037 | const Symbol_table*, |
| 2038 | size_t* defined, |
| 2039 | size_t* used) const |
| 2040 | { |
| 2041 | *defined = this->defined_count_; |
| 2042 | size_t count = 0; |
| 2043 | for (Symbols::const_iterator p = this->symbols_.begin(); |
| 2044 | p != this->symbols_.end(); |
| 2045 | ++p) |
| 2046 | if (*p != NULL |
| 2047 | && (*p)->source() == Symbol::FROM_OBJECT |
| 2048 | && (*p)->object() == this |
| 2049 | && (*p)->is_defined()) |
| 2050 | ++count; |
| 2051 | *used = count; |
| 2052 | } |
| 2053 | |
| 2054 | // Input_objects methods. |
| 2055 | |
| 2056 | // Add a regular relocatable object to the list. Return false if this |
| 2057 | // object should be ignored. |
| 2058 | |
| 2059 | bool |
| 2060 | Input_objects::add_object(Object* obj) |
| 2061 | { |
| 2062 | // Set the global target from the first object file we recognize. |
| 2063 | Target* target = obj->target(); |
| 2064 | if (!parameters->target_valid()) |
| 2065 | set_parameters_target(target); |
| 2066 | else if (target != ¶meters->target()) |
| 2067 | { |
| 2068 | obj->error(_("incompatible target")); |
| 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | // Print the filename if the -t/--trace option is selected. |
| 2073 | if (parameters->options().trace()) |
| 2074 | gold_info("%s", obj->name().c_str()); |
| 2075 | |
| 2076 | if (!obj->is_dynamic()) |
| 2077 | this->relobj_list_.push_back(static_cast<Relobj*>(obj)); |
| 2078 | else |
| 2079 | { |
| 2080 | // See if this is a duplicate SONAME. |
| 2081 | Dynobj* dynobj = static_cast<Dynobj*>(obj); |
| 2082 | const char* soname = dynobj->soname(); |
| 2083 | |
| 2084 | std::pair<Unordered_set<std::string>::iterator, bool> ins = |
| 2085 | this->sonames_.insert(soname); |
| 2086 | if (!ins.second) |
| 2087 | { |
| 2088 | // We have already seen a dynamic object with this soname. |
| 2089 | return false; |
| 2090 | } |
| 2091 | |
| 2092 | this->dynobj_list_.push_back(dynobj); |
| 2093 | } |
| 2094 | |
| 2095 | // Add this object to the cross-referencer if requested. |
| 2096 | if (parameters->options().user_set_print_symbol_counts()) |
| 2097 | { |
| 2098 | if (this->cref_ == NULL) |
| 2099 | this->cref_ = new Cref(); |
| 2100 | this->cref_->add_object(obj); |
| 2101 | } |
| 2102 | |
| 2103 | return true; |
| 2104 | } |
| 2105 | |
| 2106 | // For each dynamic object, record whether we've seen all of its |
| 2107 | // explicit dependencies. |
| 2108 | |
| 2109 | void |
| 2110 | Input_objects::check_dynamic_dependencies() const |
| 2111 | { |
| 2112 | for (Dynobj_list::const_iterator p = this->dynobj_list_.begin(); |
| 2113 | p != this->dynobj_list_.end(); |
| 2114 | ++p) |
| 2115 | { |
| 2116 | const Dynobj::Needed& needed((*p)->needed()); |
| 2117 | bool found_all = true; |
| 2118 | for (Dynobj::Needed::const_iterator pneeded = needed.begin(); |
| 2119 | pneeded != needed.end(); |
| 2120 | ++pneeded) |
| 2121 | { |
| 2122 | if (this->sonames_.find(*pneeded) == this->sonames_.end()) |
| 2123 | { |
| 2124 | found_all = false; |
| 2125 | break; |
| 2126 | } |
| 2127 | } |
| 2128 | (*p)->set_has_unknown_needed_entries(!found_all); |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | // Start processing an archive. |
| 2133 | |
| 2134 | void |
| 2135 | Input_objects::archive_start(Archive* archive) |
| 2136 | { |
| 2137 | if (parameters->options().user_set_print_symbol_counts()) |
| 2138 | { |
| 2139 | if (this->cref_ == NULL) |
| 2140 | this->cref_ = new Cref(); |
| 2141 | this->cref_->add_archive_start(archive); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | // Stop processing an archive. |
| 2146 | |
| 2147 | void |
| 2148 | Input_objects::archive_stop(Archive* archive) |
| 2149 | { |
| 2150 | if (parameters->options().user_set_print_symbol_counts()) |
| 2151 | this->cref_->add_archive_stop(archive); |
| 2152 | } |
| 2153 | |
| 2154 | // Print symbol counts |
| 2155 | |
| 2156 | void |
| 2157 | Input_objects::print_symbol_counts(const Symbol_table* symtab) const |
| 2158 | { |
| 2159 | if (parameters->options().user_set_print_symbol_counts() |
| 2160 | && this->cref_ != NULL) |
| 2161 | this->cref_->print_symbol_counts(symtab); |
| 2162 | } |
| 2163 | |
| 2164 | // Relocate_info methods. |
| 2165 | |
| 2166 | // Return a string describing the location of a relocation. This is |
| 2167 | // only used in error messages. |
| 2168 | |
| 2169 | template<int size, bool big_endian> |
| 2170 | std::string |
| 2171 | Relocate_info<size, big_endian>::location(size_t, off_t offset) const |
| 2172 | { |
| 2173 | // See if we can get line-number information from debugging sections. |
| 2174 | std::string filename; |
| 2175 | std::string file_and_lineno; // Better than filename-only, if available. |
| 2176 | |
| 2177 | Sized_dwarf_line_info<size, big_endian> line_info(this->object); |
| 2178 | // This will be "" if we failed to parse the debug info for any reason. |
| 2179 | file_and_lineno = line_info.addr2line(this->data_shndx, offset); |
| 2180 | |
| 2181 | std::string ret(this->object->name()); |
| 2182 | ret += ':'; |
| 2183 | Symbol_location_info info; |
| 2184 | if (this->object->get_symbol_location_info(this->data_shndx, offset, &info)) |
| 2185 | { |
| 2186 | ret += " in function "; |
| 2187 | ret += info.enclosing_symbol_name; |
| 2188 | ret += ":"; |
| 2189 | filename = info.source_file; |
| 2190 | } |
| 2191 | |
| 2192 | if (!file_and_lineno.empty()) |
| 2193 | ret += file_and_lineno; |
| 2194 | else |
| 2195 | { |
| 2196 | if (!filename.empty()) |
| 2197 | ret += filename; |
| 2198 | ret += "("; |
| 2199 | ret += this->object->section_name(this->data_shndx); |
| 2200 | char buf[100]; |
| 2201 | // Offsets into sections have to be positive. |
| 2202 | snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset)); |
| 2203 | ret += buf; |
| 2204 | ret += ")"; |
| 2205 | } |
| 2206 | return ret; |
| 2207 | } |
| 2208 | |
| 2209 | } // End namespace gold. |
| 2210 | |
| 2211 | namespace |
| 2212 | { |
| 2213 | |
| 2214 | using namespace gold; |
| 2215 | |
| 2216 | // Read an ELF file with the header and return the appropriate |
| 2217 | // instance of Object. |
| 2218 | |
| 2219 | template<int size, bool big_endian> |
| 2220 | Object* |
| 2221 | make_elf_sized_object(const std::string& name, Input_file* input_file, |
| 2222 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) |
| 2223 | { |
| 2224 | Target* target = select_target(ehdr.get_e_machine(), size, big_endian, |
| 2225 | ehdr.get_e_ident()[elfcpp::EI_OSABI], |
| 2226 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); |
| 2227 | if (target == NULL) |
| 2228 | gold_fatal(_("%s: unsupported ELF machine number %d"), |
| 2229 | name.c_str(), ehdr.get_e_machine()); |
| 2230 | return target->make_elf_object<size, big_endian>(name, input_file, offset, |
| 2231 | ehdr); |
| 2232 | } |
| 2233 | |
| 2234 | } // End anonymous namespace. |
| 2235 | |
| 2236 | namespace gold |
| 2237 | { |
| 2238 | |
| 2239 | // Return whether INPUT_FILE is an ELF object. |
| 2240 | |
| 2241 | bool |
| 2242 | is_elf_object(Input_file* input_file, off_t offset, |
| 2243 | const unsigned char** start, int *read_size) |
| 2244 | { |
| 2245 | off_t filesize = input_file->file().filesize(); |
| 2246 | int want = elfcpp::Elf_sizes<64>::ehdr_size; |
| 2247 | if (filesize - offset < want) |
| 2248 | want = filesize - offset; |
| 2249 | |
| 2250 | const unsigned char* p = input_file->file().get_view(offset, 0, want, |
| 2251 | true, false); |
| 2252 | *start = p; |
| 2253 | *read_size = want; |
| 2254 | |
| 2255 | if (want < 4) |
| 2256 | return false; |
| 2257 | |
| 2258 | static unsigned char elfmagic[4] = |
| 2259 | { |
| 2260 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, |
| 2261 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 |
| 2262 | }; |
| 2263 | return memcmp(p, elfmagic, 4) == 0; |
| 2264 | } |
| 2265 | |
| 2266 | // Read an ELF file and return the appropriate instance of Object. |
| 2267 | |
| 2268 | Object* |
| 2269 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, |
| 2270 | const unsigned char* p, section_offset_type bytes, |
| 2271 | bool* punconfigured) |
| 2272 | { |
| 2273 | if (punconfigured != NULL) |
| 2274 | *punconfigured = false; |
| 2275 | |
| 2276 | if (bytes < elfcpp::EI_NIDENT) |
| 2277 | { |
| 2278 | gold_error(_("%s: ELF file too short"), name.c_str()); |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | |
| 2282 | int v = p[elfcpp::EI_VERSION]; |
| 2283 | if (v != elfcpp::EV_CURRENT) |
| 2284 | { |
| 2285 | if (v == elfcpp::EV_NONE) |
| 2286 | gold_error(_("%s: invalid ELF version 0"), name.c_str()); |
| 2287 | else |
| 2288 | gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v); |
| 2289 | return NULL; |
| 2290 | } |
| 2291 | |
| 2292 | int c = p[elfcpp::EI_CLASS]; |
| 2293 | if (c == elfcpp::ELFCLASSNONE) |
| 2294 | { |
| 2295 | gold_error(_("%s: invalid ELF class 0"), name.c_str()); |
| 2296 | return NULL; |
| 2297 | } |
| 2298 | else if (c != elfcpp::ELFCLASS32 |
| 2299 | && c != elfcpp::ELFCLASS64) |
| 2300 | { |
| 2301 | gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c); |
| 2302 | return NULL; |
| 2303 | } |
| 2304 | |
| 2305 | int d = p[elfcpp::EI_DATA]; |
| 2306 | if (d == elfcpp::ELFDATANONE) |
| 2307 | { |
| 2308 | gold_error(_("%s: invalid ELF data encoding"), name.c_str()); |
| 2309 | return NULL; |
| 2310 | } |
| 2311 | else if (d != elfcpp::ELFDATA2LSB |
| 2312 | && d != elfcpp::ELFDATA2MSB) |
| 2313 | { |
| 2314 | gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d); |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | |
| 2318 | bool big_endian = d == elfcpp::ELFDATA2MSB; |
| 2319 | |
| 2320 | if (c == elfcpp::ELFCLASS32) |
| 2321 | { |
| 2322 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) |
| 2323 | { |
| 2324 | gold_error(_("%s: ELF file too short"), name.c_str()); |
| 2325 | return NULL; |
| 2326 | } |
| 2327 | if (big_endian) |
| 2328 | { |
| 2329 | #ifdef HAVE_TARGET_32_BIG |
| 2330 | elfcpp::Ehdr<32, true> ehdr(p); |
| 2331 | return make_elf_sized_object<32, true>(name, input_file, |
| 2332 | offset, ehdr); |
| 2333 | #else |
| 2334 | if (punconfigured != NULL) |
| 2335 | *punconfigured = true; |
| 2336 | else |
| 2337 | gold_error(_("%s: not configured to support " |
| 2338 | "32-bit big-endian object"), |
| 2339 | name.c_str()); |
| 2340 | return NULL; |
| 2341 | #endif |
| 2342 | } |
| 2343 | else |
| 2344 | { |
| 2345 | #ifdef HAVE_TARGET_32_LITTLE |
| 2346 | elfcpp::Ehdr<32, false> ehdr(p); |
| 2347 | return make_elf_sized_object<32, false>(name, input_file, |
| 2348 | offset, ehdr); |
| 2349 | #else |
| 2350 | if (punconfigured != NULL) |
| 2351 | *punconfigured = true; |
| 2352 | else |
| 2353 | gold_error(_("%s: not configured to support " |
| 2354 | "32-bit little-endian object"), |
| 2355 | name.c_str()); |
| 2356 | return NULL; |
| 2357 | #endif |
| 2358 | } |
| 2359 | } |
| 2360 | else |
| 2361 | { |
| 2362 | if (bytes < elfcpp::Elf_sizes<64>::ehdr_size) |
| 2363 | { |
| 2364 | gold_error(_("%s: ELF file too short"), name.c_str()); |
| 2365 | return NULL; |
| 2366 | } |
| 2367 | if (big_endian) |
| 2368 | { |
| 2369 | #ifdef HAVE_TARGET_64_BIG |
| 2370 | elfcpp::Ehdr<64, true> ehdr(p); |
| 2371 | return make_elf_sized_object<64, true>(name, input_file, |
| 2372 | offset, ehdr); |
| 2373 | #else |
| 2374 | if (punconfigured != NULL) |
| 2375 | *punconfigured = true; |
| 2376 | else |
| 2377 | gold_error(_("%s: not configured to support " |
| 2378 | "64-bit big-endian object"), |
| 2379 | name.c_str()); |
| 2380 | return NULL; |
| 2381 | #endif |
| 2382 | } |
| 2383 | else |
| 2384 | { |
| 2385 | #ifdef HAVE_TARGET_64_LITTLE |
| 2386 | elfcpp::Ehdr<64, false> ehdr(p); |
| 2387 | return make_elf_sized_object<64, false>(name, input_file, |
| 2388 | offset, ehdr); |
| 2389 | #else |
| 2390 | if (punconfigured != NULL) |
| 2391 | *punconfigured = true; |
| 2392 | else |
| 2393 | gold_error(_("%s: not configured to support " |
| 2394 | "64-bit little-endian object"), |
| 2395 | name.c_str()); |
| 2396 | return NULL; |
| 2397 | #endif |
| 2398 | } |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | // Instantiate the templates we need. |
| 2403 | |
| 2404 | #ifdef HAVE_TARGET_32_LITTLE |
| 2405 | template |
| 2406 | void |
| 2407 | Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*, |
| 2408 | Read_symbols_data*); |
| 2409 | #endif |
| 2410 | |
| 2411 | #ifdef HAVE_TARGET_32_BIG |
| 2412 | template |
| 2413 | void |
| 2414 | Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*, |
| 2415 | Read_symbols_data*); |
| 2416 | #endif |
| 2417 | |
| 2418 | #ifdef HAVE_TARGET_64_LITTLE |
| 2419 | template |
| 2420 | void |
| 2421 | Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*, |
| 2422 | Read_symbols_data*); |
| 2423 | #endif |
| 2424 | |
| 2425 | #ifdef HAVE_TARGET_64_BIG |
| 2426 | template |
| 2427 | void |
| 2428 | Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*, |
| 2429 | Read_symbols_data*); |
| 2430 | #endif |
| 2431 | |
| 2432 | #ifdef HAVE_TARGET_32_LITTLE |
| 2433 | template |
| 2434 | class Sized_relobj<32, false>; |
| 2435 | #endif |
| 2436 | |
| 2437 | #ifdef HAVE_TARGET_32_BIG |
| 2438 | template |
| 2439 | class Sized_relobj<32, true>; |
| 2440 | #endif |
| 2441 | |
| 2442 | #ifdef HAVE_TARGET_64_LITTLE |
| 2443 | template |
| 2444 | class Sized_relobj<64, false>; |
| 2445 | #endif |
| 2446 | |
| 2447 | #ifdef HAVE_TARGET_64_BIG |
| 2448 | template |
| 2449 | class Sized_relobj<64, true>; |
| 2450 | #endif |
| 2451 | |
| 2452 | #ifdef HAVE_TARGET_32_LITTLE |
| 2453 | template |
| 2454 | struct Relocate_info<32, false>; |
| 2455 | #endif |
| 2456 | |
| 2457 | #ifdef HAVE_TARGET_32_BIG |
| 2458 | template |
| 2459 | struct Relocate_info<32, true>; |
| 2460 | #endif |
| 2461 | |
| 2462 | #ifdef HAVE_TARGET_64_LITTLE |
| 2463 | template |
| 2464 | struct Relocate_info<64, false>; |
| 2465 | #endif |
| 2466 | |
| 2467 | #ifdef HAVE_TARGET_64_BIG |
| 2468 | template |
| 2469 | struct Relocate_info<64, true>; |
| 2470 | #endif |
| 2471 | |
| 2472 | } // End namespace gold. |