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