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 | ||
92e059d8 | 6 | #include <string> |
a2fb1b05 | 7 | #include <vector> |
14bfc3f5 | 8 | |
bae7f79e | 9 | #include "elfcpp.h" |
645f8123 | 10 | #include "elfcpp_file.h" |
bae7f79e | 11 | #include "fileread.h" |
14bfc3f5 | 12 | #include "target.h" |
bae7f79e ILT |
13 | |
14 | namespace gold | |
15 | { | |
16 | ||
92e059d8 | 17 | class General_options; |
75f65a3e | 18 | class Stringpool; |
a2fb1b05 | 19 | class Layout; |
61ba1cf9 ILT |
20 | class Output_section; |
21 | class Output_file; | |
f6ce93d6 | 22 | class Dynobj; |
a2fb1b05 | 23 | |
bae7f79e ILT |
24 | // Data to pass from read_symbols() to add_symbols(). |
25 | ||
26 | struct Read_symbols_data | |
27 | { | |
12e14209 ILT |
28 | // Section headers. |
29 | File_view* section_headers; | |
30 | // Section names. | |
31 | File_view* section_names; | |
32 | // Size of section name data in bytes. | |
33 | off_t section_names_size; | |
bae7f79e ILT |
34 | // Symbol data. |
35 | File_view* symbols; | |
36 | // Size of symbol data in bytes. | |
37 | off_t symbols_size; | |
38 | // Symbol names. | |
39 | File_view* symbol_names; | |
40 | // Size of symbol name data in bytes. | |
41 | off_t symbol_names_size; | |
dbe717ef ILT |
42 | |
43 | // Version information. This is only used on dynamic objects. | |
44 | // Version symbol data (from SHT_GNU_versym section). | |
45 | File_view* versym; | |
46 | off_t versym_size; | |
47 | // Version definition data (from SHT_GNU_verdef section). | |
48 | File_view* verdef; | |
49 | off_t verdef_size; | |
50 | unsigned int verdef_info; | |
51 | // Needed version data (from SHT_GNU_verneed section). | |
52 | File_view* verneed; | |
53 | off_t verneed_size; | |
54 | unsigned int verneed_info; | |
bae7f79e ILT |
55 | }; |
56 | ||
92e059d8 ILT |
57 | // Data about a single relocation section. This is read in |
58 | // read_relocs and processed in scan_relocs. | |
59 | ||
60 | struct Section_relocs | |
61 | { | |
62 | // Index of reloc section. | |
63 | unsigned int reloc_shndx; | |
64 | // Index of section that relocs apply to. | |
65 | unsigned int data_shndx; | |
66 | // Contents of reloc section. | |
67 | File_view* contents; | |
68 | // Reloc section type. | |
69 | unsigned int sh_type; | |
70 | // Number of reloc entries. | |
71 | size_t reloc_count; | |
72 | }; | |
73 | ||
74 | // Relocations in an object file. This is read in read_relocs and | |
75 | // processed in scan_relocs. | |
76 | ||
77 | struct Read_relocs_data | |
78 | { | |
79 | typedef std::vector<Section_relocs> Relocs_list; | |
80 | // The relocations. | |
81 | Relocs_list relocs; | |
82 | // The local symbols. | |
83 | File_view* local_symbols; | |
84 | }; | |
85 | ||
f6ce93d6 ILT |
86 | // Object is an abstract base class which represents either a 32-bit |
87 | // or a 64-bit input object. This can be a regular object file | |
88 | // (ET_REL) or a shared object (ET_DYN). | |
bae7f79e ILT |
89 | |
90 | class Object | |
91 | { | |
92 | public: | |
93 | // NAME is the name of the object as we would report it to the user | |
94 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
95 | // used to read the file. OFFSET is the offset within the input | |
96 | // file--0 for a .o or .so file, something else for a .a file. | |
14bfc3f5 ILT |
97 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
98 | off_t offset = 0) | |
dbe717ef | 99 | : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U), |
f6ce93d6 | 100 | is_dynamic_(is_dynamic), target_(NULL) |
bae7f79e ILT |
101 | { } |
102 | ||
103 | virtual ~Object() | |
104 | { } | |
105 | ||
14bfc3f5 | 106 | // Return the name of the object as we would report it to the tuser. |
bae7f79e ILT |
107 | const std::string& |
108 | name() const | |
109 | { return this->name_; } | |
110 | ||
14bfc3f5 ILT |
111 | // Return whether this is a dynamic object. |
112 | bool | |
113 | is_dynamic() const | |
114 | { return this->is_dynamic_; } | |
115 | ||
14bfc3f5 ILT |
116 | // Return the target structure associated with this object. |
117 | Target* | |
a2fb1b05 | 118 | target() const |
14bfc3f5 ILT |
119 | { return this->target_; } |
120 | ||
a2fb1b05 ILT |
121 | // Lock the underlying file. |
122 | void | |
123 | lock() | |
124 | { this->input_file_->file().lock(); } | |
125 | ||
126 | // Unlock the underlying file. | |
127 | void | |
128 | unlock() | |
129 | { this->input_file_->file().unlock(); } | |
130 | ||
131 | // Return whether the underlying file is locked. | |
132 | bool | |
133 | is_locked() const | |
134 | { return this->input_file_->file().is_locked(); } | |
135 | ||
14bfc3f5 ILT |
136 | // Return the sized target structure associated with this object. |
137 | // This is like the target method but it returns a pointer of | |
138 | // appropriate checked type. | |
139 | template<int size, bool big_endian> | |
140 | Sized_target<size, big_endian>* | |
5482377d | 141 | sized_target(ACCEPT_SIZE_ENDIAN_ONLY); |
bae7f79e | 142 | |
5a6f7e2d ILT |
143 | // Get the number of sections. |
144 | unsigned int | |
145 | shnum() const | |
146 | { return this->shnum_; } | |
a2fb1b05 | 147 | |
f6ce93d6 ILT |
148 | // Return a view of the contents of a section. Set *PLEN to the |
149 | // size. | |
150 | const unsigned char* | |
645f8123 | 151 | section_contents(unsigned int shndx, off_t* plen); |
f6ce93d6 ILT |
152 | |
153 | // Return the name of a section given a section index. This is only | |
154 | // used for error messages. | |
155 | std::string | |
a3ad94ed ILT |
156 | section_name(unsigned int shndx) |
157 | { return this->do_section_name(shndx); } | |
158 | ||
159 | // Return the section flags given a section index. | |
160 | uint64_t | |
161 | section_flags(unsigned int shndx) | |
162 | { return this->do_section_flags(shndx); } | |
f6ce93d6 | 163 | |
5a6f7e2d ILT |
164 | // Read the symbol information. |
165 | void | |
166 | read_symbols(Read_symbols_data* sd) | |
167 | { return this->do_read_symbols(sd); } | |
168 | ||
169 | // Pass sections which should be included in the link to the Layout | |
170 | // object, and record where the sections go in the output file. | |
171 | void | |
172 | layout(const General_options& options, Symbol_table* symtab, | |
173 | Layout* layout, Read_symbols_data* sd) | |
174 | { this->do_layout(options, symtab, layout, sd); } | |
175 | ||
176 | // Add symbol information to the global symbol table. | |
177 | void | |
178 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd) | |
179 | { this->do_add_symbols(symtab, sd); } | |
180 | ||
645f8123 ILT |
181 | // Functions and types for the elfcpp::Elf_file interface. This |
182 | // permit us to use Object as the File template parameter for | |
183 | // elfcpp::Elf_file. | |
184 | ||
185 | // The View class is returned by view. It must support a single | |
186 | // method, data(). This is trivial, because get_view does what we | |
187 | // need. | |
188 | class View | |
189 | { | |
190 | public: | |
191 | View(const unsigned char* p) | |
192 | : p_(p) | |
193 | { } | |
194 | ||
195 | const unsigned char* | |
196 | data() const | |
197 | { return this->p_; } | |
198 | ||
199 | private: | |
200 | const unsigned char* p_; | |
201 | }; | |
202 | ||
203 | // Return a View. | |
204 | View | |
205 | view(off_t file_offset, off_t data_size) | |
206 | { return View(this->get_view(file_offset, data_size)); } | |
207 | ||
208 | // Report an error. | |
209 | void | |
210 | error(const char* format, ...) ATTRIBUTE_PRINTF_2; | |
211 | ||
212 | // A location in the file. | |
213 | struct Location | |
214 | { | |
215 | off_t file_offset; | |
216 | off_t data_size; | |
217 | ||
218 | Location(off_t fo, off_t ds) | |
219 | : file_offset(fo), data_size(ds) | |
220 | { } | |
221 | }; | |
222 | ||
223 | // Get a View given a Location. | |
224 | View view(Location loc) | |
225 | { return View(this->get_view(loc.file_offset, loc.data_size)); } | |
226 | ||
f6ce93d6 ILT |
227 | protected: |
228 | // Read the symbols--implemented by child class. | |
229 | virtual void | |
230 | do_read_symbols(Read_symbols_data*) = 0; | |
231 | ||
232 | // Lay out sections--implemented by child class. | |
233 | virtual void | |
234 | do_layout(const General_options&, Symbol_table*, Layout*, | |
235 | Read_symbols_data*) = 0; | |
236 | ||
237 | // Add symbol information to the global symbol table--implemented by | |
238 | // child class. | |
239 | virtual void | |
240 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; | |
241 | ||
645f8123 ILT |
242 | // Return the location of the contents of a section. Implemented by |
243 | // child class. | |
244 | virtual Location | |
a3ad94ed | 245 | do_section_contents(unsigned int shndx) = 0; |
f6ce93d6 ILT |
246 | |
247 | // Get the name of a section--implemented by child class. | |
248 | virtual std::string | |
a3ad94ed ILT |
249 | do_section_name(unsigned int shndx) = 0; |
250 | ||
251 | // Get section flags--implemented by child class. | |
252 | virtual uint64_t | |
253 | do_section_flags(unsigned int shndx) = 0; | |
f6ce93d6 ILT |
254 | |
255 | // Get the file. | |
256 | Input_file* | |
257 | input_file() const | |
258 | { return this->input_file_; } | |
259 | ||
260 | // Get the offset into the file. | |
261 | off_t | |
262 | offset() const | |
263 | { return this->offset_; } | |
264 | ||
265 | // Get a view into the underlying file. | |
266 | const unsigned char* | |
267 | get_view(off_t start, off_t size) | |
268 | { return this->input_file_->file().get_view(start + this->offset_, size); } | |
269 | ||
270 | // Get a lasting view into the underlying file. | |
271 | File_view* | |
272 | get_lasting_view(off_t start, off_t size) | |
273 | { | |
274 | return this->input_file_->file().get_lasting_view(start + this->offset_, | |
275 | size); | |
276 | } | |
277 | ||
278 | // Read data from the underlying file. | |
279 | void | |
280 | read(off_t start, off_t size, void* p) | |
281 | { this->input_file_->file().read(start + this->offset_, size, p); } | |
282 | ||
283 | // Set the target. | |
284 | void | |
dbe717ef ILT |
285 | set_target(int machine, int size, bool big_endian, int osabi, |
286 | int abiversion); | |
287 | ||
dbe717ef ILT |
288 | // Set the number of sections. |
289 | void | |
290 | set_shnum(int shnum) | |
291 | { this->shnum_ = shnum; } | |
292 | ||
293 | // Functions used by both Sized_relobj and Sized_dynobj. | |
294 | ||
295 | // Read the section data into a Read_symbols_data object. | |
296 | template<int size, bool big_endian> | |
297 | void | |
298 | read_section_data(elfcpp::Elf_file<size, big_endian, Object>*, | |
299 | Read_symbols_data*); | |
300 | ||
301 | // If NAME is the name of a special .gnu.warning section, arrange | |
302 | // for the warning to be issued. SHNDX is the section index. | |
303 | // Return whether it is a warning section. | |
304 | bool | |
305 | handle_gnu_warning_section(const char* name, unsigned int shndx, | |
306 | Symbol_table*); | |
f6ce93d6 ILT |
307 | |
308 | private: | |
309 | // This class may not be copied. | |
310 | Object(const Object&); | |
311 | Object& operator=(const Object&); | |
312 | ||
313 | // Name of object as printed to user. | |
314 | std::string name_; | |
315 | // For reading the file. | |
316 | Input_file* input_file_; | |
317 | // Offset within the file--0 for an object file, non-0 for an | |
318 | // archive. | |
319 | off_t offset_; | |
dbe717ef ILT |
320 | // Number of input sections. |
321 | unsigned int shnum_; | |
f6ce93d6 ILT |
322 | // Whether this is a dynamic object. |
323 | bool is_dynamic_; | |
324 | // Target functions--may be NULL if the target is not known. | |
325 | Target* target_; | |
326 | }; | |
327 | ||
328 | // Implement sized_target inline for efficiency. This approach breaks | |
329 | // static type checking, but is made safe using asserts. | |
330 | ||
331 | template<int size, bool big_endian> | |
332 | inline Sized_target<size, big_endian>* | |
333 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) | |
334 | { | |
a3ad94ed ILT |
335 | gold_assert(this->target_->get_size() == size); |
336 | gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
f6ce93d6 ILT |
337 | return static_cast<Sized_target<size, big_endian>*>(this->target_); |
338 | } | |
339 | ||
340 | // A regular object (ET_REL). This is an abstract base class itself. | |
341 | // The implementations is the template class Sized_relobj. | |
342 | ||
343 | class Relobj : public Object | |
344 | { | |
345 | public: | |
346 | Relobj(const std::string& name, Input_file* input_file, off_t offset = 0) | |
347 | : Object(name, input_file, false, offset) | |
348 | { } | |
349 | ||
92e059d8 | 350 | // Read the relocs. |
a2fb1b05 | 351 | void |
92e059d8 ILT |
352 | read_relocs(Read_relocs_data* rd) |
353 | { return this->do_read_relocs(rd); } | |
354 | ||
355 | // Scan the relocs and adjust the symbol table. | |
356 | void | |
357 | scan_relocs(const General_options& options, Symbol_table* symtab, | |
ead1e424 ILT |
358 | Layout* layout, Read_relocs_data* rd) |
359 | { return this->do_scan_relocs(options, symtab, layout, rd); } | |
a2fb1b05 | 360 | |
75f65a3e ILT |
361 | // Initial local symbol processing: set the offset where local |
362 | // symbol information will be stored; add local symbol names to | |
c06b7b0b ILT |
363 | // *POOL; return the new local symbol index. |
364 | unsigned int | |
365 | finalize_local_symbols(unsigned int index, off_t off, Stringpool* pool) | |
366 | { return this->do_finalize_local_symbols(index, off, pool); } | |
75f65a3e | 367 | |
61ba1cf9 ILT |
368 | // Relocate the input sections and write out the local symbols. |
369 | void | |
370 | relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
371 | const Layout* layout, Output_file* of) |
372 | { return this->do_relocate(options, symtab, layout, of); } | |
61ba1cf9 | 373 | |
a783673b ILT |
374 | // Return whether an input section is being included in the link. |
375 | bool | |
5a6f7e2d | 376 | is_section_included(unsigned int shndx) const |
a783673b | 377 | { |
5a6f7e2d ILT |
378 | gold_assert(shndx < this->map_to_output_.size()); |
379 | return this->map_to_output_[shndx].output_section != NULL; | |
a783673b ILT |
380 | } |
381 | ||
382 | // Given a section index, return the corresponding Output_section | |
383 | // (which will be NULL if the section is not included in the link) | |
384 | // and set *POFF to the offset within that section. | |
385 | inline Output_section* | |
5a6f7e2d | 386 | output_section(unsigned int shndx, off_t* poff); |
a783673b | 387 | |
ead1e424 ILT |
388 | // Set the offset of an input section within its output section. |
389 | void | |
390 | set_section_offset(unsigned int shndx, off_t off) | |
391 | { | |
a3ad94ed | 392 | gold_assert(shndx < this->map_to_output_.size()); |
ead1e424 ILT |
393 | this->map_to_output_[shndx].offset = off; |
394 | } | |
395 | ||
a783673b | 396 | protected: |
a2fb1b05 ILT |
397 | // What we need to know to map an input section to an output |
398 | // section. We keep an array of these, one for each input section, | |
399 | // indexed by the input section number. | |
400 | struct Map_to_output | |
401 | { | |
402 | // The output section. This is NULL if the input section is to be | |
403 | // discarded. | |
404 | Output_section* output_section; | |
405 | // The offset within the output section. | |
406 | off_t offset; | |
407 | }; | |
408 | ||
92e059d8 ILT |
409 | // Read the relocs--implemented by child class. |
410 | virtual void | |
411 | do_read_relocs(Read_relocs_data*) = 0; | |
412 | ||
413 | // Scan the relocs--implemented by child class. | |
414 | virtual void | |
ead1e424 ILT |
415 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
416 | Read_relocs_data*) = 0; | |
92e059d8 | 417 | |
75f65a3e | 418 | // Finalize local symbols--implemented by child class. |
c06b7b0b ILT |
419 | virtual unsigned int |
420 | do_finalize_local_symbols(unsigned int, off_t, Stringpool*) = 0; | |
75f65a3e | 421 | |
61ba1cf9 ILT |
422 | // Relocate the input sections and write out the local |
423 | // symbols--implemented by child class. | |
424 | virtual void | |
425 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
426 | const Layout*, Output_file* of) = 0; |
427 | ||
a2fb1b05 ILT |
428 | // Return the vector mapping input sections to output sections. |
429 | std::vector<Map_to_output>& | |
430 | map_to_output() | |
431 | { return this->map_to_output_; } | |
432 | ||
bae7f79e | 433 | private: |
a2fb1b05 ILT |
434 | // Mapping from input sections to output section. |
435 | std::vector<Map_to_output> map_to_output_; | |
bae7f79e ILT |
436 | }; |
437 | ||
a783673b ILT |
438 | // Implement Object::output_section inline for efficiency. |
439 | inline Output_section* | |
5a6f7e2d | 440 | Relobj::output_section(unsigned int shndx, off_t* poff) |
a783673b | 441 | { |
5a6f7e2d ILT |
442 | gold_assert(shndx < this->map_to_output_.size()); |
443 | const Map_to_output& mo(this->map_to_output_[shndx]); | |
a783673b ILT |
444 | *poff = mo.offset; |
445 | return mo.output_section; | |
446 | } | |
447 | ||
14bfc3f5 | 448 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
449 | |
450 | template<int size, bool big_endian> | |
f6ce93d6 | 451 | class Sized_relobj : public Relobj |
bae7f79e ILT |
452 | { |
453 | public: | |
c06b7b0b ILT |
454 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
455 | typedef std::vector<Address> Local_values; | |
456 | ||
f6ce93d6 | 457 | Sized_relobj(const std::string& name, Input_file* input_file, off_t offset, |
bae7f79e ILT |
458 | const typename elfcpp::Ehdr<size, big_endian>&); |
459 | ||
f6ce93d6 | 460 | ~Sized_relobj(); |
bae7f79e | 461 | |
a2fb1b05 | 462 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
463 | void |
464 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
465 | ||
c06b7b0b ILT |
466 | // Return the index of local symbol SYM in the ordinary symbol |
467 | // table. A value of -1U means that the symbol is not being output. | |
468 | unsigned int | |
469 | symtab_index(unsigned int sym) const | |
470 | { | |
a3ad94ed ILT |
471 | gold_assert(sym < this->local_indexes_.size()); |
472 | gold_assert(this->local_indexes_[sym] != 0); | |
c06b7b0b ILT |
473 | return this->local_indexes_[sym]; |
474 | } | |
475 | ||
a2fb1b05 | 476 | // Read the symbols. |
bae7f79e | 477 | void |
12e14209 | 478 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 479 | |
dbe717ef ILT |
480 | // Lay out the input sections. |
481 | void | |
482 | do_layout(const General_options&, Symbol_table*, Layout*, | |
483 | Read_symbols_data*); | |
484 | ||
12e14209 ILT |
485 | // Add the symbols to the symbol table. |
486 | void | |
487 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 488 | |
92e059d8 ILT |
489 | // Read the relocs. |
490 | void | |
491 | do_read_relocs(Read_relocs_data*); | |
492 | ||
493 | // Scan the relocs and adjust the symbol table. | |
494 | void | |
ead1e424 ILT |
495 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
496 | Read_relocs_data*); | |
92e059d8 | 497 | |
75f65a3e | 498 | // Finalize the local symbols. |
c06b7b0b ILT |
499 | unsigned int |
500 | do_finalize_local_symbols(unsigned int, off_t, Stringpool*); | |
75f65a3e | 501 | |
61ba1cf9 ILT |
502 | // Relocate the input sections and write out the local symbols. |
503 | void | |
504 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
505 | const Layout*, Output_file* of); |
506 | ||
507 | // Get the name of a section. | |
508 | std::string | |
645f8123 ILT |
509 | do_section_name(unsigned int shndx) |
510 | { return this->elf_file_.section_name(shndx); } | |
61ba1cf9 | 511 | |
645f8123 | 512 | // Return the location of the contents of a section. |
dbe717ef | 513 | Object::Location |
645f8123 ILT |
514 | do_section_contents(unsigned int shndx) |
515 | { return this->elf_file_.section_contents(shndx); } | |
f6ce93d6 | 516 | |
a3ad94ed ILT |
517 | // Return section flags. |
518 | uint64_t | |
519 | do_section_flags(unsigned int shndx) | |
520 | { return this->elf_file_.section_flags(shndx); } | |
521 | ||
a2fb1b05 | 522 | // Return the appropriate Sized_target structure. |
14bfc3f5 ILT |
523 | Sized_target<size, big_endian>* |
524 | sized_target() | |
274e99f9 | 525 | { |
593f47df ILT |
526 | return this->Object::sized_target |
527 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
528 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
274e99f9 | 529 | } |
bae7f79e ILT |
530 | |
531 | private: | |
a2fb1b05 | 532 | // For convenience. |
f6ce93d6 | 533 | typedef Sized_relobj<size, big_endian> This; |
a2fb1b05 ILT |
534 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; |
535 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
536 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
537 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
538 | ||
dbe717ef ILT |
539 | // Find the SHT_SYMTAB section, given the section headers. |
540 | void | |
541 | find_symtab(const unsigned char* pshdrs); | |
542 | ||
a2fb1b05 ILT |
543 | // Whether to include a section group in the link. |
544 | bool | |
545 | include_section_group(Layout*, unsigned int, | |
546 | const elfcpp::Shdr<size, big_endian>&, | |
547 | std::vector<bool>*); | |
548 | ||
549 | // Whether to include a linkonce section in the link. | |
550 | bool | |
551 | include_linkonce_section(Layout*, const char*, | |
552 | const elfcpp::Shdr<size, big_endian>&); | |
553 | ||
61ba1cf9 ILT |
554 | // Views and sizes when relocating. |
555 | struct View_size | |
556 | { | |
557 | unsigned char* view; | |
558 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
559 | off_t offset; | |
560 | off_t view_size; | |
561 | }; | |
562 | ||
563 | typedef std::vector<View_size> Views; | |
564 | ||
565 | // Write section data to the output file. Record the views and | |
566 | // sizes in VIEWS for use when relocating. | |
567 | void | |
568 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); | |
569 | ||
570 | // Relocate the sections in the output file. | |
571 | void | |
92e059d8 ILT |
572 | relocate_sections(const General_options& options, const Symbol_table*, |
573 | const Layout*, const unsigned char* pshdrs, Views*); | |
61ba1cf9 ILT |
574 | |
575 | // Write out the local symbols. | |
576 | void | |
577 | write_local_symbols(Output_file*, const Stringpool*); | |
578 | ||
645f8123 ILT |
579 | // General access to the ELF file. |
580 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
bae7f79e | 581 | // Index of SHT_SYMTAB section. |
645f8123 | 582 | unsigned int symtab_shndx_; |
61ba1cf9 ILT |
583 | // The number of local symbols. |
584 | unsigned int local_symbol_count_; | |
585 | // The number of local symbols which go into the output file. | |
586 | unsigned int output_local_symbol_count_; | |
14bfc3f5 ILT |
587 | // The entries in the symbol table for the external symbols. |
588 | Symbol** symbols_; | |
75f65a3e ILT |
589 | // File offset for local symbols. |
590 | off_t local_symbol_offset_; | |
61ba1cf9 | 591 | // Values of local symbols. |
c06b7b0b ILT |
592 | Local_values local_values_; |
593 | // Indexes of local symbols in the output file; -1U if not present. | |
594 | std::vector<unsigned int> local_indexes_; | |
bae7f79e ILT |
595 | }; |
596 | ||
54dc6425 | 597 | // A class to manage the list of all objects. |
a2fb1b05 | 598 | |
54dc6425 ILT |
599 | class Input_objects |
600 | { | |
601 | public: | |
602 | Input_objects() | |
008db82e | 603 | : relobj_list_(), dynobj_list_(), target_(NULL), sonames_() |
54dc6425 ILT |
604 | { } |
605 | ||
f6ce93d6 ILT |
606 | // The type of the list of input relocateable objects. |
607 | typedef std::vector<Relobj*> Relobj_list; | |
608 | typedef Relobj_list::const_iterator Relobj_iterator; | |
609 | ||
610 | // The type of the list of input dynamic objects. | |
611 | typedef std::vector<Dynobj*> Dynobj_list; | |
612 | typedef Dynobj_list::const_iterator Dynobj_iterator; | |
54dc6425 | 613 | |
008db82e ILT |
614 | // Add an object to the list. Return true if all is well, or false |
615 | // if this object should be ignored. | |
616 | bool | |
54dc6425 ILT |
617 | add_object(Object*); |
618 | ||
75f65a3e ILT |
619 | // Get the target we should use for the output file. |
620 | Target* | |
621 | target() const | |
622 | { return this->target_; } | |
623 | ||
f6ce93d6 ILT |
624 | // Iterate over all regular objects. |
625 | ||
626 | Relobj_iterator | |
627 | relobj_begin() const | |
628 | { return this->relobj_list_.begin(); } | |
629 | ||
630 | Relobj_iterator | |
631 | relobj_end() const | |
632 | { return this->relobj_list_.end(); } | |
633 | ||
634 | // Iterate over all dynamic objects. | |
635 | ||
636 | Dynobj_iterator | |
637 | dynobj_begin() const | |
638 | { return this->dynobj_list_.begin(); } | |
54dc6425 | 639 | |
f6ce93d6 ILT |
640 | Dynobj_iterator |
641 | dynobj_end() const | |
642 | { return this->dynobj_list_.end(); } | |
54dc6425 ILT |
643 | |
644 | // Return whether we have seen any dynamic objects. | |
645 | bool | |
646 | any_dynamic() const | |
f6ce93d6 | 647 | { return !this->dynobj_list_.empty(); } |
54dc6425 ILT |
648 | |
649 | private: | |
650 | Input_objects(const Input_objects&); | |
651 | Input_objects& operator=(const Input_objects&); | |
652 | ||
008db82e | 653 | // The list of ordinary objects included in the link. |
f6ce93d6 | 654 | Relobj_list relobj_list_; |
008db82e | 655 | // The list of dynamic objects included in the link. |
f6ce93d6 | 656 | Dynobj_list dynobj_list_; |
008db82e | 657 | // The target. |
75f65a3e | 658 | Target* target_; |
008db82e ILT |
659 | // SONAMEs that we have seen. |
660 | Unordered_set<std::string> sonames_; | |
54dc6425 | 661 | }; |
a2fb1b05 | 662 | |
92e059d8 ILT |
663 | // Some of the information we pass to the relocation routines. We |
664 | // group this together to avoid passing a dozen different arguments. | |
665 | ||
666 | template<int size, bool big_endian> | |
667 | struct Relocate_info | |
668 | { | |
669 | // Command line options. | |
670 | const General_options* options; | |
671 | // Symbol table. | |
672 | const Symbol_table* symtab; | |
673 | // Layout. | |
674 | const Layout* layout; | |
675 | // Object being relocated. | |
f6ce93d6 | 676 | Sized_relobj<size, big_endian>* object; |
92e059d8 ILT |
677 | // Number of local symbols. |
678 | unsigned int local_symbol_count; | |
679 | // Values of local symbols. | |
c06b7b0b | 680 | const typename Sized_relobj<size, big_endian>::Local_values* local_values; |
92e059d8 | 681 | // Global symbols. |
c06b7b0b | 682 | const Symbol* const * symbols; |
92e059d8 ILT |
683 | // Section index of relocation section. |
684 | unsigned int reloc_shndx; | |
685 | // Section index of section being relocated. | |
686 | unsigned int data_shndx; | |
687 | ||
688 | // Return a string showing the location of a relocation. This is | |
689 | // only used for error messages. | |
690 | std::string | |
691 | location(size_t relnum, off_t reloffset) const; | |
692 | }; | |
693 | ||
bae7f79e ILT |
694 | // Return an Object appropriate for the input file. P is BYTES long, |
695 | // and holds the ELF header. | |
696 | ||
61ba1cf9 ILT |
697 | extern Object* |
698 | make_elf_object(const std::string& name, Input_file*, | |
699 | off_t offset, const unsigned char* p, | |
700 | off_t bytes); | |
bae7f79e ILT |
701 | |
702 | } // end namespace gold | |
703 | ||
704 | #endif // !defined(GOLD_OBJECT_H) |