]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/object.h
*** empty log message ***
[thirdparty/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 {
7f649c59
ILT
825 // This is a relocation against a section symbol. ADDEND is the
826 // offset in the section. The result should be the start of some
827 // merge area. If the object file wants something else, it should
828 // use a regular symbol rather than a section symbol.
829 // Unfortunately, PR 6658 shows a case in which the object file
830 // refers to the section symbol, but uses a negative ADDEND to
831 // compensate for a PC relative reloc. We can't handle the
832 // general case. However, we can handle the special case of a
833 // negative addend, by assuming that it refers to the start of the
834 // section. Of course, that means that we have to guess when
835 // ADDEND is negative. It is normal to see a 32-bit value here
836 // even when the template parameter size is 64, as 64-bit object
837 // file formats have 32-bit relocations. We know this is a merge
838 // section, so we know it has to fit into memory. So we assume
839 // that we won't see a value larger than a large 32-bit unsigned
840 // value. This will break objects with very very large merge
841 // sections; they probably break in other ways anyhow.
842 Value input_offset = this->input_value_;
843 if (addend < 0xffffff00)
844 {
845 input_offset += addend;
846 addend = 0;
847 }
a9a60db6
ILT
848 typename Output_addresses::const_iterator p =
849 this->output_addresses_.find(input_offset);
850 if (p != this->output_addresses_.end())
7f649c59 851 return p->second + addend;
a9a60db6 852
7f649c59
ILT
853 return (this->value_from_output_section(object, input_shndx, input_offset)
854 + addend);
a9a60db6
ILT
855 }
856
857 private:
858 // Get the output value for an input offset if we couldn't find it
859 // in the hash table.
860 Value
861 value_from_output_section(const Relobj*, unsigned int input_shndx,
862 Value input_offset) const;
863
864 // The value of the section symbol in the input file. This is
865 // normally zero, but could in principle be something else.
866 Value input_value_;
867 // The start address of this merged section in the output file.
868 Value output_start_address_;
869 // A hash table which maps offsets in the input section to output
870 // addresses. This only maps specific offsets, not all offsets.
871 Output_addresses output_addresses_;
872};
873
b8e6aad9
ILT
874// This POD class is holds the value of a symbol. This is used for
875// local symbols, and for all symbols during relocation processing.
730cdc88
ILT
876// For special sections, such as SHF_MERGE sections, this calls a
877// function to get the final symbol value.
b8e6aad9
ILT
878
879template<int size>
880class Symbol_value
881{
882 public:
883 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
884
885 Symbol_value()
7bf1f802 886 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
d491d34e
ILT
887 is_ordinary_shndx_(false), is_section_symbol_(false),
888 is_tls_symbol_(false), has_output_value_(true)
a9a60db6 889 { this->u_.value = 0; }
b8e6aad9
ILT
890
891 // Get the value of this symbol. OBJECT is the object in which this
892 // symbol is defined, and ADDEND is an addend to add to the value.
893 template<bool big_endian>
894 Value
895 value(const Sized_relobj<size, big_endian>* object, Value addend) const
896 {
a9a60db6
ILT
897 if (this->has_output_value_)
898 return this->u_.value + addend;
899 else
d491d34e
ILT
900 {
901 gold_assert(this->is_ordinary_shndx_);
902 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
903 addend);
904 }
b8e6aad9
ILT
905 }
906
907 // Set the value of this symbol in the output symbol table.
908 void
909 set_output_value(Value value)
a9a60db6
ILT
910 { this->u_.value = value; }
911
912 // For a section symbol in a merged section, we need more
913 // information.
914 void
915 set_merged_symbol_value(Merged_symbol_value<size>* msv)
b8e6aad9 916 {
a9a60db6
ILT
917 gold_assert(this->is_section_symbol_);
918 this->has_output_value_ = false;
919 this->u_.merged_symbol_value = msv;
b8e6aad9
ILT
920 }
921
a9a60db6
ILT
922 // Initialize the input to output map for a section symbol in a
923 // merged section. We also initialize the value of a non-section
924 // symbol in a merged section.
b8e6aad9 925 void
a9a60db6 926 initialize_input_to_output_map(const Relobj* object)
b8e6aad9 927 {
a9a60db6
ILT
928 if (!this->has_output_value_)
929 {
d491d34e 930 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
a9a60db6
ILT
931 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
932 msv->initialize_input_to_output_map(object, this->input_shndx_);
933 }
b8e6aad9
ILT
934 }
935
a9a60db6
ILT
936 // Free the input to output map for a section symbol in a merged
937 // section.
938 void
939 free_input_to_output_map()
7bf1f802 940 {
a9a60db6
ILT
941 if (!this->has_output_value_)
942 this->u_.merged_symbol_value->free_input_to_output_map();
7bf1f802
ILT
943 }
944
a9a60db6
ILT
945 // Set the value of the symbol from the input file. This is only
946 // called by count_local_symbols, to communicate the value to
947 // finalize_local_symbols.
948 void
949 set_input_value(Value value)
950 { this->u_.value = value; }
951
952 // Return the input value. This is only called by
e94cf127 953 // finalize_local_symbols and (in special cases) relocate_section.
a9a60db6
ILT
954 Value
955 input_value() const
956 { return this->u_.value; }
957
b8e6aad9
ILT
958 // Return whether this symbol should go into the output symbol
959 // table.
960 bool
961 needs_output_symtab_entry() const
ac1f0c21 962 { return this->output_symtab_index_ != -1U; }
b8e6aad9
ILT
963
964 // Return the index in the output symbol table.
965 unsigned int
966 output_symtab_index() const
967 {
968 gold_assert(this->output_symtab_index_ != 0);
969 return this->output_symtab_index_;
970 }
971
972 // Set the index in the output symbol table.
973 void
974 set_output_symtab_index(unsigned int i)
975 {
976 gold_assert(this->output_symtab_index_ == 0);
977 this->output_symtab_index_ = i;
978 }
979
980 // Record that this symbol should not go into the output symbol
981 // table.
982 void
983 set_no_output_symtab_entry()
984 {
985 gold_assert(this->output_symtab_index_ == 0);
986 this->output_symtab_index_ = -1U;
987 }
988
7bf1f802
ILT
989 // Set the index in the output dynamic symbol table.
990 void
991 set_needs_output_dynsym_entry()
992 {
dceae3c1 993 gold_assert(!this->is_section_symbol());
7bf1f802
ILT
994 this->output_dynsym_index_ = 0;
995 }
996
997 // Return whether this symbol should go into the output symbol
998 // table.
999 bool
1000 needs_output_dynsym_entry() const
1001 {
1002 return this->output_dynsym_index_ != -1U;
1003 }
1004
1005 // Record that this symbol should go into the dynamic symbol table.
1006 void
1007 set_output_dynsym_index(unsigned int i)
1008 {
1009 gold_assert(this->output_dynsym_index_ == 0);
1010 this->output_dynsym_index_ = i;
1011 }
1012
1013 // Return the index in the output dynamic symbol table.
1014 unsigned int
1015 output_dynsym_index() const
1016 {
dceae3c1
ILT
1017 gold_assert(this->output_dynsym_index_ != 0
1018 && this->output_dynsym_index_ != -1U);
7bf1f802
ILT
1019 return this->output_dynsym_index_;
1020 }
1021
b8e6aad9
ILT
1022 // Set the index of the input section in the input file.
1023 void
d491d34e 1024 set_input_shndx(unsigned int i, bool is_ordinary)
730cdc88
ILT
1025 {
1026 this->input_shndx_ = i;
ac1f0c21
ILT
1027 // input_shndx_ field is a bitfield, so make sure that the value
1028 // fits.
730cdc88 1029 gold_assert(this->input_shndx_ == i);
d491d34e 1030 this->is_ordinary_shndx_ = is_ordinary;
730cdc88 1031 }
b8e6aad9 1032
7bf1f802
ILT
1033 // Return the index of the input section in the input file.
1034 unsigned int
d491d34e
ILT
1035 input_shndx(bool* is_ordinary) const
1036 {
1037 *is_ordinary = this->is_ordinary_shndx_;
1038 return this->input_shndx_;
1039 }
7bf1f802 1040
a9a60db6
ILT
1041 // Whether this is a section symbol.
1042 bool
1043 is_section_symbol() const
1044 { return this->is_section_symbol_; }
1045
063f12a8
ILT
1046 // Record that this is a section symbol.
1047 void
1048 set_is_section_symbol()
dceae3c1
ILT
1049 {
1050 gold_assert(!this->needs_output_dynsym_entry());
1051 this->is_section_symbol_ = true;
1052 }
063f12a8 1053
7bf1f802
ILT
1054 // Record that this is a TLS symbol.
1055 void
1056 set_is_tls_symbol()
1057 { this->is_tls_symbol_ = true; }
1058
1059 // Return TRUE if this is a TLS symbol.
1060 bool
1061 is_tls_symbol() const
1062 { return this->is_tls_symbol_; }
1063
b8e6aad9
ILT
1064 private:
1065 // The index of this local symbol in the output symbol table. This
1066 // will be -1 if the symbol should not go into the symbol table.
1067 unsigned int output_symtab_index_;
7bf1f802
ILT
1068 // The index of this local symbol in the dynamic symbol table. This
1069 // will be -1 if the symbol should not go into the symbol table.
1070 unsigned int output_dynsym_index_;
b8e6aad9
ILT
1071 // The section index in the input file in which this symbol is
1072 // defined.
d491d34e
ILT
1073 unsigned int input_shndx_ : 28;
1074 // Whether the section index is an ordinary index, not a special
1075 // value.
1076 bool is_ordinary_shndx_ : 1;
063f12a8
ILT
1077 // Whether this is a STT_SECTION symbol.
1078 bool is_section_symbol_ : 1;
7bf1f802
ILT
1079 // Whether this is a STT_TLS symbol.
1080 bool is_tls_symbol_ : 1;
a9a60db6
ILT
1081 // Whether this symbol has a value for the output file. This is
1082 // normally set to true during Layout::finalize, by
1083 // finalize_local_symbols. It will be false for a section symbol in
1084 // a merge section, as for such symbols we can not determine the
1085 // value to use in a relocation until we see the addend.
1086 bool has_output_value_ : 1;
1087 union
1088 {
1089 // This is used if has_output_value_ is true. Between
1090 // count_local_symbols and finalize_local_symbols, this is the
1091 // value in the input file. After finalize_local_symbols, it is
1092 // the value in the output file.
1093 Value value;
1094 // This is used if has_output_value_ is false. It points to the
1095 // information we need to get the value for a merge section.
1096 Merged_symbol_value<size>* merged_symbol_value;
1097 } u_;
b8e6aad9
ILT
1098};
1099
0a65a3a7
CC
1100// A GOT offset list. A symbol may have more than one GOT offset
1101// (e.g., when mixing modules compiled with two different TLS models),
1102// but will usually have at most one. GOT_TYPE identifies the type of
1103// GOT entry; its values are specific to each target.
1104
1105class Got_offset_list
1106{
1107 public:
1108 Got_offset_list()
1109 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1110 { }
1111
1112 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1113 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1114 { }
1115
1116 ~Got_offset_list()
1117 {
1118 if (this->got_next_ != NULL)
1119 {
1120 delete this->got_next_;
1121 this->got_next_ = NULL;
1122 }
1123 }
1124
1125 // Initialize the fields to their default values.
1126 void
1127 init()
1128 {
1129 this->got_type_ = -1U;
1130 this->got_offset_ = 0;
1131 this->got_next_ = NULL;
1132 }
1133
1134 // Set the offset for the GOT entry of type GOT_TYPE.
1135 void
1136 set_offset(unsigned int got_type, unsigned int got_offset)
1137 {
1138 if (this->got_type_ == -1U)
1139 {
1140 this->got_type_ = got_type;
1141 this->got_offset_ = got_offset;
1142 }
1143 else
1144 {
1145 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1146 {
1147 if (g->got_type_ == got_type)
1148 {
1149 g->got_offset_ = got_offset;
1150 return;
1151 }
1152 }
1153 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1154 g->got_next_ = this->got_next_;
1155 this->got_next_ = g;
1156 }
1157 }
1158
1159 // Return the offset for a GOT entry of type GOT_TYPE.
1160 unsigned int
1161 get_offset(unsigned int got_type) const
1162 {
1163 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1164 {
1165 if (g->got_type_ == got_type)
1166 return g->got_offset_;
1167 }
1168 return -1U;
1169 }
1170
1171 private:
1172 unsigned int got_type_;
1173 unsigned int got_offset_;
1174 Got_offset_list* got_next_;
1175};
1176
14bfc3f5 1177// A regular object file. This is size and endian specific.
bae7f79e
ILT
1178
1179template<int size, bool big_endian>
f6ce93d6 1180class Sized_relobj : public Relobj
bae7f79e
ILT
1181{
1182 public:
c06b7b0b 1183 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
730cdc88 1184 typedef std::vector<Symbol*> Symbols;
b8e6aad9 1185 typedef std::vector<Symbol_value<size> > Local_values;
c06b7b0b 1186
f6ce93d6 1187 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
bae7f79e
ILT
1188 const typename elfcpp::Ehdr<size, big_endian>&);
1189
f6ce93d6 1190 ~Sized_relobj();
bae7f79e 1191
a2fb1b05 1192 // Set up the object file based on the ELF header.
bae7f79e
ILT
1193 void
1194 setup(const typename elfcpp::Ehdr<size, big_endian>&);
1195
7d9e3d98
ILT
1196 // Return the number of symbols. This is only valid after
1197 // Object::add_symbols has been called.
1198 unsigned int
1199 symbol_count() const
1200 { return this->local_symbol_count_ + this->symbols_.size(); }
1201
730cdc88
ILT
1202 // If SYM is the index of a global symbol in the object file's
1203 // symbol table, return the Symbol object. Otherwise, return NULL.
1204 Symbol*
1205 global_symbol(unsigned int sym) const
1206 {
1207 if (sym >= this->local_symbol_count_)
1208 {
1209 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1210 return this->symbols_[sym - this->local_symbol_count_];
1211 }
1212 return NULL;
1213 }
1214
1215 // Return the section index of symbol SYM. Set *VALUE to its value
d491d34e
ILT
1216 // in the object file. Set *IS_ORDINARY if this is an ordinary
1217 // section index, not a special code between SHN_LORESERVE and
1218 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1219 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1220 // it will not return the final value of the symbol in the link.
730cdc88 1221 unsigned int
d491d34e 1222 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
730cdc88
ILT
1223
1224 // Return a pointer to the Symbol_value structure which holds the
1225 // value of a local symbol.
1226 const Symbol_value<size>*
1227 local_symbol(unsigned int sym) const
1228 {
1229 gold_assert(sym < this->local_values_.size());
1230 return &this->local_values_[sym];
1231 }
1232
c06b7b0b
ILT
1233 // Return the index of local symbol SYM in the ordinary symbol
1234 // table. A value of -1U means that the symbol is not being output.
1235 unsigned int
1236 symtab_index(unsigned int sym) const
1237 {
b8e6aad9
ILT
1238 gold_assert(sym < this->local_values_.size());
1239 return this->local_values_[sym].output_symtab_index();
c06b7b0b
ILT
1240 }
1241
7bf1f802
ILT
1242 // Return the index of local symbol SYM in the dynamic symbol
1243 // table. A value of -1U means that the symbol is not being output.
1244 unsigned int
1245 dynsym_index(unsigned int sym) const
1246 {
1247 gold_assert(sym < this->local_values_.size());
1248 return this->local_values_[sym].output_dynsym_index();
1249 }
1250
6a74a719
ILT
1251 // Return the input section index of local symbol SYM.
1252 unsigned int
d491d34e 1253 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
6a74a719
ILT
1254 {
1255 gold_assert(sym < this->local_values_.size());
d491d34e 1256 return this->local_values_[sym].input_shndx(is_ordinary);
6a74a719
ILT
1257 }
1258
e727fa71
ILT
1259 // Return the appropriate Sized_target structure.
1260 Sized_target<size, big_endian>*
1261 sized_target()
7d1a9ebb 1262 { return this->Object::sized_target<size, big_endian>(); }
e727fa71 1263
d1f003c6 1264 // Record that local symbol SYM needs a dynamic symbol entry.
7bf1f802
ILT
1265 void
1266 set_needs_output_dynsym_entry(unsigned int sym)
1267 {
1268 gold_assert(sym < this->local_values_.size());
1269 this->local_values_[sym].set_needs_output_dynsym_entry();
1270 }
1271
e727fa71 1272 // Return whether the local symbol SYMNDX has a GOT offset.
07f397ab 1273 // For TLS symbols, the GOT entry will hold its tp-relative offset.
e727fa71 1274 bool
0a65a3a7 1275 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
e727fa71 1276 {
0a65a3a7
CC
1277 Local_got_offsets::const_iterator p =
1278 this->local_got_offsets_.find(symndx);
1279 return (p != this->local_got_offsets_.end()
1280 && p->second->get_offset(got_type) != -1U);
e727fa71
ILT
1281 }
1282
1283 // Return the GOT offset of the local symbol SYMNDX.
1284 unsigned int
0a65a3a7 1285 local_got_offset(unsigned int symndx, unsigned int got_type) const
e727fa71
ILT
1286 {
1287 Local_got_offsets::const_iterator p =
1288 this->local_got_offsets_.find(symndx);
1289 gold_assert(p != this->local_got_offsets_.end());
0a65a3a7
CC
1290 unsigned int off = p->second->get_offset(got_type);
1291 gold_assert(off != -1U);
1292 return off;
e727fa71
ILT
1293 }
1294
1295 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1296 void
0a65a3a7
CC
1297 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1298 unsigned int got_offset)
07f397ab 1299 {
0a65a3a7
CC
1300 Local_got_offsets::const_iterator p =
1301 this->local_got_offsets_.find(symndx);
1302 if (p != this->local_got_offsets_.end())
1303 p->second->set_offset(got_type, got_offset);
07f397ab
ILT
1304 else
1305 {
0a65a3a7
CC
1306 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1307 std::pair<Local_got_offsets::iterator, bool> ins =
1308 this->local_got_offsets_.insert(std::make_pair(symndx, g));
07f397ab
ILT
1309 gold_assert(ins.second);
1310 }
1311 }
1312
ef9beddf
ILT
1313 // Get the offset of input section SHNDX within its output section.
1314 // This is -1 if the input section requires a special mapping, such
1315 // as a merge section. The output section can be found in the
1316 // output_sections_ field of the parent class Relobj.
1317 Address
1318 get_output_section_offset(unsigned int shndx) const
1319 {
1320 gold_assert(shndx < this->section_offsets_.size());
1321 return this->section_offsets_[shndx];
1322 }
1323
f7e2ee48
ILT
1324 // Return the name of the symbol that spans the given offset in the
1325 // specified section in this object. This is used only for error
1326 // messages and is not particularly efficient.
1327 bool
1328 get_symbol_location_info(unsigned int shndx, off_t offset,
1329 Symbol_location_info* info);
1330
e94cf127
CC
1331 // Look for a kept section corresponding to the given discarded section,
1332 // and return its output address. This is used only for relocations in
1333 // debugging sections.
1334 Address
1335 map_to_kept_section(unsigned int shndx, bool* found) const;
1336
6d013333 1337 protected:
a2fb1b05 1338 // Read the symbols.
bae7f79e 1339 void
12e14209 1340 do_read_symbols(Read_symbols_data*);
14bfc3f5 1341
6d013333
ILT
1342 // Return the number of local symbols.
1343 unsigned int
1344 do_local_symbol_count() const
1345 { return this->local_symbol_count_; }
1346
dbe717ef
ILT
1347 // Lay out the input sections.
1348 void
7e1edb90 1349 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
dbe717ef 1350
12e14209
ILT
1351 // Add the symbols to the symbol table.
1352 void
1353 do_add_symbols(Symbol_table*, Read_symbols_data*);
a2fb1b05 1354
92e059d8
ILT
1355 // Read the relocs.
1356 void
1357 do_read_relocs(Read_relocs_data*);
1358
1359 // Scan the relocs and adjust the symbol table.
1360 void
ead1e424
ILT
1361 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1362 Read_relocs_data*);
92e059d8 1363
7bf1f802
ILT
1364 // Count the local symbols.
1365 void
1366 do_count_local_symbols(Stringpool_template<char>*,
1367 Stringpool_template<char>*);
1368
75f65a3e 1369 // Finalize the local symbols.
c06b7b0b 1370 unsigned int
7bf1f802
ILT
1371 do_finalize_local_symbols(unsigned int, off_t);
1372
1373 // Set the offset where local dynamic symbol information will be stored.
1374 unsigned int
1375 do_set_local_dynsym_indexes(unsigned int);
1376
1377 // Set the offset where local dynamic symbol information will be stored.
1378 unsigned int
1379 do_set_local_dynsym_offset(off_t);
75f65a3e 1380
61ba1cf9
ILT
1381 // Relocate the input sections and write out the local symbols.
1382 void
1383 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
1384 const Layout*, Output_file* of);
1385
a445fddf
ILT
1386 // Get the size of a section.
1387 uint64_t
1388 do_section_size(unsigned int shndx)
1389 { return this->elf_file_.section_size(shndx); }
1390
92e059d8
ILT
1391 // Get the name of a section.
1392 std::string
645f8123
ILT
1393 do_section_name(unsigned int shndx)
1394 { return this->elf_file_.section_name(shndx); }
61ba1cf9 1395
645f8123 1396 // Return the location of the contents of a section.
dbe717ef 1397 Object::Location
645f8123
ILT
1398 do_section_contents(unsigned int shndx)
1399 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 1400
a3ad94ed
ILT
1401 // Return section flags.
1402 uint64_t
1403 do_section_flags(unsigned int shndx)
1404 { return this->elf_file_.section_flags(shndx); }
1405
88dd47ac
ILT
1406 // Return section address.
1407 uint64_t
1408 do_section_address(unsigned int shndx)
1409 { return this->elf_file_.section_addr(shndx); }
1410
730cdc88
ILT
1411 // Return section type.
1412 unsigned int
1413 do_section_type(unsigned int shndx)
1414 { return this->elf_file_.section_type(shndx); }
1415
f7e2ee48
ILT
1416 // Return the section link field.
1417 unsigned int
1418 do_section_link(unsigned int shndx)
1419 { return this->elf_file_.section_link(shndx); }
1420
4c50553d
ILT
1421 // Return the section info field.
1422 unsigned int
1423 do_section_info(unsigned int shndx)
1424 { return this->elf_file_.section_info(shndx); }
1425
a445fddf
ILT
1426 // Return the section alignment.
1427 uint64_t
1428 do_section_addralign(unsigned int shndx)
1429 { return this->elf_file_.section_addralign(shndx); }
1430
d491d34e
ILT
1431 // Return the Xindex structure to use.
1432 Xindex*
1433 do_initialize_xindex();
1434
92de84a6
ILT
1435 // Get symbol counts.
1436 void
1437 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1438
ef9beddf
ILT
1439 // Get the offset of a section.
1440 uint64_t
1441 do_output_section_offset(unsigned int shndx) const
1442 { return this->get_output_section_offset(shndx); }
1443
1444 // Set the offset of a section.
1445 void
1446 do_set_section_offset(unsigned int shndx, uint64_t off)
1447 {
1448 gold_assert(shndx < this->section_offsets_.size());
1449 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1450 }
1451
bae7f79e 1452 private:
a2fb1b05 1453 // For convenience.
f6ce93d6 1454 typedef Sized_relobj<size, big_endian> This;
a2fb1b05
ILT
1455 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1456 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1457 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e
ILT
1458 typedef elfcpp::Shdr<size, big_endian> Shdr;
1459
ef9beddf
ILT
1460 // To keep track of discarded comdat sections, we need to map a member
1461 // section index to the object and section index of the corresponding
1462 // kept section.
1463 struct Kept_comdat_section
1464 {
1465 Kept_comdat_section(Sized_relobj<size, big_endian>* object,
1466 unsigned int shndx)
1467 : object_(object), shndx_(shndx)
1468 { }
1469 Sized_relobj<size, big_endian>* object_;
1470 unsigned int shndx_;
1471 };
1472 typedef std::map<unsigned int, Kept_comdat_section*>
1473 Kept_comdat_section_table;
1474
1475 // Information needed to keep track of kept comdat groups. This is
1476 // simply a map from the section name to its section index. This may
1477 // not be a one-to-one mapping, but we ignore that possibility since
1478 // this is used only to attempt to handle stray relocations from
1479 // non-comdat debug sections that refer to comdat loadable sections.
1480 typedef Unordered_map<std::string, unsigned int> Comdat_group;
1481
1482 // A map from group section index to the table of group members.
1483 typedef std::map<unsigned int, Comdat_group*> Comdat_group_table;
1484
1485 // Find a comdat group table given its group section SHNDX.
1486 Comdat_group*
1487 find_comdat_group(unsigned int shndx) const
1488 {
1489 Comdat_group_table::const_iterator p =
1490 this->comdat_groups_.find(shndx);
1491 if (p != this->comdat_groups_.end())
1492 return p->second;
1493 return NULL;
1494 }
1495
1496 // Record a new comdat group whose group section index is SHNDX.
1497 void
1498 add_comdat_group(unsigned int shndx, Comdat_group* group)
1499 { this->comdat_groups_[shndx] = group; }
1500
d491d34e
ILT
1501 // Adjust a section index if necessary.
1502 unsigned int
1503 adjust_shndx(unsigned int shndx)
1504 {
1505 if (shndx >= elfcpp::SHN_LORESERVE)
1506 shndx += this->elf_file_.large_shndx_offset();
1507 return shndx;
1508 }
1509
dbe717ef
ILT
1510 // Find the SHT_SYMTAB section, given the section headers.
1511 void
1512 find_symtab(const unsigned char* pshdrs);
1513
730cdc88
ILT
1514 // Return whether SHDR has the right flags for a GNU style exception
1515 // frame section.
1516 bool
1517 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1518
1519 // Return whether there is a section named .eh_frame which might be
1520 // a GNU style exception frame section.
1521 bool
1522 find_eh_frame(const unsigned char* pshdrs, const char* names,
8383303e 1523 section_size_type names_size) const;
730cdc88 1524
a2fb1b05
ILT
1525 // Whether to include a section group in the link.
1526 bool
6a74a719 1527 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
e94cf127 1528 const unsigned char*, const char *, section_size_type,
a2fb1b05
ILT
1529 std::vector<bool>*);
1530
1531 // Whether to include a linkonce section in the link.
1532 bool
e94cf127 1533 include_linkonce_section(Layout*, unsigned int, const char*,
a2fb1b05
ILT
1534 const elfcpp::Shdr<size, big_endian>&);
1535
61ba1cf9
ILT
1536 // Views and sizes when relocating.
1537 struct View_size
1538 {
1539 unsigned char* view;
1540 typename elfcpp::Elf_types<size>::Elf_Addr address;
1541 off_t offset;
8383303e 1542 section_size_type view_size;
730cdc88 1543 bool is_input_output_view;
96803768 1544 bool is_postprocessing_view;
61ba1cf9
ILT
1545 };
1546
1547 typedef std::vector<View_size> Views;
1548
1549 // Write section data to the output file. Record the views and
1550 // sizes in VIEWS for use when relocating.
1551 void
cb295612 1552 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
61ba1cf9
ILT
1553
1554 // Relocate the sections in the output file.
1555 void
92e059d8
ILT
1556 relocate_sections(const General_options& options, const Symbol_table*,
1557 const Layout*, const unsigned char* pshdrs, Views*);
61ba1cf9 1558
7019cd25
ILT
1559 // Scan the input relocations for --emit-relocs.
1560 void
1561 emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
1562 const unsigned char* plocal_syms,
1563 const Read_relocs_data::Relocs_list::iterator&);
1564
1565 // Scan the input relocations for --emit-relocs, templatized on the
1566 // type of the relocation section.
1567 template<int sh_type>
1568 void
1569 emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
1570 const unsigned char* plocal_syms,
1571 const Read_relocs_data::Relocs_list::iterator&,
1572 Relocatable_relocs*);
1573
1574 // Emit the relocs for --emit-relocs.
1575 void
1576 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1577 unsigned int sh_type, const unsigned char* prelocs,
ef9beddf 1578 size_t reloc_count, Output_section*, Address output_offset,
7019cd25
ILT
1579 unsigned char* view, Address address,
1580 section_size_type view_size,
1581 unsigned char* reloc_view, section_size_type reloc_view_size);
1582
1583 // Emit the relocs for --emit-relocs, templatized on the type of the
1584 // relocation section.
1585 template<int sh_type>
1586 void
1587 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1588 const unsigned char* prelocs, size_t reloc_count,
ef9beddf 1589 Output_section*, Address output_offset,
7019cd25
ILT
1590 unsigned char* view, Address address,
1591 section_size_type view_size,
1592 unsigned char* reloc_view,
1593 section_size_type reloc_view_size);
1594
a9a60db6
ILT
1595 // Initialize input to output maps for section symbols in merged
1596 // sections.
1597 void
1598 initialize_input_to_output_maps();
1599
1600 // Free the input to output maps for section symbols in merged
1601 // sections.
1602 void
1603 free_input_to_output_maps();
1604
61ba1cf9
ILT
1605 // Write out the local symbols.
1606 void
b8e6aad9 1607 write_local_symbols(Output_file*,
7bf1f802 1608 const Stringpool_template<char>*,
d491d34e
ILT
1609 const Stringpool_template<char>*,
1610 Output_symtab_xindex*,
1611 Output_symtab_xindex*);
61ba1cf9 1612
cb295612
ILT
1613 // Clear the local symbol information.
1614 void
1615 clear_local_symbols()
1616 {
1617 this->local_values_.clear();
1618 this->local_got_offsets_.clear();
cb295612
ILT
1619 }
1620
ef9beddf
ILT
1621 // Record a mapping from discarded section SHNDX to the corresponding
1622 // kept section.
1623 void
1624 set_kept_comdat_section(unsigned int shndx, Kept_comdat_section* kept)
1625 {
1626 this->kept_comdat_sections_[shndx] = kept;
1627 }
1628
1629 // Find the kept section corresponding to the discarded section SHNDX.
1630 Kept_comdat_section*
1631 get_kept_comdat_section(unsigned int shndx) const
1632 {
1633 typename Kept_comdat_section_table::const_iterator p =
1634 this->kept_comdat_sections_.find(shndx);
1635 if (p == this->kept_comdat_sections_.end())
1636 return NULL;
1637 return p->second;
1638 }
1639
07f397ab
ILT
1640 // The GOT offsets of local symbols. This map also stores GOT offsets
1641 // for tp-relative offsets for TLS symbols.
0a65a3a7 1642 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
e727fa71 1643
07f397ab
ILT
1644 // The TLS GOT offsets of local symbols. The map stores the offsets
1645 // for either a single GOT entry that holds the module index of a TLS
1646 // symbol, or a pair of GOT entries containing the module index and
1647 // dtv-relative offset.
1648 struct Tls_got_entry
1649 {
1650 Tls_got_entry(int got_offset, bool have_pair)
1651 : got_offset_(got_offset),
1652 have_pair_(have_pair)
1653 { }
1654 int got_offset_;
1655 bool have_pair_;
1656 };
1657 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1658
645f8123
ILT
1659 // General access to the ELF file.
1660 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
bae7f79e 1661 // Index of SHT_SYMTAB section.
645f8123 1662 unsigned int symtab_shndx_;
61ba1cf9
ILT
1663 // The number of local symbols.
1664 unsigned int local_symbol_count_;
1665 // The number of local symbols which go into the output file.
1666 unsigned int output_local_symbol_count_;
7bf1f802
ILT
1667 // The number of local symbols which go into the output file's dynamic
1668 // symbol table.
1669 unsigned int output_local_dynsym_count_;
14bfc3f5 1670 // The entries in the symbol table for the external symbols.
730cdc88 1671 Symbols symbols_;
92de84a6
ILT
1672 // Number of symbols defined in object file itself.
1673 size_t defined_count_;
75f65a3e
ILT
1674 // File offset for local symbols.
1675 off_t local_symbol_offset_;
7bf1f802
ILT
1676 // File offset for local dynamic symbols.
1677 off_t local_dynsym_offset_;
61ba1cf9 1678 // Values of local symbols.
c06b7b0b 1679 Local_values local_values_;
07f397ab
ILT
1680 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1681 // for TLS symbols, indexed by symbol number.
e727fa71 1682 Local_got_offsets local_got_offsets_;
ef9beddf
ILT
1683 // For each input section, the offset of the input section in its
1684 // output section. This is -1U if the input section requires a
1685 // special mapping.
1686 std::vector<Address> section_offsets_;
1687 // Table mapping discarded comdat sections to corresponding kept sections.
1688 Kept_comdat_section_table kept_comdat_sections_;
1689 // Table of kept comdat groups.
1690 Comdat_group_table comdat_groups_;
730cdc88
ILT
1691 // Whether this object has a GNU style .eh_frame section.
1692 bool has_eh_frame_;
bae7f79e
ILT
1693};
1694
54dc6425 1695// A class to manage the list of all objects.
a2fb1b05 1696
54dc6425
ILT
1697class Input_objects
1698{
1699 public:
1700 Input_objects()
92de84a6
ILT
1701 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_(),
1702 cref_(NULL)
54dc6425
ILT
1703 { }
1704
f6ce93d6
ILT
1705 // The type of the list of input relocateable objects.
1706 typedef std::vector<Relobj*> Relobj_list;
1707 typedef Relobj_list::const_iterator Relobj_iterator;
1708
1709 // The type of the list of input dynamic objects.
1710 typedef std::vector<Dynobj*> Dynobj_list;
1711 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425 1712
008db82e
ILT
1713 // Add an object to the list. Return true if all is well, or false
1714 // if this object should be ignored.
1715 bool
54dc6425
ILT
1716 add_object(Object*);
1717
92de84a6
ILT
1718 // Start processing an archive.
1719 void
1720 archive_start(Archive*);
1721
1722 // Stop processing an archive.
1723 void
1724 archive_stop(Archive*);
1725
e2827e5f
ILT
1726 // For each dynamic object, check whether we've seen all of its
1727 // explicit dependencies.
1728 void
1729 check_dynamic_dependencies() const;
1730
9a2d6984
ILT
1731 // Return whether an object was found in the system library
1732 // directory.
1733 bool
1734 found_in_system_library_directory(const Object*) const;
1735
92de84a6
ILT
1736 // Print symbol counts.
1737 void
1738 print_symbol_counts(const Symbol_table*) const;
1739
f6ce93d6
ILT
1740 // Iterate over all regular objects.
1741
1742 Relobj_iterator
1743 relobj_begin() const
1744 { return this->relobj_list_.begin(); }
1745
1746 Relobj_iterator
1747 relobj_end() const
1748 { return this->relobj_list_.end(); }
1749
1750 // Iterate over all dynamic objects.
1751
1752 Dynobj_iterator
1753 dynobj_begin() const
1754 { return this->dynobj_list_.begin(); }
54dc6425 1755
f6ce93d6
ILT
1756 Dynobj_iterator
1757 dynobj_end() const
1758 { return this->dynobj_list_.end(); }
54dc6425
ILT
1759
1760 // Return whether we have seen any dynamic objects.
1761 bool
1762 any_dynamic() const
f6ce93d6 1763 { return !this->dynobj_list_.empty(); }
54dc6425 1764
fe9a4c12
ILT
1765 // Return the number of input objects.
1766 int
1767 number_of_input_objects() const
1768 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1769
54dc6425
ILT
1770 private:
1771 Input_objects(const Input_objects&);
1772 Input_objects& operator=(const Input_objects&);
1773
008db82e 1774 // The list of ordinary objects included in the link.
f6ce93d6 1775 Relobj_list relobj_list_;
008db82e 1776 // The list of dynamic objects included in the link.
f6ce93d6 1777 Dynobj_list dynobj_list_;
008db82e
ILT
1778 // SONAMEs that we have seen.
1779 Unordered_set<std::string> sonames_;
9a2d6984
ILT
1780 // The directory in which we find the libc.so.
1781 std::string system_library_directory_;
92de84a6
ILT
1782 // Manage cross-references if requested.
1783 Cref* cref_;
54dc6425 1784};
a2fb1b05 1785
92e059d8
ILT
1786// Some of the information we pass to the relocation routines. We
1787// group this together to avoid passing a dozen different arguments.
1788
1789template<int size, bool big_endian>
1790struct Relocate_info
1791{
1792 // Command line options.
1793 const General_options* options;
1794 // Symbol table.
1795 const Symbol_table* symtab;
1796 // Layout.
1797 const Layout* layout;
1798 // Object being relocated.
f6ce93d6 1799 Sized_relobj<size, big_endian>* object;
92e059d8
ILT
1800 // Section index of relocation section.
1801 unsigned int reloc_shndx;
1802 // Section index of section being relocated.
1803 unsigned int data_shndx;
1804
1805 // Return a string showing the location of a relocation. This is
1806 // only used for error messages.
1807 std::string
1808 location(size_t relnum, off_t reloffset) const;
1809};
1810
bae7f79e
ILT
1811// Return an Object appropriate for the input file. P is BYTES long,
1812// and holds the ELF header.
1813
61ba1cf9
ILT
1814extern Object*
1815make_elf_object(const std::string& name, Input_file*,
1816 off_t offset, const unsigned char* p,
8383303e 1817 section_offset_type bytes);
bae7f79e
ILT
1818
1819} // end namespace gold
1820
1821#endif // !defined(GOLD_OBJECT_H)