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

Side by Side Diff: gold/mips.cc

Issue 10252012: [MIPS] Initial checkin for MIPS changes for GOLD. (Closed)
Patch Set: Code updates per previous code review. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gold/layout.cc ('k') | gold/output.h » ('j') | gold/symtab.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // mips.cc -- mips target support for gold.
2
3 // Copyright 2011 Free Software Foundation, Inc.
4 // Written by Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>
5 // based on the i386 code by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
7 // bfd/elf32-mips.c.
8
9 // This file is part of gold.
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
15
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
25
26 #include <set>
27 #include <sstream>
28
29 #include "gold.h"
30
31 #include "elfcpp.h"
32 #include "parameters.h"
33 #include "reloc.h"
34 #include "mips.h"
35 #include "object.h"
36 #include "symtab.h"
37 #include "layout.h"
38 #include "output.h"
39 #include "copy-relocs.h"
40 #include "target.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
43 #include "tls.h"
44 #include "errors.h"
45 #include "gc.h"
46
47 namespace
48 {
49 using namespace gold;
50
51 template<int size, bool big_endian>
52 class Mips_output_data_plt;
53
54 template<int size, bool big_endian>
55 class Mips_output_data_got;
56
57 template<int size, bool big_endian>
58 class Target_mips;
59
60 template<int size>
61 class Mips_output_section_reginfo;
62
63 template<int size, bool big_endian>
64 class Mips_output_data_stub;
65
66 template<int size, bool big_endian>
67 class Mips_output_data_mips_stubs;
68
69 // Mips_output_data_got class. We derive this from Output_data_got to add
70 // extra methods to handle TLS relocations.
71
72 template<int size, bool big_endian>
73 class Mips_output_data_got : public Output_data_got<size, big_endian>
74 {
75 public:
76 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
77 Reloc_section;
78
79 Mips_output_data_got(Symbol_table* symtab, Layout* layout)
80 : Output_data_got<size, big_endian>(), symbol_table_(symtab),
81 layout_(layout), tls_entries_(0), tls_entries_set_(false)
82 {
83 // According to MIPS O32 ABI zero entry of GOT is reserved.
84 this->add_constant(0);
85 // Module pointer (GNU extension)
86 this->add_constant(0x80000000);
87 }
88
89 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
90 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
91 // applied in a static link.
92 void
93 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
94 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
95
96 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
97 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
98 // relocation that needs to be applied in a static link.
99 void
100 add_static_reloc(unsigned int got_offset, unsigned int r_type,
101 Sized_relobj_file<size, big_endian>* relobj,
102 unsigned int index)
103 {
104 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
105 index));
106 }
107
108 void
109 add_tls_gd_with_static_reloc(unsigned int got_type, Symbol* gsym);
110
111 // Same as the above but for a local symbol in OBJECT with INDEX.
112 void
113 add_tls_gd_local_reloc(unsigned int got_type,
114 Sized_relobj_file<size, big_endian>* object,
115 unsigned int index);
116
117 void
118 add_got16_view(std::string key, Mips_output_data_got<size, big_endian>* got)
119
120 {
121 if(got16_views_.count(key) == 0)
122 got16_views_[key] = got->add_constant(0);
123 }
124
125 unsigned int
126 got16_offset(std::string key)
127 {
128 if(got16_views_.count(key) == 0)
129 gold_error("internal error in GOT16 relocation");
130
131 return got16_views_[key];
132 }
133
134 unsigned char*
135 got_view()
136 { return this->got_view_; }
137
138 Symbol_table*
139 symbol_table()
140 { return this->symbol_table_; }
141
142 Layout*
143 layout()
144 { return this->layout_; }
145
146 void
147 add_global_with_reloc_mips(Symbol* gsym)
148 {
149 this->global_relocs_.push(gsym);
150 }
151
152 void
153 add_global_tls_with_reloc_mips(Symbol* gsym, Reloc_section* rel_dyn,
154 unsigned int r_type)
155 {
156 this->tls_relocs_.push(Tls_reloc(gsym, rel_dyn, r_type));
157 }
158
159 void
160 add_global_relocs()
161 {
162 while(!this->global_relocs_.empty())
163 {
164 Symbol *gsym = this->global_relocs_.front();
165
166 this->add_global(gsym, 0);
167 gsym->set_needs_dynsym_entry();
168 this->global_relocs_.pop();
169 }
170 }
171
172 void
173 add_global_tls_relocs()
174 {
175 while(!this->tls_relocs_.empty())
176 {
177 Tls_reloc tls_rel = this->tls_relocs_.front();
178 Symbol *gsym = tls_rel.symbol();
179
180 if(!gsym->has_got_offset(1))
181 {
182 unsigned int r_type = tls_rel.r_type();
183 Reloc_section* rel_dyn = tls_rel.rel_dyn();
184
185 this->tls_entries_++;
186
187 if(parameters->options().output_is_position_independent())
188 {
189 this->add_global(gsym, 1);
190 rel_dyn->add_absolute(r_type, this, gsym->got_offset(1));
191 }
192 else
193 {
194 this->add_global_with_rel(gsym, 1, rel_dyn, r_type);
195 }
196
197 gsym->set_needs_dynsym_entry();
198 }
199
200 this->tls_relocs_.pop();
201 }
202
203 this->tls_entries_set_ = true;
204 }
205
206 unsigned int
207 tls_entries()
208 {
209 if(this->tls_entries_set_)
210 return this->tls_entries_;
211
212 return 0;
213 }
214 protected:
215 // Write out the GOT table.
216 void
217 do_write(Output_file*);
218
219 private:
220 class Tls_reloc
221 {
222 public:
223 Tls_reloc(Symbol* gsym, Reloc_section* rel_dyn, unsigned int r_type)
224 : symbol_(gsym), rel_dyn_(rel_dyn), r_type_(r_type)
225 { }
226
227 Symbol*
228 symbol()
229 { return this->symbol_; }
230
231 Reloc_section*
232 rel_dyn()
233 { return this->rel_dyn_; }
234
235 unsigned int
236 r_type()
237 { return this->r_type_; }
238
239 private:
240 Symbol* symbol_;
241 Reloc_section* rel_dyn_;
242 unsigned int r_type_;
243 };
244
245 // This class represent dynamic relocations that need to be applied by
246 // gold because we are using TLS relocations in a static link.
247 class Static_reloc
248 {
249 public:
250 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
251 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
252 { this->u_.global.symbol = gsym; }
253
254 Static_reloc(unsigned int got_offset, unsigned int r_type,
255 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
256 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
257 {
258 this->u_.local.relobj = relobj;
259 this->u_.local.index = index;
260 }
261
262 // Return the GOT offset.
263 unsigned int
264 got_offset() const
265 { return this->got_offset_; }
266
267 // Relocation type.
268 unsigned int
269 r_type() const
270 { return this->r_type_; }
271
272 // Whether the symbol is global or not.
273 bool
274 symbol_is_global() const
275 { return this->symbol_is_global_; }
276
277 // For a relocation against a global symbol, the global symbol.
278 Symbol*
279 symbol() const
280 {
281 gold_assert(this->symbol_is_global_);
282 return this->u_.global.symbol;
283 }
284
285 // For a relocation against a local symbol, the defining object.
286 Sized_relobj_file<size, big_endian>*
287 relobj() const
288 {
289 gold_assert(!this->symbol_is_global_);
290 return this->u_.local.relobj;
291 }
292
293 // For a relocation against a local symbol, the local symbol index.
294 unsigned int
295 index() const
296 {
297 gold_assert(!this->symbol_is_global_);
298 return this->u_.local.index;
299 }
300
301 private:
302 // GOT offset of the entry to which this relocation is applied.
303 unsigned int got_offset_;
304 // Type of relocation.
305 unsigned int r_type_;
306 // Whether this relocation is against a global symbol.
307 bool symbol_is_global_;
308 // A global or local symbol.
309 union
310 {
311 struct
312 {
313 // For a global symbol, the symbol itself.
314 Symbol* symbol;
315 } global;
316 struct
317 {
318 // For a local symbol, the object defining object.
319 Sized_relobj_file<size, big_endian>* relobj;
320 // For a local symbol, the symbol index.
321 unsigned int index;
322 } local;
323 } u_;
324 };
325
326 // Symbol table of the output object.
327 Symbol_table* symbol_table_;
328 // Layout of the output object.
329 Layout* layout_;
330 // Static relocs to be applied to the GOT.
331 std::vector<Static_reloc> static_relocs_;
332 // Got16 relocs that are related to the GOT
333 std::map<std::string, unsigned int> got16_views_;
334 // .got section view
335 unsigned char* got_view_;
336 // List of global relocs that should be at the end of .got section
337 std::queue<Symbol *> global_relocs_;
338 // List of global tls relocs that should be at the end of .got section
339 std::queue<Tls_reloc> tls_relocs_;
340 // Number of TLS entries
341 unsigned int tls_entries_;
342 // Is number of TLS entries set
343 bool tls_entries_set_;
344 };
345
346 // Mips reginfo output section class
347
348 template<int size>
349 class Mips_output_section_reginfo : public Output_section
350 {
351 public:
352 Mips_output_section_reginfo (const char *name, elfcpp::Elf_Word type,
353 elfcpp::Elf_Xword flags):Output_section (name, type, flags)
354 {
355 this->set_always_keeps_input_sections();
356
357 gp_ = NULL;
358
359 for(size_t i = 0; i < 24; i++)
360 data_[i] = 0;
361 }
362
363 ~Mips_output_section_reginfo()
364 { };
365
366 void
367 set_gp(Sized_symbol<size> *gp)
368 { this->gp_ = gp; }
369
370 // Downcast a base pointer to an Mips_output_section_reginfo pointer.
371 static Mips_output_section_reginfo<size>*
372 as_mips_output_section_reginfo(Output_section* os)
373 { return static_cast<Mips_output_section_reginfo<size>*>(os); }
374
375 protected:
376 // Set the final data size.
377 void set_final_data_size();
378
379 // Write reginfo section into file OF.
380 void do_write (Output_file * of);
381
382 private:
383 // Data.
384 unsigned char data_[24];
385
386 // _gp symbol
387 Sized_symbol<size>* gp_;
388 };
389
390 // The MIPS target has many relocation types with odd-sizes or noncontiguous
391 // bits. The default handling of relocatable relocation cannot process these
392 // relocations. So we have to extend the default code.
393
394 template<bool big_endian, int sh_type, typename Classify_reloc>
395 class Mips_scan_relocatable_relocs :
396 public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
397 {
398 public:
399 // Return the strategy to use for a local symbol which is a section
400 // symbol, given the relocation type.
401 inline Relocatable_relocs::Reloc_strategy
402 local_section_strategy(unsigned int r_type, Relobj *object)
403 {
404 switch(r_type)
405 {
406 case elfcpp::R_MIPS_26:
407 return Relocatable_relocs::RELOC_SPECIAL;
408
409 default:
410 return Default_scan_relocatable_relocs<sh_type, Classify_reloc>::local _section_strategy(r_type, object);
411 }
412 }
413 };
414
415 template<int size, bool big_endian>
416 class Target_mips : public Sized_target<size, big_endian>
417 {
418 public:
419 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
420 Reloc_section;
421 typedef 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 return this->rel_dyn_;
1542 }
1543
1544 // Get the GOT section, creating it if necessary.
1545
1546 template<int size, bool big_endian>
1547 Mips_output_data_got<size, big_endian>*
1548 Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
1549 Layout* layout)
1550 {
1551 if (this->got_ == NULL)
1552 {
1553 gold_assert(symtab != NULL && layout != NULL);
1554
1555 this->got_ = new Mips_output_data_got<size, big_endian>(symtab, layout);
1556
1557 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1558
1559 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1560 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
1561 elfcpp::SHF_MIPS_GPREL,
1562 this->got_, ORDER_DATA, false);
1563
1564 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1565 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1566 Symbol_table::PREDEFINED,
1567 this->got_,
1568 0, 0, elfcpp::STT_OBJECT,
1569 elfcpp::STB_GLOBAL,
1570 elfcpp::STV_DEFAULT, 0,
1571 false, false);
1572 }
1573
1574 // Set _gp value, too.
1575 this->set_gp(layout, symtab);
1576
1577 return this->got_;
1578 }
1579
1580 template<int size, bool big_endian>
1581 void
1582 Mips_output_data_got<size, big_endian>::add_tls_gd_with_static_reloc(
1583 unsigned int got_type,
1584 Symbol* gsym)
1585 {
1586 if (gsym->has_got_offset(got_type))
1587 return;
1588
1589 // We are doing a static link. Just mark it as belong to module 1,
1590 // the executable.
1591 unsigned int got_offset = this->add_constant(1);
1592 gsym->set_got_offset(got_type, got_offset);
1593 got_offset = this->add_constant(0);
1594 this->static_relocs_.push_back(Static_reloc(got_offset,
1595 size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : elfcpp::R_MIPS_TLS_DTPREL64,
1596 gsym));
1597 }
1598
1599 template<int size, bool big_endian>
1600 void
1601 Mips_output_data_got<size, big_endian>::add_tls_gd_local_reloc(
1602 unsigned int got_type,
1603 Sized_relobj_file<size, big_endian>* object,
1604 unsigned int index)
1605 {
1606 if (object->local_has_got_offset(index, got_type))
1607 return;
1608
1609 unsigned int got_offset = this->add_constant(1);
1610 object->set_local_got_offset(index, got_type, got_offset);
1611 got_offset = this->add_constant(0);
1612 this->static_relocs_.push_back(Static_reloc(got_offset,
1613 size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : elfcpp::R_MIPS_TLS_DTPREL64,
1614 object, index));
1615 }
1616
1617 // Calculate value of gp_.
1618
1619 template<int size, bool big_endian>
1620 void
1621 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
1622 {
1623 if(gp_ == NULL)
1624 {
1625 Sized_symbol<size>* sym =
1626 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
1627
1628 if((sym == NULL) || (sym->source() != Symbol::IS_CONSTANT))
1629 {
1630 Output_data *got_section = layout->find_output_section(".got");
1631
1632 gold_assert(got_section != NULL);
1633
1634 if(sym == NULL)
1635 sym =static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
1636 "_gp", NULL, Symbol_table::PREDEFINED,
1637 got_section, 0x7ff0, 0,
1638 elfcpp::STT_OBJECT,
1639 elfcpp::STB_GLOBAL,
1640 elfcpp::STV_DEFAULT,
1641 0, false, false));
1642 else
1643 {
1644 sym->init_output_data("_gp", NULL, got_section, 0x7ff0, 0,
1645 elfcpp::STT_OBJECT,
1646 elfcpp::STB_GLOBAL,
1647 elfcpp::STV_DEFAULT, 0,
1648 false, false);
1649 }
1650 gold_assert(sym != NULL);
1651 }
1652
1653 this->gp_ = sym;
1654 }
1655 }
1656
1657 // A class to handle the STUB data.
1658
1659 template<int size, bool big_endian>
1660 class Mips_output_data_stub : public Output_section_data
1661 {
1662 public:
1663 typedef std::vector<Symbol *> symbol_list;
1664 Mips_output_data_stub();
1665
1666 // Add an entry to the STUB.
1667 void add_entry(Symbol_table* symtab,
1668 Target_mips<size, big_endian>* target,
1669 Symbol* gsym);
1670
1671 // Return the number of STUB entries.
1672 unsigned int
1673 entry_count() const
1674 { return this->count_; }
1675
1676 // Add stub symbol
1677 void
1678 add_symbol(Symbol *sym)
1679 { symbols.push_back(sym); }
1680
1681 // Get symbol on position n
1682 Symbol *
1683 symbol(unsigned int n)
1684 { return symbols[n]; }
1685
1686 protected:
1687 void do_adjust_output_section(Output_section* os);
1688
1689 private:
1690 // Template for subsequent STUB entries.
1691 static const uint32_t stub_entry[4];
1692
1693 // Set the final size.
1694 void
1695 set_final_data_size()
1696 {
1697 unsigned int full_count = this->count_ * 4;
1698
1699 this->set_data_size(full_count * 4);
1700 }
1701
1702 // Write out the STUB data.
1703 void
1704 do_write(Output_file*);
1705
1706 // The number of STUB entries.
1707 unsigned int count_;
1708
1709 // List of symbols used in stubs
1710 symbol_list symbols;
1711 };
1712
1713 // A class to handle the PLT data.
1714
1715 template<int size, bool big_endian>
1716 class Mips_output_data_plt : public Output_section_data
1717 {
1718 public:
1719 typedef Output_data_reloc<elfcpp::SHT_REL, true,
1720 size, big_endian> Reloc_section;
1721
1722 Mips_output_data_plt(Layout*, Output_data_space*);
1723
1724 // Add an entry to the PLT.
1725 void add_entry(Symbol* gsym);
1726
1727 // Return the .rel.plt section data.
1728 const Reloc_section* rel_plt() const
1729 { return this->rel_; }
1730
1731 // Return the number of PLT entries.
1732 unsigned int
1733 entry_count() const
1734 { return this->count_; }
1735
1736 // Return the offset of the first non-reserved PLT entry.
1737 static unsigned int
1738 first_plt_entry_offset()
1739 { return sizeof(first_plt_entry[0]); }
1740
1741 // Return the size of a PLT entry.
1742 static unsigned int
1743 plt_entry_size()
1744 { return sizeof(plt_entry); }
1745
1746 protected:
1747 void do_adjust_output_section(Output_section* os);
1748
1749 // Write to a map file.
1750 void
1751 do_print_to_mapfile(Mapfile* mapfile) const
1752 { mapfile->print_output_data(this, _(".plt")); }
1753
1754 private:
1755 // Template for the first PLT entry.
1756 static const uint32_t first_plt_entry[3][8];
1757
1758 // Template for subsequent PLT entries.
1759 static const uint32_t plt_entry[4];
1760
1761 // Set the final size.
1762 void
1763 set_final_data_size()
1764 {
1765 unsigned int full_count = this->count_ * 4 + 8;
1766
1767 this->set_data_size(full_count * 4);
1768 }
1769
1770 // MIPS abi types
1771 enum Abi_type
1772 {
1773 MIPS_ABI_ERROR = -1, // Error when getting ABI type
1774 MIPS_O32 = 0, // Mips O32 ABI
1775 MIPS_N32 = 1, // Mips N32 ABI
1776 MIPS_N64 = 2, // Mips N64 ABI
1777 MIPS_O64 = 3, // Mips O64 ABI
1778 MIPS_EABI32 = 4, // Mips 32 EABI
1779 MIPS_EABI64 = 5, // Mips 64 EABI
1780 };
1781
1782 // Get MIPS abi
1783 Abi_type
1784 abi(elfcpp::Elf_Word /*flags*/) const
1785 {
1786 #if 0
1787 // MIPS ABI2
1788 if((flags & elfcpp::EF_MIPS_ABI2) != 0)
1789 {
1790 switch(flags & elfcpp::EF_MIPS_ABI)
1791 {
1792 case elfcpp::E_MIPS_ABI_O32:
1793 return MIPS_N32;
1794 case elfcpp::E_MIPS_ABI_O64:
1795 return MIPS_N64;
1796 case elfcpp::E_MIPS_ABI_EABI32:
1797 return MIPS_EABI32; // TODO AS: Check if this is correct!!!!
1798 case elfcpp::E_MIPS_ABI_EABI64:
1799 return MIPS_EABI64; // TODO AS: Check if this is correct!!!!
1800 default:
1801 return MIPS_ABI_ERROR;
1802 }
1803 }
1804 else
1805 {
1806 switch(flags & elfcpp::EF_MIPS_ABI)
1807 {
1808 case elfcpp::E_MIPS_ABI_O32:
1809 return MIPS_O32;
1810 case elfcpp::E_MIPS_ABI_O64:
1811 return MIPS_O64;
1812 case elfcpp::E_MIPS_ABI_EABI32:
1813 return MIPS_EABI32; // TODO AS: Check if this is correct!!!!
1814 case elfcpp:: E_MIPS_ABI_EABI64:
1815 return MIPS_EABI64; // TODO AS: Check if this is correct!!!!
1816 default:
1817 return MIPS_ABI_ERROR;
1818 }
1819 }
1820 #endif
1821 // TODO AS: For now only support O32 ABI
1822 return MIPS_O32;
1823 }
1824
1825 // Write out the PLT data.
1826 void
1827 do_write(Output_file*);
1828
1829 // The reloc section.
1830 Reloc_section* rel_;
1831 // The .got.plt section.
1832 Output_data_space* got_plt_;
1833 // The number of PLT entries.
1834 unsigned int count_;
1835 };
1836
1837 // A class to handle the .MIPS.stubs data.
1838
1839 template<int size, bool big_endian>
1840 class Mips_output_data_mips_stubs : public Output_section_data
1841 {
1842 public:
1843 Mips_output_data_mips_stubs(Layout* layout,
1844 Mips_output_data_got<size, big_endian>* got, Sized_symbol<size>* gp)
1845 : Output_section_data(size == 32 ? 4 : 8), count_(1), got_(got), gp_(gp),
1846 layout_(layout), big_dyn_symtab_(false)
1847 { };
1848
1849 // Add an entry to the .MIPS.stubs.
1850 void add_entry(Symbol* gsym);
1851
1852 // Return the number of .MIPS.stubs entries.
1853 unsigned int
1854 entry_count() const
1855 { return this->count_; }
1856
1857 // Return if symbol is in .plt
1858 bool
1859 is_plt_symbol(Sized_symbol<size> *sym)
1860 {
1861 if(this->symbols_.size() == 0)
1862 return true;
1863
1864 return (this->symbols_.count(sym) == 0);
1865 }
1866
1867 // Remove symbol that should not have entry.
1868 void
1869 remove(Symbol *sym)
1870 {
1871 if(this->symbols_.size() == 0)
1872 return;
1873
1874 if(this->symbols_.count(sym) == 0)
1875 return;
1876
1877 this->symbols_.erase(sym);
1878 --this->count_;
1879 }
1880
1881 // Set if dynamic symbole table has more then 0x10000 entries.
1882 void
1883 set_big_dyn_symtab(bool big_dyn_symtab)
1884 { big_dyn_symtab_ = big_dyn_symtab; }
1885
1886 // If dynamic symbole table has more then 0x10000 entries.
1887 bool
1888 big_dyn_symtab()
1889 { return this->big_dyn_symtab_; }
1890
1891 // Fix values of used symbols in symbol table.
1892 void
1893 fix_symbol_values();
1894
1895 protected:
1896 void do_adjust_output_section(Output_section* os);
1897
1898 // Write to a map file.
1899 void
1900 do_print_to_mapfile(Mapfile* mapfile) const
1901 { mapfile->print_output_data(this, _(".MIPS.stubs")); }
1902
1903 private:
1904 // Template for the .MIPS.stubs instructions.
1905 // lw t9, .got offset from _gp
1906 unsigned long
1907 stub_lw()
1908 { return 0x8f990000 | ((this->got_->address() - this->gp_->value()) & 0xffff) ; }
1909
1910 // addu t7,ra
1911 unsigned long
1912 stub_move()
1913 { return 0x03e07821; }
1914
1915 // lui t8,val
1916 unsigned long
1917 stub_lui(int val)
1918 { return (0x3c180000 + (val & 0xffff)); }
1919
1920 //jalr t9,ra
1921 unsigned long
1922 stub_jalr()
1923 { return 0x0320f809; }
1924
1925 // ori t8,t8,val
1926 unsigned long
1927 stub_ori(int val)
1928 { return (0x37180000 + (val & 0xffff)); }
1929
1930 // ori t8,zero,val unsigned
1931 unsigned long
1932 stub_li16u(int val)
1933 { return (0x34180000 + (val & 0xffff)); }
1934
1935 // addiu t8,zero,val sign extended
1936 unsigned long
1937 stub_li16s(int val)
1938 { return (0x24180000 + (val & 0xffff)); }
1939
1940 // Set the final size.
1941 void
1942 set_final_data_size()
1943 {
1944 unsigned int full_count = this->count_ * (this->big_dyn_symtab_ ? 5 : 4);
1945
1946 this->set_data_size(full_count * 4);
1947 }
1948
1949 // Write out the .MIPS.stubs data.
1950 void
1951 do_write(Output_file*);
1952
1953 // The number of .MIPS.stubs entries.
1954 unsigned int count_;
1955 // .got section
1956 Mips_output_data_got<size, big_endian>* got_;
1957 // _gp symbol
1958 Sized_symbol<size>* gp_;
1959 // Layout
1960 Layout* layout_;
1961 // Set if dynamic symbol table has more then 0x10000 entries.
1962 bool big_dyn_symtab_;
1963 // .MIPS.stubs symbols
1964 std::set<Symbol *> symbols_;
1965 };
1966
1967 template<int size, bool big_endian>
1968 void
1969 Mips_output_data_mips_stubs<size, big_endian>::do_adjust_output_section(
1970 Output_section* os)
1971 { os->set_entsize(0); }
1972
1973 // Add an entry to the .MIPS.stubs.
1974
1975 template<int size, bool big_endian>
1976 void
1977 Mips_output_data_mips_stubs<size, big_endian>::add_entry(Symbol* sym)
1978 {
1979 gold_assert(!sym->has_plt_offset());
1980 sym->set_plt_offset(0*this->count_);
1981
1982 this->symbols_.insert(sym);
1983
1984 sym->set_needs_dynsym_entry();
1985 ++this->count_;
1986 }
1987
1988 // Fix values of used symbols in symbol table.
1989 template<int size, bool big_endian>
1990 void
1991 Mips_output_data_mips_stubs<size, big_endian>::fix_symbol_values()
1992 {
1993 int i = 0;
1994 for (std::set<Symbol *>::const_iterator p = this->symbols_.begin();
1995 p != this->symbols_.end(); ++p, i++)
1996 {
1997 Sized_symbol<size> *sym = static_cast<Sized_symbol<size>*> (*p);
1998 elfcpp::Elf_Xword value = this->address() + i
1999 * (this->big_dyn_symtab_ ? 20 : 16);
2000 sym->set_value(value);
2001 }
2002 }
2003
2004 // Write out the .MIPS.stubs. This uses the hand-coded instructions and
2005 // adjusts them as needed.
2006
2007 template<int size, bool big_endian>
2008 void
2009 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
2010 {
2011 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2012
2013 // Read processor-specific flags in ELF file header.
2014 const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset,
2015 elfcpp::Elf_sizes<size>::ehdr_size);
2016 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
2017
2018 const off_t offset = this->offset();
2019 const section_size_type oview_size =
2020 convert_to_section_size_type(this->data_size()) +
2021 (this->big_dyn_symtab_ * (this->symbols_.size() + 1) * 4);
2022 unsigned char* const oview = of->get_output_view(offset, oview_size);
2023
2024 unsigned char* pov = oview;
2025
2026 for (std::set<Symbol *>::const_iterator p = this->symbols_.begin();
2027 p != this->symbols_.end(); ++p)
2028 {
2029 // Fill the stub.
2030 uint32_t stub_insn0 = stub_lw();
2031 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0);
2032 pov += 4;
2033 uint32_t stub_insn1 = stub_move();
2034 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn1);
2035 pov += 4;
2036 if(this->big_dyn_symtab_)
2037 {
2038 uint32_t stub_insn2 = stub_lui(((*p)->dynsym_index() >> 16) & 0x7fff);
2039 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn2);
2040 pov += 4;
2041 }
2042 uint32_t stub_insn3 = stub_jalr();
2043 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn3);
2044 pov += 4;
2045 if(this->big_dyn_symtab_)
2046 {
2047 uint32_t stub_insn4 = stub_ori((*p)->dynsym_index() & 0xffff);
2048 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn4);
2049 }
2050 else if ((*p)->dynsym_index() & ~0x7fff)
2051 {
2052 uint32_t stub_insn5 = stub_li16u((*p)->dynsym_index() & 0xffff);
2053 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn5);
2054 }
2055 else
2056 {
2057 uint32_t stub_insn6 = stub_li16s((*p)->dynsym_index());
2058 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn6);
2059 }
2060 pov += 4;
2061 }
2062
2063 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2064 pov += 4;
2065 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2066 pov += 4;
2067 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2068 pov += 4;
2069 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2070 pov += 4;
2071
2072 if(this->big_dyn_symtab_)
2073 {
2074 elfcpp::Swap<32, big_endian>::writeval(pov, 0UL);
2075 pov += 4;
2076 }
2077
2078 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2079
2080 of->write_output_view(offset, oview_size, oview);
2081 }
2082
2083 // Create the PLT section. The ordinary .got section is an argument,
2084 // since we need to refer to the start.
2085
2086 template<int size, bool big_endian>
2087 Mips_output_data_plt<size, big_endian>::Mips_output_data_plt(Layout* layout,
2088 Output_data_space* got_plt)
2089 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), count_(0)
2090 {
2091 this->rel_ = new Reloc_section(false);
2092 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
2093 elfcpp::SHF_ALLOC, this->rel_,
2094 ORDER_DYNAMIC_PLT_RELOCS, false);
2095 }
2096
2097 template<int size, bool big_endian>
2098 void
2099 Mips_output_data_plt<size, big_endian>::do_adjust_output_section(
2100 Output_section* os)
2101 { os->set_entsize(0); }
2102
2103 // Add an entry to the PLT.
2104
2105 template<int size, bool big_endian>
2106 void
2107 Mips_output_data_plt<size, big_endian>::add_entry(Symbol* gsym)
2108 {
2109 gold_assert(!gsym->has_plt_offset());
2110 section_offset_type plt_offset = (this->count_) * sizeof(plt_entry)
2111 + sizeof(first_plt_entry[0]);
2112 gsym->set_plt_offset(plt_offset);
2113
2114 ++this->count_;
2115
2116 section_offset_type got_offset = this->got_plt_->current_data_size();
2117
2118 // Every PLT entry needs a GOT entry which points back to the PLT
2119 // entry (this will be changed by the dynamic linker, normally
2120 // lazily when the function is called).
2121 this->got_plt_->set_current_data_size(got_offset + 4);
2122
2123 gsym->set_needs_dynsym_entry();
2124 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
2125 got_offset);
2126 }
2127
2128 // Mips PLTs.
2129
2130 // The first entry in the PLT.
2131
2132 template<int size, bool big_endian>
2133 const uint32_t Mips_output_data_plt<size, big_endian>::first_plt_entry[3][8] =
2134 {
2135 // The format of the first PLT entry in an O32 executable.
2136 {
2137 0x3c1c0000, // lui $28, %hi(&GOTPLT[0])
2138 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28)
2139 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0])
2140 0x031cc023, // subu $24, $24, $28
2141 0x03e07821, // move $15, $31
2142 0x0018c082, // srl $24, $24, 2
2143 0x0320f809, // jalr $25
2144 0x2718fffe // subu $24, $24, 2
2145 },
2146
2147 // The format of the first PLT entry in an N32 executable. Different
2148 // because gp ($28) is not available; we use t2 ($14) instead.
2149 {
2150 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
2151 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14)
2152 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
2153 0x030ec023, // subu $24, $24, $14
2154 0x03e07821, // move $15, $31
2155 0x0018c082, // srl $24, $24, 2
2156 0x0320f809, // jalr $25
2157 0x2718fffe // subu $24, $24, 2
2158 },
2159
2160 // The format of the first PLT entry in an N64 executable. Different
2161 // from N32 because of the increased size of GOT entries.
2162 {
2163 0x3c0e0000, // lui $14, %hi(&GOTPLT[0])
2164 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14)
2165 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0])
2166 0x030ec023, // subu $24, $24, $14
2167 0x03e07821, // move $15, $31
2168 0x0018c0c2, // srl $24, $24, 3
2169 0x0320f809, // jalr $25
2170 0x2718fffe // subu $24, $24, 2
2171 }
2172 };
2173
2174 // Subsequent entries in the PLT.
2175
2176 template<int size, bool big_endian>
2177 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[4] =
2178 {
2179 0x3c0f0000, // lui $15, %hi(.got.plt entry)
2180 0x8df90000, // l[wd] $25, %lo(.got.plt entry)($15)
2181 0x03200008, // jr $25
2182 0x25f80000 // addiu $24, $15, %lo(.got.plt entry)
2183 };
2184
2185 // Write out the PLT. This uses the hand-coded instructions above,
2186 // and adjusts them as needed.
2187
2188 template<int size, bool big_endian>
2189 void
2190 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
2191 {
2192 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2193
2194 // Read processor-specific flags in ELF file header.
2195 const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset,
2196 elfcpp::Elf_sizes<size>::ehdr_size);
2197 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
2198 const size_t abi = this->abi(ehdr.get_e_flags());
2199
2200 const off_t offset = this->offset();
2201 const section_size_type oview_size =
2202 convert_to_section_size_type(this->data_size());
2203 unsigned char* const oview = of->get_output_view(offset, oview_size);
2204
2205 const off_t got_file_offset = this->got_plt_->offset();
2206 const section_size_type got_size =
2207 convert_to_section_size_type(this->got_plt_->data_size());
2208 unsigned char* const got_view = of->get_output_view(got_file_offset,
2209 got_size);
2210 unsigned char* pov = oview;
2211
2212 Mips_address plt_address = this->address();
2213 Mips_address got_address = this->got_plt_->address();
2214
2215 uint32_t plt_insn0 = first_plt_entry[abi][0] | (((got_address + 0x8000) >> 16) & 0xffff);
2216 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2217 uint32_t plt_insn1 = first_plt_entry[abi][1] | (got_address & 0xffff);
2218 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2219 uint32_t plt_insn2 = first_plt_entry[abi][2] | (got_address & 0xffff);
2220 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2221
2222 for(size_t i = 3; i < 8; i++)
2223 elfcpp::Swap<32, big_endian>::writeval(pov + i * 4,
2224 first_plt_entry[abi][i]);
2225
2226 pov += sizeof(first_plt_entry[abi]);
2227
2228 unsigned char* got_pov = got_view;
2229
2230 memset(got_pov, 0, 8);
2231 got_pov += 8;
2232
2233 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
2234 unsigned int plt_offset = sizeof(first_plt_entry[abi]);
2235 unsigned int plt_rel_offset = 0;
2236 unsigned int got_offset = 8;
2237 const unsigned int count = this->count_;
2238 for (unsigned int i = 0;
2239 i < count;
2240 ++i,
2241 pov += sizeof(plt_entry),
2242 got_pov += 4,
2243 plt_offset += sizeof(plt_entry),
2244 plt_rel_offset += rel_size,
2245 got_offset += 4)
2246 {
2247 // Set and adjust the PLT entry itself.
2248 int32_t offset = (got_address + got_offset);
2249
2250 uint32_t plt_insn0 = plt_entry[0] | (((offset + 0x8000) >> 16) & 0xffff);
2251 elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
2252 uint32_t plt_insn1 = plt_entry[1] | (offset & 0xffff);
2253 elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
2254 uint32_t plt_insn2 = plt_entry[2];
2255 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
2256 uint32_t plt_insn3 = plt_entry[3] | (offset & 0xffff);
2257 elfcpp::Swap<32, big_endian>::writeval(pov + 12, plt_insn3);
2258
2259 // Set the entry in the GOT.
2260 elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
2261 }
2262
2263 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2264 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2265
2266 of->write_output_view(offset, oview_size, oview);
2267 of->write_output_view(got_file_offset, got_size, got_view);
2268 }
2269
2270 // Create a GOT entry for the TLS module index.
2271
2272 template<int size, bool big_endian>
2273 unsigned int
2274 Target_mips<size, big_endian>::got_mod_index_entry(
2275 Symbol_table* symtab,
2276 Layout* layout,
2277 Sized_relobj_file<size, big_endian>* object)
2278 {
2279 if (this->got_mod_index_offset_ == -1U)
2280 {
2281 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2282 Mips_output_data_got<size, big_endian>* got =
2283 this->got_section(symtab, layout);
2284 unsigned int got_offset;
2285 if (!parameters->doing_static_link())
2286 {
2287 got_offset = got->add_constant(0);
2288
2289 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
2290 rel_dyn->add_local(object, 0,
2291 size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 :
2292 elfcpp::R_MIPS_TLS_DTPMOD64, got,
2293 got_offset);
2294 }
2295 else
2296 {
2297 // We are doing a static link. Just mark it as belong to module 1,
2298 // the executable.
2299 got_offset = got->add_constant(1);
2300 }
2301
2302 got->add_constant(0);
2303 this->got_mod_index_offset_ = got_offset;
2304 }
2305
2306 return this->got_mod_index_offset_;
2307 }
2308
2309 // Create a PLT entry for a global symbol.
2310
2311 template<int size, bool big_endian>
2312 void
2313 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
2314 Layout* layout,
2315 Symbol* gsym)
2316 {
2317 if (gsym->has_plt_offset())
2318 return;
2319
2320 if (this->plt_ == NULL)
2321 {
2322 // Create the GOT section first.
2323 this->got_section(symtab, layout);
2324
2325 // Create .rel.dyn section.
2326 this->rel_dyn_section(layout);
2327
2328 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2329 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
2330 this->got_plt_, ORDER_DATA, false);
2331
2332 // The first two entries are reserved.
2333 this->got_plt_->set_current_data_size(2 * 4);
2334
2335 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
2336 this->got_plt_);
2337 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2338 (elfcpp::SHF_ALLOC
2339 | elfcpp::SHF_EXECINSTR),
2340 this->plt_, ORDER_PLT, false);
2341
2342 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
2343 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
2344 Symbol_table::PREDEFINED,
2345 this->plt_,
2346 0, 0, elfcpp::STT_FUNC,
2347 elfcpp::STB_LOCAL,
2348 elfcpp::STV_DEFAULT, 0,
2349 false, false);
2350 }
2351
2352 this->plt_->add_entry(gsym);
2353 }
2354
2355
2356 // Create a .MIPS.stubs entry for a global symbol.
2357
2358 template<int size, bool big_endian>
2359 void
2360 Target_mips<size, big_endian>::make_mips_stubs_entry(Symbol_table* symtab,
2361 Layout* layout,
2362 Symbol* gsym)
2363 {
2364 if (gsym->has_plt_offset())
2365 return;
2366
2367 if (this->mips_stubs_ == NULL)
2368 {
2369 // Create the .MIPS.stubs section first.
2370
2371 this->mips_stubs_ = new Mips_output_data_mips_stubs<size, big_endian>
2372 (layout, this->got_section(symtab, layout), this->gp_);
2373
2374 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
2375 (elfcpp::SHF_ALLOC
2376 | elfcpp::SHF_EXECINSTR),
2377 this->mips_stubs_, ORDER_PLT, false);
2378 }
2379 this->mips_stubs_->add_entry(gsym);
2380 }
2381
2382 template<int size, bool big_endian>
2383 Mips_output_data_stub<size, big_endian>::Mips_output_data_stub()
2384 : Output_section_data(size == 32 ? 4 : 8), count_(0)
2385 {}
2386
2387 template<int size, bool big_endian>
2388 void
2389 Mips_output_data_stub<size, big_endian>::do_adjust_output_section(
2390 Output_section* os)
2391 {
2392 os->set_entsize(0);
2393 }
2394
2395 // Add an entry to the STUB.
2396
2397 template<int size, bool big_endian>
2398 void
2399 Mips_output_data_stub<size, big_endian>::add_entry(Symbol_table* symtab,
2400 Target_mips<size, big_endian> * target,
2401 Symbol* gsym)
2402 {
2403 Symbol* sym;
2404 char* buffer = new char[strlen(gsym->name()) + 6];
2405 gold_assert(buffer != NULL);
2406
2407 section_offset_type stub_offset = this->count_ * 4 * 4;
2408
2409 ++this->count_;
2410
2411 sprintf(buffer, ".pic.%s", gsym->name());
2412
2413 sym = symtab->define_in_output_data(buffer, NULL,
2414 Symbol_table::PREDEFINED,
2415 target->stub(),
2416 stub_offset, 0x10, elfcpp::STT_FUNC,
2417 elfcpp::STB_GLOBAL,
2418 elfcpp::STV_DEFAULT, 0,
2419 false, false);
2420
2421 target->add_stub_symbol(gsym->name(), sym);
2422 this->add_symbol(gsym);
2423
2424 delete [] buffer;
2425 }
2426
2427
2428 // Create a STUB entry for a global symbol.
2429
2430 template<int size, bool big_endian>
2431 void
2432 Target_mips<size, big_endian>::make_stub_entry(Layout* layout)
2433 {
2434 if (this->stub_ == NULL)
2435 {
2436 this->stub_ = new Mips_output_data_stub<size, big_endian>();
2437 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2438 (elfcpp::SHF_ALLOC
2439 | elfcpp::SHF_EXECINSTR
2440 | elfcpp::SHF_WRITE),
2441 this->stub_, ORDER_TEXT, false);
2442 }
2443 }
2444
2445 template<int size, bool big_endian>
2446 const uint32_t Mips_output_data_stub<size, big_endian>::stub_entry[4] =
2447 {
2448 0x3c190000, // lui $25, %hi(func)
2449 0x08000000, // j func
2450 0x27390000, // add $25, $25, %lo(func)
2451 0x00000000 // nop
2452 };
2453
2454 // Write out the STUB. This uses the hand-coded instructions above,
2455 // and adjusts them as needed.
2456
2457 template<int size, bool big_endian>
2458 void
2459 Mips_output_data_stub<size, big_endian>::do_write(Output_file* of)
2460 {
2461 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
2462
2463 const off_t offset = this->offset();
2464 const section_size_type oview_size =
2465 convert_to_section_size_type(this->data_size());
2466 unsigned char* const oview = of->get_output_view(offset, oview_size);
2467
2468 unsigned char* pov = oview;
2469
2470 const unsigned int count = this->count_;
2471 for (unsigned int i = 0;
2472 i < count;
2473 ++i,
2474 pov += 4*4)
2475 {
2476 Sized_symbol<size> *sym = static_cast<Sized_symbol<size>*>
2477 (this->symbol(i));
2478
2479 unsigned int value = (unsigned int)sym->value();
2480 gold_assert(offset >= 0 && offset < 0x0fffffff);
2481 uint32_t stub_insn0 = stub_entry[0] | (((value + 0x8000) >> 16) & 0xffff);
2482 elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0);
2483 uint32_t stub_insn1 = stub_entry[1] | (((value >> 2) & 0x03ffffff));
2484 elfcpp::Swap<32, big_endian>::writeval(pov + 4, stub_insn1);
2485 uint32_t stub_insn2 = stub_entry[2] | ((value & 0x0000ffff));
2486 elfcpp::Swap<32, big_endian>::writeval(pov + 8, stub_insn2);
2487 uint32_t stub_insn3 = stub_entry[3];
2488 elfcpp::Swap<32, big_endian>::writeval(pov + 12, stub_insn3);
2489 }
2490 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2491
2492 of->write_output_view(offset, oview_size, oview);
2493 }
2494
2495 // Process the relocations to determine unreferenced sections for
2496 // garbage collection.
2497
2498 template<int size, bool big_endian>
2499 void
2500 Target_mips<size, big_endian>::gc_process_relocs(
2501 Symbol_table* symtab,
2502 Layout* layout,
2503 Sized_relobj_file<size, big_endian>* object,
2504 unsigned int data_shndx,
2505 unsigned int,
2506 const unsigned char* prelocs,
2507 size_t reloc_count,
2508 Output_section* output_section,
2509 bool needs_special_offset_handling,
2510 size_t local_symbol_count,
2511 const unsigned char* plocal_symbols)
2512 {
2513 typedef Target_mips<size, big_endian> Mips;
2514 typedef typename Target_mips<size, big_endian>::Scan Scan;
2515
2516 gold::gc_process_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan,
2517 typename Target_mips::Relocatable_size_for_reloc>(
2518 symtab,
2519 layout,
2520 this,
2521 object,
2522 data_shndx,
2523 prelocs,
2524 reloc_count,
2525 output_section,
2526 needs_special_offset_handling,
2527 local_symbol_count,
2528 plocal_symbols);
2529 }
2530
2531 // Scan the relocations to look for symbol adjustments.
2532
2533 template<int size, bool big_endian>
2534 void
2535 Target_mips<size, big_endian>::scan_relocs(
2536 Symbol_table* symtab,
2537 Layout* layout,
2538 Sized_relobj_file<size, big_endian>* object,
2539 unsigned int data_shndx,
2540 unsigned int sh_type,
2541 const unsigned char* prelocs,
2542 size_t reloc_count,
2543 Output_section* output_section,
2544 bool needs_special_offset_handling,
2545 size_t local_symbol_count,
2546 const unsigned char* plocal_symbols)
2547 {
2548 typedef Target_mips<size, big_endian> Mips;
2549 typedef typename Target_mips<size, big_endian>::Scan Scan;
2550
2551 if (sh_type == elfcpp::SHT_RELA)
2552 {
2553 gold_error(_("%s: unsupported RELA reloc section"),
2554 object->name().c_str());
2555 return;
2556 }
2557
2558 gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan>(
2559 symtab,
2560 layout,
2561 this,
2562 object,
2563 data_shndx,
2564 prelocs,
2565 reloc_count,
2566 output_section,
2567 needs_special_offset_handling,
2568 local_symbol_count,
2569 plocal_symbols);
2570 }
2571
2572 template<int size, bool big_endian>
2573 bool
2574 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
2575 {
2576 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
2577 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
2578 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
2579 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
2580 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
2581 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
2582 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
2583 }
2584
2585 template<int size, bool big_endian>
2586 bool
2587 Target_mips<size, big_endian>::mips_mach_extends(elfcpp::Elf_Word base,
2588 elfcpp::Elf_Word extension)
2589 {
2590 unsigned int i;
2591
2592 if(base == extension)
2593 return true;
2594
2595 if ((base == elfcpp::E_MIPS_ARCH_32)
2596 && mips_mach_extends(elfcpp::E_MIPS_ARCH_64, extension))
2597 return true;
2598
2599 if ((base == elfcpp::E_MIPS_ARCH_32R2)
2600 && mips_mach_extends(elfcpp::E_MIPS_ARCH_64R2, extension))
2601 return true;
2602
2603 for (i = 0; i < this->extensions_.size(); i++)
2604 if (extension == this->extensions_[i].second)
2605 {
2606 extension = this->extensions_[i].first;
2607 if (extension == base)
2608 return true;
2609 }
2610
2611 return false;
2612 }
2613
2614 template<int size, bool big_endian>
2615 void
2616 Target_mips<size, big_endian>::merge_processor_specific_flags(const char* name,
2617 elfcpp::Elf_Word in_flags)
2618 {
2619 elfcpp::Elf_Word out_flags = this->processor_specific_flags();
2620
2621 // If flags are not set yet, just copy them.
2622 if(out_flags == 0)
2623 this->set_processor_specific_flags(in_flags);
2624 else
2625 {
2626 elfcpp::Elf_Word new_flags = out_flags;
2627 new_flags |= in_flags & elfcpp::EF_MIPS_NOREORDER;
2628
2629 // Check flag compatibility.
2630 in_flags &= ~elfcpp::EF_MIPS_NOREORDER;
2631 out_flags &= ~elfcpp::EF_MIPS_NOREORDER;
2632
2633 // Some IRIX 6 BSD-compatibility objects have this bit set. It
2634 // doesn't seem to matter.
2635 in_flags &= ~elfcpp::EF_MIPS_XGOT;
2636 out_flags &= ~elfcpp::EF_MIPS_XGOT;
2637
2638 // MIPSpro generates ucode info in n64 objects. Again, we should
2639 // just be able to ignore this.
2640 in_flags &= ~elfcpp::EF_MIPS_UCODE;
2641 out_flags &= ~elfcpp::EF_MIPS_UCODE;
2642
2643 if((in_flags & elfcpp::EF_MIPS_DYNAMIC) != 0)
2644 in_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
2645
2646 // Input flags are same as output, do nothing.
2647 if(in_flags == out_flags)
2648 return;
2649
2650 // TODO AS: If input file has no sections, its flags are not important.
2651
2652 if (((in_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
2653 != ((out_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
2654 gold_warning(_("Linking abicalls files with non-abicalls files."));
2655
2656 if (in_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
2657 new_flags |= elfcpp::EF_MIPS_CPIC;
2658 if (!(in_flags & elfcpp::EF_MIPS_PIC))
2659 new_flags &= ~elfcpp::EF_MIPS_PIC;
2660
2661 in_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
2662 out_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
2663
2664 // Compare the ISAs.
2665 if (mips_32bit_flags(out_flags) != mips_32bit_flags(in_flags))
2666 {
2667 gold_error(_("Source object %s is %d-bit, but output is %d-bit"),
2668 name, mips_32bit_flags(in_flags) ? 32 : 64,
2669 mips_32bit_flags(out_flags) ? 32 : 64);
2670 }
2671 else if(!mips_mach_extends(in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIP S_MACH),
2672 out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)))
2673 {
2674 // Input ISA is not set. Just skip it.
2675 if((in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)) == 0);
2676 // ISA is not set yet, do it now.
2677 else if((out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)) == 0 )
2678 new_flags |= in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE);
2679 // OBFD's ISA isn't the same as, or an extension of, IBFD's.
2680 else if(mips_mach_extends(out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF _MIPS_MACH),
2681 in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)))
2682 {
2683 // Copy the architecture info from IBFD to OBFD. Also copy
2684 // the 32-bit flag (if set) so that we continue to recognise
2685 // OBFD as a 32-bit binary.
2686 new_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
2687 new_flags |=
2688 in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp ::EF_MIPS_32BITMODE);
2689
2690 // Copy across the ABI flags if OBFD doesn't use them
2691 // and if that was what caused us to treat IBFD as 32-bit.
2692 if((out_flags & elfcpp::EF_MIPS_ABI) == 0
2693 && mips_32bit_flags(in_flags)
2694 && !mips_32bit_flags(in_flags & ~elfcpp::EF_MIPS_ABI))
2695 new_flags |= in_flags & elfcpp::EF_MIPS_ABI;
2696 }
2697 else
2698 {
2699 // The ISAs aren't compatible.
2700 gold_error(_("%s ISA is 0x%x and it is not compatible with current"
2701 " ISA 0x%x"),
2702 name, in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIP S_MACH),
2703 out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MAC H));
2704 }
2705 }
2706
2707 in_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_ 32BITMODE);
2708 out_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS _32BITMODE);
2709
2710 // Compare ABIs. TODO AS: The 64-bit ABI does not use EF_MIPS_ABI.
2711 // But, it does set EI_CLASS differently from any 32-bit ABI.
2712 if ((in_flags & elfcpp::EF_MIPS_ABI) != (out_flags & elfcpp::EF_MIPS_ABI))
2713 {
2714 // Only error if both are set (to different values).
2715 if ((in_flags & elfcpp::EF_MIPS_ABI) && (out_flags & elfcpp::EF_MIPS_ABI ))
2716 {
2717 gold_error(_("%s ABI %d is different then current ABI %d"),
2718 name, in_flags & elfcpp::EF_MIPS_ABI,
2719 out_flags & elfcpp::EF_MIPS_ABI);
2720 }
2721
2722 in_flags &= ~elfcpp::EF_MIPS_ABI;
2723 out_flags &= ~elfcpp::EF_MIPS_ABI;
2724 }
2725
2726 // Compare ABIs.
2727 if ((in_flags & elfcpp::EF_MIPS_ABI2) != (out_flags & elfcpp::EF_MIPS_ABI2))
2728 {
2729 gold_error(_("%s is ABI%s while output is ABI%s"),
2730 name, ((in_flags & elfcpp::EF_MIPS_ABI2) ? "2" : ""),
2731 ((out_flags & elfcpp::EF_MIPS_ABI2) ? "2" : ""));
2732
2733 in_flags &= ~elfcpp::EF_MIPS_ABI2;
2734 out_flags &= ~elfcpp::EF_MIPS_ABI2;
2735 }
2736
2737 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
2738 // and allow arbitrary mixing of the remaining ASEs (retain the union).
2739 if ((in_flags & elfcpp::EF_MIPS_ARCH_ASE) != (out_flags & elfcpp::EF_MIPS_AR CH_ASE))
2740 {
2741 int old_micro = out_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
2742 int new_micro = in_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
2743 int old_m16 = out_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
2744 int new_m16 = in_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
2745 int micro_mis = old_m16 && new_micro;
2746 int m16_mis = old_micro && new_m16;
2747
2748 if (m16_mis || micro_mis)
2749 {
2750 gold_error(_("%s ASE is %s while output ASE is %s"),
2751 name, m16_mis ? "MIPS16" : "microMIPS",
2752 m16_mis ? "microMIPS" : "MIPS16");
2753 }
2754
2755 new_flags |= in_flags & elfcpp::EF_MIPS_ARCH_ASE;
2756
2757 in_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
2758 out_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
2759 }
2760
2761 // Warn about any other mismatches
2762 if (in_flags != out_flags)
2763 {
2764 gold_error(_("%s uses different flags 0x%x then output 0x%x"),
2765 name, in_flags, out_flags);
2766 }
2767
2768 this->set_processor_specific_flags(new_flags);
2769 }
2770 }
2771
2772 // Finalize the sections.
2773
2774 template <int size, bool big_endian>
2775 void
2776 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
2777 const Input_objects* input_objects,
2778 Symbol_table* symtab)
2779 {
2780 // Add NULL segment.
2781 if(!parameters->options().relocatable())
2782 layout->make_output_segment(elfcpp::PT_NULL, 0);
2783
2784 for (Layout::Section_list::const_iterator p = layout->section_list().begin();
2785 p != layout->section_list().end(); ++p)
2786 if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO)
2787 {
2788 Mips_output_section_reginfo<size>* reginfo =
2789 Mips_output_section_reginfo<size>::as_mips_output_section_reginfo(*p);
2790
2791 if(this->gp() == NULL)
2792 reginfo->set_gp(0);
2793 else
2794 reginfo->set_gp(this->gp());
2795
2796 if(!parameters->options().relocatable())
2797 {
2798 Output_segment *reginfo_segment =
2799 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO, elfcpp::P F_R);
2800 reginfo_segment->add_output_section_to_nonload(reginfo, elfcpp::PF _R);
2801 }
2802
2803 // There is only one .reginfo section
2804 break;
2805 }
2806
2807 if(this->got_section())
2808 {
2809 this->got_section()->add_global_relocs();
2810 this->got_section()->add_global_tls_relocs();
2811 }
2812
2813 // Merge processor-specific flags.
2814 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2815 p != input_objects->relobj_end();
2816 ++p)
2817 {
2818 Sized_relobj_file<size, big_endian>* relobj =
2819 static_cast<const Sized_relobj_file<size, big_endian>*>(*p);
2820
2821 Input_file::Format format = relobj->input_file()->format();
2822 if (format == Input_file::FORMAT_ELF)
2823 {
2824 // Read processor-specific flags in ELF file header.
2825 const unsigned char* pehdr = relobj->
2826 get_view(elfcpp::file_header_offset,
2827 elfcpp::Elf_sizes<size>::ehdr_size,
2828 true, false);
2829
2830 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
2831 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
2832
2833 this->merge_processor_specific_flags(relobj->name().c_str(), in_flags) ;
2834 }
2835 }
2836
2837 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
2838 p != input_objects->dynobj_end();
2839 ++p)
2840 {
2841 Sized_dynobj<size, big_endian>* dynobj =
2842 static_cast<const Sized_dynobj<size, big_endian>*>(*p);
2843
2844 // Read processor-specific flags.
2845 const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
2846 elfcpp::Elf_sizes<size>::ehdr_size,
2847 true, false);
2848
2849 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
2850 elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
2851
2852 this->merge_processor_specific_flags(dynobj->name().c_str(), in_flags);
2853 }
2854
2855 // Fill in some more dynamic tags.
2856 const Reloc_section* rel_plt = (this->plt_ == NULL
2857 ? NULL
2858 : this->plt_->rel_plt());
2859 layout->add_target_dynamic_tags(true, this->got_, rel_plt,
2860 this->rel_dyn_, true, false);
2861
2862 Output_data_dynamic* const odyn = layout->dynamic_data();
2863 if((odyn) &&
2864 (!parameters->options().relocatable() && !parameters->doing_static_link()))
2865 {
2866 // In case there is no .got section, create one.
2867 this->got_section(symtab, layout);
2868
2869 unsigned int d_val;
2870 // This element holds a 32-bit version id for the Runtime
2871 // Linker Interface. This will start at integer value 1.
2872 d_val = 0x01;
2873 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
2874
2875 // Dynamic flags
2876 d_val = elfcpp::RHF_NOTPOT;
2877 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
2878
2879 // This member holds the base address of the segment.
2880 odyn->add_target_specific(elfcpp::DT_MIPS_BASE_ADDRESS);
2881
2882 // This member holds the number of entries in the .dynsym section.
2883 odyn->add_target_specific(elfcpp::DT_MIPS_SYMTABNO);
2884
2885 // This member holds the index of the first dynamic symbol
2886 // table entry that corresponds to an entry in the global offset table.
2887 odyn->add_target_specific(elfcpp::DT_MIPS_GOTSYM);
2888
2889 // This member holds the number of local global offset table entries.
2890 odyn->add_target_specific(elfcpp::DT_MIPS_LOCAL_GOTNO);
2891
2892 if(this->plt_ != NULL)
2893 // DT_MIPS_PLTGOT dynamic tag
2894 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
2895 }
2896
2897 // Set the .text section.
2898 this->text_section_ = layout->find_output_section(".text");
2899 // Set the symbol table
2900 this->symtab_ = symtab;
2901 // Set the layout
2902 this->layout_ = layout;
2903
2904 // Emit any relocs we saved in an attempt to avoid generating COPY
2905 // relocs.
2906 if (this->copy_relocs_.any_saved_relocs())
2907 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2908
2909 Target::do_finalize_sections (layout, input_objects, symtab);
2910 }
2911
2912 // Functor class for processing the global symbol table. Processes the
2913 // index in dynamic section if the symbol has GOT entry, and finds
2914 // minimum index value.
2915
2916 template<int size, bool big_endian>
2917 class Symbol_visitor_gotsym
2918 {
2919 public:
2920 Symbol_visitor_gotsym()
2921 { }
2922
2923 void
2924 operator()(const Sized_symbol<size>* sym)
2925 {
2926 if (sym->has_got_offset(0) && sym->needs_dynsym_entry() && sym->dynsym_index () < this->min_index_)
2927 this->min_index_ = sym->dynsym_index();
2928 }
2929
2930 unsigned int min_dynsym_index()
2931 {
2932 return this->min_index_;
2933 }
2934 private:
2935 static unsigned int min_index_;
2936 };
2937
2938 template<int size, bool big_endian>
2939 unsigned int Symbol_visitor_gotsym<size, big_endian>::min_index_ = -1U;
2940
2941 template<int size, bool big_endian>
2942 void
2943 Target_mips<size, big_endian>::do_fix_sections(Layout*, Symbol_table* )
2944 {
2945 Output_section *dynsym = this->layout_->find_output_section(".dynsym");
2946
2947 if((dynsym != NULL) && (this->mips_stubs_ != NULL))
2948 {
2949 this->mips_stubs_->set_big_dyn_symtab(((unsigned int)dynsym->data_size()/1 6) > 0x10000);
2950
2951 this->mips_stubs_->fix_symbol_values();
2952 }
2953 }
2954
2955 // MIPS specific dynamic tags.
2956 template<int size, bool big_endian>
2957 unsigned int
2958 Target_mips<size, big_endian>::do_dynamic_tag_value(elfcpp::DT tag) const
2959 {
2960 Symbol_table *symtab = this->symtab_;
2961 Layout *layout = this->layout_;
2962
2963 Output_section *dynsym = layout->find_output_section(".dynsym");
2964 gold_assert(dynsym != NULL);
2965
2966 switch(tag)
2967 {
2968 case elfcpp::DT_MIPS_BASE_ADDRESS:
2969 {
2970 typedef std::vector<Output_segment*> Segment_list;
2971 const Segment_list segment_list_ = layout->segment_list();
2972 unsigned int vaddr = -1U;
2973 for(Segment_list::const_iterator p = segment_list_.begin();
2974 p != segment_list_.end();
2975 ++p)
2976 {
2977 if(((*p)->flags() & elfcpp::PF_R) != 0)
2978 {
2979 if((*p)->vaddr() < vaddr)
2980 vaddr = (*p)->vaddr();
2981 }
2982 }
2983 return vaddr;
2984 }
2985 case elfcpp::DT_MIPS_SYMTABNO:
2986 return (unsigned int)dynsym->data_size()/16;
2987 case elfcpp::DT_MIPS_GOTSYM:
2988 {
2989 unsigned int min_index = symtab->global_got_index();
2990
2991 if(min_index == -1U)
2992 min_index = (unsigned int)dynsym->data_size()/16;
2993
2994 return min_index;
2995 }
2996 case elfcpp::DT_MIPS_LOCAL_GOTNO:
2997
2998 return(unsigned int)this->got_->data_size()/4 -
2999 ((unsigned int)dynsym->data_size()/16 - symtab->global_got_index()) -
3000 this->got_->tls_entries();
3001 default:
3002 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
3003 }
3004
3005 return (unsigned int)-1;
3006 }
3007
3008 // Relocate a section.
3009
3010 template<int size, bool big_endian>
3011 void
3012 Target_mips<size, big_endian>::relocate_section(
3013 const Relocate_info<size, big_endian>* relinfo,
3014 unsigned int sh_type,
3015 const unsigned char* prelocs,
3016 size_t reloc_count,
3017 Output_section* output_section,
3018 bool needs_special_offset_handling,
3019 unsigned char* view,
3020 typename elfcpp::Elf_types<size>::Elf_Addr address,
3021 section_size_type view_size,
3022 const Reloc_symbol_changes* reloc_symbol_changes)
3023 {
3024 typedef Target_mips<size, big_endian> Mips;
3025 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
3026
3027 gold_assert(sh_type == elfcpp::SHT_REL);
3028
3029 gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_REL,
3030 Mips_relocate>(
3031 relinfo,
3032 this,
3033 prelocs,
3034 reloc_count,
3035 output_section,
3036 needs_special_offset_handling,
3037 view,
3038 address,
3039 view_size,
3040 reloc_symbol_changes);
3041 }
3042
3043 // Return the size of a relocation while scanning during a relocatable
3044 // link.
3045
3046 template<int size, bool big_endian>
3047 unsigned int
3048 Target_mips<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3049 unsigned int r_type,
3050 Relobj* object)
3051 {
3052 switch (r_type)
3053 {
3054 case elfcpp::R_MIPS_NONE:
3055 case elfcpp::R_MIPS_UNUSED1:
3056 case elfcpp::R_MIPS_UNUSED2:
3057 case elfcpp::R_MIPS_UNUSED3:
3058 case elfcpp::R_MIPS_INSERT_A:
3059 case elfcpp::R_MIPS_INSERT_B:
3060 case elfcpp::R_MIPS_DELETE:
3061 case elfcpp::R_MIPS_TLS_DTPMOD64:
3062 case elfcpp::R_MIPS_TLS_DTPREL64:
3063 case elfcpp::R_MIPS_REL16:
3064 case elfcpp::R_MIPS_ADD_IMMEDIATE:
3065 case elfcpp::R_MIPS_PJUMP:
3066 case elfcpp::R_MIPS_RELGOT:
3067 case elfcpp::R_MIPS_TLS_TPREL64:
3068 case elfcpp::R_MIPS_GNU_VTINHERIT:
3069 case elfcpp::R_MIPS_GNU_VTENTRY:
3070 return 0;
3071
3072 case elfcpp::R_MIPS_64:
3073 case elfcpp::R_MIPS_SUB:
3074 return 8;
3075
3076 case elfcpp::R_MIPS_32:
3077 case elfcpp::R_MIPS_TLS_DTPMOD32:
3078 case elfcpp::R_MIPS_TLS_DTPREL32:
3079 case elfcpp::R_MIPS_TLS_TPREL32:
3080 case elfcpp::R_MIPS_REL32:
3081 case elfcpp::R_MIPS_PC32:
3082 case elfcpp::R_MIPS_GPREL32:
3083 case elfcpp::R_MIPS_JALR:
3084 case elfcpp::R_MIPS_SCN_DISP:
3085 return 4;
3086
3087 case elfcpp::R_MIPS_16:
3088 case elfcpp::R_MIPS_HI16:
3089 case elfcpp::R_MIPS_LO16:
3090 case elfcpp::R_MIPS_HIGHER:
3091 case elfcpp::R_MIPS_HIGHEST:
3092 case elfcpp::R_MIPS_GPREL16:
3093 case elfcpp::R_MIPS16_HI16:
3094 case elfcpp::R_MIPS16_LO16:
3095 case elfcpp::R_MIPS_PC16:
3096 case elfcpp::R_MIPS_GNU_REL16_S2:
3097 case elfcpp::R_MIPS_GOT16:
3098 case elfcpp::R_MIPS16_GOT16:
3099 case elfcpp::R_MIPS_CALL16:
3100 case elfcpp::R_MIPS16_CALL16:
3101 case elfcpp::R_MIPS_GOT_HI16:
3102 case elfcpp::R_MIPS_CALL_HI16:
3103 case elfcpp::R_MIPS_GOT_LO16:
3104 case elfcpp::R_MIPS_CALL_LO16:
3105 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
3106 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
3107 case elfcpp::R_MIPS_TLS_TPREL_HI16:
3108 case elfcpp::R_MIPS_TLS_TPREL_LO16:
3109 case elfcpp::R_MIPS16_GPREL:
3110 case elfcpp::R_MIPS_GOT_DISP:
3111 case elfcpp::R_MIPS_LITERAL:
3112 case elfcpp::R_MIPS_GOT_PAGE:
3113 case elfcpp::R_MIPS_GOT_OFST:
3114 case elfcpp::R_MIPS_TLS_GD:
3115 case elfcpp::R_MIPS_TLS_LDM:
3116 case elfcpp::R_MIPS_TLS_GOTTPREL:
3117 return 2;
3118
3119 // These relocations are not byte sized
3120 case elfcpp::R_MIPS_26:
3121 case elfcpp::R_MIPS16_26:
3122 return 4;
3123
3124 // These relocations are not byte sized
3125 case elfcpp::R_MIPS_SHIFT5:
3126 case elfcpp::R_MIPS_SHIFT6:
3127 return 1;
3128
3129 case elfcpp::R_MIPS_COPY:
3130 case elfcpp::R_MIPS_GLOB_DAT:
3131 case elfcpp::R_MIPS_JUMP_SLOT:
3132 object->error(_("unexpected reloc %u in object file"), r_type);
3133 return 0;
3134
3135 default:
3136 object->error(_("unsupported reloc %u in object file"), r_type);
3137 return 0;
3138 }
3139 }
3140
3141 // Scan the relocs during a relocatable link.
3142
3143 template<int size, bool big_endian>
3144 void
3145 Target_mips<size, big_endian>::scan_relocatable_relocs(
3146 Symbol_table* symtab,
3147 Layout* layout,
3148 Sized_relobj_file<size, big_endian>* object,
3149 unsigned int data_shndx,
3150 unsigned int sh_type,
3151 const unsigned char* prelocs,
3152 size_t reloc_count,
3153 Output_section* output_section,
3154 bool needs_special_offset_handling,
3155 size_t local_symbol_count,
3156 const unsigned char* plocal_symbols,
3157 Relocatable_relocs* rr)
3158 {
3159 gold_assert(sh_type == elfcpp::SHT_REL);
3160
3161 typedef Mips_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
3162 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3163
3164 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_REL,
3165 Scan_relocatable_relocs>(
3166 symtab,
3167 layout,
3168 object,
3169 data_shndx,
3170 prelocs,
3171 reloc_count,
3172 output_section,
3173 needs_special_offset_handling,
3174 local_symbol_count,
3175 plocal_symbols,
3176 rr);
3177 }
3178
3179 // Relocate a section during a relocatable link.
3180
3181 template<int size, bool big_endian>
3182 void
3183 Target_mips<size, big_endian>::relocate_for_relocatable(
3184 const Relocate_info<size, big_endian>* relinfo,
3185 unsigned int sh_type,
3186 const unsigned char* prelocs,
3187 size_t reloc_count,
3188 Output_section* output_section,
3189 off_t offset_in_output_section,
3190 const Relocatable_relocs* rr,
3191 unsigned char* view,
3192 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3193 section_size_type view_size,
3194 unsigned char* reloc_view,
3195 section_size_type reloc_view_size)
3196 {
3197 gold_assert(sh_type == elfcpp::SHT_REL);
3198
3199 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_REL>(
3200 relinfo,
3201 prelocs,
3202 reloc_count,
3203 output_section,
3204 offset_in_output_section,
3205 rr,
3206 view,
3207 view_address,
3208 view_size,
3209 reloc_view,
3210 reloc_view_size);
3211 }
3212
3213 // Perform target-specific processing in a relocatable link. This is
3214 // only used if we use the relocation strategy RELOC_SPECIAL.
3215
3216 template<int size, bool big_endian>
3217 void
3218 Target_mips<size, big_endian>::relocate_special_relocatable(
3219 const Relocate_info<size, big_endian>* relinfo,
3220 unsigned int sh_type,
3221 const unsigned char* preloc_in,
3222 size_t relnum,
3223 Output_section* output_section,
3224 off_t offset_in_output_section,
3225 unsigned char* view,
3226 elfcpp::Elf_types<32>::Elf_Addr view_address,
3227 section_size_type,
3228 unsigned char* preloc_out)
3229 {
3230 // We can only handle REL type relocation sections.
3231 gold_assert(sh_type == elfcpp::SHT_REL);
3232
3233 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc Reltype ;
3234 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
3235 Reltype_write;
3236 typedef typename elfcpp::Elf_types<32>::Elf_Addr Mips_address;
3237 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
3238
3239 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
3240
3241 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
3242 const unsigned int local_count = object->local_symbol_count();
3243
3244 Reltype reloc(preloc_in);
3245 Reltype_write reloc_write(preloc_out);
3246
3247 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
3248 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3249 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3250
3251 // Get the new symbol index.
3252 // We only use RELOC_SPECIAL strategy in local relocations.
3253 gold_assert(r_sym < local_count);
3254
3255 // We are adjusting a section symbol. We need to find
3256 // the symbol table index of the section symbol for
3257 // the output section corresponding to input section
3258 // in which this symbol is defined.
3259 bool is_ordinary;
3260 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
3261 gold_assert(is_ordinary);
3262 Output_section* os = object->output_section(shndx);
3263 gold_assert(os != NULL);
3264 gold_assert(os->needs_symtab_index());
3265 unsigned int new_symndx = os->symtab_index();
3266
3267 // Get the new offset--the location in the output section where
3268 // this relocation should be applied.
3269
3270 Mips_address offset = reloc.get_r_offset();
3271 Mips_address new_offset;
3272 if (offset_in_output_section != invalid_address)
3273 new_offset = offset + offset_in_output_section;
3274 else
3275 {
3276 section_offset_type sot_offset =
3277 convert_types<section_offset_type, Mips_address>(offset);
3278 section_offset_type new_sot_offset =
3279 output_section->output_offset(object, relinfo->data_shndx,
3280 sot_offset);
3281 gold_assert(new_sot_offset != -1);
3282 new_offset = new_sot_offset;
3283 }
3284
3285 // In an object file, r_offset is an offset within the section.
3286 // In an executable or dynamic object, generated by
3287 // --emit-relocs, r_offset is an absolute address.
3288 if (!parameters->options().relocatable())
3289 {
3290 new_offset += view_address;
3291 if (offset_in_output_section != invalid_address)
3292 new_offset -= offset_in_output_section;
3293 }
3294
3295 reloc_write.put_r_offset(new_offset);
3296 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
3297
3298 // Handle the reloc addend.
3299 // The relocation uses a section symbol in the input file.
3300 // We are adjusting it to use a section symbol in the output
3301 // file. The input section symbol refers to some address in
3302 // the input section. We need the relocation in the output
3303 // file to refer to that same address. This adjustment to
3304 // the addend is the same calculation we use for a simple
3305 // absolute relocation for the input section symbol.
3306
3307 unsigned char* paddend = view + offset;
3308 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
3309 switch (r_type)
3310 {
3311 case elfcpp::R_MIPS_26:
3312 reloc_status = Reloc_funcs::rel26(paddend, object, object->local_symbol(r_ sym),
3313 offset_in_output_section, false);
3314
3315 break;
3316
3317 default:
3318 gold_unreachable();
3319 }
3320
3321 // Report any errors.
3322 switch (reloc_status)
3323 {
3324 case Reloc_funcs::STATUS_OKAY:
3325 break;
3326 case Reloc_funcs::STATUS_OVERFLOW:
3327 gold_error_at_location(relinfo, relnum, new_offset,
3328 _("relocation overflow"));
3329 break;
3330 case Reloc_funcs::STATUS_BAD_RELOC:
3331 gold_error_at_location(
3332 relinfo,
3333 relnum,
3334 new_offset ,
3335 _("unexpected opcode while processing relocation"));
3336 break;
3337 default:
3338 gold_unreachable();
3339 }
3340 }
3341
3342 // Optimize the TLS relocation type based on what we know about the
3343 // symbol. IS_FINAL is true if the final address of this symbol is
3344 // known at link time.
3345
3346 template<int size, bool big_endian>
3347 tls::Tls_optimization
3348 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
3349 {
3350 // FIXME: Currently we do not do any TLS optimization.
3351 return tls::TLSOPT_NONE;
3352 }
3353
3354 // We are about to emit a dynamic relocation of type R_TYPE. If the
3355 // dynamic linker does not support it, issue an error.
3356
3357 template<int size, bool big_endian>
3358 void
3359 Target_mips<size, big_endian>::Scan::check_non_pic(Relobj* object,
3360 unsigned int r_type)
3361 {
3362 switch (r_type)
3363 {
3364 // These are the relocation types supported by glibc for MIPS.
3365 case elfcpp::R_MIPS_TLS_DTPMOD32:
3366 case elfcpp::R_MIPS_TLS_DTPREL32:
3367 case elfcpp::R_MIPS_TLS_TPREL32:
3368 case elfcpp::R_MIPS_REL32:
3369 case elfcpp::R_MIPS_GLOB_DAT:
3370 case elfcpp::R_MIPS_64:
3371 return;
3372
3373 default:
3374 {
3375 // This prevents us from issuing more than one error per reloc
3376 // section. But we can still wind up issuing more than one
3377 // error per object file.
3378 if (this->issued_non_pic_error_)
3379 return;
3380 gold_assert(parameters->options().output_is_position_independent());
3381 object->error(_("requires unsupported dynamic reloc; "
3382 "recompile with -fPIC"));
3383 this->issued_non_pic_error_ = true;
3384 return;
3385 }
3386
3387 case elfcpp::R_MIPS_NONE:
3388 gold_unreachable();
3389 }
3390 }
3391
3392 // Scan a relocation for a local symbol.
3393
3394 template<int size, bool big_endian>
3395 inline void
3396 Target_mips<size, big_endian>::Scan::local(
3397 Symbol_table* symtab,
3398 Layout* layout,
3399 Target_mips<size, big_endian>* target,
3400 Sized_relobj_file<size, big_endian>* object,
3401 unsigned int data_shndx,
3402 Output_section* output_section,
3403 const elfcpp::Rel<size, big_endian>& reloc,
3404 unsigned int r_type,
3405 const elfcpp::Sym<size, big_endian>& lsym)
3406 {
3407 switch (r_type)
3408 {
3409 // Global offset table relocations
3410 case elfcpp::R_MIPS_CALL16:
3411 case elfcpp::R_MIPS16_CALL16:
3412 case elfcpp::R_MIPS_GOT16:
3413 case elfcpp::R_MIPS16_GOT16:
3414 {
3415 Mips_output_data_got<size, big_endian>* got =
3416 target->got_section(symtab, layout);
3417
3418 std::stringstream tmp;
3419 tmp << static_cast<unsigned int> (reloc.get_r_info())
3420 << static_cast<unsigned int> (reinterpret_cast<unsigned long long> (ob ject))
3421 << static_cast<unsigned int> (reloc.get_r_offset());
3422 std::string key = tmp.str();
3423
3424 got->add_got16_view(key, got);
3425 }
3426 break;
3427 case elfcpp::R_MIPS_GOT_DISP:
3428 case elfcpp::R_MIPS_GOT_HI16:
3429 case elfcpp::R_MIPS_CALL_HI16:
3430 case elfcpp::R_MIPS_GOT_LO16:
3431 case elfcpp::R_MIPS_CALL_LO16:
3432 {
3433 // The symbol requires a GOT entry.
3434 Mips_output_data_got<size, big_endian>* got =
3435 target->got_section(symtab, layout);
3436
3437 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3438
3439 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3440 {
3441 // If we are generating a shared object, we need to add a
3442 // dynamic RELATIVE relocation for this symbol's GOT entry.
3443 /* if (parameters->options().output_is_position_independent())
3444 {
3445 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3446 unsigned int r_sym = elfcpp::elf_r_sym<size>(
3447 reloc.get_r_info());
3448 rel_dyn->add_local_relative(
3449 object, r_sym, elfcpp::R_MIPS_GOT16, got,
3450 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3451 }*/
3452 }
3453 }
3454 break;
3455 case elfcpp::R_MIPS_GPREL32:
3456 case elfcpp::R_MIPS_GPREL16:
3457 case elfcpp::R_MIPS16_GPREL:
3458 case elfcpp::R_MIPS_LITERAL:
3459 break;
3460 case elfcpp::R_MIPS_NONE:
3461 case elfcpp::R_MIPS_16:
3462 break;
3463 case elfcpp::R_MIPS_32:
3464 {
3465 if (parameters->options().output_is_position_independent())
3466 {
3467 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3468 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3469
3470 rel_dyn->add_symbolless_local_addend(object, r_sym, elfcpp::R_MIPS_R EL32,
3471 output_section, data_shndx,
3472 reloc.get_r_offset());
3473 }
3474 }
3475 case elfcpp::R_MIPS_REL32:
3476 case elfcpp::R_MIPS_26:
3477 case elfcpp::R_MIPS_HI16:
3478 case elfcpp::R_MIPS_LO16:
3479 case elfcpp::R_MIPS_PC16:
3480 case elfcpp::R_MIPS_UNUSED1:
3481 case elfcpp::R_MIPS_UNUSED2:
3482 case elfcpp::R_MIPS_UNUSED3:
3483 case elfcpp::R_MIPS_SHIFT5:
3484 case elfcpp::R_MIPS_SHIFT6:
3485 case elfcpp::R_MIPS_64:
3486 case elfcpp::R_MIPS_GOT_PAGE:
3487 case elfcpp::R_MIPS_GOT_OFST:
3488 case elfcpp::R_MIPS_SUB:
3489 case elfcpp::R_MIPS_INSERT_A:
3490 case elfcpp::R_MIPS_INSERT_B:
3491 case elfcpp::R_MIPS_DELETE:
3492 case elfcpp::R_MIPS_HIGHER:
3493 case elfcpp::R_MIPS_HIGHEST:
3494 case elfcpp::R_MIPS_SCN_DISP:
3495 case elfcpp::R_MIPS_REL16:
3496 case elfcpp::R_MIPS_ADD_IMMEDIATE:
3497 case elfcpp::R_MIPS_PJUMP:
3498 case elfcpp::R_MIPS_RELGOT:
3499 case elfcpp::R_MIPS_JALR:
3500 case elfcpp::R_MIPS_GLOB_DAT:
3501 case elfcpp::R_MIPS16_26:
3502 case elfcpp::R_MIPS16_HI16:
3503 case elfcpp::R_MIPS16_LO16:
3504 case elfcpp::R_MIPS_COPY:
3505 case elfcpp::R_MIPS_JUMP_SLOT:
3506 case elfcpp::R_MIPS_PC32:
3507 case elfcpp::R_MIPS_GNU_REL16_S2:
3508 case elfcpp::R_MIPS_GNU_VTINHERIT:
3509 case elfcpp::R_MIPS_GNU_VTENTRY:
3510 break;
3511
3512 case elfcpp::R_MIPS_TLS_DTPMOD32:
3513 case elfcpp::R_MIPS_TLS_DTPREL32:
3514 case elfcpp::R_MIPS_TLS_DTPMOD64:
3515 case elfcpp::R_MIPS_TLS_DTPREL64:
3516 case elfcpp::R_MIPS_TLS_GD:
3517 case elfcpp::R_MIPS_TLS_LDM:
3518 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
3519 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
3520 case elfcpp::R_MIPS_TLS_GOTTPREL:
3521 case elfcpp::R_MIPS_TLS_TPREL32:
3522 case elfcpp::R_MIPS_TLS_TPREL64:
3523 case elfcpp::R_MIPS_TLS_TPREL_HI16:
3524 case elfcpp::R_MIPS_TLS_TPREL_LO16:
3525 {
3526 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3527 if(object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
3528 break;
3529
3530 bool output_is_shared = parameters->options().shared();
3531 const tls::Tls_optimization optimized_type
3532 = Target_mips<size, big_endian>::optimize_tls_reloc(
3533 !output_is_shared, r_type);
3534 switch (r_type)
3535 {
3536 case elfcpp::R_MIPS_TLS_GD:
3537 if (optimized_type == tls::TLSOPT_NONE)
3538 {
3539 // Create a pair of GOT entries for the module index and
3540 // dtv-relative offset.
3541 Mips_output_data_got<size, big_endian>* got
3542 = target->got_section(symtab, layout);
3543 unsigned int shndx = lsym.get_st_shndx();
3544 bool is_ordinary;
3545 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
3546 if (!is_ordinary)
3547 {
3548 object->error(_("local symbol %u has bad shndx %u"),
3549 r_sym, shndx);
3550 break;
3551 }
3552
3553 got->add_tls_gd_local_reloc(GOT_TYPE_TLS_PAIR, object, r_sym);
3554 }
3555 else
3556 // FIXME: TLS optimization not supported yet.
3557 gold_unreachable();
3558 break;
3559
3560 case elfcpp::R_MIPS_TLS_LDM:
3561 if (optimized_type == tls::TLSOPT_NONE)
3562 {
3563 // Create a GOT entry for the module index.
3564 target->got_mod_index_entry(symtab, layout, object);
3565 }
3566 else
3567 // FIXME: TLS optimization not supported yet.
3568 gold_unreachable();
3569 break;
3570 case elfcpp::R_MIPS_TLS_GOTTPREL:
3571 layout->set_has_static_tls();
3572 if (optimized_type == tls::TLSOPT_NONE)
3573 {
3574 // Create a GOT entry for the tp-relative offset.
3575 Mips_output_data_got<size, big_endian>* got
3576 = target->got_section(symtab, layout);
3577 if (!parameters->doing_static_link())
3578 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
3579 target->rel_dyn_section(layout),
3580 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
3581 elfcpp::R_MIPS_TLS_TPREL64);
3582 else if (!object->local_has_got_offset(r_sym,
3583 GOT_TYPE_TLS_OFFSET))
3584 {
3585 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
3586
3587 unsigned int got_offset =
3588 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
3589 got->add_static_reloc(got_offset,
3590 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
3591 elfcpp::R_MIPS_TLS_TPREL64, object,
3592 r_sym);
3593 }
3594 }
3595 else
3596 // FIXME: TLS optimization not supported yet.
3597 gold_unreachable();
3598 break;
3599
3600 default: // TODO AS: Check if this is correct at all
3601 layout->set_has_static_tls();
3602 if (output_is_shared)
3603 {
3604 // We need to create a dynamic relocation.
3605 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3606 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3607 rel_dyn->add_local(object, r_sym, size == 32 ?
3608 elfcpp::R_MIPS_TLS_TPREL32 :
3609 elfcpp::R_MIPS_TLS_TPREL64,
3610 output_section, data_shndx,
3611 reloc.get_r_offset());
3612 }
3613 break;
3614 }
3615 }
3616 break;
3617
3618 default:
3619 unsupported_reloc_local(object, r_type);
3620 break;
3621 }
3622 }
3623
3624 template<int size, bool big_endian>
3625 void
3626 Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
3627 {
3628 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
3629
3630 // Call parent to write out GOT.
3631 Output_data_got<size, big_endian>::do_write(of);
3632
3633 // Collect views for later fixing values of .got section
3634 if(!this->got16_views_.empty())
3635 {
3636 const off_t offset = this->offset();
3637 const section_size_type oview_size =
3638 convert_to_section_size_type(this->data_size());
3639 unsigned char* const oview = of->get_output_view(offset, oview_size);
3640
3641 this->got_view_ = oview;
3642 }
3643
3644 // We are done if there is no fix up.
3645 if (this->static_relocs_.empty())
3646 return;
3647
3648 const off_t offset = this->offset();
3649 const section_size_type oview_size =
3650 convert_to_section_size_type(this->data_size());
3651 unsigned char* const oview = of->get_output_view(offset, oview_size);
3652
3653 Output_segment* tls_segment = this->layout_->tls_segment();
3654 gold_assert(tls_segment != NULL);
3655
3656 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
3657 {
3658 Static_reloc& reloc(this->static_relocs_[i]);
3659
3660 Mips_address value;
3661 if (!reloc.symbol_is_global())
3662 {
3663 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
3664 const Symbol_value<size>* psymval =
3665 reloc.relobj()->local_symbol(reloc.index());
3666
3667 // We are doing static linking. Issue an error and skip this
3668 // relocation if the symbol is undefined or in a discarded_section.
3669 bool is_ordinary;
3670 unsigned int shndx = psymval->input_shndx(&is_ordinary);
3671 if ((shndx == elfcpp::SHN_UNDEF)
3672 || (is_ordinary
3673 && shndx != elfcpp::SHN_UNDEF
3674 && !object->is_section_included(shndx)
3675 && !this->symbol_table_->is_section_folded(object, shndx)))
3676 {
3677 gold_error(_("undefined or discarded local symbol %u from "
3678 " object %s in GOT"),
3679 reloc.index(), reloc.relobj()->name().c_str());
3680 continue;
3681 }
3682
3683 value = psymval->value(object, 0);
3684 }
3685 else
3686 {
3687 const Symbol* gsym = reloc.symbol();
3688 gold_assert(gsym != NULL);
3689 if (gsym->is_forwarder())
3690 gsym = this->symbol_table_->resolve_forwards(gsym);
3691
3692 // We are doing static linking. Issue an error and skip this
3693 // relocation if the symbol is undefined or in a discarded_section
3694 // unless it is a weakly_undefined symbol.
3695 if ((gsym->is_defined_in_discarded_section()
3696 || gsym->is_undefined())
3697 && !gsym->is_weak_undefined())
3698 {
3699 gold_error(_("undefined or discarded symbol %s in GOT"),
3700 gsym->name());
3701 continue;
3702 }
3703
3704 if (!gsym->is_weak_undefined())
3705 {
3706 const Sized_symbol<size>* sym =
3707 static_cast<const Sized_symbol<size>*>(gsym);
3708 value = sym->value();
3709 }
3710 else
3711 value = 0;
3712 }
3713
3714 unsigned got_offset = reloc.got_offset();
3715 gold_assert(got_offset < oview_size);
3716
3717 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
3718 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
3719 Valtype x;
3720
3721 switch (reloc.r_type())
3722 {
3723 case elfcpp::R_MIPS_TLS_DTPMOD32:
3724 x = value;
3725 break;
3726 case elfcpp::R_MIPS_TLS_TPREL32:
3727 x = value - elfcpp::TP_OFFSET;
3728 break;
3729 case elfcpp::R_MIPS_TLS_DTPREL32:
3730 x = value - elfcpp::DTP_OFFSET;
3731 break;
3732 default:
3733 gold_unreachable();
3734 break;
3735 }
3736
3737 elfcpp::Swap<size, big_endian>::writeval(wv, x);
3738 }
3739
3740 of->write_output_view(offset, oview_size, oview);
3741 }
3742
3743 // Scan a relocation for a global symbol.
3744
3745 template<int size, bool big_endian>
3746 inline void
3747 Target_mips<size, big_endian>::Scan::global(
3748 Symbol_table* symtab,
3749 Layout* layout,
3750 Target_mips<size, big_endian>* target,
3751 Sized_relobj_file<size, big_endian>* object,
3752 unsigned int data_shndx,
3753 Output_section* output_section,
3754 const elfcpp::Rel<size, big_endian>& reloc,
3755 unsigned int r_type,
3756 Symbol* gsym)
3757 {
3758 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
3759 // section. We check here to avoid creating a dynamic reloc against
3760 // _GLOBAL_OFFSET_TABLE_.
3761 if (!target->has_got_section()
3762 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
3763 target->got_section(symtab, layout);
3764
3765 // TODO AS: Check where to put this code!!!!!
3766 if(!parameters->options().shared())
3767 if((this->symbol_needs_plt_entry(gsym)) && (!gsym->has_plt_offset()))
3768 {
3769 if (gsym->object() != object)
3770 {
3771 if(parameters->options().output_is_position_independent())
3772 {
3773 gold_assert(!gsym->has_dynsym_index());
3774 target->make_mips_stubs_entry(symtab, layout, gsym);
3775 }
3776 else if(gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)) )
3777 {
3778 target->make_plt_entry(symtab, layout, gsym);
3779 }
3780 }
3781
3782 // Since this is not a PC-relative relocation, we may be
3783 // taking the address of a function. In that case we need to
3784 // set the entry in the dynamic symbol table to the address of
3785 // the PLT entry.
3786 if(((gsym->is_from_dynobj() && !parameters->options().shared()) ||
3787 ((this->get_reference_flags(r_type) == Symbol::ABSOLUTE_REF)
3788 && !(parameters->options().output_is_position_independent())))
3789 && (gsym->has_plt_offset()))
3790 {
3791 gsym->set_needs_dynsym_value();
3792 gsym->set_nonvis(elfcpp::STO_MIPS_PLT>>2);
3793 }
3794 }
3795
3796 if((gsym->has_plt_offset())
3797 && (parameters->options().output_is_position_independent()))
3798 switch(r_type)
3799 {
3800 case elfcpp::R_MIPS_HI16:
3801 case elfcpp::R_MIPS_LO16:
3802 case elfcpp::R_MIPS_CALL16:
3803 case elfcpp::R_MIPS_JALR:
3804 break;
3805 default:
3806 target->remove_stub_entry(gsym);
3807 break;
3808 }
3809
3810 switch (r_type)
3811 {
3812 case elfcpp::R_MIPS_NONE:
3813 case elfcpp::R_MIPS_UNUSED1:
3814 case elfcpp::R_MIPS_UNUSED2:
3815 case elfcpp::R_MIPS_UNUSED3:
3816 case elfcpp::R_MIPS_INSERT_A:
3817 case elfcpp::R_MIPS_INSERT_B:
3818 case elfcpp::R_MIPS_DELETE:
3819 case elfcpp::R_MIPS_TLS_DTPMOD64:
3820 case elfcpp::R_MIPS_TLS_DTPREL64:
3821 case elfcpp::R_MIPS_REL16:
3822 case elfcpp::R_MIPS_ADD_IMMEDIATE:
3823 case elfcpp::R_MIPS_PJUMP:
3824 case elfcpp::R_MIPS_RELGOT:
3825 case elfcpp::R_MIPS_TLS_TPREL64:
3826 case elfcpp::R_MIPS_GNU_VTINHERIT:
3827 case elfcpp::R_MIPS_GNU_VTENTRY:
3828 break;
3829 case elfcpp::R_MIPS_GOT16:
3830 case elfcpp::R_MIPS16_GOT16:
3831 case elfcpp::R_MIPS_CALL16:
3832 case elfcpp::R_MIPS16_CALL16:
3833 case elfcpp::R_MIPS_GOT_DISP:
3834 case elfcpp::R_MIPS_GOT_HI16:
3835 case elfcpp::R_MIPS_CALL_HI16:
3836 case elfcpp::R_MIPS_GOT_LO16:
3837 case elfcpp::R_MIPS_CALL_LO16:
3838 case elfcpp::R_MIPS_GOT_PAGE:
3839 case elfcpp::R_MIPS_GOT_OFST:
3840 {
3841 // The symbol requires a GOT entry.
3842 Mips_output_data_got<size, big_endian>* got =
3843 target->got_section(symtab, layout);
3844 if(!gsym->final_value_is_known()
3845 || (parameters->options().output_is_position_independent()))
3846 got->add_global_with_reloc_mips(gsym);
3847 else
3848 {
3849 got->add_global(gsym, GOT_TYPE_STANDARD);
3850 gsym->set_needs_dynsym_entry();
3851 }
3852
3853 }
3854 break;
3855 case elfcpp::R_MIPS_LITERAL:
3856 gold_error(_("%s:R_MIPS_LITERAL is defined only for local symbols"),
3857 object->name().c_str());
3858 break;
3859 case elfcpp::R_MIPS_GPREL32:
3860 gold_error(_("%s:R_MIPS_GPREL32 not defined for global symbols"),
3861 object->name().c_str());
3862 case elfcpp::R_MIPS_GPREL16:
3863 case elfcpp::R_MIPS16_GPREL:
3864 case elfcpp::R_MIPS16_26:
3865 break;
3866 case elfcpp::R_MIPS_26:
3867 case elfcpp::R_MIPS_PC16:
3868 {
3869 const unsigned char* pehdr = gsym->object()->get_view(
3870 elfcpp::file_header_offset,
3871 elfcpp::Elf_sizes<size>::ehdr_size,
3872 true, false);
3873
3874 elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
3875
3876 if(((ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0)
3877 && (gsym->is_defined_in_discarded_section() == false)
3878 && (gsym->is_func())
3879 && (!gsym->has_plt_offset())
3880 && (gsym->object() != object))
3881 {
3882 bool is_ordinary = false;
3883
3884 target->make_stub_entry(layout);
3885 target->set_stub_flag();
3886
3887 // If the function is at the beginning of the section and if we
3888 // would need no more than 2 nops.
3889 // TODO AS: For now we only support stubs of second type
3890 if((((Sized_symbol<size> *)gsym)->value() == 0)
3891 &&(gsym->object()->section_addralign(gsym->shndx(&is_ordinary))
3892 <= 16))
3893 {
3894 if(target->stub_symbol(gsym->name()) == NULL)
3895 target->stub_->add_entry(symtab, target, gsym);
3896 }
3897 else
3898 {
3899 if(target->stub_symbol(gsym->name()) == NULL)
3900 target->stub_->add_entry(symtab, target, gsym);
3901 }
3902 }
3903 break;
3904 }
3905 case elfcpp::R_MIPS_16:
3906 case elfcpp::R_MIPS_SHIFT5:
3907 case elfcpp::R_MIPS_SHIFT6:
3908 case elfcpp::R_MIPS_SUB:
3909 case elfcpp::R_MIPS_HIGHER:
3910 case elfcpp::R_MIPS_HIGHEST:
3911 case elfcpp::R_MIPS_SCN_DISP:
3912 case elfcpp::R_MIPS16_HI16:
3913 case elfcpp::R_MIPS16_LO16:
3914 case elfcpp::R_MIPS_GNU_REL16_S2:
3915 case elfcpp::R_MIPS_PC32:
3916 break;
3917 case elfcpp::R_MIPS_HI16:
3918 case elfcpp::R_MIPS_LO16:
3919 case elfcpp::R_MIPS_32:
3920 case elfcpp::R_MIPS_64:
3921 // Absolute addressing relocations.
3922 {
3923 // Make a dynamic relocation if necessary.
3924 if(gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3925 {
3926 if(gsym->may_need_copy_reloc() && gsym->in_dyn()
3927 && !((r_type == elfcpp::R_MIPS_32) || (r_type == elfcpp::R_MIPS_64 )))
3928 {
3929 target->copy_reloc(symtab, layout, object,
3930 data_shndx, output_section, gsym, reloc);
3931 }
3932 else if((r_type == elfcpp::R_MIPS_32) || (r_type == elfcpp::R_MIPS_6 4))
3933 {
3934 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3935 Mips_output_data_got<size, big_endian>*
3936 got = target->got_section(symtab, layout);
3937
3938 if(!gsym->final_value_is_known()
3939 || (parameters->options().output_is_position_independent()))
3940 got->add_global_with_reloc_mips(gsym);
3941
3942 rel_dyn->add_global(gsym, elfcpp::R_MIPS_REL32, output_section, object,
3943 data_shndx, reloc.get_r_offset());
3944 }
3945 }
3946 }
3947 break;
3948 case elfcpp::R_MIPS_TLS_DTPREL32:
3949 case elfcpp::R_MIPS_TLS_GD:
3950 case elfcpp::R_MIPS_TLS_LDM:
3951 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
3952 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
3953 case elfcpp::R_MIPS_TLS_GOTTPREL:
3954 case elfcpp::R_MIPS_TLS_TPREL32:
3955 case elfcpp::R_MIPS_TLS_TPREL_HI16:
3956 case elfcpp::R_MIPS_TLS_TPREL_LO16:
3957 {
3958 if(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
3959 break;
3960
3961 const bool is_final = gsym->final_value_is_known();
3962 const tls::Tls_optimization optimized_type =
3963 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
3964 //unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3965
3966 switch (r_type)
3967 {
3968 case elfcpp::R_MIPS_TLS_GD:
3969 if (optimized_type == tls::TLSOPT_NONE)
3970 {
3971 // Create a pair of GOT entries for the module index and
3972 // dtv-relative offset.
3973 Mips_output_data_got<size, big_endian>* got
3974 = target->got_section(symtab, layout);
3975 if (!parameters->doing_static_link())
3976 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3977 target->rel_dyn_section(layout),
3978 size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 :
3979 elfcpp::R_MIPS_TLS_DTPMOD64,
3980 size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 :
3981 elfcpp::R_MIPS_TLS_DTPREL64);
3982 else
3983 got->add_tls_gd_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
3984 }
3985 else
3986 // FIXME: TLS optimization not supported yet.
3987 gold_unreachable();
3988 break;
3989
3990 case elfcpp::R_MIPS_TLS_LDM:
3991 if (optimized_type == tls::TLSOPT_NONE)
3992 // Create a GOT entry for the module index.
3993 target->got_mod_index_entry(symtab, layout, object);
3994 else
3995 // FIXME: TLS optimization not supported yet.
3996 gold_unreachable();
3997 break;
3998 case elfcpp::R_MIPS_TLS_GOTTPREL:
3999 layout->set_has_static_tls();
4000 if (optimized_type == tls::TLSOPT_NONE)
4001 {
4002 // Create a GOT entry for the tp-relative offset.
4003 Mips_output_data_got<size, big_endian>* got
4004 = target->got_section(symtab, layout);
4005 if (!parameters->doing_static_link())
4006 got->add_global_tls_with_reloc_mips(gsym, target->rel_dyn_sect ion(layout),
4007 elfcpp::R_MIPS_TLS_TPREL32);
4008 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
4009 {
4010 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
4011 unsigned int got_offset =
4012 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
4013 got->add_static_reloc(got_offset,
4014 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
4015 elfcpp::R_MIPS_TLS_TPREL64, gsym);
4016 }
4017 }
4018 else
4019 // FIXME: TLS optimization not supported yet.
4020 gold_unreachable();
4021 break;
4022
4023 default:
4024 layout->set_has_static_tls();
4025 if (parameters->options().shared())
4026 {
4027 // We need to create a dynamic relocation.
4028 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4029 rel_dyn->add_global(gsym,
4030 size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 :
4031 elfcpp::R_MIPS_TLS_TPREL64,
4032 output_section, object,
4033 data_shndx, reloc.get_r_offset());
4034 }
4035 break;
4036 }
4037 }
4038 break;
4039 case elfcpp::R_MIPS_REL32:
4040 // Relative addressing relocations.
4041 {
4042 // Make a dynamic relocation if necessary.
4043 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
4044 {
4045 if(gsym->may_need_copy_reloc())
4046 target->copy_reloc(symtab, layout, object,
4047 data_shndx, output_section, gsym, reloc);
4048 else
4049 {
4050 check_non_pic(object, r_type);
4051 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4052 rel_dyn->add_global(gsym, r_type, output_section, object,
4053 data_shndx, reloc.get_r_offset());
4054 }
4055 }
4056 }
4057 break;
4058 case elfcpp::R_MIPS_JALR:
4059 break;
4060 case elfcpp::R_MIPS_COPY:
4061 case elfcpp::R_MIPS_GLOB_DAT:
4062 case elfcpp::R_MIPS_JUMP_SLOT:
4063 // These are relocations which should only be seen by the
4064 // dynamic linker, and should never be seen here.
4065 gold_error(_("%s: unexpected reloc %u in object file"),
4066 object->name().c_str(), r_type);
4067 break;
4068
4069 default:
4070 unsupported_reloc_global(object, r_type, gsym);
4071 break;
4072 }
4073 }
4074
4075 // Perform a relocation.
4076
4077 template<int size, bool big_endian>
4078 inline bool
4079 Target_mips<size, big_endian>::Relocate::relocate(
4080 const Relocate_info<size, big_endian>* relinfo,
4081 Target_mips* target,
4082 Output_section* output_section,
4083 size_t relnum,
4084 const elfcpp::Rel<size, big_endian>& rel,
4085 unsigned int r_type,
4086 const Sized_symbol<size>* gsym,
4087 const Symbol_value<size>* psymval,
4088 unsigned char* view,
4089 typename elfcpp::Elf_types<size>::Elf_Addr address,
4090 section_size_type)
4091 {
4092 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
4093 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
4094
4095 unsigned int got_offset = 0;
4096 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
4097
4098 Symbol_value<size> symval;
4099
4100 // __gnu_local_gp is _gp symbol, just swich it
4101 if(gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0))
4102 {
4103 gsym = target->gp();
4104
4105 gold_assert(gsym != NULL);
4106
4107 symval.set_output_value(target->gp_address());
4108
4109 psymval = &symval;
4110 }
4111 else if(gsym && (strcmp(gsym->name(), "_gp_disp") == 0))
4112 {
4113 if(r_type != elfcpp::R_MIPS_HI16 && r_type != elfcpp::R_MIPS16_HI16 &&
4114 r_type != elfcpp::R_MIPS_LO16 && r_type != elfcpp::R_MIPS16_LO16)
4115 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4116 _("relocations against _gp_disp are permitted only"
4117 " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
4118
4119 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4120 Valtype* wv = reinterpret_cast<Valtype*>(view);
4121 Valtype addend = (elfcpp::Swap<32, big_endian>::readval(wv)) & 0xffffU;
4122 Valtype value;
4123
4124 if (r_type == elfcpp::R_MIPS16_HI16)
4125 value = addend + target->gp_address() - address - 4;
4126 else if(r_type == elfcpp::R_MIPS_HI16)
4127 value = addend + target->gp_address() - address;
4128 else if (r_type == elfcpp::R_MIPS16_LO16)
4129 value = addend + target->gp_address() - address;
4130 else if(r_type == elfcpp::R_MIPS_LO16)
4131 value = addend + target->gp_address() - address + 4;
4132 else
4133 gold_unreachable();
4134
4135 symval.set_output_value(value);
4136
4137 psymval = &symval;
4138 }
4139
4140 // Get the GOT offset.
4141 // The GOT pointer points to the end of the GOT section.
4142 // We need to subtract the size of the GOT section to get
4143 // the actual offset to use in the relocation.
4144 switch (r_type)
4145 {
4146 case elfcpp::R_MIPS_GOT_DISP:
4147 case elfcpp::R_MIPS_GOT_HI16:
4148 case elfcpp::R_MIPS_CALL_HI16:
4149 case elfcpp::R_MIPS_GOT_LO16:
4150 case elfcpp::R_MIPS_CALL_LO16:
4151 if(gsym != NULL)
4152 {
4153 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4154 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4155 - target->got_size());
4156 }
4157 else
4158 {
4159 unsigned int r_sym = elfcpp::elf_r_sym<size>(rel.get_r_info());
4160 gold_assert(relinfo->object->local_has_got_offset
4161 (r_sym, GOT_TYPE_STANDARD));
4162 got_offset = (relinfo->object->local_got_offset
4163 (r_sym, GOT_TYPE_STANDARD) - target->got_size());
4164 }
4165 break;
4166 case elfcpp::R_MIPS_TLS_GD:
4167 if(gsym != NULL)
4168 {
4169 got_offset = target->got_section()->address() +
4170 gsym->got_offset(GOT_TYPE_TLS_PAIR) -
4171 target->gp_address();
4172 }
4173 else
4174 {
4175 unsigned int r_sym = elfcpp::elf_r_sym<size>(rel.get_r_info());
4176 gold_assert(relinfo->object->local_has_got_offset
4177 (r_sym, GOT_TYPE_TLS_PAIR));
4178 got_offset = target->got_section()->address() +
4179 relinfo->object->local_got_offset(r_sym, GOT_TYPE_TLS_PAIR) -
4180 target->gp_address();
4181 }
4182 break;
4183 case elfcpp::R_MIPS_TLS_GOTTPREL:
4184 if(gsym != NULL)
4185 {
4186 got_offset = target->got_section()->address() +
4187 gsym->got_offset(GOT_TYPE_TLS_OFFSET) -
4188 target->gp_address();
4189 }
4190 else
4191 {
4192 unsigned int r_sym = elfcpp::elf_r_sym<size>(rel.get_r_info());
4193 gold_assert(relinfo->object->local_has_got_offset
4194 (r_sym, GOT_TYPE_TLS_OFFSET));
4195 got_offset = target->got_section()->address() +
4196 relinfo->object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) -
4197 target->gp_address();
4198 }
4199 break;
4200 case elfcpp::R_MIPS_TLS_LDM:
4201 // Relocate the field with the offset of the GOT entry for
4202 // the module index.
4203 got_offset = target->got_section()->address() +
4204 (target->got_mod_index_entry(NULL, NULL, NULL)) -
4205 target->gp_address();
4206 break;
4207 case elfcpp::R_MIPS_GOT_OFST:
4208 if((gsym != NULL) && (!gsym->is_forced_local()))
4209 {
4210 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4211 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
4212 Valtype* wv = reinterpret_cast<Valtype*>(view);
4213 Reltype addend = elfcpp::Swap<32, big_endian>::readval(wv);
4214 addend &= 0x0000ffffU;
4215
4216 got_offset = addend;
4217 break;
4218 }
4219 case elfcpp::R_MIPS_GOT_PAGE:
4220 {
4221 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4222 typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
4223 Valtype* wv = reinterpret_cast<Valtype*>(view);
4224 Reltype addend = elfcpp::Swap<32, big_endian>::readval(wv);
4225 addend &= 0x0000ffffU;
4226 got_offset = target->got_section()->add_constant(psymval->value(object,
4227 addend) + 0x00008000U) & ~0x0000ffffU;
4228 }
4229 break;
4230 default:
4231 break;
4232 }
4233
4234 // Pick the value to use for symbols defined in shared objects.
4235 if (gsym != NULL
4236 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4237 {
4238 elfcpp::Elf_Xword value;
4239
4240 // To avoid problems with symbols that are no longer in .MIPS.stubs
4241 if(gsym->plt_offset() > 0)
4242 {
4243 if(target->is_plt_symbol((Sized_symbol<size> *)gsym))
4244 value = target->plt_section()->address() + gsym->plt_offset();
4245 else
4246 value = target->mips_stubs()->address() +
4247 gsym->plt_offset() * (target->mips_stubs()->big_dyn_symtab() ? 20 : 16);
4248
4249 symval.set_output_value(value);
4250
4251 psymval = &symval;
4252 }
4253 }
4254
4255 // Set symbol for use with stub
4256 switch (r_type)
4257 {
4258 case elfcpp::R_MIPS_26:
4259 case elfcpp::R_MIPS_PC16:
4260 case elfcpp::R_MIPS16_26:
4261 {
4262 if(target->stub_flag())
4263 {
4264 if(gsym == NULL)
4265 break;
4266
4267 Sized_symbol<size> * sym = static_cast<Sized_symbol<size>*>
4268 (target->stub_symbol(gsym->name()));
4269 if(sym != NULL)
4270 {
4271 symval.set_output_value(sym->value());
4272 psymval = &symval;
4273 }
4274 }
4275 }
4276 break;
4277 default:
4278 break;
4279 }
4280
4281 switch (r_type)
4282 {
4283 case elfcpp::R_MIPS_NONE:
4284 case elfcpp::R_MIPS_GNU_VTINHERIT:
4285 case elfcpp::R_MIPS_GNU_VTENTRY:
4286 break;
4287 case elfcpp::R_MIPS_16:
4288 reloc_status = Reloc_funcs::rel16(view, object, psymval);
4289 break;
4290 case elfcpp::R_MIPS_32:
4291 if((gsym != NULL)
4292 && ((parameters->options().shared())
4293 || (parameters->options().pie())))
4294 break;
4295
4296 reloc_status = Reloc_funcs::rel32(view, object, psymval);
4297 break;
4298 case elfcpp::R_MIPS_REL32:
4299 if(parameters->options().shared() && (gsym != NULL))
4300 break;
4301 case elfcpp::R_MIPS_PC32:
4302 reloc_status = Reloc_funcs::relrel32(view, object, psymval, address);
4303 break;
4304 case elfcpp::R_MIPS_26:
4305 // TODO AS: Check if this is correct.
4306 if(gsym != NULL)
4307 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
4308 gsym->is_forced_local());
4309 else
4310 reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
4311 false);
4312 break;
4313 case elfcpp::R_MIPS_HI16:
4314 case elfcpp::R_MIPS16_HI16:
4315 reloc_status = Reloc_funcs::relhi16(view, psymval->value(object, 0));
4316 break;
4317 case elfcpp::R_MIPS_LO16:
4318 case elfcpp::R_MIPS16_LO16:
4319 reloc_status = Reloc_funcs::rello16(view, object, psymval);
4320 break;
4321 case elfcpp::R_MIPS_GPREL16:
4322 case elfcpp::R_MIPS16_GPREL:
4323 case elfcpp::R_MIPS_LITERAL:
4324 reloc_status = Reloc_funcs::relgprel(view, object, psymval,
4325 target->gp_address());
4326 break;
4327 case elfcpp::R_MIPS_GOT16:
4328 case elfcpp::R_MIPS_CALL16:
4329 case elfcpp::R_MIPS16_GOT16:
4330 case elfcpp::R_MIPS16_CALL16:
4331 {
4332 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
4333 Mips_output_data_got<size, big_endian>* got = target->got_section();
4334 Valtype got_offset;
4335
4336 if(gsym == NULL)
4337 {
4338 std::stringstream tmp;
4339 tmp << static_cast<unsigned int> (rel.get_r_info())
4340 << static_cast<unsigned int> (reinterpret_cast<unsigned long long> (object))
4341 << static_cast<unsigned int> (rel.get_r_offset());
4342 std::string key = tmp.str();
4343
4344 got_offset = got->got16_offset(key);
4345 }
4346 else
4347 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
4348
4349 unsigned char* got_view = got_offset +
4350 (unsigned char* const)got->got_view();
4351
4352 reloc_status = Reloc_funcs::relgot16(view, object, psymval,
4353 target->gp_address(), got_view,
4354 got->address(), got_offset,
4355 gsym);
4356 }
4357 break;
4358 case elfcpp::R_MIPS_PC16:
4359 case elfcpp::R_MIPS_GNU_REL16_S2:
4360 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address);
4361 break;
4362 case elfcpp::R_MIPS_GPREL32:
4363 reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
4364 target->gp_address());
4365 break;
4366 case elfcpp::R_MIPS_UNUSED1:
4367 case elfcpp::R_MIPS_UNUSED2:
4368 case elfcpp::R_MIPS_UNUSED3:
4369 break;
4370 case elfcpp::R_MIPS_SHIFT5:
4371 reloc_status = Reloc_funcs::relsh5(view, object, psymval);
4372 break;
4373 case elfcpp::R_MIPS_SHIFT6:
4374 reloc_status = Reloc_funcs::relsh6(view, object, psymval);
4375 break;
4376 case elfcpp::R_MIPS_64:
4377 if(parameters->options().shared() && (gsym != NULL))
4378 break;
4379
4380 reloc_status = Reloc_funcs::rel64(view, object, psymval);
4381 break;
4382 case elfcpp::R_MIPS_GOT_HI16:
4383 case elfcpp::R_MIPS_CALL_HI16:
4384 got_offset = ((got_offset + 0x8000) >> 16);
4385 case elfcpp::R_MIPS_GOT_LO16:
4386 case elfcpp::R_MIPS_CALL_LO16:
4387 case elfcpp::R_MIPS_GOT_OFST:
4388 case elfcpp::R_MIPS_GOT_PAGE:
4389 case elfcpp::R_MIPS_GOT_DISP:
4390 case elfcpp::R_MIPS_TLS_GOTTPREL:
4391 case elfcpp::R_MIPS_TLS_GD:
4392 case elfcpp::R_MIPS_TLS_LDM:
4393 reloc_status = Reloc_funcs::relgotofst(view, got_offset);
4394 break;
4395 case elfcpp::R_MIPS_SUB:
4396 reloc_status = Reloc_funcs::relsub(view, object, psymval);
4397 break;
4398 case elfcpp::R_MIPS_INSERT_A:
4399 case elfcpp::R_MIPS_INSERT_B:
4400 case elfcpp::R_MIPS_DELETE:
4401 break;
4402 case elfcpp::R_MIPS_HIGHER:
4403 reloc_status = Reloc_funcs::relhigher(view, object, psymval);
4404 break;
4405 case elfcpp::R_MIPS_HIGHEST:
4406 reloc_status = Reloc_funcs::relhighest(view, object, psymval);
4407 break;
4408 case elfcpp::R_MIPS_SCN_DISP:
4409 reloc_status = Reloc_funcs::relscndisp(view, object, psymval,
4410 output_section->offset());
4411 break;
4412 case elfcpp::R_MIPS_REL16:
4413 case elfcpp::R_MIPS_ADD_IMMEDIATE:
4414 case elfcpp::R_MIPS_PJUMP:
4415 case elfcpp::R_MIPS_RELGOT:
4416 break;
4417 case elfcpp::R_MIPS_JALR:
4418 if((gsym != NULL) && !(gsym->final_value_is_known()))
4419 break;
4420
4421 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address);
4422 break;
4423 case elfcpp::R_MIPS_TLS_DTPREL32:
4424 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval, elfcpp::DTP_OF FSET);
4425 break;
4426 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
4427 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval, elfcpp::DTP_ OFFSET);
4428 break;
4429 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
4430 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval, elfcpp::DTP_ OFFSET);
4431 break;
4432 case elfcpp::R_MIPS_TLS_TPREL32:
4433 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval, elfcpp::TP_OFF SET);
4434 break;
4435 case elfcpp::R_MIPS_TLS_TPREL_LO16:
4436 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval, elfcpp::TP_O FFSET);
4437 break;
4438 case elfcpp::R_MIPS_TLS_TPREL_HI16:
4439 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval, elfcpp::TP_O FFSET);
4440 break;
4441 case elfcpp::R_MIPS_TLS_DTPMOD64:
4442 case elfcpp::R_MIPS_TLS_DTPREL64:
4443 case elfcpp::R_MIPS_TLS_TPREL64:
4444 case elfcpp::R_MIPS_TLS_DTPMOD32: // TODO AS! For now this reloc do nothing.
4445 break;
4446 case elfcpp::R_MIPS_GLOB_DAT:
4447 reloc_status = Reloc_funcs::relglob(view, object, psymval);
4448 break;
4449 default:
4450 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4451 _("unsupported reloc %u"), r_type);
4452 break;
4453 }
4454
4455 // Report any errors.
4456 switch (reloc_status)
4457 {
4458 case Reloc_funcs::STATUS_OKAY:
4459 break;
4460 case Reloc_funcs::STATUS_OVERFLOW:
4461 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4462 _("relocation overflow"));
4463 break;
4464 case Reloc_funcs::STATUS_BAD_RELOC:
4465 gold_error_at_location(
4466 relinfo,
4467 relnum,
4468 rel.get_r_offset(),
4469 _("unexpected opcode while processing relocation"));
4470 break;
4471 default:
4472 gold_unreachable();
4473 }
4474
4475 return true;
4476 }
4477
4478 // Get the Reference_flags for a particular relocation.
4479
4480 template<int size, bool big_endian>
4481 int
4482 Target_mips<size, big_endian>::Scan::get_reference_flags(
4483 unsigned int r_type)
4484 {
4485 switch (r_type)
4486 {
4487 case elfcpp::R_MIPS_NONE:
4488 case elfcpp::R_MIPS_GNU_VTINHERIT:
4489 case elfcpp::R_MIPS_GNU_VTENTRY:
4490 case elfcpp::R_MIPS_INSERT_A:
4491 case elfcpp::R_MIPS_INSERT_B:
4492 case elfcpp::R_MIPS_DELETE:
4493 case elfcpp::R_MIPS_PJUMP:
4494 case elfcpp::R_MIPS_RELGOT:
4495 case elfcpp::R_MIPS_REL16:
4496 case elfcpp::R_MIPS_ADD_IMMEDIATE:
4497 // No symbol reference.
4498 return 0;
4499
4500 case elfcpp::R_MIPS_16:
4501 case elfcpp::R_MIPS_32:
4502 case elfcpp::R_MIPS_64:
4503 case elfcpp::R_MIPS_HI16:
4504 case elfcpp::R_MIPS_LO16:
4505 case elfcpp::R_MIPS_SHIFT5:
4506 case elfcpp::R_MIPS_SHIFT6:
4507 case elfcpp::R_MIPS_SUB:
4508 case elfcpp::R_MIPS_HIGHER:
4509 case elfcpp::R_MIPS_HIGHEST:
4510 case elfcpp::R_MIPS_SCN_DISP:
4511 case elfcpp::R_MIPS16_HI16:
4512 case elfcpp::R_MIPS16_LO16:
4513 return Symbol::ABSOLUTE_REF;
4514
4515 case elfcpp::R_MIPS_26:
4516 case elfcpp::R_MIPS16_26:
4517 case elfcpp::R_MIPS_GPREL32:
4518 case elfcpp::R_MIPS_GPREL16:
4519 case elfcpp::R_MIPS16_GPREL:
4520 case elfcpp::R_MIPS_REL32:
4521 return Symbol::RELATIVE_REF;
4522
4523 case elfcpp::R_MIPS_PC16:
4524 case elfcpp::R_MIPS_PC32:
4525 case elfcpp::R_MIPS_GNU_REL16_S2:
4526 case elfcpp::R_MIPS_JALR:
4527 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
4528
4529 case elfcpp::R_MIPS_GOT16:
4530 case elfcpp::R_MIPS16_GOT16:
4531 case elfcpp::R_MIPS_CALL16:
4532 case elfcpp::R_MIPS16_CALL16:
4533 case elfcpp::R_MIPS_GOT_DISP:
4534 case elfcpp::R_MIPS_GOT_HI16:
4535 case elfcpp::R_MIPS_CALL_HI16:
4536 case elfcpp::R_MIPS_GOT_LO16:
4537 case elfcpp::R_MIPS_CALL_LO16:
4538 case elfcpp::R_MIPS_LITERAL:
4539 case elfcpp::R_MIPS_GOT_PAGE:
4540 case elfcpp::R_MIPS_GOT_OFST:
4541 // Absolute in GOT.
4542 return Symbol::RELATIVE_REF;
4543
4544 case elfcpp::R_MIPS_TLS_DTPMOD32:
4545 case elfcpp::R_MIPS_TLS_DTPREL32:
4546 case elfcpp::R_MIPS_TLS_DTPMOD64:
4547 case elfcpp::R_MIPS_TLS_DTPREL64:
4548 case elfcpp::R_MIPS_TLS_GD:
4549 case elfcpp::R_MIPS_TLS_LDM:
4550 case elfcpp::R_MIPS_TLS_DTPREL_HI16:
4551 case elfcpp::R_MIPS_TLS_DTPREL_LO16:
4552 case elfcpp::R_MIPS_TLS_GOTTPREL:
4553 case elfcpp::R_MIPS_TLS_TPREL32:
4554 case elfcpp::R_MIPS_TLS_TPREL64:
4555 case elfcpp::R_MIPS_TLS_TPREL_HI16:
4556 case elfcpp::R_MIPS_TLS_TPREL_LO16:
4557 return Symbol::TLS_REF;
4558
4559 case elfcpp::R_MIPS_COPY:
4560 case elfcpp::R_MIPS_UNUSED1:
4561 case elfcpp::R_MIPS_UNUSED2:
4562 case elfcpp::R_MIPS_UNUSED3:
4563 case elfcpp::R_MIPS_GLOB_DAT:
4564 case elfcpp::R_MIPS_JUMP_SLOT:
4565 default:
4566 // Not expected. We will give an error later.
4567 return 0;
4568 }
4569 }
4570
4571 // Report an unsupported relocation against a local symbol.
4572
4573 template<int size, bool big_endian>
4574 void
4575 Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
4576 Sized_relobj_file<size, big_endian>* object,
4577 unsigned int r_type)
4578 {
4579 gold_error(_("%s: unsupported reloc %u against local symbol"),
4580 object->name().c_str(), r_type);
4581 }
4582
4583 // Report an unsupported relocation against a global symbol.
4584
4585 template<int size, bool big_endian>
4586 void
4587 Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
4588 Sized_relobj_file<size, big_endian>* object,
4589 unsigned int r_type,
4590 Symbol* gsym)
4591 {
4592 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4593 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4594 }
4595
4596 template<int size, bool big_endian>
4597 Target::Target_info Target_mips<size, big_endian>::mips_info =
4598 {
4599 size, // size
4600 big_endian, // is_big_endian
4601 elfcpp::EM_MIPS, // machine_code
4602 false, // has_make_symbol
4603 false, // has_resolve
4604 false, // has_code_fill
4605 true, // is_default_stack_executable
4606 false, // can_icf_inline_merge_sections
4607 '\0', // wrap_char
4608 "/lib/ld.so.1", // dynamic_linker
4609 0x400000, // default_text_segment_address
4610 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
4611 4 * 1024, // common_pagesize (overridable by -z common-page-size)
4612 elfcpp::SHN_UNDEF, // small_common_shndx
4613 elfcpp::SHN_UNDEF, // large_common_shndx
4614 0, // small_common_section_flags
4615 0, // large_common_section_flags
4616 NULL, // attributes_section
4617 NULL // attributes_vendor
4618 };
4619
4620 // Adjust ELF file header.
4621
4622 template<int size, bool big_endian>
4623 void
4624 Target_mips<size, big_endian>::do_adjust_elf_header(
4625 unsigned char* view,
4626 int len) const
4627 {
4628 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
4629
4630 elfcpp::Ehdr<size, big_endian> ehdr(view);
4631 unsigned char e_ident[elfcpp::EI_NIDENT];
4632 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
4633
4634 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
4635 e_ident[elfcpp::EI_ABIVERSION] = elfcpp::EV_NONE;
4636
4637 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
4638 oehdr.put_e_ident(e_ident);
4639 oehdr.put_e_flags(this->processor_specific_flags_);
4640 }
4641
4642 // The selector for mips object files.
4643
4644 template<int size, bool big_endian>
4645 class Target_selector_mips : public Target_selector
4646 {
4647 public:
4648 Target_selector_mips()
4649 : Target_selector(elfcpp::EM_MIPS, size, big_endian,
4650 (size == 64 ?
4651 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
4652 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
4653 (size == 64 ?
4654 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
4655 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")))
4656 { }
4657
4658 Target* do_instantiate_target()
4659 { return new Target_mips<size, big_endian>(); }
4660 };
4661
4662 Target_selector_mips<32, false> target_selector_mips32;
4663
4664
4665 } // End anonymous namespace.
OLDNEW
« no previous file with comments | « gold/layout.cc ('k') | gold/output.h » ('j') | gold/symtab.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698