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