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, | |
ead1e424 ILT |
157 | Layout* layout, Read_relocs_data* rd) |
158 | { return this->do_scan_relocs(options, symtab, layout, 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 | ||
ead1e424 ILT |
187 | // Set the offset of an input section within its output section. |
188 | void | |
189 | set_section_offset(unsigned int shndx, off_t off) | |
190 | { | |
191 | assert(shndx < this->map_to_output_.size()); | |
192 | this->map_to_output_[shndx].offset = off; | |
193 | } | |
194 | ||
92e059d8 ILT |
195 | // Return the name of a section given a section index. This is only |
196 | // used for error messages. | |
197 | std::string | |
198 | section_name(unsigned int shnum) | |
199 | { return this->do_section_name(shnum); } | |
200 | ||
a783673b | 201 | protected: |
a2fb1b05 ILT |
202 | // What we need to know to map an input section to an output |
203 | // section. We keep an array of these, one for each input section, | |
204 | // indexed by the input section number. | |
205 | struct Map_to_output | |
206 | { | |
207 | // The output section. This is NULL if the input section is to be | |
208 | // discarded. | |
209 | Output_section* output_section; | |
210 | // The offset within the output section. | |
211 | off_t offset; | |
212 | }; | |
213 | ||
bae7f79e | 214 | // Read the symbols--implemented by child class. |
12e14209 ILT |
215 | virtual void |
216 | do_read_symbols(Read_symbols_data*) = 0; | |
bae7f79e ILT |
217 | |
218 | // Add symbol information to the global symbol table--implemented by | |
219 | // child class. | |
220 | virtual void | |
12e14209 | 221 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; |
bae7f79e | 222 | |
92e059d8 ILT |
223 | // Read the relocs--implemented by child class. |
224 | virtual void | |
225 | do_read_relocs(Read_relocs_data*) = 0; | |
226 | ||
227 | // Scan the relocs--implemented by child class. | |
228 | virtual void | |
ead1e424 ILT |
229 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
230 | Read_relocs_data*) = 0; | |
92e059d8 | 231 | |
a2fb1b05 ILT |
232 | // Lay out sections--implemented by child class. |
233 | virtual void | |
12e14209 | 234 | do_layout(Layout*, Read_symbols_data*) = 0; |
a2fb1b05 | 235 | |
75f65a3e ILT |
236 | // Finalize local symbols--implemented by child class. |
237 | virtual off_t | |
238 | do_finalize_local_symbols(off_t, Stringpool*) = 0; | |
239 | ||
61ba1cf9 ILT |
240 | // Relocate the input sections and write out the local |
241 | // symbols--implemented by child class. | |
242 | virtual void | |
243 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
244 | const Layout*, Output_file* of) = 0; |
245 | ||
246 | // Get the name of a section--implemented by child class. | |
247 | virtual std::string | |
248 | do_section_name(unsigned int shnum) = 0; | |
61ba1cf9 | 249 | |
bae7f79e ILT |
250 | // Get the file. |
251 | Input_file* | |
252 | input_file() const | |
253 | { return this->input_file_; } | |
254 | ||
255 | // Get the offset into the file. | |
256 | off_t | |
257 | offset() const | |
258 | { return this->offset_; } | |
259 | ||
260 | // Get a view into the underlying file. | |
261 | const unsigned char* | |
ead1e424 ILT |
262 | get_view(off_t start, off_t size) |
263 | { return this->input_file_->file().get_view(start + this->offset_, size); } | |
bae7f79e | 264 | |
a2fb1b05 ILT |
265 | // Get the number of sections. |
266 | unsigned int | |
75f65a3e | 267 | shnum() const |
a2fb1b05 ILT |
268 | { return this->shnum_; } |
269 | ||
270 | // Set the number of sections. | |
271 | void | |
272 | set_shnum(int shnum) | |
273 | { this->shnum_ = shnum; } | |
274 | ||
14bfc3f5 ILT |
275 | // Set the target. |
276 | void | |
277 | set_target(Target* target) | |
278 | { this->target_ = target; } | |
279 | ||
bae7f79e ILT |
280 | // Read data from the underlying file. |
281 | void | |
ead1e424 ILT |
282 | read(off_t start, off_t size, void* p) |
283 | { this->input_file_->file().read(start + this->offset_, size, p); } | |
bae7f79e ILT |
284 | |
285 | // Get a lasting view into the underlying file. | |
286 | File_view* | |
ead1e424 ILT |
287 | get_lasting_view(off_t start, off_t size) |
288 | { | |
289 | return this->input_file_->file().get_lasting_view(start + this->offset_, | |
290 | size); | |
291 | } | |
bae7f79e | 292 | |
a2fb1b05 ILT |
293 | // Return the vector mapping input sections to output sections. |
294 | std::vector<Map_to_output>& | |
295 | map_to_output() | |
296 | { return this->map_to_output_; } | |
297 | ||
bae7f79e ILT |
298 | private: |
299 | // This class may not be copied. | |
300 | Object(const Object&); | |
301 | Object& operator=(const Object&); | |
302 | ||
61ba1cf9 | 303 | // Name of object as printed to user. |
bae7f79e ILT |
304 | std::string name_; |
305 | // For reading the file. | |
306 | Input_file* input_file_; | |
307 | // Offset within the file--0 for an object file, non-0 for an | |
308 | // archive. | |
309 | off_t offset_; | |
a2fb1b05 ILT |
310 | // Number of input sections. |
311 | unsigned int shnum_; | |
14bfc3f5 ILT |
312 | // Whether this is a dynamic object. |
313 | bool is_dynamic_; | |
314 | // Target functions--may be NULL if the target is not known. | |
315 | Target* target_; | |
a2fb1b05 ILT |
316 | // Mapping from input sections to output section. |
317 | std::vector<Map_to_output> map_to_output_; | |
bae7f79e ILT |
318 | }; |
319 | ||
14bfc3f5 ILT |
320 | // Implement sized_target inline for efficiency. This approach breaks |
321 | // static type checking, but is made safe using asserts. | |
322 | ||
323 | template<int size, bool big_endian> | |
324 | inline Sized_target<size, big_endian>* | |
5482377d | 325 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) |
14bfc3f5 ILT |
326 | { |
327 | assert(this->target_->get_size() == size); | |
328 | assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
329 | return static_cast<Sized_target<size, big_endian>*>(this->target_); | |
330 | } | |
331 | ||
a783673b ILT |
332 | // Implement Object::output_section inline for efficiency. |
333 | inline Output_section* | |
334 | Object::output_section(unsigned int shnum, off_t* poff) | |
335 | { | |
336 | assert(shnum < this->map_to_output_.size()); | |
337 | const Map_to_output& mo(this->map_to_output_[shnum]); | |
338 | *poff = mo.offset; | |
339 | return mo.output_section; | |
340 | } | |
341 | ||
14bfc3f5 | 342 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
343 | |
344 | template<int size, bool big_endian> | |
345 | class Sized_object : public Object | |
346 | { | |
347 | public: | |
348 | Sized_object(const std::string& name, Input_file* input_file, off_t offset, | |
349 | const typename elfcpp::Ehdr<size, big_endian>&); | |
350 | ||
351 | ~Sized_object(); | |
352 | ||
a2fb1b05 | 353 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
354 | void |
355 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
356 | ||
a2fb1b05 | 357 | // Read the symbols. |
bae7f79e | 358 | void |
12e14209 | 359 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 360 | |
12e14209 ILT |
361 | // Add the symbols to the symbol table. |
362 | void | |
363 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 364 | |
92e059d8 ILT |
365 | // Read the relocs. |
366 | void | |
367 | do_read_relocs(Read_relocs_data*); | |
368 | ||
369 | // Scan the relocs and adjust the symbol table. | |
370 | void | |
ead1e424 ILT |
371 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
372 | Read_relocs_data*); | |
92e059d8 ILT |
373 | |
374 | // Lay out the input sections. | |
375 | void | |
376 | do_layout(Layout*, Read_symbols_data*); | |
377 | ||
75f65a3e ILT |
378 | // Finalize the local symbols. |
379 | off_t | |
380 | do_finalize_local_symbols(off_t, Stringpool*); | |
381 | ||
61ba1cf9 ILT |
382 | // Relocate the input sections and write out the local symbols. |
383 | void | |
384 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
385 | const Layout*, Output_file* of); |
386 | ||
387 | // Get the name of a section. | |
388 | std::string | |
389 | do_section_name(unsigned int shnum); | |
61ba1cf9 | 390 | |
a2fb1b05 | 391 | // Return the appropriate Sized_target structure. |
14bfc3f5 ILT |
392 | Sized_target<size, big_endian>* |
393 | sized_target() | |
274e99f9 | 394 | { |
593f47df ILT |
395 | return this->Object::sized_target |
396 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
397 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
274e99f9 | 398 | } |
bae7f79e ILT |
399 | |
400 | private: | |
401 | // This object may not be copied. | |
402 | Sized_object(const Sized_object&); | |
403 | Sized_object& operator=(const Sized_object&); | |
404 | ||
a2fb1b05 ILT |
405 | // For convenience. |
406 | typedef Sized_object<size, big_endian> This; | |
407 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
408 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
409 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
410 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
411 | ||
412 | // Read the section header for section SHNUM. | |
413 | const unsigned char* | |
414 | section_header(unsigned int shnum); | |
a2fb1b05 ILT |
415 | |
416 | // Whether to include a section group in the link. | |
417 | bool | |
418 | include_section_group(Layout*, unsigned int, | |
419 | const elfcpp::Shdr<size, big_endian>&, | |
420 | std::vector<bool>*); | |
421 | ||
422 | // Whether to include a linkonce section in the link. | |
423 | bool | |
424 | include_linkonce_section(Layout*, const char*, | |
425 | const elfcpp::Shdr<size, big_endian>&); | |
426 | ||
61ba1cf9 ILT |
427 | // Views and sizes when relocating. |
428 | struct View_size | |
429 | { | |
430 | unsigned char* view; | |
431 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
432 | off_t offset; | |
433 | off_t view_size; | |
434 | }; | |
435 | ||
436 | typedef std::vector<View_size> Views; | |
437 | ||
438 | // Write section data to the output file. Record the views and | |
439 | // sizes in VIEWS for use when relocating. | |
440 | void | |
441 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); | |
442 | ||
443 | // Relocate the sections in the output file. | |
444 | void | |
92e059d8 ILT |
445 | relocate_sections(const General_options& options, const Symbol_table*, |
446 | const Layout*, const unsigned char* pshdrs, Views*); | |
61ba1cf9 ILT |
447 | |
448 | // Write out the local symbols. | |
449 | void | |
450 | write_local_symbols(Output_file*, const Stringpool*); | |
451 | ||
12e14209 ILT |
452 | // If non-NULL, a view of the section header data. |
453 | File_view* section_headers_; | |
bae7f79e ILT |
454 | // ELF file header e_flags field. |
455 | unsigned int flags_; | |
bae7f79e ILT |
456 | // File offset of section header table. |
457 | off_t shoff_; | |
bae7f79e ILT |
458 | // Offset of SHT_STRTAB section holding section names. |
459 | unsigned int shstrndx_; | |
460 | // Index of SHT_SYMTAB section. | |
461 | unsigned int symtab_shnum_; | |
61ba1cf9 ILT |
462 | // The number of local symbols. |
463 | unsigned int local_symbol_count_; | |
464 | // The number of local symbols which go into the output file. | |
465 | unsigned int output_local_symbol_count_; | |
14bfc3f5 ILT |
466 | // The entries in the symbol table for the external symbols. |
467 | Symbol** symbols_; | |
75f65a3e ILT |
468 | // File offset for local symbols. |
469 | off_t local_symbol_offset_; | |
61ba1cf9 ILT |
470 | // Values of local symbols. |
471 | typename elfcpp::Elf_types<size>::Elf_Addr *values_; | |
bae7f79e ILT |
472 | }; |
473 | ||
54dc6425 | 474 | // A class to manage the list of all objects. |
a2fb1b05 | 475 | |
54dc6425 ILT |
476 | class Input_objects |
477 | { | |
478 | public: | |
479 | Input_objects() | |
75f65a3e | 480 | : object_list_(), target_(NULL), any_dynamic_(false) |
54dc6425 ILT |
481 | { } |
482 | ||
483 | // The type of the list of input objects. | |
484 | typedef std::list<Object*> Object_list; | |
485 | ||
486 | // Add an object to the list. | |
487 | void | |
488 | add_object(Object*); | |
489 | ||
75f65a3e ILT |
490 | // Get the target we should use for the output file. |
491 | Target* | |
492 | target() const | |
493 | { return this->target_; } | |
494 | ||
54dc6425 ILT |
495 | // Iterate over all objects. |
496 | Object_list::const_iterator | |
497 | begin() const | |
498 | { return this->object_list_.begin(); } | |
499 | ||
500 | Object_list::const_iterator | |
501 | end() const | |
502 | { return this->object_list_.end(); } | |
503 | ||
504 | // Return whether we have seen any dynamic objects. | |
505 | bool | |
506 | any_dynamic() const | |
507 | { return this->any_dynamic_; } | |
508 | ||
509 | private: | |
510 | Input_objects(const Input_objects&); | |
511 | Input_objects& operator=(const Input_objects&); | |
512 | ||
513 | Object_list object_list_; | |
75f65a3e | 514 | Target* target_; |
54dc6425 ILT |
515 | bool any_dynamic_; |
516 | }; | |
a2fb1b05 | 517 | |
92e059d8 ILT |
518 | // Some of the information we pass to the relocation routines. We |
519 | // group this together to avoid passing a dozen different arguments. | |
520 | ||
521 | template<int size, bool big_endian> | |
522 | struct Relocate_info | |
523 | { | |
524 | // Command line options. | |
525 | const General_options* options; | |
526 | // Symbol table. | |
527 | const Symbol_table* symtab; | |
528 | // Layout. | |
529 | const Layout* layout; | |
530 | // Object being relocated. | |
531 | Sized_object<size, big_endian>* object; | |
532 | // Number of local symbols. | |
533 | unsigned int local_symbol_count; | |
534 | // Values of local symbols. | |
535 | typename elfcpp::Elf_types<size>::Elf_Addr *values; | |
536 | // Global symbols. | |
537 | Symbol** symbols; | |
538 | // Section index of relocation section. | |
539 | unsigned int reloc_shndx; | |
540 | // Section index of section being relocated. | |
541 | unsigned int data_shndx; | |
542 | ||
543 | // Return a string showing the location of a relocation. This is | |
544 | // only used for error messages. | |
545 | std::string | |
546 | location(size_t relnum, off_t reloffset) const; | |
547 | }; | |
548 | ||
bae7f79e ILT |
549 | // Return an Object appropriate for the input file. P is BYTES long, |
550 | // and holds the ELF header. | |
551 | ||
61ba1cf9 ILT |
552 | extern Object* |
553 | make_elf_object(const std::string& name, Input_file*, | |
554 | off_t offset, const unsigned char* p, | |
555 | off_t bytes); | |
bae7f79e ILT |
556 | |
557 | } // end namespace gold | |
558 | ||
559 | #endif // !defined(GOLD_OBJECT_H) |