2006-11-29 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / gold / object.h
CommitLineData
bae7f79e
ILT
1// object.h -- support for an object file for linking in gold -*- C++ -*-
2
3#ifndef GOLD_OBJECT_H
4#define GOLD_OBJECT_H
5
14bfc3f5 6#include <cassert>
92e059d8 7#include <string>
a2fb1b05 8#include <vector>
14bfc3f5 9
bae7f79e 10#include "elfcpp.h"
645f8123 11#include "elfcpp_file.h"
bae7f79e 12#include "fileread.h"
14bfc3f5 13#include "target.h"
bae7f79e
ILT
14
15namespace gold
16{
17
92e059d8 18class General_options;
75f65a3e 19class Stringpool;
a2fb1b05 20class Layout;
61ba1cf9
ILT
21class Output_section;
22class Output_file;
f6ce93d6 23class Dynobj;
a2fb1b05 24
bae7f79e
ILT
25// Data to pass from read_symbols() to add_symbols().
26
27struct Read_symbols_data
28{
12e14209
ILT
29 // Section headers.
30 File_view* section_headers;
31 // Section names.
32 File_view* section_names;
33 // Size of section name data in bytes.
34 off_t section_names_size;
bae7f79e
ILT
35 // Symbol data.
36 File_view* symbols;
37 // Size of symbol data in bytes.
38 off_t symbols_size;
39 // Symbol names.
40 File_view* symbol_names;
41 // Size of symbol name data in bytes.
42 off_t symbol_names_size;
dbe717ef
ILT
43
44 // Version information. This is only used on dynamic objects.
45 // Version symbol data (from SHT_GNU_versym section).
46 File_view* versym;
47 off_t versym_size;
48 // Version definition data (from SHT_GNU_verdef section).
49 File_view* verdef;
50 off_t verdef_size;
51 unsigned int verdef_info;
52 // Needed version data (from SHT_GNU_verneed section).
53 File_view* verneed;
54 off_t verneed_size;
55 unsigned int verneed_info;
bae7f79e
ILT
56};
57
92e059d8
ILT
58// Data about a single relocation section. This is read in
59// read_relocs and processed in scan_relocs.
60
61struct Section_relocs
62{
63 // Index of reloc section.
64 unsigned int reloc_shndx;
65 // Index of section that relocs apply to.
66 unsigned int data_shndx;
67 // Contents of reloc section.
68 File_view* contents;
69 // Reloc section type.
70 unsigned int sh_type;
71 // Number of reloc entries.
72 size_t reloc_count;
73};
74
75// Relocations in an object file. This is read in read_relocs and
76// processed in scan_relocs.
77
78struct Read_relocs_data
79{
80 typedef std::vector<Section_relocs> Relocs_list;
81 // The relocations.
82 Relocs_list relocs;
83 // The local symbols.
84 File_view* local_symbols;
85};
86
f6ce93d6
ILT
87// Object is an abstract base class which represents either a 32-bit
88// or a 64-bit input object. This can be a regular object file
89// (ET_REL) or a shared object (ET_DYN).
bae7f79e
ILT
90
91class Object
92{
93 public:
94 // NAME is the name of the object as we would report it to the user
95 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
96 // used to read the file. OFFSET is the offset within the input
97 // file--0 for a .o or .so file, something else for a .a file.
14bfc3f5
ILT
98 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
99 off_t offset = 0)
dbe717ef 100 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
f6ce93d6 101 is_dynamic_(is_dynamic), target_(NULL)
bae7f79e
ILT
102 { }
103
104 virtual ~Object()
105 { }
106
14bfc3f5 107 // Return the name of the object as we would report it to the tuser.
bae7f79e
ILT
108 const std::string&
109 name() const
110 { return this->name_; }
111
14bfc3f5
ILT
112 // Return whether this is a dynamic object.
113 bool
114 is_dynamic() const
115 { return this->is_dynamic_; }
116
14bfc3f5
ILT
117 // Return the target structure associated with this object.
118 Target*
a2fb1b05 119 target() const
14bfc3f5
ILT
120 { return this->target_; }
121
a2fb1b05
ILT
122 // Lock the underlying file.
123 void
124 lock()
125 { this->input_file_->file().lock(); }
126
127 // Unlock the underlying file.
128 void
129 unlock()
130 { this->input_file_->file().unlock(); }
131
132 // Return whether the underlying file is locked.
133 bool
134 is_locked() const
135 { return this->input_file_->file().is_locked(); }
136
14bfc3f5
ILT
137 // Return the sized target structure associated with this object.
138 // This is like the target method but it returns a pointer of
139 // appropriate checked type.
140 template<int size, bool big_endian>
141 Sized_target<size, big_endian>*
5482377d 142 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
bae7f79e 143
92e059d8 144 // Read the symbol information.
12e14209
ILT
145 void
146 read_symbols(Read_symbols_data* sd)
147 { return this->do_read_symbols(sd); }
a2fb1b05 148
92e059d8
ILT
149 // Pass sections which should be included in the link to the Layout
150 // object, and record where the sections go in the output file.
151 void
f6ce93d6
ILT
152 layout(const General_options& options, Symbol_table* symtab,
153 Layout* layout, Read_symbols_data* sd)
154 { this->do_layout(options, symtab, layout, sd); }
92e059d8 155
a2fb1b05
ILT
156 // Add symbol information to the global symbol table.
157 void
12e14209
ILT
158 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
159 { this->do_add_symbols(symtab, sd); }
a2fb1b05 160
f6ce93d6
ILT
161 // Return a view of the contents of a section. Set *PLEN to the
162 // size.
163 const unsigned char*
645f8123 164 section_contents(unsigned int shndx, off_t* plen);
f6ce93d6
ILT
165
166 // Return the name of a section given a section index. This is only
167 // used for error messages.
168 std::string
169 section_name(unsigned int shnum)
170 { return this->do_section_name(shnum); }
171
645f8123
ILT
172 // Functions and types for the elfcpp::Elf_file interface. This
173 // permit us to use Object as the File template parameter for
174 // elfcpp::Elf_file.
175
176 // The View class is returned by view. It must support a single
177 // method, data(). This is trivial, because get_view does what we
178 // need.
179 class View
180 {
181 public:
182 View(const unsigned char* p)
183 : p_(p)
184 { }
185
186 const unsigned char*
187 data() const
188 { return this->p_; }
189
190 private:
191 const unsigned char* p_;
192 };
193
194 // Return a View.
195 View
196 view(off_t file_offset, off_t data_size)
197 { return View(this->get_view(file_offset, data_size)); }
198
199 // Report an error.
200 void
201 error(const char* format, ...) ATTRIBUTE_PRINTF_2;
202
203 // A location in the file.
204 struct Location
205 {
206 off_t file_offset;
207 off_t data_size;
208
209 Location(off_t fo, off_t ds)
210 : file_offset(fo), data_size(ds)
211 { }
212 };
213
214 // Get a View given a Location.
215 View view(Location loc)
216 { return View(this->get_view(loc.file_offset, loc.data_size)); }
217
f6ce93d6
ILT
218 protected:
219 // Read the symbols--implemented by child class.
220 virtual void
221 do_read_symbols(Read_symbols_data*) = 0;
222
223 // Lay out sections--implemented by child class.
224 virtual void
225 do_layout(const General_options&, Symbol_table*, Layout*,
226 Read_symbols_data*) = 0;
227
228 // Add symbol information to the global symbol table--implemented by
229 // child class.
230 virtual void
231 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
232
645f8123
ILT
233 // Return the location of the contents of a section. Implemented by
234 // child class.
235 virtual Location
236 do_section_contents(unsigned int shnum) = 0;
f6ce93d6
ILT
237
238 // Get the name of a section--implemented by child class.
239 virtual std::string
240 do_section_name(unsigned int shnum) = 0;
241
242 // Get the file.
243 Input_file*
244 input_file() const
245 { return this->input_file_; }
246
247 // Get the offset into the file.
248 off_t
249 offset() const
250 { return this->offset_; }
251
252 // Get a view into the underlying file.
253 const unsigned char*
254 get_view(off_t start, off_t size)
255 { return this->input_file_->file().get_view(start + this->offset_, size); }
256
257 // Get a lasting view into the underlying file.
258 File_view*
259 get_lasting_view(off_t start, off_t size)
260 {
261 return this->input_file_->file().get_lasting_view(start + this->offset_,
262 size);
263 }
264
265 // Read data from the underlying file.
266 void
267 read(off_t start, off_t size, void* p)
268 { this->input_file_->file().read(start + this->offset_, size, p); }
269
270 // Set the target.
271 void
dbe717ef
ILT
272 set_target(int machine, int size, bool big_endian, int osabi,
273 int abiversion);
274
275 // Get the number of sections.
276 unsigned int
277 shnum() const
278 { return this->shnum_; }
279
280 // Set the number of sections.
281 void
282 set_shnum(int shnum)
283 { this->shnum_ = shnum; }
284
285 // Functions used by both Sized_relobj and Sized_dynobj.
286
287 // Read the section data into a Read_symbols_data object.
288 template<int size, bool big_endian>
289 void
290 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
291 Read_symbols_data*);
292
293 // If NAME is the name of a special .gnu.warning section, arrange
294 // for the warning to be issued. SHNDX is the section index.
295 // Return whether it is a warning section.
296 bool
297 handle_gnu_warning_section(const char* name, unsigned int shndx,
298 Symbol_table*);
f6ce93d6
ILT
299
300 private:
301 // This class may not be copied.
302 Object(const Object&);
303 Object& operator=(const Object&);
304
305 // Name of object as printed to user.
306 std::string name_;
307 // For reading the file.
308 Input_file* input_file_;
309 // Offset within the file--0 for an object file, non-0 for an
310 // archive.
311 off_t offset_;
dbe717ef
ILT
312 // Number of input sections.
313 unsigned int shnum_;
f6ce93d6
ILT
314 // Whether this is a dynamic object.
315 bool is_dynamic_;
316 // Target functions--may be NULL if the target is not known.
317 Target* target_;
318};
319
320// Implement sized_target inline for efficiency. This approach breaks
321// static type checking, but is made safe using asserts.
322
323template<int size, bool big_endian>
324inline Sized_target<size, big_endian>*
325Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
326{
327 assert(this->target_->get_size() == size);
328 assert(this->target_->is_big_endian() ? big_endian : !big_endian);
329 return static_cast<Sized_target<size, big_endian>*>(this->target_);
330}
331
332// A regular object (ET_REL). This is an abstract base class itself.
333// The implementations is the template class Sized_relobj.
334
335class Relobj : public Object
336{
337 public:
338 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
339 : Object(name, input_file, false, offset)
340 { }
341
92e059d8 342 // Read the relocs.
a2fb1b05 343 void
92e059d8
ILT
344 read_relocs(Read_relocs_data* rd)
345 { return this->do_read_relocs(rd); }
346
347 // Scan the relocs and adjust the symbol table.
348 void
349 scan_relocs(const General_options& options, Symbol_table* symtab,
ead1e424
ILT
350 Layout* layout, Read_relocs_data* rd)
351 { return this->do_scan_relocs(options, symtab, layout, rd); }
a2fb1b05 352
75f65a3e
ILT
353 // Initial local symbol processing: set the offset where local
354 // symbol information will be stored; add local symbol names to
c06b7b0b
ILT
355 // *POOL; return the new local symbol index.
356 unsigned int
357 finalize_local_symbols(unsigned int index, off_t off, Stringpool* pool)
358 { return this->do_finalize_local_symbols(index, off, pool); }
75f65a3e 359
61ba1cf9
ILT
360 // Relocate the input sections and write out the local symbols.
361 void
362 relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
363 const Layout* layout, Output_file* of)
364 { return this->do_relocate(options, symtab, layout, of); }
61ba1cf9 365
a783673b
ILT
366 // Return whether an input section is being included in the link.
367 bool
368 is_section_included(unsigned int shnum) const
369 {
370 assert(shnum < this->map_to_output_.size());
371 return this->map_to_output_[shnum].output_section != NULL;
372 }
373
374 // Given a section index, return the corresponding Output_section
375 // (which will be NULL if the section is not included in the link)
376 // and set *POFF to the offset within that section.
377 inline Output_section*
378 output_section(unsigned int shnum, off_t* poff);
379
ead1e424
ILT
380 // Set the offset of an input section within its output section.
381 void
382 set_section_offset(unsigned int shndx, off_t off)
383 {
384 assert(shndx < this->map_to_output_.size());
385 this->map_to_output_[shndx].offset = off;
386 }
387
a783673b 388 protected:
a2fb1b05
ILT
389 // What we need to know to map an input section to an output
390 // section. We keep an array of these, one for each input section,
391 // indexed by the input section number.
392 struct Map_to_output
393 {
394 // The output section. This is NULL if the input section is to be
395 // discarded.
396 Output_section* output_section;
397 // The offset within the output section.
398 off_t offset;
399 };
400
92e059d8
ILT
401 // Read the relocs--implemented by child class.
402 virtual void
403 do_read_relocs(Read_relocs_data*) = 0;
404
405 // Scan the relocs--implemented by child class.
406 virtual void
ead1e424
ILT
407 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
408 Read_relocs_data*) = 0;
92e059d8 409
75f65a3e 410 // Finalize local symbols--implemented by child class.
c06b7b0b
ILT
411 virtual unsigned int
412 do_finalize_local_symbols(unsigned int, off_t, Stringpool*) = 0;
75f65a3e 413
61ba1cf9
ILT
414 // Relocate the input sections and write out the local
415 // symbols--implemented by child class.
416 virtual void
417 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
418 const Layout*, Output_file* of) = 0;
419
a2fb1b05
ILT
420 // Return the vector mapping input sections to output sections.
421 std::vector<Map_to_output>&
422 map_to_output()
423 { return this->map_to_output_; }
424
bae7f79e 425 private:
a2fb1b05
ILT
426 // Mapping from input sections to output section.
427 std::vector<Map_to_output> map_to_output_;
bae7f79e
ILT
428};
429
a783673b
ILT
430// Implement Object::output_section inline for efficiency.
431inline Output_section*
f6ce93d6 432Relobj::output_section(unsigned int shnum, off_t* poff)
a783673b
ILT
433{
434 assert(shnum < this->map_to_output_.size());
435 const Map_to_output& mo(this->map_to_output_[shnum]);
436 *poff = mo.offset;
437 return mo.output_section;
438}
439
14bfc3f5 440// A regular object file. This is size and endian specific.
bae7f79e
ILT
441
442template<int size, bool big_endian>
f6ce93d6 443class Sized_relobj : public Relobj
bae7f79e
ILT
444{
445 public:
c06b7b0b
ILT
446 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
447 typedef std::vector<Address> Local_values;
448
f6ce93d6 449 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
bae7f79e
ILT
450 const typename elfcpp::Ehdr<size, big_endian>&);
451
f6ce93d6 452 ~Sized_relobj();
bae7f79e 453
a2fb1b05 454 // Set up the object file based on the ELF header.
bae7f79e
ILT
455 void
456 setup(const typename elfcpp::Ehdr<size, big_endian>&);
457
c06b7b0b
ILT
458 // Return the index of local symbol SYM in the ordinary symbol
459 // table. A value of -1U means that the symbol is not being output.
460 unsigned int
461 symtab_index(unsigned int sym) const
462 {
463 assert(sym < this->local_indexes_.size());
464 assert(this->local_indexes_[sym] != 0);
465 return this->local_indexes_[sym];
466 }
467
a2fb1b05 468 // Read the symbols.
bae7f79e 469 void
12e14209 470 do_read_symbols(Read_symbols_data*);
14bfc3f5 471
dbe717ef
ILT
472 // Lay out the input sections.
473 void
474 do_layout(const General_options&, Symbol_table*, Layout*,
475 Read_symbols_data*);
476
12e14209
ILT
477 // Add the symbols to the symbol table.
478 void
479 do_add_symbols(Symbol_table*, Read_symbols_data*);
a2fb1b05 480
92e059d8
ILT
481 // Read the relocs.
482 void
483 do_read_relocs(Read_relocs_data*);
484
485 // Scan the relocs and adjust the symbol table.
486 void
ead1e424
ILT
487 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
488 Read_relocs_data*);
92e059d8 489
75f65a3e 490 // Finalize the local symbols.
c06b7b0b
ILT
491 unsigned int
492 do_finalize_local_symbols(unsigned int, off_t, Stringpool*);
75f65a3e 493
61ba1cf9
ILT
494 // Relocate the input sections and write out the local symbols.
495 void
496 do_relocate(const General_options& options, const Symbol_table* symtab,
92e059d8
ILT
497 const Layout*, Output_file* of);
498
499 // Get the name of a section.
500 std::string
645f8123
ILT
501 do_section_name(unsigned int shndx)
502 { return this->elf_file_.section_name(shndx); }
61ba1cf9 503
645f8123 504 // Return the location of the contents of a section.
dbe717ef 505 Object::Location
645f8123
ILT
506 do_section_contents(unsigned int shndx)
507 { return this->elf_file_.section_contents(shndx); }
f6ce93d6 508
a2fb1b05 509 // Return the appropriate Sized_target structure.
14bfc3f5
ILT
510 Sized_target<size, big_endian>*
511 sized_target()
274e99f9 512 {
593f47df
ILT
513 return this->Object::sized_target
514 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
515 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
274e99f9 516 }
bae7f79e
ILT
517
518 private:
a2fb1b05 519 // For convenience.
f6ce93d6 520 typedef Sized_relobj<size, big_endian> This;
a2fb1b05
ILT
521 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
522 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
523 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
75f65a3e
ILT
524 typedef elfcpp::Shdr<size, big_endian> Shdr;
525
dbe717ef
ILT
526 // Find the SHT_SYMTAB section, given the section headers.
527 void
528 find_symtab(const unsigned char* pshdrs);
529
a2fb1b05
ILT
530 // Whether to include a section group in the link.
531 bool
532 include_section_group(Layout*, unsigned int,
533 const elfcpp::Shdr<size, big_endian>&,
534 std::vector<bool>*);
535
536 // Whether to include a linkonce section in the link.
537 bool
538 include_linkonce_section(Layout*, const char*,
539 const elfcpp::Shdr<size, big_endian>&);
540
61ba1cf9
ILT
541 // Views and sizes when relocating.
542 struct View_size
543 {
544 unsigned char* view;
545 typename elfcpp::Elf_types<size>::Elf_Addr address;
546 off_t offset;
547 off_t view_size;
548 };
549
550 typedef std::vector<View_size> Views;
551
552 // Write section data to the output file. Record the views and
553 // sizes in VIEWS for use when relocating.
554 void
555 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
556
557 // Relocate the sections in the output file.
558 void
92e059d8
ILT
559 relocate_sections(const General_options& options, const Symbol_table*,
560 const Layout*, const unsigned char* pshdrs, Views*);
61ba1cf9
ILT
561
562 // Write out the local symbols.
563 void
564 write_local_symbols(Output_file*, const Stringpool*);
565
645f8123
ILT
566 // General access to the ELF file.
567 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
bae7f79e 568 // Index of SHT_SYMTAB section.
645f8123 569 unsigned int symtab_shndx_;
61ba1cf9
ILT
570 // The number of local symbols.
571 unsigned int local_symbol_count_;
572 // The number of local symbols which go into the output file.
573 unsigned int output_local_symbol_count_;
14bfc3f5
ILT
574 // The entries in the symbol table for the external symbols.
575 Symbol** symbols_;
75f65a3e
ILT
576 // File offset for local symbols.
577 off_t local_symbol_offset_;
61ba1cf9 578 // Values of local symbols.
c06b7b0b
ILT
579 Local_values local_values_;
580 // Indexes of local symbols in the output file; -1U if not present.
581 std::vector<unsigned int> local_indexes_;
bae7f79e
ILT
582};
583
54dc6425 584// A class to manage the list of all objects.
a2fb1b05 585
54dc6425
ILT
586class Input_objects
587{
588 public:
589 Input_objects()
f6ce93d6 590 : relobj_list_(), target_(NULL)
54dc6425
ILT
591 { }
592
f6ce93d6
ILT
593 // The type of the list of input relocateable objects.
594 typedef std::vector<Relobj*> Relobj_list;
595 typedef Relobj_list::const_iterator Relobj_iterator;
596
597 // The type of the list of input dynamic objects.
598 typedef std::vector<Dynobj*> Dynobj_list;
599 typedef Dynobj_list::const_iterator Dynobj_iterator;
54dc6425
ILT
600
601 // Add an object to the list.
602 void
603 add_object(Object*);
604
75f65a3e
ILT
605 // Get the target we should use for the output file.
606 Target*
607 target() const
608 { return this->target_; }
609
f6ce93d6
ILT
610 // Iterate over all regular objects.
611
612 Relobj_iterator
613 relobj_begin() const
614 { return this->relobj_list_.begin(); }
615
616 Relobj_iterator
617 relobj_end() const
618 { return this->relobj_list_.end(); }
619
620 // Iterate over all dynamic objects.
621
622 Dynobj_iterator
623 dynobj_begin() const
624 { return this->dynobj_list_.begin(); }
54dc6425 625
f6ce93d6
ILT
626 Dynobj_iterator
627 dynobj_end() const
628 { return this->dynobj_list_.end(); }
54dc6425
ILT
629
630 // Return whether we have seen any dynamic objects.
631 bool
632 any_dynamic() const
f6ce93d6 633 { return !this->dynobj_list_.empty(); }
54dc6425
ILT
634
635 private:
636 Input_objects(const Input_objects&);
637 Input_objects& operator=(const Input_objects&);
638
f6ce93d6
ILT
639 Relobj_list relobj_list_;
640 Dynobj_list dynobj_list_;
75f65a3e 641 Target* target_;
54dc6425 642};
a2fb1b05 643
92e059d8
ILT
644// Some of the information we pass to the relocation routines. We
645// group this together to avoid passing a dozen different arguments.
646
647template<int size, bool big_endian>
648struct Relocate_info
649{
650 // Command line options.
651 const General_options* options;
652 // Symbol table.
653 const Symbol_table* symtab;
654 // Layout.
655 const Layout* layout;
656 // Object being relocated.
f6ce93d6 657 Sized_relobj<size, big_endian>* object;
92e059d8
ILT
658 // Number of local symbols.
659 unsigned int local_symbol_count;
660 // Values of local symbols.
c06b7b0b 661 const typename Sized_relobj<size, big_endian>::Local_values* local_values;
92e059d8 662 // Global symbols.
c06b7b0b 663 const Symbol* const * symbols;
92e059d8
ILT
664 // Section index of relocation section.
665 unsigned int reloc_shndx;
666 // Section index of section being relocated.
667 unsigned int data_shndx;
668
669 // Return a string showing the location of a relocation. This is
670 // only used for error messages.
671 std::string
672 location(size_t relnum, off_t reloffset) const;
673};
674
bae7f79e
ILT
675// Return an Object appropriate for the input file. P is BYTES long,
676// and holds the ELF header.
677
61ba1cf9
ILT
678extern Object*
679make_elf_object(const std::string& name, Input_file*,
680 off_t offset, const unsigned char* p,
681 off_t bytes);
bae7f79e
ILT
682
683} // end namespace gold
684
685#endif // !defined(GOLD_OBJECT_H)
This page took 0.061051 seconds and 4 git commands to generate.