1 // reloc.cc -- relocate input files for gold.
13 // Read_relocs methods.
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.
19 Task::Is_runnable_type
20 Read_relocs::is_runnable(Workqueue*)
22 return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE;
28 Read_relocs::locks(Workqueue*)
30 return new Task_locker_obj<Object>(*this->object_);
33 // Read the relocations and then start a Scan_relocs_task.
36 Read_relocs::run(Workqueue* workqueue)
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_,
41 this->layout_, this->object_, rd,
42 this->symtab_lock_, this->blocker_));
45 // Scan_relocs methods.
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
52 Task::Is_runnable_type
53 Scan_relocs::is_runnable(Workqueue*)
55 if (!this->symtab_lock_->is_writable() || this->object_->is_locked())
60 // Return the locks we hold: one on the file, one on the symbol table
63 class Scan_relocs::Scan_relocs_locker : public Task_locker
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)
73 Task_locker_obj<Object> objlock_;
74 Task_locker_write symtab_locker_;
75 Task_locker_block blocker_;
79 Scan_relocs::locks(Workqueue* workqueue)
81 return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this,
82 *this->blocker_, workqueue);
88 Scan_relocs::run(Workqueue*)
90 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
96 // Relocate_task methods.
98 // These tasks are always runnable.
100 Task::Is_runnable_type
101 Relocate_task::is_runnable(Workqueue*)
106 // We want to lock the file while we run. We want to unblock
107 // FINAL_BLOCKER when we are done.
109 class Relocate_task::Relocate_locker : public Task_locker
112 Relocate_locker(Task_token& token, Workqueue* workqueue,
114 : blocker_(token, workqueue), objlock_(*object)
118 Task_locker_block blocker_;
119 Task_locker_obj<Object> objlock_;
123 Relocate_task::locks(Workqueue* workqueue)
125 return new Relocate_locker(*this->final_blocker_, workqueue,
132 Relocate_task::run(Workqueue*)
134 this->object_->relocate(this->options_, this->symtab_, this->layout_,
138 // Read the relocs and local symbols from the object file and store
139 // the information in RD.
141 template<int size, bool big_endian>
143 Sized_object<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
147 unsigned int shnum = this->shnum();
151 rd->relocs.reserve(shnum / 2);
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)
159 typename This::Shdr shdr(ps);
161 unsigned int sh_type = shdr.get_sh_type();
162 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
165 unsigned int shndx = shdr.get_sh_info();
168 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
169 program_name, this->name().c_str(), i, shndx);
173 if (!this->is_section_included(shndx))
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)
184 if (shdr.get_sh_link() != this->symtab_shnum_)
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());
193 off_t sh_size = shdr.get_sh_size();
195 unsigned int reloc_size;
196 if (sh_type == elfcpp::SHT_REL)
197 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
199 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
200 if (reloc_size != shdr.get_sh_entsize())
203 _("%s: %s: unexpected entsize for reloc section %u: "
205 program_name, this->name().c_str(), i,
206 static_cast<unsigned long>(shdr.get_sh_entsize()),
211 size_t reloc_count = sh_size / reloc_size;
212 if (reloc_count * reloc_size != sh_size)
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));
220 rd->relocs.push_back(Section_relocs());
221 Section_relocs& sr(rd->relocs.back());
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;
229 // Read the local symbols.
230 if (this->symtab_shnum_ == 0 || this->local_symbol_count_ == 0)
231 rd->local_symbols = NULL;
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(),
246 // Scan the relocs and adjust the symbol table. This looks for
247 // relocations which require GOT/PLT/COPY relocations.
249 template<int size, bool big_endian>
251 Sized_object<size, big_endian>::do_scan_relocs(const General_options& options,
252 Symbol_table* symtab,
254 Read_relocs_data* rd)
256 Sized_target<size, big_endian>* target = this->sized_target();
258 const unsigned char* local_symbols;
259 if (rd->local_symbols == NULL)
260 local_symbols = NULL;
262 local_symbols = rd->local_symbols->data();
264 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
265 p != rd->relocs.end();
268 target->scan_relocs(options, symtab, layout, this, p->sh_type,
269 p->contents->data(), p->reloc_count,
270 this->local_symbol_count_,
277 if (rd->local_symbols != NULL)
279 delete rd->local_symbols;
280 rd->local_symbols = NULL;
284 // Relocate the input sections and write out the local symbols.
286 template<int size, bool big_endian>
288 Sized_object<size, big_endian>::do_relocate(const General_options& options,
289 const Symbol_table* symtab,
290 const Layout* layout,
293 unsigned int shnum = this->shnum();
295 // Read the section headers.
296 const unsigned char* pshdrs = this->get_view(this->shoff_,
297 shnum * This::shdr_size);
302 // Make two passes over the sections. The first one copies the
303 // section data to the output file. The second one applies
306 this->write_sections(pshdrs, of, &views);
308 // Apply relocations.
310 this->relocate_sections(options, symtab, layout, pshdrs, &views);
312 // Write out the accumulated views.
313 for (unsigned int i = 1; i < shnum; ++i)
315 if (views[i].view != NULL)
316 of->write_output_view(views[i].offset, views[i].view_size,
320 // Write out the local symbols.
321 this->write_local_symbols(of, layout->sympool());
324 // Write section data to the output file. PSHDRS points to the
325 // section headers. Record the views in *PVIEWS for use when
328 template<int size, bool big_endian>
330 Sized_object<size, big_endian>::write_sections(const unsigned char* pshdrs,
334 unsigned int shnum = this->shnum();
335 std::vector<Map_to_output>& map_sections(this->map_to_output());
337 const unsigned char* p = pshdrs + This::shdr_size;
338 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
340 View_size* pvs = &(*pviews)[i];
344 const Output_section* os = map_sections[i].output_section;
348 typename This::Shdr shdr(p);
350 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
353 off_t start = os->offset() + map_sections[i].offset;
354 off_t sh_size = shdr.get_sh_size();
359 assert(map_sections[i].offset >= 0
360 && map_sections[i].offset + sh_size <= os->data_size());
362 unsigned char* view = of->get_output_view(start, sh_size);
363 this->read(shdr.get_sh_offset(), sh_size, view);
366 pvs->address = os->address() + map_sections[i].offset;
368 pvs->view_size = sh_size;
372 // Relocate section data. VIEWS points to the section data as views
373 // in the output file.
375 template<int size, bool big_endian>
377 Sized_object<size, big_endian>::relocate_sections(
378 const General_options& options,
379 const Symbol_table* symtab,
380 const Layout* layout,
381 const unsigned char* pshdrs,
384 unsigned int shnum = this->shnum();
385 Sized_target<size, big_endian>* target = this->sized_target();
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_;
396 const unsigned char* p = pshdrs + This::shdr_size;
397 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
399 typename This::Shdr shdr(p);
401 unsigned int sh_type = shdr.get_sh_type();
402 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
405 unsigned int index = shdr.get_sh_info();
406 if (index >= this->shnum())
408 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
409 program_name, this->name().c_str(), i, index);
413 if (!this->is_section_included(index))
415 // This relocation section is against a section which we
420 assert((*pviews)[index].view != NULL);
422 if (shdr.get_sh_link() != this->symtab_shnum_)
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());
431 off_t sh_size = shdr.get_sh_size();
432 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
435 unsigned int reloc_size;
436 if (sh_type == elfcpp::SHT_REL)
437 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
439 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
441 if (reloc_size != shdr.get_sh_entsize())
444 _("%s: %s: unexpected entsize for reloc section %u: "
446 program_name, this->name().c_str(), i,
447 static_cast<unsigned long>(shdr.get_sh_entsize()),
452 size_t reloc_count = sh_size / reloc_size;
453 if (reloc_count * reloc_size != sh_size)
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));
461 relinfo.reloc_shndx = i;
462 relinfo.data_shndx = index;
463 target->relocate_section(&relinfo,
467 (*pviews)[index].view,
468 (*pviews)[index].address,
469 (*pviews)[index].view_size);
473 // Instantiate the templates we need. We could use the configure
474 // script to restrict this to only the ones for implemented targets.
478 Sized_object<32, false>::do_read_relocs(Read_relocs_data* rd);
482 Sized_object<32, true>::do_read_relocs(Read_relocs_data* rd);
486 Sized_object<64, false>::do_read_relocs(Read_relocs_data* rd);
490 Sized_object<64, true>::do_read_relocs(Read_relocs_data* rd);
494 Sized_object<32, false>::do_scan_relocs(const General_options& options,
495 Symbol_table* symtab,
497 Read_relocs_data* rd);
501 Sized_object<32, true>::do_scan_relocs(const General_options& options,
502 Symbol_table* symtab,
504 Read_relocs_data* rd);
508 Sized_object<64, false>::do_scan_relocs(const General_options& options,
509 Symbol_table* symtab,
511 Read_relocs_data* rd);
515 Sized_object<64, true>::do_scan_relocs(const General_options& options,
516 Symbol_table* symtab,
518 Read_relocs_data* rd);
522 Sized_object<32, false>::do_relocate(const General_options& options,
523 const Symbol_table* symtab,
524 const Layout* layout,
529 Sized_object<32, true>::do_relocate(const General_options& options,
530 const Symbol_table* symtab,
531 const Layout* layout,
536 Sized_object<64, false>::do_relocate(const General_options& options,
537 const Symbol_table* symtab,
538 const Layout* layout,
543 Sized_object<64, true>::do_relocate(const General_options& options,
544 const Symbol_table* symtab,
545 const Layout* layout,
549 } // End namespace gold.