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