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