Snapshot. Now able to produce a minimal executable which actually
[binutils-gdb.git] / gold / object.h
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
6 #include <cassert>
7 #include <vector>
8
9 #include "elfcpp.h"
10 #include "fileread.h"
11 #include "target.h"
12 #include "symtab.h"
13
14 namespace gold
15 {
16
17 class Stringpool;
18 class Layout;
19 class Output_section;
20 class Output_file;
21
22 // Data to pass from read_symbols() to add_symbols().
23
24 struct Read_symbols_data
25 {
26 // Symbol data.
27 File_view* symbols;
28 // Size of symbol data in bytes.
29 off_t symbols_size;
30 // Symbol names.
31 File_view* symbol_names;
32 // Size of symbol name data in bytes.
33 off_t symbol_names_size;
34 };
35
36 // Object is an interface which represents either a 32-bit or a 64-bit
37 // input object. This can be a regular object file (ET_REL) or a
38 // shared object (ET_DYN). The actual instantiations are
39 // Sized_object<32> and Sized_object<64>
40
41 class Object
42 {
43 public:
44 // NAME is the name of the object as we would report it to the user
45 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
46 // used to read the file. OFFSET is the offset within the input
47 // file--0 for a .o or .so file, something else for a .a file.
48 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
49 off_t offset = 0)
50 : name_(name), input_file_(input_file), offset_(offset),
51 shnum_(0), is_dynamic_(is_dynamic), target_(NULL),
52 map_to_output_()
53 { }
54
55 virtual ~Object()
56 { }
57
58 // Return the name of the object as we would report it to the tuser.
59 const std::string&
60 name() const
61 { return this->name_; }
62
63 // Return whether this is a dynamic object.
64 bool
65 is_dynamic() const
66 { return this->is_dynamic_; }
67
68 // Return the target structure associated with this object.
69 Target*
70 target() const
71 { return this->target_; }
72
73 // Lock the underlying file.
74 void
75 lock()
76 { this->input_file_->file().lock(); }
77
78 // Unlock the underlying file.
79 void
80 unlock()
81 { this->input_file_->file().unlock(); }
82
83 // Return whether the underlying file is locked.
84 bool
85 is_locked() const
86 { return this->input_file_->file().is_locked(); }
87
88 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
89 // Return the sized target structure associated with this object.
90 // This is like the target method but it returns a pointer of
91 // appropriate checked type.
92 template<int size, bool big_endian>
93 Sized_target<size, big_endian>*
94 sized_target();
95 #endif
96
97 // Read the symbol and relocation information.
98 Read_symbols_data
99 read_symbols()
100 { return this->do_read_symbols(); }
101
102 // Add symbol information to the global symbol table.
103 void
104 add_symbols(Symbol_table* symtab, Read_symbols_data rd)
105 { this->do_add_symbols(symtab, rd); }
106
107 // Pass sections which should be included in the link to the Layout
108 // object, and record where the sections go in the output file.
109 void
110 layout(Layout* lay)
111 { this->do_layout(lay); }
112
113 // Initial local symbol processing: set the offset where local
114 // symbol information will be stored; add local symbol names to
115 // *POOL; return the offset following the local symbols.
116 off_t
117 finalize_local_symbols(off_t off, Stringpool* pool)
118 { return this->do_finalize_local_symbols(off, pool); }
119
120 // Relocate the input sections and write out the local symbols.
121 void
122 relocate(const General_options& options, const Symbol_table* symtab,
123 const Stringpool* sympool, Output_file* of)
124 { return this->do_relocate(options, symtab, sympool, of); }
125
126 // What we need to know to map an input section to an output
127 // section. We keep an array of these, one for each input section,
128 // indexed by the input section number.
129 struct Map_to_output
130 {
131 // The output section. This is NULL if the input section is to be
132 // discarded.
133 Output_section* output_section;
134 // The offset within the output section.
135 off_t offset;
136 };
137
138 // Given a section index, return the corresponding Map_to_output
139 // information.
140 const Map_to_output*
141 section_output_info(unsigned int shnum) const
142 {
143 assert(shnum < this->map_to_output_.size());
144 return &this->map_to_output_[shnum];
145 }
146
147 protected:
148 // Read the symbols--implemented by child class.
149 virtual Read_symbols_data
150 do_read_symbols() = 0;
151
152 // Add symbol information to the global symbol table--implemented by
153 // child class.
154 virtual void
155 do_add_symbols(Symbol_table*, Read_symbols_data) = 0;
156
157 // Lay out sections--implemented by child class.
158 virtual void
159 do_layout(Layout*) = 0;
160
161 // Finalize local symbols--implemented by child class.
162 virtual off_t
163 do_finalize_local_symbols(off_t, Stringpool*) = 0;
164
165 // Relocate the input sections and write out the local
166 // symbols--implemented by child class.
167 virtual void
168 do_relocate(const General_options& options, const Symbol_table* symtab,
169 const Stringpool*, Output_file* of) = 0;
170
171 // Get the file.
172 Input_file*
173 input_file() const
174 { return this->input_file_; }
175
176 // Get the offset into the file.
177 off_t
178 offset() const
179 { return this->offset_; }
180
181 // Get a view into the underlying file.
182 const unsigned char*
183 get_view(off_t start, off_t size);
184
185 // Get the number of sections.
186 unsigned int
187 shnum() const
188 { return this->shnum_; }
189
190 // Set the number of sections.
191 void
192 set_shnum(int shnum)
193 { this->shnum_ = shnum; }
194
195 // Set the target.
196 void
197 set_target(Target* target)
198 { this->target_ = target; }
199
200 // Read data from the underlying file.
201 void
202 read(off_t start, off_t size, void* p);
203
204 // Get a lasting view into the underlying file.
205 File_view*
206 get_lasting_view(off_t start, off_t size);
207
208 // Return the vector mapping input sections to output sections.
209 std::vector<Map_to_output>&
210 map_to_output()
211 { return this->map_to_output_; }
212
213 private:
214 // This class may not be copied.
215 Object(const Object&);
216 Object& operator=(const Object&);
217
218 // Name of object as printed to user.
219 std::string name_;
220 // For reading the file.
221 Input_file* input_file_;
222 // Offset within the file--0 for an object file, non-0 for an
223 // archive.
224 off_t offset_;
225 // Number of input sections.
226 unsigned int shnum_;
227 // Whether this is a dynamic object.
228 bool is_dynamic_;
229 // Target functions--may be NULL if the target is not known.
230 Target* target_;
231 // Mapping from input sections to output section.
232 std::vector<Map_to_output> map_to_output_;
233 };
234
235 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
236
237 // Implement sized_target inline for efficiency. This approach breaks
238 // static type checking, but is made safe using asserts.
239
240 template<int size, bool big_endian>
241 inline Sized_target<size, big_endian>*
242 Object::sized_target()
243 {
244 assert(this->target_->get_size() == size);
245 assert(this->target_->is_big_endian() ? big_endian : !big_endian);
246 return static_cast<Sized_target<size, big_endian>*>(this->target_);
247 }
248
249 #endif
250
251 // A regular object file. This is size and endian specific.
252
253 template<int size, bool big_endian>
254 class Sized_object : public Object
255 {
256 public:
257 Sized_object(const std::string& name, Input_file* input_file, off_t offset,
258 const typename elfcpp::Ehdr<size, big_endian>&);
259
260 ~Sized_object();
261
262 // Set up the object file based on the ELF header.
263 void
264 setup(const typename elfcpp::Ehdr<size, big_endian>&);
265
266 // Read the symbols.
267 Read_symbols_data
268 do_read_symbols();
269
270 // Add the symbols to the symbol table.
271 void
272 do_add_symbols(Symbol_table*, Read_symbols_data);
273
274 // Lay out the input sections.
275 void
276 do_layout(Layout*);
277
278 // Finalize the local symbols.
279 off_t
280 do_finalize_local_symbols(off_t, Stringpool*);
281
282 // Relocate the input sections and write out the local symbols.
283 void
284 do_relocate(const General_options& options, const Symbol_table* symtab,
285 const Stringpool*, Output_file* of);
286
287 // Return the appropriate Sized_target structure.
288 Sized_target<size, big_endian>*
289 sized_target()
290 {
291 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
292 return this->Object::sized_target<size, big_endian>();
293 #else
294 return static_cast<Sized_target<size, big_endian>*>(this->target());
295 #endif
296 }
297
298 private:
299 // This object may not be copied.
300 Sized_object(const Sized_object&);
301 Sized_object& operator=(const Sized_object&);
302
303 // For convenience.
304 typedef Sized_object<size, big_endian> This;
305 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
306 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
307 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
308 typedef elfcpp::Shdr<size, big_endian> Shdr;
309
310 // Read the section header for section SHNUM.
311 const unsigned char*
312 section_header(unsigned int shnum);
313
314 // Whether to include a section group in the link.
315 bool
316 include_section_group(Layout*, unsigned int,
317 const elfcpp::Shdr<size, big_endian>&,
318 std::vector<bool>*);
319
320 // Whether to include a linkonce section in the link.
321 bool
322 include_linkonce_section(Layout*, const char*,
323 const elfcpp::Shdr<size, big_endian>&);
324
325 // Views and sizes when relocating.
326 struct View_size
327 {
328 unsigned char* view;
329 typename elfcpp::Elf_types<size>::Elf_Addr address;
330 off_t offset;
331 off_t view_size;
332 };
333
334 typedef std::vector<View_size> Views;
335
336 // Write section data to the output file. Record the views and
337 // sizes in VIEWS for use when relocating.
338 void
339 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
340
341 // Relocate the sections in the output file.
342 void
343 relocate_sections(const Symbol_table*, const unsigned char* pshdrs, Views*);
344
345 // Write out the local symbols.
346 void
347 write_local_symbols(Output_file*, const Stringpool*);
348
349 // ELF file header e_flags field.
350 unsigned int flags_;
351 // File offset of section header table.
352 off_t shoff_;
353 // Offset of SHT_STRTAB section holding section names.
354 unsigned int shstrndx_;
355 // Index of SHT_SYMTAB section.
356 unsigned int symtab_shnum_;
357 // The number of local symbols.
358 unsigned int local_symbol_count_;
359 // The number of local symbols which go into the output file.
360 unsigned int output_local_symbol_count_;
361 // The entries in the symbol table for the external symbols.
362 Symbol** symbols_;
363 // File offset for local symbols.
364 off_t local_symbol_offset_;
365 // Values of local symbols.
366 typename elfcpp::Elf_types<size>::Elf_Addr *values_;
367 };
368
369 // A class to manage the list of all objects.
370
371 class Input_objects
372 {
373 public:
374 Input_objects()
375 : object_list_(), target_(NULL), any_dynamic_(false)
376 { }
377
378 // The type of the list of input objects.
379 typedef std::list<Object*> Object_list;
380
381 // Add an object to the list.
382 void
383 add_object(Object*);
384
385 // Get the target we should use for the output file.
386 Target*
387 target() const
388 { return this->target_; }
389
390 // Iterate over all objects.
391 Object_list::const_iterator
392 begin() const
393 { return this->object_list_.begin(); }
394
395 Object_list::const_iterator
396 end() const
397 { return this->object_list_.end(); }
398
399 // Return whether we have seen any dynamic objects.
400 bool
401 any_dynamic() const
402 { return this->any_dynamic_; }
403
404 private:
405 Input_objects(const Input_objects&);
406 Input_objects& operator=(const Input_objects&);
407
408 Object_list object_list_;
409 Target* target_;
410 bool any_dynamic_;
411 };
412
413 // Return an Object appropriate for the input file. P is BYTES long,
414 // and holds the ELF header.
415
416 extern Object*
417 make_elf_object(const std::string& name, Input_file*,
418 off_t offset, const unsigned char* p,
419 off_t bytes);
420
421 } // end namespace gold
422
423 #endif // !defined(GOLD_OBJECT_H)