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 | ||
a2fb1b05 ILT |
130 | // What we need to know to map an input section to an output |
131 | // section. We keep an array of these, one for each input section, | |
132 | // indexed by the input section number. | |
133 | struct Map_to_output | |
134 | { | |
135 | // The output section. This is NULL if the input section is to be | |
136 | // discarded. | |
137 | Output_section* output_section; | |
138 | // The offset within the output section. | |
139 | off_t offset; | |
140 | }; | |
141 | ||
75f65a3e ILT |
142 | // Given a section index, return the corresponding Map_to_output |
143 | // information. | |
144 | const Map_to_output* | |
145 | section_output_info(unsigned int shnum) const | |
61ba1cf9 ILT |
146 | { |
147 | assert(shnum < this->map_to_output_.size()); | |
148 | return &this->map_to_output_[shnum]; | |
149 | } | |
75f65a3e ILT |
150 | |
151 | protected: | |
bae7f79e | 152 | // Read the symbols--implemented by child class. |
12e14209 ILT |
153 | virtual void |
154 | do_read_symbols(Read_symbols_data*) = 0; | |
bae7f79e ILT |
155 | |
156 | // Add symbol information to the global symbol table--implemented by | |
157 | // child class. | |
158 | virtual void | |
12e14209 | 159 | do_add_symbols(Symbol_table*, Read_symbols_data*) = 0; |
bae7f79e | 160 | |
a2fb1b05 ILT |
161 | // Lay out sections--implemented by child class. |
162 | virtual void | |
12e14209 | 163 | do_layout(Layout*, Read_symbols_data*) = 0; |
a2fb1b05 | 164 | |
75f65a3e ILT |
165 | // Finalize local symbols--implemented by child class. |
166 | virtual off_t | |
167 | do_finalize_local_symbols(off_t, Stringpool*) = 0; | |
168 | ||
61ba1cf9 ILT |
169 | // Relocate the input sections and write out the local |
170 | // symbols--implemented by child class. | |
171 | virtual void | |
172 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
173 | const Stringpool*, Output_file* of) = 0; | |
174 | ||
bae7f79e ILT |
175 | // Get the file. |
176 | Input_file* | |
177 | input_file() const | |
178 | { return this->input_file_; } | |
179 | ||
180 | // Get the offset into the file. | |
181 | off_t | |
182 | offset() const | |
183 | { return this->offset_; } | |
184 | ||
185 | // Get a view into the underlying file. | |
186 | const unsigned char* | |
187 | get_view(off_t start, off_t size); | |
188 | ||
a2fb1b05 ILT |
189 | // Get the number of sections. |
190 | unsigned int | |
75f65a3e | 191 | shnum() const |
a2fb1b05 ILT |
192 | { return this->shnum_; } |
193 | ||
194 | // Set the number of sections. | |
195 | void | |
196 | set_shnum(int shnum) | |
197 | { this->shnum_ = shnum; } | |
198 | ||
14bfc3f5 ILT |
199 | // Set the target. |
200 | void | |
201 | set_target(Target* target) | |
202 | { this->target_ = target; } | |
203 | ||
bae7f79e ILT |
204 | // Read data from the underlying file. |
205 | void | |
206 | read(off_t start, off_t size, void* p); | |
207 | ||
208 | // Get a lasting view into the underlying file. | |
209 | File_view* | |
210 | get_lasting_view(off_t start, off_t size); | |
211 | ||
a2fb1b05 ILT |
212 | // Return the vector mapping input sections to output sections. |
213 | std::vector<Map_to_output>& | |
214 | map_to_output() | |
215 | { return this->map_to_output_; } | |
216 | ||
bae7f79e ILT |
217 | private: |
218 | // This class may not be copied. | |
219 | Object(const Object&); | |
220 | Object& operator=(const Object&); | |
221 | ||
61ba1cf9 | 222 | // Name of object as printed to user. |
bae7f79e ILT |
223 | std::string name_; |
224 | // For reading the file. | |
225 | Input_file* input_file_; | |
226 | // Offset within the file--0 for an object file, non-0 for an | |
227 | // archive. | |
228 | off_t offset_; | |
a2fb1b05 ILT |
229 | // Number of input sections. |
230 | unsigned int shnum_; | |
14bfc3f5 ILT |
231 | // Whether this is a dynamic object. |
232 | bool is_dynamic_; | |
233 | // Target functions--may be NULL if the target is not known. | |
234 | Target* target_; | |
a2fb1b05 ILT |
235 | // Mapping from input sections to output section. |
236 | std::vector<Map_to_output> map_to_output_; | |
bae7f79e ILT |
237 | }; |
238 | ||
14bfc3f5 ILT |
239 | // Implement sized_target inline for efficiency. This approach breaks |
240 | // static type checking, but is made safe using asserts. | |
241 | ||
242 | template<int size, bool big_endian> | |
243 | inline Sized_target<size, big_endian>* | |
5482377d | 244 | Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) |
14bfc3f5 ILT |
245 | { |
246 | assert(this->target_->get_size() == size); | |
247 | assert(this->target_->is_big_endian() ? big_endian : !big_endian); | |
248 | return static_cast<Sized_target<size, big_endian>*>(this->target_); | |
249 | } | |
250 | ||
251 | // A regular object file. This is size and endian specific. | |
bae7f79e ILT |
252 | |
253 | template<int size, bool big_endian> | |
254 | class Sized_object : public Object | |
255 | { | |
256 | public: | |
257 | Sized_object(const std::string& name, Input_file* input_file, off_t offset, | |
258 | const typename elfcpp::Ehdr<size, big_endian>&); | |
259 | ||
260 | ~Sized_object(); | |
261 | ||
a2fb1b05 | 262 | // Set up the object file based on the ELF header. |
bae7f79e ILT |
263 | void |
264 | setup(const typename elfcpp::Ehdr<size, big_endian>&); | |
265 | ||
a2fb1b05 | 266 | // Read the symbols. |
bae7f79e | 267 | void |
12e14209 | 268 | do_read_symbols(Read_symbols_data*); |
14bfc3f5 | 269 | |
a2fb1b05 ILT |
270 | // Lay out the input sections. |
271 | void | |
12e14209 ILT |
272 | do_layout(Layout*, Read_symbols_data*); |
273 | ||
274 | // Add the symbols to the symbol table. | |
275 | void | |
276 | do_add_symbols(Symbol_table*, Read_symbols_data*); | |
a2fb1b05 | 277 | |
75f65a3e ILT |
278 | // Finalize the local symbols. |
279 | off_t | |
280 | do_finalize_local_symbols(off_t, Stringpool*); | |
281 | ||
61ba1cf9 ILT |
282 | // Relocate the input sections and write out the local symbols. |
283 | void | |
284 | do_relocate(const General_options& options, const Symbol_table* symtab, | |
285 | const Stringpool*, Output_file* of); | |
286 | ||
a2fb1b05 | 287 | // Return the appropriate Sized_target structure. |
14bfc3f5 ILT |
288 | Sized_target<size, big_endian>* |
289 | sized_target() | |
274e99f9 | 290 | { |
5482377d ILT |
291 | return this->Object::sized_target SELECT_SIZE_ENDIAN_NAME ( |
292 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); | |
274e99f9 | 293 | } |
bae7f79e ILT |
294 | |
295 | private: | |
296 | // This object may not be copied. | |
297 | Sized_object(const Sized_object&); | |
298 | Sized_object& operator=(const Sized_object&); | |
299 | ||
a2fb1b05 ILT |
300 | // For convenience. |
301 | typedef Sized_object<size, big_endian> This; | |
302 | static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
303 | static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
304 | static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
75f65a3e ILT |
305 | typedef elfcpp::Shdr<size, big_endian> Shdr; |
306 | ||
307 | // Read the section header for section SHNUM. | |
308 | const unsigned char* | |
309 | section_header(unsigned int shnum); | |
a2fb1b05 ILT |
310 | |
311 | // Whether to include a section group in the link. | |
312 | bool | |
313 | include_section_group(Layout*, unsigned int, | |
314 | const elfcpp::Shdr<size, big_endian>&, | |
315 | std::vector<bool>*); | |
316 | ||
317 | // Whether to include a linkonce section in the link. | |
318 | bool | |
319 | include_linkonce_section(Layout*, const char*, | |
320 | const elfcpp::Shdr<size, big_endian>&); | |
321 | ||
61ba1cf9 ILT |
322 | // Views and sizes when relocating. |
323 | struct View_size | |
324 | { | |
325 | unsigned char* view; | |
326 | typename elfcpp::Elf_types<size>::Elf_Addr address; | |
327 | off_t offset; | |
328 | off_t view_size; | |
329 | }; | |
330 | ||
331 | typedef std::vector<View_size> Views; | |
332 | ||
333 | // Write section data to the output file. Record the views and | |
334 | // sizes in VIEWS for use when relocating. | |
335 | void | |
336 | write_sections(const unsigned char* pshdrs, Output_file*, Views*); | |
337 | ||
338 | // Relocate the sections in the output file. | |
339 | void | |
340 | relocate_sections(const Symbol_table*, const unsigned char* pshdrs, Views*); | |
341 | ||
342 | // Write out the local symbols. | |
343 | void | |
344 | write_local_symbols(Output_file*, const Stringpool*); | |
345 | ||
12e14209 ILT |
346 | // If non-NULL, a view of the section header data. |
347 | File_view* section_headers_; | |
bae7f79e ILT |
348 | // ELF file header e_flags field. |
349 | unsigned int flags_; | |
bae7f79e ILT |
350 | // File offset of section header table. |
351 | off_t shoff_; | |
bae7f79e ILT |
352 | // Offset of SHT_STRTAB section holding section names. |
353 | unsigned int shstrndx_; | |
354 | // Index of SHT_SYMTAB section. | |
355 | unsigned int symtab_shnum_; | |
61ba1cf9 ILT |
356 | // The number of local symbols. |
357 | unsigned int local_symbol_count_; | |
358 | // The number of local symbols which go into the output file. | |
359 | unsigned int output_local_symbol_count_; | |
14bfc3f5 ILT |
360 | // The entries in the symbol table for the external symbols. |
361 | Symbol** symbols_; | |
75f65a3e ILT |
362 | // File offset for local symbols. |
363 | off_t local_symbol_offset_; | |
61ba1cf9 ILT |
364 | // Values of local symbols. |
365 | typename elfcpp::Elf_types<size>::Elf_Addr *values_; | |
bae7f79e ILT |
366 | }; |
367 | ||
54dc6425 | 368 | // A class to manage the list of all objects. |
a2fb1b05 | 369 | |
54dc6425 ILT |
370 | class Input_objects |
371 | { | |
372 | public: | |
373 | Input_objects() | |
75f65a3e | 374 | : object_list_(), target_(NULL), any_dynamic_(false) |
54dc6425 ILT |
375 | { } |
376 | ||
377 | // The type of the list of input objects. | |
378 | typedef std::list<Object*> Object_list; | |
379 | ||
380 | // Add an object to the list. | |
381 | void | |
382 | add_object(Object*); | |
383 | ||
75f65a3e ILT |
384 | // Get the target we should use for the output file. |
385 | Target* | |
386 | target() const | |
387 | { return this->target_; } | |
388 | ||
54dc6425 ILT |
389 | // Iterate over all objects. |
390 | Object_list::const_iterator | |
391 | begin() const | |
392 | { return this->object_list_.begin(); } | |
393 | ||
394 | Object_list::const_iterator | |
395 | end() const | |
396 | { return this->object_list_.end(); } | |
397 | ||
398 | // Return whether we have seen any dynamic objects. | |
399 | bool | |
400 | any_dynamic() const | |
401 | { return this->any_dynamic_; } | |
402 | ||
403 | private: | |
404 | Input_objects(const Input_objects&); | |
405 | Input_objects& operator=(const Input_objects&); | |
406 | ||
407 | Object_list object_list_; | |
75f65a3e | 408 | Target* target_; |
54dc6425 ILT |
409 | bool any_dynamic_; |
410 | }; | |
a2fb1b05 | 411 | |
bae7f79e ILT |
412 | // Return an Object appropriate for the input file. P is BYTES long, |
413 | // and holds the ELF header. | |
414 | ||
61ba1cf9 ILT |
415 | extern Object* |
416 | make_elf_object(const std::string& name, Input_file*, | |
417 | off_t offset, const unsigned char* p, | |
418 | off_t bytes); | |
bae7f79e ILT |
419 | |
420 | } // end namespace gold | |
421 | ||
422 | #endif // !defined(GOLD_OBJECT_H) |