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