Use partial register read/writes in transfer_regset
[deliverable/binutils-gdb.git] / gold / object.h
CommitLineData
bae7f79e
ILT
1// object.h -- support for an object file for linking in gold -*- C++ -*-
2
219d1afa 3// Copyright (C) 2006-2018 Free Software Foundation, Inc.
6cb15b7f
ILT
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
bae7f79e
ILT
23#ifndef GOLD_OBJECT_H
24#define GOLD_OBJECT_H
25
92e059d8 26#include <string>
a2fb1b05 27#include <vector>
14bfc3f5 28
bae7f79e 29#include "elfcpp.h"
645f8123 30#include "elfcpp_file.h"
bae7f79e 31#include "fileread.h"
14bfc3f5 32#include "target.h"
b0193076 33#include "archive.h"
bae7f79e
ILT
34
35namespace gold
36{
37
92e059d8 38class General_options;
17a1d0a9 39class Task;
92de84a6 40class Cref;
a2fb1b05 41class Layout;
43193fe9 42class Kept_section;
7223e9ca 43class Output_data;
61ba1cf9 44class Output_section;
dbe40a88 45class Output_section_data;
61ba1cf9 46class Output_file;
d491d34e 47class Output_symtab_xindex;
89fc3421 48class Pluginobj;
f6ce93d6 49class Dynobj;
4625f782 50class Object_merge_map;
6a74a719 51class Relocatable_relocs;
2c54b4f4 52struct Symbols_data;
a2fb1b05 53
b8e6aad9
ILT
54template<typename Stringpool_char>
55class Stringpool_template;
56
bae7f79e
ILT
57// Data to pass from read_symbols() to add_symbols().
58
59struct Read_symbols_data
60{
00698fc5
CC
61 Read_symbols_data()
62 : section_headers(NULL), section_names(NULL), symbols(NULL),
63 symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL)
64 { }
65
66 ~Read_symbols_data();
67
12e14209
ILT
68 // Section headers.
69 File_view* section_headers;
70 // Section names.
71 File_view* section_names;
72 // Size of section name data in bytes.
8383303e 73 section_size_type section_names_size;
bae7f79e
ILT
74 // Symbol data.
75 File_view* symbols;
76 // Size of symbol data in bytes.
8383303e 77 section_size_type symbols_size;
730cdc88
ILT
78 // Offset of external symbols within symbol data. This structure
79 // sometimes contains only external symbols, in which case this will
80 // be zero. Sometimes it contains all symbols.
8383303e 81 section_offset_type external_symbols_offset;
bae7f79e
ILT
82 // Symbol names.
83 File_view* symbol_names;
84 // Size of symbol name data in bytes.
8383303e 85 section_size_type symbol_names_size;
dbe717ef
ILT
86
87 // Version information. This is only used on dynamic objects.
88 // Version symbol data (from SHT_GNU_versym section).
89 File_view* versym;
8383303e 90 section_size_type versym_size;
dbe717ef
ILT
91 // Version definition data (from SHT_GNU_verdef section).
92 File_view* verdef;
8383303e 93 section_size_type verdef_size;
dbe717ef
ILT
94 unsigned int verdef_info;
95 // Needed version data (from SHT_GNU_verneed section).
96 File_view* verneed;
8383303e 97 section_size_type verneed_size;
dbe717ef 98 unsigned int verneed_info;
bae7f79e
ILT
99};
100
f7e2ee48
ILT
101// Information used to print error messages.
102
103struct Symbol_location_info
104{
105 std::string source_file;
106 std::string enclosing_symbol_name;
60e8b3fc 107 elfcpp::STT enclosing_symbol_type;
f7e2ee48
ILT
108};
109
92e059d8
ILT
110// Data about a single relocation section. This is read in
111// read_relocs and processed in scan_relocs.
112
113struct Section_relocs
114{
00698fc5
CC
115 Section_relocs()
116 : contents(NULL)
117 { }
118
119 ~Section_relocs()
120 { delete this->contents; }
121
92e059d8
ILT
122 // Index of reloc section.
123 unsigned int reloc_shndx;
124 // Index of section that relocs apply to.
125 unsigned int data_shndx;
126 // Contents of reloc section.
127 File_view* contents;
128 // Reloc section type.
129 unsigned int sh_type;
130 // Number of reloc entries.
131 size_t reloc_count;
730cdc88
ILT
132 // Output section.
133 Output_section* output_section;
134 // Whether this section has special handling for offsets.
135 bool needs_special_offset_handling;
7019cd25
ILT
136 // Whether the data section is allocated (has the SHF_ALLOC flag set).
137 bool is_data_section_allocated;
92e059d8
ILT
138};
139
140// Relocations in an object file. This is read in read_relocs and
141// processed in scan_relocs.
142
143struct Read_relocs_data
144{
00698fc5
CC
145 Read_relocs_data()
146 : local_symbols(NULL)
147 { }
148
149 ~Read_relocs_data()
150 { delete this->local_symbols; }
151
92e059d8
ILT
152 typedef std::vector<Section_relocs> Relocs_list;
153 // The relocations.
154 Relocs_list relocs;
155 // The local symbols.
156 File_view* local_symbols;
157};
158
d491d34e
ILT
159// The Xindex class manages section indexes for objects with more than
160// 0xff00 sections.
161
162class Xindex
163{
164 public:
165 Xindex(int large_shndx_offset)
166 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
167 { }
168
169 // Initialize the symtab_xindex_ array, given the object and the
170 // section index of the symbol table to use.
171 template<int size, bool big_endian>
172 void
173 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
174
175 // Read in the symtab_xindex_ array, given its section index.
176 // PSHDRS may optionally point to the section headers.
177 template<int size, bool big_endian>
178 void
179 read_symtab_xindex(Object*, unsigned int xindex_shndx,
180 const unsigned char* pshdrs);
181
182 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
183 // real section index.
184 unsigned int
185 sym_xindex_to_shndx(Object* object, unsigned int symndx);
186
187 private:
188 // The type of the array giving the real section index for symbols
189 // whose st_shndx field holds SHN_XINDEX.
190 typedef std::vector<unsigned int> Symtab_xindex;
191
192 // Adjust a section index if necessary. This should only be called
193 // for ordinary section indexes.
194 unsigned int
195 adjust_shndx(unsigned int shndx)
196 {
197 if (shndx >= elfcpp::SHN_LORESERVE)
198 shndx += this->large_shndx_offset_;
199 return shndx;
200 }
201
202 // Adjust to apply to large section indexes.
203 int large_shndx_offset_;
204 // The data from the SHT_SYMTAB_SHNDX section.
205 Symtab_xindex symtab_xindex_;
206};
207
cdc29364
CC
208// A GOT offset list. A symbol may have more than one GOT offset
209// (e.g., when mixing modules compiled with two different TLS models),
210// but will usually have at most one. GOT_TYPE identifies the type of
211// GOT entry; its values are specific to each target.
212
213class Got_offset_list
214{
215 public:
216 Got_offset_list()
217 : got_type_(-1U), got_offset_(0), got_next_(NULL)
218 { }
219
220 Got_offset_list(unsigned int got_type, unsigned int got_offset)
221 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
222 { }
223
224 ~Got_offset_list()
225 {
226 if (this->got_next_ != NULL)
227 {
228 delete this->got_next_;
229 this->got_next_ = NULL;
230 }
231 }
232
233 // Initialize the fields to their default values.
234 void
235 init()
236 {
237 this->got_type_ = -1U;
238 this->got_offset_ = 0;
239 this->got_next_ = NULL;
240 }
241
242 // Set the offset for the GOT entry of type GOT_TYPE.
243 void
244 set_offset(unsigned int got_type, unsigned int got_offset)
245 {
246 if (this->got_type_ == -1U)
247 {
248 this->got_type_ = got_type;
249 this->got_offset_ = got_offset;
250 }
251 else
252 {
253 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
254 {
255 if (g->got_type_ == got_type)
256 {
257 g->got_offset_ = got_offset;
258 return;
259 }
260 }
261 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
262 g->got_next_ = this->got_next_;
263 this->got_next_ = g;
264 }
265 }
266
267 // Return the offset for a GOT entry of type GOT_TYPE.
268 unsigned int
269 get_offset(unsigned int got_type) const
270 {
271 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
272 {
273 if (g->got_type_ == got_type)
274 return g->got_offset_;
275 }
276 return -1U;
277 }
278
279 // Return a pointer to the list, or NULL if the list is empty.
280 const Got_offset_list*
281 get_list() const
282 {
283 if (this->got_type_ == -1U)
284 return NULL;
285 return this;
286 }
287
288 // Abstract visitor class for iterating over GOT offsets.
289 class Visitor
290 {
291 public:
292 Visitor()
293 { }
294
295 virtual
296 ~Visitor()
297 { }
298
299 virtual void
300 visit(unsigned int, unsigned int) = 0;
301 };
302
303 // Loop over all GOT offset entries, calling a visitor class V for each.
304 void
305 for_all_got_offsets(Visitor* v) const
306 {
307 if (this->got_type_ == -1U)
308 return;
309 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
310 v->visit(g->got_type_, g->got_offset_);
311 }
312
313 private:
314 unsigned int got_type_;
315 unsigned int got_offset_;
316 Got_offset_list* got_next_;
317};
318
7ef8ae7c
VR
319// The Local_got_entry_key used to index the GOT offsets for local
320// non-TLS symbols, and tp-relative offsets for TLS symbols.
321
322class Local_got_entry_key
323{
324 public:
325 Local_got_entry_key(unsigned int symndx, uint64_t addend)
326 : symndx_(symndx), addend_(addend)
327 {}
328
329 // Whether this equals to another Local_got_entry_key.
330 bool
331 eq(const Local_got_entry_key& key) const
332 {
333 return (this->symndx_ == key.symndx_ && this->addend_ == key.addend_);
334 }
335
336 // Compute a hash value for this using 64-bit FNV-1a hash.
337 size_t
338 hash_value() const
339 {
340 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
341 uint64_t prime = 1099511628211ULL;
342 h = (h ^ static_cast<uint64_t>(this->symndx_)) * prime;
343 h = (h ^ static_cast<uint64_t>(this->addend_)) * prime;
344 return h;
345 }
346
347 // Functors for associative containers.
348 struct equal_to
349 {
350 bool
351 operator()(const Local_got_entry_key& key1,
352 const Local_got_entry_key& key2) const
353 { return key1.eq(key2); }
354 };
355
356 struct hash
357 {
358 size_t
359 operator()(const Local_got_entry_key& key) const
360 { return key.hash_value(); }
361 };
362
363 private:
364 // The local symbol index.
365 unsigned int symndx_;
366 // The addend.
367 uint64_t addend_;
368};
369
0d5bbdb0
CC
370// Type for mapping section index to uncompressed size and contents.
371
372struct Compressed_section_info
373{
374 section_size_type size;
48058663 375 elfcpp::Elf_Xword flag;
0d5bbdb0
CC
376 const unsigned char* contents;
377};
378typedef std::map<unsigned int, Compressed_section_info> Compressed_section_map;
379
380template<int size, bool big_endian>
381Compressed_section_map*
382build_compressed_section_map(const unsigned char* pshdrs, unsigned int shnum,
383 const char* names, section_size_type names_size,
384 Object* obj, bool decompress_if_needed);
385
f6ce93d6
ILT
386// Object is an abstract base class which represents either a 32-bit
387// or a 64-bit input object. This can be a regular object file
388// (ET_REL) or a shared object (ET_DYN).
bae7f79e
ILT
389
390class Object
391{
392 public:
dde3f402
ILT
393 typedef std::vector<Symbol*> Symbols;
394
bae7f79e
ILT
395 // NAME is the name of the object as we would report it to the user
396 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
397 // used to read the file. OFFSET is the offset within the input
398 // file--0 for a .o or .so file, something else for a .a file.
2ea97941
ILT
399 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
400 off_t offset = 0)
401 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
402 is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
cdc29364 403 has_no_split_stack_(false), no_export_(false),
0d5bbdb0
CC
404 is_in_system_directory_(false), as_needed_(false), xindex_(NULL),
405 compressed_sections_(NULL)
cdc29364
CC
406 {
407 if (input_file != NULL)
408 {
409 input_file->file().add_object();
410 this->is_in_system_directory_ = input_file->is_in_system_directory();
0f1c85a6 411 this->as_needed_ = input_file->options().as_needed();
cdc29364
CC
412 }
413 }
bae7f79e
ILT
414
415 virtual ~Object()
cdc29364
CC
416 {
417 if (this->input_file_ != NULL)
418 this->input_file_->file().remove_object();
419 }
bae7f79e 420
cf43a2fe 421 // Return the name of the object as we would report it to the user.
bae7f79e
ILT
422 const std::string&
423 name() const
424 { return this->name_; }
425
cec9d2f3
ILT
426 // Get the offset into the file.
427 off_t
428 offset() const
429 { return this->offset_; }
430
14bfc3f5
ILT
431 // Return whether this is a dynamic object.
432 bool
433 is_dynamic() const
434 { return this->is_dynamic_; }
435
48058663
L
436 // Return the word size of the object file.
437 virtual int elfsize() const = 0;
438
439 // Return TRUE if this is a big-endian object file.
440 virtual bool is_big_endian() const = 0;
441
594c8e5e
ILT
442 // Return whether this object is needed--true if it is a dynamic
443 // object which defines some symbol referenced by a regular object.
444 // We keep the flag here rather than in Dynobj for convenience when
445 // setting it.
446 bool
447 is_needed() const
448 { return this->is_needed_; }
449
450 // Record that this object is needed.
451 void
452 set_is_needed()
453 { this->is_needed_ = true; }
454
364c7fa5
ILT
455 // Return whether this object was compiled with -fsplit-stack.
456 bool
457 uses_split_stack() const
458 { return this->uses_split_stack_; }
459
460 // Return whether this object contains any functions compiled with
461 // the no_split_stack attribute.
462 bool
463 has_no_split_stack() const
464 { return this->has_no_split_stack_; }
465
0f1c85a6
CC
466 // Returns NULL for Objects that are not dynamic objects. This method
467 // is overridden in the Dynobj class.
468 Dynobj*
469 dynobj()
470 { return this->do_dynobj(); }
471
89fc3421
CC
472 // Returns NULL for Objects that are not plugin objects. This method
473 // is overridden in the Pluginobj class.
474 Pluginobj*
475 pluginobj()
476 { return this->do_pluginobj(); }
477
15f8229b
ILT
478 // Get the file. We pass on const-ness.
479 Input_file*
480 input_file()
cdc29364
CC
481 {
482 gold_assert(this->input_file_ != NULL);
483 return this->input_file_;
484 }
15f8229b
ILT
485
486 const Input_file*
487 input_file() const
cdc29364
CC
488 {
489 gold_assert(this->input_file_ != NULL);
490 return this->input_file_;
491 }
15f8229b 492
a2fb1b05
ILT
493 // Lock the underlying file.
494 void
17a1d0a9 495 lock(const Task* t)
cdc29364
CC
496 {
497 if (this->input_file_ != NULL)
498 this->input_file_->file().lock(t);
499 }
a2fb1b05
ILT
500
501 // Unlock the underlying file.
502 void
17a1d0a9 503 unlock(const Task* t)
cdc29364
CC
504 {
505 if (this->input_file_ != NULL)
506 this->input_file()->file().unlock(t);
507 }
a2fb1b05
ILT
508
509 // Return whether the underlying file is locked.
510 bool
511 is_locked() const
cdc29364 512 { return this->input_file_ != NULL && this->input_file_->file().is_locked(); }
a2fb1b05 513
17a1d0a9
ILT
514 // Return the token, so that the task can be queued.
515 Task_token*
516 token()
cdc29364
CC
517 {
518 if (this->input_file_ == NULL)
519 return NULL;
520 return this->input_file()->file().token();
521 }
17a1d0a9
ILT
522
523 // Release the underlying file.
524 void
525 release()
cdc29364
CC
526 {
527 if (this->input_file_ != NULL)
528 this->input_file()->file().release();
529 }
17a1d0a9 530
88dd47ac
ILT
531 // Return whether we should just read symbols from this file.
532 bool
533 just_symbols() const
534 { return this->input_file()->just_symbols(); }
535
cdc29364
CC
536 // Return whether this is an incremental object.
537 bool
538 is_incremental() const
539 { return this->do_is_incremental(); }
540
541 // Return the last modified time of the file.
542 Timespec
543 get_mtime()
544 { return this->do_get_mtime(); }
545
5a6f7e2d
ILT
546 // Get the number of sections.
547 unsigned int
548 shnum() const
549 { return this->shnum_; }
a2fb1b05 550
f6ce93d6 551 // Return a view of the contents of a section. Set *PLEN to the
9eb9fa57 552 // size. CACHE is a hint as in File_read::get_view.
f6ce93d6 553 const unsigned char*
8383303e 554 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
f6ce93d6 555
d491d34e
ILT
556 // Adjust a symbol's section index as needed. SYMNDX is the index
557 // of the symbol and SHNDX is the symbol's section from
558 // get_st_shndx. This returns the section index. It sets
559 // *IS_ORDINARY to indicate whether this is a normal section index,
560 // rather than a special code between SHN_LORESERVE and
561 // SHN_HIRESERVE.
562 unsigned int
563 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
564 {
565 if (shndx < elfcpp::SHN_LORESERVE)
566 *is_ordinary = true;
567 else if (shndx == elfcpp::SHN_XINDEX)
568 {
569 if (this->xindex_ == NULL)
570 this->xindex_ = this->do_initialize_xindex();
571 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
572 *is_ordinary = true;
573 }
574 else
575 *is_ordinary = false;
576 return shndx;
577 }
578
a445fddf
ILT
579 // Return the size of a section given a section index.
580 uint64_t
581 section_size(unsigned int shndx)
582 { return this->do_section_size(shndx); }
583
584 // Return the name of a section given a section index.
f6ce93d6 585 std::string
54674d38 586 section_name(unsigned int shndx) const
a3ad94ed
ILT
587 { return this->do_section_name(shndx); }
588
589 // Return the section flags given a section index.
590 uint64_t
591 section_flags(unsigned int shndx)
592 { return this->do_section_flags(shndx); }
f6ce93d6 593
ef15dade
ST
594 // Return the section entsize given a section index.
595 uint64_t
596 section_entsize(unsigned int shndx)
597 { return this->do_section_entsize(shndx); }
598
88dd47ac
ILT
599 // Return the section address given a section index.
600 uint64_t
601 section_address(unsigned int shndx)
602 { return this->do_section_address(shndx); }
603
730cdc88
ILT
604 // Return the section type given a section index.
605 unsigned int
606 section_type(unsigned int shndx)
607 { return this->do_section_type(shndx); }
608
f7e2ee48
ILT
609 // Return the section link field given a section index.
610 unsigned int
611 section_link(unsigned int shndx)
612 { return this->do_section_link(shndx); }
613
4c50553d
ILT
614 // Return the section info field given a section index.
615 unsigned int
616 section_info(unsigned int shndx)
617 { return this->do_section_info(shndx); }
618
a445fddf
ILT
619 // Return the required section alignment given a section index.
620 uint64_t
621 section_addralign(unsigned int shndx)
622 { return this->do_section_addralign(shndx); }
623
09ec0418
CC
624 // Return the output section given a section index.
625 Output_section*
626 output_section(unsigned int shndx) const
627 { return this->do_output_section(shndx); }
628
c6905c28
CC
629 // Given a section index, return its address.
630 // The return value will be -1U if the section is specially mapped,
631 // such as a merge section.
632 uint64_t
633 output_section_address(unsigned int shndx)
634 { return this->do_output_section_address(shndx); }
635
09ec0418
CC
636 // Given a section index, return the offset in the Output_section.
637 // The return value will be -1U if the section is specially mapped,
638 // such as a merge section.
639 uint64_t
640 output_section_offset(unsigned int shndx) const
641 { return this->do_output_section_offset(shndx); }
642
5a6f7e2d
ILT
643 // Read the symbol information.
644 void
645 read_symbols(Read_symbols_data* sd)
646 { return this->do_read_symbols(sd); }
647
648 // Pass sections which should be included in the link to the Layout
649 // object, and record where the sections go in the output file.
650 void
2ea97941
ILT
651 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
652 { this->do_layout(symtab, layout, sd); }
5a6f7e2d
ILT
653
654 // Add symbol information to the global symbol table.
655 void
2ea97941
ILT
656 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout)
657 { this->do_add_symbols(symtab, sd, layout); }
5a6f7e2d 658
b0193076
RÁE
659 // Add symbol information to the global symbol table.
660 Archive::Should_include
88a4108b
ILT
661 should_include_member(Symbol_table* symtab, Layout* layout,
662 Read_symbols_data* sd, std::string* why)
663 { return this->do_should_include_member(symtab, layout, sd, why); }
b0193076 664
e0c52780
CC
665 // Iterate over global symbols, calling a visitor class V for each.
666 void
667 for_all_global_symbols(Read_symbols_data* sd,
668 Library_base::Symbol_visitor_base* v)
669 { return this->do_for_all_global_symbols(sd, v); }
670
cdc29364
CC
671 // Iterate over local symbols, calling a visitor class V for each GOT offset
672 // associated with a local symbol.
673 void
674 for_all_local_got_entries(Got_offset_list::Visitor* v) const
675 { this->do_for_all_local_got_entries(v); }
676
645f8123
ILT
677 // Functions and types for the elfcpp::Elf_file interface. This
678 // permit us to use Object as the File template parameter for
679 // elfcpp::Elf_file.
680
681 // The View class is returned by view. It must support a single
682 // method, data(). This is trivial, because get_view does what we
683 // need.
684 class View
685 {
686 public:
687 View(const unsigned char* p)
688 : p_(p)
689 { }
690
691 const unsigned char*
692 data() const
693 { return this->p_; }
694
695 private:
696 const unsigned char* p_;
697 };
698
699 // Return a View.
700 View
8383303e 701 view(off_t file_offset, section_size_type data_size)
39d0cb0e 702 { return View(this->get_view(file_offset, data_size, true, true)); }
645f8123
ILT
703
704 // Report an error.
705 void
75f2446e 706 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
645f8123
ILT
707
708 // A location in the file.
709 struct Location
710 {
711 off_t file_offset;
712 off_t data_size;
713
8383303e 714 Location(off_t fo, section_size_type ds)
645f8123
ILT
715 : file_offset(fo), data_size(ds)
716 { }
717 };
718
719 // Get a View given a Location.
720 View view(Location loc)
39d0cb0e 721 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
645f8123 722
cb295612
ILT
723 // Get a view into the underlying file.
724 const unsigned char*
39d0cb0e 725 get_view(off_t start, section_size_type size, bool aligned, bool cache)
cb295612 726 {
39d0cb0e
ILT
727 return this->input_file()->file().get_view(this->offset_, start, size,
728 aligned, cache);
cb295612
ILT
729 }
730
731 // Get a lasting view into the underlying file.
732 File_view*
39d0cb0e
ILT
733 get_lasting_view(off_t start, section_size_type size, bool aligned,
734 bool cache)
cb295612 735 {
39d0cb0e
ILT
736 return this->input_file()->file().get_lasting_view(this->offset_, start,
737 size, aligned, cache);
cb295612
ILT
738 }
739
740 // Read data from the underlying file.
741 void
2a00e4fb 742 read(off_t start, section_size_type size, void* p)
cb295612
ILT
743 { this->input_file()->file().read(start + this->offset_, size, p); }
744
745 // Read multiple data from the underlying file.
746 void
747 read_multiple(const File_read::Read_multiple& rm)
748 { this->input_file()->file().read_multiple(this->offset_, rm); }
749
750 // Stop caching views in the underlying file.
751 void
752 clear_view_cache_marks()
cdc29364
CC
753 {
754 if (this->input_file_ != NULL)
755 this->input_file_->file().clear_view_cache_marks();
756 }
cb295612 757
92de84a6
ILT
758 // Get the number of global symbols defined by this object, and the
759 // number of the symbols whose final definition came from this
760 // object.
761 void
762 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
763 size_t* used) const
764 { this->do_get_global_symbol_counts(symtab, defined, used); }
765
dde3f402
ILT
766 // Get the symbols defined in this object.
767 const Symbols*
768 get_global_symbols() const
769 { return this->do_get_global_symbols(); }
770
cdc29364
CC
771 // Set flag that this object was found in a system directory.
772 void
773 set_is_in_system_directory()
774 { this->is_in_system_directory_ = true; }
775
fd9d194f
ILT
776 // Return whether this object was found in a system directory.
777 bool
778 is_in_system_directory() const
cdc29364 779 { return this->is_in_system_directory_; }
fd9d194f 780
0f1c85a6
CC
781 // Set flag that this object was linked with --as-needed.
782 void
783 set_as_needed()
784 { this->as_needed_ = true; }
785
4bfacfd3
CC
786 // Clear flag that this object was linked with --as-needed.
787 void
788 clear_as_needed()
789 { this->as_needed_ = false; }
790
0f1c85a6
CC
791 // Return whether this object was linked with --as-needed.
792 bool
793 as_needed() const
794 { return this->as_needed_; }
795
15f8229b
ILT
796 // Return whether we found this object by searching a directory.
797 bool
798 searched_for() const
799 { return this->input_file()->will_search_for(); }
800
65514900
CC
801 bool
802 no_export() const
803 { return this->no_export_; }
804
805 void
806 set_no_export(bool value)
807 { this->no_export_ = value; }
808
a2e47362
CC
809 bool
810 section_is_compressed(unsigned int shndx,
811 section_size_type* uncompressed_size) const
0d5bbdb0
CC
812 {
813 if (this->compressed_sections_ == NULL)
814 return false;
815 Compressed_section_map::const_iterator p =
816 this->compressed_sections_->find(shndx);
817 if (p != this->compressed_sections_->end())
818 {
819 if (uncompressed_size != NULL)
820 *uncompressed_size = p->second.size;
821 return true;
822 }
823 return false;
824 }
a2e47362 825
c1027032 826 // Return a view of the decompressed contents of a section. Set *PLEN
5dd8762a
CC
827 // to the size. Set *IS_NEW to true if the contents need to be freed
828 // by the caller.
829 const unsigned char*
830 decompressed_section_contents(unsigned int shndx, section_size_type* plen,
0d5bbdb0 831 bool* is_cached);
5dd8762a
CC
832
833 // Discard any buffers of decompressed sections. This is done
834 // at the end of the Add_symbols task.
835 void
0d5bbdb0 836 discard_decompressed_sections();
5dd8762a 837
09ec0418
CC
838 // Return the index of the first incremental relocation for symbol SYMNDX.
839 unsigned int
840 get_incremental_reloc_base(unsigned int symndx) const
841 { return this->do_get_incremental_reloc_base(symndx); }
842
843 // Return the number of incremental relocations for symbol SYMNDX.
844 unsigned int
845 get_incremental_reloc_count(unsigned int symndx) const
846 { return this->do_get_incremental_reloc_count(symndx); }
847
6b2353a5 848 // Return the output view for section SHNDX.
39040bb9 849 unsigned char*
6b2353a5
CC
850 get_output_view(unsigned int shndx, section_size_type* plen) const
851 { return this->do_get_output_view(shndx, plen); }
852
f6ce93d6 853 protected:
0f1c85a6
CC
854 // Returns NULL for Objects that are not dynamic objects. This method
855 // is overridden in the Dynobj class.
856 virtual Dynobj*
857 do_dynobj()
858 { return NULL; }
859
89fc3421
CC
860 // Returns NULL for Objects that are not plugin objects. This method
861 // is overridden in the Pluginobj class.
862 virtual Pluginobj*
863 do_pluginobj()
864 { return NULL; }
865
cdc29364
CC
866 // Return TRUE if this is an incremental (unchanged) input file.
867 // We return FALSE by default; the incremental object classes
868 // override this method.
869 virtual bool
870 do_is_incremental() const
871 { return false; }
872
873 // Return the last modified time of the file. This method may be
874 // overridden for subclasses that don't use an actual file (e.g.,
875 // Incremental objects).
876 virtual Timespec
877 do_get_mtime()
878 { return this->input_file()->file().get_mtime(); }
879
f6ce93d6
ILT
880 // Read the symbols--implemented by child class.
881 virtual void
882 do_read_symbols(Read_symbols_data*) = 0;
883
884 // Lay out sections--implemented by child class.
885 virtual void
7e1edb90 886 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
f6ce93d6
ILT
887
888 // Add symbol information to the global symbol table--implemented by
889 // child class.
890 virtual void
f488e4b0 891 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
f6ce93d6 892
b0193076 893 virtual Archive::Should_include
88a4108b 894 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
b0193076
RÁE
895 std::string* why) = 0;
896
e0c52780
CC
897 // Iterate over global symbols, calling a visitor class V for each.
898 virtual void
899 do_for_all_global_symbols(Read_symbols_data* sd,
900 Library_base::Symbol_visitor_base* v) = 0;
901
cdc29364
CC
902 // Iterate over local symbols, calling a visitor class V for each GOT offset
903 // associated with a local symbol.
904 virtual void
905 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const = 0;
906
645f8123
ILT
907 // Return the location of the contents of a section. Implemented by
908 // child class.
c1027032
CC
909 virtual const unsigned char*
910 do_section_contents(unsigned int shndx, section_size_type* plen,
911 bool cache) = 0;
f6ce93d6 912
a445fddf
ILT
913 // Get the size of a section--implemented by child class.
914 virtual uint64_t
915 do_section_size(unsigned int shndx) = 0;
916
f6ce93d6
ILT
917 // Get the name of a section--implemented by child class.
918 virtual std::string
54674d38 919 do_section_name(unsigned int shndx) const = 0;
a3ad94ed
ILT
920
921 // Get section flags--implemented by child class.
922 virtual uint64_t
923 do_section_flags(unsigned int shndx) = 0;
f6ce93d6 924
ef15dade
ST
925 // Get section entsize--implemented by child class.
926 virtual uint64_t
927 do_section_entsize(unsigned int shndx) = 0;
928
88dd47ac
ILT
929 // Get section address--implemented by child class.
930 virtual uint64_t
931 do_section_address(unsigned int shndx) = 0;
932
730cdc88
ILT
933 // Get section type--implemented by child class.
934 virtual unsigned int
935 do_section_type(unsigned int shndx) = 0;
936
f7e2ee48
ILT
937 // Get section link field--implemented by child class.
938 virtual unsigned int
939 do_section_link(unsigned int shndx) = 0;
940
4c50553d
ILT
941 // Get section info field--implemented by child class.
942 virtual unsigned int
943 do_section_info(unsigned int shndx) = 0;
944
a445fddf
ILT
945 // Get section alignment--implemented by child class.
946 virtual uint64_t
947 do_section_addralign(unsigned int shndx) = 0;
948
09ec0418
CC
949 // Return the output section given a section index--implemented
950 // by child class.
951 virtual Output_section*
952 do_output_section(unsigned int) const
953 { gold_unreachable(); }
954
c6905c28
CC
955 // Get the address of a section--implemented by child class.
956 virtual uint64_t
957 do_output_section_address(unsigned int)
958 { gold_unreachable(); }
959
09ec0418
CC
960 // Get the offset of a section--implemented by child class.
961 virtual uint64_t
962 do_output_section_offset(unsigned int) const
963 { gold_unreachable(); }
964
d491d34e
ILT
965 // Return the Xindex structure to use.
966 virtual Xindex*
967 do_initialize_xindex() = 0;
968
92de84a6
ILT
969 // Implement get_global_symbol_counts--implemented by child class.
970 virtual void
971 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
972
dde3f402
ILT
973 virtual const Symbols*
974 do_get_global_symbols() const = 0;
975
dbe717ef
ILT
976 // Set the number of sections.
977 void
2ea97941
ILT
978 set_shnum(int shnum)
979 { this->shnum_ = shnum; }
dbe717ef 980
6fa2a40b 981 // Functions used by both Sized_relobj_file and Sized_dynobj.
dbe717ef
ILT
982
983 // Read the section data into a Read_symbols_data object.
984 template<int size, bool big_endian>
985 void
986 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
987 Read_symbols_data*);
988
dc3714f3
AM
989 // Find the section header with the given NAME. If HDR is non-NULL
990 // then it is a section header returned from a previous call to this
991 // function and the next section header with the same name will be
992 // returned.
993 template<int size, bool big_endian>
994 const unsigned char*
995 find_shdr(const unsigned char* pshdrs, const char* name,
996 const char* names, section_size_type names_size,
997 const unsigned char* hdr) const;
998
d491d34e
ILT
999 // Let the child class initialize the xindex object directly.
1000 void
1001 set_xindex(Xindex* xindex)
1002 {
1003 gold_assert(this->xindex_ == NULL);
1004 this->xindex_ = xindex;
1005 }
1006
dbe717ef
ILT
1007 // If NAME is the name of a special .gnu.warning section, arrange
1008 // for the warning to be issued. SHNDX is the section index.
1009 // Return whether it is a warning section.
1010 bool
1011 handle_gnu_warning_section(const char* name, unsigned int shndx,
1012 Symbol_table*);
f6ce93d6 1013
364c7fa5 1014 // If NAME is the name of the special section which indicates that
9b547ce6 1015 // this object was compiled with -fsplit-stack, mark it accordingly,
364c7fa5
ILT
1016 // and return true. Otherwise return false.
1017 bool
1018 handle_split_stack_section(const char* name);
1019
5dd8762a
CC
1020 // Discard any buffers of decompressed sections. This is done
1021 // at the end of the Add_symbols task.
1022 virtual void
1023 do_discard_decompressed_sections()
1024 { }
1025
09ec0418
CC
1026 // Return the index of the first incremental relocation for symbol SYMNDX--
1027 // implemented by child class.
1028 virtual unsigned int
1029 do_get_incremental_reloc_base(unsigned int) const
1030 { gold_unreachable(); }
1031
1032 // Return the number of incremental relocations for symbol SYMNDX--
1033 // implemented by child class.
1034 virtual unsigned int
1035 do_get_incremental_reloc_count(unsigned int) const
1036 { gold_unreachable(); }
1037
6b2353a5 1038 // Return the output view for a section.
39040bb9 1039 virtual unsigned char*
6b2353a5
CC
1040 do_get_output_view(unsigned int, section_size_type*) const
1041 { gold_unreachable(); }
1042
0d5bbdb0
CC
1043 void
1044 set_compressed_sections(Compressed_section_map* compressed_sections)
1045 { this->compressed_sections_ = compressed_sections; }
1046
1047 Compressed_section_map*
1048 compressed_sections()
1049 { return this->compressed_sections_; }
1050
f6ce93d6
ILT
1051 private:
1052 // This class may not be copied.
1053 Object(const Object&);
1054 Object& operator=(const Object&);
1055
1056 // Name of object as printed to user.
1057 std::string name_;
1058 // For reading the file.
1059 Input_file* input_file_;
1060 // Offset within the file--0 for an object file, non-0 for an
1061 // archive.
1062 off_t offset_;
dbe717ef
ILT
1063 // Number of input sections.
1064 unsigned int shnum_;
f6ce93d6 1065 // Whether this is a dynamic object.
594c8e5e
ILT
1066 bool is_dynamic_ : 1;
1067 // Whether this object is needed. This is only set for dynamic
1068 // objects, and means that the object defined a symbol which was
1069 // used by a reference from a regular object.
1070 bool is_needed_ : 1;
364c7fa5 1071 // Whether this object was compiled with -fsplit-stack.
594c8e5e 1072 bool uses_split_stack_ : 1;
364c7fa5
ILT
1073 // Whether this object contains any functions compiled with the
1074 // no_split_stack attribute.
594c8e5e 1075 bool has_no_split_stack_ : 1;
65514900
CC
1076 // True if exclude this object from automatic symbol export.
1077 // This is used only for archive objects.
594c8e5e 1078 bool no_export_ : 1;
cdc29364
CC
1079 // True if the object was found in a system directory.
1080 bool is_in_system_directory_ : 1;
0f1c85a6
CC
1081 // True if the object was linked with --as-needed.
1082 bool as_needed_ : 1;
594c8e5e
ILT
1083 // Many sections for objects with more than SHN_LORESERVE sections.
1084 Xindex* xindex_;
0d5bbdb0
CC
1085 // For compressed debug sections, map section index to uncompressed size
1086 // and contents.
1087 Compressed_section_map* compressed_sections_;
f6ce93d6
ILT
1088};
1089
f6ce93d6 1090// A regular object (ET_REL). This is an abstract base class itself.
6fa2a40b 1091// The implementation is the template class Sized_relobj_file.
f6ce93d6
ILT
1092
1093class Relobj : public Object
1094{
1095 public:
2ea97941
ILT
1096 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
1097 : Object(name, input_file, false, offset),
ef9beddf 1098 output_sections_(),
6a74a719 1099 map_to_relocatable_relocs_(NULL),
4625f782 1100 object_merge_map_(NULL),
ef15dade 1101 relocs_must_follow_section_writes_(false),
09ec0418
CC
1102 sd_(NULL),
1103 reloc_counts_(NULL),
6fa2a40b
CC
1104 reloc_bases_(NULL),
1105 first_dyn_reloc_(0),
1106 dyn_reloc_count_(0)
f6ce93d6
ILT
1107 { }
1108
6d03d481
ST
1109 // During garbage collection, the Read_symbols_data pass for
1110 // each object is stored as layout needs to be done after
1111 // reloc processing.
1112 Symbols_data*
1113 get_symbols_data()
1114 { return this->sd_; }
1115
1116 // Decides which section names have to be included in the worklist
1117 // as roots.
1118 bool
ca09d69a 1119 is_section_name_included(const char* name);
6d03d481
ST
1120
1121 void
1122 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
1123 unsigned int section_header_size);
1124
1125 void
1126 set_symbols_data(Symbols_data* sd)
1127 { this->sd_ = sd; }
1128
1129 // During garbage collection, the Read_relocs pass for all objects
1130 // is done before scanning the relocs. In that case, this->rd_ is
1131 // used to store the information from Read_relocs for each object.
1132 // This data is also used to compute the list of relevant sections.
1133 Read_relocs_data*
1134 get_relocs_data()
1135 { return this->rd_; }
1136
1137 void
1138 set_relocs_data(Read_relocs_data* rd)
1139 { this->rd_ = rd; }
1140
1141 virtual bool
1142 is_output_section_offset_invalid(unsigned int shndx) const = 0;
1143
92e059d8 1144 // Read the relocs.
a2fb1b05 1145 void
92e059d8
ILT
1146 read_relocs(Read_relocs_data* rd)
1147 { return this->do_read_relocs(rd); }
1148
6d03d481
ST
1149 // Process the relocs, during garbage collection only.
1150 void
2ea97941
ILT
1151 gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
1152 { return this->do_gc_process_relocs(symtab, layout, rd); }
6d03d481 1153
92e059d8
ILT
1154 // Scan the relocs and adjust the symbol table.
1155 void
2ea97941
ILT
1156 scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
1157 { return this->do_scan_relocs(symtab, layout, rd); }
a2fb1b05 1158
83896202
ILT
1159 // Return the value of the local symbol whose index is SYMNDX, plus
1160 // ADDEND. ADDEND is passed in so that we can correctly handle the
1161 // section symbol for a merge section.
1162 uint64_t
1163 local_symbol_value(unsigned int symndx, uint64_t addend) const
1164 { return this->do_local_symbol_value(symndx, addend); }
1165
1166 // Return the PLT offset for a local symbol. It is an error to call
1167 // this if it doesn't have one.
1168 unsigned int
1169 local_plt_offset(unsigned int symndx) const
1170 { return this->do_local_plt_offset(symndx); }
1171
1172 // Return whether the local symbol SYMNDX has a GOT offset of type
1173 // GOT_TYPE.
1174 bool
1175 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
7ef8ae7c
VR
1176 { return this->do_local_has_got_offset(symndx, got_type, 0); }
1177
1178 // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
1179 // of type GOT_TYPE.
1180 bool
1181 local_has_got_offset(unsigned int symndx, unsigned int got_type,
1182 uint64_t addend) const
1183 { return this->do_local_has_got_offset(symndx, got_type, addend); }
83896202
ILT
1184
1185 // Return the GOT offset of type GOT_TYPE of the local symbol
1186 // SYMNDX. It is an error to call this if the symbol does not have
1187 // a GOT offset of the specified type.
1188 unsigned int
1189 local_got_offset(unsigned int symndx, unsigned int got_type) const
7ef8ae7c
VR
1190 { return this->do_local_got_offset(symndx, got_type, 0); }
1191
1192 // Return the GOT offset of type GOT_TYPE of the local symbol
1193 // SYMNDX plus ADDEND. It is an error to call this if the symbol
1194 // does not have a GOT offset of the specified type.
1195 unsigned int
1196 local_got_offset(unsigned int symndx, unsigned int got_type,
1197 uint64_t addend) const
1198 { return this->do_local_got_offset(symndx, got_type, addend); }
83896202
ILT
1199
1200 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1201 // to GOT_OFFSET.
1202 void
1203 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1204 unsigned int got_offset)
7ef8ae7c
VR
1205 { this->do_set_local_got_offset(symndx, got_type, got_offset, 0); }
1206
1207 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1208 // plus ADDEND to GOT_OFFSET.
1209 void
1210 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1211 unsigned int got_offset, uint64_t addend)
1212 { this->do_set_local_got_offset(symndx, got_type, got_offset, addend); }
83896202 1213
d77a0555
ILT
1214 // Return whether the local symbol SYMNDX is a TLS symbol.
1215 bool
1216 local_is_tls(unsigned int symndx) const
1217 { return this->do_local_is_tls(symndx); }
1218
6d013333
ILT
1219 // The number of local symbols in the input symbol table.
1220 virtual unsigned int
1221 local_symbol_count() const
1222 { return this->do_local_symbol_count(); }
1223
f0f9babf
CC
1224 // The number of local symbols in the output symbol table.
1225 virtual unsigned int
1226 output_local_symbol_count() const
1227 { return this->do_output_local_symbol_count(); }
1228
1229 // The file offset for local symbols in the output symbol table.
1230 virtual off_t
1231 local_symbol_offset() const
1232 { return this->do_local_symbol_offset(); }
1233
7bf1f802
ILT
1234 // Initial local symbol processing: count the number of local symbols
1235 // in the output symbol table and dynamic symbol table; add local symbol
1236 // names to *POOL and *DYNPOOL.
1237 void
1238 count_local_symbols(Stringpool_template<char>* pool,
1239 Stringpool_template<char>* dynpool)
1240 { return this->do_count_local_symbols(pool, dynpool); }
1241
1242 // Set the values of the local symbols, set the output symbol table
1243 // indexes for the local variables, and set the offset where local
1244 // symbol information will be stored. Returns the new local symbol index.
1245 unsigned int
ef15dade
ST
1246 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
1247 { return this->do_finalize_local_symbols(index, off, symtab); }
7bf1f802
ILT
1248
1249 // Set the output dynamic symbol table indexes for the local variables.
c06b7b0b 1250 unsigned int
7bf1f802
ILT
1251 set_local_dynsym_indexes(unsigned int index)
1252 { return this->do_set_local_dynsym_indexes(index); }
1253
1254 // Set the offset where local dynamic symbol information will be stored.
1255 unsigned int
1256 set_local_dynsym_offset(off_t off)
1257 { return this->do_set_local_dynsym_offset(off); }
75f65a3e 1258
6fa2a40b
CC
1259 // Record a dynamic relocation against an input section from this object.
1260 void
1261 add_dyn_reloc(unsigned int index)
1262 {
1263 if (this->dyn_reloc_count_ == 0)
1264 this->first_dyn_reloc_ = index;
1265 ++this->dyn_reloc_count_;
1266 }
1267
1268 // Return the index of the first dynamic relocation.
1269 unsigned int
1270 first_dyn_reloc() const
1271 { return this->first_dyn_reloc_; }
1272
1273 // Return the count of dynamic relocations.
1274 unsigned int
1275 dyn_reloc_count() const
1276 { return this->dyn_reloc_count_; }
1277
61ba1cf9
ILT
1278 // Relocate the input sections and write out the local symbols.
1279 void
2ea97941
ILT
1280 relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of)
1281 { return this->do_relocate(symtab, layout, of); }
61ba1cf9 1282
a783673b
ILT
1283 // Return whether an input section is being included in the link.
1284 bool
5a6f7e2d 1285 is_section_included(unsigned int shndx) const
a783673b 1286 {
ef9beddf
ILT
1287 gold_assert(shndx < this->output_sections_.size());
1288 return this->output_sections_[shndx] != NULL;
a783673b
ILT
1289 }
1290
9aff4b7a 1291 // The output section of the input section with index SHNDX.
2b328d4e
DK
1292 // This is only used currently to remove a section from the link in
1293 // relaxation.
1294 void
1295 set_output_section(unsigned int shndx, Output_section* os)
1296 {
1297 gold_assert(shndx < this->output_sections_.size());
1298 this->output_sections_[shndx] = os;
1299 }
1300
ead1e424 1301 // Set the offset of an input section within its output section.
5995b570 1302 void
ef9beddf
ILT
1303 set_section_offset(unsigned int shndx, uint64_t off)
1304 { this->do_set_section_offset(shndx, off); }
e94cf127 1305
730cdc88
ILT
1306 // Return true if we need to wait for output sections to be written
1307 // before we can apply relocations. This is true if the object has
1308 // any relocations for sections which require special handling, such
1309 // as the exception frame section.
1310 bool
17a1d0a9 1311 relocs_must_follow_section_writes() const
730cdc88
ILT
1312 { return this->relocs_must_follow_section_writes_; }
1313
0916f9e7
RÁE
1314 Object_merge_map*
1315 get_or_create_merge_map();
1316
dbe40a88
RÁE
1317 template<int size>
1318 void
1319 initialize_input_to_output_map(unsigned int shndx,
1320 typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
1321 Unordered_map<section_offset_type,
1322 typename elfcpp::Elf_types<size>::Elf_Addr>* output_address) const;
4625f782 1323
4625f782 1324 void
dbe40a88
RÁE
1325 add_merge_mapping(Output_section_data *output_data,
1326 unsigned int shndx, section_offset_type offset,
1327 section_size_type length,
1328 section_offset_type output_offset);
1329
1330 bool
1331 merge_output_offset(unsigned int shndx, section_offset_type offset,
1332 section_offset_type *poutput) const;
1333
67f95b96
RÁE
1334 const Output_section_data*
1335 find_merge_section(unsigned int shndx) const;
4625f782 1336
6a74a719
ILT
1337 // Record the relocatable reloc info for an input reloc section.
1338 void
1339 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
1340 {
1341 gold_assert(reloc_shndx < this->shnum());
1342 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
1343 }
1344
1345 // Get the relocatable reloc info for an input reloc section.
1346 Relocatable_relocs*
1347 relocatable_relocs(unsigned int reloc_shndx)
1348 {
1349 gold_assert(reloc_shndx < this->shnum());
1350 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
1351 }
1352
5995b570
CC
1353 // Layout sections whose layout was deferred while waiting for
1354 // input files from a plugin.
1355 void
2ea97941
ILT
1356 layout_deferred_sections(Layout* layout)
1357 { this->do_layout_deferred_sections(layout); }
5995b570 1358
09ec0418
CC
1359 // Return the index of the first incremental relocation for symbol SYMNDX.
1360 virtual unsigned int
1361 do_get_incremental_reloc_base(unsigned int symndx) const
1362 { return this->reloc_bases_[symndx]; }
1363
1364 // Return the number of incremental relocations for symbol SYMNDX.
1365 virtual unsigned int
1366 do_get_incremental_reloc_count(unsigned int symndx) const
1367 { return this->reloc_counts_[symndx]; }
1368
9fc236f3
CC
1369 // Return the word size of the object file.
1370 int
1371 elfsize() const
1372 { return this->do_elfsize(); }
1373
1374 // Return TRUE if this is a big-endian object file.
1375 bool
1376 is_big_endian() const
1377 { return this->do_is_big_endian(); }
1378
a783673b 1379 protected:
ef9beddf
ILT
1380 // The output section to be used for each input section, indexed by
1381 // the input section number. The output section is NULL if the
1382 // input section is to be discarded.
1383 typedef std::vector<Output_section*> Output_sections;
e94cf127 1384
92e059d8
ILT
1385 // Read the relocs--implemented by child class.
1386 virtual void
1387 do_read_relocs(Read_relocs_data*) = 0;
1388
6d03d481
ST
1389 // Process the relocs--implemented by child class.
1390 virtual void
ad0f2072 1391 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
6d03d481 1392
92e059d8
ILT
1393 // Scan the relocs--implemented by child class.
1394 virtual void
ad0f2072 1395 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
92e059d8 1396
83896202
ILT
1397 // Return the value of a local symbol.
1398 virtual uint64_t
1399 do_local_symbol_value(unsigned int symndx, uint64_t addend) const = 0;
1400
1401 // Return the PLT offset of a local symbol.
1402 virtual unsigned int
1403 do_local_plt_offset(unsigned int symndx) const = 0;
1404
7ef8ae7c
VR
1405 // Return whether a local symbol plus addend has a GOT offset
1406 // of a given type.
83896202
ILT
1407 virtual bool
1408 do_local_has_got_offset(unsigned int symndx,
7ef8ae7c 1409 unsigned int got_type, uint64_t addend) const = 0;
83896202 1410
7ef8ae7c 1411 // Return the GOT offset of a given type of a local symbol plus addend.
83896202 1412 virtual unsigned int
7ef8ae7c
VR
1413 do_local_got_offset(unsigned int symndx, unsigned int got_type,
1414 uint64_t addend) const = 0;
83896202 1415
7ef8ae7c 1416 // Set the GOT offset with a given type for a local symbol plus addend.
83896202
ILT
1417 virtual void
1418 do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
7ef8ae7c 1419 unsigned int got_offset, uint64_t addend) = 0;
83896202 1420
d77a0555
ILT
1421 // Return whether local symbol SYMNDX is a TLS symbol.
1422 virtual bool
1423 do_local_is_tls(unsigned int symndx) const = 0;
1424
6d013333
ILT
1425 // Return the number of local symbols--implemented by child class.
1426 virtual unsigned int
1427 do_local_symbol_count() const = 0;
1428
f0f9babf
CC
1429 // Return the number of output local symbols--implemented by child class.
1430 virtual unsigned int
1431 do_output_local_symbol_count() const = 0;
1432
1433 // Return the file offset for local symbols--implemented by child class.
1434 virtual off_t
1435 do_local_symbol_offset() const = 0;
1436
7bf1f802
ILT
1437 // Count local symbols--implemented by child class.
1438 virtual void
1439 do_count_local_symbols(Stringpool_template<char>*,
6a74a719 1440 Stringpool_template<char>*) = 0;
75f65a3e 1441
6a74a719
ILT
1442 // Finalize the local symbols. Set the output symbol table indexes
1443 // for the local variables, and set the offset where local symbol
1444 // information will be stored.
7bf1f802 1445 virtual unsigned int
ef15dade 1446 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
7bf1f802
ILT
1447
1448 // Set the output dynamic symbol table indexes for the local variables.
1449 virtual unsigned int
1450 do_set_local_dynsym_indexes(unsigned int) = 0;
1451
1452 // Set the offset where local dynamic symbol information will be stored.
1453 virtual unsigned int
1454 do_set_local_dynsym_offset(off_t) = 0;
1455
61ba1cf9
ILT
1456 // Relocate the input sections and write out the local
1457 // symbols--implemented by child class.
1458 virtual void
ad0f2072 1459 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
92e059d8 1460
ef9beddf
ILT
1461 // Set the offset of a section--implemented by child class.
1462 virtual void
1463 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
e94cf127 1464
5995b570
CC
1465 // Layout sections whose layout was deferred while waiting for
1466 // input files from a plugin--implemented by child class.
1467 virtual void
1468 do_layout_deferred_sections(Layout*) = 0;
1469
09ec0418
CC
1470 // Given a section index, return the corresponding Output_section.
1471 // The return value will be NULL if the section is not included in
1472 // the link.
1473 Output_section*
1474 do_output_section(unsigned int shndx) const
1475 {
1476 gold_assert(shndx < this->output_sections_.size());
1477 return this->output_sections_[shndx];
1478 }
1479
ef9beddf
ILT
1480 // Return the vector mapping input sections to output sections.
1481 Output_sections&
1482 output_sections()
1483 { return this->output_sections_; }
e94cf127 1484
ef9beddf
ILT
1485 const Output_sections&
1486 output_sections() const
1487 { return this->output_sections_; }
e94cf127 1488
6a74a719
ILT
1489 // Set the size of the relocatable relocs array.
1490 void
1491 size_relocatable_relocs()
1492 {
1493 this->map_to_relocatable_relocs_ =
1494 new std::vector<Relocatable_relocs*>(this->shnum());
1495 }
1496
730cdc88
ILT
1497 // Record that we must wait for the output sections to be written
1498 // before applying relocations.
1499 void
1500 set_relocs_must_follow_section_writes()
1501 { this->relocs_must_follow_section_writes_ = true; }
1502
09ec0418
CC
1503 // Allocate the array for counting incremental relocations.
1504 void
1505 allocate_incremental_reloc_counts()
1506 {
1507 unsigned int nsyms = this->do_get_global_symbols()->size();
1508 this->reloc_counts_ = new unsigned int[nsyms];
1509 gold_assert(this->reloc_counts_ != NULL);
1510 memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int));
1511 }
1512
1513 // Record a relocation in this object referencing global symbol SYMNDX.
1514 // Used for tracking incremental link information.
1515 void
1516 count_incremental_reloc(unsigned int symndx)
1517 {
1518 unsigned int nsyms = this->do_get_global_symbols()->size();
1519 gold_assert(symndx < nsyms);
1520 gold_assert(this->reloc_counts_ != NULL);
1521 ++this->reloc_counts_[symndx];
1522 }
1523
1524 // Finalize the incremental relocation information.
1525 void
cdc29364 1526 finalize_incremental_relocs(Layout* layout, bool clear_counts);
09ec0418
CC
1527
1528 // Return the index of the next relocation to be written for global symbol
1529 // SYMNDX. Only valid after finalize_incremental_relocs() has been called.
1530 unsigned int
1531 next_incremental_reloc_index(unsigned int symndx)
1532 {
1533 unsigned int nsyms = this->do_get_global_symbols()->size();
1534
1535 gold_assert(this->reloc_counts_ != NULL);
1536 gold_assert(this->reloc_bases_ != NULL);
1537 gold_assert(symndx < nsyms);
1538
1539 unsigned int counter = this->reloc_counts_[symndx]++;
1540 return this->reloc_bases_[symndx] + counter;
1541 }
1542
9fc236f3
CC
1543 // Return the word size of the object file--
1544 // implemented by child class.
1545 virtual int
1546 do_elfsize() const = 0;
1547
1548 // Return TRUE if this is a big-endian object file--
1549 // implemented by child class.
1550 virtual bool
1551 do_is_big_endian() const = 0;
1552
bae7f79e 1553 private:
a2fb1b05 1554 // Mapping from input sections to output section.
ef9beddf 1555 Output_sections output_sections_;
6a74a719
ILT
1556 // Mapping from input section index to the information recorded for
1557 // the relocations. This is only used for a relocatable link.
1558 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
4625f782
ILT
1559 // Mappings for merge sections. This is managed by the code in the
1560 // Merge_map class.
1561 Object_merge_map* object_merge_map_;
730cdc88
ILT
1562 // Whether we need to wait for output sections to be written before
1563 // we can apply relocations.
1564 bool relocs_must_follow_section_writes_;
6d03d481
ST
1565 // Used to store the relocs data computed by the Read_relocs pass.
1566 // Used during garbage collection of unused sections.
1567 Read_relocs_data* rd_;
1568 // Used to store the symbols data computed by the Read_symbols pass.
1569 // Again used during garbage collection when laying out referenced
1570 // sections.
ca09d69a 1571 gold::Symbols_data* sd_;
09ec0418
CC
1572 // Per-symbol counts of relocations, for incremental links.
1573 unsigned int* reloc_counts_;
1574 // Per-symbol base indexes of relocations, for incremental links.
1575 unsigned int* reloc_bases_;
6fa2a40b
CC
1576 // Index of the first dynamic relocation for this object.
1577 unsigned int first_dyn_reloc_;
1578 // Count of dynamic relocations for this object.
1579 unsigned int dyn_reloc_count_;
bae7f79e
ILT
1580};
1581
a9a60db6
ILT
1582// This class is used to handle relocations against a section symbol
1583// in an SHF_MERGE section. For such a symbol, we need to know the
1584// addend of the relocation before we can determine the final value.
1585// The addend gives us the location in the input section, and we can
1586// determine how it is mapped to the output section. For a
1587// non-section symbol, we apply the addend to the final value of the
1588// symbol; that is done in finalize_local_symbols, and does not use
1589// this class.
1590
1591template<int size>
1592class Merged_symbol_value
1593{
1594 public:
1595 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1596
1597 // We use a hash table to map offsets in the input section to output
1598 // addresses.
1599 typedef Unordered_map<section_offset_type, Value> Output_addresses;
1600
1601 Merged_symbol_value(Value input_value, Value output_start_address)
1602 : input_value_(input_value), output_start_address_(output_start_address),
1603 output_addresses_()
1604 { }
1605
1606 // Initialize the hash table.
1607 void
1608 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
1609
1610 // Release the hash table to save space.
1611 void
1612 free_input_to_output_map()
1613 { this->output_addresses_.clear(); }
1614
1615 // Get the output value corresponding to an addend. The object and
1616 // input section index are passed in because the caller will have
1617 // them; otherwise we could store them here.
1618 Value
1619 value(const Relobj* object, unsigned int input_shndx, Value addend) const
1620 {
7f649c59
ILT
1621 // This is a relocation against a section symbol. ADDEND is the
1622 // offset in the section. The result should be the start of some
1623 // merge area. If the object file wants something else, it should
1624 // use a regular symbol rather than a section symbol.
1625 // Unfortunately, PR 6658 shows a case in which the object file
1626 // refers to the section symbol, but uses a negative ADDEND to
1627 // compensate for a PC relative reloc. We can't handle the
1628 // general case. However, we can handle the special case of a
1629 // negative addend, by assuming that it refers to the start of the
1630 // section. Of course, that means that we have to guess when
1631 // ADDEND is negative. It is normal to see a 32-bit value here
1632 // even when the template parameter size is 64, as 64-bit object
1633 // file formats have 32-bit relocations. We know this is a merge
1634 // section, so we know it has to fit into memory. So we assume
1635 // that we won't see a value larger than a large 32-bit unsigned
1636 // value. This will break objects with very very large merge
1637 // sections; they probably break in other ways anyhow.
1638 Value input_offset = this->input_value_;
1639 if (addend < 0xffffff00)
1640 {
1641 input_offset += addend;
1642 addend = 0;
1643 }
a9a60db6
ILT
1644 typename Output_addresses::const_iterator p =
1645 this->output_addresses_.find(input_offset);
1646 if (p != this->output_addresses_.end())
7f649c59 1647 return p->second + addend;
a9a60db6 1648
7f649c59
ILT
1649 return (this->value_from_output_section(object, input_shndx, input_offset)
1650 + addend);
a9a60db6
ILT
1651 }
1652
1653 private:
1654 // Get the output value for an input offset if we couldn't find it
1655 // in the hash table.
1656 Value
1657 value_from_output_section(const Relobj*, unsigned int input_shndx,
1658 Value input_offset) const;
1659
1660 // The value of the section symbol in the input file. This is
1661 // normally zero, but could in principle be something else.
1662 Value input_value_;
1663 // The start address of this merged section in the output file.
1664 Value output_start_address_;
1665 // A hash table which maps offsets in the input section to output
1666 // addresses. This only maps specific offsets, not all offsets.
1667 Output_addresses output_addresses_;
1668};
1669
b8e6aad9
ILT
1670// This POD class is holds the value of a symbol. This is used for
1671// local symbols, and for all symbols during relocation processing.
730cdc88
ILT
1672// For special sections, such as SHF_MERGE sections, this calls a
1673// function to get the final symbol value.
b8e6aad9
ILT
1674
1675template<int size>
1676class Symbol_value
1677{
1678 public:
1679 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1680
1681 Symbol_value()
7bf1f802 1682 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
d491d34e 1683 is_ordinary_shndx_(false), is_section_symbol_(false),
7223e9ca 1684 is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
a9a60db6 1685 { this->u_.value = 0; }
b8e6aad9 1686
aa98ff75
DK
1687 ~Symbol_value()
1688 {
1689 if (!this->has_output_value_)
1690 delete this->u_.merged_symbol_value;
1691 }
1692
b8e6aad9
ILT
1693 // Get the value of this symbol. OBJECT is the object in which this
1694 // symbol is defined, and ADDEND is an addend to add to the value.
1695 template<bool big_endian>
1696 Value
6fa2a40b 1697 value(const Sized_relobj_file<size, big_endian>* object, Value addend) const
b8e6aad9 1698 {
a9a60db6
ILT
1699 if (this->has_output_value_)
1700 return this->u_.value + addend;
1701 else
d491d34e
ILT
1702 {
1703 gold_assert(this->is_ordinary_shndx_);
1704 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1705 addend);
1706 }
b8e6aad9
ILT
1707 }
1708
1709 // Set the value of this symbol in the output symbol table.
1710 void
2ea97941
ILT
1711 set_output_value(Value value)
1712 { this->u_.value = value; }
a9a60db6
ILT
1713
1714 // For a section symbol in a merged section, we need more
1715 // information.
1716 void
1717 set_merged_symbol_value(Merged_symbol_value<size>* msv)
b8e6aad9 1718 {
a9a60db6
ILT
1719 gold_assert(this->is_section_symbol_);
1720 this->has_output_value_ = false;
1721 this->u_.merged_symbol_value = msv;
b8e6aad9
ILT
1722 }
1723
a9a60db6
ILT
1724 // Initialize the input to output map for a section symbol in a
1725 // merged section. We also initialize the value of a non-section
1726 // symbol in a merged section.
b8e6aad9 1727 void
a9a60db6 1728 initialize_input_to_output_map(const Relobj* object)
b8e6aad9 1729 {
a9a60db6
ILT
1730 if (!this->has_output_value_)
1731 {
d491d34e 1732 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
a9a60db6
ILT
1733 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1734 msv->initialize_input_to_output_map(object, this->input_shndx_);
1735 }
b8e6aad9
ILT
1736 }
1737
a9a60db6
ILT
1738 // Free the input to output map for a section symbol in a merged
1739 // section.
1740 void
1741 free_input_to_output_map()
7bf1f802 1742 {
a9a60db6
ILT
1743 if (!this->has_output_value_)
1744 this->u_.merged_symbol_value->free_input_to_output_map();
7bf1f802
ILT
1745 }
1746
a9a60db6
ILT
1747 // Set the value of the symbol from the input file. This is only
1748 // called by count_local_symbols, to communicate the value to
1749 // finalize_local_symbols.
1750 void
2ea97941
ILT
1751 set_input_value(Value value)
1752 { this->u_.value = value; }
a9a60db6
ILT
1753
1754 // Return the input value. This is only called by
e94cf127 1755 // finalize_local_symbols and (in special cases) relocate_section.
a9a60db6
ILT
1756 Value
1757 input_value() const
1758 { return this->u_.value; }
1759
d3bbad62
ILT
1760 // Return whether we have set the index in the output symbol table
1761 // yet.
1762 bool
1763 is_output_symtab_index_set() const
1764 {
1765 return (this->output_symtab_index_ != 0
1766 && this->output_symtab_index_ != -2U);
1767 }
1768
1769 // Return whether this symbol may be discarded from the normal
1770 // symbol table.
1771 bool
1772 may_be_discarded_from_output_symtab() const
1773 {
1774 gold_assert(!this->is_output_symtab_index_set());
1775 return this->output_symtab_index_ != -2U;
1776 }
1777
1778 // Return whether this symbol has an entry in the output symbol
b8e6aad9
ILT
1779 // table.
1780 bool
d3bbad62
ILT
1781 has_output_symtab_entry() const
1782 {
1783 gold_assert(this->is_output_symtab_index_set());
1784 return this->output_symtab_index_ != -1U;
1785 }
b8e6aad9
ILT
1786
1787 // Return the index in the output symbol table.
1788 unsigned int
1789 output_symtab_index() const
1790 {
d3bbad62
ILT
1791 gold_assert(this->is_output_symtab_index_set()
1792 && this->output_symtab_index_ != -1U);
b8e6aad9
ILT
1793 return this->output_symtab_index_;
1794 }
1795
1796 // Set the index in the output symbol table.
1797 void
1798 set_output_symtab_index(unsigned int i)
1799 {
d3bbad62
ILT
1800 gold_assert(!this->is_output_symtab_index_set());
1801 gold_assert(i != 0 && i != -1U && i != -2U);
b8e6aad9
ILT
1802 this->output_symtab_index_ = i;
1803 }
1804
1805 // Record that this symbol should not go into the output symbol
1806 // table.
1807 void
1808 set_no_output_symtab_entry()
1809 {
1810 gold_assert(this->output_symtab_index_ == 0);
1811 this->output_symtab_index_ = -1U;
1812 }
1813
d3bbad62
ILT
1814 // Record that this symbol must go into the output symbol table,
1815 // because it there is a relocation that uses it.
1816 void
1817 set_must_have_output_symtab_entry()
1818 {
1819 gold_assert(!this->is_output_symtab_index_set());
1820 this->output_symtab_index_ = -2U;
1821 }
1822
7bf1f802
ILT
1823 // Set the index in the output dynamic symbol table.
1824 void
1825 set_needs_output_dynsym_entry()
1826 {
dceae3c1 1827 gold_assert(!this->is_section_symbol());
7bf1f802
ILT
1828 this->output_dynsym_index_ = 0;
1829 }
1830
d3bbad62 1831 // Return whether this symbol should go into the dynamic symbol
7bf1f802
ILT
1832 // table.
1833 bool
1834 needs_output_dynsym_entry() const
1835 {
1836 return this->output_dynsym_index_ != -1U;
1837 }
1838
d3bbad62
ILT
1839 // Return whether this symbol has an entry in the dynamic symbol
1840 // table.
1841 bool
1842 has_output_dynsym_entry() const
1843 {
1844 gold_assert(this->output_dynsym_index_ != 0);
1845 return this->output_dynsym_index_ != -1U;
1846 }
1847
7bf1f802
ILT
1848 // Record that this symbol should go into the dynamic symbol table.
1849 void
1850 set_output_dynsym_index(unsigned int i)
1851 {
1852 gold_assert(this->output_dynsym_index_ == 0);
d3bbad62 1853 gold_assert(i != 0 && i != -1U);
7bf1f802
ILT
1854 this->output_dynsym_index_ = i;
1855 }
1856
1857 // Return the index in the output dynamic symbol table.
1858 unsigned int
1859 output_dynsym_index() const
1860 {
dceae3c1
ILT
1861 gold_assert(this->output_dynsym_index_ != 0
1862 && this->output_dynsym_index_ != -1U);
7bf1f802
ILT
1863 return this->output_dynsym_index_;
1864 }
1865
b8e6aad9
ILT
1866 // Set the index of the input section in the input file.
1867 void
d491d34e 1868 set_input_shndx(unsigned int i, bool is_ordinary)
730cdc88
ILT
1869 {
1870 this->input_shndx_ = i;
ac1f0c21
ILT
1871 // input_shndx_ field is a bitfield, so make sure that the value
1872 // fits.
730cdc88 1873 gold_assert(this->input_shndx_ == i);
d491d34e 1874 this->is_ordinary_shndx_ = is_ordinary;
730cdc88 1875 }
b8e6aad9 1876
7bf1f802
ILT
1877 // Return the index of the input section in the input file.
1878 unsigned int
d491d34e
ILT
1879 input_shndx(bool* is_ordinary) const
1880 {
1881 *is_ordinary = this->is_ordinary_shndx_;
1882 return this->input_shndx_;
1883 }
7bf1f802 1884
a9a60db6
ILT
1885 // Whether this is a section symbol.
1886 bool
1887 is_section_symbol() const
1888 { return this->is_section_symbol_; }
1889
063f12a8
ILT
1890 // Record that this is a section symbol.
1891 void
1892 set_is_section_symbol()
dceae3c1
ILT
1893 {
1894 gold_assert(!this->needs_output_dynsym_entry());
1895 this->is_section_symbol_ = true;
1896 }
063f12a8 1897
7bf1f802
ILT
1898 // Record that this is a TLS symbol.
1899 void
1900 set_is_tls_symbol()
1901 { this->is_tls_symbol_ = true; }
1902
7223e9ca 1903 // Return true if this is a TLS symbol.
7bf1f802
ILT
1904 bool
1905 is_tls_symbol() const
1906 { return this->is_tls_symbol_; }
1907
7223e9ca
ILT
1908 // Record that this is an IFUNC symbol.
1909 void
1910 set_is_ifunc_symbol()
1911 { this->is_ifunc_symbol_ = true; }
1912
1913 // Return true if this is an IFUNC symbol.
1914 bool
1915 is_ifunc_symbol() const
1916 { return this->is_ifunc_symbol_; }
1917
aa98ff75
DK
1918 // Return true if this has output value.
1919 bool
1920 has_output_value() const
1921 { return this->has_output_value_; }
1922
b8e6aad9
ILT
1923 private:
1924 // The index of this local symbol in the output symbol table. This
d3bbad62
ILT
1925 // will be 0 if no value has been assigned yet, and the symbol may
1926 // be omitted. This will be -1U if the symbol should not go into
1927 // the symbol table. This will be -2U if the symbol must go into
1928 // the symbol table, but no index has been assigned yet.
b8e6aad9 1929 unsigned int output_symtab_index_;
7bf1f802 1930 // The index of this local symbol in the dynamic symbol table. This
d3bbad62 1931 // will be -1U if the symbol should not go into the symbol table.
7bf1f802 1932 unsigned int output_dynsym_index_;
b8e6aad9
ILT
1933 // The section index in the input file in which this symbol is
1934 // defined.
7223e9ca 1935 unsigned int input_shndx_ : 27;
d491d34e
ILT
1936 // Whether the section index is an ordinary index, not a special
1937 // value.
1938 bool is_ordinary_shndx_ : 1;
063f12a8
ILT
1939 // Whether this is a STT_SECTION symbol.
1940 bool is_section_symbol_ : 1;
7bf1f802
ILT
1941 // Whether this is a STT_TLS symbol.
1942 bool is_tls_symbol_ : 1;
7223e9ca
ILT
1943 // Whether this is a STT_GNU_IFUNC symbol.
1944 bool is_ifunc_symbol_ : 1;
a9a60db6
ILT
1945 // Whether this symbol has a value for the output file. This is
1946 // normally set to true during Layout::finalize, by
1947 // finalize_local_symbols. It will be false for a section symbol in
1948 // a merge section, as for such symbols we can not determine the
1949 // value to use in a relocation until we see the addend.
1950 bool has_output_value_ : 1;
1951 union
1952 {
1953 // This is used if has_output_value_ is true. Between
1954 // count_local_symbols and finalize_local_symbols, this is the
1955 // value in the input file. After finalize_local_symbols, it is
1956 // the value in the output file.
1957 Value value;
1958 // This is used if has_output_value_ is false. It points to the
1959 // information we need to get the value for a merge section.
1960 Merged_symbol_value<size>* merged_symbol_value;
1961 } u_;
b8e6aad9
ILT
1962};
1963
364c7fa5
ILT
1964// This type is used to modify relocations for -fsplit-stack. It is
1965// indexed by relocation index, and means that the relocation at that
1966// index should use the symbol from the vector, rather than the one
1967// indicated by the relocation.
1968
1969class Reloc_symbol_changes
1970{
1971 public:
1972 Reloc_symbol_changes(size_t count)
1973 : vec_(count, NULL)
1974 { }
1975
1976 void
1977 set(size_t i, Symbol* sym)
1978 { this->vec_[i] = sym; }
1979
1980 const Symbol*
1981 operator[](size_t i) const
1982 { return this->vec_[i]; }
1983
1984 private:
1985 std::vector<Symbol*> vec_;
1986};
1987
cdc29364
CC
1988// Abstract base class for a regular object file, either a real object file
1989// or an incremental (unchanged) object. This is size and endian specific.
1990
1991template<int size, bool big_endian>
6fa2a40b 1992class Sized_relobj : public Relobj
cdc29364
CC
1993{
1994 public:
6fa2a40b 1995 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
cdc29364
CC
1996 typedef Relobj::Symbols Symbols;
1997
6fa2a40b
CC
1998 static const Address invalid_address = static_cast<Address>(0) - 1;
1999
2000 Sized_relobj(const std::string& name, Input_file* input_file)
2001 : Relobj(name, input_file), local_got_offsets_(), section_offsets_()
cdc29364
CC
2002 { }
2003
6fa2a40b 2004 Sized_relobj(const std::string& name, Input_file* input_file,
cdc29364 2005 off_t offset)
6fa2a40b 2006 : Relobj(name, input_file, offset), local_got_offsets_(), section_offsets_()
cdc29364
CC
2007 { }
2008
6fa2a40b 2009 ~Sized_relobj()
cdc29364
CC
2010 { }
2011
6fa2a40b
CC
2012 // If this is a regular object, return a pointer to the Sized_relobj_file
2013 // object. Otherwise, return NULL.
2014 virtual Sized_relobj_file<size, big_endian>*
2015 sized_relobj()
2016 { return NULL; }
2017
2018 const virtual Sized_relobj_file<size, big_endian>*
2019 sized_relobj() const
2020 { return NULL; }
2021
2022 // Checks if the offset of input section SHNDX within its output
2023 // section is invalid.
2024 bool
2025 is_output_section_offset_invalid(unsigned int shndx) const
2026 { return this->get_output_section_offset(shndx) == invalid_address; }
2027
2028 // Get the offset of input section SHNDX within its output section.
2029 // This is -1 if the input section requires a special mapping, such
2030 // as a merge section. The output section can be found in the
2031 // output_sections_ field of the parent class Relobj.
2032 Address
2033 get_output_section_offset(unsigned int shndx) const
2034 {
2035 gold_assert(shndx < this->section_offsets_.size());
2036 return this->section_offsets_[shndx];
2037 }
2038
6fa2a40b
CC
2039 // Iterate over local symbols, calling a visitor class V for each GOT offset
2040 // associated with a local symbol.
2041 void
2042 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
2043
cdc29364
CC
2044 protected:
2045 typedef Relobj::Output_sections Output_sections;
2046
6fa2a40b
CC
2047 // Clear the local symbol information.
2048 void
2049 clear_got_offsets()
2050 { this->local_got_offsets_.clear(); }
2051
2052 // Return the vector of section offsets.
2053 std::vector<Address>&
2054 section_offsets()
2055 { return this->section_offsets_; }
2056
c6905c28
CC
2057 // Get the address of an output section.
2058 uint64_t
2059 do_output_section_address(unsigned int shndx);
2060
6fa2a40b
CC
2061 // Get the offset of a section.
2062 uint64_t
2063 do_output_section_offset(unsigned int shndx) const
2064 {
2065 Address off = this->get_output_section_offset(shndx);
2066 if (off == invalid_address)
2067 return -1ULL;
2068 return off;
2069 }
2070
2071 // Set the offset of a section.
2072 void
2073 do_set_section_offset(unsigned int shndx, uint64_t off)
2074 {
2075 gold_assert(shndx < this->section_offsets_.size());
2076 this->section_offsets_[shndx] =
2077 (off == static_cast<uint64_t>(-1)
2078 ? invalid_address
2079 : convert_types<Address, uint64_t>(off));
2080 }
2081
7ef8ae7c
VR
2082 // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
2083 // of type GOT_TYPE.
83896202 2084 bool
7ef8ae7c
VR
2085 do_local_has_got_offset(unsigned int symndx, unsigned int got_type,
2086 uint64_t addend) const
83896202 2087 {
7ef8ae7c 2088 Local_got_entry_key key(symndx, addend);
83896202 2089 Local_got_offsets::const_iterator p =
7ef8ae7c 2090 this->local_got_offsets_.find(key);
83896202
ILT
2091 return (p != this->local_got_offsets_.end()
2092 && p->second->get_offset(got_type) != -1U);
2093 }
2094
2095 // Return the GOT offset of type GOT_TYPE of the local symbol
7ef8ae7c 2096 // SYMNDX plus ADDEND.
83896202 2097 unsigned int
7ef8ae7c
VR
2098 do_local_got_offset(unsigned int symndx, unsigned int got_type,
2099 uint64_t addend) const
83896202 2100 {
7ef8ae7c 2101 Local_got_entry_key key(symndx, addend);
83896202 2102 Local_got_offsets::const_iterator p =
7ef8ae7c 2103 this->local_got_offsets_.find(key);
83896202
ILT
2104 gold_assert(p != this->local_got_offsets_.end());
2105 unsigned int off = p->second->get_offset(got_type);
2106 gold_assert(off != -1U);
2107 return off;
2108 }
2109
2110 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
7ef8ae7c 2111 // plus ADDEND to GOT_OFFSET.
83896202
ILT
2112 void
2113 do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
7ef8ae7c 2114 unsigned int got_offset, uint64_t addend)
83896202 2115 {
7ef8ae7c 2116 Local_got_entry_key key(symndx, addend);
83896202 2117 Local_got_offsets::const_iterator p =
7ef8ae7c 2118 this->local_got_offsets_.find(key);
83896202
ILT
2119 if (p != this->local_got_offsets_.end())
2120 p->second->set_offset(got_type, got_offset);
2121 else
2122 {
2123 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
2124 std::pair<Local_got_offsets::iterator, bool> ins =
7ef8ae7c 2125 this->local_got_offsets_.insert(std::make_pair(key, g));
83896202
ILT
2126 gold_assert(ins.second);
2127 }
2128 }
2129
9fc236f3
CC
2130 // Return the word size of the object file.
2131 virtual int
2132 do_elfsize() const
2133 { return size; }
2134
2135 // Return TRUE if this is a big-endian object file.
2136 virtual bool
2137 do_is_big_endian() const
2138 { return big_endian; }
2139
cdc29364 2140 private:
6fa2a40b
CC
2141 // The GOT offsets of local symbols. This map also stores GOT offsets
2142 // for tp-relative offsets for TLS symbols.
7ef8ae7c
VR
2143 typedef Unordered_map<Local_got_entry_key, Got_offset_list*,
2144 Local_got_entry_key::hash,
2145 Local_got_entry_key::equal_to> Local_got_offsets;
6fa2a40b
CC
2146
2147 // GOT offsets for local non-TLS symbols, and tp-relative offsets
7ef8ae7c 2148 // for TLS symbols, indexed by local got entry key class.
6fa2a40b
CC
2149 Local_got_offsets local_got_offsets_;
2150 // For each input section, the offset of the input section in its
2151 // output section. This is INVALID_ADDRESS if the input section requires a
2152 // special mapping.
2153 std::vector<Address> section_offsets_;
cdc29364
CC
2154};
2155
14bfc3f5 2156// A regular object file. This is size and endian specific.
bae7f79e
ILT
2157
2158template<int size, bool big_endian>
6fa2a40b 2159class Sized_relobj_file : public Sized_relobj<size, big_endian>
bae7f79e
ILT
2160{
2161 public:
c06b7b0b 2162 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6fa2a40b 2163 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
b8e6aad9 2164 typedef std::vector<Symbol_value<size> > Local_values;
c06b7b0b 2165
eff45813
CC
2166 static const Address invalid_address = static_cast<Address>(0) - 1;
2167
aa98ff75
DK
2168 enum Compute_final_local_value_status
2169 {
2170 // No error.
2171 CFLV_OK,
2172 // An error occurred.
2173 CFLV_ERROR,
2174 // The local symbol has no output section.
2175 CFLV_DISCARDED
2176 };
2177
6fa2a40b
CC
2178 Sized_relobj_file(const std::string& name,
2179 Input_file* input_file,
2180 off_t offset,
2181 const typename elfcpp::Ehdr<size, big_endian>&);
bae7f79e 2182
6fa2a40b 2183 ~Sized_relobj_file();
6d03d481 2184
f733487b 2185 // Set up the object file based on TARGET.
bae7f79e 2186 void
c0a62865
DK
2187 setup()
2188 { this->do_setup(); }
bae7f79e 2189
6fa2a40b
CC
2190 // Return a pointer to the Sized_relobj_file object.
2191 Sized_relobj_file<size, big_endian>*
2192 sized_relobj()
2193 { return this; }
2194
2195 const Sized_relobj_file<size, big_endian>*
2196 sized_relobj() const
2197 { return this; }
2198
9590bf25
CC
2199 // Return the ELF file type.
2200 int
2201 e_type() const
2202 { return this->e_type_; }
2203
7d9e3d98
ILT
2204 // Return the number of symbols. This is only valid after
2205 // Object::add_symbols has been called.
2206 unsigned int
2207 symbol_count() const
2208 { return this->local_symbol_count_ + this->symbols_.size(); }
2209
730cdc88
ILT
2210 // If SYM is the index of a global symbol in the object file's
2211 // symbol table, return the Symbol object. Otherwise, return NULL.
2212 Symbol*
2213 global_symbol(unsigned int sym) const
2214 {
2215 if (sym >= this->local_symbol_count_)
2216 {
2217 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
2218 return this->symbols_[sym - this->local_symbol_count_];
2219 }
2220 return NULL;
2221 }
2222
2223 // Return the section index of symbol SYM. Set *VALUE to its value
d491d34e
ILT
2224 // in the object file. Set *IS_ORDINARY if this is an ordinary
2225 // section index, not a special code between SHN_LORESERVE and
2226 // SHN_HIRESERVE. Note that for a symbol which is not defined in
2227 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
2228 // it will not return the final value of the symbol in the link.
730cdc88 2229 unsigned int
d491d34e 2230 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
730cdc88
ILT
2231
2232 // Return a pointer to the Symbol_value structure which holds the
2233 // value of a local symbol.
2234 const Symbol_value<size>*
2235 local_symbol(unsigned int sym) const
2236 {
2237 gold_assert(sym < this->local_values_.size());
2238 return &this->local_values_[sym];
2239 }
2240
c06b7b0b
ILT
2241 // Return the index of local symbol SYM in the ordinary symbol
2242 // table. A value of -1U means that the symbol is not being output.
2243 unsigned int
2244 symtab_index(unsigned int sym) const
2245 {
b8e6aad9
ILT
2246 gold_assert(sym < this->local_values_.size());
2247 return this->local_values_[sym].output_symtab_index();
c06b7b0b
ILT
2248 }
2249
7bf1f802
ILT
2250 // Return the index of local symbol SYM in the dynamic symbol
2251 // table. A value of -1U means that the symbol is not being output.
2252 unsigned int
2253 dynsym_index(unsigned int sym) const
2254 {
2255 gold_assert(sym < this->local_values_.size());
2256 return this->local_values_[sym].output_dynsym_index();
2257 }
2258
6a74a719
ILT
2259 // Return the input section index of local symbol SYM.
2260 unsigned int
d491d34e 2261 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
6a74a719
ILT
2262 {
2263 gold_assert(sym < this->local_values_.size());
d491d34e 2264 return this->local_values_[sym].input_shndx(is_ordinary);
6a74a719
ILT
2265 }
2266
d3bbad62
ILT
2267 // Record that local symbol SYM must be in the output symbol table.
2268 void
2269 set_must_have_output_symtab_entry(unsigned int sym)
2270 {
2271 gold_assert(sym < this->local_values_.size());
2272 this->local_values_[sym].set_must_have_output_symtab_entry();
2273 }
2274
d1f003c6 2275 // Record that local symbol SYM needs a dynamic symbol entry.
7bf1f802
ILT
2276 void
2277 set_needs_output_dynsym_entry(unsigned int sym)
2278 {
2279 gold_assert(sym < this->local_values_.size());
2280 this->local_values_[sym].set_needs_output_dynsym_entry();
2281 }
2282
7223e9ca
ILT
2283 // Return whether the local symbol SYMNDX has a PLT offset.
2284 bool
2285 local_has_plt_offset(unsigned int symndx) const;
2286
7223e9ca
ILT
2287 // Set the PLT offset of the local symbol SYMNDX.
2288 void
2289 set_local_plt_offset(unsigned int symndx, unsigned int plt_offset);
2290
ec4dbad3
AM
2291 // Adjust this local symbol value. Return false if the symbol
2292 // should be discarded from the output file.
2293 bool
2294 adjust_local_symbol(Symbol_value<size>* lv) const
2295 { return this->do_adjust_local_symbol(lv); }
2296
f7e2ee48
ILT
2297 // Return the name of the symbol that spans the given offset in the
2298 // specified section in this object. This is used only for error
2299 // messages and is not particularly efficient.
2300 bool
2301 get_symbol_location_info(unsigned int shndx, off_t offset,
2302 Symbol_location_info* info);
2303
e94cf127
CC
2304 // Look for a kept section corresponding to the given discarded section,
2305 // and return its output address. This is used only for relocations in
2306 // debugging sections.
2307 Address
43193fe9
CC
2308 map_to_kept_section(unsigned int shndx, std::string& section_name,
2309 bool* found) const;
2310
2311 // Look for a kept section corresponding to the given discarded section,
2312 // and return its object file.
2313 Relobj*
2314 find_kept_section_object(unsigned int shndx, unsigned int* symndx_p) const;
2315
2316 // Return the name of symbol SYMNDX.
2317 const char*
2318 get_symbol_name(unsigned int symndx);
e94cf127 2319
aa98ff75
DK
2320 // Compute final local symbol value. R_SYM is the local symbol index.
2321 // LV_IN points to a local symbol value containing the input value.
2322 // LV_OUT points to a local symbol value storing the final output value,
2323 // which must not be a merged symbol value since before calling this
2324 // method to avoid memory leak. SYMTAB points to a symbol table.
2325 //
2326 // The method returns a status code at return. If the return status is
2327 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2328 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2329 // *LV_OUT is not modified.
2330 Compute_final_local_value_status
2331 compute_final_local_value(unsigned int r_sym,
2332 const Symbol_value<size>* lv_in,
2333 Symbol_value<size>* lv_out,
2334 const Symbol_table* symtab);
2335
c924eb67
CC
2336 // Return true if the layout for this object was deferred.
2337 bool is_deferred_layout() const
2338 { return this->is_deferred_layout_; }
2339
6d013333 2340 protected:
6fa2a40b 2341 typedef typename Sized_relobj<size, big_endian>::Output_sections
cdc29364
CC
2342 Output_sections;
2343
c0a62865
DK
2344 // Set up.
2345 virtual void
2346 do_setup();
2347
a2fb1b05 2348 // Read the symbols.
bae7f79e 2349 void
12e14209 2350 do_read_symbols(Read_symbols_data*);
14bfc3f5 2351
f35c4853
CC
2352 // Read the symbols. This is common code for all target-specific
2353 // overrides of do_read_symbols.
2354 void
2355 base_read_symbols(Read_symbols_data*);
2356
83896202
ILT
2357 // Return the value of a local symbol.
2358 uint64_t
2359 do_local_symbol_value(unsigned int symndx, uint64_t addend) const
2360 {
2361 const Symbol_value<size>* symval = this->local_symbol(symndx);
2362 return symval->value(this, addend);
2363 }
2364
2365 // Return the PLT offset for a local symbol. It is an error to call
2366 // this if it doesn't have one.
2367 unsigned int
2368 do_local_plt_offset(unsigned int symndx) const;
2369
d77a0555
ILT
2370 // Return whether local symbol SYMNDX is a TLS symbol.
2371 bool
2372 do_local_is_tls(unsigned int symndx) const
2373 { return this->local_symbol(symndx)->is_tls_symbol(); }
2374
6d013333
ILT
2375 // Return the number of local symbols.
2376 unsigned int
2377 do_local_symbol_count() const
2378 { return this->local_symbol_count_; }
2379
f0f9babf
CC
2380 // Return the number of local symbols in the output symbol table.
2381 unsigned int
2382 do_output_local_symbol_count() const
2383 { return this->output_local_symbol_count_; }
2384
2385 // Return the number of local symbols in the output symbol table.
2386 off_t
2387 do_local_symbol_offset() const
2388 { return this->local_symbol_offset_; }
2389
dbe717ef
ILT
2390 // Lay out the input sections.
2391 void
7e1edb90 2392 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
dbe717ef 2393
5995b570
CC
2394 // Layout sections whose layout was deferred while waiting for
2395 // input files from a plugin.
2396 void
2397 do_layout_deferred_sections(Layout*);
2398
12e14209
ILT
2399 // Add the symbols to the symbol table.
2400 void
f488e4b0 2401 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
b0193076
RÁE
2402
2403 Archive::Should_include
88a4108b 2404 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
b0193076 2405 std::string* why);
a2fb1b05 2406
e0c52780
CC
2407 // Iterate over global symbols, calling a visitor class V for each.
2408 void
2409 do_for_all_global_symbols(Read_symbols_data* sd,
2410 Library_base::Symbol_visitor_base* v);
2411
92e059d8
ILT
2412 // Read the relocs.
2413 void
2414 do_read_relocs(Read_relocs_data*);
2415
6d03d481
ST
2416 // Process the relocs to find list of referenced sections. Used only
2417 // during garbage collection.
2418 void
ad0f2072 2419 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
6d03d481 2420
92e059d8
ILT
2421 // Scan the relocs and adjust the symbol table.
2422 void
ad0f2072 2423 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
92e059d8 2424
7bf1f802
ILT
2425 // Count the local symbols.
2426 void
2427 do_count_local_symbols(Stringpool_template<char>*,
2428 Stringpool_template<char>*);
2429
75f65a3e 2430 // Finalize the local symbols.
c06b7b0b 2431 unsigned int
ef15dade 2432 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
7bf1f802
ILT
2433
2434 // Set the offset where local dynamic symbol information will be stored.
2435 unsigned int
2436 do_set_local_dynsym_indexes(unsigned int);
2437
2438 // Set the offset where local dynamic symbol information will be stored.
2439 unsigned int
2440 do_set_local_dynsym_offset(off_t);
75f65a3e 2441
61ba1cf9
ILT
2442 // Relocate the input sections and write out the local symbols.
2443 void
ad0f2072 2444 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
92e059d8 2445
a445fddf
ILT
2446 // Get the size of a section.
2447 uint64_t
2448 do_section_size(unsigned int shndx)
2449 { return this->elf_file_.section_size(shndx); }
2450
92e059d8
ILT
2451 // Get the name of a section.
2452 std::string
54674d38 2453 do_section_name(unsigned int shndx) const
645f8123 2454 { return this->elf_file_.section_name(shndx); }
61ba1cf9 2455
645f8123 2456 // Return the location of the contents of a section.
c1027032
CC
2457 const unsigned char*
2458 do_section_contents(unsigned int shndx, section_size_type* plen,
2459 bool cache)
2460 {
2461 Object::Location loc(this->elf_file_.section_contents(shndx));
2462 *plen = convert_to_section_size_type(loc.data_size);
2463 if (*plen == 0)
2464 {
2465 static const unsigned char empty[1] = { '\0' };
2466 return empty;
2467 }
2468 return this->get_view(loc.file_offset, *plen, true, cache);
2469 }
f6ce93d6 2470
a3ad94ed
ILT
2471 // Return section flags.
2472 uint64_t
ef15dade
ST
2473 do_section_flags(unsigned int shndx);
2474
2475 // Return section entsize.
2476 uint64_t
2477 do_section_entsize(unsigned int shndx);
a3ad94ed 2478
88dd47ac
ILT
2479 // Return section address.
2480 uint64_t
2481 do_section_address(unsigned int shndx)
2482 { return this->elf_file_.section_addr(shndx); }
2483
730cdc88
ILT
2484 // Return section type.
2485 unsigned int
2486 do_section_type(unsigned int shndx)
2487 { return this->elf_file_.section_type(shndx); }
2488
f7e2ee48
ILT
2489 // Return the section link field.
2490 unsigned int
2491 do_section_link(unsigned int shndx)
2492 { return this->elf_file_.section_link(shndx); }
2493
4c50553d
ILT
2494 // Return the section info field.
2495 unsigned int
2496 do_section_info(unsigned int shndx)
2497 { return this->elf_file_.section_info(shndx); }
2498
a445fddf
ILT
2499 // Return the section alignment.
2500 uint64_t
2501 do_section_addralign(unsigned int shndx)
2502 { return this->elf_file_.section_addralign(shndx); }
2503
d491d34e
ILT
2504 // Return the Xindex structure to use.
2505 Xindex*
2506 do_initialize_xindex();
2507
92de84a6
ILT
2508 // Get symbol counts.
2509 void
2510 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
2511
dde3f402
ILT
2512 // Get the global symbols.
2513 const Symbols*
2514 do_get_global_symbols() const
2515 { return &this->symbols_; }
2516
c0a62865
DK
2517 // Adjust a section index if necessary.
2518 unsigned int
2519 adjust_shndx(unsigned int shndx)
2520 {
2521 if (shndx >= elfcpp::SHN_LORESERVE)
2522 shndx += this->elf_file_.large_shndx_offset();
2523 return shndx;
2524 }
2525
2526 // Initialize input to output maps for section symbols in merged
2527 // sections.
2528 void
2529 initialize_input_to_output_maps();
2530
2531 // Free the input to output maps for section symbols in merged
2532 // sections.
2533 void
2534 free_input_to_output_maps();
2535
2536 // Return symbol table section index.
2537 unsigned int
2538 symtab_shndx() const
2539 { return this->symtab_shndx_; }
2540
2541 // Allow a child class to access the ELF file.
2542 elfcpp::Elf_file<size, big_endian, Object>*
2543 elf_file()
2544 { return &this->elf_file_; }
2545
2546 // Allow a child class to access the local values.
2547 Local_values*
2548 local_values()
2549 { return &this->local_values_; }
2550
72adc4fa
DK
2551 // Views and sizes when relocating.
2552 struct View_size
2553 {
2554 unsigned char* view;
2555 typename elfcpp::Elf_types<size>::Elf_Addr address;
2556 off_t offset;
2557 section_size_type view_size;
2558 bool is_input_output_view;
2559 bool is_postprocessing_view;
487b39df 2560 bool is_ctors_reverse_view;
72adc4fa
DK
2561 };
2562
2563 typedef std::vector<View_size> Views;
2564
cf43a2fe
AM
2565 // Stash away info for a number of special sections.
2566 // Return true if any of the sections found require local symbols to be read.
2567 virtual bool
2568 do_find_special_sections(Read_symbols_data* sd);
2569
72adc4fa
DK
2570 // This may be overriden by a child class.
2571 virtual void
ad0f2072 2572 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
09ec0418
CC
2573 const unsigned char* pshdrs, Output_file* of,
2574 Views* pviews);
72adc4fa 2575
98461510
CC
2576 // Relocate section data for a range of sections.
2577 void
2578 relocate_section_range(const Symbol_table* symtab, const Layout* layout,
2579 const unsigned char* pshdrs, Output_file* of,
2580 Views* pviews, unsigned int start_shndx,
2581 unsigned int end_shndx);
2582
ec4dbad3
AM
2583 // Adjust this local symbol value. Return false if the symbol
2584 // should be discarded from the output file.
2585 virtual bool
2586 do_adjust_local_symbol(Symbol_value<size>*) const
2587 { return true; }
2588
e7eca48c
DK
2589 // Allow a child to set output local symbol count.
2590 void
2591 set_output_local_symbol_count(unsigned int value)
2592 { this->output_local_symbol_count_ = value; }
a2e47362 2593
6b2353a5 2594 // Return the output view for a section.
39040bb9 2595 unsigned char*
6b2353a5
CC
2596 do_get_output_view(unsigned int, section_size_type*) const;
2597
bae7f79e 2598 private:
a2fb1b05 2599 // For convenience.
6fa2a40b 2600 typedef Sized_relobj_file<size, big_endian> This;
a2fb1b05
ILT
2601 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
2602 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2603 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e 2604 typedef elfcpp::Shdr<size, big_endian> Shdr;
bce5a025 2605 typedef elfcpp::Shdr_write<size, big_endian> Shdr_write;
75f65a3e 2606
ef9beddf
ILT
2607 // To keep track of discarded comdat sections, we need to map a member
2608 // section index to the object and section index of the corresponding
2609 // kept section.
2610 struct Kept_comdat_section
2611 {
43193fe9
CC
2612 Kept_comdat_section(uint64_t a_sh_size, Kept_section* a_kept_section,
2613 unsigned int a_symndx, bool a_is_comdat)
2614 : sh_size(a_sh_size), kept_section(a_kept_section),
2615 symndx (a_symndx), is_comdat(a_is_comdat)
ef9beddf 2616 { }
43193fe9
CC
2617 uint64_t sh_size; // Section size
2618 Kept_section* kept_section; // Kept section info
2619 unsigned int symndx; // Index of key symbol
2620 bool is_comdat; // True if comdat group, false if linkonce
ef9beddf 2621 };
1ef4d87f 2622 typedef std::map<unsigned int, Kept_comdat_section>
ef9beddf
ILT
2623 Kept_comdat_section_table;
2624
dbe717ef
ILT
2625 // Find the SHT_SYMTAB section, given the section headers.
2626 void
2627 find_symtab(const unsigned char* pshdrs);
2628
730cdc88
ILT
2629 // Return whether SHDR has the right flags for a GNU style exception
2630 // frame section.
2631 bool
2632 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
2633
2634 // Return whether there is a section named .eh_frame which might be
2635 // a GNU style exception frame section.
2636 bool
2637 find_eh_frame(const unsigned char* pshdrs, const char* names,
8383303e 2638 section_size_type names_size) const;
730cdc88 2639
a2fb1b05
ILT
2640 // Whether to include a section group in the link.
2641 bool
6a74a719 2642 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
ca09d69a 2643 const unsigned char*, const char*, section_size_type,
a2fb1b05
ILT
2644 std::vector<bool>*);
2645
2646 // Whether to include a linkonce section in the link.
2647 bool
e94cf127 2648 include_linkonce_section(Layout*, unsigned int, const char*,
a2fb1b05
ILT
2649 const elfcpp::Shdr<size, big_endian>&);
2650
5995b570
CC
2651 // Layout an input section.
2652 void
2653 layout_section(Layout* layout, unsigned int shndx, const char* name,
bce5a025
CC
2654 const typename This::Shdr& shdr, unsigned int sh_type,
2655 unsigned int reloc_shndx, unsigned int reloc_type);
5995b570 2656
14788a3f
ILT
2657 // Layout an input .eh_frame section.
2658 void
2659 layout_eh_frame_section(Layout* layout, const unsigned char* symbols_data,
2660 section_size_type symbols_size,
2661 const unsigned char* symbol_names_data,
2662 section_size_type symbol_names_size,
2663 unsigned int shndx, const typename This::Shdr&,
2664 unsigned int reloc_shndx, unsigned int reloc_type);
2665
61ba1cf9
ILT
2666 // Write section data to the output file. Record the views and
2667 // sizes in VIEWS for use when relocating.
2668 void
487b39df
ILT
2669 write_sections(const Layout*, const unsigned char* pshdrs, Output_file*,
2670 Views*);
61ba1cf9
ILT
2671
2672 // Relocate the sections in the output file.
2673 void
2ea97941 2674 relocate_sections(const Symbol_table* symtab, const Layout* layout,
09ec0418
CC
2675 const unsigned char* pshdrs, Output_file* of,
2676 Views* pviews)
2677 { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); }
61ba1cf9 2678
487b39df
ILT
2679 // Reverse the words in a section. Used for .ctors sections mapped
2680 // to .init_array sections.
2681 void
2682 reverse_words(unsigned char*, section_size_type);
2683
7019cd25
ILT
2684 // Scan the input relocations for --emit-relocs.
2685 void
ad0f2072 2686 emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
7019cd25
ILT
2687 const Read_relocs_data::Relocs_list::iterator&);
2688
2689 // Scan the input relocations for --emit-relocs, templatized on the
2690 // type of the relocation section.
2691 template<int sh_type>
2692 void
ad0f2072 2693 emit_relocs_scan_reltype(Symbol_table*, Layout*,
7019cd25
ILT
2694 const unsigned char* plocal_syms,
2695 const Read_relocs_data::Relocs_list::iterator&,
2696 Relocatable_relocs*);
2697
09ec0418
CC
2698 // Scan the input relocations for --incremental.
2699 void
2700 incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&);
2701
2702 // Scan the input relocations for --incremental, templatized on the
2703 // type of the relocation section.
2704 template<int sh_type>
2705 void
2706 incremental_relocs_scan_reltype(
2707 const Read_relocs_data::Relocs_list::iterator&);
2708
2709 void
2710 incremental_relocs_write(const Relocate_info<size, big_endian>*,
2711 unsigned int sh_type,
2712 const unsigned char* prelocs,
2713 size_t reloc_count,
2714 Output_section*,
2715 Address output_offset,
2716 Output_file*);
2717
2718 template<int sh_type>
2719 void
2720 incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*,
2721 const unsigned char* prelocs,
2722 size_t reloc_count,
2723 Output_section*,
2724 Address output_offset,
2725 Output_file*);
2726
364c7fa5
ILT
2727 // A type shared by split_stack_adjust_reltype and find_functions.
2728 typedef std::map<section_offset_type, section_size_type> Function_offsets;
2729
2730 // Check for -fsplit-stack routines calling non-split-stack routines.
2731 void
2732 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
2733 unsigned int sh_type, unsigned int shndx,
2734 const unsigned char* prelocs, size_t reloc_count,
2735 unsigned char* view, section_size_type view_size,
4d625b70
CC
2736 Reloc_symbol_changes** reloc_map,
2737 const Sized_target<size, big_endian>* target);
364c7fa5
ILT
2738
2739 template<int sh_type>
2740 void
2741 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
2742 unsigned int shndx, const unsigned char* prelocs,
2743 size_t reloc_count, unsigned char* view,
2744 section_size_type view_size,
4d625b70
CC
2745 Reloc_symbol_changes** reloc_map,
2746 const Sized_target<size, big_endian>* target);
364c7fa5
ILT
2747
2748 // Find all functions in a section.
2749 void
2750 find_functions(const unsigned char* pshdrs, unsigned int shndx,
2751 Function_offsets*);
2752
61ba1cf9
ILT
2753 // Write out the local symbols.
2754 void
b8e6aad9 2755 write_local_symbols(Output_file*,
7bf1f802 2756 const Stringpool_template<char>*,
d491d34e
ILT
2757 const Stringpool_template<char>*,
2758 Output_symtab_xindex*,
cdc29364
CC
2759 Output_symtab_xindex*,
2760 off_t);
61ba1cf9 2761
ef9beddf
ILT
2762 // Record a mapping from discarded section SHNDX to the corresponding
2763 // kept section.
2764 void
43193fe9
CC
2765 set_kept_comdat_section(unsigned int shndx, bool is_comdat,
2766 unsigned int symndx, uint64_t sh_size,
2767 Kept_section* kept_section)
ef9beddf 2768 {
43193fe9 2769 Kept_comdat_section kept(sh_size, kept_section, symndx, is_comdat);
1ef4d87f 2770 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
ef9beddf
ILT
2771 }
2772
1ef4d87f
ILT
2773 // Find the kept section corresponding to the discarded section
2774 // SHNDX. Return true if found.
2775 bool
43193fe9
CC
2776 get_kept_comdat_section(unsigned int shndx, bool* is_comdat,
2777 unsigned int *symndx, uint64_t* sh_size,
2778 Kept_section** kept_section) const
ef9beddf
ILT
2779 {
2780 typename Kept_comdat_section_table::const_iterator p =
2781 this->kept_comdat_sections_.find(shndx);
2782 if (p == this->kept_comdat_sections_.end())
1ef4d87f 2783 return false;
43193fe9
CC
2784 *is_comdat = p->second.is_comdat;
2785 *symndx = p->second.symndx;
2786 *sh_size = p->second.sh_size;
2787 *kept_section = p->second.kept_section;
1ef4d87f 2788 return true;
ef9beddf
ILT
2789 }
2790
aa98ff75
DK
2791 // Compute final local symbol value. R_SYM is the local symbol index.
2792 // LV_IN points to a local symbol value containing the input value.
2793 // LV_OUT points to a local symbol value storing the final output value,
2794 // which must not be a merged symbol value since before calling this
033bfb73
CC
2795 // method to avoid memory leak. RELOCATABLE indicates whether we are
2796 // linking a relocatable output. OUT_SECTIONS is an array of output
aa98ff75
DK
2797 // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB
2798 // points to a symbol table.
2799 //
2800 // The method returns a status code at return. If the return status is
2801 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2802 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2803 // *LV_OUT is not modified.
2804 inline Compute_final_local_value_status
2805 compute_final_local_value_internal(unsigned int r_sym,
2806 const Symbol_value<size>* lv_in,
2807 Symbol_value<size>* lv_out,
033bfb73 2808 bool relocatable,
aa98ff75
DK
2809 const Output_sections& out_sections,
2810 const std::vector<Address>& out_offsets,
2811 const Symbol_table* symtab);
2812
7223e9ca
ILT
2813 // The PLT offsets of local symbols.
2814 typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets;
07f397ab 2815
5995b570
CC
2816 // Saved information for sections whose layout was deferred.
2817 struct Deferred_layout
2818 {
2819 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2820 Deferred_layout(unsigned int shndx, const char* name,
bce5a025 2821 unsigned int sh_type,
5995b570
CC
2822 const unsigned char* pshdr,
2823 unsigned int reloc_shndx, unsigned int reloc_type)
bce5a025 2824 : name_(name), shndx_(shndx), reloc_shndx_(reloc_shndx),
5995b570
CC
2825 reloc_type_(reloc_type)
2826 {
bce5a025 2827 typename This::Shdr_write shdr(this->shdr_data_);
5995b570 2828 memcpy(this->shdr_data_, pshdr, shdr_size);
bce5a025 2829 shdr.put_sh_type(sh_type);
5995b570 2830 }
5995b570 2831 std::string name_;
bce5a025 2832 unsigned int shndx_;
5995b570
CC
2833 unsigned int reloc_shndx_;
2834 unsigned int reloc_type_;
2835 unsigned char shdr_data_[shdr_size];
2836 };
2837
645f8123
ILT
2838 // General access to the ELF file.
2839 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
9590bf25
CC
2840 // Type of ELF file (ET_REL or ET_EXEC). ET_EXEC files are allowed
2841 // as input files only for the --just-symbols option.
2842 int e_type_;
bae7f79e 2843 // Index of SHT_SYMTAB section.
645f8123 2844 unsigned int symtab_shndx_;
61ba1cf9
ILT
2845 // The number of local symbols.
2846 unsigned int local_symbol_count_;
2847 // The number of local symbols which go into the output file.
2848 unsigned int output_local_symbol_count_;
7bf1f802
ILT
2849 // The number of local symbols which go into the output file's dynamic
2850 // symbol table.
2851 unsigned int output_local_dynsym_count_;
14bfc3f5 2852 // The entries in the symbol table for the external symbols.
730cdc88 2853 Symbols symbols_;
92de84a6
ILT
2854 // Number of symbols defined in object file itself.
2855 size_t defined_count_;
cdc29364 2856 // File offset for local symbols (relative to start of symbol table).
75f65a3e 2857 off_t local_symbol_offset_;
cdc29364 2858 // File offset for local dynamic symbols (absolute).
7bf1f802 2859 off_t local_dynsym_offset_;
61ba1cf9 2860 // Values of local symbols.
c06b7b0b 2861 Local_values local_values_;
7223e9ca
ILT
2862 // PLT offsets for local symbols.
2863 Local_plt_offsets local_plt_offsets_;
ef9beddf
ILT
2864 // Table mapping discarded comdat sections to corresponding kept sections.
2865 Kept_comdat_section_table kept_comdat_sections_;
730cdc88
ILT
2866 // Whether this object has a GNU style .eh_frame section.
2867 bool has_eh_frame_;
c924eb67
CC
2868 // True if the layout of this object was deferred, waiting for plugin
2869 // replacement files.
2870 bool is_deferred_layout_;
5995b570
CC
2871 // The list of sections whose layout was deferred.
2872 std::vector<Deferred_layout> deferred_layout_;
f3a2388f
CC
2873 // The list of relocation sections whose layout was deferred.
2874 std::vector<Deferred_layout> deferred_layout_relocs_;
6b2353a5
CC
2875 // Pointer to the list of output views; valid only during do_relocate().
2876 const Views* output_views_;
bae7f79e
ILT
2877};
2878
54dc6425 2879// A class to manage the list of all objects.
a2fb1b05 2880
54dc6425
ILT
2881class Input_objects
2882{
2883 public:
2884 Input_objects()
fd9d194f 2885 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
54dc6425
ILT
2886 { }
2887
f6ce93d6
ILT
2888 // The type of the list of input relocateable objects.
2889 typedef std::vector<Relobj*> Relobj_list;
2890 typedef Relobj_list::const_iterator Relobj_iterator;
2891
2892 // The type of the list of input dynamic objects.
2893 typedef std::vector<Dynobj*> Dynobj_list;
2894 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425 2895
008db82e
ILT
2896 // Add an object to the list. Return true if all is well, or false
2897 // if this object should be ignored.
2898 bool
54dc6425
ILT
2899 add_object(Object*);
2900
92de84a6
ILT
2901 // Start processing an archive.
2902 void
2903 archive_start(Archive*);
2904
2905 // Stop processing an archive.
2906 void
2907 archive_stop(Archive*);
2908
e2827e5f
ILT
2909 // For each dynamic object, check whether we've seen all of its
2910 // explicit dependencies.
2911 void
2912 check_dynamic_dependencies() const;
2913
9a2d6984
ILT
2914 // Return whether an object was found in the system library
2915 // directory.
2916 bool
2917 found_in_system_library_directory(const Object*) const;
2918
92de84a6
ILT
2919 // Print symbol counts.
2920 void
2921 print_symbol_counts(const Symbol_table*) const;
2922
dde3f402
ILT
2923 // Print a cross reference table.
2924 void
2925 print_cref(const Symbol_table*, FILE*) const;
2926
f6ce93d6
ILT
2927 // Iterate over all regular objects.
2928
2929 Relobj_iterator
2930 relobj_begin() const
2931 { return this->relobj_list_.begin(); }
2932
2933 Relobj_iterator
2934 relobj_end() const
2935 { return this->relobj_list_.end(); }
2936
2937 // Iterate over all dynamic objects.
2938
2939 Dynobj_iterator
2940 dynobj_begin() const
2941 { return this->dynobj_list_.begin(); }
54dc6425 2942
f6ce93d6
ILT
2943 Dynobj_iterator
2944 dynobj_end() const
2945 { return this->dynobj_list_.end(); }
54dc6425
ILT
2946
2947 // Return whether we have seen any dynamic objects.
2948 bool
2949 any_dynamic() const
f6ce93d6 2950 { return !this->dynobj_list_.empty(); }
54dc6425 2951
fa17a3f4
ILT
2952 // Return the number of non dynamic objects.
2953 int
2954 number_of_relobjs() const
2955 { return this->relobj_list_.size(); }
2956
fe9a4c12
ILT
2957 // Return the number of input objects.
2958 int
2959 number_of_input_objects() const
2960 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
2961
54dc6425
ILT
2962 private:
2963 Input_objects(const Input_objects&);
2964 Input_objects& operator=(const Input_objects&);
2965
008db82e 2966 // The list of ordinary objects included in the link.
f6ce93d6 2967 Relobj_list relobj_list_;
008db82e 2968 // The list of dynamic objects included in the link.
f6ce93d6 2969 Dynobj_list dynobj_list_;
008db82e 2970 // SONAMEs that we have seen.
4bfacfd3 2971 Unordered_map<std::string, Object*> sonames_;
92de84a6
ILT
2972 // Manage cross-references if requested.
2973 Cref* cref_;
54dc6425 2974};
a2fb1b05 2975
92e059d8
ILT
2976// Some of the information we pass to the relocation routines. We
2977// group this together to avoid passing a dozen different arguments.
2978
2979template<int size, bool big_endian>
2980struct Relocate_info
2981{
92e059d8
ILT
2982 // Symbol table.
2983 const Symbol_table* symtab;
2984 // Layout.
2985 const Layout* layout;
2986 // Object being relocated.
6fa2a40b 2987 Sized_relobj_file<size, big_endian>* object;
92e059d8
ILT
2988 // Section index of relocation section.
2989 unsigned int reloc_shndx;
82bb573a
ILT
2990 // Section header of relocation section.
2991 const unsigned char* reloc_shdr;
91a65d2f
AM
2992 // Info about how relocs should be handled
2993 Relocatable_relocs* rr;
92e059d8
ILT
2994 // Section index of section being relocated.
2995 unsigned int data_shndx;
82bb573a
ILT
2996 // Section header of data section.
2997 const unsigned char* data_shdr;
92e059d8
ILT
2998
2999 // Return a string showing the location of a relocation. This is
3000 // only used for error messages.
3001 std::string
3002 location(size_t relnum, off_t reloffset) const;
3003};
3004
5ac169d4
DK
3005// This is used to represent a section in an object and is used as the
3006// key type for various section maps.
efc6fa12 3007typedef std::pair<Relobj*, unsigned int> Section_id;
5ac169d4
DK
3008
3009// This is similar to Section_id but is used when the section
3010// pointers are const.
efc6fa12 3011typedef std::pair<const Relobj*, unsigned int> Const_section_id;
5ac169d4
DK
3012
3013// The hash value is based on the address of an object in memory during
3014// linking. It is okay to use this for looking up sections but never use
3015// this in an unordered container that we want to traverse in a repeatable
3016// manner.
3017
3018struct Section_id_hash
3019{
3020 size_t operator()(const Section_id& loc) const
3021 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
3022};
3023
3024struct Const_section_id_hash
3025{
3026 size_t operator()(const Const_section_id& loc) const
3027 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
3028};
3029
f6060a4d
ILT
3030// Return whether INPUT_FILE contains an ELF object start at file
3031// offset OFFSET. This sets *START to point to a view of the start of
3032// the file. It sets *READ_SIZE to the number of bytes in the view.
3033
3034extern bool
3035is_elf_object(Input_file* input_file, off_t offset,
ca09d69a 3036 const unsigned char** start, int* read_size);
f6060a4d 3037
bae7f79e 3038// Return an Object appropriate for the input file. P is BYTES long,
15f8229b
ILT
3039// and holds the ELF header. If PUNCONFIGURED is not NULL, then if
3040// this sees an object the linker is not configured to support, it
3041// sets *PUNCONFIGURED to true and returns NULL without giving an
3042// error message.
bae7f79e 3043
61ba1cf9
ILT
3044extern Object*
3045make_elf_object(const std::string& name, Input_file*,
3046 off_t offset, const unsigned char* p,
15f8229b 3047 section_offset_type bytes, bool* punconfigured);
bae7f79e
ILT
3048
3049} // end namespace gold
3050
3051#endif // !defined(GOLD_OBJECT_H)
This page took 0.662095 seconds and 4 git commands to generate.