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