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