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