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