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