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