Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.h -- support for an object file for linking in gold -*- C++ -*- |
2 | ||
3 | #ifndef GOLD_OBJECT_H | |
4 | #define GOLD_OBJECT_H | |
5 | ||
14bfc3f5 | 6 | #include <cassert> |
a2fb1b05 | 7 | #include <vector> |
14bfc3f5 | 8 | |
bae7f79e | 9 | #include "elfcpp.h" |
bae7f79e | 10 | #include "fileread.h" |
14bfc3f5 ILT |
11 | #include "target.h" |
12 | #include "symtab.h" | |
bae7f79e ILT |
13 | |
14 | namespace gold | |
15 | { | |
16 | ||
75f65a3e | 17 | class Stringpool; |
a2fb1b05 | 18 | class Layout; |
61ba1cf9 ILT |
19 | class Output_section; |
20 | class Output_file; | |
a2fb1b05 | 21 | |
bae7f79e ILT |
22 | // Data to pass from read_symbols() to add_symbols(). |
23 | ||
24 | struct Read_symbols_data | |
25 | { | |
12e14209 ILT |
26 | // Section headers. |
27 | File_view* section_headers; | |
28 | // Section names. | |
29 | File_view* section_names; | |
30 | // Size of section name data in bytes. | |
31 | off_t section_names_size; | |
bae7f79e ILT |
32 | // Symbol data. |
33 | File_view* symbols; | |
34 | // Size of symbol data in bytes. | |
35 | off_t symbols_size; | |
36 | // Symbol names. | |
37 | File_view* symbol_names; | |
38 | // Size of symbol name data in bytes. | |
39 | off_t symbol_names_size; | |
40 | }; | |
41 | ||
42 | // Object is an interface which represents either a 32-bit or a 64-bit | |
14bfc3f5 ILT |
43 | // input object. This can be a regular object file (ET_REL) or a |
44 | // shared object (ET_DYN). The actual instantiations are | |
45 | // Sized_object<32> and Sized_object<64> | |
bae7f79e ILT |
46 | |
47 | class Object | |
48 | { | |
49 | public: | |
50 | // NAME is the name of the object as we would report it to the user | |
51 | // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is | |
52 | // used to read the file. OFFSET is the offset within the input | |
53 | // file--0 for a .o or .so file, something else for a .a file. | |
14bfc3f5 ILT |
54 | Object(const std::string& name, Input_file* input_file, bool is_dynamic, |
55 | off_t offset = 0) | |
56 | : name_(name), input_file_(input_file), offset_(offset), | |
a2fb1b05 ILT |
57 | shnum_(0), is_dynamic_(is_dynamic), target_(NULL), |
58 | map_to_output_() | |
bae7f79e ILT |
59 | { } |
60 | ||
61 | virtual ~Object() | |
62 | { } | |
63 | ||
14bfc3f5 | 64 | // Return the name of the object as we would report it to the tuser. |
bae7f79e ILT |
65 | const std::string& |
66 | name() const | |
67 | { return this->name_; } | |
68 | ||
14bfc3f5 ILT |
69 | // Return whether this is a dynamic object. |
70 | bool | |
71 | is_dynamic() const | |
72 | { return this->is_dynamic_; } | |
73 | ||
14bfc3f5 ILT |
74 | // Return the target structure associated with this object. |
75 | Target* | |
a2fb1b05 | 76 | target() const |
14bfc3f5 ILT |
77 | { return this->target_; } |
78 | ||
a2fb1b05 ILT |
79 | // Lock the underlying file. |
80 | void | |
81 | lock() | |
82 | { this->input_file_->file().lock(); } | |
83 | ||
84 | // Unlock the underlying file. | |
85 | void | |
86 | unlock() | |
87 | { this->input_file_->file().unlock(); } | |
88 | ||
89 | // Return whether the underlying file is locked. | |
90 | bool | |
91 | is_locked() const | |
92 | { return this->input_file_->file().is_locked(); } | |
93 | ||
14bfc3f5 ILT |
94 | // Return the sized target structure associated with this object. |
95 | // This is like the target method but it returns a pointer of | |
96 | // appropriate checked type. | |
97 | template<int size, bool big_endian> | |
98 | Sized_target<size, big_endian>* | |
5482377d | 99 | sized_target(ACCEPT_SIZE_ENDIAN_ONLY); |
bae7f79e | 100 | |
a2fb1b05 | 101 | // Read the symbol and relocation information. |
12e14209 ILT |
102 | void |
103 | read_symbols(Read_symbols_data* sd) | |
104 | { return this->do_read_symbols(sd); } | |
a2fb1b05 ILT |
105 | |
106 | // Add symbol information to the global symbol table. | |
107 | void | |
12e14209 ILT |
108 | add_symbols(Symbol_table* symtab, Read_symbols_data* sd) |
109 | { this->do_add_symbols(symtab, sd); } | |
a2fb1b05 ILT |
110 | |
111 | // Pass sections which should be included in the link to the Layout | |
112 | // object, and record where the sections go in the output file. | |
113 | void | |
12e14209 ILT |
114 | layout(Layout* lay, Read_symbols_data* sd) |
115 | { this->do_layout(lay, sd); } | |
a2fb1b05 | 116 | |
75f65a3e ILT |
117 | // Initial local symbol processing: set the offset where local |
118 | // symbol information will be stored; add local symbol names to | |
119 | // *POOL; return the offset following the local symbols. | |
120 | off_t | |
121 | finalize_local_symbols(off_t off, Stringpool* pool) | |
122 | { return this->do_finalize_local_symbols(off, pool); } | |
123 | ||
61ba1cf9 ILT |
124 | // Relocate the input sections and write out the local symbols. |
125 | void | |
126 | relocate(const General_options& options, const Symbol_table* symtab, | |
127 | const Stringpool* sympool, Output_file* of) | |
128 | { return this->do_relocate(options, symtab, sympool, of); } | |
129 | ||
a783673b ILT |
130 | // Return whether an input section is being included in the link. |
131 | bool | |
132 | is_section_included(unsigned int shnum) const | |
133 | { | |
134 | assert(shnum < this->map_to_output_.size()); | |
135 | return this->map_to_output_[shnum].output_section != NULL; | |
136 | } | |
137 | ||
138 | // Given a section index, return the corresponding Output_section | |
139 | // (which will be NULL if the section is not included in the link) | |
140 | // and set *POFF to the offset within that section. | |
141 | inline Output_section* | |
142 | output_section(unsigned int shnum, off_t* poff); | |
143 | ||
144 | protected: | |
a2fb1b05 ILT |
145 | // What we need to know to map an input section to an output |
146 | // section. We keep an array of these, one for each input section, | |
147 | // indexed by the input section number. | |
148 | struct Map_to_output | |
149 | { | |
150 | // The output section. This is NULL if the input section is to be | |
151 | // discarded. | |
152 | Output_section* output_section; | |
153 | // The offset within the output section. | |
154 | off_t offset; | |
155 | }; | |
156 | ||
bae7f79e | 157 | // Read the symbols--implemented by child class. |
12e14209 ILT |
158 | virtual void |
159 | do_read_symbols(Read_symbols_data*) = 0; | |
bae7f79e ILT |
160 | |
161 | // Add symbol information to the global symbol table--implemented by | |
162 | // child class. | |
163 | virtual void | |
12e14209 | 164 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; |
bae7f79e | 165 | |
a2fb1b05 ILT |
166 | // Lay out sections--implemented by child class. |
167 | virtual void | |
12e14209 | 168 | do_layout(Layout*, Read_symbols_data*) = 0; |
a2fb1b05 | 169 | |
75f65a3e ILT |
170 | // Finalize local symbols--implemented by child class. |
171 | virtual off_t | |
172 | do_finalize_local_symbols(off_t, Stringpool*) = 0; | |
173 | ||
61ba1cf9 ILT |
174 | // Relocate the input sections and write out the local |
175 | // symbols--implemented by child class. | |
176 | virtual void | |
177 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
178 | const Stringpool*, Output_file* of) = 0; | |
179 | ||
bae7f79e ILT |
180 | // Get the file. |
181 | Input_file* | |
182 | input_file() const | |
183 | { return this->input_file_; } | |
184 | ||
185 | // Get the offset into the file. | |
186 | off_t | |
187 | offset() const | |
188 | { return this->offset_; } | |
189 | ||
190 | // Get a view into the underlying file. | |
191 | const unsigned char* | |
192 | get_view(off_t start, off_t size); | |
193 | ||
a2fb1b05 ILT |
194 | // Get the number of sections. |
195 | unsigned int | |
75f65a3e | 196 | shnum() const |
a2fb1b05 ILT |
197 | { return this->shnum_; } |
198 | ||
199 | // Set the number of sections. | |
200 | void | |
201 | set_shnum(int shnum) | |
202 | { this->shnum_ = shnum; } | |
203 | ||
14bfc3f5 ILT |
204 | // Set the target. |
205 | void | |
206 | set_target(Target* target) | |
207 | { this->target_ = target; } | |
208 | ||
bae7f79e ILT |
209 | // Read data from the underlying file. |
210 | void | |
211 | read(off_t start, off_t size, void* p); | |
212 | ||
213 | // Get a lasting view into the underlying file. | |
214 | File_view* | |
215 | get_lasting_view(off_t start, off_t size); | |
216 | ||
a2fb1b05 ILT |
217 | // Return the vector mapping input sections to output sections. |
218 | std::vector<Map_to_output>& | |
219 | map_to_output() | |
220 | { return this->map_to_output_; } | |
221 | ||
bae7f79e ILT |
222 | private: |
223 | // This class may not be copied. | |
224 | Object(const Object&); | |
225 | Object& operator=(const Object&); | |
226 | ||
61ba1cf9 | 227 | // Name of object as printed to user. |
bae7f79e ILT |
228 | std::string name_; |
229 | // For reading the file. | |
230 | Input_file* input_file_; | |
231 | // Offset within the file--0 for an object file, non-0 for an | |
232 | // archive. | |
233 | off_t offset_; | |
a2fb1b05 ILT |
234 | // Number of input sections. |
235 | unsigned int shnum_; | |
14bfc3f5 ILT |
236 | // Whether this is a dynamic object. |
237 | bool is_dynamic_; | |
238 | // Target functions--may be NULL if the target is not known. | |
239 | Target* target_; | |
a2fb1b05 ILT |
240 | // Mapping from input sections to output section. |
241 | std::vector<Map_to_output> map_to_output_; | |
bae7f79e ILT |
242 | }; |
243 | ||
14bfc3f5 ILT |
244 | // Implement sized_target inline for efficiency. This approach breaks |
245 | // static type checking, but is made safe using asserts. | |
246 | ||
247 | template<int size, bool big_endian> | |
248 | inline Sized_target<size, big_endian>* | |
5482377d | 249 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) |
14bfc3f5 ILT |
250 | { |
251 | assert(this->target_->get_size() == size); | |
252 | assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
253 | return static_cast<Sized_target<size, big_endian>*>(this->target_); | |
254 | } | |
255 | ||
a783673b ILT |
256 | // Implement Object::output_section inline for efficiency. |
257 | inline Output_section* | |
258 | Object::output_section(unsigned int shnum, off_t* poff) | |
259 | { | |
260 | assert(shnum < this->map_to_output_.size()); | |
261 | const Map_to_output& mo(this->map_to_output_[shnum]); | |
262 | *poff = mo.offset; | |
263 | return mo.output_section; | |
264 | } | |
265 | ||
14bfc3f5 | 266 | // A regular object file. This is size and endian specific. |
bae7f79e ILT |
267 | |
268 | template<int size, bool big_endian> | |
269 | class Sized_object : public Object | |
270 | { | |
271 | public: | |
272 | Sized_object(const std::string& name, Input_file* input_file, off_t offset, | |
273 | const typename elfcpp::Ehdr<size, big_endian>&); | |
274 | ||
275 | ~Sized_object(); | |
276 | ||
a2fb1b05 | 277 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
278 | void |
279 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
280 | ||
a2fb1b05 | 281 | // Read the symbols. |
bae7f79e | 282 | void |
12e14209 | 283 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 284 | |
a2fb1b05 ILT |
285 | // Lay out the input sections. |
286 | void | |
12e14209 ILT |
287 | do_layout(Layout*, Read_symbols_data*); |
288 | ||
289 | // Add the symbols to the symbol table. | |
290 | void | |
291 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 292 | |
75f65a3e ILT |
293 | // Finalize the local symbols. |
294 | off_t | |
295 | do_finalize_local_symbols(off_t, Stringpool*); | |
296 | ||
61ba1cf9 ILT |
297 | // Relocate the input sections and write out the local symbols. |
298 | void | |
299 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
300 | const Stringpool*, Output_file* of); | |
301 | ||
a2fb1b05 | 302 | // Return the appropriate Sized_target structure. |
14bfc3f5 ILT |
303 | Sized_target<size, big_endian>* |
304 | sized_target() | |
274e99f9 | 305 | { |
5482377d ILT |
306 | return this->Object::sized_target SELECT_SIZE_ENDIAN_NAME ( |
307 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
274e99f9 | 308 | } |
bae7f79e ILT |
309 | |
310 | private: | |
311 | // This object may not be copied. | |
312 | Sized_object(const Sized_object&); | |
313 | Sized_object& operator=(const Sized_object&); | |
314 | ||
a2fb1b05 ILT |
315 | // For convenience. |
316 | typedef Sized_object<size, big_endian> This; | |
317 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
318 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
319 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
320 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
321 | ||
322 | // Read the section header for section SHNUM. | |
323 | const unsigned char* | |
324 | section_header(unsigned int shnum); | |
a2fb1b05 ILT |
325 | |
326 | // Whether to include a section group in the link. | |
327 | bool | |
328 | include_section_group(Layout*, unsigned int, | |
329 | const elfcpp::Shdr<size, big_endian>&, | |
330 | std::vector<bool>*); | |
331 | ||
332 | // Whether to include a linkonce section in the link. | |
333 | bool | |
334 | include_linkonce_section(Layout*, const char*, | |
335 | const elfcpp::Shdr<size, big_endian>&); | |
336 | ||
61ba1cf9 ILT |
337 | // Views and sizes when relocating. |
338 | struct View_size | |
339 | { | |
340 | unsigned char* view; | |
341 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
342 | off_t offset; | |
343 | off_t view_size; | |
344 | }; | |
345 | ||
346 | typedef std::vector<View_size> Views; | |
347 | ||
348 | // Write section data to the output file. Record the views and | |
349 | // sizes in VIEWS for use when relocating. | |
350 | void | |
351 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); | |
352 | ||
353 | // Relocate the sections in the output file. | |
354 | void | |
355 | relocate_sections(const Symbol_table*, const unsigned char* pshdrs, Views*); | |
356 | ||
357 | // Write out the local symbols. | |
358 | void | |
359 | write_local_symbols(Output_file*, const Stringpool*); | |
360 | ||
12e14209 ILT |
361 | // If non-NULL, a view of the section header data. |
362 | File_view* section_headers_; | |
bae7f79e ILT |
363 | // ELF file header e_flags field. |
364 | unsigned int flags_; | |
bae7f79e ILT |
365 | // File offset of section header table. |
366 | off_t shoff_; | |
bae7f79e ILT |
367 | // Offset of SHT_STRTAB section holding section names. |
368 | unsigned int shstrndx_; | |
369 | // Index of SHT_SYMTAB section. | |
370 | unsigned int symtab_shnum_; | |
61ba1cf9 ILT |
371 | // The number of local symbols. |
372 | unsigned int local_symbol_count_; | |
373 | // The number of local symbols which go into the output file. | |
374 | unsigned int output_local_symbol_count_; | |
14bfc3f5 ILT |
375 | // The entries in the symbol table for the external symbols. |
376 | Symbol** symbols_; | |
75f65a3e ILT |
377 | // File offset for local symbols. |
378 | off_t local_symbol_offset_; | |
61ba1cf9 ILT |
379 | // Values of local symbols. |
380 | typename elfcpp::Elf_types<size>::Elf_Addr *values_; | |
bae7f79e ILT |
381 | }; |
382 | ||
54dc6425 | 383 | // A class to manage the list of all objects. |
a2fb1b05 | 384 | |
54dc6425 ILT |
385 | class Input_objects |
386 | { | |
387 | public: | |
388 | Input_objects() | |
75f65a3e | 389 | : object_list_(), target_(NULL), any_dynamic_(false) |
54dc6425 ILT |
390 | { } |
391 | ||
392 | // The type of the list of input objects. | |
393 | typedef std::list<Object*> Object_list; | |
394 | ||
395 | // Add an object to the list. | |
396 | void | |
397 | add_object(Object*); | |
398 | ||
75f65a3e ILT |
399 | // Get the target we should use for the output file. |
400 | Target* | |
401 | target() const | |
402 | { return this->target_; } | |
403 | ||
54dc6425 ILT |
404 | // Iterate over all objects. |
405 | Object_list::const_iterator | |
406 | begin() const | |
407 | { return this->object_list_.begin(); } | |
408 | ||
409 | Object_list::const_iterator | |
410 | end() const | |
411 | { return this->object_list_.end(); } | |
412 | ||
413 | // Return whether we have seen any dynamic objects. | |
414 | bool | |
415 | any_dynamic() const | |
416 | { return this->any_dynamic_; } | |
417 | ||
418 | private: | |
419 | Input_objects(const Input_objects&); | |
420 | Input_objects& operator=(const Input_objects&); | |
421 | ||
422 | Object_list object_list_; | |
75f65a3e | 423 | Target* target_; |
54dc6425 ILT |
424 | bool any_dynamic_; |
425 | }; | |
a2fb1b05 | 426 | |
bae7f79e ILT |
427 | // Return an Object appropriate for the input file. P is BYTES long, |
428 | // and holds the ELF header. | |
429 | ||
61ba1cf9 ILT |
430 | extern Object* |
431 | make_elf_object(const std::string& name, Input_file*, | |
432 | off_t offset, const unsigned char* p, | |
433 | off_t bytes); | |
bae7f79e ILT |
434 | |
435 | } // end namespace gold | |
436 | ||
437 | #endif // !defined(GOLD_OBJECT_H) |