PR binutils/10924
[deliverable/binutils-gdb.git] / gold / object.h
CommitLineData
bae7f79e
ILT
1// object.h -- support for an object file for linking in gold -*- C++ -*-
2
6d03d481 3// Copyright 2006, 2007, 2008, 2009 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"
bae7f79e
ILT
33
34namespace gold
35{
36
92e059d8 37class General_options;
17a1d0a9 38class Task;
92de84a6
ILT
39class Cref;
40class Archive;
a2fb1b05 41class Layout;
61ba1cf9
ILT
42class Output_section;
43class Output_file;
d491d34e 44class Output_symtab_xindex;
89fc3421 45class Pluginobj;
f6ce93d6 46class Dynobj;
4625f782 47class Object_merge_map;
6a74a719 48class Relocatable_relocs;
6d03d481 49class Symbols_data;
a2fb1b05 50
b8e6aad9
ILT
51template<typename Stringpool_char>
52class Stringpool_template;
53
bae7f79e
ILT
54// Data to pass from read_symbols() to add_symbols().
55
56struct Read_symbols_data
57{
12e14209
ILT
58 // Section headers.
59 File_view* section_headers;
60 // Section names.
61 File_view* section_names;
62 // Size of section name data in bytes.
8383303e 63 section_size_type section_names_size;
bae7f79e
ILT
64 // Symbol data.
65 File_view* symbols;
66 // Size of symbol data in bytes.
8383303e 67 section_size_type symbols_size;
730cdc88
ILT
68 // Offset of external symbols within symbol data. This structure
69 // sometimes contains only external symbols, in which case this will
70 // be zero. Sometimes it contains all symbols.
8383303e 71 section_offset_type external_symbols_offset;
bae7f79e
ILT
72 // Symbol names.
73 File_view* symbol_names;
74 // Size of symbol name data in bytes.
8383303e 75 section_size_type symbol_names_size;
dbe717ef
ILT
76
77 // Version information. This is only used on dynamic objects.
78 // Version symbol data (from SHT_GNU_versym section).
79 File_view* versym;
8383303e 80 section_size_type versym_size;
dbe717ef
ILT
81 // Version definition data (from SHT_GNU_verdef section).
82 File_view* verdef;
8383303e 83 section_size_type verdef_size;
dbe717ef
ILT
84 unsigned int verdef_info;
85 // Needed version data (from SHT_GNU_verneed section).
86 File_view* verneed;
8383303e 87 section_size_type verneed_size;
dbe717ef 88 unsigned int verneed_info;
bae7f79e
ILT
89};
90
f7e2ee48
ILT
91// Information used to print error messages.
92
93struct Symbol_location_info
94{
95 std::string source_file;
96 std::string enclosing_symbol_name;
97 int line_number;
98};
99
92e059d8
ILT
100// Data about a single relocation section. This is read in
101// read_relocs and processed in scan_relocs.
102
103struct Section_relocs
104{
105 // Index of reloc section.
106 unsigned int reloc_shndx;
107 // Index of section that relocs apply to.
108 unsigned int data_shndx;
109 // Contents of reloc section.
110 File_view* contents;
111 // Reloc section type.
112 unsigned int sh_type;
113 // Number of reloc entries.
114 size_t reloc_count;
730cdc88
ILT
115 // Output section.
116 Output_section* output_section;
117 // Whether this section has special handling for offsets.
118 bool needs_special_offset_handling;
7019cd25
ILT
119 // Whether the data section is allocated (has the SHF_ALLOC flag set).
120 bool is_data_section_allocated;
92e059d8
ILT
121};
122
123// Relocations in an object file. This is read in read_relocs and
124// processed in scan_relocs.
125
126struct Read_relocs_data
127{
128 typedef std::vector<Section_relocs> Relocs_list;
129 // The relocations.
130 Relocs_list relocs;
131 // The local symbols.
132 File_view* local_symbols;
133};
134
d491d34e
ILT
135// The Xindex class manages section indexes for objects with more than
136// 0xff00 sections.
137
138class Xindex
139{
140 public:
141 Xindex(int large_shndx_offset)
142 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
143 { }
144
145 // Initialize the symtab_xindex_ array, given the object and the
146 // section index of the symbol table to use.
147 template<int size, bool big_endian>
148 void
149 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
150
151 // Read in the symtab_xindex_ array, given its section index.
152 // PSHDRS may optionally point to the section headers.
153 template<int size, bool big_endian>
154 void
155 read_symtab_xindex(Object*, unsigned int xindex_shndx,
156 const unsigned char* pshdrs);
157
158 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
159 // real section index.
160 unsigned int
161 sym_xindex_to_shndx(Object* object, unsigned int symndx);
162
163 private:
164 // The type of the array giving the real section index for symbols
165 // whose st_shndx field holds SHN_XINDEX.
166 typedef std::vector<unsigned int> Symtab_xindex;
167
168 // Adjust a section index if necessary. This should only be called
169 // for ordinary section indexes.
170 unsigned int
171 adjust_shndx(unsigned int shndx)
172 {
173 if (shndx >= elfcpp::SHN_LORESERVE)
174 shndx += this->large_shndx_offset_;
175 return shndx;
176 }
177
178 // Adjust to apply to large section indexes.
179 int large_shndx_offset_;
180 // The data from the SHT_SYMTAB_SHNDX section.
181 Symtab_xindex symtab_xindex_;
182};
183
f6ce93d6
ILT
184// Object is an abstract base class which represents either a 32-bit
185// or a 64-bit input object. This can be a regular object file
186// (ET_REL) or a shared object (ET_DYN).
bae7f79e
ILT
187
188class Object
189{
190 public:
191 // NAME is the name of the object as we would report it to the user
192 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
193 // used to read the file. OFFSET is the offset within the input
194 // file--0 for a .o or .so file, something else for a .a file.
91d6fa6a
NC
195 Object(const std::string& oname, Input_file* oinput_file, bool ois_dynamic,
196 off_t ooffset = 0)
197 : name_(oname), input_file_(oinput_file), offset_(ooffset), shnum_(-1U),
198 is_dynamic_(ois_dynamic), is_needed_(false), uses_split_stack_(false),
594c8e5e 199 has_no_split_stack_(false), no_export_(false), xindex_(NULL)
91d6fa6a 200 { oinput_file->file().add_object(); }
bae7f79e
ILT
201
202 virtual ~Object()
cb295612 203 { this->input_file_->file().remove_object(); }
bae7f79e 204
14bfc3f5 205 // Return the name of the object as we would report it to the tuser.
bae7f79e
ILT
206 const std::string&
207 name() const
208 { return this->name_; }
209
cec9d2f3
ILT
210 // Get the offset into the file.
211 off_t
212 offset() const
213 { return this->offset_; }
214
14bfc3f5
ILT
215 // Return whether this is a dynamic object.
216 bool
217 is_dynamic() const
218 { return this->is_dynamic_; }
219
594c8e5e
ILT
220 // Return whether this object is needed--true if it is a dynamic
221 // object which defines some symbol referenced by a regular object.
222 // We keep the flag here rather than in Dynobj for convenience when
223 // setting it.
224 bool
225 is_needed() const
226 { return this->is_needed_; }
227
228 // Record that this object is needed.
229 void
230 set_is_needed()
231 { this->is_needed_ = true; }
232
364c7fa5
ILT
233 // Return whether this object was compiled with -fsplit-stack.
234 bool
235 uses_split_stack() const
236 { return this->uses_split_stack_; }
237
238 // Return whether this object contains any functions compiled with
239 // the no_split_stack attribute.
240 bool
241 has_no_split_stack() const
242 { return this->has_no_split_stack_; }
243
89fc3421
CC
244 // Returns NULL for Objects that are not plugin objects. This method
245 // is overridden in the Pluginobj class.
246 Pluginobj*
247 pluginobj()
248 { return this->do_pluginobj(); }
249
15f8229b
ILT
250 // Get the file. We pass on const-ness.
251 Input_file*
252 input_file()
253 { return this->input_file_; }
254
255 const Input_file*
256 input_file() const
257 { return this->input_file_; }
258
a2fb1b05
ILT
259 // Lock the underlying file.
260 void
17a1d0a9
ILT
261 lock(const Task* t)
262 { this->input_file()->file().lock(t); }
a2fb1b05
ILT
263
264 // Unlock the underlying file.
265 void
17a1d0a9
ILT
266 unlock(const Task* t)
267 { this->input_file()->file().unlock(t); }
a2fb1b05
ILT
268
269 // Return whether the underlying file is locked.
270 bool
271 is_locked() const
7004837e 272 { return this->input_file()->file().is_locked(); }
a2fb1b05 273
17a1d0a9
ILT
274 // Return the token, so that the task can be queued.
275 Task_token*
276 token()
277 { return this->input_file()->file().token(); }
278
279 // Release the underlying file.
280 void
281 release()
282 { this->input_file_->file().release(); }
283
88dd47ac
ILT
284 // Return whether we should just read symbols from this file.
285 bool
286 just_symbols() const
287 { return this->input_file()->just_symbols(); }
288
5a6f7e2d
ILT
289 // Get the number of sections.
290 unsigned int
291 shnum() const
292 { return this->shnum_; }
a2fb1b05 293
f6ce93d6 294 // Return a view of the contents of a section. Set *PLEN to the
9eb9fa57 295 // size. CACHE is a hint as in File_read::get_view.
f6ce93d6 296 const unsigned char*
8383303e 297 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
f6ce93d6 298
d491d34e
ILT
299 // Adjust a symbol's section index as needed. SYMNDX is the index
300 // of the symbol and SHNDX is the symbol's section from
301 // get_st_shndx. This returns the section index. It sets
302 // *IS_ORDINARY to indicate whether this is a normal section index,
303 // rather than a special code between SHN_LORESERVE and
304 // SHN_HIRESERVE.
305 unsigned int
306 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
307 {
308 if (shndx < elfcpp::SHN_LORESERVE)
309 *is_ordinary = true;
310 else if (shndx == elfcpp::SHN_XINDEX)
311 {
312 if (this->xindex_ == NULL)
313 this->xindex_ = this->do_initialize_xindex();
314 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
315 *is_ordinary = true;
316 }
317 else
318 *is_ordinary = false;
319 return shndx;
320 }
321
a445fddf
ILT
322 // Return the size of a section given a section index.
323 uint64_t
324 section_size(unsigned int shndx)
325 { return this->do_section_size(shndx); }
326
327 // Return the name of a section given a section index.
f6ce93d6 328 std::string
a3ad94ed
ILT
329 section_name(unsigned int shndx)
330 { return this->do_section_name(shndx); }
331
332 // Return the section flags given a section index.
333 uint64_t
334 section_flags(unsigned int shndx)
335 { return this->do_section_flags(shndx); }
f6ce93d6 336
ef15dade
ST
337 // Return the section entsize given a section index.
338 uint64_t
339 section_entsize(unsigned int shndx)
340 { return this->do_section_entsize(shndx); }
341
88dd47ac
ILT
342 // Return the section address given a section index.
343 uint64_t
344 section_address(unsigned int shndx)
345 { return this->do_section_address(shndx); }
346
730cdc88
ILT
347 // Return the section type given a section index.
348 unsigned int
349 section_type(unsigned int shndx)
350 { return this->do_section_type(shndx); }
351
f7e2ee48
ILT
352 // Return the section link field given a section index.
353 unsigned int
354 section_link(unsigned int shndx)
355 { return this->do_section_link(shndx); }
356
4c50553d
ILT
357 // Return the section info field given a section index.
358 unsigned int
359 section_info(unsigned int shndx)
360 { return this->do_section_info(shndx); }
361
a445fddf
ILT
362 // Return the required section alignment given a section index.
363 uint64_t
364 section_addralign(unsigned int shndx)
365 { return this->do_section_addralign(shndx); }
366
5a6f7e2d
ILT
367 // Read the symbol information.
368 void
369 read_symbols(Read_symbols_data* sd)
370 { return this->do_read_symbols(sd); }
371
372 // Pass sections which should be included in the link to the Layout
373 // object, and record where the sections go in the output file.
374 void
91d6fa6a
NC
375 layout(Symbol_table* symtab, Layout* olayout, Read_symbols_data* sd)
376 { this->do_layout(symtab, olayout, sd); }
5a6f7e2d
ILT
377
378 // Add symbol information to the global symbol table.
379 void
91d6fa6a
NC
380 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *olayout)
381 { this->do_add_symbols(symtab, sd, olayout); }
5a6f7e2d 382
645f8123
ILT
383 // Functions and types for the elfcpp::Elf_file interface. This
384 // permit us to use Object as the File template parameter for
385 // elfcpp::Elf_file.
386
387 // The View class is returned by view. It must support a single
388 // method, data(). This is trivial, because get_view does what we
389 // need.
390 class View
391 {
392 public:
393 View(const unsigned char* p)
394 : p_(p)
395 { }
396
397 const unsigned char*
398 data() const
399 { return this->p_; }
400
401 private:
402 const unsigned char* p_;
403 };
404
405 // Return a View.
406 View
8383303e 407 view(off_t file_offset, section_size_type data_size)
39d0cb0e 408 { return View(this->get_view(file_offset, data_size, true, true)); }
645f8123
ILT
409
410 // Report an error.
411 void
75f2446e 412 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
645f8123
ILT
413
414 // A location in the file.
415 struct Location
416 {
417 off_t file_offset;
418 off_t data_size;
419
8383303e 420 Location(off_t fo, section_size_type ds)
645f8123
ILT
421 : file_offset(fo), data_size(ds)
422 { }
423 };
424
425 // Get a View given a Location.
426 View view(Location loc)
39d0cb0e 427 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
645f8123 428
cb295612
ILT
429 // Get a view into the underlying file.
430 const unsigned char*
39d0cb0e 431 get_view(off_t start, section_size_type size, bool aligned, bool cache)
cb295612 432 {
39d0cb0e
ILT
433 return this->input_file()->file().get_view(this->offset_, start, size,
434 aligned, cache);
cb295612
ILT
435 }
436
437 // Get a lasting view into the underlying file.
438 File_view*
39d0cb0e
ILT
439 get_lasting_view(off_t start, section_size_type size, bool aligned,
440 bool cache)
cb295612 441 {
39d0cb0e
ILT
442 return this->input_file()->file().get_lasting_view(this->offset_, start,
443 size, aligned, cache);
cb295612
ILT
444 }
445
446 // Read data from the underlying file.
447 void
2a00e4fb 448 read(off_t start, section_size_type size, void* p)
cb295612
ILT
449 { this->input_file()->file().read(start + this->offset_, size, p); }
450
451 // Read multiple data from the underlying file.
452 void
453 read_multiple(const File_read::Read_multiple& rm)
454 { this->input_file()->file().read_multiple(this->offset_, rm); }
455
456 // Stop caching views in the underlying file.
457 void
458 clear_view_cache_marks()
459 { this->input_file()->file().clear_view_cache_marks(); }
460
92de84a6
ILT
461 // Get the number of global symbols defined by this object, and the
462 // number of the symbols whose final definition came from this
463 // object.
464 void
465 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
466 size_t* used) const
467 { this->do_get_global_symbol_counts(symtab, defined, used); }
468
fd9d194f
ILT
469 // Return whether this object was found in a system directory.
470 bool
471 is_in_system_directory() const
472 { return this->input_file()->is_in_system_directory(); }
473
15f8229b
ILT
474 // Return whether we found this object by searching a directory.
475 bool
476 searched_for() const
477 { return this->input_file()->will_search_for(); }
478
65514900
CC
479 bool
480 no_export() const
481 { return this->no_export_; }
482
483 void
484 set_no_export(bool value)
485 { this->no_export_ = value; }
486
f6ce93d6 487 protected:
89fc3421
CC
488 // Returns NULL for Objects that are not plugin objects. This method
489 // is overridden in the Pluginobj class.
490 virtual Pluginobj*
491 do_pluginobj()
492 { return NULL; }
493
f6ce93d6
ILT
494 // Read the symbols--implemented by child class.
495 virtual void
496 do_read_symbols(Read_symbols_data*) = 0;
497
498 // Lay out sections--implemented by child class.
499 virtual void
7e1edb90 500 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
f6ce93d6
ILT
501
502 // Add symbol information to the global symbol table--implemented by
503 // child class.
504 virtual void
f488e4b0 505 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
f6ce93d6 506
645f8123
ILT
507 // Return the location of the contents of a section. Implemented by
508 // child class.
509 virtual Location
a3ad94ed 510 do_section_contents(unsigned int shndx) = 0;
f6ce93d6 511
a445fddf
ILT
512 // Get the size of a section--implemented by child class.
513 virtual uint64_t
514 do_section_size(unsigned int shndx) = 0;
515
f6ce93d6
ILT
516 // Get the name of a section--implemented by child class.
517 virtual std::string
a3ad94ed
ILT
518 do_section_name(unsigned int shndx) = 0;
519
520 // Get section flags--implemented by child class.
521 virtual uint64_t
522 do_section_flags(unsigned int shndx) = 0;
f6ce93d6 523
ef15dade
ST
524 // Get section entsize--implemented by child class.
525 virtual uint64_t
526 do_section_entsize(unsigned int shndx) = 0;
527
88dd47ac
ILT
528 // Get section address--implemented by child class.
529 virtual uint64_t
530 do_section_address(unsigned int shndx) = 0;
531
730cdc88
ILT
532 // Get section type--implemented by child class.
533 virtual unsigned int
534 do_section_type(unsigned int shndx) = 0;
535
f7e2ee48
ILT
536 // Get section link field--implemented by child class.
537 virtual unsigned int
538 do_section_link(unsigned int shndx) = 0;
539
4c50553d
ILT
540 // Get section info field--implemented by child class.
541 virtual unsigned int
542 do_section_info(unsigned int shndx) = 0;
543
a445fddf
ILT
544 // Get section alignment--implemented by child class.
545 virtual uint64_t
546 do_section_addralign(unsigned int shndx) = 0;
547
d491d34e
ILT
548 // Return the Xindex structure to use.
549 virtual Xindex*
550 do_initialize_xindex() = 0;
551
92de84a6
ILT
552 // Implement get_global_symbol_counts--implemented by child class.
553 virtual void
554 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
555
dbe717ef
ILT
556 // Set the number of sections.
557 void
91d6fa6a
NC
558 set_shnum(int sec_shnum)
559 { this->shnum_ = sec_shnum; }
dbe717ef
ILT
560
561 // Functions used by both Sized_relobj and Sized_dynobj.
562
563 // Read the section data into a Read_symbols_data object.
564 template<int size, bool big_endian>
565 void
566 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
567 Read_symbols_data*);
568
d491d34e
ILT
569 // Let the child class initialize the xindex object directly.
570 void
571 set_xindex(Xindex* xindex)
572 {
573 gold_assert(this->xindex_ == NULL);
574 this->xindex_ = xindex;
575 }
576
dbe717ef
ILT
577 // If NAME is the name of a special .gnu.warning section, arrange
578 // for the warning to be issued. SHNDX is the section index.
579 // Return whether it is a warning section.
580 bool
581 handle_gnu_warning_section(const char* name, unsigned int shndx,
582 Symbol_table*);
f6ce93d6 583
364c7fa5
ILT
584 // If NAME is the name of the special section which indicates that
585 // this object was compiled with -fstack-split, mark it accordingly,
586 // and return true. Otherwise return false.
587 bool
588 handle_split_stack_section(const char* name);
589
f6ce93d6
ILT
590 private:
591 // This class may not be copied.
592 Object(const Object&);
593 Object& operator=(const Object&);
594
595 // Name of object as printed to user.
596 std::string name_;
597 // For reading the file.
598 Input_file* input_file_;
599 // Offset within the file--0 for an object file, non-0 for an
600 // archive.
601 off_t offset_;
dbe717ef
ILT
602 // Number of input sections.
603 unsigned int shnum_;
f6ce93d6 604 // Whether this is a dynamic object.
594c8e5e
ILT
605 bool is_dynamic_ : 1;
606 // Whether this object is needed. This is only set for dynamic
607 // objects, and means that the object defined a symbol which was
608 // used by a reference from a regular object.
609 bool is_needed_ : 1;
364c7fa5 610 // Whether this object was compiled with -fsplit-stack.
594c8e5e 611 bool uses_split_stack_ : 1;
364c7fa5
ILT
612 // Whether this object contains any functions compiled with the
613 // no_split_stack attribute.
594c8e5e 614 bool has_no_split_stack_ : 1;
65514900
CC
615 // True if exclude this object from automatic symbol export.
616 // This is used only for archive objects.
594c8e5e
ILT
617 bool no_export_ : 1;
618 // Many sections for objects with more than SHN_LORESERVE sections.
619 Xindex* xindex_;
f6ce93d6
ILT
620};
621
f6ce93d6 622// A regular object (ET_REL). This is an abstract base class itself.
b8e6aad9 623// The implementation is the template class Sized_relobj.
f6ce93d6
ILT
624
625class Relobj : public Object
626{
627 public:
91d6fa6a
NC
628 Relobj(const std::string& rname, Input_file* rinput_file, off_t roffset = 0)
629 : Object(rname, rinput_file, false, roffset),
ef9beddf 630 output_sections_(),
6a74a719 631 map_to_relocatable_relocs_(NULL),
4625f782 632 object_merge_map_(NULL),
ef15dade
ST
633 relocs_must_follow_section_writes_(false),
634 sd_(NULL)
f6ce93d6
ILT
635 { }
636
6d03d481
ST
637 // During garbage collection, the Read_symbols_data pass for
638 // each object is stored as layout needs to be done after
639 // reloc processing.
640 Symbols_data*
641 get_symbols_data()
642 { return this->sd_; }
643
644 // Decides which section names have to be included in the worklist
645 // as roots.
646 bool
647 is_section_name_included(const char *name);
648
649 void
650 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
651 unsigned int section_header_size);
652
653 void
654 set_symbols_data(Symbols_data* sd)
655 { this->sd_ = sd; }
656
657 // During garbage collection, the Read_relocs pass for all objects
658 // is done before scanning the relocs. In that case, this->rd_ is
659 // used to store the information from Read_relocs for each object.
660 // This data is also used to compute the list of relevant sections.
661 Read_relocs_data*
662 get_relocs_data()
663 { return this->rd_; }
664
665 void
666 set_relocs_data(Read_relocs_data* rd)
667 { this->rd_ = rd; }
668
669 virtual bool
670 is_output_section_offset_invalid(unsigned int shndx) const = 0;
671
92e059d8 672 // Read the relocs.
a2fb1b05 673 void
92e059d8
ILT
674 read_relocs(Read_relocs_data* rd)
675 { return this->do_read_relocs(rd); }
676
6d03d481
ST
677 // Process the relocs, during garbage collection only.
678 void
91d6fa6a
NC
679 gc_process_relocs(Symbol_table* symtab, Layout* olayout, Read_relocs_data* rd)
680 { return this->do_gc_process_relocs(symtab, olayout, rd); }
6d03d481 681
92e059d8
ILT
682 // Scan the relocs and adjust the symbol table.
683 void
91d6fa6a
NC
684 scan_relocs(Symbol_table* symtab, Layout* olayout, Read_relocs_data* rd)
685 { return this->do_scan_relocs(symtab, olayout, rd); }
a2fb1b05 686
6d013333
ILT
687 // The number of local symbols in the input symbol table.
688 virtual unsigned int
689 local_symbol_count() const
690 { return this->do_local_symbol_count(); }
691
7bf1f802
ILT
692 // Initial local symbol processing: count the number of local symbols
693 // in the output symbol table and dynamic symbol table; add local symbol
694 // names to *POOL and *DYNPOOL.
695 void
696 count_local_symbols(Stringpool_template<char>* pool,
697 Stringpool_template<char>* dynpool)
698 { return this->do_count_local_symbols(pool, dynpool); }
699
700 // Set the values of the local symbols, set the output symbol table
701 // indexes for the local variables, and set the offset where local
702 // symbol information will be stored. Returns the new local symbol index.
703 unsigned int
ef15dade
ST
704 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
705 { return this->do_finalize_local_symbols(index, off, symtab); }
7bf1f802
ILT
706
707 // Set the output dynamic symbol table indexes for the local variables.
c06b7b0b 708 unsigned int
7bf1f802
ILT
709 set_local_dynsym_indexes(unsigned int index)
710 { return this->do_set_local_dynsym_indexes(index); }
711
712 // Set the offset where local dynamic symbol information will be stored.
713 unsigned int
714 set_local_dynsym_offset(off_t off)
715 { return this->do_set_local_dynsym_offset(off); }
75f65a3e 716
61ba1cf9
ILT
717 // Relocate the input sections and write out the local symbols.
718 void
91d6fa6a
NC
719 relocate(const Symbol_table* symtab, const Layout* olayout, Output_file* of)
720 { return this->do_relocate(symtab, olayout, of); }
61ba1cf9 721
a783673b
ILT
722 // Return whether an input section is being included in the link.
723 bool
5a6f7e2d 724 is_section_included(unsigned int shndx) const
a783673b 725 {
ef9beddf
ILT
726 gold_assert(shndx < this->output_sections_.size());
727 return this->output_sections_[shndx] != NULL;
a783673b
ILT
728 }
729
ef9beddf
ILT
730 // Given a section index, return the corresponding Output_section.
731 // The return value will be NULL if the section is not included in
732 // the link.
733 Output_section*
734 output_section(unsigned int shndx) const
730cdc88 735 {
ef9beddf
ILT
736 gold_assert(shndx < this->output_sections_.size());
737 return this->output_sections_[shndx];
730cdc88
ILT
738 }
739
ef9beddf
ILT
740 // Given a section index, return the offset in the Output_section.
741 // The return value will be -1U if the section is specially mapped,
742 // such as a merge section.
743 uint64_t
744 output_section_offset(unsigned int shndx) const
745 { return this->do_output_section_offset(shndx); }
a783673b 746
ead1e424 747 // Set the offset of an input section within its output section.
5995b570 748 void
ef9beddf
ILT
749 set_section_offset(unsigned int shndx, uint64_t off)
750 { this->do_set_section_offset(shndx, off); }
e94cf127 751
730cdc88
ILT
752 // Return true if we need to wait for output sections to be written
753 // before we can apply relocations. This is true if the object has
754 // any relocations for sections which require special handling, such
755 // as the exception frame section.
756 bool
17a1d0a9 757 relocs_must_follow_section_writes() const
730cdc88
ILT
758 { return this->relocs_must_follow_section_writes_; }
759
4625f782
ILT
760 // Return the object merge map.
761 Object_merge_map*
762 merge_map() const
763 { return this->object_merge_map_; }
764
765 // Set the object merge map.
766 void
767 set_merge_map(Object_merge_map* object_merge_map)
768 {
769 gold_assert(this->object_merge_map_ == NULL);
770 this->object_merge_map_ = object_merge_map;
771 }
772
6a74a719
ILT
773 // Record the relocatable reloc info for an input reloc section.
774 void
775 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
776 {
777 gold_assert(reloc_shndx < this->shnum());
778 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
779 }
780
781 // Get the relocatable reloc info for an input reloc section.
782 Relocatable_relocs*
783 relocatable_relocs(unsigned int reloc_shndx)
784 {
785 gold_assert(reloc_shndx < this->shnum());
786 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
787 }
788
5995b570
CC
789 // Layout sections whose layout was deferred while waiting for
790 // input files from a plugin.
791 void
91d6fa6a
NC
792 layout_deferred_sections(Layout* olayout)
793 { this->do_layout_deferred_sections(olayout); }
5995b570 794
a783673b 795 protected:
ef9beddf
ILT
796 // The output section to be used for each input section, indexed by
797 // the input section number. The output section is NULL if the
798 // input section is to be discarded.
799 typedef std::vector<Output_section*> Output_sections;
e94cf127 800
92e059d8
ILT
801 // Read the relocs--implemented by child class.
802 virtual void
803 do_read_relocs(Read_relocs_data*) = 0;
804
6d03d481
ST
805 // Process the relocs--implemented by child class.
806 virtual void
ad0f2072 807 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
6d03d481 808
92e059d8
ILT
809 // Scan the relocs--implemented by child class.
810 virtual void
ad0f2072 811 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
92e059d8 812
6d013333
ILT
813 // Return the number of local symbols--implemented by child class.
814 virtual unsigned int
815 do_local_symbol_count() const = 0;
816
7bf1f802
ILT
817 // Count local symbols--implemented by child class.
818 virtual void
819 do_count_local_symbols(Stringpool_template<char>*,
6a74a719 820 Stringpool_template<char>*) = 0;
75f65a3e 821
6a74a719
ILT
822 // Finalize the local symbols. Set the output symbol table indexes
823 // for the local variables, and set the offset where local symbol
824 // information will be stored.
7bf1f802 825 virtual unsigned int
ef15dade 826 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
7bf1f802
ILT
827
828 // Set the output dynamic symbol table indexes for the local variables.
829 virtual unsigned int
830 do_set_local_dynsym_indexes(unsigned int) = 0;
831
832 // Set the offset where local dynamic symbol information will be stored.
833 virtual unsigned int
834 do_set_local_dynsym_offset(off_t) = 0;
835
61ba1cf9
ILT
836 // Relocate the input sections and write out the local
837 // symbols--implemented by child class.
838 virtual void
ad0f2072 839 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
92e059d8 840
ef9beddf
ILT
841 // Get the offset of a section--implemented by child class.
842 virtual uint64_t
843 do_output_section_offset(unsigned int shndx) const = 0;
b8e6aad9 844
ef9beddf
ILT
845 // Set the offset of a section--implemented by child class.
846 virtual void
847 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
e94cf127 848
5995b570
CC
849 // Layout sections whose layout was deferred while waiting for
850 // input files from a plugin--implemented by child class.
851 virtual void
852 do_layout_deferred_sections(Layout*) = 0;
853
ef9beddf
ILT
854 // Return the vector mapping input sections to output sections.
855 Output_sections&
856 output_sections()
857 { return this->output_sections_; }
e94cf127 858
ef9beddf
ILT
859 const Output_sections&
860 output_sections() const
861 { return this->output_sections_; }
e94cf127 862
6a74a719
ILT
863 // Set the size of the relocatable relocs array.
864 void
865 size_relocatable_relocs()
866 {
867 this->map_to_relocatable_relocs_ =
868 new std::vector<Relocatable_relocs*>(this->shnum());
869 }
870
730cdc88
ILT
871 // Record that we must wait for the output sections to be written
872 // before applying relocations.
873 void
874 set_relocs_must_follow_section_writes()
875 { this->relocs_must_follow_section_writes_ = true; }
876
bae7f79e 877 private:
a2fb1b05 878 // Mapping from input sections to output section.
ef9beddf 879 Output_sections output_sections_;
6a74a719
ILT
880 // Mapping from input section index to the information recorded for
881 // the relocations. This is only used for a relocatable link.
882 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
4625f782
ILT
883 // Mappings for merge sections. This is managed by the code in the
884 // Merge_map class.
885 Object_merge_map* object_merge_map_;
730cdc88
ILT
886 // Whether we need to wait for output sections to be written before
887 // we can apply relocations.
888 bool relocs_must_follow_section_writes_;
6d03d481
ST
889 // Used to store the relocs data computed by the Read_relocs pass.
890 // Used during garbage collection of unused sections.
891 Read_relocs_data* rd_;
892 // Used to store the symbols data computed by the Read_symbols pass.
893 // Again used during garbage collection when laying out referenced
894 // sections.
895 gold::Symbols_data *sd_;
bae7f79e
ILT
896};
897
a9a60db6
ILT
898// This class is used to handle relocations against a section symbol
899// in an SHF_MERGE section. For such a symbol, we need to know the
900// addend of the relocation before we can determine the final value.
901// The addend gives us the location in the input section, and we can
902// determine how it is mapped to the output section. For a
903// non-section symbol, we apply the addend to the final value of the
904// symbol; that is done in finalize_local_symbols, and does not use
905// this class.
906
907template<int size>
908class Merged_symbol_value
909{
910 public:
911 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
912
913 // We use a hash table to map offsets in the input section to output
914 // addresses.
915 typedef Unordered_map<section_offset_type, Value> Output_addresses;
916
917 Merged_symbol_value(Value input_value, Value output_start_address)
918 : input_value_(input_value), output_start_address_(output_start_address),
919 output_addresses_()
920 { }
921
922 // Initialize the hash table.
923 void
924 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
925
926 // Release the hash table to save space.
927 void
928 free_input_to_output_map()
929 { this->output_addresses_.clear(); }
930
931 // Get the output value corresponding to an addend. The object and
932 // input section index are passed in because the caller will have
933 // them; otherwise we could store them here.
934 Value
935 value(const Relobj* object, unsigned int input_shndx, Value addend) const
936 {
7f649c59
ILT
937 // This is a relocation against a section symbol. ADDEND is the
938 // offset in the section. The result should be the start of some
939 // merge area. If the object file wants something else, it should
940 // use a regular symbol rather than a section symbol.
941 // Unfortunately, PR 6658 shows a case in which the object file
942 // refers to the section symbol, but uses a negative ADDEND to
943 // compensate for a PC relative reloc. We can't handle the
944 // general case. However, we can handle the special case of a
945 // negative addend, by assuming that it refers to the start of the
946 // section. Of course, that means that we have to guess when
947 // ADDEND is negative. It is normal to see a 32-bit value here
948 // even when the template parameter size is 64, as 64-bit object
949 // file formats have 32-bit relocations. We know this is a merge
950 // section, so we know it has to fit into memory. So we assume
951 // that we won't see a value larger than a large 32-bit unsigned
952 // value. This will break objects with very very large merge
953 // sections; they probably break in other ways anyhow.
954 Value input_offset = this->input_value_;
955 if (addend < 0xffffff00)
956 {
957 input_offset += addend;
958 addend = 0;
959 }
a9a60db6
ILT
960 typename Output_addresses::const_iterator p =
961 this->output_addresses_.find(input_offset);
962 if (p != this->output_addresses_.end())
7f649c59 963 return p->second + addend;
a9a60db6 964
7f649c59
ILT
965 return (this->value_from_output_section(object, input_shndx, input_offset)
966 + addend);
a9a60db6
ILT
967 }
968
969 private:
970 // Get the output value for an input offset if we couldn't find it
971 // in the hash table.
972 Value
973 value_from_output_section(const Relobj*, unsigned int input_shndx,
974 Value input_offset) const;
975
976 // The value of the section symbol in the input file. This is
977 // normally zero, but could in principle be something else.
978 Value input_value_;
979 // The start address of this merged section in the output file.
980 Value output_start_address_;
981 // A hash table which maps offsets in the input section to output
982 // addresses. This only maps specific offsets, not all offsets.
983 Output_addresses output_addresses_;
984};
985
b8e6aad9
ILT
986// This POD class is holds the value of a symbol. This is used for
987// local symbols, and for all symbols during relocation processing.
730cdc88
ILT
988// For special sections, such as SHF_MERGE sections, this calls a
989// function to get the final symbol value.
b8e6aad9
ILT
990
991template<int size>
992class Symbol_value
993{
994 public:
995 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
996
997 Symbol_value()
7bf1f802 998 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
d491d34e
ILT
999 is_ordinary_shndx_(false), is_section_symbol_(false),
1000 is_tls_symbol_(false), has_output_value_(true)
a9a60db6 1001 { this->u_.value = 0; }
b8e6aad9
ILT
1002
1003 // Get the value of this symbol. OBJECT is the object in which this
1004 // symbol is defined, and ADDEND is an addend to add to the value.
1005 template<bool big_endian>
1006 Value
1007 value(const Sized_relobj<size, big_endian>* object, Value addend) const
1008 {
a9a60db6
ILT
1009 if (this->has_output_value_)
1010 return this->u_.value + addend;
1011 else
d491d34e
ILT
1012 {
1013 gold_assert(this->is_ordinary_shndx_);
1014 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1015 addend);
1016 }
b8e6aad9
ILT
1017 }
1018
1019 // Set the value of this symbol in the output symbol table.
1020 void
91d6fa6a
NC
1021 set_output_value(Value val)
1022 { this->u_.value = val; }
a9a60db6
ILT
1023
1024 // For a section symbol in a merged section, we need more
1025 // information.
1026 void
1027 set_merged_symbol_value(Merged_symbol_value<size>* msv)
b8e6aad9 1028 {
a9a60db6
ILT
1029 gold_assert(this->is_section_symbol_);
1030 this->has_output_value_ = false;
1031 this->u_.merged_symbol_value = msv;
b8e6aad9
ILT
1032 }
1033
a9a60db6
ILT
1034 // Initialize the input to output map for a section symbol in a
1035 // merged section. We also initialize the value of a non-section
1036 // symbol in a merged section.
b8e6aad9 1037 void
a9a60db6 1038 initialize_input_to_output_map(const Relobj* object)
b8e6aad9 1039 {
a9a60db6
ILT
1040 if (!this->has_output_value_)
1041 {
d491d34e 1042 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
a9a60db6
ILT
1043 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1044 msv->initialize_input_to_output_map(object, this->input_shndx_);
1045 }
b8e6aad9
ILT
1046 }
1047
a9a60db6
ILT
1048 // Free the input to output map for a section symbol in a merged
1049 // section.
1050 void
1051 free_input_to_output_map()
7bf1f802 1052 {
a9a60db6
ILT
1053 if (!this->has_output_value_)
1054 this->u_.merged_symbol_value->free_input_to_output_map();
7bf1f802
ILT
1055 }
1056
a9a60db6
ILT
1057 // Set the value of the symbol from the input file. This is only
1058 // called by count_local_symbols, to communicate the value to
1059 // finalize_local_symbols.
1060 void
91d6fa6a
NC
1061 set_input_value(Value val)
1062 { this->u_.value = val; }
a9a60db6
ILT
1063
1064 // Return the input value. This is only called by
e94cf127 1065 // finalize_local_symbols and (in special cases) relocate_section.
a9a60db6
ILT
1066 Value
1067 input_value() const
1068 { return this->u_.value; }
1069
b8e6aad9
ILT
1070 // Return whether this symbol should go into the output symbol
1071 // table.
1072 bool
1073 needs_output_symtab_entry() const
ac1f0c21 1074 { return this->output_symtab_index_ != -1U; }
b8e6aad9
ILT
1075
1076 // Return the index in the output symbol table.
1077 unsigned int
1078 output_symtab_index() const
1079 {
1080 gold_assert(this->output_symtab_index_ != 0);
1081 return this->output_symtab_index_;
1082 }
1083
1084 // Set the index in the output symbol table.
1085 void
1086 set_output_symtab_index(unsigned int i)
1087 {
1088 gold_assert(this->output_symtab_index_ == 0);
1089 this->output_symtab_index_ = i;
1090 }
1091
1092 // Record that this symbol should not go into the output symbol
1093 // table.
1094 void
1095 set_no_output_symtab_entry()
1096 {
1097 gold_assert(this->output_symtab_index_ == 0);
1098 this->output_symtab_index_ = -1U;
1099 }
1100
7bf1f802
ILT
1101 // Set the index in the output dynamic symbol table.
1102 void
1103 set_needs_output_dynsym_entry()
1104 {
dceae3c1 1105 gold_assert(!this->is_section_symbol());
7bf1f802
ILT
1106 this->output_dynsym_index_ = 0;
1107 }
1108
1109 // Return whether this symbol should go into the output symbol
1110 // table.
1111 bool
1112 needs_output_dynsym_entry() const
1113 {
1114 return this->output_dynsym_index_ != -1U;
1115 }
1116
1117 // Record that this symbol should go into the dynamic symbol table.
1118 void
1119 set_output_dynsym_index(unsigned int i)
1120 {
1121 gold_assert(this->output_dynsym_index_ == 0);
1122 this->output_dynsym_index_ = i;
1123 }
1124
1125 // Return the index in the output dynamic symbol table.
1126 unsigned int
1127 output_dynsym_index() const
1128 {
dceae3c1
ILT
1129 gold_assert(this->output_dynsym_index_ != 0
1130 && this->output_dynsym_index_ != -1U);
7bf1f802
ILT
1131 return this->output_dynsym_index_;
1132 }
1133
b8e6aad9
ILT
1134 // Set the index of the input section in the input file.
1135 void
d491d34e 1136 set_input_shndx(unsigned int i, bool is_ordinary)
730cdc88
ILT
1137 {
1138 this->input_shndx_ = i;
ac1f0c21
ILT
1139 // input_shndx_ field is a bitfield, so make sure that the value
1140 // fits.
730cdc88 1141 gold_assert(this->input_shndx_ == i);
d491d34e 1142 this->is_ordinary_shndx_ = is_ordinary;
730cdc88 1143 }
b8e6aad9 1144
7bf1f802
ILT
1145 // Return the index of the input section in the input file.
1146 unsigned int
d491d34e
ILT
1147 input_shndx(bool* is_ordinary) const
1148 {
1149 *is_ordinary = this->is_ordinary_shndx_;
1150 return this->input_shndx_;
1151 }
7bf1f802 1152
a9a60db6
ILT
1153 // Whether this is a section symbol.
1154 bool
1155 is_section_symbol() const
1156 { return this->is_section_symbol_; }
1157
063f12a8
ILT
1158 // Record that this is a section symbol.
1159 void
1160 set_is_section_symbol()
dceae3c1
ILT
1161 {
1162 gold_assert(!this->needs_output_dynsym_entry());
1163 this->is_section_symbol_ = true;
1164 }
063f12a8 1165
7bf1f802
ILT
1166 // Record that this is a TLS symbol.
1167 void
1168 set_is_tls_symbol()
1169 { this->is_tls_symbol_ = true; }
1170
1171 // Return TRUE if this is a TLS symbol.
1172 bool
1173 is_tls_symbol() const
1174 { return this->is_tls_symbol_; }
1175
b8e6aad9
ILT
1176 private:
1177 // The index of this local symbol in the output symbol table. This
1178 // will be -1 if the symbol should not go into the symbol table.
1179 unsigned int output_symtab_index_;
7bf1f802
ILT
1180 // The index of this local symbol in the dynamic symbol table. This
1181 // will be -1 if the symbol should not go into the symbol table.
1182 unsigned int output_dynsym_index_;
b8e6aad9
ILT
1183 // The section index in the input file in which this symbol is
1184 // defined.
d491d34e
ILT
1185 unsigned int input_shndx_ : 28;
1186 // Whether the section index is an ordinary index, not a special
1187 // value.
1188 bool is_ordinary_shndx_ : 1;
063f12a8
ILT
1189 // Whether this is a STT_SECTION symbol.
1190 bool is_section_symbol_ : 1;
7bf1f802
ILT
1191 // Whether this is a STT_TLS symbol.
1192 bool is_tls_symbol_ : 1;
a9a60db6
ILT
1193 // Whether this symbol has a value for the output file. This is
1194 // normally set to true during Layout::finalize, by
1195 // finalize_local_symbols. It will be false for a section symbol in
1196 // a merge section, as for such symbols we can not determine the
1197 // value to use in a relocation until we see the addend.
1198 bool has_output_value_ : 1;
1199 union
1200 {
1201 // This is used if has_output_value_ is true. Between
1202 // count_local_symbols and finalize_local_symbols, this is the
1203 // value in the input file. After finalize_local_symbols, it is
1204 // the value in the output file.
1205 Value value;
1206 // This is used if has_output_value_ is false. It points to the
1207 // information we need to get the value for a merge section.
1208 Merged_symbol_value<size>* merged_symbol_value;
1209 } u_;
b8e6aad9
ILT
1210};
1211
0a65a3a7
CC
1212// A GOT offset list. A symbol may have more than one GOT offset
1213// (e.g., when mixing modules compiled with two different TLS models),
1214// but will usually have at most one. GOT_TYPE identifies the type of
1215// GOT entry; its values are specific to each target.
1216
1217class Got_offset_list
1218{
1219 public:
1220 Got_offset_list()
1221 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1222 { }
1223
1224 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1225 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1226 { }
1227
1228 ~Got_offset_list()
1229 {
1230 if (this->got_next_ != NULL)
1231 {
1232 delete this->got_next_;
1233 this->got_next_ = NULL;
1234 }
1235 }
1236
1237 // Initialize the fields to their default values.
1238 void
1239 init()
1240 {
1241 this->got_type_ = -1U;
1242 this->got_offset_ = 0;
1243 this->got_next_ = NULL;
1244 }
1245
1246 // Set the offset for the GOT entry of type GOT_TYPE.
1247 void
1248 set_offset(unsigned int got_type, unsigned int got_offset)
1249 {
1250 if (this->got_type_ == -1U)
1251 {
1252 this->got_type_ = got_type;
1253 this->got_offset_ = got_offset;
1254 }
1255 else
1256 {
1257 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1258 {
1259 if (g->got_type_ == got_type)
1260 {
1261 g->got_offset_ = got_offset;
1262 return;
1263 }
1264 }
1265 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1266 g->got_next_ = this->got_next_;
1267 this->got_next_ = g;
1268 }
1269 }
1270
1271 // Return the offset for a GOT entry of type GOT_TYPE.
1272 unsigned int
1273 get_offset(unsigned int got_type) const
1274 {
1275 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1276 {
1277 if (g->got_type_ == got_type)
1278 return g->got_offset_;
1279 }
1280 return -1U;
1281 }
1282
1283 private:
1284 unsigned int got_type_;
1285 unsigned int got_offset_;
1286 Got_offset_list* got_next_;
1287};
1288
364c7fa5
ILT
1289// This type is used to modify relocations for -fsplit-stack. It is
1290// indexed by relocation index, and means that the relocation at that
1291// index should use the symbol from the vector, rather than the one
1292// indicated by the relocation.
1293
1294class Reloc_symbol_changes
1295{
1296 public:
1297 Reloc_symbol_changes(size_t count)
1298 : vec_(count, NULL)
1299 { }
1300
1301 void
1302 set(size_t i, Symbol* sym)
1303 { this->vec_[i] = sym; }
1304
1305 const Symbol*
1306 operator[](size_t i) const
1307 { return this->vec_[i]; }
1308
1309 private:
1310 std::vector<Symbol*> vec_;
1311};
1312
14bfc3f5 1313// A regular object file. This is size and endian specific.
bae7f79e
ILT
1314
1315template<int size, bool big_endian>
f6ce93d6 1316class Sized_relobj : public Relobj
bae7f79e
ILT
1317{
1318 public:
c06b7b0b 1319 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
730cdc88 1320 typedef std::vector<Symbol*> Symbols;
b8e6aad9 1321 typedef std::vector<Symbol_value<size> > Local_values;
c06b7b0b 1322
eff45813
CC
1323 static const Address invalid_address = static_cast<Address>(0) - 1;
1324
f6ce93d6 1325 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
bae7f79e
ILT
1326 const typename elfcpp::Ehdr<size, big_endian>&);
1327
f6ce93d6 1328 ~Sized_relobj();
bae7f79e 1329
6d03d481
ST
1330 // Checks if the offset of input section SHNDX within its output
1331 // section is invalid.
1332 bool
1333 is_output_section_offset_invalid(unsigned int shndx) const
1334 { return this->get_output_section_offset(shndx) == invalid_address; }
1335
f733487b 1336 // Set up the object file based on TARGET.
bae7f79e 1337 void
c0a62865
DK
1338 setup()
1339 { this->do_setup(); }
bae7f79e 1340
7d9e3d98
ILT
1341 // Return the number of symbols. This is only valid after
1342 // Object::add_symbols has been called.
1343 unsigned int
1344 symbol_count() const
1345 { return this->local_symbol_count_ + this->symbols_.size(); }
1346
730cdc88
ILT
1347 // If SYM is the index of a global symbol in the object file's
1348 // symbol table, return the Symbol object. Otherwise, return NULL.
1349 Symbol*
1350 global_symbol(unsigned int sym) const
1351 {
1352 if (sym >= this->local_symbol_count_)
1353 {
1354 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1355 return this->symbols_[sym - this->local_symbol_count_];
1356 }
1357 return NULL;
1358 }
1359
1360 // Return the section index of symbol SYM. Set *VALUE to its value
d491d34e
ILT
1361 // in the object file. Set *IS_ORDINARY if this is an ordinary
1362 // section index, not a special code between SHN_LORESERVE and
1363 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1364 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1365 // it will not return the final value of the symbol in the link.
730cdc88 1366 unsigned int
d491d34e 1367 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
730cdc88
ILT
1368
1369 // Return a pointer to the Symbol_value structure which holds the
1370 // value of a local symbol.
1371 const Symbol_value<size>*
1372 local_symbol(unsigned int sym) const
1373 {
1374 gold_assert(sym < this->local_values_.size());
1375 return &this->local_values_[sym];
1376 }
1377
c06b7b0b
ILT
1378 // Return the index of local symbol SYM in the ordinary symbol
1379 // table. A value of -1U means that the symbol is not being output.
1380 unsigned int
1381 symtab_index(unsigned int sym) const
1382 {
b8e6aad9
ILT
1383 gold_assert(sym < this->local_values_.size());
1384 return this->local_values_[sym].output_symtab_index();
c06b7b0b
ILT
1385 }
1386
7bf1f802
ILT
1387 // Return the index of local symbol SYM in the dynamic symbol
1388 // table. A value of -1U means that the symbol is not being output.
1389 unsigned int
1390 dynsym_index(unsigned int sym) const
1391 {
1392 gold_assert(sym < this->local_values_.size());
1393 return this->local_values_[sym].output_dynsym_index();
1394 }
1395
6a74a719
ILT
1396 // Return the input section index of local symbol SYM.
1397 unsigned int
d491d34e 1398 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
6a74a719
ILT
1399 {
1400 gold_assert(sym < this->local_values_.size());
d491d34e 1401 return this->local_values_[sym].input_shndx(is_ordinary);
6a74a719
ILT
1402 }
1403
d1f003c6 1404 // Record that local symbol SYM needs a dynamic symbol entry.
7bf1f802
ILT
1405 void
1406 set_needs_output_dynsym_entry(unsigned int sym)
1407 {
1408 gold_assert(sym < this->local_values_.size());
1409 this->local_values_[sym].set_needs_output_dynsym_entry();
1410 }
1411
e727fa71 1412 // Return whether the local symbol SYMNDX has a GOT offset.
07f397ab 1413 // For TLS symbols, the GOT entry will hold its tp-relative offset.
e727fa71 1414 bool
0a65a3a7 1415 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
e727fa71 1416 {
0a65a3a7
CC
1417 Local_got_offsets::const_iterator p =
1418 this->local_got_offsets_.find(symndx);
1419 return (p != this->local_got_offsets_.end()
1420 && p->second->get_offset(got_type) != -1U);
e727fa71
ILT
1421 }
1422
1423 // Return the GOT offset of the local symbol SYMNDX.
1424 unsigned int
0a65a3a7 1425 local_got_offset(unsigned int symndx, unsigned int got_type) const
e727fa71
ILT
1426 {
1427 Local_got_offsets::const_iterator p =
1428 this->local_got_offsets_.find(symndx);
1429 gold_assert(p != this->local_got_offsets_.end());
0a65a3a7
CC
1430 unsigned int off = p->second->get_offset(got_type);
1431 gold_assert(off != -1U);
1432 return off;
e727fa71
ILT
1433 }
1434
1435 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1436 void
0a65a3a7
CC
1437 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1438 unsigned int got_offset)
07f397ab 1439 {
0a65a3a7
CC
1440 Local_got_offsets::const_iterator p =
1441 this->local_got_offsets_.find(symndx);
1442 if (p != this->local_got_offsets_.end())
1443 p->second->set_offset(got_type, got_offset);
07f397ab
ILT
1444 else
1445 {
0a65a3a7
CC
1446 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1447 std::pair<Local_got_offsets::iterator, bool> ins =
1448 this->local_got_offsets_.insert(std::make_pair(symndx, g));
07f397ab
ILT
1449 gold_assert(ins.second);
1450 }
1451 }
1452
ef9beddf
ILT
1453 // Get the offset of input section SHNDX within its output section.
1454 // This is -1 if the input section requires a special mapping, such
1455 // as a merge section. The output section can be found in the
1456 // output_sections_ field of the parent class Relobj.
1457 Address
1458 get_output_section_offset(unsigned int shndx) const
1459 {
1460 gold_assert(shndx < this->section_offsets_.size());
1461 return this->section_offsets_[shndx];
1462 }
1463
f7e2ee48
ILT
1464 // Return the name of the symbol that spans the given offset in the
1465 // specified section in this object. This is used only for error
1466 // messages and is not particularly efficient.
1467 bool
1468 get_symbol_location_info(unsigned int shndx, off_t offset,
1469 Symbol_location_info* info);
1470
e94cf127
CC
1471 // Look for a kept section corresponding to the given discarded section,
1472 // and return its output address. This is used only for relocations in
1473 // debugging sections.
1474 Address
1475 map_to_kept_section(unsigned int shndx, bool* found) const;
1476
c0a62865
DK
1477 // Make section offset invalid. This is needed for relaxation.
1478 void
1479 invalidate_section_offset(unsigned int shndx)
1480 { this->do_invalidate_section_offset(shndx); }
1481
6d013333 1482 protected:
c0a62865
DK
1483 // Set up.
1484 virtual void
1485 do_setup();
1486
a2fb1b05 1487 // Read the symbols.
bae7f79e 1488 void
12e14209 1489 do_read_symbols(Read_symbols_data*);
14bfc3f5 1490
6d013333
ILT
1491 // Return the number of local symbols.
1492 unsigned int
1493 do_local_symbol_count() const
1494 { return this->local_symbol_count_; }
1495
dbe717ef
ILT
1496 // Lay out the input sections.
1497 void
7e1edb90 1498 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
dbe717ef 1499
5995b570
CC
1500 // Layout sections whose layout was deferred while waiting for
1501 // input files from a plugin.
1502 void
1503 do_layout_deferred_sections(Layout*);
1504
12e14209
ILT
1505 // Add the symbols to the symbol table.
1506 void
f488e4b0 1507 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
a2fb1b05 1508
92e059d8
ILT
1509 // Read the relocs.
1510 void
1511 do_read_relocs(Read_relocs_data*);
1512
6d03d481
ST
1513 // Process the relocs to find list of referenced sections. Used only
1514 // during garbage collection.
1515 void
ad0f2072 1516 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
6d03d481 1517
92e059d8
ILT
1518 // Scan the relocs and adjust the symbol table.
1519 void
ad0f2072 1520 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
92e059d8 1521
7bf1f802
ILT
1522 // Count the local symbols.
1523 void
1524 do_count_local_symbols(Stringpool_template<char>*,
1525 Stringpool_template<char>*);
1526
75f65a3e 1527 // Finalize the local symbols.
c06b7b0b 1528 unsigned int
ef15dade 1529 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
7bf1f802
ILT
1530
1531 // Set the offset where local dynamic symbol information will be stored.
1532 unsigned int
1533 do_set_local_dynsym_indexes(unsigned int);
1534
1535 // Set the offset where local dynamic symbol information will be stored.
1536 unsigned int
1537 do_set_local_dynsym_offset(off_t);
75f65a3e 1538
61ba1cf9
ILT
1539 // Relocate the input sections and write out the local symbols.
1540 void
ad0f2072 1541 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
92e059d8 1542
a445fddf
ILT
1543 // Get the size of a section.
1544 uint64_t
1545 do_section_size(unsigned int shndx)
1546 { return this->elf_file_.section_size(shndx); }
1547
92e059d8
ILT
1548 // Get the name of a section.
1549 std::string
645f8123
ILT
1550 do_section_name(unsigned int shndx)
1551 { return this->elf_file_.section_name(shndx); }
61ba1cf9 1552
645f8123 1553 // Return the location of the contents of a section.
dbe717ef 1554 Object::Location
645f8123
ILT
1555 do_section_contents(unsigned int shndx)
1556 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 1557
a3ad94ed
ILT
1558 // Return section flags.
1559 uint64_t
ef15dade
ST
1560 do_section_flags(unsigned int shndx);
1561
1562 // Return section entsize.
1563 uint64_t
1564 do_section_entsize(unsigned int shndx);
a3ad94ed 1565
88dd47ac
ILT
1566 // Return section address.
1567 uint64_t
1568 do_section_address(unsigned int shndx)
1569 { return this->elf_file_.section_addr(shndx); }
1570
730cdc88
ILT
1571 // Return section type.
1572 unsigned int
1573 do_section_type(unsigned int shndx)
1574 { return this->elf_file_.section_type(shndx); }
1575
f7e2ee48
ILT
1576 // Return the section link field.
1577 unsigned int
1578 do_section_link(unsigned int shndx)
1579 { return this->elf_file_.section_link(shndx); }
1580
4c50553d
ILT
1581 // Return the section info field.
1582 unsigned int
1583 do_section_info(unsigned int shndx)
1584 { return this->elf_file_.section_info(shndx); }
1585
a445fddf
ILT
1586 // Return the section alignment.
1587 uint64_t
1588 do_section_addralign(unsigned int shndx)
1589 { return this->elf_file_.section_addralign(shndx); }
1590
d491d34e
ILT
1591 // Return the Xindex structure to use.
1592 Xindex*
1593 do_initialize_xindex();
1594
92de84a6
ILT
1595 // Get symbol counts.
1596 void
1597 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1598
ef9beddf
ILT
1599 // Get the offset of a section.
1600 uint64_t
1601 do_output_section_offset(unsigned int shndx) const
eff45813
CC
1602 {
1603 Address off = this->get_output_section_offset(shndx);
1604 if (off == invalid_address)
1605 return -1ULL;
1606 return off;
1607 }
ef9beddf
ILT
1608
1609 // Set the offset of a section.
1610 void
1611 do_set_section_offset(unsigned int shndx, uint64_t off)
1612 {
1613 gold_assert(shndx < this->section_offsets_.size());
1614 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1615 }
1616
c0a62865
DK
1617 // Set the offset of a section to invalid_address.
1618 virtual void
1619 do_invalidate_section_offset(unsigned int shndx)
1620 {
1621 gold_assert(shndx < this->section_offsets_.size());
1622 this->section_offsets_[shndx] = invalid_address;
1623 }
1624
1625 // Adjust a section index if necessary.
1626 unsigned int
1627 adjust_shndx(unsigned int shndx)
1628 {
1629 if (shndx >= elfcpp::SHN_LORESERVE)
1630 shndx += this->elf_file_.large_shndx_offset();
1631 return shndx;
1632 }
1633
1634 // Initialize input to output maps for section symbols in merged
1635 // sections.
1636 void
1637 initialize_input_to_output_maps();
1638
1639 // Free the input to output maps for section symbols in merged
1640 // sections.
1641 void
1642 free_input_to_output_maps();
1643
1644 // Return symbol table section index.
1645 unsigned int
1646 symtab_shndx() const
1647 { return this->symtab_shndx_; }
1648
1649 // Allow a child class to access the ELF file.
1650 elfcpp::Elf_file<size, big_endian, Object>*
1651 elf_file()
1652 { return &this->elf_file_; }
1653
1654 // Allow a child class to access the local values.
1655 Local_values*
1656 local_values()
1657 { return &this->local_values_; }
1658
72adc4fa
DK
1659 // Views and sizes when relocating.
1660 struct View_size
1661 {
1662 unsigned char* view;
1663 typename elfcpp::Elf_types<size>::Elf_Addr address;
1664 off_t offset;
1665 section_size_type view_size;
1666 bool is_input_output_view;
1667 bool is_postprocessing_view;
1668 };
1669
1670 typedef std::vector<View_size> Views;
1671
1672 // This may be overriden by a child class.
1673 virtual void
ad0f2072 1674 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
72adc4fa
DK
1675 const unsigned char* pshdrs, Views* pviews);
1676
bae7f79e 1677 private:
a2fb1b05 1678 // For convenience.
f6ce93d6 1679 typedef Sized_relobj<size, big_endian> This;
a2fb1b05
ILT
1680 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1681 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1682 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e
ILT
1683 typedef elfcpp::Shdr<size, big_endian> Shdr;
1684
ef9beddf
ILT
1685 // To keep track of discarded comdat sections, we need to map a member
1686 // section index to the object and section index of the corresponding
1687 // kept section.
1688 struct Kept_comdat_section
1689 {
1ef4d87f
ILT
1690 Kept_comdat_section(Relobj* a_object, unsigned int a_shndx)
1691 : object(a_object), shndx(a_shndx)
ef9beddf 1692 { }
1ef4d87f
ILT
1693 Relobj* object;
1694 unsigned int shndx;
ef9beddf 1695 };
1ef4d87f 1696 typedef std::map<unsigned int, Kept_comdat_section>
ef9beddf
ILT
1697 Kept_comdat_section_table;
1698
dbe717ef
ILT
1699 // Find the SHT_SYMTAB section, given the section headers.
1700 void
1701 find_symtab(const unsigned char* pshdrs);
1702
730cdc88
ILT
1703 // Return whether SHDR has the right flags for a GNU style exception
1704 // frame section.
1705 bool
1706 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1707
1708 // Return whether there is a section named .eh_frame which might be
1709 // a GNU style exception frame section.
1710 bool
1711 find_eh_frame(const unsigned char* pshdrs, const char* names,
8383303e 1712 section_size_type names_size) const;
730cdc88 1713
a2fb1b05
ILT
1714 // Whether to include a section group in the link.
1715 bool
6a74a719 1716 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
e94cf127 1717 const unsigned char*, const char *, section_size_type,
a2fb1b05
ILT
1718 std::vector<bool>*);
1719
1720 // Whether to include a linkonce section in the link.
1721 bool
e94cf127 1722 include_linkonce_section(Layout*, unsigned int, const char*,
a2fb1b05
ILT
1723 const elfcpp::Shdr<size, big_endian>&);
1724
5995b570
CC
1725 // Layout an input section.
1726 void
1727 layout_section(Layout* layout, unsigned int shndx, const char* name,
1728 typename This::Shdr& shdr, unsigned int reloc_shndx,
1729 unsigned int reloc_type);
1730
61ba1cf9
ILT
1731 // Write section data to the output file. Record the views and
1732 // sizes in VIEWS for use when relocating.
1733 void
cb295612 1734 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
61ba1cf9
ILT
1735
1736 // Relocate the sections in the output file.
1737 void
91d6fa6a 1738 relocate_sections(const Symbol_table* symtab, const Layout* olayout,
ad0f2072 1739 const unsigned char* pshdrs, Views* pviews)
91d6fa6a 1740 { this->do_relocate_sections(symtab, olayout, pshdrs, pviews); }
61ba1cf9 1741
7019cd25
ILT
1742 // Scan the input relocations for --emit-relocs.
1743 void
ad0f2072 1744 emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
7019cd25
ILT
1745 const Read_relocs_data::Relocs_list::iterator&);
1746
1747 // Scan the input relocations for --emit-relocs, templatized on the
1748 // type of the relocation section.
1749 template<int sh_type>
1750 void
ad0f2072 1751 emit_relocs_scan_reltype(Symbol_table*, Layout*,
7019cd25
ILT
1752 const unsigned char* plocal_syms,
1753 const Read_relocs_data::Relocs_list::iterator&,
1754 Relocatable_relocs*);
1755
1756 // Emit the relocs for --emit-relocs.
1757 void
1758 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1759 unsigned int sh_type, const unsigned char* prelocs,
ef9beddf 1760 size_t reloc_count, Output_section*, Address output_offset,
7019cd25
ILT
1761 unsigned char* view, Address address,
1762 section_size_type view_size,
1763 unsigned char* reloc_view, section_size_type reloc_view_size);
1764
1765 // Emit the relocs for --emit-relocs, templatized on the type of the
1766 // relocation section.
1767 template<int sh_type>
1768 void
1769 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1770 const unsigned char* prelocs, size_t reloc_count,
ef9beddf 1771 Output_section*, Address output_offset,
7019cd25
ILT
1772 unsigned char* view, Address address,
1773 section_size_type view_size,
1774 unsigned char* reloc_view,
1775 section_size_type reloc_view_size);
1776
364c7fa5
ILT
1777 // A type shared by split_stack_adjust_reltype and find_functions.
1778 typedef std::map<section_offset_type, section_size_type> Function_offsets;
1779
1780 // Check for -fsplit-stack routines calling non-split-stack routines.
1781 void
1782 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
1783 unsigned int sh_type, unsigned int shndx,
1784 const unsigned char* prelocs, size_t reloc_count,
1785 unsigned char* view, section_size_type view_size,
1786 Reloc_symbol_changes** reloc_map);
1787
1788 template<int sh_type>
1789 void
1790 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
1791 unsigned int shndx, const unsigned char* prelocs,
1792 size_t reloc_count, unsigned char* view,
1793 section_size_type view_size,
1794 Reloc_symbol_changes** reloc_map);
1795
1796 // Find all functions in a section.
1797 void
1798 find_functions(const unsigned char* pshdrs, unsigned int shndx,
1799 Function_offsets*);
1800
61ba1cf9
ILT
1801 // Write out the local symbols.
1802 void
b8e6aad9 1803 write_local_symbols(Output_file*,
7bf1f802 1804 const Stringpool_template<char>*,
d491d34e
ILT
1805 const Stringpool_template<char>*,
1806 Output_symtab_xindex*,
1807 Output_symtab_xindex*);
61ba1cf9 1808
cb295612
ILT
1809 // Clear the local symbol information.
1810 void
1811 clear_local_symbols()
1812 {
1813 this->local_values_.clear();
1814 this->local_got_offsets_.clear();
cb295612
ILT
1815 }
1816
ef9beddf
ILT
1817 // Record a mapping from discarded section SHNDX to the corresponding
1818 // kept section.
1819 void
1ef4d87f
ILT
1820 set_kept_comdat_section(unsigned int shndx, Relobj* kept_object,
1821 unsigned int kept_shndx)
ef9beddf 1822 {
1ef4d87f
ILT
1823 Kept_comdat_section kept(kept_object, kept_shndx);
1824 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
ef9beddf
ILT
1825 }
1826
1ef4d87f
ILT
1827 // Find the kept section corresponding to the discarded section
1828 // SHNDX. Return true if found.
1829 bool
1830 get_kept_comdat_section(unsigned int shndx, Relobj** kept_object,
1831 unsigned int* kept_shndx) const
ef9beddf
ILT
1832 {
1833 typename Kept_comdat_section_table::const_iterator p =
1834 this->kept_comdat_sections_.find(shndx);
1835 if (p == this->kept_comdat_sections_.end())
1ef4d87f
ILT
1836 return false;
1837 *kept_object = p->second.object;
1838 *kept_shndx = p->second.shndx;
1839 return true;
ef9beddf
ILT
1840 }
1841
07f397ab
ILT
1842 // The GOT offsets of local symbols. This map also stores GOT offsets
1843 // for tp-relative offsets for TLS symbols.
0a65a3a7 1844 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
e727fa71 1845
07f397ab
ILT
1846 // The TLS GOT offsets of local symbols. The map stores the offsets
1847 // for either a single GOT entry that holds the module index of a TLS
1848 // symbol, or a pair of GOT entries containing the module index and
1849 // dtv-relative offset.
1850 struct Tls_got_entry
1851 {
1852 Tls_got_entry(int got_offset, bool have_pair)
1853 : got_offset_(got_offset),
1854 have_pair_(have_pair)
1855 { }
1856 int got_offset_;
1857 bool have_pair_;
1858 };
1859 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1860
5995b570
CC
1861 // Saved information for sections whose layout was deferred.
1862 struct Deferred_layout
1863 {
1864 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1865 Deferred_layout(unsigned int shndx, const char* name,
1866 const unsigned char* pshdr,
1867 unsigned int reloc_shndx, unsigned int reloc_type)
1868 : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx),
1869 reloc_type_(reloc_type)
1870 {
1871 memcpy(this->shdr_data_, pshdr, shdr_size);
1872 }
1873 unsigned int shndx_;
1874 std::string name_;
1875 unsigned int reloc_shndx_;
1876 unsigned int reloc_type_;
1877 unsigned char shdr_data_[shdr_size];
1878 };
1879
645f8123
ILT
1880 // General access to the ELF file.
1881 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
bae7f79e 1882 // Index of SHT_SYMTAB section.
645f8123 1883 unsigned int symtab_shndx_;
61ba1cf9
ILT
1884 // The number of local symbols.
1885 unsigned int local_symbol_count_;
1886 // The number of local symbols which go into the output file.
1887 unsigned int output_local_symbol_count_;
7bf1f802
ILT
1888 // The number of local symbols which go into the output file's dynamic
1889 // symbol table.
1890 unsigned int output_local_dynsym_count_;
14bfc3f5 1891 // The entries in the symbol table for the external symbols.
730cdc88 1892 Symbols symbols_;
92de84a6
ILT
1893 // Number of symbols defined in object file itself.
1894 size_t defined_count_;
75f65a3e
ILT
1895 // File offset for local symbols.
1896 off_t local_symbol_offset_;
7bf1f802
ILT
1897 // File offset for local dynamic symbols.
1898 off_t local_dynsym_offset_;
61ba1cf9 1899 // Values of local symbols.
c06b7b0b 1900 Local_values local_values_;
07f397ab
ILT
1901 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1902 // for TLS symbols, indexed by symbol number.
e727fa71 1903 Local_got_offsets local_got_offsets_;
ef9beddf 1904 // For each input section, the offset of the input section in its
eff45813 1905 // output section. This is INVALID_ADDRESS if the input section requires a
ef9beddf
ILT
1906 // special mapping.
1907 std::vector<Address> section_offsets_;
1908 // Table mapping discarded comdat sections to corresponding kept sections.
1909 Kept_comdat_section_table kept_comdat_sections_;
730cdc88
ILT
1910 // Whether this object has a GNU style .eh_frame section.
1911 bool has_eh_frame_;
805bb01c
DK
1912 // If this object has a GNU style .eh_frame section that is discarded in
1913 // output, record the index here. Otherwise it is -1U.
1914 unsigned int discarded_eh_frame_shndx_;
5995b570
CC
1915 // The list of sections whose layout was deferred.
1916 std::vector<Deferred_layout> deferred_layout_;
bae7f79e
ILT
1917};
1918
54dc6425 1919// A class to manage the list of all objects.
a2fb1b05 1920
54dc6425
ILT
1921class Input_objects
1922{
1923 public:
1924 Input_objects()
fd9d194f 1925 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
54dc6425
ILT
1926 { }
1927
f6ce93d6
ILT
1928 // The type of the list of input relocateable objects.
1929 typedef std::vector<Relobj*> Relobj_list;
1930 typedef Relobj_list::const_iterator Relobj_iterator;
1931
1932 // The type of the list of input dynamic objects.
1933 typedef std::vector<Dynobj*> Dynobj_list;
1934 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425 1935
008db82e
ILT
1936 // Add an object to the list. Return true if all is well, or false
1937 // if this object should be ignored.
1938 bool
54dc6425
ILT
1939 add_object(Object*);
1940
92de84a6
ILT
1941 // Start processing an archive.
1942 void
1943 archive_start(Archive*);
1944
1945 // Stop processing an archive.
1946 void
1947 archive_stop(Archive*);
1948
e2827e5f
ILT
1949 // For each dynamic object, check whether we've seen all of its
1950 // explicit dependencies.
1951 void
1952 check_dynamic_dependencies() const;
1953
9a2d6984
ILT
1954 // Return whether an object was found in the system library
1955 // directory.
1956 bool
1957 found_in_system_library_directory(const Object*) const;
1958
92de84a6
ILT
1959 // Print symbol counts.
1960 void
1961 print_symbol_counts(const Symbol_table*) const;
1962
f6ce93d6
ILT
1963 // Iterate over all regular objects.
1964
1965 Relobj_iterator
1966 relobj_begin() const
1967 { return this->relobj_list_.begin(); }
1968
1969 Relobj_iterator
1970 relobj_end() const
1971 { return this->relobj_list_.end(); }
1972
1973 // Iterate over all dynamic objects.
1974
1975 Dynobj_iterator
1976 dynobj_begin() const
1977 { return this->dynobj_list_.begin(); }
54dc6425 1978
f6ce93d6
ILT
1979 Dynobj_iterator
1980 dynobj_end() const
1981 { return this->dynobj_list_.end(); }
54dc6425
ILT
1982
1983 // Return whether we have seen any dynamic objects.
1984 bool
1985 any_dynamic() const
f6ce93d6 1986 { return !this->dynobj_list_.empty(); }
54dc6425 1987
fe9a4c12
ILT
1988 // Return the number of input objects.
1989 int
1990 number_of_input_objects() const
1991 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1992
54dc6425
ILT
1993 private:
1994 Input_objects(const Input_objects&);
1995 Input_objects& operator=(const Input_objects&);
1996
008db82e 1997 // The list of ordinary objects included in the link.
f6ce93d6 1998 Relobj_list relobj_list_;
008db82e 1999 // The list of dynamic objects included in the link.
f6ce93d6 2000 Dynobj_list dynobj_list_;
008db82e
ILT
2001 // SONAMEs that we have seen.
2002 Unordered_set<std::string> sonames_;
92de84a6
ILT
2003 // Manage cross-references if requested.
2004 Cref* cref_;
54dc6425 2005};
a2fb1b05 2006
92e059d8
ILT
2007// Some of the information we pass to the relocation routines. We
2008// group this together to avoid passing a dozen different arguments.
2009
2010template<int size, bool big_endian>
2011struct Relocate_info
2012{
92e059d8
ILT
2013 // Symbol table.
2014 const Symbol_table* symtab;
2015 // Layout.
2016 const Layout* layout;
2017 // Object being relocated.
f6ce93d6 2018 Sized_relobj<size, big_endian>* object;
92e059d8
ILT
2019 // Section index of relocation section.
2020 unsigned int reloc_shndx;
82bb573a
ILT
2021 // Section header of relocation section.
2022 const unsigned char* reloc_shdr;
92e059d8
ILT
2023 // Section index of section being relocated.
2024 unsigned int data_shndx;
82bb573a
ILT
2025 // Section header of data section.
2026 const unsigned char* data_shdr;
92e059d8
ILT
2027
2028 // Return a string showing the location of a relocation. This is
2029 // only used for error messages.
2030 std::string
2031 location(size_t relnum, off_t reloffset) const;
2032};
2033
f6060a4d
ILT
2034// Return whether INPUT_FILE contains an ELF object start at file
2035// offset OFFSET. This sets *START to point to a view of the start of
2036// the file. It sets *READ_SIZE to the number of bytes in the view.
2037
2038extern bool
2039is_elf_object(Input_file* input_file, off_t offset,
2040 const unsigned char** start, int *read_size);
2041
bae7f79e 2042// Return an Object appropriate for the input file. P is BYTES long,
15f8229b
ILT
2043// and holds the ELF header. If PUNCONFIGURED is not NULL, then if
2044// this sees an object the linker is not configured to support, it
2045// sets *PUNCONFIGURED to true and returns NULL without giving an
2046// error message.
bae7f79e 2047
61ba1cf9
ILT
2048extern Object*
2049make_elf_object(const std::string& name, Input_file*,
2050 off_t offset, const unsigned char* p,
15f8229b 2051 section_offset_type bytes, bool* punconfigured);
bae7f79e
ILT
2052
2053} // end namespace gold
2054
2055#endif // !defined(GOLD_OBJECT_H)
This page took 0.292358 seconds and 4 git commands to generate.