]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/object.h
bfd/
[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;
17a1d0a9 38class Task;
a2fb1b05 39class Layout;
61ba1cf9
ILT
40class Output_section;
41class Output_file;
f6ce93d6 42class Dynobj;
4625f782 43class Object_merge_map;
6a74a719 44class Relocatable_relocs;
a2fb1b05 45
b8e6aad9
ILT
46template<typename Stringpool_char>
47class Stringpool_template;
48
bae7f79e
ILT
49// Data to pass from read_symbols() to add_symbols().
50
51struct Read_symbols_data
52{
12e14209
ILT
53 // Section headers.
54 File_view* section_headers;
55 // Section names.
56 File_view* section_names;
57 // Size of section name data in bytes.
8383303e 58 section_size_type section_names_size;
bae7f79e
ILT
59 // Symbol data.
60 File_view* symbols;
61 // Size of symbol data in bytes.
8383303e 62 section_size_type symbols_size;
730cdc88
ILT
63 // Offset of external symbols within symbol data. This structure
64 // sometimes contains only external symbols, in which case this will
65 // be zero. Sometimes it contains all symbols.
8383303e 66 section_offset_type external_symbols_offset;
bae7f79e
ILT
67 // Symbol names.
68 File_view* symbol_names;
69 // Size of symbol name data in bytes.
8383303e 70 section_size_type symbol_names_size;
dbe717ef
ILT
71
72 // Version information. This is only used on dynamic objects.
73 // Version symbol data (from SHT_GNU_versym section).
74 File_view* versym;
8383303e 75 section_size_type versym_size;
dbe717ef
ILT
76 // Version definition data (from SHT_GNU_verdef section).
77 File_view* verdef;
8383303e 78 section_size_type verdef_size;
dbe717ef
ILT
79 unsigned int verdef_info;
80 // Needed version data (from SHT_GNU_verneed section).
81 File_view* verneed;
8383303e 82 section_size_type verneed_size;
dbe717ef 83 unsigned int verneed_info;
bae7f79e
ILT
84};
85
f7e2ee48
ILT
86// Information used to print error messages.
87
88struct Symbol_location_info
89{
90 std::string source_file;
91 std::string enclosing_symbol_name;
92 int line_number;
93};
94
92e059d8
ILT
95// Data about a single relocation section. This is read in
96// read_relocs and processed in scan_relocs.
97
98struct Section_relocs
99{
100 // Index of reloc section.
101 unsigned int reloc_shndx;
102 // Index of section that relocs apply to.
103 unsigned int data_shndx;
104 // Contents of reloc section.
105 File_view* contents;
106 // Reloc section type.
107 unsigned int sh_type;
108 // Number of reloc entries.
109 size_t reloc_count;
730cdc88
ILT
110 // Output section.
111 Output_section* output_section;
112 // Whether this section has special handling for offsets.
113 bool needs_special_offset_handling;
92e059d8
ILT
114};
115
116// Relocations in an object file. This is read in read_relocs and
117// processed in scan_relocs.
118
119struct Read_relocs_data
120{
121 typedef std::vector<Section_relocs> Relocs_list;
122 // The relocations.
123 Relocs_list relocs;
124 // The local symbols.
125 File_view* local_symbols;
126};
127
f6ce93d6
ILT
128// Object is an abstract base class which represents either a 32-bit
129// or a 64-bit input object. This can be a regular object file
130// (ET_REL) or a shared object (ET_DYN).
bae7f79e
ILT
131
132class Object
133{
134 public:
135 // NAME is the name of the object as we would report it to the user
136 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
137 // used to read the file. OFFSET is the offset within the input
138 // file--0 for a .o or .so file, something else for a .a file.
14bfc3f5
ILT
139 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
140 off_t offset = 0)
dbe717ef 141 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
f6ce93d6 142 is_dynamic_(is_dynamic), target_(NULL)
cb295612 143 { input_file->file().add_object(); }
bae7f79e
ILT
144
145 virtual ~Object()
cb295612 146 { this->input_file_->file().remove_object(); }
bae7f79e 147
14bfc3f5 148 // Return the name of the object as we would report it to the tuser.
bae7f79e
ILT
149 const std::string&
150 name() const
151 { return this->name_; }
152
cec9d2f3
ILT
153 // Get the offset into the file.
154 off_t
155 offset() const
156 { return this->offset_; }
157
14bfc3f5
ILT
158 // Return whether this is a dynamic object.
159 bool
160 is_dynamic() const
161 { return this->is_dynamic_; }
162
14bfc3f5
ILT
163 // Return the target structure associated with this object.
164 Target*
a2fb1b05 165 target() const
14bfc3f5
ILT
166 { return this->target_; }
167
a2fb1b05
ILT
168 // Lock the underlying file.
169 void
17a1d0a9
ILT
170 lock(const Task* t)
171 { this->input_file()->file().lock(t); }
a2fb1b05
ILT
172
173 // Unlock the underlying file.
174 void
17a1d0a9
ILT
175 unlock(const Task* t)
176 { this->input_file()->file().unlock(t); }
a2fb1b05
ILT
177
178 // Return whether the underlying file is locked.
179 bool
180 is_locked() const
7004837e 181 { return this->input_file()->file().is_locked(); }
a2fb1b05 182
17a1d0a9
ILT
183 // Return the token, so that the task can be queued.
184 Task_token*
185 token()
186 { return this->input_file()->file().token(); }
187
188 // Release the underlying file.
189 void
190 release()
191 { this->input_file_->file().release(); }
192
14bfc3f5
ILT
193 // Return the sized target structure associated with this object.
194 // This is like the target method but it returns a pointer of
195 // appropriate checked type.
196 template<int size, bool big_endian>
197 Sized_target<size, big_endian>*
7004837e 198 sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const;
bae7f79e 199
5a6f7e2d
ILT
200 // Get the number of sections.
201 unsigned int
202 shnum() const
203 { return this->shnum_; }
a2fb1b05 204
f6ce93d6 205 // Return a view of the contents of a section. Set *PLEN to the
9eb9fa57 206 // size. CACHE is a hint as in File_read::get_view.
f6ce93d6 207 const unsigned char*
8383303e 208 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
f6ce93d6 209
a445fddf
ILT
210 // Return the size of a section given a section index.
211 uint64_t
212 section_size(unsigned int shndx)
213 { return this->do_section_size(shndx); }
214
215 // Return the name of a section given a section index.
f6ce93d6 216 std::string
a3ad94ed
ILT
217 section_name(unsigned int shndx)
218 { return this->do_section_name(shndx); }
219
220 // Return the section flags given a section index.
221 uint64_t
222 section_flags(unsigned int shndx)
223 { return this->do_section_flags(shndx); }
f6ce93d6 224
730cdc88
ILT
225 // Return the section type given a section index.
226 unsigned int
227 section_type(unsigned int shndx)
228 { return this->do_section_type(shndx); }
229
f7e2ee48
ILT
230 // Return the section link field given a section index.
231 unsigned int
232 section_link(unsigned int shndx)
233 { return this->do_section_link(shndx); }
234
4c50553d
ILT
235 // Return the section info field given a section index.
236 unsigned int
237 section_info(unsigned int shndx)
238 { return this->do_section_info(shndx); }
239
a445fddf
ILT
240 // Return the required section alignment given a section index.
241 uint64_t
242 section_addralign(unsigned int shndx)
243 { return this->do_section_addralign(shndx); }
244
5a6f7e2d
ILT
245 // Read the symbol information.
246 void
247 read_symbols(Read_symbols_data* sd)
248 { return this->do_read_symbols(sd); }
249
250 // Pass sections which should be included in the link to the Layout
251 // object, and record where the sections go in the output file.
252 void
7e1edb90
ILT
253 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
254 { this->do_layout(symtab, layout, sd); }
5a6f7e2d
ILT
255
256 // Add symbol information to the global symbol table.
257 void
258 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
259 { this->do_add_symbols(symtab, sd); }
260
645f8123
ILT
261 // Functions and types for the elfcpp::Elf_file interface. This
262 // permit us to use Object as the File template parameter for
263 // elfcpp::Elf_file.
264
265 // The View class is returned by view. It must support a single
266 // method, data(). This is trivial, because get_view does what we
267 // need.
268 class View
269 {
270 public:
271 View(const unsigned char* p)
272 : p_(p)
273 { }
274
275 const unsigned char*
276 data() const
277 { return this->p_; }
278
279 private:
280 const unsigned char* p_;
281 };
282
283 // Return a View.
284 View
8383303e 285 view(off_t file_offset, section_size_type data_size)
9eb9fa57 286 { return View(this->get_view(file_offset, data_size, true)); }
645f8123
ILT
287
288 // Report an error.
289 void
75f2446e 290 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
645f8123
ILT
291
292 // A location in the file.
293 struct Location
294 {
295 off_t file_offset;
296 off_t data_size;
297
8383303e 298 Location(off_t fo, section_size_type ds)
645f8123
ILT
299 : file_offset(fo), data_size(ds)
300 { }
301 };
302
303 // Get a View given a Location.
304 View view(Location loc)
9eb9fa57 305 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
645f8123 306
cb295612
ILT
307 // Get a view into the underlying file.
308 const unsigned char*
309 get_view(off_t start, section_size_type size, bool cache)
310 {
311 return this->input_file()->file().get_view(start + this->offset_, size,
312 cache);
313 }
314
315 // Get a lasting view into the underlying file.
316 File_view*
317 get_lasting_view(off_t start, section_size_type size, bool cache)
318 {
319 return this->input_file()->file().get_lasting_view(start + this->offset_,
320 size, cache);
321 }
322
323 // Read data from the underlying file.
324 void
325 read(off_t start, section_size_type size, void* p) const
326 { this->input_file()->file().read(start + this->offset_, size, p); }
327
328 // Read multiple data from the underlying file.
329 void
330 read_multiple(const File_read::Read_multiple& rm)
331 { this->input_file()->file().read_multiple(this->offset_, rm); }
332
333 // Stop caching views in the underlying file.
334 void
335 clear_view_cache_marks()
336 { this->input_file()->file().clear_view_cache_marks(); }
337
f6ce93d6
ILT
338 protected:
339 // Read the symbols--implemented by child class.
340 virtual void
341 do_read_symbols(Read_symbols_data*) = 0;
342
343 // Lay out sections--implemented by child class.
344 virtual void
7e1edb90 345 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
f6ce93d6
ILT
346
347 // Add symbol information to the global symbol table--implemented by
348 // child class.
349 virtual void
350 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
351
645f8123
ILT
352 // Return the location of the contents of a section. Implemented by
353 // child class.
354 virtual Location
a3ad94ed 355 do_section_contents(unsigned int shndx) = 0;
f6ce93d6 356
a445fddf
ILT
357 // Get the size of a section--implemented by child class.
358 virtual uint64_t
359 do_section_size(unsigned int shndx) = 0;
360
f6ce93d6
ILT
361 // Get the name of a section--implemented by child class.
362 virtual std::string
a3ad94ed
ILT
363 do_section_name(unsigned int shndx) = 0;
364
365 // Get section flags--implemented by child class.
366 virtual uint64_t
367 do_section_flags(unsigned int shndx) = 0;
f6ce93d6 368
730cdc88
ILT
369 // Get section type--implemented by child class.
370 virtual unsigned int
371 do_section_type(unsigned int shndx) = 0;
372
f7e2ee48
ILT
373 // Get section link field--implemented by child class.
374 virtual unsigned int
375 do_section_link(unsigned int shndx) = 0;
376
4c50553d
ILT
377 // Get section info field--implemented by child class.
378 virtual unsigned int
379 do_section_info(unsigned int shndx) = 0;
380
a445fddf
ILT
381 // Get section alignment--implemented by child class.
382 virtual uint64_t
383 do_section_addralign(unsigned int shndx) = 0;
384
17a1d0a9 385 // Get the file. We pass on const-ness.
f6ce93d6 386 Input_file*
7004837e
ILT
387 input_file()
388 { return this->input_file_; }
389
390 const Input_file*
f6ce93d6
ILT
391 input_file() const
392 { return this->input_file_; }
393
f6ce93d6
ILT
394 // Set the target.
395 void
dbe717ef
ILT
396 set_target(int machine, int size, bool big_endian, int osabi,
397 int abiversion);
398
dbe717ef
ILT
399 // Set the number of sections.
400 void
401 set_shnum(int shnum)
402 { this->shnum_ = shnum; }
403
404 // Functions used by both Sized_relobj and Sized_dynobj.
405
406 // Read the section data into a Read_symbols_data object.
407 template<int size, bool big_endian>
408 void
409 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
410 Read_symbols_data*);
411
412 // If NAME is the name of a special .gnu.warning section, arrange
413 // for the warning to be issued. SHNDX is the section index.
414 // Return whether it is a warning section.
415 bool
416 handle_gnu_warning_section(const char* name, unsigned int shndx,
417 Symbol_table*);
f6ce93d6
ILT
418
419 private:
420 // This class may not be copied.
421 Object(const Object&);
422 Object& operator=(const Object&);
423
424 // Name of object as printed to user.
425 std::string name_;
426 // For reading the file.
427 Input_file* input_file_;
428 // Offset within the file--0 for an object file, non-0 for an
429 // archive.
430 off_t offset_;
dbe717ef
ILT
431 // Number of input sections.
432 unsigned int shnum_;
f6ce93d6
ILT
433 // Whether this is a dynamic object.
434 bool is_dynamic_;
435 // Target functions--may be NULL if the target is not known.
436 Target* target_;
437};
438
439// Implement sized_target inline for efficiency. This approach breaks
440// static type checking, but is made safe using asserts.
441
442template<int size, bool big_endian>
443inline Sized_target<size, big_endian>*
7004837e 444Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const
f6ce93d6 445{
a3ad94ed
ILT
446 gold_assert(this->target_->get_size() == size);
447 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
f6ce93d6
ILT
448 return static_cast<Sized_target<size, big_endian>*>(this->target_);
449}
450
451// A regular object (ET_REL). This is an abstract base class itself.
b8e6aad9 452// The implementation is the template class Sized_relobj.
f6ce93d6
ILT
453
454class Relobj : public Object
455{
456 public:
457 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
4625f782
ILT
458 : Object(name, input_file, false, offset),
459 map_to_output_(),
6a74a719 460 map_to_relocatable_relocs_(NULL),
4625f782
ILT
461 object_merge_map_(NULL),
462 relocs_must_follow_section_writes_(false)
f6ce93d6
ILT
463 { }
464
92e059d8 465 // Read the relocs.
a2fb1b05 466 void
92e059d8
ILT
467 read_relocs(Read_relocs_data* rd)
468 { return this->do_read_relocs(rd); }
469
470 // Scan the relocs and adjust the symbol table.
471 void
472 scan_relocs(const General_options& options, Symbol_table* symtab,
ead1e424
ILT
473 Layout* layout, Read_relocs_data* rd)
474 { return this->do_scan_relocs(options, symtab, layout, rd); }
a2fb1b05 475
6d013333
ILT
476 // The number of local symbols in the input symbol table.
477 virtual unsigned int
478 local_symbol_count() const
479 { return this->do_local_symbol_count(); }
480
7bf1f802
ILT
481 // Initial local symbol processing: count the number of local symbols
482 // in the output symbol table and dynamic symbol table; add local symbol
483 // names to *POOL and *DYNPOOL.
484 void
485 count_local_symbols(Stringpool_template<char>* pool,
486 Stringpool_template<char>* dynpool)
487 { return this->do_count_local_symbols(pool, dynpool); }
488
489 // Set the values of the local symbols, set the output symbol table
490 // indexes for the local variables, and set the offset where local
491 // symbol information will be stored. Returns the new local symbol index.
492 unsigned int
493 finalize_local_symbols(unsigned int index, off_t off)
494 { return this->do_finalize_local_symbols(index, off); }
495
496 // Set the output dynamic symbol table indexes for the local variables.
c06b7b0b 497 unsigned int
7bf1f802
ILT
498 set_local_dynsym_indexes(unsigned int index)
499 { return this->do_set_local_dynsym_indexes(index); }
500
501 // Set the offset where local dynamic symbol information will be stored.
502 unsigned int
503 set_local_dynsym_offset(off_t off)
504 { return this->do_set_local_dynsym_offset(off); }
75f65a3e 505
61ba1cf9
ILT
506 // Relocate the input sections and write out the local symbols.
507 void
508 relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
509 const Layout* layout, Output_file* of)
510 { return this->do_relocate(options, symtab, layout, of); }
61ba1cf9 511
a783673b
ILT
512 // Return whether an input section is being included in the link.
513 bool
5a6f7e2d 514 is_section_included(unsigned int shndx) const
a783673b 515 {
5a6f7e2d
ILT
516 gold_assert(shndx < this->map_to_output_.size());
517 return this->map_to_output_[shndx].output_section != NULL;
a783673b
ILT
518 }
519
730cdc88
ILT
520 // Return whether an input section requires special
521 // handling--whether it is not simply mapped from the input file to
522 // the output file.
523 bool
524 is_section_specially_mapped(unsigned int shndx) const
525 {
526 gold_assert(shndx < this->map_to_output_.size());
527 return (this->map_to_output_[shndx].output_section != NULL
528 && this->map_to_output_[shndx].offset == -1);
529 }
530
a783673b
ILT
531 // Given a section index, return the corresponding Output_section
532 // (which will be NULL if the section is not included in the link)
730cdc88
ILT
533 // and set *POFF to the offset within that section. *POFF will be
534 // set to -1 if the section requires special handling.
a783673b 535 inline Output_section*
8383303e 536 output_section(unsigned int shndx, section_offset_type* poff) const;
a783673b 537
ead1e424
ILT
538 // Set the offset of an input section within its output section.
539 void
8383303e 540 set_section_offset(unsigned int shndx, section_offset_type off)
ead1e424 541 {
a3ad94ed 542 gold_assert(shndx < this->map_to_output_.size());
ead1e424
ILT
543 this->map_to_output_[shndx].offset = off;
544 }
545
730cdc88
ILT
546 // Return true if we need to wait for output sections to be written
547 // before we can apply relocations. This is true if the object has
548 // any relocations for sections which require special handling, such
549 // as the exception frame section.
550 bool
17a1d0a9 551 relocs_must_follow_section_writes() const
730cdc88
ILT
552 { return this->relocs_must_follow_section_writes_; }
553
4625f782
ILT
554 // Return the object merge map.
555 Object_merge_map*
556 merge_map() const
557 { return this->object_merge_map_; }
558
559 // Set the object merge map.
560 void
561 set_merge_map(Object_merge_map* object_merge_map)
562 {
563 gold_assert(this->object_merge_map_ == NULL);
564 this->object_merge_map_ = object_merge_map;
565 }
566
6a74a719
ILT
567 // Record the relocatable reloc info for an input reloc section.
568 void
569 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
570 {
571 gold_assert(reloc_shndx < this->shnum());
572 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
573 }
574
575 // Get the relocatable reloc info for an input reloc section.
576 Relocatable_relocs*
577 relocatable_relocs(unsigned int reloc_shndx)
578 {
579 gold_assert(reloc_shndx < this->shnum());
580 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
581 }
582
a783673b 583 protected:
a2fb1b05
ILT
584 // What we need to know to map an input section to an output
585 // section. We keep an array of these, one for each input section,
586 // indexed by the input section number.
587 struct Map_to_output
588 {
589 // The output section. This is NULL if the input section is to be
590 // discarded.
591 Output_section* output_section;
b8e6aad9
ILT
592 // The offset within the output section. This is -1 if the
593 // section requires special handling.
8383303e 594 section_offset_type offset;
a2fb1b05
ILT
595 };
596
92e059d8
ILT
597 // Read the relocs--implemented by child class.
598 virtual void
599 do_read_relocs(Read_relocs_data*) = 0;
600
601 // Scan the relocs--implemented by child class.
602 virtual void
ead1e424
ILT
603 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
604 Read_relocs_data*) = 0;
92e059d8 605
6d013333
ILT
606 // Return the number of local symbols--implemented by child class.
607 virtual unsigned int
608 do_local_symbol_count() const = 0;
609
7bf1f802
ILT
610 // Count local symbols--implemented by child class.
611 virtual void
612 do_count_local_symbols(Stringpool_template<char>*,
6a74a719 613 Stringpool_template<char>*) = 0;
75f65a3e 614
6a74a719
ILT
615 // Finalize the local symbols. Set the output symbol table indexes
616 // for the local variables, and set the offset where local symbol
617 // information will be stored.
7bf1f802
ILT
618 virtual unsigned int
619 do_finalize_local_symbols(unsigned int, off_t) = 0;
620
621 // Set the output dynamic symbol table indexes for the local variables.
622 virtual unsigned int
623 do_set_local_dynsym_indexes(unsigned int) = 0;
624
625 // Set the offset where local dynamic symbol information will be stored.
626 virtual unsigned int
627 do_set_local_dynsym_offset(off_t) = 0;
628
61ba1cf9
ILT
629 // Relocate the input sections and write out the local
630 // symbols--implemented by child class.
631 virtual void
632 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
633 const Layout*, Output_file* of) = 0;
634
a2fb1b05
ILT
635 // Return the vector mapping input sections to output sections.
636 std::vector<Map_to_output>&
637 map_to_output()
638 { return this->map_to_output_; }
639
b8e6aad9
ILT
640 const std::vector<Map_to_output>&
641 map_to_output() const
642 { return this->map_to_output_; }
643
6a74a719
ILT
644 // Set the size of the relocatable relocs array.
645 void
646 size_relocatable_relocs()
647 {
648 this->map_to_relocatable_relocs_ =
649 new std::vector<Relocatable_relocs*>(this->shnum());
650 }
651
730cdc88
ILT
652 // Record that we must wait for the output sections to be written
653 // before applying relocations.
654 void
655 set_relocs_must_follow_section_writes()
656 { this->relocs_must_follow_section_writes_ = true; }
657
bae7f79e 658 private:
a2fb1b05
ILT
659 // Mapping from input sections to output section.
660 std::vector<Map_to_output> map_to_output_;
6a74a719
ILT
661 // Mapping from input section index to the information recorded for
662 // the relocations. This is only used for a relocatable link.
663 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
4625f782
ILT
664 // Mappings for merge sections. This is managed by the code in the
665 // Merge_map class.
666 Object_merge_map* object_merge_map_;
730cdc88
ILT
667 // Whether we need to wait for output sections to be written before
668 // we can apply relocations.
669 bool relocs_must_follow_section_writes_;
bae7f79e
ILT
670};
671
a783673b
ILT
672// Implement Object::output_section inline for efficiency.
673inline Output_section*
8383303e 674Relobj::output_section(unsigned int shndx, section_offset_type* poff) const
a783673b 675{
5a6f7e2d
ILT
676 gold_assert(shndx < this->map_to_output_.size());
677 const Map_to_output& mo(this->map_to_output_[shndx]);
a783673b
ILT
678 *poff = mo.offset;
679 return mo.output_section;
680}
681
a9a60db6
ILT
682// This class is used to handle relocations against a section symbol
683// in an SHF_MERGE section. For such a symbol, we need to know the
684// addend of the relocation before we can determine the final value.
685// The addend gives us the location in the input section, and we can
686// determine how it is mapped to the output section. For a
687// non-section symbol, we apply the addend to the final value of the
688// symbol; that is done in finalize_local_symbols, and does not use
689// this class.
690
691template<int size>
692class Merged_symbol_value
693{
694 public:
695 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
696
697 // We use a hash table to map offsets in the input section to output
698 // addresses.
699 typedef Unordered_map<section_offset_type, Value> Output_addresses;
700
701 Merged_symbol_value(Value input_value, Value output_start_address)
702 : input_value_(input_value), output_start_address_(output_start_address),
703 output_addresses_()
704 { }
705
706 // Initialize the hash table.
707 void
708 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
709
710 // Release the hash table to save space.
711 void
712 free_input_to_output_map()
713 { this->output_addresses_.clear(); }
714
715 // Get the output value corresponding to an addend. The object and
716 // input section index are passed in because the caller will have
717 // them; otherwise we could store them here.
718 Value
719 value(const Relobj* object, unsigned int input_shndx, Value addend) const
720 {
721 Value input_offset = this->input_value_ + addend;
722 typename Output_addresses::const_iterator p =
723 this->output_addresses_.find(input_offset);
724 if (p != this->output_addresses_.end())
725 return p->second;
726
727 return this->value_from_output_section(object, input_shndx, input_offset);
728 }
729
730 private:
731 // Get the output value for an input offset if we couldn't find it
732 // in the hash table.
733 Value
734 value_from_output_section(const Relobj*, unsigned int input_shndx,
735 Value input_offset) const;
736
737 // The value of the section symbol in the input file. This is
738 // normally zero, but could in principle be something else.
739 Value input_value_;
740 // The start address of this merged section in the output file.
741 Value output_start_address_;
742 // A hash table which maps offsets in the input section to output
743 // addresses. This only maps specific offsets, not all offsets.
744 Output_addresses output_addresses_;
745};
746
b8e6aad9
ILT
747// This POD class is holds the value of a symbol. This is used for
748// local symbols, and for all symbols during relocation processing.
730cdc88
ILT
749// For special sections, such as SHF_MERGE sections, this calls a
750// function to get the final symbol value.
b8e6aad9
ILT
751
752template<int size>
753class Symbol_value
754{
755 public:
756 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
757
758 Symbol_value()
7bf1f802
ILT
759 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
760 is_section_symbol_(false), is_tls_symbol_(false),
a9a60db6
ILT
761 has_output_value_(true)
762 { this->u_.value = 0; }
b8e6aad9
ILT
763
764 // Get the value of this symbol. OBJECT is the object in which this
765 // symbol is defined, and ADDEND is an addend to add to the value.
766 template<bool big_endian>
767 Value
768 value(const Sized_relobj<size, big_endian>* object, Value addend) const
769 {
a9a60db6
ILT
770 if (this->has_output_value_)
771 return this->u_.value + addend;
772 else
773 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
774 addend);
b8e6aad9
ILT
775 }
776
777 // Set the value of this symbol in the output symbol table.
778 void
779 set_output_value(Value value)
a9a60db6
ILT
780 { this->u_.value = value; }
781
782 // For a section symbol in a merged section, we need more
783 // information.
784 void
785 set_merged_symbol_value(Merged_symbol_value<size>* msv)
b8e6aad9 786 {
a9a60db6
ILT
787 gold_assert(this->is_section_symbol_);
788 this->has_output_value_ = false;
789 this->u_.merged_symbol_value = msv;
b8e6aad9
ILT
790 }
791
a9a60db6
ILT
792 // Initialize the input to output map for a section symbol in a
793 // merged section. We also initialize the value of a non-section
794 // symbol in a merged section.
b8e6aad9 795 void
a9a60db6 796 initialize_input_to_output_map(const Relobj* object)
b8e6aad9 797 {
a9a60db6
ILT
798 if (!this->has_output_value_)
799 {
800 gold_assert(this->is_section_symbol_);
801 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
802 msv->initialize_input_to_output_map(object, this->input_shndx_);
803 }
b8e6aad9
ILT
804 }
805
a9a60db6
ILT
806 // Free the input to output map for a section symbol in a merged
807 // section.
808 void
809 free_input_to_output_map()
7bf1f802 810 {
a9a60db6
ILT
811 if (!this->has_output_value_)
812 this->u_.merged_symbol_value->free_input_to_output_map();
7bf1f802
ILT
813 }
814
a9a60db6
ILT
815 // Set the value of the symbol from the input file. This is only
816 // called by count_local_symbols, to communicate the value to
817 // finalize_local_symbols.
818 void
819 set_input_value(Value value)
820 { this->u_.value = value; }
821
822 // Return the input value. This is only called by
823 // finalize_local_symbols.
824 Value
825 input_value() const
826 { return this->u_.value; }
827
b8e6aad9
ILT
828 // Return whether this symbol should go into the output symbol
829 // table.
830 bool
831 needs_output_symtab_entry() const
ac1f0c21 832 { return this->output_symtab_index_ != -1U; }
b8e6aad9
ILT
833
834 // Return the index in the output symbol table.
835 unsigned int
836 output_symtab_index() const
837 {
838 gold_assert(this->output_symtab_index_ != 0);
839 return this->output_symtab_index_;
840 }
841
842 // Set the index in the output symbol table.
843 void
844 set_output_symtab_index(unsigned int i)
845 {
846 gold_assert(this->output_symtab_index_ == 0);
847 this->output_symtab_index_ = i;
848 }
849
850 // Record that this symbol should not go into the output symbol
851 // table.
852 void
853 set_no_output_symtab_entry()
854 {
855 gold_assert(this->output_symtab_index_ == 0);
856 this->output_symtab_index_ = -1U;
857 }
858
7bf1f802
ILT
859 // Set the index in the output dynamic symbol table.
860 void
861 set_needs_output_dynsym_entry()
862 {
863 this->output_dynsym_index_ = 0;
864 }
865
866 // Return whether this symbol should go into the output symbol
867 // table.
868 bool
869 needs_output_dynsym_entry() const
870 {
871 return this->output_dynsym_index_ != -1U;
872 }
873
874 // Record that this symbol should go into the dynamic symbol table.
875 void
876 set_output_dynsym_index(unsigned int i)
877 {
878 gold_assert(this->output_dynsym_index_ == 0);
879 this->output_dynsym_index_ = i;
880 }
881
882 // Return the index in the output dynamic symbol table.
883 unsigned int
884 output_dynsym_index() const
885 {
886 gold_assert(this->output_dynsym_index_ != 0);
887 return this->output_dynsym_index_;
888 }
889
b8e6aad9
ILT
890 // Set the index of the input section in the input file.
891 void
892 set_input_shndx(unsigned int i)
730cdc88
ILT
893 {
894 this->input_shndx_ = i;
ac1f0c21
ILT
895 // input_shndx_ field is a bitfield, so make sure that the value
896 // fits.
730cdc88
ILT
897 gold_assert(this->input_shndx_ == i);
898 }
b8e6aad9 899
7bf1f802
ILT
900 // Return the index of the input section in the input file.
901 unsigned int
902 input_shndx() const
903 { return this->input_shndx_; }
904
a9a60db6
ILT
905 // Whether this is a section symbol.
906 bool
907 is_section_symbol() const
908 { return this->is_section_symbol_; }
909
063f12a8
ILT
910 // Record that this is a section symbol.
911 void
912 set_is_section_symbol()
913 { this->is_section_symbol_ = true; }
914
7bf1f802
ILT
915 // Record that this is a TLS symbol.
916 void
917 set_is_tls_symbol()
918 { this->is_tls_symbol_ = true; }
919
920 // Return TRUE if this is a TLS symbol.
921 bool
922 is_tls_symbol() const
923 { return this->is_tls_symbol_; }
924
b8e6aad9
ILT
925 private:
926 // The index of this local symbol in the output symbol table. This
927 // will be -1 if the symbol should not go into the symbol table.
928 unsigned int output_symtab_index_;
7bf1f802
ILT
929 // The index of this local symbol in the dynamic symbol table. This
930 // will be -1 if the symbol should not go into the symbol table.
931 unsigned int output_dynsym_index_;
b8e6aad9
ILT
932 // The section index in the input file in which this symbol is
933 // defined.
7bf1f802 934 unsigned int input_shndx_ : 29;
063f12a8
ILT
935 // Whether this is a STT_SECTION symbol.
936 bool is_section_symbol_ : 1;
7bf1f802
ILT
937 // Whether this is a STT_TLS symbol.
938 bool is_tls_symbol_ : 1;
a9a60db6
ILT
939 // Whether this symbol has a value for the output file. This is
940 // normally set to true during Layout::finalize, by
941 // finalize_local_symbols. It will be false for a section symbol in
942 // a merge section, as for such symbols we can not determine the
943 // value to use in a relocation until we see the addend.
944 bool has_output_value_ : 1;
945 union
946 {
947 // This is used if has_output_value_ is true. Between
948 // count_local_symbols and finalize_local_symbols, this is the
949 // value in the input file. After finalize_local_symbols, it is
950 // the value in the output file.
951 Value value;
952 // This is used if has_output_value_ is false. It points to the
953 // information we need to get the value for a merge section.
954 Merged_symbol_value<size>* merged_symbol_value;
955 } u_;
b8e6aad9
ILT
956};
957
14bfc3f5 958// A regular object file. This is size and endian specific.
bae7f79e
ILT
959
960template<int size, bool big_endian>
f6ce93d6 961class Sized_relobj : public Relobj
bae7f79e
ILT
962{
963 public:
c06b7b0b 964 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
730cdc88 965 typedef std::vector<Symbol*> Symbols;
b8e6aad9 966 typedef std::vector<Symbol_value<size> > Local_values;
c06b7b0b 967
f6ce93d6 968 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
bae7f79e
ILT
969 const typename elfcpp::Ehdr<size, big_endian>&);
970
f6ce93d6 971 ~Sized_relobj();
bae7f79e 972
a2fb1b05 973 // Set up the object file based on the ELF header.
bae7f79e
ILT
974 void
975 setup(const typename elfcpp::Ehdr<size, big_endian>&);
976
730cdc88
ILT
977 // If SYM is the index of a global symbol in the object file's
978 // symbol table, return the Symbol object. Otherwise, return NULL.
979 Symbol*
980 global_symbol(unsigned int sym) const
981 {
982 if (sym >= this->local_symbol_count_)
983 {
984 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
985 return this->symbols_[sym - this->local_symbol_count_];
986 }
987 return NULL;
988 }
989
990 // Return the section index of symbol SYM. Set *VALUE to its value
991 // in the object file. Note that for a symbol which is not defined
992 // in this object file, this will set *VALUE to 0 and return
993 // SHN_UNDEF; it will not return the final value of the symbol in
994 // the link.
995 unsigned int
996 symbol_section_and_value(unsigned int sym, Address* value);
997
998 // Return a pointer to the Symbol_value structure which holds the
999 // value of a local symbol.
1000 const Symbol_value<size>*
1001 local_symbol(unsigned int sym) const
1002 {
1003 gold_assert(sym < this->local_values_.size());
1004 return &this->local_values_[sym];
1005 }
1006
c06b7b0b
ILT
1007 // Return the index of local symbol SYM in the ordinary symbol
1008 // table. A value of -1U means that the symbol is not being output.
1009 unsigned int
1010 symtab_index(unsigned int sym) const
1011 {
b8e6aad9
ILT
1012 gold_assert(sym < this->local_values_.size());
1013 return this->local_values_[sym].output_symtab_index();
c06b7b0b
ILT
1014 }
1015
7bf1f802
ILT
1016 // Return the index of local symbol SYM in the dynamic symbol
1017 // table. A value of -1U means that the symbol is not being output.
1018 unsigned int
1019 dynsym_index(unsigned int sym) const
1020 {
1021 gold_assert(sym < this->local_values_.size());
1022 return this->local_values_[sym].output_dynsym_index();
1023 }
1024
6a74a719
ILT
1025 // Return the input section index of local symbol SYM.
1026 unsigned int
1027 local_symbol_input_shndx(unsigned int sym) const
1028 {
1029 gold_assert(sym < this->local_values_.size());
1030 return this->local_values_[sym].input_shndx();
1031 }
1032
e727fa71
ILT
1033 // Return the appropriate Sized_target structure.
1034 Sized_target<size, big_endian>*
1035 sized_target()
1036 {
1037 return this->Object::sized_target
1038 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1039 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
1040 }
1041
1042 // Return the value of the local symbol symndx.
1043 Address
1044 local_symbol_value(unsigned int symndx) const;
1045
7bf1f802
ILT
1046 void
1047 set_needs_output_dynsym_entry(unsigned int sym)
1048 {
1049 gold_assert(sym < this->local_values_.size());
1050 this->local_values_[sym].set_needs_output_dynsym_entry();
1051 }
1052
e727fa71 1053 // Return whether the local symbol SYMNDX has a GOT offset.
07f397ab 1054 // For TLS symbols, the GOT entry will hold its tp-relative offset.
e727fa71
ILT
1055 bool
1056 local_has_got_offset(unsigned int symndx) const
1057 {
1058 return (this->local_got_offsets_.find(symndx)
1059 != this->local_got_offsets_.end());
1060 }
1061
1062 // Return the GOT offset of the local symbol SYMNDX.
1063 unsigned int
1064 local_got_offset(unsigned int symndx) const
1065 {
1066 Local_got_offsets::const_iterator p =
1067 this->local_got_offsets_.find(symndx);
1068 gold_assert(p != this->local_got_offsets_.end());
1069 return p->second;
1070 }
1071
1072 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1073 void
1074 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
1075 {
1076 std::pair<Local_got_offsets::iterator, bool> ins =
1077 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
1078 gold_assert(ins.second);
1079 }
1080
07f397ab
ILT
1081 // Return whether the local TLS symbol SYMNDX has a GOT offset.
1082 // The GOT entry at this offset will contain a module index. If
1083 // NEED_PAIR is true, a second entry immediately following the first
1084 // will contain the dtv-relative offset.
1085 bool
1086 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
1087 {
1088 typename Local_tls_got_offsets::const_iterator p =
1089 this->local_tls_got_offsets_.find(symndx);
1090 if (p == this->local_tls_got_offsets_.end()
1091 || (need_pair && !p->second.have_pair_))
1092 return false;
1093 return true;
1094 }
1095
1096 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
1097 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
1098 // otherwise we need the offset of the GOT entry for the module index.
1099 unsigned int
1100 local_tls_got_offset(unsigned int symndx, bool need_pair) const
1101 {
1102 typename Local_tls_got_offsets::const_iterator p =
1103 this->local_tls_got_offsets_.find(symndx);
1104 gold_assert(p != this->local_tls_got_offsets_.end());
1105 gold_assert(!need_pair || p->second.have_pair_);
1106 return p->second.got_offset_;
1107 }
1108
1109 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
1110 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
1111 // otherwise, we have just a single entry for the module index.
1112 void
1113 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
1114 bool have_pair)
1115 {
1116 typename Local_tls_got_offsets::iterator p =
1117 this->local_tls_got_offsets_.find(symndx);
1118 if (p != this->local_tls_got_offsets_.end())
1119 {
1120 // An entry already existed for this symbol. This can happen
1121 // if we see a relocation asking for the module index before
1122 // a relocation asking for the pair. In that case, the original
1123 // GOT entry will remain, but won't get used by any further
1124 // relocations.
1125 p->second.got_offset_ = got_offset;
1126 gold_assert(have_pair);
1127 p->second.have_pair_ = true;
1128 }
1129 else
1130 {
1131 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
1132 this->local_tls_got_offsets_.insert(
1133 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
1134 gold_assert(ins.second);
1135 }
1136 }
1137
f7e2ee48
ILT
1138 // Return the name of the symbol that spans the given offset in the
1139 // specified section in this object. This is used only for error
1140 // messages and is not particularly efficient.
1141 bool
1142 get_symbol_location_info(unsigned int shndx, off_t offset,
1143 Symbol_location_info* info);
1144
6d013333 1145 protected:
a2fb1b05 1146 // Read the symbols.
bae7f79e 1147 void
12e14209 1148 do_read_symbols(Read_symbols_data*);
14bfc3f5 1149
6d013333
ILT
1150 // Return the number of local symbols.
1151 unsigned int
1152 do_local_symbol_count() const
1153 { return this->local_symbol_count_; }
1154
dbe717ef
ILT
1155 // Lay out the input sections.
1156 void
7e1edb90 1157 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
dbe717ef 1158
12e14209
ILT
1159 // Add the symbols to the symbol table.
1160 void
1161 do_add_symbols(Symbol_table*, Read_symbols_data*);
a2fb1b05 1162
92e059d8
ILT
1163 // Read the relocs.
1164 void
1165 do_read_relocs(Read_relocs_data*);
1166
1167 // Scan the relocs and adjust the symbol table.
1168 void
ead1e424
ILT
1169 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1170 Read_relocs_data*);
92e059d8 1171
7bf1f802
ILT
1172 // Count the local symbols.
1173 void
1174 do_count_local_symbols(Stringpool_template<char>*,
1175 Stringpool_template<char>*);
1176
75f65a3e 1177 // Finalize the local symbols.
c06b7b0b 1178 unsigned int
7bf1f802
ILT
1179 do_finalize_local_symbols(unsigned int, off_t);
1180
1181 // Set the offset where local dynamic symbol information will be stored.
1182 unsigned int
1183 do_set_local_dynsym_indexes(unsigned int);
1184
1185 // Set the offset where local dynamic symbol information will be stored.
1186 unsigned int
1187 do_set_local_dynsym_offset(off_t);
75f65a3e 1188
61ba1cf9
ILT
1189 // Relocate the input sections and write out the local symbols.
1190 void
1191 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
1192 const Layout*, Output_file* of);
1193
a445fddf
ILT
1194 // Get the size of a section.
1195 uint64_t
1196 do_section_size(unsigned int shndx)
1197 { return this->elf_file_.section_size(shndx); }
1198
92e059d8
ILT
1199 // Get the name of a section.
1200 std::string
645f8123
ILT
1201 do_section_name(unsigned int shndx)
1202 { return this->elf_file_.section_name(shndx); }
61ba1cf9 1203
645f8123 1204 // Return the location of the contents of a section.
dbe717ef 1205 Object::Location
645f8123
ILT
1206 do_section_contents(unsigned int shndx)
1207 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 1208
a3ad94ed
ILT
1209 // Return section flags.
1210 uint64_t
1211 do_section_flags(unsigned int shndx)
1212 { return this->elf_file_.section_flags(shndx); }
1213
730cdc88
ILT
1214 // Return section type.
1215 unsigned int
1216 do_section_type(unsigned int shndx)
1217 { return this->elf_file_.section_type(shndx); }
1218
f7e2ee48
ILT
1219 // Return the section link field.
1220 unsigned int
1221 do_section_link(unsigned int shndx)
1222 { return this->elf_file_.section_link(shndx); }
1223
4c50553d
ILT
1224 // Return the section info field.
1225 unsigned int
1226 do_section_info(unsigned int shndx)
1227 { return this->elf_file_.section_info(shndx); }
1228
a445fddf
ILT
1229 // Return the section alignment.
1230 uint64_t
1231 do_section_addralign(unsigned int shndx)
1232 { return this->elf_file_.section_addralign(shndx); }
1233
bae7f79e 1234 private:
a2fb1b05 1235 // For convenience.
f6ce93d6 1236 typedef Sized_relobj<size, big_endian> This;
a2fb1b05
ILT
1237 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1238 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1239 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e
ILT
1240 typedef elfcpp::Shdr<size, big_endian> Shdr;
1241
dbe717ef
ILT
1242 // Find the SHT_SYMTAB section, given the section headers.
1243 void
1244 find_symtab(const unsigned char* pshdrs);
1245
730cdc88
ILT
1246 // Return whether SHDR has the right flags for a GNU style exception
1247 // frame section.
1248 bool
1249 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1250
1251 // Return whether there is a section named .eh_frame which might be
1252 // a GNU style exception frame section.
1253 bool
1254 find_eh_frame(const unsigned char* pshdrs, const char* names,
8383303e 1255 section_size_type names_size) const;
730cdc88 1256
a2fb1b05
ILT
1257 // Whether to include a section group in the link.
1258 bool
6a74a719 1259 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
a2fb1b05
ILT
1260 const elfcpp::Shdr<size, big_endian>&,
1261 std::vector<bool>*);
1262
1263 // Whether to include a linkonce section in the link.
1264 bool
1265 include_linkonce_section(Layout*, const char*,
1266 const elfcpp::Shdr<size, big_endian>&);
1267
61ba1cf9
ILT
1268 // Views and sizes when relocating.
1269 struct View_size
1270 {
1271 unsigned char* view;
1272 typename elfcpp::Elf_types<size>::Elf_Addr address;
1273 off_t offset;
8383303e 1274 section_size_type view_size;
730cdc88 1275 bool is_input_output_view;
96803768 1276 bool is_postprocessing_view;
61ba1cf9
ILT
1277 };
1278
1279 typedef std::vector<View_size> Views;
1280
1281 // Write section data to the output file. Record the views and
1282 // sizes in VIEWS for use when relocating.
1283 void
cb295612 1284 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
61ba1cf9
ILT
1285
1286 // Relocate the sections in the output file.
1287 void
92e059d8
ILT
1288 relocate_sections(const General_options& options, const Symbol_table*,
1289 const Layout*, const unsigned char* pshdrs, Views*);
61ba1cf9 1290
a9a60db6
ILT
1291 // Initialize input to output maps for section symbols in merged
1292 // sections.
1293 void
1294 initialize_input_to_output_maps();
1295
1296 // Free the input to output maps for section symbols in merged
1297 // sections.
1298 void
1299 free_input_to_output_maps();
1300
61ba1cf9
ILT
1301 // Write out the local symbols.
1302 void
b8e6aad9 1303 write_local_symbols(Output_file*,
7bf1f802 1304 const Stringpool_template<char>*,
b8e6aad9 1305 const Stringpool_template<char>*);
61ba1cf9 1306
cb295612
ILT
1307 // Clear the local symbol information.
1308 void
1309 clear_local_symbols()
1310 {
1311 this->local_values_.clear();
1312 this->local_got_offsets_.clear();
1313 this->local_tls_got_offsets_.clear();
1314 }
1315
07f397ab
ILT
1316 // The GOT offsets of local symbols. This map also stores GOT offsets
1317 // for tp-relative offsets for TLS symbols.
e727fa71
ILT
1318 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1319
07f397ab
ILT
1320 // The TLS GOT offsets of local symbols. The map stores the offsets
1321 // for either a single GOT entry that holds the module index of a TLS
1322 // symbol, or a pair of GOT entries containing the module index and
1323 // dtv-relative offset.
1324 struct Tls_got_entry
1325 {
1326 Tls_got_entry(int got_offset, bool have_pair)
1327 : got_offset_(got_offset),
1328 have_pair_(have_pair)
1329 { }
1330 int got_offset_;
1331 bool have_pair_;
1332 };
1333 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1334
645f8123
ILT
1335 // General access to the ELF file.
1336 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
bae7f79e 1337 // Index of SHT_SYMTAB section.
645f8123 1338 unsigned int symtab_shndx_;
61ba1cf9
ILT
1339 // The number of local symbols.
1340 unsigned int local_symbol_count_;
1341 // The number of local symbols which go into the output file.
1342 unsigned int output_local_symbol_count_;
7bf1f802
ILT
1343 // The number of local symbols which go into the output file's dynamic
1344 // symbol table.
1345 unsigned int output_local_dynsym_count_;
14bfc3f5 1346 // The entries in the symbol table for the external symbols.
730cdc88 1347 Symbols symbols_;
75f65a3e
ILT
1348 // File offset for local symbols.
1349 off_t local_symbol_offset_;
7bf1f802
ILT
1350 // File offset for local dynamic symbols.
1351 off_t local_dynsym_offset_;
61ba1cf9 1352 // Values of local symbols.
c06b7b0b 1353 Local_values local_values_;
07f397ab
ILT
1354 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1355 // for TLS symbols, indexed by symbol number.
e727fa71 1356 Local_got_offsets local_got_offsets_;
07f397ab
ILT
1357 // GOT offsets for local TLS symbols, indexed by symbol number
1358 // and GOT entry type.
1359 Local_tls_got_offsets local_tls_got_offsets_;
730cdc88
ILT
1360 // Whether this object has a GNU style .eh_frame section.
1361 bool has_eh_frame_;
bae7f79e
ILT
1362};
1363
54dc6425 1364// A class to manage the list of all objects.
a2fb1b05 1365
54dc6425
ILT
1366class Input_objects
1367{
1368 public:
1369 Input_objects()
fbfba508 1370 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_()
54dc6425
ILT
1371 { }
1372
f6ce93d6
ILT
1373 // The type of the list of input relocateable objects.
1374 typedef std::vector<Relobj*> Relobj_list;
1375 typedef Relobj_list::const_iterator Relobj_iterator;
1376
1377 // The type of the list of input dynamic objects.
1378 typedef std::vector<Dynobj*> Dynobj_list;
1379 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425 1380
008db82e
ILT
1381 // Add an object to the list. Return true if all is well, or false
1382 // if this object should be ignored.
1383 bool
54dc6425
ILT
1384 add_object(Object*);
1385
e2827e5f
ILT
1386 // For each dynamic object, check whether we've seen all of its
1387 // explicit dependencies.
1388 void
1389 check_dynamic_dependencies() const;
1390
9a2d6984
ILT
1391 // Return whether an object was found in the system library
1392 // directory.
1393 bool
1394 found_in_system_library_directory(const Object*) const;
1395
f6ce93d6
ILT
1396 // Iterate over all regular objects.
1397
1398 Relobj_iterator
1399 relobj_begin() const
1400 { return this->relobj_list_.begin(); }
1401
1402 Relobj_iterator
1403 relobj_end() const
1404 { return this->relobj_list_.end(); }
1405
1406 // Iterate over all dynamic objects.
1407
1408 Dynobj_iterator
1409 dynobj_begin() const
1410 { return this->dynobj_list_.begin(); }
54dc6425 1411
f6ce93d6
ILT
1412 Dynobj_iterator
1413 dynobj_end() const
1414 { return this->dynobj_list_.end(); }
54dc6425
ILT
1415
1416 // Return whether we have seen any dynamic objects.
1417 bool
1418 any_dynamic() const
f6ce93d6 1419 { return !this->dynobj_list_.empty(); }
54dc6425 1420
fe9a4c12
ILT
1421 // Return the number of input objects.
1422 int
1423 number_of_input_objects() const
1424 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1425
54dc6425
ILT
1426 private:
1427 Input_objects(const Input_objects&);
1428 Input_objects& operator=(const Input_objects&);
1429
008db82e 1430 // The list of ordinary objects included in the link.
f6ce93d6 1431 Relobj_list relobj_list_;
008db82e 1432 // The list of dynamic objects included in the link.
f6ce93d6 1433 Dynobj_list dynobj_list_;
008db82e
ILT
1434 // SONAMEs that we have seen.
1435 Unordered_set<std::string> sonames_;
9a2d6984
ILT
1436 // The directory in which we find the libc.so.
1437 std::string system_library_directory_;
54dc6425 1438};
a2fb1b05 1439
92e059d8
ILT
1440// Some of the information we pass to the relocation routines. We
1441// group this together to avoid passing a dozen different arguments.
1442
1443template<int size, bool big_endian>
1444struct Relocate_info
1445{
1446 // Command line options.
1447 const General_options* options;
1448 // Symbol table.
1449 const Symbol_table* symtab;
1450 // Layout.
1451 const Layout* layout;
1452 // Object being relocated.
f6ce93d6 1453 Sized_relobj<size, big_endian>* object;
92e059d8
ILT
1454 // Section index of relocation section.
1455 unsigned int reloc_shndx;
1456 // Section index of section being relocated.
1457 unsigned int data_shndx;
1458
1459 // Return a string showing the location of a relocation. This is
1460 // only used for error messages.
1461 std::string
1462 location(size_t relnum, off_t reloffset) const;
1463};
1464
bae7f79e
ILT
1465// Return an Object appropriate for the input file. P is BYTES long,
1466// and holds the ELF header.
1467
61ba1cf9
ILT
1468extern Object*
1469make_elf_object(const std::string& name, Input_file*,
1470 off_t offset, const unsigned char* p,
8383303e 1471 section_offset_type bytes);
bae7f79e
ILT
1472
1473} // end namespace gold
1474
1475#endif // !defined(GOLD_OBJECT_H)