Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: gold/mips.cc

Issue 10252012: [MIPS] Initial checkin for MIPS changes for GOLD. (Closed)
Patch Set: Updated MIPS code w/ SH_RELA. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gold/layout.cc ('k') | gold/output.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // mips.cc -- mips target support for gold.
2
3 // Copyright 2011 Free Software Foundation, Inc.
4 // Written by Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>
5 // based on the i386 code by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
7 // bfd/elf32-mips.c.
8
9 // This file is part of gold.
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
15
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
25
26 #include <set>
27 #include <sstream>
28
29 #include "gold.h"
30
31 #include "elfcpp.h"
32 #include "parameters.h"
33 #include "reloc.h"
34 #include "mips.h"
35 #include "object.h"
36 #include "symtab.h"
37 #include "layout.h"
38 #include "output.h"
39 #include "copy-relocs.h"
40 #include "target.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
43 #include "tls.h"
44 #include "errors.h"
45 #include "gc.h"
46
47 namespace
48 {
49 using namespace gold;
50
51 template<int size, bool big_endian>
52 class Mips_output_data_plt;
53
54 template<int size, bool big_endian>
55 class Mips_output_data_got;
56
57 template<int size, bool big_endian>
58 class Target_mips;
59
60 template<int size>
61 class Mips_output_section_reginfo;
62
63 template<int size, bool big_endian>
64 class Mips_output_data_stub;
65
66 template<int size, bool big_endian>
67 class Mips_output_data_mips_stubs;
68
69 // Mips_output_data_got class. We derive this from Output_data_got to add
70 // extra methods to handle TLS relocations.
71
72 template<int size, bool big_endian>
73 class Mips_output_data_got : public Output_data_got<size, big_endian>
74 {
75 public:
76 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
77 Reloc_section;
78
79 Mips_output_data_got(Symbol_table* symtab, Layout* layout)
80 : Output_data_got<size, big_endian>(), symbol_table_(symtab),
81 layout_(layout), tls_entries_(0), tls_entries_set_(false)
82 {
83 // According to MIPS O32 ABI zero entry of GOT is reserved.
84 this->add_constant(0);
85 // Module pointer (GNU extension)
86 this->add_constant(0x80000000);
87 }
88
89 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
90 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
91 // applied in a static link.
92 void
93 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
94 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
95
96 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
97 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
98 // relocation that needs to be applied in a static link.
99 void
100 add_static_reloc(unsigned int got_offset, unsigned int r_type,
101 Sized_relobj_file<size, big_endian>* relobj,
102 unsigned int index)
103 {
104 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
105 index));
106 }
107
108 void
109 add_tls_gd_with_static_reloc(unsigned int got_type, Symbol* gsym);
110
111 // Same as the above but for a local symbol in OBJECT with INDEX.
112 void
113 add_tls_gd_local_reloc(unsigned int got_type,
114 Sized_relobj_file<size, big_endian>* object,
115 unsigned int index);
116
117 void
118 add_got16_view(std::string key, Mips_output_data_got<size, big_endian>* got)
119
120 {
121 if (got16_views_.count(key) == 0)
122 got16_views_[key] = got->add_constant(0);
123 }
124
125 unsigned int
126 got16_offset(std::string key)
127 {
128 if (got16_views_.count(key) == 0)
129 gold_error("internal error in GOT16 relocation");
130
131 return got16_views_[key];
132 }
133
134 unsigned char*
135 got_view()
136 { return this->got_view_; }
137
138 Symbol_table*
139 symbol_table()
140 { return this->symbol_table_; }
141
142 Layout*
143 layout()
144 { return this->layout_; }
145
146 void
147 add_global_with_reloc_mips(Symbol* gsym)
148 {
149 this->global_relocs_.push(gsym);
150 }
151
152 void
153 add_global_tls_with_reloc_mips(Symbol* gsym, Reloc_section* rel_dyn,
154 unsigned int r_type)
155 {
156 this->tls_relocs_.push(Tls_reloc(gsym, rel_dyn, r_type));
157 }
158
159 void
160 add_global_relocs()
161 {
162 while (!this->global_relocs_.empty())
163 {
164 Symbol *gsym = this->global_relocs_.front();
165
166 this->add_global(gsym, 0);
167 gsym->set_needs_dynsym_entry();
168 this->global_relocs_.pop();
169 }
170 }
171
172 void
173 add_global_tls_relocs()
174 {
175 while (!this->tls_relocs_.empty())
176 {
177 Tls_reloc tls_rel = this->tls_relocs_.front();
178 Symbol *gsym = tls_rel.symbol();
179
180 if (!gsym->has_got_offset(1))
181 {
182 unsigned int r_type = tls_rel.r_type();
183 Reloc_section* rel_dyn = tls_rel.rel_dyn();
184
185 this->tls_entries_++;
186
187 if (parameters->options().output_is_position_independent())
188 {
189 this->add_global(gsym, 1);
190 rel_dyn->add_absolute(r_type, this, gsym->got_offset(1));
191 }
192 else
193 {
194 this->add_global_with_rel(gsym, 1, rel_dyn, r_type);
195 }
196
197 gsym->set_needs_dynsym_entry();
198 }
199
200 this->tls_relocs_.pop();
201 }
202
203 this->tls_entries_set_ = true;
204 }
205
206 unsigned int
207 tls_entries()
208 {
209 if (this->tls_entries_set_)
210 return this->tls_entries_;
211
212 return 0;
213 }
214 protected:
215 // Write out the GOT table.
216 void
217 do_write(Output_file*);
218
219 private:
220 class Tls_reloc
221 {
222 public:
223 Tls_reloc(Symbol* gsym, Reloc_section* rel_dyn, unsigned int r_type)
224 : symbol_(gsym), rel_dyn_(rel_dyn), r_type_(r_type)
225 { }
226
227 Symbol*
228 symbol()
229 { return this->symbol_; }
230
231 Reloc_section*
232 rel_dyn()
233 { return this->rel_dyn_; }
234
235 unsigned int
236 r_type()
237 { return this->r_type_; }
238
239 private:
240 Symbol* symbol_;
241 Reloc_section* rel_dyn_;
242 unsigned int r_type_;
243 };
244
245 // This class represent dynamic relocations that need to be applied by
246 // gold because we are using TLS relocations in a static link.
247 class Static_reloc
248 {
249 public:
250 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
251 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
252 { this->u_.global.symbol = gsym; }
253
254 Static_reloc(unsigned int got_offset, unsigned int r_type,
255 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
256 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
257 {
258 this->u_.local.relobj = relobj;
259 this->u_.local.index = index;
260 }
261
262 // Return the GOT offset.
263 unsigned int
264 got_offset() const
265 { return this->got_offset_; }
266
267 // Relocation type.
268 unsigned int
269 r_type() const
270 { return this->r_type_; }
271
272 // Whether the symbol is global or not.
273 bool
274 symbol_is_global() const
275 { return this->symbol_is_global_; }
276
277 // For a relocation against a global symbol, the global symbol.
278 Symbol*
279 symbol() const
280 {
281 gold_assert(this->symbol_is_global_);
282 return this->u_.global.symbol;
283 }
284
285 // For a relocation against a local symbol, the defining object.
286 Sized_relobj_file<size, big_endian>*
287 relobj() const
288 {
289 gold_assert(!this->symbol_is_global_);
290 return this->u_.local.relobj;
291 }
292
293 // For a relocation against a local symbol, the local symbol index.
294 unsigned int
295 index() const
296 {
297 gold_assert(!this->symbol_is_global_);
298 return this->u_.local.index;
299 }
300
301 private:
302 // GOT offset of the entry to which this relocation is applied.
303 unsigned int got_offset_;
304 // Type of relocation.
305 unsigned int r_type_;
306 // Whether this relocation is against a global symbol.
307 bool symbol_is_global_;
308 // A global or local symbol.
309 union
310 {
311 struct
312 {
313 // For a global symbol, the symbol itself.
314 Symbol* symbol;
315 } global;
316 struct
317 {
318 // For a local symbol, the object defining object.
319 Sized_relobj_file<size, big_endian>* relobj;
320 // For a local symbol, the symbol index.
321 unsigned int index;
322 } local;
323 } u_;
324 };
325
326 // Symbol table of the output object.
327 Symbol_table* symbol_table_;
328 // Layout of the output object.
329 Layout* layout_;
330 // Static relocs to be applied to the GOT.
331 std::vector<Static_reloc> static_relocs_;
332 // Got16 relocs that are related to the GOT
333 std::map<std::string, unsigned int> got16_views_;
334 // .got section view
335 unsigned char* got_view_;
336 // List of global relocs that should be at the end of .got section
337 std::queue<Symbol *> global_relocs_;
338 // List of global tls relocs that should be at the end of .got section
339 std::queue<Tls_reloc> tls_relocs_;
340 // Number of TLS entries
341 unsigned int tls_entries_;
342 // Is number of TLS entries set
343 bool tls_entries_set_;
344 };
345
346 // Mips reginfo output section class
347
348 template<int size>
349 class Mips_output_section_reginfo : public Output_section
350 {
351 public:
352 Mips_output_section_reginfo (const char *name, elfcpp::Elf_Word type,
353 elfcpp::Elf_Xword flags):Output_section (name, type, flags)
354 {
355 this->set_always_keeps_input_sections();
356
357 gp_ = NULL;
358
359 for (size_t i = 0; i < 24; i++)
360 data_[i] = 0;
361 }
362
363 ~Mips_output_section_reginfo()
364 { };
365
366 void
367 set_gp(Sized_symbol<size> *gp)
368 { this->gp_ = gp; }
369
370 // Downcast a base pointer to an Mips_output_section_reginfo pointer.
371 static Mips_output_section_reginfo<size>*
372 as_mips_output_section_reginfo(Output_section* os)
373 { return static_cast<Mips_output_section_reginfo<size>*>(os); }
374
375 protected:
376 // Set the final data size.
377 void set_final_data_size();
378
379 // Write reginfo section into file OF.
380 void do_write (Output_file * of);
381
382 private:
383 // Data.
384 unsigned char data_[24];
385
386 // _gp symbol
387 Sized_symbol<size>* gp_;
388 };
389
390 // The MIPS target has many relocation types with odd-sizes or noncontiguous
391 // bits. The default handling of relocatable relocation cannot process these
392 // relocations. So we have to extend the default code.
393
394 template<bool big_endian, int sh_type, typename Classify_reloc>
395 class Mips_scan_relocatable_relocs :
396 public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
397 {
398 public:
399 // Return the strategy to use for a local symbol which is a section
400 // symbol, given the relocation type.
401 inline Relocatable_relocs::Reloc_strategy
402 local_section_strategy(unsigned int r_type, Relobj *object)
403 {
404 switch (r_type)
405 {
406 case elfcpp::R_MIPS_26:
407 return Relocatable_relocs::RELOC_SPECIAL;
408
409 default:
410 return Default_scan_relocatable_relocs<sh_type, Classify_reloc>::local _section_strategy(r_type, object);
411 }
412 }
413 };
414
415 template<int size, bool big_endian>
416 class Target_mips : public Sized_target<size, big_endian>
417 {
418 public:
419 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
420 Reloc_section;
421 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
422 Reloca_section;
423 typedef Unordered_map<const char*, Symbol*> Stub_symbol_info;
424
425 Target_mips()
426 : Sized_target<size, big_endian>(&mips_info),
427 got_(NULL), plt_(NULL), got_plt_(NULL),
428 rel_dyn_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_MIPS_COPY),
429 copy_relocsa_(elfcpp::R_MIPS_COPY), got_mod_index_offset_(-1U),
430 gp_(NULL), stub_(NULL), stub_flag_(false),
431 processor_specific_flags_(0), mips_stubs_(NULL), text_section_(NULL),
432 layout_(NULL), symtab_(NULL)
433 {
434 add_extension(elfcpp::E_MIPS_ARCH_64R2, elfcpp::E_MIPS_MACH_OCTEON);
435 add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64R2);
436 add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64
437 | elfcpp::E_MIPS_MACH_SB1);
438 add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64
439 | elfcpp::E_MIPS_MACH_XLR);
440 add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64
441 | elfcpp::E_MIPS_MACH_LS3A);
442 add_extension(elfcpp::E_MIPS_ARCH_5, elfcpp::E_MIPS_ARCH_64);
443 add_extension(elfcpp::E_MIPS_ARCH_4 | elfcpp::E_MIPS_MACH_5400,
444 elfcpp::E_MIPS_ARCH_4 | elfcpp::E_MIPS_MACH_5500);
445 add_extension(elfcpp::E_MIPS_ARCH_4, elfcpp::E_MIPS_ARCH_4
446 | elfcpp::E_MIPS_MACH_5400);
447 add_extension(elfcpp::E_MIPS_ARCH_4, elfcpp::E_MIPS_ARCH_5);
448 add_extension(elfcpp::E_MIPS_ARCH_4, elfcpp::E_MIPS_ARCH_4
449 | elfcpp::E_MIPS_MACH_9000);
450 add_extension(elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4100,
451 elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4120);
452 add_extension(elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4100,
453 elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4111);
454 add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3
455 | elfcpp::E_MIPS_MACH_LS2F);
456 add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3
457 | elfcpp::E_MIPS_MACH_LS2F);
458 add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_4);
459 add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3
460 | elfcpp::E_MIPS_MACH_4650);
461 add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3
462 | elfcpp::E_MIPS_MACH_4100);
463 add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3
464 | elfcpp::E_MIPS_MACH_4111);
465 add_extension(elfcpp::E_MIPS_ARCH_32, elfcpp::E_MIPS_ARCH_32R2);
466 add_extension(elfcpp::E_MIPS_ARCH_2, elfcpp::E_MIPS_ARCH_3);
467 add_extension(elfcpp::E_MIPS_ARCH_2, elfcpp::E_MIPS_ARCH_32);
468 add_extension(elfcpp::E_MIPS_ARCH_1, elfcpp::E_MIPS_ARCH_2);
469 add_extension(elfcpp::E_MIPS_ARCH_1, elfcpp::E_MIPS_ARCH_1
470 | elfcpp::E_MIPS_MACH_3900);
471 }
472
473 // Process the relocations to determine unreferenced sections for
474 // garbage collection.
475 void
476 gc_process_relocs(Symbol_table* symtab,
477 Layout* layout,
478 Sized_relobj_file<size, big_endian>* object,
479 unsigned int data_shndx,
480 unsigned int sh_type,
481 const unsigned char* prelocs,
482 size_t reloc_count,
483 Output_section* output_section,
484 bool needs_special_offset_handling,
485 size_t local_symbol_count,
486 const unsigned char* plocal_symbols);
487
488 // Scan the relocations to look for symbol adjustments.
489 void
490 scan_relocs(Symbol_table* symtab,
491 Layout* layout,
492 Sized_relobj_file<size, big_endian>* object,
493 unsigned int data_shndx,
494 unsigned int sh_type,
495 const unsigned char* prelocs,
496 size_t reloc_count,
497 Output_section* output_section,
498 bool needs_special_offset_handling,
499 size_t local_symbol_count,
500 const unsigned char* plocal_symbols);
501
502 // Finalize the sections.
503 void
504 do_finalize_sections (Layout *, const Input_objects *, Symbol_table *);
505
506 // Fix values that are known only after sections are finalized.
507 void
508 do_fix_sections(Layout*, Symbol_table*);
509
510 // Relocate a section.
511 void
512 relocate_section(const Relocate_info<size, big_endian>*,
513 unsigned int sh_type,
514 const unsigned char* prelocs,
515 size_t reloc_count,
516 Output_section* output_section,
517 bool needs_special_offset_handling,
518 unsigned char* view,
519 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
520 section_size_type view_size,
521 const Reloc_symbol_changes*);
522
523 // Scan the relocs during a relocatable link.
524 void
525 scan_relocatable_relocs(Symbol_table* symtab,
526 Layout* layout,
527 Sized_relobj_file<size, big_endian>* object,
528 unsigned int data_shndx,
529 unsigned int sh_type,
530 const unsigned char* prelocs,
531 size_t reloc_count,
532 Output_section* output_section,
533 bool needs_special_offset_handling,
534 size_t local_symbol_count,
535 const unsigned char* plocal_symbols,
536 Relocatable_relocs*);
537
538 // Relocate a section during a relocatable link.
539 void
540 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
541 unsigned int sh_type,
542 const unsigned char* prelocs,
543 size_t reloc_count,
544 Output_section* output_section,
545 off_t offset_in_output_section,
546 const Relocatable_relocs*,
547 unsigned char* view,
548 typename elfcpp::Elf_types<size>::Elf_Addr view_addre ss,
549 section_size_type view_size,
550 unsigned char* reloc_view,
551 section_size_type reloc_view_size);
552
553 // Perform target-specific processing in a relocatable link. This is
554 // only used if we use the relocation strategy RELOC_SPECIAL.
555 void
556 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo,
557 unsigned int sh_type,
558 const unsigned char* preloc_in,
559 size_t relnum,
560 Output_section* output_section,
561 off_t offset_in_output_section,
562 unsigned char* view,
563 typename elfcpp::Elf_types<32>::Elf_Addr
564 view_address,
565 section_size_type view_size,
566 unsigned char* preloc_out);
567
568 // Return whether SYM is defined by the ABI.
569 bool
570 do_is_defined_by_abi(const Symbol* sym) const
571 {
572 return ((strcmp(sym->name(), "__gnu_local_gp") == 0)
573 || (strcmp(sym->name(), "_gp_disp") == 0)
574 || (strcmp(sym->name(), "___tls_get_addr") == 0));
575 }
576
577 // Return the number of entries in the GOT.
578 unsigned int
579 got_entry_count() const
580 {
581 if (!this->has_got_section())
582 return 0;
583 return this->got_size() / 4;
584 }
585
586 // Return the number of entries in the PLT.
587 unsigned int
588 plt_entry_count() const
589 {
590 if (this->plt_ == NULL)
591 return 0;
592 return this->plt_->entry_count();
593 }
594
595 // Return the offset of the first non-reserved PLT entry.
596 unsigned int
597 first_plt_entry_offset() const
598 { return Mips_output_data_plt<size, big_endian>::first_plt_entry_offset(); }
599
600 // Return the size of each PLT entry.
601 unsigned int
602 plt_entry_size() const
603 { return Mips_output_data_plt<size, big_endian>::plt_entry_size(); }
604
605 // Get stub section
606 Mips_output_data_stub<size, big_endian>*
607 stub()
608 { return this->stub_; }
609
610 // Add stub symbol to map
611 void
612 add_stub_symbol(const char* name, Symbol *gsym)
613 { this->stub_symbol_[name] = gsym; }
614
615 // Get stub symbol from map
616 Symbol*
617 stub_symbol(const char* name)
618 { return this->stub_symbol_[name]; }
619
620 // Check if symbol is in .plt section
621 bool
622 is_plt_symbol(Sized_symbol<size> *sym)
623 {
624 if (this->mips_stubs_ == NULL)
625 return true;
626
627 return this->mips_stubs_->is_plt_symbol(sym);
628 }
629
630 // Return .MIPS.stubs output section.
631 Mips_output_data_mips_stubs<size, big_endian> *
632 mips_stubs() const
633 { return this->mips_stubs_; }
634
635 // Retrun address of .text section
636 uint64_t
637 text_section_address() const
638 {
639 if (this->text_section_ == 0)
640 return 0;
641
642 return this->text_section_->address();
643 }
644
645 protected:
646 // Return the value to use for a dynamic symbol which requires special
647 // treatment. This is how we support equality comparisons of function
648 // pointers across shared library boundaries, as described in the
649 // processor specific ABI supplement.
650 uint64_t
651 do_dynsym_value(const Symbol* gsym) const;
652
653 // Make an output section.
654 Output_section*
655 do_make_output_section (const char *name, elfcpp::Elf_Word type,
656 elfcpp::Elf_Xword flags)
657 {
658 if (type == elfcpp::SHT_MIPS_REGINFO)
659 return new Mips_output_section_reginfo<size>(name, type, flags);
660 else
661 return new Output_section(name, type, flags);
662 }
663
664 void
665 do_adjust_elf_header(unsigned char* view, int len) const;
666
667 // MIPS specific dynamic tags.
668 unsigned int
669 do_dynamic_tag_value(elfcpp::DT) const;
670
671 private:
672 // The class which scans relocations.
673 class Scan
674 {
675 public:
676 Scan()
677 : issued_non_pic_error_(false)
678 { }
679
680 static inline int
681 get_reference_flags(unsigned int r_type);
682
683 inline void
684 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
685 Sized_relobj_file<size, big_endian>* object,
686 unsigned int data_shndx,
687 Output_section* output_section,
688 const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type,
689 const elfcpp::Sym<size, big_endian>& lsym);
690
691 inline void
692 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
693 Sized_relobj_file<size, big_endian>* object,
694 unsigned int data_shndx,
695 Output_section* output_section,
696 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
697 const elfcpp::Sym<size, big_endian>& lsym);
698
699 inline void
700 local(Symbol_table* symtab, Layout* layout, Target_mips* target,
701 Sized_relobj_file<size, big_endian>* object,
702 unsigned int data_shndx,
703 Output_section* output_section,
704 const elfcpp::Rela<size, big_endian>* rela,
705 const elfcpp::Rel<size, big_endian>* rel,
706 unsigned int rel_type,
707 unsigned int r_type,
708 const elfcpp::Sym<size, big_endian>& lsym);
709
710 inline void
711 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
712 Sized_relobj_file<size, big_endian>* object,
713 unsigned int data_shndx,
714 Output_section* output_section,
715 const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type,
716 Symbol* gsym);
717
718 inline void
719 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
720 Sized_relobj_file<size, big_endian>* object,
721 unsigned int data_shndx,
722 Output_section* output_section,
723 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
724 Symbol* gsym);
725
726 inline void
727 global(Symbol_table* symtab, Layout* layout, Target_mips* target,
728 Sized_relobj_file<size, big_endian>* object,
729 unsigned int data_shndx,
730 Output_section* output_section,
731 const elfcpp::Rela<size, big_endian>* rela,
732 const elfcpp::Rel<size, big_endian>* rel,
733 unsigned int rel_type,
734 unsigned int r_type,
735 Symbol* gsym);
736
737 inline bool
738 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
739 Target_mips* ,
740 Sized_relobj_file<size, big_endian>* ,
741 unsigned int ,
742 Output_section* ,
743 const elfcpp::Rel<size, big_endian>& ,
744 unsigned int ,
745 const elfcpp::Sym<size, big_endian>&)
746 { return false; }
747
748 inline bool
749 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
750 Target_mips* ,
751 Sized_relobj_file<size, big_endian>* ,
752 unsigned int ,
753 Output_section* ,
754 const elfcpp::Rel<size, big_endian>& ,
755 unsigned int , Symbol*)
756 { return false; }
757
758 inline bool
759 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
760 Target_mips* ,
761 Sized_relobj_file<size, big_endian>* ,
762 unsigned int ,
763 Output_section* ,
764 const elfcpp::Rela<size, big_endian>& ,
765 unsigned int ,
766 const elfcpp::Sym<size, big_endian>&)
767 { return false; }
768
769 inline bool
770 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
771 Target_mips* ,
772 Sized_relobj_file<size, big_endian>* ,
773 unsigned int ,
774 Output_section* ,
775 const elfcpp::Rela<size, big_endian>& ,
776 unsigned int , Symbol*)
777 { return false; }
778 private:
779 static void
780 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
781 unsigned int r_type);
782
783 static void
784 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
785 unsigned int r_type, Symbol*);
786
787 static void
788 generate_tls_call(Symbol_table* symtab, Layout* layout,
789 Target_mips* target);
790
791 void
792 check_non_pic(Relobj*, unsigned int r_type);
793
794 static bool
795 symbol_needs_plt_entry(const Symbol* sym)
796 {
797 // An undefined symbol from an executable does not need a PLT entry.
798 if (sym->is_undefined() && !parameters->options().shared())
799 return false;
800
801 return (!parameters->doing_static_link()
802 && sym->is_func()
803 && (!sym->is_undef_binding_weak())
804 && (sym->is_from_dynobj()
805 || sym->is_undefined()
806 || sym->is_preemptible()));
807 }
808
809 // Whether we have issued an error about a non-PIC compilation.
810 bool issued_non_pic_error_;
811 };
812
813 // The class which implements relocation.
814 class Relocate
815 {
816 public:
817 // Do a relocation. Return false if the caller should not issue
818 // any warnings about this relocation.
819 inline bool
820 relocate(const Relocate_info<size, big_endian>*, Target_mips*,
821 Output_section*, size_t relnum,
822 const elfcpp::Rela<size, big_endian>*,
823 const elfcpp::Rel<size, big_endian>*,
824 unsigned int,
825 unsigned int, const Sized_symbol<size>*,
826 const Symbol_value<size>*,
827 unsigned char*,
828 typename elfcpp::Elf_types<size>::Elf_Addr,
829 section_size_type);
830
831 inline bool
832 relocate(const Relocate_info<size, big_endian>*, Target_mips*,
833 Output_section*, size_t relnum,
834 const elfcpp::Rel<size, big_endian>&,
835 unsigned int, const Sized_symbol<size>*,
836 const Symbol_value<size>*,
837 unsigned char*,
838 typename elfcpp::Elf_types<size>::Elf_Addr,
839 section_size_type);
840
841 inline bool
842 relocate(const Relocate_info<size, big_endian>*, Target_mips*,
843 Output_section*, size_t relnum,
844 const elfcpp::Rela<size, big_endian>&,
845 unsigned int, const Sized_symbol<size>*,
846 const Symbol_value<size>*,
847 unsigned char*,
848 typename elfcpp::Elf_types<size>::Elf_Addr,
849 section_size_type);
850 };
851
852 // A class which returns the size required for a relocation type,
853 // used while scanning relocs during a relocatable link.
854 class Relocatable_size_for_reloc
855 {
856 public:
857 unsigned int
858 get_size_for_reloc(unsigned int, Relobj*);
859 };
860
861 // Adjust TLS relocation type based on the options and whether this
862 // is a local symbol.
863 static tls::Tls_optimization
864 optimize_tls_reloc(bool is_final, int r_type);
865
866 // Return whether there is a GOT section.
867 bool
868 has_got_section() const
869 { return this->got_ != NULL; }
870
871 // Get the GOT section, creating it if necessary.
872 Mips_output_data_got<size, big_endian>*
873 got_section(Symbol_table*, Layout*);
874
875 Mips_output_data_got<size, big_endian>*
876 got_section()
877 { return this->got_; }
878
879 // Return processor specific flags
880 elfcpp::Elf_Word
881 processor_specific_flags() const
882 { return this->processor_specific_flags_; }
883
884 // Set processor specific flags
885 void
886 set_processor_specific_flags(elfcpp::Elf_Word psf)
887 { this->processor_specific_flags_ = psf; }
888
889 // Check if the given ELF header glags describe a 32-bit binary.
890 bool
891 mips_32bit_flags(elfcpp::Elf_Word);
892
893 // Check if new architecture extends previous one.
894 bool
895 mips_mach_extends(elfcpp::Elf_Word, elfcpp::Elf_Word);
896
897 // Merge processor specific flags.
898 void
899 merge_processor_specific_flags(const char*, elfcpp::Elf_Word);
900
901 // Return the size of the GOT section.
902 section_size_type
903 got_size() const
904 {
905 gold_assert(this->got_ != NULL);
906 return this->got_->data_size();
907 }
908
909 // Create a GOT entry for the TLS module index.
910 unsigned int
911 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
912 Sized_relobj_file<size, big_endian>* object);
913
914 // Create a PLT entry for a global symbol.
915 void
916 make_plt_entry(Symbol_table*, Layout*, Symbol*);
917
918 // Create a .MIPS.stubs entry for a global symbol.
919 void
920 make_mips_stubs_entry(Symbol_table*, Layout*, Symbol*);
921
922 // Remove symbol that should not have entry.
923 void
924 remove_stub_entry(Symbol *sym)
925 {
926 if (this->mips_stubs_ != NULL)
927 this->mips_stubs_->remove(sym);
928 }
929
930 // Create a STUB entry for a global symbol.
931 void
932 make_stub_entry(Layout*);
933
934 void
935 set_stub_flag()
936 { this->stub_flag_ = true; }
937
938 bool
939 stub_flag() const
940 { return this->stub_flag_; }
941
942 // Get the PLT section.
943 const Mips_output_data_plt<size, big_endian>*
944 plt_section() const
945 {
946 gold_assert(this->plt_ != NULL);
947 return this->plt_;
948 }
949
950 // Get the GOT PLT section.
951 const Mips_output_data_plt<size, big_endian>*
952 got_plt_section() const
953 {
954 gold_assert(this->got_plt_ != NULL);
955 return this->got_plt_;
956 }
957
958 // Get _gp.
959 Sized_symbol<size>*
960 gp()
961 { return this->gp_; }
962
963 // Get value of _gp.
964 const typename elfcpp::Elf_types<size>::Elf_Addr
965 gp_address() const
966 { return this->gp_->value(); }
967
968 // Copy a relocation against a global symbol.
969 void
970 copy_reloc(Symbol_table* symtab, Layout* layout,
971 Sized_relobj_file<size, big_endian>* object,
972 unsigned int shndx, Output_section* output_section,
973 Symbol* sym, const elfcpp::Rel<size, big_endian>& reloc)
974 {
975 this->copy_relocs_.copy_reloc(symtab, layout,
976 symtab->get_sized_symbol<size>(sym),
977 object, shndx, output_section,
978 reloc, this->rel_dyn_section(layout));
979 }
980
981 // Copy a relocation against a global symbol.
982 void
983 copy_reloc(Symbol_table* symtab, Layout* layout,
984 Sized_relobj_file<size, big_endian>* object,
985 unsigned int shndx, Output_section* output_section,
986 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
987 {
988 this->copy_relocsa_.copy_reloc(symtab, layout,
989 symtab->get_sized_symbol<size>(sym),
990 object, shndx, output_section,
991 reloc, this->rela_dyn_section(layout));
992 }
993
994 // Calculate value of _gp.
995 void
996 set_gp(Layout*, Symbol_table*);
997
998 // Get the dynamic reloc section, creating it if necessary.
999 Reloc_section*
1000 rel_dyn_section(Layout*);
1001
1002 Reloca_section*
1003 rela_dyn_section(Layout*);
1004
1005 // Add value to MIPS extenstions.
1006 void
1007 add_extension(unsigned long base, unsigned long extension)
1008 {
1009 std::pair<unsigned long, unsigned long> ext(base, extension);
1010 this->extensions_.push_back(ext);
1011 }
1012
1013 // The types of GOT entries needed for this platform.
1014 // These values are exposed to the ABI in an incremental link.
1015 // Do not renumber existing values without changing the version
1016 // number of the .gnu_incremental_inputs section.
1017 enum Got_type
1018 {
1019 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
1020 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
1021 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
1022 };
1023
1024 // Information about this specific target which we pass to the
1025 // general Target structure.
1026 static Target::Target_info mips_info;
1027 // The GOT section.
1028 Mips_output_data_got<size, big_endian>* got_;
1029 // The PLT section.
1030 Mips_output_data_plt<size, big_endian>* plt_;
1031 // The GOT PLT section.
1032 Output_data_space* got_plt_;
1033 // The dynamic reloc section.
1034 Reloc_section* rel_dyn_;
1035 Reloca_section* rela_dyn_;
1036 // Relocs saved to avoid a COPY reloc.
1037 Copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
1038 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocsa_;
1039 // Offset of the GOT entry for the TLS module index.
1040 unsigned int got_mod_index_offset_;
1041 // Value of the _gp
1042 Sized_symbol<size>* gp_;
1043 // The stub section.
1044 Mips_output_data_stub<size, big_endian>* stub_;
1045 // Stub flag
1046 bool stub_flag_;
1047 // Map of stub symbols
1048 Stub_symbol_info stub_symbol_;
1049 // processor-specific flags in ELF file header.
1050 elfcpp::Elf_Word processor_specific_flags_;
1051 // Architecture extensions.
1052 std::vector<std::pair<unsigned long, unsigned long> > extensions_;
1053 // .MIPS.stubs
1054 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
1055 // .text section
1056 Output_section *text_section_;
1057 // Layout
1058 Layout* layout_;
1059 // Symbol table
1060 Symbol_table* symtab_;
1061 };
1062
1063 // Helper structure for R_MIPS_HI/LO16 and R_MIPS16_HI/LO16 relocations
1064
1065 template<int size, bool big_endian>
1066 struct hi16
1067 {
1068 unsigned char* view;
1069 typename elfcpp::Swap<32, big_endian>::Valtype symval;
1070 typename elfcpp::Elf_types<size>::Elf_Addr addend;
1071 };
1072
1073 template<int size, bool big_endian>
1074 class Mips_relocate_functions : public Relocate_functions<size, big_endian>
1075 {
1076 public:
1077 typedef enum
1078 {
1079 STATUS_OKAY, // No error during relocation.
1080 STATUS_OVERFLOW, // Relocation overflow.
1081 STATUS_BAD_RELOC // Relocation cannot be applied.
1082 } Status;
1083
1084 private:
1085 typedef Relocate_functions<size, big_endian> Base;
1086 typedef Mips_relocate_functions<size, big_endian> This;
1087
1088 public:
1089 // R_MIPS_16
1090 static inline typename This::Status
1091 rel16(unsigned char* view,
1092 const Sized_relobj_file<size, big_endian>* object,
1093 const Symbol_value<size>* psymval,
1094 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1095 unsigned int rel_type)
1096 {
1097 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1098 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1099 Valtype* wv = reinterpret_cast<Valtype*>(view);
1100 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1101 Reltype addend;
1102
1103 if (rel_type == elfcpp::SHT_RELA)
1104 addend = Bits<16>::sign_extend32(addend_o);
1105 else
1106 addend = Bits<16>::sign_extend32(val);
1107
1108 Reltype x = psymval->value(object, addend);
1109 val = Bits<16>::bit_select32(val, x, 0x0000ffffU);
1110 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1111 return (Bits<16>::has_signed_unsigned_overflow32(x)
1112 ? This::STATUS_OVERFLOW
1113 : This::STATUS_OKAY);
1114 }
1115
1116 // R_MIPS_32
1117 static inline typename This::Status
1118 rel32(unsigned char* view,
1119 const Sized_relobj_file<size, big_endian>* object,
1120 const Symbol_value<size>* psymval,
1121 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1122 unsigned int rel_type)
1123 {
1124 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1125 Valtype* wv = reinterpret_cast<Valtype*>(view);
1126 Valtype addend;
1127
1128 if (rel_type == elfcpp::SHT_RELA)
1129 addend = Bits<32>::sign_extend32(addend_o);
1130 else
1131 addend = elfcpp::Swap<32, big_endian>::readval(wv);
1132
1133 Valtype x = psymval->value(object, addend);
1134 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1135 return This::STATUS_OKAY;
1136 }
1137
1138 // R_MIPS_JALR
1139 static inline typename This::Status
1140 reljalr(unsigned char* view,
1141 const Sized_relobj_file<size, big_endian>* object,
1142 const Symbol_value<size>* psymval,
1143 typename elfcpp::Elf_types<size>::Elf_Addr address)
1144 {
1145 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1146 Valtype* wv = reinterpret_cast<Valtype*>(view);
1147 Valtype instr = elfcpp::Swap<32, big_endian>::readval(wv);
1148 Valtype x = psymval->value(object, 0);
1149 Valtype offset = x - address - 4;
1150
1151 if (!Bits<17>::has_signed_unsigned_overflow32(offset))
1152 {
1153 if (instr == 0x0320f809) // jalr t9
1154 {
1155 // bal addr
1156 Valtype jalr = 0x04110000 | ((offset >> 2) & 0x0000ffffU);
1157 elfcpp::Swap<32, big_endian>::writeval(wv, jalr);
1158 }
1159 else if (instr == 0x03200008) //jr t9
1160 {
1161 // b addr
1162 Valtype jalr = 0x10000000 | ((offset >> 2) & 0x0000ffffU);
1163 elfcpp::Swap<32, big_endian>::writeval(wv, jalr);
1164 }
1165 }
1166 return This::STATUS_OKAY;
1167 }
1168
1169 // R_MIPS_REL32, R_MIPS_PC32
1170 static inline typename This::Status
1171 relrel32(unsigned char* view,
1172 const Sized_relobj_file<size, big_endian>* object,
1173 const Symbol_value<size>* psymval,
1174 typename elfcpp::Elf_types<size>::Elf_Addr address,
1175 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1176 unsigned int rel_type)
1177 {
1178 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1179 Valtype* wv = reinterpret_cast<Valtype*>(view);
1180 Valtype addend;
1181
1182 if (rel_type == elfcpp::SHT_RELA)
1183 addend = Bits<32>::sign_extend32(addend_o);
1184 else
1185 addend = elfcpp::Swap<32, big_endian>::readval(wv);
1186
1187 Valtype x = (psymval->value(object, addend)) - address;
1188 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1189 return This::STATUS_OKAY;
1190 }
1191
1192 // R_MIPS_26
1193 static inline typename This::Status
1194 rel26(unsigned char* view,
1195 const Sized_relobj_file<size, big_endian>* object,
1196 const Symbol_value<size>* psymval,
1197 typename elfcpp::Elf_types<size>::Elf_Addr address,
1198 bool local,
1199 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1200 unsigned int rel_type)
1201 {
1202 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1203 Valtype* wv = reinterpret_cast<Valtype*>(view);
1204 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1205 Valtype addend;
1206
1207 if (local)
1208 {
1209 if (rel_type == elfcpp::SHT_RELA)
1210 addend = ((addend_o & 0x03ffffffU) | ((address + 4) & 0xf0000000U));
1211 else
1212 addend = ((val & 0x03ffffffU) | ((address + 4) & 0xf0000000U));
1213 }
1214 else
1215 {
1216 if (rel_type == elfcpp::SHT_RELA)
1217 addend = Bits<26>::sign_extend32(addend_o);
1218 else
1219 addend = Bits<26>::sign_extend32(val & 0x03ffffffU);
1220 }
1221
1222 Valtype x = psymval->value(object, addend << 2);
1223 val = Bits<32>::bit_select32(val, x >> 2, 0x03ffffffU);
1224 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1225 return This::STATUS_OKAY;
1226 }
1227
1228 // R_MIPS_SHIFT5
1229 static inline typename This::Status
1230 relsh5(unsigned char* view,
1231 const Sized_relobj_file<size, big_endian>* object,
1232 const Symbol_value<size>* psymval,
1233 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1234 unsigned int rel_type)
1235 {
1236 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1237 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1238 Valtype* wv = reinterpret_cast<Valtype*>(view);
1239 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1240 Reltype addend;
1241
1242 if (rel_type == elfcpp::SHT_RELA)
1243 addend = Bits<11>::sign_extend32(addend_o);
1244 else
1245 addend = Bits<11>::sign_extend32(val & 0x000007c0U);
1246
1247 Reltype x = psymval->value(object, addend);
1248 val = Bits<11>::bit_select32(val, x, 0x000007c0U);
1249 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1250 return ((Bits<11>::has_signed_unsigned_overflow32(x) || ((x & 0x3fU) != 0))
1251 ? This::STATUS_OVERFLOW
1252 : This::STATUS_OKAY);
1253 }
1254
1255 // R_MIPS_SHIFT6
1256 static inline typename This::Status
1257 relsh6(unsigned char* view,
1258 const Sized_relobj_file<size, big_endian>* object,
1259 const Symbol_value<size>* psymval,
1260 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1261 unsigned int rel_type)
1262 {
1263 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1264 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1265 Valtype* wv = reinterpret_cast<Valtype*>(view);
1266 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1267 Reltype addend;
1268
1269 if (rel_type == elfcpp::SHT_RELA)
1270 addend = Bits<11>::sign_extend32(addend_o);
1271 else
1272 addend = Bits<11>::sign_extend32(val & 0x000007c0U)
1273 || ((val & 0x00004U) << 2);
1274 Reltype x = psymval->value(object, addend);
1275 x = (x & ~0x00000020U) | ((x & 0x00000020U) >> 2);
1276 val = Bits<16>::bit_select32(val, x, 0x000007c4U);
1277 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1278 return ((Bits<11>::has_signed_unsigned_overflow32(x) || ((x & 0x37U) != 0))
1279 ? This::STATUS_OVERFLOW
1280 : This::STATUS_OKAY);
1281 }
1282
1283 // R_MIPS_SUB
1284 static inline typename This::Status
1285 relsub(unsigned char* view,
1286 const Sized_relobj_file<size, big_endian>* object,
1287 const Symbol_value<size>* psymval,
1288 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1289 unsigned int rel_type)
1290 {
1291 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1292 Valtype* wv = reinterpret_cast<Valtype*>(view);
1293 Valtype addend;
1294
1295 if (rel_type == elfcpp::SHT_RELA)
1296 addend = addend_o;
1297 else
1298 addend = elfcpp::Swap<64, big_endian>::readval(wv);
1299
1300 Valtype x = (psymval->value(object, -addend));
1301 elfcpp::Swap<64, big_endian>::writeval(wv, x);
1302 return This::STATUS_OKAY;
1303 }
1304
1305 // R_MIPS_HIGHER
1306 static inline typename This::Status
1307 relhigher(unsigned char* view,
1308 const Sized_relobj_file<size, big_endian>* object,
1309 const Symbol_value<size>* psymval,
1310 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1311 unsigned int rel_type)
1312 {
1313 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1314 Valtype* wv = reinterpret_cast<Valtype*>(view);
1315 Valtype addend;
1316
1317 if (rel_type == elfcpp::SHT_RELA)
1318 addend = addend_o;
1319 else
1320 addend = elfcpp::Swap<64, big_endian>::readval(wv);
1321
1322 Valtype x = (((Valtype)(psymval->value(object, addend) + 0x80008000) >> 32)
1323 & 0xffffU);
1324 elfcpp::Swap<64, big_endian>::writeval(wv, x);
1325 return This::STATUS_OKAY;
1326 }
1327
1328 // R_MIPS_HIGHEST
1329 static inline typename This::Status
1330 relhighest(unsigned char* view,
1331 const Sized_relobj_file<size, big_endian>* object,
1332 const Symbol_value<size>* psymval,
1333 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1334 unsigned int rel_type)
1335 {
1336 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1337 Valtype* wv = reinterpret_cast<Valtype*>(view);
1338 Valtype addend = elfcpp::Swap<64, big_endian>::readval(wv);
1339
1340 if (rel_type == elfcpp::SHT_RELA)
1341 addend = addend_o;
1342 else
1343 addend = elfcpp::Swap<64, big_endian>::readval(wv);
1344
1345 Valtype x = (((Valtype)(psymval->value(object, addend) + 0x800080008000)
1346 >> 48) & 0xffffU);
1347 elfcpp::Swap<64, big_endian>::writeval(wv, x);
1348 return This::STATUS_OKAY;
1349 }
1350
1351 // R_MIPS_SCN_DISP
1352 static inline typename This::Status
1353 relscndisp(unsigned char* view,
1354 const Sized_relobj_file<size, big_endian>* object,
1355 const Symbol_value<size>* psymval,
1356 typename elfcpp::Elf_types<size>::Elf_Off offset,
1357 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1358 unsigned int rel_type)
1359 {
1360 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1361 Valtype* wv = reinterpret_cast<Valtype*>(view);
1362 Valtype addend;
1363
1364 if (rel_type == elfcpp::SHT_RELA)
1365 addend = addend_o;
1366 else
1367 addend = elfcpp::Swap<32, big_endian>::readval(wv);
1368
1369 Valtype x = (psymval->value(object, addend)) - offset;
1370 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1371 return This::STATUS_OKAY;
1372 }
1373
1374 // R_MIPS_PC16, R_MIPS_GNU_REL16_S2
1375 static inline typename This::Status
1376 relpc16(unsigned char* view,
1377 const Sized_relobj_file<size, big_endian>* object,
1378 const Symbol_value<size>* psymval,
1379 typename elfcpp::Elf_types<size>::Elf_Addr address,
1380 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1381 unsigned int rel_type)
1382 {
1383 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1384 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1385 Valtype* wv = reinterpret_cast<Valtype*>(view);
1386 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1387 Reltype addend;
1388
1389 if (rel_type == elfcpp::SHT_REL)
1390 addend = Bits<16>::sign_extend32(val);
1391 else
1392 addend = Bits<18>::sign_extend32(addend_o);
1393
1394 Reltype x = psymval->value(object, addend) - address;
1395 val = Bits<16>::bit_select32(val, x >> 2, 0x0000ffffU);
1396 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1397 return (Bits<18>::has_signed_unsigned_overflow32(x)
1398 ? This::STATUS_OVERFLOW
1399 : This::STATUS_OKAY);
1400 }
1401
1402 // R_MIPS_HI16, R_MIPS16_HI16, R_MIPS_GOT16, R_MIPS16_GOT16
1403 static inline typename This::Status
1404 relhi16(unsigned char* view,
1405 typename elfcpp::Swap<32, big_endian>::Valtype symval,
1406 typename elfcpp::Elf_types<size>::Elf_Addr addend)
1407 {
1408 struct hi16<size, big_endian>* _hi16 = new hi16<size, big_endian>;
1409
1410 if (!_hi16)
1411 return This::STATUS_OVERFLOW;
1412
1413 _hi16->view = view;
1414 _hi16->symval = symval;
1415 _hi16->addend = addend;
1416
1417 This::hi16_list.push_back(_hi16);
1418 return This::STATUS_OKAY;
1419 }
1420
1421 // R_MIPS_LO16, R_MIPS16_LO16
1422 static inline typename This::Status
1423 rello16(unsigned char* view,
1424 const Sized_relobj_file<size, big_endian>* object,
1425 const Symbol_value<size>* psymval,
1426 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1427 unsigned int rel_type)
1428 {
1429 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1430 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1431 Valtype* wvlo = reinterpret_cast<Valtype*>(view), *wv;
1432 Valtype vallo = elfcpp::Swap<16, big_endian>::readval(wvlo), val;
1433 Reltype addendlo, addend;
1434 Reltype x, xlo;
1435
1436 if (rel_type == elfcpp::SHT_RELA)
1437 addendlo = addend_o;
1438 else
1439 addendlo = Bits<16>::sign_extend32(vallo);
1440
1441 xlo = psymval->value(object, addendlo);
1442 vallo = Bits<16>::bit_select32(vallo, xlo, 0x0000ffffU);
1443
1444 while (hi16_list.size())
1445 {
1446 struct hi16<size, big_endian>* _hi16 = hi16_list.back();
1447 wv = reinterpret_cast<Valtype*>(_hi16->view);
1448
1449 if (rel_type == elfcpp::SHT_RELA)
1450 addend = _hi16->addend;
1451 else
1452 addend = elfcpp::Swap<16, big_endian>::readval(wv);
1453
1454 x = _hi16->symval + addendlo + (addend << 16);
1455 x += 0x00008000U;
1456 x >>= 16;
1457
1458 val = Bits<16>::bit_select32(val, x, 0x0000ffffU);
1459 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1460
1461 free(_hi16);
1462 This::hi16_list.pop_back();
1463 };
1464
1465 elfcpp::Swap<16, big_endian>::writeval(wvlo, vallo);
1466 return This::STATUS_OKAY;
1467 }
1468
1469 //R_MIPS_64
1470 static inline typename This::Status
1471 rel64(unsigned char* view,
1472 const Sized_relobj_file<size, big_endian>* object,
1473 const Symbol_value<size>* psymval,
1474 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1475 unsigned int rel_type)
1476 {
1477 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1478 Valtype* wv = reinterpret_cast<Valtype*>(view);
1479 Valtype addend;
1480
1481 if (rel_type == elfcpp::SHT_RELA)
1482 addend = addend_o;
1483 else
1484 addend = elfcpp::Swap<64, big_endian>::readval(wv);
1485
1486 Valtype x = psymval->value(object, addend);
1487 elfcpp::Swap<64, big_endian>::writeval(wv, x);
1488 return This::STATUS_OKAY;
1489 }
1490 static typename std::vector<struct hi16<size, big_endian> *> hi16_list;
1491
1492 // R_MIPS_GOT_DISP, R_MIPS_GOT_PAGE,
1493 // R_MIPS_TLS_GOTTPREL, R_MIPS_TLS_GD, R_MIPS_TLS_LDM
1494 static inline typename This::Status
1495 relgotofst(unsigned char* view,
1496 typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1497 {
1498 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1499 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1500 Valtype* wv = reinterpret_cast<Valtype*>(view);
1501 Reltype x = got_offset;
1502 elfcpp::Swap<16, big_endian>::writeval(wv, x);
1503 return (Bits<16>::has_signed_unsigned_overflow32(x)
1504 ? This::STATUS_OVERFLOW
1505 : This::STATUS_OKAY);
1506 }
1507
1508 // R_MIPS_GOT16, R_MIPS16_GOT16
1509 static inline typename This::Status
1510 relgot16(unsigned char* view,
1511 const Sized_relobj_file<size, big_endian>* object,
1512 const Symbol_value<size>* psymval,
1513 const typename elfcpp::Swap<size, big_endian>::Valtype _gp,
1514 unsigned char* got_view,
1515 const typename elfcpp::Swap<size, big_endian>::Valtype got_address,
1516 const typename elfcpp::Swap<size, big_endian>::Valtype got_offset,
1517 const Sized_symbol<size>* gsym,
1518 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1519 unsigned int rel_type)
1520 {
1521 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1522 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1523 Valtype* wv = reinterpret_cast<Valtype*>(view);
1524 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1525 Reltype x = got_address - _gp;
1526
1527 if (gsym != NULL)
1528 // TODO AS: Constant GOT_TYPE_STANDARD should be used.
1529 x += gsym->got_offset(0);
1530 else
1531 {
1532 Valtype addend;
1533
1534 if (rel_type == elfcpp::SHT_RELA)
1535 addend = Bits<16>::sign_extend32(addend_o);
1536 else
1537 addend = elfcpp::Swap<16, big_endian>::readval(wv);
1538
1539 // got_view points to beggining of location, data is written
1540 // in the second half
1541 relhi16(got_view + 2, psymval->value(object, addend << 16), 0);
1542 x = got_address + got_offset - _gp;
1543 }
1544
1545 val = Bits<16>::bit_select32(val, x, 0x0000ffffU);
1546
1547 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1548
1549 return (Bits<16>::has_signed_unsigned_overflow32(x)
1550 ? This::STATUS_OVERFLOW
1551 : This::STATUS_OKAY);
1552 }
1553
1554 // R_MIPS_GPREL16, R_MIPS16_GPREL and R_MIPS_LITERAL
1555 static inline typename This::Status
1556 relgprel(unsigned char* view,
1557 const Sized_relobj_file<size, big_endian>* object,
1558 const Symbol_value<size>* psymval,
1559 const typename elfcpp::Swap<size, big_endian>::Valtype _gp,
1560 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1561 unsigned int rel_type)
1562 {
1563 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1564 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1565 Valtype* wv = reinterpret_cast<Valtype*>(view);
1566 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1567 Reltype addend;
1568
1569 if (rel_type == elfcpp::SHT_RELA)
1570 addend = Bits<16>::sign_extend32(addend_o);
1571 else
1572 addend = Bits<16>::sign_extend32(val);
1573
1574 Reltype x = psymval->value(object, addend) - _gp;
1575 val = Bits<16>::bit_select32(val, x, 0x0000ffffU);
1576 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1577 return (Bits<16>::has_signed_unsigned_overflow32(x)
1578 ? This::STATUS_OVERFLOW
1579 : This::STATUS_OKAY);
1580 }
1581
1582 // R_MIPS_GPREL32
1583 static inline typename This::Status
1584 relgprel32(unsigned char* view,
1585 const Sized_relobj_file<size, big_endian>* object,
1586 const Symbol_value<size>* psymval,
1587 const typename elfcpp::Swap<size, big_endian>::Valtype gp,
1588 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1589 unsigned int rel_type)
1590 {
1591 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1592 size_t sec_num;
1593 for (sec_num = 0; sec_num < object->shnum(); sec_num++)
1594 if (((Object*)object)->section_type(sec_num) == elfcpp::SHT_MIPS_REGINFO)
1595 break;
1596
1597 gold::section_size_type section_size = 24;
1598 const unsigned char* reginfo = ((Object*)object)->section_contents(sec_num,
1599 &section_size, false) + 20;
1600 const Valtype* ri = reinterpret_cast<const Valtype*>(reginfo);
1601 Valtype gp0 = elfcpp::Swap<32, big_endian>::readval(ri);
1602 Valtype* wv = reinterpret_cast<Valtype*>(view);
1603 Valtype addend;
1604
1605 if (rel_type == elfcpp::SHT_RELA)
1606 addend = Bits<32>::sign_extend32(addend_o);
1607 else
1608 addend = elfcpp::Swap<32, big_endian>::readval(wv);
1609
1610 Valtype x = (psymval->value(object, addend)) + gp0 - gp;
1611 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1612 return This::STATUS_OKAY;
1613 }
1614
1615 // R_MIPS_GLOB_DAT
1616 static inline typename This::Status
1617 relglob(unsigned char* view,
1618 const Sized_relobj_file<size, big_endian>* object,
1619 const Symbol_value<size>* psymval)
1620 {
1621 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1622 Valtype* wv = reinterpret_cast<Valtype*>(view);
1623 elfcpp::Swap<32, big_endian>::writeval(wv, psymval->value(object, 0));
1624 return This::STATUS_OKAY;
1625 }
1626
1627 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_DTPREL32
1628 static inline typename This::Status
1629 tlsrel32(unsigned char* view,
1630 const Sized_relobj_file<size, big_endian>* object,
1631 const Symbol_value<size>* psymval,
1632 const typename elfcpp::Swap<32, big_endian>::Valtype offset,
1633 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1634 unsigned int rel_type)
1635 {
1636 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1637 Valtype* wv = reinterpret_cast<Valtype*>(view);
1638 Valtype addend;
1639
1640 if (rel_type == elfcpp::SHT_RELA)
1641 addend = Bits<32>::sign_extend32(addend_o);
1642 else
1643 addend = elfcpp::Swap<32, big_endian>::readval(wv);
1644
1645 Valtype x = psymval->value(object, addend) - offset;
1646 elfcpp::Swap<32, big_endian>::writeval(wv, x);
1647 return This::STATUS_OKAY;
1648 }
1649
1650 // R_MIPS_TLS_TPREL_LO16, R_MIPS_TLS_DTPREL_LO16
1651 static inline typename This::Status
1652 tlsrello16(unsigned char* view,
1653 const Sized_relobj_file<size, big_endian>* object,
1654 const Symbol_value<size>* psymval,
1655 const typename elfcpp::Swap<32, big_endian>::Valtype offset,
1656 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1657 unsigned int rel_type)
1658 {
1659 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1660 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1661 Valtype* wv = reinterpret_cast<Valtype*>(view);
1662 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1663 Reltype addend;
1664
1665 if (rel_type == elfcpp::SHT_RELA)
1666 addend = Bits<16>::sign_extend32(addend_o);
1667 else
1668 addend = Bits<16>::sign_extend32(val);
1669
1670 Reltype x = psymval->value(object, addend) - offset;
1671 val = Bits<16>::bit_select32(val, x, 0x0000ffffU);
1672 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1673 return (Bits<16>::has_signed_unsigned_overflow32(x)
1674 ? This::STATUS_OVERFLOW
1675 : This::STATUS_OKAY);
1676 }
1677
1678 // R_MIPS_TLS_TPREL_HI16, R_MIPS_TLS_DTPREL_HI16
1679 static inline typename This::Status
1680 tlsrelhi16(unsigned char* view,
1681 const Sized_relobj_file<size, big_endian>* object,
1682 const Symbol_value<size>* psymval,
1683 const typename elfcpp::Swap<32, big_endian>::Valtype offset,
1684 typename elfcpp::Elf_types<size>::Elf_Addr addend_o,
1685 unsigned int rel_type)
1686 {
1687 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1688 typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1689 Valtype* wv = reinterpret_cast<Valtype*>(view);
1690 Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1691 Reltype addend;
1692
1693 if (rel_type == elfcpp::SHT_RELA)
1694 addend = Bits<16>::sign_extend32(addend_o);
1695 else
1696 addend = Bits<16>::sign_extend32(val);
1697
1698 Reltype x = ((psymval->value(object, addend) - offset) + 0x8000U) >> 16;
1699 val = Bits<16>::bit_select32(val, x, 0x0000ffffU);
1700 elfcpp::Swap<16, big_endian>::writeval(wv, val);
1701 return (Bits<16>::has_signed_unsigned_overflow32(x)
1702 ? This::STATUS_OVERFLOW
1703 : This::STATUS_OKAY);
1704 }
1705 };
1706
1707 template<int size>
1708 void
1709 Mips_output_section_reginfo<size>::set_final_data_size()
1710 {
1711 // Set data size.
1712 this->set_data_size(24);
1713 }
1714
1715 template<int size>
1716 void
1717 Mips_output_section_reginfo<size>::do_write(Output_file* of)
1718 {
1719 // Set output data
1720 for (Input_section_list::const_iterator p = this->input_sections().begin();
1721 p != this->input_sections().end();
1722 ++p)
1723 {
1724 unsigned int shndx = p->shndx();
1725 Relobj* relobj = p->relobj();
1726
1727 section_size_type section_size;
1728 const unsigned char* section_contents =
1729 relobj->section_contents(shndx, &section_size, false);
1730
1731 for (size_t i = 0; i < 20; i++)
1732 this->data_[i] |= section_contents[i];
1733 }
1734
1735 unsigned long gp_val = 0;
1736 if (this->gp_ != NULL)
1737 gp_val = this->gp_->value();
1738
1739 this->data_[20]=gp_val & 0xff;
1740 this->data_[21]=(gp_val >> 8) & 0xff;
1741 this->data_[22]=(gp_val >> 16) & 0xff;
1742 this->data_[23]=(gp_val >> 24) & 0xff;
1743
1744 off_t offset = this->offset();
1745 off_t data_size = this->data_size();
1746
1747 unsigned char* view = of->get_output_view(offset, data_size);
1748 memcpy(view, this->data_, data_size);
1749 of->write_output_view(offset, data_size, view);
1750 }
1751
1752 // Return the value to use for a dynamic symbol which requires special
1753 // treatment. This is how we support equality comparisons of function
1754 // pointers across shared library boundaries, as described in the
1755 // processor specific ABI supplement.
1756
1757 template<int size, bool big_endian>
1758 uint64_t
1759 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1760 {
1761 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1762
1763 if (gsym->plt_offset() == 0)
1764 return 0;
1765
1766 if ((this->mips_stubs() == NULL) ||
1767 (this->mips_stubs()->is_plt_symbol((Sized_symbol<size> *)gsym)))
1768 return this->plt_section()->address() + gsym->plt_offset();
1769 else
1770 return this->mips_stubs()->address() +
1771 gsym->plt_offset() * (this->mips_stubs()->big_dyn_symtab() ? 20 : 16);
1772 }
1773
1774 template<int size, bool big_endian>
1775 typename std::vector<struct hi16<size, big_endian> *>
1776 Mips_relocate_functions<size, big_endian>::hi16_list;
1777
1778 // Get the dynamic reloc section, creating it if necessary.
1779
1780 template<int size, bool big_endian>
1781 typename Target_mips<size, big_endian>::Reloc_section*
1782 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
1783 {
1784 if (this->rel_dyn_ == NULL)
1785 {
1786 gold_assert(layout != NULL);
1787 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1788 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1789 elfcpp::SHF_ALLOC, this->rel_dyn_,
1790 ORDER_DYNAMIC_RELOCS, false);
1791 }
1792 return this->rel_dyn_;
1793 }
1794
1795 template<int size, bool big_endian>
1796 typename Target_mips<size, big_endian>::Reloca_section*
1797 Target_mips<size, big_endian>::rela_dyn_section(Layout* layout)
1798 {
1799 if (this->rela_dyn_ == NULL)
1800 {
1801 gold_assert(layout != NULL);
1802 this->rela_dyn_ = new Reloca_section(parameters->options().combreloc());
1803 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1804 elfcpp::SHF_ALLOC, this->rela_dyn_,
1805 ORDER_DYNAMIC_RELOCS, false);
1806 }
1807 return this->rela_dyn_;
1808 }
1809
1810 // Get the GOT section, creating it if necessary.
1811
1812 template<int size, bool big_endian>
1813 Mips_output_data_got<size, big_endian>*
1814 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
1815 Layout* layout)
1816 {
1817 if (this->got_ == NULL)
1818 {
1819 gold_assert(symtab != NULL && layout != NULL);
1820
1821 this->got_ = new Mips_output_data_got<size, big_endian>(symtab, layout);
1822
1823 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1824
1825 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1826 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
1827 elfcpp::SHF_MIPS_GPREL,
1828 this->got_, ORDER_DATA, false);
1829
1830 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1831 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1832 Symbol_table::PREDEFINED,
1833 this->got_,
1834 0, 0, elfcpp::STT_OBJECT,
1835 elfcpp::STB_GLOBAL,
1836 elfcpp::STV_DEFAULT, 0,
1837 false, false);
1838 }
1839
1840 // Set _gp value, too.
1841 this->set_gp(layout, symtab);
1842
1843 return this->got_;
1844 }
1845
1846 template<int size, bool big_endian>
1847 void
1848 Mips_output_data_got<size, big_endian>::add_tls_gd_with_static_reloc(
1849 unsigned int got_type,
1850 Symbol* gsym)
1851 {
1852 if (gsym->has_got_offset(got_type))
1853 return;
1854
1855 // We are doing a static link. Just mark it as belong to module 1,
1856 // the executable.
1857 unsigned int got_offset = this->add_constant(1);
1858 gsym->set_got_offset(got_type, got_offset);
1859 got_offset = this->add_constant(0);
1860 this->static_relocs_.push_back(Static_reloc(got_offset,
1861 size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : elfcpp::R_MIPS_TLS_DTPREL64,
1862 gsym));
1863 }
1864
1865 template<int size, bool big_endian>
1866 void
1867 Mips_output_data_got<size, big_endian>::add_tls_gd_local_reloc(
1868 unsigned int got_type,
1869 Sized_relobj_file<size, big_endian>* object,
1870 unsigned int index)
1871 {
1872 if (object->local_has_got_offset(index, got_type))
1873 return;
1874
1875 unsigned int got_offset = this->add_constant(1);
1876 object->set_local_got_offset(index, got_type, got_offset);
1877 got_offset = this->add_constant(0);
1878 this->static_relocs_.push_back(Static_reloc(got_offset,
1879 size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : elfcpp::R_MIPS_TLS_DTPREL64,
1880 object, index));
1881 }
1882
1883 // Calculate value of gp_.
1884
1885 template<int size, bool big_endian>
1886 void
1887 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
1888 {
1889 if (gp_ == NULL)
1890 {
1891 Sized_symbol<size>* sym =
1892 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
1893
1894 if ((sym == NULL) || (sym->source() != Symbol::IS_CONSTANT))
1895 {
1896 Output_data *got_section = layout->find_output_section(".got");
1897
1898 gold_assert(got_section != NULL);
1899
1900 if (sym == NULL)
1901 sym =static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
1902 "_gp", NULL, Symbol_table::PREDEFINED,
1903 got_section, 0x7ff0, 0,
1904 elfcpp::STT_OBJECT,
1905 elfcpp::STB_GLOBAL,
1906 elfcpp::STV_DEFAULT,
1907 0, false, false));
1908 else
1909 {
1910 sym->init_output_data("_gp", NULL, got_section, 0x7ff0, 0,
1911 elfcpp::STT_OBJECT,
1912 elfcpp::STB_GLOBAL,
1913 elfcpp::STV_DEFAULT, 0,
1914 false, false);
1915 }
1916 gold_assert(sym != NULL);
1917 }
1918
1919 this->gp_ = sym;
1920 }
1921 }
1922
1923 // A class to handle the STUB data.
1924
1925 template<int size, bool big_endian>
1926 class Mips_output_data_stub : public Output_section_data
1927 {
1928 public:
1929 typedef std::vector<Symbol *> symbol_list;
1930 Mips_output_data_stub();
1931
1932 // Add an entry to the STUB.
1933 void add_entry(Symbol_table* symtab,
1934 Target_mips<size, big_endian>* target,
1935 Symbol* gsym);
1936
1937 // Return the number of STUB entries.
1938 unsigned int
1939 entry_count() const
1940 { return this->count_; }
1941
1942 // Add stub symbol
1943 void
1944 add_symbol(Symbol *sym)
1945 { symbols.push_back(sym); }
1946
1947 // Get symbol on position n
1948 Symbol *
1949 symbol(unsigned int n)
1950 { return symbols[n]; }
1951
1952 protected:
1953 void do_adjust_output_section(Output_section* os);
1954
1955 private:
1956 // Template for subsequent STUB entries.
1957 static const uint32_t stub_entry[4];
1958
1959 // Set the final size.
1960 void
1961 set_final_data_size()
1962 {
1963 unsigned int full_count = this->count_ * 4;
1964
1965 this->set_data_size(full_count * 4);
1966 }
1967
1968 // Write out the STUB data.
1969 void
1970 do_write(Output_file*);
1971
1972 // The number of STUB entries.
1973 unsigned int count_;
1974
1975 // List of symbols used in stubs
1976 symbol_list symbols;
1977 };
1978
1979 // A class to handle the PLT data.
1980
1981 template<int size, bool big_endian>
1982 class Mips_output_data_plt : public Output_section_data
1983 {
1984 public:
1985 typedef Output_data_reloc<elfcpp::SHT_REL, true,
1986 size, big_endian> Reloc_section;
1987
1988 Mips_output_data_plt(Layout*, Output_data_space*);
1989
1990 // Add an entry to the PLT.
1991 void add_entry(Symbol* gsym);
1992
1993 // Return the .rel.plt section data.
1994 const Reloc_section* rel_plt() const
1995 { return this->rel_; }
1996
1997 // Return the number of PLT entries.
1998 unsigned int
1999 entry_count() const
2000 { return this->count_; }
2001
2002 // Return the offset of the first non-reserved PLT entry.
2003 static unsigned int
2004 first_plt_entry_offset()
2005 { return sizeof(first_plt_entry[0]); }
2006
2007 // Return the size of a PLT entry.
2008 static unsigned int
2009 plt_entry_size()
2010 { return sizeof(plt_entry); }
2011
2012 protected:
2013 void do_adjust_output_section(Output_section* os);
2014
2015 // Write to a map file.
2016 void
2017 do_print_to_mapfile(Mapfile* mapfile) const
2018 { mapfile->print_output_data(this, _(".plt")); }
2019
2020 private:
2021 // Template for the first PLT entry.
2022 static const uint32_t first_plt_entry[3][8];
2023
2024 // Template for subsequent PLT entries.
2025 static const uint32_t plt_entry[4];
2026
2027 // Set the final size.
2028 void
2029 set_final_data_size()
2030 {
2031 unsigned int full_count = this->count_ * 4 + 8;
2032
2033 this->set_data_size(full_count * 4);
2034 }
2035
2036 // MIPS abi types
2037 enum Abi_type
2038 {
2039 MIPS_ABI_ERROR = -1, // Error when getting ABI type
2040 MIPS_O32 = 0, // Mips O32 ABI
2041 MIPS_N32 = 1, // Mips N32 ABI
2042 MIPS_N64 = 2, // Mips N64 ABI
2043 MIPS_O64 = 3, // Mips O64 ABI
2044 MIPS_EABI32 = 4, // Mips 32 EABI
2045 MIPS_EABI64 = 5, // Mips 64 EABI
2046 };
2047
2048 // Get MIPS abi
2049 Abi_type
2050 abi(elfcpp::Elf_Word /*flags*/) const
2051 {
2052 #if 0
2053 // MIPS ABI2
2054 if ((flags & elfcpp::EF_MIPS_ABI2) != 0)
2055 {
2056 switch (flags & elfcpp::EF_MIPS_ABI)
2057 {
2058 case elfcpp::E_MIPS_ABI_O32:
2059 return MIPS_N32;
2060 case elfcpp::E_MIPS_ABI_O64:
2061 return MIPS_N64;
2062 case elfcpp::E_MIPS_ABI_EABI32:
2063 return MIPS_EABI32; // TODO AS: Check if this is correct!!!!
2064 case elfcpp::E_MIPS_ABI_EABI64:
2065 return MIPS_EABI64; // TODO AS: Check if this is correct!!!!
2066 default:
2067 return MIPS_ABI_ERROR;
2068 }
2069 }
2070 else
2071 {
2072 switch (flags & elfcpp::EF_MIPS_ABI)
2073 {
2074 case elfcpp::E_MIPS_ABI_O32:
2075 return MIPS_O32;
2076 case elfcpp::E_MIPS_ABI_O64:
2077 return MIPS_O64;
2078 case elfcpp::E_MIPS_ABI_EABI32:
2079 return MIPS_EABI32; // TODO AS: Check if this is correct!!!!
2080 case elfcpp:: E_MIPS_ABI_EABI64:
2081 return MIPS_EABI64; // TODO AS: Check if this is correct!!!!
2082 default:
2083 return MIPS_ABI_ERROR;
2084 }
2085 }
2086 #endif
2087 // TODO AS: For now only support O32 ABI
2088 return MIPS_O32;
2089 }
2090
2091 // Write out the PLT data.
2092 void
2093 do_write(Output_file*);
2094
2095 // The reloc section.
2096 Reloc_section* rel_;
2097 // The .got.plt section.
2098 Output_data_space* got_plt_;
2099 // The number of PLT entries.
2100 unsigned int count_;
2101 };
2102
2103 // A class to handle the .MIPS.stubs data.
2104
2105 template<int size, bool big_endian>
2106 class Mips_output_data_mips_stubs : public Output_section_data
2107 {
2108 public:
2109 Mips_output_data_mips_stubs(Layout* layout,
2110 Mips_output_data_got<size, big_endian>* got, Sized_symbol<size>* gp)
2111 : Output_section_data(size == 32 ? 4 : 8), count_(1), got_(got), gp_(gp),
2112 layout_(layout), big_dyn_symtab_(false)
2113 { };
2114
2115 // Add an entry to the .MIPS.stubs.
2116 void add_entry(Symbol* gsym);
2117
2118 // Return the number of .MIPS.stubs entries.
2119 unsigned int
2120 entry_count() const
2121 { return this->count_; }
2122
2123 // Return if symbol is in .plt
2124 bool
2125 is_plt_symbol(Sized_symbol<size> *sym)
2126 {
2127 if (this->symbols_.size() == 0)
2128 return true;
2129
2130 return (this->symbols_.count(sym) == 0);
2131 }
2132
2133 // Remove symbol that should not have entry.
2134 void
2135 remove(Symbol *sym)
2136 {
2137 if (this->symbols_.size() == 0)
2138 return;
2139
2140 if (this->symbols_.count(sym) == 0)
2141 return;
2142
2143 this->symbols_.erase(sym);
2144 --this->count_;
2145 }
2146
2147 // Set if dynamic symbole table has more then 0x10000 entries.
2148 void
2149 set_big_dyn_symtab(bool big_dyn_symtab)
2150 { big_dyn_symtab_ = big_dyn_symtab; }
2151
2152 // If dynamic symbole table has more then 0x10000 entries.
2153 bool
2154 big_dyn_symtab()
2155 { return this->big_dyn_symtab_; }
2156
2157 // Fix values of used symbols in symbol table.
2158 void
2159 fix_symbol_values();
2160
2161 protected:
2162 void do_adjust_output_section(Output_section* os);
2163
2164 // Write to a map file.
2165 void
2166 do_print_to_mapfile(Mapfile* mapfile) const
2167 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
2168
2169 private:
2170 // Template for the .MIPS.stubs instructions.
2171 // lw t9, .got offset from _gp
2172 unsigned long
2173 stub_lw()
2174 { return 0x8f990000 | ((this->got_->address() - this->gp_->value()) & 0xffff) ; }
2175
2176 // addu t7,ra
2177 unsigned long
2178 stub_move()
2179 { return 0x03e07821; }
2180
2181 // lui t8,val
2182 unsigned long
2183 stub_lui(int val)
2184 { return (0x3c180000 + (val & 0xffff)); }
2185
2186 //jalr t9,ra
2187 unsigned long
2188 stub_jalr()
2189 { return 0x0320f809; }
2190
2191 // ori t8,t8,val
2192 unsigned long
2193 stub_ori(int val)
2194 { return (0x37180000 + (val & 0xffff)); }
2195
2196 // ori t8,zero,val unsigned
2197 unsigned long
2198 stub_li16u(int val)
2199 { return (0x34180000 + (val & 0xffff)); }
2200
2201 // addiu t8,zero,val sign extended
2202 unsigned long
2203 stub_li16s(int val)
2204 { return (0x24180000 + (val & 0xffff)); }
2205
2206 // Set the final size.
2207 void
2208 set_final_data_size()
2209 {
2210 unsigned int full_count = this->count_ * (this->big_dyn_symtab_ ? 5 : 4);
2211
2212 this->set_data_size(full_count * 4);
2213 }
2214
2215 // Write out the .MIPS.stubs data.
2216 void
2217 do_write(Output_file*);
2218
2219 // The number of .MIPS.stubs entries.
2220 unsigned int count_;
2221 // .got section
2222 Mips_output_data_got<size, big_endian>* got_;
2223 // _gp symbol
2224 Sized_symbol<size>* gp_;
2225 // Layout
2226 Layout* layout_;
2227 // Set if dynamic symbol table has more then 0x10000 entries.
2228 bool big_dyn_symtab_;
2229 // .MIPS.stubs symbols
2230 std::set<Symbol *> symbols_;
2231 };
2232
2233 template<int size, bool big_endian>
2234 void
2235 Mips_output_data_mips_stubs<size, big_endian>::do_adjust_output_section(
2236 Output_section* os)
2237 { os->set_entsize(0); }
2238
2239 // Add an entry to the .MIPS.stubs.
2240
2241 template<int size, bool big_endian>
2242 void
2243 Mips_output_data_mips_stubs<size, big_endian>::add_entry(Symbol* sym)
2244 {
2245 gold_assert(!sym->has_plt_offset());
2246 sym->set_plt_offset(0*this->count_);
2247
2248 this->symbols_.insert(sym);
2249
2250 sym->set_needs_dynsym_entry();
2251 ++this->count_;
2252 }
2253
2254 // Fix values of used symbols in symbol table.
2255 template<int size, bool big_endian>
2256 void
2257 Mips_output_data_mips_stubs<size, big_endian>::fix_symbol_values()
2258 {
2259 int i = 0;
2260 for (std::set<Symbol *>::const_iterator p = this->symbols_.begin();
2261 p != this->symbols_.end(); ++p, i++)
2262 {
2263 Sized_symbol<size> *sym = static_cast<Sized_symbol<size>*> (*p);
2264 elfcpp::Elf_Xword value = this->address() + i
2265 * (this->big_dyn_symtab_ ? 20 : 16);
2266 sym->set_value(value);
2267 }
2268 }
2269
2270 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
2271 // adjusts them as needed.
2272
2273 template<int size, bool big_endian>
2274 void
2275 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
2276 {
2277 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2278
2279 // Read processor-specific flags in ELF file header.
2280 const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset,
2281 elfcpp::Elf_sizes<size>::ehdr_size);
2282 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
2283
2284 const off_t offset = this->offset();
2285 const section_size_type oview_size =
2286 convert_to_section_size_type(this->data_size()) +
2287 (this->big_dyn_symtab_ * (this->symbols_.size() + 1) * 4);
2288 unsigned char* const oview = of->get_output_view(offset, oview_size);
2289
2290 unsigned char* pov = oview;
2291
2292 for (std::set<Symbol *>::const_iterator p = this->symbols_.begin();
2293 p != this->symbols_.end(); ++p)
2294 {
2295 // Fill the stub.
2296 uint32_t stub_insn0 = stub_lw();
2297 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0);
2298 pov += 4;
2299 uint32_t stub_insn1 = stub_move();
2300 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn1);
2301 pov += 4;
2302 if (this->big_dyn_symtab_)
2303 {
2304 uint32_t stub_insn2 = stub_lui(((*p)->dynsym_index() >> 16) & 0x7fff);
2305 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn2);
2306 pov += 4;
2307 }
2308 uint32_t stub_insn3 = stub_jalr();
2309 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn3);
2310 pov += 4;
2311 if (this->big_dyn_symtab_)
2312 {
2313 uint32_t stub_insn4 = stub_ori((*p)->dynsym_index() & 0xffff);
2314 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn4);
2315 }
2316 else if ((*p)->dynsym_index() & ~0x7fff)
2317 {
2318 uint32_t stub_insn5 = stub_li16u((*p)->dynsym_index() & 0xffff);
2319 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn5);
2320 }
2321 else
2322 {
2323 uint32_t stub_insn6 = stub_li16s((*p)->dynsym_index());
2324 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn6);
2325 }
2326 pov += 4;
2327 }
2328
2329 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2330 pov += 4;
2331 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2332 pov += 4;
2333 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2334 pov += 4;
2335 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2336 pov += 4;
2337
2338 if (this->big_dyn_symtab_)
2339 {
2340 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2341 pov += 4;
2342 }
2343
2344 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2345
2346 of->write_output_view(offset, oview_size, oview);
2347 }
2348
2349 // Create the PLT section. The ordinary .got section is an argument,
2350 // since we need to refer to the start.
2351
2352 template<int size, bool big_endian>
2353 Mips_output_data_plt<size, big_endian>::Mips_output_data_plt(Layout* layout,
2354 Output_data_space* got_plt)
2355 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), count_(0)
2356 {
2357 this->rel_ = new Reloc_section(false);
2358 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2359 elfcpp::SHF_ALLOC, this->rel_,
2360 ORDER_DYNAMIC_PLT_RELOCS, false);
2361 }
2362
2363 template<int size, bool big_endian>
2364 void
2365 Mips_output_data_plt<size, big_endian>::do_adjust_output_section(
2366 Output_section* os)
2367 { os->set_entsize(0); }
2368
2369 // Add an entry to the PLT.
2370
2371 template<int size, bool big_endian>
2372 void
2373 Mips_output_data_plt<size, big_endian>::add_entry(Symbol* gsym)
2374 {
2375 gold_assert(!gsym->has_plt_offset());
2376 section_offset_type plt_offset = (this->count_) * sizeof(plt_entry)
2377 + sizeof(first_plt_entry[0]);
2378 gsym->set_plt_offset(plt_offset);
2379
2380 ++this->count_;
2381
2382 section_offset_type got_offset = this->got_plt_->current_data_size();
2383
2384 // Every PLT entry needs a GOT entry which points back to the PLT
2385 // entry (this will be changed by the dynamic linker, normally
2386 // lazily when the function is called).
2387 this->got_plt_->set_current_data_size(got_offset + 4);
2388
2389 gsym->set_needs_dynsym_entry();
2390 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
2391 got_offset);
2392 }
2393
2394 // Mips PLTs.
2395
2396 // The first entry in the PLT.
2397
2398 template<int size, bool big_endian>
2399 const uint32_t Mips_output_data_plt<size, big_endian>::first_plt_entry[3][8] =
2400 {
2401 // The format of the first PLT entry in an O32 executable.
2402 {
2403 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
2404 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
2405 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
2406 0x031cc023, // subu $24, $24, $28
2407 0x03e07821, // move $15, $31
2408 0x0018c082, // srl $24, $24, 2
2409 0x0320f809, // jalr $25
2410 0x2718fffe // subu $24, $24, 2
2411 },
2412
2413 // The format of the first PLT entry in an N32 executable. Different
2414 // because gp ($28) is not available; we use t2 ($14) instead.
2415 {
2416 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
2417 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
2418 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
2419 0x030ec023, // subu $24, $24, $14
2420 0x03e07821, // move $15, $31
2421 0x0018c082, // srl $24, $24, 2
2422 0x0320f809, // jalr $25
2423 0x2718fffe // subu $24, $24, 2
2424 },
2425
2426 // The format of the first PLT entry in an N64 executable. Different
2427 // from N32 because of the increased size of GOT entries.
2428 {
2429 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
2430 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
2431 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
2432 0x030ec023, // subu $24, $24, $14
2433 0x03e07821, // move $15, $31
2434 0x0018c0c2, // srl $24, $24, 3
2435 0x0320f809, // jalr $25
2436 0x2718fffe // subu $24, $24, 2
2437 }
2438 };
2439
2440 // Subsequent entries in the PLT.
2441
2442 template<int size, bool big_endian>
2443 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[4] =
2444 {
2445 0x3c0f0000, // lui $15, %hi(.got.plt entry)
2446 0x8df90000, // l[wd] $25, %lo(.got.plt entry)($15)
2447 0x03200008, // jr $25
2448 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
2449 };
2450
2451 // Write out the PLT. This uses the hand-coded instructions above,
2452 // and adjusts them as needed.
2453
2454 template<int size, bool big_endian>
2455 void
2456 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
2457 {
2458 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2459
2460 // Read processor-specific flags in ELF file header.
2461 const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset,
2462 elfcpp::Elf_sizes<size>::ehdr_size);
2463 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
2464 const size_t abi = this->abi(ehdr.get_e_flags());
2465
2466 const off_t offset = this->offset();
2467 const section_size_type oview_size =
2468 convert_to_section_size_type(this->data_size());
2469 unsigned char* const oview = of->get_output_view(offset, oview_size);
2470
2471 const off_t got_file_offset = this->got_plt_->offset();
2472 const section_size_type got_size =
2473 convert_to_section_size_type(this->got_plt_->data_size());
2474 unsigned char* const got_view = of->get_output_view(got_file_offset,
2475 got_size);
2476 unsigned char* pov = oview;
2477
2478 Mips_address plt_address = this->address();
2479 Mips_address got_address = this->got_plt_->address();
2480
2481 uint32_t plt_insn0 = first_plt_entry[abi][0] | (((got_address + 0x8000) >> 16) & 0xffff);
2482 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2483 uint32_t plt_insn1 = first_plt_entry[abi][1] | (got_address & 0xffff);
2484 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2485 uint32_t plt_insn2 = first_plt_entry[abi][2] | (got_address & 0xffff);
2486 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2487
2488 for (size_t i = 3; i < 8; i++)
2489 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4,
2490 first_plt_entry[abi][i]);
2491
2492 pov += sizeof(first_plt_entry[abi]);
2493
2494 unsigned char* got_pov = got_view;
2495
2496 memset(got_pov, 0, 8);
2497 got_pov += 8;
2498
2499 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
2500 unsigned int plt_offset = sizeof(first_plt_entry[abi]);
2501 unsigned int plt_rel_offset = 0;
2502 unsigned int got_offset = 8;
2503 const unsigned int count = this->count_;
2504 for (unsigned int i = 0;
2505 i < count;
2506 ++i,
2507 pov += sizeof(plt_entry),
2508 got_pov += 4,
2509 plt_offset += sizeof(plt_entry),
2510 plt_rel_offset += rel_size,
2511 got_offset += 4)
2512 {
2513 // Set and adjust the PLT entry itself.
2514 int32_t offset = (got_address + got_offset);
2515
2516 uint32_t plt_insn0 = plt_entry[0] | (((offset + 0x8000) >> 16) & 0xffff);
2517 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2518 uint32_t plt_insn1 = plt_entry[1] | (offset & 0xffff);
2519 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2520 uint32_t plt_insn2 = plt_entry[2];
2521 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2522 uint32_t plt_insn3 = plt_entry[3] | (offset & 0xffff);
2523 elfcpp::Swap<32, big_endian>::writeval(pov + 12, plt_insn3);
2524
2525 // Set the entry in the GOT.
2526 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
2527 }
2528
2529 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2530 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2531
2532 of->write_output_view(offset, oview_size, oview);
2533 of->write_output_view(got_file_offset, got_size, got_view);
2534 }
2535
2536 // Create a GOT entry for the TLS module index.
2537
2538 template<int size, bool big_endian>
2539 unsigned int
2540 Target_mips<size, big_endian>::got_mod_index_entry(
2541 Symbol_table* symtab,
2542 Layout* layout,
2543 Sized_relobj_file<size, big_endian>* object)
2544 {
2545 if (this->got_mod_index_offset_ == -1U)
2546 {
2547 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2548 Mips_output_data_got<size, big_endian>* got =
2549 this->got_section(symtab, layout);
2550 unsigned int got_offset;
2551 if (!parameters->doing_static_link())
2552 {
2553 got_offset = got->add_constant(0);
2554
2555 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
2556 rel_dyn->add_local(object, 0,
2557 size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 :
2558 elfcpp::R_MIPS_TLS_DTPMOD64, got,
2559 got_offset);
2560 }
2561 else
2562 {
2563 // We are doing a static link. Just mark it as belong to module 1,
2564 // the executable.
2565 got_offset = got->add_constant(1);
2566 }
2567
2568 got->add_constant(0);
2569 this->got_mod_index_offset_ = got_offset;
2570 }
2571
2572 return this->got_mod_index_offset_;
2573 }
2574
2575 // Create a PLT entry for a global symbol.
2576
2577 template<int size, bool big_endian>
2578 void
2579 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
2580 Layout* layout,
2581 Symbol* gsym)
2582 {
2583 if (gsym->has_plt_offset())
2584 return;
2585
2586 if (this->plt_ == NULL)
2587 {
2588 // Create the GOT section first.
2589 this->got_section(symtab, layout);
2590
2591 // Create .rel.dyn section.
2592 this->rel_dyn_section(layout);
2593
2594 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2595 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
2596 this->got_plt_, ORDER_DATA, false);
2597
2598 // The first two entries are reserved.
2599 this->got_plt_->set_current_data_size(2 * 4);
2600
2601 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
2602 this->got_plt_);
2603 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2604 (elfcpp::SHF_ALLOC
2605 | elfcpp::SHF_EXECINSTR),
2606 this->plt_, ORDER_PLT, false);
2607
2608 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
2609 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
2610 Symbol_table::PREDEFINED,
2611 this->plt_,
2612 0, 0, elfcpp::STT_FUNC,
2613 elfcpp::STB_LOCAL,
2614 elfcpp::STV_DEFAULT, 0,
2615 false, false);
2616 }
2617
2618 this->plt_->add_entry(gsym);
2619 }
2620
2621
2622 // Create a .MIPS.stubs entry for a global symbol.
2623
2624 template<int size, bool big_endian>
2625 void
2626 Target_mips<size, big_endian>::make_mips_stubs_entry(Symbol_table* symtab,
2627 Layout* layout,
2628 Symbol* gsym)
2629 {
2630 if (gsym->has_plt_offset())
2631 return;
2632
2633 if (this->mips_stubs_ == NULL)
2634 {
2635 // Create the .MIPS.stubs section first.
2636
2637 this->mips_stubs_ = new Mips_output_data_mips_stubs<size, big_endian>
2638 (layout, this->got_section(symtab, layout), this->gp_);
2639
2640 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
2641 (elfcpp::SHF_ALLOC
2642 | elfcpp::SHF_EXECINSTR),
2643 this->mips_stubs_, ORDER_PLT, false);
2644 }
2645 this->mips_stubs_->add_entry(gsym);
2646 }
2647
2648 template<int size, bool big_endian>
2649 Mips_output_data_stub<size, big_endian>::Mips_output_data_stub()
2650 : Output_section_data(size == 32 ? 4 : 8), count_(0)
2651 {}
2652
2653 template<int size, bool big_endian>
2654 void
2655 Mips_output_data_stub<size, big_endian>::do_adjust_output_section(
2656 Output_section* os)
2657 {
2658 os->set_entsize(0);
2659 }
2660
2661 // Add an entry to the STUB.
2662
2663 template<int size, bool big_endian>
2664 void
2665 Mips_output_data_stub<size, big_endian>::add_entry(Symbol_table* symtab,
2666 Target_mips<size, big_endian> * target,
2667 Symbol* gsym)
2668 {
2669 Symbol* sym;
2670 char* buffer = new char[strlen(gsym->name()) + 6];
2671 gold_assert(buffer != NULL);
2672
2673 section_offset_type stub_offset = this->count_ * 4 * 4;
2674
2675 ++this->count_;
2676
2677 sprintf(buffer, ".pic.%s", gsym->name());
2678
2679 sym = symtab->define_in_output_data(buffer, NULL,
2680 Symbol_table::PREDEFINED,
2681 target->stub(),
2682 stub_offset, 0x10, elfcpp::STT_FUNC,
2683 elfcpp::STB_GLOBAL,
2684 elfcpp::STV_DEFAULT, 0,
2685 false, false);
2686
2687 target->add_stub_symbol(gsym->name(), sym);
2688 this->add_symbol(gsym);
2689
2690 delete [] buffer;
2691 }
2692
2693
2694 // Create a STUB entry for a global symbol.
2695
2696 template<int size, bool big_endian>
2697 void
2698 Target_mips<size, big_endian>::make_stub_entry(Layout* layout)
2699 {
2700 if (this->stub_ == NULL)
2701 {
2702 this->stub_ = new Mips_output_data_stub<size, big_endian>();
2703 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2704 (elfcpp::SHF_ALLOC
2705 | elfcpp::SHF_EXECINSTR
2706 | elfcpp::SHF_WRITE),
2707 this->stub_, ORDER_TEXT, false);
2708 }
2709 }
2710
2711 template<int size, bool big_endian>
2712 const uint32_t Mips_output_data_stub<size, big_endian>::stub_entry[4] =
2713 {
2714 0x3c190000, // lui $25, %hi(func)
2715 0x08000000, // j func
2716 0x27390000, // add $25, $25, %lo(func)
2717 0x00000000 // nop
2718 };
2719
2720 // Write out the STUB. This uses the hand-coded instructions above,
2721 // and adjusts them as needed.
2722
2723 template<int size, bool big_endian>
2724 void
2725 Mips_output_data_stub<size, big_endian>::do_write(Output_file* of)
2726 {
2727 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2728
2729 const off_t offset = this->offset();
2730 const section_size_type oview_size =
2731 convert_to_section_size_type(this->data_size());
2732 unsigned char* const oview = of->get_output_view(offset, oview_size);
2733
2734 unsigned char* pov = oview;
2735
2736 const unsigned int count = this->count_;
2737 for (unsigned int i = 0;
2738 i < count;
2739 ++i,
2740 pov += 4*4)
2741 {
2742 Sized_symbol<size> *sym = static_cast<Sized_symbol<size>*>
2743 (this->symbol(i));
2744
2745 unsigned int value = (unsigned int)sym->value();
2746 gold_assert(offset >= 0 && offset < 0x0fffffff);
2747 uint32_t stub_insn0 = stub_entry[0] | (((value + 0x8000) >> 16) & 0xffff);
2748 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0);
2749 uint32_t stub_insn1 = stub_entry[1] | (((value >> 2) & 0x03ffffff));
2750 elfcpp::Swap<32, big_endian>::writeval(pov + 4, stub_insn1);
2751 uint32_t stub_insn2 = stub_entry[2] | ((value & 0x0000ffff));
2752 elfcpp::Swap<32, big_endian>::writeval(pov + 8, stub_insn2);
2753 uint32_t stub_insn3 = stub_entry[3];
2754 elfcpp::Swap<32, big_endian>::writeval(pov + 12, stub_insn3);
2755 }
2756 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2757
2758 of->write_output_view(offset, oview_size, oview);
2759 }
2760
2761 // Process the relocations to determine unreferenced sections for
2762 // garbage collection.
2763
2764 template<int size, bool big_endian>
2765 void
2766 Target_mips<size, big_endian>::gc_process_relocs(
2767 Symbol_table* symtab,
2768 Layout* layout,
2769 Sized_relobj_file<size, big_endian>* object,
2770 unsigned int data_shndx,
2771 unsigned int,
2772 const unsigned char* prelocs,
2773 size_t reloc_count,
2774 Output_section* output_section,
2775 bool needs_special_offset_handling,
2776 size_t local_symbol_count,
2777 const unsigned char* plocal_symbols)
2778 {
2779 typedef Target_mips<size, big_endian> Mips;
2780 typedef typename Target_mips<size, big_endian>::Scan Scan;
2781
2782 gold::gc_process_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan,
2783 typename Target_mips::Relocatable_size_for_reloc>(
2784 symtab,
2785 layout,
2786 this,
2787 object,
2788 data_shndx,
2789 prelocs,
2790 reloc_count,
2791 output_section,
2792 needs_special_offset_handling,
2793 local_symbol_count,
2794 plocal_symbols);
2795 }
2796
2797 // Scan the relocations to look for symbol adjustments.
2798
2799 template<int size, bool big_endian>
2800 void
2801 Target_mips<size, big_endian>::scan_relocs(
2802 Symbol_table* symtab,
2803 Layout* layout,
2804 Sized_relobj_file<size, big_endian>* object,
2805 unsigned int data_shndx,
2806 unsigned int sh_type,
2807 const unsigned char* prelocs,
2808 size_t reloc_count,
2809 Output_section* output_section,
2810 bool needs_special_offset_handling,
2811 size_t local_symbol_count,
2812 const unsigned char* plocal_symbols)
2813 {
2814 typedef Target_mips<size, big_endian> Mips;
2815 typedef typename Target_mips<size, big_endian>::Scan Scan;
2816
2817 if (sh_type == elfcpp::SHT_REL)
2818 gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan>(
2819 symtab,
2820 layout,
2821 this,
2822 object,
2823 data_shndx,
2824 prelocs,
2825 reloc_count,
2826 output_section,
2827 needs_special_offset_handling,
2828 local_symbol_count,
2829 plocal_symbols);
2830 else if (sh_type == elfcpp::SHT_RELA)
2831 gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_RELA, Scan>(
2832 symtab,
2833 layout,
2834 this,
2835 object,
2836 data_shndx,
2837 prelocs,
2838 reloc_count,
2839 output_section,
2840 needs_special_offset_handling,
2841 local_symbol_count,
2842 plocal_symbols);
2843 }
2844
2845 template<int size, bool big_endian>
2846 bool
2847 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
2848 {
2849 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
2850 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
2851 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
2852 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
2853 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
2854 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
2855 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
2856 }
2857
2858 template<int size, bool big_endian>
2859 bool
2860 Target_mips<size, big_endian>::mips_mach_extends(elfcpp::Elf_Word base,
2861 elfcpp::Elf_Word extension)
2862 {
2863 unsigned int i;
2864
2865 if (base == extension)
2866 return true;
2867
2868 if ((base == elfcpp::E_MIPS_ARCH_32)
2869 && mips_mach_extends(elfcpp::E_MIPS_ARCH_64, extension))
2870 return true;
2871
2872 if ((base == elfcpp::E_MIPS_ARCH_32R2)
2873 && mips_mach_extends(elfcpp::E_MIPS_ARCH_64R2, extension))
2874 return true;
2875
2876 for (i = 0; i < this->extensions_.size(); i++)
2877 if (extension == this->extensions_[i].second)
2878 {
2879 extension = this->extensions_[i].first;
2880 if (extension == base)
2881 return true;
2882 }
2883
2884 return false;
2885 }
2886
2887 template<int size, bool big_endian>
2888 void
2889 Target_mips<size, big_endian>::merge_processor_specific_flags(const char* name,
2890 elfcpp::Elf_Word in_flags)
2891 {
2892 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
2893
2894 // If flags are not set yet, just copy them.
2895 if (out_flags == 0)
2896 this->set_processor_specific_flags(in_flags);
2897 else
2898 {
2899 elfcpp::Elf_Word new_flags = out_flags;
2900 new_flags |= in_flags & elfcpp::EF_MIPS_NOREORDER;
2901
2902 // Check flag compatibility.
2903 in_flags &= ~elfcpp::EF_MIPS_NOREORDER;
2904 out_flags &= ~elfcpp::EF_MIPS_NOREORDER;
2905
2906 // Some IRIX 6 BSD-compatibility objects have this bit set. It
2907 // doesn't seem to matter.
2908 in_flags &= ~elfcpp::EF_MIPS_XGOT;
2909 out_flags &= ~elfcpp::EF_MIPS_XGOT;
2910
2911 // MIPSpro generates ucode info in n64 objects. Again, we should
2912 // just be able to ignore this.
2913 in_flags &= ~elfcpp::EF_MIPS_UCODE;
2914 out_flags &= ~elfcpp::EF_MIPS_UCODE;
2915
2916 if ((in_flags & elfcpp::EF_MIPS_DYNAMIC) != 0)
2917 in_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
2918
2919 // Input flags are same as output, do nothing.
2920 if (in_flags == out_flags)
2921 return;
2922
2923 // TODO AS: If input file has no sections, its flags are not important.
2924
2925 if (((in_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
2926 != ((out_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
2927 gold_warning(_("Linking abicalls files with non-abicalls files."));
2928
2929 if (in_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
2930 new_flags |= elfcpp::EF_MIPS_CPIC;
2931 if (!(in_flags & elfcpp::EF_MIPS_PIC))
2932 new_flags &= ~elfcpp::EF_MIPS_PIC;
2933
2934 in_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
2935 out_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
2936
2937 // Compare the ISAs.
2938 if (mips_32bit_flags(out_flags) != mips_32bit_flags(in_flags))
2939 {
2940 gold_error(_("Source object %s is %d-bit, but output is %d-bit"),
2941 name, mips_32bit_flags(in_flags) ? 32 : 64,
2942 mips_32bit_flags(out_flags) ? 32 : 64);
2943 }
2944 else if (!mips_mach_extends(in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MI PS_MACH),
2945 out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)))
2946 {
2947 // Input ISA is not set. Just skip it.
2948 if ((in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)) == 0);
2949 // ISA is not set yet, do it now.
2950 else if ((out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)) == 0)
2951 new_flags |= in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE);
2952 // OBFD's ISA isn't the same as, or an extension of, IBFD's.
2953 else if (mips_mach_extends(out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::E F_MIPS_MACH),
2954 in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)))
2955 {
2956 // Copy the architecture info from IBFD to OBFD. Also copy
2957 // the 32-bit flag (if set) so that we continue to recognise
2958 // OBFD as a 32-bit binary.
2959 new_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
2960 new_flags |=
2961 in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp ::EF_MIPS_32BITMODE);
2962
2963 // Copy across the ABI flags if OBFD doesn't use them
2964 // and if that was what caused us to treat IBFD as 32-bit.
2965 if ((out_flags & elfcpp::EF_MIPS_ABI) == 0
2966 && mips_32bit_flags(in_flags)
2967 && !mips_32bit_flags(in_flags & ~elfcpp::EF_MIPS_ABI))
2968 new_flags |= in_flags & elfcpp::EF_MIPS_ABI;
2969 }
2970 else
2971 {
2972 // The ISAs aren't compatible.
2973 gold_error(_("%s ISA is 0x%x and it is not compatible with current"
2974 " ISA 0x%x"),
2975 name, in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIP S_MACH),
2976 out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MAC H));
2977 }
2978 }
2979
2980 in_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_ 32BITMODE);
2981 out_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS _32BITMODE);
2982
2983 // Compare ABIs. TODO AS: The 64-bit ABI does not use EF_MIPS_ABI.
2984 // But, it does set EI_CLASS differently from any 32-bit ABI.
2985 if ((in_flags & elfcpp::EF_MIPS_ABI) != (out_flags & elfcpp::EF_MIPS_ABI))
2986 {
2987 // Only error if both are set (to different values).
2988 if ((in_flags & elfcpp::EF_MIPS_ABI) && (out_flags & elfcpp::EF_MIPS_ABI ))
2989 {
2990 gold_error(_("%s ABI %d is different then current ABI %d"),
2991 name, in_flags & elfcpp::EF_MIPS_ABI,
2992 out_flags & elfcpp::EF_MIPS_ABI);
2993 }
2994
2995 in_flags &= ~elfcpp::EF_MIPS_ABI;
2996 out_flags &= ~elfcpp::EF_MIPS_ABI;
2997 }
2998
2999 // Compare ABIs.
3000 if ((in_flags & elfcpp::EF_MIPS_ABI2) != (out_flags & elfcpp::EF_MIPS_ABI2))
3001 {
3002 gold_error(_("%s is ABI%s while output is ABI%s"),
3003 name, ((in_flags & elfcpp::EF_MIPS_ABI2) ? "2" : ""),
3004 ((out_flags & elfcpp::EF_MIPS_ABI2) ? "2" : ""));
3005
3006 in_flags &= ~elfcpp::EF_MIPS_ABI2;
3007 out_flags &= ~elfcpp::EF_MIPS_ABI2;
3008 }
3009
3010 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
3011 // and allow arbitrary mixing of the remaining ASEs (retain the union).
3012 if ((in_flags & elfcpp::EF_MIPS_ARCH_ASE) != (out_flags & elfcpp::EF_MIPS_AR CH_ASE))
3013 {
3014 int old_micro = out_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
3015 int new_micro = in_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
3016 int old_m16 = out_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
3017 int new_m16 = in_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
3018 int micro_mis = old_m16 && new_micro;
3019 int m16_mis = old_micro && new_m16;
3020
3021 if (m16_mis || micro_mis)
3022 {
3023 gold_error(_("%s ASE is %s while output ASE is %s"),
3024 name, m16_mis ? "MIPS16" : "microMIPS",
3025 m16_mis ? "microMIPS" : "MIPS16");
3026 }
3027
3028 new_flags |= in_flags & elfcpp::EF_MIPS_ARCH_ASE;
3029
3030 in_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
3031 out_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
3032 }
3033
3034 // Warn about any other mismatches
3035 if (in_flags != out_flags)
3036 {
3037 gold_error(_("%s uses different flags 0x%x then output 0x%x"),
3038 name, in_flags, out_flags);
3039 }
3040
3041 this->set_processor_specific_flags(new_flags);
3042 }
3043 }
3044
3045 // Finalize the sections.
3046
3047 template <int size, bool big_endian>
3048 void
3049 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
3050 const Input_objects* input_objects,
3051 Symbol_table* symtab)
3052 {
3053 // Add NULL segment.
3054 if (!parameters->options().relocatable())
3055 layout->make_output_segment(elfcpp::PT_NULL, 0);
3056
3057 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
3058 p != layout->section_list().end(); ++p)
3059 if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO)
3060 {
3061 Mips_output_section_reginfo<size>* reginfo =
3062 Mips_output_section_reginfo<size>::as_mips_output_section_reginfo(*p);
3063
3064 if (this->gp() == NULL)
3065 reginfo->set_gp(0);
3066 else
3067 reginfo->set_gp(this->gp());
3068
3069 if (!parameters->options().relocatable())
3070 {
3071 Output_segment *reginfo_segment =
3072 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO, elfcpp::P F_R);
3073 reginfo_segment->add_output_section_to_nonload(reginfo, elfcpp::PF _R);
3074 }
3075
3076 // There is only one .reginfo section
3077 break;
3078 }
3079
3080 if (this->got_section())
3081 {
3082 this->got_section()->add_global_relocs();
3083 this->got_section()->add_global_tls_relocs();
3084 }
3085
3086 // Merge processor-specific flags.
3087 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
3088 p != input_objects->relobj_end();
3089 ++p)
3090 {
3091 Sized_relobj_file<size, big_endian>* relobj =
3092 static_cast<const Sized_relobj_file<size, big_endian>*>(*p);
3093
3094 Input_file::Format format = relobj->input_file()->format();
3095 if (format == Input_file::FORMAT_ELF)
3096 {
3097 // Read processor-specific flags in ELF file header.
3098 const unsigned char* pehdr = relobj->
3099 get_view(elfcpp::file_header_offset,
3100 elfcpp::Elf_sizes<size>::ehdr_size,
3101 true, false);
3102
3103 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
3104 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
3105
3106 this->merge_processor_specific_flags(relobj->name().c_str(), in_flags) ;
3107 }
3108 }
3109
3110 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
3111 p != input_objects->dynobj_end();
3112 ++p)
3113 {
3114 Sized_dynobj<size, big_endian>* dynobj =
3115 static_cast<const Sized_dynobj<size, big_endian>*>(*p);
3116
3117 // Read processor-specific flags.
3118 const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
3119 elfcpp::Elf_sizes<size>::ehdr_size,
3120 true, false);
3121
3122 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
3123 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
3124
3125 this->merge_processor_specific_flags(dynobj->name().c_str(), in_flags);
3126 }
3127
3128 // Fill in some more dynamic tags.
3129 const Reloc_section* rel_plt = (this->plt_ == NULL
3130 ? NULL
3131 : this->plt_->rel_plt());
3132 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
3133 this->rel_dyn_, true, false);
3134
3135 Output_data_dynamic* const odyn = layout->dynamic_data();
3136 if ((odyn) &&
3137 (!parameters->options().relocatable() && !parameters->doing_static_link()))
3138 {
3139 // In case there is no .got section, create one.
3140 this->got_section(symtab, layout);
3141
3142 unsigned int d_val;
3143 // This element holds a 32-bit version id for the Runtime
3144 // Linker Interface. This will start at integer value 1.
3145 d_val = 0x01;
3146 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
3147
3148 // Dynamic flags
3149 d_val = elfcpp::RHF_NOTPOT;
3150 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
3151
3152 // This member holds the base address of the segment.
3153 odyn->add_target_specific(elfcpp::DT_MIPS_BASE_ADDRESS);
3154
3155 // This member holds the number of entries in the .dynsym section.
3156 odyn->add_target_specific(elfcpp::DT_MIPS_SYMTABNO);
3157
3158 // This member holds the index of the first dynamic symbol
3159 // table entry that corresponds to an entry in the global offset table.
3160 odyn->add_target_specific(elfcpp::DT_MIPS_GOTSYM);
3161
3162 // This member holds the number of local global offset table entries.
3163 odyn->add_target_specific(elfcpp::DT_MIPS_LOCAL_GOTNO);
3164
3165 if (this->plt_ != NULL)
3166 // DT_MIPS_PLTGOT dynamic tag
3167 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
3168 }
3169
3170 // Set the .text section.
3171 this->text_section_ = layout->find_output_section(".text");
3172 // Set the symbol table
3173 this->symtab_ = symtab;
3174 // Set the layout
3175 this->layout_ = layout;
3176
3177 // Emit any relocs we saved in an attempt to avoid generating COPY
3178 // relocs.
3179 if (this->copy_relocs_.any_saved_relocs())
3180 this->copy_relocs_.emit(this->rel_dyn_section(layout));
3181
3182 if (this->copy_relocsa_.any_saved_relocs())
3183 this->copy_relocsa_.emit(this->rela_dyn_section(layout));
3184
3185 Target::do_finalize_sections (layout, input_objects, symtab);
3186 }
3187
3188 // Functor class for processing the global symbol table. Processes the
3189 // index in dynamic section if the symbol has GOT entry, and finds
3190 // minimum index value.
3191
3192 template<int size, bool big_endian>
3193 class Symbol_visitor_gotsym
3194 {
3195 public:
3196 Symbol_visitor_gotsym()
3197 { }
3198
3199 void
3200 operator()(const Sized_symbol<size>* sym)
3201 {
3202 if (sym->has_got_offset(0) && sym->needs_dynsym_entry() && sym->dynsym_index () < this->min_index_)
3203 this->min_index_ = sym->dynsym_index();
3204 }
3205
3206 unsigned int min_dynsym_index()
3207 {
3208 return this->min_index_;
3209 }
3210 private:
3211 static unsigned int min_index_;
3212 };
3213
3214 template<int size, bool big_endian>
3215 unsigned int Symbol_visitor_gotsym<size, big_endian>::min_index_ = -1U;
3216
3217 template<int size, bool big_endian>
3218 void
3219 Target_mips<size, big_endian>::do_fix_sections(Layout*, Symbol_table*)
3220 {
3221 Output_section *dynsym = this->layout_->find_output_section(".dynsym");
3222
3223 if ((dynsym != NULL) && (this->mips_stubs_ != NULL))
3224 {
3225 this->mips_stubs_->set_big_dyn_symtab(((unsigned int)dynsym->data_size()/1 6) > 0x10000);
3226
3227 this->mips_stubs_->fix_symbol_values();
3228 }
3229 }
3230
3231 // MIPS specific dynamic tags.
3232 template<int size, bool big_endian>
3233 unsigned int
3234 Target_mips<size, big_endian>::do_dynamic_tag_value(elfcpp::DT tag) const
3235 {
3236 Symbol_table *symtab = this->symtab_;
3237 Layout *layout = this->layout_;
3238
3239 Output_section *dynsym = layout->find_output_section(".dynsym");
3240 gold_assert(dynsym != NULL);
3241
3242 switch (tag)
3243 {
3244 case elfcpp::DT_MIPS_BASE_ADDRESS:
3245 {
3246 typedef std::vector<Output_segment*> Segment_list;
3247 const Segment_list segment_list_ = layout->segment_list();
3248 unsigned int vaddr = -1U;
3249 for (Segment_list::const_iterator p = segment_list_.begin();
3250 p != segment_list_.end();
3251 ++p)
3252 {
3253 if (((*p)->flags() & elfcpp::PF_R) != 0)
3254 {
3255 if ((*p)->vaddr() < vaddr)
3256 vaddr = (*p)->vaddr();
3257 }
3258 }
3259 return vaddr;
3260 }
3261 case elfcpp::DT_MIPS_SYMTABNO:
3262 return (unsigned int)dynsym->data_size()/16;
3263 case elfcpp::DT_MIPS_GOTSYM:
3264 {
3265 unsigned int min_index = symtab->global_got_index();
3266
3267 if (min_index == -1U)
3268 min_index = (unsigned int)dynsym->data_size()/16;
3269
3270 return min_index;
3271 }
3272 case elfcpp::DT_MIPS_LOCAL_GOTNO:
3273
3274 return (unsigned int)this->got_->data_size()/4 -
3275 ((unsigned int)dynsym->data_size()/16 - symtab->global_got_index()) -
3276 this->got_->tls_entries();
3277 default:
3278 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
3279 }
3280
3281 return (unsigned int)-1;
3282 }
3283
3284 // Relocate a section.
3285
3286 template<int size, bool big_endian>
3287 void
3288 Target_mips<size, big_endian>::relocate_section(
3289 const Relocate_info<size, big_endian>* relinfo,
3290 unsigned int sh_type,
3291 const unsigned char* prelocs,
3292 size_t reloc_count,
3293 Output_section* output_section,
3294 bool needs_special_offset_handling,
3295 unsigned char* view,
3296 typename elfcpp::Elf_types<size>::Elf_Addr address,
3297 section_size_type view_size,
3298 const Reloc_symbol_changes* reloc_symbol_changes)
3299 {
3300 typedef Target_mips<size, big_endian> Mips;
3301 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
3302
3303
3304 if (sh_type == elfcpp::SHT_REL)
3305 gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_REL,
3306 Mips_relocate>(
3307 relinfo,
3308 this,
3309 prelocs,
3310 reloc_count,
3311 output_section,
3312 needs_special_offset_handling,
3313 view,
3314 address,
3315 view_size,
3316 reloc_symbol_changes);
3317 else if (sh_type == elfcpp::SHT_RELA)
3318 gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_RELA,
3319 Mips_relocate>(
3320 relinfo,
3321 this,
3322 prelocs,
3323 reloc_count,
3324 output_section,
3325 needs_special_offset_handling,
3326 view,
3327 address,
3328 view_size,
3329 reloc_symbol_changes);
3330 }
3331
3332 // Return the size of a relocation while scanning during a relocatable
3333 // link.
3334
3335 template<int size, bool big_endian>
3336 unsigned int
3337 Target_mips<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3338 unsigned int r_type,
3339 Relobj* object)
3340 {
3341 switch (r_type)
3342 {
3343 case elfcpp::R_MIPS_NONE:
3344 case elfcpp::R_MIPS_UNUSED1:
3345 case elfcpp::R_MIPS_UNUSED2:
3346 case elfcpp::R_MIPS_UNUSED3:
3347 case elfcpp::R_MIPS_INSERT_A:
3348 case elfcpp::R_MIPS_INSERT_B:
3349 case elfcpp::R_MIPS_DELETE:
3350 case elfcpp::R_MIPS_TLS_DTPMOD64:
3351 case elfcpp::R_MIPS_TLS_DTPREL64:
3352 case elfcpp::R_MIPS_REL16:
3353 case elfcpp::R_MIPS_ADD_IMMEDIATE:
3354 case elfcpp::R_MIPS_PJUMP:
3355 case elfcpp::R_MIPS_RELGOT:
3356 case elfcpp::R_MIPS_TLS_TPREL64:
3357 case elfcpp::R_MIPS_GNU_VTINHERIT:
3358 case elfcpp::R_MIPS_GNU_VTENTRY:
3359 return 0;
3360
3361 case elfcpp::R_MIPS_64:
3362 case elfcpp::R_MIPS_SUB:
3363 return 8;
3364
3365 case elfcpp::R_MIPS_32:
3366 case elfcpp::R_MIPS_TLS_DTPMOD32:
3367 case elfcpp::R_MIPS_TLS_DTPREL32:
3368 case elfcpp::R_MIPS_TLS_TPREL32:
3369 case elfcpp::R_MIPS_REL32:
3370 case elfcpp::R_MIPS_PC32:
3371 case elfcpp::R_MIPS_GPREL32:
3372 case elfcpp::R_MIPS_JALR:
3373 case elfcpp::R_MIPS_SCN_DISP:
3374 return 4;
3375
3376 case elfcpp::R_MIPS_16:
3377 case elfcpp::R_MIPS_HI16:
3378 case elfcpp::R_MIPS_LO16:
3379 case elfcpp::R_MIPS_HIGHER:
3380 case elfcpp::R_MIPS_HIGHEST:
3381 case elfcpp::R_MIPS_GPREL16:
3382 case elfcpp::R_MIPS16_HI16:
3383 case elfcpp::R_MIPS16_LO16:
3384 case elfcpp::R_MIPS_PC16:
3385 case elfcpp::R_MIPS_GNU_REL16_S2:
3386 case elfcpp::R_MIPS_GOT16:
3387 case elfcpp::R_MIPS16_GOT16:
3388 case elfcpp::R_MIPS_CALL16:
3389 case elfcpp::R_MIPS16_CALL16:
3390 case elfcpp::R_MIPS_GOT_HI16:
3391 case elfcpp::R_MIPS_CALL_HI16:
3392 case elfcpp::R_MIPS_GOT_LO16:
3393 case elfcpp::R_MIPS_CALL_LO16:
3394 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
3395 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
3396 case elfcpp::R_MIPS_TLS_TPREL_HI16:
3397 case elfcpp::R_MIPS_TLS_TPREL_LO16:
3398 case elfcpp::R_MIPS16_GPREL:
3399 case elfcpp::R_MIPS_GOT_DISP:
3400 case elfcpp::R_MIPS_LITERAL:
3401 case elfcpp::R_MIPS_GOT_PAGE:
3402 case elfcpp::R_MIPS_GOT_OFST:
3403 case elfcpp::R_MIPS_TLS_GD:
3404 case elfcpp::R_MIPS_TLS_LDM:
3405 case elfcpp::R_MIPS_TLS_GOTTPREL:
3406 return 2;
3407
3408 // These relocations are not byte sized
3409 case elfcpp::R_MIPS_26:
3410 case elfcpp::R_MIPS16_26:
3411 return 4;
3412
3413 // These relocations are not byte sized
3414 case elfcpp::R_MIPS_SHIFT5:
3415 case elfcpp::R_MIPS_SHIFT6:
3416 return 1;
3417
3418 case elfcpp::R_MIPS_COPY:
3419 case elfcpp::R_MIPS_GLOB_DAT:
3420 case elfcpp::R_MIPS_JUMP_SLOT:
3421 object->error(_("unexpected reloc %u in object file"), r_type);
3422 return 0;
3423
3424 default:
3425 object->error(_("unsupported reloc %u in object file"), r_type);
3426 return 0;
3427 }
3428 }
3429
3430 // Scan the relocs during a relocatable link.
3431
3432 template<int size, bool big_endian>
3433 void
3434 Target_mips<size, big_endian>::scan_relocatable_relocs(
3435 Symbol_table* symtab,
3436 Layout* layout,
3437 Sized_relobj_file<size, big_endian>* object,
3438 unsigned int data_shndx,
3439 unsigned int sh_type,
3440 const unsigned char* prelocs,
3441 size_t reloc_count,
3442 Output_section* output_section,
3443 bool needs_special_offset_handling,
3444 size_t local_symbol_count,
3445 const unsigned char* plocal_symbols,
3446 Relocatable_relocs* rr)
3447 {
3448 gold_assert(sh_type == elfcpp::SHT_REL);
3449
3450 typedef Mips_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
3451 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3452
3453 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_REL,
3454 Scan_relocatable_relocs>(
3455 symtab,
3456 layout,
3457 object,
3458 data_shndx,
3459 prelocs,
3460 reloc_count,
3461 output_section,
3462 needs_special_offset_handling,
3463 local_symbol_count,
3464 plocal_symbols,
3465 rr);
3466 }
3467
3468 // Relocate a section during a relocatable link.
3469
3470 template<int size, bool big_endian>
3471 void
3472 Target_mips<size, big_endian>::relocate_for_relocatable(
3473 const Relocate_info<size, big_endian>* relinfo,
3474 unsigned int sh_type,
3475 const unsigned char* prelocs,
3476 size_t reloc_count,
3477 Output_section* output_section,
3478 off_t offset_in_output_section,
3479 const Relocatable_relocs* rr,
3480 unsigned char* view,
3481 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3482 section_size_type view_size,
3483 unsigned char* reloc_view,
3484 section_size_type reloc_view_size)
3485 {
3486 gold_assert(sh_type == elfcpp::SHT_REL);
3487
3488 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_REL>(
3489 relinfo,
3490 prelocs,
3491 reloc_count,
3492 output_section,
3493 offset_in_output_section,
3494 rr,
3495 view,
3496 view_address,
3497 view_size,
3498 reloc_view,
3499 reloc_view_size);
3500 }
3501
3502 // Perform target-specific processing in a relocatable link. This is
3503 // only used if we use the relocation strategy RELOC_SPECIAL.
3504
3505 template<int size, bool big_endian>
3506 void
3507 Target_mips<size, big_endian>::relocate_special_relocatable(
3508 const Relocate_info<size, big_endian>* relinfo,
3509 unsigned int sh_type,
3510 const unsigned char* preloc_in,
3511 size_t relnum,
3512 Output_section* output_section,
3513 off_t offset_in_output_section,
3514 unsigned char* view,
3515 elfcpp::Elf_types<32>::Elf_Addr view_address,
3516 section_size_type,
3517 unsigned char* preloc_out)
3518 {
3519 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc Reltype ;
3520 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc Relaty pe;
3521
3522 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
3523 Reltype_write;
3524 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
3525 Relatype_write;
3526
3527 typedef typename elfcpp::Elf_types<32>::Elf_Addr Mips_address;
3528 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
3529
3530 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
3531
3532 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
3533 const unsigned int local_count = object->local_symbol_count();
3534
3535 // SHT_REL types
3536 Reltype reloc(preloc_in);
3537 Reltype_write reloc_write(preloc_out);
3538
3539 // SHT_RELA types
3540 Relatype reloca(preloc_in);
3541 Relatype_write reloca_write(preloc_out);
3542
3543 elfcpp::Elf_types<32>::Elf_WXword r_info;
3544
3545 if (sh_type == elfcpp::SHT_RELA)
3546 r_info = reloca.get_r_info();
3547 else
3548 r_info = reloc.get_r_info();
3549
3550 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3551 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3552
3553 // Get the new symbol index.
3554 // We only use RELOC_SPECIAL strategy in local relocations.
3555 gold_assert(r_sym < local_count);
3556
3557 // We are adjusting a section symbol. We need to find
3558 // the symbol table index of the section symbol for
3559 // the output section corresponding to input section
3560 // in which this symbol is defined.
3561 bool is_ordinary;
3562 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
3563 gold_assert(is_ordinary);
3564 Output_section* os = object->output_section(shndx);
3565 gold_assert(os != NULL);
3566 gold_assert(os->needs_symtab_index());
3567 unsigned int new_symndx = os->symtab_index();
3568
3569 // Get the new offset--the location in the output section where
3570 // this relocation should be applied.
3571
3572 Mips_address offset;
3573
3574 if (sh_type == elfcpp::SHT_RELA)
3575 offset = reloca.get_r_offset();
3576 else
3577 offset = reloc.get_r_offset();
3578
3579 Mips_address new_offset;
3580 if (offset_in_output_section != invalid_address)
3581 new_offset = offset + offset_in_output_section;
3582 else
3583 {
3584 section_offset_type sot_offset =
3585 convert_types<section_offset_type, Mips_address>(offset);
3586 section_offset_type new_sot_offset =
3587 output_section->output_offset(object, relinfo->data_shndx,
3588 sot_offset);
3589 gold_assert(new_sot_offset != -1);
3590 new_offset = new_sot_offset;
3591 }
3592
3593 // In an object file, r_offset is an offset within the section.
3594 // In an executable or dynamic object, generated by
3595 // --emit-relocs, r_offset is an absolute address.
3596 if (!parameters->options().relocatable())
3597 {
3598 new_offset += view_address;
3599 if (offset_in_output_section != invalid_address)
3600 new_offset -= offset_in_output_section;
3601 }
3602
3603 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
3604 if (sh_type == elfcpp::SHT_RELA)
3605 r_addend = reloca.get_r_addend();
3606 else
3607 r_addend = 0;
3608
3609 if (sh_type == elfcpp::SHT_RELA)
3610 {
3611 reloca_write.put_r_offset(new_offset);
3612 reloca_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
3613 reloca_write.put_r_addend(r_addend);
3614 }
3615 else
3616 {
3617 reloc_write.put_r_offset(new_offset);
3618 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
3619 }
3620
3621 // Handle the reloc addend.
3622 // The relocation uses a section symbol in the input file.
3623 // We are adjusting it to use a section symbol in the output
3624 // file. The input section symbol refers to some address in
3625 // the input section. We need the relocation in the output
3626 // file to refer to that same address. This adjustment to
3627 // the addend is the same calculation we use for a simple
3628 // absolute relocation for the input section symbol.
3629
3630 unsigned char* paddend = view + offset;
3631 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
3632 switch (r_type)
3633 {
3634 case elfcpp::R_MIPS_26:
3635 reloc_status = Reloc_funcs::rel26(paddend, object, object->local_symbol(r_ sym),
3636 offset_in_output_section, false,
3637 r_addend, sh_type);
3638
3639 break;
3640
3641 default:
3642 gold_unreachable();
3643 }
3644
3645 // Report any errors.
3646 switch (reloc_status)
3647 {
3648 case Reloc_funcs::STATUS_OKAY:
3649 break;
3650 case Reloc_funcs::STATUS_OVERFLOW:
3651 gold_error_at_location(relinfo, relnum, new_offset,
3652 _("relocation overflow"));
3653 break;
3654 case Reloc_funcs::STATUS_BAD_RELOC:
3655 gold_error_at_location(
3656 relinfo,
3657 relnum,
3658 new_offset ,
3659 _("unexpected opcode while processing relocation"));
3660 break;
3661 default:
3662 gold_unreachable();
3663 }
3664 }
3665
3666 // Optimize the TLS relocation type based on what we know about the
3667 // symbol. IS_FINAL is true if the final address of this symbol is
3668 // known at link time.
3669
3670 template<int size, bool big_endian>
3671 tls::Tls_optimization
3672 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
3673 {
3674 // FIXME: Currently we do not do any TLS optimization.
3675 return tls::TLSOPT_NONE;
3676 }
3677
3678 // We are about to emit a dynamic relocation of type R_TYPE. If the
3679 // dynamic linker does not support it, issue an error.
3680
3681 template<int size, bool big_endian>
3682 void
3683 Target_mips<size, big_endian>::Scan::check_non_pic(Relobj* object,
3684 unsigned int r_type)
3685 {
3686 switch (r_type)
3687 {
3688 // These are the relocation types supported by glibc for MIPS.
3689 case elfcpp::R_MIPS_TLS_DTPMOD32:
3690 case elfcpp::R_MIPS_TLS_DTPREL32:
3691 case elfcpp::R_MIPS_TLS_TPREL32:
3692 case elfcpp::R_MIPS_REL32:
3693 case elfcpp::R_MIPS_GLOB_DAT:
3694 case elfcpp::R_MIPS_64:
3695 return;
3696
3697 default:
3698 {
3699 // This prevents us from issuing more than one error per reloc
3700 // section. But we can still wind up issuing more than one
3701 // error per object file.
3702 if (this->issued_non_pic_error_)
3703 return;
3704 gold_assert(parameters->options().output_is_position_independent());
3705 object->error(_("requires unsupported dynamic reloc; "
3706 "recompile with -fPIC"));
3707 this->issued_non_pic_error_ = true;
3708 return;
3709 }
3710
3711 case elfcpp::R_MIPS_NONE:
3712 gold_unreachable();
3713 }
3714 }
3715
3716 // Scan a relocation for a local symbol.
3717
3718 template<int size, bool big_endian>
3719 inline void
3720 Target_mips<size, big_endian>::Scan::local(
3721 Symbol_table* symtab,
3722 Layout* layout,
3723 Target_mips<size, big_endian>* target,
3724 Sized_relobj_file<size, big_endian>* object,
3725 unsigned int data_shndx,
3726 Output_section* output_section,
3727 const elfcpp::Rela<size, big_endian>* rela,
3728 const elfcpp::Rel<size, big_endian>* rel,
3729 unsigned int rel_type,
3730 unsigned int r_type,
3731 const elfcpp::Sym<size, big_endian>& lsym)
3732 {
3733 typename elfcpp::Elf_types<size>::Elf_Addr r_offset;
3734 typename elfcpp::Elf_types<size>::Elf_WXword r_info;
3735 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
3736
3737 if (rel_type == elfcpp::SHT_RELA)
3738 {
3739 r_offset = rela->get_r_offset();
3740 r_info = rela->get_r_info();
3741 r_addend = rela->get_r_addend();
3742 }
3743 else
3744 {
3745 r_offset = rel->get_r_offset();
3746 r_info = rel->get_r_info();
3747 r_addend = 0;
3748 }
3749
3750 switch (r_type)
3751 {
3752 // Global offset table relocations
3753 case elfcpp::R_MIPS_CALL16:
3754 case elfcpp::R_MIPS16_CALL16:
3755 case elfcpp::R_MIPS_GOT16:
3756 case elfcpp::R_MIPS16_GOT16:
3757 {
3758 Mips_output_data_got<size, big_endian>* got =
3759 target->got_section(symtab, layout);
3760
3761 std::stringstream tmp;
3762 tmp << static_cast<unsigned int> (r_info)
3763 << static_cast<unsigned int> (reinterpret_cast<unsigned long long> (ob ject))
3764 << static_cast<unsigned int> (r_offset);
3765 std::string key = tmp.str();
3766
3767 got->add_got16_view(key, got);
3768 }
3769 break;
3770 case elfcpp::R_MIPS_GOT_DISP:
3771 case elfcpp::R_MIPS_GOT_HI16:
3772 case elfcpp::R_MIPS_CALL_HI16:
3773 case elfcpp::R_MIPS_GOT_LO16:
3774 case elfcpp::R_MIPS_CALL_LO16:
3775 {
3776 // The symbol requires a GOT entry.
3777 Mips_output_data_got<size, big_endian>* got =
3778 target->got_section(symtab, layout);
3779
3780 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3781
3782 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3783 }
3784 break;
3785 case elfcpp::R_MIPS_GPREL32:
3786 case elfcpp::R_MIPS_GPREL16:
3787 case elfcpp::R_MIPS16_GPREL:
3788 case elfcpp::R_MIPS_LITERAL:
3789 break;
3790 case elfcpp::R_MIPS_NONE:
3791 case elfcpp::R_MIPS_16:
3792 break;
3793 case elfcpp::R_MIPS_32:
3794 {
3795 if (parameters->options().output_is_position_independent())
3796 {
3797 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3798 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
3799
3800 rel_dyn->add_symbolless_local_addend(object, r_sym, elfcpp::R_MIPS_R EL32,
3801 output_section, data_shndx,
3802 r_offset);
3803 }
3804 }
3805 case elfcpp::R_MIPS_REL32:
3806 case elfcpp::R_MIPS_26:
3807 case elfcpp::R_MIPS_HI16:
3808 case elfcpp::R_MIPS_LO16:
3809 case elfcpp::R_MIPS_PC16:
3810 case elfcpp::R_MIPS_UNUSED1:
3811 case elfcpp::R_MIPS_UNUSED2:
3812 case elfcpp::R_MIPS_UNUSED3:
3813 case elfcpp::R_MIPS_SHIFT5:
3814 case elfcpp::R_MIPS_SHIFT6:
3815 case elfcpp::R_MIPS_64:
3816 case elfcpp::R_MIPS_GOT_PAGE:
3817 case elfcpp::R_MIPS_GOT_OFST:
3818 case elfcpp::R_MIPS_SUB:
3819 case elfcpp::R_MIPS_INSERT_A:
3820 case elfcpp::R_MIPS_INSERT_B:
3821 case elfcpp::R_MIPS_DELETE:
3822 case elfcpp::R_MIPS_HIGHER:
3823 case elfcpp::R_MIPS_HIGHEST:
3824 case elfcpp::R_MIPS_SCN_DISP:
3825 case elfcpp::R_MIPS_REL16:
3826 case elfcpp::R_MIPS_ADD_IMMEDIATE:
3827 case elfcpp::R_MIPS_PJUMP:
3828 case elfcpp::R_MIPS_RELGOT:
3829 case elfcpp::R_MIPS_JALR:
3830 case elfcpp::R_MIPS_GLOB_DAT:
3831 case elfcpp::R_MIPS16_26:
3832 case elfcpp::R_MIPS16_HI16:
3833 case elfcpp::R_MIPS16_LO16:
3834 case elfcpp::R_MIPS_COPY:
3835 case elfcpp::R_MIPS_JUMP_SLOT:
3836 case elfcpp::R_MIPS_PC32:
3837 case elfcpp::R_MIPS_GNU_REL16_S2:
3838 case elfcpp::R_MIPS_GNU_VTINHERIT:
3839 case elfcpp::R_MIPS_GNU_VTENTRY:
3840 break;
3841
3842 case elfcpp::R_MIPS_TLS_DTPMOD32:
3843 case elfcpp::R_MIPS_TLS_DTPREL32:
3844 case elfcpp::R_MIPS_TLS_DTPMOD64:
3845 case elfcpp::R_MIPS_TLS_DTPREL64:
3846 case elfcpp::R_MIPS_TLS_GD:
3847 case elfcpp::R_MIPS_TLS_LDM:
3848 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
3849 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
3850 case elfcpp::R_MIPS_TLS_GOTTPREL:
3851 case elfcpp::R_MIPS_TLS_TPREL32:
3852 case elfcpp::R_MIPS_TLS_TPREL64:
3853 case elfcpp::R_MIPS_TLS_TPREL_HI16:
3854 case elfcpp::R_MIPS_TLS_TPREL_LO16:
3855 {
3856 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3857 if (object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
3858 break;
3859
3860 bool output_is_shared = parameters->options().shared();
3861 const tls::Tls_optimization optimized_type
3862 = Target_mips<size, big_endian>::optimize_tls_reloc(
3863 !output_is_shared, r_type);
3864 switch (r_type)
3865 {
3866 case elfcpp::R_MIPS_TLS_GD:
3867 if (optimized_type == tls::TLSOPT_NONE)
3868 {
3869 // Create a pair of GOT entries for the module index and
3870 // dtv-relative offset.
3871 Mips_output_data_got<size, big_endian>* got
3872 = target->got_section(symtab, layout);
3873 unsigned int shndx = lsym.get_st_shndx();
3874 bool is_ordinary;
3875 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3876 if (!is_ordinary)
3877 {
3878 object->error(_("local symbol %u has bad shndx %u"),
3879 r_sym, shndx);
3880 break;
3881 }
3882
3883 got->add_tls_gd_local_reloc(GOT_TYPE_TLS_PAIR, object, r_sym);
3884 }
3885 else
3886 // FIXME: TLS optimization not supported yet.
3887 gold_unreachable();
3888 break;
3889
3890 case elfcpp::R_MIPS_TLS_LDM:
3891 if (optimized_type == tls::TLSOPT_NONE)
3892 {
3893 // Create a GOT entry for the module index.
3894 target->got_mod_index_entry(symtab, layout, object);
3895 }
3896 else
3897 // FIXME: TLS optimization not supported yet.
3898 gold_unreachable();
3899 break;
3900 case elfcpp::R_MIPS_TLS_GOTTPREL:
3901 layout->set_has_static_tls();
3902 if (optimized_type == tls::TLSOPT_NONE)
3903 {
3904 // Create a GOT entry for the tp-relative offset.
3905 Mips_output_data_got<size, big_endian>* got
3906 = target->got_section(symtab, layout);
3907 if (!parameters->doing_static_link())
3908 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3909 target->rel_dyn_section(layout),
3910 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
3911 elfcpp::R_MIPS_TLS_TPREL64);
3912 else if (!object->local_has_got_offset(r_sym,
3913 GOT_TYPE_TLS_OFFSET))
3914 {
3915 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
3916
3917 unsigned int got_offset =
3918 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
3919 got->add_static_reloc(got_offset,
3920 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
3921 elfcpp::R_MIPS_TLS_TPREL64, object,
3922 r_sym);
3923 }
3924 }
3925 else
3926 // FIXME: TLS optimization not supported yet.
3927 gold_unreachable();
3928 break;
3929
3930 default: // TODO AS: Check if this is correct at all
3931 layout->set_has_static_tls();
3932 if (output_is_shared)
3933 {
3934 // We need to create a dynamic relocation.
3935 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3936 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3937 rel_dyn->add_local(object, r_sym, size == 32 ?
3938 elfcpp::R_MIPS_TLS_TPREL32 :
3939 elfcpp::R_MIPS_TLS_TPREL64,
3940 output_section, data_shndx, r_offset);
3941 }
3942 break;
3943 }
3944 }
3945 break;
3946
3947 default:
3948 unsupported_reloc_local(object, r_type);
3949 break;
3950 }
3951 }
3952
3953 template<int size, bool big_endian>
3954 inline void
3955 Target_mips<size, big_endian>::Scan::local(
3956 Symbol_table* symtab,
3957 Layout* layout,
3958 Target_mips<size, big_endian>* target,
3959 Sized_relobj_file<size, big_endian>* object,
3960 unsigned int data_shndx,
3961 Output_section* output_section,
3962 const elfcpp::Rel<size, big_endian>& reloc,
3963 unsigned int r_type,
3964 const elfcpp::Sym<size, big_endian>& lsym)
3965 {
3966 local(
3967 symtab,
3968 layout,
3969 target,
3970 object,
3971 data_shndx,
3972 output_section,
3973 (const elfcpp::Rela<size, big_endian>*) NULL,
3974 &reloc,
3975 elfcpp::SHT_REL,
3976 r_type,
3977 lsym);
3978 }
3979
3980
3981 template<int size, bool big_endian>
3982 inline void
3983 Target_mips<size, big_endian>::Scan::local(
3984 Symbol_table* symtab,
3985 Layout* layout,
3986 Target_mips<size, big_endian>* target,
3987 Sized_relobj_file<size, big_endian>* object,
3988 unsigned int data_shndx,
3989 Output_section* output_section,
3990 const elfcpp::Rela<size, big_endian>& reloc,
3991 unsigned int r_type,
3992 const elfcpp::Sym<size, big_endian>& lsym)
3993 {
3994 local(
3995 symtab,
3996 layout,
3997 target,
3998 object,
3999 data_shndx,
4000 output_section,
4001 &reloc,
4002 (const elfcpp::Rel<size, big_endian>*) NULL,
4003 elfcpp::SHT_RELA,
4004 r_type,
4005 lsym);
4006 }
4007
4008 template<int size, bool big_endian>
4009 void
4010 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
4011 {
4012 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
4013
4014 // Call parent to write out GOT.
4015 Output_data_got<size, big_endian>::do_write(of);
4016
4017 // Collect views for later fixing values of .got section
4018 if (!this->got16_views_.empty())
4019 {
4020 const off_t offset = this->offset();
4021 const section_size_type oview_size =
4022 convert_to_section_size_type(this->data_size());
4023 unsigned char* const oview = of->get_output_view(offset, oview_size);
4024
4025 this->got_view_ = oview;
4026 }
4027
4028 // We are done if there is no fix up.
4029 if (this->static_relocs_.empty())
4030 return;
4031
4032 const off_t offset = this->offset();
4033 const section_size_type oview_size =
4034 convert_to_section_size_type(this->data_size());
4035 unsigned char* const oview = of->get_output_view(offset, oview_size);
4036
4037 Output_segment* tls_segment = this->layout_->tls_segment();
4038 gold_assert(tls_segment != NULL);
4039
4040 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
4041 {
4042 Static_reloc& reloc(this->static_relocs_[i]);
4043
4044 Mips_address value;
4045 if (!reloc.symbol_is_global())
4046 {
4047 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
4048 const Symbol_value<size>* psymval =
4049 reloc.relobj()->local_symbol(reloc.index());
4050
4051 // We are doing static linking. Issue an error and skip this
4052 // relocation if the symbol is undefined or in a discarded_section.
4053 bool is_ordinary;
4054 unsigned int shndx = psymval->input_shndx(&is_ordinary);
4055 if ((shndx == elfcpp::SHN_UNDEF)
4056 || (is_ordinary
4057 && shndx != elfcpp::SHN_UNDEF
4058 && !object->is_section_included(shndx)
4059 && !this->symbol_table_->is_section_folded(object, shndx)))
4060 {
4061 gold_error(_("undefined or discarded local symbol %u from "
4062 " object %s in GOT"),
4063 reloc.index(), reloc.relobj()->name().c_str());
4064 continue;
4065 }
4066
4067 value = psymval->value(object, 0);
4068 }
4069 else
4070 {
4071 const Symbol* gsym = reloc.symbol();
4072 gold_assert(gsym != NULL);
4073 if (gsym->is_forwarder())
4074 gsym = this->symbol_table_->resolve_forwards(gsym);
4075
4076 // We are doing static linking. Issue an error and skip this
4077 // relocation if the symbol is undefined or in a discarded_section
4078 // unless it is a weakly_undefined symbol.
4079 if ((gsym->is_defined_in_discarded_section()
4080 || gsym->is_undefined())
4081 && !gsym->is_weak_undefined())
4082 {
4083 gold_error(_("undefined or discarded symbol %s in GOT"),
4084 gsym->name());
4085 continue;
4086 }
4087
4088 if (!gsym->is_weak_undefined())
4089 {
4090 const Sized_symbol<size>* sym =
4091 static_cast<const Sized_symbol<size>*>(gsym);
4092 value = sym->value();
4093 }
4094 else
4095 value = 0;
4096 }
4097
4098 unsigned got_offset = reloc.got_offset();
4099 gold_assert(got_offset < oview_size);
4100
4101 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4102 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
4103 Valtype x;
4104
4105 switch (reloc.r_type())
4106 {
4107 case elfcpp::R_MIPS_TLS_DTPMOD32:
4108 x = value;
4109 break;
4110 case elfcpp::R_MIPS_TLS_TPREL32:
4111 x = value - elfcpp::TP_OFFSET;
4112 break;
4113 case elfcpp::R_MIPS_TLS_DTPREL32:
4114 x = value - elfcpp::DTP_OFFSET;
4115 break;
4116 default:
4117 gold_unreachable();
4118 break;
4119 }
4120
4121 elfcpp::Swap<size, big_endian>::writeval(wv, x);
4122 }
4123
4124 of->write_output_view(offset, oview_size, oview);
4125 }
4126
4127 // Scan a relocation for a global symbol.
4128
4129 template<int size, bool big_endian>
4130 inline void
4131 Target_mips<size, big_endian>::Scan::global(
4132 Symbol_table* symtab,
4133 Layout* layout,
4134 Target_mips<size, big_endian>* target,
4135 Sized_relobj_file<size, big_endian>* object,
4136 unsigned int data_shndx,
4137 Output_section* output_section,
4138 const elfcpp::Rela<size, big_endian>* rela,
4139 const elfcpp::Rel<size, big_endian>* rel,
4140 unsigned int rel_type,
4141 unsigned int r_type,
4142 Symbol* gsym)
4143 {
4144 typename elfcpp::Elf_types<size>::Elf_Addr r_offset;
4145 typename elfcpp::Elf_types<size>::Elf_WXword r_info;
4146 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
4147
4148 if (rel_type == elfcpp::SHT_RELA)
4149 {
4150 r_offset = rela->get_r_offset();
4151 r_info = rela->get_r_info();
4152 r_addend = rela->get_r_addend();
4153 }
4154 else
4155 {
4156 r_offset = rel->get_r_offset();
4157 r_info = rel->get_r_info();
4158 r_addend = 0;
4159 }
4160
4161 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
4162 // section. We check here to avoid creating a dynamic reloc against
4163 // _GLOBAL_OFFSET_TABLE_.
4164 if (!target->has_got_section()
4165 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
4166 target->got_section(symtab, layout);
4167
4168 // TODO AS: Check where to put this code!!!!!
4169 if (!parameters->options().shared())
4170 if ((this->symbol_needs_plt_entry(gsym)) && (!gsym->has_plt_offset()))
4171 {
4172 if (gsym->object() != object)
4173 {
4174 if (parameters->options().output_is_position_independent())
4175 {
4176 gold_assert(!gsym->has_dynsym_index());
4177 target->make_mips_stubs_entry(symtab, layout, gsym);
4178 }
4179 else if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type) ))
4180 {
4181 target->make_plt_entry(symtab, layout, gsym);
4182 }
4183 }
4184
4185 // Since this is not a PC-relative relocation, we may be
4186 // taking the address of a function. In that case we need to
4187 // set the entry in the dynamic symbol table to the address of
4188 // the PLT entry.
4189 if (((gsym->is_from_dynobj() && !parameters->options().shared()) ||
4190 ((this->get_reference_flags(r_type) == Symbol::ABSOLUTE_REF)
4191 && !(parameters->options().output_is_position_independent())))
4192 && (gsym->has_plt_offset()))
4193 {
4194 gsym->set_needs_dynsym_value();
4195 gsym->set_nonvis(elfcpp::STO_MIPS_PLT>>2);
4196 }
4197 }
4198
4199 if ((gsym->has_plt_offset())
4200 && (parameters->options().output_is_position_independent()))
4201 switch (r_type)
4202 {
4203 case elfcpp::R_MIPS_HI16:
4204 case elfcpp::R_MIPS_LO16:
4205 case elfcpp::R_MIPS_CALL16:
4206 case elfcpp::R_MIPS_JALR:
4207 break;
4208 default:
4209 target->remove_stub_entry(gsym);
4210 break;
4211 }
4212
4213 switch (r_type)
4214 {
4215 case elfcpp::R_MIPS_NONE:
4216 case elfcpp::R_MIPS_UNUSED1:
4217 case elfcpp::R_MIPS_UNUSED2:
4218 case elfcpp::R_MIPS_UNUSED3:
4219 case elfcpp::R_MIPS_INSERT_A:
4220 case elfcpp::R_MIPS_INSERT_B:
4221 case elfcpp::R_MIPS_DELETE:
4222 case elfcpp::R_MIPS_TLS_DTPMOD64:
4223 case elfcpp::R_MIPS_TLS_DTPREL64:
4224 case elfcpp::R_MIPS_REL16:
4225 case elfcpp::R_MIPS_ADD_IMMEDIATE:
4226 case elfcpp::R_MIPS_PJUMP:
4227 case elfcpp::R_MIPS_RELGOT:
4228 case elfcpp::R_MIPS_TLS_TPREL64:
4229 case elfcpp::R_MIPS_GNU_VTINHERIT:
4230 case elfcpp::R_MIPS_GNU_VTENTRY:
4231 break;
4232 case elfcpp::R_MIPS_GOT16:
4233 case elfcpp::R_MIPS16_GOT16:
4234 case elfcpp::R_MIPS_CALL16:
4235 case elfcpp::R_MIPS16_CALL16:
4236 case elfcpp::R_MIPS_GOT_DISP:
4237 case elfcpp::R_MIPS_GOT_HI16:
4238 case elfcpp::R_MIPS_CALL_HI16:
4239 case elfcpp::R_MIPS_GOT_LO16:
4240 case elfcpp::R_MIPS_CALL_LO16:
4241 case elfcpp::R_MIPS_GOT_PAGE:
4242 case elfcpp::R_MIPS_GOT_OFST:
4243 {
4244 // The symbol requires a GOT entry.
4245 Mips_output_data_got<size, big_endian>* got =
4246 target->got_section(symtab, layout);
4247 if (!gsym->final_value_is_known()
4248 || (parameters->options().output_is_position_independent()))
4249 got->add_global_with_reloc_mips(gsym);
4250 else
4251 {
4252 got->add_global(gsym, GOT_TYPE_STANDARD);
4253 gsym->set_needs_dynsym_entry();
4254 }
4255
4256 }
4257 break;
4258 case elfcpp::R_MIPS_LITERAL:
4259 gold_error(_("%s:R_MIPS_LITERAL is defined only for local symbols"),
4260 object->name().c_str());
4261 break;
4262 case elfcpp::R_MIPS_GPREL32:
4263 gold_error(_("%s:R_MIPS_GPREL32 not defined for global symbols"),
4264 object->name().c_str());
4265 case elfcpp::R_MIPS_GPREL16:
4266 case elfcpp::R_MIPS16_GPREL:
4267 case elfcpp::R_MIPS16_26:
4268 break;
4269 case elfcpp::R_MIPS_26:
4270 case elfcpp::R_MIPS_PC16:
4271 {
4272 const unsigned char* pehdr = gsym->object()->get_view(
4273 elfcpp::file_header_offset,
4274 elfcpp::Elf_sizes<size>::ehdr_size,
4275 true, false);
4276
4277 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
4278
4279 if (((ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0)
4280 && (gsym->is_defined_in_discarded_section() == false)
4281 && (gsym->is_func())
4282 && (!gsym->has_plt_offset())
4283 && (gsym->object() != object))
4284 {
4285 bool is_ordinary = false;
4286
4287 target->make_stub_entry(layout);
4288 target->set_stub_flag();
4289
4290 // If the function is at the beginning of the section and if we
4291 // would need no more than 2 nops.
4292 // TODO AS: For now we only support stubs of second type
4293 if ((((Sized_symbol<size> *)gsym)->value() == 0)
4294 &&(gsym->object()->section_addralign(gsym->shndx(&is_ordinary))
4295 <= 16))
4296 {
4297 if (target->stub_symbol(gsym->name()) == NULL)
4298 target->stub_->add_entry(symtab, target, gsym);
4299 }
4300 else
4301 {
4302 if (target->stub_symbol(gsym->name()) == NULL)
4303 target->stub_->add_entry(symtab, target, gsym);
4304 }
4305 }
4306 break;
4307 }
4308 case elfcpp::R_MIPS_16:
4309 case elfcpp::R_MIPS_SHIFT5:
4310 case elfcpp::R_MIPS_SHIFT6:
4311 case elfcpp::R_MIPS_SUB:
4312 case elfcpp::R_MIPS_HIGHER:
4313 case elfcpp::R_MIPS_HIGHEST:
4314 case elfcpp::R_MIPS_SCN_DISP:
4315 case elfcpp::R_MIPS16_HI16:
4316 case elfcpp::R_MIPS16_LO16:
4317 case elfcpp::R_MIPS_GNU_REL16_S2:
4318 case elfcpp::R_MIPS_PC32:
4319 break;
4320 case elfcpp::R_MIPS_HI16:
4321 case elfcpp::R_MIPS_LO16:
4322 case elfcpp::R_MIPS_32:
4323 case elfcpp::R_MIPS_64:
4324 // Absolute addressing relocations.
4325 {
4326 // Make a dynamic relocation if necessary.
4327 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4328 {
4329 if (gsym->may_need_copy_reloc() && gsym->in_dyn()
4330 && !((r_type == elfcpp::R_MIPS_32) || (r_type == elfcpp::R_MIPS_64 )))
4331 {
4332 if (rel_type == elfcpp::SHT_RELA)
4333 target->copy_reloc(symtab, layout, object,
4334 data_shndx, output_section, gsym, *rela);
4335 else
4336 target->copy_reloc(symtab, layout, object,
4337 data_shndx, output_section, gsym, *rel);
4338 }
4339 else if ((r_type == elfcpp::R_MIPS_32) || (r_type == elfcpp::R_MIPS_ 64))
4340 {
4341 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4342 Mips_output_data_got<size, big_endian>*
4343 got = target->got_section(symtab, layout);
4344
4345 if (!gsym->final_value_is_known()
4346 || (parameters->options().output_is_position_independent()))
4347 got->add_global_with_reloc_mips(gsym);
4348
4349 rel_dyn->add_global(gsym, elfcpp::R_MIPS_REL32, output_section, object,
4350 data_shndx, r_offset);
4351 }
4352 }
4353 }
4354 break;
4355 case elfcpp::R_MIPS_TLS_DTPREL32:
4356 case elfcpp::R_MIPS_TLS_GD:
4357 case elfcpp::R_MIPS_TLS_LDM:
4358 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
4359 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
4360 case elfcpp::R_MIPS_TLS_GOTTPREL:
4361 case elfcpp::R_MIPS_TLS_TPREL32:
4362 case elfcpp::R_MIPS_TLS_TPREL_HI16:
4363 case elfcpp::R_MIPS_TLS_TPREL_LO16:
4364 {
4365 if (gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
4366 break;
4367
4368 const bool is_final = gsym->final_value_is_known();
4369 const tls::Tls_optimization optimized_type =
4370 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
4371 //unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4372
4373 switch (r_type)
4374 {
4375 case elfcpp::R_MIPS_TLS_GD:
4376 if (optimized_type == tls::TLSOPT_NONE)
4377 {
4378 // Create a pair of GOT entries for the module index and
4379 // dtv-relative offset.
4380 Mips_output_data_got<size, big_endian>* got
4381 = target->got_section(symtab, layout);
4382 if (!parameters->doing_static_link())
4383 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
4384 target->rel_dyn_section(layout),
4385 size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 :
4386 elfcpp::R_MIPS_TLS_DTPMOD64,
4387 size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 :
4388 elfcpp::R_MIPS_TLS_DTPREL64);
4389 else
4390 got->add_tls_gd_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
4391 }
4392 else
4393 // FIXME: TLS optimization not supported yet.
4394 gold_unreachable();
4395 break;
4396
4397 case elfcpp::R_MIPS_TLS_LDM:
4398 if (optimized_type == tls::TLSOPT_NONE)
4399 // Create a GOT entry for the module index.
4400 target->got_mod_index_entry(symtab, layout, object);
4401 else
4402 // FIXME: TLS optimization not supported yet.
4403 gold_unreachable();
4404 break;
4405 case elfcpp::R_MIPS_TLS_GOTTPREL:
4406 layout->set_has_static_tls();
4407 if (optimized_type == tls::TLSOPT_NONE)
4408 {
4409 // Create a GOT entry for the tp-relative offset.
4410 Mips_output_data_got<size, big_endian>* got
4411 = target->got_section(symtab, layout);
4412 if (!parameters->doing_static_link())
4413 got->add_global_tls_with_reloc_mips(gsym, target->rel_dyn_sect ion(layout),
4414 elfcpp::R_MIPS_TLS_TPREL32);
4415 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
4416 {
4417 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
4418 unsigned int got_offset =
4419 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
4420 got->add_static_reloc(got_offset,
4421 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
4422 elfcpp::R_MIPS_TLS_TPREL64, gsym);
4423 }
4424 }
4425 else
4426 // FIXME: TLS optimization not supported yet.
4427 gold_unreachable();
4428 break;
4429
4430 default:
4431 layout->set_has_static_tls();
4432 if (parameters->options().shared())
4433 {
4434 // We need to create a dynamic relocation.
4435 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4436 rel_dyn->add_global(gsym,
4437 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
4438 elfcpp::R_MIPS_TLS_TPREL64,
4439 output_section, object,
4440 data_shndx, r_offset);
4441 }
4442 break;
4443 }
4444 }
4445 break;
4446 case elfcpp::R_MIPS_REL32:
4447 // Relative addressing relocations.
4448 {
4449 // Make a dynamic relocation if necessary.
4450 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4451 {
4452 if (gsym->may_need_copy_reloc())
4453 {
4454 if (rel_type == elfcpp::SHT_RELA)
4455 target->copy_reloc(symtab, layout, object,
4456 data_shndx, output_section, gsym, *rela);
4457 else
4458 target->copy_reloc(symtab, layout, object,
4459 data_shndx, output_section, gsym, *rel);
4460 }
4461 else
4462 {
4463 check_non_pic(object, r_type);
4464 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4465 rel_dyn->add_global(gsym, r_type, output_section, object,
4466 data_shndx, r_offset);
4467 }
4468 }
4469 }
4470 break;
4471 case elfcpp::R_MIPS_JALR:
4472 break;
4473 case elfcpp::R_MIPS_COPY:
4474 case elfcpp::R_MIPS_GLOB_DAT:
4475 case elfcpp::R_MIPS_JUMP_SLOT:
4476 // These are relocations which should only be seen by the
4477 // dynamic linker, and should never be seen here.
4478 gold_error(_("%s: unexpected reloc %u in object file"),
4479 object->name().c_str(), r_type);
4480 break;
4481
4482 default:
4483 unsupported_reloc_global(object, r_type, gsym);
4484 break;
4485 }
4486 }
4487
4488 template<int size, bool big_endian>
4489 inline void
4490 Target_mips<size, big_endian>::Scan::global(
4491 Symbol_table* symtab,
4492 Layout* layout,
4493 Target_mips<size, big_endian>* target,
4494 Sized_relobj_file<size, big_endian>* object,
4495 unsigned int data_shndx,
4496 Output_section* output_section,
4497 const elfcpp::Rela<size, big_endian>& reloc,
4498 unsigned int r_type,
4499 Symbol* gsym)
4500 {
4501 global(
4502 symtab,
4503 layout,
4504 target,
4505 object,
4506 data_shndx,
4507 output_section,
4508 &reloc,
4509 (const elfcpp::Rel<size, big_endian>*) NULL,
4510 elfcpp::SHT_RELA,
4511 r_type,
4512 gsym);
4513 }
4514
4515 template<int size, bool big_endian>
4516 inline void
4517 Target_mips<size, big_endian>::Scan::global(
4518 Symbol_table* symtab,
4519 Layout* layout,
4520 Target_mips<size, big_endian>* target,
4521 Sized_relobj_file<size, big_endian>* object,
4522 unsigned int data_shndx,
4523 Output_section* output_section,
4524 const elfcpp::Rel<size, big_endian>& reloc,
4525 unsigned int r_type,
4526 Symbol* gsym)
4527 {
4528 global(
4529 symtab,
4530 layout,
4531 target,
4532 object,
4533 data_shndx,
4534 output_section,
4535 (const elfcpp::Rela<size, big_endian>*) NULL,
4536 &reloc,
4537 elfcpp::SHT_REL,
4538 r_type,
4539 gsym);
4540 }
4541
4542 // Perform a relocation.
4543
4544 template<int size, bool big_endian>
4545 inline bool
4546 Target_mips<size, big_endian>::Relocate::relocate(
4547 const Relocate_info<size, big_endian>* relinfo,
4548 Target_mips* target,
4549 Output_section* output_section,
4550 size_t relnum,
4551 const elfcpp::Rela<size, big_endian>* rela,
4552 const elfcpp::Rel<size, big_endian>* rel,
4553 unsigned int rel_type,
4554 unsigned int r_type,
4555 const Sized_symbol<size>* gsym,
4556 const Symbol_value<size>* psymval,
4557 unsigned char* view,
4558 typename elfcpp::Elf_types<size>::Elf_Addr address,
4559 section_size_type)
4560 {
4561 typename elfcpp::Elf_types<size>::Elf_Addr r_offset;
4562 typename elfcpp::Elf_types<size>::Elf_WXword r_info;
4563 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
4564
4565 if (rel_type == elfcpp::SHT_RELA)
4566 {
4567 r_offset = rela->get_r_offset();
4568 r_info = rela->get_r_info();
4569 r_addend = rela->get_r_addend();
4570 }
4571 else
4572 {
4573 r_offset = rel->get_r_offset();
4574 r_info = rel->get_r_info();
4575 r_addend = 0;
4576 }
4577
4578 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
4579 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
4580
4581 unsigned int got_offset = 0;
4582 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
4583
4584 Symbol_value<size> symval;
4585
4586 // __gnu_local_gp is _gp symbol, just swich it
4587 if (gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0))
4588 {
4589 gsym = target->gp();
4590
4591 gold_assert(gsym != NULL);
4592
4593 symval.set_output_value(target->gp_address());
4594
4595 psymval = &symval;
4596 }
4597 else if (gsym && (strcmp(gsym->name(), "_gp_disp") == 0))
4598 {
4599 if (r_type != elfcpp::R_MIPS_HI16 && r_type != elfcpp::R_MIPS16_HI16 &&
4600 r_type != elfcpp::R_MIPS_LO16 && r_type != elfcpp::R_MIPS16_LO16)
4601 gold_error_at_location(relinfo, relnum, r_offset,
4602 _("relocations against _gp_disp are permitted only"
4603 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
4604
4605 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4606 Valtype* wv = reinterpret_cast<Valtype*>(view);
4607 Valtype addend = (elfcpp::Swap<32, big_endian>::readval(wv)) & 0xffffU;
4608 Valtype value;
4609
4610 if (r_type == elfcpp::R_MIPS16_HI16)
4611 value = addend + target->gp_address() - address - 4;
4612 else if (r_type == elfcpp::R_MIPS_HI16)
4613 value = addend + target->gp_address() - address;
4614 else if (r_type == elfcpp::R_MIPS16_LO16)
4615 value = addend + target->gp_address() - address;
4616 else if (r_type == elfcpp::R_MIPS_LO16)
4617 value = addend + target->gp_address() - address + 4;
4618 else
4619 gold_unreachable();
4620
4621 symval.set_output_value(value);
4622
4623 psymval = &symval;
4624 }
4625
4626 // Get the GOT offset.
4627 // The GOT pointer points to the end of the GOT section.
4628 // We need to subtract the size of the GOT section to get
4629 // the actual offset to use in the relocation.
4630 switch (r_type)
4631 {
4632 case elfcpp::R_MIPS_GOT_DISP:
4633 case elfcpp::R_MIPS_GOT_HI16:
4634 case elfcpp::R_MIPS_CALL_HI16:
4635 case elfcpp::R_MIPS_GOT_LO16:
4636 case elfcpp::R_MIPS_CALL_LO16:
4637 if (gsym != NULL)
4638 {
4639 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4640 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4641 - target->got_size());
4642 }
4643 else
4644 {
4645 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4646 gold_assert(relinfo->object->local_has_got_offset
4647 (r_sym, GOT_TYPE_STANDARD));
4648 got_offset = (relinfo->object->local_got_offset
4649 (r_sym, GOT_TYPE_STANDARD) - target->got_size());
4650 }
4651 break;
4652 case elfcpp::R_MIPS_TLS_GD:
4653 if (gsym != NULL)
4654 {
4655 got_offset = target->got_section()->address() +
4656 gsym->got_offset(GOT_TYPE_TLS_PAIR) -
4657 target->gp_address();
4658 }
4659 else
4660 {
4661 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4662 gold_assert(relinfo->object->local_has_got_offset
4663 (r_sym, GOT_TYPE_TLS_PAIR));
4664 got_offset = target->got_section()->address() +
4665 relinfo->object->local_got_offset(r_sym, GOT_TYPE_TLS_PAIR) -
4666 target->gp_address();
4667 }
4668 break;
4669 case elfcpp::R_MIPS_TLS_GOTTPREL:
4670 if (gsym != NULL)
4671 {
4672 got_offset = target->got_section()->address() +
4673 gsym->got_offset(GOT_TYPE_TLS_OFFSET) -
4674 target->gp_address();
4675 }
4676 else
4677 {
4678 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4679 gold_assert(relinfo->object->local_has_got_offset
4680 (r_sym, GOT_TYPE_TLS_OFFSET));
4681 got_offset = target->got_section()->address() +
4682 relinfo->object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) -
4683 target->gp_address();
4684 }
4685 break;
4686 case elfcpp::R_MIPS_TLS_LDM:
4687 // Relocate the field with the offset of the GOT entry for
4688 // the module index.
4689 got_offset = target->got_section()->address() +
4690 (target->got_mod_index_entry(NULL, NULL, NULL)) -
4691 target->gp_address();
4692 break;
4693 case elfcpp::R_MIPS_GOT_OFST:
4694 if ((gsym != NULL) && (!gsym->is_forced_local()))
4695 {
4696 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4697 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
4698 Valtype* wv = reinterpret_cast<Valtype*>(view);
4699 Reltype addend = elfcpp::Swap<32, big_endian>::readval(wv);
4700 addend &= 0x0000ffffU;
4701
4702 got_offset = addend;
4703 break;
4704 }
4705 case elfcpp::R_MIPS_GOT_PAGE:
4706 {
4707 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4708 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
4709 Valtype* wv = reinterpret_cast<Valtype*>(view);
4710 Reltype addend = elfcpp::Swap<32, big_endian>::readval(wv);
4711 addend &= 0x0000ffffU;
4712 got_offset = target->got_section()->add_constant(psymval->value(object,
4713 addend) + 0x00008000U) & ~0x0000ffffU;
4714 }
4715 break;
4716 default:
4717 break;
4718 }
4719
4720 // Pick the value to use for symbols defined in shared objects.
4721 if (gsym != NULL
4722 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4723 {
4724 elfcpp::Elf_Xword value;
4725
4726 // To avoid problems with symbols that are no longer in .MIPS.stubs
4727 if (gsym->plt_offset() > 0)
4728 {
4729 if (target->is_plt_symbol((Sized_symbol<size> *)gsym))
4730 value = target->plt_section()->address() + gsym->plt_offset();
4731 else
4732 value = target->mips_stubs()->address() +
4733 gsym->plt_offset() * (target->mips_stubs()->big_dyn_symtab() ? 20 : 16);
4734
4735 symval.set_output_value(value);
4736
4737 psymval = &symval;
4738 }
4739 }
4740
4741 // Set symbol for use with stub
4742 switch (r_type)
4743 {
4744 case elfcpp::R_MIPS_26:
4745 case elfcpp::R_MIPS_PC16:
4746 case elfcpp::R_MIPS16_26:
4747 {
4748 if (target->stub_flag())
4749 {
4750 if (gsym == NULL)
4751 break;
4752
4753 Sized_symbol<size> * sym = static_cast<Sized_symbol<size>*>
4754 (target->stub_symbol(gsym->name()));
4755 if (sym != NULL)
4756 {
4757 symval.set_output_value(sym->value());
4758 psymval = &symval;
4759 }
4760 }
4761 }
4762 break;
4763 default:
4764 break;
4765 }
4766
4767 switch (r_type)
4768 {
4769 case elfcpp::R_MIPS_NONE:
4770 case elfcpp::R_MIPS_GNU_VTINHERIT:
4771 case elfcpp::R_MIPS_GNU_VTENTRY:
4772 break;
4773 case elfcpp::R_MIPS_16:
4774 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend, rel_typ e);
4775 break;
4776 case elfcpp::R_MIPS_32:
4777 if ((gsym != NULL)
4778 && ((parameters->options().shared())
4779 || (parameters->options().pie())))
4780 break;
4781
4782 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend, rel_typ e);
4783 break;
4784 case elfcpp::R_MIPS_REL32:
4785 if (parameters->options().shared() && (gsym != NULL))
4786 break;
4787 case elfcpp::R_MIPS_PC32:
4788 reloc_status = Reloc_funcs::relrel32(view, object, psymval, address, r_add end, rel_type);
4789 break;
4790 case elfcpp::R_MIPS_26:
4791 // TODO AS: Check if this is correct.
4792 if (gsym != NULL)
4793 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
4794 gsym->is_forced_local(), r_addend, rel _type);
4795 else
4796 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
4797 false, r_addend, rel_type);
4798 break;
4799 case elfcpp::R_MIPS_HI16:
4800 case elfcpp::R_MIPS16_HI16:
4801 reloc_status = Reloc_funcs::relhi16(view, psymval->value(object, 0), r_add end);
4802 break;
4803 case elfcpp::R_MIPS_LO16:
4804 case elfcpp::R_MIPS16_LO16:
4805 reloc_status = Reloc_funcs::rello16(view, object, psymval, r_addend, rel_t ype);
4806 break;
4807 case elfcpp::R_MIPS_GPREL16:
4808 case elfcpp::R_MIPS16_GPREL:
4809 case elfcpp::R_MIPS_LITERAL:
4810 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
4811 target->gp_address(), r_addend, rel_t ype);
4812 break;
4813 case elfcpp::R_MIPS_GOT16:
4814 case elfcpp::R_MIPS_CALL16:
4815 case elfcpp::R_MIPS16_GOT16:
4816 case elfcpp::R_MIPS16_CALL16:
4817 {
4818 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4819 Mips_output_data_got<size, big_endian>* got = target->got_section();
4820 Valtype got_offset;
4821
4822 if (gsym == NULL)
4823 {
4824 std::stringstream tmp;
4825 tmp << static_cast<unsigned int> (r_info)
4826 << static_cast<unsigned int> (reinterpret_cast<unsigned long long> (object))
4827 << static_cast<unsigned int> (r_offset);
4828 std::string key = tmp.str();
4829
4830 got_offset = got->got16_offset(key);
4831 }
4832 else
4833 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
4834
4835 unsigned char* got_view = got_offset +
4836 (unsigned char* const)got->got_view();
4837
4838 reloc_status = Reloc_funcs::relgot16(view, object, psymval,
4839 target->gp_address(), got_view,
4840 got->address(), got_offset,
4841 gsym, r_addend, rel_type);
4842 }
4843 break;
4844 case elfcpp::R_MIPS_PC16:
4845 case elfcpp::R_MIPS_GNU_REL16_S2:
4846 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
4847 r_addend, rel_type);
4848 break;
4849 case elfcpp::R_MIPS_GPREL32:
4850 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
4851 target->gp_address(), r_addend, rel _type);
4852 break;
4853 case elfcpp::R_MIPS_UNUSED1:
4854 case elfcpp::R_MIPS_UNUSED2:
4855 case elfcpp::R_MIPS_UNUSED3:
4856 break;
4857 case elfcpp::R_MIPS_SHIFT5:
4858 reloc_status = Reloc_funcs::relsh5(view, object, psymval, r_addend, rel_ty pe);
4859 break;
4860 case elfcpp::R_MIPS_SHIFT6:
4861 reloc_status = Reloc_funcs::relsh6(view, object, psymval, r_addend, rel_ty pe);
4862 break;
4863 case elfcpp::R_MIPS_64:
4864 if (parameters->options().shared() && (gsym != NULL))
4865 break;
4866
4867 reloc_status = Reloc_funcs::rel64(view, object, psymval, r_addend, rel_typ e);
4868 break;
4869 case elfcpp::R_MIPS_GOT_HI16:
4870 case elfcpp::R_MIPS_CALL_HI16:
4871 got_offset = ((got_offset + 0x8000) >> 16);
4872 case elfcpp::R_MIPS_GOT_LO16:
4873 case elfcpp::R_MIPS_CALL_LO16:
4874 case elfcpp::R_MIPS_GOT_OFST:
4875 case elfcpp::R_MIPS_GOT_PAGE:
4876 case elfcpp::R_MIPS_GOT_DISP:
4877 case elfcpp::R_MIPS_TLS_GOTTPREL:
4878 case elfcpp::R_MIPS_TLS_GD:
4879 case elfcpp::R_MIPS_TLS_LDM:
4880 reloc_status = Reloc_funcs::relgotofst(view, got_offset);
4881 break;
4882 case elfcpp::R_MIPS_SUB:
4883 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend, rel_ty pe);
4884 break;
4885 case elfcpp::R_MIPS_INSERT_A:
4886 case elfcpp::R_MIPS_INSERT_B:
4887 case elfcpp::R_MIPS_DELETE:
4888 break;
4889 case elfcpp::R_MIPS_HIGHER:
4890 reloc_status = Reloc_funcs::relhigher(view, object, psymval, r_addend, rel _type);
4891 break;
4892 case elfcpp::R_MIPS_HIGHEST:
4893 reloc_status = Reloc_funcs::relhighest(view, object, psymval, r_addend, re l_type);
4894 break;
4895 case elfcpp::R_MIPS_SCN_DISP:
4896 reloc_status = Reloc_funcs::relscndisp(view, object, psymval,
4897 output_section->offset(), r_addend, rel_type);
4898 break;
4899 case elfcpp::R_MIPS_REL16:
4900 case elfcpp::R_MIPS_ADD_IMMEDIATE:
4901 case elfcpp::R_MIPS_PJUMP:
4902 case elfcpp::R_MIPS_RELGOT:
4903 break;
4904 case elfcpp::R_MIPS_JALR:
4905 if ((gsym != NULL) && !(gsym->final_value_is_known()))
4906 break;
4907
4908 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address);
4909 break;
4910 case elfcpp::R_MIPS_TLS_DTPREL32:
4911 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
4912 elfcpp::DTP_OFFSET, r_addend, rel_typ e);
4913 break;
4914 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
4915 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
4916 elfcpp::DTP_OFFSET, r_addend, rel_t ype);
4917 break;
4918 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
4919 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
4920 elfcpp::DTP_OFFSET, r_addend, rel_t ype);
4921 break;
4922 case elfcpp::R_MIPS_TLS_TPREL32:
4923 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval,
4924 elfcpp::TP_OFFSET, r_addend, rel_type );
4925 break;
4926 case elfcpp::R_MIPS_TLS_TPREL_LO16:
4927 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
4928 elfcpp::TP_OFFSET, r_addend, rel_ty pe);
4929 break;
4930 case elfcpp::R_MIPS_TLS_TPREL_HI16:
4931 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
4932 elfcpp::TP_OFFSET, r_addend, rel_ty pe);
4933 break;
4934 case elfcpp::R_MIPS_TLS_DTPMOD64:
4935 case elfcpp::R_MIPS_TLS_DTPREL64:
4936 case elfcpp::R_MIPS_TLS_TPREL64:
4937 case elfcpp::R_MIPS_TLS_DTPMOD32: // TODO AS! For now this reloc do nothing.
4938 break;
4939 case elfcpp::R_MIPS_GLOB_DAT:
4940 reloc_status = Reloc_funcs::relglob(view, object, psymval);
4941 break;
4942 default:
4943 gold_error_at_location(relinfo, relnum, r_offset,
4944 _("unsupported reloc %u"), r_type);
4945 break;
4946 }
4947
4948 // Report any errors.
4949 switch (reloc_status)
4950 {
4951 case Reloc_funcs::STATUS_OKAY:
4952 break;
4953 case Reloc_funcs::STATUS_OVERFLOW:
4954 gold_error_at_location(relinfo, relnum, r_offset,
4955 _("relocation overflow"));
4956 break;
4957 case Reloc_funcs::STATUS_BAD_RELOC:
4958 gold_error_at_location(
4959 relinfo,
4960 relnum,
4961 r_offset,
4962 _("unexpected opcode while processing relocation"));
4963 break;
4964 default:
4965 gold_unreachable();
4966 }
4967
4968 return true;
4969 }
4970
4971 template<int size, bool big_endian>
4972 inline bool
4973 Target_mips<size, big_endian>::Relocate::relocate(
4974 const Relocate_info<size, big_endian>* relinfo,
4975 Target_mips* target,
4976 Output_section* output_section,
4977 size_t relnum,
4978 const elfcpp::Rela<size, big_endian>& reloc,
4979 unsigned int r_type,
4980 const Sized_symbol<size>* gsym,
4981 const Symbol_value<size>* psymval,
4982 unsigned char* view,
4983 typename elfcpp::Elf_types<size>::Elf_Addr address,
4984 section_size_type view_size)
4985 {
4986 return relocate(
4987 relinfo,
4988 target,
4989 output_section,
4990 relnum,
4991 &reloc,
4992 (const elfcpp::Rel<size, big_endian>*) NULL,
4993 elfcpp::SHT_RELA,
4994 r_type,
4995 gsym,
4996 psymval,
4997 view,
4998 address,
4999 view_size);
5000 }
5001
5002 template<int size, bool big_endian>
5003 inline bool
5004 Target_mips<size, big_endian>::Relocate::relocate(
5005 const Relocate_info<size, big_endian>* relinfo,
5006 Target_mips* target,
5007 Output_section* output_section,
5008 size_t relnum,
5009 const elfcpp::Rel<size, big_endian>& reloc,
5010 unsigned int r_type,
5011 const Sized_symbol<size>* gsym,
5012 const Symbol_value<size>* psymval,
5013 unsigned char* view,
5014 typename elfcpp::Elf_types<size>::Elf_Addr address,
5015 section_size_type view_size)
5016 {
5017 return relocate(
5018 relinfo,
5019 target,
5020 output_section,
5021 relnum,
5022 (const elfcpp::Rela<size, big_endian>*) NULL,
5023 &reloc,
5024 elfcpp::SHT_REL,
5025 r_type,
5026 gsym,
5027 psymval,
5028 view,
5029 address,
5030 view_size);
5031 }
5032
5033 // Get the Reference_flags for a particular relocation.
5034
5035 template<int size, bool big_endian>
5036 int
5037 Target_mips<size, big_endian>::Scan::get_reference_flags(
5038 unsigned int r_type)
5039 {
5040 switch (r_type)
5041 {
5042 case elfcpp::R_MIPS_NONE:
5043 case elfcpp::R_MIPS_GNU_VTINHERIT:
5044 case elfcpp::R_MIPS_GNU_VTENTRY:
5045 case elfcpp::R_MIPS_INSERT_A:
5046 case elfcpp::R_MIPS_INSERT_B:
5047 case elfcpp::R_MIPS_DELETE:
5048 case elfcpp::R_MIPS_PJUMP:
5049 case elfcpp::R_MIPS_RELGOT:
5050 case elfcpp::R_MIPS_REL16:
5051 case elfcpp::R_MIPS_ADD_IMMEDIATE:
5052 // No symbol reference.
5053 return 0;
5054
5055 case elfcpp::R_MIPS_16:
5056 case elfcpp::R_MIPS_32:
5057 case elfcpp::R_MIPS_64:
5058 case elfcpp::R_MIPS_HI16:
5059 case elfcpp::R_MIPS_LO16:
5060 case elfcpp::R_MIPS_SHIFT5:
5061 case elfcpp::R_MIPS_SHIFT6:
5062 case elfcpp::R_MIPS_SUB:
5063 case elfcpp::R_MIPS_HIGHER:
5064 case elfcpp::R_MIPS_HIGHEST:
5065 case elfcpp::R_MIPS_SCN_DISP:
5066 case elfcpp::R_MIPS16_HI16:
5067 case elfcpp::R_MIPS16_LO16:
5068 return Symbol::ABSOLUTE_REF;
5069
5070 case elfcpp::R_MIPS_26:
5071 case elfcpp::R_MIPS16_26:
5072 case elfcpp::R_MIPS_GPREL32:
5073 case elfcpp::R_MIPS_GPREL16:
5074 case elfcpp::R_MIPS16_GPREL:
5075 case elfcpp::R_MIPS_REL32:
5076 return Symbol::RELATIVE_REF;
5077
5078 case elfcpp::R_MIPS_PC16:
5079 case elfcpp::R_MIPS_PC32:
5080 case elfcpp::R_MIPS_GNU_REL16_S2:
5081 case elfcpp::R_MIPS_JALR:
5082 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5083
5084 case elfcpp::R_MIPS_GOT16:
5085 case elfcpp::R_MIPS16_GOT16:
5086 case elfcpp::R_MIPS_CALL16:
5087 case elfcpp::R_MIPS16_CALL16:
5088 case elfcpp::R_MIPS_GOT_DISP:
5089 case elfcpp::R_MIPS_GOT_HI16:
5090 case elfcpp::R_MIPS_CALL_HI16:
5091 case elfcpp::R_MIPS_GOT_LO16:
5092 case elfcpp::R_MIPS_CALL_LO16:
5093 case elfcpp::R_MIPS_LITERAL:
5094 case elfcpp::R_MIPS_GOT_PAGE:
5095 case elfcpp::R_MIPS_GOT_OFST:
5096 // Absolute in GOT.
5097 return Symbol::RELATIVE_REF;
5098
5099 case elfcpp::R_MIPS_TLS_DTPMOD32:
5100 case elfcpp::R_MIPS_TLS_DTPREL32:
5101 case elfcpp::R_MIPS_TLS_DTPMOD64:
5102 case elfcpp::R_MIPS_TLS_DTPREL64:
5103 case elfcpp::R_MIPS_TLS_GD:
5104 case elfcpp::R_MIPS_TLS_LDM:
5105 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
5106 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
5107 case elfcpp::R_MIPS_TLS_GOTTPREL:
5108 case elfcpp::R_MIPS_TLS_TPREL32:
5109 case elfcpp::R_MIPS_TLS_TPREL64:
5110 case elfcpp::R_MIPS_TLS_TPREL_HI16:
5111 case elfcpp::R_MIPS_TLS_TPREL_LO16:
5112 return Symbol::TLS_REF;
5113
5114 case elfcpp::R_MIPS_COPY:
5115 case elfcpp::R_MIPS_UNUSED1:
5116 case elfcpp::R_MIPS_UNUSED2:
5117 case elfcpp::R_MIPS_UNUSED3:
5118 case elfcpp::R_MIPS_GLOB_DAT:
5119 case elfcpp::R_MIPS_JUMP_SLOT:
5120 default:
5121 // Not expected. We will give an error later.
5122 return 0;
5123 }
5124 }
5125
5126 // Report an unsupported relocation against a local symbol.
5127
5128 template<int size, bool big_endian>
5129 void
5130 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
5131 Sized_relobj_file<size, big_endian>* object,
5132 unsigned int r_type)
5133 {
5134 gold_error(_("%s: unsupported reloc %u against local symbol"),
5135 object->name().c_str(), r_type);
5136 }
5137
5138 // Report an unsupported relocation against a global symbol.
5139
5140 template<int size, bool big_endian>
5141 void
5142 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
5143 Sized_relobj_file<size, big_endian>* object,
5144 unsigned int r_type,
5145 Symbol* gsym)
5146 {
5147 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5148 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5149 }
5150
5151 template<int size, bool big_endian>
5152 Target::Target_info Target_mips<size, big_endian>::mips_info =
5153 {
5154 size, // size
5155 big_endian, // is_big_endian
5156 elfcpp::EM_MIPS, // machine_code
5157 false, // has_make_symbol
5158 false, // has_resolve
5159 false, // has_code_fill
5160 true, // is_default_stack_executable
5161 false, // can_icf_inline_merge_sections
5162 '\0', // wrap_char
5163 "/lib/ld.so.1", // dynamic_linker
5164 0x400000, // default_text_segment_address
5165 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
5166 4 * 1024, // common_pagesize (overridable by -z common-page-size)
5167 elfcpp::SHN_UNDEF, // small_common_shndx
5168 elfcpp::SHN_UNDEF, // large_common_shndx
5169 0, // small_common_section_flags
5170 0, // large_common_section_flags
5171 NULL, // attributes_section
5172 NULL // attributes_vendor
5173 };
5174
5175 // Adjust ELF file header.
5176
5177 template<int size, bool big_endian>
5178 void
5179 Target_mips<size, big_endian>::do_adjust_elf_header(
5180 unsigned char* view,
5181 int len) const
5182 {
5183 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
5184
5185 elfcpp::Ehdr<size, big_endian> ehdr(view);
5186 unsigned char e_ident[elfcpp::EI_NIDENT];
5187 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
5188
5189 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
5190 e_ident[elfcpp::EI_ABIVERSION] = elfcpp::EV_NONE;
5191
5192 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
5193 oehdr.put_e_ident(e_ident);
5194 oehdr.put_e_flags(this->processor_specific_flags_);
5195 }
5196
5197 // The selector for mips object files.
5198
5199 template<int size, bool big_endian>
5200 class Target_selector_mips : public Target_selector
5201 {
5202 public:
5203 Target_selector_mips()
5204 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
5205 (size == 64 ?
5206 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
5207 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
5208 (size == 64 ?
5209 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
5210 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")))
5211 { }
5212
5213 Target* do_instantiate_target()
5214 { return new Target_mips<size, big_endian>(); }
5215 };
5216
5217 Target_selector_mips<32, false> target_selector_mips32;
5218
5219
5220 } // End anonymous namespace.
OLDNEW
« no previous file with comments | « gold/layout.cc ('k') | gold/output.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698