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

Side by Side Diff: gold/mips.cc

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

Powered by Google App Engine
This is Rietveld 408576698