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; | |
730cdc88 ILT |
60 | // Offset of external symbols within symbol data. This structure |
61 | // sometimes contains only external symbols, in which case this will | |
62 | // be zero. Sometimes it contains all symbols. | |
63 | off_t external_symbols_offset; | |
bae7f79e ILT |
64 | // Symbol names. |
65 | File_view* symbol_names; | |
66 | // Size of symbol name data in bytes. | |
67 | off_t symbol_names_size; | |
dbe717ef ILT |
68 | |
69 | // Version information. This is only used on dynamic objects. | |
70 | // Version symbol data (from SHT_GNU_versym section). | |
71 | File_view* versym; | |
72 | off_t versym_size; | |
73 | // Version definition data (from SHT_GNU_verdef section). | |
74 | File_view* verdef; | |
75 | off_t verdef_size; | |
76 | unsigned int verdef_info; | |
77 | // Needed version data (from SHT_GNU_verneed section). | |
78 | File_view* verneed; | |
79 | off_t verneed_size; | |
80 | unsigned int verneed_info; | |
bae7f79e ILT |
81 | }; |
82 | ||
f7e2ee48 ILT |
83 | // Information used to print error messages. |
84 | ||
85 | struct Symbol_location_info | |
86 | { | |
87 | std::string source_file; | |
88 | std::string enclosing_symbol_name; | |
89 | int line_number; | |
90 | }; | |
91 | ||
92e059d8 ILT |
92 | // Data about a single relocation section. This is read in |
93 | // read_relocs and processed in scan_relocs. | |
94 | ||
95 | struct Section_relocs | |
96 | { | |
97 | // Index of reloc section. | |
98 | unsigned int reloc_shndx; | |
99 | // Index of section that relocs apply to. | |
100 | unsigned int data_shndx; | |
101 | // Contents of reloc section. | |
102 | File_view* contents; | |
103 | // Reloc section type. | |
104 | unsigned int sh_type; | |
105 | // Number of reloc entries. | |
106 | size_t reloc_count; | |
730cdc88 ILT |
107 | // Output section. |
108 | Output_section* output_section; | |
109 | // Whether this section has special handling for offsets. | |
110 | bool needs_special_offset_handling; | |
92e059d8 ILT |
111 | }; |
112 | ||
113 | // Relocations in an object file. This is read in read_relocs and | |
114 | // processed in scan_relocs. | |
115 | ||
116 | struct Read_relocs_data | |
117 | { | |
118 | typedef std::vector<Section_relocs> Relocs_list; | |
119 | // The relocations. | |
120 | Relocs_list relocs; | |
121 | // The local symbols. | |
122 | File_view* local_symbols; | |
123 | }; | |
124 | ||
f6ce93d6 ILT |
125 | // Object is an abstract base class which represents either a 32-bit |
126 | // or a 64-bit input object. This can be a regular object file | |
127 | // (ET_REL) or a shared object (ET_DYN). | |
bae7f79e ILT |
128 | |
129 | class Object | |
130 | { | |
131 | public: | |
132 | // NAME is the name of the object as we would report it to the user | |
133 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
134 | // used to read the file. OFFSET is the offset within the input | |
135 | // file--0 for a .o or .so file, something else for a .a file. | |
14bfc3f5 ILT |
136 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
137 | off_t offset = 0) | |
dbe717ef | 138 | : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U), |
f6ce93d6 | 139 | is_dynamic_(is_dynamic), target_(NULL) |
bae7f79e ILT |
140 | { } |
141 | ||
142 | virtual ~Object() | |
143 | { } | |
144 | ||
14bfc3f5 | 145 | // Return the name of the object as we would report it to the tuser. |
bae7f79e ILT |
146 | const std::string& |
147 | name() const | |
148 | { return this->name_; } | |
149 | ||
cec9d2f3 ILT |
150 | // Get the offset into the file. |
151 | off_t | |
152 | offset() const | |
153 | { return this->offset_; } | |
154 | ||
14bfc3f5 ILT |
155 | // Return whether this is a dynamic object. |
156 | bool | |
157 | is_dynamic() const | |
158 | { return this->is_dynamic_; } | |
159 | ||
14bfc3f5 ILT |
160 | // Return the target structure associated with this object. |
161 | Target* | |
a2fb1b05 | 162 | target() const |
14bfc3f5 ILT |
163 | { return this->target_; } |
164 | ||
a2fb1b05 ILT |
165 | // Lock the underlying file. |
166 | void | |
167 | lock() | |
168 | { this->input_file_->file().lock(); } | |
169 | ||
170 | // Unlock the underlying file. | |
171 | void | |
172 | unlock() | |
173 | { this->input_file_->file().unlock(); } | |
174 | ||
175 | // Return whether the underlying file is locked. | |
176 | bool | |
177 | is_locked() const | |
178 | { return this->input_file_->file().is_locked(); } | |
179 | ||
14bfc3f5 ILT |
180 | // Return the sized target structure associated with this object. |
181 | // This is like the target method but it returns a pointer of | |
182 | // appropriate checked type. | |
183 | template<int size, bool big_endian> | |
184 | Sized_target<size, big_endian>* | |
5482377d | 185 | sized_target(ACCEPT_SIZE_ENDIAN_ONLY); |
bae7f79e | 186 | |
5a6f7e2d ILT |
187 | // Get the number of sections. |
188 | unsigned int | |
189 | shnum() const | |
190 | { return this->shnum_; } | |
a2fb1b05 | 191 | |
f6ce93d6 | 192 | // Return a view of the contents of a section. Set *PLEN to the |
9eb9fa57 | 193 | // size. CACHE is a hint as in File_read::get_view. |
f6ce93d6 | 194 | const unsigned char* |
9eb9fa57 | 195 | section_contents(unsigned int shndx, off_t* plen, bool cache); |
f6ce93d6 ILT |
196 | |
197 | // Return the name of a section given a section index. This is only | |
198 | // used for error messages. | |
199 | std::string | |
a3ad94ed ILT |
200 | section_name(unsigned int shndx) |
201 | { return this->do_section_name(shndx); } | |
202 | ||
203 | // Return the section flags given a section index. | |
204 | uint64_t | |
205 | section_flags(unsigned int shndx) | |
206 | { return this->do_section_flags(shndx); } | |
f6ce93d6 | 207 | |
730cdc88 ILT |
208 | // Return the section type given a section index. |
209 | unsigned int | |
210 | section_type(unsigned int shndx) | |
211 | { return this->do_section_type(shndx); } | |
212 | ||
f7e2ee48 ILT |
213 | // Return the section link field given a section index. |
214 | unsigned int | |
215 | section_link(unsigned int shndx) | |
216 | { return this->do_section_link(shndx); } | |
217 | ||
5a6f7e2d ILT |
218 | // Read the symbol information. |
219 | void | |
220 | read_symbols(Read_symbols_data* sd) | |
221 | { return this->do_read_symbols(sd); } | |
222 | ||
223 | // Pass sections which should be included in the link to the Layout | |
224 | // object, and record where the sections go in the output file. | |
225 | void | |
7e1edb90 ILT |
226 | layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd) |
227 | { this->do_layout(symtab, layout, sd); } | |
5a6f7e2d ILT |
228 | |
229 | // Add symbol information to the global symbol table. | |
230 | void | |
231 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd) | |
232 | { this->do_add_symbols(symtab, sd); } | |
233 | ||
645f8123 ILT |
234 | // Functions and types for the elfcpp::Elf_file interface. This |
235 | // permit us to use Object as the File template parameter for | |
236 | // elfcpp::Elf_file. | |
237 | ||
238 | // The View class is returned by view. It must support a single | |
239 | // method, data(). This is trivial, because get_view does what we | |
240 | // need. | |
241 | class View | |
242 | { | |
243 | public: | |
244 | View(const unsigned char* p) | |
245 | : p_(p) | |
246 | { } | |
247 | ||
248 | const unsigned char* | |
249 | data() const | |
250 | { return this->p_; } | |
251 | ||
252 | private: | |
253 | const unsigned char* p_; | |
254 | }; | |
255 | ||
256 | // Return a View. | |
257 | View | |
258 | view(off_t file_offset, off_t data_size) | |
9eb9fa57 | 259 | { return View(this->get_view(file_offset, data_size, true)); } |
645f8123 ILT |
260 | |
261 | // Report an error. | |
262 | void | |
75f2446e | 263 | error(const char* format, ...) const ATTRIBUTE_PRINTF_2; |
645f8123 ILT |
264 | |
265 | // A location in the file. | |
266 | struct Location | |
267 | { | |
268 | off_t file_offset; | |
269 | off_t data_size; | |
270 | ||
271 | Location(off_t fo, off_t ds) | |
272 | : file_offset(fo), data_size(ds) | |
273 | { } | |
274 | }; | |
275 | ||
276 | // Get a View given a Location. | |
277 | View view(Location loc) | |
9eb9fa57 | 278 | { return View(this->get_view(loc.file_offset, loc.data_size, true)); } |
645f8123 | 279 | |
f6ce93d6 ILT |
280 | protected: |
281 | // Read the symbols--implemented by child class. | |
282 | virtual void | |
283 | do_read_symbols(Read_symbols_data*) = 0; | |
284 | ||
285 | // Lay out sections--implemented by child class. | |
286 | virtual void | |
7e1edb90 | 287 | do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0; |
f6ce93d6 ILT |
288 | |
289 | // Add symbol information to the global symbol table--implemented by | |
290 | // child class. | |
291 | virtual void | |
292 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; | |
293 | ||
645f8123 ILT |
294 | // Return the location of the contents of a section. Implemented by |
295 | // child class. | |
296 | virtual Location | |
a3ad94ed | 297 | do_section_contents(unsigned int shndx) = 0; |
f6ce93d6 ILT |
298 | |
299 | // Get the name of a section--implemented by child class. | |
300 | virtual std::string | |
a3ad94ed ILT |
301 | do_section_name(unsigned int shndx) = 0; |
302 | ||
303 | // Get section flags--implemented by child class. | |
304 | virtual uint64_t | |
305 | do_section_flags(unsigned int shndx) = 0; | |
f6ce93d6 | 306 | |
730cdc88 ILT |
307 | // Get section type--implemented by child class. |
308 | virtual unsigned int | |
309 | do_section_type(unsigned int shndx) = 0; | |
310 | ||
f7e2ee48 ILT |
311 | // Get section link field--implemented by child class. |
312 | virtual unsigned int | |
313 | do_section_link(unsigned int shndx) = 0; | |
314 | ||
f6ce93d6 ILT |
315 | // Get the file. |
316 | Input_file* | |
317 | input_file() const | |
318 | { return this->input_file_; } | |
319 | ||
f6ce93d6 ILT |
320 | // Get a view into the underlying file. |
321 | const unsigned char* | |
9eb9fa57 ILT |
322 | get_view(off_t start, off_t size, bool cache) |
323 | { | |
324 | return this->input_file_->file().get_view(start + this->offset_, size, | |
325 | cache); | |
326 | } | |
f6ce93d6 ILT |
327 | |
328 | // Get a lasting view into the underlying file. | |
329 | File_view* | |
9eb9fa57 | 330 | get_lasting_view(off_t start, off_t size, bool cache) |
f6ce93d6 ILT |
331 | { |
332 | return this->input_file_->file().get_lasting_view(start + this->offset_, | |
9eb9fa57 | 333 | size, cache); |
f6ce93d6 ILT |
334 | } |
335 | ||
336 | // Read data from the underlying file. | |
337 | void | |
338 | read(off_t start, off_t size, void* p) | |
339 | { this->input_file_->file().read(start + this->offset_, size, p); } | |
340 | ||
341 | // Set the target. | |
342 | void | |
dbe717ef ILT |
343 | set_target(int machine, int size, bool big_endian, int osabi, |
344 | int abiversion); | |
345 | ||
dbe717ef ILT |
346 | // Set the number of sections. |
347 | void | |
348 | set_shnum(int shnum) | |
349 | { this->shnum_ = shnum; } | |
350 | ||
351 | // Functions used by both Sized_relobj and Sized_dynobj. | |
352 | ||
353 | // Read the section data into a Read_symbols_data object. | |
354 | template<int size, bool big_endian> | |
355 | void | |
356 | read_section_data(elfcpp::Elf_file<size, big_endian, Object>*, | |
357 | Read_symbols_data*); | |
358 | ||
359 | // If NAME is the name of a special .gnu.warning section, arrange | |
360 | // for the warning to be issued. SHNDX is the section index. | |
361 | // Return whether it is a warning section. | |
362 | bool | |
363 | handle_gnu_warning_section(const char* name, unsigned int shndx, | |
364 | Symbol_table*); | |
f6ce93d6 ILT |
365 | |
366 | private: | |
367 | // This class may not be copied. | |
368 | Object(const Object&); | |
369 | Object& operator=(const Object&); | |
370 | ||
371 | // Name of object as printed to user. | |
372 | std::string name_; | |
373 | // For reading the file. | |
374 | Input_file* input_file_; | |
375 | // Offset within the file--0 for an object file, non-0 for an | |
376 | // archive. | |
377 | off_t offset_; | |
dbe717ef ILT |
378 | // Number of input sections. |
379 | unsigned int shnum_; | |
f6ce93d6 ILT |
380 | // Whether this is a dynamic object. |
381 | bool is_dynamic_; | |
382 | // Target functions--may be NULL if the target is not known. | |
383 | Target* target_; | |
384 | }; | |
385 | ||
386 | // Implement sized_target inline for efficiency. This approach breaks | |
387 | // static type checking, but is made safe using asserts. | |
388 | ||
389 | template<int size, bool big_endian> | |
390 | inline Sized_target<size, big_endian>* | |
391 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) | |
392 | { | |
a3ad94ed ILT |
393 | gold_assert(this->target_->get_size() == size); |
394 | gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
f6ce93d6 ILT |
395 | return static_cast<Sized_target<size, big_endian>*>(this->target_); |
396 | } | |
397 | ||
398 | // A regular object (ET_REL). This is an abstract base class itself. | |
b8e6aad9 | 399 | // The implementation is the template class Sized_relobj. |
f6ce93d6 ILT |
400 | |
401 | class Relobj : public Object | |
402 | { | |
403 | public: | |
404 | Relobj(const std::string& name, Input_file* input_file, off_t offset = 0) | |
405 | : Object(name, input_file, false, offset) | |
406 | { } | |
407 | ||
92e059d8 | 408 | // Read the relocs. |
a2fb1b05 | 409 | void |
92e059d8 ILT |
410 | read_relocs(Read_relocs_data* rd) |
411 | { return this->do_read_relocs(rd); } | |
412 | ||
413 | // Scan the relocs and adjust the symbol table. | |
414 | void | |
415 | scan_relocs(const General_options& options, Symbol_table* symtab, | |
ead1e424 ILT |
416 | Layout* layout, Read_relocs_data* rd) |
417 | { return this->do_scan_relocs(options, symtab, layout, rd); } | |
a2fb1b05 | 418 | |
75f65a3e ILT |
419 | // Initial local symbol processing: set the offset where local |
420 | // symbol information will be stored; add local symbol names to | |
c06b7b0b ILT |
421 | // *POOL; return the new local symbol index. |
422 | unsigned int | |
b8e6aad9 ILT |
423 | finalize_local_symbols(unsigned int index, off_t off, |
424 | Stringpool_template<char>* pool) | |
c06b7b0b | 425 | { return this->do_finalize_local_symbols(index, off, pool); } |
75f65a3e | 426 | |
61ba1cf9 ILT |
427 | // Relocate the input sections and write out the local symbols. |
428 | void | |
429 | relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
430 | const Layout* layout, Output_file* of) |
431 | { return this->do_relocate(options, symtab, layout, of); } | |
61ba1cf9 | 432 | |
a783673b ILT |
433 | // Return whether an input section is being included in the link. |
434 | bool | |
5a6f7e2d | 435 | is_section_included(unsigned int shndx) const |
a783673b | 436 | { |
5a6f7e2d ILT |
437 | gold_assert(shndx < this->map_to_output_.size()); |
438 | return this->map_to_output_[shndx].output_section != NULL; | |
a783673b ILT |
439 | } |
440 | ||
730cdc88 ILT |
441 | // Return whether an input section requires special |
442 | // handling--whether it is not simply mapped from the input file to | |
443 | // the output file. | |
444 | bool | |
445 | is_section_specially_mapped(unsigned int shndx) const | |
446 | { | |
447 | gold_assert(shndx < this->map_to_output_.size()); | |
448 | return (this->map_to_output_[shndx].output_section != NULL | |
449 | && this->map_to_output_[shndx].offset == -1); | |
450 | } | |
451 | ||
a783673b ILT |
452 | // Given a section index, return the corresponding Output_section |
453 | // (which will be NULL if the section is not included in the link) | |
730cdc88 ILT |
454 | // and set *POFF to the offset within that section. *POFF will be |
455 | // set to -1 if the section requires special handling. | |
a783673b | 456 | inline Output_section* |
b8e6aad9 | 457 | output_section(unsigned int shndx, off_t* poff) const; |
a783673b | 458 | |
ead1e424 ILT |
459 | // Set the offset of an input section within its output section. |
460 | void | |
461 | set_section_offset(unsigned int shndx, off_t off) | |
462 | { | |
a3ad94ed | 463 | gold_assert(shndx < this->map_to_output_.size()); |
ead1e424 ILT |
464 | this->map_to_output_[shndx].offset = off; |
465 | } | |
466 | ||
730cdc88 ILT |
467 | // Return true if we need to wait for output sections to be written |
468 | // before we can apply relocations. This is true if the object has | |
469 | // any relocations for sections which require special handling, such | |
470 | // as the exception frame section. | |
471 | bool | |
472 | relocs_must_follow_section_writes() | |
473 | { return this->relocs_must_follow_section_writes_; } | |
474 | ||
a783673b | 475 | protected: |
a2fb1b05 ILT |
476 | // What we need to know to map an input section to an output |
477 | // section. We keep an array of these, one for each input section, | |
478 | // indexed by the input section number. | |
479 | struct Map_to_output | |
480 | { | |
481 | // The output section. This is NULL if the input section is to be | |
482 | // discarded. | |
483 | Output_section* output_section; | |
b8e6aad9 ILT |
484 | // The offset within the output section. This is -1 if the |
485 | // section requires special handling. | |
a2fb1b05 ILT |
486 | off_t offset; |
487 | }; | |
488 | ||
92e059d8 ILT |
489 | // Read the relocs--implemented by child class. |
490 | virtual void | |
491 | do_read_relocs(Read_relocs_data*) = 0; | |
492 | ||
493 | // Scan the relocs--implemented by child class. | |
494 | virtual void | |
ead1e424 ILT |
495 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
496 | Read_relocs_data*) = 0; | |
92e059d8 | 497 | |
75f65a3e | 498 | // Finalize local symbols--implemented by child class. |
c06b7b0b | 499 | virtual unsigned int |
b8e6aad9 ILT |
500 | do_finalize_local_symbols(unsigned int, off_t, |
501 | Stringpool_template<char>*) = 0; | |
75f65a3e | 502 | |
61ba1cf9 ILT |
503 | // Relocate the input sections and write out the local |
504 | // symbols--implemented by child class. | |
505 | virtual void | |
506 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
507 | const Layout*, Output_file* of) = 0; |
508 | ||
a2fb1b05 ILT |
509 | // Return the vector mapping input sections to output sections. |
510 | std::vector<Map_to_output>& | |
511 | map_to_output() | |
512 | { return this->map_to_output_; } | |
513 | ||
b8e6aad9 ILT |
514 | const std::vector<Map_to_output>& |
515 | map_to_output() const | |
516 | { return this->map_to_output_; } | |
517 | ||
730cdc88 ILT |
518 | // Record that we must wait for the output sections to be written |
519 | // before applying relocations. | |
520 | void | |
521 | set_relocs_must_follow_section_writes() | |
522 | { this->relocs_must_follow_section_writes_ = true; } | |
523 | ||
bae7f79e | 524 | private: |
a2fb1b05 ILT |
525 | // Mapping from input sections to output section. |
526 | std::vector<Map_to_output> map_to_output_; | |
730cdc88 ILT |
527 | // Whether we need to wait for output sections to be written before |
528 | // we can apply relocations. | |
529 | bool relocs_must_follow_section_writes_; | |
bae7f79e ILT |
530 | }; |
531 | ||
a783673b ILT |
532 | // Implement Object::output_section inline for efficiency. |
533 | inline Output_section* | |
b8e6aad9 | 534 | Relobj::output_section(unsigned int shndx, off_t* poff) const |
a783673b | 535 | { |
5a6f7e2d ILT |
536 | gold_assert(shndx < this->map_to_output_.size()); |
537 | const Map_to_output& mo(this->map_to_output_[shndx]); | |
a783673b ILT |
538 | *poff = mo.offset; |
539 | return mo.output_section; | |
540 | } | |
541 | ||
b8e6aad9 ILT |
542 | // This POD class is holds the value of a symbol. This is used for |
543 | // local symbols, and for all symbols during relocation processing. | |
730cdc88 ILT |
544 | // For special sections, such as SHF_MERGE sections, this calls a |
545 | // function to get the final symbol value. | |
b8e6aad9 ILT |
546 | |
547 | template<int size> | |
548 | class Symbol_value | |
549 | { | |
550 | public: | |
551 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; | |
552 | ||
553 | Symbol_value() | |
063f12a8 ILT |
554 | : output_symtab_index_(0), input_shndx_(0), is_section_symbol_(false), |
555 | needs_output_address_(false), value_(0) | |
b8e6aad9 ILT |
556 | { } |
557 | ||
558 | // Get the value of this symbol. OBJECT is the object in which this | |
559 | // symbol is defined, and ADDEND is an addend to add to the value. | |
560 | template<bool big_endian> | |
561 | Value | |
562 | value(const Sized_relobj<size, big_endian>* object, Value addend) const | |
563 | { | |
564 | if (!this->needs_output_address_) | |
565 | return this->value_ + addend; | |
063f12a8 ILT |
566 | return object->local_value(this->input_shndx_, this->value_, |
567 | this->is_section_symbol_, addend); | |
b8e6aad9 ILT |
568 | } |
569 | ||
570 | // Set the value of this symbol in the output symbol table. | |
571 | void | |
572 | set_output_value(Value value) | |
573 | { | |
574 | this->value_ = value; | |
575 | this->needs_output_address_ = false; | |
576 | } | |
577 | ||
578 | // If this symbol is mapped to an output section which requires | |
579 | // special handling to determine the output value, we store the | |
580 | // value of the symbol in the input file. This is used for | |
581 | // SHF_MERGE sections. | |
582 | void | |
583 | set_input_value(Value value) | |
584 | { | |
585 | this->value_ = value; | |
586 | this->needs_output_address_ = true; | |
587 | } | |
588 | ||
589 | // Return whether this symbol should go into the output symbol | |
590 | // table. | |
591 | bool | |
592 | needs_output_symtab_entry() const | |
593 | { | |
594 | gold_assert(this->output_symtab_index_ != 0); | |
595 | return this->output_symtab_index_ != -1U; | |
596 | } | |
597 | ||
598 | // Return the index in the output symbol table. | |
599 | unsigned int | |
600 | output_symtab_index() const | |
601 | { | |
602 | gold_assert(this->output_symtab_index_ != 0); | |
603 | return this->output_symtab_index_; | |
604 | } | |
605 | ||
606 | // Set the index in the output symbol table. | |
607 | void | |
608 | set_output_symtab_index(unsigned int i) | |
609 | { | |
610 | gold_assert(this->output_symtab_index_ == 0); | |
611 | this->output_symtab_index_ = i; | |
612 | } | |
613 | ||
614 | // Record that this symbol should not go into the output symbol | |
615 | // table. | |
616 | void | |
617 | set_no_output_symtab_entry() | |
618 | { | |
619 | gold_assert(this->output_symtab_index_ == 0); | |
620 | this->output_symtab_index_ = -1U; | |
621 | } | |
622 | ||
623 | // Set the index of the input section in the input file. | |
624 | void | |
625 | set_input_shndx(unsigned int i) | |
730cdc88 ILT |
626 | { |
627 | this->input_shndx_ = i; | |
628 | gold_assert(this->input_shndx_ == i); | |
629 | } | |
b8e6aad9 | 630 | |
063f12a8 ILT |
631 | // Record that this is a section symbol. |
632 | void | |
633 | set_is_section_symbol() | |
634 | { this->is_section_symbol_ = true; } | |
635 | ||
b8e6aad9 ILT |
636 | private: |
637 | // The index of this local symbol in the output symbol table. This | |
638 | // will be -1 if the symbol should not go into the symbol table. | |
639 | unsigned int output_symtab_index_; | |
640 | // The section index in the input file in which this symbol is | |
641 | // defined. | |
063f12a8 ILT |
642 | unsigned int input_shndx_ : 30; |
643 | // Whether this is a STT_SECTION symbol. | |
644 | bool is_section_symbol_ : 1; | |
b8e6aad9 ILT |
645 | // Whether getting the value of this symbol requires calling an |
646 | // Output_section method. For example, this will be true of a | |
063f12a8 | 647 | // symbol in a SHF_MERGE section. |
b8e6aad9 ILT |
648 | bool needs_output_address_ : 1; |
649 | // The value of the symbol. If !needs_output_address_, this is the | |
650 | // value in the output file. If needs_output_address_, this is the | |
651 | // value in the input file. | |
652 | Value value_; | |
653 | }; | |
654 | ||
14bfc3f5 | 655 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
656 | |
657 | template<int size, bool big_endian> | |
f6ce93d6 | 658 | class Sized_relobj : public Relobj |
bae7f79e ILT |
659 | { |
660 | public: | |
c06b7b0b | 661 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
730cdc88 | 662 | typedef std::vector<Symbol*> Symbols; |
b8e6aad9 | 663 | typedef std::vector<Symbol_value<size> > Local_values; |
c06b7b0b | 664 | |
f6ce93d6 | 665 | Sized_relobj(const std::string& name, Input_file* input_file, off_t offset, |
bae7f79e ILT |
666 | const typename elfcpp::Ehdr<size, big_endian>&); |
667 | ||
f6ce93d6 | 668 | ~Sized_relobj(); |
bae7f79e | 669 | |
a2fb1b05 | 670 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
671 | void |
672 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
673 | ||
730cdc88 ILT |
674 | // Return the number of local symbols. |
675 | unsigned int | |
676 | local_symbol_count() const | |
677 | { return this->local_symbol_count_; } | |
678 | ||
679 | // If SYM is the index of a global symbol in the object file's | |
680 | // symbol table, return the Symbol object. Otherwise, return NULL. | |
681 | Symbol* | |
682 | global_symbol(unsigned int sym) const | |
683 | { | |
684 | if (sym >= this->local_symbol_count_) | |
685 | { | |
686 | gold_assert(sym - this->local_symbol_count_ < this->symbols_.size()); | |
687 | return this->symbols_[sym - this->local_symbol_count_]; | |
688 | } | |
689 | return NULL; | |
690 | } | |
691 | ||
692 | // Return the section index of symbol SYM. Set *VALUE to its value | |
693 | // in the object file. Note that for a symbol which is not defined | |
694 | // in this object file, this will set *VALUE to 0 and return | |
695 | // SHN_UNDEF; it will not return the final value of the symbol in | |
696 | // the link. | |
697 | unsigned int | |
698 | symbol_section_and_value(unsigned int sym, Address* value); | |
699 | ||
700 | // Return a pointer to the Symbol_value structure which holds the | |
701 | // value of a local symbol. | |
702 | const Symbol_value<size>* | |
703 | local_symbol(unsigned int sym) const | |
704 | { | |
705 | gold_assert(sym < this->local_values_.size()); | |
706 | return &this->local_values_[sym]; | |
707 | } | |
708 | ||
c06b7b0b ILT |
709 | // Return the index of local symbol SYM in the ordinary symbol |
710 | // table. A value of -1U means that the symbol is not being output. | |
711 | unsigned int | |
712 | symtab_index(unsigned int sym) const | |
713 | { | |
b8e6aad9 ILT |
714 | gold_assert(sym < this->local_values_.size()); |
715 | return this->local_values_[sym].output_symtab_index(); | |
c06b7b0b ILT |
716 | } |
717 | ||
e727fa71 ILT |
718 | // Return the appropriate Sized_target structure. |
719 | Sized_target<size, big_endian>* | |
720 | sized_target() | |
721 | { | |
722 | return this->Object::sized_target | |
723 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
724 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
725 | } | |
726 | ||
727 | // Return the value of the local symbol symndx. | |
728 | Address | |
729 | local_symbol_value(unsigned int symndx) const; | |
730 | ||
731 | // Return the value of a local symbol defined in input section | |
732 | // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL | |
733 | // indicates whether the symbol is a section symbol. This handles | |
734 | // SHF_MERGE sections. | |
735 | Address | |
736 | local_value(unsigned int shndx, Address value, bool is_section_symbol, | |
737 | Address addend) const; | |
738 | ||
739 | // Return whether the local symbol SYMNDX has a GOT offset. | |
740 | bool | |
741 | local_has_got_offset(unsigned int symndx) const | |
742 | { | |
743 | return (this->local_got_offsets_.find(symndx) | |
744 | != this->local_got_offsets_.end()); | |
745 | } | |
746 | ||
747 | // Return the GOT offset of the local symbol SYMNDX. | |
748 | unsigned int | |
749 | local_got_offset(unsigned int symndx) const | |
750 | { | |
751 | Local_got_offsets::const_iterator p = | |
752 | this->local_got_offsets_.find(symndx); | |
753 | gold_assert(p != this->local_got_offsets_.end()); | |
754 | return p->second; | |
755 | } | |
756 | ||
757 | // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET. | |
758 | void | |
759 | set_local_got_offset(unsigned int symndx, unsigned int got_offset) | |
760 | { | |
761 | std::pair<Local_got_offsets::iterator, bool> ins = | |
762 | this->local_got_offsets_.insert(std::make_pair(symndx, got_offset)); | |
763 | gold_assert(ins.second); | |
764 | } | |
765 | ||
f7e2ee48 ILT |
766 | // Return the name of the symbol that spans the given offset in the |
767 | // specified section in this object. This is used only for error | |
768 | // messages and is not particularly efficient. | |
769 | bool | |
770 | get_symbol_location_info(unsigned int shndx, off_t offset, | |
771 | Symbol_location_info* info); | |
772 | ||
a2fb1b05 | 773 | // Read the symbols. |
bae7f79e | 774 | void |
12e14209 | 775 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 776 | |
dbe717ef ILT |
777 | // Lay out the input sections. |
778 | void | |
7e1edb90 | 779 | do_layout(Symbol_table*, Layout*, Read_symbols_data*); |
dbe717ef | 780 | |
12e14209 ILT |
781 | // Add the symbols to the symbol table. |
782 | void | |
783 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 784 | |
92e059d8 ILT |
785 | // Read the relocs. |
786 | void | |
787 | do_read_relocs(Read_relocs_data*); | |
788 | ||
789 | // Scan the relocs and adjust the symbol table. | |
790 | void | |
ead1e424 ILT |
791 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
792 | Read_relocs_data*); | |
92e059d8 | 793 | |
75f65a3e | 794 | // Finalize the local symbols. |
c06b7b0b | 795 | unsigned int |
b8e6aad9 ILT |
796 | do_finalize_local_symbols(unsigned int, off_t, |
797 | Stringpool_template<char>*); | |
75f65a3e | 798 | |
61ba1cf9 ILT |
799 | // Relocate the input sections and write out the local symbols. |
800 | void | |
801 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
802 | const Layout*, Output_file* of); |
803 | ||
804 | // Get the name of a section. | |
805 | std::string | |
645f8123 ILT |
806 | do_section_name(unsigned int shndx) |
807 | { return this->elf_file_.section_name(shndx); } | |
61ba1cf9 | 808 | |
645f8123 | 809 | // Return the location of the contents of a section. |
dbe717ef | 810 | Object::Location |
645f8123 ILT |
811 | do_section_contents(unsigned int shndx) |
812 | { return this->elf_file_.section_contents(shndx); } | |
f6ce93d6 | 813 | |
a3ad94ed ILT |
814 | // Return section flags. |
815 | uint64_t | |
816 | do_section_flags(unsigned int shndx) | |
817 | { return this->elf_file_.section_flags(shndx); } | |
818 | ||
730cdc88 ILT |
819 | // Return section type. |
820 | unsigned int | |
821 | do_section_type(unsigned int shndx) | |
822 | { return this->elf_file_.section_type(shndx); } | |
823 | ||
f7e2ee48 ILT |
824 | // Return the section link field. |
825 | unsigned int | |
826 | do_section_link(unsigned int shndx) | |
827 | { return this->elf_file_.section_link(shndx); } | |
828 | ||
bae7f79e | 829 | private: |
a2fb1b05 | 830 | // For convenience. |
f6ce93d6 | 831 | typedef Sized_relobj<size, big_endian> This; |
a2fb1b05 ILT |
832 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; |
833 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
834 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
835 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
836 | ||
dbe717ef ILT |
837 | // Find the SHT_SYMTAB section, given the section headers. |
838 | void | |
839 | find_symtab(const unsigned char* pshdrs); | |
840 | ||
730cdc88 ILT |
841 | // Return whether SHDR has the right flags for a GNU style exception |
842 | // frame section. | |
843 | bool | |
844 | check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const; | |
845 | ||
846 | // Return whether there is a section named .eh_frame which might be | |
847 | // a GNU style exception frame section. | |
848 | bool | |
849 | find_eh_frame(const unsigned char* pshdrs, const char* names, | |
850 | off_t names_size) const; | |
851 | ||
a2fb1b05 ILT |
852 | // Whether to include a section group in the link. |
853 | bool | |
854 | include_section_group(Layout*, unsigned int, | |
855 | const elfcpp::Shdr<size, big_endian>&, | |
856 | std::vector<bool>*); | |
857 | ||
858 | // Whether to include a linkonce section in the link. | |
859 | bool | |
860 | include_linkonce_section(Layout*, const char*, | |
861 | const elfcpp::Shdr<size, big_endian>&); | |
862 | ||
61ba1cf9 ILT |
863 | // Views and sizes when relocating. |
864 | struct View_size | |
865 | { | |
866 | unsigned char* view; | |
867 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
868 | off_t offset; | |
869 | off_t view_size; | |
730cdc88 | 870 | bool is_input_output_view; |
61ba1cf9 ILT |
871 | }; |
872 | ||
873 | typedef std::vector<View_size> Views; | |
874 | ||
875 | // Write section data to the output file. Record the views and | |
876 | // sizes in VIEWS for use when relocating. | |
877 | void | |
878 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); | |
879 | ||
880 | // Relocate the sections in the output file. | |
881 | void | |
92e059d8 ILT |
882 | relocate_sections(const General_options& options, const Symbol_table*, |
883 | const Layout*, const unsigned char* pshdrs, Views*); | |
61ba1cf9 ILT |
884 | |
885 | // Write out the local symbols. | |
886 | void | |
b8e6aad9 ILT |
887 | write_local_symbols(Output_file*, |
888 | const Stringpool_template<char>*); | |
61ba1cf9 | 889 | |
e727fa71 ILT |
890 | // The GOT offsets of local symbols. |
891 | typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets; | |
892 | ||
645f8123 ILT |
893 | // General access to the ELF file. |
894 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
bae7f79e | 895 | // Index of SHT_SYMTAB section. |
645f8123 | 896 | unsigned int symtab_shndx_; |
61ba1cf9 ILT |
897 | // The number of local symbols. |
898 | unsigned int local_symbol_count_; | |
899 | // The number of local symbols which go into the output file. | |
900 | unsigned int output_local_symbol_count_; | |
14bfc3f5 | 901 | // The entries in the symbol table for the external symbols. |
730cdc88 | 902 | Symbols symbols_; |
75f65a3e ILT |
903 | // File offset for local symbols. |
904 | off_t local_symbol_offset_; | |
61ba1cf9 | 905 | // Values of local symbols. |
c06b7b0b | 906 | Local_values local_values_; |
e727fa71 ILT |
907 | // GOT offsets for local symbols, indexed by symbol number. |
908 | Local_got_offsets local_got_offsets_; | |
730cdc88 ILT |
909 | // Whether this object has a GNU style .eh_frame section. |
910 | bool has_eh_frame_; | |
bae7f79e ILT |
911 | }; |
912 | ||
54dc6425 | 913 | // A class to manage the list of all objects. |
a2fb1b05 | 914 | |
54dc6425 ILT |
915 | class Input_objects |
916 | { | |
917 | public: | |
918 | Input_objects() | |
008db82e | 919 | : relobj_list_(), dynobj_list_(), target_(NULL), sonames_() |
54dc6425 ILT |
920 | { } |
921 | ||
f6ce93d6 ILT |
922 | // The type of the list of input relocateable objects. |
923 | typedef std::vector<Relobj*> Relobj_list; | |
924 | typedef Relobj_list::const_iterator Relobj_iterator; | |
925 | ||
926 | // The type of the list of input dynamic objects. | |
927 | typedef std::vector<Dynobj*> Dynobj_list; | |
928 | typedef Dynobj_list::const_iterator Dynobj_iterator; | |
54dc6425 | 929 | |
008db82e ILT |
930 | // Add an object to the list. Return true if all is well, or false |
931 | // if this object should be ignored. | |
932 | bool | |
54dc6425 ILT |
933 | add_object(Object*); |
934 | ||
75f65a3e ILT |
935 | // Get the target we should use for the output file. |
936 | Target* | |
937 | target() const | |
938 | { return this->target_; } | |
939 | ||
f6ce93d6 ILT |
940 | // Iterate over all regular objects. |
941 | ||
942 | Relobj_iterator | |
943 | relobj_begin() const | |
944 | { return this->relobj_list_.begin(); } | |
945 | ||
946 | Relobj_iterator | |
947 | relobj_end() const | |
948 | { return this->relobj_list_.end(); } | |
949 | ||
950 | // Iterate over all dynamic objects. | |
951 | ||
952 | Dynobj_iterator | |
953 | dynobj_begin() const | |
954 | { return this->dynobj_list_.begin(); } | |
54dc6425 | 955 | |
f6ce93d6 ILT |
956 | Dynobj_iterator |
957 | dynobj_end() const | |
958 | { return this->dynobj_list_.end(); } | |
54dc6425 ILT |
959 | |
960 | // Return whether we have seen any dynamic objects. | |
961 | bool | |
962 | any_dynamic() const | |
f6ce93d6 | 963 | { return !this->dynobj_list_.empty(); } |
54dc6425 | 964 | |
fe9a4c12 ILT |
965 | // Return the number of input objects. |
966 | int | |
967 | number_of_input_objects() const | |
968 | { return this->relobj_list_.size() + this->dynobj_list_.size(); } | |
969 | ||
54dc6425 ILT |
970 | private: |
971 | Input_objects(const Input_objects&); | |
972 | Input_objects& operator=(const Input_objects&); | |
973 | ||
008db82e | 974 | // The list of ordinary objects included in the link. |
f6ce93d6 | 975 | Relobj_list relobj_list_; |
008db82e | 976 | // The list of dynamic objects included in the link. |
f6ce93d6 | 977 | Dynobj_list dynobj_list_; |
008db82e | 978 | // The target. |
75f65a3e | 979 | Target* target_; |
008db82e ILT |
980 | // SONAMEs that we have seen. |
981 | Unordered_set<std::string> sonames_; | |
54dc6425 | 982 | }; |
a2fb1b05 | 983 | |
92e059d8 ILT |
984 | // Some of the information we pass to the relocation routines. We |
985 | // group this together to avoid passing a dozen different arguments. | |
986 | ||
987 | template<int size, bool big_endian> | |
988 | struct Relocate_info | |
989 | { | |
990 | // Command line options. | |
991 | const General_options* options; | |
992 | // Symbol table. | |
993 | const Symbol_table* symtab; | |
994 | // Layout. | |
995 | const Layout* layout; | |
996 | // Object being relocated. | |
f6ce93d6 | 997 | Sized_relobj<size, big_endian>* object; |
92e059d8 ILT |
998 | // Section index of relocation section. |
999 | unsigned int reloc_shndx; | |
1000 | // Section index of section being relocated. | |
1001 | unsigned int data_shndx; | |
1002 | ||
1003 | // Return a string showing the location of a relocation. This is | |
1004 | // only used for error messages. | |
1005 | std::string | |
1006 | location(size_t relnum, off_t reloffset) const; | |
1007 | }; | |
1008 | ||
bae7f79e ILT |
1009 | // Return an Object appropriate for the input file. P is BYTES long, |
1010 | // and holds the ELF header. | |
1011 | ||
61ba1cf9 ILT |
1012 | extern Object* |
1013 | make_elf_object(const std::string& name, Input_file*, | |
1014 | off_t offset, const unsigned char* p, | |
1015 | off_t bytes); | |
bae7f79e ILT |
1016 | |
1017 | } // end namespace gold | |
1018 | ||
1019 | #endif // !defined(GOLD_OBJECT_H) |