]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/object.h
Snapshot. Now able to produce a minimal executable which actually
[thirdparty/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>
a2fb1b05 7#include <vector>
14bfc3f5 8
bae7f79e 9#include "elfcpp.h"
bae7f79e 10#include "fileread.h"
14bfc3f5
ILT
11#include "target.h"
12#include "symtab.h"
bae7f79e
ILT
13
14namespace gold
15{
16
75f65a3e 17class Stringpool;
a2fb1b05 18class Layout;
61ba1cf9
ILT
19class Output_section;
20class Output_file;
a2fb1b05 21
bae7f79e
ILT
22// Data to pass from read_symbols() to add_symbols().
23
24struct 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
14bfc3f5
ILT
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>
bae7f79e
ILT
40
41class 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.
14bfc3f5
ILT
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),
a2fb1b05
ILT
51 shnum_(0), is_dynamic_(is_dynamic), target_(NULL),
52 map_to_output_()
bae7f79e
ILT
53 { }
54
55 virtual ~Object()
56 { }
57
14bfc3f5 58 // Return the name of the object as we would report it to the tuser.
bae7f79e
ILT
59 const std::string&
60 name() const
61 { return this->name_; }
62
14bfc3f5
ILT
63 // Return whether this is a dynamic object.
64 bool
65 is_dynamic() const
66 { return this->is_dynamic_; }
67
14bfc3f5
ILT
68 // Return the target structure associated with this object.
69 Target*
a2fb1b05 70 target() const
14bfc3f5
ILT
71 { return this->target_; }
72
a2fb1b05
ILT
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
d288e464 88#ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
14bfc3f5
ILT
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();
d288e464 95#endif
bae7f79e 96
a2fb1b05
ILT
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
75f65a3e
ILT
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
61ba1cf9
ILT
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
a2fb1b05
ILT
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
75f65a3e
ILT
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
61ba1cf9
ILT
142 {
143 assert(shnum < this->map_to_output_.size());
144 return &this->map_to_output_[shnum];
145 }
75f65a3e
ILT
146
147 protected:
bae7f79e
ILT
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
14bfc3f5 155 do_add_symbols(Symbol_table*, Read_symbols_data) = 0;
bae7f79e 156
a2fb1b05
ILT
157 // Lay out sections--implemented by child class.
158 virtual void
159 do_layout(Layout*) = 0;
160
75f65a3e
ILT
161 // Finalize local symbols--implemented by child class.
162 virtual off_t
163 do_finalize_local_symbols(off_t, Stringpool*) = 0;
164
61ba1cf9
ILT
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
bae7f79e
ILT
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
a2fb1b05
ILT
185 // Get the number of sections.
186 unsigned int
75f65a3e 187 shnum() const
a2fb1b05
ILT
188 { return this->shnum_; }
189
190 // Set the number of sections.
191 void
192 set_shnum(int shnum)
193 { this->shnum_ = shnum; }
194
14bfc3f5
ILT
195 // Set the target.
196 void
197 set_target(Target* target)
198 { this->target_ = target; }
199
bae7f79e
ILT
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
a2fb1b05
ILT
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
bae7f79e
ILT
213 private:
214 // This class may not be copied.
215 Object(const Object&);
216 Object& operator=(const Object&);
217
61ba1cf9 218 // Name of object as printed to user.
bae7f79e
ILT
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_;
a2fb1b05
ILT
225 // Number of input sections.
226 unsigned int shnum_;
14bfc3f5
ILT
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_;
a2fb1b05
ILT
231 // Mapping from input sections to output section.
232 std::vector<Map_to_output> map_to_output_;
bae7f79e
ILT
233};
234
d288e464
ILT
235#ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
236
14bfc3f5
ILT
237// Implement sized_target inline for efficiency. This approach breaks
238// static type checking, but is made safe using asserts.
239
240template<int size, bool big_endian>
241inline Sized_target<size, big_endian>*
242Object::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
d288e464
ILT
249#endif
250
14bfc3f5 251// A regular object file. This is size and endian specific.
bae7f79e
ILT
252
253template<int size, bool big_endian>
254class 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
a2fb1b05 262 // Set up the object file based on the ELF header.
bae7f79e
ILT
263 void
264 setup(const typename elfcpp::Ehdr<size, big_endian>&);
265
a2fb1b05 266 // Read the symbols.
bae7f79e
ILT
267 Read_symbols_data
268 do_read_symbols();
269
a2fb1b05 270 // Add the symbols to the symbol table.
bae7f79e 271 void
14bfc3f5
ILT
272 do_add_symbols(Symbol_table*, Read_symbols_data);
273
a2fb1b05
ILT
274 // Lay out the input sections.
275 void
276 do_layout(Layout*);
277
75f65a3e
ILT
278 // Finalize the local symbols.
279 off_t
280 do_finalize_local_symbols(off_t, Stringpool*);
281
61ba1cf9
ILT
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
a2fb1b05 287 // Return the appropriate Sized_target structure.
14bfc3f5
ILT
288 Sized_target<size, big_endian>*
289 sized_target()
274e99f9
ILT
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 }
bae7f79e
ILT
297
298 private:
299 // This object may not be copied.
300 Sized_object(const Sized_object&);
301 Sized_object& operator=(const Sized_object&);
302
a2fb1b05
ILT
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;
75f65a3e
ILT
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);
a2fb1b05
ILT
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
61ba1cf9
ILT
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
bae7f79e
ILT
349 // ELF file header e_flags field.
350 unsigned int flags_;
bae7f79e
ILT
351 // File offset of section header table.
352 off_t shoff_;
bae7f79e
ILT
353 // Offset of SHT_STRTAB section holding section names.
354 unsigned int shstrndx_;
355 // Index of SHT_SYMTAB section.
356 unsigned int symtab_shnum_;
61ba1cf9
ILT
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_;
14bfc3f5
ILT
361 // The entries in the symbol table for the external symbols.
362 Symbol** symbols_;
75f65a3e
ILT
363 // File offset for local symbols.
364 off_t local_symbol_offset_;
61ba1cf9
ILT
365 // Values of local symbols.
366 typename elfcpp::Elf_types<size>::Elf_Addr *values_;
bae7f79e
ILT
367};
368
54dc6425 369// A class to manage the list of all objects.
a2fb1b05 370
54dc6425
ILT
371class Input_objects
372{
373 public:
374 Input_objects()
75f65a3e 375 : object_list_(), target_(NULL), any_dynamic_(false)
54dc6425
ILT
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
75f65a3e
ILT
385 // Get the target we should use for the output file.
386 Target*
387 target() const
388 { return this->target_; }
389
54dc6425
ILT
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_;
75f65a3e 409 Target* target_;
54dc6425
ILT
410 bool any_dynamic_;
411};
a2fb1b05 412
bae7f79e
ILT
413// Return an Object appropriate for the input file. P is BYTES long,
414// and holds the ELF header.
415
61ba1cf9
ILT
416extern Object*
417make_elf_object(const std::string& name, Input_file*,
418 off_t offset, const unsigned char* p,
419 off_t bytes);
bae7f79e
ILT
420
421} // end namespace gold
422
423#endif // !defined(GOLD_OBJECT_H)