Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // reloc.cc -- relocate input files for gold. |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include "workqueue.h" | |
6 | #include "object.h" | |
7 | #include "output.h" | |
8 | #include "reloc.h" | |
9 | ||
10 | namespace gold | |
11 | { | |
12 | ||
92e059d8 ILT |
13 | // Read_relocs methods. |
14 | ||
15 | // These tasks just read the relocation information from the file. | |
16 | // After reading it, the start another task to process the | |
17 | // information. These tasks requires access to the file. | |
18 | ||
19 | Task::Is_runnable_type | |
20 | Read_relocs::is_runnable(Workqueue*) | |
21 | { | |
22 | return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE; | |
23 | } | |
24 | ||
25 | // Lock the file. | |
26 | ||
27 | Task_locker* | |
28 | Read_relocs::locks(Workqueue*) | |
29 | { | |
30 | return new Task_locker_obj<Object>(*this->object_); | |
31 | } | |
32 | ||
33 | // Read the relocations and then start a Scan_relocs_task. | |
34 | ||
35 | void | |
36 | Read_relocs::run(Workqueue* workqueue) | |
37 | { | |
38 | Read_relocs_data *rd = new Read_relocs_data; | |
39 | this->object_->read_relocs(rd); | |
40 | workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_, | |
ead1e424 ILT |
41 | this->layout_, this->object_, rd, |
42 | this->symtab_lock_, this->blocker_)); | |
92e059d8 ILT |
43 | } |
44 | ||
45 | // Scan_relocs methods. | |
46 | ||
47 | // These tasks scan the relocations read by Read_relocs and mark up | |
48 | // the symbol table to indicate which relocations are required. We | |
49 | // use a lock on the symbol table to keep them from interfering with | |
50 | // each other. | |
51 | ||
52 | Task::Is_runnable_type | |
53 | Scan_relocs::is_runnable(Workqueue*) | |
54 | { | |
ead1e424 ILT |
55 | if (!this->symtab_lock_->is_writable() || this->object_->is_locked()) |
56 | return IS_LOCKED; | |
57 | return IS_RUNNABLE; | |
92e059d8 ILT |
58 | } |
59 | ||
60 | // Return the locks we hold: one on the file, one on the symbol table | |
61 | // and one blocker. | |
62 | ||
63 | class Scan_relocs::Scan_relocs_locker : public Task_locker | |
64 | { | |
65 | public: | |
66 | Scan_relocs_locker(Object* object, Task_token& symtab_lock, Task* task, | |
67 | Task_token& blocker, Workqueue* workqueue) | |
68 | : objlock_(*object), symtab_locker_(symtab_lock, task), | |
69 | blocker_(blocker, workqueue) | |
70 | { } | |
71 | ||
72 | private: | |
73 | Task_locker_obj<Object> objlock_; | |
74 | Task_locker_write symtab_locker_; | |
75 | Task_locker_block blocker_; | |
76 | }; | |
77 | ||
78 | Task_locker* | |
79 | Scan_relocs::locks(Workqueue* workqueue) | |
80 | { | |
81 | return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this, | |
82 | *this->blocker_, workqueue); | |
83 | } | |
84 | ||
85 | // Scan the relocs. | |
86 | ||
87 | void | |
88 | Scan_relocs::run(Workqueue*) | |
89 | { | |
ead1e424 ILT |
90 | this->object_->scan_relocs(this->options_, this->symtab_, this->layout_, |
91 | this->rd_); | |
92e059d8 ILT |
92 | delete this->rd_; |
93 | this->rd_ = NULL; | |
94 | } | |
95 | ||
61ba1cf9 ILT |
96 | // Relocate_task methods. |
97 | ||
98 | // These tasks are always runnable. | |
99 | ||
100 | Task::Is_runnable_type | |
101 | Relocate_task::is_runnable(Workqueue*) | |
102 | { | |
103 | return IS_RUNNABLE; | |
104 | } | |
105 | ||
106 | // We want to lock the file while we run. We want to unblock | |
107 | // FINAL_BLOCKER when we are done. | |
108 | ||
109 | class Relocate_task::Relocate_locker : public Task_locker | |
110 | { | |
111 | public: | |
112 | Relocate_locker(Task_token& token, Workqueue* workqueue, | |
113 | Object* object) | |
114 | : blocker_(token, workqueue), objlock_(*object) | |
115 | { } | |
116 | ||
117 | private: | |
118 | Task_locker_block blocker_; | |
119 | Task_locker_obj<Object> objlock_; | |
120 | }; | |
121 | ||
122 | Task_locker* | |
123 | Relocate_task::locks(Workqueue* workqueue) | |
124 | { | |
125 | return new Relocate_locker(*this->final_blocker_, workqueue, | |
126 | this->object_); | |
127 | } | |
128 | ||
129 | // Run the task. | |
130 | ||
131 | void | |
132 | Relocate_task::run(Workqueue*) | |
133 | { | |
92e059d8 | 134 | this->object_->relocate(this->options_, this->symtab_, this->layout_, |
61ba1cf9 ILT |
135 | this->of_); |
136 | } | |
137 | ||
92e059d8 ILT |
138 | // Read the relocs and local symbols from the object file and store |
139 | // the information in RD. | |
140 | ||
141 | template<int size, bool big_endian> | |
142 | void | |
f6ce93d6 | 143 | Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd) |
92e059d8 ILT |
144 | { |
145 | rd->relocs.clear(); | |
146 | ||
147 | unsigned int shnum = this->shnum(); | |
148 | if (shnum == 0) | |
149 | return; | |
150 | ||
151 | rd->relocs.reserve(shnum / 2); | |
152 | ||
153 | const unsigned char *pshdrs = this->get_view(this->shoff_, | |
154 | shnum * This::shdr_size); | |
155 | // Skip the first, dummy, section. | |
156 | const unsigned char *ps = pshdrs + This::shdr_size; | |
157 | for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size) | |
158 | { | |
159 | typename This::Shdr shdr(ps); | |
160 | ||
161 | unsigned int sh_type = shdr.get_sh_type(); | |
162 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
163 | continue; | |
164 | ||
165 | unsigned int shndx = shdr.get_sh_info(); | |
166 | if (shndx >= shnum) | |
167 | { | |
168 | fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"), | |
169 | program_name, this->name().c_str(), i, shndx); | |
170 | gold_exit(false); | |
171 | } | |
172 | ||
173 | if (!this->is_section_included(shndx)) | |
174 | continue; | |
175 | ||
ead1e424 ILT |
176 | // We are scanning relocations in order to fill out the GOT and |
177 | // PLT sections. Relocations for sections which are not | |
178 | // allocated (typically debugging sections) should not add new | |
179 | // GOT and PLT entries. So we skip them. | |
180 | typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); | |
181 | if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
182 | continue; | |
183 | ||
92e059d8 ILT |
184 | if (shdr.get_sh_link() != this->symtab_shnum_) |
185 | { | |
186 | fprintf(stderr, | |
187 | _("%s: %s: relocation section %u uses unexpected " | |
188 | "symbol table %u\n"), | |
189 | program_name, this->name().c_str(), i, shdr.get_sh_link()); | |
190 | gold_exit(false); | |
191 | } | |
192 | ||
193 | off_t sh_size = shdr.get_sh_size(); | |
194 | ||
195 | unsigned int reloc_size; | |
196 | if (sh_type == elfcpp::SHT_REL) | |
197 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
198 | else | |
199 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
200 | if (reloc_size != shdr.get_sh_entsize()) | |
201 | { | |
202 | fprintf(stderr, | |
203 | _("%s: %s: unexpected entsize for reloc section %u: " | |
204 | "%lu != %u"), | |
205 | program_name, this->name().c_str(), i, | |
206 | static_cast<unsigned long>(shdr.get_sh_entsize()), | |
207 | reloc_size); | |
208 | gold_exit(false); | |
209 | } | |
210 | ||
211 | size_t reloc_count = sh_size / reloc_size; | |
212 | if (reloc_count * reloc_size != sh_size) | |
213 | { | |
214 | fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"), | |
215 | program_name, this->name().c_str(), i, | |
216 | static_cast<unsigned long>(sh_size)); | |
217 | gold_exit(false); | |
218 | } | |
219 | ||
220 | rd->relocs.push_back(Section_relocs()); | |
221 | Section_relocs& sr(rd->relocs.back()); | |
222 | sr.reloc_shndx = i; | |
223 | sr.data_shndx = shndx; | |
224 | sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size); | |
225 | sr.sh_type = sh_type; | |
226 | sr.reloc_count = reloc_count; | |
227 | } | |
228 | ||
229 | // Read the local symbols. | |
230 | if (this->symtab_shnum_ == 0 || this->local_symbol_count_ == 0) | |
231 | rd->local_symbols = NULL; | |
232 | else | |
233 | { | |
234 | typename This::Shdr symtabshdr(pshdrs | |
235 | + this->symtab_shnum_ * This::shdr_size); | |
236 | assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); | |
237 | const int sym_size = This::sym_size; | |
238 | const unsigned int loccount = this->local_symbol_count_; | |
239 | assert(loccount == symtabshdr.get_sh_info()); | |
240 | off_t locsize = loccount * sym_size; | |
241 | rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), | |
242 | locsize); | |
243 | } | |
244 | } | |
245 | ||
246 | // Scan the relocs and adjust the symbol table. This looks for | |
247 | // relocations which require GOT/PLT/COPY relocations. | |
248 | ||
249 | template<int size, bool big_endian> | |
250 | void | |
f6ce93d6 | 251 | Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options, |
92e059d8 | 252 | Symbol_table* symtab, |
ead1e424 | 253 | Layout* layout, |
92e059d8 ILT |
254 | Read_relocs_data* rd) |
255 | { | |
256 | Sized_target<size, big_endian>* target = this->sized_target(); | |
257 | ||
258 | const unsigned char* local_symbols; | |
259 | if (rd->local_symbols == NULL) | |
260 | local_symbols = NULL; | |
261 | else | |
262 | local_symbols = rd->local_symbols->data(); | |
263 | ||
264 | for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); | |
265 | p != rd->relocs.end(); | |
266 | ++p) | |
267 | { | |
ead1e424 | 268 | target->scan_relocs(options, symtab, layout, this, p->sh_type, |
92e059d8 ILT |
269 | p->contents->data(), p->reloc_count, |
270 | this->local_symbol_count_, | |
271 | local_symbols, | |
272 | this->symbols_); | |
273 | delete p->contents; | |
274 | p->contents = NULL; | |
275 | } | |
276 | ||
277 | if (rd->local_symbols != NULL) | |
278 | { | |
279 | delete rd->local_symbols; | |
280 | rd->local_symbols = NULL; | |
281 | } | |
282 | } | |
283 | ||
61ba1cf9 ILT |
284 | // Relocate the input sections and write out the local symbols. |
285 | ||
286 | template<int size, bool big_endian> | |
287 | void | |
f6ce93d6 | 288 | Sized_relobj<size, big_endian>::do_relocate(const General_options& options, |
61ba1cf9 | 289 | const Symbol_table* symtab, |
92e059d8 | 290 | const Layout* layout, |
61ba1cf9 ILT |
291 | Output_file* of) |
292 | { | |
293 | unsigned int shnum = this->shnum(); | |
294 | ||
295 | // Read the section headers. | |
296 | const unsigned char* pshdrs = this->get_view(this->shoff_, | |
297 | shnum * This::shdr_size); | |
298 | ||
299 | Views views; | |
300 | views.resize(shnum); | |
301 | ||
302 | // Make two passes over the sections. The first one copies the | |
303 | // section data to the output file. The second one applies | |
304 | // relocations. | |
305 | ||
306 | this->write_sections(pshdrs, of, &views); | |
307 | ||
308 | // Apply relocations. | |
309 | ||
92e059d8 | 310 | this->relocate_sections(options, symtab, layout, pshdrs, &views); |
61ba1cf9 ILT |
311 | |
312 | // Write out the accumulated views. | |
313 | for (unsigned int i = 1; i < shnum; ++i) | |
314 | { | |
315 | if (views[i].view != NULL) | |
316 | of->write_output_view(views[i].offset, views[i].view_size, | |
317 | views[i].view); | |
318 | } | |
319 | ||
320 | // Write out the local symbols. | |
92e059d8 | 321 | this->write_local_symbols(of, layout->sympool()); |
61ba1cf9 ILT |
322 | } |
323 | ||
324 | // Write section data to the output file. PSHDRS points to the | |
325 | // section headers. Record the views in *PVIEWS for use when | |
326 | // relocating. | |
327 | ||
328 | template<int size, bool big_endian> | |
329 | void | |
f6ce93d6 | 330 | Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs, |
61ba1cf9 ILT |
331 | Output_file* of, |
332 | Views* pviews) | |
333 | { | |
334 | unsigned int shnum = this->shnum(); | |
335 | std::vector<Map_to_output>& map_sections(this->map_to_output()); | |
336 | ||
337 | const unsigned char* p = pshdrs + This::shdr_size; | |
338 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
339 | { | |
340 | View_size* pvs = &(*pviews)[i]; | |
341 | ||
342 | pvs->view = NULL; | |
343 | ||
344 | const Output_section* os = map_sections[i].output_section; | |
345 | if (os == NULL) | |
346 | continue; | |
347 | ||
348 | typename This::Shdr shdr(p); | |
349 | ||
350 | if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) | |
351 | continue; | |
352 | ||
61ba1cf9 ILT |
353 | off_t start = os->offset() + map_sections[i].offset; |
354 | off_t sh_size = shdr.get_sh_size(); | |
355 | ||
ead1e424 ILT |
356 | if (sh_size == 0) |
357 | continue; | |
358 | ||
359 | assert(map_sections[i].offset >= 0 | |
360 | && map_sections[i].offset + sh_size <= os->data_size()); | |
361 | ||
61ba1cf9 | 362 | unsigned char* view = of->get_output_view(start, sh_size); |
92e059d8 ILT |
363 | this->read(shdr.get_sh_offset(), sh_size, view); |
364 | ||
61ba1cf9 ILT |
365 | pvs->view = view; |
366 | pvs->address = os->address() + map_sections[i].offset; | |
367 | pvs->offset = start; | |
368 | pvs->view_size = sh_size; | |
369 | } | |
370 | } | |
371 | ||
372 | // Relocate section data. VIEWS points to the section data as views | |
373 | // in the output file. | |
374 | ||
375 | template<int size, bool big_endian> | |
376 | void | |
f6ce93d6 | 377 | Sized_relobj<size, big_endian>::relocate_sections( |
92e059d8 ILT |
378 | const General_options& options, |
379 | const Symbol_table* symtab, | |
380 | const Layout* layout, | |
381 | const unsigned char* pshdrs, | |
382 | Views* pviews) | |
61ba1cf9 ILT |
383 | { |
384 | unsigned int shnum = this->shnum(); | |
61ba1cf9 ILT |
385 | Sized_target<size, big_endian>* target = this->sized_target(); |
386 | ||
92e059d8 ILT |
387 | Relocate_info<size, big_endian> relinfo; |
388 | relinfo.options = &options; | |
389 | relinfo.symtab = symtab; | |
390 | relinfo.layout = layout; | |
391 | relinfo.object = this; | |
392 | relinfo.local_symbol_count = this->local_symbol_count_; | |
393 | relinfo.values = this->values_; | |
394 | relinfo.symbols = this->symbols_; | |
395 | ||
61ba1cf9 ILT |
396 | const unsigned char* p = pshdrs + This::shdr_size; |
397 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
398 | { | |
399 | typename This::Shdr shdr(p); | |
400 | ||
401 | unsigned int sh_type = shdr.get_sh_type(); | |
402 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
403 | continue; | |
404 | ||
405 | unsigned int index = shdr.get_sh_info(); | |
406 | if (index >= this->shnum()) | |
407 | { | |
408 | fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"), | |
409 | program_name, this->name().c_str(), i, index); | |
410 | gold_exit(false); | |
411 | } | |
412 | ||
92e059d8 | 413 | if (!this->is_section_included(index)) |
61ba1cf9 ILT |
414 | { |
415 | // This relocation section is against a section which we | |
416 | // discarded. | |
417 | continue; | |
418 | } | |
419 | ||
420 | assert((*pviews)[index].view != NULL); | |
421 | ||
422 | if (shdr.get_sh_link() != this->symtab_shnum_) | |
423 | { | |
424 | fprintf(stderr, | |
425 | _("%s: %s: relocation section %u uses unexpected " | |
426 | "symbol table %u\n"), | |
427 | program_name, this->name().c_str(), i, shdr.get_sh_link()); | |
428 | gold_exit(false); | |
429 | } | |
430 | ||
431 | off_t sh_size = shdr.get_sh_size(); | |
432 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), | |
433 | sh_size); | |
434 | ||
435 | unsigned int reloc_size; | |
436 | if (sh_type == elfcpp::SHT_REL) | |
437 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
438 | else | |
439 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
440 | ||
441 | if (reloc_size != shdr.get_sh_entsize()) | |
442 | { | |
443 | fprintf(stderr, | |
444 | _("%s: %s: unexpected entsize for reloc section %u: " | |
445 | "%lu != %u"), | |
446 | program_name, this->name().c_str(), i, | |
447 | static_cast<unsigned long>(shdr.get_sh_entsize()), | |
448 | reloc_size); | |
449 | gold_exit(false); | |
450 | } | |
451 | ||
452 | size_t reloc_count = sh_size / reloc_size; | |
453 | if (reloc_count * reloc_size != sh_size) | |
454 | { | |
455 | fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"), | |
456 | program_name, this->name().c_str(), i, | |
457 | static_cast<unsigned long>(sh_size)); | |
458 | gold_exit(false); | |
459 | } | |
460 | ||
92e059d8 ILT |
461 | relinfo.reloc_shndx = i; |
462 | relinfo.data_shndx = index; | |
463 | target->relocate_section(&relinfo, | |
464 | sh_type, | |
465 | prelocs, | |
466 | reloc_count, | |
61ba1cf9 ILT |
467 | (*pviews)[index].view, |
468 | (*pviews)[index].address, | |
469 | (*pviews)[index].view_size); | |
470 | } | |
471 | } | |
472 | ||
473 | // Instantiate the templates we need. We could use the configure | |
474 | // script to restrict this to only the ones for implemented targets. | |
475 | ||
92e059d8 ILT |
476 | template |
477 | void | |
f6ce93d6 | 478 | Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd); |
92e059d8 ILT |
479 | |
480 | template | |
481 | void | |
f6ce93d6 | 482 | Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd); |
92e059d8 ILT |
483 | |
484 | template | |
485 | void | |
f6ce93d6 | 486 | Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd); |
92e059d8 ILT |
487 | |
488 | template | |
489 | void | |
f6ce93d6 | 490 | Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd); |
92e059d8 ILT |
491 | |
492 | template | |
493 | void | |
f6ce93d6 | 494 | Sized_relobj<32, false>::do_scan_relocs(const General_options& options, |
92e059d8 | 495 | Symbol_table* symtab, |
ead1e424 | 496 | Layout* layout, |
92e059d8 ILT |
497 | Read_relocs_data* rd); |
498 | ||
499 | template | |
500 | void | |
f6ce93d6 | 501 | Sized_relobj<32, true>::do_scan_relocs(const General_options& options, |
92e059d8 | 502 | Symbol_table* symtab, |
ead1e424 | 503 | Layout* layout, |
92e059d8 ILT |
504 | Read_relocs_data* rd); |
505 | ||
506 | template | |
507 | void | |
f6ce93d6 | 508 | Sized_relobj<64, false>::do_scan_relocs(const General_options& options, |
92e059d8 | 509 | Symbol_table* symtab, |
ead1e424 | 510 | Layout* layout, |
92e059d8 ILT |
511 | Read_relocs_data* rd); |
512 | ||
513 | template | |
514 | void | |
f6ce93d6 | 515 | Sized_relobj<64, true>::do_scan_relocs(const General_options& options, |
92e059d8 | 516 | Symbol_table* symtab, |
ead1e424 | 517 | Layout* layout, |
92e059d8 ILT |
518 | Read_relocs_data* rd); |
519 | ||
61ba1cf9 ILT |
520 | template |
521 | void | |
f6ce93d6 | 522 | Sized_relobj<32, false>::do_relocate(const General_options& options, |
61ba1cf9 | 523 | const Symbol_table* symtab, |
92e059d8 | 524 | const Layout* layout, |
61ba1cf9 ILT |
525 | Output_file* of); |
526 | ||
527 | template | |
528 | void | |
f6ce93d6 | 529 | Sized_relobj<32, true>::do_relocate(const General_options& options, |
61ba1cf9 | 530 | const Symbol_table* symtab, |
92e059d8 | 531 | const Layout* layout, |
61ba1cf9 ILT |
532 | Output_file* of); |
533 | ||
534 | template | |
535 | void | |
f6ce93d6 | 536 | Sized_relobj<64, false>::do_relocate(const General_options& options, |
61ba1cf9 | 537 | const Symbol_table* symtab, |
92e059d8 | 538 | const Layout* layout, |
61ba1cf9 ILT |
539 | Output_file* of); |
540 | ||
541 | template | |
542 | void | |
f6ce93d6 | 543 | Sized_relobj<64, true>::do_relocate(const General_options& options, |
61ba1cf9 | 544 | const Symbol_table* symtab, |
92e059d8 | 545 | const Layout* layout, |
61ba1cf9 ILT |
546 | Output_file* of); |
547 | ||
548 | ||
549 | } // End namespace gold. |