]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/object.h
* hostio.c: Correct copyright year.
[thirdparty/binutils-gdb.git] / gold / object.h
CommitLineData
bae7f79e
ILT
1// object.h -- support for an object file for linking in gold -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
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;
a2fb1b05 38class Layout;
61ba1cf9
ILT
39class Output_section;
40class Output_file;
f6ce93d6 41class Dynobj;
4625f782 42class Object_merge_map;
a2fb1b05 43
b8e6aad9
ILT
44template<typename Stringpool_char>
45class Stringpool_template;
46
bae7f79e
ILT
47// Data to pass from read_symbols() to add_symbols().
48
49struct Read_symbols_data
50{
12e14209
ILT
51 // Section headers.
52 File_view* section_headers;
53 // Section names.
54 File_view* section_names;
55 // Size of section name data in bytes.
56 off_t section_names_size;
bae7f79e
ILT
57 // Symbol data.
58 File_view* symbols;
59 // Size of symbol data in bytes.
60 off_t symbols_size;
730cdc88
ILT
61 // Offset of external symbols within symbol data. This structure
62 // sometimes contains only external symbols, in which case this will
63 // be zero. Sometimes it contains all symbols.
64 off_t external_symbols_offset;
bae7f79e
ILT
65 // Symbol names.
66 File_view* symbol_names;
67 // Size of symbol name data in bytes.
68 off_t symbol_names_size;
dbe717ef
ILT
69
70 // Version information. This is only used on dynamic objects.
71 // Version symbol data (from SHT_GNU_versym section).
72 File_view* versym;
73 off_t versym_size;
74 // Version definition data (from SHT_GNU_verdef section).
75 File_view* verdef;
76 off_t verdef_size;
77 unsigned int verdef_info;
78 // Needed version data (from SHT_GNU_verneed section).
79 File_view* verneed;
80 off_t verneed_size;
81 unsigned int verneed_info;
bae7f79e
ILT
82};
83
f7e2ee48
ILT
84// Information used to print error messages.
85
86struct Symbol_location_info
87{
88 std::string source_file;
89 std::string enclosing_symbol_name;
90 int line_number;
91};
92
92e059d8
ILT
93// Data about a single relocation section. This is read in
94// read_relocs and processed in scan_relocs.
95
96struct Section_relocs
97{
98 // Index of reloc section.
99 unsigned int reloc_shndx;
100 // Index of section that relocs apply to.
101 unsigned int data_shndx;
102 // Contents of reloc section.
103 File_view* contents;
104 // Reloc section type.
105 unsigned int sh_type;
106 // Number of reloc entries.
107 size_t reloc_count;
730cdc88
ILT
108 // Output section.
109 Output_section* output_section;
110 // Whether this section has special handling for offsets.
111 bool needs_special_offset_handling;
92e059d8
ILT
112};
113
114// Relocations in an object file. This is read in read_relocs and
115// processed in scan_relocs.
116
117struct Read_relocs_data
118{
119 typedef std::vector<Section_relocs> Relocs_list;
120 // The relocations.
121 Relocs_list relocs;
122 // The local symbols.
123 File_view* local_symbols;
124};
125
f6ce93d6
ILT
126// Object is an abstract base class which represents either a 32-bit
127// or a 64-bit input object. This can be a regular object file
128// (ET_REL) or a shared object (ET_DYN).
bae7f79e
ILT
129
130class Object
131{
132 public:
133 // NAME is the name of the object as we would report it to the user
134 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
135 // used to read the file. OFFSET is the offset within the input
136 // file--0 for a .o or .so file, something else for a .a file.
14bfc3f5
ILT
137 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
138 off_t offset = 0)
dbe717ef 139 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
f6ce93d6 140 is_dynamic_(is_dynamic), target_(NULL)
bae7f79e
ILT
141 { }
142
143 virtual ~Object()
144 { }
145
14bfc3f5 146 // Return the name of the object as we would report it to the tuser.
bae7f79e
ILT
147 const std::string&
148 name() const
149 { return this->name_; }
150
cec9d2f3
ILT
151 // Get the offset into the file.
152 off_t
153 offset() const
154 { return this->offset_; }
155
14bfc3f5
ILT
156 // Return whether this is a dynamic object.
157 bool
158 is_dynamic() const
159 { return this->is_dynamic_; }
160
14bfc3f5
ILT
161 // Return the target structure associated with this object.
162 Target*
a2fb1b05 163 target() const
14bfc3f5
ILT
164 { return this->target_; }
165
a2fb1b05
ILT
166 // Lock the underlying file.
167 void
168 lock()
169 { this->input_file_->file().lock(); }
170
171 // Unlock the underlying file.
172 void
173 unlock()
174 { this->input_file_->file().unlock(); }
175
176 // Return whether the underlying file is locked.
177 bool
178 is_locked() const
179 { return this->input_file_->file().is_locked(); }
180
14bfc3f5
ILT
181 // Return the sized target structure associated with this object.
182 // This is like the target method but it returns a pointer of
183 // appropriate checked type.
184 template<int size, bool big_endian>
185 Sized_target<size, big_endian>*
5482377d 186 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
bae7f79e 187
5a6f7e2d
ILT
188 // Get the number of sections.
189 unsigned int
190 shnum() const
191 { return this->shnum_; }
a2fb1b05 192
f6ce93d6 193 // Return a view of the contents of a section. Set *PLEN to the
9eb9fa57 194 // size. CACHE is a hint as in File_read::get_view.
f6ce93d6 195 const unsigned char*
9eb9fa57 196 section_contents(unsigned int shndx, off_t* plen, bool cache);
f6ce93d6
ILT
197
198 // Return the name of a section given a section index. This is only
199 // used for error messages.
200 std::string
a3ad94ed
ILT
201 section_name(unsigned int shndx)
202 { return this->do_section_name(shndx); }
203
204 // Return the section flags given a section index.
205 uint64_t
206 section_flags(unsigned int shndx)
207 { return this->do_section_flags(shndx); }
f6ce93d6 208
730cdc88
ILT
209 // Return the section type given a section index.
210 unsigned int
211 section_type(unsigned int shndx)
212 { return this->do_section_type(shndx); }
213
f7e2ee48
ILT
214 // Return the section link field given a section index.
215 unsigned int
216 section_link(unsigned int shndx)
217 { return this->do_section_link(shndx); }
218
4c50553d
ILT
219 // Return the section info field given a section index.
220 unsigned int
221 section_info(unsigned int shndx)
222 { return this->do_section_info(shndx); }
223
5a6f7e2d
ILT
224 // Read the symbol information.
225 void
226 read_symbols(Read_symbols_data* sd)
227 { return this->do_read_symbols(sd); }
228
229 // Pass sections which should be included in the link to the Layout
230 // object, and record where the sections go in the output file.
231 void
7e1edb90
ILT
232 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
233 { this->do_layout(symtab, layout, sd); }
5a6f7e2d
ILT
234
235 // Add symbol information to the global symbol table.
236 void
237 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
238 { this->do_add_symbols(symtab, sd); }
239
645f8123
ILT
240 // Functions and types for the elfcpp::Elf_file interface. This
241 // permit us to use Object as the File template parameter for
242 // elfcpp::Elf_file.
243
244 // The View class is returned by view. It must support a single
245 // method, data(). This is trivial, because get_view does what we
246 // need.
247 class View
248 {
249 public:
250 View(const unsigned char* p)
251 : p_(p)
252 { }
253
254 const unsigned char*
255 data() const
256 { return this->p_; }
257
258 private:
259 const unsigned char* p_;
260 };
261
262 // Return a View.
263 View
264 view(off_t file_offset, off_t data_size)
9eb9fa57 265 { return View(this->get_view(file_offset, data_size, true)); }
645f8123
ILT
266
267 // Report an error.
268 void
75f2446e 269 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
645f8123
ILT
270
271 // A location in the file.
272 struct Location
273 {
274 off_t file_offset;
275 off_t data_size;
276
277 Location(off_t fo, off_t ds)
278 : file_offset(fo), data_size(ds)
279 { }
280 };
281
282 // Get a View given a Location.
283 View view(Location loc)
9eb9fa57 284 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
645f8123 285
f6ce93d6
ILT
286 protected:
287 // Read the symbols--implemented by child class.
288 virtual void
289 do_read_symbols(Read_symbols_data*) = 0;
290
291 // Lay out sections--implemented by child class.
292 virtual void
7e1edb90 293 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
f6ce93d6
ILT
294
295 // Add symbol information to the global symbol table--implemented by
296 // child class.
297 virtual void
298 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
299
645f8123
ILT
300 // Return the location of the contents of a section. Implemented by
301 // child class.
302 virtual Location
a3ad94ed 303 do_section_contents(unsigned int shndx) = 0;
f6ce93d6
ILT
304
305 // Get the name of a section--implemented by child class.
306 virtual std::string
a3ad94ed
ILT
307 do_section_name(unsigned int shndx) = 0;
308
309 // Get section flags--implemented by child class.
310 virtual uint64_t
311 do_section_flags(unsigned int shndx) = 0;
f6ce93d6 312
730cdc88
ILT
313 // Get section type--implemented by child class.
314 virtual unsigned int
315 do_section_type(unsigned int shndx) = 0;
316
f7e2ee48
ILT
317 // Get section link field--implemented by child class.
318 virtual unsigned int
319 do_section_link(unsigned int shndx) = 0;
320
4c50553d
ILT
321 // Get section info field--implemented by child class.
322 virtual unsigned int
323 do_section_info(unsigned int shndx) = 0;
324
f6ce93d6
ILT
325 // Get the file.
326 Input_file*
327 input_file() const
328 { return this->input_file_; }
329
f6ce93d6
ILT
330 // Get a view into the underlying file.
331 const unsigned char*
9eb9fa57
ILT
332 get_view(off_t start, off_t size, bool cache)
333 {
334 return this->input_file_->file().get_view(start + this->offset_, size,
335 cache);
336 }
f6ce93d6
ILT
337
338 // Get a lasting view into the underlying file.
339 File_view*
9eb9fa57 340 get_lasting_view(off_t start, off_t size, bool cache)
f6ce93d6
ILT
341 {
342 return this->input_file_->file().get_lasting_view(start + this->offset_,
9eb9fa57 343 size, cache);
f6ce93d6
ILT
344 }
345
346 // Read data from the underlying file.
347 void
348 read(off_t start, off_t size, void* p)
349 { this->input_file_->file().read(start + this->offset_, size, p); }
350
351 // Set the target.
352 void
dbe717ef
ILT
353 set_target(int machine, int size, bool big_endian, int osabi,
354 int abiversion);
355
dbe717ef
ILT
356 // Set the number of sections.
357 void
358 set_shnum(int shnum)
359 { this->shnum_ = shnum; }
360
361 // Functions used by both Sized_relobj and Sized_dynobj.
362
363 // Read the section data into a Read_symbols_data object.
364 template<int size, bool big_endian>
365 void
366 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
367 Read_symbols_data*);
368
369 // If NAME is the name of a special .gnu.warning section, arrange
370 // for the warning to be issued. SHNDX is the section index.
371 // Return whether it is a warning section.
372 bool
373 handle_gnu_warning_section(const char* name, unsigned int shndx,
374 Symbol_table*);
f6ce93d6
ILT
375
376 private:
377 // This class may not be copied.
378 Object(const Object&);
379 Object& operator=(const Object&);
380
381 // Name of object as printed to user.
382 std::string name_;
383 // For reading the file.
384 Input_file* input_file_;
385 // Offset within the file--0 for an object file, non-0 for an
386 // archive.
387 off_t offset_;
dbe717ef
ILT
388 // Number of input sections.
389 unsigned int shnum_;
f6ce93d6
ILT
390 // Whether this is a dynamic object.
391 bool is_dynamic_;
392 // Target functions--may be NULL if the target is not known.
393 Target* target_;
394};
395
396// Implement sized_target inline for efficiency. This approach breaks
397// static type checking, but is made safe using asserts.
398
399template<int size, bool big_endian>
400inline Sized_target<size, big_endian>*
401Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
402{
a3ad94ed
ILT
403 gold_assert(this->target_->get_size() == size);
404 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
f6ce93d6
ILT
405 return static_cast<Sized_target<size, big_endian>*>(this->target_);
406}
407
408// A regular object (ET_REL). This is an abstract base class itself.
b8e6aad9 409// The implementation is the template class Sized_relobj.
f6ce93d6
ILT
410
411class Relobj : public Object
412{
413 public:
414 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
4625f782
ILT
415 : Object(name, input_file, false, offset),
416 map_to_output_(),
417 object_merge_map_(NULL),
418 relocs_must_follow_section_writes_(false)
f6ce93d6
ILT
419 { }
420
92e059d8 421 // Read the relocs.
a2fb1b05 422 void
92e059d8
ILT
423 read_relocs(Read_relocs_data* rd)
424 { return this->do_read_relocs(rd); }
425
426 // Scan the relocs and adjust the symbol table.
427 void
428 scan_relocs(const General_options& options, Symbol_table* symtab,
ead1e424
ILT
429 Layout* layout, Read_relocs_data* rd)
430 { return this->do_scan_relocs(options, symtab, layout, rd); }
a2fb1b05 431
75f65a3e
ILT
432 // Initial local symbol processing: set the offset where local
433 // symbol information will be stored; add local symbol names to
c06b7b0b
ILT
434 // *POOL; return the new local symbol index.
435 unsigned int
b8e6aad9
ILT
436 finalize_local_symbols(unsigned int index, off_t off,
437 Stringpool_template<char>* pool)
c06b7b0b 438 { return this->do_finalize_local_symbols(index, off, pool); }
75f65a3e 439
61ba1cf9
ILT
440 // Relocate the input sections and write out the local symbols.
441 void
442 relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
443 const Layout* layout, Output_file* of)
444 { return this->do_relocate(options, symtab, layout, of); }
61ba1cf9 445
a783673b
ILT
446 // Return whether an input section is being included in the link.
447 bool
5a6f7e2d 448 is_section_included(unsigned int shndx) const
a783673b 449 {
5a6f7e2d
ILT
450 gold_assert(shndx < this->map_to_output_.size());
451 return this->map_to_output_[shndx].output_section != NULL;
a783673b
ILT
452 }
453
730cdc88
ILT
454 // Return whether an input section requires special
455 // handling--whether it is not simply mapped from the input file to
456 // the output file.
457 bool
458 is_section_specially_mapped(unsigned int shndx) const
459 {
460 gold_assert(shndx < this->map_to_output_.size());
461 return (this->map_to_output_[shndx].output_section != NULL
462 && this->map_to_output_[shndx].offset == -1);
463 }
464
a783673b
ILT
465 // Given a section index, return the corresponding Output_section
466 // (which will be NULL if the section is not included in the link)
730cdc88
ILT
467 // and set *POFF to the offset within that section. *POFF will be
468 // set to -1 if the section requires special handling.
a783673b 469 inline Output_section*
b8e6aad9 470 output_section(unsigned int shndx, off_t* poff) const;
a783673b 471
ead1e424
ILT
472 // Set the offset of an input section within its output section.
473 void
474 set_section_offset(unsigned int shndx, off_t off)
475 {
a3ad94ed 476 gold_assert(shndx < this->map_to_output_.size());
ead1e424
ILT
477 this->map_to_output_[shndx].offset = off;
478 }
479
730cdc88
ILT
480 // Return true if we need to wait for output sections to be written
481 // before we can apply relocations. This is true if the object has
482 // any relocations for sections which require special handling, such
483 // as the exception frame section.
484 bool
485 relocs_must_follow_section_writes()
486 { return this->relocs_must_follow_section_writes_; }
487
4625f782
ILT
488 // Return the object merge map.
489 Object_merge_map*
490 merge_map() const
491 { return this->object_merge_map_; }
492
493 // Set the object merge map.
494 void
495 set_merge_map(Object_merge_map* object_merge_map)
496 {
497 gold_assert(this->object_merge_map_ == NULL);
498 this->object_merge_map_ = object_merge_map;
499 }
500
a783673b 501 protected:
a2fb1b05
ILT
502 // What we need to know to map an input section to an output
503 // section. We keep an array of these, one for each input section,
504 // indexed by the input section number.
505 struct Map_to_output
506 {
507 // The output section. This is NULL if the input section is to be
508 // discarded.
509 Output_section* output_section;
b8e6aad9
ILT
510 // The offset within the output section. This is -1 if the
511 // section requires special handling.
a2fb1b05
ILT
512 off_t offset;
513 };
514
92e059d8
ILT
515 // Read the relocs--implemented by child class.
516 virtual void
517 do_read_relocs(Read_relocs_data*) = 0;
518
519 // Scan the relocs--implemented by child class.
520 virtual void
ead1e424
ILT
521 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
522 Read_relocs_data*) = 0;
92e059d8 523
75f65a3e 524 // Finalize local symbols--implemented by child class.
c06b7b0b 525 virtual unsigned int
b8e6aad9
ILT
526 do_finalize_local_symbols(unsigned int, off_t,
527 Stringpool_template<char>*) = 0;
75f65a3e 528
61ba1cf9
ILT
529 // Relocate the input sections and write out the local
530 // symbols--implemented by child class.
531 virtual void
532 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
533 const Layout*, Output_file* of) = 0;
534
a2fb1b05
ILT
535 // Return the vector mapping input sections to output sections.
536 std::vector<Map_to_output>&
537 map_to_output()
538 { return this->map_to_output_; }
539
b8e6aad9
ILT
540 const std::vector<Map_to_output>&
541 map_to_output() const
542 { return this->map_to_output_; }
543
730cdc88
ILT
544 // Record that we must wait for the output sections to be written
545 // before applying relocations.
546 void
547 set_relocs_must_follow_section_writes()
548 { this->relocs_must_follow_section_writes_ = true; }
549
bae7f79e 550 private:
a2fb1b05
ILT
551 // Mapping from input sections to output section.
552 std::vector<Map_to_output> map_to_output_;
4625f782
ILT
553 // Mappings for merge sections. This is managed by the code in the
554 // Merge_map class.
555 Object_merge_map* object_merge_map_;
730cdc88
ILT
556 // Whether we need to wait for output sections to be written before
557 // we can apply relocations.
558 bool relocs_must_follow_section_writes_;
bae7f79e
ILT
559};
560
a783673b
ILT
561// Implement Object::output_section inline for efficiency.
562inline Output_section*
b8e6aad9 563Relobj::output_section(unsigned int shndx, off_t* poff) const
a783673b 564{
5a6f7e2d
ILT
565 gold_assert(shndx < this->map_to_output_.size());
566 const Map_to_output& mo(this->map_to_output_[shndx]);
a783673b
ILT
567 *poff = mo.offset;
568 return mo.output_section;
569}
570
b8e6aad9
ILT
571// This POD class is holds the value of a symbol. This is used for
572// local symbols, and for all symbols during relocation processing.
730cdc88
ILT
573// For special sections, such as SHF_MERGE sections, this calls a
574// function to get the final symbol value.
b8e6aad9
ILT
575
576template<int size>
577class Symbol_value
578{
579 public:
580 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
581
582 Symbol_value()
063f12a8
ILT
583 : output_symtab_index_(0), input_shndx_(0), is_section_symbol_(false),
584 needs_output_address_(false), value_(0)
b8e6aad9
ILT
585 { }
586
587 // Get the value of this symbol. OBJECT is the object in which this
588 // symbol is defined, and ADDEND is an addend to add to the value.
589 template<bool big_endian>
590 Value
591 value(const Sized_relobj<size, big_endian>* object, Value addend) const
592 {
593 if (!this->needs_output_address_)
594 return this->value_ + addend;
063f12a8
ILT
595 return object->local_value(this->input_shndx_, this->value_,
596 this->is_section_symbol_, addend);
b8e6aad9
ILT
597 }
598
599 // Set the value of this symbol in the output symbol table.
600 void
601 set_output_value(Value value)
602 {
603 this->value_ = value;
604 this->needs_output_address_ = false;
605 }
606
607 // If this symbol is mapped to an output section which requires
608 // special handling to determine the output value, we store the
609 // value of the symbol in the input file. This is used for
610 // SHF_MERGE sections.
611 void
612 set_input_value(Value value)
613 {
614 this->value_ = value;
615 this->needs_output_address_ = true;
616 }
617
618 // Return whether this symbol should go into the output symbol
619 // table.
620 bool
621 needs_output_symtab_entry() const
622 {
623 gold_assert(this->output_symtab_index_ != 0);
624 return this->output_symtab_index_ != -1U;
625 }
626
627 // Return the index in the output symbol table.
628 unsigned int
629 output_symtab_index() const
630 {
631 gold_assert(this->output_symtab_index_ != 0);
632 return this->output_symtab_index_;
633 }
634
635 // Set the index in the output symbol table.
636 void
637 set_output_symtab_index(unsigned int i)
638 {
639 gold_assert(this->output_symtab_index_ == 0);
640 this->output_symtab_index_ = i;
641 }
642
643 // Record that this symbol should not go into the output symbol
644 // table.
645 void
646 set_no_output_symtab_entry()
647 {
648 gold_assert(this->output_symtab_index_ == 0);
649 this->output_symtab_index_ = -1U;
650 }
651
652 // Set the index of the input section in the input file.
653 void
654 set_input_shndx(unsigned int i)
730cdc88
ILT
655 {
656 this->input_shndx_ = i;
657 gold_assert(this->input_shndx_ == i);
658 }
b8e6aad9 659
063f12a8
ILT
660 // Record that this is a section symbol.
661 void
662 set_is_section_symbol()
663 { this->is_section_symbol_ = true; }
664
b8e6aad9
ILT
665 private:
666 // The index of this local symbol in the output symbol table. This
667 // will be -1 if the symbol should not go into the symbol table.
668 unsigned int output_symtab_index_;
669 // The section index in the input file in which this symbol is
670 // defined.
063f12a8
ILT
671 unsigned int input_shndx_ : 30;
672 // Whether this is a STT_SECTION symbol.
673 bool is_section_symbol_ : 1;
b8e6aad9
ILT
674 // Whether getting the value of this symbol requires calling an
675 // Output_section method. For example, this will be true of a
063f12a8 676 // symbol in a SHF_MERGE section.
b8e6aad9
ILT
677 bool needs_output_address_ : 1;
678 // The value of the symbol. If !needs_output_address_, this is the
679 // value in the output file. If needs_output_address_, this is the
680 // value in the input file.
681 Value value_;
682};
683
14bfc3f5 684// A regular object file. This is size and endian specific.
bae7f79e
ILT
685
686template<int size, bool big_endian>
f6ce93d6 687class Sized_relobj : public Relobj
bae7f79e
ILT
688{
689 public:
c06b7b0b 690 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
730cdc88 691 typedef std::vector<Symbol*> Symbols;
b8e6aad9 692 typedef std::vector<Symbol_value<size> > Local_values;
c06b7b0b 693
f6ce93d6 694 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
bae7f79e
ILT
695 const typename elfcpp::Ehdr<size, big_endian>&);
696
f6ce93d6 697 ~Sized_relobj();
bae7f79e 698
a2fb1b05 699 // Set up the object file based on the ELF header.
bae7f79e
ILT
700 void
701 setup(const typename elfcpp::Ehdr<size, big_endian>&);
702
730cdc88
ILT
703 // Return the number of local symbols.
704 unsigned int
705 local_symbol_count() const
706 { return this->local_symbol_count_; }
707
708 // If SYM is the index of a global symbol in the object file's
709 // symbol table, return the Symbol object. Otherwise, return NULL.
710 Symbol*
711 global_symbol(unsigned int sym) const
712 {
713 if (sym >= this->local_symbol_count_)
714 {
715 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
716 return this->symbols_[sym - this->local_symbol_count_];
717 }
718 return NULL;
719 }
720
721 // Return the section index of symbol SYM. Set *VALUE to its value
722 // in the object file. Note that for a symbol which is not defined
723 // in this object file, this will set *VALUE to 0 and return
724 // SHN_UNDEF; it will not return the final value of the symbol in
725 // the link.
726 unsigned int
727 symbol_section_and_value(unsigned int sym, Address* value);
728
729 // Return a pointer to the Symbol_value structure which holds the
730 // value of a local symbol.
731 const Symbol_value<size>*
732 local_symbol(unsigned int sym) const
733 {
734 gold_assert(sym < this->local_values_.size());
735 return &this->local_values_[sym];
736 }
737
c06b7b0b
ILT
738 // Return the index of local symbol SYM in the ordinary symbol
739 // table. A value of -1U means that the symbol is not being output.
740 unsigned int
741 symtab_index(unsigned int sym) const
742 {
b8e6aad9
ILT
743 gold_assert(sym < this->local_values_.size());
744 return this->local_values_[sym].output_symtab_index();
c06b7b0b
ILT
745 }
746
e727fa71
ILT
747 // Return the appropriate Sized_target structure.
748 Sized_target<size, big_endian>*
749 sized_target()
750 {
751 return this->Object::sized_target
752 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
753 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
754 }
755
756 // Return the value of the local symbol symndx.
757 Address
758 local_symbol_value(unsigned int symndx) const;
759
760 // Return the value of a local symbol defined in input section
761 // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
762 // indicates whether the symbol is a section symbol. This handles
763 // SHF_MERGE sections.
764 Address
765 local_value(unsigned int shndx, Address value, bool is_section_symbol,
766 Address addend) const;
767
768 // Return whether the local symbol SYMNDX has a GOT offset.
07f397ab 769 // For TLS symbols, the GOT entry will hold its tp-relative offset.
e727fa71
ILT
770 bool
771 local_has_got_offset(unsigned int symndx) const
772 {
773 return (this->local_got_offsets_.find(symndx)
774 != this->local_got_offsets_.end());
775 }
776
777 // Return the GOT offset of the local symbol SYMNDX.
778 unsigned int
779 local_got_offset(unsigned int symndx) const
780 {
781 Local_got_offsets::const_iterator p =
782 this->local_got_offsets_.find(symndx);
783 gold_assert(p != this->local_got_offsets_.end());
784 return p->second;
785 }
786
787 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
788 void
789 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
790 {
791 std::pair<Local_got_offsets::iterator, bool> ins =
792 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
793 gold_assert(ins.second);
794 }
795
07f397ab
ILT
796 // Return whether the local TLS symbol SYMNDX has a GOT offset.
797 // The GOT entry at this offset will contain a module index. If
798 // NEED_PAIR is true, a second entry immediately following the first
799 // will contain the dtv-relative offset.
800 bool
801 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
802 {
803 typename Local_tls_got_offsets::const_iterator p =
804 this->local_tls_got_offsets_.find(symndx);
805 if (p == this->local_tls_got_offsets_.end()
806 || (need_pair && !p->second.have_pair_))
807 return false;
808 return true;
809 }
810
811 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
812 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
813 // otherwise we need the offset of the GOT entry for the module index.
814 unsigned int
815 local_tls_got_offset(unsigned int symndx, bool need_pair) const
816 {
817 typename Local_tls_got_offsets::const_iterator p =
818 this->local_tls_got_offsets_.find(symndx);
819 gold_assert(p != this->local_tls_got_offsets_.end());
820 gold_assert(!need_pair || p->second.have_pair_);
821 return p->second.got_offset_;
822 }
823
824 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
825 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
826 // otherwise, we have just a single entry for the module index.
827 void
828 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
829 bool have_pair)
830 {
831 typename Local_tls_got_offsets::iterator p =
832 this->local_tls_got_offsets_.find(symndx);
833 if (p != this->local_tls_got_offsets_.end())
834 {
835 // An entry already existed for this symbol. This can happen
836 // if we see a relocation asking for the module index before
837 // a relocation asking for the pair. In that case, the original
838 // GOT entry will remain, but won't get used by any further
839 // relocations.
840 p->second.got_offset_ = got_offset;
841 gold_assert(have_pair);
842 p->second.have_pair_ = true;
843 }
844 else
845 {
846 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
847 this->local_tls_got_offsets_.insert(
848 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
849 gold_assert(ins.second);
850 }
851 }
852
f7e2ee48
ILT
853 // Return the name of the symbol that spans the given offset in the
854 // specified section in this object. This is used only for error
855 // messages and is not particularly efficient.
856 bool
857 get_symbol_location_info(unsigned int shndx, off_t offset,
858 Symbol_location_info* info);
859
a2fb1b05 860 // Read the symbols.
bae7f79e 861 void
12e14209 862 do_read_symbols(Read_symbols_data*);
14bfc3f5 863
dbe717ef
ILT
864 // Lay out the input sections.
865 void
7e1edb90 866 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
dbe717ef 867
12e14209
ILT
868 // Add the symbols to the symbol table.
869 void
870 do_add_symbols(Symbol_table*, Read_symbols_data*);
a2fb1b05 871
92e059d8
ILT
872 // Read the relocs.
873 void
874 do_read_relocs(Read_relocs_data*);
875
876 // Scan the relocs and adjust the symbol table.
877 void
ead1e424
ILT
878 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
879 Read_relocs_data*);
92e059d8 880
75f65a3e 881 // Finalize the local symbols.
c06b7b0b 882 unsigned int
b8e6aad9
ILT
883 do_finalize_local_symbols(unsigned int, off_t,
884 Stringpool_template<char>*);
75f65a3e 885
61ba1cf9
ILT
886 // Relocate the input sections and write out the local symbols.
887 void
888 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
889 const Layout*, Output_file* of);
890
891 // Get the name of a section.
892 std::string
645f8123
ILT
893 do_section_name(unsigned int shndx)
894 { return this->elf_file_.section_name(shndx); }
61ba1cf9 895
645f8123 896 // Return the location of the contents of a section.
dbe717ef 897 Object::Location
645f8123
ILT
898 do_section_contents(unsigned int shndx)
899 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 900
a3ad94ed
ILT
901 // Return section flags.
902 uint64_t
903 do_section_flags(unsigned int shndx)
904 { return this->elf_file_.section_flags(shndx); }
905
730cdc88
ILT
906 // Return section type.
907 unsigned int
908 do_section_type(unsigned int shndx)
909 { return this->elf_file_.section_type(shndx); }
910
f7e2ee48
ILT
911 // Return the section link field.
912 unsigned int
913 do_section_link(unsigned int shndx)
914 { return this->elf_file_.section_link(shndx); }
915
4c50553d
ILT
916 // Return the section info field.
917 unsigned int
918 do_section_info(unsigned int shndx)
919 { return this->elf_file_.section_info(shndx); }
920
bae7f79e 921 private:
a2fb1b05 922 // For convenience.
f6ce93d6 923 typedef Sized_relobj<size, big_endian> This;
a2fb1b05
ILT
924 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
925 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
926 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e
ILT
927 typedef elfcpp::Shdr<size, big_endian> Shdr;
928
dbe717ef
ILT
929 // Find the SHT_SYMTAB section, given the section headers.
930 void
931 find_symtab(const unsigned char* pshdrs);
932
730cdc88
ILT
933 // Return whether SHDR has the right flags for a GNU style exception
934 // frame section.
935 bool
936 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
937
938 // Return whether there is a section named .eh_frame which might be
939 // a GNU style exception frame section.
940 bool
941 find_eh_frame(const unsigned char* pshdrs, const char* names,
942 off_t names_size) const;
943
a2fb1b05
ILT
944 // Whether to include a section group in the link.
945 bool
946 include_section_group(Layout*, unsigned int,
947 const elfcpp::Shdr<size, big_endian>&,
948 std::vector<bool>*);
949
950 // Whether to include a linkonce section in the link.
951 bool
952 include_linkonce_section(Layout*, const char*,
953 const elfcpp::Shdr<size, big_endian>&);
954
61ba1cf9
ILT
955 // Views and sizes when relocating.
956 struct View_size
957 {
958 unsigned char* view;
959 typename elfcpp::Elf_types<size>::Elf_Addr address;
960 off_t offset;
961 off_t view_size;
730cdc88 962 bool is_input_output_view;
61ba1cf9
ILT
963 };
964
965 typedef std::vector<View_size> Views;
966
967 // Write section data to the output file. Record the views and
968 // sizes in VIEWS for use when relocating.
969 void
970 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
971
972 // Relocate the sections in the output file.
973 void
92e059d8
ILT
974 relocate_sections(const General_options& options, const Symbol_table*,
975 const Layout*, const unsigned char* pshdrs, Views*);
61ba1cf9
ILT
976
977 // Write out the local symbols.
978 void
b8e6aad9
ILT
979 write_local_symbols(Output_file*,
980 const Stringpool_template<char>*);
61ba1cf9 981
07f397ab
ILT
982 // The GOT offsets of local symbols. This map also stores GOT offsets
983 // for tp-relative offsets for TLS symbols.
e727fa71
ILT
984 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
985
07f397ab
ILT
986 // The TLS GOT offsets of local symbols. The map stores the offsets
987 // for either a single GOT entry that holds the module index of a TLS
988 // symbol, or a pair of GOT entries containing the module index and
989 // dtv-relative offset.
990 struct Tls_got_entry
991 {
992 Tls_got_entry(int got_offset, bool have_pair)
993 : got_offset_(got_offset),
994 have_pair_(have_pair)
995 { }
996 int got_offset_;
997 bool have_pair_;
998 };
999 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1000
645f8123
ILT
1001 // General access to the ELF file.
1002 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
bae7f79e 1003 // Index of SHT_SYMTAB section.
645f8123 1004 unsigned int symtab_shndx_;
61ba1cf9
ILT
1005 // The number of local symbols.
1006 unsigned int local_symbol_count_;
1007 // The number of local symbols which go into the output file.
1008 unsigned int output_local_symbol_count_;
14bfc3f5 1009 // The entries in the symbol table for the external symbols.
730cdc88 1010 Symbols symbols_;
75f65a3e
ILT
1011 // File offset for local symbols.
1012 off_t local_symbol_offset_;
61ba1cf9 1013 // Values of local symbols.
c06b7b0b 1014 Local_values local_values_;
07f397ab
ILT
1015 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1016 // for TLS symbols, indexed by symbol number.
e727fa71 1017 Local_got_offsets local_got_offsets_;
07f397ab
ILT
1018 // GOT offsets for local TLS symbols, indexed by symbol number
1019 // and GOT entry type.
1020 Local_tls_got_offsets local_tls_got_offsets_;
730cdc88
ILT
1021 // Whether this object has a GNU style .eh_frame section.
1022 bool has_eh_frame_;
bae7f79e
ILT
1023};
1024
54dc6425 1025// A class to manage the list of all objects.
a2fb1b05 1026
54dc6425
ILT
1027class Input_objects
1028{
1029 public:
1030 Input_objects()
9a2d6984
ILT
1031 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1032 system_library_directory_()
54dc6425
ILT
1033 { }
1034
f6ce93d6
ILT
1035 // The type of the list of input relocateable objects.
1036 typedef std::vector<Relobj*> Relobj_list;
1037 typedef Relobj_list::const_iterator Relobj_iterator;
1038
1039 // The type of the list of input dynamic objects.
1040 typedef std::vector<Dynobj*> Dynobj_list;
1041 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425 1042
008db82e
ILT
1043 // Add an object to the list. Return true if all is well, or false
1044 // if this object should be ignored.
1045 bool
54dc6425
ILT
1046 add_object(Object*);
1047
75f65a3e
ILT
1048 // Get the target we should use for the output file.
1049 Target*
1050 target() const
1051 { return this->target_; }
1052
e2827e5f
ILT
1053 // For each dynamic object, check whether we've seen all of its
1054 // explicit dependencies.
1055 void
1056 check_dynamic_dependencies() const;
1057
9a2d6984
ILT
1058 // Return whether an object was found in the system library
1059 // directory.
1060 bool
1061 found_in_system_library_directory(const Object*) const;
1062
f6ce93d6
ILT
1063 // Iterate over all regular objects.
1064
1065 Relobj_iterator
1066 relobj_begin() const
1067 { return this->relobj_list_.begin(); }
1068
1069 Relobj_iterator
1070 relobj_end() const
1071 { return this->relobj_list_.end(); }
1072
1073 // Iterate over all dynamic objects.
1074
1075 Dynobj_iterator
1076 dynobj_begin() const
1077 { return this->dynobj_list_.begin(); }
54dc6425 1078
f6ce93d6
ILT
1079 Dynobj_iterator
1080 dynobj_end() const
1081 { return this->dynobj_list_.end(); }
54dc6425
ILT
1082
1083 // Return whether we have seen any dynamic objects.
1084 bool
1085 any_dynamic() const
f6ce93d6 1086 { return !this->dynobj_list_.empty(); }
54dc6425 1087
fe9a4c12
ILT
1088 // Return the number of input objects.
1089 int
1090 number_of_input_objects() const
1091 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1092
54dc6425
ILT
1093 private:
1094 Input_objects(const Input_objects&);
1095 Input_objects& operator=(const Input_objects&);
1096
008db82e 1097 // The list of ordinary objects included in the link.
f6ce93d6 1098 Relobj_list relobj_list_;
008db82e 1099 // The list of dynamic objects included in the link.
f6ce93d6 1100 Dynobj_list dynobj_list_;
008db82e 1101 // The target.
75f65a3e 1102 Target* target_;
008db82e
ILT
1103 // SONAMEs that we have seen.
1104 Unordered_set<std::string> sonames_;
9a2d6984
ILT
1105 // The directory in which we find the libc.so.
1106 std::string system_library_directory_;
54dc6425 1107};
a2fb1b05 1108
92e059d8
ILT
1109// Some of the information we pass to the relocation routines. We
1110// group this together to avoid passing a dozen different arguments.
1111
1112template<int size, bool big_endian>
1113struct Relocate_info
1114{
1115 // Command line options.
1116 const General_options* options;
1117 // Symbol table.
1118 const Symbol_table* symtab;
1119 // Layout.
1120 const Layout* layout;
1121 // Object being relocated.
f6ce93d6 1122 Sized_relobj<size, big_endian>* object;
92e059d8
ILT
1123 // Section index of relocation section.
1124 unsigned int reloc_shndx;
1125 // Section index of section being relocated.
1126 unsigned int data_shndx;
1127
1128 // Return a string showing the location of a relocation. This is
1129 // only used for error messages.
1130 std::string
1131 location(size_t relnum, off_t reloffset) const;
1132};
1133
bae7f79e
ILT
1134// Return an Object appropriate for the input file. P is BYTES long,
1135// and holds the ELF header.
1136
61ba1cf9
ILT
1137extern Object*
1138make_elf_object(const std::string& name, Input_file*,
1139 off_t offset, const unsigned char* p,
1140 off_t bytes);
bae7f79e
ILT
1141
1142} // end namespace gold
1143
1144#endif // !defined(GOLD_OBJECT_H)