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