Add const to Object::read and Object::sized_target.
[deliverable/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()
7004837e 169 { this->input_file()->file().lock(); }
a2fb1b05
ILT
170
171 // Unlock the underlying file.
172 void
173 unlock()
7004837e 174 { this->input_file()->file().unlock(); }
a2fb1b05
ILT
175
176 // Return whether the underlying file is locked.
177 bool
178 is_locked() const
7004837e 179 { return this->input_file()->file().is_locked(); }
a2fb1b05 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>*
7004837e 186 sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const;
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*
7004837e
ILT
327 input_file()
328 { return this->input_file_; }
329
330 const Input_file*
f6ce93d6
ILT
331 input_file() const
332 { return this->input_file_; }
333
f6ce93d6
ILT
334 // Get a view into the underlying file.
335 const unsigned char*
9eb9fa57
ILT
336 get_view(off_t start, off_t size, bool cache)
337 {
7004837e
ILT
338 return this->input_file()->file().get_view(start + this->offset_, size,
339 cache);
9eb9fa57 340 }
f6ce93d6
ILT
341
342 // Get a lasting view into the underlying file.
343 File_view*
9eb9fa57 344 get_lasting_view(off_t start, off_t size, bool cache)
f6ce93d6 345 {
7004837e
ILT
346 return this->input_file()->file().get_lasting_view(start + this->offset_,
347 size, cache);
f6ce93d6
ILT
348 }
349
350 // Read data from the underlying file.
351 void
7004837e
ILT
352 read(off_t start, off_t size, void* p) const
353 { this->input_file()->file().read(start + this->offset_, size, p); }
f6ce93d6
ILT
354
355 // Set the target.
356 void
dbe717ef
ILT
357 set_target(int machine, int size, bool big_endian, int osabi,
358 int abiversion);
359
dbe717ef
ILT
360 // Set the number of sections.
361 void
362 set_shnum(int shnum)
363 { this->shnum_ = shnum; }
364
365 // Functions used by both Sized_relobj and Sized_dynobj.
366
367 // Read the section data into a Read_symbols_data object.
368 template<int size, bool big_endian>
369 void
370 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
371 Read_symbols_data*);
372
373 // If NAME is the name of a special .gnu.warning section, arrange
374 // for the warning to be issued. SHNDX is the section index.
375 // Return whether it is a warning section.
376 bool
377 handle_gnu_warning_section(const char* name, unsigned int shndx,
378 Symbol_table*);
f6ce93d6
ILT
379
380 private:
381 // This class may not be copied.
382 Object(const Object&);
383 Object& operator=(const Object&);
384
385 // Name of object as printed to user.
386 std::string name_;
387 // For reading the file.
388 Input_file* input_file_;
389 // Offset within the file--0 for an object file, non-0 for an
390 // archive.
391 off_t offset_;
dbe717ef
ILT
392 // Number of input sections.
393 unsigned int shnum_;
f6ce93d6
ILT
394 // Whether this is a dynamic object.
395 bool is_dynamic_;
396 // Target functions--may be NULL if the target is not known.
397 Target* target_;
398};
399
400// Implement sized_target inline for efficiency. This approach breaks
401// static type checking, but is made safe using asserts.
402
403template<int size, bool big_endian>
404inline Sized_target<size, big_endian>*
7004837e 405Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const
f6ce93d6 406{
a3ad94ed
ILT
407 gold_assert(this->target_->get_size() == size);
408 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
f6ce93d6
ILT
409 return static_cast<Sized_target<size, big_endian>*>(this->target_);
410}
411
412// A regular object (ET_REL). This is an abstract base class itself.
b8e6aad9 413// The implementation is the template class Sized_relobj.
f6ce93d6
ILT
414
415class Relobj : public Object
416{
417 public:
418 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
4625f782
ILT
419 : Object(name, input_file, false, offset),
420 map_to_output_(),
421 object_merge_map_(NULL),
422 relocs_must_follow_section_writes_(false)
f6ce93d6
ILT
423 { }
424
92e059d8 425 // Read the relocs.
a2fb1b05 426 void
92e059d8
ILT
427 read_relocs(Read_relocs_data* rd)
428 { return this->do_read_relocs(rd); }
429
430 // Scan the relocs and adjust the symbol table.
431 void
432 scan_relocs(const General_options& options, Symbol_table* symtab,
ead1e424
ILT
433 Layout* layout, Read_relocs_data* rd)
434 { return this->do_scan_relocs(options, symtab, layout, rd); }
a2fb1b05 435
6d013333
ILT
436 // The number of local symbols in the input symbol table.
437 virtual unsigned int
438 local_symbol_count() const
439 { return this->do_local_symbol_count(); }
440
7bf1f802
ILT
441 // Initial local symbol processing: count the number of local symbols
442 // in the output symbol table and dynamic symbol table; add local symbol
443 // names to *POOL and *DYNPOOL.
444 void
445 count_local_symbols(Stringpool_template<char>* pool,
446 Stringpool_template<char>* dynpool)
447 { return this->do_count_local_symbols(pool, dynpool); }
448
449 // Set the values of the local symbols, set the output symbol table
450 // indexes for the local variables, and set the offset where local
451 // symbol information will be stored. Returns the new local symbol index.
452 unsigned int
453 finalize_local_symbols(unsigned int index, off_t off)
454 { return this->do_finalize_local_symbols(index, off); }
455
456 // Set the output dynamic symbol table indexes for the local variables.
c06b7b0b 457 unsigned int
7bf1f802
ILT
458 set_local_dynsym_indexes(unsigned int index)
459 { return this->do_set_local_dynsym_indexes(index); }
460
461 // Set the offset where local dynamic symbol information will be stored.
462 unsigned int
463 set_local_dynsym_offset(off_t off)
464 { return this->do_set_local_dynsym_offset(off); }
75f65a3e 465
61ba1cf9
ILT
466 // Relocate the input sections and write out the local symbols.
467 void
468 relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
469 const Layout* layout, Output_file* of)
470 { return this->do_relocate(options, symtab, layout, of); }
61ba1cf9 471
a783673b
ILT
472 // Return whether an input section is being included in the link.
473 bool
5a6f7e2d 474 is_section_included(unsigned int shndx) const
a783673b 475 {
5a6f7e2d
ILT
476 gold_assert(shndx < this->map_to_output_.size());
477 return this->map_to_output_[shndx].output_section != NULL;
a783673b
ILT
478 }
479
730cdc88
ILT
480 // Return whether an input section requires special
481 // handling--whether it is not simply mapped from the input file to
482 // the output file.
483 bool
484 is_section_specially_mapped(unsigned int shndx) const
485 {
486 gold_assert(shndx < this->map_to_output_.size());
487 return (this->map_to_output_[shndx].output_section != NULL
488 && this->map_to_output_[shndx].offset == -1);
489 }
490
a783673b
ILT
491 // Given a section index, return the corresponding Output_section
492 // (which will be NULL if the section is not included in the link)
730cdc88
ILT
493 // and set *POFF to the offset within that section. *POFF will be
494 // set to -1 if the section requires special handling.
a783673b 495 inline Output_section*
b8e6aad9 496 output_section(unsigned int shndx, off_t* poff) const;
a783673b 497
ead1e424
ILT
498 // Set the offset of an input section within its output section.
499 void
500 set_section_offset(unsigned int shndx, off_t off)
501 {
a3ad94ed 502 gold_assert(shndx < this->map_to_output_.size());
ead1e424
ILT
503 this->map_to_output_[shndx].offset = off;
504 }
505
730cdc88
ILT
506 // Return true if we need to wait for output sections to be written
507 // before we can apply relocations. This is true if the object has
508 // any relocations for sections which require special handling, such
509 // as the exception frame section.
510 bool
511 relocs_must_follow_section_writes()
512 { return this->relocs_must_follow_section_writes_; }
513
4625f782
ILT
514 // Return the object merge map.
515 Object_merge_map*
516 merge_map() const
517 { return this->object_merge_map_; }
518
519 // Set the object merge map.
520 void
521 set_merge_map(Object_merge_map* object_merge_map)
522 {
523 gold_assert(this->object_merge_map_ == NULL);
524 this->object_merge_map_ = object_merge_map;
525 }
526
a783673b 527 protected:
a2fb1b05
ILT
528 // What we need to know to map an input section to an output
529 // section. We keep an array of these, one for each input section,
530 // indexed by the input section number.
531 struct Map_to_output
532 {
533 // The output section. This is NULL if the input section is to be
534 // discarded.
535 Output_section* output_section;
b8e6aad9
ILT
536 // The offset within the output section. This is -1 if the
537 // section requires special handling.
a2fb1b05
ILT
538 off_t offset;
539 };
540
92e059d8
ILT
541 // Read the relocs--implemented by child class.
542 virtual void
543 do_read_relocs(Read_relocs_data*) = 0;
544
545 // Scan the relocs--implemented by child class.
546 virtual void
ead1e424
ILT
547 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
548 Read_relocs_data*) = 0;
92e059d8 549
6d013333
ILT
550 // Return the number of local symbols--implemented by child class.
551 virtual unsigned int
552 do_local_symbol_count() const = 0;
553
7bf1f802
ILT
554 // Count local symbols--implemented by child class.
555 virtual void
556 do_count_local_symbols(Stringpool_template<char>*,
b8e6aad9 557 Stringpool_template<char>*) = 0;
75f65a3e 558
7bf1f802
ILT
559 // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the
560 // offset where local symbol information will be stored.
561 virtual unsigned int
562 do_finalize_local_symbols(unsigned int, off_t) = 0;
563
564 // Set the output dynamic symbol table indexes for the local variables.
565 virtual unsigned int
566 do_set_local_dynsym_indexes(unsigned int) = 0;
567
568 // Set the offset where local dynamic symbol information will be stored.
569 virtual unsigned int
570 do_set_local_dynsym_offset(off_t) = 0;
571
61ba1cf9
ILT
572 // Relocate the input sections and write out the local
573 // symbols--implemented by child class.
574 virtual void
575 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
576 const Layout*, Output_file* of) = 0;
577
a2fb1b05
ILT
578 // Return the vector mapping input sections to output sections.
579 std::vector<Map_to_output>&
580 map_to_output()
581 { return this->map_to_output_; }
582
b8e6aad9
ILT
583 const std::vector<Map_to_output>&
584 map_to_output() const
585 { return this->map_to_output_; }
586
730cdc88
ILT
587 // Record that we must wait for the output sections to be written
588 // before applying relocations.
589 void
590 set_relocs_must_follow_section_writes()
591 { this->relocs_must_follow_section_writes_ = true; }
592
bae7f79e 593 private:
a2fb1b05
ILT
594 // Mapping from input sections to output section.
595 std::vector<Map_to_output> map_to_output_;
4625f782
ILT
596 // Mappings for merge sections. This is managed by the code in the
597 // Merge_map class.
598 Object_merge_map* object_merge_map_;
730cdc88
ILT
599 // Whether we need to wait for output sections to be written before
600 // we can apply relocations.
601 bool relocs_must_follow_section_writes_;
bae7f79e
ILT
602};
603
a783673b
ILT
604// Implement Object::output_section inline for efficiency.
605inline Output_section*
b8e6aad9 606Relobj::output_section(unsigned int shndx, off_t* poff) const
a783673b 607{
5a6f7e2d
ILT
608 gold_assert(shndx < this->map_to_output_.size());
609 const Map_to_output& mo(this->map_to_output_[shndx]);
a783673b
ILT
610 *poff = mo.offset;
611 return mo.output_section;
612}
613
b8e6aad9
ILT
614// This POD class is holds the value of a symbol. This is used for
615// local symbols, and for all symbols during relocation processing.
730cdc88
ILT
616// For special sections, such as SHF_MERGE sections, this calls a
617// function to get the final symbol value.
b8e6aad9
ILT
618
619template<int size>
620class Symbol_value
621{
622 public:
623 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
624
625 Symbol_value()
7bf1f802
ILT
626 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
627 is_section_symbol_(false), is_tls_symbol_(false),
063f12a8 628 needs_output_address_(false), value_(0)
b8e6aad9
ILT
629 { }
630
631 // Get the value of this symbol. OBJECT is the object in which this
632 // symbol is defined, and ADDEND is an addend to add to the value.
633 template<bool big_endian>
634 Value
635 value(const Sized_relobj<size, big_endian>* object, Value addend) const
636 {
637 if (!this->needs_output_address_)
638 return this->value_ + addend;
063f12a8
ILT
639 return object->local_value(this->input_shndx_, this->value_,
640 this->is_section_symbol_, addend);
b8e6aad9
ILT
641 }
642
643 // Set the value of this symbol in the output symbol table.
644 void
645 set_output_value(Value value)
646 {
647 this->value_ = value;
648 this->needs_output_address_ = false;
649 }
650
7bf1f802
ILT
651 // Set the value of the symbol from the input file. This value
652 // will usually be replaced during finalization with the output
653 // value, but if the symbol is mapped to an output section which
654 // requires special handling to determine the output value, we
655 // leave the input value in place until later. This is used for
b8e6aad9
ILT
656 // SHF_MERGE sections.
657 void
658 set_input_value(Value value)
659 {
660 this->value_ = value;
661 this->needs_output_address_ = true;
662 }
663
7bf1f802
ILT
664 // Return the input value.
665 Value
666 input_value() const
667 {
668 gold_assert(this->needs_output_address_);
669 return this->value_;
670 }
671
b8e6aad9
ILT
672 // Return whether this symbol should go into the output symbol
673 // table.
674 bool
675 needs_output_symtab_entry() const
ac1f0c21 676 { return this->output_symtab_index_ != -1U; }
b8e6aad9
ILT
677
678 // Return the index in the output symbol table.
679 unsigned int
680 output_symtab_index() const
681 {
682 gold_assert(this->output_symtab_index_ != 0);
683 return this->output_symtab_index_;
684 }
685
686 // Set the index in the output symbol table.
687 void
688 set_output_symtab_index(unsigned int i)
689 {
690 gold_assert(this->output_symtab_index_ == 0);
691 this->output_symtab_index_ = i;
692 }
693
694 // Record that this symbol should not go into the output symbol
695 // table.
696 void
697 set_no_output_symtab_entry()
698 {
699 gold_assert(this->output_symtab_index_ == 0);
700 this->output_symtab_index_ = -1U;
701 }
702
7bf1f802
ILT
703 // Set the index in the output dynamic symbol table.
704 void
705 set_needs_output_dynsym_entry()
706 {
707 this->output_dynsym_index_ = 0;
708 }
709
710 // Return whether this symbol should go into the output symbol
711 // table.
712 bool
713 needs_output_dynsym_entry() const
714 {
715 return this->output_dynsym_index_ != -1U;
716 }
717
718 // Record that this symbol should go into the dynamic symbol table.
719 void
720 set_output_dynsym_index(unsigned int i)
721 {
722 gold_assert(this->output_dynsym_index_ == 0);
723 this->output_dynsym_index_ = i;
724 }
725
726 // Return the index in the output dynamic symbol table.
727 unsigned int
728 output_dynsym_index() const
729 {
730 gold_assert(this->output_dynsym_index_ != 0);
731 return this->output_dynsym_index_;
732 }
733
b8e6aad9
ILT
734 // Set the index of the input section in the input file.
735 void
736 set_input_shndx(unsigned int i)
730cdc88
ILT
737 {
738 this->input_shndx_ = i;
ac1f0c21
ILT
739 // input_shndx_ field is a bitfield, so make sure that the value
740 // fits.
730cdc88
ILT
741 gold_assert(this->input_shndx_ == i);
742 }
b8e6aad9 743
7bf1f802
ILT
744 // Return the index of the input section in the input file.
745 unsigned int
746 input_shndx() const
747 { return this->input_shndx_; }
748
063f12a8
ILT
749 // Record that this is a section symbol.
750 void
751 set_is_section_symbol()
752 { this->is_section_symbol_ = true; }
753
7bf1f802
ILT
754 // Record that this is a TLS symbol.
755 void
756 set_is_tls_symbol()
757 { this->is_tls_symbol_ = true; }
758
759 // Return TRUE if this is a TLS symbol.
760 bool
761 is_tls_symbol() const
762 { return this->is_tls_symbol_; }
763
b8e6aad9
ILT
764 private:
765 // The index of this local symbol in the output symbol table. This
766 // will be -1 if the symbol should not go into the symbol table.
767 unsigned int output_symtab_index_;
7bf1f802
ILT
768 // The index of this local symbol in the dynamic symbol table. This
769 // will be -1 if the symbol should not go into the symbol table.
770 unsigned int output_dynsym_index_;
b8e6aad9
ILT
771 // The section index in the input file in which this symbol is
772 // defined.
7bf1f802 773 unsigned int input_shndx_ : 29;
063f12a8
ILT
774 // Whether this is a STT_SECTION symbol.
775 bool is_section_symbol_ : 1;
7bf1f802
ILT
776 // Whether this is a STT_TLS symbol.
777 bool is_tls_symbol_ : 1;
b8e6aad9
ILT
778 // Whether getting the value of this symbol requires calling an
779 // Output_section method. For example, this will be true of a
063f12a8 780 // symbol in a SHF_MERGE section.
b8e6aad9
ILT
781 bool needs_output_address_ : 1;
782 // The value of the symbol. If !needs_output_address_, this is the
783 // value in the output file. If needs_output_address_, this is the
784 // value in the input file.
785 Value value_;
786};
787
14bfc3f5 788// A regular object file. This is size and endian specific.
bae7f79e
ILT
789
790template<int size, bool big_endian>
f6ce93d6 791class Sized_relobj : public Relobj
bae7f79e
ILT
792{
793 public:
c06b7b0b 794 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
730cdc88 795 typedef std::vector<Symbol*> Symbols;
b8e6aad9 796 typedef std::vector<Symbol_value<size> > Local_values;
c06b7b0b 797
f6ce93d6 798 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
bae7f79e
ILT
799 const typename elfcpp::Ehdr<size, big_endian>&);
800
f6ce93d6 801 ~Sized_relobj();
bae7f79e 802
a2fb1b05 803 // Set up the object file based on the ELF header.
bae7f79e
ILT
804 void
805 setup(const typename elfcpp::Ehdr<size, big_endian>&);
806
730cdc88
ILT
807 // If SYM is the index of a global symbol in the object file's
808 // symbol table, return the Symbol object. Otherwise, return NULL.
809 Symbol*
810 global_symbol(unsigned int sym) const
811 {
812 if (sym >= this->local_symbol_count_)
813 {
814 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
815 return this->symbols_[sym - this->local_symbol_count_];
816 }
817 return NULL;
818 }
819
820 // Return the section index of symbol SYM. Set *VALUE to its value
821 // in the object file. Note that for a symbol which is not defined
822 // in this object file, this will set *VALUE to 0 and return
823 // SHN_UNDEF; it will not return the final value of the symbol in
824 // the link.
825 unsigned int
826 symbol_section_and_value(unsigned int sym, Address* value);
827
828 // Return a pointer to the Symbol_value structure which holds the
829 // value of a local symbol.
830 const Symbol_value<size>*
831 local_symbol(unsigned int sym) const
832 {
833 gold_assert(sym < this->local_values_.size());
834 return &this->local_values_[sym];
835 }
836
c06b7b0b
ILT
837 // Return the index of local symbol SYM in the ordinary symbol
838 // table. A value of -1U means that the symbol is not being output.
839 unsigned int
840 symtab_index(unsigned int sym) const
841 {
b8e6aad9
ILT
842 gold_assert(sym < this->local_values_.size());
843 return this->local_values_[sym].output_symtab_index();
c06b7b0b
ILT
844 }
845
7bf1f802
ILT
846 // Return the index of local symbol SYM in the dynamic symbol
847 // table. A value of -1U means that the symbol is not being output.
848 unsigned int
849 dynsym_index(unsigned int sym) const
850 {
851 gold_assert(sym < this->local_values_.size());
852 return this->local_values_[sym].output_dynsym_index();
853 }
854
e727fa71
ILT
855 // Return the appropriate Sized_target structure.
856 Sized_target<size, big_endian>*
857 sized_target()
858 {
859 return this->Object::sized_target
860 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
861 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
862 }
863
864 // Return the value of the local symbol symndx.
865 Address
866 local_symbol_value(unsigned int symndx) const;
867
868 // Return the value of a local symbol defined in input section
869 // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
870 // indicates whether the symbol is a section symbol. This handles
871 // SHF_MERGE sections.
872 Address
873 local_value(unsigned int shndx, Address value, bool is_section_symbol,
874 Address addend) const;
875
7bf1f802
ILT
876 void
877 set_needs_output_dynsym_entry(unsigned int sym)
878 {
879 gold_assert(sym < this->local_values_.size());
880 this->local_values_[sym].set_needs_output_dynsym_entry();
881 }
882
e727fa71 883 // Return whether the local symbol SYMNDX has a GOT offset.
07f397ab 884 // For TLS symbols, the GOT entry will hold its tp-relative offset.
e727fa71
ILT
885 bool
886 local_has_got_offset(unsigned int symndx) const
887 {
888 return (this->local_got_offsets_.find(symndx)
889 != this->local_got_offsets_.end());
890 }
891
892 // Return the GOT offset of the local symbol SYMNDX.
893 unsigned int
894 local_got_offset(unsigned int symndx) const
895 {
896 Local_got_offsets::const_iterator p =
897 this->local_got_offsets_.find(symndx);
898 gold_assert(p != this->local_got_offsets_.end());
899 return p->second;
900 }
901
902 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
903 void
904 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
905 {
906 std::pair<Local_got_offsets::iterator, bool> ins =
907 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
908 gold_assert(ins.second);
909 }
910
07f397ab
ILT
911 // Return whether the local TLS symbol SYMNDX has a GOT offset.
912 // The GOT entry at this offset will contain a module index. If
913 // NEED_PAIR is true, a second entry immediately following the first
914 // will contain the dtv-relative offset.
915 bool
916 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
917 {
918 typename Local_tls_got_offsets::const_iterator p =
919 this->local_tls_got_offsets_.find(symndx);
920 if (p == this->local_tls_got_offsets_.end()
921 || (need_pair && !p->second.have_pair_))
922 return false;
923 return true;
924 }
925
926 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
927 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
928 // otherwise we need the offset of the GOT entry for the module index.
929 unsigned int
930 local_tls_got_offset(unsigned int symndx, bool need_pair) const
931 {
932 typename Local_tls_got_offsets::const_iterator p =
933 this->local_tls_got_offsets_.find(symndx);
934 gold_assert(p != this->local_tls_got_offsets_.end());
935 gold_assert(!need_pair || p->second.have_pair_);
936 return p->second.got_offset_;
937 }
938
939 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
940 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
941 // otherwise, we have just a single entry for the module index.
942 void
943 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
944 bool have_pair)
945 {
946 typename Local_tls_got_offsets::iterator p =
947 this->local_tls_got_offsets_.find(symndx);
948 if (p != this->local_tls_got_offsets_.end())
949 {
950 // An entry already existed for this symbol. This can happen
951 // if we see a relocation asking for the module index before
952 // a relocation asking for the pair. In that case, the original
953 // GOT entry will remain, but won't get used by any further
954 // relocations.
955 p->second.got_offset_ = got_offset;
956 gold_assert(have_pair);
957 p->second.have_pair_ = true;
958 }
959 else
960 {
961 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
962 this->local_tls_got_offsets_.insert(
963 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
964 gold_assert(ins.second);
965 }
966 }
967
f7e2ee48
ILT
968 // Return the name of the symbol that spans the given offset in the
969 // specified section in this object. This is used only for error
970 // messages and is not particularly efficient.
971 bool
972 get_symbol_location_info(unsigned int shndx, off_t offset,
973 Symbol_location_info* info);
974
6d013333 975 protected:
a2fb1b05 976 // Read the symbols.
bae7f79e 977 void
12e14209 978 do_read_symbols(Read_symbols_data*);
14bfc3f5 979
6d013333
ILT
980 // Return the number of local symbols.
981 unsigned int
982 do_local_symbol_count() const
983 { return this->local_symbol_count_; }
984
dbe717ef
ILT
985 // Lay out the input sections.
986 void
7e1edb90 987 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
dbe717ef 988
12e14209
ILT
989 // Add the symbols to the symbol table.
990 void
991 do_add_symbols(Symbol_table*, Read_symbols_data*);
a2fb1b05 992
92e059d8
ILT
993 // Read the relocs.
994 void
995 do_read_relocs(Read_relocs_data*);
996
997 // Scan the relocs and adjust the symbol table.
998 void
ead1e424
ILT
999 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1000 Read_relocs_data*);
92e059d8 1001
7bf1f802
ILT
1002 // Count the local symbols.
1003 void
1004 do_count_local_symbols(Stringpool_template<char>*,
1005 Stringpool_template<char>*);
1006
75f65a3e 1007 // Finalize the local symbols.
c06b7b0b 1008 unsigned int
7bf1f802
ILT
1009 do_finalize_local_symbols(unsigned int, off_t);
1010
1011 // Set the offset where local dynamic symbol information will be stored.
1012 unsigned int
1013 do_set_local_dynsym_indexes(unsigned int);
1014
1015 // Set the offset where local dynamic symbol information will be stored.
1016 unsigned int
1017 do_set_local_dynsym_offset(off_t);
75f65a3e 1018
61ba1cf9
ILT
1019 // Relocate the input sections and write out the local symbols.
1020 void
1021 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
1022 const Layout*, Output_file* of);
1023
1024 // Get the name of a section.
1025 std::string
645f8123
ILT
1026 do_section_name(unsigned int shndx)
1027 { return this->elf_file_.section_name(shndx); }
61ba1cf9 1028
645f8123 1029 // Return the location of the contents of a section.
dbe717ef 1030 Object::Location
645f8123
ILT
1031 do_section_contents(unsigned int shndx)
1032 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 1033
a3ad94ed
ILT
1034 // Return section flags.
1035 uint64_t
1036 do_section_flags(unsigned int shndx)
1037 { return this->elf_file_.section_flags(shndx); }
1038
730cdc88
ILT
1039 // Return section type.
1040 unsigned int
1041 do_section_type(unsigned int shndx)
1042 { return this->elf_file_.section_type(shndx); }
1043
f7e2ee48
ILT
1044 // Return the section link field.
1045 unsigned int
1046 do_section_link(unsigned int shndx)
1047 { return this->elf_file_.section_link(shndx); }
1048
4c50553d
ILT
1049 // Return the section info field.
1050 unsigned int
1051 do_section_info(unsigned int shndx)
1052 { return this->elf_file_.section_info(shndx); }
1053
bae7f79e 1054 private:
a2fb1b05 1055 // For convenience.
f6ce93d6 1056 typedef Sized_relobj<size, big_endian> This;
a2fb1b05
ILT
1057 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1058 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1059 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e
ILT
1060 typedef elfcpp::Shdr<size, big_endian> Shdr;
1061
dbe717ef
ILT
1062 // Find the SHT_SYMTAB section, given the section headers.
1063 void
1064 find_symtab(const unsigned char* pshdrs);
1065
730cdc88
ILT
1066 // Return whether SHDR has the right flags for a GNU style exception
1067 // frame section.
1068 bool
1069 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1070
1071 // Return whether there is a section named .eh_frame which might be
1072 // a GNU style exception frame section.
1073 bool
1074 find_eh_frame(const unsigned char* pshdrs, const char* names,
1075 off_t names_size) const;
1076
a2fb1b05
ILT
1077 // Whether to include a section group in the link.
1078 bool
1079 include_section_group(Layout*, unsigned int,
1080 const elfcpp::Shdr<size, big_endian>&,
1081 std::vector<bool>*);
1082
1083 // Whether to include a linkonce section in the link.
1084 bool
1085 include_linkonce_section(Layout*, const char*,
1086 const elfcpp::Shdr<size, big_endian>&);
1087
61ba1cf9
ILT
1088 // Views and sizes when relocating.
1089 struct View_size
1090 {
1091 unsigned char* view;
1092 typename elfcpp::Elf_types<size>::Elf_Addr address;
1093 off_t offset;
1094 off_t view_size;
730cdc88 1095 bool is_input_output_view;
96803768 1096 bool is_postprocessing_view;
61ba1cf9
ILT
1097 };
1098
1099 typedef std::vector<View_size> Views;
1100
1101 // Write section data to the output file. Record the views and
1102 // sizes in VIEWS for use when relocating.
1103 void
1104 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1105
1106 // Relocate the sections in the output file.
1107 void
92e059d8
ILT
1108 relocate_sections(const General_options& options, const Symbol_table*,
1109 const Layout*, const unsigned char* pshdrs, Views*);
61ba1cf9
ILT
1110
1111 // Write out the local symbols.
1112 void
b8e6aad9 1113 write_local_symbols(Output_file*,
7bf1f802 1114 const Stringpool_template<char>*,
b8e6aad9 1115 const Stringpool_template<char>*);
61ba1cf9 1116
07f397ab
ILT
1117 // The GOT offsets of local symbols. This map also stores GOT offsets
1118 // for tp-relative offsets for TLS symbols.
e727fa71
ILT
1119 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1120
07f397ab
ILT
1121 // The TLS GOT offsets of local symbols. The map stores the offsets
1122 // for either a single GOT entry that holds the module index of a TLS
1123 // symbol, or a pair of GOT entries containing the module index and
1124 // dtv-relative offset.
1125 struct Tls_got_entry
1126 {
1127 Tls_got_entry(int got_offset, bool have_pair)
1128 : got_offset_(got_offset),
1129 have_pair_(have_pair)
1130 { }
1131 int got_offset_;
1132 bool have_pair_;
1133 };
1134 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1135
645f8123
ILT
1136 // General access to the ELF file.
1137 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
bae7f79e 1138 // Index of SHT_SYMTAB section.
645f8123 1139 unsigned int symtab_shndx_;
61ba1cf9
ILT
1140 // The number of local symbols.
1141 unsigned int local_symbol_count_;
1142 // The number of local symbols which go into the output file.
1143 unsigned int output_local_symbol_count_;
7bf1f802
ILT
1144 // The number of local symbols which go into the output file's dynamic
1145 // symbol table.
1146 unsigned int output_local_dynsym_count_;
14bfc3f5 1147 // The entries in the symbol table for the external symbols.
730cdc88 1148 Symbols symbols_;
75f65a3e
ILT
1149 // File offset for local symbols.
1150 off_t local_symbol_offset_;
7bf1f802
ILT
1151 // File offset for local dynamic symbols.
1152 off_t local_dynsym_offset_;
61ba1cf9 1153 // Values of local symbols.
c06b7b0b 1154 Local_values local_values_;
07f397ab
ILT
1155 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1156 // for TLS symbols, indexed by symbol number.
e727fa71 1157 Local_got_offsets local_got_offsets_;
07f397ab
ILT
1158 // GOT offsets for local TLS symbols, indexed by symbol number
1159 // and GOT entry type.
1160 Local_tls_got_offsets local_tls_got_offsets_;
730cdc88
ILT
1161 // Whether this object has a GNU style .eh_frame section.
1162 bool has_eh_frame_;
bae7f79e
ILT
1163};
1164
54dc6425 1165// A class to manage the list of all objects.
a2fb1b05 1166
54dc6425
ILT
1167class Input_objects
1168{
1169 public:
1170 Input_objects()
9a2d6984
ILT
1171 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1172 system_library_directory_()
54dc6425
ILT
1173 { }
1174
f6ce93d6
ILT
1175 // The type of the list of input relocateable objects.
1176 typedef std::vector<Relobj*> Relobj_list;
1177 typedef Relobj_list::const_iterator Relobj_iterator;
1178
1179 // The type of the list of input dynamic objects.
1180 typedef std::vector<Dynobj*> Dynobj_list;
1181 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425 1182
008db82e
ILT
1183 // Add an object to the list. Return true if all is well, or false
1184 // if this object should be ignored.
1185 bool
54dc6425
ILT
1186 add_object(Object*);
1187
75f65a3e
ILT
1188 // Get the target we should use for the output file.
1189 Target*
1190 target() const
1191 { return this->target_; }
1192
e2827e5f
ILT
1193 // For each dynamic object, check whether we've seen all of its
1194 // explicit dependencies.
1195 void
1196 check_dynamic_dependencies() const;
1197
9a2d6984
ILT
1198 // Return whether an object was found in the system library
1199 // directory.
1200 bool
1201 found_in_system_library_directory(const Object*) const;
1202
f6ce93d6
ILT
1203 // Iterate over all regular objects.
1204
1205 Relobj_iterator
1206 relobj_begin() const
1207 { return this->relobj_list_.begin(); }
1208
1209 Relobj_iterator
1210 relobj_end() const
1211 { return this->relobj_list_.end(); }
1212
1213 // Iterate over all dynamic objects.
1214
1215 Dynobj_iterator
1216 dynobj_begin() const
1217 { return this->dynobj_list_.begin(); }
54dc6425 1218
f6ce93d6
ILT
1219 Dynobj_iterator
1220 dynobj_end() const
1221 { return this->dynobj_list_.end(); }
54dc6425
ILT
1222
1223 // Return whether we have seen any dynamic objects.
1224 bool
1225 any_dynamic() const
f6ce93d6 1226 { return !this->dynobj_list_.empty(); }
54dc6425 1227
fe9a4c12
ILT
1228 // Return the number of input objects.
1229 int
1230 number_of_input_objects() const
1231 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1232
54dc6425
ILT
1233 private:
1234 Input_objects(const Input_objects&);
1235 Input_objects& operator=(const Input_objects&);
1236
008db82e 1237 // The list of ordinary objects included in the link.
f6ce93d6 1238 Relobj_list relobj_list_;
008db82e 1239 // The list of dynamic objects included in the link.
f6ce93d6 1240 Dynobj_list dynobj_list_;
008db82e 1241 // The target.
75f65a3e 1242 Target* target_;
008db82e
ILT
1243 // SONAMEs that we have seen.
1244 Unordered_set<std::string> sonames_;
9a2d6984
ILT
1245 // The directory in which we find the libc.so.
1246 std::string system_library_directory_;
54dc6425 1247};
a2fb1b05 1248
92e059d8
ILT
1249// Some of the information we pass to the relocation routines. We
1250// group this together to avoid passing a dozen different arguments.
1251
1252template<int size, bool big_endian>
1253struct Relocate_info
1254{
1255 // Command line options.
1256 const General_options* options;
1257 // Symbol table.
1258 const Symbol_table* symtab;
1259 // Layout.
1260 const Layout* layout;
1261 // Object being relocated.
f6ce93d6 1262 Sized_relobj<size, big_endian>* object;
92e059d8
ILT
1263 // Section index of relocation section.
1264 unsigned int reloc_shndx;
1265 // Section index of section being relocated.
1266 unsigned int data_shndx;
1267
1268 // Return a string showing the location of a relocation. This is
1269 // only used for error messages.
1270 std::string
1271 location(size_t relnum, off_t reloffset) const;
1272};
1273
bae7f79e
ILT
1274// Return an Object appropriate for the input file. P is BYTES long,
1275// and holds the ELF header.
1276
61ba1cf9
ILT
1277extern Object*
1278make_elf_object(const std::string& name, Input_file*,
1279 off_t offset, const unsigned char* p,
1280 off_t bytes);
bae7f79e
ILT
1281
1282} // end namespace gold
1283
1284#endif // !defined(GOLD_OBJECT_H)
This page took 0.130381 seconds and 4 git commands to generate.