Add Elf_file interface which can be used by both Sized_relobj and
[binutils.git] / gold / object.h
blob54f0350016e0716fbbd98538d053e7e3ffbf5780
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 #ifndef GOLD_OBJECT_H
4 #define GOLD_OBJECT_H
6 #include <cassert>
7 #include <string>
8 #include <vector>
10 #include "elfcpp.h"
11 #include "elfcpp_file.h"
12 #include "fileread.h"
13 #include "target.h"
15 namespace gold
18 class General_options;
19 class Stringpool;
20 class Layout;
21 class Output_section;
22 class Output_file;
23 class Dynobj;
25 // Data to pass from read_symbols() to add_symbols().
27 struct Read_symbols_data
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;
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;
45 // Data about a single relocation section. This is read in
46 // read_relocs and processed in scan_relocs.
48 struct Section_relocs
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;
62 // Relocations in an object file. This is read in read_relocs and
63 // processed in scan_relocs.
65 struct Read_relocs_data
67 typedef std::vector<Section_relocs> Relocs_list;
68 // The relocations.
69 Relocs_list relocs;
70 // The local symbols.
71 File_view* local_symbols;
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).
78 class Object
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.
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),
88 is_dynamic_(is_dynamic), target_(NULL)
89 { }
91 virtual ~Object()
92 { }
94 // Return the name of the object as we would report it to the tuser.
95 const std::string&
96 name() const
97 { return this->name_; }
99 // Return whether this is a dynamic object.
100 bool
101 is_dynamic() const
102 { return this->is_dynamic_; }
104 // Return the target structure associated with this object.
105 Target*
106 target() const
107 { return this->target_; }
109 // Lock the underlying file.
110 void
111 lock()
112 { this->input_file_->file().lock(); }
114 // Unlock the underlying file.
115 void
116 unlock()
117 { this->input_file_->file().unlock(); }
119 // Return whether the underlying file is locked.
120 bool
121 is_locked() const
122 { return this->input_file_->file().is_locked(); }
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>*
129 sized_target(ACCEPT_SIZE_ENDIAN_ONLY);
131 // Read the symbol information.
132 void
133 read_symbols(Read_symbols_data* sd)
134 { return this->do_read_symbols(sd); }
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
139 layout(const General_options& options, Symbol_table* symtab,
140 Layout* layout, Read_symbols_data* sd)
141 { this->do_layout(options, symtab, layout, sd); }
143 // Add symbol information to the global symbol table.
144 void
145 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
146 { this->do_add_symbols(symtab, sd); }
148 // Return a view of the contents of a section. Set *PLEN to the
149 // size.
150 const unsigned char*
151 section_contents(unsigned int shndx, off_t* plen);
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); }
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.
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
168 public:
169 View(const unsigned char* p)
170 : p_(p)
173 const unsigned char*
174 data() const
175 { return this->p_; }
177 private:
178 const unsigned char* p_;
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)); }
186 // Report an error.
187 void
188 error(const char* format, ...) ATTRIBUTE_PRINTF_2;
190 // A location in the file.
191 struct Location
193 off_t file_offset;
194 off_t data_size;
196 Location(off_t fo, off_t ds)
197 : file_offset(fo), data_size(ds)
201 // Get a View given a Location.
202 View view(Location loc)
203 { return View(this->get_view(loc.file_offset, loc.data_size)); }
205 protected:
206 // Read the symbols--implemented by child class.
207 virtual void
208 do_read_symbols(Read_symbols_data*) = 0;
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;
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;
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;
225 // Get the name of a section--implemented by child class.
226 virtual std::string
227 do_section_name(unsigned int shnum) = 0;
229 // Get the file.
230 Input_file*
231 input_file() const
232 { return this->input_file_; }
234 // Get the offset into the file.
235 off_t
236 offset() const
237 { return this->offset_; }
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); }
244 // Get a lasting view into the underlying file.
245 File_view*
246 get_lasting_view(off_t start, off_t size)
248 return this->input_file_->file().get_lasting_view(start + this->offset_,
249 size);
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); }
257 // Set the target.
258 void
259 set_target(Target* target)
260 { this->target_ = target; }
262 private:
263 // This class may not be copied.
264 Object(const Object&);
265 Object& operator=(const Object&);
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_;
280 // Implement sized_target inline for efficiency. This approach breaks
281 // static type checking, but is made safe using asserts.
283 template<int size, bool big_endian>
284 inline Sized_target<size, big_endian>*
285 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY)
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_);
292 // A regular object (ET_REL). This is an abstract base class itself.
293 // The implementations is the template class Sized_relobj.
295 class Relobj : public Object
297 public:
298 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
299 : Object(name, input_file, false, offset)
302 // Read the relocs.
303 void
304 read_relocs(Read_relocs_data* rd)
305 { return this->do_read_relocs(rd); }
307 // Scan the relocs and adjust the symbol table.
308 void
309 scan_relocs(const General_options& options, Symbol_table* symtab,
310 Layout* layout, Read_relocs_data* rd)
311 { return this->do_scan_relocs(options, symtab, layout, rd); }
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); }
320 // Relocate the input sections and write out the local symbols.
321 void
322 relocate(const General_options& options, const Symbol_table* symtab,
323 const Layout* layout, Output_file* of)
324 { return this->do_relocate(options, symtab, layout, of); }
326 // Return whether an input section is being included in the link.
327 bool
328 is_section_included(unsigned int shnum) const
330 assert(shnum < this->map_to_output_.size());
331 return this->map_to_output_[shnum].output_section != NULL;
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);
340 // Set the offset of an input section within its output section.
341 void
342 set_section_offset(unsigned int shndx, off_t off)
344 assert(shndx < this->map_to_output_.size());
345 this->map_to_output_[shndx].offset = off;
348 protected:
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
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;
361 // Read the relocs--implemented by child class.
362 virtual void
363 do_read_relocs(Read_relocs_data*) = 0;
365 // Scan the relocs--implemented by child class.
366 virtual void
367 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
368 Read_relocs_data*) = 0;
370 // Finalize local symbols--implemented by child class.
371 virtual off_t
372 do_finalize_local_symbols(off_t, Stringpool*) = 0;
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,
378 const Layout*, Output_file* of) = 0;
380 // Get the number of sections.
381 unsigned int
382 shnum() const
383 { return this->shnum_; }
385 // Set the number of sections.
386 void
387 set_shnum(int shnum)
388 { this->shnum_ = shnum; }
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_; }
395 private:
396 // Number of input sections.
397 unsigned int shnum_;
398 // Mapping from input sections to output section.
399 std::vector<Map_to_output> map_to_output_;
402 // Implement Object::output_section inline for efficiency.
403 inline Output_section*
404 Relobj::output_section(unsigned int shnum, off_t* poff)
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;
412 // A regular object file. This is size and endian specific.
414 template<int size, bool big_endian>
415 class Sized_relobj : public Relobj
417 public:
418 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
419 const typename elfcpp::Ehdr<size, big_endian>&);
421 ~Sized_relobj();
423 // Set up the object file based on the ELF header.
424 void
425 setup(const typename elfcpp::Ehdr<size, big_endian>&);
427 // Read the symbols.
428 void
429 do_read_symbols(Read_symbols_data*);
431 // Add the symbols to the symbol table.
432 void
433 do_add_symbols(Symbol_table*, Read_symbols_data*);
435 // Read the relocs.
436 void
437 do_read_relocs(Read_relocs_data*);
439 // Scan the relocs and adjust the symbol table.
440 void
441 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
442 Read_relocs_data*);
444 // Lay out the input sections.
445 void
446 do_layout(const General_options&, Symbol_table*, Layout*,
447 Read_symbols_data*);
449 // Finalize the local symbols.
450 off_t
451 do_finalize_local_symbols(off_t, Stringpool*);
453 // Relocate the input sections and write out the local symbols.
454 void
455 do_relocate(const General_options& options, const Symbol_table* symtab,
456 const Layout*, Output_file* of);
458 // Get the name of a section.
459 std::string
460 do_section_name(unsigned int shndx)
461 { return this->elf_file_.section_name(shndx); }
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); }
468 // Return the appropriate Sized_target structure.
469 Sized_target<size, big_endian>*
470 sized_target()
472 return this->Object::sized_target
473 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
474 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
477 private:
478 // For convenience.
479 typedef Sized_relobj<size, big_endian> This;
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;
483 typedef elfcpp::Shdr<size, big_endian> Shdr;
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>*);
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>&);
496 // Views and sizes when relocating.
497 struct View_size
499 unsigned char* view;
500 typename elfcpp::Elf_types<size>::Elf_Addr address;
501 off_t offset;
502 off_t view_size;
505 typedef std::vector<View_size> Views;
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*);
512 // Relocate the sections in the output file.
513 void
514 relocate_sections(const General_options& options, const Symbol_table*,
515 const Layout*, const unsigned char* pshdrs, Views*);
517 // Write out the local symbols.
518 void
519 write_local_symbols(Output_file*, const Stringpool*);
521 // General access to the ELF file.
522 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
523 // If non-NULL, a view of the section header data.
524 File_view* section_headers_;
525 // Index of SHT_SYMTAB section.
526 unsigned int symtab_shndx_;
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_;
531 // The entries in the symbol table for the external symbols.
532 Symbol** symbols_;
533 // File offset for local symbols.
534 off_t local_symbol_offset_;
535 // Values of local symbols.
536 typename elfcpp::Elf_types<size>::Elf_Addr *values_;
539 // A class to manage the list of all objects.
541 class Input_objects
543 public:
544 Input_objects()
545 : relobj_list_(), target_(NULL)
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;
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;
556 // Add an object to the list.
557 void
558 add_object(Object*);
560 // Get the target we should use for the output file.
561 Target*
562 target() const
563 { return this->target_; }
565 // Iterate over all regular objects.
567 Relobj_iterator
568 relobj_begin() const
569 { return this->relobj_list_.begin(); }
571 Relobj_iterator
572 relobj_end() const
573 { return this->relobj_list_.end(); }
575 // Iterate over all dynamic objects.
577 Dynobj_iterator
578 dynobj_begin() const
579 { return this->dynobj_list_.begin(); }
581 Dynobj_iterator
582 dynobj_end() const
583 { return this->dynobj_list_.end(); }
585 // Return whether we have seen any dynamic objects.
586 bool
587 any_dynamic() const
588 { return !this->dynobj_list_.empty(); }
590 private:
591 Input_objects(const Input_objects&);
592 Input_objects& operator=(const Input_objects&);
594 Relobj_list relobj_list_;
595 Dynobj_list dynobj_list_;
596 Target* target_;
599 // Some of the information we pass to the relocation routines. We
600 // group this together to avoid passing a dozen different arguments.
602 template<int size, bool big_endian>
603 struct Relocate_info
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.
612 Sized_relobj<size, big_endian>* object;
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;
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;
630 // Return an Object appropriate for the input file. P is BYTES long,
631 // and holds the ELF header.
633 extern Object*
634 make_elf_object(const std::string& name, Input_file*,
635 off_t offset, const unsigned char* p,
636 off_t bytes);
638 } // end namespace gold
640 #endif // !defined(GOLD_OBJECT_H)