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_relobj
<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->elf_file_
.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_shndx_
)
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 gold_assert(this->symtab_shndx_
!= -1U);
231 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
232 rd
->local_symbols
= NULL
;
235 typename
This::Shdr
symtabshdr(pshdrs
236 + this->symtab_shndx_
* This::shdr_size
);
237 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
238 const int sym_size
= This::sym_size
;
239 const unsigned int loccount
= this->local_symbol_count_
;
240 gold_assert(loccount
== symtabshdr
.get_sh_info());
241 off_t locsize
= loccount
* sym_size
;
242 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
247 // Scan the relocs and adjust the symbol table. This looks for
248 // relocations which require GOT/PLT/COPY relocations.
250 template<int size
, bool big_endian
>
252 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
253 Symbol_table
* symtab
,
255 Read_relocs_data
* rd
)
257 Sized_target
<size
, big_endian
>* target
= this->sized_target();
259 const unsigned char* local_symbols
;
260 if (rd
->local_symbols
== NULL
)
261 local_symbols
= NULL
;
263 local_symbols
= rd
->local_symbols
->data();
265 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
266 p
!= rd
->relocs
.end();
269 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
270 p
->sh_type
, p
->contents
->data(), p
->reloc_count
,
271 this->local_symbol_count_
,
278 if (rd
->local_symbols
!= NULL
)
280 delete rd
->local_symbols
;
281 rd
->local_symbols
= NULL
;
285 // Relocate the input sections and write out the local symbols.
287 template<int size
, bool big_endian
>
289 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
290 const Symbol_table
* symtab
,
291 const Layout
* layout
,
294 unsigned int shnum
= this->shnum();
296 // Read the section headers.
297 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
298 shnum
* This::shdr_size
);
303 // Make two passes over the sections. The first one copies the
304 // section data to the output file. The second one applies
307 this->write_sections(pshdrs
, of
, &views
);
309 // Apply relocations.
311 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
313 // Write out the accumulated views.
314 for (unsigned int i
= 1; i
< shnum
; ++i
)
316 if (views
[i
].view
!= NULL
)
317 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
321 // Write out the local symbols.
322 this->write_local_symbols(of
, layout
->sympool());
325 // Write section data to the output file. PSHDRS points to the
326 // section headers. Record the views in *PVIEWS for use when
329 template<int size
, bool big_endian
>
331 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
335 unsigned int shnum
= this->shnum();
336 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
338 const unsigned char* p
= pshdrs
+ This::shdr_size
;
339 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
341 View_size
* pvs
= &(*pviews
)[i
];
345 const Output_section
* os
= map_sections
[i
].output_section
;
349 typename
This::Shdr
shdr(p
);
351 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
354 off_t start
= os
->offset() + map_sections
[i
].offset
;
355 off_t sh_size
= shdr
.get_sh_size();
360 gold_assert(map_sections
[i
].offset
>= 0
361 && map_sections
[i
].offset
+ sh_size
<= os
->data_size());
363 unsigned char* view
= of
->get_output_view(start
, sh_size
);
364 this->read(shdr
.get_sh_offset(), sh_size
, view
);
367 pvs
->address
= os
->address() + map_sections
[i
].offset
;
369 pvs
->view_size
= sh_size
;
373 // Relocate section data. VIEWS points to the section data as views
374 // in the output file.
376 template<int size
, bool big_endian
>
378 Sized_relobj
<size
, big_endian
>::relocate_sections(
379 const General_options
& options
,
380 const Symbol_table
* symtab
,
381 const Layout
* layout
,
382 const unsigned char* pshdrs
,
385 unsigned int shnum
= this->shnum();
386 Sized_target
<size
, big_endian
>* target
= this->sized_target();
388 Relocate_info
<size
, big_endian
> relinfo
;
389 relinfo
.options
= &options
;
390 relinfo
.symtab
= symtab
;
391 relinfo
.layout
= layout
;
392 relinfo
.object
= this;
393 relinfo
.local_symbol_count
= this->local_symbol_count_
;
394 relinfo
.local_values
= &this->local_values_
;
395 relinfo
.symbols
= this->symbols_
;
397 const unsigned char* p
= pshdrs
+ This::shdr_size
;
398 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
400 typename
This::Shdr
shdr(p
);
402 unsigned int sh_type
= shdr
.get_sh_type();
403 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
406 unsigned int index
= shdr
.get_sh_info();
407 if (index
>= this->shnum())
409 fprintf(stderr
, _("%s: %s: relocation section %u has bad info %u\n"),
410 program_name
, this->name().c_str(), i
, index
);
414 if (!this->is_section_included(index
))
416 // This relocation section is against a section which we
421 gold_assert((*pviews
)[index
].view
!= NULL
);
423 if (shdr
.get_sh_link() != this->symtab_shndx_
)
426 _("%s: %s: relocation section %u uses unexpected "
427 "symbol table %u\n"),
428 program_name
, this->name().c_str(), i
, shdr
.get_sh_link());
432 off_t sh_size
= shdr
.get_sh_size();
433 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
436 unsigned int reloc_size
;
437 if (sh_type
== elfcpp::SHT_REL
)
438 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
440 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
442 if (reloc_size
!= shdr
.get_sh_entsize())
445 _("%s: %s: unexpected entsize for reloc section %u: "
447 program_name
, this->name().c_str(), i
,
448 static_cast<unsigned long>(shdr
.get_sh_entsize()),
453 size_t reloc_count
= sh_size
/ reloc_size
;
454 if (reloc_count
* reloc_size
!= sh_size
)
456 fprintf(stderr
, _("%s: %s: reloc section %u size %lu uneven"),
457 program_name
, this->name().c_str(), i
,
458 static_cast<unsigned long>(sh_size
));
462 relinfo
.reloc_shndx
= i
;
463 relinfo
.data_shndx
= index
;
464 target
->relocate_section(&relinfo
,
468 (*pviews
)[index
].view
,
469 (*pviews
)[index
].address
,
470 (*pviews
)[index
].view_size
);
474 // Relocate_functions functions.
476 // Return whether we need a COPY reloc for a relocation against GSYM.
477 // The relocation is being applied to section SHNDX in OBJECT.
479 template<int size
, bool big_endian
>
481 Relocate_functions
<size
, big_endian
>::need_copy_reloc(
482 const General_options
*,
487 // FIXME: Handle -z nocopyrelocs.
489 // If this is a readonly section, then we need a COPY reloc.
490 // Otherwise we can use a dynamic reloc.
491 if ((object
->section_flags(shndx
) & elfcpp::SHF_WRITE
) == 0)
497 // Instantiate the templates we need. We could use the configure
498 // script to restrict this to only the ones for implemented targets.
502 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
506 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
510 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
514 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
518 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
519 Symbol_table
* symtab
,
521 Read_relocs_data
* rd
);
525 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
526 Symbol_table
* symtab
,
528 Read_relocs_data
* rd
);
532 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
533 Symbol_table
* symtab
,
535 Read_relocs_data
* rd
);
539 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
540 Symbol_table
* symtab
,
542 Read_relocs_data
* rd
);
546 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
547 const Symbol_table
* symtab
,
548 const Layout
* layout
,
553 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
554 const Symbol_table
* symtab
,
555 const Layout
* layout
,
560 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
561 const Symbol_table
* symtab
,
562 const Layout
* layout
,
567 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
568 const Symbol_table
* symtab
,
569 const Layout
* layout
,
574 Relocate_functions
<32, false>::need_copy_reloc(const General_options
*,
575 Relobj
*, unsigned int,
580 Relocate_functions
<32, true>::need_copy_reloc(const General_options
*,
581 Relobj
*, unsigned int,
586 Relocate_functions
<64, false>::need_copy_reloc(const General_options
*,
587 Relobj
*, unsigned int,
592 Relocate_functions
<64, true>::need_copy_reloc(const General_options
*,
593 Relobj
*, unsigned int,
596 } // End namespace gold.