Commit | Line | Data |
---|---|---|
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 ILT |
7 | #include <list> |
8 | #include <string> | |
a2fb1b05 | 9 | #include <vector> |
14bfc3f5 | 10 | |
bae7f79e | 11 | #include "elfcpp.h" |
bae7f79e | 12 | #include "fileread.h" |
14bfc3f5 ILT |
13 | #include "target.h" |
14 | #include "symtab.h" | |
bae7f79e ILT |
15 | |
16 | namespace gold | |
17 | { | |
18 | ||
92e059d8 | 19 | class General_options; |
75f65a3e | 20 | class Stringpool; |
a2fb1b05 | 21 | class Layout; |
61ba1cf9 ILT |
22 | class Output_section; |
23 | class Output_file; | |
a2fb1b05 | 24 | |
bae7f79e ILT |
25 | // Data to pass from read_symbols() to add_symbols(). |
26 | ||
27 | struct 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 | ||
48 | struct 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 | ||
65 | struct 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 | ||
bae7f79e | 74 | // Object is an interface which represents either a 32-bit or a 64-bit |
14bfc3f5 ILT |
75 | // input object. This can be a regular object file (ET_REL) or a |
76 | // shared object (ET_DYN). The actual instantiations are | |
77 | // Sized_object<32> and Sized_object<64> | |
bae7f79e ILT |
78 | |
79 | class Object | |
80 | { | |
81 | public: | |
82 | // NAME is the name of the object as we would report it to the user | |
83 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
84 | // used to read the file. OFFSET is the offset within the input | |
85 | // file--0 for a .o or .so file, something else for a .a file. | |
14bfc3f5 ILT |
86 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
87 | off_t offset = 0) | |
88 | : name_(name), input_file_(input_file), offset_(offset), | |
a2fb1b05 ILT |
89 | shnum_(0), is_dynamic_(is_dynamic), target_(NULL), |
90 | map_to_output_() | |
bae7f79e ILT |
91 | { } |
92 | ||
93 | virtual ~Object() | |
94 | { } | |
95 | ||
14bfc3f5 | 96 | // Return the name of the object as we would report it to the tuser. |
bae7f79e ILT |
97 | const std::string& |
98 | name() const | |
99 | { return this->name_; } | |
100 | ||
14bfc3f5 ILT |
101 | // Return whether this is a dynamic object. |
102 | bool | |
103 | is_dynamic() const | |
104 | { return this->is_dynamic_; } | |
105 | ||
14bfc3f5 ILT |
106 | // Return the target structure associated with this object. |
107 | Target* | |
a2fb1b05 | 108 | target() const |
14bfc3f5 ILT |
109 | { return this->target_; } |
110 | ||
a2fb1b05 ILT |
111 | // Lock the underlying file. |
112 | void | |
113 | lock() | |
114 | { this->input_file_->file().lock(); } | |
115 | ||
116 | // Unlock the underlying file. | |
117 | void | |
118 | unlock() | |
119 | { this->input_file_->file().unlock(); } | |
120 | ||
121 | // Return whether the underlying file is locked. | |
122 | bool | |
123 | is_locked() const | |
124 | { return this->input_file_->file().is_locked(); } | |
125 | ||
14bfc3f5 ILT |
126 | // Return the sized target structure associated with this object. |
127 | // This is like the target method but it returns a pointer of | |
128 | // appropriate checked type. | |
129 | template<int size, bool big_endian> | |
130 | Sized_target<size, big_endian>* | |
5482377d | 131 | sized_target(ACCEPT_SIZE_ENDIAN_ONLY); |
bae7f79e | 132 | |
92e059d8 | 133 | // Read the symbol information. |
12e14209 ILT |
134 | void |
135 | read_symbols(Read_symbols_data* sd) | |
136 | { return this->do_read_symbols(sd); } | |
a2fb1b05 | 137 | |
92e059d8 ILT |
138 | // Pass sections which should be included in the link to the Layout |
139 | // object, and record where the sections go in the output file. | |
140 | void | |
141 | layout(Layout* lay, Read_symbols_data* sd) | |
142 | { this->do_layout(lay, sd); } | |
143 | ||
a2fb1b05 ILT |
144 | // Add symbol information to the global symbol table. |
145 | void | |
12e14209 ILT |
146 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd) |
147 | { this->do_add_symbols(symtab, sd); } | |
a2fb1b05 | 148 | |
92e059d8 | 149 | // Read the relocs. |
a2fb1b05 | 150 | void |
92e059d8 ILT |
151 | read_relocs(Read_relocs_data* rd) |
152 | { return this->do_read_relocs(rd); } | |
153 | ||
154 | // Scan the relocs and adjust the symbol table. | |
155 | void | |
156 | scan_relocs(const General_options& options, Symbol_table* symtab, | |
157 | Read_relocs_data* rd) | |
158 | { return this->do_scan_relocs(options, symtab, rd); } | |
a2fb1b05 | 159 | |
75f65a3e ILT |
160 | // Initial local symbol processing: set the offset where local |
161 | // symbol information will be stored; add local symbol names to | |
162 | // *POOL; return the offset following the local symbols. | |
163 | off_t | |
164 | finalize_local_symbols(off_t off, Stringpool* pool) | |
165 | { return this->do_finalize_local_symbols(off, pool); } | |
166 | ||
61ba1cf9 ILT |
167 | // Relocate the input sections and write out the local symbols. |
168 | void | |
169 | relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
170 | const Layout* layout, Output_file* of) |
171 | { return this->do_relocate(options, symtab, layout, of); } | |
61ba1cf9 | 172 | |
a783673b ILT |
173 | // Return whether an input section is being included in the link. |
174 | bool | |
175 | is_section_included(unsigned int shnum) const | |
176 | { | |
177 | assert(shnum < this->map_to_output_.size()); | |
178 | return this->map_to_output_[shnum].output_section != NULL; | |
179 | } | |
180 | ||
181 | // Given a section index, return the corresponding Output_section | |
182 | // (which will be NULL if the section is not included in the link) | |
183 | // and set *POFF to the offset within that section. | |
184 | inline Output_section* | |
185 | output_section(unsigned int shnum, off_t* poff); | |
186 | ||
92e059d8 ILT |
187 | // Return the name of a section given a section index. This is only |
188 | // used for error messages. | |
189 | std::string | |
190 | section_name(unsigned int shnum) | |
191 | { return this->do_section_name(shnum); } | |
192 | ||
a783673b | 193 | protected: |
a2fb1b05 ILT |
194 | // What we need to know to map an input section to an output |
195 | // section. We keep an array of these, one for each input section, | |
196 | // indexed by the input section number. | |
197 | struct Map_to_output | |
198 | { | |
199 | // The output section. This is NULL if the input section is to be | |
200 | // discarded. | |
201 | Output_section* output_section; | |
202 | // The offset within the output section. | |
203 | off_t offset; | |
204 | }; | |
205 | ||
bae7f79e | 206 | // Read the symbols--implemented by child class. |
12e14209 ILT |
207 | virtual void |
208 | do_read_symbols(Read_symbols_data*) = 0; | |
bae7f79e ILT |
209 | |
210 | // Add symbol information to the global symbol table--implemented by | |
211 | // child class. | |
212 | virtual void | |
12e14209 | 213 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; |
bae7f79e | 214 | |
92e059d8 ILT |
215 | // Read the relocs--implemented by child class. |
216 | virtual void | |
217 | do_read_relocs(Read_relocs_data*) = 0; | |
218 | ||
219 | // Scan the relocs--implemented by child class. | |
220 | virtual void | |
221 | do_scan_relocs(const General_options&, Symbol_table*, Read_relocs_data*) = 0; | |
222 | ||
a2fb1b05 ILT |
223 | // Lay out sections--implemented by child class. |
224 | virtual void | |
12e14209 | 225 | do_layout(Layout*, Read_symbols_data*) = 0; |
a2fb1b05 | 226 | |
75f65a3e ILT |
227 | // Finalize local symbols--implemented by child class. |
228 | virtual off_t | |
229 | do_finalize_local_symbols(off_t, Stringpool*) = 0; | |
230 | ||
61ba1cf9 ILT |
231 | // Relocate the input sections and write out the local |
232 | // symbols--implemented by child class. | |
233 | virtual void | |
234 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
235 | const Layout*, Output_file* of) = 0; |
236 | ||
237 | // Get the name of a section--implemented by child class. | |
238 | virtual std::string | |
239 | do_section_name(unsigned int shnum) = 0; | |
61ba1cf9 | 240 | |
bae7f79e ILT |
241 | // Get the file. |
242 | Input_file* | |
243 | input_file() const | |
244 | { return this->input_file_; } | |
245 | ||
246 | // Get the offset into the file. | |
247 | off_t | |
248 | offset() const | |
249 | { return this->offset_; } | |
250 | ||
251 | // Get a view into the underlying file. | |
252 | const unsigned char* | |
253 | get_view(off_t start, off_t size); | |
254 | ||
a2fb1b05 ILT |
255 | // Get the number of sections. |
256 | unsigned int | |
75f65a3e | 257 | shnum() const |
a2fb1b05 ILT |
258 | { return this->shnum_; } |
259 | ||
260 | // Set the number of sections. | |
261 | void | |
262 | set_shnum(int shnum) | |
263 | { this->shnum_ = shnum; } | |
264 | ||
14bfc3f5 ILT |
265 | // Set the target. |
266 | void | |
267 | set_target(Target* target) | |
268 | { this->target_ = target; } | |
269 | ||
bae7f79e ILT |
270 | // Read data from the underlying file. |
271 | void | |
272 | read(off_t start, off_t size, void* p); | |
273 | ||
274 | // Get a lasting view into the underlying file. | |
275 | File_view* | |
276 | get_lasting_view(off_t start, off_t size); | |
277 | ||
a2fb1b05 ILT |
278 | // Return the vector mapping input sections to output sections. |
279 | std::vector<Map_to_output>& | |
280 | map_to_output() | |
281 | { return this->map_to_output_; } | |
282 | ||
bae7f79e ILT |
283 | private: |
284 | // This class may not be copied. | |
285 | Object(const Object&); | |
286 | Object& operator=(const Object&); | |
287 | ||
61ba1cf9 | 288 | // Name of object as printed to user. |
bae7f79e ILT |
289 | std::string name_; |
290 | // For reading the file. | |
291 | Input_file* input_file_; | |
292 | // Offset within the file--0 for an object file, non-0 for an | |
293 | // archive. | |
294 | off_t offset_; | |
a2fb1b05 ILT |
295 | // Number of input sections. |
296 | unsigned int shnum_; | |
14bfc3f5 ILT |
297 | // Whether this is a dynamic object. |
298 | bool is_dynamic_; | |
299 | // Target functions--may be NULL if the target is not known. | |
300 | Target* target_; | |
a2fb1b05 ILT |
301 | // Mapping from input sections to output section. |
302 | std::vector<Map_to_output> map_to_output_; | |
bae7f79e ILT |
303 | }; |
304 | ||
14bfc3f5 ILT |
305 | // Implement sized_target inline for efficiency. This approach breaks |
306 | // static type checking, but is made safe using asserts. | |
307 | ||
308 | template<int size, bool big_endian> | |
309 | inline Sized_target<size, big_endian>* | |
5482377d | 310 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) |
14bfc3f5 ILT |
311 | { |
312 | assert(this->target_->get_size() == size); | |
313 | assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
314 | return static_cast<Sized_target<size, big_endian>*>(this->target_); | |
315 | } | |
316 | ||
a783673b ILT |
317 | // Implement Object::output_section inline for efficiency. |
318 | inline Output_section* | |
319 | Object::output_section(unsigned int shnum, off_t* poff) | |
320 | { | |
321 | assert(shnum < this->map_to_output_.size()); | |
322 | const Map_to_output& mo(this->map_to_output_[shnum]); | |
323 | *poff = mo.offset; | |
324 | return mo.output_section; | |
325 | } | |
326 | ||
14bfc3f5 | 327 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
328 | |
329 | template<int size, bool big_endian> | |
330 | class Sized_object : public Object | |
331 | { | |
332 | public: | |
333 | Sized_object(const std::string& name, Input_file* input_file, off_t offset, | |
334 | const typename elfcpp::Ehdr<size, big_endian>&); | |
335 | ||
336 | ~Sized_object(); | |
337 | ||
a2fb1b05 | 338 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
339 | void |
340 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
341 | ||
a2fb1b05 | 342 | // Read the symbols. |
bae7f79e | 343 | void |
12e14209 | 344 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 345 | |
12e14209 ILT |
346 | // Add the symbols to the symbol table. |
347 | void | |
348 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 349 | |
92e059d8 ILT |
350 | // Read the relocs. |
351 | void | |
352 | do_read_relocs(Read_relocs_data*); | |
353 | ||
354 | // Scan the relocs and adjust the symbol table. | |
355 | void | |
356 | do_scan_relocs(const General_options&, Symbol_table*, Read_relocs_data*); | |
357 | ||
358 | // Lay out the input sections. | |
359 | void | |
360 | do_layout(Layout*, Read_symbols_data*); | |
361 | ||
75f65a3e ILT |
362 | // Finalize the local symbols. |
363 | off_t | |
364 | do_finalize_local_symbols(off_t, Stringpool*); | |
365 | ||
61ba1cf9 ILT |
366 | // Relocate the input sections and write out the local symbols. |
367 | void | |
368 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
369 | const Layout*, Output_file* of); |
370 | ||
371 | // Get the name of a section. | |
372 | std::string | |
373 | do_section_name(unsigned int shnum); | |
61ba1cf9 | 374 | |
a2fb1b05 | 375 | // Return the appropriate Sized_target structure. |
14bfc3f5 ILT |
376 | Sized_target<size, big_endian>* |
377 | sized_target() | |
274e99f9 | 378 | { |
5482377d ILT |
379 | return this->Object::sized_target SELECT_SIZE_ENDIAN_NAME ( |
380 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
274e99f9 | 381 | } |
bae7f79e ILT |
382 | |
383 | private: | |
384 | // This object may not be copied. | |
385 | Sized_object(const Sized_object&); | |
386 | Sized_object& operator=(const Sized_object&); | |
387 | ||
a2fb1b05 ILT |
388 | // For convenience. |
389 | typedef Sized_object<size, big_endian> This; | |
390 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
391 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
392 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
393 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
394 | ||
395 | // Read the section header for section SHNUM. | |
396 | const unsigned char* | |
397 | section_header(unsigned int shnum); | |
a2fb1b05 ILT |
398 | |
399 | // Whether to include a section group in the link. | |
400 | bool | |
401 | include_section_group(Layout*, unsigned int, | |
402 | const elfcpp::Shdr<size, big_endian>&, | |
403 | std::vector<bool>*); | |
404 | ||
405 | // Whether to include a linkonce section in the link. | |
406 | bool | |
407 | include_linkonce_section(Layout*, const char*, | |
408 | const elfcpp::Shdr<size, big_endian>&); | |
409 | ||
61ba1cf9 ILT |
410 | // Views and sizes when relocating. |
411 | struct View_size | |
412 | { | |
413 | unsigned char* view; | |
414 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
415 | off_t offset; | |
416 | off_t view_size; | |
417 | }; | |
418 | ||
419 | typedef std::vector<View_size> Views; | |
420 | ||
421 | // Write section data to the output file. Record the views and | |
422 | // sizes in VIEWS for use when relocating. | |
423 | void | |
424 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); | |
425 | ||
426 | // Relocate the sections in the output file. | |
427 | void | |
92e059d8 ILT |
428 | relocate_sections(const General_options& options, const Symbol_table*, |
429 | const Layout*, const unsigned char* pshdrs, Views*); | |
61ba1cf9 ILT |
430 | |
431 | // Write out the local symbols. | |
432 | void | |
433 | write_local_symbols(Output_file*, const Stringpool*); | |
434 | ||
12e14209 ILT |
435 | // If non-NULL, a view of the section header data. |
436 | File_view* section_headers_; | |
bae7f79e ILT |
437 | // ELF file header e_flags field. |
438 | unsigned int flags_; | |
bae7f79e ILT |
439 | // File offset of section header table. |
440 | off_t shoff_; | |
bae7f79e ILT |
441 | // Offset of SHT_STRTAB section holding section names. |
442 | unsigned int shstrndx_; | |
443 | // Index of SHT_SYMTAB section. | |
444 | unsigned int symtab_shnum_; | |
61ba1cf9 ILT |
445 | // The number of local symbols. |
446 | unsigned int local_symbol_count_; | |
447 | // The number of local symbols which go into the output file. | |
448 | unsigned int output_local_symbol_count_; | |
14bfc3f5 ILT |
449 | // The entries in the symbol table for the external symbols. |
450 | Symbol** symbols_; | |
75f65a3e ILT |
451 | // File offset for local symbols. |
452 | off_t local_symbol_offset_; | |
61ba1cf9 ILT |
453 | // Values of local symbols. |
454 | typename elfcpp::Elf_types<size>::Elf_Addr *values_; | |
bae7f79e ILT |
455 | }; |
456 | ||
54dc6425 | 457 | // A class to manage the list of all objects. |
a2fb1b05 | 458 | |
54dc6425 ILT |
459 | class Input_objects |
460 | { | |
461 | public: | |
462 | Input_objects() | |
75f65a3e | 463 | : object_list_(), target_(NULL), any_dynamic_(false) |
54dc6425 ILT |
464 | { } |
465 | ||
466 | // The type of the list of input objects. | |
467 | typedef std::list<Object*> Object_list; | |
468 | ||
469 | // Add an object to the list. | |
470 | void | |
471 | add_object(Object*); | |
472 | ||
75f65a3e ILT |
473 | // Get the target we should use for the output file. |
474 | Target* | |
475 | target() const | |
476 | { return this->target_; } | |
477 | ||
54dc6425 ILT |
478 | // Iterate over all objects. |
479 | Object_list::const_iterator | |
480 | begin() const | |
481 | { return this->object_list_.begin(); } | |
482 | ||
483 | Object_list::const_iterator | |
484 | end() const | |
485 | { return this->object_list_.end(); } | |
486 | ||
487 | // Return whether we have seen any dynamic objects. | |
488 | bool | |
489 | any_dynamic() const | |
490 | { return this->any_dynamic_; } | |
491 | ||
492 | private: | |
493 | Input_objects(const Input_objects&); | |
494 | Input_objects& operator=(const Input_objects&); | |
495 | ||
496 | Object_list object_list_; | |
75f65a3e | 497 | Target* target_; |
54dc6425 ILT |
498 | bool any_dynamic_; |
499 | }; | |
a2fb1b05 | 500 | |
92e059d8 ILT |
501 | // Some of the information we pass to the relocation routines. We |
502 | // group this together to avoid passing a dozen different arguments. | |
503 | ||
504 | template<int size, bool big_endian> | |
505 | struct Relocate_info | |
506 | { | |
507 | // Command line options. | |
508 | const General_options* options; | |
509 | // Symbol table. | |
510 | const Symbol_table* symtab; | |
511 | // Layout. | |
512 | const Layout* layout; | |
513 | // Object being relocated. | |
514 | Sized_object<size, big_endian>* object; | |
515 | // Number of local symbols. | |
516 | unsigned int local_symbol_count; | |
517 | // Values of local symbols. | |
518 | typename elfcpp::Elf_types<size>::Elf_Addr *values; | |
519 | // Global symbols. | |
520 | Symbol** symbols; | |
521 | // Section index of relocation section. | |
522 | unsigned int reloc_shndx; | |
523 | // Section index of section being relocated. | |
524 | unsigned int data_shndx; | |
525 | ||
526 | // Return a string showing the location of a relocation. This is | |
527 | // only used for error messages. | |
528 | std::string | |
529 | location(size_t relnum, off_t reloffset) const; | |
530 | }; | |
531 | ||
bae7f79e ILT |
532 | // Return an Object appropriate for the input file. P is BYTES long, |
533 | // and holds the ELF header. | |
534 | ||
61ba1cf9 ILT |
535 | extern Object* |
536 | make_elf_object(const std::string& name, Input_file*, | |
537 | off_t offset, const unsigned char* p, | |
538 | off_t bytes); | |
bae7f79e ILT |
539 | |
540 | } // end namespace gold | |
541 | ||
542 | #endif // !defined(GOLD_OBJECT_H) |