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