2012-03-21 Cary Coutant <ccoutant@google.com>
[deliverable/binutils-gdb.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 #include "compressed_output.h"
43 #include "incremental.h"
44
45 namespace gold
46 {
47
48 // Struct Read_symbols_data.
49
50 // Destroy any remaining File_view objects.
51
52 Read_symbols_data::~Read_symbols_data()
53 {
54 if (this->section_headers != NULL)
55 delete this->section_headers;
56 if (this->section_names != NULL)
57 delete this->section_names;
58 if (this->symbols != NULL)
59 delete this->symbols;
60 if (this->symbol_names != NULL)
61 delete this->symbol_names;
62 if (this->versym != NULL)
63 delete this->versym;
64 if (this->verdef != NULL)
65 delete this->verdef;
66 if (this->verneed != NULL)
67 delete this->verneed;
68 }
69
70 // Class Xindex.
71
72 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
73 // section and read it in. SYMTAB_SHNDX is the index of the symbol
74 // table we care about.
75
76 template<int size, bool big_endian>
77 void
78 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
79 {
80 if (!this->symtab_xindex_.empty())
81 return;
82
83 gold_assert(symtab_shndx != 0);
84
85 // Look through the sections in reverse order, on the theory that it
86 // is more likely to be near the end than the beginning.
87 unsigned int i = object->shnum();
88 while (i > 0)
89 {
90 --i;
91 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
92 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
93 {
94 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
95 return;
96 }
97 }
98
99 object->error(_("missing SHT_SYMTAB_SHNDX section"));
100 }
101
102 // Read in the symtab_xindex_ array, given the section index of the
103 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
104 // section headers.
105
106 template<int size, bool big_endian>
107 void
108 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
109 const unsigned char* pshdrs)
110 {
111 section_size_type bytecount;
112 const unsigned char* contents;
113 if (pshdrs == NULL)
114 contents = object->section_contents(xindex_shndx, &bytecount, false);
115 else
116 {
117 const unsigned char* p = (pshdrs
118 + (xindex_shndx
119 * elfcpp::Elf_sizes<size>::shdr_size));
120 typename elfcpp::Shdr<size, big_endian> shdr(p);
121 bytecount = convert_to_section_size_type(shdr.get_sh_size());
122 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
123 }
124
125 gold_assert(this->symtab_xindex_.empty());
126 this->symtab_xindex_.reserve(bytecount / 4);
127 for (section_size_type i = 0; i < bytecount; i += 4)
128 {
129 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
130 // We preadjust the section indexes we save.
131 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
132 }
133 }
134
135 // Symbol symndx has a section of SHN_XINDEX; return the real section
136 // index.
137
138 unsigned int
139 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
140 {
141 if (symndx >= this->symtab_xindex_.size())
142 {
143 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
144 symndx);
145 return elfcpp::SHN_UNDEF;
146 }
147 unsigned int shndx = this->symtab_xindex_[symndx];
148 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
149 {
150 object->error(_("extended index for symbol %u out of range: %u"),
151 symndx, shndx);
152 return elfcpp::SHN_UNDEF;
153 }
154 return shndx;
155 }
156
157 // Class Object.
158
159 // Report an error for this object file. This is used by the
160 // elfcpp::Elf_file interface, and also called by the Object code
161 // itself.
162
163 void
164 Object::error(const char* format, ...) const
165 {
166 va_list args;
167 va_start(args, format);
168 char* buf = NULL;
169 if (vasprintf(&buf, format, args) < 0)
170 gold_nomem();
171 va_end(args);
172 gold_error(_("%s: %s"), this->name().c_str(), buf);
173 free(buf);
174 }
175
176 // Return a view of the contents of a section.
177
178 const unsigned char*
179 Object::section_contents(unsigned int shndx, section_size_type* plen,
180 bool cache)
181 { return this->do_section_contents(shndx, plen, cache); }
182
183 // Read the section data into SD. This is code common to Sized_relobj_file
184 // and Sized_dynobj, so we put it into Object.
185
186 template<int size, bool big_endian>
187 void
188 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
189 Read_symbols_data* sd)
190 {
191 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
192
193 // Read the section headers.
194 const off_t shoff = elf_file->shoff();
195 const unsigned int shnum = this->shnum();
196 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
197 true, true);
198
199 // Read the section names.
200 const unsigned char* pshdrs = sd->section_headers->data();
201 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
202 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
203
204 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
205 this->error(_("section name section has wrong type: %u"),
206 static_cast<unsigned int>(shdrnames.get_sh_type()));
207
208 sd->section_names_size =
209 convert_to_section_size_type(shdrnames.get_sh_size());
210 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
211 sd->section_names_size, false,
212 false);
213 }
214
215 // If NAME is the name of a special .gnu.warning section, arrange for
216 // the warning to be issued. SHNDX is the section index. Return
217 // whether it is a warning section.
218
219 bool
220 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
221 Symbol_table* symtab)
222 {
223 const char warn_prefix[] = ".gnu.warning.";
224 const int warn_prefix_len = sizeof warn_prefix - 1;
225 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
226 {
227 // Read the section contents to get the warning text. It would
228 // be nicer if we only did this if we have to actually issue a
229 // warning. Unfortunately, warnings are issued as we relocate
230 // sections. That means that we can not lock the object then,
231 // as we might try to issue the same warning multiple times
232 // simultaneously.
233 section_size_type len;
234 const unsigned char* contents = this->section_contents(shndx, &len,
235 false);
236 if (len == 0)
237 {
238 const char* warning = name + warn_prefix_len;
239 contents = reinterpret_cast<const unsigned char*>(warning);
240 len = strlen(warning);
241 }
242 std::string warning(reinterpret_cast<const char*>(contents), len);
243 symtab->add_warning(name + warn_prefix_len, this, warning);
244 return true;
245 }
246 return false;
247 }
248
249 // If NAME is the name of the special section which indicates that
250 // this object was compiled with -fsplit-stack, mark it accordingly.
251
252 bool
253 Object::handle_split_stack_section(const char* name)
254 {
255 if (strcmp(name, ".note.GNU-split-stack") == 0)
256 {
257 this->uses_split_stack_ = true;
258 return true;
259 }
260 if (strcmp(name, ".note.GNU-no-split-stack") == 0)
261 {
262 this->has_no_split_stack_ = true;
263 return true;
264 }
265 return false;
266 }
267
268 // Class Relobj
269
270 // To copy the symbols data read from the file to a local data structure.
271 // This function is called from do_layout only while doing garbage
272 // collection.
273
274 void
275 Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
276 unsigned int section_header_size)
277 {
278 gc_sd->section_headers_data =
279 new unsigned char[(section_header_size)];
280 memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
281 section_header_size);
282 gc_sd->section_names_data =
283 new unsigned char[sd->section_names_size];
284 memcpy(gc_sd->section_names_data, sd->section_names->data(),
285 sd->section_names_size);
286 gc_sd->section_names_size = sd->section_names_size;
287 if (sd->symbols != NULL)
288 {
289 gc_sd->symbols_data =
290 new unsigned char[sd->symbols_size];
291 memcpy(gc_sd->symbols_data, sd->symbols->data(),
292 sd->symbols_size);
293 }
294 else
295 {
296 gc_sd->symbols_data = NULL;
297 }
298 gc_sd->symbols_size = sd->symbols_size;
299 gc_sd->external_symbols_offset = sd->external_symbols_offset;
300 if (sd->symbol_names != NULL)
301 {
302 gc_sd->symbol_names_data =
303 new unsigned char[sd->symbol_names_size];
304 memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
305 sd->symbol_names_size);
306 }
307 else
308 {
309 gc_sd->symbol_names_data = NULL;
310 }
311 gc_sd->symbol_names_size = sd->symbol_names_size;
312 }
313
314 // This function determines if a particular section name must be included
315 // in the link. This is used during garbage collection to determine the
316 // roots of the worklist.
317
318 bool
319 Relobj::is_section_name_included(const char* name)
320 {
321 if (is_prefix_of(".ctors", name)
322 || is_prefix_of(".dtors", name)
323 || is_prefix_of(".note", name)
324 || is_prefix_of(".init", name)
325 || is_prefix_of(".fini", name)
326 || is_prefix_of(".gcc_except_table", name)
327 || is_prefix_of(".jcr", name)
328 || is_prefix_of(".preinit_array", name)
329 || (is_prefix_of(".text", name)
330 && strstr(name, "personality"))
331 || (is_prefix_of(".data", name)
332 && strstr(name, "personality"))
333 || (is_prefix_of(".gnu.linkonce.d", name)
334 && strstr(name, "personality")))
335 {
336 return true;
337 }
338 return false;
339 }
340
341 // Finalize the incremental relocation information. Allocates a block
342 // of relocation entries for each symbol, and sets the reloc_bases_
343 // array to point to the first entry in each block. If CLEAR_COUNTS
344 // is TRUE, also clear the per-symbol relocation counters.
345
346 void
347 Relobj::finalize_incremental_relocs(Layout* layout, bool clear_counts)
348 {
349 unsigned int nsyms = this->get_global_symbols()->size();
350 this->reloc_bases_ = new unsigned int[nsyms];
351
352 gold_assert(this->reloc_bases_ != NULL);
353 gold_assert(layout->incremental_inputs() != NULL);
354
355 unsigned int rindex = layout->incremental_inputs()->get_reloc_count();
356 for (unsigned int i = 0; i < nsyms; ++i)
357 {
358 this->reloc_bases_[i] = rindex;
359 rindex += this->reloc_counts_[i];
360 if (clear_counts)
361 this->reloc_counts_[i] = 0;
362 }
363 layout->incremental_inputs()->set_reloc_count(rindex);
364 }
365
366 // Class Sized_relobj.
367
368 // Iterate over local symbols, calling a visitor class V for each GOT offset
369 // associated with a local symbol.
370
371 template<int size, bool big_endian>
372 void
373 Sized_relobj<size, big_endian>::do_for_all_local_got_entries(
374 Got_offset_list::Visitor* v) const
375 {
376 unsigned int nsyms = this->local_symbol_count();
377 for (unsigned int i = 0; i < nsyms; i++)
378 {
379 Local_got_offsets::const_iterator p = this->local_got_offsets_.find(i);
380 if (p != this->local_got_offsets_.end())
381 {
382 const Got_offset_list* got_offsets = p->second;
383 got_offsets->for_all_got_offsets(v);
384 }
385 }
386 }
387
388 // Class Sized_relobj_file.
389
390 template<int size, bool big_endian>
391 Sized_relobj_file<size, big_endian>::Sized_relobj_file(
392 const std::string& name,
393 Input_file* input_file,
394 off_t offset,
395 const elfcpp::Ehdr<size, big_endian>& ehdr)
396 : Sized_relobj<size, big_endian>(name, input_file, offset),
397 elf_file_(this, ehdr),
398 symtab_shndx_(-1U),
399 local_symbol_count_(0),
400 output_local_symbol_count_(0),
401 output_local_dynsym_count_(0),
402 symbols_(),
403 defined_count_(0),
404 local_symbol_offset_(0),
405 local_dynsym_offset_(0),
406 local_values_(),
407 local_plt_offsets_(),
408 kept_comdat_sections_(),
409 has_eh_frame_(false),
410 discarded_eh_frame_shndx_(-1U),
411 deferred_layout_(),
412 deferred_layout_relocs_(),
413 compressed_sections_()
414 {
415 this->e_type_ = ehdr.get_e_type();
416 }
417
418 template<int size, bool big_endian>
419 Sized_relobj_file<size, big_endian>::~Sized_relobj_file()
420 {
421 }
422
423 // Set up an object file based on the file header. This sets up the
424 // section information.
425
426 template<int size, bool big_endian>
427 void
428 Sized_relobj_file<size, big_endian>::do_setup()
429 {
430 const unsigned int shnum = this->elf_file_.shnum();
431 this->set_shnum(shnum);
432 }
433
434 // Find the SHT_SYMTAB section, given the section headers. The ELF
435 // standard says that maybe in the future there can be more than one
436 // SHT_SYMTAB section. Until somebody figures out how that could
437 // work, we assume there is only one.
438
439 template<int size, bool big_endian>
440 void
441 Sized_relobj_file<size, big_endian>::find_symtab(const unsigned char* pshdrs)
442 {
443 const unsigned int shnum = this->shnum();
444 this->symtab_shndx_ = 0;
445 if (shnum > 0)
446 {
447 // Look through the sections in reverse order, since gas tends
448 // to put the symbol table at the end.
449 const unsigned char* p = pshdrs + shnum * This::shdr_size;
450 unsigned int i = shnum;
451 unsigned int xindex_shndx = 0;
452 unsigned int xindex_link = 0;
453 while (i > 0)
454 {
455 --i;
456 p -= This::shdr_size;
457 typename This::Shdr shdr(p);
458 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
459 {
460 this->symtab_shndx_ = i;
461 if (xindex_shndx > 0 && xindex_link == i)
462 {
463 Xindex* xindex =
464 new Xindex(this->elf_file_.large_shndx_offset());
465 xindex->read_symtab_xindex<size, big_endian>(this,
466 xindex_shndx,
467 pshdrs);
468 this->set_xindex(xindex);
469 }
470 break;
471 }
472
473 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
474 // one. This will work if it follows the SHT_SYMTAB
475 // section.
476 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
477 {
478 xindex_shndx = i;
479 xindex_link = this->adjust_shndx(shdr.get_sh_link());
480 }
481 }
482 }
483 }
484
485 // Return the Xindex structure to use for object with lots of
486 // sections.
487
488 template<int size, bool big_endian>
489 Xindex*
490 Sized_relobj_file<size, big_endian>::do_initialize_xindex()
491 {
492 gold_assert(this->symtab_shndx_ != -1U);
493 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
494 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
495 return xindex;
496 }
497
498 // Return whether SHDR has the right type and flags to be a GNU
499 // .eh_frame section.
500
501 template<int size, bool big_endian>
502 bool
503 Sized_relobj_file<size, big_endian>::check_eh_frame_flags(
504 const elfcpp::Shdr<size, big_endian>* shdr) const
505 {
506 elfcpp::Elf_Word sh_type = shdr->get_sh_type();
507 return ((sh_type == elfcpp::SHT_PROGBITS
508 || sh_type == elfcpp::SHT_X86_64_UNWIND)
509 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
510 }
511
512 // Return whether there is a GNU .eh_frame section, given the section
513 // headers and the section names.
514
515 template<int size, bool big_endian>
516 bool
517 Sized_relobj_file<size, big_endian>::find_eh_frame(
518 const unsigned char* pshdrs,
519 const char* names,
520 section_size_type names_size) const
521 {
522 const unsigned int shnum = this->shnum();
523 const unsigned char* p = pshdrs + This::shdr_size;
524 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
525 {
526 typename This::Shdr shdr(p);
527 if (this->check_eh_frame_flags(&shdr))
528 {
529 if (shdr.get_sh_name() >= names_size)
530 {
531 this->error(_("bad section name offset for section %u: %lu"),
532 i, static_cast<unsigned long>(shdr.get_sh_name()));
533 continue;
534 }
535
536 const char* name = names + shdr.get_sh_name();
537 if (strcmp(name, ".eh_frame") == 0)
538 return true;
539 }
540 }
541 return false;
542 }
543
544 // Return TRUE if this is a section whose contents will be needed in the
545 // Add_symbols task. This function is only called for sections that have
546 // already passed the test in is_compressed_debug_section(), so we know
547 // that the section name begins with ".zdebug".
548
549 static bool
550 need_decompressed_section(const char* name)
551 {
552 // Skip over the ".zdebug" and a quick check for the "_".
553 name += 7;
554 if (*name++ != '_')
555 return false;
556
557 #ifdef ENABLE_THREADS
558 // Decompressing these sections now will help only if we're
559 // multithreaded.
560 if (parameters->options().threads())
561 {
562 // We will need .zdebug_str if this is not an incremental link
563 // (i.e., we are processing string merge sections) or if we need
564 // to build a gdb index.
565 if ((!parameters->incremental() || parameters->options().gdb_index())
566 && strcmp(name, "str") == 0)
567 return true;
568
569 // We will need these other sections when building a gdb index.
570 if (parameters->options().gdb_index()
571 && (strcmp(name, "info") == 0
572 || strcmp(name, "types") == 0
573 || strcmp(name, "pubnames") == 0
574 || strcmp(name, "pubtypes") == 0
575 || strcmp(name, "ranges") == 0
576 || strcmp(name, "abbrev") == 0))
577 return true;
578 }
579 #endif
580
581 // Even when single-threaded, we will need .zdebug_str if this is
582 // not an incremental link and we are building a gdb index.
583 // Otherwise, we would decompress the section twice: once for
584 // string merge processing, and once for building the gdb index.
585 if (!parameters->incremental()
586 && parameters->options().gdb_index()
587 && strcmp(name, "str") == 0)
588 return true;
589
590 return false;
591 }
592
593 // Build a table for any compressed debug sections, mapping each section index
594 // to the uncompressed size and (if needed) the decompressed contents.
595
596 template<int size, bool big_endian>
597 Compressed_section_map*
598 build_compressed_section_map(
599 const unsigned char* pshdrs,
600 unsigned int shnum,
601 const char* names,
602 section_size_type names_size,
603 Sized_relobj_file<size, big_endian>* obj)
604 {
605 Compressed_section_map* uncompressed_map = new Compressed_section_map();
606 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
607 const unsigned char* p = pshdrs + shdr_size;
608
609 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
610 {
611 typename elfcpp::Shdr<size, big_endian> shdr(p);
612 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
613 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
614 {
615 if (shdr.get_sh_name() >= names_size)
616 {
617 obj->error(_("bad section name offset for section %u: %lu"),
618 i, static_cast<unsigned long>(shdr.get_sh_name()));
619 continue;
620 }
621
622 const char* name = names + shdr.get_sh_name();
623 if (is_compressed_debug_section(name))
624 {
625 section_size_type len;
626 const unsigned char* contents =
627 obj->section_contents(i, &len, false);
628 uint64_t uncompressed_size = get_uncompressed_size(contents, len);
629 Compressed_section_info info;
630 info.size = convert_to_section_size_type(uncompressed_size);
631 info.contents = NULL;
632 if (uncompressed_size != -1ULL)
633 {
634 unsigned char* uncompressed_data = NULL;
635 if (need_decompressed_section(name))
636 {
637 uncompressed_data = new unsigned char[uncompressed_size];
638 if (decompress_input_section(contents, len,
639 uncompressed_data,
640 uncompressed_size))
641 info.contents = uncompressed_data;
642 else
643 delete[] uncompressed_data;
644 }
645 (*uncompressed_map)[i] = info;
646 }
647 }
648 }
649 }
650 return uncompressed_map;
651 }
652
653 // Read the sections and symbols from an object file.
654
655 template<int size, bool big_endian>
656 void
657 Sized_relobj_file<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
658 {
659 bool need_local_symbols = false;
660
661 this->read_section_data(&this->elf_file_, sd);
662
663 const unsigned char* const pshdrs = sd->section_headers->data();
664
665 this->find_symtab(pshdrs);
666
667 const unsigned char* namesu = sd->section_names->data();
668 const char* names = reinterpret_cast<const char*>(namesu);
669 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
670 {
671 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
672 this->has_eh_frame_ = true;
673 }
674 if (memmem(names, sd->section_names_size, ".zdebug_", 8) != NULL)
675 this->compressed_sections_ =
676 build_compressed_section_map(pshdrs, this->shnum(), names,
677 sd->section_names_size, this);
678
679 if (this->has_eh_frame_
680 || (!parameters->options().relocatable()
681 && parameters->options().gdb_index()
682 && (memmem(names, sd->section_names_size, "debug_info", 12) == 0
683 || memmem(names, sd->section_names_size, "debug_types",
684 13) == 0)))
685 need_local_symbols = true;
686
687 sd->symbols = NULL;
688 sd->symbols_size = 0;
689 sd->external_symbols_offset = 0;
690 sd->symbol_names = NULL;
691 sd->symbol_names_size = 0;
692
693 if (this->symtab_shndx_ == 0)
694 {
695 // No symbol table. Weird but legal.
696 return;
697 }
698
699 // Get the symbol table section header.
700 typename This::Shdr symtabshdr(pshdrs
701 + this->symtab_shndx_ * This::shdr_size);
702 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
703
704 // If this object has a .eh_frame section, or if building a .gdb_index
705 // section and there is debug info, we need all the symbols.
706 // Otherwise we only need the external symbols. While it would be
707 // simpler to just always read all the symbols, I've seen object
708 // files with well over 2000 local symbols, which for a 64-bit
709 // object file format is over 5 pages that we don't need to read
710 // now.
711
712 const int sym_size = This::sym_size;
713 const unsigned int loccount = symtabshdr.get_sh_info();
714 this->local_symbol_count_ = loccount;
715 this->local_values_.resize(loccount);
716 section_offset_type locsize = loccount * sym_size;
717 off_t dataoff = symtabshdr.get_sh_offset();
718 section_size_type datasize =
719 convert_to_section_size_type(symtabshdr.get_sh_size());
720 off_t extoff = dataoff + locsize;
721 section_size_type extsize = datasize - locsize;
722
723 off_t readoff = need_local_symbols ? dataoff : extoff;
724 section_size_type readsize = need_local_symbols ? datasize : extsize;
725
726 if (readsize == 0)
727 {
728 // No external symbols. Also weird but also legal.
729 return;
730 }
731
732 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
733
734 // Read the section header for the symbol names.
735 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
736 if (strtab_shndx >= this->shnum())
737 {
738 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
739 return;
740 }
741 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
742 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
743 {
744 this->error(_("symbol table name section has wrong type: %u"),
745 static_cast<unsigned int>(strtabshdr.get_sh_type()));
746 return;
747 }
748
749 // Read the symbol names.
750 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
751 strtabshdr.get_sh_size(),
752 false, true);
753
754 sd->symbols = fvsymtab;
755 sd->symbols_size = readsize;
756 sd->external_symbols_offset = need_local_symbols ? locsize : 0;
757 sd->symbol_names = fvstrtab;
758 sd->symbol_names_size =
759 convert_to_section_size_type(strtabshdr.get_sh_size());
760 }
761
762 // Return the section index of symbol SYM. Set *VALUE to its value in
763 // the object file. Set *IS_ORDINARY if this is an ordinary section
764 // index, not a special code between SHN_LORESERVE and SHN_HIRESERVE.
765 // Note that for a symbol which is not defined in this object file,
766 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
767 // the final value of the symbol in the link.
768
769 template<int size, bool big_endian>
770 unsigned int
771 Sized_relobj_file<size, big_endian>::symbol_section_and_value(unsigned int sym,
772 Address* value,
773 bool* is_ordinary)
774 {
775 section_size_type symbols_size;
776 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
777 &symbols_size,
778 false);
779
780 const size_t count = symbols_size / This::sym_size;
781 gold_assert(sym < count);
782
783 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
784 *value = elfsym.get_st_value();
785
786 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
787 }
788
789 // Return whether to include a section group in the link. LAYOUT is
790 // used to keep track of which section groups we have already seen.
791 // INDEX is the index of the section group and SHDR is the section
792 // header. If we do not want to include this group, we set bits in
793 // OMIT for each section which should be discarded.
794
795 template<int size, bool big_endian>
796 bool
797 Sized_relobj_file<size, big_endian>::include_section_group(
798 Symbol_table* symtab,
799 Layout* layout,
800 unsigned int index,
801 const char* name,
802 const unsigned char* shdrs,
803 const char* section_names,
804 section_size_type section_names_size,
805 std::vector<bool>* omit)
806 {
807 // Read the section contents.
808 typename This::Shdr shdr(shdrs + index * This::shdr_size);
809 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
810 shdr.get_sh_size(), true, false);
811 const elfcpp::Elf_Word* pword =
812 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
813
814 // The first word contains flags. We only care about COMDAT section
815 // groups. Other section groups are always included in the link
816 // just like ordinary sections.
817 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
818
819 // Look up the group signature, which is the name of a symbol. ELF
820 // uses a symbol name because some group signatures are long, and
821 // the name is generally already in the symbol table, so it makes
822 // sense to put the long string just once in .strtab rather than in
823 // both .strtab and .shstrtab.
824
825 // Get the appropriate symbol table header (this will normally be
826 // the single SHT_SYMTAB section, but in principle it need not be).
827 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
828 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
829
830 // Read the symbol table entry.
831 unsigned int symndx = shdr.get_sh_info();
832 if (symndx >= symshdr.get_sh_size() / This::sym_size)
833 {
834 this->error(_("section group %u info %u out of range"),
835 index, symndx);
836 return false;
837 }
838 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
839 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
840 false);
841 elfcpp::Sym<size, big_endian> sym(psym);
842
843 // Read the symbol table names.
844 section_size_type symnamelen;
845 const unsigned char* psymnamesu;
846 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
847 &symnamelen, true);
848 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
849
850 // Get the section group signature.
851 if (sym.get_st_name() >= symnamelen)
852 {
853 this->error(_("symbol %u name offset %u out of range"),
854 symndx, sym.get_st_name());
855 return false;
856 }
857
858 std::string signature(psymnames + sym.get_st_name());
859
860 // It seems that some versions of gas will create a section group
861 // associated with a section symbol, and then fail to give a name to
862 // the section symbol. In such a case, use the name of the section.
863 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
864 {
865 bool is_ordinary;
866 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
867 sym.get_st_shndx(),
868 &is_ordinary);
869 if (!is_ordinary || sym_shndx >= this->shnum())
870 {
871 this->error(_("symbol %u invalid section index %u"),
872 symndx, sym_shndx);
873 return false;
874 }
875 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
876 if (member_shdr.get_sh_name() < section_names_size)
877 signature = section_names + member_shdr.get_sh_name();
878 }
879
880 // Record this section group in the layout, and see whether we've already
881 // seen one with the same signature.
882 bool include_group;
883 bool is_comdat;
884 Kept_section* kept_section = NULL;
885
886 if ((flags & elfcpp::GRP_COMDAT) == 0)
887 {
888 include_group = true;
889 is_comdat = false;
890 }
891 else
892 {
893 include_group = layout->find_or_add_kept_section(signature,
894 this, index, true,
895 true, &kept_section);
896 is_comdat = true;
897 }
898
899 if (is_comdat && include_group)
900 {
901 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
902 if (incremental_inputs != NULL)
903 incremental_inputs->report_comdat_group(this, signature.c_str());
904 }
905
906 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
907
908 std::vector<unsigned int> shndxes;
909 bool relocate_group = include_group && parameters->options().relocatable();
910 if (relocate_group)
911 shndxes.reserve(count - 1);
912
913 for (size_t i = 1; i < count; ++i)
914 {
915 elfcpp::Elf_Word shndx =
916 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
917
918 if (relocate_group)
919 shndxes.push_back(shndx);
920
921 if (shndx >= this->shnum())
922 {
923 this->error(_("section %u in section group %u out of range"),
924 shndx, index);
925 continue;
926 }
927
928 // Check for an earlier section number, since we're going to get
929 // it wrong--we may have already decided to include the section.
930 if (shndx < index)
931 this->error(_("invalid section group %u refers to earlier section %u"),
932 index, shndx);
933
934 // Get the name of the member section.
935 typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size);
936 if (member_shdr.get_sh_name() >= section_names_size)
937 {
938 // This is an error, but it will be diagnosed eventually
939 // in do_layout, so we don't need to do anything here but
940 // ignore it.
941 continue;
942 }
943 std::string mname(section_names + member_shdr.get_sh_name());
944
945 if (include_group)
946 {
947 if (is_comdat)
948 kept_section->add_comdat_section(mname, shndx,
949 member_shdr.get_sh_size());
950 }
951 else
952 {
953 (*omit)[shndx] = true;
954
955 if (is_comdat)
956 {
957 Relobj* kept_object = kept_section->object();
958 if (kept_section->is_comdat())
959 {
960 // Find the corresponding kept section, and store
961 // that info in the discarded section table.
962 unsigned int kept_shndx;
963 uint64_t kept_size;
964 if (kept_section->find_comdat_section(mname, &kept_shndx,
965 &kept_size))
966 {
967 // We don't keep a mapping for this section if
968 // it has a different size. The mapping is only
969 // used for relocation processing, and we don't
970 // want to treat the sections as similar if the
971 // sizes are different. Checking the section
972 // size is the approach used by the GNU linker.
973 if (kept_size == member_shdr.get_sh_size())
974 this->set_kept_comdat_section(shndx, kept_object,
975 kept_shndx);
976 }
977 }
978 else
979 {
980 // The existing section is a linkonce section. Add
981 // a mapping if there is exactly one section in the
982 // group (which is true when COUNT == 2) and if it
983 // is the same size.
984 if (count == 2
985 && (kept_section->linkonce_size()
986 == member_shdr.get_sh_size()))
987 this->set_kept_comdat_section(shndx, kept_object,
988 kept_section->shndx());
989 }
990 }
991 }
992 }
993
994 if (relocate_group)
995 layout->layout_group(symtab, this, index, name, signature.c_str(),
996 shdr, flags, &shndxes);
997
998 return include_group;
999 }
1000
1001 // Whether to include a linkonce section in the link. NAME is the
1002 // name of the section and SHDR is the section header.
1003
1004 // Linkonce sections are a GNU extension implemented in the original
1005 // GNU linker before section groups were defined. The semantics are
1006 // that we only include one linkonce section with a given name. The
1007 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
1008 // where T is the type of section and SYMNAME is the name of a symbol.
1009 // In an attempt to make linkonce sections interact well with section
1010 // groups, we try to identify SYMNAME and use it like a section group
1011 // signature. We want to block section groups with that signature,
1012 // but not other linkonce sections with that signature. We also use
1013 // the full name of the linkonce section as a normal section group
1014 // signature.
1015
1016 template<int size, bool big_endian>
1017 bool
1018 Sized_relobj_file<size, big_endian>::include_linkonce_section(
1019 Layout* layout,
1020 unsigned int index,
1021 const char* name,
1022 const elfcpp::Shdr<size, big_endian>& shdr)
1023 {
1024 typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
1025 // In general the symbol name we want will be the string following
1026 // the last '.'. However, we have to handle the case of
1027 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
1028 // some versions of gcc. So we use a heuristic: if the name starts
1029 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
1030 // we look for the last '.'. We can't always simply skip
1031 // ".gnu.linkonce.X", because we have to deal with cases like
1032 // ".gnu.linkonce.d.rel.ro.local".
1033 const char* const linkonce_t = ".gnu.linkonce.t.";
1034 const char* symname;
1035 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
1036 symname = name + strlen(linkonce_t);
1037 else
1038 symname = strrchr(name, '.') + 1;
1039 std::string sig1(symname);
1040 std::string sig2(name);
1041 Kept_section* kept1;
1042 Kept_section* kept2;
1043 bool include1 = layout->find_or_add_kept_section(sig1, this, index, false,
1044 false, &kept1);
1045 bool include2 = layout->find_or_add_kept_section(sig2, this, index, false,
1046 true, &kept2);
1047
1048 if (!include2)
1049 {
1050 // We are not including this section because we already saw the
1051 // name of the section as a signature. This normally implies
1052 // that the kept section is another linkonce section. If it is
1053 // the same size, record it as the section which corresponds to
1054 // this one.
1055 if (kept2->object() != NULL
1056 && !kept2->is_comdat()
1057 && kept2->linkonce_size() == sh_size)
1058 this->set_kept_comdat_section(index, kept2->object(), kept2->shndx());
1059 }
1060 else if (!include1)
1061 {
1062 // The section is being discarded on the basis of its symbol
1063 // name. This means that the corresponding kept section was
1064 // part of a comdat group, and it will be difficult to identify
1065 // the specific section within that group that corresponds to
1066 // this linkonce section. We'll handle the simple case where
1067 // the group has only one member section. Otherwise, it's not
1068 // worth the effort.
1069 unsigned int kept_shndx;
1070 uint64_t kept_size;
1071 if (kept1->object() != NULL
1072 && kept1->is_comdat()
1073 && kept1->find_single_comdat_section(&kept_shndx, &kept_size)
1074 && kept_size == sh_size)
1075 this->set_kept_comdat_section(index, kept1->object(), kept_shndx);
1076 }
1077 else
1078 {
1079 kept1->set_linkonce_size(sh_size);
1080 kept2->set_linkonce_size(sh_size);
1081 }
1082
1083 return include1 && include2;
1084 }
1085
1086 // Layout an input section.
1087
1088 template<int size, bool big_endian>
1089 inline void
1090 Sized_relobj_file<size, big_endian>::layout_section(
1091 Layout* layout,
1092 unsigned int shndx,
1093 const char* name,
1094 const typename This::Shdr& shdr,
1095 unsigned int reloc_shndx,
1096 unsigned int reloc_type)
1097 {
1098 off_t offset;
1099 Output_section* os = layout->layout(this, shndx, name, shdr,
1100 reloc_shndx, reloc_type, &offset);
1101
1102 this->output_sections()[shndx] = os;
1103 if (offset == -1)
1104 this->section_offsets()[shndx] = invalid_address;
1105 else
1106 this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
1107
1108 // If this section requires special handling, and if there are
1109 // relocs that apply to it, then we must do the special handling
1110 // before we apply the relocs.
1111 if (offset == -1 && reloc_shndx != 0)
1112 this->set_relocs_must_follow_section_writes();
1113 }
1114
1115 // Layout an input .eh_frame section.
1116
1117 template<int size, bool big_endian>
1118 void
1119 Sized_relobj_file<size, big_endian>::layout_eh_frame_section(
1120 Layout* layout,
1121 const unsigned char* symbols_data,
1122 section_size_type symbols_size,
1123 const unsigned char* symbol_names_data,
1124 section_size_type symbol_names_size,
1125 unsigned int shndx,
1126 const typename This::Shdr& shdr,
1127 unsigned int reloc_shndx,
1128 unsigned int reloc_type)
1129 {
1130 gold_assert(this->has_eh_frame_);
1131
1132 off_t offset;
1133 Output_section* os = layout->layout_eh_frame(this,
1134 symbols_data,
1135 symbols_size,
1136 symbol_names_data,
1137 symbol_names_size,
1138 shndx,
1139 shdr,
1140 reloc_shndx,
1141 reloc_type,
1142 &offset);
1143 this->output_sections()[shndx] = os;
1144 if (os == NULL || offset == -1)
1145 {
1146 // An object can contain at most one section holding exception
1147 // frame information.
1148 gold_assert(this->discarded_eh_frame_shndx_ == -1U);
1149 this->discarded_eh_frame_shndx_ = shndx;
1150 this->section_offsets()[shndx] = invalid_address;
1151 }
1152 else
1153 this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
1154
1155 // If this section requires special handling, and if there are
1156 // relocs that aply to it, then we must do the special handling
1157 // before we apply the relocs.
1158 if (os != NULL && offset == -1 && reloc_shndx != 0)
1159 this->set_relocs_must_follow_section_writes();
1160 }
1161
1162 // Lay out the input sections. We walk through the sections and check
1163 // whether they should be included in the link. If they should, we
1164 // pass them to the Layout object, which will return an output section
1165 // and an offset.
1166 // During garbage collection (--gc-sections) and identical code folding
1167 // (--icf), this function is called twice. When it is called the first
1168 // time, it is for setting up some sections as roots to a work-list for
1169 // --gc-sections and to do comdat processing. Actual layout happens the
1170 // second time around after all the relevant sections have been determined.
1171 // The first time, is_worklist_ready or is_icf_ready is false. It is then
1172 // set to true after the garbage collection worklist or identical code
1173 // folding is processed and the relevant sections to be kept are
1174 // determined. Then, this function is called again to layout the sections.
1175
1176 template<int size, bool big_endian>
1177 void
1178 Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab,
1179 Layout* layout,
1180 Read_symbols_data* sd)
1181 {
1182 const unsigned int shnum = this->shnum();
1183 bool is_gc_pass_one = ((parameters->options().gc_sections()
1184 && !symtab->gc()->is_worklist_ready())
1185 || (parameters->options().icf_enabled()
1186 && !symtab->icf()->is_icf_ready()));
1187
1188 bool is_gc_pass_two = ((parameters->options().gc_sections()
1189 && symtab->gc()->is_worklist_ready())
1190 || (parameters->options().icf_enabled()
1191 && symtab->icf()->is_icf_ready()));
1192
1193 bool is_gc_or_icf = (parameters->options().gc_sections()
1194 || parameters->options().icf_enabled());
1195
1196 // Both is_gc_pass_one and is_gc_pass_two should not be true.
1197 gold_assert(!(is_gc_pass_one && is_gc_pass_two));
1198
1199 if (shnum == 0)
1200 return;
1201 Symbols_data* gc_sd = NULL;
1202 if (is_gc_pass_one)
1203 {
1204 // During garbage collection save the symbols data to use it when
1205 // re-entering this function.
1206 gc_sd = new Symbols_data;
1207 this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
1208 this->set_symbols_data(gc_sd);
1209 }
1210 else if (is_gc_pass_two)
1211 {
1212 gc_sd = this->get_symbols_data();
1213 }
1214
1215 const unsigned char* section_headers_data = NULL;
1216 section_size_type section_names_size;
1217 const unsigned char* symbols_data = NULL;
1218 section_size_type symbols_size;
1219 const unsigned char* symbol_names_data = NULL;
1220 section_size_type symbol_names_size;
1221
1222 if (is_gc_or_icf)
1223 {
1224 section_headers_data = gc_sd->section_headers_data;
1225 section_names_size = gc_sd->section_names_size;
1226 symbols_data = gc_sd->symbols_data;
1227 symbols_size = gc_sd->symbols_size;
1228 symbol_names_data = gc_sd->symbol_names_data;
1229 symbol_names_size = gc_sd->symbol_names_size;
1230 }
1231 else
1232 {
1233 section_headers_data = sd->section_headers->data();
1234 section_names_size = sd->section_names_size;
1235 if (sd->symbols != NULL)
1236 symbols_data = sd->symbols->data();
1237 symbols_size = sd->symbols_size;
1238 if (sd->symbol_names != NULL)
1239 symbol_names_data = sd->symbol_names->data();
1240 symbol_names_size = sd->symbol_names_size;
1241 }
1242
1243 // Get the section headers.
1244 const unsigned char* shdrs = section_headers_data;
1245 const unsigned char* pshdrs;
1246
1247 // Get the section names.
1248 const unsigned char* pnamesu = (is_gc_or_icf)
1249 ? gc_sd->section_names_data
1250 : sd->section_names->data();
1251
1252 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1253
1254 // If any input files have been claimed by plugins, we need to defer
1255 // actual layout until the replacement files have arrived.
1256 const bool should_defer_layout =
1257 (parameters->options().has_plugins()
1258 && parameters->options().plugins()->should_defer_layout());
1259 unsigned int num_sections_to_defer = 0;
1260
1261 // For each section, record the index of the reloc section if any.
1262 // Use 0 to mean that there is no reloc section, -1U to mean that
1263 // there is more than one.
1264 std::vector<unsigned int> reloc_shndx(shnum, 0);
1265 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
1266 // Skip the first, dummy, section.
1267 pshdrs = shdrs + This::shdr_size;
1268 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1269 {
1270 typename This::Shdr shdr(pshdrs);
1271
1272 // Count the number of sections whose layout will be deferred.
1273 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1274 ++num_sections_to_defer;
1275
1276 unsigned int sh_type = shdr.get_sh_type();
1277 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
1278 {
1279 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
1280 if (target_shndx == 0 || target_shndx >= shnum)
1281 {
1282 this->error(_("relocation section %u has bad info %u"),
1283 i, target_shndx);
1284 continue;
1285 }
1286
1287 if (reloc_shndx[target_shndx] != 0)
1288 reloc_shndx[target_shndx] = -1U;
1289 else
1290 {
1291 reloc_shndx[target_shndx] = i;
1292 reloc_type[target_shndx] = sh_type;
1293 }
1294 }
1295 }
1296
1297 Output_sections& out_sections(this->output_sections());
1298 std::vector<Address>& out_section_offsets(this->section_offsets());
1299
1300 if (!is_gc_pass_two)
1301 {
1302 out_sections.resize(shnum);
1303 out_section_offsets.resize(shnum);
1304 }
1305
1306 // If we are only linking for symbols, then there is nothing else to
1307 // do here.
1308 if (this->input_file()->just_symbols())
1309 {
1310 if (!is_gc_pass_two)
1311 {
1312 delete sd->section_headers;
1313 sd->section_headers = NULL;
1314 delete sd->section_names;
1315 sd->section_names = NULL;
1316 }
1317 return;
1318 }
1319
1320 if (num_sections_to_defer > 0)
1321 {
1322 parameters->options().plugins()->add_deferred_layout_object(this);
1323 this->deferred_layout_.reserve(num_sections_to_defer);
1324 }
1325
1326 // Whether we've seen a .note.GNU-stack section.
1327 bool seen_gnu_stack = false;
1328 // The flags of a .note.GNU-stack section.
1329 uint64_t gnu_stack_flags = 0;
1330
1331 // Keep track of which sections to omit.
1332 std::vector<bool> omit(shnum, false);
1333
1334 // Keep track of reloc sections when emitting relocations.
1335 const bool relocatable = parameters->options().relocatable();
1336 const bool emit_relocs = (relocatable
1337 || parameters->options().emit_relocs());
1338 std::vector<unsigned int> reloc_sections;
1339
1340 // Keep track of .eh_frame sections.
1341 std::vector<unsigned int> eh_frame_sections;
1342
1343 // Keep track of .debug_info and .debug_types sections.
1344 std::vector<unsigned int> debug_info_sections;
1345 std::vector<unsigned int> debug_types_sections;
1346
1347 // Skip the first, dummy, section.
1348 pshdrs = shdrs + This::shdr_size;
1349 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1350 {
1351 typename This::Shdr shdr(pshdrs);
1352
1353 if (shdr.get_sh_name() >= section_names_size)
1354 {
1355 this->error(_("bad section name offset for section %u: %lu"),
1356 i, static_cast<unsigned long>(shdr.get_sh_name()));
1357 return;
1358 }
1359
1360 const char* name = pnames + shdr.get_sh_name();
1361
1362 if (!is_gc_pass_two)
1363 {
1364 if (this->handle_gnu_warning_section(name, i, symtab))
1365 {
1366 if (!relocatable && !parameters->options().shared())
1367 omit[i] = true;
1368 }
1369
1370 // The .note.GNU-stack section is special. It gives the
1371 // protection flags that this object file requires for the stack
1372 // in memory.
1373 if (strcmp(name, ".note.GNU-stack") == 0)
1374 {
1375 seen_gnu_stack = true;
1376 gnu_stack_flags |= shdr.get_sh_flags();
1377 omit[i] = true;
1378 }
1379
1380 // The .note.GNU-split-stack section is also special. It
1381 // indicates that the object was compiled with
1382 // -fsplit-stack.
1383 if (this->handle_split_stack_section(name))
1384 {
1385 if (!relocatable && !parameters->options().shared())
1386 omit[i] = true;
1387 }
1388
1389 // Skip attributes section.
1390 if (parameters->target().is_attributes_section(name))
1391 {
1392 omit[i] = true;
1393 }
1394
1395 bool discard = omit[i];
1396 if (!discard)
1397 {
1398 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
1399 {
1400 if (!this->include_section_group(symtab, layout, i, name,
1401 shdrs, pnames,
1402 section_names_size,
1403 &omit))
1404 discard = true;
1405 }
1406 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
1407 && Layout::is_linkonce(name))
1408 {
1409 if (!this->include_linkonce_section(layout, i, name, shdr))
1410 discard = true;
1411 }
1412 }
1413
1414 // Add the section to the incremental inputs layout.
1415 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1416 if (incremental_inputs != NULL
1417 && !discard
1418 && can_incremental_update(shdr.get_sh_type()))
1419 {
1420 off_t sh_size = shdr.get_sh_size();
1421 section_size_type uncompressed_size;
1422 if (this->section_is_compressed(i, &uncompressed_size))
1423 sh_size = uncompressed_size;
1424 incremental_inputs->report_input_section(this, i, name, sh_size);
1425 }
1426
1427 if (discard)
1428 {
1429 // Do not include this section in the link.
1430 out_sections[i] = NULL;
1431 out_section_offsets[i] = invalid_address;
1432 continue;
1433 }
1434 }
1435
1436 if (is_gc_pass_one && parameters->options().gc_sections())
1437 {
1438 if (this->is_section_name_included(name)
1439 || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY
1440 || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
1441 {
1442 symtab->gc()->worklist().push(Section_id(this, i));
1443 }
1444 // If the section name XXX can be represented as a C identifier
1445 // it cannot be discarded if there are references to
1446 // __start_XXX and __stop_XXX symbols. These need to be
1447 // specially handled.
1448 if (is_cident(name))
1449 {
1450 symtab->gc()->add_cident_section(name, Section_id(this, i));
1451 }
1452 }
1453
1454 // When doing a relocatable link we are going to copy input
1455 // reloc sections into the output. We only want to copy the
1456 // ones associated with sections which are not being discarded.
1457 // However, we don't know that yet for all sections. So save
1458 // reloc sections and process them later. Garbage collection is
1459 // not triggered when relocatable code is desired.
1460 if (emit_relocs
1461 && (shdr.get_sh_type() == elfcpp::SHT_REL
1462 || shdr.get_sh_type() == elfcpp::SHT_RELA))
1463 {
1464 reloc_sections.push_back(i);
1465 continue;
1466 }
1467
1468 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
1469 continue;
1470
1471 // The .eh_frame section is special. It holds exception frame
1472 // information that we need to read in order to generate the
1473 // exception frame header. We process these after all the other
1474 // sections so that the exception frame reader can reliably
1475 // determine which sections are being discarded, and discard the
1476 // corresponding information.
1477 if (!relocatable
1478 && strcmp(name, ".eh_frame") == 0
1479 && this->check_eh_frame_flags(&shdr))
1480 {
1481 if (is_gc_pass_one)
1482 {
1483 out_sections[i] = reinterpret_cast<Output_section*>(1);
1484 out_section_offsets[i] = invalid_address;
1485 }
1486 else if (should_defer_layout)
1487 this->deferred_layout_.push_back(Deferred_layout(i, name,
1488 pshdrs,
1489 reloc_shndx[i],
1490 reloc_type[i]));
1491 else
1492 eh_frame_sections.push_back(i);
1493 continue;
1494 }
1495
1496 if (is_gc_pass_two && parameters->options().gc_sections())
1497 {
1498 // This is executed during the second pass of garbage
1499 // collection. do_layout has been called before and some
1500 // sections have been already discarded. Simply ignore
1501 // such sections this time around.
1502 if (out_sections[i] == NULL)
1503 {
1504 gold_assert(out_section_offsets[i] == invalid_address);
1505 continue;
1506 }
1507 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1508 && symtab->gc()->is_section_garbage(this, i))
1509 {
1510 if (parameters->options().print_gc_sections())
1511 gold_info(_("%s: removing unused section from '%s'"
1512 " in file '%s'"),
1513 program_name, this->section_name(i).c_str(),
1514 this->name().c_str());
1515 out_sections[i] = NULL;
1516 out_section_offsets[i] = invalid_address;
1517 continue;
1518 }
1519 }
1520
1521 if (is_gc_pass_two && parameters->options().icf_enabled())
1522 {
1523 if (out_sections[i] == NULL)
1524 {
1525 gold_assert(out_section_offsets[i] == invalid_address);
1526 continue;
1527 }
1528 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1529 && symtab->icf()->is_section_folded(this, i))
1530 {
1531 if (parameters->options().print_icf_sections())
1532 {
1533 Section_id folded =
1534 symtab->icf()->get_folded_section(this, i);
1535 Relobj* folded_obj =
1536 reinterpret_cast<Relobj*>(folded.first);
1537 gold_info(_("%s: ICF folding section '%s' in file '%s'"
1538 "into '%s' in file '%s'"),
1539 program_name, this->section_name(i).c_str(),
1540 this->name().c_str(),
1541 folded_obj->section_name(folded.second).c_str(),
1542 folded_obj->name().c_str());
1543 }
1544 out_sections[i] = NULL;
1545 out_section_offsets[i] = invalid_address;
1546 continue;
1547 }
1548 }
1549
1550 // Defer layout here if input files are claimed by plugins. When gc
1551 // is turned on this function is called twice. For the second call
1552 // should_defer_layout should be false.
1553 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1554 {
1555 gold_assert(!is_gc_pass_two);
1556 this->deferred_layout_.push_back(Deferred_layout(i, name,
1557 pshdrs,
1558 reloc_shndx[i],
1559 reloc_type[i]));
1560 // Put dummy values here; real values will be supplied by
1561 // do_layout_deferred_sections.
1562 out_sections[i] = reinterpret_cast<Output_section*>(2);
1563 out_section_offsets[i] = invalid_address;
1564 continue;
1565 }
1566
1567 // During gc_pass_two if a section that was previously deferred is
1568 // found, do not layout the section as layout_deferred_sections will
1569 // do it later from gold.cc.
1570 if (is_gc_pass_two
1571 && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
1572 continue;
1573
1574 if (is_gc_pass_one)
1575 {
1576 // This is during garbage collection. The out_sections are
1577 // assigned in the second call to this function.
1578 out_sections[i] = reinterpret_cast<Output_section*>(1);
1579 out_section_offsets[i] = invalid_address;
1580 }
1581 else
1582 {
1583 // When garbage collection is switched on the actual layout
1584 // only happens in the second call.
1585 this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1586 reloc_type[i]);
1587
1588 // When generating a .gdb_index section, we do additional
1589 // processing of .debug_info and .debug_types sections after all
1590 // the other sections for the same reason as above.
1591 if (!relocatable
1592 && parameters->options().gdb_index()
1593 && !(shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1594 {
1595 if (strcmp(name, ".debug_info") == 0
1596 || strcmp(name, ".zdebug_info") == 0)
1597 debug_info_sections.push_back(i);
1598 else if (strcmp(name, ".debug_types") == 0
1599 || strcmp(name, ".zdebug_types") == 0)
1600 debug_types_sections.push_back(i);
1601 }
1602 }
1603 }
1604
1605 if (!is_gc_pass_two)
1606 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this);
1607
1608 // When doing a relocatable link handle the reloc sections at the
1609 // end. Garbage collection and Identical Code Folding is not
1610 // turned on for relocatable code.
1611 if (emit_relocs)
1612 this->size_relocatable_relocs();
1613
1614 gold_assert(!(is_gc_or_icf) || reloc_sections.empty());
1615
1616 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1617 p != reloc_sections.end();
1618 ++p)
1619 {
1620 unsigned int i = *p;
1621 const unsigned char* pshdr;
1622 pshdr = section_headers_data + i * This::shdr_size;
1623 typename This::Shdr shdr(pshdr);
1624
1625 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1626 if (data_shndx >= shnum)
1627 {
1628 // We already warned about this above.
1629 continue;
1630 }
1631
1632 Output_section* data_section = out_sections[data_shndx];
1633 if (data_section == reinterpret_cast<Output_section*>(2))
1634 {
1635 // The layout for the data section was deferred, so we need
1636 // to defer the relocation section, too.
1637 const char* name = pnames + shdr.get_sh_name();
1638 this->deferred_layout_relocs_.push_back(
1639 Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL));
1640 out_sections[i] = reinterpret_cast<Output_section*>(2);
1641 out_section_offsets[i] = invalid_address;
1642 continue;
1643 }
1644 if (data_section == NULL)
1645 {
1646 out_sections[i] = NULL;
1647 out_section_offsets[i] = invalid_address;
1648 continue;
1649 }
1650
1651 Relocatable_relocs* rr = new Relocatable_relocs();
1652 this->set_relocatable_relocs(i, rr);
1653
1654 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1655 rr);
1656 out_sections[i] = os;
1657 out_section_offsets[i] = invalid_address;
1658 }
1659
1660 // Handle the .eh_frame sections at the end.
1661 gold_assert(!is_gc_pass_one || eh_frame_sections.empty());
1662 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1663 p != eh_frame_sections.end();
1664 ++p)
1665 {
1666 unsigned int i = *p;
1667 const unsigned char* pshdr;
1668 pshdr = section_headers_data + i * This::shdr_size;
1669 typename This::Shdr shdr(pshdr);
1670
1671 this->layout_eh_frame_section(layout,
1672 symbols_data,
1673 symbols_size,
1674 symbol_names_data,
1675 symbol_names_size,
1676 i,
1677 shdr,
1678 reloc_shndx[i],
1679 reloc_type[i]);
1680 }
1681
1682 // When building a .gdb_index section, scan the .debug_info and
1683 // .debug_types sections.
1684 gold_assert(!is_gc_pass_one
1685 || (debug_info_sections.empty() && debug_types_sections.empty()));
1686 for (std::vector<unsigned int>::const_iterator p
1687 = debug_info_sections.begin();
1688 p != debug_info_sections.end();
1689 ++p)
1690 {
1691 unsigned int i = *p;
1692 layout->add_to_gdb_index(false, this, symbols_data, symbols_size,
1693 i, reloc_shndx[i], reloc_type[i]);
1694 }
1695 for (std::vector<unsigned int>::const_iterator p
1696 = debug_types_sections.begin();
1697 p != debug_types_sections.end();
1698 ++p)
1699 {
1700 unsigned int i = *p;
1701 layout->add_to_gdb_index(true, this, symbols_data, symbols_size,
1702 i, reloc_shndx[i], reloc_type[i]);
1703 }
1704
1705 if (is_gc_pass_two)
1706 {
1707 delete[] gc_sd->section_headers_data;
1708 delete[] gc_sd->section_names_data;
1709 delete[] gc_sd->symbols_data;
1710 delete[] gc_sd->symbol_names_data;
1711 this->set_symbols_data(NULL);
1712 }
1713 else
1714 {
1715 delete sd->section_headers;
1716 sd->section_headers = NULL;
1717 delete sd->section_names;
1718 sd->section_names = NULL;
1719 }
1720 }
1721
1722 // Layout sections whose layout was deferred while waiting for
1723 // input files from a plugin.
1724
1725 template<int size, bool big_endian>
1726 void
1727 Sized_relobj_file<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1728 {
1729 typename std::vector<Deferred_layout>::iterator deferred;
1730
1731 for (deferred = this->deferred_layout_.begin();
1732 deferred != this->deferred_layout_.end();
1733 ++deferred)
1734 {
1735 typename This::Shdr shdr(deferred->shdr_data_);
1736 // If the section is not included, it is because the garbage collector
1737 // decided it is not needed. Avoid reverting that decision.
1738 if (!this->is_section_included(deferred->shndx_))
1739 continue;
1740
1741 if (parameters->options().relocatable()
1742 || deferred->name_ != ".eh_frame"
1743 || !this->check_eh_frame_flags(&shdr))
1744 this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1745 shdr, deferred->reloc_shndx_,
1746 deferred->reloc_type_);
1747 else
1748 {
1749 // Reading the symbols again here may be slow.
1750 Read_symbols_data sd;
1751 this->read_symbols(&sd);
1752 this->layout_eh_frame_section(layout,
1753 sd.symbols->data(),
1754 sd.symbols_size,
1755 sd.symbol_names->data(),
1756 sd.symbol_names_size,
1757 deferred->shndx_,
1758 shdr,
1759 deferred->reloc_shndx_,
1760 deferred->reloc_type_);
1761 }
1762 }
1763
1764 this->deferred_layout_.clear();
1765
1766 // Now handle the deferred relocation sections.
1767
1768 Output_sections& out_sections(this->output_sections());
1769 std::vector<Address>& out_section_offsets(this->section_offsets());
1770
1771 for (deferred = this->deferred_layout_relocs_.begin();
1772 deferred != this->deferred_layout_relocs_.end();
1773 ++deferred)
1774 {
1775 unsigned int shndx = deferred->shndx_;
1776 typename This::Shdr shdr(deferred->shdr_data_);
1777 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1778
1779 Output_section* data_section = out_sections[data_shndx];
1780 if (data_section == NULL)
1781 {
1782 out_sections[shndx] = NULL;
1783 out_section_offsets[shndx] = invalid_address;
1784 continue;
1785 }
1786
1787 Relocatable_relocs* rr = new Relocatable_relocs();
1788 this->set_relocatable_relocs(shndx, rr);
1789
1790 Output_section* os = layout->layout_reloc(this, shndx, shdr,
1791 data_section, rr);
1792 out_sections[shndx] = os;
1793 out_section_offsets[shndx] = invalid_address;
1794 }
1795 }
1796
1797 // Add the symbols to the symbol table.
1798
1799 template<int size, bool big_endian>
1800 void
1801 Sized_relobj_file<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1802 Read_symbols_data* sd,
1803 Layout*)
1804 {
1805 if (sd->symbols == NULL)
1806 {
1807 gold_assert(sd->symbol_names == NULL);
1808 return;
1809 }
1810
1811 const int sym_size = This::sym_size;
1812 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1813 / sym_size);
1814 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1815 {
1816 this->error(_("size of symbols is not multiple of symbol size"));
1817 return;
1818 }
1819
1820 this->symbols_.resize(symcount);
1821
1822 const char* sym_names =
1823 reinterpret_cast<const char*>(sd->symbol_names->data());
1824 symtab->add_from_relobj(this,
1825 sd->symbols->data() + sd->external_symbols_offset,
1826 symcount, this->local_symbol_count_,
1827 sym_names, sd->symbol_names_size,
1828 &this->symbols_,
1829 &this->defined_count_);
1830
1831 delete sd->symbols;
1832 sd->symbols = NULL;
1833 delete sd->symbol_names;
1834 sd->symbol_names = NULL;
1835 }
1836
1837 // Find out if this object, that is a member of a lib group, should be included
1838 // in the link. We check every symbol defined by this object. If the symbol
1839 // table has a strong undefined reference to that symbol, we have to include
1840 // the object.
1841
1842 template<int size, bool big_endian>
1843 Archive::Should_include
1844 Sized_relobj_file<size, big_endian>::do_should_include_member(
1845 Symbol_table* symtab,
1846 Layout* layout,
1847 Read_symbols_data* sd,
1848 std::string* why)
1849 {
1850 char* tmpbuf = NULL;
1851 size_t tmpbuflen = 0;
1852 const char* sym_names =
1853 reinterpret_cast<const char*>(sd->symbol_names->data());
1854 const unsigned char* syms =
1855 sd->symbols->data() + sd->external_symbols_offset;
1856 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1857 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1858 / sym_size);
1859
1860 const unsigned char* p = syms;
1861
1862 for (size_t i = 0; i < symcount; ++i, p += sym_size)
1863 {
1864 elfcpp::Sym<size, big_endian> sym(p);
1865 unsigned int st_shndx = sym.get_st_shndx();
1866 if (st_shndx == elfcpp::SHN_UNDEF)
1867 continue;
1868
1869 unsigned int st_name = sym.get_st_name();
1870 const char* name = sym_names + st_name;
1871 Symbol* symbol;
1872 Archive::Should_include t = Archive::should_include_member(symtab,
1873 layout,
1874 name,
1875 &symbol, why,
1876 &tmpbuf,
1877 &tmpbuflen);
1878 if (t == Archive::SHOULD_INCLUDE_YES)
1879 {
1880 if (tmpbuf != NULL)
1881 free(tmpbuf);
1882 return t;
1883 }
1884 }
1885 if (tmpbuf != NULL)
1886 free(tmpbuf);
1887 return Archive::SHOULD_INCLUDE_UNKNOWN;
1888 }
1889
1890 // Iterate over global defined symbols, calling a visitor class V for each.
1891
1892 template<int size, bool big_endian>
1893 void
1894 Sized_relobj_file<size, big_endian>::do_for_all_global_symbols(
1895 Read_symbols_data* sd,
1896 Library_base::Symbol_visitor_base* v)
1897 {
1898 const char* sym_names =
1899 reinterpret_cast<const char*>(sd->symbol_names->data());
1900 const unsigned char* syms =
1901 sd->symbols->data() + sd->external_symbols_offset;
1902 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1903 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1904 / sym_size);
1905 const unsigned char* p = syms;
1906
1907 for (size_t i = 0; i < symcount; ++i, p += sym_size)
1908 {
1909 elfcpp::Sym<size, big_endian> sym(p);
1910 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF)
1911 v->visit(sym_names + sym.get_st_name());
1912 }
1913 }
1914
1915 // Return whether the local symbol SYMNDX has a PLT offset.
1916
1917 template<int size, bool big_endian>
1918 bool
1919 Sized_relobj_file<size, big_endian>::local_has_plt_offset(
1920 unsigned int symndx) const
1921 {
1922 typename Local_plt_offsets::const_iterator p =
1923 this->local_plt_offsets_.find(symndx);
1924 return p != this->local_plt_offsets_.end();
1925 }
1926
1927 // Get the PLT offset of a local symbol.
1928
1929 template<int size, bool big_endian>
1930 unsigned int
1931 Sized_relobj_file<size, big_endian>::do_local_plt_offset(
1932 unsigned int symndx) const
1933 {
1934 typename Local_plt_offsets::const_iterator p =
1935 this->local_plt_offsets_.find(symndx);
1936 gold_assert(p != this->local_plt_offsets_.end());
1937 return p->second;
1938 }
1939
1940 // Set the PLT offset of a local symbol.
1941
1942 template<int size, bool big_endian>
1943 void
1944 Sized_relobj_file<size, big_endian>::set_local_plt_offset(
1945 unsigned int symndx, unsigned int plt_offset)
1946 {
1947 std::pair<typename Local_plt_offsets::iterator, bool> ins =
1948 this->local_plt_offsets_.insert(std::make_pair(symndx, plt_offset));
1949 gold_assert(ins.second);
1950 }
1951
1952 // First pass over the local symbols. Here we add their names to
1953 // *POOL and *DYNPOOL, and we store the symbol value in
1954 // THIS->LOCAL_VALUES_. This function is always called from a
1955 // singleton thread. This is followed by a call to
1956 // finalize_local_symbols.
1957
1958 template<int size, bool big_endian>
1959 void
1960 Sized_relobj_file<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1961 Stringpool* dynpool)
1962 {
1963 gold_assert(this->symtab_shndx_ != -1U);
1964 if (this->symtab_shndx_ == 0)
1965 {
1966 // This object has no symbols. Weird but legal.
1967 return;
1968 }
1969
1970 // Read the symbol table section header.
1971 const unsigned int symtab_shndx = this->symtab_shndx_;
1972 typename This::Shdr symtabshdr(this,
1973 this->elf_file_.section_header(symtab_shndx));
1974 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1975
1976 // Read the local symbols.
1977 const int sym_size = This::sym_size;
1978 const unsigned int loccount = this->local_symbol_count_;
1979 gold_assert(loccount == symtabshdr.get_sh_info());
1980 off_t locsize = loccount * sym_size;
1981 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1982 locsize, true, true);
1983
1984 // Read the symbol names.
1985 const unsigned int strtab_shndx =
1986 this->adjust_shndx(symtabshdr.get_sh_link());
1987 section_size_type strtab_size;
1988 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1989 &strtab_size,
1990 true);
1991 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1992
1993 // Loop over the local symbols.
1994
1995 const Output_sections& out_sections(this->output_sections());
1996 unsigned int shnum = this->shnum();
1997 unsigned int count = 0;
1998 unsigned int dyncount = 0;
1999 // Skip the first, dummy, symbol.
2000 psyms += sym_size;
2001 bool strip_all = parameters->options().strip_all();
2002 bool discard_all = parameters->options().discard_all();
2003 bool discard_locals = parameters->options().discard_locals();
2004 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2005 {
2006 elfcpp::Sym<size, big_endian> sym(psyms);
2007
2008 Symbol_value<size>& lv(this->local_values_[i]);
2009
2010 bool is_ordinary;
2011 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
2012 &is_ordinary);
2013 lv.set_input_shndx(shndx, is_ordinary);
2014
2015 if (sym.get_st_type() == elfcpp::STT_SECTION)
2016 lv.set_is_section_symbol();
2017 else if (sym.get_st_type() == elfcpp::STT_TLS)
2018 lv.set_is_tls_symbol();
2019 else if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
2020 lv.set_is_ifunc_symbol();
2021
2022 // Save the input symbol value for use in do_finalize_local_symbols().
2023 lv.set_input_value(sym.get_st_value());
2024
2025 // Decide whether this symbol should go into the output file.
2026
2027 if ((shndx < shnum && out_sections[shndx] == NULL)
2028 || shndx == this->discarded_eh_frame_shndx_)
2029 {
2030 lv.set_no_output_symtab_entry();
2031 gold_assert(!lv.needs_output_dynsym_entry());
2032 continue;
2033 }
2034
2035 if (sym.get_st_type() == elfcpp::STT_SECTION)
2036 {
2037 lv.set_no_output_symtab_entry();
2038 gold_assert(!lv.needs_output_dynsym_entry());
2039 continue;
2040 }
2041
2042 if (sym.get_st_name() >= strtab_size)
2043 {
2044 this->error(_("local symbol %u section name out of range: %u >= %u"),
2045 i, sym.get_st_name(),
2046 static_cast<unsigned int>(strtab_size));
2047 lv.set_no_output_symtab_entry();
2048 continue;
2049 }
2050
2051 const char* name = pnames + sym.get_st_name();
2052
2053 // If needed, add the symbol to the dynamic symbol table string pool.
2054 if (lv.needs_output_dynsym_entry())
2055 {
2056 dynpool->add(name, true, NULL);
2057 ++dyncount;
2058 }
2059
2060 if (strip_all
2061 || (discard_all && lv.may_be_discarded_from_output_symtab()))
2062 {
2063 lv.set_no_output_symtab_entry();
2064 continue;
2065 }
2066
2067 // If --discard-locals option is used, discard all temporary local
2068 // symbols. These symbols start with system-specific local label
2069 // prefixes, typically .L for ELF system. We want to be compatible
2070 // with GNU ld so here we essentially use the same check in
2071 // bfd_is_local_label(). The code is different because we already
2072 // know that:
2073 //
2074 // - the symbol is local and thus cannot have global or weak binding.
2075 // - the symbol is not a section symbol.
2076 // - the symbol has a name.
2077 //
2078 // We do not discard a symbol if it needs a dynamic symbol entry.
2079 if (discard_locals
2080 && sym.get_st_type() != elfcpp::STT_FILE
2081 && !lv.needs_output_dynsym_entry()
2082 && lv.may_be_discarded_from_output_symtab()
2083 && parameters->target().is_local_label_name(name))
2084 {
2085 lv.set_no_output_symtab_entry();
2086 continue;
2087 }
2088
2089 // Discard the local symbol if -retain_symbols_file is specified
2090 // and the local symbol is not in that file.
2091 if (!parameters->options().should_retain_symbol(name))
2092 {
2093 lv.set_no_output_symtab_entry();
2094 continue;
2095 }
2096
2097 // Add the symbol to the symbol table string pool.
2098 pool->add(name, true, NULL);
2099 ++count;
2100 }
2101
2102 this->output_local_symbol_count_ = count;
2103 this->output_local_dynsym_count_ = dyncount;
2104 }
2105
2106 // Compute the final value of a local symbol.
2107
2108 template<int size, bool big_endian>
2109 typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
2110 Sized_relobj_file<size, big_endian>::compute_final_local_value_internal(
2111 unsigned int r_sym,
2112 const Symbol_value<size>* lv_in,
2113 Symbol_value<size>* lv_out,
2114 bool relocatable,
2115 const Output_sections& out_sections,
2116 const std::vector<Address>& out_offsets,
2117 const Symbol_table* symtab)
2118 {
2119 // We are going to overwrite *LV_OUT, if it has a merged symbol value,
2120 // we may have a memory leak.
2121 gold_assert(lv_out->has_output_value());
2122
2123 bool is_ordinary;
2124 unsigned int shndx = lv_in->input_shndx(&is_ordinary);
2125
2126 // Set the output symbol value.
2127
2128 if (!is_ordinary)
2129 {
2130 if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx))
2131 lv_out->set_output_value(lv_in->input_value());
2132 else
2133 {
2134 this->error(_("unknown section index %u for local symbol %u"),
2135 shndx, r_sym);
2136 lv_out->set_output_value(0);
2137 return This::CFLV_ERROR;
2138 }
2139 }
2140 else
2141 {
2142 if (shndx >= this->shnum())
2143 {
2144 this->error(_("local symbol %u section index %u out of range"),
2145 r_sym, shndx);
2146 lv_out->set_output_value(0);
2147 return This::CFLV_ERROR;
2148 }
2149
2150 Output_section* os = out_sections[shndx];
2151 Address secoffset = out_offsets[shndx];
2152 if (symtab->is_section_folded(this, shndx))
2153 {
2154 gold_assert(os == NULL && secoffset == invalid_address);
2155 // Get the os of the section it is folded onto.
2156 Section_id folded = symtab->icf()->get_folded_section(this,
2157 shndx);
2158 gold_assert(folded.first != NULL);
2159 Sized_relobj_file<size, big_endian>* folded_obj = reinterpret_cast
2160 <Sized_relobj_file<size, big_endian>*>(folded.first);
2161 os = folded_obj->output_section(folded.second);
2162 gold_assert(os != NULL);
2163 secoffset = folded_obj->get_output_section_offset(folded.second);
2164
2165 // This could be a relaxed input section.
2166 if (secoffset == invalid_address)
2167 {
2168 const Output_relaxed_input_section* relaxed_section =
2169 os->find_relaxed_input_section(folded_obj, folded.second);
2170 gold_assert(relaxed_section != NULL);
2171 secoffset = relaxed_section->address() - os->address();
2172 }
2173 }
2174
2175 if (os == NULL)
2176 {
2177 // This local symbol belongs to a section we are discarding.
2178 // In some cases when applying relocations later, we will
2179 // attempt to match it to the corresponding kept section,
2180 // so we leave the input value unchanged here.
2181 return This::CFLV_DISCARDED;
2182 }
2183 else if (secoffset == invalid_address)
2184 {
2185 uint64_t start;
2186
2187 // This is a SHF_MERGE section or one which otherwise
2188 // requires special handling.
2189 if (shndx == this->discarded_eh_frame_shndx_)
2190 {
2191 // This local symbol belongs to a discarded .eh_frame
2192 // section. Just treat it like the case in which
2193 // os == NULL above.
2194 gold_assert(this->has_eh_frame_);
2195 return This::CFLV_DISCARDED;
2196 }
2197 else if (!lv_in->is_section_symbol())
2198 {
2199 // This is not a section symbol. We can determine
2200 // the final value now.
2201 lv_out->set_output_value(
2202 os->output_address(this, shndx, lv_in->input_value()));
2203 }
2204 else if (!os->find_starting_output_address(this, shndx, &start))
2205 {
2206 // This is a section symbol, but apparently not one in a
2207 // merged section. First check to see if this is a relaxed
2208 // input section. If so, use its address. Otherwise just
2209 // use the start of the output section. This happens with
2210 // relocatable links when the input object has section
2211 // symbols for arbitrary non-merge sections.
2212 const Output_section_data* posd =
2213 os->find_relaxed_input_section(this, shndx);
2214 if (posd != NULL)
2215 {
2216 Address relocatable_link_adjustment =
2217 relocatable ? os->address() : 0;
2218 lv_out->set_output_value(posd->address()
2219 - relocatable_link_adjustment);
2220 }
2221 else
2222 lv_out->set_output_value(os->address());
2223 }
2224 else
2225 {
2226 // We have to consider the addend to determine the
2227 // value to use in a relocation. START is the start
2228 // of this input section. If we are doing a relocatable
2229 // link, use offset from start output section instead of
2230 // address.
2231 Address adjusted_start =
2232 relocatable ? start - os->address() : start;
2233 Merged_symbol_value<size>* msv =
2234 new Merged_symbol_value<size>(lv_in->input_value(),
2235 adjusted_start);
2236 lv_out->set_merged_symbol_value(msv);
2237 }
2238 }
2239 else if (lv_in->is_tls_symbol())
2240 lv_out->set_output_value(os->tls_offset()
2241 + secoffset
2242 + lv_in->input_value());
2243 else
2244 lv_out->set_output_value((relocatable ? 0 : os->address())
2245 + secoffset
2246 + lv_in->input_value());
2247 }
2248 return This::CFLV_OK;
2249 }
2250
2251 // Compute final local symbol value. R_SYM is the index of a local
2252 // symbol in symbol table. LV points to a symbol value, which is
2253 // expected to hold the input value and to be over-written by the
2254 // final value. SYMTAB points to a symbol table. Some targets may want
2255 // to know would-be-finalized local symbol values in relaxation.
2256 // Hence we provide this method. Since this method updates *LV, a
2257 // callee should make a copy of the original local symbol value and
2258 // use the copy instead of modifying an object's local symbols before
2259 // everything is finalized. The caller should also free up any allocated
2260 // memory in the return value in *LV.
2261 template<int size, bool big_endian>
2262 typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
2263 Sized_relobj_file<size, big_endian>::compute_final_local_value(
2264 unsigned int r_sym,
2265 const Symbol_value<size>* lv_in,
2266 Symbol_value<size>* lv_out,
2267 const Symbol_table* symtab)
2268 {
2269 // This is just a wrapper of compute_final_local_value_internal.
2270 const bool relocatable = parameters->options().relocatable();
2271 const Output_sections& out_sections(this->output_sections());
2272 const std::vector<Address>& out_offsets(this->section_offsets());
2273 return this->compute_final_local_value_internal(r_sym, lv_in, lv_out,
2274 relocatable, out_sections,
2275 out_offsets, symtab);
2276 }
2277
2278 // Finalize the local symbols. Here we set the final value in
2279 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
2280 // This function is always called from a singleton thread. The actual
2281 // output of the local symbols will occur in a separate task.
2282
2283 template<int size, bool big_endian>
2284 unsigned int
2285 Sized_relobj_file<size, big_endian>::do_finalize_local_symbols(
2286 unsigned int index,
2287 off_t off,
2288 Symbol_table* symtab)
2289 {
2290 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
2291
2292 const unsigned int loccount = this->local_symbol_count_;
2293 this->local_symbol_offset_ = off;
2294
2295 const bool relocatable = parameters->options().relocatable();
2296 const Output_sections& out_sections(this->output_sections());
2297 const std::vector<Address>& out_offsets(this->section_offsets());
2298
2299 for (unsigned int i = 1; i < loccount; ++i)
2300 {
2301 Symbol_value<size>* lv = &this->local_values_[i];
2302
2303 Compute_final_local_value_status cflv_status =
2304 this->compute_final_local_value_internal(i, lv, lv, relocatable,
2305 out_sections, out_offsets,
2306 symtab);
2307 switch (cflv_status)
2308 {
2309 case CFLV_OK:
2310 if (!lv->is_output_symtab_index_set())
2311 {
2312 lv->set_output_symtab_index(index);
2313 ++index;
2314 }
2315 break;
2316 case CFLV_DISCARDED:
2317 case CFLV_ERROR:
2318 // Do nothing.
2319 break;
2320 default:
2321 gold_unreachable();
2322 }
2323 }
2324 return index;
2325 }
2326
2327 // Set the output dynamic symbol table indexes for the local variables.
2328
2329 template<int size, bool big_endian>
2330 unsigned int
2331 Sized_relobj_file<size, big_endian>::do_set_local_dynsym_indexes(
2332 unsigned int index)
2333 {
2334 const unsigned int loccount = this->local_symbol_count_;
2335 for (unsigned int i = 1; i < loccount; ++i)
2336 {
2337 Symbol_value<size>& lv(this->local_values_[i]);
2338 if (lv.needs_output_dynsym_entry())
2339 {
2340 lv.set_output_dynsym_index(index);
2341 ++index;
2342 }
2343 }
2344 return index;
2345 }
2346
2347 // Set the offset where local dynamic symbol information will be stored.
2348 // Returns the count of local symbols contributed to the symbol table by
2349 // this object.
2350
2351 template<int size, bool big_endian>
2352 unsigned int
2353 Sized_relobj_file<size, big_endian>::do_set_local_dynsym_offset(off_t off)
2354 {
2355 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
2356 this->local_dynsym_offset_ = off;
2357 return this->output_local_dynsym_count_;
2358 }
2359
2360 // If Symbols_data is not NULL get the section flags from here otherwise
2361 // get it from the file.
2362
2363 template<int size, bool big_endian>
2364 uint64_t
2365 Sized_relobj_file<size, big_endian>::do_section_flags(unsigned int shndx)
2366 {
2367 Symbols_data* sd = this->get_symbols_data();
2368 if (sd != NULL)
2369 {
2370 const unsigned char* pshdrs = sd->section_headers_data
2371 + This::shdr_size * shndx;
2372 typename This::Shdr shdr(pshdrs);
2373 return shdr.get_sh_flags();
2374 }
2375 // If sd is NULL, read the section header from the file.
2376 return this->elf_file_.section_flags(shndx);
2377 }
2378
2379 // Get the section's ent size from Symbols_data. Called by get_section_contents
2380 // in icf.cc
2381
2382 template<int size, bool big_endian>
2383 uint64_t
2384 Sized_relobj_file<size, big_endian>::do_section_entsize(unsigned int shndx)
2385 {
2386 Symbols_data* sd = this->get_symbols_data();
2387 gold_assert(sd != NULL);
2388
2389 const unsigned char* pshdrs = sd->section_headers_data
2390 + This::shdr_size * shndx;
2391 typename This::Shdr shdr(pshdrs);
2392 return shdr.get_sh_entsize();
2393 }
2394
2395 // Write out the local symbols.
2396
2397 template<int size, bool big_endian>
2398 void
2399 Sized_relobj_file<size, big_endian>::write_local_symbols(
2400 Output_file* of,
2401 const Stringpool* sympool,
2402 const Stringpool* dynpool,
2403 Output_symtab_xindex* symtab_xindex,
2404 Output_symtab_xindex* dynsym_xindex,
2405 off_t symtab_off)
2406 {
2407 const bool strip_all = parameters->options().strip_all();
2408 if (strip_all)
2409 {
2410 if (this->output_local_dynsym_count_ == 0)
2411 return;
2412 this->output_local_symbol_count_ = 0;
2413 }
2414
2415 gold_assert(this->symtab_shndx_ != -1U);
2416 if (this->symtab_shndx_ == 0)
2417 {
2418 // This object has no symbols. Weird but legal.
2419 return;
2420 }
2421
2422 // Read the symbol table section header.
2423 const unsigned int symtab_shndx = this->symtab_shndx_;
2424 typename This::Shdr symtabshdr(this,
2425 this->elf_file_.section_header(symtab_shndx));
2426 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
2427 const unsigned int loccount = this->local_symbol_count_;
2428 gold_assert(loccount == symtabshdr.get_sh_info());
2429
2430 // Read the local symbols.
2431 const int sym_size = This::sym_size;
2432 off_t locsize = loccount * sym_size;
2433 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
2434 locsize, true, false);
2435
2436 // Read the symbol names.
2437 const unsigned int strtab_shndx =
2438 this->adjust_shndx(symtabshdr.get_sh_link());
2439 section_size_type strtab_size;
2440 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
2441 &strtab_size,
2442 false);
2443 const char* pnames = reinterpret_cast<const char*>(pnamesu);
2444
2445 // Get views into the output file for the portions of the symbol table
2446 // and the dynamic symbol table that we will be writing.
2447 off_t output_size = this->output_local_symbol_count_ * sym_size;
2448 unsigned char* oview = NULL;
2449 if (output_size > 0)
2450 oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2451 output_size);
2452
2453 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2454 unsigned char* dyn_oview = NULL;
2455 if (dyn_output_size > 0)
2456 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2457 dyn_output_size);
2458
2459 const Output_sections out_sections(this->output_sections());
2460
2461 gold_assert(this->local_values_.size() == loccount);
2462
2463 unsigned char* ov = oview;
2464 unsigned char* dyn_ov = dyn_oview;
2465 psyms += sym_size;
2466 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2467 {
2468 elfcpp::Sym<size, big_endian> isym(psyms);
2469
2470 Symbol_value<size>& lv(this->local_values_[i]);
2471
2472 bool is_ordinary;
2473 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
2474 &is_ordinary);
2475 if (is_ordinary)
2476 {
2477 gold_assert(st_shndx < out_sections.size());
2478 if (out_sections[st_shndx] == NULL)
2479 continue;
2480 st_shndx = out_sections[st_shndx]->out_shndx();
2481 if (st_shndx >= elfcpp::SHN_LORESERVE)
2482 {
2483 if (lv.has_output_symtab_entry())
2484 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
2485 if (lv.has_output_dynsym_entry())
2486 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
2487 st_shndx = elfcpp::SHN_XINDEX;
2488 }
2489 }
2490
2491 // Write the symbol to the output symbol table.
2492 if (lv.has_output_symtab_entry())
2493 {
2494 elfcpp::Sym_write<size, big_endian> osym(ov);
2495
2496 gold_assert(isym.get_st_name() < strtab_size);
2497 const char* name = pnames + isym.get_st_name();
2498 osym.put_st_name(sympool->get_offset(name));
2499 osym.put_st_value(this->local_values_[i].value(this, 0));
2500 osym.put_st_size(isym.get_st_size());
2501 osym.put_st_info(isym.get_st_info());
2502 osym.put_st_other(isym.get_st_other());
2503 osym.put_st_shndx(st_shndx);
2504
2505 ov += sym_size;
2506 }
2507
2508 // Write the symbol to the output dynamic symbol table.
2509 if (lv.has_output_dynsym_entry())
2510 {
2511 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2512 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2513
2514 gold_assert(isym.get_st_name() < strtab_size);
2515 const char* name = pnames + isym.get_st_name();
2516 osym.put_st_name(dynpool->get_offset(name));
2517 osym.put_st_value(this->local_values_[i].value(this, 0));
2518 osym.put_st_size(isym.get_st_size());
2519 osym.put_st_info(isym.get_st_info());
2520 osym.put_st_other(isym.get_st_other());
2521 osym.put_st_shndx(st_shndx);
2522
2523 dyn_ov += sym_size;
2524 }
2525 }
2526
2527
2528 if (output_size > 0)
2529 {
2530 gold_assert(ov - oview == output_size);
2531 of->write_output_view(symtab_off + this->local_symbol_offset_,
2532 output_size, oview);
2533 }
2534
2535 if (dyn_output_size > 0)
2536 {
2537 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2538 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2539 dyn_oview);
2540 }
2541 }
2542
2543 // Set *INFO to symbolic information about the offset OFFSET in the
2544 // section SHNDX. Return true if we found something, false if we
2545 // found nothing.
2546
2547 template<int size, bool big_endian>
2548 bool
2549 Sized_relobj_file<size, big_endian>::get_symbol_location_info(
2550 unsigned int shndx,
2551 off_t offset,
2552 Symbol_location_info* info)
2553 {
2554 if (this->symtab_shndx_ == 0)
2555 return false;
2556
2557 section_size_type symbols_size;
2558 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
2559 &symbols_size,
2560 false);
2561
2562 unsigned int symbol_names_shndx =
2563 this->adjust_shndx(this->section_link(this->symtab_shndx_));
2564 section_size_type names_size;
2565 const unsigned char* symbol_names_u =
2566 this->section_contents(symbol_names_shndx, &names_size, false);
2567 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
2568
2569 const int sym_size = This::sym_size;
2570 const size_t count = symbols_size / sym_size;
2571
2572 const unsigned char* p = symbols;
2573 for (size_t i = 0; i < count; ++i, p += sym_size)
2574 {
2575 elfcpp::Sym<size, big_endian> sym(p);
2576
2577 if (sym.get_st_type() == elfcpp::STT_FILE)
2578 {
2579 if (sym.get_st_name() >= names_size)
2580 info->source_file = "(invalid)";
2581 else
2582 info->source_file = symbol_names + sym.get_st_name();
2583 continue;
2584 }
2585
2586 bool is_ordinary;
2587 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
2588 &is_ordinary);
2589 if (is_ordinary
2590 && st_shndx == shndx
2591 && static_cast<off_t>(sym.get_st_value()) <= offset
2592 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
2593 > offset))
2594 {
2595 if (sym.get_st_name() > names_size)
2596 info->enclosing_symbol_name = "(invalid)";
2597 else
2598 {
2599 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
2600 if (parameters->options().do_demangle())
2601 {
2602 char* demangled_name = cplus_demangle(
2603 info->enclosing_symbol_name.c_str(),
2604 DMGL_ANSI | DMGL_PARAMS);
2605 if (demangled_name != NULL)
2606 {
2607 info->enclosing_symbol_name.assign(demangled_name);
2608 free(demangled_name);
2609 }
2610 }
2611 }
2612 return true;
2613 }
2614 }
2615
2616 return false;
2617 }
2618
2619 // Look for a kept section corresponding to the given discarded section,
2620 // and return its output address. This is used only for relocations in
2621 // debugging sections. If we can't find the kept section, return 0.
2622
2623 template<int size, bool big_endian>
2624 typename Sized_relobj_file<size, big_endian>::Address
2625 Sized_relobj_file<size, big_endian>::map_to_kept_section(
2626 unsigned int shndx,
2627 bool* found) const
2628 {
2629 Relobj* kept_object;
2630 unsigned int kept_shndx;
2631 if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx))
2632 {
2633 Sized_relobj_file<size, big_endian>* kept_relobj =
2634 static_cast<Sized_relobj_file<size, big_endian>*>(kept_object);
2635 Output_section* os = kept_relobj->output_section(kept_shndx);
2636 Address offset = kept_relobj->get_output_section_offset(kept_shndx);
2637 if (os != NULL && offset != invalid_address)
2638 {
2639 *found = true;
2640 return os->address() + offset;
2641 }
2642 }
2643 *found = false;
2644 return 0;
2645 }
2646
2647 // Get symbol counts.
2648
2649 template<int size, bool big_endian>
2650 void
2651 Sized_relobj_file<size, big_endian>::do_get_global_symbol_counts(
2652 const Symbol_table*,
2653 size_t* defined,
2654 size_t* used) const
2655 {
2656 *defined = this->defined_count_;
2657 size_t count = 0;
2658 for (typename Symbols::const_iterator p = this->symbols_.begin();
2659 p != this->symbols_.end();
2660 ++p)
2661 if (*p != NULL
2662 && (*p)->source() == Symbol::FROM_OBJECT
2663 && (*p)->object() == this
2664 && (*p)->is_defined())
2665 ++count;
2666 *used = count;
2667 }
2668
2669 // Return a view of the decompressed contents of a section. Set *PLEN
2670 // to the size. Set *IS_NEW to true if the contents need to be freed
2671 // by the caller.
2672
2673 template<int size, bool big_endian>
2674 const unsigned char*
2675 Sized_relobj_file<size, big_endian>::do_decompressed_section_contents(
2676 unsigned int shndx,
2677 section_size_type* plen,
2678 bool* is_new)
2679 {
2680 section_size_type buffer_size;
2681 const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size,
2682 false);
2683
2684 if (this->compressed_sections_ == NULL)
2685 {
2686 *plen = buffer_size;
2687 *is_new = false;
2688 return buffer;
2689 }
2690
2691 Compressed_section_map::const_iterator p =
2692 this->compressed_sections_->find(shndx);
2693 if (p == this->compressed_sections_->end())
2694 {
2695 *plen = buffer_size;
2696 *is_new = false;
2697 return buffer;
2698 }
2699
2700 section_size_type uncompressed_size = p->second.size;
2701 if (p->second.contents != NULL)
2702 {
2703 *plen = uncompressed_size;
2704 *is_new = false;
2705 return p->second.contents;
2706 }
2707
2708 unsigned char* uncompressed_data = new unsigned char[uncompressed_size];
2709 if (!decompress_input_section(buffer,
2710 buffer_size,
2711 uncompressed_data,
2712 uncompressed_size))
2713 this->error(_("could not decompress section %s"),
2714 this->do_section_name(shndx).c_str());
2715
2716 // We could cache the results in p->second.contents and store
2717 // false in *IS_NEW, but build_compressed_section_map() would
2718 // have done so if it had expected it to be profitable. If
2719 // we reach this point, we expect to need the contents only
2720 // once in this pass.
2721 *plen = uncompressed_size;
2722 *is_new = true;
2723 return uncompressed_data;
2724 }
2725
2726 // Discard any buffers of uncompressed sections. This is done
2727 // at the end of the Add_symbols task.
2728
2729 template<int size, bool big_endian>
2730 void
2731 Sized_relobj_file<size, big_endian>::do_discard_decompressed_sections()
2732 {
2733 if (this->compressed_sections_ == NULL)
2734 return;
2735
2736 for (Compressed_section_map::iterator p = this->compressed_sections_->begin();
2737 p != this->compressed_sections_->end();
2738 ++p)
2739 {
2740 if (p->second.contents != NULL)
2741 {
2742 delete[] p->second.contents;
2743 p->second.contents = NULL;
2744 }
2745 }
2746 }
2747
2748 // Input_objects methods.
2749
2750 // Add a regular relocatable object to the list. Return false if this
2751 // object should be ignored.
2752
2753 bool
2754 Input_objects::add_object(Object* obj)
2755 {
2756 // Print the filename if the -t/--trace option is selected.
2757 if (parameters->options().trace())
2758 gold_info("%s", obj->name().c_str());
2759
2760 if (!obj->is_dynamic())
2761 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
2762 else
2763 {
2764 // See if this is a duplicate SONAME.
2765 Dynobj* dynobj = static_cast<Dynobj*>(obj);
2766 const char* soname = dynobj->soname();
2767
2768 std::pair<Unordered_set<std::string>::iterator, bool> ins =
2769 this->sonames_.insert(soname);
2770 if (!ins.second)
2771 {
2772 // We have already seen a dynamic object with this soname.
2773 return false;
2774 }
2775
2776 this->dynobj_list_.push_back(dynobj);
2777 }
2778
2779 // Add this object to the cross-referencer if requested.
2780 if (parameters->options().user_set_print_symbol_counts()
2781 || parameters->options().cref())
2782 {
2783 if (this->cref_ == NULL)
2784 this->cref_ = new Cref();
2785 this->cref_->add_object(obj);
2786 }
2787
2788 return true;
2789 }
2790
2791 // For each dynamic object, record whether we've seen all of its
2792 // explicit dependencies.
2793
2794 void
2795 Input_objects::check_dynamic_dependencies() const
2796 {
2797 bool issued_copy_dt_needed_error = false;
2798 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
2799 p != this->dynobj_list_.end();
2800 ++p)
2801 {
2802 const Dynobj::Needed& needed((*p)->needed());
2803 bool found_all = true;
2804 Dynobj::Needed::const_iterator pneeded;
2805 for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded)
2806 {
2807 if (this->sonames_.find(*pneeded) == this->sonames_.end())
2808 {
2809 found_all = false;
2810 break;
2811 }
2812 }
2813 (*p)->set_has_unknown_needed_entries(!found_all);
2814
2815 // --copy-dt-needed-entries aka --add-needed is a GNU ld option
2816 // that gold does not support. However, they cause no trouble
2817 // unless there is a DT_NEEDED entry that we don't know about;
2818 // warn only in that case.
2819 if (!found_all
2820 && !issued_copy_dt_needed_error
2821 && (parameters->options().copy_dt_needed_entries()
2822 || parameters->options().add_needed()))
2823 {
2824 const char* optname;
2825 if (parameters->options().copy_dt_needed_entries())
2826 optname = "--copy-dt-needed-entries";
2827 else
2828 optname = "--add-needed";
2829 gold_error(_("%s is not supported but is required for %s in %s"),
2830 optname, (*pneeded).c_str(), (*p)->name().c_str());
2831 issued_copy_dt_needed_error = true;
2832 }
2833 }
2834 }
2835
2836 // Start processing an archive.
2837
2838 void
2839 Input_objects::archive_start(Archive* archive)
2840 {
2841 if (parameters->options().user_set_print_symbol_counts()
2842 || parameters->options().cref())
2843 {
2844 if (this->cref_ == NULL)
2845 this->cref_ = new Cref();
2846 this->cref_->add_archive_start(archive);
2847 }
2848 }
2849
2850 // Stop processing an archive.
2851
2852 void
2853 Input_objects::archive_stop(Archive* archive)
2854 {
2855 if (parameters->options().user_set_print_symbol_counts()
2856 || parameters->options().cref())
2857 this->cref_->add_archive_stop(archive);
2858 }
2859
2860 // Print symbol counts
2861
2862 void
2863 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
2864 {
2865 if (parameters->options().user_set_print_symbol_counts()
2866 && this->cref_ != NULL)
2867 this->cref_->print_symbol_counts(symtab);
2868 }
2869
2870 // Print a cross reference table.
2871
2872 void
2873 Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const
2874 {
2875 if (parameters->options().cref() && this->cref_ != NULL)
2876 this->cref_->print_cref(symtab, f);
2877 }
2878
2879 // Relocate_info methods.
2880
2881 // Return a string describing the location of a relocation when file
2882 // and lineno information is not available. This is only used in
2883 // error messages.
2884
2885 template<int size, bool big_endian>
2886 std::string
2887 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
2888 {
2889 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
2890 std::string ret = line_info.addr2line(this->data_shndx, offset, NULL);
2891 if (!ret.empty())
2892 return ret;
2893
2894 ret = this->object->name();
2895
2896 Symbol_location_info info;
2897 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
2898 {
2899 if (!info.source_file.empty())
2900 {
2901 ret += ":";
2902 ret += info.source_file;
2903 }
2904 size_t len = info.enclosing_symbol_name.length() + 100;
2905 char* buf = new char[len];
2906 snprintf(buf, len, _(":function %s"),
2907 info.enclosing_symbol_name.c_str());
2908 ret += buf;
2909 delete[] buf;
2910 return ret;
2911 }
2912
2913 ret += "(";
2914 ret += this->object->section_name(this->data_shndx);
2915 char buf[100];
2916 snprintf(buf, sizeof buf, "+0x%lx)", static_cast<long>(offset));
2917 ret += buf;
2918 return ret;
2919 }
2920
2921 } // End namespace gold.
2922
2923 namespace
2924 {
2925
2926 using namespace gold;
2927
2928 // Read an ELF file with the header and return the appropriate
2929 // instance of Object.
2930
2931 template<int size, bool big_endian>
2932 Object*
2933 make_elf_sized_object(const std::string& name, Input_file* input_file,
2934 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr,
2935 bool* punconfigured)
2936 {
2937 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
2938 ehdr.get_e_ident()[elfcpp::EI_OSABI],
2939 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
2940 if (target == NULL)
2941 gold_fatal(_("%s: unsupported ELF machine number %d"),
2942 name.c_str(), ehdr.get_e_machine());
2943
2944 if (!parameters->target_valid())
2945 set_parameters_target(target);
2946 else if (target != &parameters->target())
2947 {
2948 if (punconfigured != NULL)
2949 *punconfigured = true;
2950 else
2951 gold_error(_("%s: incompatible target"), name.c_str());
2952 return NULL;
2953 }
2954
2955 return target->make_elf_object<size, big_endian>(name, input_file, offset,
2956 ehdr);
2957 }
2958
2959 } // End anonymous namespace.
2960
2961 namespace gold
2962 {
2963
2964 // Return whether INPUT_FILE is an ELF object.
2965
2966 bool
2967 is_elf_object(Input_file* input_file, off_t offset,
2968 const unsigned char** start, int* read_size)
2969 {
2970 off_t filesize = input_file->file().filesize();
2971 int want = elfcpp::Elf_recognizer::max_header_size;
2972 if (filesize - offset < want)
2973 want = filesize - offset;
2974
2975 const unsigned char* p = input_file->file().get_view(offset, 0, want,
2976 true, false);
2977 *start = p;
2978 *read_size = want;
2979
2980 return elfcpp::Elf_recognizer::is_elf_file(p, want);
2981 }
2982
2983 // Read an ELF file and return the appropriate instance of Object.
2984
2985 Object*
2986 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
2987 const unsigned char* p, section_offset_type bytes,
2988 bool* punconfigured)
2989 {
2990 if (punconfigured != NULL)
2991 *punconfigured = false;
2992
2993 std::string error;
2994 bool big_endian = false;
2995 int size = 0;
2996 if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size,
2997 &big_endian, &error))
2998 {
2999 gold_error(_("%s: %s"), name.c_str(), error.c_str());
3000 return NULL;
3001 }
3002
3003 if (size == 32)
3004 {
3005 if (big_endian)
3006 {
3007 #ifdef HAVE_TARGET_32_BIG
3008 elfcpp::Ehdr<32, true> ehdr(p);
3009 return make_elf_sized_object<32, true>(name, input_file,
3010 offset, ehdr, punconfigured);
3011 #else
3012 if (punconfigured != NULL)
3013 *punconfigured = true;
3014 else
3015 gold_error(_("%s: not configured to support "
3016 "32-bit big-endian object"),
3017 name.c_str());
3018 return NULL;
3019 #endif
3020 }
3021 else
3022 {
3023 #ifdef HAVE_TARGET_32_LITTLE
3024 elfcpp::Ehdr<32, false> ehdr(p);
3025 return make_elf_sized_object<32, false>(name, input_file,
3026 offset, ehdr, punconfigured);
3027 #else
3028 if (punconfigured != NULL)
3029 *punconfigured = true;
3030 else
3031 gold_error(_("%s: not configured to support "
3032 "32-bit little-endian object"),
3033 name.c_str());
3034 return NULL;
3035 #endif
3036 }
3037 }
3038 else if (size == 64)
3039 {
3040 if (big_endian)
3041 {
3042 #ifdef HAVE_TARGET_64_BIG
3043 elfcpp::Ehdr<64, true> ehdr(p);
3044 return make_elf_sized_object<64, true>(name, input_file,
3045 offset, ehdr, punconfigured);
3046 #else
3047 if (punconfigured != NULL)
3048 *punconfigured = true;
3049 else
3050 gold_error(_("%s: not configured to support "
3051 "64-bit big-endian object"),
3052 name.c_str());
3053 return NULL;
3054 #endif
3055 }
3056 else
3057 {
3058 #ifdef HAVE_TARGET_64_LITTLE
3059 elfcpp::Ehdr<64, false> ehdr(p);
3060 return make_elf_sized_object<64, false>(name, input_file,
3061 offset, ehdr, punconfigured);
3062 #else
3063 if (punconfigured != NULL)
3064 *punconfigured = true;
3065 else
3066 gold_error(_("%s: not configured to support "
3067 "64-bit little-endian object"),
3068 name.c_str());
3069 return NULL;
3070 #endif
3071 }
3072 }
3073 else
3074 gold_unreachable();
3075 }
3076
3077 // Instantiate the templates we need.
3078
3079 #ifdef HAVE_TARGET_32_LITTLE
3080 template
3081 void
3082 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
3083 Read_symbols_data*);
3084 #endif
3085
3086 #ifdef HAVE_TARGET_32_BIG
3087 template
3088 void
3089 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
3090 Read_symbols_data*);
3091 #endif
3092
3093 #ifdef HAVE_TARGET_64_LITTLE
3094 template
3095 void
3096 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
3097 Read_symbols_data*);
3098 #endif
3099
3100 #ifdef HAVE_TARGET_64_BIG
3101 template
3102 void
3103 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
3104 Read_symbols_data*);
3105 #endif
3106
3107 #ifdef HAVE_TARGET_32_LITTLE
3108 template
3109 class Sized_relobj_file<32, false>;
3110 #endif
3111
3112 #ifdef HAVE_TARGET_32_BIG
3113 template
3114 class Sized_relobj_file<32, true>;
3115 #endif
3116
3117 #ifdef HAVE_TARGET_64_LITTLE
3118 template
3119 class Sized_relobj_file<64, false>;
3120 #endif
3121
3122 #ifdef HAVE_TARGET_64_BIG
3123 template
3124 class Sized_relobj_file<64, true>;
3125 #endif
3126
3127 #ifdef HAVE_TARGET_32_LITTLE
3128 template
3129 struct Relocate_info<32, false>;
3130 #endif
3131
3132 #ifdef HAVE_TARGET_32_BIG
3133 template
3134 struct Relocate_info<32, true>;
3135 #endif
3136
3137 #ifdef HAVE_TARGET_64_LITTLE
3138 template
3139 struct Relocate_info<64, false>;
3140 #endif
3141
3142 #ifdef HAVE_TARGET_64_BIG
3143 template
3144 struct Relocate_info<64, true>;
3145 #endif
3146
3147 #ifdef HAVE_TARGET_32_LITTLE
3148 template
3149 void
3150 Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int);
3151
3152 template
3153 void
3154 Xindex::read_symtab_xindex<32, false>(Object*, unsigned int,
3155 const unsigned char*);
3156 #endif
3157
3158 #ifdef HAVE_TARGET_32_BIG
3159 template
3160 void
3161 Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int);
3162
3163 template
3164 void
3165 Xindex::read_symtab_xindex<32, true>(Object*, unsigned int,
3166 const unsigned char*);
3167 #endif
3168
3169 #ifdef HAVE_TARGET_64_LITTLE
3170 template
3171 void
3172 Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int);
3173
3174 template
3175 void
3176 Xindex::read_symtab_xindex<64, false>(Object*, unsigned int,
3177 const unsigned char*);
3178 #endif
3179
3180 #ifdef HAVE_TARGET_64_BIG
3181 template
3182 void
3183 Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int);
3184
3185 template
3186 void
3187 Xindex::read_symtab_xindex<64, true>(Object*, unsigned int,
3188 const unsigned char*);
3189 #endif
3190
3191 } // End namespace gold.
This page took 0.093675 seconds and 5 git commands to generate.