Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.h -- support for an object file for linking in gold -*- C++ -*- |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
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; |
17a1d0a9 | 38 | class Task; |
a2fb1b05 | 39 | class Layout; |
61ba1cf9 ILT |
40 | class Output_section; |
41 | class Output_file; | |
f6ce93d6 | 42 | class Dynobj; |
4625f782 | 43 | class Object_merge_map; |
6a74a719 | 44 | class Relocatable_relocs; |
a2fb1b05 | 45 | |
b8e6aad9 ILT |
46 | template<typename Stringpool_char> |
47 | class Stringpool_template; | |
48 | ||
bae7f79e ILT |
49 | // Data to pass from read_symbols() to add_symbols(). |
50 | ||
51 | struct Read_symbols_data | |
52 | { | |
12e14209 ILT |
53 | // Section headers. |
54 | File_view* section_headers; | |
55 | // Section names. | |
56 | File_view* section_names; | |
57 | // Size of section name data in bytes. | |
8383303e | 58 | section_size_type section_names_size; |
bae7f79e ILT |
59 | // Symbol data. |
60 | File_view* symbols; | |
61 | // Size of symbol data in bytes. | |
8383303e | 62 | section_size_type symbols_size; |
730cdc88 ILT |
63 | // Offset of external symbols within symbol data. This structure |
64 | // sometimes contains only external symbols, in which case this will | |
65 | // be zero. Sometimes it contains all symbols. | |
8383303e | 66 | section_offset_type external_symbols_offset; |
bae7f79e ILT |
67 | // Symbol names. |
68 | File_view* symbol_names; | |
69 | // Size of symbol name data in bytes. | |
8383303e | 70 | section_size_type symbol_names_size; |
dbe717ef ILT |
71 | |
72 | // Version information. This is only used on dynamic objects. | |
73 | // Version symbol data (from SHT_GNU_versym section). | |
74 | File_view* versym; | |
8383303e | 75 | section_size_type versym_size; |
dbe717ef ILT |
76 | // Version definition data (from SHT_GNU_verdef section). |
77 | File_view* verdef; | |
8383303e | 78 | section_size_type verdef_size; |
dbe717ef ILT |
79 | unsigned int verdef_info; |
80 | // Needed version data (from SHT_GNU_verneed section). | |
81 | File_view* verneed; | |
8383303e | 82 | section_size_type verneed_size; |
dbe717ef | 83 | unsigned int verneed_info; |
bae7f79e ILT |
84 | }; |
85 | ||
f7e2ee48 ILT |
86 | // Information used to print error messages. |
87 | ||
88 | struct Symbol_location_info | |
89 | { | |
90 | std::string source_file; | |
91 | std::string enclosing_symbol_name; | |
92 | int line_number; | |
93 | }; | |
94 | ||
92e059d8 ILT |
95 | // Data about a single relocation section. This is read in |
96 | // read_relocs and processed in scan_relocs. | |
97 | ||
98 | struct Section_relocs | |
99 | { | |
100 | // Index of reloc section. | |
101 | unsigned int reloc_shndx; | |
102 | // Index of section that relocs apply to. | |
103 | unsigned int data_shndx; | |
104 | // Contents of reloc section. | |
105 | File_view* contents; | |
106 | // Reloc section type. | |
107 | unsigned int sh_type; | |
108 | // Number of reloc entries. | |
109 | size_t reloc_count; | |
730cdc88 ILT |
110 | // Output section. |
111 | Output_section* output_section; | |
112 | // Whether this section has special handling for offsets. | |
113 | bool needs_special_offset_handling; | |
7019cd25 ILT |
114 | // Whether the data section is allocated (has the SHF_ALLOC flag set). |
115 | bool is_data_section_allocated; | |
92e059d8 ILT |
116 | }; |
117 | ||
118 | // Relocations in an object file. This is read in read_relocs and | |
119 | // processed in scan_relocs. | |
120 | ||
121 | struct Read_relocs_data | |
122 | { | |
123 | typedef std::vector<Section_relocs> Relocs_list; | |
124 | // The relocations. | |
125 | Relocs_list relocs; | |
126 | // The local symbols. | |
127 | File_view* local_symbols; | |
128 | }; | |
129 | ||
f6ce93d6 ILT |
130 | // Object is an abstract base class which represents either a 32-bit |
131 | // or a 64-bit input object. This can be a regular object file | |
132 | // (ET_REL) or a shared object (ET_DYN). | |
bae7f79e ILT |
133 | |
134 | class Object | |
135 | { | |
136 | public: | |
137 | // NAME is the name of the object as we would report it to the user | |
138 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
139 | // used to read the file. OFFSET is the offset within the input | |
140 | // file--0 for a .o or .so file, something else for a .a file. | |
14bfc3f5 ILT |
141 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
142 | off_t offset = 0) | |
dbe717ef | 143 | : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U), |
f6ce93d6 | 144 | is_dynamic_(is_dynamic), target_(NULL) |
cb295612 | 145 | { input_file->file().add_object(); } |
bae7f79e ILT |
146 | |
147 | virtual ~Object() | |
cb295612 | 148 | { this->input_file_->file().remove_object(); } |
bae7f79e | 149 | |
14bfc3f5 | 150 | // Return the name of the object as we would report it to the tuser. |
bae7f79e ILT |
151 | const std::string& |
152 | name() const | |
153 | { return this->name_; } | |
154 | ||
cec9d2f3 ILT |
155 | // Get the offset into the file. |
156 | off_t | |
157 | offset() const | |
158 | { return this->offset_; } | |
159 | ||
14bfc3f5 ILT |
160 | // Return whether this is a dynamic object. |
161 | bool | |
162 | is_dynamic() const | |
163 | { return this->is_dynamic_; } | |
164 | ||
14bfc3f5 ILT |
165 | // Return the target structure associated with this object. |
166 | Target* | |
a2fb1b05 | 167 | target() const |
14bfc3f5 ILT |
168 | { return this->target_; } |
169 | ||
a2fb1b05 ILT |
170 | // Lock the underlying file. |
171 | void | |
17a1d0a9 ILT |
172 | lock(const Task* t) |
173 | { this->input_file()->file().lock(t); } | |
a2fb1b05 ILT |
174 | |
175 | // Unlock the underlying file. | |
176 | void | |
17a1d0a9 ILT |
177 | unlock(const Task* t) |
178 | { this->input_file()->file().unlock(t); } | |
a2fb1b05 ILT |
179 | |
180 | // Return whether the underlying file is locked. | |
181 | bool | |
182 | is_locked() const | |
7004837e | 183 | { return this->input_file()->file().is_locked(); } |
a2fb1b05 | 184 | |
17a1d0a9 ILT |
185 | // Return the token, so that the task can be queued. |
186 | Task_token* | |
187 | token() | |
188 | { return this->input_file()->file().token(); } | |
189 | ||
190 | // Release the underlying file. | |
191 | void | |
192 | release() | |
193 | { this->input_file_->file().release(); } | |
194 | ||
88dd47ac ILT |
195 | // Return whether we should just read symbols from this file. |
196 | bool | |
197 | just_symbols() const | |
198 | { return this->input_file()->just_symbols(); } | |
199 | ||
14bfc3f5 ILT |
200 | // Return the sized target structure associated with this object. |
201 | // This is like the target method but it returns a pointer of | |
202 | // appropriate checked type. | |
203 | template<int size, bool big_endian> | |
204 | Sized_target<size, big_endian>* | |
7d1a9ebb | 205 | sized_target() const; |
bae7f79e | 206 | |
5a6f7e2d ILT |
207 | // Get the number of sections. |
208 | unsigned int | |
209 | shnum() const | |
210 | { return this->shnum_; } | |
a2fb1b05 | 211 | |
f6ce93d6 | 212 | // Return a view of the contents of a section. Set *PLEN to the |
9eb9fa57 | 213 | // size. CACHE is a hint as in File_read::get_view. |
f6ce93d6 | 214 | const unsigned char* |
8383303e | 215 | section_contents(unsigned int shndx, section_size_type* plen, bool cache); |
f6ce93d6 | 216 | |
a445fddf ILT |
217 | // Return the size of a section given a section index. |
218 | uint64_t | |
219 | section_size(unsigned int shndx) | |
220 | { return this->do_section_size(shndx); } | |
221 | ||
222 | // Return the name of a section given a section index. | |
f6ce93d6 | 223 | std::string |
a3ad94ed ILT |
224 | section_name(unsigned int shndx) |
225 | { return this->do_section_name(shndx); } | |
226 | ||
227 | // Return the section flags given a section index. | |
228 | uint64_t | |
229 | section_flags(unsigned int shndx) | |
230 | { return this->do_section_flags(shndx); } | |
f6ce93d6 | 231 | |
88dd47ac ILT |
232 | // Return the section address given a section index. |
233 | uint64_t | |
234 | section_address(unsigned int shndx) | |
235 | { return this->do_section_address(shndx); } | |
236 | ||
730cdc88 ILT |
237 | // Return the section type given a section index. |
238 | unsigned int | |
239 | section_type(unsigned int shndx) | |
240 | { return this->do_section_type(shndx); } | |
241 | ||
f7e2ee48 ILT |
242 | // Return the section link field given a section index. |
243 | unsigned int | |
244 | section_link(unsigned int shndx) | |
245 | { return this->do_section_link(shndx); } | |
246 | ||
4c50553d ILT |
247 | // Return the section info field given a section index. |
248 | unsigned int | |
249 | section_info(unsigned int shndx) | |
250 | { return this->do_section_info(shndx); } | |
251 | ||
a445fddf ILT |
252 | // Return the required section alignment given a section index. |
253 | uint64_t | |
254 | section_addralign(unsigned int shndx) | |
255 | { return this->do_section_addralign(shndx); } | |
256 | ||
5a6f7e2d ILT |
257 | // Read the symbol information. |
258 | void | |
259 | read_symbols(Read_symbols_data* sd) | |
260 | { return this->do_read_symbols(sd); } | |
261 | ||
262 | // Pass sections which should be included in the link to the Layout | |
263 | // object, and record where the sections go in the output file. | |
264 | void | |
7e1edb90 ILT |
265 | layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd) |
266 | { this->do_layout(symtab, layout, sd); } | |
5a6f7e2d ILT |
267 | |
268 | // Add symbol information to the global symbol table. | |
269 | void | |
270 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd) | |
271 | { this->do_add_symbols(symtab, sd); } | |
272 | ||
645f8123 ILT |
273 | // Functions and types for the elfcpp::Elf_file interface. This |
274 | // permit us to use Object as the File template parameter for | |
275 | // elfcpp::Elf_file. | |
276 | ||
277 | // The View class is returned by view. It must support a single | |
278 | // method, data(). This is trivial, because get_view does what we | |
279 | // need. | |
280 | class View | |
281 | { | |
282 | public: | |
283 | View(const unsigned char* p) | |
284 | : p_(p) | |
285 | { } | |
286 | ||
287 | const unsigned char* | |
288 | data() const | |
289 | { return this->p_; } | |
290 | ||
291 | private: | |
292 | const unsigned char* p_; | |
293 | }; | |
294 | ||
295 | // Return a View. | |
296 | View | |
8383303e | 297 | view(off_t file_offset, section_size_type data_size) |
39d0cb0e | 298 | { return View(this->get_view(file_offset, data_size, true, true)); } |
645f8123 ILT |
299 | |
300 | // Report an error. | |
301 | void | |
75f2446e | 302 | error(const char* format, ...) const ATTRIBUTE_PRINTF_2; |
645f8123 ILT |
303 | |
304 | // A location in the file. | |
305 | struct Location | |
306 | { | |
307 | off_t file_offset; | |
308 | off_t data_size; | |
309 | ||
8383303e | 310 | Location(off_t fo, section_size_type ds) |
645f8123 ILT |
311 | : file_offset(fo), data_size(ds) |
312 | { } | |
313 | }; | |
314 | ||
315 | // Get a View given a Location. | |
316 | View view(Location loc) | |
39d0cb0e | 317 | { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); } |
645f8123 | 318 | |
cb295612 ILT |
319 | // Get a view into the underlying file. |
320 | const unsigned char* | |
39d0cb0e | 321 | get_view(off_t start, section_size_type size, bool aligned, bool cache) |
cb295612 | 322 | { |
39d0cb0e ILT |
323 | return this->input_file()->file().get_view(this->offset_, start, size, |
324 | aligned, cache); | |
cb295612 ILT |
325 | } |
326 | ||
327 | // Get a lasting view into the underlying file. | |
328 | File_view* | |
39d0cb0e ILT |
329 | get_lasting_view(off_t start, section_size_type size, bool aligned, |
330 | bool cache) | |
cb295612 | 331 | { |
39d0cb0e ILT |
332 | return this->input_file()->file().get_lasting_view(this->offset_, start, |
333 | size, aligned, cache); | |
cb295612 ILT |
334 | } |
335 | ||
336 | // Read data from the underlying file. | |
337 | void | |
338 | read(off_t start, section_size_type size, void* p) const | |
339 | { this->input_file()->file().read(start + this->offset_, size, p); } | |
340 | ||
341 | // Read multiple data from the underlying file. | |
342 | void | |
343 | read_multiple(const File_read::Read_multiple& rm) | |
344 | { this->input_file()->file().read_multiple(this->offset_, rm); } | |
345 | ||
346 | // Stop caching views in the underlying file. | |
347 | void | |
348 | clear_view_cache_marks() | |
349 | { this->input_file()->file().clear_view_cache_marks(); } | |
350 | ||
f6ce93d6 ILT |
351 | protected: |
352 | // Read the symbols--implemented by child class. | |
353 | virtual void | |
354 | do_read_symbols(Read_symbols_data*) = 0; | |
355 | ||
356 | // Lay out sections--implemented by child class. | |
357 | virtual void | |
7e1edb90 | 358 | do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0; |
f6ce93d6 ILT |
359 | |
360 | // Add symbol information to the global symbol table--implemented by | |
361 | // child class. | |
362 | virtual void | |
363 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; | |
364 | ||
645f8123 ILT |
365 | // Return the location of the contents of a section. Implemented by |
366 | // child class. | |
367 | virtual Location | |
a3ad94ed | 368 | do_section_contents(unsigned int shndx) = 0; |
f6ce93d6 | 369 | |
a445fddf ILT |
370 | // Get the size of a section--implemented by child class. |
371 | virtual uint64_t | |
372 | do_section_size(unsigned int shndx) = 0; | |
373 | ||
f6ce93d6 ILT |
374 | // Get the name of a section--implemented by child class. |
375 | virtual std::string | |
a3ad94ed ILT |
376 | do_section_name(unsigned int shndx) = 0; |
377 | ||
378 | // Get section flags--implemented by child class. | |
379 | virtual uint64_t | |
380 | do_section_flags(unsigned int shndx) = 0; | |
f6ce93d6 | 381 | |
88dd47ac ILT |
382 | // Get section address--implemented by child class. |
383 | virtual uint64_t | |
384 | do_section_address(unsigned int shndx) = 0; | |
385 | ||
730cdc88 ILT |
386 | // Get section type--implemented by child class. |
387 | virtual unsigned int | |
388 | do_section_type(unsigned int shndx) = 0; | |
389 | ||
f7e2ee48 ILT |
390 | // Get section link field--implemented by child class. |
391 | virtual unsigned int | |
392 | do_section_link(unsigned int shndx) = 0; | |
393 | ||
4c50553d ILT |
394 | // Get section info field--implemented by child class. |
395 | virtual unsigned int | |
396 | do_section_info(unsigned int shndx) = 0; | |
397 | ||
a445fddf ILT |
398 | // Get section alignment--implemented by child class. |
399 | virtual uint64_t | |
400 | do_section_addralign(unsigned int shndx) = 0; | |
401 | ||
17a1d0a9 | 402 | // Get the file. We pass on const-ness. |
f6ce93d6 | 403 | Input_file* |
7004837e ILT |
404 | input_file() |
405 | { return this->input_file_; } | |
406 | ||
407 | const Input_file* | |
f6ce93d6 ILT |
408 | input_file() const |
409 | { return this->input_file_; } | |
410 | ||
f6ce93d6 ILT |
411 | // Set the target. |
412 | void | |
dbe717ef ILT |
413 | set_target(int machine, int size, bool big_endian, int osabi, |
414 | int abiversion); | |
415 | ||
dbe717ef ILT |
416 | // Set the number of sections. |
417 | void | |
418 | set_shnum(int shnum) | |
419 | { this->shnum_ = shnum; } | |
420 | ||
421 | // Functions used by both Sized_relobj and Sized_dynobj. | |
422 | ||
423 | // Read the section data into a Read_symbols_data object. | |
424 | template<int size, bool big_endian> | |
425 | void | |
426 | read_section_data(elfcpp::Elf_file<size, big_endian, Object>*, | |
427 | Read_symbols_data*); | |
428 | ||
429 | // If NAME is the name of a special .gnu.warning section, arrange | |
430 | // for the warning to be issued. SHNDX is the section index. | |
431 | // Return whether it is a warning section. | |
432 | bool | |
433 | handle_gnu_warning_section(const char* name, unsigned int shndx, | |
434 | Symbol_table*); | |
f6ce93d6 ILT |
435 | |
436 | private: | |
437 | // This class may not be copied. | |
438 | Object(const Object&); | |
439 | Object& operator=(const Object&); | |
440 | ||
441 | // Name of object as printed to user. | |
442 | std::string name_; | |
443 | // For reading the file. | |
444 | Input_file* input_file_; | |
445 | // Offset within the file--0 for an object file, non-0 for an | |
446 | // archive. | |
447 | off_t offset_; | |
dbe717ef ILT |
448 | // Number of input sections. |
449 | unsigned int shnum_; | |
f6ce93d6 ILT |
450 | // Whether this is a dynamic object. |
451 | bool is_dynamic_; | |
452 | // Target functions--may be NULL if the target is not known. | |
453 | Target* target_; | |
454 | }; | |
455 | ||
456 | // Implement sized_target inline for efficiency. This approach breaks | |
457 | // static type checking, but is made safe using asserts. | |
458 | ||
459 | template<int size, bool big_endian> | |
460 | inline Sized_target<size, big_endian>* | |
7d1a9ebb | 461 | Object::sized_target() const |
f6ce93d6 | 462 | { |
a3ad94ed ILT |
463 | gold_assert(this->target_->get_size() == size); |
464 | gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
f6ce93d6 ILT |
465 | return static_cast<Sized_target<size, big_endian>*>(this->target_); |
466 | } | |
467 | ||
468 | // A regular object (ET_REL). This is an abstract base class itself. | |
b8e6aad9 | 469 | // The implementation is the template class Sized_relobj. |
f6ce93d6 ILT |
470 | |
471 | class Relobj : public Object | |
472 | { | |
473 | public: | |
474 | Relobj(const std::string& name, Input_file* input_file, off_t offset = 0) | |
4625f782 ILT |
475 | : Object(name, input_file, false, offset), |
476 | map_to_output_(), | |
6a74a719 | 477 | map_to_relocatable_relocs_(NULL), |
4625f782 ILT |
478 | object_merge_map_(NULL), |
479 | relocs_must_follow_section_writes_(false) | |
f6ce93d6 ILT |
480 | { } |
481 | ||
92e059d8 | 482 | // Read the relocs. |
a2fb1b05 | 483 | void |
92e059d8 ILT |
484 | read_relocs(Read_relocs_data* rd) |
485 | { return this->do_read_relocs(rd); } | |
486 | ||
487 | // Scan the relocs and adjust the symbol table. | |
488 | void | |
489 | scan_relocs(const General_options& options, Symbol_table* symtab, | |
ead1e424 ILT |
490 | Layout* layout, Read_relocs_data* rd) |
491 | { return this->do_scan_relocs(options, symtab, layout, rd); } | |
a2fb1b05 | 492 | |
6d013333 ILT |
493 | // The number of local symbols in the input symbol table. |
494 | virtual unsigned int | |
495 | local_symbol_count() const | |
496 | { return this->do_local_symbol_count(); } | |
497 | ||
7bf1f802 ILT |
498 | // Initial local symbol processing: count the number of local symbols |
499 | // in the output symbol table and dynamic symbol table; add local symbol | |
500 | // names to *POOL and *DYNPOOL. | |
501 | void | |
502 | count_local_symbols(Stringpool_template<char>* pool, | |
503 | Stringpool_template<char>* dynpool) | |
504 | { return this->do_count_local_symbols(pool, dynpool); } | |
505 | ||
506 | // Set the values of the local symbols, set the output symbol table | |
507 | // indexes for the local variables, and set the offset where local | |
508 | // symbol information will be stored. Returns the new local symbol index. | |
509 | unsigned int | |
510 | finalize_local_symbols(unsigned int index, off_t off) | |
511 | { return this->do_finalize_local_symbols(index, off); } | |
512 | ||
513 | // Set the output dynamic symbol table indexes for the local variables. | |
c06b7b0b | 514 | unsigned int |
7bf1f802 ILT |
515 | set_local_dynsym_indexes(unsigned int index) |
516 | { return this->do_set_local_dynsym_indexes(index); } | |
517 | ||
518 | // Set the offset where local dynamic symbol information will be stored. | |
519 | unsigned int | |
520 | set_local_dynsym_offset(off_t off) | |
521 | { return this->do_set_local_dynsym_offset(off); } | |
75f65a3e | 522 | |
61ba1cf9 ILT |
523 | // Relocate the input sections and write out the local symbols. |
524 | void | |
525 | relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
526 | const Layout* layout, Output_file* of) |
527 | { return this->do_relocate(options, symtab, layout, of); } | |
61ba1cf9 | 528 | |
a783673b ILT |
529 | // Return whether an input section is being included in the link. |
530 | bool | |
5a6f7e2d | 531 | is_section_included(unsigned int shndx) const |
a783673b | 532 | { |
5a6f7e2d ILT |
533 | gold_assert(shndx < this->map_to_output_.size()); |
534 | return this->map_to_output_[shndx].output_section != NULL; | |
a783673b ILT |
535 | } |
536 | ||
730cdc88 ILT |
537 | // Return whether an input section requires special |
538 | // handling--whether it is not simply mapped from the input file to | |
539 | // the output file. | |
540 | bool | |
541 | is_section_specially_mapped(unsigned int shndx) const | |
542 | { | |
543 | gold_assert(shndx < this->map_to_output_.size()); | |
544 | return (this->map_to_output_[shndx].output_section != NULL | |
545 | && this->map_to_output_[shndx].offset == -1); | |
546 | } | |
547 | ||
a783673b ILT |
548 | // Given a section index, return the corresponding Output_section |
549 | // (which will be NULL if the section is not included in the link) | |
730cdc88 ILT |
550 | // and set *POFF to the offset within that section. *POFF will be |
551 | // set to -1 if the section requires special handling. | |
a783673b | 552 | inline Output_section* |
8383303e | 553 | output_section(unsigned int shndx, section_offset_type* poff) const; |
a783673b | 554 | |
ead1e424 ILT |
555 | // Set the offset of an input section within its output section. |
556 | void | |
8383303e | 557 | set_section_offset(unsigned int shndx, section_offset_type off) |
ead1e424 | 558 | { |
a3ad94ed | 559 | gold_assert(shndx < this->map_to_output_.size()); |
ead1e424 ILT |
560 | this->map_to_output_[shndx].offset = off; |
561 | } | |
562 | ||
730cdc88 ILT |
563 | // Return true if we need to wait for output sections to be written |
564 | // before we can apply relocations. This is true if the object has | |
565 | // any relocations for sections which require special handling, such | |
566 | // as the exception frame section. | |
567 | bool | |
17a1d0a9 | 568 | relocs_must_follow_section_writes() const |
730cdc88 ILT |
569 | { return this->relocs_must_follow_section_writes_; } |
570 | ||
4625f782 ILT |
571 | // Return the object merge map. |
572 | Object_merge_map* | |
573 | merge_map() const | |
574 | { return this->object_merge_map_; } | |
575 | ||
576 | // Set the object merge map. | |
577 | void | |
578 | set_merge_map(Object_merge_map* object_merge_map) | |
579 | { | |
580 | gold_assert(this->object_merge_map_ == NULL); | |
581 | this->object_merge_map_ = object_merge_map; | |
582 | } | |
583 | ||
6a74a719 ILT |
584 | // Record the relocatable reloc info for an input reloc section. |
585 | void | |
586 | set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr) | |
587 | { | |
588 | gold_assert(reloc_shndx < this->shnum()); | |
589 | (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr; | |
590 | } | |
591 | ||
592 | // Get the relocatable reloc info for an input reloc section. | |
593 | Relocatable_relocs* | |
594 | relocatable_relocs(unsigned int reloc_shndx) | |
595 | { | |
596 | gold_assert(reloc_shndx < this->shnum()); | |
597 | return (*this->map_to_relocatable_relocs_)[reloc_shndx]; | |
598 | } | |
599 | ||
a783673b | 600 | protected: |
a2fb1b05 ILT |
601 | // What we need to know to map an input section to an output |
602 | // section. We keep an array of these, one for each input section, | |
603 | // indexed by the input section number. | |
604 | struct Map_to_output | |
605 | { | |
606 | // The output section. This is NULL if the input section is to be | |
607 | // discarded. | |
608 | Output_section* output_section; | |
b8e6aad9 ILT |
609 | // The offset within the output section. This is -1 if the |
610 | // section requires special handling. | |
8383303e | 611 | section_offset_type offset; |
a2fb1b05 ILT |
612 | }; |
613 | ||
92e059d8 ILT |
614 | // Read the relocs--implemented by child class. |
615 | virtual void | |
616 | do_read_relocs(Read_relocs_data*) = 0; | |
617 | ||
618 | // Scan the relocs--implemented by child class. | |
619 | virtual void | |
ead1e424 ILT |
620 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
621 | Read_relocs_data*) = 0; | |
92e059d8 | 622 | |
6d013333 ILT |
623 | // Return the number of local symbols--implemented by child class. |
624 | virtual unsigned int | |
625 | do_local_symbol_count() const = 0; | |
626 | ||
7bf1f802 ILT |
627 | // Count local symbols--implemented by child class. |
628 | virtual void | |
629 | do_count_local_symbols(Stringpool_template<char>*, | |
6a74a719 | 630 | Stringpool_template<char>*) = 0; |
75f65a3e | 631 | |
6a74a719 ILT |
632 | // Finalize the local symbols. Set the output symbol table indexes |
633 | // for the local variables, and set the offset where local symbol | |
634 | // information will be stored. | |
7bf1f802 ILT |
635 | virtual unsigned int |
636 | do_finalize_local_symbols(unsigned int, off_t) = 0; | |
637 | ||
638 | // Set the output dynamic symbol table indexes for the local variables. | |
639 | virtual unsigned int | |
640 | do_set_local_dynsym_indexes(unsigned int) = 0; | |
641 | ||
642 | // Set the offset where local dynamic symbol information will be stored. | |
643 | virtual unsigned int | |
644 | do_set_local_dynsym_offset(off_t) = 0; | |
645 | ||
61ba1cf9 ILT |
646 | // Relocate the input sections and write out the local |
647 | // symbols--implemented by child class. | |
648 | virtual void | |
649 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
650 | const Layout*, Output_file* of) = 0; |
651 | ||
a2fb1b05 ILT |
652 | // Return the vector mapping input sections to output sections. |
653 | std::vector<Map_to_output>& | |
654 | map_to_output() | |
655 | { return this->map_to_output_; } | |
656 | ||
b8e6aad9 ILT |
657 | const std::vector<Map_to_output>& |
658 | map_to_output() const | |
659 | { return this->map_to_output_; } | |
660 | ||
6a74a719 ILT |
661 | // Set the size of the relocatable relocs array. |
662 | void | |
663 | size_relocatable_relocs() | |
664 | { | |
665 | this->map_to_relocatable_relocs_ = | |
666 | new std::vector<Relocatable_relocs*>(this->shnum()); | |
667 | } | |
668 | ||
730cdc88 ILT |
669 | // Record that we must wait for the output sections to be written |
670 | // before applying relocations. | |
671 | void | |
672 | set_relocs_must_follow_section_writes() | |
673 | { this->relocs_must_follow_section_writes_ = true; } | |
674 | ||
bae7f79e | 675 | private: |
a2fb1b05 ILT |
676 | // Mapping from input sections to output section. |
677 | std::vector<Map_to_output> map_to_output_; | |
6a74a719 ILT |
678 | // Mapping from input section index to the information recorded for |
679 | // the relocations. This is only used for a relocatable link. | |
680 | std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_; | |
4625f782 ILT |
681 | // Mappings for merge sections. This is managed by the code in the |
682 | // Merge_map class. | |
683 | Object_merge_map* object_merge_map_; | |
730cdc88 ILT |
684 | // Whether we need to wait for output sections to be written before |
685 | // we can apply relocations. | |
686 | bool relocs_must_follow_section_writes_; | |
bae7f79e ILT |
687 | }; |
688 | ||
a783673b ILT |
689 | // Implement Object::output_section inline for efficiency. |
690 | inline Output_section* | |
8383303e | 691 | Relobj::output_section(unsigned int shndx, section_offset_type* poff) const |
a783673b | 692 | { |
5a6f7e2d ILT |
693 | gold_assert(shndx < this->map_to_output_.size()); |
694 | const Map_to_output& mo(this->map_to_output_[shndx]); | |
a783673b ILT |
695 | *poff = mo.offset; |
696 | return mo.output_section; | |
697 | } | |
698 | ||
a9a60db6 ILT |
699 | // This class is used to handle relocations against a section symbol |
700 | // in an SHF_MERGE section. For such a symbol, we need to know the | |
701 | // addend of the relocation before we can determine the final value. | |
702 | // The addend gives us the location in the input section, and we can | |
703 | // determine how it is mapped to the output section. For a | |
704 | // non-section symbol, we apply the addend to the final value of the | |
705 | // symbol; that is done in finalize_local_symbols, and does not use | |
706 | // this class. | |
707 | ||
708 | template<int size> | |
709 | class Merged_symbol_value | |
710 | { | |
711 | public: | |
712 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; | |
713 | ||
714 | // We use a hash table to map offsets in the input section to output | |
715 | // addresses. | |
716 | typedef Unordered_map<section_offset_type, Value> Output_addresses; | |
717 | ||
718 | Merged_symbol_value(Value input_value, Value output_start_address) | |
719 | : input_value_(input_value), output_start_address_(output_start_address), | |
720 | output_addresses_() | |
721 | { } | |
722 | ||
723 | // Initialize the hash table. | |
724 | void | |
725 | initialize_input_to_output_map(const Relobj*, unsigned int input_shndx); | |
726 | ||
727 | // Release the hash table to save space. | |
728 | void | |
729 | free_input_to_output_map() | |
730 | { this->output_addresses_.clear(); } | |
731 | ||
732 | // Get the output value corresponding to an addend. The object and | |
733 | // input section index are passed in because the caller will have | |
734 | // them; otherwise we could store them here. | |
735 | Value | |
736 | value(const Relobj* object, unsigned int input_shndx, Value addend) const | |
737 | { | |
738 | Value input_offset = this->input_value_ + addend; | |
739 | typename Output_addresses::const_iterator p = | |
740 | this->output_addresses_.find(input_offset); | |
741 | if (p != this->output_addresses_.end()) | |
742 | return p->second; | |
743 | ||
744 | return this->value_from_output_section(object, input_shndx, input_offset); | |
745 | } | |
746 | ||
747 | private: | |
748 | // Get the output value for an input offset if we couldn't find it | |
749 | // in the hash table. | |
750 | Value | |
751 | value_from_output_section(const Relobj*, unsigned int input_shndx, | |
752 | Value input_offset) const; | |
753 | ||
754 | // The value of the section symbol in the input file. This is | |
755 | // normally zero, but could in principle be something else. | |
756 | Value input_value_; | |
757 | // The start address of this merged section in the output file. | |
758 | Value output_start_address_; | |
759 | // A hash table which maps offsets in the input section to output | |
760 | // addresses. This only maps specific offsets, not all offsets. | |
761 | Output_addresses output_addresses_; | |
762 | }; | |
763 | ||
b8e6aad9 ILT |
764 | // This POD class is holds the value of a symbol. This is used for |
765 | // local symbols, and for all symbols during relocation processing. | |
730cdc88 ILT |
766 | // For special sections, such as SHF_MERGE sections, this calls a |
767 | // function to get the final symbol value. | |
b8e6aad9 ILT |
768 | |
769 | template<int size> | |
770 | class Symbol_value | |
771 | { | |
772 | public: | |
773 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; | |
774 | ||
775 | Symbol_value() | |
7bf1f802 ILT |
776 | : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0), |
777 | is_section_symbol_(false), is_tls_symbol_(false), | |
a9a60db6 ILT |
778 | has_output_value_(true) |
779 | { this->u_.value = 0; } | |
b8e6aad9 ILT |
780 | |
781 | // Get the value of this symbol. OBJECT is the object in which this | |
782 | // symbol is defined, and ADDEND is an addend to add to the value. | |
783 | template<bool big_endian> | |
784 | Value | |
785 | value(const Sized_relobj<size, big_endian>* object, Value addend) const | |
786 | { | |
a9a60db6 ILT |
787 | if (this->has_output_value_) |
788 | return this->u_.value + addend; | |
789 | else | |
790 | return this->u_.merged_symbol_value->value(object, this->input_shndx_, | |
791 | addend); | |
b8e6aad9 ILT |
792 | } |
793 | ||
794 | // Set the value of this symbol in the output symbol table. | |
795 | void | |
796 | set_output_value(Value value) | |
a9a60db6 ILT |
797 | { this->u_.value = value; } |
798 | ||
799 | // For a section symbol in a merged section, we need more | |
800 | // information. | |
801 | void | |
802 | set_merged_symbol_value(Merged_symbol_value<size>* msv) | |
b8e6aad9 | 803 | { |
a9a60db6 ILT |
804 | gold_assert(this->is_section_symbol_); |
805 | this->has_output_value_ = false; | |
806 | this->u_.merged_symbol_value = msv; | |
b8e6aad9 ILT |
807 | } |
808 | ||
a9a60db6 ILT |
809 | // Initialize the input to output map for a section symbol in a |
810 | // merged section. We also initialize the value of a non-section | |
811 | // symbol in a merged section. | |
b8e6aad9 | 812 | void |
a9a60db6 | 813 | initialize_input_to_output_map(const Relobj* object) |
b8e6aad9 | 814 | { |
a9a60db6 ILT |
815 | if (!this->has_output_value_) |
816 | { | |
817 | gold_assert(this->is_section_symbol_); | |
818 | Merged_symbol_value<size>* msv = this->u_.merged_symbol_value; | |
819 | msv->initialize_input_to_output_map(object, this->input_shndx_); | |
820 | } | |
b8e6aad9 ILT |
821 | } |
822 | ||
a9a60db6 ILT |
823 | // Free the input to output map for a section symbol in a merged |
824 | // section. | |
825 | void | |
826 | free_input_to_output_map() | |
7bf1f802 | 827 | { |
a9a60db6 ILT |
828 | if (!this->has_output_value_) |
829 | this->u_.merged_symbol_value->free_input_to_output_map(); | |
7bf1f802 ILT |
830 | } |
831 | ||
a9a60db6 ILT |
832 | // Set the value of the symbol from the input file. This is only |
833 | // called by count_local_symbols, to communicate the value to | |
834 | // finalize_local_symbols. | |
835 | void | |
836 | set_input_value(Value value) | |
837 | { this->u_.value = value; } | |
838 | ||
839 | // Return the input value. This is only called by | |
840 | // finalize_local_symbols. | |
841 | Value | |
842 | input_value() const | |
843 | { return this->u_.value; } | |
844 | ||
b8e6aad9 ILT |
845 | // Return whether this symbol should go into the output symbol |
846 | // table. | |
847 | bool | |
848 | needs_output_symtab_entry() const | |
ac1f0c21 | 849 | { return this->output_symtab_index_ != -1U; } |
b8e6aad9 ILT |
850 | |
851 | // Return the index in the output symbol table. | |
852 | unsigned int | |
853 | output_symtab_index() const | |
854 | { | |
855 | gold_assert(this->output_symtab_index_ != 0); | |
856 | return this->output_symtab_index_; | |
857 | } | |
858 | ||
859 | // Set the index in the output symbol table. | |
860 | void | |
861 | set_output_symtab_index(unsigned int i) | |
862 | { | |
863 | gold_assert(this->output_symtab_index_ == 0); | |
864 | this->output_symtab_index_ = i; | |
865 | } | |
866 | ||
867 | // Record that this symbol should not go into the output symbol | |
868 | // table. | |
869 | void | |
870 | set_no_output_symtab_entry() | |
871 | { | |
872 | gold_assert(this->output_symtab_index_ == 0); | |
873 | this->output_symtab_index_ = -1U; | |
874 | } | |
875 | ||
7bf1f802 ILT |
876 | // Set the index in the output dynamic symbol table. |
877 | void | |
878 | set_needs_output_dynsym_entry() | |
879 | { | |
dceae3c1 | 880 | gold_assert(!this->is_section_symbol()); |
7bf1f802 ILT |
881 | this->output_dynsym_index_ = 0; |
882 | } | |
883 | ||
884 | // Return whether this symbol should go into the output symbol | |
885 | // table. | |
886 | bool | |
887 | needs_output_dynsym_entry() const | |
888 | { | |
889 | return this->output_dynsym_index_ != -1U; | |
890 | } | |
891 | ||
892 | // Record that this symbol should go into the dynamic symbol table. | |
893 | void | |
894 | set_output_dynsym_index(unsigned int i) | |
895 | { | |
896 | gold_assert(this->output_dynsym_index_ == 0); | |
897 | this->output_dynsym_index_ = i; | |
898 | } | |
899 | ||
900 | // Return the index in the output dynamic symbol table. | |
901 | unsigned int | |
902 | output_dynsym_index() const | |
903 | { | |
dceae3c1 ILT |
904 | gold_assert(this->output_dynsym_index_ != 0 |
905 | && this->output_dynsym_index_ != -1U); | |
7bf1f802 ILT |
906 | return this->output_dynsym_index_; |
907 | } | |
908 | ||
b8e6aad9 ILT |
909 | // Set the index of the input section in the input file. |
910 | void | |
911 | set_input_shndx(unsigned int i) | |
730cdc88 ILT |
912 | { |
913 | this->input_shndx_ = i; | |
ac1f0c21 ILT |
914 | // input_shndx_ field is a bitfield, so make sure that the value |
915 | // fits. | |
730cdc88 ILT |
916 | gold_assert(this->input_shndx_ == i); |
917 | } | |
b8e6aad9 | 918 | |
7bf1f802 ILT |
919 | // Return the index of the input section in the input file. |
920 | unsigned int | |
921 | input_shndx() const | |
922 | { return this->input_shndx_; } | |
923 | ||
a9a60db6 ILT |
924 | // Whether this is a section symbol. |
925 | bool | |
926 | is_section_symbol() const | |
927 | { return this->is_section_symbol_; } | |
928 | ||
063f12a8 ILT |
929 | // Record that this is a section symbol. |
930 | void | |
931 | set_is_section_symbol() | |
dceae3c1 ILT |
932 | { |
933 | gold_assert(!this->needs_output_dynsym_entry()); | |
934 | this->is_section_symbol_ = true; | |
935 | } | |
063f12a8 | 936 | |
7bf1f802 ILT |
937 | // Record that this is a TLS symbol. |
938 | void | |
939 | set_is_tls_symbol() | |
940 | { this->is_tls_symbol_ = true; } | |
941 | ||
942 | // Return TRUE if this is a TLS symbol. | |
943 | bool | |
944 | is_tls_symbol() const | |
945 | { return this->is_tls_symbol_; } | |
946 | ||
b8e6aad9 ILT |
947 | private: |
948 | // The index of this local symbol in the output symbol table. This | |
949 | // will be -1 if the symbol should not go into the symbol table. | |
950 | unsigned int output_symtab_index_; | |
7bf1f802 ILT |
951 | // The index of this local symbol in the dynamic symbol table. This |
952 | // will be -1 if the symbol should not go into the symbol table. | |
953 | unsigned int output_dynsym_index_; | |
b8e6aad9 ILT |
954 | // The section index in the input file in which this symbol is |
955 | // defined. | |
7bf1f802 | 956 | unsigned int input_shndx_ : 29; |
063f12a8 ILT |
957 | // Whether this is a STT_SECTION symbol. |
958 | bool is_section_symbol_ : 1; | |
7bf1f802 ILT |
959 | // Whether this is a STT_TLS symbol. |
960 | bool is_tls_symbol_ : 1; | |
a9a60db6 ILT |
961 | // Whether this symbol has a value for the output file. This is |
962 | // normally set to true during Layout::finalize, by | |
963 | // finalize_local_symbols. It will be false for a section symbol in | |
964 | // a merge section, as for such symbols we can not determine the | |
965 | // value to use in a relocation until we see the addend. | |
966 | bool has_output_value_ : 1; | |
967 | union | |
968 | { | |
969 | // This is used if has_output_value_ is true. Between | |
970 | // count_local_symbols and finalize_local_symbols, this is the | |
971 | // value in the input file. After finalize_local_symbols, it is | |
972 | // the value in the output file. | |
973 | Value value; | |
974 | // This is used if has_output_value_ is false. It points to the | |
975 | // information we need to get the value for a merge section. | |
976 | Merged_symbol_value<size>* merged_symbol_value; | |
977 | } u_; | |
b8e6aad9 ILT |
978 | }; |
979 | ||
0a65a3a7 CC |
980 | // A GOT offset list. A symbol may have more than one GOT offset |
981 | // (e.g., when mixing modules compiled with two different TLS models), | |
982 | // but will usually have at most one. GOT_TYPE identifies the type of | |
983 | // GOT entry; its values are specific to each target. | |
984 | ||
985 | class Got_offset_list | |
986 | { | |
987 | public: | |
988 | Got_offset_list() | |
989 | : got_type_(-1U), got_offset_(0), got_next_(NULL) | |
990 | { } | |
991 | ||
992 | Got_offset_list(unsigned int got_type, unsigned int got_offset) | |
993 | : got_type_(got_type), got_offset_(got_offset), got_next_(NULL) | |
994 | { } | |
995 | ||
996 | ~Got_offset_list() | |
997 | { | |
998 | if (this->got_next_ != NULL) | |
999 | { | |
1000 | delete this->got_next_; | |
1001 | this->got_next_ = NULL; | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | // Initialize the fields to their default values. | |
1006 | void | |
1007 | init() | |
1008 | { | |
1009 | this->got_type_ = -1U; | |
1010 | this->got_offset_ = 0; | |
1011 | this->got_next_ = NULL; | |
1012 | } | |
1013 | ||
1014 | // Set the offset for the GOT entry of type GOT_TYPE. | |
1015 | void | |
1016 | set_offset(unsigned int got_type, unsigned int got_offset) | |
1017 | { | |
1018 | if (this->got_type_ == -1U) | |
1019 | { | |
1020 | this->got_type_ = got_type; | |
1021 | this->got_offset_ = got_offset; | |
1022 | } | |
1023 | else | |
1024 | { | |
1025 | for (Got_offset_list* g = this; g != NULL; g = g->got_next_) | |
1026 | { | |
1027 | if (g->got_type_ == got_type) | |
1028 | { | |
1029 | g->got_offset_ = got_offset; | |
1030 | return; | |
1031 | } | |
1032 | } | |
1033 | Got_offset_list* g = new Got_offset_list(got_type, got_offset); | |
1034 | g->got_next_ = this->got_next_; | |
1035 | this->got_next_ = g; | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | // Return the offset for a GOT entry of type GOT_TYPE. | |
1040 | unsigned int | |
1041 | get_offset(unsigned int got_type) const | |
1042 | { | |
1043 | for (const Got_offset_list* g = this; g != NULL; g = g->got_next_) | |
1044 | { | |
1045 | if (g->got_type_ == got_type) | |
1046 | return g->got_offset_; | |
1047 | } | |
1048 | return -1U; | |
1049 | } | |
1050 | ||
1051 | private: | |
1052 | unsigned int got_type_; | |
1053 | unsigned int got_offset_; | |
1054 | Got_offset_list* got_next_; | |
1055 | }; | |
1056 | ||
14bfc3f5 | 1057 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
1058 | |
1059 | template<int size, bool big_endian> | |
f6ce93d6 | 1060 | class Sized_relobj : public Relobj |
bae7f79e ILT |
1061 | { |
1062 | public: | |
c06b7b0b | 1063 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
730cdc88 | 1064 | typedef std::vector<Symbol*> Symbols; |
b8e6aad9 | 1065 | typedef std::vector<Symbol_value<size> > Local_values; |
c06b7b0b | 1066 | |
f6ce93d6 | 1067 | Sized_relobj(const std::string& name, Input_file* input_file, off_t offset, |
bae7f79e ILT |
1068 | const typename elfcpp::Ehdr<size, big_endian>&); |
1069 | ||
f6ce93d6 | 1070 | ~Sized_relobj(); |
bae7f79e | 1071 | |
a2fb1b05 | 1072 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
1073 | void |
1074 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
1075 | ||
730cdc88 ILT |
1076 | // If SYM is the index of a global symbol in the object file's |
1077 | // symbol table, return the Symbol object. Otherwise, return NULL. | |
1078 | Symbol* | |
1079 | global_symbol(unsigned int sym) const | |
1080 | { | |
1081 | if (sym >= this->local_symbol_count_) | |
1082 | { | |
1083 | gold_assert(sym - this->local_symbol_count_ < this->symbols_.size()); | |
1084 | return this->symbols_[sym - this->local_symbol_count_]; | |
1085 | } | |
1086 | return NULL; | |
1087 | } | |
1088 | ||
1089 | // Return the section index of symbol SYM. Set *VALUE to its value | |
1090 | // in the object file. Note that for a symbol which is not defined | |
1091 | // in this object file, this will set *VALUE to 0 and return | |
1092 | // SHN_UNDEF; it will not return the final value of the symbol in | |
1093 | // the link. | |
1094 | unsigned int | |
1095 | symbol_section_and_value(unsigned int sym, Address* value); | |
1096 | ||
1097 | // Return a pointer to the Symbol_value structure which holds the | |
1098 | // value of a local symbol. | |
1099 | const Symbol_value<size>* | |
1100 | local_symbol(unsigned int sym) const | |
1101 | { | |
1102 | gold_assert(sym < this->local_values_.size()); | |
1103 | return &this->local_values_[sym]; | |
1104 | } | |
1105 | ||
c06b7b0b ILT |
1106 | // Return the index of local symbol SYM in the ordinary symbol |
1107 | // table. A value of -1U means that the symbol is not being output. | |
1108 | unsigned int | |
1109 | symtab_index(unsigned int sym) const | |
1110 | { | |
b8e6aad9 ILT |
1111 | gold_assert(sym < this->local_values_.size()); |
1112 | return this->local_values_[sym].output_symtab_index(); | |
c06b7b0b ILT |
1113 | } |
1114 | ||
7bf1f802 ILT |
1115 | // Return the index of local symbol SYM in the dynamic symbol |
1116 | // table. A value of -1U means that the symbol is not being output. | |
1117 | unsigned int | |
1118 | dynsym_index(unsigned int sym) const | |
1119 | { | |
1120 | gold_assert(sym < this->local_values_.size()); | |
1121 | return this->local_values_[sym].output_dynsym_index(); | |
1122 | } | |
1123 | ||
6a74a719 ILT |
1124 | // Return the input section index of local symbol SYM. |
1125 | unsigned int | |
1126 | local_symbol_input_shndx(unsigned int sym) const | |
1127 | { | |
1128 | gold_assert(sym < this->local_values_.size()); | |
1129 | return this->local_values_[sym].input_shndx(); | |
1130 | } | |
1131 | ||
e727fa71 ILT |
1132 | // Return the appropriate Sized_target structure. |
1133 | Sized_target<size, big_endian>* | |
1134 | sized_target() | |
7d1a9ebb | 1135 | { return this->Object::sized_target<size, big_endian>(); } |
e727fa71 | 1136 | |
d1f003c6 | 1137 | // Record that local symbol SYM needs a dynamic symbol entry. |
7bf1f802 ILT |
1138 | void |
1139 | set_needs_output_dynsym_entry(unsigned int sym) | |
1140 | { | |
1141 | gold_assert(sym < this->local_values_.size()); | |
1142 | this->local_values_[sym].set_needs_output_dynsym_entry(); | |
1143 | } | |
1144 | ||
e727fa71 | 1145 | // Return whether the local symbol SYMNDX has a GOT offset. |
07f397ab | 1146 | // For TLS symbols, the GOT entry will hold its tp-relative offset. |
e727fa71 | 1147 | bool |
0a65a3a7 | 1148 | local_has_got_offset(unsigned int symndx, unsigned int got_type) const |
e727fa71 | 1149 | { |
0a65a3a7 CC |
1150 | Local_got_offsets::const_iterator p = |
1151 | this->local_got_offsets_.find(symndx); | |
1152 | return (p != this->local_got_offsets_.end() | |
1153 | && p->second->get_offset(got_type) != -1U); | |
e727fa71 ILT |
1154 | } |
1155 | ||
1156 | // Return the GOT offset of the local symbol SYMNDX. | |
1157 | unsigned int | |
0a65a3a7 | 1158 | local_got_offset(unsigned int symndx, unsigned int got_type) const |
e727fa71 ILT |
1159 | { |
1160 | Local_got_offsets::const_iterator p = | |
1161 | this->local_got_offsets_.find(symndx); | |
1162 | gold_assert(p != this->local_got_offsets_.end()); | |
0a65a3a7 CC |
1163 | unsigned int off = p->second->get_offset(got_type); |
1164 | gold_assert(off != -1U); | |
1165 | return off; | |
e727fa71 ILT |
1166 | } |
1167 | ||
1168 | // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET. | |
1169 | void | |
0a65a3a7 CC |
1170 | set_local_got_offset(unsigned int symndx, unsigned int got_type, |
1171 | unsigned int got_offset) | |
07f397ab | 1172 | { |
0a65a3a7 CC |
1173 | Local_got_offsets::const_iterator p = |
1174 | this->local_got_offsets_.find(symndx); | |
1175 | if (p != this->local_got_offsets_.end()) | |
1176 | p->second->set_offset(got_type, got_offset); | |
07f397ab ILT |
1177 | else |
1178 | { | |
0a65a3a7 CC |
1179 | Got_offset_list* g = new Got_offset_list(got_type, got_offset); |
1180 | std::pair<Local_got_offsets::iterator, bool> ins = | |
1181 | this->local_got_offsets_.insert(std::make_pair(symndx, g)); | |
07f397ab ILT |
1182 | gold_assert(ins.second); |
1183 | } | |
1184 | } | |
1185 | ||
f7e2ee48 ILT |
1186 | // Return the name of the symbol that spans the given offset in the |
1187 | // specified section in this object. This is used only for error | |
1188 | // messages and is not particularly efficient. | |
1189 | bool | |
1190 | get_symbol_location_info(unsigned int shndx, off_t offset, | |
1191 | Symbol_location_info* info); | |
1192 | ||
6d013333 | 1193 | protected: |
a2fb1b05 | 1194 | // Read the symbols. |
bae7f79e | 1195 | void |
12e14209 | 1196 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 1197 | |
6d013333 ILT |
1198 | // Return the number of local symbols. |
1199 | unsigned int | |
1200 | do_local_symbol_count() const | |
1201 | { return this->local_symbol_count_; } | |
1202 | ||
dbe717ef ILT |
1203 | // Lay out the input sections. |
1204 | void | |
7e1edb90 | 1205 | do_layout(Symbol_table*, Layout*, Read_symbols_data*); |
dbe717ef | 1206 | |
12e14209 ILT |
1207 | // Add the symbols to the symbol table. |
1208 | void | |
1209 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 1210 | |
92e059d8 ILT |
1211 | // Read the relocs. |
1212 | void | |
1213 | do_read_relocs(Read_relocs_data*); | |
1214 | ||
1215 | // Scan the relocs and adjust the symbol table. | |
1216 | void | |
ead1e424 ILT |
1217 | do_scan_relocs(const General_options&, Symbol_table*, Layout*, |
1218 | Read_relocs_data*); | |
92e059d8 | 1219 | |
7bf1f802 ILT |
1220 | // Count the local symbols. |
1221 | void | |
1222 | do_count_local_symbols(Stringpool_template<char>*, | |
1223 | Stringpool_template<char>*); | |
1224 | ||
75f65a3e | 1225 | // Finalize the local symbols. |
c06b7b0b | 1226 | unsigned int |
7bf1f802 ILT |
1227 | do_finalize_local_symbols(unsigned int, off_t); |
1228 | ||
1229 | // Set the offset where local dynamic symbol information will be stored. | |
1230 | unsigned int | |
1231 | do_set_local_dynsym_indexes(unsigned int); | |
1232 | ||
1233 | // Set the offset where local dynamic symbol information will be stored. | |
1234 | unsigned int | |
1235 | do_set_local_dynsym_offset(off_t); | |
75f65a3e | 1236 | |
61ba1cf9 ILT |
1237 | // Relocate the input sections and write out the local symbols. |
1238 | void | |
1239 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
92e059d8 ILT |
1240 | const Layout*, Output_file* of); |
1241 | ||
a445fddf ILT |
1242 | // Get the size of a section. |
1243 | uint64_t | |
1244 | do_section_size(unsigned int shndx) | |
1245 | { return this->elf_file_.section_size(shndx); } | |
1246 | ||
92e059d8 ILT |
1247 | // Get the name of a section. |
1248 | std::string | |
645f8123 ILT |
1249 | do_section_name(unsigned int shndx) |
1250 | { return this->elf_file_.section_name(shndx); } | |
61ba1cf9 | 1251 | |
645f8123 | 1252 | // Return the location of the contents of a section. |
dbe717ef | 1253 | Object::Location |
645f8123 ILT |
1254 | do_section_contents(unsigned int shndx) |
1255 | { return this->elf_file_.section_contents(shndx); } | |
f6ce93d6 | 1256 | |
a3ad94ed ILT |
1257 | // Return section flags. |
1258 | uint64_t | |
1259 | do_section_flags(unsigned int shndx) | |
1260 | { return this->elf_file_.section_flags(shndx); } | |
1261 | ||
88dd47ac ILT |
1262 | // Return section address. |
1263 | uint64_t | |
1264 | do_section_address(unsigned int shndx) | |
1265 | { return this->elf_file_.section_addr(shndx); } | |
1266 | ||
730cdc88 ILT |
1267 | // Return section type. |
1268 | unsigned int | |
1269 | do_section_type(unsigned int shndx) | |
1270 | { return this->elf_file_.section_type(shndx); } | |
1271 | ||
f7e2ee48 ILT |
1272 | // Return the section link field. |
1273 | unsigned int | |
1274 | do_section_link(unsigned int shndx) | |
1275 | { return this->elf_file_.section_link(shndx); } | |
1276 | ||
4c50553d ILT |
1277 | // Return the section info field. |
1278 | unsigned int | |
1279 | do_section_info(unsigned int shndx) | |
1280 | { return this->elf_file_.section_info(shndx); } | |
1281 | ||
a445fddf ILT |
1282 | // Return the section alignment. |
1283 | uint64_t | |
1284 | do_section_addralign(unsigned int shndx) | |
1285 | { return this->elf_file_.section_addralign(shndx); } | |
1286 | ||
bae7f79e | 1287 | private: |
a2fb1b05 | 1288 | // For convenience. |
f6ce93d6 | 1289 | typedef Sized_relobj<size, big_endian> This; |
a2fb1b05 ILT |
1290 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; |
1291 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
1292 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
1293 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
1294 | ||
dbe717ef ILT |
1295 | // Find the SHT_SYMTAB section, given the section headers. |
1296 | void | |
1297 | find_symtab(const unsigned char* pshdrs); | |
1298 | ||
730cdc88 ILT |
1299 | // Return whether SHDR has the right flags for a GNU style exception |
1300 | // frame section. | |
1301 | bool | |
1302 | check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const; | |
1303 | ||
1304 | // Return whether there is a section named .eh_frame which might be | |
1305 | // a GNU style exception frame section. | |
1306 | bool | |
1307 | find_eh_frame(const unsigned char* pshdrs, const char* names, | |
8383303e | 1308 | section_size_type names_size) const; |
730cdc88 | 1309 | |
a2fb1b05 ILT |
1310 | // Whether to include a section group in the link. |
1311 | bool | |
6a74a719 | 1312 | include_section_group(Symbol_table*, Layout*, unsigned int, const char*, |
a2fb1b05 ILT |
1313 | const elfcpp::Shdr<size, big_endian>&, |
1314 | std::vector<bool>*); | |
1315 | ||
1316 | // Whether to include a linkonce section in the link. | |
1317 | bool | |
1318 | include_linkonce_section(Layout*, const char*, | |
1319 | const elfcpp::Shdr<size, big_endian>&); | |
1320 | ||
61ba1cf9 ILT |
1321 | // Views and sizes when relocating. |
1322 | struct View_size | |
1323 | { | |
1324 | unsigned char* view; | |
1325 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
1326 | off_t offset; | |
8383303e | 1327 | section_size_type view_size; |
730cdc88 | 1328 | bool is_input_output_view; |
96803768 | 1329 | bool is_postprocessing_view; |
61ba1cf9 ILT |
1330 | }; |
1331 | ||
1332 | typedef std::vector<View_size> Views; | |
1333 | ||
1334 | // Write section data to the output file. Record the views and | |
1335 | // sizes in VIEWS for use when relocating. | |
1336 | void | |
cb295612 | 1337 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); |
61ba1cf9 ILT |
1338 | |
1339 | // Relocate the sections in the output file. | |
1340 | void | |
92e059d8 ILT |
1341 | relocate_sections(const General_options& options, const Symbol_table*, |
1342 | const Layout*, const unsigned char* pshdrs, Views*); | |
61ba1cf9 | 1343 | |
7019cd25 ILT |
1344 | // Scan the input relocations for --emit-relocs. |
1345 | void | |
1346 | emit_relocs_scan(const General_options&, Symbol_table*, Layout*, | |
1347 | const unsigned char* plocal_syms, | |
1348 | const Read_relocs_data::Relocs_list::iterator&); | |
1349 | ||
1350 | // Scan the input relocations for --emit-relocs, templatized on the | |
1351 | // type of the relocation section. | |
1352 | template<int sh_type> | |
1353 | void | |
1354 | emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*, | |
1355 | const unsigned char* plocal_syms, | |
1356 | const Read_relocs_data::Relocs_list::iterator&, | |
1357 | Relocatable_relocs*); | |
1358 | ||
1359 | // Emit the relocs for --emit-relocs. | |
1360 | void | |
1361 | emit_relocs(const Relocate_info<size, big_endian>*, unsigned int, | |
1362 | unsigned int sh_type, const unsigned char* prelocs, | |
1363 | size_t reloc_count, Output_section*, off_t output_offset, | |
1364 | unsigned char* view, Address address, | |
1365 | section_size_type view_size, | |
1366 | unsigned char* reloc_view, section_size_type reloc_view_size); | |
1367 | ||
1368 | // Emit the relocs for --emit-relocs, templatized on the type of the | |
1369 | // relocation section. | |
1370 | template<int sh_type> | |
1371 | void | |
1372 | emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int, | |
1373 | const unsigned char* prelocs, size_t reloc_count, | |
1374 | Output_section*, off_t output_offset, | |
1375 | unsigned char* view, Address address, | |
1376 | section_size_type view_size, | |
1377 | unsigned char* reloc_view, | |
1378 | section_size_type reloc_view_size); | |
1379 | ||
a9a60db6 ILT |
1380 | // Initialize input to output maps for section symbols in merged |
1381 | // sections. | |
1382 | void | |
1383 | initialize_input_to_output_maps(); | |
1384 | ||
1385 | // Free the input to output maps for section symbols in merged | |
1386 | // sections. | |
1387 | void | |
1388 | free_input_to_output_maps(); | |
1389 | ||
61ba1cf9 ILT |
1390 | // Write out the local symbols. |
1391 | void | |
b8e6aad9 | 1392 | write_local_symbols(Output_file*, |
7bf1f802 | 1393 | const Stringpool_template<char>*, |
b8e6aad9 | 1394 | const Stringpool_template<char>*); |
61ba1cf9 | 1395 | |
cb295612 ILT |
1396 | // Clear the local symbol information. |
1397 | void | |
1398 | clear_local_symbols() | |
1399 | { | |
1400 | this->local_values_.clear(); | |
1401 | this->local_got_offsets_.clear(); | |
cb295612 ILT |
1402 | } |
1403 | ||
07f397ab ILT |
1404 | // The GOT offsets of local symbols. This map also stores GOT offsets |
1405 | // for tp-relative offsets for TLS symbols. | |
0a65a3a7 | 1406 | typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets; |
e727fa71 | 1407 | |
07f397ab ILT |
1408 | // The TLS GOT offsets of local symbols. The map stores the offsets |
1409 | // for either a single GOT entry that holds the module index of a TLS | |
1410 | // symbol, or a pair of GOT entries containing the module index and | |
1411 | // dtv-relative offset. | |
1412 | struct Tls_got_entry | |
1413 | { | |
1414 | Tls_got_entry(int got_offset, bool have_pair) | |
1415 | : got_offset_(got_offset), | |
1416 | have_pair_(have_pair) | |
1417 | { } | |
1418 | int got_offset_; | |
1419 | bool have_pair_; | |
1420 | }; | |
1421 | typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets; | |
1422 | ||
645f8123 ILT |
1423 | // General access to the ELF file. |
1424 | elfcpp::Elf_file<size, big_endian, Object> elf_file_; | |
bae7f79e | 1425 | // Index of SHT_SYMTAB section. |
645f8123 | 1426 | unsigned int symtab_shndx_; |
61ba1cf9 ILT |
1427 | // The number of local symbols. |
1428 | unsigned int local_symbol_count_; | |
1429 | // The number of local symbols which go into the output file. | |
1430 | unsigned int output_local_symbol_count_; | |
7bf1f802 ILT |
1431 | // The number of local symbols which go into the output file's dynamic |
1432 | // symbol table. | |
1433 | unsigned int output_local_dynsym_count_; | |
14bfc3f5 | 1434 | // The entries in the symbol table for the external symbols. |
730cdc88 | 1435 | Symbols symbols_; |
75f65a3e ILT |
1436 | // File offset for local symbols. |
1437 | off_t local_symbol_offset_; | |
7bf1f802 ILT |
1438 | // File offset for local dynamic symbols. |
1439 | off_t local_dynsym_offset_; | |
61ba1cf9 | 1440 | // Values of local symbols. |
c06b7b0b | 1441 | Local_values local_values_; |
07f397ab ILT |
1442 | // GOT offsets for local non-TLS symbols, and tp-relative offsets |
1443 | // for TLS symbols, indexed by symbol number. | |
e727fa71 | 1444 | Local_got_offsets local_got_offsets_; |
730cdc88 ILT |
1445 | // Whether this object has a GNU style .eh_frame section. |
1446 | bool has_eh_frame_; | |
bae7f79e ILT |
1447 | }; |
1448 | ||
54dc6425 | 1449 | // A class to manage the list of all objects. |
a2fb1b05 | 1450 | |
54dc6425 ILT |
1451 | class Input_objects |
1452 | { | |
1453 | public: | |
1454 | Input_objects() | |
fbfba508 | 1455 | : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_() |
54dc6425 ILT |
1456 | { } |
1457 | ||
f6ce93d6 ILT |
1458 | // The type of the list of input relocateable objects. |
1459 | typedef std::vector<Relobj*> Relobj_list; | |
1460 | typedef Relobj_list::const_iterator Relobj_iterator; | |
1461 | ||
1462 | // The type of the list of input dynamic objects. | |
1463 | typedef std::vector<Dynobj*> Dynobj_list; | |
1464 | typedef Dynobj_list::const_iterator Dynobj_iterator; | |
54dc6425 | 1465 | |
008db82e ILT |
1466 | // Add an object to the list. Return true if all is well, or false |
1467 | // if this object should be ignored. | |
1468 | bool | |
54dc6425 ILT |
1469 | add_object(Object*); |
1470 | ||
e2827e5f ILT |
1471 | // For each dynamic object, check whether we've seen all of its |
1472 | // explicit dependencies. | |
1473 | void | |
1474 | check_dynamic_dependencies() const; | |
1475 | ||
9a2d6984 ILT |
1476 | // Return whether an object was found in the system library |
1477 | // directory. | |
1478 | bool | |
1479 | found_in_system_library_directory(const Object*) const; | |
1480 | ||
f6ce93d6 ILT |
1481 | // Iterate over all regular objects. |
1482 | ||
1483 | Relobj_iterator | |
1484 | relobj_begin() const | |
1485 | { return this->relobj_list_.begin(); } | |
1486 | ||
1487 | Relobj_iterator | |
1488 | relobj_end() const | |
1489 | { return this->relobj_list_.end(); } | |
1490 | ||
1491 | // Iterate over all dynamic objects. | |
1492 | ||
1493 | Dynobj_iterator | |
1494 | dynobj_begin() const | |
1495 | { return this->dynobj_list_.begin(); } | |
54dc6425 | 1496 | |
f6ce93d6 ILT |
1497 | Dynobj_iterator |
1498 | dynobj_end() const | |
1499 | { return this->dynobj_list_.end(); } | |
54dc6425 ILT |
1500 | |
1501 | // Return whether we have seen any dynamic objects. | |
1502 | bool | |
1503 | any_dynamic() const | |
f6ce93d6 | 1504 | { return !this->dynobj_list_.empty(); } |
54dc6425 | 1505 | |
fe9a4c12 ILT |
1506 | // Return the number of input objects. |
1507 | int | |
1508 | number_of_input_objects() const | |
1509 | { return this->relobj_list_.size() + this->dynobj_list_.size(); } | |
1510 | ||
54dc6425 ILT |
1511 | private: |
1512 | Input_objects(const Input_objects&); | |
1513 | Input_objects& operator=(const Input_objects&); | |
1514 | ||
008db82e | 1515 | // The list of ordinary objects included in the link. |
f6ce93d6 | 1516 | Relobj_list relobj_list_; |
008db82e | 1517 | // The list of dynamic objects included in the link. |
f6ce93d6 | 1518 | Dynobj_list dynobj_list_; |
008db82e ILT |
1519 | // SONAMEs that we have seen. |
1520 | Unordered_set<std::string> sonames_; | |
9a2d6984 ILT |
1521 | // The directory in which we find the libc.so. |
1522 | std::string system_library_directory_; | |
54dc6425 | 1523 | }; |
a2fb1b05 | 1524 | |
92e059d8 ILT |
1525 | // Some of the information we pass to the relocation routines. We |
1526 | // group this together to avoid passing a dozen different arguments. | |
1527 | ||
1528 | template<int size, bool big_endian> | |
1529 | struct Relocate_info | |
1530 | { | |
1531 | // Command line options. | |
1532 | const General_options* options; | |
1533 | // Symbol table. | |
1534 | const Symbol_table* symtab; | |
1535 | // Layout. | |
1536 | const Layout* layout; | |
1537 | // Object being relocated. | |
f6ce93d6 | 1538 | Sized_relobj<size, big_endian>* object; |
92e059d8 ILT |
1539 | // Section index of relocation section. |
1540 | unsigned int reloc_shndx; | |
1541 | // Section index of section being relocated. | |
1542 | unsigned int data_shndx; | |
1543 | ||
1544 | // Return a string showing the location of a relocation. This is | |
1545 | // only used for error messages. | |
1546 | std::string | |
1547 | location(size_t relnum, off_t reloffset) const; | |
1548 | }; | |
1549 | ||
bae7f79e ILT |
1550 | // Return an Object appropriate for the input file. P is BYTES long, |
1551 | // and holds the ELF header. | |
1552 | ||
61ba1cf9 ILT |
1553 | extern Object* |
1554 | make_elf_object(const std::string& name, Input_file*, | |
1555 | off_t offset, const unsigned char* p, | |
8383303e | 1556 | section_offset_type bytes); |
bae7f79e ILT |
1557 | |
1558 | } // end namespace gold | |
1559 | ||
1560 | #endif // !defined(GOLD_OBJECT_H) |