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