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