Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // object.cc -- support for an object file for linking in gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cerrno> | |
6 | #include <cstring> | |
7 | #include <cassert> | |
8 | ||
9 | #include "object.h" | |
14bfc3f5 | 10 | #include "target-select.h" |
bae7f79e ILT |
11 | |
12 | namespace gold | |
13 | { | |
14 | ||
15 | // Class Object. | |
16 | ||
17 | const unsigned char* | |
18 | Object::get_view(off_t start, off_t size) | |
19 | { | |
20 | return this->input_file_->file().get_view(start + this->offset_, size); | |
21 | } | |
22 | ||
23 | void | |
24 | Object::read(off_t start, off_t size, void* p) | |
25 | { | |
26 | this->input_file_->file().read(start + this->offset_, size, p); | |
27 | } | |
28 | ||
29 | File_view* | |
30 | Object::get_lasting_view(off_t start, off_t size) | |
31 | { | |
32 | return this->input_file_->file().get_lasting_view(start + this->offset_, | |
33 | size); | |
34 | } | |
35 | ||
36 | // Class Sized_object. | |
37 | ||
38 | template<int size, bool big_endian> | |
39 | Sized_object<size, big_endian>::Sized_object( | |
40 | const std::string& name, | |
41 | Input_file* input_file, | |
42 | off_t offset, | |
43 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
14bfc3f5 | 44 | : Object(name, input_file, false, offset), |
bae7f79e ILT |
45 | osabi_(ehdr.get_e_ident()[elfcpp::EI_OSABI]), |
46 | abiversion_(ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]), | |
47 | machine_(ehdr.get_e_machine()), | |
48 | flags_(ehdr.get_e_flags()), | |
bae7f79e ILT |
49 | shoff_(ehdr.get_e_shoff()), |
50 | shnum_(0), | |
51 | shstrndx_(0), | |
14bfc3f5 ILT |
52 | symtab_shnum_(0), |
53 | symbols_(NULL) | |
bae7f79e ILT |
54 | { |
55 | if (ehdr.get_e_ehsize() != elfcpp::Elf_sizes<size>::ehdr_size) | |
56 | { | |
57 | fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"), | |
58 | program_name, this->name().c_str(), ehdr.get_e_ehsize(), | |
59 | elfcpp::Elf_sizes<size>::ehdr_size); | |
60 | gold_exit(false); | |
61 | } | |
62 | if (ehdr.get_e_shentsize() != elfcpp::Elf_sizes<size>::shdr_size) | |
63 | { | |
64 | fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"), | |
65 | program_name, this->name().c_str(), ehdr.get_e_shentsize(), | |
66 | elfcpp::Elf_sizes<size>::shdr_size); | |
67 | gold_exit(false); | |
68 | } | |
69 | } | |
70 | ||
71 | template<int size, bool big_endian> | |
72 | Sized_object<size, big_endian>::~Sized_object() | |
73 | { | |
74 | } | |
75 | ||
76 | // Set up an object file bsaed on the file header. This sets up the | |
77 | // target and reads the section information. | |
78 | ||
79 | template<int size, bool big_endian> | |
80 | void | |
81 | Sized_object<size, big_endian>::setup( | |
82 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
83 | { | |
14bfc3f5 ILT |
84 | Target* target = select_target(this->machine_, size, big_endian, |
85 | this->osabi_, this->abiversion_); | |
86 | if (target == NULL) | |
87 | { | |
88 | fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"), | |
89 | program_name, this->name().c_str(), this->machine_); | |
90 | gold_exit(false); | |
91 | } | |
92 | this->set_target(target); | |
bae7f79e ILT |
93 | unsigned int shnum = ehdr.get_e_shnum(); |
94 | unsigned int shstrndx = ehdr.get_e_shstrndx(); | |
95 | if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX) | |
96 | && this->shoff_ != 0) | |
97 | { | |
98 | const unsigned char* p = this->get_view | |
99 | (this->shoff_, elfcpp::Elf_sizes<size>::shdr_size); | |
100 | elfcpp::Shdr<size, big_endian> shdr(p); | |
101 | if (shnum == 0) | |
102 | shnum = shdr.get_sh_size(); | |
103 | if (shstrndx == elfcpp::SHN_XINDEX) | |
104 | shstrndx = shdr.get_sh_link(); | |
105 | } | |
106 | this->shnum_ = shnum; | |
107 | this->shstrndx_ = shstrndx; | |
108 | ||
109 | if (shnum == 0) | |
110 | return; | |
111 | ||
112 | // Find the SHT_SYMTAB section. | |
113 | const unsigned char* p = this->get_view | |
114 | (this->shoff_, shnum * elfcpp::Elf_sizes<size>::shdr_size); | |
115 | // Skip the first section, which is always empty. | |
116 | p += elfcpp::Elf_sizes<size>::shdr_size; | |
117 | for (unsigned int i = 1; i < shnum; ++i) | |
118 | { | |
119 | elfcpp::Shdr<size, big_endian> shdr(p); | |
120 | if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB) | |
121 | { | |
122 | this->symtab_shnum_ = i; | |
123 | break; | |
124 | } | |
125 | p += elfcpp::Elf_sizes<size>::shdr_size; | |
126 | } | |
127 | } | |
128 | ||
129 | // Read the symbols and relocations from an object file. | |
130 | ||
131 | template<int size, bool big_endian> | |
132 | Read_symbols_data | |
133 | Sized_object<size, big_endian>::do_read_symbols() | |
134 | { | |
135 | if (this->symtab_shnum_ == 0) | |
136 | { | |
137 | // No symbol table. Weird but legal. | |
138 | Read_symbols_data ret; | |
139 | ret.symbols = NULL; | |
140 | ret.symbols_size = 0; | |
141 | ret.symbol_names = NULL; | |
142 | ret.symbol_names_size = 0; | |
143 | return ret; | |
144 | } | |
145 | ||
146 | int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
147 | ||
148 | // Read the symbol table section header. | |
149 | off_t symtabshdroff = this->shoff_ + (this->symtab_shnum_ * shdr_size); | |
150 | const unsigned char* psymtabshdr = this->get_view(symtabshdroff, shdr_size); | |
151 | elfcpp::Shdr<size, big_endian> symtabshdr(psymtabshdr); | |
152 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); | |
153 | ||
14bfc3f5 ILT |
154 | // We only need the external symbols. |
155 | int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
156 | off_t locsize = symtabshdr.get_sh_info() * sym_size; | |
157 | off_t extoff = symtabshdr.get_sh_offset() + locsize; | |
158 | off_t extsize = symtabshdr.get_sh_size() - locsize; | |
159 | ||
bae7f79e | 160 | // Read the symbol table. |
14bfc3f5 | 161 | File_view* fvsymtab = this->get_lasting_view(extoff, extsize); |
bae7f79e ILT |
162 | |
163 | // Read the section header for the symbol names. | |
164 | unsigned int strtab_shnum = symtabshdr.get_sh_link(); | |
165 | if (strtab_shnum == 0 || strtab_shnum >= this->shnum_) | |
166 | { | |
167 | fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"), | |
168 | program_name, this->name().c_str(), strtab_shnum); | |
169 | gold_exit(false); | |
170 | } | |
171 | off_t strtabshdroff = this->shoff_ + (strtab_shnum * shdr_size); | |
172 | const unsigned char *pstrtabshdr = this->get_view(strtabshdroff, shdr_size); | |
173 | elfcpp::Shdr<size, big_endian> strtabshdr(pstrtabshdr); | |
174 | if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB) | |
175 | { | |
176 | fprintf(stderr, | |
177 | _("%s: %s: symbol table name section has wrong type: %u\n"), | |
178 | program_name, this->name().c_str(), | |
179 | static_cast<unsigned int>(strtabshdr.get_sh_type())); | |
180 | gold_exit(false); | |
181 | } | |
182 | ||
183 | // Read the symbol names. | |
184 | File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(), | |
185 | strtabshdr.get_sh_size()); | |
186 | ||
187 | Read_symbols_data ret; | |
188 | ret.symbols = fvsymtab; | |
14bfc3f5 | 189 | ret.symbols_size = extsize; |
bae7f79e ILT |
190 | ret.symbol_names = fvstrtab; |
191 | ret.symbol_names_size = strtabshdr.get_sh_size(); | |
192 | ||
193 | return ret; | |
194 | } | |
195 | ||
196 | // Add the symbols to the symbol table. | |
197 | ||
198 | template<int size, bool big_endian> | |
199 | void | |
14bfc3f5 ILT |
200 | Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab, |
201 | Read_symbols_data sd) | |
bae7f79e ILT |
202 | { |
203 | if (sd.symbols == NULL) | |
204 | { | |
205 | assert(sd.symbol_names == NULL); | |
206 | return; | |
207 | } | |
208 | ||
14bfc3f5 ILT |
209 | unsigned int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
210 | size_t symcount = sd.symbols_size / sym_size; | |
211 | if (symcount * sym_size != sd.symbols_size) | |
bae7f79e | 212 | { |
14bfc3f5 ILT |
213 | fprintf(stderr, |
214 | _("%s: %s: size of symbols is not multiple of symbol size\n"), | |
215 | program_name, this->name().c_str()); | |
216 | gold_exit(false); | |
bae7f79e | 217 | } |
14bfc3f5 ILT |
218 | |
219 | this->symbols_ = new Symbol*[symcount]; | |
220 | ||
221 | const elfcpp::Sym<size, big_endian>* syms = | |
222 | reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data()); | |
223 | const char* sym_names = | |
224 | reinterpret_cast<const char*>(sd.symbol_names->data()); | |
225 | symtab->add_from_object(this, syms, symcount, sym_names, | |
226 | sd.symbol_names_size, this->symbols_); | |
bae7f79e ILT |
227 | } |
228 | ||
229 | } // End namespace gold. | |
230 | ||
231 | namespace | |
232 | { | |
233 | ||
234 | using namespace gold; | |
235 | ||
236 | // Read an ELF file with the header and return the appropriate | |
237 | // instance of Object. | |
238 | ||
239 | template<int size, bool big_endian> | |
240 | Object* | |
241 | make_elf_sized_object(const std::string& name, Input_file* input_file, | |
242 | off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) | |
243 | { | |
244 | int et = ehdr.get_e_type(); | |
245 | if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN) | |
246 | { | |
247 | fprintf(stderr, "%s: %s: unsupported ELF type %d\n", | |
248 | program_name, name.c_str(), static_cast<int>(et)); | |
249 | gold_exit(false); | |
250 | } | |
251 | ||
252 | if (et == elfcpp::ET_REL) | |
253 | { | |
254 | Sized_object<size, big_endian>* obj = | |
255 | new Sized_object<size, big_endian>(name, input_file, offset, ehdr); | |
256 | obj->setup(ehdr); | |
257 | return obj; | |
258 | } | |
259 | else | |
260 | { | |
261 | // elfcpp::ET_DYN | |
262 | fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"), | |
263 | program_name, name.c_str()); | |
264 | gold_exit(false); | |
265 | // Sized_dynobj<size, big_endian>* obj = | |
266 | // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file, | |
267 | // offset, ehdr); | |
268 | // obj->setup(ehdr); | |
269 | // return obj; | |
270 | } | |
271 | } | |
272 | ||
273 | } // End anonymous namespace. | |
274 | ||
275 | namespace gold | |
276 | { | |
277 | ||
278 | // Read an ELF file and return the appropriate instance of Object. | |
279 | ||
280 | Object* | |
281 | make_elf_object(const std::string& name, Input_file* input_file, off_t offset, | |
282 | const unsigned char* p, off_t bytes) | |
283 | { | |
284 | if (bytes < elfcpp::EI_NIDENT) | |
285 | { | |
286 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
287 | program_name, name.c_str()); | |
288 | gold_exit(false); | |
289 | } | |
290 | ||
291 | int v = p[elfcpp::EI_VERSION]; | |
292 | if (v != elfcpp::EV_CURRENT) | |
293 | { | |
294 | if (v == elfcpp::EV_NONE) | |
295 | fprintf(stderr, _("%s: %s: invalid ELF version 0\n"), | |
296 | program_name, name.c_str()); | |
297 | else | |
298 | fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"), | |
299 | program_name, name.c_str(), v); | |
300 | gold_exit(false); | |
301 | } | |
302 | ||
303 | int c = p[elfcpp::EI_CLASS]; | |
304 | if (c == elfcpp::ELFCLASSNONE) | |
305 | { | |
306 | fprintf(stderr, _("%s: %s: invalid ELF class 0\n"), | |
307 | program_name, name.c_str()); | |
308 | gold_exit(false); | |
309 | } | |
310 | else if (c != elfcpp::ELFCLASS32 | |
311 | && c != elfcpp::ELFCLASS64) | |
312 | { | |
313 | fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"), | |
314 | program_name, name.c_str(), c); | |
315 | gold_exit(false); | |
316 | } | |
317 | ||
318 | int d = p[elfcpp::EI_DATA]; | |
319 | if (d == elfcpp::ELFDATANONE) | |
320 | { | |
321 | fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"), | |
322 | program_name, name.c_str()); | |
323 | gold_exit(false); | |
324 | } | |
325 | else if (d != elfcpp::ELFDATA2LSB | |
326 | && d != elfcpp::ELFDATA2MSB) | |
327 | { | |
328 | fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"), | |
329 | program_name, name.c_str(), d); | |
330 | gold_exit(false); | |
331 | } | |
332 | ||
333 | bool big_endian = d == elfcpp::ELFDATA2MSB; | |
334 | ||
335 | if (c == elfcpp::ELFCLASS32) | |
336 | { | |
337 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
338 | { | |
339 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
340 | program_name, name.c_str()); | |
341 | gold_exit(false); | |
342 | } | |
343 | if (big_endian) | |
344 | { | |
345 | elfcpp::Ehdr<32, true> ehdr(p); | |
346 | return make_elf_sized_object<32, true>(name, input_file, | |
347 | offset, ehdr); | |
348 | } | |
349 | else | |
350 | { | |
351 | elfcpp::Ehdr<32, false> ehdr(p); | |
352 | return make_elf_sized_object<32, false>(name, input_file, | |
353 | offset, ehdr); | |
354 | } | |
355 | } | |
356 | else | |
357 | { | |
358 | if (bytes < elfcpp::Elf_sizes<32>::ehdr_size) | |
359 | { | |
360 | fprintf(stderr, _("%s: %s: ELF file too short\n"), | |
361 | program_name, name.c_str()); | |
362 | gold_exit(false); | |
363 | } | |
364 | if (big_endian) | |
365 | { | |
366 | elfcpp::Ehdr<64, true> ehdr(p); | |
367 | return make_elf_sized_object<64, true>(name, input_file, | |
368 | offset, ehdr); | |
369 | } | |
370 | else | |
371 | { | |
372 | elfcpp::Ehdr<64, false> ehdr(p); | |
373 | return make_elf_sized_object<64, false>(name, input_file, | |
374 | offset, ehdr); | |
375 | } | |
376 | } | |
377 | } | |
378 | ||
379 | // Instantiate the templates we need. We could use the configure | |
380 | // script to restrict this to only the ones for implemented targets. | |
381 | ||
382 | template | |
383 | class Sized_object<32, false>; | |
384 | ||
385 | template | |
386 | class Sized_object<32, true>; | |
387 | ||
388 | template | |
389 | class Sized_object<64, false>; | |
390 | ||
391 | template | |
392 | class Sized_object<64, true>; | |
393 | ||
394 | } // End namespace gold. |