bfd/
[binutils-gdb.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
12 This file is part of BFD, the Binary File Descriptor library.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27 MA 02110-1301, USA. */
28
29
30 /* This file handles functionality common to the different MIPS ABI's. */
31
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libbfd.h"
35 #include "libiberty.h"
36 #include "elf-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
40
41 /* Get the ECOFF swapping routines. */
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/ecoff.h"
45 #include "coff/mips.h"
46
47 #include "hashtab.h"
48
49 /* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
51
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
56 (3) global and forced-local symbols
57 (abfd != NULL, symndx == -1)
58
59 Type (3) entries are treated differently for different types of GOT.
60 In the "master" GOT -- i.e. the one that describes every GOT
61 reference needed in the link -- the mips_got_entry is keyed on both
62 the symbol and the input bfd that references it. If it turns out
63 that we need multiple GOTs, we can then use this information to
64 create separate GOTs for each input bfd.
65
66 However, we want each of these separate GOTs to have at most one
67 entry for a given symbol, so their type (3) entries are keyed only
68 on the symbol. The input bfd given by the "abfd" field is somewhat
69 arbitrary in this case.
70
71 This means that when there are multiple GOTs, each GOT has a unique
72 mips_got_entry for every symbol within it. We can therefore use the
73 mips_got_entry fields (tls_type and gotidx) to track the symbol's
74 GOT index.
75
76 However, if it turns out that we need only a single GOT, we continue
77 to use the master GOT to describe it. There may therefore be several
78 mips_got_entries for the same symbol, each with a different input bfd.
79 We want to make sure that each symbol gets a unique GOT entry, so when
80 there's a single GOT, we use the symbol's hash entry, not the
81 mips_got_entry fields, to track a symbol's GOT index. */
82 struct mips_got_entry
83 {
84 /* The input bfd in which the symbol is defined. */
85 bfd *abfd;
86 /* The index of the symbol, as stored in the relocation r_info, if
87 we have a local symbol; -1 otherwise. */
88 long symndx;
89 union
90 {
91 /* If abfd == NULL, an address that must be stored in the got. */
92 bfd_vma address;
93 /* If abfd != NULL && symndx != -1, the addend of the relocation
94 that should be added to the symbol value. */
95 bfd_vma addend;
96 /* If abfd != NULL && symndx == -1, the hash table entry
97 corresponding to a global symbol in the got (or, local, if
98 h->forced_local). */
99 struct mips_elf_link_hash_entry *h;
100 } d;
101
102 /* The TLS types included in this GOT entry (specifically, GD and
103 IE). The GD and IE flags can be added as we encounter new
104 relocations. LDM can also be set; it will always be alone, not
105 combined with any GD or IE flags. An LDM GOT entry will be
106 a local symbol entry with r_symndx == 0. */
107 unsigned char tls_type;
108
109 /* The offset from the beginning of the .got section to the entry
110 corresponding to this symbol+addend. If it's a global symbol
111 whose offset is yet to be decided, it's going to be -1. */
112 long gotidx;
113 };
114
115 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116 The structures form a non-overlapping list that is sorted by increasing
117 MIN_ADDEND. */
118 struct mips_got_page_range
119 {
120 struct mips_got_page_range *next;
121 bfd_signed_vma min_addend;
122 bfd_signed_vma max_addend;
123 };
124
125 /* This structure describes the range of addends that are applied to page
126 relocations against a given symbol. */
127 struct mips_got_page_entry
128 {
129 /* The input bfd in which the symbol is defined. */
130 bfd *abfd;
131 /* The index of the symbol, as stored in the relocation r_info. */
132 long symndx;
133 /* The ranges for this page entry. */
134 struct mips_got_page_range *ranges;
135 /* The maximum number of page entries needed for RANGES. */
136 bfd_vma num_pages;
137 };
138
139 /* This structure is used to hold .got information when linking. */
140
141 struct mips_got_info
142 {
143 /* The global symbol in the GOT with the lowest index in the dynamic
144 symbol table. */
145 struct elf_link_hash_entry *global_gotsym;
146 /* The number of global .got entries. */
147 unsigned int global_gotno;
148 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
149 unsigned int reloc_only_gotno;
150 /* The number of .got slots used for TLS. */
151 unsigned int tls_gotno;
152 /* The first unused TLS .got entry. Used only during
153 mips_elf_initialize_tls_index. */
154 unsigned int tls_assigned_gotno;
155 /* The number of local .got entries, eventually including page entries. */
156 unsigned int local_gotno;
157 /* The maximum number of page entries needed. */
158 unsigned int page_gotno;
159 /* The number of local .got entries we have used. */
160 unsigned int assigned_gotno;
161 /* A hash table holding members of the got. */
162 struct htab *got_entries;
163 /* A hash table of mips_got_page_entry structures. */
164 struct htab *got_page_entries;
165 /* A hash table mapping input bfds to other mips_got_info. NULL
166 unless multi-got was necessary. */
167 struct htab *bfd2got;
168 /* In multi-got links, a pointer to the next got (err, rather, most
169 of the time, it points to the previous got). */
170 struct mips_got_info *next;
171 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
172 for none, or MINUS_TWO for not yet assigned. This is needed
173 because a single-GOT link may have multiple hash table entries
174 for the LDM. It does not get initialized in multi-GOT mode. */
175 bfd_vma tls_ldm_offset;
176 };
177
178 /* Map an input bfd to a got in a multi-got link. */
179
180 struct mips_elf_bfd2got_hash
181 {
182 bfd *bfd;
183 struct mips_got_info *g;
184 };
185
186 /* Structure passed when traversing the bfd2got hash table, used to
187 create and merge bfd's gots. */
188
189 struct mips_elf_got_per_bfd_arg
190 {
191 /* A hashtable that maps bfds to gots. */
192 htab_t bfd2got;
193 /* The output bfd. */
194 bfd *obfd;
195 /* The link information. */
196 struct bfd_link_info *info;
197 /* A pointer to the primary got, i.e., the one that's going to get
198 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
199 DT_MIPS_GOTSYM. */
200 struct mips_got_info *primary;
201 /* A non-primary got we're trying to merge with other input bfd's
202 gots. */
203 struct mips_got_info *current;
204 /* The maximum number of got entries that can be addressed with a
205 16-bit offset. */
206 unsigned int max_count;
207 /* The maximum number of page entries needed by each got. */
208 unsigned int max_pages;
209 /* The total number of global entries which will live in the
210 primary got and be automatically relocated. This includes
211 those not referenced by the primary GOT but included in
212 the "master" GOT. */
213 unsigned int global_count;
214 };
215
216 /* Another structure used to pass arguments for got entries traversal. */
217
218 struct mips_elf_set_global_got_offset_arg
219 {
220 struct mips_got_info *g;
221 int value;
222 unsigned int needed_relocs;
223 struct bfd_link_info *info;
224 };
225
226 /* A structure used to count TLS relocations or GOT entries, for GOT
227 entry or ELF symbol table traversal. */
228
229 struct mips_elf_count_tls_arg
230 {
231 struct bfd_link_info *info;
232 unsigned int needed;
233 };
234
235 struct _mips_elf_section_data
236 {
237 struct bfd_elf_section_data elf;
238 union
239 {
240 bfd_byte *tdata;
241 } u;
242 };
243
244 #define mips_elf_section_data(sec) \
245 ((struct _mips_elf_section_data *) elf_section_data (sec))
246
247 #define is_mips_elf(bfd) \
248 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
249 && elf_tdata (bfd) != NULL \
250 && elf_object_id (bfd) == MIPS_ELF_DATA)
251
252 /* The ABI says that every symbol used by dynamic relocations must have
253 a global GOT entry. Among other things, this provides the dynamic
254 linker with a free, directly-indexed cache. The GOT can therefore
255 contain symbols that are not referenced by GOT relocations themselves
256 (in other words, it may have symbols that are not referenced by things
257 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
258
259 GOT relocations are less likely to overflow if we put the associated
260 GOT entries towards the beginning. We therefore divide the global
261 GOT entries into two areas: "normal" and "reloc-only". Entries in
262 the first area can be used for both dynamic relocations and GP-relative
263 accesses, while those in the "reloc-only" area are for dynamic
264 relocations only.
265
266 These GGA_* ("Global GOT Area") values are organised so that lower
267 values are more general than higher values. Also, non-GGA_NONE
268 values are ordered by the position of the area in the GOT. */
269 #define GGA_NORMAL 0
270 #define GGA_RELOC_ONLY 1
271 #define GGA_NONE 2
272
273 /* Information about a non-PIC interface to a PIC function. There are
274 two ways of creating these interfaces. The first is to add:
275
276 lui $25,%hi(func)
277 addiu $25,$25,%lo(func)
278
279 immediately before a PIC function "func". The second is to add:
280
281 lui $25,%hi(func)
282 j func
283 addiu $25,$25,%lo(func)
284
285 to a separate trampoline section.
286
287 Stubs of the first kind go in a new section immediately before the
288 target function. Stubs of the second kind go in a single section
289 pointed to by the hash table's "strampoline" field. */
290 struct mips_elf_la25_stub {
291 /* The generated section that contains this stub. */
292 asection *stub_section;
293
294 /* The offset of the stub from the start of STUB_SECTION. */
295 bfd_vma offset;
296
297 /* One symbol for the original function. Its location is available
298 in H->root.root.u.def. */
299 struct mips_elf_link_hash_entry *h;
300 };
301
302 /* Macros for populating a mips_elf_la25_stub. */
303
304 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
305 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
306 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
307
308 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
309 the dynamic symbols. */
310
311 struct mips_elf_hash_sort_data
312 {
313 /* The symbol in the global GOT with the lowest dynamic symbol table
314 index. */
315 struct elf_link_hash_entry *low;
316 /* The least dynamic symbol table index corresponding to a non-TLS
317 symbol with a GOT entry. */
318 long min_got_dynindx;
319 /* The greatest dynamic symbol table index corresponding to a symbol
320 with a GOT entry that is not referenced (e.g., a dynamic symbol
321 with dynamic relocations pointing to it from non-primary GOTs). */
322 long max_unref_got_dynindx;
323 /* The greatest dynamic symbol table index not corresponding to a
324 symbol without a GOT entry. */
325 long max_non_got_dynindx;
326 };
327
328 /* The MIPS ELF linker needs additional information for each symbol in
329 the global hash table. */
330
331 struct mips_elf_link_hash_entry
332 {
333 struct elf_link_hash_entry root;
334
335 /* External symbol information. */
336 EXTR esym;
337
338 /* The la25 stub we have created for ths symbol, if any. */
339 struct mips_elf_la25_stub *la25_stub;
340
341 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
342 this symbol. */
343 unsigned int possibly_dynamic_relocs;
344
345 /* If there is a stub that 32 bit functions should use to call this
346 16 bit function, this points to the section containing the stub. */
347 asection *fn_stub;
348
349 /* If there is a stub that 16 bit functions should use to call this
350 32 bit function, this points to the section containing the stub. */
351 asection *call_stub;
352
353 /* This is like the call_stub field, but it is used if the function
354 being called returns a floating point value. */
355 asection *call_fp_stub;
356
357 #define GOT_NORMAL 0
358 #define GOT_TLS_GD 1
359 #define GOT_TLS_LDM 2
360 #define GOT_TLS_IE 4
361 #define GOT_TLS_OFFSET_DONE 0x40
362 #define GOT_TLS_DONE 0x80
363 unsigned char tls_type;
364
365 /* This is only used in single-GOT mode; in multi-GOT mode there
366 is one mips_got_entry per GOT entry, so the offset is stored
367 there. In single-GOT mode there may be many mips_got_entry
368 structures all referring to the same GOT slot. It might be
369 possible to use root.got.offset instead, but that field is
370 overloaded already. */
371 bfd_vma tls_got_offset;
372
373 /* The highest GGA_* value that satisfies all references to this symbol. */
374 unsigned int global_got_area : 2;
375
376 /* True if one of the relocations described by possibly_dynamic_relocs
377 is against a readonly section. */
378 unsigned int readonly_reloc : 1;
379
380 /* True if there is a relocation against this symbol that must be
381 resolved by the static linker (in other words, if the relocation
382 cannot possibly be made dynamic). */
383 unsigned int has_static_relocs : 1;
384
385 /* True if we must not create a .MIPS.stubs entry for this symbol.
386 This is set, for example, if there are relocations related to
387 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
388 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
389 unsigned int no_fn_stub : 1;
390
391 /* Whether we need the fn_stub; this is true if this symbol appears
392 in any relocs other than a 16 bit call. */
393 unsigned int need_fn_stub : 1;
394
395 /* True if this symbol is referenced by branch relocations from
396 any non-PIC input file. This is used to determine whether an
397 la25 stub is required. */
398 unsigned int has_nonpic_branches : 1;
399
400 /* Does this symbol need a traditional MIPS lazy-binding stub
401 (as opposed to a PLT entry)? */
402 unsigned int needs_lazy_stub : 1;
403 };
404
405 /* MIPS ELF linker hash table. */
406
407 struct mips_elf_link_hash_table
408 {
409 struct elf_link_hash_table root;
410 #if 0
411 /* We no longer use this. */
412 /* String section indices for the dynamic section symbols. */
413 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
414 #endif
415
416 /* The number of .rtproc entries. */
417 bfd_size_type procedure_count;
418
419 /* The size of the .compact_rel section (if SGI_COMPAT). */
420 bfd_size_type compact_rel_size;
421
422 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
423 entry is set to the address of __rld_obj_head as in IRIX5. */
424 bfd_boolean use_rld_obj_head;
425
426 /* This is the value of the __rld_map or __rld_obj_head symbol. */
427 bfd_vma rld_value;
428
429 /* This is set if we see any mips16 stub sections. */
430 bfd_boolean mips16_stubs_seen;
431
432 /* True if we can generate copy relocs and PLTs. */
433 bfd_boolean use_plts_and_copy_relocs;
434
435 /* True if we're generating code for VxWorks. */
436 bfd_boolean is_vxworks;
437
438 /* True if we already reported the small-data section overflow. */
439 bfd_boolean small_data_overflow_reported;
440
441 /* Shortcuts to some dynamic sections, or NULL if they are not
442 being used. */
443 asection *srelbss;
444 asection *sdynbss;
445 asection *srelplt;
446 asection *srelplt2;
447 asection *sgotplt;
448 asection *splt;
449 asection *sstubs;
450 asection *sgot;
451
452 /* The master GOT information. */
453 struct mips_got_info *got_info;
454
455 /* The size of the PLT header in bytes. */
456 bfd_vma plt_header_size;
457
458 /* The size of a PLT entry in bytes. */
459 bfd_vma plt_entry_size;
460
461 /* The number of functions that need a lazy-binding stub. */
462 bfd_vma lazy_stub_count;
463
464 /* The size of a function stub entry in bytes. */
465 bfd_vma function_stub_size;
466
467 /* The number of reserved entries at the beginning of the GOT. */
468 unsigned int reserved_gotno;
469
470 /* The section used for mips_elf_la25_stub trampolines.
471 See the comment above that structure for details. */
472 asection *strampoline;
473
474 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
475 pairs. */
476 htab_t la25_stubs;
477
478 /* A function FN (NAME, IS, OS) that creates a new input section
479 called NAME and links it to output section OS. If IS is nonnull,
480 the new section should go immediately before it, otherwise it
481 should go at the (current) beginning of OS.
482
483 The function returns the new section on success, otherwise it
484 returns null. */
485 asection *(*add_stub_section) (const char *, asection *, asection *);
486 };
487
488 /* Get the MIPS ELF linker hash table from a link_info structure. */
489
490 #define mips_elf_hash_table(p) \
491 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
492 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
493
494 /* A structure used to communicate with htab_traverse callbacks. */
495 struct mips_htab_traverse_info
496 {
497 /* The usual link-wide information. */
498 struct bfd_link_info *info;
499 bfd *output_bfd;
500
501 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
502 bfd_boolean error;
503 };
504
505 #define TLS_RELOC_P(r_type) \
506 (r_type == R_MIPS_TLS_DTPMOD32 \
507 || r_type == R_MIPS_TLS_DTPMOD64 \
508 || r_type == R_MIPS_TLS_DTPREL32 \
509 || r_type == R_MIPS_TLS_DTPREL64 \
510 || r_type == R_MIPS_TLS_GD \
511 || r_type == R_MIPS_TLS_LDM \
512 || r_type == R_MIPS_TLS_DTPREL_HI16 \
513 || r_type == R_MIPS_TLS_DTPREL_LO16 \
514 || r_type == R_MIPS_TLS_GOTTPREL \
515 || r_type == R_MIPS_TLS_TPREL32 \
516 || r_type == R_MIPS_TLS_TPREL64 \
517 || r_type == R_MIPS_TLS_TPREL_HI16 \
518 || r_type == R_MIPS_TLS_TPREL_LO16)
519
520 /* Structure used to pass information to mips_elf_output_extsym. */
521
522 struct extsym_info
523 {
524 bfd *abfd;
525 struct bfd_link_info *info;
526 struct ecoff_debug_info *debug;
527 const struct ecoff_debug_swap *swap;
528 bfd_boolean failed;
529 };
530
531 /* The names of the runtime procedure table symbols used on IRIX5. */
532
533 static const char * const mips_elf_dynsym_rtproc_names[] =
534 {
535 "_procedure_table",
536 "_procedure_string_table",
537 "_procedure_table_size",
538 NULL
539 };
540
541 /* These structures are used to generate the .compact_rel section on
542 IRIX5. */
543
544 typedef struct
545 {
546 unsigned long id1; /* Always one? */
547 unsigned long num; /* Number of compact relocation entries. */
548 unsigned long id2; /* Always two? */
549 unsigned long offset; /* The file offset of the first relocation. */
550 unsigned long reserved0; /* Zero? */
551 unsigned long reserved1; /* Zero? */
552 } Elf32_compact_rel;
553
554 typedef struct
555 {
556 bfd_byte id1[4];
557 bfd_byte num[4];
558 bfd_byte id2[4];
559 bfd_byte offset[4];
560 bfd_byte reserved0[4];
561 bfd_byte reserved1[4];
562 } Elf32_External_compact_rel;
563
564 typedef struct
565 {
566 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
567 unsigned int rtype : 4; /* Relocation types. See below. */
568 unsigned int dist2to : 8;
569 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
570 unsigned long konst; /* KONST field. See below. */
571 unsigned long vaddr; /* VADDR to be relocated. */
572 } Elf32_crinfo;
573
574 typedef struct
575 {
576 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
577 unsigned int rtype : 4; /* Relocation types. See below. */
578 unsigned int dist2to : 8;
579 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
580 unsigned long konst; /* KONST field. See below. */
581 } Elf32_crinfo2;
582
583 typedef struct
584 {
585 bfd_byte info[4];
586 bfd_byte konst[4];
587 bfd_byte vaddr[4];
588 } Elf32_External_crinfo;
589
590 typedef struct
591 {
592 bfd_byte info[4];
593 bfd_byte konst[4];
594 } Elf32_External_crinfo2;
595
596 /* These are the constants used to swap the bitfields in a crinfo. */
597
598 #define CRINFO_CTYPE (0x1)
599 #define CRINFO_CTYPE_SH (31)
600 #define CRINFO_RTYPE (0xf)
601 #define CRINFO_RTYPE_SH (27)
602 #define CRINFO_DIST2TO (0xff)
603 #define CRINFO_DIST2TO_SH (19)
604 #define CRINFO_RELVADDR (0x7ffff)
605 #define CRINFO_RELVADDR_SH (0)
606
607 /* A compact relocation info has long (3 words) or short (2 words)
608 formats. A short format doesn't have VADDR field and relvaddr
609 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
610 #define CRF_MIPS_LONG 1
611 #define CRF_MIPS_SHORT 0
612
613 /* There are 4 types of compact relocation at least. The value KONST
614 has different meaning for each type:
615
616 (type) (konst)
617 CT_MIPS_REL32 Address in data
618 CT_MIPS_WORD Address in word (XXX)
619 CT_MIPS_GPHI_LO GP - vaddr
620 CT_MIPS_JMPAD Address to jump
621 */
622
623 #define CRT_MIPS_REL32 0xa
624 #define CRT_MIPS_WORD 0xb
625 #define CRT_MIPS_GPHI_LO 0xc
626 #define CRT_MIPS_JMPAD 0xd
627
628 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
629 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
630 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
631 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
632 \f
633 /* The structure of the runtime procedure descriptor created by the
634 loader for use by the static exception system. */
635
636 typedef struct runtime_pdr {
637 bfd_vma adr; /* Memory address of start of procedure. */
638 long regmask; /* Save register mask. */
639 long regoffset; /* Save register offset. */
640 long fregmask; /* Save floating point register mask. */
641 long fregoffset; /* Save floating point register offset. */
642 long frameoffset; /* Frame size. */
643 short framereg; /* Frame pointer register. */
644 short pcreg; /* Offset or reg of return pc. */
645 long irpss; /* Index into the runtime string table. */
646 long reserved;
647 struct exception_info *exception_info;/* Pointer to exception array. */
648 } RPDR, *pRPDR;
649 #define cbRPDR sizeof (RPDR)
650 #define rpdNil ((pRPDR) 0)
651 \f
652 static struct mips_got_entry *mips_elf_create_local_got_entry
653 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
654 struct mips_elf_link_hash_entry *, int);
655 static bfd_boolean mips_elf_sort_hash_table_f
656 (struct mips_elf_link_hash_entry *, void *);
657 static bfd_vma mips_elf_high
658 (bfd_vma);
659 static bfd_boolean mips_elf_create_dynamic_relocation
660 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
661 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
662 bfd_vma *, asection *);
663 static hashval_t mips_elf_got_entry_hash
664 (const void *);
665 static bfd_vma mips_elf_adjust_gp
666 (bfd *, struct mips_got_info *, bfd *);
667 static struct mips_got_info *mips_elf_got_for_ibfd
668 (struct mips_got_info *, bfd *);
669
670 /* This will be used when we sort the dynamic relocation records. */
671 static bfd *reldyn_sorting_bfd;
672
673 /* True if ABFD is for CPUs with load interlocking that include
674 non-MIPS1 CPUs and R3900. */
675 #define LOAD_INTERLOCKS_P(abfd) \
676 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
677 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
678
679 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
680 This should be safe for all architectures. We enable this predicate
681 for RM9000 for now. */
682 #define JAL_TO_BAL_P(abfd) \
683 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
684
685 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
686 This should be safe for all architectures. We enable this predicate for
687 all CPUs. */
688 #define JALR_TO_BAL_P(abfd) 1
689
690 /* True if ABFD is for CPUs that are faster if JR is converted to B.
691 This should be safe for all architectures. We enable this predicate for
692 all CPUs. */
693 #define JR_TO_B_P(abfd) 1
694
695 /* True if ABFD is a PIC object. */
696 #define PIC_OBJECT_P(abfd) \
697 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
698
699 /* Nonzero if ABFD is using the N32 ABI. */
700 #define ABI_N32_P(abfd) \
701 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
702
703 /* Nonzero if ABFD is using the N64 ABI. */
704 #define ABI_64_P(abfd) \
705 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
706
707 /* Nonzero if ABFD is using NewABI conventions. */
708 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
709
710 /* The IRIX compatibility level we are striving for. */
711 #define IRIX_COMPAT(abfd) \
712 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
713
714 /* Whether we are trying to be compatible with IRIX at all. */
715 #define SGI_COMPAT(abfd) \
716 (IRIX_COMPAT (abfd) != ict_none)
717
718 /* The name of the options section. */
719 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
720 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
721
722 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
723 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
724 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
725 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
726
727 /* Whether the section is readonly. */
728 #define MIPS_ELF_READONLY_SECTION(sec) \
729 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
730 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
731
732 /* The name of the stub section. */
733 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
734
735 /* The size of an external REL relocation. */
736 #define MIPS_ELF_REL_SIZE(abfd) \
737 (get_elf_backend_data (abfd)->s->sizeof_rel)
738
739 /* The size of an external RELA relocation. */
740 #define MIPS_ELF_RELA_SIZE(abfd) \
741 (get_elf_backend_data (abfd)->s->sizeof_rela)
742
743 /* The size of an external dynamic table entry. */
744 #define MIPS_ELF_DYN_SIZE(abfd) \
745 (get_elf_backend_data (abfd)->s->sizeof_dyn)
746
747 /* The size of a GOT entry. */
748 #define MIPS_ELF_GOT_SIZE(abfd) \
749 (get_elf_backend_data (abfd)->s->arch_size / 8)
750
751 /* The size of a symbol-table entry. */
752 #define MIPS_ELF_SYM_SIZE(abfd) \
753 (get_elf_backend_data (abfd)->s->sizeof_sym)
754
755 /* The default alignment for sections, as a power of two. */
756 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
757 (get_elf_backend_data (abfd)->s->log_file_align)
758
759 /* Get word-sized data. */
760 #define MIPS_ELF_GET_WORD(abfd, ptr) \
761 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
762
763 /* Put out word-sized data. */
764 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
765 (ABI_64_P (abfd) \
766 ? bfd_put_64 (abfd, val, ptr) \
767 : bfd_put_32 (abfd, val, ptr))
768
769 /* The opcode for word-sized loads (LW or LD). */
770 #define MIPS_ELF_LOAD_WORD(abfd) \
771 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
772
773 /* Add a dynamic symbol table-entry. */
774 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
775 _bfd_elf_add_dynamic_entry (info, tag, val)
776
777 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
778 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
779
780 /* Determine whether the internal relocation of index REL_IDX is REL
781 (zero) or RELA (non-zero). The assumption is that, if there are
782 two relocation sections for this section, one of them is REL and
783 the other is RELA. If the index of the relocation we're testing is
784 in range for the first relocation section, check that the external
785 relocation size is that for RELA. It is also assumed that, if
786 rel_idx is not in range for the first section, and this first
787 section contains REL relocs, then the relocation is in the second
788 section, that is RELA. */
789 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
790 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
791 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
792 > (bfd_vma)(rel_idx)) \
793 == (elf_section_data (sec)->rel_hdr.sh_entsize \
794 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
795 : sizeof (Elf32_External_Rela))))
796
797 /* The name of the dynamic relocation section. */
798 #define MIPS_ELF_REL_DYN_NAME(INFO) \
799 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
800
801 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
802 from smaller values. Start with zero, widen, *then* decrement. */
803 #define MINUS_ONE (((bfd_vma)0) - 1)
804 #define MINUS_TWO (((bfd_vma)0) - 2)
805
806 /* The value to write into got[1] for SVR4 targets, to identify it is
807 a GNU object. The dynamic linker can then use got[1] to store the
808 module pointer. */
809 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
810 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
811
812 /* The offset of $gp from the beginning of the .got section. */
813 #define ELF_MIPS_GP_OFFSET(INFO) \
814 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
815
816 /* The maximum size of the GOT for it to be addressable using 16-bit
817 offsets from $gp. */
818 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
819
820 /* Instructions which appear in a stub. */
821 #define STUB_LW(abfd) \
822 ((ABI_64_P (abfd) \
823 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
824 : 0x8f998010)) /* lw t9,0x8010(gp) */
825 #define STUB_MOVE(abfd) \
826 ((ABI_64_P (abfd) \
827 ? 0x03e0782d /* daddu t7,ra */ \
828 : 0x03e07821)) /* addu t7,ra */
829 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
830 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
831 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
832 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
833 #define STUB_LI16S(abfd, VAL) \
834 ((ABI_64_P (abfd) \
835 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
836 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
837
838 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
839 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
840
841 /* The name of the dynamic interpreter. This is put in the .interp
842 section. */
843
844 #define ELF_DYNAMIC_INTERPRETER(abfd) \
845 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
846 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
847 : "/usr/lib/libc.so.1")
848
849 #ifdef BFD64
850 #define MNAME(bfd,pre,pos) \
851 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
852 #define ELF_R_SYM(bfd, i) \
853 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
854 #define ELF_R_TYPE(bfd, i) \
855 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
856 #define ELF_R_INFO(bfd, s, t) \
857 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
858 #else
859 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
860 #define ELF_R_SYM(bfd, i) \
861 (ELF32_R_SYM (i))
862 #define ELF_R_TYPE(bfd, i) \
863 (ELF32_R_TYPE (i))
864 #define ELF_R_INFO(bfd, s, t) \
865 (ELF32_R_INFO (s, t))
866 #endif
867 \f
868 /* The mips16 compiler uses a couple of special sections to handle
869 floating point arguments.
870
871 Section names that look like .mips16.fn.FNNAME contain stubs that
872 copy floating point arguments from the fp regs to the gp regs and
873 then jump to FNNAME. If any 32 bit function calls FNNAME, the
874 call should be redirected to the stub instead. If no 32 bit
875 function calls FNNAME, the stub should be discarded. We need to
876 consider any reference to the function, not just a call, because
877 if the address of the function is taken we will need the stub,
878 since the address might be passed to a 32 bit function.
879
880 Section names that look like .mips16.call.FNNAME contain stubs
881 that copy floating point arguments from the gp regs to the fp
882 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
883 then any 16 bit function that calls FNNAME should be redirected
884 to the stub instead. If FNNAME is not a 32 bit function, the
885 stub should be discarded.
886
887 .mips16.call.fp.FNNAME sections are similar, but contain stubs
888 which call FNNAME and then copy the return value from the fp regs
889 to the gp regs. These stubs store the return value in $18 while
890 calling FNNAME; any function which might call one of these stubs
891 must arrange to save $18 around the call. (This case is not
892 needed for 32 bit functions that call 16 bit functions, because
893 16 bit functions always return floating point values in both
894 $f0/$f1 and $2/$3.)
895
896 Note that in all cases FNNAME might be defined statically.
897 Therefore, FNNAME is not used literally. Instead, the relocation
898 information will indicate which symbol the section is for.
899
900 We record any stubs that we find in the symbol table. */
901
902 #define FN_STUB ".mips16.fn."
903 #define CALL_STUB ".mips16.call."
904 #define CALL_FP_STUB ".mips16.call.fp."
905
906 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
907 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
908 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
909 \f
910 /* The format of the first PLT entry in an O32 executable. */
911 static const bfd_vma mips_o32_exec_plt0_entry[] =
912 {
913 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
914 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
915 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
916 0x031cc023, /* subu $24, $24, $28 */
917 0x03e07821, /* move $15, $31 */
918 0x0018c082, /* srl $24, $24, 2 */
919 0x0320f809, /* jalr $25 */
920 0x2718fffe /* subu $24, $24, 2 */
921 };
922
923 /* The format of the first PLT entry in an N32 executable. Different
924 because gp ($28) is not available; we use t2 ($14) instead. */
925 static const bfd_vma mips_n32_exec_plt0_entry[] =
926 {
927 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
928 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
929 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
930 0x030ec023, /* subu $24, $24, $14 */
931 0x03e07821, /* move $15, $31 */
932 0x0018c082, /* srl $24, $24, 2 */
933 0x0320f809, /* jalr $25 */
934 0x2718fffe /* subu $24, $24, 2 */
935 };
936
937 /* The format of the first PLT entry in an N64 executable. Different
938 from N32 because of the increased size of GOT entries. */
939 static const bfd_vma mips_n64_exec_plt0_entry[] =
940 {
941 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
942 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
943 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
944 0x030ec023, /* subu $24, $24, $14 */
945 0x03e07821, /* move $15, $31 */
946 0x0018c0c2, /* srl $24, $24, 3 */
947 0x0320f809, /* jalr $25 */
948 0x2718fffe /* subu $24, $24, 2 */
949 };
950
951 /* The format of subsequent PLT entries. */
952 static const bfd_vma mips_exec_plt_entry[] =
953 {
954 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
955 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
956 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
957 0x03200008 /* jr $25 */
958 };
959
960 /* The format of the first PLT entry in a VxWorks executable. */
961 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
962 {
963 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
964 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
965 0x8f390008, /* lw t9, 8(t9) */
966 0x00000000, /* nop */
967 0x03200008, /* jr t9 */
968 0x00000000 /* nop */
969 };
970
971 /* The format of subsequent PLT entries. */
972 static const bfd_vma mips_vxworks_exec_plt_entry[] =
973 {
974 0x10000000, /* b .PLT_resolver */
975 0x24180000, /* li t8, <pltindex> */
976 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
977 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
978 0x8f390000, /* lw t9, 0(t9) */
979 0x00000000, /* nop */
980 0x03200008, /* jr t9 */
981 0x00000000 /* nop */
982 };
983
984 /* The format of the first PLT entry in a VxWorks shared object. */
985 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
986 {
987 0x8f990008, /* lw t9, 8(gp) */
988 0x00000000, /* nop */
989 0x03200008, /* jr t9 */
990 0x00000000, /* nop */
991 0x00000000, /* nop */
992 0x00000000 /* nop */
993 };
994
995 /* The format of subsequent PLT entries. */
996 static const bfd_vma mips_vxworks_shared_plt_entry[] =
997 {
998 0x10000000, /* b .PLT_resolver */
999 0x24180000 /* li t8, <pltindex> */
1000 };
1001 \f
1002 /* Look up an entry in a MIPS ELF linker hash table. */
1003
1004 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1005 ((struct mips_elf_link_hash_entry *) \
1006 elf_link_hash_lookup (&(table)->root, (string), (create), \
1007 (copy), (follow)))
1008
1009 /* Traverse a MIPS ELF linker hash table. */
1010
1011 #define mips_elf_link_hash_traverse(table, func, info) \
1012 (elf_link_hash_traverse \
1013 (&(table)->root, \
1014 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1015 (info)))
1016
1017 /* Find the base offsets for thread-local storage in this object,
1018 for GD/LD and IE/LE respectively. */
1019
1020 #define TP_OFFSET 0x7000
1021 #define DTP_OFFSET 0x8000
1022
1023 static bfd_vma
1024 dtprel_base (struct bfd_link_info *info)
1025 {
1026 /* If tls_sec is NULL, we should have signalled an error already. */
1027 if (elf_hash_table (info)->tls_sec == NULL)
1028 return 0;
1029 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1030 }
1031
1032 static bfd_vma
1033 tprel_base (struct bfd_link_info *info)
1034 {
1035 /* If tls_sec is NULL, we should have signalled an error already. */
1036 if (elf_hash_table (info)->tls_sec == NULL)
1037 return 0;
1038 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1039 }
1040
1041 /* Create an entry in a MIPS ELF linker hash table. */
1042
1043 static struct bfd_hash_entry *
1044 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1045 struct bfd_hash_table *table, const char *string)
1046 {
1047 struct mips_elf_link_hash_entry *ret =
1048 (struct mips_elf_link_hash_entry *) entry;
1049
1050 /* Allocate the structure if it has not already been allocated by a
1051 subclass. */
1052 if (ret == NULL)
1053 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1054 if (ret == NULL)
1055 return (struct bfd_hash_entry *) ret;
1056
1057 /* Call the allocation method of the superclass. */
1058 ret = ((struct mips_elf_link_hash_entry *)
1059 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1060 table, string));
1061 if (ret != NULL)
1062 {
1063 /* Set local fields. */
1064 memset (&ret->esym, 0, sizeof (EXTR));
1065 /* We use -2 as a marker to indicate that the information has
1066 not been set. -1 means there is no associated ifd. */
1067 ret->esym.ifd = -2;
1068 ret->la25_stub = 0;
1069 ret->possibly_dynamic_relocs = 0;
1070 ret->fn_stub = NULL;
1071 ret->call_stub = NULL;
1072 ret->call_fp_stub = NULL;
1073 ret->tls_type = GOT_NORMAL;
1074 ret->global_got_area = GGA_NONE;
1075 ret->readonly_reloc = FALSE;
1076 ret->has_static_relocs = FALSE;
1077 ret->no_fn_stub = FALSE;
1078 ret->need_fn_stub = FALSE;
1079 ret->has_nonpic_branches = FALSE;
1080 ret->needs_lazy_stub = FALSE;
1081 }
1082
1083 return (struct bfd_hash_entry *) ret;
1084 }
1085
1086 bfd_boolean
1087 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1088 {
1089 if (!sec->used_by_bfd)
1090 {
1091 struct _mips_elf_section_data *sdata;
1092 bfd_size_type amt = sizeof (*sdata);
1093
1094 sdata = bfd_zalloc (abfd, amt);
1095 if (sdata == NULL)
1096 return FALSE;
1097 sec->used_by_bfd = sdata;
1098 }
1099
1100 return _bfd_elf_new_section_hook (abfd, sec);
1101 }
1102 \f
1103 /* Read ECOFF debugging information from a .mdebug section into a
1104 ecoff_debug_info structure. */
1105
1106 bfd_boolean
1107 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1108 struct ecoff_debug_info *debug)
1109 {
1110 HDRR *symhdr;
1111 const struct ecoff_debug_swap *swap;
1112 char *ext_hdr;
1113
1114 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1115 memset (debug, 0, sizeof (*debug));
1116
1117 ext_hdr = bfd_malloc (swap->external_hdr_size);
1118 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1119 goto error_return;
1120
1121 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1122 swap->external_hdr_size))
1123 goto error_return;
1124
1125 symhdr = &debug->symbolic_header;
1126 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1127
1128 /* The symbolic header contains absolute file offsets and sizes to
1129 read. */
1130 #define READ(ptr, offset, count, size, type) \
1131 if (symhdr->count == 0) \
1132 debug->ptr = NULL; \
1133 else \
1134 { \
1135 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1136 debug->ptr = bfd_malloc (amt); \
1137 if (debug->ptr == NULL) \
1138 goto error_return; \
1139 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1140 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1141 goto error_return; \
1142 }
1143
1144 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1145 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1146 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1147 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1148 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1149 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1150 union aux_ext *);
1151 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1152 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1153 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1154 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1155 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1156 #undef READ
1157
1158 debug->fdr = NULL;
1159
1160 return TRUE;
1161
1162 error_return:
1163 if (ext_hdr != NULL)
1164 free (ext_hdr);
1165 if (debug->line != NULL)
1166 free (debug->line);
1167 if (debug->external_dnr != NULL)
1168 free (debug->external_dnr);
1169 if (debug->external_pdr != NULL)
1170 free (debug->external_pdr);
1171 if (debug->external_sym != NULL)
1172 free (debug->external_sym);
1173 if (debug->external_opt != NULL)
1174 free (debug->external_opt);
1175 if (debug->external_aux != NULL)
1176 free (debug->external_aux);
1177 if (debug->ss != NULL)
1178 free (debug->ss);
1179 if (debug->ssext != NULL)
1180 free (debug->ssext);
1181 if (debug->external_fdr != NULL)
1182 free (debug->external_fdr);
1183 if (debug->external_rfd != NULL)
1184 free (debug->external_rfd);
1185 if (debug->external_ext != NULL)
1186 free (debug->external_ext);
1187 return FALSE;
1188 }
1189 \f
1190 /* Swap RPDR (runtime procedure table entry) for output. */
1191
1192 static void
1193 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1194 {
1195 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1196 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1197 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1198 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1199 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1200 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1201
1202 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1203 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1204
1205 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1206 }
1207
1208 /* Create a runtime procedure table from the .mdebug section. */
1209
1210 static bfd_boolean
1211 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1212 struct bfd_link_info *info, asection *s,
1213 struct ecoff_debug_info *debug)
1214 {
1215 const struct ecoff_debug_swap *swap;
1216 HDRR *hdr = &debug->symbolic_header;
1217 RPDR *rpdr, *rp;
1218 struct rpdr_ext *erp;
1219 void *rtproc;
1220 struct pdr_ext *epdr;
1221 struct sym_ext *esym;
1222 char *ss, **sv;
1223 char *str;
1224 bfd_size_type size;
1225 bfd_size_type count;
1226 unsigned long sindex;
1227 unsigned long i;
1228 PDR pdr;
1229 SYMR sym;
1230 const char *no_name_func = _("static procedure (no name)");
1231
1232 epdr = NULL;
1233 rpdr = NULL;
1234 esym = NULL;
1235 ss = NULL;
1236 sv = NULL;
1237
1238 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1239
1240 sindex = strlen (no_name_func) + 1;
1241 count = hdr->ipdMax;
1242 if (count > 0)
1243 {
1244 size = swap->external_pdr_size;
1245
1246 epdr = bfd_malloc (size * count);
1247 if (epdr == NULL)
1248 goto error_return;
1249
1250 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1251 goto error_return;
1252
1253 size = sizeof (RPDR);
1254 rp = rpdr = bfd_malloc (size * count);
1255 if (rpdr == NULL)
1256 goto error_return;
1257
1258 size = sizeof (char *);
1259 sv = bfd_malloc (size * count);
1260 if (sv == NULL)
1261 goto error_return;
1262
1263 count = hdr->isymMax;
1264 size = swap->external_sym_size;
1265 esym = bfd_malloc (size * count);
1266 if (esym == NULL)
1267 goto error_return;
1268
1269 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1270 goto error_return;
1271
1272 count = hdr->issMax;
1273 ss = bfd_malloc (count);
1274 if (ss == NULL)
1275 goto error_return;
1276 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1277 goto error_return;
1278
1279 count = hdr->ipdMax;
1280 for (i = 0; i < (unsigned long) count; i++, rp++)
1281 {
1282 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1283 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1284 rp->adr = sym.value;
1285 rp->regmask = pdr.regmask;
1286 rp->regoffset = pdr.regoffset;
1287 rp->fregmask = pdr.fregmask;
1288 rp->fregoffset = pdr.fregoffset;
1289 rp->frameoffset = pdr.frameoffset;
1290 rp->framereg = pdr.framereg;
1291 rp->pcreg = pdr.pcreg;
1292 rp->irpss = sindex;
1293 sv[i] = ss + sym.iss;
1294 sindex += strlen (sv[i]) + 1;
1295 }
1296 }
1297
1298 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1299 size = BFD_ALIGN (size, 16);
1300 rtproc = bfd_alloc (abfd, size);
1301 if (rtproc == NULL)
1302 {
1303 mips_elf_hash_table (info)->procedure_count = 0;
1304 goto error_return;
1305 }
1306
1307 mips_elf_hash_table (info)->procedure_count = count + 2;
1308
1309 erp = rtproc;
1310 memset (erp, 0, sizeof (struct rpdr_ext));
1311 erp++;
1312 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1313 strcpy (str, no_name_func);
1314 str += strlen (no_name_func) + 1;
1315 for (i = 0; i < count; i++)
1316 {
1317 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1318 strcpy (str, sv[i]);
1319 str += strlen (sv[i]) + 1;
1320 }
1321 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1322
1323 /* Set the size and contents of .rtproc section. */
1324 s->size = size;
1325 s->contents = rtproc;
1326
1327 /* Skip this section later on (I don't think this currently
1328 matters, but someday it might). */
1329 s->map_head.link_order = NULL;
1330
1331 if (epdr != NULL)
1332 free (epdr);
1333 if (rpdr != NULL)
1334 free (rpdr);
1335 if (esym != NULL)
1336 free (esym);
1337 if (ss != NULL)
1338 free (ss);
1339 if (sv != NULL)
1340 free (sv);
1341
1342 return TRUE;
1343
1344 error_return:
1345 if (epdr != NULL)
1346 free (epdr);
1347 if (rpdr != NULL)
1348 free (rpdr);
1349 if (esym != NULL)
1350 free (esym);
1351 if (ss != NULL)
1352 free (ss);
1353 if (sv != NULL)
1354 free (sv);
1355 return FALSE;
1356 }
1357 \f
1358 /* We're going to create a stub for H. Create a symbol for the stub's
1359 value and size, to help make the disassembly easier to read. */
1360
1361 static bfd_boolean
1362 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1363 struct mips_elf_link_hash_entry *h,
1364 const char *prefix, asection *s, bfd_vma value,
1365 bfd_vma size)
1366 {
1367 struct bfd_link_hash_entry *bh;
1368 struct elf_link_hash_entry *elfh;
1369 const char *name;
1370
1371 /* Create a new symbol. */
1372 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1373 bh = NULL;
1374 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1375 BSF_LOCAL, s, value, NULL,
1376 TRUE, FALSE, &bh))
1377 return FALSE;
1378
1379 /* Make it a local function. */
1380 elfh = (struct elf_link_hash_entry *) bh;
1381 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1382 elfh->size = size;
1383 elfh->forced_local = 1;
1384 return TRUE;
1385 }
1386
1387 /* We're about to redefine H. Create a symbol to represent H's
1388 current value and size, to help make the disassembly easier
1389 to read. */
1390
1391 static bfd_boolean
1392 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1393 struct mips_elf_link_hash_entry *h,
1394 const char *prefix)
1395 {
1396 struct bfd_link_hash_entry *bh;
1397 struct elf_link_hash_entry *elfh;
1398 const char *name;
1399 asection *s;
1400 bfd_vma value;
1401
1402 /* Read the symbol's value. */
1403 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1404 || h->root.root.type == bfd_link_hash_defweak);
1405 s = h->root.root.u.def.section;
1406 value = h->root.root.u.def.value;
1407
1408 /* Create a new symbol. */
1409 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1410 bh = NULL;
1411 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1412 BSF_LOCAL, s, value, NULL,
1413 TRUE, FALSE, &bh))
1414 return FALSE;
1415
1416 /* Make it local and copy the other attributes from H. */
1417 elfh = (struct elf_link_hash_entry *) bh;
1418 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1419 elfh->other = h->root.other;
1420 elfh->size = h->root.size;
1421 elfh->forced_local = 1;
1422 return TRUE;
1423 }
1424
1425 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1426 function rather than to a hard-float stub. */
1427
1428 static bfd_boolean
1429 section_allows_mips16_refs_p (asection *section)
1430 {
1431 const char *name;
1432
1433 name = bfd_get_section_name (section->owner, section);
1434 return (FN_STUB_P (name)
1435 || CALL_STUB_P (name)
1436 || CALL_FP_STUB_P (name)
1437 || strcmp (name, ".pdr") == 0);
1438 }
1439
1440 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1441 stub section of some kind. Return the R_SYMNDX of the target
1442 function, or 0 if we can't decide which function that is. */
1443
1444 static unsigned long
1445 mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1446 const Elf_Internal_Rela *relocs,
1447 const Elf_Internal_Rela *relend)
1448 {
1449 const Elf_Internal_Rela *rel;
1450
1451 /* Trust the first R_MIPS_NONE relocation, if any. */
1452 for (rel = relocs; rel < relend; rel++)
1453 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1454 return ELF_R_SYM (sec->owner, rel->r_info);
1455
1456 /* Otherwise trust the first relocation, whatever its kind. This is
1457 the traditional behavior. */
1458 if (relocs < relend)
1459 return ELF_R_SYM (sec->owner, relocs->r_info);
1460
1461 return 0;
1462 }
1463
1464 /* Check the mips16 stubs for a particular symbol, and see if we can
1465 discard them. */
1466
1467 static void
1468 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1469 struct mips_elf_link_hash_entry *h)
1470 {
1471 /* Dynamic symbols must use the standard call interface, in case other
1472 objects try to call them. */
1473 if (h->fn_stub != NULL
1474 && h->root.dynindx != -1)
1475 {
1476 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1477 h->need_fn_stub = TRUE;
1478 }
1479
1480 if (h->fn_stub != NULL
1481 && ! h->need_fn_stub)
1482 {
1483 /* We don't need the fn_stub; the only references to this symbol
1484 are 16 bit calls. Clobber the size to 0 to prevent it from
1485 being included in the link. */
1486 h->fn_stub->size = 0;
1487 h->fn_stub->flags &= ~SEC_RELOC;
1488 h->fn_stub->reloc_count = 0;
1489 h->fn_stub->flags |= SEC_EXCLUDE;
1490 }
1491
1492 if (h->call_stub != NULL
1493 && ELF_ST_IS_MIPS16 (h->root.other))
1494 {
1495 /* We don't need the call_stub; this is a 16 bit function, so
1496 calls from other 16 bit functions are OK. Clobber the size
1497 to 0 to prevent it from being included in the link. */
1498 h->call_stub->size = 0;
1499 h->call_stub->flags &= ~SEC_RELOC;
1500 h->call_stub->reloc_count = 0;
1501 h->call_stub->flags |= SEC_EXCLUDE;
1502 }
1503
1504 if (h->call_fp_stub != NULL
1505 && ELF_ST_IS_MIPS16 (h->root.other))
1506 {
1507 /* We don't need the call_stub; this is a 16 bit function, so
1508 calls from other 16 bit functions are OK. Clobber the size
1509 to 0 to prevent it from being included in the link. */
1510 h->call_fp_stub->size = 0;
1511 h->call_fp_stub->flags &= ~SEC_RELOC;
1512 h->call_fp_stub->reloc_count = 0;
1513 h->call_fp_stub->flags |= SEC_EXCLUDE;
1514 }
1515 }
1516
1517 /* Hashtable callbacks for mips_elf_la25_stubs. */
1518
1519 static hashval_t
1520 mips_elf_la25_stub_hash (const void *entry_)
1521 {
1522 const struct mips_elf_la25_stub *entry;
1523
1524 entry = (struct mips_elf_la25_stub *) entry_;
1525 return entry->h->root.root.u.def.section->id
1526 + entry->h->root.root.u.def.value;
1527 }
1528
1529 static int
1530 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1531 {
1532 const struct mips_elf_la25_stub *entry1, *entry2;
1533
1534 entry1 = (struct mips_elf_la25_stub *) entry1_;
1535 entry2 = (struct mips_elf_la25_stub *) entry2_;
1536 return ((entry1->h->root.root.u.def.section
1537 == entry2->h->root.root.u.def.section)
1538 && (entry1->h->root.root.u.def.value
1539 == entry2->h->root.root.u.def.value));
1540 }
1541
1542 /* Called by the linker to set up the la25 stub-creation code. FN is
1543 the linker's implementation of add_stub_function. Return true on
1544 success. */
1545
1546 bfd_boolean
1547 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1548 asection *(*fn) (const char *, asection *,
1549 asection *))
1550 {
1551 struct mips_elf_link_hash_table *htab;
1552
1553 htab = mips_elf_hash_table (info);
1554 if (htab == NULL)
1555 return FALSE;
1556
1557 htab->add_stub_section = fn;
1558 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1559 mips_elf_la25_stub_eq, NULL);
1560 if (htab->la25_stubs == NULL)
1561 return FALSE;
1562
1563 return TRUE;
1564 }
1565
1566 /* Return true if H is a locally-defined PIC function, in the sense
1567 that it might need $25 to be valid on entry. Note that MIPS16
1568 functions never need $25 to be valid on entry; they set up $gp
1569 using PC-relative instructions instead. */
1570
1571 static bfd_boolean
1572 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1573 {
1574 return ((h->root.root.type == bfd_link_hash_defined
1575 || h->root.root.type == bfd_link_hash_defweak)
1576 && h->root.def_regular
1577 && !bfd_is_abs_section (h->root.root.u.def.section)
1578 && !ELF_ST_IS_MIPS16 (h->root.other)
1579 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1580 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1581 }
1582
1583 /* STUB describes an la25 stub that we have decided to implement
1584 by inserting an LUI/ADDIU pair before the target function.
1585 Create the section and redirect the function symbol to it. */
1586
1587 static bfd_boolean
1588 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1589 struct bfd_link_info *info)
1590 {
1591 struct mips_elf_link_hash_table *htab;
1592 char *name;
1593 asection *s, *input_section;
1594 unsigned int align;
1595
1596 htab = mips_elf_hash_table (info);
1597 if (htab == NULL)
1598 return FALSE;
1599
1600 /* Create a unique name for the new section. */
1601 name = bfd_malloc (11 + sizeof (".text.stub."));
1602 if (name == NULL)
1603 return FALSE;
1604 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1605
1606 /* Create the section. */
1607 input_section = stub->h->root.root.u.def.section;
1608 s = htab->add_stub_section (name, input_section,
1609 input_section->output_section);
1610 if (s == NULL)
1611 return FALSE;
1612
1613 /* Make sure that any padding goes before the stub. */
1614 align = input_section->alignment_power;
1615 if (!bfd_set_section_alignment (s->owner, s, align))
1616 return FALSE;
1617 if (align > 3)
1618 s->size = (1 << align) - 8;
1619
1620 /* Create a symbol for the stub. */
1621 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1622 stub->stub_section = s;
1623 stub->offset = s->size;
1624
1625 /* Allocate room for it. */
1626 s->size += 8;
1627 return TRUE;
1628 }
1629
1630 /* STUB describes an la25 stub that we have decided to implement
1631 with a separate trampoline. Allocate room for it and redirect
1632 the function symbol to it. */
1633
1634 static bfd_boolean
1635 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1636 struct bfd_link_info *info)
1637 {
1638 struct mips_elf_link_hash_table *htab;
1639 asection *s;
1640
1641 htab = mips_elf_hash_table (info);
1642 if (htab == NULL)
1643 return FALSE;
1644
1645 /* Create a trampoline section, if we haven't already. */
1646 s = htab->strampoline;
1647 if (s == NULL)
1648 {
1649 asection *input_section = stub->h->root.root.u.def.section;
1650 s = htab->add_stub_section (".text", NULL,
1651 input_section->output_section);
1652 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1653 return FALSE;
1654 htab->strampoline = s;
1655 }
1656
1657 /* Create a symbol for the stub. */
1658 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1659 stub->stub_section = s;
1660 stub->offset = s->size;
1661
1662 /* Allocate room for it. */
1663 s->size += 16;
1664 return TRUE;
1665 }
1666
1667 /* H describes a symbol that needs an la25 stub. Make sure that an
1668 appropriate stub exists and point H at it. */
1669
1670 static bfd_boolean
1671 mips_elf_add_la25_stub (struct bfd_link_info *info,
1672 struct mips_elf_link_hash_entry *h)
1673 {
1674 struct mips_elf_link_hash_table *htab;
1675 struct mips_elf_la25_stub search, *stub;
1676 bfd_boolean use_trampoline_p;
1677 asection *s;
1678 bfd_vma value;
1679 void **slot;
1680
1681 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1682 of the section and if we would need no more than 2 nops. */
1683 s = h->root.root.u.def.section;
1684 value = h->root.root.u.def.value;
1685 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1686
1687 /* Describe the stub we want. */
1688 search.stub_section = NULL;
1689 search.offset = 0;
1690 search.h = h;
1691
1692 /* See if we've already created an equivalent stub. */
1693 htab = mips_elf_hash_table (info);
1694 if (htab == NULL)
1695 return FALSE;
1696
1697 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1698 if (slot == NULL)
1699 return FALSE;
1700
1701 stub = (struct mips_elf_la25_stub *) *slot;
1702 if (stub != NULL)
1703 {
1704 /* We can reuse the existing stub. */
1705 h->la25_stub = stub;
1706 return TRUE;
1707 }
1708
1709 /* Create a permanent copy of ENTRY and add it to the hash table. */
1710 stub = bfd_malloc (sizeof (search));
1711 if (stub == NULL)
1712 return FALSE;
1713 *stub = search;
1714 *slot = stub;
1715
1716 h->la25_stub = stub;
1717 return (use_trampoline_p
1718 ? mips_elf_add_la25_trampoline (stub, info)
1719 : mips_elf_add_la25_intro (stub, info));
1720 }
1721
1722 /* A mips_elf_link_hash_traverse callback that is called before sizing
1723 sections. DATA points to a mips_htab_traverse_info structure. */
1724
1725 static bfd_boolean
1726 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1727 {
1728 struct mips_htab_traverse_info *hti;
1729
1730 hti = (struct mips_htab_traverse_info *) data;
1731 if (h->root.root.type == bfd_link_hash_warning)
1732 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1733
1734 if (!hti->info->relocatable)
1735 mips_elf_check_mips16_stubs (hti->info, h);
1736
1737 if (mips_elf_local_pic_function_p (h))
1738 {
1739 /* H is a function that might need $25 to be valid on entry.
1740 If we're creating a non-PIC relocatable object, mark H as
1741 being PIC. If we're creating a non-relocatable object with
1742 non-PIC branches and jumps to H, make sure that H has an la25
1743 stub. */
1744 if (hti->info->relocatable)
1745 {
1746 if (!PIC_OBJECT_P (hti->output_bfd))
1747 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1748 }
1749 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1750 {
1751 hti->error = TRUE;
1752 return FALSE;
1753 }
1754 }
1755 return TRUE;
1756 }
1757 \f
1758 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1759 Most mips16 instructions are 16 bits, but these instructions
1760 are 32 bits.
1761
1762 The format of these instructions is:
1763
1764 +--------------+--------------------------------+
1765 | JALX | X| Imm 20:16 | Imm 25:21 |
1766 +--------------+--------------------------------+
1767 | Immediate 15:0 |
1768 +-----------------------------------------------+
1769
1770 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1771 Note that the immediate value in the first word is swapped.
1772
1773 When producing a relocatable object file, R_MIPS16_26 is
1774 handled mostly like R_MIPS_26. In particular, the addend is
1775 stored as a straight 26-bit value in a 32-bit instruction.
1776 (gas makes life simpler for itself by never adjusting a
1777 R_MIPS16_26 reloc to be against a section, so the addend is
1778 always zero). However, the 32 bit instruction is stored as 2
1779 16-bit values, rather than a single 32-bit value. In a
1780 big-endian file, the result is the same; in a little-endian
1781 file, the two 16-bit halves of the 32 bit value are swapped.
1782 This is so that a disassembler can recognize the jal
1783 instruction.
1784
1785 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1786 instruction stored as two 16-bit values. The addend A is the
1787 contents of the targ26 field. The calculation is the same as
1788 R_MIPS_26. When storing the calculated value, reorder the
1789 immediate value as shown above, and don't forget to store the
1790 value as two 16-bit values.
1791
1792 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1793 defined as
1794
1795 big-endian:
1796 +--------+----------------------+
1797 | | |
1798 | | targ26-16 |
1799 |31 26|25 0|
1800 +--------+----------------------+
1801
1802 little-endian:
1803 +----------+------+-------------+
1804 | | | |
1805 | sub1 | | sub2 |
1806 |0 9|10 15|16 31|
1807 +----------+--------------------+
1808 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1809 ((sub1 << 16) | sub2)).
1810
1811 When producing a relocatable object file, the calculation is
1812 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1813 When producing a fully linked file, the calculation is
1814 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1815 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1816
1817 The table below lists the other MIPS16 instruction relocations.
1818 Each one is calculated in the same way as the non-MIPS16 relocation
1819 given on the right, but using the extended MIPS16 layout of 16-bit
1820 immediate fields:
1821
1822 R_MIPS16_GPREL R_MIPS_GPREL16
1823 R_MIPS16_GOT16 R_MIPS_GOT16
1824 R_MIPS16_CALL16 R_MIPS_CALL16
1825 R_MIPS16_HI16 R_MIPS_HI16
1826 R_MIPS16_LO16 R_MIPS_LO16
1827
1828 A typical instruction will have a format like this:
1829
1830 +--------------+--------------------------------+
1831 | EXTEND | Imm 10:5 | Imm 15:11 |
1832 +--------------+--------------------------------+
1833 | Major | rx | ry | Imm 4:0 |
1834 +--------------+--------------------------------+
1835
1836 EXTEND is the five bit value 11110. Major is the instruction
1837 opcode.
1838
1839 All we need to do here is shuffle the bits appropriately.
1840 As above, the two 16-bit halves must be swapped on a
1841 little-endian system. */
1842
1843 static inline bfd_boolean
1844 mips16_reloc_p (int r_type)
1845 {
1846 switch (r_type)
1847 {
1848 case R_MIPS16_26:
1849 case R_MIPS16_GPREL:
1850 case R_MIPS16_GOT16:
1851 case R_MIPS16_CALL16:
1852 case R_MIPS16_HI16:
1853 case R_MIPS16_LO16:
1854 return TRUE;
1855
1856 default:
1857 return FALSE;
1858 }
1859 }
1860
1861 static inline bfd_boolean
1862 got16_reloc_p (int r_type)
1863 {
1864 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1865 }
1866
1867 static inline bfd_boolean
1868 call16_reloc_p (int r_type)
1869 {
1870 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1871 }
1872
1873 static inline bfd_boolean
1874 hi16_reloc_p (int r_type)
1875 {
1876 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1877 }
1878
1879 static inline bfd_boolean
1880 lo16_reloc_p (int r_type)
1881 {
1882 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1883 }
1884
1885 static inline bfd_boolean
1886 mips16_call_reloc_p (int r_type)
1887 {
1888 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1889 }
1890
1891 static inline bfd_boolean
1892 jal_reloc_p (int r_type)
1893 {
1894 return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1895 }
1896
1897 void
1898 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1899 bfd_boolean jal_shuffle, bfd_byte *data)
1900 {
1901 bfd_vma extend, insn, val;
1902
1903 if (!mips16_reloc_p (r_type))
1904 return;
1905
1906 /* Pick up the mips16 extend instruction and the real instruction. */
1907 extend = bfd_get_16 (abfd, data);
1908 insn = bfd_get_16 (abfd, data + 2);
1909 if (r_type == R_MIPS16_26)
1910 {
1911 if (jal_shuffle)
1912 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1913 | ((extend & 0x1f) << 21) | insn;
1914 else
1915 val = extend << 16 | insn;
1916 }
1917 else
1918 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1919 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1920 bfd_put_32 (abfd, val, data);
1921 }
1922
1923 void
1924 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1925 bfd_boolean jal_shuffle, bfd_byte *data)
1926 {
1927 bfd_vma extend, insn, val;
1928
1929 if (!mips16_reloc_p (r_type))
1930 return;
1931
1932 val = bfd_get_32 (abfd, data);
1933 if (r_type == R_MIPS16_26)
1934 {
1935 if (jal_shuffle)
1936 {
1937 insn = val & 0xffff;
1938 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1939 | ((val >> 21) & 0x1f);
1940 }
1941 else
1942 {
1943 insn = val & 0xffff;
1944 extend = val >> 16;
1945 }
1946 }
1947 else
1948 {
1949 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1950 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1951 }
1952 bfd_put_16 (abfd, insn, data + 2);
1953 bfd_put_16 (abfd, extend, data);
1954 }
1955
1956 bfd_reloc_status_type
1957 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1958 arelent *reloc_entry, asection *input_section,
1959 bfd_boolean relocatable, void *data, bfd_vma gp)
1960 {
1961 bfd_vma relocation;
1962 bfd_signed_vma val;
1963 bfd_reloc_status_type status;
1964
1965 if (bfd_is_com_section (symbol->section))
1966 relocation = 0;
1967 else
1968 relocation = symbol->value;
1969
1970 relocation += symbol->section->output_section->vma;
1971 relocation += symbol->section->output_offset;
1972
1973 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1974 return bfd_reloc_outofrange;
1975
1976 /* Set val to the offset into the section or symbol. */
1977 val = reloc_entry->addend;
1978
1979 _bfd_mips_elf_sign_extend (val, 16);
1980
1981 /* Adjust val for the final section location and GP value. If we
1982 are producing relocatable output, we don't want to do this for
1983 an external symbol. */
1984 if (! relocatable
1985 || (symbol->flags & BSF_SECTION_SYM) != 0)
1986 val += relocation - gp;
1987
1988 if (reloc_entry->howto->partial_inplace)
1989 {
1990 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1991 (bfd_byte *) data
1992 + reloc_entry->address);
1993 if (status != bfd_reloc_ok)
1994 return status;
1995 }
1996 else
1997 reloc_entry->addend = val;
1998
1999 if (relocatable)
2000 reloc_entry->address += input_section->output_offset;
2001
2002 return bfd_reloc_ok;
2003 }
2004
2005 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2006 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2007 that contains the relocation field and DATA points to the start of
2008 INPUT_SECTION. */
2009
2010 struct mips_hi16
2011 {
2012 struct mips_hi16 *next;
2013 bfd_byte *data;
2014 asection *input_section;
2015 arelent rel;
2016 };
2017
2018 /* FIXME: This should not be a static variable. */
2019
2020 static struct mips_hi16 *mips_hi16_list;
2021
2022 /* A howto special_function for REL *HI16 relocations. We can only
2023 calculate the correct value once we've seen the partnering
2024 *LO16 relocation, so just save the information for later.
2025
2026 The ABI requires that the *LO16 immediately follow the *HI16.
2027 However, as a GNU extension, we permit an arbitrary number of
2028 *HI16s to be associated with a single *LO16. This significantly
2029 simplies the relocation handling in gcc. */
2030
2031 bfd_reloc_status_type
2032 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2033 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2034 asection *input_section, bfd *output_bfd,
2035 char **error_message ATTRIBUTE_UNUSED)
2036 {
2037 struct mips_hi16 *n;
2038
2039 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2040 return bfd_reloc_outofrange;
2041
2042 n = bfd_malloc (sizeof *n);
2043 if (n == NULL)
2044 return bfd_reloc_outofrange;
2045
2046 n->next = mips_hi16_list;
2047 n->data = data;
2048 n->input_section = input_section;
2049 n->rel = *reloc_entry;
2050 mips_hi16_list = n;
2051
2052 if (output_bfd != NULL)
2053 reloc_entry->address += input_section->output_offset;
2054
2055 return bfd_reloc_ok;
2056 }
2057
2058 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2059 like any other 16-bit relocation when applied to global symbols, but is
2060 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2061
2062 bfd_reloc_status_type
2063 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2064 void *data, asection *input_section,
2065 bfd *output_bfd, char **error_message)
2066 {
2067 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2068 || bfd_is_und_section (bfd_get_section (symbol))
2069 || bfd_is_com_section (bfd_get_section (symbol)))
2070 /* The relocation is against a global symbol. */
2071 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2072 input_section, output_bfd,
2073 error_message);
2074
2075 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2076 input_section, output_bfd, error_message);
2077 }
2078
2079 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2080 is a straightforward 16 bit inplace relocation, but we must deal with
2081 any partnering high-part relocations as well. */
2082
2083 bfd_reloc_status_type
2084 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2085 void *data, asection *input_section,
2086 bfd *output_bfd, char **error_message)
2087 {
2088 bfd_vma vallo;
2089 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2090
2091 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2092 return bfd_reloc_outofrange;
2093
2094 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2095 location);
2096 vallo = bfd_get_32 (abfd, location);
2097 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2098 location);
2099
2100 while (mips_hi16_list != NULL)
2101 {
2102 bfd_reloc_status_type ret;
2103 struct mips_hi16 *hi;
2104
2105 hi = mips_hi16_list;
2106
2107 /* R_MIPS*_GOT16 relocations are something of a special case. We
2108 want to install the addend in the same way as for a R_MIPS*_HI16
2109 relocation (with a rightshift of 16). However, since GOT16
2110 relocations can also be used with global symbols, their howto
2111 has a rightshift of 0. */
2112 if (hi->rel.howto->type == R_MIPS_GOT16)
2113 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2114 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2115 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2116
2117 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2118 carry or borrow will induce a change of +1 or -1 in the high part. */
2119 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2120
2121 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2122 hi->input_section, output_bfd,
2123 error_message);
2124 if (ret != bfd_reloc_ok)
2125 return ret;
2126
2127 mips_hi16_list = hi->next;
2128 free (hi);
2129 }
2130
2131 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2132 input_section, output_bfd,
2133 error_message);
2134 }
2135
2136 /* A generic howto special_function. This calculates and installs the
2137 relocation itself, thus avoiding the oft-discussed problems in
2138 bfd_perform_relocation and bfd_install_relocation. */
2139
2140 bfd_reloc_status_type
2141 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2142 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2143 asection *input_section, bfd *output_bfd,
2144 char **error_message ATTRIBUTE_UNUSED)
2145 {
2146 bfd_signed_vma val;
2147 bfd_reloc_status_type status;
2148 bfd_boolean relocatable;
2149
2150 relocatable = (output_bfd != NULL);
2151
2152 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2153 return bfd_reloc_outofrange;
2154
2155 /* Build up the field adjustment in VAL. */
2156 val = 0;
2157 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2158 {
2159 /* Either we're calculating the final field value or we have a
2160 relocation against a section symbol. Add in the section's
2161 offset or address. */
2162 val += symbol->section->output_section->vma;
2163 val += symbol->section->output_offset;
2164 }
2165
2166 if (!relocatable)
2167 {
2168 /* We're calculating the final field value. Add in the symbol's value
2169 and, if pc-relative, subtract the address of the field itself. */
2170 val += symbol->value;
2171 if (reloc_entry->howto->pc_relative)
2172 {
2173 val -= input_section->output_section->vma;
2174 val -= input_section->output_offset;
2175 val -= reloc_entry->address;
2176 }
2177 }
2178
2179 /* VAL is now the final adjustment. If we're keeping this relocation
2180 in the output file, and if the relocation uses a separate addend,
2181 we just need to add VAL to that addend. Otherwise we need to add
2182 VAL to the relocation field itself. */
2183 if (relocatable && !reloc_entry->howto->partial_inplace)
2184 reloc_entry->addend += val;
2185 else
2186 {
2187 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2188
2189 /* Add in the separate addend, if any. */
2190 val += reloc_entry->addend;
2191
2192 /* Add VAL to the relocation field. */
2193 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2194 location);
2195 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2196 location);
2197 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2198 location);
2199
2200 if (status != bfd_reloc_ok)
2201 return status;
2202 }
2203
2204 if (relocatable)
2205 reloc_entry->address += input_section->output_offset;
2206
2207 return bfd_reloc_ok;
2208 }
2209 \f
2210 /* Swap an entry in a .gptab section. Note that these routines rely
2211 on the equivalence of the two elements of the union. */
2212
2213 static void
2214 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2215 Elf32_gptab *in)
2216 {
2217 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2218 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2219 }
2220
2221 static void
2222 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2223 Elf32_External_gptab *ex)
2224 {
2225 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2226 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2227 }
2228
2229 static void
2230 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2231 Elf32_External_compact_rel *ex)
2232 {
2233 H_PUT_32 (abfd, in->id1, ex->id1);
2234 H_PUT_32 (abfd, in->num, ex->num);
2235 H_PUT_32 (abfd, in->id2, ex->id2);
2236 H_PUT_32 (abfd, in->offset, ex->offset);
2237 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2238 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2239 }
2240
2241 static void
2242 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2243 Elf32_External_crinfo *ex)
2244 {
2245 unsigned long l;
2246
2247 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2248 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2249 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2250 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2251 H_PUT_32 (abfd, l, ex->info);
2252 H_PUT_32 (abfd, in->konst, ex->konst);
2253 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2254 }
2255 \f
2256 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2257 routines swap this structure in and out. They are used outside of
2258 BFD, so they are globally visible. */
2259
2260 void
2261 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2262 Elf32_RegInfo *in)
2263 {
2264 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2265 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2266 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2267 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2268 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2269 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2270 }
2271
2272 void
2273 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2274 Elf32_External_RegInfo *ex)
2275 {
2276 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2277 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2278 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2279 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2280 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2281 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2282 }
2283
2284 /* In the 64 bit ABI, the .MIPS.options section holds register
2285 information in an Elf64_Reginfo structure. These routines swap
2286 them in and out. They are globally visible because they are used
2287 outside of BFD. These routines are here so that gas can call them
2288 without worrying about whether the 64 bit ABI has been included. */
2289
2290 void
2291 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2292 Elf64_Internal_RegInfo *in)
2293 {
2294 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2295 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2296 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2297 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2298 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2299 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2300 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2301 }
2302
2303 void
2304 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2305 Elf64_External_RegInfo *ex)
2306 {
2307 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2308 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2309 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2310 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2311 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2312 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2313 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2314 }
2315
2316 /* Swap in an options header. */
2317
2318 void
2319 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2320 Elf_Internal_Options *in)
2321 {
2322 in->kind = H_GET_8 (abfd, ex->kind);
2323 in->size = H_GET_8 (abfd, ex->size);
2324 in->section = H_GET_16 (abfd, ex->section);
2325 in->info = H_GET_32 (abfd, ex->info);
2326 }
2327
2328 /* Swap out an options header. */
2329
2330 void
2331 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2332 Elf_External_Options *ex)
2333 {
2334 H_PUT_8 (abfd, in->kind, ex->kind);
2335 H_PUT_8 (abfd, in->size, ex->size);
2336 H_PUT_16 (abfd, in->section, ex->section);
2337 H_PUT_32 (abfd, in->info, ex->info);
2338 }
2339 \f
2340 /* This function is called via qsort() to sort the dynamic relocation
2341 entries by increasing r_symndx value. */
2342
2343 static int
2344 sort_dynamic_relocs (const void *arg1, const void *arg2)
2345 {
2346 Elf_Internal_Rela int_reloc1;
2347 Elf_Internal_Rela int_reloc2;
2348 int diff;
2349
2350 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2351 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2352
2353 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2354 if (diff != 0)
2355 return diff;
2356
2357 if (int_reloc1.r_offset < int_reloc2.r_offset)
2358 return -1;
2359 if (int_reloc1.r_offset > int_reloc2.r_offset)
2360 return 1;
2361 return 0;
2362 }
2363
2364 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2365
2366 static int
2367 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2368 const void *arg2 ATTRIBUTE_UNUSED)
2369 {
2370 #ifdef BFD64
2371 Elf_Internal_Rela int_reloc1[3];
2372 Elf_Internal_Rela int_reloc2[3];
2373
2374 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2375 (reldyn_sorting_bfd, arg1, int_reloc1);
2376 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2377 (reldyn_sorting_bfd, arg2, int_reloc2);
2378
2379 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2380 return -1;
2381 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2382 return 1;
2383
2384 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2385 return -1;
2386 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2387 return 1;
2388 return 0;
2389 #else
2390 abort ();
2391 #endif
2392 }
2393
2394
2395 /* This routine is used to write out ECOFF debugging external symbol
2396 information. It is called via mips_elf_link_hash_traverse. The
2397 ECOFF external symbol information must match the ELF external
2398 symbol information. Unfortunately, at this point we don't know
2399 whether a symbol is required by reloc information, so the two
2400 tables may wind up being different. We must sort out the external
2401 symbol information before we can set the final size of the .mdebug
2402 section, and we must set the size of the .mdebug section before we
2403 can relocate any sections, and we can't know which symbols are
2404 required by relocation until we relocate the sections.
2405 Fortunately, it is relatively unlikely that any symbol will be
2406 stripped but required by a reloc. In particular, it can not happen
2407 when generating a final executable. */
2408
2409 static bfd_boolean
2410 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2411 {
2412 struct extsym_info *einfo = data;
2413 bfd_boolean strip;
2414 asection *sec, *output_section;
2415
2416 if (h->root.root.type == bfd_link_hash_warning)
2417 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2418
2419 if (h->root.indx == -2)
2420 strip = FALSE;
2421 else if ((h->root.def_dynamic
2422 || h->root.ref_dynamic
2423 || h->root.type == bfd_link_hash_new)
2424 && !h->root.def_regular
2425 && !h->root.ref_regular)
2426 strip = TRUE;
2427 else if (einfo->info->strip == strip_all
2428 || (einfo->info->strip == strip_some
2429 && bfd_hash_lookup (einfo->info->keep_hash,
2430 h->root.root.root.string,
2431 FALSE, FALSE) == NULL))
2432 strip = TRUE;
2433 else
2434 strip = FALSE;
2435
2436 if (strip)
2437 return TRUE;
2438
2439 if (h->esym.ifd == -2)
2440 {
2441 h->esym.jmptbl = 0;
2442 h->esym.cobol_main = 0;
2443 h->esym.weakext = 0;
2444 h->esym.reserved = 0;
2445 h->esym.ifd = ifdNil;
2446 h->esym.asym.value = 0;
2447 h->esym.asym.st = stGlobal;
2448
2449 if (h->root.root.type == bfd_link_hash_undefined
2450 || h->root.root.type == bfd_link_hash_undefweak)
2451 {
2452 const char *name;
2453
2454 /* Use undefined class. Also, set class and type for some
2455 special symbols. */
2456 name = h->root.root.root.string;
2457 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2458 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2459 {
2460 h->esym.asym.sc = scData;
2461 h->esym.asym.st = stLabel;
2462 h->esym.asym.value = 0;
2463 }
2464 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2465 {
2466 h->esym.asym.sc = scAbs;
2467 h->esym.asym.st = stLabel;
2468 h->esym.asym.value =
2469 mips_elf_hash_table (einfo->info)->procedure_count;
2470 }
2471 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2472 {
2473 h->esym.asym.sc = scAbs;
2474 h->esym.asym.st = stLabel;
2475 h->esym.asym.value = elf_gp (einfo->abfd);
2476 }
2477 else
2478 h->esym.asym.sc = scUndefined;
2479 }
2480 else if (h->root.root.type != bfd_link_hash_defined
2481 && h->root.root.type != bfd_link_hash_defweak)
2482 h->esym.asym.sc = scAbs;
2483 else
2484 {
2485 const char *name;
2486
2487 sec = h->root.root.u.def.section;
2488 output_section = sec->output_section;
2489
2490 /* When making a shared library and symbol h is the one from
2491 the another shared library, OUTPUT_SECTION may be null. */
2492 if (output_section == NULL)
2493 h->esym.asym.sc = scUndefined;
2494 else
2495 {
2496 name = bfd_section_name (output_section->owner, output_section);
2497
2498 if (strcmp (name, ".text") == 0)
2499 h->esym.asym.sc = scText;
2500 else if (strcmp (name, ".data") == 0)
2501 h->esym.asym.sc = scData;
2502 else if (strcmp (name, ".sdata") == 0)
2503 h->esym.asym.sc = scSData;
2504 else if (strcmp (name, ".rodata") == 0
2505 || strcmp (name, ".rdata") == 0)
2506 h->esym.asym.sc = scRData;
2507 else if (strcmp (name, ".bss") == 0)
2508 h->esym.asym.sc = scBss;
2509 else if (strcmp (name, ".sbss") == 0)
2510 h->esym.asym.sc = scSBss;
2511 else if (strcmp (name, ".init") == 0)
2512 h->esym.asym.sc = scInit;
2513 else if (strcmp (name, ".fini") == 0)
2514 h->esym.asym.sc = scFini;
2515 else
2516 h->esym.asym.sc = scAbs;
2517 }
2518 }
2519
2520 h->esym.asym.reserved = 0;
2521 h->esym.asym.index = indexNil;
2522 }
2523
2524 if (h->root.root.type == bfd_link_hash_common)
2525 h->esym.asym.value = h->root.root.u.c.size;
2526 else if (h->root.root.type == bfd_link_hash_defined
2527 || h->root.root.type == bfd_link_hash_defweak)
2528 {
2529 if (h->esym.asym.sc == scCommon)
2530 h->esym.asym.sc = scBss;
2531 else if (h->esym.asym.sc == scSCommon)
2532 h->esym.asym.sc = scSBss;
2533
2534 sec = h->root.root.u.def.section;
2535 output_section = sec->output_section;
2536 if (output_section != NULL)
2537 h->esym.asym.value = (h->root.root.u.def.value
2538 + sec->output_offset
2539 + output_section->vma);
2540 else
2541 h->esym.asym.value = 0;
2542 }
2543 else
2544 {
2545 struct mips_elf_link_hash_entry *hd = h;
2546
2547 while (hd->root.root.type == bfd_link_hash_indirect)
2548 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2549
2550 if (hd->needs_lazy_stub)
2551 {
2552 /* Set type and value for a symbol with a function stub. */
2553 h->esym.asym.st = stProc;
2554 sec = hd->root.root.u.def.section;
2555 if (sec == NULL)
2556 h->esym.asym.value = 0;
2557 else
2558 {
2559 output_section = sec->output_section;
2560 if (output_section != NULL)
2561 h->esym.asym.value = (hd->root.plt.offset
2562 + sec->output_offset
2563 + output_section->vma);
2564 else
2565 h->esym.asym.value = 0;
2566 }
2567 }
2568 }
2569
2570 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2571 h->root.root.root.string,
2572 &h->esym))
2573 {
2574 einfo->failed = TRUE;
2575 return FALSE;
2576 }
2577
2578 return TRUE;
2579 }
2580
2581 /* A comparison routine used to sort .gptab entries. */
2582
2583 static int
2584 gptab_compare (const void *p1, const void *p2)
2585 {
2586 const Elf32_gptab *a1 = p1;
2587 const Elf32_gptab *a2 = p2;
2588
2589 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2590 }
2591 \f
2592 /* Functions to manage the got entry hash table. */
2593
2594 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2595 hash number. */
2596
2597 static INLINE hashval_t
2598 mips_elf_hash_bfd_vma (bfd_vma addr)
2599 {
2600 #ifdef BFD64
2601 return addr + (addr >> 32);
2602 #else
2603 return addr;
2604 #endif
2605 }
2606
2607 /* got_entries only match if they're identical, except for gotidx, so
2608 use all fields to compute the hash, and compare the appropriate
2609 union members. */
2610
2611 static hashval_t
2612 mips_elf_got_entry_hash (const void *entry_)
2613 {
2614 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2615
2616 return entry->symndx
2617 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2618 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2619 : entry->abfd->id
2620 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2621 : entry->d.h->root.root.root.hash));
2622 }
2623
2624 static int
2625 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2626 {
2627 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2628 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2629
2630 /* An LDM entry can only match another LDM entry. */
2631 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2632 return 0;
2633
2634 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2635 && (! e1->abfd ? e1->d.address == e2->d.address
2636 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2637 : e1->d.h == e2->d.h);
2638 }
2639
2640 /* multi_got_entries are still a match in the case of global objects,
2641 even if the input bfd in which they're referenced differs, so the
2642 hash computation and compare functions are adjusted
2643 accordingly. */
2644
2645 static hashval_t
2646 mips_elf_multi_got_entry_hash (const void *entry_)
2647 {
2648 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2649
2650 return entry->symndx
2651 + (! entry->abfd
2652 ? mips_elf_hash_bfd_vma (entry->d.address)
2653 : entry->symndx >= 0
2654 ? ((entry->tls_type & GOT_TLS_LDM)
2655 ? (GOT_TLS_LDM << 17)
2656 : (entry->abfd->id
2657 + mips_elf_hash_bfd_vma (entry->d.addend)))
2658 : entry->d.h->root.root.root.hash);
2659 }
2660
2661 static int
2662 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2663 {
2664 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2665 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2666
2667 /* Any two LDM entries match. */
2668 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2669 return 1;
2670
2671 /* Nothing else matches an LDM entry. */
2672 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2673 return 0;
2674
2675 return e1->symndx == e2->symndx
2676 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2677 : e1->abfd == NULL || e2->abfd == NULL
2678 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2679 : e1->d.h == e2->d.h);
2680 }
2681
2682 static hashval_t
2683 mips_got_page_entry_hash (const void *entry_)
2684 {
2685 const struct mips_got_page_entry *entry;
2686
2687 entry = (const struct mips_got_page_entry *) entry_;
2688 return entry->abfd->id + entry->symndx;
2689 }
2690
2691 static int
2692 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2693 {
2694 const struct mips_got_page_entry *entry1, *entry2;
2695
2696 entry1 = (const struct mips_got_page_entry *) entry1_;
2697 entry2 = (const struct mips_got_page_entry *) entry2_;
2698 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2699 }
2700 \f
2701 /* Return the dynamic relocation section. If it doesn't exist, try to
2702 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2703 if creation fails. */
2704
2705 static asection *
2706 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2707 {
2708 const char *dname;
2709 asection *sreloc;
2710 bfd *dynobj;
2711
2712 dname = MIPS_ELF_REL_DYN_NAME (info);
2713 dynobj = elf_hash_table (info)->dynobj;
2714 sreloc = bfd_get_section_by_name (dynobj, dname);
2715 if (sreloc == NULL && create_p)
2716 {
2717 sreloc = bfd_make_section_with_flags (dynobj, dname,
2718 (SEC_ALLOC
2719 | SEC_LOAD
2720 | SEC_HAS_CONTENTS
2721 | SEC_IN_MEMORY
2722 | SEC_LINKER_CREATED
2723 | SEC_READONLY));
2724 if (sreloc == NULL
2725 || ! bfd_set_section_alignment (dynobj, sreloc,
2726 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2727 return NULL;
2728 }
2729 return sreloc;
2730 }
2731
2732 /* Count the number of relocations needed for a TLS GOT entry, with
2733 access types from TLS_TYPE, and symbol H (or a local symbol if H
2734 is NULL). */
2735
2736 static int
2737 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2738 struct elf_link_hash_entry *h)
2739 {
2740 int indx = 0;
2741 int ret = 0;
2742 bfd_boolean need_relocs = FALSE;
2743 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2744
2745 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2746 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2747 indx = h->dynindx;
2748
2749 if ((info->shared || indx != 0)
2750 && (h == NULL
2751 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2752 || h->root.type != bfd_link_hash_undefweak))
2753 need_relocs = TRUE;
2754
2755 if (!need_relocs)
2756 return FALSE;
2757
2758 if (tls_type & GOT_TLS_GD)
2759 {
2760 ret++;
2761 if (indx != 0)
2762 ret++;
2763 }
2764
2765 if (tls_type & GOT_TLS_IE)
2766 ret++;
2767
2768 if ((tls_type & GOT_TLS_LDM) && info->shared)
2769 ret++;
2770
2771 return ret;
2772 }
2773
2774 /* Count the number of TLS relocations required for the GOT entry in
2775 ARG1, if it describes a local symbol. */
2776
2777 static int
2778 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2779 {
2780 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2781 struct mips_elf_count_tls_arg *arg = arg2;
2782
2783 if (entry->abfd != NULL && entry->symndx != -1)
2784 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2785
2786 return 1;
2787 }
2788
2789 /* Count the number of TLS GOT entries required for the global (or
2790 forced-local) symbol in ARG1. */
2791
2792 static int
2793 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2794 {
2795 struct mips_elf_link_hash_entry *hm
2796 = (struct mips_elf_link_hash_entry *) arg1;
2797 struct mips_elf_count_tls_arg *arg = arg2;
2798
2799 if (hm->tls_type & GOT_TLS_GD)
2800 arg->needed += 2;
2801 if (hm->tls_type & GOT_TLS_IE)
2802 arg->needed += 1;
2803
2804 return 1;
2805 }
2806
2807 /* Count the number of TLS relocations required for the global (or
2808 forced-local) symbol in ARG1. */
2809
2810 static int
2811 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2812 {
2813 struct mips_elf_link_hash_entry *hm
2814 = (struct mips_elf_link_hash_entry *) arg1;
2815 struct mips_elf_count_tls_arg *arg = arg2;
2816
2817 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2818
2819 return 1;
2820 }
2821
2822 /* Output a simple dynamic relocation into SRELOC. */
2823
2824 static void
2825 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2826 asection *sreloc,
2827 unsigned long reloc_index,
2828 unsigned long indx,
2829 int r_type,
2830 bfd_vma offset)
2831 {
2832 Elf_Internal_Rela rel[3];
2833
2834 memset (rel, 0, sizeof (rel));
2835
2836 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2837 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2838
2839 if (ABI_64_P (output_bfd))
2840 {
2841 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2842 (output_bfd, &rel[0],
2843 (sreloc->contents
2844 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
2845 }
2846 else
2847 bfd_elf32_swap_reloc_out
2848 (output_bfd, &rel[0],
2849 (sreloc->contents
2850 + reloc_index * sizeof (Elf32_External_Rel)));
2851 }
2852
2853 /* Initialize a set of TLS GOT entries for one symbol. */
2854
2855 static void
2856 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2857 unsigned char *tls_type_p,
2858 struct bfd_link_info *info,
2859 struct mips_elf_link_hash_entry *h,
2860 bfd_vma value)
2861 {
2862 struct mips_elf_link_hash_table *htab;
2863 int indx;
2864 asection *sreloc, *sgot;
2865 bfd_vma offset, offset2;
2866 bfd_boolean need_relocs = FALSE;
2867
2868 htab = mips_elf_hash_table (info);
2869 if (htab == NULL)
2870 return;
2871
2872 sgot = htab->sgot;
2873
2874 indx = 0;
2875 if (h != NULL)
2876 {
2877 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2878
2879 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2880 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2881 indx = h->root.dynindx;
2882 }
2883
2884 if (*tls_type_p & GOT_TLS_DONE)
2885 return;
2886
2887 if ((info->shared || indx != 0)
2888 && (h == NULL
2889 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2890 || h->root.type != bfd_link_hash_undefweak))
2891 need_relocs = TRUE;
2892
2893 /* MINUS_ONE means the symbol is not defined in this object. It may not
2894 be defined at all; assume that the value doesn't matter in that
2895 case. Otherwise complain if we would use the value. */
2896 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2897 || h->root.root.type == bfd_link_hash_undefweak);
2898
2899 /* Emit necessary relocations. */
2900 sreloc = mips_elf_rel_dyn_section (info, FALSE);
2901
2902 /* General Dynamic. */
2903 if (*tls_type_p & GOT_TLS_GD)
2904 {
2905 offset = got_offset;
2906 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2907
2908 if (need_relocs)
2909 {
2910 mips_elf_output_dynamic_relocation
2911 (abfd, sreloc, sreloc->reloc_count++, indx,
2912 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2913 sgot->output_offset + sgot->output_section->vma + offset);
2914
2915 if (indx)
2916 mips_elf_output_dynamic_relocation
2917 (abfd, sreloc, sreloc->reloc_count++, indx,
2918 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2919 sgot->output_offset + sgot->output_section->vma + offset2);
2920 else
2921 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2922 sgot->contents + offset2);
2923 }
2924 else
2925 {
2926 MIPS_ELF_PUT_WORD (abfd, 1,
2927 sgot->contents + offset);
2928 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2929 sgot->contents + offset2);
2930 }
2931
2932 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2933 }
2934
2935 /* Initial Exec model. */
2936 if (*tls_type_p & GOT_TLS_IE)
2937 {
2938 offset = got_offset;
2939
2940 if (need_relocs)
2941 {
2942 if (indx == 0)
2943 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2944 sgot->contents + offset);
2945 else
2946 MIPS_ELF_PUT_WORD (abfd, 0,
2947 sgot->contents + offset);
2948
2949 mips_elf_output_dynamic_relocation
2950 (abfd, sreloc, sreloc->reloc_count++, indx,
2951 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2952 sgot->output_offset + sgot->output_section->vma + offset);
2953 }
2954 else
2955 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2956 sgot->contents + offset);
2957 }
2958
2959 if (*tls_type_p & GOT_TLS_LDM)
2960 {
2961 /* The initial offset is zero, and the LD offsets will include the
2962 bias by DTP_OFFSET. */
2963 MIPS_ELF_PUT_WORD (abfd, 0,
2964 sgot->contents + got_offset
2965 + MIPS_ELF_GOT_SIZE (abfd));
2966
2967 if (!info->shared)
2968 MIPS_ELF_PUT_WORD (abfd, 1,
2969 sgot->contents + got_offset);
2970 else
2971 mips_elf_output_dynamic_relocation
2972 (abfd, sreloc, sreloc->reloc_count++, indx,
2973 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2974 sgot->output_offset + sgot->output_section->vma + got_offset);
2975 }
2976
2977 *tls_type_p |= GOT_TLS_DONE;
2978 }
2979
2980 /* Return the GOT index to use for a relocation of type R_TYPE against
2981 a symbol accessed using TLS_TYPE models. The GOT entries for this
2982 symbol in this GOT start at GOT_INDEX. This function initializes the
2983 GOT entries and corresponding relocations. */
2984
2985 static bfd_vma
2986 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2987 int r_type, struct bfd_link_info *info,
2988 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2989 {
2990 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2991 || r_type == R_MIPS_TLS_LDM);
2992
2993 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2994
2995 if (r_type == R_MIPS_TLS_GOTTPREL)
2996 {
2997 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2998 if (*tls_type & GOT_TLS_GD)
2999 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
3000 else
3001 return got_index;
3002 }
3003
3004 if (r_type == R_MIPS_TLS_GD)
3005 {
3006 BFD_ASSERT (*tls_type & GOT_TLS_GD);
3007 return got_index;
3008 }
3009
3010 if (r_type == R_MIPS_TLS_LDM)
3011 {
3012 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3013 return got_index;
3014 }
3015
3016 return got_index;
3017 }
3018
3019 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3020 for global symbol H. .got.plt comes before the GOT, so the offset
3021 will be negative. */
3022
3023 static bfd_vma
3024 mips_elf_gotplt_index (struct bfd_link_info *info,
3025 struct elf_link_hash_entry *h)
3026 {
3027 bfd_vma plt_index, got_address, got_value;
3028 struct mips_elf_link_hash_table *htab;
3029
3030 htab = mips_elf_hash_table (info);
3031 BFD_ASSERT (htab != NULL);
3032
3033 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3034
3035 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3036 section starts with reserved entries. */
3037 BFD_ASSERT (htab->is_vxworks);
3038
3039 /* Calculate the index of the symbol's PLT entry. */
3040 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3041
3042 /* Calculate the address of the associated .got.plt entry. */
3043 got_address = (htab->sgotplt->output_section->vma
3044 + htab->sgotplt->output_offset
3045 + plt_index * 4);
3046
3047 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3048 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3049 + htab->root.hgot->root.u.def.section->output_offset
3050 + htab->root.hgot->root.u.def.value);
3051
3052 return got_address - got_value;
3053 }
3054
3055 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3056 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3057 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3058 offset can be found. */
3059
3060 static bfd_vma
3061 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3062 bfd_vma value, unsigned long r_symndx,
3063 struct mips_elf_link_hash_entry *h, int r_type)
3064 {
3065 struct mips_elf_link_hash_table *htab;
3066 struct mips_got_entry *entry;
3067
3068 htab = mips_elf_hash_table (info);
3069 BFD_ASSERT (htab != NULL);
3070
3071 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3072 r_symndx, h, r_type);
3073 if (!entry)
3074 return MINUS_ONE;
3075
3076 if (TLS_RELOC_P (r_type))
3077 {
3078 if (entry->symndx == -1 && htab->got_info->next == NULL)
3079 /* A type (3) entry in the single-GOT case. We use the symbol's
3080 hash table entry to track the index. */
3081 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3082 r_type, info, h, value);
3083 else
3084 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3085 r_type, info, h, value);
3086 }
3087 else
3088 return entry->gotidx;
3089 }
3090
3091 /* Returns the GOT index for the global symbol indicated by H. */
3092
3093 static bfd_vma
3094 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3095 int r_type, struct bfd_link_info *info)
3096 {
3097 struct mips_elf_link_hash_table *htab;
3098 bfd_vma got_index;
3099 struct mips_got_info *g, *gg;
3100 long global_got_dynindx = 0;
3101
3102 htab = mips_elf_hash_table (info);
3103 BFD_ASSERT (htab != NULL);
3104
3105 gg = g = htab->got_info;
3106 if (g->bfd2got && ibfd)
3107 {
3108 struct mips_got_entry e, *p;
3109
3110 BFD_ASSERT (h->dynindx >= 0);
3111
3112 g = mips_elf_got_for_ibfd (g, ibfd);
3113 if (g->next != gg || TLS_RELOC_P (r_type))
3114 {
3115 e.abfd = ibfd;
3116 e.symndx = -1;
3117 e.d.h = (struct mips_elf_link_hash_entry *)h;
3118 e.tls_type = 0;
3119
3120 p = htab_find (g->got_entries, &e);
3121
3122 BFD_ASSERT (p->gotidx > 0);
3123
3124 if (TLS_RELOC_P (r_type))
3125 {
3126 bfd_vma value = MINUS_ONE;
3127 if ((h->root.type == bfd_link_hash_defined
3128 || h->root.type == bfd_link_hash_defweak)
3129 && h->root.u.def.section->output_section)
3130 value = (h->root.u.def.value
3131 + h->root.u.def.section->output_offset
3132 + h->root.u.def.section->output_section->vma);
3133
3134 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3135 info, e.d.h, value);
3136 }
3137 else
3138 return p->gotidx;
3139 }
3140 }
3141
3142 if (gg->global_gotsym != NULL)
3143 global_got_dynindx = gg->global_gotsym->dynindx;
3144
3145 if (TLS_RELOC_P (r_type))
3146 {
3147 struct mips_elf_link_hash_entry *hm
3148 = (struct mips_elf_link_hash_entry *) h;
3149 bfd_vma value = MINUS_ONE;
3150
3151 if ((h->root.type == bfd_link_hash_defined
3152 || h->root.type == bfd_link_hash_defweak)
3153 && h->root.u.def.section->output_section)
3154 value = (h->root.u.def.value
3155 + h->root.u.def.section->output_offset
3156 + h->root.u.def.section->output_section->vma);
3157
3158 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3159 r_type, info, hm, value);
3160 }
3161 else
3162 {
3163 /* Once we determine the global GOT entry with the lowest dynamic
3164 symbol table index, we must put all dynamic symbols with greater
3165 indices into the GOT. That makes it easy to calculate the GOT
3166 offset. */
3167 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3168 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3169 * MIPS_ELF_GOT_SIZE (abfd));
3170 }
3171 BFD_ASSERT (got_index < htab->sgot->size);
3172
3173 return got_index;
3174 }
3175
3176 /* Find a GOT page entry that points to within 32KB of VALUE. These
3177 entries are supposed to be placed at small offsets in the GOT, i.e.,
3178 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3179 entry could be created. If OFFSETP is nonnull, use it to return the
3180 offset of the GOT entry from VALUE. */
3181
3182 static bfd_vma
3183 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3184 bfd_vma value, bfd_vma *offsetp)
3185 {
3186 bfd_vma page, got_index;
3187 struct mips_got_entry *entry;
3188
3189 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3190 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3191 NULL, R_MIPS_GOT_PAGE);
3192
3193 if (!entry)
3194 return MINUS_ONE;
3195
3196 got_index = entry->gotidx;
3197
3198 if (offsetp)
3199 *offsetp = value - entry->d.address;
3200
3201 return got_index;
3202 }
3203
3204 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3205 EXTERNAL is true if the relocation was against a global symbol
3206 that has been forced local. */
3207
3208 static bfd_vma
3209 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3210 bfd_vma value, bfd_boolean external)
3211 {
3212 struct mips_got_entry *entry;
3213
3214 /* GOT16 relocations against local symbols are followed by a LO16
3215 relocation; those against global symbols are not. Thus if the
3216 symbol was originally local, the GOT16 relocation should load the
3217 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3218 if (! external)
3219 value = mips_elf_high (value) << 16;
3220
3221 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3222 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3223 same in all cases. */
3224 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3225 NULL, R_MIPS_GOT16);
3226 if (entry)
3227 return entry->gotidx;
3228 else
3229 return MINUS_ONE;
3230 }
3231
3232 /* Returns the offset for the entry at the INDEXth position
3233 in the GOT. */
3234
3235 static bfd_vma
3236 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3237 bfd *input_bfd, bfd_vma got_index)
3238 {
3239 struct mips_elf_link_hash_table *htab;
3240 asection *sgot;
3241 bfd_vma gp;
3242
3243 htab = mips_elf_hash_table (info);
3244 BFD_ASSERT (htab != NULL);
3245
3246 sgot = htab->sgot;
3247 gp = _bfd_get_gp_value (output_bfd)
3248 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3249
3250 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3251 }
3252
3253 /* Create and return a local GOT entry for VALUE, which was calculated
3254 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3255 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3256 instead. */
3257
3258 static struct mips_got_entry *
3259 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3260 bfd *ibfd, bfd_vma value,
3261 unsigned long r_symndx,
3262 struct mips_elf_link_hash_entry *h,
3263 int r_type)
3264 {
3265 struct mips_got_entry entry, **loc;
3266 struct mips_got_info *g;
3267 struct mips_elf_link_hash_table *htab;
3268
3269 htab = mips_elf_hash_table (info);
3270 BFD_ASSERT (htab != NULL);
3271
3272 entry.abfd = NULL;
3273 entry.symndx = -1;
3274 entry.d.address = value;
3275 entry.tls_type = 0;
3276
3277 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
3278 if (g == NULL)
3279 {
3280 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
3281 BFD_ASSERT (g != NULL);
3282 }
3283
3284 /* We might have a symbol, H, if it has been forced local. Use the
3285 global entry then. It doesn't matter whether an entry is local
3286 or global for TLS, since the dynamic linker does not
3287 automatically relocate TLS GOT entries. */
3288 BFD_ASSERT (h == NULL || h->root.forced_local);
3289 if (TLS_RELOC_P (r_type))
3290 {
3291 struct mips_got_entry *p;
3292
3293 entry.abfd = ibfd;
3294 if (r_type == R_MIPS_TLS_LDM)
3295 {
3296 entry.tls_type = GOT_TLS_LDM;
3297 entry.symndx = 0;
3298 entry.d.addend = 0;
3299 }
3300 else if (h == NULL)
3301 {
3302 entry.symndx = r_symndx;
3303 entry.d.addend = 0;
3304 }
3305 else
3306 entry.d.h = h;
3307
3308 p = (struct mips_got_entry *)
3309 htab_find (g->got_entries, &entry);
3310
3311 BFD_ASSERT (p);
3312 return p;
3313 }
3314
3315 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3316 INSERT);
3317 if (*loc)
3318 return *loc;
3319
3320 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3321 entry.tls_type = 0;
3322
3323 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3324
3325 if (! *loc)
3326 return NULL;
3327
3328 memcpy (*loc, &entry, sizeof entry);
3329
3330 if (g->assigned_gotno > g->local_gotno)
3331 {
3332 (*loc)->gotidx = -1;
3333 /* We didn't allocate enough space in the GOT. */
3334 (*_bfd_error_handler)
3335 (_("not enough GOT space for local GOT entries"));
3336 bfd_set_error (bfd_error_bad_value);
3337 return NULL;
3338 }
3339
3340 MIPS_ELF_PUT_WORD (abfd, value,
3341 (htab->sgot->contents + entry.gotidx));
3342
3343 /* These GOT entries need a dynamic relocation on VxWorks. */
3344 if (htab->is_vxworks)
3345 {
3346 Elf_Internal_Rela outrel;
3347 asection *s;
3348 bfd_byte *rloc;
3349 bfd_vma got_address;
3350
3351 s = mips_elf_rel_dyn_section (info, FALSE);
3352 got_address = (htab->sgot->output_section->vma
3353 + htab->sgot->output_offset
3354 + entry.gotidx);
3355
3356 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3357 outrel.r_offset = got_address;
3358 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3359 outrel.r_addend = value;
3360 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3361 }
3362
3363 return *loc;
3364 }
3365
3366 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3367 The number might be exact or a worst-case estimate, depending on how
3368 much information is available to elf_backend_omit_section_dynsym at
3369 the current linking stage. */
3370
3371 static bfd_size_type
3372 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3373 {
3374 bfd_size_type count;
3375
3376 count = 0;
3377 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3378 {
3379 asection *p;
3380 const struct elf_backend_data *bed;
3381
3382 bed = get_elf_backend_data (output_bfd);
3383 for (p = output_bfd->sections; p ; p = p->next)
3384 if ((p->flags & SEC_EXCLUDE) == 0
3385 && (p->flags & SEC_ALLOC) != 0
3386 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3387 ++count;
3388 }
3389 return count;
3390 }
3391
3392 /* Sort the dynamic symbol table so that symbols that need GOT entries
3393 appear towards the end. */
3394
3395 static bfd_boolean
3396 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3397 {
3398 struct mips_elf_link_hash_table *htab;
3399 struct mips_elf_hash_sort_data hsd;
3400 struct mips_got_info *g;
3401
3402 if (elf_hash_table (info)->dynsymcount == 0)
3403 return TRUE;
3404
3405 htab = mips_elf_hash_table (info);
3406 BFD_ASSERT (htab != NULL);
3407
3408 g = htab->got_info;
3409 if (g == NULL)
3410 return TRUE;
3411
3412 hsd.low = NULL;
3413 hsd.max_unref_got_dynindx
3414 = hsd.min_got_dynindx
3415 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3416 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3417 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3418 elf_hash_table (info)),
3419 mips_elf_sort_hash_table_f,
3420 &hsd);
3421
3422 /* There should have been enough room in the symbol table to
3423 accommodate both the GOT and non-GOT symbols. */
3424 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3425 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3426 == elf_hash_table (info)->dynsymcount);
3427 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3428 == g->global_gotno);
3429
3430 /* Now we know which dynamic symbol has the lowest dynamic symbol
3431 table index in the GOT. */
3432 g->global_gotsym = hsd.low;
3433
3434 return TRUE;
3435 }
3436
3437 /* If H needs a GOT entry, assign it the highest available dynamic
3438 index. Otherwise, assign it the lowest available dynamic
3439 index. */
3440
3441 static bfd_boolean
3442 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3443 {
3444 struct mips_elf_hash_sort_data *hsd = data;
3445
3446 if (h->root.root.type == bfd_link_hash_warning)
3447 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3448
3449 /* Symbols without dynamic symbol table entries aren't interesting
3450 at all. */
3451 if (h->root.dynindx == -1)
3452 return TRUE;
3453
3454 switch (h->global_got_area)
3455 {
3456 case GGA_NONE:
3457 h->root.dynindx = hsd->max_non_got_dynindx++;
3458 break;
3459
3460 case GGA_NORMAL:
3461 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3462
3463 h->root.dynindx = --hsd->min_got_dynindx;
3464 hsd->low = (struct elf_link_hash_entry *) h;
3465 break;
3466
3467 case GGA_RELOC_ONLY:
3468 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3469
3470 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3471 hsd->low = (struct elf_link_hash_entry *) h;
3472 h->root.dynindx = hsd->max_unref_got_dynindx++;
3473 break;
3474 }
3475
3476 return TRUE;
3477 }
3478
3479 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3480 symbol table index lower than any we've seen to date, record it for
3481 posterity. */
3482
3483 static bfd_boolean
3484 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3485 bfd *abfd, struct bfd_link_info *info,
3486 unsigned char tls_flag)
3487 {
3488 struct mips_elf_link_hash_table *htab;
3489 struct mips_elf_link_hash_entry *hmips;
3490 struct mips_got_entry entry, **loc;
3491 struct mips_got_info *g;
3492
3493 htab = mips_elf_hash_table (info);
3494 BFD_ASSERT (htab != NULL);
3495
3496 hmips = (struct mips_elf_link_hash_entry *) h;
3497
3498 /* A global symbol in the GOT must also be in the dynamic symbol
3499 table. */
3500 if (h->dynindx == -1)
3501 {
3502 switch (ELF_ST_VISIBILITY (h->other))
3503 {
3504 case STV_INTERNAL:
3505 case STV_HIDDEN:
3506 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3507 break;
3508 }
3509 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3510 return FALSE;
3511 }
3512
3513 /* Make sure we have a GOT to put this entry into. */
3514 g = htab->got_info;
3515 BFD_ASSERT (g != NULL);
3516
3517 entry.abfd = abfd;
3518 entry.symndx = -1;
3519 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3520 entry.tls_type = 0;
3521
3522 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3523 INSERT);
3524
3525 /* If we've already marked this entry as needing GOT space, we don't
3526 need to do it again. */
3527 if (*loc)
3528 {
3529 (*loc)->tls_type |= tls_flag;
3530 return TRUE;
3531 }
3532
3533 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3534
3535 if (! *loc)
3536 return FALSE;
3537
3538 entry.gotidx = -1;
3539 entry.tls_type = tls_flag;
3540
3541 memcpy (*loc, &entry, sizeof entry);
3542
3543 if (tls_flag == 0)
3544 hmips->global_got_area = GGA_NORMAL;
3545
3546 return TRUE;
3547 }
3548
3549 /* Reserve space in G for a GOT entry containing the value of symbol
3550 SYMNDX in input bfd ABDF, plus ADDEND. */
3551
3552 static bfd_boolean
3553 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3554 struct bfd_link_info *info,
3555 unsigned char tls_flag)
3556 {
3557 struct mips_elf_link_hash_table *htab;
3558 struct mips_got_info *g;
3559 struct mips_got_entry entry, **loc;
3560
3561 htab = mips_elf_hash_table (info);
3562 BFD_ASSERT (htab != NULL);
3563
3564 g = htab->got_info;
3565 BFD_ASSERT (g != NULL);
3566
3567 entry.abfd = abfd;
3568 entry.symndx = symndx;
3569 entry.d.addend = addend;
3570 entry.tls_type = tls_flag;
3571 loc = (struct mips_got_entry **)
3572 htab_find_slot (g->got_entries, &entry, INSERT);
3573
3574 if (*loc)
3575 {
3576 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3577 {
3578 g->tls_gotno += 2;
3579 (*loc)->tls_type |= tls_flag;
3580 }
3581 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3582 {
3583 g->tls_gotno += 1;
3584 (*loc)->tls_type |= tls_flag;
3585 }
3586 return TRUE;
3587 }
3588
3589 if (tls_flag != 0)
3590 {
3591 entry.gotidx = -1;
3592 entry.tls_type = tls_flag;
3593 if (tls_flag == GOT_TLS_IE)
3594 g->tls_gotno += 1;
3595 else if (tls_flag == GOT_TLS_GD)
3596 g->tls_gotno += 2;
3597 else if (g->tls_ldm_offset == MINUS_ONE)
3598 {
3599 g->tls_ldm_offset = MINUS_TWO;
3600 g->tls_gotno += 2;
3601 }
3602 }
3603 else
3604 {
3605 entry.gotidx = g->local_gotno++;
3606 entry.tls_type = 0;
3607 }
3608
3609 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3610
3611 if (! *loc)
3612 return FALSE;
3613
3614 memcpy (*loc, &entry, sizeof entry);
3615
3616 return TRUE;
3617 }
3618
3619 /* Return the maximum number of GOT page entries required for RANGE. */
3620
3621 static bfd_vma
3622 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3623 {
3624 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3625 }
3626
3627 /* Record that ABFD has a page relocation against symbol SYMNDX and
3628 that ADDEND is the addend for that relocation.
3629
3630 This function creates an upper bound on the number of GOT slots
3631 required; no attempt is made to combine references to non-overridable
3632 global symbols across multiple input files. */
3633
3634 static bfd_boolean
3635 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3636 long symndx, bfd_signed_vma addend)
3637 {
3638 struct mips_elf_link_hash_table *htab;
3639 struct mips_got_info *g;
3640 struct mips_got_page_entry lookup, *entry;
3641 struct mips_got_page_range **range_ptr, *range;
3642 bfd_vma old_pages, new_pages;
3643 void **loc;
3644
3645 htab = mips_elf_hash_table (info);
3646 BFD_ASSERT (htab != NULL);
3647
3648 g = htab->got_info;
3649 BFD_ASSERT (g != NULL);
3650
3651 /* Find the mips_got_page_entry hash table entry for this symbol. */
3652 lookup.abfd = abfd;
3653 lookup.symndx = symndx;
3654 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3655 if (loc == NULL)
3656 return FALSE;
3657
3658 /* Create a mips_got_page_entry if this is the first time we've
3659 seen the symbol. */
3660 entry = (struct mips_got_page_entry *) *loc;
3661 if (!entry)
3662 {
3663 entry = bfd_alloc (abfd, sizeof (*entry));
3664 if (!entry)
3665 return FALSE;
3666
3667 entry->abfd = abfd;
3668 entry->symndx = symndx;
3669 entry->ranges = NULL;
3670 entry->num_pages = 0;
3671 *loc = entry;
3672 }
3673
3674 /* Skip over ranges whose maximum extent cannot share a page entry
3675 with ADDEND. */
3676 range_ptr = &entry->ranges;
3677 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3678 range_ptr = &(*range_ptr)->next;
3679
3680 /* If we scanned to the end of the list, or found a range whose
3681 minimum extent cannot share a page entry with ADDEND, create
3682 a new singleton range. */
3683 range = *range_ptr;
3684 if (!range || addend < range->min_addend - 0xffff)
3685 {
3686 range = bfd_alloc (abfd, sizeof (*range));
3687 if (!range)
3688 return FALSE;
3689
3690 range->next = *range_ptr;
3691 range->min_addend = addend;
3692 range->max_addend = addend;
3693
3694 *range_ptr = range;
3695 entry->num_pages++;
3696 g->page_gotno++;
3697 return TRUE;
3698 }
3699
3700 /* Remember how many pages the old range contributed. */
3701 old_pages = mips_elf_pages_for_range (range);
3702
3703 /* Update the ranges. */
3704 if (addend < range->min_addend)
3705 range->min_addend = addend;
3706 else if (addend > range->max_addend)
3707 {
3708 if (range->next && addend >= range->next->min_addend - 0xffff)
3709 {
3710 old_pages += mips_elf_pages_for_range (range->next);
3711 range->max_addend = range->next->max_addend;
3712 range->next = range->next->next;
3713 }
3714 else
3715 range->max_addend = addend;
3716 }
3717
3718 /* Record any change in the total estimate. */
3719 new_pages = mips_elf_pages_for_range (range);
3720 if (old_pages != new_pages)
3721 {
3722 entry->num_pages += new_pages - old_pages;
3723 g->page_gotno += new_pages - old_pages;
3724 }
3725
3726 return TRUE;
3727 }
3728
3729 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3730
3731 static void
3732 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3733 unsigned int n)
3734 {
3735 asection *s;
3736 struct mips_elf_link_hash_table *htab;
3737
3738 htab = mips_elf_hash_table (info);
3739 BFD_ASSERT (htab != NULL);
3740
3741 s = mips_elf_rel_dyn_section (info, FALSE);
3742 BFD_ASSERT (s != NULL);
3743
3744 if (htab->is_vxworks)
3745 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3746 else
3747 {
3748 if (s->size == 0)
3749 {
3750 /* Make room for a null element. */
3751 s->size += MIPS_ELF_REL_SIZE (abfd);
3752 ++s->reloc_count;
3753 }
3754 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3755 }
3756 }
3757 \f
3758 /* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3759 if the GOT entry is for an indirect or warning symbol. */
3760
3761 static int
3762 mips_elf_check_recreate_got (void **entryp, void *data)
3763 {
3764 struct mips_got_entry *entry;
3765 bfd_boolean *must_recreate;
3766
3767 entry = (struct mips_got_entry *) *entryp;
3768 must_recreate = (bfd_boolean *) data;
3769 if (entry->abfd != NULL && entry->symndx == -1)
3770 {
3771 struct mips_elf_link_hash_entry *h;
3772
3773 h = entry->d.h;
3774 if (h->root.root.type == bfd_link_hash_indirect
3775 || h->root.root.type == bfd_link_hash_warning)
3776 {
3777 *must_recreate = TRUE;
3778 return 0;
3779 }
3780 }
3781 return 1;
3782 }
3783
3784 /* A htab_traverse callback for GOT entries. Add all entries to
3785 hash table *DATA, converting entries for indirect and warning
3786 symbols into entries for the target symbol. Set *DATA to null
3787 on error. */
3788
3789 static int
3790 mips_elf_recreate_got (void **entryp, void *data)
3791 {
3792 htab_t *new_got;
3793 struct mips_got_entry *entry;
3794 void **slot;
3795
3796 new_got = (htab_t *) data;
3797 entry = (struct mips_got_entry *) *entryp;
3798 if (entry->abfd != NULL && entry->symndx == -1)
3799 {
3800 struct mips_elf_link_hash_entry *h;
3801
3802 h = entry->d.h;
3803 while (h->root.root.type == bfd_link_hash_indirect
3804 || h->root.root.type == bfd_link_hash_warning)
3805 {
3806 BFD_ASSERT (h->global_got_area == GGA_NONE);
3807 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3808 }
3809 entry->d.h = h;
3810 }
3811 slot = htab_find_slot (*new_got, entry, INSERT);
3812 if (slot == NULL)
3813 {
3814 *new_got = NULL;
3815 return 0;
3816 }
3817 if (*slot == NULL)
3818 *slot = entry;
3819 else
3820 free (entry);
3821 return 1;
3822 }
3823
3824 /* If any entries in G->got_entries are for indirect or warning symbols,
3825 replace them with entries for the target symbol. */
3826
3827 static bfd_boolean
3828 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3829 {
3830 bfd_boolean must_recreate;
3831 htab_t new_got;
3832
3833 must_recreate = FALSE;
3834 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3835 if (must_recreate)
3836 {
3837 new_got = htab_create (htab_size (g->got_entries),
3838 mips_elf_got_entry_hash,
3839 mips_elf_got_entry_eq, NULL);
3840 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3841 if (new_got == NULL)
3842 return FALSE;
3843
3844 /* Each entry in g->got_entries has either been copied to new_got
3845 or freed. Now delete the hash table itself. */
3846 htab_delete (g->got_entries);
3847 g->got_entries = new_got;
3848 }
3849 return TRUE;
3850 }
3851
3852 /* A mips_elf_link_hash_traverse callback for which DATA points
3853 to a mips_got_info. Count the number of type (3) entries. */
3854
3855 static int
3856 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
3857 {
3858 struct mips_got_info *g;
3859
3860 g = (struct mips_got_info *) data;
3861 if (h->global_got_area != GGA_NONE)
3862 {
3863 if (h->root.forced_local || h->root.dynindx == -1)
3864 {
3865 /* We no longer need this entry if it was only used for
3866 relocations; those relocations will be against the
3867 null or section symbol instead of H. */
3868 if (h->global_got_area != GGA_RELOC_ONLY)
3869 g->local_gotno++;
3870 h->global_got_area = GGA_NONE;
3871 }
3872 else
3873 {
3874 g->global_gotno++;
3875 if (h->global_got_area == GGA_RELOC_ONLY)
3876 g->reloc_only_gotno++;
3877 }
3878 }
3879 return 1;
3880 }
3881 \f
3882 /* Compute the hash value of the bfd in a bfd2got hash entry. */
3883
3884 static hashval_t
3885 mips_elf_bfd2got_entry_hash (const void *entry_)
3886 {
3887 const struct mips_elf_bfd2got_hash *entry
3888 = (struct mips_elf_bfd2got_hash *)entry_;
3889
3890 return entry->bfd->id;
3891 }
3892
3893 /* Check whether two hash entries have the same bfd. */
3894
3895 static int
3896 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3897 {
3898 const struct mips_elf_bfd2got_hash *e1
3899 = (const struct mips_elf_bfd2got_hash *)entry1;
3900 const struct mips_elf_bfd2got_hash *e2
3901 = (const struct mips_elf_bfd2got_hash *)entry2;
3902
3903 return e1->bfd == e2->bfd;
3904 }
3905
3906 /* In a multi-got link, determine the GOT to be used for IBFD. G must
3907 be the master GOT data. */
3908
3909 static struct mips_got_info *
3910 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3911 {
3912 struct mips_elf_bfd2got_hash e, *p;
3913
3914 if (! g->bfd2got)
3915 return g;
3916
3917 e.bfd = ibfd;
3918 p = htab_find (g->bfd2got, &e);
3919 return p ? p->g : NULL;
3920 }
3921
3922 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3923 Return NULL if an error occured. */
3924
3925 static struct mips_got_info *
3926 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3927 bfd *input_bfd)
3928 {
3929 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3930 struct mips_got_info *g;
3931 void **bfdgotp;
3932
3933 bfdgot_entry.bfd = input_bfd;
3934 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3935 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3936
3937 if (bfdgot == NULL)
3938 {
3939 bfdgot = ((struct mips_elf_bfd2got_hash *)
3940 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3941 if (bfdgot == NULL)
3942 return NULL;
3943
3944 *bfdgotp = bfdgot;
3945
3946 g = ((struct mips_got_info *)
3947 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3948 if (g == NULL)
3949 return NULL;
3950
3951 bfdgot->bfd = input_bfd;
3952 bfdgot->g = g;
3953
3954 g->global_gotsym = NULL;
3955 g->global_gotno = 0;
3956 g->reloc_only_gotno = 0;
3957 g->local_gotno = 0;
3958 g->page_gotno = 0;
3959 g->assigned_gotno = -1;
3960 g->tls_gotno = 0;
3961 g->tls_assigned_gotno = 0;
3962 g->tls_ldm_offset = MINUS_ONE;
3963 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3964 mips_elf_multi_got_entry_eq, NULL);
3965 if (g->got_entries == NULL)
3966 return NULL;
3967
3968 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3969 mips_got_page_entry_eq, NULL);
3970 if (g->got_page_entries == NULL)
3971 return NULL;
3972
3973 g->bfd2got = NULL;
3974 g->next = NULL;
3975 }
3976
3977 return bfdgot->g;
3978 }
3979
3980 /* A htab_traverse callback for the entries in the master got.
3981 Create one separate got for each bfd that has entries in the global
3982 got, such that we can tell how many local and global entries each
3983 bfd requires. */
3984
3985 static int
3986 mips_elf_make_got_per_bfd (void **entryp, void *p)
3987 {
3988 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3989 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3990 struct mips_got_info *g;
3991
3992 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3993 if (g == NULL)
3994 {
3995 arg->obfd = NULL;
3996 return 0;
3997 }
3998
3999 /* Insert the GOT entry in the bfd's got entry hash table. */
4000 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4001 if (*entryp != NULL)
4002 return 1;
4003
4004 *entryp = entry;
4005
4006 if (entry->tls_type)
4007 {
4008 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4009 g->tls_gotno += 2;
4010 if (entry->tls_type & GOT_TLS_IE)
4011 g->tls_gotno += 1;
4012 }
4013 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
4014 ++g->local_gotno;
4015 else
4016 ++g->global_gotno;
4017
4018 return 1;
4019 }
4020
4021 /* A htab_traverse callback for the page entries in the master got.
4022 Associate each page entry with the bfd's got. */
4023
4024 static int
4025 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4026 {
4027 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4028 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4029 struct mips_got_info *g;
4030
4031 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4032 if (g == NULL)
4033 {
4034 arg->obfd = NULL;
4035 return 0;
4036 }
4037
4038 /* Insert the GOT entry in the bfd's got entry hash table. */
4039 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4040 if (*entryp != NULL)
4041 return 1;
4042
4043 *entryp = entry;
4044 g->page_gotno += entry->num_pages;
4045 return 1;
4046 }
4047
4048 /* Consider merging the got described by BFD2GOT with TO, using the
4049 information given by ARG. Return -1 if this would lead to overflow,
4050 1 if they were merged successfully, and 0 if a merge failed due to
4051 lack of memory. (These values are chosen so that nonnegative return
4052 values can be returned by a htab_traverse callback.) */
4053
4054 static int
4055 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4056 struct mips_got_info *to,
4057 struct mips_elf_got_per_bfd_arg *arg)
4058 {
4059 struct mips_got_info *from = bfd2got->g;
4060 unsigned int estimate;
4061
4062 /* Work out how many page entries we would need for the combined GOT. */
4063 estimate = arg->max_pages;
4064 if (estimate >= from->page_gotno + to->page_gotno)
4065 estimate = from->page_gotno + to->page_gotno;
4066
4067 /* And conservatively estimate how many local, global and TLS entries
4068 would be needed. */
4069 estimate += (from->local_gotno
4070 + from->global_gotno
4071 + from->tls_gotno
4072 + to->local_gotno
4073 + to->global_gotno
4074 + to->tls_gotno);
4075
4076 /* Bail out if the combined GOT might be too big. */
4077 if (estimate > arg->max_count)
4078 return -1;
4079
4080 /* Commit to the merge. Record that TO is now the bfd for this got. */
4081 bfd2got->g = to;
4082
4083 /* Transfer the bfd's got information from FROM to TO. */
4084 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4085 if (arg->obfd == NULL)
4086 return 0;
4087
4088 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4089 if (arg->obfd == NULL)
4090 return 0;
4091
4092 /* We don't have to worry about releasing memory of the actual
4093 got entries, since they're all in the master got_entries hash
4094 table anyway. */
4095 htab_delete (from->got_entries);
4096 htab_delete (from->got_page_entries);
4097 return 1;
4098 }
4099
4100 /* Attempt to merge gots of different input bfds. Try to use as much
4101 as possible of the primary got, since it doesn't require explicit
4102 dynamic relocations, but don't use bfds that would reference global
4103 symbols out of the addressable range. Failing the primary got,
4104 attempt to merge with the current got, or finish the current got
4105 and then make make the new got current. */
4106
4107 static int
4108 mips_elf_merge_gots (void **bfd2got_, void *p)
4109 {
4110 struct mips_elf_bfd2got_hash *bfd2got
4111 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4112 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4113 struct mips_got_info *g;
4114 unsigned int estimate;
4115 int result;
4116
4117 g = bfd2got->g;
4118
4119 /* Work out the number of page, local and TLS entries. */
4120 estimate = arg->max_pages;
4121 if (estimate > g->page_gotno)
4122 estimate = g->page_gotno;
4123 estimate += g->local_gotno + g->tls_gotno;
4124
4125 /* We place TLS GOT entries after both locals and globals. The globals
4126 for the primary GOT may overflow the normal GOT size limit, so be
4127 sure not to merge a GOT which requires TLS with the primary GOT in that
4128 case. This doesn't affect non-primary GOTs. */
4129 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4130
4131 if (estimate <= arg->max_count)
4132 {
4133 /* If we don't have a primary GOT, use it as
4134 a starting point for the primary GOT. */
4135 if (!arg->primary)
4136 {
4137 arg->primary = bfd2got->g;
4138 return 1;
4139 }
4140
4141 /* Try merging with the primary GOT. */
4142 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4143 if (result >= 0)
4144 return result;
4145 }
4146
4147 /* If we can merge with the last-created got, do it. */
4148 if (arg->current)
4149 {
4150 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4151 if (result >= 0)
4152 return result;
4153 }
4154
4155 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4156 fits; if it turns out that it doesn't, we'll get relocation
4157 overflows anyway. */
4158 g->next = arg->current;
4159 arg->current = g;
4160
4161 return 1;
4162 }
4163
4164 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4165 is null iff there is just a single GOT. */
4166
4167 static int
4168 mips_elf_initialize_tls_index (void **entryp, void *p)
4169 {
4170 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4171 struct mips_got_info *g = p;
4172 bfd_vma next_index;
4173 unsigned char tls_type;
4174
4175 /* We're only interested in TLS symbols. */
4176 if (entry->tls_type == 0)
4177 return 1;
4178
4179 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4180
4181 if (entry->symndx == -1 && g->next == NULL)
4182 {
4183 /* A type (3) got entry in the single-GOT case. We use the symbol's
4184 hash table entry to track its index. */
4185 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4186 return 1;
4187 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4188 entry->d.h->tls_got_offset = next_index;
4189 tls_type = entry->d.h->tls_type;
4190 }
4191 else
4192 {
4193 if (entry->tls_type & GOT_TLS_LDM)
4194 {
4195 /* There are separate mips_got_entry objects for each input bfd
4196 that requires an LDM entry. Make sure that all LDM entries in
4197 a GOT resolve to the same index. */
4198 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4199 {
4200 entry->gotidx = g->tls_ldm_offset;
4201 return 1;
4202 }
4203 g->tls_ldm_offset = next_index;
4204 }
4205 entry->gotidx = next_index;
4206 tls_type = entry->tls_type;
4207 }
4208
4209 /* Account for the entries we've just allocated. */
4210 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4211 g->tls_assigned_gotno += 2;
4212 if (tls_type & GOT_TLS_IE)
4213 g->tls_assigned_gotno += 1;
4214
4215 return 1;
4216 }
4217
4218 /* If passed a NULL mips_got_info in the argument, set the marker used
4219 to tell whether a global symbol needs a got entry (in the primary
4220 got) to the given VALUE.
4221
4222 If passed a pointer G to a mips_got_info in the argument (it must
4223 not be the primary GOT), compute the offset from the beginning of
4224 the (primary) GOT section to the entry in G corresponding to the
4225 global symbol. G's assigned_gotno must contain the index of the
4226 first available global GOT entry in G. VALUE must contain the size
4227 of a GOT entry in bytes. For each global GOT entry that requires a
4228 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4229 marked as not eligible for lazy resolution through a function
4230 stub. */
4231 static int
4232 mips_elf_set_global_got_offset (void **entryp, void *p)
4233 {
4234 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4235 struct mips_elf_set_global_got_offset_arg *arg
4236 = (struct mips_elf_set_global_got_offset_arg *)p;
4237 struct mips_got_info *g = arg->g;
4238
4239 if (g && entry->tls_type != GOT_NORMAL)
4240 arg->needed_relocs +=
4241 mips_tls_got_relocs (arg->info, entry->tls_type,
4242 entry->symndx == -1 ? &entry->d.h->root : NULL);
4243
4244 if (entry->abfd != NULL
4245 && entry->symndx == -1
4246 && entry->d.h->global_got_area != GGA_NONE)
4247 {
4248 if (g)
4249 {
4250 BFD_ASSERT (g->global_gotsym == NULL);
4251
4252 entry->gotidx = arg->value * (long) g->assigned_gotno++;
4253 if (arg->info->shared
4254 || (elf_hash_table (arg->info)->dynamic_sections_created
4255 && entry->d.h->root.def_dynamic
4256 && !entry->d.h->root.def_regular))
4257 ++arg->needed_relocs;
4258 }
4259 else
4260 entry->d.h->global_got_area = arg->value;
4261 }
4262
4263 return 1;
4264 }
4265
4266 /* A htab_traverse callback for GOT entries for which DATA is the
4267 bfd_link_info. Forbid any global symbols from having traditional
4268 lazy-binding stubs. */
4269
4270 static int
4271 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4272 {
4273 struct bfd_link_info *info;
4274 struct mips_elf_link_hash_table *htab;
4275 struct mips_got_entry *entry;
4276
4277 entry = (struct mips_got_entry *) *entryp;
4278 info = (struct bfd_link_info *) data;
4279 htab = mips_elf_hash_table (info);
4280 BFD_ASSERT (htab != NULL);
4281
4282 if (entry->abfd != NULL
4283 && entry->symndx == -1
4284 && entry->d.h->needs_lazy_stub)
4285 {
4286 entry->d.h->needs_lazy_stub = FALSE;
4287 htab->lazy_stub_count--;
4288 }
4289
4290 return 1;
4291 }
4292
4293 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4294 the primary GOT. */
4295 static bfd_vma
4296 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4297 {
4298 if (g->bfd2got == NULL)
4299 return 0;
4300
4301 g = mips_elf_got_for_ibfd (g, ibfd);
4302 if (! g)
4303 return 0;
4304
4305 BFD_ASSERT (g->next);
4306
4307 g = g->next;
4308
4309 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4310 * MIPS_ELF_GOT_SIZE (abfd);
4311 }
4312
4313 /* Turn a single GOT that is too big for 16-bit addressing into
4314 a sequence of GOTs, each one 16-bit addressable. */
4315
4316 static bfd_boolean
4317 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4318 asection *got, bfd_size_type pages)
4319 {
4320 struct mips_elf_link_hash_table *htab;
4321 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4322 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
4323 struct mips_got_info *g, *gg;
4324 unsigned int assign, needed_relocs;
4325 bfd *dynobj;
4326
4327 dynobj = elf_hash_table (info)->dynobj;
4328 htab = mips_elf_hash_table (info);
4329 BFD_ASSERT (htab != NULL);
4330
4331 g = htab->got_info;
4332 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
4333 mips_elf_bfd2got_entry_eq, NULL);
4334 if (g->bfd2got == NULL)
4335 return FALSE;
4336
4337 got_per_bfd_arg.bfd2got = g->bfd2got;
4338 got_per_bfd_arg.obfd = abfd;
4339 got_per_bfd_arg.info = info;
4340
4341 /* Count how many GOT entries each input bfd requires, creating a
4342 map from bfd to got info while at that. */
4343 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4344 if (got_per_bfd_arg.obfd == NULL)
4345 return FALSE;
4346
4347 /* Also count how many page entries each input bfd requires. */
4348 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4349 &got_per_bfd_arg);
4350 if (got_per_bfd_arg.obfd == NULL)
4351 return FALSE;
4352
4353 got_per_bfd_arg.current = NULL;
4354 got_per_bfd_arg.primary = NULL;
4355 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4356 / MIPS_ELF_GOT_SIZE (abfd))
4357 - htab->reserved_gotno);
4358 got_per_bfd_arg.max_pages = pages;
4359 /* The number of globals that will be included in the primary GOT.
4360 See the calls to mips_elf_set_global_got_offset below for more
4361 information. */
4362 got_per_bfd_arg.global_count = g->global_gotno;
4363
4364 /* Try to merge the GOTs of input bfds together, as long as they
4365 don't seem to exceed the maximum GOT size, choosing one of them
4366 to be the primary GOT. */
4367 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4368 if (got_per_bfd_arg.obfd == NULL)
4369 return FALSE;
4370
4371 /* If we do not find any suitable primary GOT, create an empty one. */
4372 if (got_per_bfd_arg.primary == NULL)
4373 {
4374 g->next = (struct mips_got_info *)
4375 bfd_alloc (abfd, sizeof (struct mips_got_info));
4376 if (g->next == NULL)
4377 return FALSE;
4378
4379 g->next->global_gotsym = NULL;
4380 g->next->global_gotno = 0;
4381 g->next->reloc_only_gotno = 0;
4382 g->next->local_gotno = 0;
4383 g->next->page_gotno = 0;
4384 g->next->tls_gotno = 0;
4385 g->next->assigned_gotno = 0;
4386 g->next->tls_assigned_gotno = 0;
4387 g->next->tls_ldm_offset = MINUS_ONE;
4388 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4389 mips_elf_multi_got_entry_eq,
4390 NULL);
4391 if (g->next->got_entries == NULL)
4392 return FALSE;
4393 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4394 mips_got_page_entry_eq,
4395 NULL);
4396 if (g->next->got_page_entries == NULL)
4397 return FALSE;
4398 g->next->bfd2got = NULL;
4399 }
4400 else
4401 g->next = got_per_bfd_arg.primary;
4402 g->next->next = got_per_bfd_arg.current;
4403
4404 /* GG is now the master GOT, and G is the primary GOT. */
4405 gg = g;
4406 g = g->next;
4407
4408 /* Map the output bfd to the primary got. That's what we're going
4409 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4410 didn't mark in check_relocs, and we want a quick way to find it.
4411 We can't just use gg->next because we're going to reverse the
4412 list. */
4413 {
4414 struct mips_elf_bfd2got_hash *bfdgot;
4415 void **bfdgotp;
4416
4417 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4418 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4419
4420 if (bfdgot == NULL)
4421 return FALSE;
4422
4423 bfdgot->bfd = abfd;
4424 bfdgot->g = g;
4425 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4426
4427 BFD_ASSERT (*bfdgotp == NULL);
4428 *bfdgotp = bfdgot;
4429 }
4430
4431 /* Every symbol that is referenced in a dynamic relocation must be
4432 present in the primary GOT, so arrange for them to appear after
4433 those that are actually referenced. */
4434 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4435 g->global_gotno = gg->global_gotno;
4436
4437 set_got_offset_arg.g = NULL;
4438 set_got_offset_arg.value = GGA_RELOC_ONLY;
4439 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4440 &set_got_offset_arg);
4441 set_got_offset_arg.value = GGA_NORMAL;
4442 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4443 &set_got_offset_arg);
4444
4445 /* Now go through the GOTs assigning them offset ranges.
4446 [assigned_gotno, local_gotno[ will be set to the range of local
4447 entries in each GOT. We can then compute the end of a GOT by
4448 adding local_gotno to global_gotno. We reverse the list and make
4449 it circular since then we'll be able to quickly compute the
4450 beginning of a GOT, by computing the end of its predecessor. To
4451 avoid special cases for the primary GOT, while still preserving
4452 assertions that are valid for both single- and multi-got links,
4453 we arrange for the main got struct to have the right number of
4454 global entries, but set its local_gotno such that the initial
4455 offset of the primary GOT is zero. Remember that the primary GOT
4456 will become the last item in the circular linked list, so it
4457 points back to the master GOT. */
4458 gg->local_gotno = -g->global_gotno;
4459 gg->global_gotno = g->global_gotno;
4460 gg->tls_gotno = 0;
4461 assign = 0;
4462 gg->next = gg;
4463
4464 do
4465 {
4466 struct mips_got_info *gn;
4467
4468 assign += htab->reserved_gotno;
4469 g->assigned_gotno = assign;
4470 g->local_gotno += assign;
4471 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4472 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4473
4474 /* Take g out of the direct list, and push it onto the reversed
4475 list that gg points to. g->next is guaranteed to be nonnull after
4476 this operation, as required by mips_elf_initialize_tls_index. */
4477 gn = g->next;
4478 g->next = gg->next;
4479 gg->next = g;
4480
4481 /* Set up any TLS entries. We always place the TLS entries after
4482 all non-TLS entries. */
4483 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4484 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
4485
4486 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4487 g = gn;
4488
4489 /* Forbid global symbols in every non-primary GOT from having
4490 lazy-binding stubs. */
4491 if (g)
4492 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4493 }
4494 while (g);
4495
4496 got->size = (gg->next->local_gotno
4497 + gg->next->global_gotno
4498 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4499
4500 needed_relocs = 0;
4501 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4502 set_got_offset_arg.info = info;
4503 for (g = gg->next; g && g->next != gg; g = g->next)
4504 {
4505 unsigned int save_assign;
4506
4507 /* Assign offsets to global GOT entries. */
4508 save_assign = g->assigned_gotno;
4509 g->assigned_gotno = g->local_gotno;
4510 set_got_offset_arg.g = g;
4511 set_got_offset_arg.needed_relocs = 0;
4512 htab_traverse (g->got_entries,
4513 mips_elf_set_global_got_offset,
4514 &set_got_offset_arg);
4515 needed_relocs += set_got_offset_arg.needed_relocs;
4516 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4517
4518 g->assigned_gotno = save_assign;
4519 if (info->shared)
4520 {
4521 needed_relocs += g->local_gotno - g->assigned_gotno;
4522 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4523 + g->next->global_gotno
4524 + g->next->tls_gotno
4525 + htab->reserved_gotno);
4526 }
4527 }
4528
4529 if (needed_relocs)
4530 mips_elf_allocate_dynamic_relocations (dynobj, info,
4531 needed_relocs);
4532
4533 return TRUE;
4534 }
4535
4536 \f
4537 /* Returns the first relocation of type r_type found, beginning with
4538 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4539
4540 static const Elf_Internal_Rela *
4541 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4542 const Elf_Internal_Rela *relocation,
4543 const Elf_Internal_Rela *relend)
4544 {
4545 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4546
4547 while (relocation < relend)
4548 {
4549 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4550 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4551 return relocation;
4552
4553 ++relocation;
4554 }
4555
4556 /* We didn't find it. */
4557 return NULL;
4558 }
4559
4560 /* Return whether a relocation is against a local symbol. */
4561
4562 static bfd_boolean
4563 mips_elf_local_relocation_p (bfd *input_bfd,
4564 const Elf_Internal_Rela *relocation,
4565 asection **local_sections,
4566 bfd_boolean check_forced)
4567 {
4568 unsigned long r_symndx;
4569 Elf_Internal_Shdr *symtab_hdr;
4570 struct mips_elf_link_hash_entry *h;
4571 size_t extsymoff;
4572
4573 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4574 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4575 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4576
4577 if (r_symndx < extsymoff)
4578 return TRUE;
4579 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4580 return TRUE;
4581
4582 if (check_forced)
4583 {
4584 /* Look up the hash table to check whether the symbol
4585 was forced local. */
4586 h = (struct mips_elf_link_hash_entry *)
4587 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4588 /* Find the real hash-table entry for this symbol. */
4589 while (h->root.root.type == bfd_link_hash_indirect
4590 || h->root.root.type == bfd_link_hash_warning)
4591 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4592 if (h->root.forced_local)
4593 return TRUE;
4594 }
4595
4596 return FALSE;
4597 }
4598 \f
4599 /* Sign-extend VALUE, which has the indicated number of BITS. */
4600
4601 bfd_vma
4602 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4603 {
4604 if (value & ((bfd_vma) 1 << (bits - 1)))
4605 /* VALUE is negative. */
4606 value |= ((bfd_vma) - 1) << bits;
4607
4608 return value;
4609 }
4610
4611 /* Return non-zero if the indicated VALUE has overflowed the maximum
4612 range expressible by a signed number with the indicated number of
4613 BITS. */
4614
4615 static bfd_boolean
4616 mips_elf_overflow_p (bfd_vma value, int bits)
4617 {
4618 bfd_signed_vma svalue = (bfd_signed_vma) value;
4619
4620 if (svalue > (1 << (bits - 1)) - 1)
4621 /* The value is too big. */
4622 return TRUE;
4623 else if (svalue < -(1 << (bits - 1)))
4624 /* The value is too small. */
4625 return TRUE;
4626
4627 /* All is well. */
4628 return FALSE;
4629 }
4630
4631 /* Calculate the %high function. */
4632
4633 static bfd_vma
4634 mips_elf_high (bfd_vma value)
4635 {
4636 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4637 }
4638
4639 /* Calculate the %higher function. */
4640
4641 static bfd_vma
4642 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4643 {
4644 #ifdef BFD64
4645 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4646 #else
4647 abort ();
4648 return MINUS_ONE;
4649 #endif
4650 }
4651
4652 /* Calculate the %highest function. */
4653
4654 static bfd_vma
4655 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4656 {
4657 #ifdef BFD64
4658 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4659 #else
4660 abort ();
4661 return MINUS_ONE;
4662 #endif
4663 }
4664 \f
4665 /* Create the .compact_rel section. */
4666
4667 static bfd_boolean
4668 mips_elf_create_compact_rel_section
4669 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4670 {
4671 flagword flags;
4672 register asection *s;
4673
4674 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4675 {
4676 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4677 | SEC_READONLY);
4678
4679 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4680 if (s == NULL
4681 || ! bfd_set_section_alignment (abfd, s,
4682 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4683 return FALSE;
4684
4685 s->size = sizeof (Elf32_External_compact_rel);
4686 }
4687
4688 return TRUE;
4689 }
4690
4691 /* Create the .got section to hold the global offset table. */
4692
4693 static bfd_boolean
4694 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4695 {
4696 flagword flags;
4697 register asection *s;
4698 struct elf_link_hash_entry *h;
4699 struct bfd_link_hash_entry *bh;
4700 struct mips_got_info *g;
4701 bfd_size_type amt;
4702 struct mips_elf_link_hash_table *htab;
4703
4704 htab = mips_elf_hash_table (info);
4705 BFD_ASSERT (htab != NULL);
4706
4707 /* This function may be called more than once. */
4708 if (htab->sgot)
4709 return TRUE;
4710
4711 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4712 | SEC_LINKER_CREATED);
4713
4714 /* We have to use an alignment of 2**4 here because this is hardcoded
4715 in the function stub generation and in the linker script. */
4716 s = bfd_make_section_with_flags (abfd, ".got", flags);
4717 if (s == NULL
4718 || ! bfd_set_section_alignment (abfd, s, 4))
4719 return FALSE;
4720 htab->sgot = s;
4721
4722 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4723 linker script because we don't want to define the symbol if we
4724 are not creating a global offset table. */
4725 bh = NULL;
4726 if (! (_bfd_generic_link_add_one_symbol
4727 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4728 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4729 return FALSE;
4730
4731 h = (struct elf_link_hash_entry *) bh;
4732 h->non_elf = 0;
4733 h->def_regular = 1;
4734 h->type = STT_OBJECT;
4735 elf_hash_table (info)->hgot = h;
4736
4737 if (info->shared
4738 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4739 return FALSE;
4740
4741 amt = sizeof (struct mips_got_info);
4742 g = bfd_alloc (abfd, amt);
4743 if (g == NULL)
4744 return FALSE;
4745 g->global_gotsym = NULL;
4746 g->global_gotno = 0;
4747 g->reloc_only_gotno = 0;
4748 g->tls_gotno = 0;
4749 g->local_gotno = 0;
4750 g->page_gotno = 0;
4751 g->assigned_gotno = 0;
4752 g->bfd2got = NULL;
4753 g->next = NULL;
4754 g->tls_ldm_offset = MINUS_ONE;
4755 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4756 mips_elf_got_entry_eq, NULL);
4757 if (g->got_entries == NULL)
4758 return FALSE;
4759 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4760 mips_got_page_entry_eq, NULL);
4761 if (g->got_page_entries == NULL)
4762 return FALSE;
4763 htab->got_info = g;
4764 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4765 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4766
4767 /* We also need a .got.plt section when generating PLTs. */
4768 s = bfd_make_section_with_flags (abfd, ".got.plt",
4769 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4770 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4771 if (s == NULL)
4772 return FALSE;
4773 htab->sgotplt = s;
4774
4775 return TRUE;
4776 }
4777 \f
4778 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4779 __GOTT_INDEX__ symbols. These symbols are only special for
4780 shared objects; they are not used in executables. */
4781
4782 static bfd_boolean
4783 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4784 {
4785 return (mips_elf_hash_table (info)->is_vxworks
4786 && info->shared
4787 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4788 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4789 }
4790
4791 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4792 require an la25 stub. See also mips_elf_local_pic_function_p,
4793 which determines whether the destination function ever requires a
4794 stub. */
4795
4796 static bfd_boolean
4797 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4798 {
4799 /* We specifically ignore branches and jumps from EF_PIC objects,
4800 where the onus is on the compiler or programmer to perform any
4801 necessary initialization of $25. Sometimes such initialization
4802 is unnecessary; for example, -mno-shared functions do not use
4803 the incoming value of $25, and may therefore be called directly. */
4804 if (PIC_OBJECT_P (input_bfd))
4805 return FALSE;
4806
4807 switch (r_type)
4808 {
4809 case R_MIPS_26:
4810 case R_MIPS_PC16:
4811 case R_MIPS16_26:
4812 return TRUE;
4813
4814 default:
4815 return FALSE;
4816 }
4817 }
4818 \f
4819 /* Calculate the value produced by the RELOCATION (which comes from
4820 the INPUT_BFD). The ADDEND is the addend to use for this
4821 RELOCATION; RELOCATION->R_ADDEND is ignored.
4822
4823 The result of the relocation calculation is stored in VALUEP.
4824 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4825 is a MIPS16 jump to non-MIPS16 code, or vice versa.
4826
4827 This function returns bfd_reloc_continue if the caller need take no
4828 further action regarding this relocation, bfd_reloc_notsupported if
4829 something goes dramatically wrong, bfd_reloc_overflow if an
4830 overflow occurs, and bfd_reloc_ok to indicate success. */
4831
4832 static bfd_reloc_status_type
4833 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4834 asection *input_section,
4835 struct bfd_link_info *info,
4836 const Elf_Internal_Rela *relocation,
4837 bfd_vma addend, reloc_howto_type *howto,
4838 Elf_Internal_Sym *local_syms,
4839 asection **local_sections, bfd_vma *valuep,
4840 const char **namep,
4841 bfd_boolean *cross_mode_jump_p,
4842 bfd_boolean save_addend)
4843 {
4844 /* The eventual value we will return. */
4845 bfd_vma value;
4846 /* The address of the symbol against which the relocation is
4847 occurring. */
4848 bfd_vma symbol = 0;
4849 /* The final GP value to be used for the relocatable, executable, or
4850 shared object file being produced. */
4851 bfd_vma gp;
4852 /* The place (section offset or address) of the storage unit being
4853 relocated. */
4854 bfd_vma p;
4855 /* The value of GP used to create the relocatable object. */
4856 bfd_vma gp0;
4857 /* The offset into the global offset table at which the address of
4858 the relocation entry symbol, adjusted by the addend, resides
4859 during execution. */
4860 bfd_vma g = MINUS_ONE;
4861 /* The section in which the symbol referenced by the relocation is
4862 located. */
4863 asection *sec = NULL;
4864 struct mips_elf_link_hash_entry *h = NULL;
4865 /* TRUE if the symbol referred to by this relocation is a local
4866 symbol. */
4867 bfd_boolean local_p, was_local_p;
4868 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4869 bfd_boolean gp_disp_p = FALSE;
4870 /* TRUE if the symbol referred to by this relocation is
4871 "__gnu_local_gp". */
4872 bfd_boolean gnu_local_gp_p = FALSE;
4873 Elf_Internal_Shdr *symtab_hdr;
4874 size_t extsymoff;
4875 unsigned long r_symndx;
4876 int r_type;
4877 /* TRUE if overflow occurred during the calculation of the
4878 relocation value. */
4879 bfd_boolean overflowed_p;
4880 /* TRUE if this relocation refers to a MIPS16 function. */
4881 bfd_boolean target_is_16_bit_code_p = FALSE;
4882 struct mips_elf_link_hash_table *htab;
4883 bfd *dynobj;
4884
4885 dynobj = elf_hash_table (info)->dynobj;
4886 htab = mips_elf_hash_table (info);
4887 BFD_ASSERT (htab != NULL);
4888
4889 /* Parse the relocation. */
4890 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4891 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4892 p = (input_section->output_section->vma
4893 + input_section->output_offset
4894 + relocation->r_offset);
4895
4896 /* Assume that there will be no overflow. */
4897 overflowed_p = FALSE;
4898
4899 /* Figure out whether or not the symbol is local, and get the offset
4900 used in the array of hash table entries. */
4901 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4902 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4903 local_sections, FALSE);
4904 was_local_p = local_p;
4905 if (! elf_bad_symtab (input_bfd))
4906 extsymoff = symtab_hdr->sh_info;
4907 else
4908 {
4909 /* The symbol table does not follow the rule that local symbols
4910 must come before globals. */
4911 extsymoff = 0;
4912 }
4913
4914 /* Figure out the value of the symbol. */
4915 if (local_p)
4916 {
4917 Elf_Internal_Sym *sym;
4918
4919 sym = local_syms + r_symndx;
4920 sec = local_sections[r_symndx];
4921
4922 symbol = sec->output_section->vma + sec->output_offset;
4923 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4924 || (sec->flags & SEC_MERGE))
4925 symbol += sym->st_value;
4926 if ((sec->flags & SEC_MERGE)
4927 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4928 {
4929 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4930 addend -= symbol;
4931 addend += sec->output_section->vma + sec->output_offset;
4932 }
4933
4934 /* MIPS16 text labels should be treated as odd. */
4935 if (ELF_ST_IS_MIPS16 (sym->st_other))
4936 ++symbol;
4937
4938 /* Record the name of this symbol, for our caller. */
4939 *namep = bfd_elf_string_from_elf_section (input_bfd,
4940 symtab_hdr->sh_link,
4941 sym->st_name);
4942 if (*namep == '\0')
4943 *namep = bfd_section_name (input_bfd, sec);
4944
4945 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4946 }
4947 else
4948 {
4949 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4950
4951 /* For global symbols we look up the symbol in the hash-table. */
4952 h = ((struct mips_elf_link_hash_entry *)
4953 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4954 /* Find the real hash-table entry for this symbol. */
4955 while (h->root.root.type == bfd_link_hash_indirect
4956 || h->root.root.type == bfd_link_hash_warning)
4957 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4958
4959 /* Record the name of this symbol, for our caller. */
4960 *namep = h->root.root.root.string;
4961
4962 /* See if this is the special _gp_disp symbol. Note that such a
4963 symbol must always be a global symbol. */
4964 if (strcmp (*namep, "_gp_disp") == 0
4965 && ! NEWABI_P (input_bfd))
4966 {
4967 /* Relocations against _gp_disp are permitted only with
4968 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
4969 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4970 return bfd_reloc_notsupported;
4971
4972 gp_disp_p = TRUE;
4973 }
4974 /* See if this is the special _gp symbol. Note that such a
4975 symbol must always be a global symbol. */
4976 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4977 gnu_local_gp_p = TRUE;
4978
4979
4980 /* If this symbol is defined, calculate its address. Note that
4981 _gp_disp is a magic symbol, always implicitly defined by the
4982 linker, so it's inappropriate to check to see whether or not
4983 its defined. */
4984 else if ((h->root.root.type == bfd_link_hash_defined
4985 || h->root.root.type == bfd_link_hash_defweak)
4986 && h->root.root.u.def.section)
4987 {
4988 sec = h->root.root.u.def.section;
4989 if (sec->output_section)
4990 symbol = (h->root.root.u.def.value
4991 + sec->output_section->vma
4992 + sec->output_offset);
4993 else
4994 symbol = h->root.root.u.def.value;
4995 }
4996 else if (h->root.root.type == bfd_link_hash_undefweak)
4997 /* We allow relocations against undefined weak symbols, giving
4998 it the value zero, so that you can undefined weak functions
4999 and check to see if they exist by looking at their
5000 addresses. */
5001 symbol = 0;
5002 else if (info->unresolved_syms_in_objects == RM_IGNORE
5003 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5004 symbol = 0;
5005 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5006 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5007 {
5008 /* If this is a dynamic link, we should have created a
5009 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5010 in in _bfd_mips_elf_create_dynamic_sections.
5011 Otherwise, we should define the symbol with a value of 0.
5012 FIXME: It should probably get into the symbol table
5013 somehow as well. */
5014 BFD_ASSERT (! info->shared);
5015 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5016 symbol = 0;
5017 }
5018 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5019 {
5020 /* This is an optional symbol - an Irix specific extension to the
5021 ELF spec. Ignore it for now.
5022 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5023 than simply ignoring them, but we do not handle this for now.
5024 For information see the "64-bit ELF Object File Specification"
5025 which is available from here:
5026 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5027 symbol = 0;
5028 }
5029 else if ((*info->callbacks->undefined_symbol)
5030 (info, h->root.root.root.string, input_bfd,
5031 input_section, relocation->r_offset,
5032 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5033 || ELF_ST_VISIBILITY (h->root.other)))
5034 {
5035 return bfd_reloc_undefined;
5036 }
5037 else
5038 {
5039 return bfd_reloc_notsupported;
5040 }
5041
5042 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5043 }
5044
5045 /* If this is a reference to a 16-bit function with a stub, we need
5046 to redirect the relocation to the stub unless:
5047
5048 (a) the relocation is for a MIPS16 JAL;
5049
5050 (b) the relocation is for a MIPS16 PIC call, and there are no
5051 non-MIPS16 uses of the GOT slot; or
5052
5053 (c) the section allows direct references to MIPS16 functions. */
5054 if (r_type != R_MIPS16_26
5055 && !info->relocatable
5056 && ((h != NULL
5057 && h->fn_stub != NULL
5058 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5059 || (local_p
5060 && elf_tdata (input_bfd)->local_stubs != NULL
5061 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5062 && !section_allows_mips16_refs_p (input_section))
5063 {
5064 /* This is a 32- or 64-bit call to a 16-bit function. We should
5065 have already noticed that we were going to need the
5066 stub. */
5067 if (local_p)
5068 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5069 else
5070 {
5071 BFD_ASSERT (h->need_fn_stub);
5072 sec = h->fn_stub;
5073 }
5074
5075 symbol = sec->output_section->vma + sec->output_offset;
5076 /* The target is 16-bit, but the stub isn't. */
5077 target_is_16_bit_code_p = FALSE;
5078 }
5079 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5080 need to redirect the call to the stub. Note that we specifically
5081 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5082 use an indirect stub instead. */
5083 else if (r_type == R_MIPS16_26 && !info->relocatable
5084 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5085 || (local_p
5086 && elf_tdata (input_bfd)->local_call_stubs != NULL
5087 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5088 && !target_is_16_bit_code_p)
5089 {
5090 if (local_p)
5091 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5092 else
5093 {
5094 /* If both call_stub and call_fp_stub are defined, we can figure
5095 out which one to use by checking which one appears in the input
5096 file. */
5097 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5098 {
5099 asection *o;
5100
5101 sec = NULL;
5102 for (o = input_bfd->sections; o != NULL; o = o->next)
5103 {
5104 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5105 {
5106 sec = h->call_fp_stub;
5107 break;
5108 }
5109 }
5110 if (sec == NULL)
5111 sec = h->call_stub;
5112 }
5113 else if (h->call_stub != NULL)
5114 sec = h->call_stub;
5115 else
5116 sec = h->call_fp_stub;
5117 }
5118
5119 BFD_ASSERT (sec->size > 0);
5120 symbol = sec->output_section->vma + sec->output_offset;
5121 }
5122 /* If this is a direct call to a PIC function, redirect to the
5123 non-PIC stub. */
5124 else if (h != NULL && h->la25_stub
5125 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5126 symbol = (h->la25_stub->stub_section->output_section->vma
5127 + h->la25_stub->stub_section->output_offset
5128 + h->la25_stub->offset);
5129
5130 /* Calls from 16-bit code to 32-bit code and vice versa require the
5131 mode change. */
5132 *cross_mode_jump_p = !info->relocatable
5133 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5134 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5135 && target_is_16_bit_code_p));
5136
5137 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5138 local_sections, TRUE);
5139
5140 gp0 = _bfd_get_gp_value (input_bfd);
5141 gp = _bfd_get_gp_value (abfd);
5142 if (htab->got_info)
5143 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5144
5145 if (gnu_local_gp_p)
5146 symbol = gp;
5147
5148 /* If we haven't already determined the GOT offset, oand we're going
5149 to need it, get it now. */
5150 switch (r_type)
5151 {
5152 case R_MIPS_GOT_PAGE:
5153 case R_MIPS_GOT_OFST:
5154 /* We need to decay to GOT_DISP/addend if the symbol doesn't
5155 bind locally. */
5156 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
5157 if (local_p || r_type == R_MIPS_GOT_OFST)
5158 break;
5159 /* Fall through. */
5160
5161 case R_MIPS16_CALL16:
5162 case R_MIPS16_GOT16:
5163 case R_MIPS_CALL16:
5164 case R_MIPS_GOT16:
5165 case R_MIPS_GOT_DISP:
5166 case R_MIPS_GOT_HI16:
5167 case R_MIPS_CALL_HI16:
5168 case R_MIPS_GOT_LO16:
5169 case R_MIPS_CALL_LO16:
5170 case R_MIPS_TLS_GD:
5171 case R_MIPS_TLS_GOTTPREL:
5172 case R_MIPS_TLS_LDM:
5173 /* Find the index into the GOT where this value is located. */
5174 if (r_type == R_MIPS_TLS_LDM)
5175 {
5176 g = mips_elf_local_got_index (abfd, input_bfd, info,
5177 0, 0, NULL, r_type);
5178 if (g == MINUS_ONE)
5179 return bfd_reloc_outofrange;
5180 }
5181 else if (!local_p)
5182 {
5183 /* On VxWorks, CALL relocations should refer to the .got.plt
5184 entry, which is initialized to point at the PLT stub. */
5185 if (htab->is_vxworks
5186 && (r_type == R_MIPS_CALL_HI16
5187 || r_type == R_MIPS_CALL_LO16
5188 || call16_reloc_p (r_type)))
5189 {
5190 BFD_ASSERT (addend == 0);
5191 BFD_ASSERT (h->root.needs_plt);
5192 g = mips_elf_gotplt_index (info, &h->root);
5193 }
5194 else
5195 {
5196 /* GOT_PAGE may take a non-zero addend, that is ignored in a
5197 GOT_PAGE relocation that decays to GOT_DISP because the
5198 symbol turns out to be global. The addend is then added
5199 as GOT_OFST. */
5200 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5201 g = mips_elf_global_got_index (dynobj, input_bfd,
5202 &h->root, r_type, info);
5203 if (h->tls_type == GOT_NORMAL
5204 && (! elf_hash_table(info)->dynamic_sections_created
5205 || (info->shared
5206 && (info->symbolic || h->root.forced_local)
5207 && h->root.def_regular)))
5208 /* This is a static link or a -Bsymbolic link. The
5209 symbol is defined locally, or was forced to be local.
5210 We must initialize this entry in the GOT. */
5211 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5212 }
5213 }
5214 else if (!htab->is_vxworks
5215 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5216 /* The calculation below does not involve "g". */
5217 break;
5218 else
5219 {
5220 g = mips_elf_local_got_index (abfd, input_bfd, info,
5221 symbol + addend, r_symndx, h, r_type);
5222 if (g == MINUS_ONE)
5223 return bfd_reloc_outofrange;
5224 }
5225
5226 /* Convert GOT indices to actual offsets. */
5227 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5228 break;
5229 }
5230
5231 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5232 symbols are resolved by the loader. Add them to .rela.dyn. */
5233 if (h != NULL && is_gott_symbol (info, &h->root))
5234 {
5235 Elf_Internal_Rela outrel;
5236 bfd_byte *loc;
5237 asection *s;
5238
5239 s = mips_elf_rel_dyn_section (info, FALSE);
5240 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5241
5242 outrel.r_offset = (input_section->output_section->vma
5243 + input_section->output_offset
5244 + relocation->r_offset);
5245 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5246 outrel.r_addend = addend;
5247 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5248
5249 /* If we've written this relocation for a readonly section,
5250 we need to set DF_TEXTREL again, so that we do not delete the
5251 DT_TEXTREL tag. */
5252 if (MIPS_ELF_READONLY_SECTION (input_section))
5253 info->flags |= DF_TEXTREL;
5254
5255 *valuep = 0;
5256 return bfd_reloc_ok;
5257 }
5258
5259 /* Figure out what kind of relocation is being performed. */
5260 switch (r_type)
5261 {
5262 case R_MIPS_NONE:
5263 return bfd_reloc_continue;
5264
5265 case R_MIPS_16:
5266 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5267 overflowed_p = mips_elf_overflow_p (value, 16);
5268 break;
5269
5270 case R_MIPS_32:
5271 case R_MIPS_REL32:
5272 case R_MIPS_64:
5273 if ((info->shared
5274 || (htab->root.dynamic_sections_created
5275 && h != NULL
5276 && h->root.def_dynamic
5277 && !h->root.def_regular
5278 && !h->has_static_relocs))
5279 && r_symndx != 0
5280 && (h == NULL
5281 || h->root.root.type != bfd_link_hash_undefweak
5282 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5283 && (input_section->flags & SEC_ALLOC) != 0)
5284 {
5285 /* If we're creating a shared library, then we can't know
5286 where the symbol will end up. So, we create a relocation
5287 record in the output, and leave the job up to the dynamic
5288 linker. We must do the same for executable references to
5289 shared library symbols, unless we've decided to use copy
5290 relocs or PLTs instead. */
5291 value = addend;
5292 if (!mips_elf_create_dynamic_relocation (abfd,
5293 info,
5294 relocation,
5295 h,
5296 sec,
5297 symbol,
5298 &value,
5299 input_section))
5300 return bfd_reloc_undefined;
5301 }
5302 else
5303 {
5304 if (r_type != R_MIPS_REL32)
5305 value = symbol + addend;
5306 else
5307 value = addend;
5308 }
5309 value &= howto->dst_mask;
5310 break;
5311
5312 case R_MIPS_PC32:
5313 value = symbol + addend - p;
5314 value &= howto->dst_mask;
5315 break;
5316
5317 case R_MIPS16_26:
5318 /* The calculation for R_MIPS16_26 is just the same as for an
5319 R_MIPS_26. It's only the storage of the relocated field into
5320 the output file that's different. That's handled in
5321 mips_elf_perform_relocation. So, we just fall through to the
5322 R_MIPS_26 case here. */
5323 case R_MIPS_26:
5324 if (local_p)
5325 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
5326 else
5327 {
5328 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
5329 if (h->root.root.type != bfd_link_hash_undefweak)
5330 overflowed_p = (value >> 26) != ((p + 4) >> 28);
5331 }
5332 value &= howto->dst_mask;
5333 break;
5334
5335 case R_MIPS_TLS_DTPREL_HI16:
5336 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5337 & howto->dst_mask);
5338 break;
5339
5340 case R_MIPS_TLS_DTPREL_LO16:
5341 case R_MIPS_TLS_DTPREL32:
5342 case R_MIPS_TLS_DTPREL64:
5343 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5344 break;
5345
5346 case R_MIPS_TLS_TPREL_HI16:
5347 value = (mips_elf_high (addend + symbol - tprel_base (info))
5348 & howto->dst_mask);
5349 break;
5350
5351 case R_MIPS_TLS_TPREL_LO16:
5352 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5353 break;
5354
5355 case R_MIPS_HI16:
5356 case R_MIPS16_HI16:
5357 if (!gp_disp_p)
5358 {
5359 value = mips_elf_high (addend + symbol);
5360 value &= howto->dst_mask;
5361 }
5362 else
5363 {
5364 /* For MIPS16 ABI code we generate this sequence
5365 0: li $v0,%hi(_gp_disp)
5366 4: addiupc $v1,%lo(_gp_disp)
5367 8: sll $v0,16
5368 12: addu $v0,$v1
5369 14: move $gp,$v0
5370 So the offsets of hi and lo relocs are the same, but the
5371 $pc is four higher than $t9 would be, so reduce
5372 both reloc addends by 4. */
5373 if (r_type == R_MIPS16_HI16)
5374 value = mips_elf_high (addend + gp - p - 4);
5375 else
5376 value = mips_elf_high (addend + gp - p);
5377 overflowed_p = mips_elf_overflow_p (value, 16);
5378 }
5379 break;
5380
5381 case R_MIPS_LO16:
5382 case R_MIPS16_LO16:
5383 if (!gp_disp_p)
5384 value = (symbol + addend) & howto->dst_mask;
5385 else
5386 {
5387 /* See the comment for R_MIPS16_HI16 above for the reason
5388 for this conditional. */
5389 if (r_type == R_MIPS16_LO16)
5390 value = addend + gp - p;
5391 else
5392 value = addend + gp - p + 4;
5393 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5394 for overflow. But, on, say, IRIX5, relocations against
5395 _gp_disp are normally generated from the .cpload
5396 pseudo-op. It generates code that normally looks like
5397 this:
5398
5399 lui $gp,%hi(_gp_disp)
5400 addiu $gp,$gp,%lo(_gp_disp)
5401 addu $gp,$gp,$t9
5402
5403 Here $t9 holds the address of the function being called,
5404 as required by the MIPS ELF ABI. The R_MIPS_LO16
5405 relocation can easily overflow in this situation, but the
5406 R_MIPS_HI16 relocation will handle the overflow.
5407 Therefore, we consider this a bug in the MIPS ABI, and do
5408 not check for overflow here. */
5409 }
5410 break;
5411
5412 case R_MIPS_LITERAL:
5413 /* Because we don't merge literal sections, we can handle this
5414 just like R_MIPS_GPREL16. In the long run, we should merge
5415 shared literals, and then we will need to additional work
5416 here. */
5417
5418 /* Fall through. */
5419
5420 case R_MIPS16_GPREL:
5421 /* The R_MIPS16_GPREL performs the same calculation as
5422 R_MIPS_GPREL16, but stores the relocated bits in a different
5423 order. We don't need to do anything special here; the
5424 differences are handled in mips_elf_perform_relocation. */
5425 case R_MIPS_GPREL16:
5426 /* Only sign-extend the addend if it was extracted from the
5427 instruction. If the addend was separate, leave it alone,
5428 otherwise we may lose significant bits. */
5429 if (howto->partial_inplace)
5430 addend = _bfd_mips_elf_sign_extend (addend, 16);
5431 value = symbol + addend - gp;
5432 /* If the symbol was local, any earlier relocatable links will
5433 have adjusted its addend with the gp offset, so compensate
5434 for that now. Don't do it for symbols forced local in this
5435 link, though, since they won't have had the gp offset applied
5436 to them before. */
5437 if (was_local_p)
5438 value += gp0;
5439 overflowed_p = mips_elf_overflow_p (value, 16);
5440 break;
5441
5442 case R_MIPS16_GOT16:
5443 case R_MIPS16_CALL16:
5444 case R_MIPS_GOT16:
5445 case R_MIPS_CALL16:
5446 /* VxWorks does not have separate local and global semantics for
5447 R_MIPS*_GOT16; every relocation evaluates to "G". */
5448 if (!htab->is_vxworks && local_p)
5449 {
5450 bfd_boolean forced;
5451
5452 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
5453 local_sections, FALSE);
5454 value = mips_elf_got16_entry (abfd, input_bfd, info,
5455 symbol + addend, forced);
5456 if (value == MINUS_ONE)
5457 return bfd_reloc_outofrange;
5458 value
5459 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5460 overflowed_p = mips_elf_overflow_p (value, 16);
5461 break;
5462 }
5463
5464 /* Fall through. */
5465
5466 case R_MIPS_TLS_GD:
5467 case R_MIPS_TLS_GOTTPREL:
5468 case R_MIPS_TLS_LDM:
5469 case R_MIPS_GOT_DISP:
5470 got_disp:
5471 value = g;
5472 overflowed_p = mips_elf_overflow_p (value, 16);
5473 break;
5474
5475 case R_MIPS_GPREL32:
5476 value = (addend + symbol + gp0 - gp);
5477 if (!save_addend)
5478 value &= howto->dst_mask;
5479 break;
5480
5481 case R_MIPS_PC16:
5482 case R_MIPS_GNU_REL16_S2:
5483 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5484 overflowed_p = mips_elf_overflow_p (value, 18);
5485 value >>= howto->rightshift;
5486 value &= howto->dst_mask;
5487 break;
5488
5489 case R_MIPS_GOT_HI16:
5490 case R_MIPS_CALL_HI16:
5491 /* We're allowed to handle these two relocations identically.
5492 The dynamic linker is allowed to handle the CALL relocations
5493 differently by creating a lazy evaluation stub. */
5494 value = g;
5495 value = mips_elf_high (value);
5496 value &= howto->dst_mask;
5497 break;
5498
5499 case R_MIPS_GOT_LO16:
5500 case R_MIPS_CALL_LO16:
5501 value = g & howto->dst_mask;
5502 break;
5503
5504 case R_MIPS_GOT_PAGE:
5505 /* GOT_PAGE relocations that reference non-local symbols decay
5506 to GOT_DISP. The corresponding GOT_OFST relocation decays to
5507 0. */
5508 if (! local_p)
5509 goto got_disp;
5510 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5511 if (value == MINUS_ONE)
5512 return bfd_reloc_outofrange;
5513 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5514 overflowed_p = mips_elf_overflow_p (value, 16);
5515 break;
5516
5517 case R_MIPS_GOT_OFST:
5518 if (local_p)
5519 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5520 else
5521 value = addend;
5522 overflowed_p = mips_elf_overflow_p (value, 16);
5523 break;
5524
5525 case R_MIPS_SUB:
5526 value = symbol - addend;
5527 value &= howto->dst_mask;
5528 break;
5529
5530 case R_MIPS_HIGHER:
5531 value = mips_elf_higher (addend + symbol);
5532 value &= howto->dst_mask;
5533 break;
5534
5535 case R_MIPS_HIGHEST:
5536 value = mips_elf_highest (addend + symbol);
5537 value &= howto->dst_mask;
5538 break;
5539
5540 case R_MIPS_SCN_DISP:
5541 value = symbol + addend - sec->output_offset;
5542 value &= howto->dst_mask;
5543 break;
5544
5545 case R_MIPS_JALR:
5546 /* This relocation is only a hint. In some cases, we optimize
5547 it into a bal instruction. But we don't try to optimize
5548 when the symbol does not resolve locally. */
5549 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5550 return bfd_reloc_continue;
5551 value = symbol + addend;
5552 break;
5553
5554 case R_MIPS_PJUMP:
5555 case R_MIPS_GNU_VTINHERIT:
5556 case R_MIPS_GNU_VTENTRY:
5557 /* We don't do anything with these at present. */
5558 return bfd_reloc_continue;
5559
5560 default:
5561 /* An unrecognized relocation type. */
5562 return bfd_reloc_notsupported;
5563 }
5564
5565 /* Store the VALUE for our caller. */
5566 *valuep = value;
5567 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5568 }
5569
5570 /* Obtain the field relocated by RELOCATION. */
5571
5572 static bfd_vma
5573 mips_elf_obtain_contents (reloc_howto_type *howto,
5574 const Elf_Internal_Rela *relocation,
5575 bfd *input_bfd, bfd_byte *contents)
5576 {
5577 bfd_vma x;
5578 bfd_byte *location = contents + relocation->r_offset;
5579
5580 /* Obtain the bytes. */
5581 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5582
5583 return x;
5584 }
5585
5586 /* It has been determined that the result of the RELOCATION is the
5587 VALUE. Use HOWTO to place VALUE into the output file at the
5588 appropriate position. The SECTION is the section to which the
5589 relocation applies.
5590 CROSS_MODE_JUMP_P is true if the relocation field
5591 is a MIPS16 jump to non-MIPS16 code, or vice versa.
5592
5593 Returns FALSE if anything goes wrong. */
5594
5595 static bfd_boolean
5596 mips_elf_perform_relocation (struct bfd_link_info *info,
5597 reloc_howto_type *howto,
5598 const Elf_Internal_Rela *relocation,
5599 bfd_vma value, bfd *input_bfd,
5600 asection *input_section, bfd_byte *contents,
5601 bfd_boolean cross_mode_jump_p)
5602 {
5603 bfd_vma x;
5604 bfd_byte *location;
5605 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5606
5607 /* Figure out where the relocation is occurring. */
5608 location = contents + relocation->r_offset;
5609
5610 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5611
5612 /* Obtain the current value. */
5613 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5614
5615 /* Clear the field we are setting. */
5616 x &= ~howto->dst_mask;
5617
5618 /* Set the field. */
5619 x |= (value & howto->dst_mask);
5620
5621 /* If required, turn JAL into JALX. */
5622 if (cross_mode_jump_p && jal_reloc_p (r_type))
5623 {
5624 bfd_boolean ok;
5625 bfd_vma opcode = x >> 26;
5626 bfd_vma jalx_opcode;
5627
5628 /* Check to see if the opcode is already JAL or JALX. */
5629 if (r_type == R_MIPS16_26)
5630 {
5631 ok = ((opcode == 0x6) || (opcode == 0x7));
5632 jalx_opcode = 0x7;
5633 }
5634 else
5635 {
5636 ok = ((opcode == 0x3) || (opcode == 0x1d));
5637 jalx_opcode = 0x1d;
5638 }
5639
5640 /* If the opcode is not JAL or JALX, there's a problem. */
5641 if (!ok)
5642 {
5643 (*_bfd_error_handler)
5644 (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
5645 input_bfd,
5646 input_section,
5647 (unsigned long) relocation->r_offset);
5648 bfd_set_error (bfd_error_bad_value);
5649 return FALSE;
5650 }
5651
5652 /* Make this the JALX opcode. */
5653 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5654 }
5655
5656 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5657 range. */
5658 if (!info->relocatable
5659 && !cross_mode_jump_p
5660 && ((JAL_TO_BAL_P (input_bfd)
5661 && r_type == R_MIPS_26
5662 && (x >> 26) == 0x3) /* jal addr */
5663 || (JALR_TO_BAL_P (input_bfd)
5664 && r_type == R_MIPS_JALR
5665 && x == 0x0320f809) /* jalr t9 */
5666 || (JR_TO_B_P (input_bfd)
5667 && r_type == R_MIPS_JALR
5668 && x == 0x03200008))) /* jr t9 */
5669 {
5670 bfd_vma addr;
5671 bfd_vma dest;
5672 bfd_signed_vma off;
5673
5674 addr = (input_section->output_section->vma
5675 + input_section->output_offset
5676 + relocation->r_offset
5677 + 4);
5678 if (r_type == R_MIPS_26)
5679 dest = (value << 2) | ((addr >> 28) << 28);
5680 else
5681 dest = value;
5682 off = dest - addr;
5683 if (off <= 0x1ffff && off >= -0x20000)
5684 {
5685 if (x == 0x03200008) /* jr t9 */
5686 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5687 else
5688 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5689 }
5690 }
5691
5692 /* Put the value into the output. */
5693 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5694
5695 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5696 location);
5697
5698 return TRUE;
5699 }
5700 \f
5701 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5702 is the original relocation, which is now being transformed into a
5703 dynamic relocation. The ADDENDP is adjusted if necessary; the
5704 caller should store the result in place of the original addend. */
5705
5706 static bfd_boolean
5707 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5708 struct bfd_link_info *info,
5709 const Elf_Internal_Rela *rel,
5710 struct mips_elf_link_hash_entry *h,
5711 asection *sec, bfd_vma symbol,
5712 bfd_vma *addendp, asection *input_section)
5713 {
5714 Elf_Internal_Rela outrel[3];
5715 asection *sreloc;
5716 bfd *dynobj;
5717 int r_type;
5718 long indx;
5719 bfd_boolean defined_p;
5720 struct mips_elf_link_hash_table *htab;
5721
5722 htab = mips_elf_hash_table (info);
5723 BFD_ASSERT (htab != NULL);
5724
5725 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5726 dynobj = elf_hash_table (info)->dynobj;
5727 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5728 BFD_ASSERT (sreloc != NULL);
5729 BFD_ASSERT (sreloc->contents != NULL);
5730 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5731 < sreloc->size);
5732
5733 outrel[0].r_offset =
5734 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5735 if (ABI_64_P (output_bfd))
5736 {
5737 outrel[1].r_offset =
5738 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5739 outrel[2].r_offset =
5740 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5741 }
5742
5743 if (outrel[0].r_offset == MINUS_ONE)
5744 /* The relocation field has been deleted. */
5745 return TRUE;
5746
5747 if (outrel[0].r_offset == MINUS_TWO)
5748 {
5749 /* The relocation field has been converted into a relative value of
5750 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5751 the field to be fully relocated, so add in the symbol's value. */
5752 *addendp += symbol;
5753 return TRUE;
5754 }
5755
5756 /* We must now calculate the dynamic symbol table index to use
5757 in the relocation. */
5758 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5759 {
5760 indx = h->root.dynindx;
5761 if (SGI_COMPAT (output_bfd))
5762 defined_p = h->root.def_regular;
5763 else
5764 /* ??? glibc's ld.so just adds the final GOT entry to the
5765 relocation field. It therefore treats relocs against
5766 defined symbols in the same way as relocs against
5767 undefined symbols. */
5768 defined_p = FALSE;
5769 }
5770 else
5771 {
5772 if (sec != NULL && bfd_is_abs_section (sec))
5773 indx = 0;
5774 else if (sec == NULL || sec->owner == NULL)
5775 {
5776 bfd_set_error (bfd_error_bad_value);
5777 return FALSE;
5778 }
5779 else
5780 {
5781 indx = elf_section_data (sec->output_section)->dynindx;
5782 if (indx == 0)
5783 {
5784 asection *osec = htab->root.text_index_section;
5785 indx = elf_section_data (osec)->dynindx;
5786 }
5787 if (indx == 0)
5788 abort ();
5789 }
5790
5791 /* Instead of generating a relocation using the section
5792 symbol, we may as well make it a fully relative
5793 relocation. We want to avoid generating relocations to
5794 local symbols because we used to generate them
5795 incorrectly, without adding the original symbol value,
5796 which is mandated by the ABI for section symbols. In
5797 order to give dynamic loaders and applications time to
5798 phase out the incorrect use, we refrain from emitting
5799 section-relative relocations. It's not like they're
5800 useful, after all. This should be a bit more efficient
5801 as well. */
5802 /* ??? Although this behavior is compatible with glibc's ld.so,
5803 the ABI says that relocations against STN_UNDEF should have
5804 a symbol value of 0. Irix rld honors this, so relocations
5805 against STN_UNDEF have no effect. */
5806 if (!SGI_COMPAT (output_bfd))
5807 indx = 0;
5808 defined_p = TRUE;
5809 }
5810
5811 /* If the relocation was previously an absolute relocation and
5812 this symbol will not be referred to by the relocation, we must
5813 adjust it by the value we give it in the dynamic symbol table.
5814 Otherwise leave the job up to the dynamic linker. */
5815 if (defined_p && r_type != R_MIPS_REL32)
5816 *addendp += symbol;
5817
5818 if (htab->is_vxworks)
5819 /* VxWorks uses non-relative relocations for this. */
5820 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5821 else
5822 /* The relocation is always an REL32 relocation because we don't
5823 know where the shared library will wind up at load-time. */
5824 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5825 R_MIPS_REL32);
5826
5827 /* For strict adherence to the ABI specification, we should
5828 generate a R_MIPS_64 relocation record by itself before the
5829 _REL32/_64 record as well, such that the addend is read in as
5830 a 64-bit value (REL32 is a 32-bit relocation, after all).
5831 However, since none of the existing ELF64 MIPS dynamic
5832 loaders seems to care, we don't waste space with these
5833 artificial relocations. If this turns out to not be true,
5834 mips_elf_allocate_dynamic_relocation() should be tweaked so
5835 as to make room for a pair of dynamic relocations per
5836 invocation if ABI_64_P, and here we should generate an
5837 additional relocation record with R_MIPS_64 by itself for a
5838 NULL symbol before this relocation record. */
5839 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5840 ABI_64_P (output_bfd)
5841 ? R_MIPS_64
5842 : R_MIPS_NONE);
5843 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5844
5845 /* Adjust the output offset of the relocation to reference the
5846 correct location in the output file. */
5847 outrel[0].r_offset += (input_section->output_section->vma
5848 + input_section->output_offset);
5849 outrel[1].r_offset += (input_section->output_section->vma
5850 + input_section->output_offset);
5851 outrel[2].r_offset += (input_section->output_section->vma
5852 + input_section->output_offset);
5853
5854 /* Put the relocation back out. We have to use the special
5855 relocation outputter in the 64-bit case since the 64-bit
5856 relocation format is non-standard. */
5857 if (ABI_64_P (output_bfd))
5858 {
5859 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5860 (output_bfd, &outrel[0],
5861 (sreloc->contents
5862 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5863 }
5864 else if (htab->is_vxworks)
5865 {
5866 /* VxWorks uses RELA rather than REL dynamic relocations. */
5867 outrel[0].r_addend = *addendp;
5868 bfd_elf32_swap_reloca_out
5869 (output_bfd, &outrel[0],
5870 (sreloc->contents
5871 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5872 }
5873 else
5874 bfd_elf32_swap_reloc_out
5875 (output_bfd, &outrel[0],
5876 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5877
5878 /* We've now added another relocation. */
5879 ++sreloc->reloc_count;
5880
5881 /* Make sure the output section is writable. The dynamic linker
5882 will be writing to it. */
5883 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5884 |= SHF_WRITE;
5885
5886 /* On IRIX5, make an entry of compact relocation info. */
5887 if (IRIX_COMPAT (output_bfd) == ict_irix5)
5888 {
5889 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5890 bfd_byte *cr;
5891
5892 if (scpt)
5893 {
5894 Elf32_crinfo cptrel;
5895
5896 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5897 cptrel.vaddr = (rel->r_offset
5898 + input_section->output_section->vma
5899 + input_section->output_offset);
5900 if (r_type == R_MIPS_REL32)
5901 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5902 else
5903 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5904 mips_elf_set_cr_dist2to (cptrel, 0);
5905 cptrel.konst = *addendp;
5906
5907 cr = (scpt->contents
5908 + sizeof (Elf32_External_compact_rel));
5909 mips_elf_set_cr_relvaddr (cptrel, 0);
5910 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5911 ((Elf32_External_crinfo *) cr
5912 + scpt->reloc_count));
5913 ++scpt->reloc_count;
5914 }
5915 }
5916
5917 /* If we've written this relocation for a readonly section,
5918 we need to set DF_TEXTREL again, so that we do not delete the
5919 DT_TEXTREL tag. */
5920 if (MIPS_ELF_READONLY_SECTION (input_section))
5921 info->flags |= DF_TEXTREL;
5922
5923 return TRUE;
5924 }
5925 \f
5926 /* Return the MACH for a MIPS e_flags value. */
5927
5928 unsigned long
5929 _bfd_elf_mips_mach (flagword flags)
5930 {
5931 switch (flags & EF_MIPS_MACH)
5932 {
5933 case E_MIPS_MACH_3900:
5934 return bfd_mach_mips3900;
5935
5936 case E_MIPS_MACH_4010:
5937 return bfd_mach_mips4010;
5938
5939 case E_MIPS_MACH_4100:
5940 return bfd_mach_mips4100;
5941
5942 case E_MIPS_MACH_4111:
5943 return bfd_mach_mips4111;
5944
5945 case E_MIPS_MACH_4120:
5946 return bfd_mach_mips4120;
5947
5948 case E_MIPS_MACH_4650:
5949 return bfd_mach_mips4650;
5950
5951 case E_MIPS_MACH_5400:
5952 return bfd_mach_mips5400;
5953
5954 case E_MIPS_MACH_5500:
5955 return bfd_mach_mips5500;
5956
5957 case E_MIPS_MACH_9000:
5958 return bfd_mach_mips9000;
5959
5960 case E_MIPS_MACH_SB1:
5961 return bfd_mach_mips_sb1;
5962
5963 case E_MIPS_MACH_LS2E:
5964 return bfd_mach_mips_loongson_2e;
5965
5966 case E_MIPS_MACH_LS2F:
5967 return bfd_mach_mips_loongson_2f;
5968
5969 case E_MIPS_MACH_OCTEON:
5970 return bfd_mach_mips_octeon;
5971
5972 case E_MIPS_MACH_XLR:
5973 return bfd_mach_mips_xlr;
5974
5975 default:
5976 switch (flags & EF_MIPS_ARCH)
5977 {
5978 default:
5979 case E_MIPS_ARCH_1:
5980 return bfd_mach_mips3000;
5981
5982 case E_MIPS_ARCH_2:
5983 return bfd_mach_mips6000;
5984
5985 case E_MIPS_ARCH_3:
5986 return bfd_mach_mips4000;
5987
5988 case E_MIPS_ARCH_4:
5989 return bfd_mach_mips8000;
5990
5991 case E_MIPS_ARCH_5:
5992 return bfd_mach_mips5;
5993
5994 case E_MIPS_ARCH_32:
5995 return bfd_mach_mipsisa32;
5996
5997 case E_MIPS_ARCH_64:
5998 return bfd_mach_mipsisa64;
5999
6000 case E_MIPS_ARCH_32R2:
6001 return bfd_mach_mipsisa32r2;
6002
6003 case E_MIPS_ARCH_64R2:
6004 return bfd_mach_mipsisa64r2;
6005 }
6006 }
6007
6008 return 0;
6009 }
6010
6011 /* Return printable name for ABI. */
6012
6013 static INLINE char *
6014 elf_mips_abi_name (bfd *abfd)
6015 {
6016 flagword flags;
6017
6018 flags = elf_elfheader (abfd)->e_flags;
6019 switch (flags & EF_MIPS_ABI)
6020 {
6021 case 0:
6022 if (ABI_N32_P (abfd))
6023 return "N32";
6024 else if (ABI_64_P (abfd))
6025 return "64";
6026 else
6027 return "none";
6028 case E_MIPS_ABI_O32:
6029 return "O32";
6030 case E_MIPS_ABI_O64:
6031 return "O64";
6032 case E_MIPS_ABI_EABI32:
6033 return "EABI32";
6034 case E_MIPS_ABI_EABI64:
6035 return "EABI64";
6036 default:
6037 return "unknown abi";
6038 }
6039 }
6040 \f
6041 /* MIPS ELF uses two common sections. One is the usual one, and the
6042 other is for small objects. All the small objects are kept
6043 together, and then referenced via the gp pointer, which yields
6044 faster assembler code. This is what we use for the small common
6045 section. This approach is copied from ecoff.c. */
6046 static asection mips_elf_scom_section;
6047 static asymbol mips_elf_scom_symbol;
6048 static asymbol *mips_elf_scom_symbol_ptr;
6049
6050 /* MIPS ELF also uses an acommon section, which represents an
6051 allocated common symbol which may be overridden by a
6052 definition in a shared library. */
6053 static asection mips_elf_acom_section;
6054 static asymbol mips_elf_acom_symbol;
6055 static asymbol *mips_elf_acom_symbol_ptr;
6056
6057 /* This is used for both the 32-bit and the 64-bit ABI. */
6058
6059 void
6060 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6061 {
6062 elf_symbol_type *elfsym;
6063
6064 /* Handle the special MIPS section numbers that a symbol may use. */
6065 elfsym = (elf_symbol_type *) asym;
6066 switch (elfsym->internal_elf_sym.st_shndx)
6067 {
6068 case SHN_MIPS_ACOMMON:
6069 /* This section is used in a dynamically linked executable file.
6070 It is an allocated common section. The dynamic linker can
6071 either resolve these symbols to something in a shared
6072 library, or it can just leave them here. For our purposes,
6073 we can consider these symbols to be in a new section. */
6074 if (mips_elf_acom_section.name == NULL)
6075 {
6076 /* Initialize the acommon section. */
6077 mips_elf_acom_section.name = ".acommon";
6078 mips_elf_acom_section.flags = SEC_ALLOC;
6079 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6080 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6081 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6082 mips_elf_acom_symbol.name = ".acommon";
6083 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6084 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6085 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6086 }
6087 asym->section = &mips_elf_acom_section;
6088 break;
6089
6090 case SHN_COMMON:
6091 /* Common symbols less than the GP size are automatically
6092 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6093 if (asym->value > elf_gp_size (abfd)
6094 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6095 || IRIX_COMPAT (abfd) == ict_irix6)
6096 break;
6097 /* Fall through. */
6098 case SHN_MIPS_SCOMMON:
6099 if (mips_elf_scom_section.name == NULL)
6100 {
6101 /* Initialize the small common section. */
6102 mips_elf_scom_section.name = ".scommon";
6103 mips_elf_scom_section.flags = SEC_IS_COMMON;
6104 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6105 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6106 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6107 mips_elf_scom_symbol.name = ".scommon";
6108 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6109 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6110 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6111 }
6112 asym->section = &mips_elf_scom_section;
6113 asym->value = elfsym->internal_elf_sym.st_size;
6114 break;
6115
6116 case SHN_MIPS_SUNDEFINED:
6117 asym->section = bfd_und_section_ptr;
6118 break;
6119
6120 case SHN_MIPS_TEXT:
6121 {
6122 asection *section = bfd_get_section_by_name (abfd, ".text");
6123
6124 BFD_ASSERT (SGI_COMPAT (abfd));
6125 if (section != NULL)
6126 {
6127 asym->section = section;
6128 /* MIPS_TEXT is a bit special, the address is not an offset
6129 to the base of the .text section. So substract the section
6130 base address to make it an offset. */
6131 asym->value -= section->vma;
6132 }
6133 }
6134 break;
6135
6136 case SHN_MIPS_DATA:
6137 {
6138 asection *section = bfd_get_section_by_name (abfd, ".data");
6139
6140 BFD_ASSERT (SGI_COMPAT (abfd));
6141 if (section != NULL)
6142 {
6143 asym->section = section;
6144 /* MIPS_DATA is a bit special, the address is not an offset
6145 to the base of the .data section. So substract the section
6146 base address to make it an offset. */
6147 asym->value -= section->vma;
6148 }
6149 }
6150 break;
6151 }
6152
6153 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6154 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6155 && (asym->value & 1) != 0)
6156 {
6157 asym->value--;
6158 elfsym->internal_elf_sym.st_other
6159 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6160 }
6161 }
6162 \f
6163 /* Implement elf_backend_eh_frame_address_size. This differs from
6164 the default in the way it handles EABI64.
6165
6166 EABI64 was originally specified as an LP64 ABI, and that is what
6167 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6168 historically accepted the combination of -mabi=eabi and -mlong32,
6169 and this ILP32 variation has become semi-official over time.
6170 Both forms use elf32 and have pointer-sized FDE addresses.
6171
6172 If an EABI object was generated by GCC 4.0 or above, it will have
6173 an empty .gcc_compiled_longXX section, where XX is the size of longs
6174 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6175 have no special marking to distinguish them from LP64 objects.
6176
6177 We don't want users of the official LP64 ABI to be punished for the
6178 existence of the ILP32 variant, but at the same time, we don't want
6179 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6180 We therefore take the following approach:
6181
6182 - If ABFD contains a .gcc_compiled_longXX section, use it to
6183 determine the pointer size.
6184
6185 - Otherwise check the type of the first relocation. Assume that
6186 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6187
6188 - Otherwise punt.
6189
6190 The second check is enough to detect LP64 objects generated by pre-4.0
6191 compilers because, in the kind of output generated by those compilers,
6192 the first relocation will be associated with either a CIE personality
6193 routine or an FDE start address. Furthermore, the compilers never
6194 used a special (non-pointer) encoding for this ABI.
6195
6196 Checking the relocation type should also be safe because there is no
6197 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6198 did so. */
6199
6200 unsigned int
6201 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6202 {
6203 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6204 return 8;
6205 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6206 {
6207 bfd_boolean long32_p, long64_p;
6208
6209 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6210 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6211 if (long32_p && long64_p)
6212 return 0;
6213 if (long32_p)
6214 return 4;
6215 if (long64_p)
6216 return 8;
6217
6218 if (sec->reloc_count > 0
6219 && elf_section_data (sec)->relocs != NULL
6220 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6221 == R_MIPS_64))
6222 return 8;
6223
6224 return 0;
6225 }
6226 return 4;
6227 }
6228 \f
6229 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6230 relocations against two unnamed section symbols to resolve to the
6231 same address. For example, if we have code like:
6232
6233 lw $4,%got_disp(.data)($gp)
6234 lw $25,%got_disp(.text)($gp)
6235 jalr $25
6236
6237 then the linker will resolve both relocations to .data and the program
6238 will jump there rather than to .text.
6239
6240 We can work around this problem by giving names to local section symbols.
6241 This is also what the MIPSpro tools do. */
6242
6243 bfd_boolean
6244 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6245 {
6246 return SGI_COMPAT (abfd);
6247 }
6248 \f
6249 /* Work over a section just before writing it out. This routine is
6250 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6251 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6252 a better way. */
6253
6254 bfd_boolean
6255 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6256 {
6257 if (hdr->sh_type == SHT_MIPS_REGINFO
6258 && hdr->sh_size > 0)
6259 {
6260 bfd_byte buf[4];
6261
6262 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6263 BFD_ASSERT (hdr->contents == NULL);
6264
6265 if (bfd_seek (abfd,
6266 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6267 SEEK_SET) != 0)
6268 return FALSE;
6269 H_PUT_32 (abfd, elf_gp (abfd), buf);
6270 if (bfd_bwrite (buf, 4, abfd) != 4)
6271 return FALSE;
6272 }
6273
6274 if (hdr->sh_type == SHT_MIPS_OPTIONS
6275 && hdr->bfd_section != NULL
6276 && mips_elf_section_data (hdr->bfd_section) != NULL
6277 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6278 {
6279 bfd_byte *contents, *l, *lend;
6280
6281 /* We stored the section contents in the tdata field in the
6282 set_section_contents routine. We save the section contents
6283 so that we don't have to read them again.
6284 At this point we know that elf_gp is set, so we can look
6285 through the section contents to see if there is an
6286 ODK_REGINFO structure. */
6287
6288 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6289 l = contents;
6290 lend = contents + hdr->sh_size;
6291 while (l + sizeof (Elf_External_Options) <= lend)
6292 {
6293 Elf_Internal_Options intopt;
6294
6295 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6296 &intopt);
6297 if (intopt.size < sizeof (Elf_External_Options))
6298 {
6299 (*_bfd_error_handler)
6300 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6301 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6302 break;
6303 }
6304 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6305 {
6306 bfd_byte buf[8];
6307
6308 if (bfd_seek (abfd,
6309 (hdr->sh_offset
6310 + (l - contents)
6311 + sizeof (Elf_External_Options)
6312 + (sizeof (Elf64_External_RegInfo) - 8)),
6313 SEEK_SET) != 0)
6314 return FALSE;
6315 H_PUT_64 (abfd, elf_gp (abfd), buf);
6316 if (bfd_bwrite (buf, 8, abfd) != 8)
6317 return FALSE;
6318 }
6319 else if (intopt.kind == ODK_REGINFO)
6320 {
6321 bfd_byte buf[4];
6322
6323 if (bfd_seek (abfd,
6324 (hdr->sh_offset
6325 + (l - contents)
6326 + sizeof (Elf_External_Options)
6327 + (sizeof (Elf32_External_RegInfo) - 4)),
6328 SEEK_SET) != 0)
6329 return FALSE;
6330 H_PUT_32 (abfd, elf_gp (abfd), buf);
6331 if (bfd_bwrite (buf, 4, abfd) != 4)
6332 return FALSE;
6333 }
6334 l += intopt.size;
6335 }
6336 }
6337
6338 if (hdr->bfd_section != NULL)
6339 {
6340 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6341
6342 /* .sbss is not handled specially here because the GNU/Linux
6343 prelinker can convert .sbss from NOBITS to PROGBITS and
6344 changing it back to NOBITS breaks the binary. The entry in
6345 _bfd_mips_elf_special_sections will ensure the correct flags
6346 are set on .sbss if BFD creates it without reading it from an
6347 input file, and without special handling here the flags set
6348 on it in an input file will be followed. */
6349 if (strcmp (name, ".sdata") == 0
6350 || strcmp (name, ".lit8") == 0
6351 || strcmp (name, ".lit4") == 0)
6352 {
6353 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6354 hdr->sh_type = SHT_PROGBITS;
6355 }
6356 else if (strcmp (name, ".srdata") == 0)
6357 {
6358 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6359 hdr->sh_type = SHT_PROGBITS;
6360 }
6361 else if (strcmp (name, ".compact_rel") == 0)
6362 {
6363 hdr->sh_flags = 0;
6364 hdr->sh_type = SHT_PROGBITS;
6365 }
6366 else if (strcmp (name, ".rtproc") == 0)
6367 {
6368 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6369 {
6370 unsigned int adjust;
6371
6372 adjust = hdr->sh_size % hdr->sh_addralign;
6373 if (adjust != 0)
6374 hdr->sh_size += hdr->sh_addralign - adjust;
6375 }
6376 }
6377 }
6378
6379 return TRUE;
6380 }
6381
6382 /* Handle a MIPS specific section when reading an object file. This
6383 is called when elfcode.h finds a section with an unknown type.
6384 This routine supports both the 32-bit and 64-bit ELF ABI.
6385
6386 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6387 how to. */
6388
6389 bfd_boolean
6390 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6391 Elf_Internal_Shdr *hdr,
6392 const char *name,
6393 int shindex)
6394 {
6395 flagword flags = 0;
6396
6397 /* There ought to be a place to keep ELF backend specific flags, but
6398 at the moment there isn't one. We just keep track of the
6399 sections by their name, instead. Fortunately, the ABI gives
6400 suggested names for all the MIPS specific sections, so we will
6401 probably get away with this. */
6402 switch (hdr->sh_type)
6403 {
6404 case SHT_MIPS_LIBLIST:
6405 if (strcmp (name, ".liblist") != 0)
6406 return FALSE;
6407 break;
6408 case SHT_MIPS_MSYM:
6409 if (strcmp (name, ".msym") != 0)
6410 return FALSE;
6411 break;
6412 case SHT_MIPS_CONFLICT:
6413 if (strcmp (name, ".conflict") != 0)
6414 return FALSE;
6415 break;
6416 case SHT_MIPS_GPTAB:
6417 if (! CONST_STRNEQ (name, ".gptab."))
6418 return FALSE;
6419 break;
6420 case SHT_MIPS_UCODE:
6421 if (strcmp (name, ".ucode") != 0)
6422 return FALSE;
6423 break;
6424 case SHT_MIPS_DEBUG:
6425 if (strcmp (name, ".mdebug") != 0)
6426 return FALSE;
6427 flags = SEC_DEBUGGING;
6428 break;
6429 case SHT_MIPS_REGINFO:
6430 if (strcmp (name, ".reginfo") != 0
6431 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6432 return FALSE;
6433 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6434 break;
6435 case SHT_MIPS_IFACE:
6436 if (strcmp (name, ".MIPS.interfaces") != 0)
6437 return FALSE;
6438 break;
6439 case SHT_MIPS_CONTENT:
6440 if (! CONST_STRNEQ (name, ".MIPS.content"))
6441 return FALSE;
6442 break;
6443 case SHT_MIPS_OPTIONS:
6444 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6445 return FALSE;
6446 break;
6447 case SHT_MIPS_DWARF:
6448 if (! CONST_STRNEQ (name, ".debug_")
6449 && ! CONST_STRNEQ (name, ".zdebug_"))
6450 return FALSE;
6451 break;
6452 case SHT_MIPS_SYMBOL_LIB:
6453 if (strcmp (name, ".MIPS.symlib") != 0)
6454 return FALSE;
6455 break;
6456 case SHT_MIPS_EVENTS:
6457 if (! CONST_STRNEQ (name, ".MIPS.events")
6458 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6459 return FALSE;
6460 break;
6461 default:
6462 break;
6463 }
6464
6465 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6466 return FALSE;
6467
6468 if (flags)
6469 {
6470 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6471 (bfd_get_section_flags (abfd,
6472 hdr->bfd_section)
6473 | flags)))
6474 return FALSE;
6475 }
6476
6477 /* FIXME: We should record sh_info for a .gptab section. */
6478
6479 /* For a .reginfo section, set the gp value in the tdata information
6480 from the contents of this section. We need the gp value while
6481 processing relocs, so we just get it now. The .reginfo section
6482 is not used in the 64-bit MIPS ELF ABI. */
6483 if (hdr->sh_type == SHT_MIPS_REGINFO)
6484 {
6485 Elf32_External_RegInfo ext;
6486 Elf32_RegInfo s;
6487
6488 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6489 &ext, 0, sizeof ext))
6490 return FALSE;
6491 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6492 elf_gp (abfd) = s.ri_gp_value;
6493 }
6494
6495 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6496 set the gp value based on what we find. We may see both
6497 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6498 they should agree. */
6499 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6500 {
6501 bfd_byte *contents, *l, *lend;
6502
6503 contents = bfd_malloc (hdr->sh_size);
6504 if (contents == NULL)
6505 return FALSE;
6506 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6507 0, hdr->sh_size))
6508 {
6509 free (contents);
6510 return FALSE;
6511 }
6512 l = contents;
6513 lend = contents + hdr->sh_size;
6514 while (l + sizeof (Elf_External_Options) <= lend)
6515 {
6516 Elf_Internal_Options intopt;
6517
6518 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6519 &intopt);
6520 if (intopt.size < sizeof (Elf_External_Options))
6521 {
6522 (*_bfd_error_handler)
6523 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6524 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6525 break;
6526 }
6527 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6528 {
6529 Elf64_Internal_RegInfo intreg;
6530
6531 bfd_mips_elf64_swap_reginfo_in
6532 (abfd,
6533 ((Elf64_External_RegInfo *)
6534 (l + sizeof (Elf_External_Options))),
6535 &intreg);
6536 elf_gp (abfd) = intreg.ri_gp_value;
6537 }
6538 else if (intopt.kind == ODK_REGINFO)
6539 {
6540 Elf32_RegInfo intreg;
6541
6542 bfd_mips_elf32_swap_reginfo_in
6543 (abfd,
6544 ((Elf32_External_RegInfo *)
6545 (l + sizeof (Elf_External_Options))),
6546 &intreg);
6547 elf_gp (abfd) = intreg.ri_gp_value;
6548 }
6549 l += intopt.size;
6550 }
6551 free (contents);
6552 }
6553
6554 return TRUE;
6555 }
6556
6557 /* Set the correct type for a MIPS ELF section. We do this by the
6558 section name, which is a hack, but ought to work. This routine is
6559 used by both the 32-bit and the 64-bit ABI. */
6560
6561 bfd_boolean
6562 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6563 {
6564 const char *name = bfd_get_section_name (abfd, sec);
6565
6566 if (strcmp (name, ".liblist") == 0)
6567 {
6568 hdr->sh_type = SHT_MIPS_LIBLIST;
6569 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6570 /* The sh_link field is set in final_write_processing. */
6571 }
6572 else if (strcmp (name, ".conflict") == 0)
6573 hdr->sh_type = SHT_MIPS_CONFLICT;
6574 else if (CONST_STRNEQ (name, ".gptab."))
6575 {
6576 hdr->sh_type = SHT_MIPS_GPTAB;
6577 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6578 /* The sh_info field is set in final_write_processing. */
6579 }
6580 else if (strcmp (name, ".ucode") == 0)
6581 hdr->sh_type = SHT_MIPS_UCODE;
6582 else if (strcmp (name, ".mdebug") == 0)
6583 {
6584 hdr->sh_type = SHT_MIPS_DEBUG;
6585 /* In a shared object on IRIX 5.3, the .mdebug section has an
6586 entsize of 0. FIXME: Does this matter? */
6587 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6588 hdr->sh_entsize = 0;
6589 else
6590 hdr->sh_entsize = 1;
6591 }
6592 else if (strcmp (name, ".reginfo") == 0)
6593 {
6594 hdr->sh_type = SHT_MIPS_REGINFO;
6595 /* In a shared object on IRIX 5.3, the .reginfo section has an
6596 entsize of 0x18. FIXME: Does this matter? */
6597 if (SGI_COMPAT (abfd))
6598 {
6599 if ((abfd->flags & DYNAMIC) != 0)
6600 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6601 else
6602 hdr->sh_entsize = 1;
6603 }
6604 else
6605 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6606 }
6607 else if (SGI_COMPAT (abfd)
6608 && (strcmp (name, ".hash") == 0
6609 || strcmp (name, ".dynamic") == 0
6610 || strcmp (name, ".dynstr") == 0))
6611 {
6612 if (SGI_COMPAT (abfd))
6613 hdr->sh_entsize = 0;
6614 #if 0
6615 /* This isn't how the IRIX6 linker behaves. */
6616 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6617 #endif
6618 }
6619 else if (strcmp (name, ".got") == 0
6620 || strcmp (name, ".srdata") == 0
6621 || strcmp (name, ".sdata") == 0
6622 || strcmp (name, ".sbss") == 0
6623 || strcmp (name, ".lit4") == 0
6624 || strcmp (name, ".lit8") == 0)
6625 hdr->sh_flags |= SHF_MIPS_GPREL;
6626 else if (strcmp (name, ".MIPS.interfaces") == 0)
6627 {
6628 hdr->sh_type = SHT_MIPS_IFACE;
6629 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6630 }
6631 else if (CONST_STRNEQ (name, ".MIPS.content"))
6632 {
6633 hdr->sh_type = SHT_MIPS_CONTENT;
6634 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6635 /* The sh_info field is set in final_write_processing. */
6636 }
6637 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6638 {
6639 hdr->sh_type = SHT_MIPS_OPTIONS;
6640 hdr->sh_entsize = 1;
6641 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6642 }
6643 else if (CONST_STRNEQ (name, ".debug_")
6644 || CONST_STRNEQ (name, ".zdebug_"))
6645 {
6646 hdr->sh_type = SHT_MIPS_DWARF;
6647
6648 /* Irix facilities such as libexc expect a single .debug_frame
6649 per executable, the system ones have NOSTRIP set and the linker
6650 doesn't merge sections with different flags so ... */
6651 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6652 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6653 }
6654 else if (strcmp (name, ".MIPS.symlib") == 0)
6655 {
6656 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6657 /* The sh_link and sh_info fields are set in
6658 final_write_processing. */
6659 }
6660 else if (CONST_STRNEQ (name, ".MIPS.events")
6661 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6662 {
6663 hdr->sh_type = SHT_MIPS_EVENTS;
6664 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6665 /* The sh_link field is set in final_write_processing. */
6666 }
6667 else if (strcmp (name, ".msym") == 0)
6668 {
6669 hdr->sh_type = SHT_MIPS_MSYM;
6670 hdr->sh_flags |= SHF_ALLOC;
6671 hdr->sh_entsize = 8;
6672 }
6673
6674 /* The generic elf_fake_sections will set up REL_HDR using the default
6675 kind of relocations. We used to set up a second header for the
6676 non-default kind of relocations here, but only NewABI would use
6677 these, and the IRIX ld doesn't like resulting empty RELA sections.
6678 Thus we create those header only on demand now. */
6679
6680 return TRUE;
6681 }
6682
6683 /* Given a BFD section, try to locate the corresponding ELF section
6684 index. This is used by both the 32-bit and the 64-bit ABI.
6685 Actually, it's not clear to me that the 64-bit ABI supports these,
6686 but for non-PIC objects we will certainly want support for at least
6687 the .scommon section. */
6688
6689 bfd_boolean
6690 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6691 asection *sec, int *retval)
6692 {
6693 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6694 {
6695 *retval = SHN_MIPS_SCOMMON;
6696 return TRUE;
6697 }
6698 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6699 {
6700 *retval = SHN_MIPS_ACOMMON;
6701 return TRUE;
6702 }
6703 return FALSE;
6704 }
6705 \f
6706 /* Hook called by the linker routine which adds symbols from an object
6707 file. We must handle the special MIPS section numbers here. */
6708
6709 bfd_boolean
6710 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6711 Elf_Internal_Sym *sym, const char **namep,
6712 flagword *flagsp ATTRIBUTE_UNUSED,
6713 asection **secp, bfd_vma *valp)
6714 {
6715 if (SGI_COMPAT (abfd)
6716 && (abfd->flags & DYNAMIC) != 0
6717 && strcmp (*namep, "_rld_new_interface") == 0)
6718 {
6719 /* Skip IRIX5 rld entry name. */
6720 *namep = NULL;
6721 return TRUE;
6722 }
6723
6724 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6725 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6726 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6727 a magic symbol resolved by the linker, we ignore this bogus definition
6728 of _gp_disp. New ABI objects do not suffer from this problem so this
6729 is not done for them. */
6730 if (!NEWABI_P(abfd)
6731 && (sym->st_shndx == SHN_ABS)
6732 && (strcmp (*namep, "_gp_disp") == 0))
6733 {
6734 *namep = NULL;
6735 return TRUE;
6736 }
6737
6738 switch (sym->st_shndx)
6739 {
6740 case SHN_COMMON:
6741 /* Common symbols less than the GP size are automatically
6742 treated as SHN_MIPS_SCOMMON symbols. */
6743 if (sym->st_size > elf_gp_size (abfd)
6744 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6745 || IRIX_COMPAT (abfd) == ict_irix6)
6746 break;
6747 /* Fall through. */
6748 case SHN_MIPS_SCOMMON:
6749 *secp = bfd_make_section_old_way (abfd, ".scommon");
6750 (*secp)->flags |= SEC_IS_COMMON;
6751 *valp = sym->st_size;
6752 break;
6753
6754 case SHN_MIPS_TEXT:
6755 /* This section is used in a shared object. */
6756 if (elf_tdata (abfd)->elf_text_section == NULL)
6757 {
6758 asymbol *elf_text_symbol;
6759 asection *elf_text_section;
6760 bfd_size_type amt = sizeof (asection);
6761
6762 elf_text_section = bfd_zalloc (abfd, amt);
6763 if (elf_text_section == NULL)
6764 return FALSE;
6765
6766 amt = sizeof (asymbol);
6767 elf_text_symbol = bfd_zalloc (abfd, amt);
6768 if (elf_text_symbol == NULL)
6769 return FALSE;
6770
6771 /* Initialize the section. */
6772
6773 elf_tdata (abfd)->elf_text_section = elf_text_section;
6774 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6775
6776 elf_text_section->symbol = elf_text_symbol;
6777 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6778
6779 elf_text_section->name = ".text";
6780 elf_text_section->flags = SEC_NO_FLAGS;
6781 elf_text_section->output_section = NULL;
6782 elf_text_section->owner = abfd;
6783 elf_text_symbol->name = ".text";
6784 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6785 elf_text_symbol->section = elf_text_section;
6786 }
6787 /* This code used to do *secp = bfd_und_section_ptr if
6788 info->shared. I don't know why, and that doesn't make sense,
6789 so I took it out. */
6790 *secp = elf_tdata (abfd)->elf_text_section;
6791 break;
6792
6793 case SHN_MIPS_ACOMMON:
6794 /* Fall through. XXX Can we treat this as allocated data? */
6795 case SHN_MIPS_DATA:
6796 /* This section is used in a shared object. */
6797 if (elf_tdata (abfd)->elf_data_section == NULL)
6798 {
6799 asymbol *elf_data_symbol;
6800 asection *elf_data_section;
6801 bfd_size_type amt = sizeof (asection);
6802
6803 elf_data_section = bfd_zalloc (abfd, amt);
6804 if (elf_data_section == NULL)
6805 return FALSE;
6806
6807 amt = sizeof (asymbol);
6808 elf_data_symbol = bfd_zalloc (abfd, amt);
6809 if (elf_data_symbol == NULL)
6810 return FALSE;
6811
6812 /* Initialize the section. */
6813
6814 elf_tdata (abfd)->elf_data_section = elf_data_section;
6815 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6816
6817 elf_data_section->symbol = elf_data_symbol;
6818 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6819
6820 elf_data_section->name = ".data";
6821 elf_data_section->flags = SEC_NO_FLAGS;
6822 elf_data_section->output_section = NULL;
6823 elf_data_section->owner = abfd;
6824 elf_data_symbol->name = ".data";
6825 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6826 elf_data_symbol->section = elf_data_section;
6827 }
6828 /* This code used to do *secp = bfd_und_section_ptr if
6829 info->shared. I don't know why, and that doesn't make sense,
6830 so I took it out. */
6831 *secp = elf_tdata (abfd)->elf_data_section;
6832 break;
6833
6834 case SHN_MIPS_SUNDEFINED:
6835 *secp = bfd_und_section_ptr;
6836 break;
6837 }
6838
6839 if (SGI_COMPAT (abfd)
6840 && ! info->shared
6841 && info->output_bfd->xvec == abfd->xvec
6842 && strcmp (*namep, "__rld_obj_head") == 0)
6843 {
6844 struct elf_link_hash_entry *h;
6845 struct bfd_link_hash_entry *bh;
6846
6847 /* Mark __rld_obj_head as dynamic. */
6848 bh = NULL;
6849 if (! (_bfd_generic_link_add_one_symbol
6850 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6851 get_elf_backend_data (abfd)->collect, &bh)))
6852 return FALSE;
6853
6854 h = (struct elf_link_hash_entry *) bh;
6855 h->non_elf = 0;
6856 h->def_regular = 1;
6857 h->type = STT_OBJECT;
6858
6859 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6860 return FALSE;
6861
6862 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6863 }
6864
6865 /* If this is a mips16 text symbol, add 1 to the value to make it
6866 odd. This will cause something like .word SYM to come up with
6867 the right value when it is loaded into the PC. */
6868 if (ELF_ST_IS_MIPS16 (sym->st_other))
6869 ++*valp;
6870
6871 return TRUE;
6872 }
6873
6874 /* This hook function is called before the linker writes out a global
6875 symbol. We mark symbols as small common if appropriate. This is
6876 also where we undo the increment of the value for a mips16 symbol. */
6877
6878 int
6879 _bfd_mips_elf_link_output_symbol_hook
6880 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6881 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6882 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6883 {
6884 /* If we see a common symbol, which implies a relocatable link, then
6885 if a symbol was small common in an input file, mark it as small
6886 common in the output file. */
6887 if (sym->st_shndx == SHN_COMMON
6888 && strcmp (input_sec->name, ".scommon") == 0)
6889 sym->st_shndx = SHN_MIPS_SCOMMON;
6890
6891 if (ELF_ST_IS_MIPS16 (sym->st_other))
6892 sym->st_value &= ~1;
6893
6894 return 1;
6895 }
6896 \f
6897 /* Functions for the dynamic linker. */
6898
6899 /* Create dynamic sections when linking against a dynamic object. */
6900
6901 bfd_boolean
6902 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6903 {
6904 struct elf_link_hash_entry *h;
6905 struct bfd_link_hash_entry *bh;
6906 flagword flags;
6907 register asection *s;
6908 const char * const *namep;
6909 struct mips_elf_link_hash_table *htab;
6910
6911 htab = mips_elf_hash_table (info);
6912 BFD_ASSERT (htab != NULL);
6913
6914 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6915 | SEC_LINKER_CREATED | SEC_READONLY);
6916
6917 /* The psABI requires a read-only .dynamic section, but the VxWorks
6918 EABI doesn't. */
6919 if (!htab->is_vxworks)
6920 {
6921 s = bfd_get_section_by_name (abfd, ".dynamic");
6922 if (s != NULL)
6923 {
6924 if (! bfd_set_section_flags (abfd, s, flags))
6925 return FALSE;
6926 }
6927 }
6928
6929 /* We need to create .got section. */
6930 if (!mips_elf_create_got_section (abfd, info))
6931 return FALSE;
6932
6933 if (! mips_elf_rel_dyn_section (info, TRUE))
6934 return FALSE;
6935
6936 /* Create .stub section. */
6937 s = bfd_make_section_with_flags (abfd,
6938 MIPS_ELF_STUB_SECTION_NAME (abfd),
6939 flags | SEC_CODE);
6940 if (s == NULL
6941 || ! bfd_set_section_alignment (abfd, s,
6942 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6943 return FALSE;
6944 htab->sstubs = s;
6945
6946 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6947 && !info->shared
6948 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6949 {
6950 s = bfd_make_section_with_flags (abfd, ".rld_map",
6951 flags &~ (flagword) SEC_READONLY);
6952 if (s == NULL
6953 || ! bfd_set_section_alignment (abfd, s,
6954 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6955 return FALSE;
6956 }
6957
6958 /* On IRIX5, we adjust add some additional symbols and change the
6959 alignments of several sections. There is no ABI documentation
6960 indicating that this is necessary on IRIX6, nor any evidence that
6961 the linker takes such action. */
6962 if (IRIX_COMPAT (abfd) == ict_irix5)
6963 {
6964 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6965 {
6966 bh = NULL;
6967 if (! (_bfd_generic_link_add_one_symbol
6968 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6969 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6970 return FALSE;
6971
6972 h = (struct elf_link_hash_entry *) bh;
6973 h->non_elf = 0;
6974 h->def_regular = 1;
6975 h->type = STT_SECTION;
6976
6977 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6978 return FALSE;
6979 }
6980
6981 /* We need to create a .compact_rel section. */
6982 if (SGI_COMPAT (abfd))
6983 {
6984 if (!mips_elf_create_compact_rel_section (abfd, info))
6985 return FALSE;
6986 }
6987
6988 /* Change alignments of some sections. */
6989 s = bfd_get_section_by_name (abfd, ".hash");
6990 if (s != NULL)
6991 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6992 s = bfd_get_section_by_name (abfd, ".dynsym");
6993 if (s != NULL)
6994 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6995 s = bfd_get_section_by_name (abfd, ".dynstr");
6996 if (s != NULL)
6997 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6998 s = bfd_get_section_by_name (abfd, ".reginfo");
6999 if (s != NULL)
7000 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7001 s = bfd_get_section_by_name (abfd, ".dynamic");
7002 if (s != NULL)
7003 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7004 }
7005
7006 if (!info->shared)
7007 {
7008 const char *name;
7009
7010 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7011 bh = NULL;
7012 if (!(_bfd_generic_link_add_one_symbol
7013 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7014 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7015 return FALSE;
7016
7017 h = (struct elf_link_hash_entry *) bh;
7018 h->non_elf = 0;
7019 h->def_regular = 1;
7020 h->type = STT_SECTION;
7021
7022 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7023 return FALSE;
7024
7025 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7026 {
7027 /* __rld_map is a four byte word located in the .data section
7028 and is filled in by the rtld to contain a pointer to
7029 the _r_debug structure. Its symbol value will be set in
7030 _bfd_mips_elf_finish_dynamic_symbol. */
7031 s = bfd_get_section_by_name (abfd, ".rld_map");
7032 BFD_ASSERT (s != NULL);
7033
7034 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7035 bh = NULL;
7036 if (!(_bfd_generic_link_add_one_symbol
7037 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7038 get_elf_backend_data (abfd)->collect, &bh)))
7039 return FALSE;
7040
7041 h = (struct elf_link_hash_entry *) bh;
7042 h->non_elf = 0;
7043 h->def_regular = 1;
7044 h->type = STT_OBJECT;
7045
7046 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7047 return FALSE;
7048 }
7049 }
7050
7051 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7052 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7053 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7054 return FALSE;
7055
7056 /* Cache the sections created above. */
7057 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7058 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
7059 if (htab->is_vxworks)
7060 {
7061 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7062 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
7063 }
7064 else
7065 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7066 if (!htab->sdynbss
7067 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7068 || !htab->srelplt
7069 || !htab->splt)
7070 abort ();
7071
7072 if (htab->is_vxworks)
7073 {
7074 /* Do the usual VxWorks handling. */
7075 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7076 return FALSE;
7077
7078 /* Work out the PLT sizes. */
7079 if (info->shared)
7080 {
7081 htab->plt_header_size
7082 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7083 htab->plt_entry_size
7084 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7085 }
7086 else
7087 {
7088 htab->plt_header_size
7089 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7090 htab->plt_entry_size
7091 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7092 }
7093 }
7094 else if (!info->shared)
7095 {
7096 /* All variants of the plt0 entry are the same size. */
7097 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7098 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7099 }
7100
7101 return TRUE;
7102 }
7103 \f
7104 /* Return true if relocation REL against section SEC is a REL rather than
7105 RELA relocation. RELOCS is the first relocation in the section and
7106 ABFD is the bfd that contains SEC. */
7107
7108 static bfd_boolean
7109 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7110 const Elf_Internal_Rela *relocs,
7111 const Elf_Internal_Rela *rel)
7112 {
7113 Elf_Internal_Shdr *rel_hdr;
7114 const struct elf_backend_data *bed;
7115
7116 /* To determine which flavor or relocation this is, we depend on the
7117 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7118 rel_hdr = &elf_section_data (sec)->rel_hdr;
7119 bed = get_elf_backend_data (abfd);
7120 if ((size_t) (rel - relocs)
7121 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7122 rel_hdr = elf_section_data (sec)->rel_hdr2;
7123 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7124 }
7125
7126 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7127 HOWTO is the relocation's howto and CONTENTS points to the contents
7128 of the section that REL is against. */
7129
7130 static bfd_vma
7131 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7132 reloc_howto_type *howto, bfd_byte *contents)
7133 {
7134 bfd_byte *location;
7135 unsigned int r_type;
7136 bfd_vma addend;
7137
7138 r_type = ELF_R_TYPE (abfd, rel->r_info);
7139 location = contents + rel->r_offset;
7140
7141 /* Get the addend, which is stored in the input file. */
7142 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7143 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7144 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7145
7146 return addend & howto->src_mask;
7147 }
7148
7149 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7150 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7151 and update *ADDEND with the final addend. Return true on success
7152 or false if the LO16 could not be found. RELEND is the exclusive
7153 upper bound on the relocations for REL's section. */
7154
7155 static bfd_boolean
7156 mips_elf_add_lo16_rel_addend (bfd *abfd,
7157 const Elf_Internal_Rela *rel,
7158 const Elf_Internal_Rela *relend,
7159 bfd_byte *contents, bfd_vma *addend)
7160 {
7161 unsigned int r_type, lo16_type;
7162 const Elf_Internal_Rela *lo16_relocation;
7163 reloc_howto_type *lo16_howto;
7164 bfd_vma l;
7165
7166 r_type = ELF_R_TYPE (abfd, rel->r_info);
7167 if (mips16_reloc_p (r_type))
7168 lo16_type = R_MIPS16_LO16;
7169 else
7170 lo16_type = R_MIPS_LO16;
7171
7172 /* The combined value is the sum of the HI16 addend, left-shifted by
7173 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7174 code does a `lui' of the HI16 value, and then an `addiu' of the
7175 LO16 value.)
7176
7177 Scan ahead to find a matching LO16 relocation.
7178
7179 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7180 be immediately following. However, for the IRIX6 ABI, the next
7181 relocation may be a composed relocation consisting of several
7182 relocations for the same address. In that case, the R_MIPS_LO16
7183 relocation may occur as one of these. We permit a similar
7184 extension in general, as that is useful for GCC.
7185
7186 In some cases GCC dead code elimination removes the LO16 but keeps
7187 the corresponding HI16. This is strictly speaking a violation of
7188 the ABI but not immediately harmful. */
7189 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7190 if (lo16_relocation == NULL)
7191 return FALSE;
7192
7193 /* Obtain the addend kept there. */
7194 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7195 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7196
7197 l <<= lo16_howto->rightshift;
7198 l = _bfd_mips_elf_sign_extend (l, 16);
7199
7200 *addend <<= 16;
7201 *addend += l;
7202 return TRUE;
7203 }
7204
7205 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7206 store the contents in *CONTENTS on success. Assume that *CONTENTS
7207 already holds the contents if it is nonull on entry. */
7208
7209 static bfd_boolean
7210 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7211 {
7212 if (*contents)
7213 return TRUE;
7214
7215 /* Get cached copy if it exists. */
7216 if (elf_section_data (sec)->this_hdr.contents != NULL)
7217 {
7218 *contents = elf_section_data (sec)->this_hdr.contents;
7219 return TRUE;
7220 }
7221
7222 return bfd_malloc_and_get_section (abfd, sec, contents);
7223 }
7224
7225 /* Look through the relocs for a section during the first phase, and
7226 allocate space in the global offset table. */
7227
7228 bfd_boolean
7229 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7230 asection *sec, const Elf_Internal_Rela *relocs)
7231 {
7232 const char *name;
7233 bfd *dynobj;
7234 Elf_Internal_Shdr *symtab_hdr;
7235 struct elf_link_hash_entry **sym_hashes;
7236 size_t extsymoff;
7237 const Elf_Internal_Rela *rel;
7238 const Elf_Internal_Rela *rel_end;
7239 asection *sreloc;
7240 const struct elf_backend_data *bed;
7241 struct mips_elf_link_hash_table *htab;
7242 bfd_byte *contents;
7243 bfd_vma addend;
7244 reloc_howto_type *howto;
7245
7246 if (info->relocatable)
7247 return TRUE;
7248
7249 htab = mips_elf_hash_table (info);
7250 BFD_ASSERT (htab != NULL);
7251
7252 dynobj = elf_hash_table (info)->dynobj;
7253 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7254 sym_hashes = elf_sym_hashes (abfd);
7255 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7256
7257 bed = get_elf_backend_data (abfd);
7258 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7259
7260 /* Check for the mips16 stub sections. */
7261
7262 name = bfd_get_section_name (abfd, sec);
7263 if (FN_STUB_P (name))
7264 {
7265 unsigned long r_symndx;
7266
7267 /* Look at the relocation information to figure out which symbol
7268 this is for. */
7269
7270 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7271 if (r_symndx == 0)
7272 {
7273 (*_bfd_error_handler)
7274 (_("%B: Warning: cannot determine the target function for"
7275 " stub section `%s'"),
7276 abfd, name);
7277 bfd_set_error (bfd_error_bad_value);
7278 return FALSE;
7279 }
7280
7281 if (r_symndx < extsymoff
7282 || sym_hashes[r_symndx - extsymoff] == NULL)
7283 {
7284 asection *o;
7285
7286 /* This stub is for a local symbol. This stub will only be
7287 needed if there is some relocation in this BFD, other
7288 than a 16 bit function call, which refers to this symbol. */
7289 for (o = abfd->sections; o != NULL; o = o->next)
7290 {
7291 Elf_Internal_Rela *sec_relocs;
7292 const Elf_Internal_Rela *r, *rend;
7293
7294 /* We can ignore stub sections when looking for relocs. */
7295 if ((o->flags & SEC_RELOC) == 0
7296 || o->reloc_count == 0
7297 || section_allows_mips16_refs_p (o))
7298 continue;
7299
7300 sec_relocs
7301 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7302 info->keep_memory);
7303 if (sec_relocs == NULL)
7304 return FALSE;
7305
7306 rend = sec_relocs + o->reloc_count;
7307 for (r = sec_relocs; r < rend; r++)
7308 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7309 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7310 break;
7311
7312 if (elf_section_data (o)->relocs != sec_relocs)
7313 free (sec_relocs);
7314
7315 if (r < rend)
7316 break;
7317 }
7318
7319 if (o == NULL)
7320 {
7321 /* There is no non-call reloc for this stub, so we do
7322 not need it. Since this function is called before
7323 the linker maps input sections to output sections, we
7324 can easily discard it by setting the SEC_EXCLUDE
7325 flag. */
7326 sec->flags |= SEC_EXCLUDE;
7327 return TRUE;
7328 }
7329
7330 /* Record this stub in an array of local symbol stubs for
7331 this BFD. */
7332 if (elf_tdata (abfd)->local_stubs == NULL)
7333 {
7334 unsigned long symcount;
7335 asection **n;
7336 bfd_size_type amt;
7337
7338 if (elf_bad_symtab (abfd))
7339 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7340 else
7341 symcount = symtab_hdr->sh_info;
7342 amt = symcount * sizeof (asection *);
7343 n = bfd_zalloc (abfd, amt);
7344 if (n == NULL)
7345 return FALSE;
7346 elf_tdata (abfd)->local_stubs = n;
7347 }
7348
7349 sec->flags |= SEC_KEEP;
7350 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7351
7352 /* We don't need to set mips16_stubs_seen in this case.
7353 That flag is used to see whether we need to look through
7354 the global symbol table for stubs. We don't need to set
7355 it here, because we just have a local stub. */
7356 }
7357 else
7358 {
7359 struct mips_elf_link_hash_entry *h;
7360
7361 h = ((struct mips_elf_link_hash_entry *)
7362 sym_hashes[r_symndx - extsymoff]);
7363
7364 while (h->root.root.type == bfd_link_hash_indirect
7365 || h->root.root.type == bfd_link_hash_warning)
7366 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7367
7368 /* H is the symbol this stub is for. */
7369
7370 /* If we already have an appropriate stub for this function, we
7371 don't need another one, so we can discard this one. Since
7372 this function is called before the linker maps input sections
7373 to output sections, we can easily discard it by setting the
7374 SEC_EXCLUDE flag. */
7375 if (h->fn_stub != NULL)
7376 {
7377 sec->flags |= SEC_EXCLUDE;
7378 return TRUE;
7379 }
7380
7381 sec->flags |= SEC_KEEP;
7382 h->fn_stub = sec;
7383 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7384 }
7385 }
7386 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7387 {
7388 unsigned long r_symndx;
7389 struct mips_elf_link_hash_entry *h;
7390 asection **loc;
7391
7392 /* Look at the relocation information to figure out which symbol
7393 this is for. */
7394
7395 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7396 if (r_symndx == 0)
7397 {
7398 (*_bfd_error_handler)
7399 (_("%B: Warning: cannot determine the target function for"
7400 " stub section `%s'"),
7401 abfd, name);
7402 bfd_set_error (bfd_error_bad_value);
7403 return FALSE;
7404 }
7405
7406 if (r_symndx < extsymoff
7407 || sym_hashes[r_symndx - extsymoff] == NULL)
7408 {
7409 asection *o;
7410
7411 /* This stub is for a local symbol. This stub will only be
7412 needed if there is some relocation (R_MIPS16_26) in this BFD
7413 that refers to this symbol. */
7414 for (o = abfd->sections; o != NULL; o = o->next)
7415 {
7416 Elf_Internal_Rela *sec_relocs;
7417 const Elf_Internal_Rela *r, *rend;
7418
7419 /* We can ignore stub sections when looking for relocs. */
7420 if ((o->flags & SEC_RELOC) == 0
7421 || o->reloc_count == 0
7422 || section_allows_mips16_refs_p (o))
7423 continue;
7424
7425 sec_relocs
7426 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7427 info->keep_memory);
7428 if (sec_relocs == NULL)
7429 return FALSE;
7430
7431 rend = sec_relocs + o->reloc_count;
7432 for (r = sec_relocs; r < rend; r++)
7433 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7434 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7435 break;
7436
7437 if (elf_section_data (o)->relocs != sec_relocs)
7438 free (sec_relocs);
7439
7440 if (r < rend)
7441 break;
7442 }
7443
7444 if (o == NULL)
7445 {
7446 /* There is no non-call reloc for this stub, so we do
7447 not need it. Since this function is called before
7448 the linker maps input sections to output sections, we
7449 can easily discard it by setting the SEC_EXCLUDE
7450 flag. */
7451 sec->flags |= SEC_EXCLUDE;
7452 return TRUE;
7453 }
7454
7455 /* Record this stub in an array of local symbol call_stubs for
7456 this BFD. */
7457 if (elf_tdata (abfd)->local_call_stubs == NULL)
7458 {
7459 unsigned long symcount;
7460 asection **n;
7461 bfd_size_type amt;
7462
7463 if (elf_bad_symtab (abfd))
7464 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7465 else
7466 symcount = symtab_hdr->sh_info;
7467 amt = symcount * sizeof (asection *);
7468 n = bfd_zalloc (abfd, amt);
7469 if (n == NULL)
7470 return FALSE;
7471 elf_tdata (abfd)->local_call_stubs = n;
7472 }
7473
7474 sec->flags |= SEC_KEEP;
7475 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7476
7477 /* We don't need to set mips16_stubs_seen in this case.
7478 That flag is used to see whether we need to look through
7479 the global symbol table for stubs. We don't need to set
7480 it here, because we just have a local stub. */
7481 }
7482 else
7483 {
7484 h = ((struct mips_elf_link_hash_entry *)
7485 sym_hashes[r_symndx - extsymoff]);
7486
7487 /* H is the symbol this stub is for. */
7488
7489 if (CALL_FP_STUB_P (name))
7490 loc = &h->call_fp_stub;
7491 else
7492 loc = &h->call_stub;
7493
7494 /* If we already have an appropriate stub for this function, we
7495 don't need another one, so we can discard this one. Since
7496 this function is called before the linker maps input sections
7497 to output sections, we can easily discard it by setting the
7498 SEC_EXCLUDE flag. */
7499 if (*loc != NULL)
7500 {
7501 sec->flags |= SEC_EXCLUDE;
7502 return TRUE;
7503 }
7504
7505 sec->flags |= SEC_KEEP;
7506 *loc = sec;
7507 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7508 }
7509 }
7510
7511 sreloc = NULL;
7512 contents = NULL;
7513 for (rel = relocs; rel < rel_end; ++rel)
7514 {
7515 unsigned long r_symndx;
7516 unsigned int r_type;
7517 struct elf_link_hash_entry *h;
7518 bfd_boolean can_make_dynamic_p;
7519
7520 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7521 r_type = ELF_R_TYPE (abfd, rel->r_info);
7522
7523 if (r_symndx < extsymoff)
7524 h = NULL;
7525 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7526 {
7527 (*_bfd_error_handler)
7528 (_("%B: Malformed reloc detected for section %s"),
7529 abfd, name);
7530 bfd_set_error (bfd_error_bad_value);
7531 return FALSE;
7532 }
7533 else
7534 {
7535 h = sym_hashes[r_symndx - extsymoff];
7536 while (h != NULL
7537 && (h->root.type == bfd_link_hash_indirect
7538 || h->root.type == bfd_link_hash_warning))
7539 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7540 }
7541
7542 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7543 relocation into a dynamic one. */
7544 can_make_dynamic_p = FALSE;
7545 switch (r_type)
7546 {
7547 case R_MIPS16_GOT16:
7548 case R_MIPS16_CALL16:
7549 case R_MIPS_GOT16:
7550 case R_MIPS_CALL16:
7551 case R_MIPS_CALL_HI16:
7552 case R_MIPS_CALL_LO16:
7553 case R_MIPS_GOT_HI16:
7554 case R_MIPS_GOT_LO16:
7555 case R_MIPS_GOT_PAGE:
7556 case R_MIPS_GOT_OFST:
7557 case R_MIPS_GOT_DISP:
7558 case R_MIPS_TLS_GOTTPREL:
7559 case R_MIPS_TLS_GD:
7560 case R_MIPS_TLS_LDM:
7561 if (dynobj == NULL)
7562 elf_hash_table (info)->dynobj = dynobj = abfd;
7563 if (!mips_elf_create_got_section (dynobj, info))
7564 return FALSE;
7565 if (htab->is_vxworks && !info->shared)
7566 {
7567 (*_bfd_error_handler)
7568 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7569 abfd, (unsigned long) rel->r_offset);
7570 bfd_set_error (bfd_error_bad_value);
7571 return FALSE;
7572 }
7573 break;
7574
7575 /* This is just a hint; it can safely be ignored. Don't set
7576 has_static_relocs for the corresponding symbol. */
7577 case R_MIPS_JALR:
7578 break;
7579
7580 case R_MIPS_32:
7581 case R_MIPS_REL32:
7582 case R_MIPS_64:
7583 /* In VxWorks executables, references to external symbols
7584 must be handled using copy relocs or PLT entries; it is not
7585 possible to convert this relocation into a dynamic one.
7586
7587 For executables that use PLTs and copy-relocs, we have a
7588 choice between converting the relocation into a dynamic
7589 one or using copy relocations or PLT entries. It is
7590 usually better to do the former, unless the relocation is
7591 against a read-only section. */
7592 if ((info->shared
7593 || (h != NULL
7594 && !htab->is_vxworks
7595 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7596 && !(!info->nocopyreloc
7597 && !PIC_OBJECT_P (abfd)
7598 && MIPS_ELF_READONLY_SECTION (sec))))
7599 && (sec->flags & SEC_ALLOC) != 0)
7600 {
7601 can_make_dynamic_p = TRUE;
7602 if (dynobj == NULL)
7603 elf_hash_table (info)->dynobj = dynobj = abfd;
7604 break;
7605 }
7606 /* Fall through. */
7607
7608 default:
7609 /* Most static relocations require pointer equality, except
7610 for branches. */
7611 if (h)
7612 h->pointer_equality_needed = TRUE;
7613 /* Fall through. */
7614
7615 case R_MIPS_26:
7616 case R_MIPS_PC16:
7617 case R_MIPS16_26:
7618 if (h)
7619 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7620 break;
7621 }
7622
7623 if (h)
7624 {
7625 /* Relocations against the special VxWorks __GOTT_BASE__ and
7626 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7627 room for them in .rela.dyn. */
7628 if (is_gott_symbol (info, h))
7629 {
7630 if (sreloc == NULL)
7631 {
7632 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7633 if (sreloc == NULL)
7634 return FALSE;
7635 }
7636 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7637 if (MIPS_ELF_READONLY_SECTION (sec))
7638 /* We tell the dynamic linker that there are
7639 relocations against the text segment. */
7640 info->flags |= DF_TEXTREL;
7641 }
7642 }
7643 else if (r_type == R_MIPS_CALL_LO16
7644 || r_type == R_MIPS_GOT_LO16
7645 || r_type == R_MIPS_GOT_DISP
7646 || (got16_reloc_p (r_type) && htab->is_vxworks))
7647 {
7648 /* We may need a local GOT entry for this relocation. We
7649 don't count R_MIPS_GOT_PAGE because we can estimate the
7650 maximum number of pages needed by looking at the size of
7651 the segment. Similar comments apply to R_MIPS*_GOT16 and
7652 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7653 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7654 R_MIPS_CALL_HI16 because these are always followed by an
7655 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7656 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7657 rel->r_addend, info, 0))
7658 return FALSE;
7659 }
7660
7661 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7662 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7663
7664 switch (r_type)
7665 {
7666 case R_MIPS_CALL16:
7667 case R_MIPS16_CALL16:
7668 if (h == NULL)
7669 {
7670 (*_bfd_error_handler)
7671 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7672 abfd, (unsigned long) rel->r_offset);
7673 bfd_set_error (bfd_error_bad_value);
7674 return FALSE;
7675 }
7676 /* Fall through. */
7677
7678 case R_MIPS_CALL_HI16:
7679 case R_MIPS_CALL_LO16:
7680 if (h != NULL)
7681 {
7682 /* VxWorks call relocations point at the function's .got.plt
7683 entry, which will be allocated by adjust_dynamic_symbol.
7684 Otherwise, this symbol requires a global GOT entry. */
7685 if ((!htab->is_vxworks || h->forced_local)
7686 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7687 return FALSE;
7688
7689 /* We need a stub, not a plt entry for the undefined
7690 function. But we record it as if it needs plt. See
7691 _bfd_elf_adjust_dynamic_symbol. */
7692 h->needs_plt = 1;
7693 h->type = STT_FUNC;
7694 }
7695 break;
7696
7697 case R_MIPS_GOT_PAGE:
7698 /* If this is a global, overridable symbol, GOT_PAGE will
7699 decay to GOT_DISP, so we'll need a GOT entry for it. */
7700 if (h)
7701 {
7702 struct mips_elf_link_hash_entry *hmips =
7703 (struct mips_elf_link_hash_entry *) h;
7704
7705 /* This symbol is definitely not overridable. */
7706 if (hmips->root.def_regular
7707 && ! (info->shared && ! info->symbolic
7708 && ! hmips->root.forced_local))
7709 h = NULL;
7710 }
7711 /* Fall through. */
7712
7713 case R_MIPS16_GOT16:
7714 case R_MIPS_GOT16:
7715 case R_MIPS_GOT_HI16:
7716 case R_MIPS_GOT_LO16:
7717 if (!h || r_type == R_MIPS_GOT_PAGE)
7718 {
7719 /* This relocation needs (or may need, if h != NULL) a
7720 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7721 know for sure until we know whether the symbol is
7722 preemptible. */
7723 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7724 {
7725 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7726 return FALSE;
7727 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7728 addend = mips_elf_read_rel_addend (abfd, rel,
7729 howto, contents);
7730 if (got16_reloc_p (r_type))
7731 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7732 contents, &addend);
7733 else
7734 addend <<= howto->rightshift;
7735 }
7736 else
7737 addend = rel->r_addend;
7738 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7739 addend))
7740 return FALSE;
7741 break;
7742 }
7743 /* Fall through. */
7744
7745 case R_MIPS_GOT_DISP:
7746 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7747 return FALSE;
7748 break;
7749
7750 case R_MIPS_TLS_GOTTPREL:
7751 if (info->shared)
7752 info->flags |= DF_STATIC_TLS;
7753 /* Fall through */
7754
7755 case R_MIPS_TLS_LDM:
7756 if (r_type == R_MIPS_TLS_LDM)
7757 {
7758 r_symndx = 0;
7759 h = NULL;
7760 }
7761 /* Fall through */
7762
7763 case R_MIPS_TLS_GD:
7764 /* This symbol requires a global offset table entry, or two
7765 for TLS GD relocations. */
7766 {
7767 unsigned char flag = (r_type == R_MIPS_TLS_GD
7768 ? GOT_TLS_GD
7769 : r_type == R_MIPS_TLS_LDM
7770 ? GOT_TLS_LDM
7771 : GOT_TLS_IE);
7772 if (h != NULL)
7773 {
7774 struct mips_elf_link_hash_entry *hmips =
7775 (struct mips_elf_link_hash_entry *) h;
7776 hmips->tls_type |= flag;
7777
7778 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7779 info, flag))
7780 return FALSE;
7781 }
7782 else
7783 {
7784 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7785
7786 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7787 rel->r_addend,
7788 info, flag))
7789 return FALSE;
7790 }
7791 }
7792 break;
7793
7794 case R_MIPS_32:
7795 case R_MIPS_REL32:
7796 case R_MIPS_64:
7797 /* In VxWorks executables, references to external symbols
7798 are handled using copy relocs or PLT stubs, so there's
7799 no need to add a .rela.dyn entry for this relocation. */
7800 if (can_make_dynamic_p)
7801 {
7802 if (sreloc == NULL)
7803 {
7804 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7805 if (sreloc == NULL)
7806 return FALSE;
7807 }
7808 if (info->shared && h == NULL)
7809 {
7810 /* When creating a shared object, we must copy these
7811 reloc types into the output file as R_MIPS_REL32
7812 relocs. Make room for this reloc in .rel(a).dyn. */
7813 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7814 if (MIPS_ELF_READONLY_SECTION (sec))
7815 /* We tell the dynamic linker that there are
7816 relocations against the text segment. */
7817 info->flags |= DF_TEXTREL;
7818 }
7819 else
7820 {
7821 struct mips_elf_link_hash_entry *hmips;
7822
7823 /* For a shared object, we must copy this relocation
7824 unless the symbol turns out to be undefined and
7825 weak with non-default visibility, in which case
7826 it will be left as zero.
7827
7828 We could elide R_MIPS_REL32 for locally binding symbols
7829 in shared libraries, but do not yet do so.
7830
7831 For an executable, we only need to copy this
7832 reloc if the symbol is defined in a dynamic
7833 object. */
7834 hmips = (struct mips_elf_link_hash_entry *) h;
7835 ++hmips->possibly_dynamic_relocs;
7836 if (MIPS_ELF_READONLY_SECTION (sec))
7837 /* We need it to tell the dynamic linker if there
7838 are relocations against the text segment. */
7839 hmips->readonly_reloc = TRUE;
7840 }
7841 }
7842
7843 if (SGI_COMPAT (abfd))
7844 mips_elf_hash_table (info)->compact_rel_size +=
7845 sizeof (Elf32_External_crinfo);
7846 break;
7847
7848 case R_MIPS_26:
7849 case R_MIPS_GPREL16:
7850 case R_MIPS_LITERAL:
7851 case R_MIPS_GPREL32:
7852 if (SGI_COMPAT (abfd))
7853 mips_elf_hash_table (info)->compact_rel_size +=
7854 sizeof (Elf32_External_crinfo);
7855 break;
7856
7857 /* This relocation describes the C++ object vtable hierarchy.
7858 Reconstruct it for later use during GC. */
7859 case R_MIPS_GNU_VTINHERIT:
7860 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7861 return FALSE;
7862 break;
7863
7864 /* This relocation describes which C++ vtable entries are actually
7865 used. Record for later use during GC. */
7866 case R_MIPS_GNU_VTENTRY:
7867 BFD_ASSERT (h != NULL);
7868 if (h != NULL
7869 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7870 return FALSE;
7871 break;
7872
7873 default:
7874 break;
7875 }
7876
7877 /* We must not create a stub for a symbol that has relocations
7878 related to taking the function's address. This doesn't apply to
7879 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7880 a normal .got entry. */
7881 if (!htab->is_vxworks && h != NULL)
7882 switch (r_type)
7883 {
7884 default:
7885 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7886 break;
7887 case R_MIPS16_CALL16:
7888 case R_MIPS_CALL16:
7889 case R_MIPS_CALL_HI16:
7890 case R_MIPS_CALL_LO16:
7891 case R_MIPS_JALR:
7892 break;
7893 }
7894
7895 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7896 if there is one. We only need to handle global symbols here;
7897 we decide whether to keep or delete stubs for local symbols
7898 when processing the stub's relocations. */
7899 if (h != NULL
7900 && !mips16_call_reloc_p (r_type)
7901 && !section_allows_mips16_refs_p (sec))
7902 {
7903 struct mips_elf_link_hash_entry *mh;
7904
7905 mh = (struct mips_elf_link_hash_entry *) h;
7906 mh->need_fn_stub = TRUE;
7907 }
7908
7909 /* Refuse some position-dependent relocations when creating a
7910 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7911 not PIC, but we can create dynamic relocations and the result
7912 will be fine. Also do not refuse R_MIPS_LO16, which can be
7913 combined with R_MIPS_GOT16. */
7914 if (info->shared)
7915 {
7916 switch (r_type)
7917 {
7918 case R_MIPS16_HI16:
7919 case R_MIPS_HI16:
7920 case R_MIPS_HIGHER:
7921 case R_MIPS_HIGHEST:
7922 /* Don't refuse a high part relocation if it's against
7923 no symbol (e.g. part of a compound relocation). */
7924 if (r_symndx == 0)
7925 break;
7926
7927 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7928 and has a special meaning. */
7929 if (!NEWABI_P (abfd) && h != NULL
7930 && strcmp (h->root.root.string, "_gp_disp") == 0)
7931 break;
7932
7933 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
7934 if (is_gott_symbol (info, h))
7935 break;
7936
7937 /* FALLTHROUGH */
7938
7939 case R_MIPS16_26:
7940 case R_MIPS_26:
7941 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7942 (*_bfd_error_handler)
7943 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7944 abfd, howto->name,
7945 (h) ? h->root.root.string : "a local symbol");
7946 bfd_set_error (bfd_error_bad_value);
7947 return FALSE;
7948 default:
7949 break;
7950 }
7951 }
7952 }
7953
7954 return TRUE;
7955 }
7956 \f
7957 bfd_boolean
7958 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7959 struct bfd_link_info *link_info,
7960 bfd_boolean *again)
7961 {
7962 Elf_Internal_Rela *internal_relocs;
7963 Elf_Internal_Rela *irel, *irelend;
7964 Elf_Internal_Shdr *symtab_hdr;
7965 bfd_byte *contents = NULL;
7966 size_t extsymoff;
7967 bfd_boolean changed_contents = FALSE;
7968 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7969 Elf_Internal_Sym *isymbuf = NULL;
7970
7971 /* We are not currently changing any sizes, so only one pass. */
7972 *again = FALSE;
7973
7974 if (link_info->relocatable)
7975 return TRUE;
7976
7977 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7978 link_info->keep_memory);
7979 if (internal_relocs == NULL)
7980 return TRUE;
7981
7982 irelend = internal_relocs + sec->reloc_count
7983 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7984 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7985 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7986
7987 for (irel = internal_relocs; irel < irelend; irel++)
7988 {
7989 bfd_vma symval;
7990 bfd_signed_vma sym_offset;
7991 unsigned int r_type;
7992 unsigned long r_symndx;
7993 asection *sym_sec;
7994 unsigned long instruction;
7995
7996 /* Turn jalr into bgezal, and jr into beq, if they're marked
7997 with a JALR relocation, that indicate where they jump to.
7998 This saves some pipeline bubbles. */
7999 r_type = ELF_R_TYPE (abfd, irel->r_info);
8000 if (r_type != R_MIPS_JALR)
8001 continue;
8002
8003 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8004 /* Compute the address of the jump target. */
8005 if (r_symndx >= extsymoff)
8006 {
8007 struct mips_elf_link_hash_entry *h
8008 = ((struct mips_elf_link_hash_entry *)
8009 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8010
8011 while (h->root.root.type == bfd_link_hash_indirect
8012 || h->root.root.type == bfd_link_hash_warning)
8013 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8014
8015 /* If a symbol is undefined, or if it may be overridden,
8016 skip it. */
8017 if (! ((h->root.root.type == bfd_link_hash_defined
8018 || h->root.root.type == bfd_link_hash_defweak)
8019 && h->root.root.u.def.section)
8020 || (link_info->shared && ! link_info->symbolic
8021 && !h->root.forced_local))
8022 continue;
8023
8024 sym_sec = h->root.root.u.def.section;
8025 if (sym_sec->output_section)
8026 symval = (h->root.root.u.def.value
8027 + sym_sec->output_section->vma
8028 + sym_sec->output_offset);
8029 else
8030 symval = h->root.root.u.def.value;
8031 }
8032 else
8033 {
8034 Elf_Internal_Sym *isym;
8035
8036 /* Read this BFD's symbols if we haven't done so already. */
8037 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8038 {
8039 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8040 if (isymbuf == NULL)
8041 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8042 symtab_hdr->sh_info, 0,
8043 NULL, NULL, NULL);
8044 if (isymbuf == NULL)
8045 goto relax_return;
8046 }
8047
8048 isym = isymbuf + r_symndx;
8049 if (isym->st_shndx == SHN_UNDEF)
8050 continue;
8051 else if (isym->st_shndx == SHN_ABS)
8052 sym_sec = bfd_abs_section_ptr;
8053 else if (isym->st_shndx == SHN_COMMON)
8054 sym_sec = bfd_com_section_ptr;
8055 else
8056 sym_sec
8057 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8058 symval = isym->st_value
8059 + sym_sec->output_section->vma
8060 + sym_sec->output_offset;
8061 }
8062
8063 /* Compute branch offset, from delay slot of the jump to the
8064 branch target. */
8065 sym_offset = (symval + irel->r_addend)
8066 - (sec_start + irel->r_offset + 4);
8067
8068 /* Branch offset must be properly aligned. */
8069 if ((sym_offset & 3) != 0)
8070 continue;
8071
8072 sym_offset >>= 2;
8073
8074 /* Check that it's in range. */
8075 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8076 continue;
8077
8078 /* Get the section contents if we haven't done so already. */
8079 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8080 goto relax_return;
8081
8082 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8083
8084 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8085 if ((instruction & 0xfc1fffff) == 0x0000f809)
8086 instruction = 0x04110000;
8087 /* If it was jr <reg>, turn it into b <target>. */
8088 else if ((instruction & 0xfc1fffff) == 0x00000008)
8089 instruction = 0x10000000;
8090 else
8091 continue;
8092
8093 instruction |= (sym_offset & 0xffff);
8094 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8095 changed_contents = TRUE;
8096 }
8097
8098 if (contents != NULL
8099 && elf_section_data (sec)->this_hdr.contents != contents)
8100 {
8101 if (!changed_contents && !link_info->keep_memory)
8102 free (contents);
8103 else
8104 {
8105 /* Cache the section contents for elf_link_input_bfd. */
8106 elf_section_data (sec)->this_hdr.contents = contents;
8107 }
8108 }
8109 return TRUE;
8110
8111 relax_return:
8112 if (contents != NULL
8113 && elf_section_data (sec)->this_hdr.contents != contents)
8114 free (contents);
8115 return FALSE;
8116 }
8117 \f
8118 /* Allocate space for global sym dynamic relocs. */
8119
8120 static bfd_boolean
8121 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8122 {
8123 struct bfd_link_info *info = inf;
8124 bfd *dynobj;
8125 struct mips_elf_link_hash_entry *hmips;
8126 struct mips_elf_link_hash_table *htab;
8127
8128 htab = mips_elf_hash_table (info);
8129 BFD_ASSERT (htab != NULL);
8130
8131 dynobj = elf_hash_table (info)->dynobj;
8132 hmips = (struct mips_elf_link_hash_entry *) h;
8133
8134 /* VxWorks executables are handled elsewhere; we only need to
8135 allocate relocations in shared objects. */
8136 if (htab->is_vxworks && !info->shared)
8137 return TRUE;
8138
8139 /* Ignore indirect and warning symbols. All relocations against
8140 such symbols will be redirected to the target symbol. */
8141 if (h->root.type == bfd_link_hash_indirect
8142 || h->root.type == bfd_link_hash_warning)
8143 return TRUE;
8144
8145 /* If this symbol is defined in a dynamic object, or we are creating
8146 a shared library, we will need to copy any R_MIPS_32 or
8147 R_MIPS_REL32 relocs against it into the output file. */
8148 if (! info->relocatable
8149 && hmips->possibly_dynamic_relocs != 0
8150 && (h->root.type == bfd_link_hash_defweak
8151 || !h->def_regular
8152 || info->shared))
8153 {
8154 bfd_boolean do_copy = TRUE;
8155
8156 if (h->root.type == bfd_link_hash_undefweak)
8157 {
8158 /* Do not copy relocations for undefined weak symbols with
8159 non-default visibility. */
8160 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8161 do_copy = FALSE;
8162
8163 /* Make sure undefined weak symbols are output as a dynamic
8164 symbol in PIEs. */
8165 else if (h->dynindx == -1 && !h->forced_local)
8166 {
8167 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8168 return FALSE;
8169 }
8170 }
8171
8172 if (do_copy)
8173 {
8174 /* Even though we don't directly need a GOT entry for this symbol,
8175 a symbol must have a dynamic symbol table index greater that
8176 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
8177 if (hmips->global_got_area > GGA_RELOC_ONLY)
8178 hmips->global_got_area = GGA_RELOC_ONLY;
8179
8180 mips_elf_allocate_dynamic_relocations
8181 (dynobj, info, hmips->possibly_dynamic_relocs);
8182 if (hmips->readonly_reloc)
8183 /* We tell the dynamic linker that there are relocations
8184 against the text segment. */
8185 info->flags |= DF_TEXTREL;
8186 }
8187 }
8188
8189 return TRUE;
8190 }
8191
8192 /* Adjust a symbol defined by a dynamic object and referenced by a
8193 regular object. The current definition is in some section of the
8194 dynamic object, but we're not including those sections. We have to
8195 change the definition to something the rest of the link can
8196 understand. */
8197
8198 bfd_boolean
8199 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8200 struct elf_link_hash_entry *h)
8201 {
8202 bfd *dynobj;
8203 struct mips_elf_link_hash_entry *hmips;
8204 struct mips_elf_link_hash_table *htab;
8205
8206 htab = mips_elf_hash_table (info);
8207 BFD_ASSERT (htab != NULL);
8208
8209 dynobj = elf_hash_table (info)->dynobj;
8210 hmips = (struct mips_elf_link_hash_entry *) h;
8211
8212 /* Make sure we know what is going on here. */
8213 BFD_ASSERT (dynobj != NULL
8214 && (h->needs_plt
8215 || h->u.weakdef != NULL
8216 || (h->def_dynamic
8217 && h->ref_regular
8218 && !h->def_regular)));
8219
8220 hmips = (struct mips_elf_link_hash_entry *) h;
8221
8222 /* If there are call relocations against an externally-defined symbol,
8223 see whether we can create a MIPS lazy-binding stub for it. We can
8224 only do this if all references to the function are through call
8225 relocations, and in that case, the traditional lazy-binding stubs
8226 are much more efficient than PLT entries.
8227
8228 Traditional stubs are only available on SVR4 psABI-based systems;
8229 VxWorks always uses PLTs instead. */
8230 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8231 {
8232 if (! elf_hash_table (info)->dynamic_sections_created)
8233 return TRUE;
8234
8235 /* If this symbol is not defined in a regular file, then set
8236 the symbol to the stub location. This is required to make
8237 function pointers compare as equal between the normal
8238 executable and the shared library. */
8239 if (!h->def_regular)
8240 {
8241 hmips->needs_lazy_stub = TRUE;
8242 htab->lazy_stub_count++;
8243 return TRUE;
8244 }
8245 }
8246 /* As above, VxWorks requires PLT entries for externally-defined
8247 functions that are only accessed through call relocations.
8248
8249 Both VxWorks and non-VxWorks targets also need PLT entries if there
8250 are static-only relocations against an externally-defined function.
8251 This can technically occur for shared libraries if there are
8252 branches to the symbol, although it is unlikely that this will be
8253 used in practice due to the short ranges involved. It can occur
8254 for any relative or absolute relocation in executables; in that
8255 case, the PLT entry becomes the function's canonical address. */
8256 else if (((h->needs_plt && !hmips->no_fn_stub)
8257 || (h->type == STT_FUNC && hmips->has_static_relocs))
8258 && htab->use_plts_and_copy_relocs
8259 && !SYMBOL_CALLS_LOCAL (info, h)
8260 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8261 && h->root.type == bfd_link_hash_undefweak))
8262 {
8263 /* If this is the first symbol to need a PLT entry, allocate room
8264 for the header. */
8265 if (htab->splt->size == 0)
8266 {
8267 BFD_ASSERT (htab->sgotplt->size == 0);
8268
8269 /* If we're using the PLT additions to the psABI, each PLT
8270 entry is 16 bytes and the PLT0 entry is 32 bytes.
8271 Encourage better cache usage by aligning. We do this
8272 lazily to avoid pessimizing traditional objects. */
8273 if (!htab->is_vxworks
8274 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8275 return FALSE;
8276
8277 /* Make sure that .got.plt is word-aligned. We do this lazily
8278 for the same reason as above. */
8279 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8280 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8281 return FALSE;
8282
8283 htab->splt->size += htab->plt_header_size;
8284
8285 /* On non-VxWorks targets, the first two entries in .got.plt
8286 are reserved. */
8287 if (!htab->is_vxworks)
8288 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
8289
8290 /* On VxWorks, also allocate room for the header's
8291 .rela.plt.unloaded entries. */
8292 if (htab->is_vxworks && !info->shared)
8293 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8294 }
8295
8296 /* Assign the next .plt entry to this symbol. */
8297 h->plt.offset = htab->splt->size;
8298 htab->splt->size += htab->plt_entry_size;
8299
8300 /* If the output file has no definition of the symbol, set the
8301 symbol's value to the address of the stub. */
8302 if (!info->shared && !h->def_regular)
8303 {
8304 h->root.u.def.section = htab->splt;
8305 h->root.u.def.value = h->plt.offset;
8306 /* For VxWorks, point at the PLT load stub rather than the
8307 lazy resolution stub; this stub will become the canonical
8308 function address. */
8309 if (htab->is_vxworks)
8310 h->root.u.def.value += 8;
8311 }
8312
8313 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8314 relocation. */
8315 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8316 htab->srelplt->size += (htab->is_vxworks
8317 ? MIPS_ELF_RELA_SIZE (dynobj)
8318 : MIPS_ELF_REL_SIZE (dynobj));
8319
8320 /* Make room for the .rela.plt.unloaded relocations. */
8321 if (htab->is_vxworks && !info->shared)
8322 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8323
8324 /* All relocations against this symbol that could have been made
8325 dynamic will now refer to the PLT entry instead. */
8326 hmips->possibly_dynamic_relocs = 0;
8327
8328 return TRUE;
8329 }
8330
8331 /* If this is a weak symbol, and there is a real definition, the
8332 processor independent code will have arranged for us to see the
8333 real definition first, and we can just use the same value. */
8334 if (h->u.weakdef != NULL)
8335 {
8336 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8337 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8338 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8339 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8340 return TRUE;
8341 }
8342
8343 /* Otherwise, there is nothing further to do for symbols defined
8344 in regular objects. */
8345 if (h->def_regular)
8346 return TRUE;
8347
8348 /* There's also nothing more to do if we'll convert all relocations
8349 against this symbol into dynamic relocations. */
8350 if (!hmips->has_static_relocs)
8351 return TRUE;
8352
8353 /* We're now relying on copy relocations. Complain if we have
8354 some that we can't convert. */
8355 if (!htab->use_plts_and_copy_relocs || info->shared)
8356 {
8357 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8358 "dynamic symbol %s"),
8359 h->root.root.string);
8360 bfd_set_error (bfd_error_bad_value);
8361 return FALSE;
8362 }
8363
8364 /* We must allocate the symbol in our .dynbss section, which will
8365 become part of the .bss section of the executable. There will be
8366 an entry for this symbol in the .dynsym section. The dynamic
8367 object will contain position independent code, so all references
8368 from the dynamic object to this symbol will go through the global
8369 offset table. The dynamic linker will use the .dynsym entry to
8370 determine the address it must put in the global offset table, so
8371 both the dynamic object and the regular object will refer to the
8372 same memory location for the variable. */
8373
8374 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8375 {
8376 if (htab->is_vxworks)
8377 htab->srelbss->size += sizeof (Elf32_External_Rela);
8378 else
8379 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8380 h->needs_copy = 1;
8381 }
8382
8383 /* All relocations against this symbol that could have been made
8384 dynamic will now refer to the local copy instead. */
8385 hmips->possibly_dynamic_relocs = 0;
8386
8387 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8388 }
8389 \f
8390 /* This function is called after all the input files have been read,
8391 and the input sections have been assigned to output sections. We
8392 check for any mips16 stub sections that we can discard. */
8393
8394 bfd_boolean
8395 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8396 struct bfd_link_info *info)
8397 {
8398 asection *ri;
8399 struct mips_elf_link_hash_table *htab;
8400 struct mips_htab_traverse_info hti;
8401
8402 htab = mips_elf_hash_table (info);
8403 BFD_ASSERT (htab != NULL);
8404
8405 /* The .reginfo section has a fixed size. */
8406 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8407 if (ri != NULL)
8408 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8409
8410 hti.info = info;
8411 hti.output_bfd = output_bfd;
8412 hti.error = FALSE;
8413 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8414 mips_elf_check_symbols, &hti);
8415 if (hti.error)
8416 return FALSE;
8417
8418 return TRUE;
8419 }
8420
8421 /* If the link uses a GOT, lay it out and work out its size. */
8422
8423 static bfd_boolean
8424 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8425 {
8426 bfd *dynobj;
8427 asection *s;
8428 struct mips_got_info *g;
8429 bfd_size_type loadable_size = 0;
8430 bfd_size_type page_gotno;
8431 bfd *sub;
8432 struct mips_elf_count_tls_arg count_tls_arg;
8433 struct mips_elf_link_hash_table *htab;
8434
8435 htab = mips_elf_hash_table (info);
8436 BFD_ASSERT (htab != NULL);
8437
8438 s = htab->sgot;
8439 if (s == NULL)
8440 return TRUE;
8441
8442 dynobj = elf_hash_table (info)->dynobj;
8443 g = htab->got_info;
8444
8445 /* Allocate room for the reserved entries. VxWorks always reserves
8446 3 entries; other objects only reserve 2 entries. */
8447 BFD_ASSERT (g->assigned_gotno == 0);
8448 if (htab->is_vxworks)
8449 htab->reserved_gotno = 3;
8450 else
8451 htab->reserved_gotno = 2;
8452 g->local_gotno += htab->reserved_gotno;
8453 g->assigned_gotno = htab->reserved_gotno;
8454
8455 /* Replace entries for indirect and warning symbols with entries for
8456 the target symbol. */
8457 if (!mips_elf_resolve_final_got_entries (g))
8458 return FALSE;
8459
8460 /* Count the number of GOT symbols. */
8461 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
8462
8463 /* Calculate the total loadable size of the output. That
8464 will give us the maximum number of GOT_PAGE entries
8465 required. */
8466 for (sub = info->input_bfds; sub; sub = sub->link_next)
8467 {
8468 asection *subsection;
8469
8470 for (subsection = sub->sections;
8471 subsection;
8472 subsection = subsection->next)
8473 {
8474 if ((subsection->flags & SEC_ALLOC) == 0)
8475 continue;
8476 loadable_size += ((subsection->size + 0xf)
8477 &~ (bfd_size_type) 0xf);
8478 }
8479 }
8480
8481 if (htab->is_vxworks)
8482 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8483 relocations against local symbols evaluate to "G", and the EABI does
8484 not include R_MIPS_GOT_PAGE. */
8485 page_gotno = 0;
8486 else
8487 /* Assume there are two loadable segments consisting of contiguous
8488 sections. Is 5 enough? */
8489 page_gotno = (loadable_size >> 16) + 5;
8490
8491 /* Choose the smaller of the two estimates; both are intended to be
8492 conservative. */
8493 if (page_gotno > g->page_gotno)
8494 page_gotno = g->page_gotno;
8495
8496 g->local_gotno += page_gotno;
8497 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8498 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8499
8500 /* We need to calculate tls_gotno for global symbols at this point
8501 instead of building it up earlier, to avoid doublecounting
8502 entries for one global symbol from multiple input files. */
8503 count_tls_arg.info = info;
8504 count_tls_arg.needed = 0;
8505 elf_link_hash_traverse (elf_hash_table (info),
8506 mips_elf_count_global_tls_entries,
8507 &count_tls_arg);
8508 g->tls_gotno += count_tls_arg.needed;
8509 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8510
8511 /* VxWorks does not support multiple GOTs. It initializes $gp to
8512 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8513 dynamic loader. */
8514 if (htab->is_vxworks)
8515 {
8516 /* VxWorks executables do not need a GOT. */
8517 if (info->shared)
8518 {
8519 /* Each VxWorks GOT entry needs an explicit relocation. */
8520 unsigned int count;
8521
8522 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8523 if (count)
8524 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8525 }
8526 }
8527 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8528 {
8529 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8530 return FALSE;
8531 }
8532 else
8533 {
8534 struct mips_elf_count_tls_arg arg;
8535
8536 /* Set up TLS entries. */
8537 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8538 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
8539
8540 /* Allocate room for the TLS relocations. */
8541 arg.info = info;
8542 arg.needed = 0;
8543 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8544 elf_link_hash_traverse (elf_hash_table (info),
8545 mips_elf_count_global_tls_relocs,
8546 &arg);
8547 if (arg.needed)
8548 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
8549 }
8550
8551 return TRUE;
8552 }
8553
8554 /* Estimate the size of the .MIPS.stubs section. */
8555
8556 static void
8557 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8558 {
8559 struct mips_elf_link_hash_table *htab;
8560 bfd_size_type dynsymcount;
8561
8562 htab = mips_elf_hash_table (info);
8563 BFD_ASSERT (htab != NULL);
8564
8565 if (htab->lazy_stub_count == 0)
8566 return;
8567
8568 /* IRIX rld assumes that a function stub isn't at the end of the .text
8569 section, so add a dummy entry to the end. */
8570 htab->lazy_stub_count++;
8571
8572 /* Get a worst-case estimate of the number of dynamic symbols needed.
8573 At this point, dynsymcount does not account for section symbols
8574 and count_section_dynsyms may overestimate the number that will
8575 be needed. */
8576 dynsymcount = (elf_hash_table (info)->dynsymcount
8577 + count_section_dynsyms (output_bfd, info));
8578
8579 /* Determine the size of one stub entry. */
8580 htab->function_stub_size = (dynsymcount > 0x10000
8581 ? MIPS_FUNCTION_STUB_BIG_SIZE
8582 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8583
8584 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8585 }
8586
8587 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8588 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8589 allocate an entry in the stubs section. */
8590
8591 static bfd_boolean
8592 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8593 {
8594 struct mips_elf_link_hash_table *htab;
8595
8596 htab = (struct mips_elf_link_hash_table *) data;
8597 if (h->needs_lazy_stub)
8598 {
8599 h->root.root.u.def.section = htab->sstubs;
8600 h->root.root.u.def.value = htab->sstubs->size;
8601 h->root.plt.offset = htab->sstubs->size;
8602 htab->sstubs->size += htab->function_stub_size;
8603 }
8604 return TRUE;
8605 }
8606
8607 /* Allocate offsets in the stubs section to each symbol that needs one.
8608 Set the final size of the .MIPS.stub section. */
8609
8610 static void
8611 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8612 {
8613 struct mips_elf_link_hash_table *htab;
8614
8615 htab = mips_elf_hash_table (info);
8616 BFD_ASSERT (htab != NULL);
8617
8618 if (htab->lazy_stub_count == 0)
8619 return;
8620
8621 htab->sstubs->size = 0;
8622 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
8623 htab->sstubs->size += htab->function_stub_size;
8624 BFD_ASSERT (htab->sstubs->size
8625 == htab->lazy_stub_count * htab->function_stub_size);
8626 }
8627
8628 /* Set the sizes of the dynamic sections. */
8629
8630 bfd_boolean
8631 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8632 struct bfd_link_info *info)
8633 {
8634 bfd *dynobj;
8635 asection *s, *sreldyn;
8636 bfd_boolean reltext;
8637 struct mips_elf_link_hash_table *htab;
8638
8639 htab = mips_elf_hash_table (info);
8640 BFD_ASSERT (htab != NULL);
8641 dynobj = elf_hash_table (info)->dynobj;
8642 BFD_ASSERT (dynobj != NULL);
8643
8644 if (elf_hash_table (info)->dynamic_sections_created)
8645 {
8646 /* Set the contents of the .interp section to the interpreter. */
8647 if (info->executable)
8648 {
8649 s = bfd_get_section_by_name (dynobj, ".interp");
8650 BFD_ASSERT (s != NULL);
8651 s->size
8652 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8653 s->contents
8654 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8655 }
8656
8657 /* Create a symbol for the PLT, if we know that we are using it. */
8658 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8659 {
8660 struct elf_link_hash_entry *h;
8661
8662 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8663
8664 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8665 "_PROCEDURE_LINKAGE_TABLE_");
8666 htab->root.hplt = h;
8667 if (h == NULL)
8668 return FALSE;
8669 h->type = STT_FUNC;
8670 }
8671 }
8672
8673 /* Allocate space for global sym dynamic relocs. */
8674 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8675
8676 mips_elf_estimate_stub_size (output_bfd, info);
8677
8678 if (!mips_elf_lay_out_got (output_bfd, info))
8679 return FALSE;
8680
8681 mips_elf_lay_out_lazy_stubs (info);
8682
8683 /* The check_relocs and adjust_dynamic_symbol entry points have
8684 determined the sizes of the various dynamic sections. Allocate
8685 memory for them. */
8686 reltext = FALSE;
8687 for (s = dynobj->sections; s != NULL; s = s->next)
8688 {
8689 const char *name;
8690
8691 /* It's OK to base decisions on the section name, because none
8692 of the dynobj section names depend upon the input files. */
8693 name = bfd_get_section_name (dynobj, s);
8694
8695 if ((s->flags & SEC_LINKER_CREATED) == 0)
8696 continue;
8697
8698 if (CONST_STRNEQ (name, ".rel"))
8699 {
8700 if (s->size != 0)
8701 {
8702 const char *outname;
8703 asection *target;
8704
8705 /* If this relocation section applies to a read only
8706 section, then we probably need a DT_TEXTREL entry.
8707 If the relocation section is .rel(a).dyn, we always
8708 assert a DT_TEXTREL entry rather than testing whether
8709 there exists a relocation to a read only section or
8710 not. */
8711 outname = bfd_get_section_name (output_bfd,
8712 s->output_section);
8713 target = bfd_get_section_by_name (output_bfd, outname + 4);
8714 if ((target != NULL
8715 && (target->flags & SEC_READONLY) != 0
8716 && (target->flags & SEC_ALLOC) != 0)
8717 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8718 reltext = TRUE;
8719
8720 /* We use the reloc_count field as a counter if we need
8721 to copy relocs into the output file. */
8722 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8723 s->reloc_count = 0;
8724
8725 /* If combreloc is enabled, elf_link_sort_relocs() will
8726 sort relocations, but in a different way than we do,
8727 and before we're done creating relocations. Also, it
8728 will move them around between input sections'
8729 relocation's contents, so our sorting would be
8730 broken, so don't let it run. */
8731 info->combreloc = 0;
8732 }
8733 }
8734 else if (! info->shared
8735 && ! mips_elf_hash_table (info)->use_rld_obj_head
8736 && CONST_STRNEQ (name, ".rld_map"))
8737 {
8738 /* We add a room for __rld_map. It will be filled in by the
8739 rtld to contain a pointer to the _r_debug structure. */
8740 s->size += 4;
8741 }
8742 else if (SGI_COMPAT (output_bfd)
8743 && CONST_STRNEQ (name, ".compact_rel"))
8744 s->size += mips_elf_hash_table (info)->compact_rel_size;
8745 else if (s == htab->splt)
8746 {
8747 /* If the last PLT entry has a branch delay slot, allocate
8748 room for an extra nop to fill the delay slot. This is
8749 for CPUs without load interlocking. */
8750 if (! LOAD_INTERLOCKS_P (output_bfd)
8751 && ! htab->is_vxworks && s->size > 0)
8752 s->size += 4;
8753 }
8754 else if (! CONST_STRNEQ (name, ".init")
8755 && s != htab->sgot
8756 && s != htab->sgotplt
8757 && s != htab->sstubs
8758 && s != htab->sdynbss)
8759 {
8760 /* It's not one of our sections, so don't allocate space. */
8761 continue;
8762 }
8763
8764 if (s->size == 0)
8765 {
8766 s->flags |= SEC_EXCLUDE;
8767 continue;
8768 }
8769
8770 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8771 continue;
8772
8773 /* Allocate memory for the section contents. */
8774 s->contents = bfd_zalloc (dynobj, s->size);
8775 if (s->contents == NULL)
8776 {
8777 bfd_set_error (bfd_error_no_memory);
8778 return FALSE;
8779 }
8780 }
8781
8782 if (elf_hash_table (info)->dynamic_sections_created)
8783 {
8784 /* Add some entries to the .dynamic section. We fill in the
8785 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8786 must add the entries now so that we get the correct size for
8787 the .dynamic section. */
8788
8789 /* SGI object has the equivalence of DT_DEBUG in the
8790 DT_MIPS_RLD_MAP entry. This must come first because glibc
8791 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8792 looks at the first one it sees. */
8793 if (!info->shared
8794 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8795 return FALSE;
8796
8797 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8798 used by the debugger. */
8799 if (info->executable
8800 && !SGI_COMPAT (output_bfd)
8801 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8802 return FALSE;
8803
8804 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8805 info->flags |= DF_TEXTREL;
8806
8807 if ((info->flags & DF_TEXTREL) != 0)
8808 {
8809 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8810 return FALSE;
8811
8812 /* Clear the DF_TEXTREL flag. It will be set again if we
8813 write out an actual text relocation; we may not, because
8814 at this point we do not know whether e.g. any .eh_frame
8815 absolute relocations have been converted to PC-relative. */
8816 info->flags &= ~DF_TEXTREL;
8817 }
8818
8819 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8820 return FALSE;
8821
8822 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
8823 if (htab->is_vxworks)
8824 {
8825 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8826 use any of the DT_MIPS_* tags. */
8827 if (sreldyn && sreldyn->size > 0)
8828 {
8829 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8830 return FALSE;
8831
8832 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8833 return FALSE;
8834
8835 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8836 return FALSE;
8837 }
8838 }
8839 else
8840 {
8841 if (sreldyn && sreldyn->size > 0)
8842 {
8843 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8844 return FALSE;
8845
8846 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8847 return FALSE;
8848
8849 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8850 return FALSE;
8851 }
8852
8853 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8854 return FALSE;
8855
8856 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8857 return FALSE;
8858
8859 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8860 return FALSE;
8861
8862 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8863 return FALSE;
8864
8865 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8866 return FALSE;
8867
8868 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8869 return FALSE;
8870
8871 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8872 return FALSE;
8873
8874 if (IRIX_COMPAT (dynobj) == ict_irix5
8875 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8876 return FALSE;
8877
8878 if (IRIX_COMPAT (dynobj) == ict_irix6
8879 && (bfd_get_section_by_name
8880 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8881 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8882 return FALSE;
8883 }
8884 if (htab->splt->size > 0)
8885 {
8886 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8887 return FALSE;
8888
8889 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8890 return FALSE;
8891
8892 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8893 return FALSE;
8894
8895 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8896 return FALSE;
8897 }
8898 if (htab->is_vxworks
8899 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8900 return FALSE;
8901 }
8902
8903 return TRUE;
8904 }
8905 \f
8906 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8907 Adjust its R_ADDEND field so that it is correct for the output file.
8908 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8909 and sections respectively; both use symbol indexes. */
8910
8911 static void
8912 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8913 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8914 asection **local_sections, Elf_Internal_Rela *rel)
8915 {
8916 unsigned int r_type, r_symndx;
8917 Elf_Internal_Sym *sym;
8918 asection *sec;
8919
8920 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8921 {
8922 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8923 if (r_type == R_MIPS16_GPREL
8924 || r_type == R_MIPS_GPREL16
8925 || r_type == R_MIPS_GPREL32
8926 || r_type == R_MIPS_LITERAL)
8927 {
8928 rel->r_addend += _bfd_get_gp_value (input_bfd);
8929 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8930 }
8931
8932 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8933 sym = local_syms + r_symndx;
8934
8935 /* Adjust REL's addend to account for section merging. */
8936 if (!info->relocatable)
8937 {
8938 sec = local_sections[r_symndx];
8939 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8940 }
8941
8942 /* This would normally be done by the rela_normal code in elflink.c. */
8943 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8944 rel->r_addend += local_sections[r_symndx]->output_offset;
8945 }
8946 }
8947
8948 /* Relocate a MIPS ELF section. */
8949
8950 bfd_boolean
8951 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8952 bfd *input_bfd, asection *input_section,
8953 bfd_byte *contents, Elf_Internal_Rela *relocs,
8954 Elf_Internal_Sym *local_syms,
8955 asection **local_sections)
8956 {
8957 Elf_Internal_Rela *rel;
8958 const Elf_Internal_Rela *relend;
8959 bfd_vma addend = 0;
8960 bfd_boolean use_saved_addend_p = FALSE;
8961 const struct elf_backend_data *bed;
8962
8963 bed = get_elf_backend_data (output_bfd);
8964 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8965 for (rel = relocs; rel < relend; ++rel)
8966 {
8967 const char *name;
8968 bfd_vma value = 0;
8969 reloc_howto_type *howto;
8970 bfd_boolean cross_mode_jump_p;
8971 /* TRUE if the relocation is a RELA relocation, rather than a
8972 REL relocation. */
8973 bfd_boolean rela_relocation_p = TRUE;
8974 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8975 const char *msg;
8976 unsigned long r_symndx;
8977 asection *sec;
8978 Elf_Internal_Shdr *symtab_hdr;
8979 struct elf_link_hash_entry *h;
8980
8981 /* Find the relocation howto for this relocation. */
8982 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8983 NEWABI_P (input_bfd)
8984 && (MIPS_RELOC_RELA_P
8985 (input_bfd, input_section,
8986 rel - relocs)));
8987
8988 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8989 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8990 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8991 {
8992 sec = local_sections[r_symndx];
8993 h = NULL;
8994 }
8995 else
8996 {
8997 unsigned long extsymoff;
8998
8999 extsymoff = 0;
9000 if (!elf_bad_symtab (input_bfd))
9001 extsymoff = symtab_hdr->sh_info;
9002 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9003 while (h->root.type == bfd_link_hash_indirect
9004 || h->root.type == bfd_link_hash_warning)
9005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9006
9007 sec = NULL;
9008 if (h->root.type == bfd_link_hash_defined
9009 || h->root.type == bfd_link_hash_defweak)
9010 sec = h->root.u.def.section;
9011 }
9012
9013 if (sec != NULL && elf_discarded_section (sec))
9014 {
9015 /* For relocs against symbols from removed linkonce sections,
9016 or sections discarded by a linker script, we just want the
9017 section contents zeroed. Avoid any special processing. */
9018 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
9019 rel->r_info = 0;
9020 rel->r_addend = 0;
9021 continue;
9022 }
9023
9024 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9025 {
9026 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9027 64-bit code, but make sure all their addresses are in the
9028 lowermost or uppermost 32-bit section of the 64-bit address
9029 space. Thus, when they use an R_MIPS_64 they mean what is
9030 usually meant by R_MIPS_32, with the exception that the
9031 stored value is sign-extended to 64 bits. */
9032 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9033
9034 /* On big-endian systems, we need to lie about the position
9035 of the reloc. */
9036 if (bfd_big_endian (input_bfd))
9037 rel->r_offset += 4;
9038 }
9039
9040 if (!use_saved_addend_p)
9041 {
9042 /* If these relocations were originally of the REL variety,
9043 we must pull the addend out of the field that will be
9044 relocated. Otherwise, we simply use the contents of the
9045 RELA relocation. */
9046 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9047 relocs, rel))
9048 {
9049 rela_relocation_p = FALSE;
9050 addend = mips_elf_read_rel_addend (input_bfd, rel,
9051 howto, contents);
9052 if (hi16_reloc_p (r_type)
9053 || (got16_reloc_p (r_type)
9054 && mips_elf_local_relocation_p (input_bfd, rel,
9055 local_sections, FALSE)))
9056 {
9057 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9058 contents, &addend))
9059 {
9060 if (h)
9061 name = h->root.root.string;
9062 else
9063 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9064 local_syms + r_symndx,
9065 sec);
9066 (*_bfd_error_handler)
9067 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9068 input_bfd, input_section, name, howto->name,
9069 rel->r_offset);
9070 }
9071 }
9072 else
9073 addend <<= howto->rightshift;
9074 }
9075 else
9076 addend = rel->r_addend;
9077 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9078 local_syms, local_sections, rel);
9079 }
9080
9081 if (info->relocatable)
9082 {
9083 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9084 && bfd_big_endian (input_bfd))
9085 rel->r_offset -= 4;
9086
9087 if (!rela_relocation_p && rel->r_addend)
9088 {
9089 addend += rel->r_addend;
9090 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9091 addend = mips_elf_high (addend);
9092 else if (r_type == R_MIPS_HIGHER)
9093 addend = mips_elf_higher (addend);
9094 else if (r_type == R_MIPS_HIGHEST)
9095 addend = mips_elf_highest (addend);
9096 else
9097 addend >>= howto->rightshift;
9098
9099 /* We use the source mask, rather than the destination
9100 mask because the place to which we are writing will be
9101 source of the addend in the final link. */
9102 addend &= howto->src_mask;
9103
9104 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9105 /* See the comment above about using R_MIPS_64 in the 32-bit
9106 ABI. Here, we need to update the addend. It would be
9107 possible to get away with just using the R_MIPS_32 reloc
9108 but for endianness. */
9109 {
9110 bfd_vma sign_bits;
9111 bfd_vma low_bits;
9112 bfd_vma high_bits;
9113
9114 if (addend & ((bfd_vma) 1 << 31))
9115 #ifdef BFD64
9116 sign_bits = ((bfd_vma) 1 << 32) - 1;
9117 #else
9118 sign_bits = -1;
9119 #endif
9120 else
9121 sign_bits = 0;
9122
9123 /* If we don't know that we have a 64-bit type,
9124 do two separate stores. */
9125 if (bfd_big_endian (input_bfd))
9126 {
9127 /* Store the sign-bits (which are most significant)
9128 first. */
9129 low_bits = sign_bits;
9130 high_bits = addend;
9131 }
9132 else
9133 {
9134 low_bits = addend;
9135 high_bits = sign_bits;
9136 }
9137 bfd_put_32 (input_bfd, low_bits,
9138 contents + rel->r_offset);
9139 bfd_put_32 (input_bfd, high_bits,
9140 contents + rel->r_offset + 4);
9141 continue;
9142 }
9143
9144 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9145 input_bfd, input_section,
9146 contents, FALSE))
9147 return FALSE;
9148 }
9149
9150 /* Go on to the next relocation. */
9151 continue;
9152 }
9153
9154 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9155 relocations for the same offset. In that case we are
9156 supposed to treat the output of each relocation as the addend
9157 for the next. */
9158 if (rel + 1 < relend
9159 && rel->r_offset == rel[1].r_offset
9160 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9161 use_saved_addend_p = TRUE;
9162 else
9163 use_saved_addend_p = FALSE;
9164
9165 /* Figure out what value we are supposed to relocate. */
9166 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9167 input_section, info, rel,
9168 addend, howto, local_syms,
9169 local_sections, &value,
9170 &name, &cross_mode_jump_p,
9171 use_saved_addend_p))
9172 {
9173 case bfd_reloc_continue:
9174 /* There's nothing to do. */
9175 continue;
9176
9177 case bfd_reloc_undefined:
9178 /* mips_elf_calculate_relocation already called the
9179 undefined_symbol callback. There's no real point in
9180 trying to perform the relocation at this point, so we
9181 just skip ahead to the next relocation. */
9182 continue;
9183
9184 case bfd_reloc_notsupported:
9185 msg = _("internal error: unsupported relocation error");
9186 info->callbacks->warning
9187 (info, msg, name, input_bfd, input_section, rel->r_offset);
9188 return FALSE;
9189
9190 case bfd_reloc_overflow:
9191 if (use_saved_addend_p)
9192 /* Ignore overflow until we reach the last relocation for
9193 a given location. */
9194 ;
9195 else
9196 {
9197 struct mips_elf_link_hash_table *htab;
9198
9199 htab = mips_elf_hash_table (info);
9200 BFD_ASSERT (htab != NULL);
9201 BFD_ASSERT (name != NULL);
9202 if (!htab->small_data_overflow_reported
9203 && (gprel16_reloc_p (howto->type)
9204 || howto->type == R_MIPS_LITERAL))
9205 {
9206 msg = _("small-data section exceeds 64KB;"
9207 " lower small-data size limit (see option -G)");
9208
9209 htab->small_data_overflow_reported = TRUE;
9210 (*info->callbacks->einfo) ("%P: %s\n", msg);
9211 }
9212 if (! ((*info->callbacks->reloc_overflow)
9213 (info, NULL, name, howto->name, (bfd_vma) 0,
9214 input_bfd, input_section, rel->r_offset)))
9215 return FALSE;
9216 }
9217 break;
9218
9219 case bfd_reloc_ok:
9220 break;
9221
9222 default:
9223 abort ();
9224 break;
9225 }
9226
9227 /* If we've got another relocation for the address, keep going
9228 until we reach the last one. */
9229 if (use_saved_addend_p)
9230 {
9231 addend = value;
9232 continue;
9233 }
9234
9235 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9236 /* See the comment above about using R_MIPS_64 in the 32-bit
9237 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9238 that calculated the right value. Now, however, we
9239 sign-extend the 32-bit result to 64-bits, and store it as a
9240 64-bit value. We are especially generous here in that we
9241 go to extreme lengths to support this usage on systems with
9242 only a 32-bit VMA. */
9243 {
9244 bfd_vma sign_bits;
9245 bfd_vma low_bits;
9246 bfd_vma high_bits;
9247
9248 if (value & ((bfd_vma) 1 << 31))
9249 #ifdef BFD64
9250 sign_bits = ((bfd_vma) 1 << 32) - 1;
9251 #else
9252 sign_bits = -1;
9253 #endif
9254 else
9255 sign_bits = 0;
9256
9257 /* If we don't know that we have a 64-bit type,
9258 do two separate stores. */
9259 if (bfd_big_endian (input_bfd))
9260 {
9261 /* Undo what we did above. */
9262 rel->r_offset -= 4;
9263 /* Store the sign-bits (which are most significant)
9264 first. */
9265 low_bits = sign_bits;
9266 high_bits = value;
9267 }
9268 else
9269 {
9270 low_bits = value;
9271 high_bits = sign_bits;
9272 }
9273 bfd_put_32 (input_bfd, low_bits,
9274 contents + rel->r_offset);
9275 bfd_put_32 (input_bfd, high_bits,
9276 contents + rel->r_offset + 4);
9277 continue;
9278 }
9279
9280 /* Actually perform the relocation. */
9281 if (! mips_elf_perform_relocation (info, howto, rel, value,
9282 input_bfd, input_section,
9283 contents, cross_mode_jump_p))
9284 return FALSE;
9285 }
9286
9287 return TRUE;
9288 }
9289 \f
9290 /* A function that iterates over each entry in la25_stubs and fills
9291 in the code for each one. DATA points to a mips_htab_traverse_info. */
9292
9293 static int
9294 mips_elf_create_la25_stub (void **slot, void *data)
9295 {
9296 struct mips_htab_traverse_info *hti;
9297 struct mips_elf_link_hash_table *htab;
9298 struct mips_elf_la25_stub *stub;
9299 asection *s;
9300 bfd_byte *loc;
9301 bfd_vma offset, target, target_high, target_low;
9302
9303 stub = (struct mips_elf_la25_stub *) *slot;
9304 hti = (struct mips_htab_traverse_info *) data;
9305 htab = mips_elf_hash_table (hti->info);
9306 BFD_ASSERT (htab != NULL);
9307
9308 /* Create the section contents, if we haven't already. */
9309 s = stub->stub_section;
9310 loc = s->contents;
9311 if (loc == NULL)
9312 {
9313 loc = bfd_malloc (s->size);
9314 if (loc == NULL)
9315 {
9316 hti->error = TRUE;
9317 return FALSE;
9318 }
9319 s->contents = loc;
9320 }
9321
9322 /* Work out where in the section this stub should go. */
9323 offset = stub->offset;
9324
9325 /* Work out the target address. */
9326 target = (stub->h->root.root.u.def.section->output_section->vma
9327 + stub->h->root.root.u.def.section->output_offset
9328 + stub->h->root.root.u.def.value);
9329 target_high = ((target + 0x8000) >> 16) & 0xffff;
9330 target_low = (target & 0xffff);
9331
9332 if (stub->stub_section != htab->strampoline)
9333 {
9334 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9335 of the section and write the two instructions at the end. */
9336 memset (loc, 0, offset);
9337 loc += offset;
9338 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9339 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9340 }
9341 else
9342 {
9343 /* This is trampoline. */
9344 loc += offset;
9345 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9346 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9347 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9348 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9349 }
9350 return TRUE;
9351 }
9352
9353 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9354 adjust it appropriately now. */
9355
9356 static void
9357 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9358 const char *name, Elf_Internal_Sym *sym)
9359 {
9360 /* The linker script takes care of providing names and values for
9361 these, but we must place them into the right sections. */
9362 static const char* const text_section_symbols[] = {
9363 "_ftext",
9364 "_etext",
9365 "__dso_displacement",
9366 "__elf_header",
9367 "__program_header_table",
9368 NULL
9369 };
9370
9371 static const char* const data_section_symbols[] = {
9372 "_fdata",
9373 "_edata",
9374 "_end",
9375 "_fbss",
9376 NULL
9377 };
9378
9379 const char* const *p;
9380 int i;
9381
9382 for (i = 0; i < 2; ++i)
9383 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9384 *p;
9385 ++p)
9386 if (strcmp (*p, name) == 0)
9387 {
9388 /* All of these symbols are given type STT_SECTION by the
9389 IRIX6 linker. */
9390 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9391 sym->st_other = STO_PROTECTED;
9392
9393 /* The IRIX linker puts these symbols in special sections. */
9394 if (i == 0)
9395 sym->st_shndx = SHN_MIPS_TEXT;
9396 else
9397 sym->st_shndx = SHN_MIPS_DATA;
9398
9399 break;
9400 }
9401 }
9402
9403 /* Finish up dynamic symbol handling. We set the contents of various
9404 dynamic sections here. */
9405
9406 bfd_boolean
9407 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9408 struct bfd_link_info *info,
9409 struct elf_link_hash_entry *h,
9410 Elf_Internal_Sym *sym)
9411 {
9412 bfd *dynobj;
9413 asection *sgot;
9414 struct mips_got_info *g, *gg;
9415 const char *name;
9416 int idx;
9417 struct mips_elf_link_hash_table *htab;
9418 struct mips_elf_link_hash_entry *hmips;
9419
9420 htab = mips_elf_hash_table (info);
9421 BFD_ASSERT (htab != NULL);
9422 dynobj = elf_hash_table (info)->dynobj;
9423 hmips = (struct mips_elf_link_hash_entry *) h;
9424
9425 BFD_ASSERT (!htab->is_vxworks);
9426
9427 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9428 {
9429 /* We've decided to create a PLT entry for this symbol. */
9430 bfd_byte *loc;
9431 bfd_vma header_address, plt_index, got_address;
9432 bfd_vma got_address_high, got_address_low, load;
9433 const bfd_vma *plt_entry;
9434
9435 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9436 BFD_ASSERT (h->dynindx != -1);
9437 BFD_ASSERT (htab->splt != NULL);
9438 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9439 BFD_ASSERT (!h->def_regular);
9440
9441 /* Calculate the address of the PLT header. */
9442 header_address = (htab->splt->output_section->vma
9443 + htab->splt->output_offset);
9444
9445 /* Calculate the index of the entry. */
9446 plt_index = ((h->plt.offset - htab->plt_header_size)
9447 / htab->plt_entry_size);
9448
9449 /* Calculate the address of the .got.plt entry. */
9450 got_address = (htab->sgotplt->output_section->vma
9451 + htab->sgotplt->output_offset
9452 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9453 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9454 got_address_low = got_address & 0xffff;
9455
9456 /* Initially point the .got.plt entry at the PLT header. */
9457 loc = (htab->sgotplt->contents
9458 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9459 if (ABI_64_P (output_bfd))
9460 bfd_put_64 (output_bfd, header_address, loc);
9461 else
9462 bfd_put_32 (output_bfd, header_address, loc);
9463
9464 /* Find out where the .plt entry should go. */
9465 loc = htab->splt->contents + h->plt.offset;
9466
9467 /* Pick the load opcode. */
9468 load = MIPS_ELF_LOAD_WORD (output_bfd);
9469
9470 /* Fill in the PLT entry itself. */
9471 plt_entry = mips_exec_plt_entry;
9472 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9473 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9474
9475 if (! LOAD_INTERLOCKS_P (output_bfd))
9476 {
9477 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9478 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9479 }
9480 else
9481 {
9482 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9483 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9484 }
9485
9486 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9487 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9488 plt_index, h->dynindx,
9489 R_MIPS_JUMP_SLOT, got_address);
9490
9491 /* We distinguish between PLT entries and lazy-binding stubs by
9492 giving the former an st_other value of STO_MIPS_PLT. Set the
9493 flag and leave the value if there are any relocations in the
9494 binary where pointer equality matters. */
9495 sym->st_shndx = SHN_UNDEF;
9496 if (h->pointer_equality_needed)
9497 sym->st_other = STO_MIPS_PLT;
9498 else
9499 sym->st_value = 0;
9500 }
9501 else if (h->plt.offset != MINUS_ONE)
9502 {
9503 /* We've decided to create a lazy-binding stub. */
9504 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9505
9506 /* This symbol has a stub. Set it up. */
9507
9508 BFD_ASSERT (h->dynindx != -1);
9509
9510 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9511 || (h->dynindx <= 0xffff));
9512
9513 /* Values up to 2^31 - 1 are allowed. Larger values would cause
9514 sign extension at runtime in the stub, resulting in a negative
9515 index value. */
9516 if (h->dynindx & ~0x7fffffff)
9517 return FALSE;
9518
9519 /* Fill the stub. */
9520 idx = 0;
9521 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9522 idx += 4;
9523 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9524 idx += 4;
9525 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9526 {
9527 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9528 stub + idx);
9529 idx += 4;
9530 }
9531 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9532 idx += 4;
9533
9534 /* If a large stub is not required and sign extension is not a
9535 problem, then use legacy code in the stub. */
9536 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9537 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9538 else if (h->dynindx & ~0x7fff)
9539 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9540 else
9541 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9542 stub + idx);
9543
9544 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9545 memcpy (htab->sstubs->contents + h->plt.offset,
9546 stub, htab->function_stub_size);
9547
9548 /* Mark the symbol as undefined. plt.offset != -1 occurs
9549 only for the referenced symbol. */
9550 sym->st_shndx = SHN_UNDEF;
9551
9552 /* The run-time linker uses the st_value field of the symbol
9553 to reset the global offset table entry for this external
9554 to its stub address when unlinking a shared object. */
9555 sym->st_value = (htab->sstubs->output_section->vma
9556 + htab->sstubs->output_offset
9557 + h->plt.offset);
9558 }
9559
9560 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9561 refer to the stub, since only the stub uses the standard calling
9562 conventions. */
9563 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9564 {
9565 BFD_ASSERT (hmips->need_fn_stub);
9566 sym->st_value = (hmips->fn_stub->output_section->vma
9567 + hmips->fn_stub->output_offset);
9568 sym->st_size = hmips->fn_stub->size;
9569 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9570 }
9571
9572 BFD_ASSERT (h->dynindx != -1
9573 || h->forced_local);
9574
9575 sgot = htab->sgot;
9576 g = htab->got_info;
9577 BFD_ASSERT (g != NULL);
9578
9579 /* Run through the global symbol table, creating GOT entries for all
9580 the symbols that need them. */
9581 if (g->global_gotsym != NULL
9582 && h->dynindx >= g->global_gotsym->dynindx)
9583 {
9584 bfd_vma offset;
9585 bfd_vma value;
9586
9587 value = sym->st_value;
9588 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9589 R_MIPS_GOT16, info);
9590 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9591 }
9592
9593 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
9594 {
9595 struct mips_got_entry e, *p;
9596 bfd_vma entry;
9597 bfd_vma offset;
9598
9599 gg = g;
9600
9601 e.abfd = output_bfd;
9602 e.symndx = -1;
9603 e.d.h = hmips;
9604 e.tls_type = 0;
9605
9606 for (g = g->next; g->next != gg; g = g->next)
9607 {
9608 if (g->got_entries
9609 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9610 &e)))
9611 {
9612 offset = p->gotidx;
9613 if (info->shared
9614 || (elf_hash_table (info)->dynamic_sections_created
9615 && p->d.h != NULL
9616 && p->d.h->root.def_dynamic
9617 && !p->d.h->root.def_regular))
9618 {
9619 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9620 the various compatibility problems, it's easier to mock
9621 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9622 mips_elf_create_dynamic_relocation to calculate the
9623 appropriate addend. */
9624 Elf_Internal_Rela rel[3];
9625
9626 memset (rel, 0, sizeof (rel));
9627 if (ABI_64_P (output_bfd))
9628 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9629 else
9630 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9631 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9632
9633 entry = 0;
9634 if (! (mips_elf_create_dynamic_relocation
9635 (output_bfd, info, rel,
9636 e.d.h, NULL, sym->st_value, &entry, sgot)))
9637 return FALSE;
9638 }
9639 else
9640 entry = sym->st_value;
9641 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
9642 }
9643 }
9644 }
9645
9646 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9647 name = h->root.root.string;
9648 if (strcmp (name, "_DYNAMIC") == 0
9649 || h == elf_hash_table (info)->hgot)
9650 sym->st_shndx = SHN_ABS;
9651 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9652 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9653 {
9654 sym->st_shndx = SHN_ABS;
9655 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9656 sym->st_value = 1;
9657 }
9658 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
9659 {
9660 sym->st_shndx = SHN_ABS;
9661 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9662 sym->st_value = elf_gp (output_bfd);
9663 }
9664 else if (SGI_COMPAT (output_bfd))
9665 {
9666 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9667 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9668 {
9669 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9670 sym->st_other = STO_PROTECTED;
9671 sym->st_value = 0;
9672 sym->st_shndx = SHN_MIPS_DATA;
9673 }
9674 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9675 {
9676 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9677 sym->st_other = STO_PROTECTED;
9678 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9679 sym->st_shndx = SHN_ABS;
9680 }
9681 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9682 {
9683 if (h->type == STT_FUNC)
9684 sym->st_shndx = SHN_MIPS_TEXT;
9685 else if (h->type == STT_OBJECT)
9686 sym->st_shndx = SHN_MIPS_DATA;
9687 }
9688 }
9689
9690 /* Emit a copy reloc, if needed. */
9691 if (h->needs_copy)
9692 {
9693 asection *s;
9694 bfd_vma symval;
9695
9696 BFD_ASSERT (h->dynindx != -1);
9697 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9698
9699 s = mips_elf_rel_dyn_section (info, FALSE);
9700 symval = (h->root.u.def.section->output_section->vma
9701 + h->root.u.def.section->output_offset
9702 + h->root.u.def.value);
9703 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9704 h->dynindx, R_MIPS_COPY, symval);
9705 }
9706
9707 /* Handle the IRIX6-specific symbols. */
9708 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9709 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9710
9711 if (! info->shared)
9712 {
9713 if (! mips_elf_hash_table (info)->use_rld_obj_head
9714 && (strcmp (name, "__rld_map") == 0
9715 || strcmp (name, "__RLD_MAP") == 0))
9716 {
9717 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9718 BFD_ASSERT (s != NULL);
9719 sym->st_value = s->output_section->vma + s->output_offset;
9720 bfd_put_32 (output_bfd, 0, s->contents);
9721 if (mips_elf_hash_table (info)->rld_value == 0)
9722 mips_elf_hash_table (info)->rld_value = sym->st_value;
9723 }
9724 else if (mips_elf_hash_table (info)->use_rld_obj_head
9725 && strcmp (name, "__rld_obj_head") == 0)
9726 {
9727 /* IRIX6 does not use a .rld_map section. */
9728 if (IRIX_COMPAT (output_bfd) == ict_irix5
9729 || IRIX_COMPAT (output_bfd) == ict_none)
9730 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9731 != NULL);
9732 mips_elf_hash_table (info)->rld_value = sym->st_value;
9733 }
9734 }
9735
9736 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9737 treat MIPS16 symbols like any other. */
9738 if (ELF_ST_IS_MIPS16 (sym->st_other))
9739 {
9740 BFD_ASSERT (sym->st_value & 1);
9741 sym->st_other -= STO_MIPS16;
9742 }
9743
9744 return TRUE;
9745 }
9746
9747 /* Likewise, for VxWorks. */
9748
9749 bfd_boolean
9750 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9751 struct bfd_link_info *info,
9752 struct elf_link_hash_entry *h,
9753 Elf_Internal_Sym *sym)
9754 {
9755 bfd *dynobj;
9756 asection *sgot;
9757 struct mips_got_info *g;
9758 struct mips_elf_link_hash_table *htab;
9759
9760 htab = mips_elf_hash_table (info);
9761 BFD_ASSERT (htab != NULL);
9762 dynobj = elf_hash_table (info)->dynobj;
9763
9764 if (h->plt.offset != (bfd_vma) -1)
9765 {
9766 bfd_byte *loc;
9767 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9768 Elf_Internal_Rela rel;
9769 static const bfd_vma *plt_entry;
9770
9771 BFD_ASSERT (h->dynindx != -1);
9772 BFD_ASSERT (htab->splt != NULL);
9773 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9774
9775 /* Calculate the address of the .plt entry. */
9776 plt_address = (htab->splt->output_section->vma
9777 + htab->splt->output_offset
9778 + h->plt.offset);
9779
9780 /* Calculate the index of the entry. */
9781 plt_index = ((h->plt.offset - htab->plt_header_size)
9782 / htab->plt_entry_size);
9783
9784 /* Calculate the address of the .got.plt entry. */
9785 got_address = (htab->sgotplt->output_section->vma
9786 + htab->sgotplt->output_offset
9787 + plt_index * 4);
9788
9789 /* Calculate the offset of the .got.plt entry from
9790 _GLOBAL_OFFSET_TABLE_. */
9791 got_offset = mips_elf_gotplt_index (info, h);
9792
9793 /* Calculate the offset for the branch at the start of the PLT
9794 entry. The branch jumps to the beginning of .plt. */
9795 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9796
9797 /* Fill in the initial value of the .got.plt entry. */
9798 bfd_put_32 (output_bfd, plt_address,
9799 htab->sgotplt->contents + plt_index * 4);
9800
9801 /* Find out where the .plt entry should go. */
9802 loc = htab->splt->contents + h->plt.offset;
9803
9804 if (info->shared)
9805 {
9806 plt_entry = mips_vxworks_shared_plt_entry;
9807 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9808 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9809 }
9810 else
9811 {
9812 bfd_vma got_address_high, got_address_low;
9813
9814 plt_entry = mips_vxworks_exec_plt_entry;
9815 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9816 got_address_low = got_address & 0xffff;
9817
9818 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9819 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9820 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9821 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9822 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9823 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9824 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9825 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9826
9827 loc = (htab->srelplt2->contents
9828 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9829
9830 /* Emit a relocation for the .got.plt entry. */
9831 rel.r_offset = got_address;
9832 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9833 rel.r_addend = h->plt.offset;
9834 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9835
9836 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9837 loc += sizeof (Elf32_External_Rela);
9838 rel.r_offset = plt_address + 8;
9839 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9840 rel.r_addend = got_offset;
9841 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9842
9843 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9844 loc += sizeof (Elf32_External_Rela);
9845 rel.r_offset += 4;
9846 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9847 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9848 }
9849
9850 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9851 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9852 rel.r_offset = got_address;
9853 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9854 rel.r_addend = 0;
9855 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9856
9857 if (!h->def_regular)
9858 sym->st_shndx = SHN_UNDEF;
9859 }
9860
9861 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9862
9863 sgot = htab->sgot;
9864 g = htab->got_info;
9865 BFD_ASSERT (g != NULL);
9866
9867 /* See if this symbol has an entry in the GOT. */
9868 if (g->global_gotsym != NULL
9869 && h->dynindx >= g->global_gotsym->dynindx)
9870 {
9871 bfd_vma offset;
9872 Elf_Internal_Rela outrel;
9873 bfd_byte *loc;
9874 asection *s;
9875
9876 /* Install the symbol value in the GOT. */
9877 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9878 R_MIPS_GOT16, info);
9879 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9880
9881 /* Add a dynamic relocation for it. */
9882 s = mips_elf_rel_dyn_section (info, FALSE);
9883 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9884 outrel.r_offset = (sgot->output_section->vma
9885 + sgot->output_offset
9886 + offset);
9887 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9888 outrel.r_addend = 0;
9889 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9890 }
9891
9892 /* Emit a copy reloc, if needed. */
9893 if (h->needs_copy)
9894 {
9895 Elf_Internal_Rela rel;
9896
9897 BFD_ASSERT (h->dynindx != -1);
9898
9899 rel.r_offset = (h->root.u.def.section->output_section->vma
9900 + h->root.u.def.section->output_offset
9901 + h->root.u.def.value);
9902 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9903 rel.r_addend = 0;
9904 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9905 htab->srelbss->contents
9906 + (htab->srelbss->reloc_count
9907 * sizeof (Elf32_External_Rela)));
9908 ++htab->srelbss->reloc_count;
9909 }
9910
9911 /* If this is a mips16 symbol, force the value to be even. */
9912 if (ELF_ST_IS_MIPS16 (sym->st_other))
9913 sym->st_value &= ~1;
9914
9915 return TRUE;
9916 }
9917
9918 /* Write out a plt0 entry to the beginning of .plt. */
9919
9920 static void
9921 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9922 {
9923 bfd_byte *loc;
9924 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9925 static const bfd_vma *plt_entry;
9926 struct mips_elf_link_hash_table *htab;
9927
9928 htab = mips_elf_hash_table (info);
9929 BFD_ASSERT (htab != NULL);
9930
9931 if (ABI_64_P (output_bfd))
9932 plt_entry = mips_n64_exec_plt0_entry;
9933 else if (ABI_N32_P (output_bfd))
9934 plt_entry = mips_n32_exec_plt0_entry;
9935 else
9936 plt_entry = mips_o32_exec_plt0_entry;
9937
9938 /* Calculate the value of .got.plt. */
9939 gotplt_value = (htab->sgotplt->output_section->vma
9940 + htab->sgotplt->output_offset);
9941 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9942 gotplt_value_low = gotplt_value & 0xffff;
9943
9944 /* The PLT sequence is not safe for N64 if .got.plt's address can
9945 not be loaded in two instructions. */
9946 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9947 || ~(gotplt_value | 0x7fffffff) == 0);
9948
9949 /* Install the PLT header. */
9950 loc = htab->splt->contents;
9951 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9952 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9953 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9954 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9955 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9956 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9957 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9958 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9959 }
9960
9961 /* Install the PLT header for a VxWorks executable and finalize the
9962 contents of .rela.plt.unloaded. */
9963
9964 static void
9965 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9966 {
9967 Elf_Internal_Rela rela;
9968 bfd_byte *loc;
9969 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9970 static const bfd_vma *plt_entry;
9971 struct mips_elf_link_hash_table *htab;
9972
9973 htab = mips_elf_hash_table (info);
9974 BFD_ASSERT (htab != NULL);
9975
9976 plt_entry = mips_vxworks_exec_plt0_entry;
9977
9978 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9979 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9980 + htab->root.hgot->root.u.def.section->output_offset
9981 + htab->root.hgot->root.u.def.value);
9982
9983 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9984 got_value_low = got_value & 0xffff;
9985
9986 /* Calculate the address of the PLT header. */
9987 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9988
9989 /* Install the PLT header. */
9990 loc = htab->splt->contents;
9991 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9992 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9993 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9994 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9995 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9996 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9997
9998 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9999 loc = htab->srelplt2->contents;
10000 rela.r_offset = plt_address;
10001 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10002 rela.r_addend = 0;
10003 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10004 loc += sizeof (Elf32_External_Rela);
10005
10006 /* Output the relocation for the following addiu of
10007 %lo(_GLOBAL_OFFSET_TABLE_). */
10008 rela.r_offset += 4;
10009 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10010 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10011 loc += sizeof (Elf32_External_Rela);
10012
10013 /* Fix up the remaining relocations. They may have the wrong
10014 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10015 in which symbols were output. */
10016 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10017 {
10018 Elf_Internal_Rela rel;
10019
10020 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10021 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10022 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10023 loc += sizeof (Elf32_External_Rela);
10024
10025 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10026 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10027 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10028 loc += sizeof (Elf32_External_Rela);
10029
10030 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10031 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10032 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10033 loc += sizeof (Elf32_External_Rela);
10034 }
10035 }
10036
10037 /* Install the PLT header for a VxWorks shared library. */
10038
10039 static void
10040 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10041 {
10042 unsigned int i;
10043 struct mips_elf_link_hash_table *htab;
10044
10045 htab = mips_elf_hash_table (info);
10046 BFD_ASSERT (htab != NULL);
10047
10048 /* We just need to copy the entry byte-by-byte. */
10049 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10050 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10051 htab->splt->contents + i * 4);
10052 }
10053
10054 /* Finish up the dynamic sections. */
10055
10056 bfd_boolean
10057 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10058 struct bfd_link_info *info)
10059 {
10060 bfd *dynobj;
10061 asection *sdyn;
10062 asection *sgot;
10063 struct mips_got_info *gg, *g;
10064 struct mips_elf_link_hash_table *htab;
10065
10066 htab = mips_elf_hash_table (info);
10067 BFD_ASSERT (htab != NULL);
10068
10069 dynobj = elf_hash_table (info)->dynobj;
10070
10071 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10072
10073 sgot = htab->sgot;
10074 gg = htab->got_info;
10075
10076 if (elf_hash_table (info)->dynamic_sections_created)
10077 {
10078 bfd_byte *b;
10079 int dyn_to_skip = 0, dyn_skipped = 0;
10080
10081 BFD_ASSERT (sdyn != NULL);
10082 BFD_ASSERT (gg != NULL);
10083
10084 g = mips_elf_got_for_ibfd (gg, output_bfd);
10085 BFD_ASSERT (g != NULL);
10086
10087 for (b = sdyn->contents;
10088 b < sdyn->contents + sdyn->size;
10089 b += MIPS_ELF_DYN_SIZE (dynobj))
10090 {
10091 Elf_Internal_Dyn dyn;
10092 const char *name;
10093 size_t elemsize;
10094 asection *s;
10095 bfd_boolean swap_out_p;
10096
10097 /* Read in the current dynamic entry. */
10098 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10099
10100 /* Assume that we're going to modify it and write it out. */
10101 swap_out_p = TRUE;
10102
10103 switch (dyn.d_tag)
10104 {
10105 case DT_RELENT:
10106 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10107 break;
10108
10109 case DT_RELAENT:
10110 BFD_ASSERT (htab->is_vxworks);
10111 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10112 break;
10113
10114 case DT_STRSZ:
10115 /* Rewrite DT_STRSZ. */
10116 dyn.d_un.d_val =
10117 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10118 break;
10119
10120 case DT_PLTGOT:
10121 s = htab->sgot;
10122 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10123 break;
10124
10125 case DT_MIPS_PLTGOT:
10126 s = htab->sgotplt;
10127 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10128 break;
10129
10130 case DT_MIPS_RLD_VERSION:
10131 dyn.d_un.d_val = 1; /* XXX */
10132 break;
10133
10134 case DT_MIPS_FLAGS:
10135 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10136 break;
10137
10138 case DT_MIPS_TIME_STAMP:
10139 {
10140 time_t t;
10141 time (&t);
10142 dyn.d_un.d_val = t;
10143 }
10144 break;
10145
10146 case DT_MIPS_ICHECKSUM:
10147 /* XXX FIXME: */
10148 swap_out_p = FALSE;
10149 break;
10150
10151 case DT_MIPS_IVERSION:
10152 /* XXX FIXME: */
10153 swap_out_p = FALSE;
10154 break;
10155
10156 case DT_MIPS_BASE_ADDRESS:
10157 s = output_bfd->sections;
10158 BFD_ASSERT (s != NULL);
10159 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10160 break;
10161
10162 case DT_MIPS_LOCAL_GOTNO:
10163 dyn.d_un.d_val = g->local_gotno;
10164 break;
10165
10166 case DT_MIPS_UNREFEXTNO:
10167 /* The index into the dynamic symbol table which is the
10168 entry of the first external symbol that is not
10169 referenced within the same object. */
10170 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10171 break;
10172
10173 case DT_MIPS_GOTSYM:
10174 if (gg->global_gotsym)
10175 {
10176 dyn.d_un.d_val = gg->global_gotsym->dynindx;
10177 break;
10178 }
10179 /* In case if we don't have global got symbols we default
10180 to setting DT_MIPS_GOTSYM to the same value as
10181 DT_MIPS_SYMTABNO, so we just fall through. */
10182
10183 case DT_MIPS_SYMTABNO:
10184 name = ".dynsym";
10185 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10186 s = bfd_get_section_by_name (output_bfd, name);
10187 BFD_ASSERT (s != NULL);
10188
10189 dyn.d_un.d_val = s->size / elemsize;
10190 break;
10191
10192 case DT_MIPS_HIPAGENO:
10193 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10194 break;
10195
10196 case DT_MIPS_RLD_MAP:
10197 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10198 break;
10199
10200 case DT_MIPS_OPTIONS:
10201 s = (bfd_get_section_by_name
10202 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10203 dyn.d_un.d_ptr = s->vma;
10204 break;
10205
10206 case DT_RELASZ:
10207 BFD_ASSERT (htab->is_vxworks);
10208 /* The count does not include the JUMP_SLOT relocations. */
10209 if (htab->srelplt)
10210 dyn.d_un.d_val -= htab->srelplt->size;
10211 break;
10212
10213 case DT_PLTREL:
10214 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10215 if (htab->is_vxworks)
10216 dyn.d_un.d_val = DT_RELA;
10217 else
10218 dyn.d_un.d_val = DT_REL;
10219 break;
10220
10221 case DT_PLTRELSZ:
10222 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10223 dyn.d_un.d_val = htab->srelplt->size;
10224 break;
10225
10226 case DT_JMPREL:
10227 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10228 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10229 + htab->srelplt->output_offset);
10230 break;
10231
10232 case DT_TEXTREL:
10233 /* If we didn't need any text relocations after all, delete
10234 the dynamic tag. */
10235 if (!(info->flags & DF_TEXTREL))
10236 {
10237 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10238 swap_out_p = FALSE;
10239 }
10240 break;
10241
10242 case DT_FLAGS:
10243 /* If we didn't need any text relocations after all, clear
10244 DF_TEXTREL from DT_FLAGS. */
10245 if (!(info->flags & DF_TEXTREL))
10246 dyn.d_un.d_val &= ~DF_TEXTREL;
10247 else
10248 swap_out_p = FALSE;
10249 break;
10250
10251 default:
10252 swap_out_p = FALSE;
10253 if (htab->is_vxworks
10254 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10255 swap_out_p = TRUE;
10256 break;
10257 }
10258
10259 if (swap_out_p || dyn_skipped)
10260 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10261 (dynobj, &dyn, b - dyn_skipped);
10262
10263 if (dyn_to_skip)
10264 {
10265 dyn_skipped += dyn_to_skip;
10266 dyn_to_skip = 0;
10267 }
10268 }
10269
10270 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10271 if (dyn_skipped > 0)
10272 memset (b - dyn_skipped, 0, dyn_skipped);
10273 }
10274
10275 if (sgot != NULL && sgot->size > 0
10276 && !bfd_is_abs_section (sgot->output_section))
10277 {
10278 if (htab->is_vxworks)
10279 {
10280 /* The first entry of the global offset table points to the
10281 ".dynamic" section. The second is initialized by the
10282 loader and contains the shared library identifier.
10283 The third is also initialized by the loader and points
10284 to the lazy resolution stub. */
10285 MIPS_ELF_PUT_WORD (output_bfd,
10286 sdyn->output_offset + sdyn->output_section->vma,
10287 sgot->contents);
10288 MIPS_ELF_PUT_WORD (output_bfd, 0,
10289 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10290 MIPS_ELF_PUT_WORD (output_bfd, 0,
10291 sgot->contents
10292 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10293 }
10294 else
10295 {
10296 /* The first entry of the global offset table will be filled at
10297 runtime. The second entry will be used by some runtime loaders.
10298 This isn't the case of IRIX rld. */
10299 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10300 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10301 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10302 }
10303
10304 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10305 = MIPS_ELF_GOT_SIZE (output_bfd);
10306 }
10307
10308 /* Generate dynamic relocations for the non-primary gots. */
10309 if (gg != NULL && gg->next)
10310 {
10311 Elf_Internal_Rela rel[3];
10312 bfd_vma addend = 0;
10313
10314 memset (rel, 0, sizeof (rel));
10315 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10316
10317 for (g = gg->next; g->next != gg; g = g->next)
10318 {
10319 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
10320 + g->next->tls_gotno;
10321
10322 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10323 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10324 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10325 sgot->contents
10326 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10327
10328 if (! info->shared)
10329 continue;
10330
10331 while (got_index < g->assigned_gotno)
10332 {
10333 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10334 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10335 if (!(mips_elf_create_dynamic_relocation
10336 (output_bfd, info, rel, NULL,
10337 bfd_abs_section_ptr,
10338 0, &addend, sgot)))
10339 return FALSE;
10340 BFD_ASSERT (addend == 0);
10341 }
10342 }
10343 }
10344
10345 /* The generation of dynamic relocations for the non-primary gots
10346 adds more dynamic relocations. We cannot count them until
10347 here. */
10348
10349 if (elf_hash_table (info)->dynamic_sections_created)
10350 {
10351 bfd_byte *b;
10352 bfd_boolean swap_out_p;
10353
10354 BFD_ASSERT (sdyn != NULL);
10355
10356 for (b = sdyn->contents;
10357 b < sdyn->contents + sdyn->size;
10358 b += MIPS_ELF_DYN_SIZE (dynobj))
10359 {
10360 Elf_Internal_Dyn dyn;
10361 asection *s;
10362
10363 /* Read in the current dynamic entry. */
10364 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10365
10366 /* Assume that we're going to modify it and write it out. */
10367 swap_out_p = TRUE;
10368
10369 switch (dyn.d_tag)
10370 {
10371 case DT_RELSZ:
10372 /* Reduce DT_RELSZ to account for any relocations we
10373 decided not to make. This is for the n64 irix rld,
10374 which doesn't seem to apply any relocations if there
10375 are trailing null entries. */
10376 s = mips_elf_rel_dyn_section (info, FALSE);
10377 dyn.d_un.d_val = (s->reloc_count
10378 * (ABI_64_P (output_bfd)
10379 ? sizeof (Elf64_Mips_External_Rel)
10380 : sizeof (Elf32_External_Rel)));
10381 /* Adjust the section size too. Tools like the prelinker
10382 can reasonably expect the values to the same. */
10383 elf_section_data (s->output_section)->this_hdr.sh_size
10384 = dyn.d_un.d_val;
10385 break;
10386
10387 default:
10388 swap_out_p = FALSE;
10389 break;
10390 }
10391
10392 if (swap_out_p)
10393 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10394 (dynobj, &dyn, b);
10395 }
10396 }
10397
10398 {
10399 asection *s;
10400 Elf32_compact_rel cpt;
10401
10402 if (SGI_COMPAT (output_bfd))
10403 {
10404 /* Write .compact_rel section out. */
10405 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10406 if (s != NULL)
10407 {
10408 cpt.id1 = 1;
10409 cpt.num = s->reloc_count;
10410 cpt.id2 = 2;
10411 cpt.offset = (s->output_section->filepos
10412 + sizeof (Elf32_External_compact_rel));
10413 cpt.reserved0 = 0;
10414 cpt.reserved1 = 0;
10415 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10416 ((Elf32_External_compact_rel *)
10417 s->contents));
10418
10419 /* Clean up a dummy stub function entry in .text. */
10420 if (htab->sstubs != NULL)
10421 {
10422 file_ptr dummy_offset;
10423
10424 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10425 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10426 memset (htab->sstubs->contents + dummy_offset, 0,
10427 htab->function_stub_size);
10428 }
10429 }
10430 }
10431
10432 /* The psABI says that the dynamic relocations must be sorted in
10433 increasing order of r_symndx. The VxWorks EABI doesn't require
10434 this, and because the code below handles REL rather than RELA
10435 relocations, using it for VxWorks would be outright harmful. */
10436 if (!htab->is_vxworks)
10437 {
10438 s = mips_elf_rel_dyn_section (info, FALSE);
10439 if (s != NULL
10440 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10441 {
10442 reldyn_sorting_bfd = output_bfd;
10443
10444 if (ABI_64_P (output_bfd))
10445 qsort ((Elf64_External_Rel *) s->contents + 1,
10446 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10447 sort_dynamic_relocs_64);
10448 else
10449 qsort ((Elf32_External_Rel *) s->contents + 1,
10450 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10451 sort_dynamic_relocs);
10452 }
10453 }
10454 }
10455
10456 if (htab->splt && htab->splt->size > 0)
10457 {
10458 if (htab->is_vxworks)
10459 {
10460 if (info->shared)
10461 mips_vxworks_finish_shared_plt (output_bfd, info);
10462 else
10463 mips_vxworks_finish_exec_plt (output_bfd, info);
10464 }
10465 else
10466 {
10467 BFD_ASSERT (!info->shared);
10468 mips_finish_exec_plt (output_bfd, info);
10469 }
10470 }
10471 return TRUE;
10472 }
10473
10474
10475 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10476
10477 static void
10478 mips_set_isa_flags (bfd *abfd)
10479 {
10480 flagword val;
10481
10482 switch (bfd_get_mach (abfd))
10483 {
10484 default:
10485 case bfd_mach_mips3000:
10486 val = E_MIPS_ARCH_1;
10487 break;
10488
10489 case bfd_mach_mips3900:
10490 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10491 break;
10492
10493 case bfd_mach_mips6000:
10494 val = E_MIPS_ARCH_2;
10495 break;
10496
10497 case bfd_mach_mips4000:
10498 case bfd_mach_mips4300:
10499 case bfd_mach_mips4400:
10500 case bfd_mach_mips4600:
10501 val = E_MIPS_ARCH_3;
10502 break;
10503
10504 case bfd_mach_mips4010:
10505 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10506 break;
10507
10508 case bfd_mach_mips4100:
10509 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10510 break;
10511
10512 case bfd_mach_mips4111:
10513 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10514 break;
10515
10516 case bfd_mach_mips4120:
10517 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10518 break;
10519
10520 case bfd_mach_mips4650:
10521 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10522 break;
10523
10524 case bfd_mach_mips5400:
10525 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10526 break;
10527
10528 case bfd_mach_mips5500:
10529 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10530 break;
10531
10532 case bfd_mach_mips9000:
10533 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10534 break;
10535
10536 case bfd_mach_mips5000:
10537 case bfd_mach_mips7000:
10538 case bfd_mach_mips8000:
10539 case bfd_mach_mips10000:
10540 case bfd_mach_mips12000:
10541 case bfd_mach_mips14000:
10542 case bfd_mach_mips16000:
10543 val = E_MIPS_ARCH_4;
10544 break;
10545
10546 case bfd_mach_mips5:
10547 val = E_MIPS_ARCH_5;
10548 break;
10549
10550 case bfd_mach_mips_loongson_2e:
10551 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10552 break;
10553
10554 case bfd_mach_mips_loongson_2f:
10555 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10556 break;
10557
10558 case bfd_mach_mips_sb1:
10559 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10560 break;
10561
10562 case bfd_mach_mips_octeon:
10563 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10564 break;
10565
10566 case bfd_mach_mips_xlr:
10567 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10568 break;
10569
10570 case bfd_mach_mipsisa32:
10571 val = E_MIPS_ARCH_32;
10572 break;
10573
10574 case bfd_mach_mipsisa64:
10575 val = E_MIPS_ARCH_64;
10576 break;
10577
10578 case bfd_mach_mipsisa32r2:
10579 val = E_MIPS_ARCH_32R2;
10580 break;
10581
10582 case bfd_mach_mipsisa64r2:
10583 val = E_MIPS_ARCH_64R2;
10584 break;
10585 }
10586 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10587 elf_elfheader (abfd)->e_flags |= val;
10588
10589 }
10590
10591
10592 /* The final processing done just before writing out a MIPS ELF object
10593 file. This gets the MIPS architecture right based on the machine
10594 number. This is used by both the 32-bit and the 64-bit ABI. */
10595
10596 void
10597 _bfd_mips_elf_final_write_processing (bfd *abfd,
10598 bfd_boolean linker ATTRIBUTE_UNUSED)
10599 {
10600 unsigned int i;
10601 Elf_Internal_Shdr **hdrpp;
10602 const char *name;
10603 asection *sec;
10604
10605 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10606 is nonzero. This is for compatibility with old objects, which used
10607 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10608 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10609 mips_set_isa_flags (abfd);
10610
10611 /* Set the sh_info field for .gptab sections and other appropriate
10612 info for each special section. */
10613 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10614 i < elf_numsections (abfd);
10615 i++, hdrpp++)
10616 {
10617 switch ((*hdrpp)->sh_type)
10618 {
10619 case SHT_MIPS_MSYM:
10620 case SHT_MIPS_LIBLIST:
10621 sec = bfd_get_section_by_name (abfd, ".dynstr");
10622 if (sec != NULL)
10623 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10624 break;
10625
10626 case SHT_MIPS_GPTAB:
10627 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10628 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10629 BFD_ASSERT (name != NULL
10630 && CONST_STRNEQ (name, ".gptab."));
10631 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10632 BFD_ASSERT (sec != NULL);
10633 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10634 break;
10635
10636 case SHT_MIPS_CONTENT:
10637 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10638 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10639 BFD_ASSERT (name != NULL
10640 && CONST_STRNEQ (name, ".MIPS.content"));
10641 sec = bfd_get_section_by_name (abfd,
10642 name + sizeof ".MIPS.content" - 1);
10643 BFD_ASSERT (sec != NULL);
10644 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10645 break;
10646
10647 case SHT_MIPS_SYMBOL_LIB:
10648 sec = bfd_get_section_by_name (abfd, ".dynsym");
10649 if (sec != NULL)
10650 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10651 sec = bfd_get_section_by_name (abfd, ".liblist");
10652 if (sec != NULL)
10653 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10654 break;
10655
10656 case SHT_MIPS_EVENTS:
10657 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10658 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10659 BFD_ASSERT (name != NULL);
10660 if (CONST_STRNEQ (name, ".MIPS.events"))
10661 sec = bfd_get_section_by_name (abfd,
10662 name + sizeof ".MIPS.events" - 1);
10663 else
10664 {
10665 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
10666 sec = bfd_get_section_by_name (abfd,
10667 (name
10668 + sizeof ".MIPS.post_rel" - 1));
10669 }
10670 BFD_ASSERT (sec != NULL);
10671 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10672 break;
10673
10674 }
10675 }
10676 }
10677 \f
10678 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
10679 segments. */
10680
10681 int
10682 _bfd_mips_elf_additional_program_headers (bfd *abfd,
10683 struct bfd_link_info *info ATTRIBUTE_UNUSED)
10684 {
10685 asection *s;
10686 int ret = 0;
10687
10688 /* See if we need a PT_MIPS_REGINFO segment. */
10689 s = bfd_get_section_by_name (abfd, ".reginfo");
10690 if (s && (s->flags & SEC_LOAD))
10691 ++ret;
10692
10693 /* See if we need a PT_MIPS_OPTIONS segment. */
10694 if (IRIX_COMPAT (abfd) == ict_irix6
10695 && bfd_get_section_by_name (abfd,
10696 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10697 ++ret;
10698
10699 /* See if we need a PT_MIPS_RTPROC segment. */
10700 if (IRIX_COMPAT (abfd) == ict_irix5
10701 && bfd_get_section_by_name (abfd, ".dynamic")
10702 && bfd_get_section_by_name (abfd, ".mdebug"))
10703 ++ret;
10704
10705 /* Allocate a PT_NULL header in dynamic objects. See
10706 _bfd_mips_elf_modify_segment_map for details. */
10707 if (!SGI_COMPAT (abfd)
10708 && bfd_get_section_by_name (abfd, ".dynamic"))
10709 ++ret;
10710
10711 return ret;
10712 }
10713
10714 /* Modify the segment map for an IRIX5 executable. */
10715
10716 bfd_boolean
10717 _bfd_mips_elf_modify_segment_map (bfd *abfd,
10718 struct bfd_link_info *info)
10719 {
10720 asection *s;
10721 struct elf_segment_map *m, **pm;
10722 bfd_size_type amt;
10723
10724 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10725 segment. */
10726 s = bfd_get_section_by_name (abfd, ".reginfo");
10727 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10728 {
10729 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10730 if (m->p_type == PT_MIPS_REGINFO)
10731 break;
10732 if (m == NULL)
10733 {
10734 amt = sizeof *m;
10735 m = bfd_zalloc (abfd, amt);
10736 if (m == NULL)
10737 return FALSE;
10738
10739 m->p_type = PT_MIPS_REGINFO;
10740 m->count = 1;
10741 m->sections[0] = s;
10742
10743 /* We want to put it after the PHDR and INTERP segments. */
10744 pm = &elf_tdata (abfd)->segment_map;
10745 while (*pm != NULL
10746 && ((*pm)->p_type == PT_PHDR
10747 || (*pm)->p_type == PT_INTERP))
10748 pm = &(*pm)->next;
10749
10750 m->next = *pm;
10751 *pm = m;
10752 }
10753 }
10754
10755 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10756 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
10757 PT_MIPS_OPTIONS segment immediately following the program header
10758 table. */
10759 if (NEWABI_P (abfd)
10760 /* On non-IRIX6 new abi, we'll have already created a segment
10761 for this section, so don't create another. I'm not sure this
10762 is not also the case for IRIX 6, but I can't test it right
10763 now. */
10764 && IRIX_COMPAT (abfd) == ict_irix6)
10765 {
10766 for (s = abfd->sections; s; s = s->next)
10767 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10768 break;
10769
10770 if (s)
10771 {
10772 struct elf_segment_map *options_segment;
10773
10774 pm = &elf_tdata (abfd)->segment_map;
10775 while (*pm != NULL
10776 && ((*pm)->p_type == PT_PHDR
10777 || (*pm)->p_type == PT_INTERP))
10778 pm = &(*pm)->next;
10779
10780 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10781 {
10782 amt = sizeof (struct elf_segment_map);
10783 options_segment = bfd_zalloc (abfd, amt);
10784 options_segment->next = *pm;
10785 options_segment->p_type = PT_MIPS_OPTIONS;
10786 options_segment->p_flags = PF_R;
10787 options_segment->p_flags_valid = TRUE;
10788 options_segment->count = 1;
10789 options_segment->sections[0] = s;
10790 *pm = options_segment;
10791 }
10792 }
10793 }
10794 else
10795 {
10796 if (IRIX_COMPAT (abfd) == ict_irix5)
10797 {
10798 /* If there are .dynamic and .mdebug sections, we make a room
10799 for the RTPROC header. FIXME: Rewrite without section names. */
10800 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10801 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10802 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10803 {
10804 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10805 if (m->p_type == PT_MIPS_RTPROC)
10806 break;
10807 if (m == NULL)
10808 {
10809 amt = sizeof *m;
10810 m = bfd_zalloc (abfd, amt);
10811 if (m == NULL)
10812 return FALSE;
10813
10814 m->p_type = PT_MIPS_RTPROC;
10815
10816 s = bfd_get_section_by_name (abfd, ".rtproc");
10817 if (s == NULL)
10818 {
10819 m->count = 0;
10820 m->p_flags = 0;
10821 m->p_flags_valid = 1;
10822 }
10823 else
10824 {
10825 m->count = 1;
10826 m->sections[0] = s;
10827 }
10828
10829 /* We want to put it after the DYNAMIC segment. */
10830 pm = &elf_tdata (abfd)->segment_map;
10831 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10832 pm = &(*pm)->next;
10833 if (*pm != NULL)
10834 pm = &(*pm)->next;
10835
10836 m->next = *pm;
10837 *pm = m;
10838 }
10839 }
10840 }
10841 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10842 .dynstr, .dynsym, and .hash sections, and everything in
10843 between. */
10844 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10845 pm = &(*pm)->next)
10846 if ((*pm)->p_type == PT_DYNAMIC)
10847 break;
10848 m = *pm;
10849 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10850 {
10851 /* For a normal mips executable the permissions for the PT_DYNAMIC
10852 segment are read, write and execute. We do that here since
10853 the code in elf.c sets only the read permission. This matters
10854 sometimes for the dynamic linker. */
10855 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10856 {
10857 m->p_flags = PF_R | PF_W | PF_X;
10858 m->p_flags_valid = 1;
10859 }
10860 }
10861 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10862 glibc's dynamic linker has traditionally derived the number of
10863 tags from the p_filesz field, and sometimes allocates stack
10864 arrays of that size. An overly-big PT_DYNAMIC segment can
10865 be actively harmful in such cases. Making PT_DYNAMIC contain
10866 other sections can also make life hard for the prelinker,
10867 which might move one of the other sections to a different
10868 PT_LOAD segment. */
10869 if (SGI_COMPAT (abfd)
10870 && m != NULL
10871 && m->count == 1
10872 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10873 {
10874 static const char *sec_names[] =
10875 {
10876 ".dynamic", ".dynstr", ".dynsym", ".hash"
10877 };
10878 bfd_vma low, high;
10879 unsigned int i, c;
10880 struct elf_segment_map *n;
10881
10882 low = ~(bfd_vma) 0;
10883 high = 0;
10884 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10885 {
10886 s = bfd_get_section_by_name (abfd, sec_names[i]);
10887 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10888 {
10889 bfd_size_type sz;
10890
10891 if (low > s->vma)
10892 low = s->vma;
10893 sz = s->size;
10894 if (high < s->vma + sz)
10895 high = s->vma + sz;
10896 }
10897 }
10898
10899 c = 0;
10900 for (s = abfd->sections; s != NULL; s = s->next)
10901 if ((s->flags & SEC_LOAD) != 0
10902 && s->vma >= low
10903 && s->vma + s->size <= high)
10904 ++c;
10905
10906 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10907 n = bfd_zalloc (abfd, amt);
10908 if (n == NULL)
10909 return FALSE;
10910 *n = *m;
10911 n->count = c;
10912
10913 i = 0;
10914 for (s = abfd->sections; s != NULL; s = s->next)
10915 {
10916 if ((s->flags & SEC_LOAD) != 0
10917 && s->vma >= low
10918 && s->vma + s->size <= high)
10919 {
10920 n->sections[i] = s;
10921 ++i;
10922 }
10923 }
10924
10925 *pm = n;
10926 }
10927 }
10928
10929 /* Allocate a spare program header in dynamic objects so that tools
10930 like the prelinker can add an extra PT_LOAD entry.
10931
10932 If the prelinker needs to make room for a new PT_LOAD entry, its
10933 standard procedure is to move the first (read-only) sections into
10934 the new (writable) segment. However, the MIPS ABI requires
10935 .dynamic to be in a read-only segment, and the section will often
10936 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10937
10938 Although the prelinker could in principle move .dynamic to a
10939 writable segment, it seems better to allocate a spare program
10940 header instead, and avoid the need to move any sections.
10941 There is a long tradition of allocating spare dynamic tags,
10942 so allocating a spare program header seems like a natural
10943 extension.
10944
10945 If INFO is NULL, we may be copying an already prelinked binary
10946 with objcopy or strip, so do not add this header. */
10947 if (info != NULL
10948 && !SGI_COMPAT (abfd)
10949 && bfd_get_section_by_name (abfd, ".dynamic"))
10950 {
10951 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10952 if ((*pm)->p_type == PT_NULL)
10953 break;
10954 if (*pm == NULL)
10955 {
10956 m = bfd_zalloc (abfd, sizeof (*m));
10957 if (m == NULL)
10958 return FALSE;
10959
10960 m->p_type = PT_NULL;
10961 *pm = m;
10962 }
10963 }
10964
10965 return TRUE;
10966 }
10967 \f
10968 /* Return the section that should be marked against GC for a given
10969 relocation. */
10970
10971 asection *
10972 _bfd_mips_elf_gc_mark_hook (asection *sec,
10973 struct bfd_link_info *info,
10974 Elf_Internal_Rela *rel,
10975 struct elf_link_hash_entry *h,
10976 Elf_Internal_Sym *sym)
10977 {
10978 /* ??? Do mips16 stub sections need to be handled special? */
10979
10980 if (h != NULL)
10981 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10982 {
10983 case R_MIPS_GNU_VTINHERIT:
10984 case R_MIPS_GNU_VTENTRY:
10985 return NULL;
10986 }
10987
10988 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10989 }
10990
10991 /* Update the got entry reference counts for the section being removed. */
10992
10993 bfd_boolean
10994 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10995 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10996 asection *sec ATTRIBUTE_UNUSED,
10997 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10998 {
10999 #if 0
11000 Elf_Internal_Shdr *symtab_hdr;
11001 struct elf_link_hash_entry **sym_hashes;
11002 bfd_signed_vma *local_got_refcounts;
11003 const Elf_Internal_Rela *rel, *relend;
11004 unsigned long r_symndx;
11005 struct elf_link_hash_entry *h;
11006
11007 if (info->relocatable)
11008 return TRUE;
11009
11010 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11011 sym_hashes = elf_sym_hashes (abfd);
11012 local_got_refcounts = elf_local_got_refcounts (abfd);
11013
11014 relend = relocs + sec->reloc_count;
11015 for (rel = relocs; rel < relend; rel++)
11016 switch (ELF_R_TYPE (abfd, rel->r_info))
11017 {
11018 case R_MIPS16_GOT16:
11019 case R_MIPS16_CALL16:
11020 case R_MIPS_GOT16:
11021 case R_MIPS_CALL16:
11022 case R_MIPS_CALL_HI16:
11023 case R_MIPS_CALL_LO16:
11024 case R_MIPS_GOT_HI16:
11025 case R_MIPS_GOT_LO16:
11026 case R_MIPS_GOT_DISP:
11027 case R_MIPS_GOT_PAGE:
11028 case R_MIPS_GOT_OFST:
11029 /* ??? It would seem that the existing MIPS code does no sort
11030 of reference counting or whatnot on its GOT and PLT entries,
11031 so it is not possible to garbage collect them at this time. */
11032 break;
11033
11034 default:
11035 break;
11036 }
11037 #endif
11038
11039 return TRUE;
11040 }
11041 \f
11042 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11043 hiding the old indirect symbol. Process additional relocation
11044 information. Also called for weakdefs, in which case we just let
11045 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11046
11047 void
11048 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
11049 struct elf_link_hash_entry *dir,
11050 struct elf_link_hash_entry *ind)
11051 {
11052 struct mips_elf_link_hash_entry *dirmips, *indmips;
11053
11054 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
11055
11056 dirmips = (struct mips_elf_link_hash_entry *) dir;
11057 indmips = (struct mips_elf_link_hash_entry *) ind;
11058 /* Any absolute non-dynamic relocations against an indirect or weak
11059 definition will be against the target symbol. */
11060 if (indmips->has_static_relocs)
11061 dirmips->has_static_relocs = TRUE;
11062
11063 if (ind->root.type != bfd_link_hash_indirect)
11064 return;
11065
11066 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11067 if (indmips->readonly_reloc)
11068 dirmips->readonly_reloc = TRUE;
11069 if (indmips->no_fn_stub)
11070 dirmips->no_fn_stub = TRUE;
11071 if (indmips->fn_stub)
11072 {
11073 dirmips->fn_stub = indmips->fn_stub;
11074 indmips->fn_stub = NULL;
11075 }
11076 if (indmips->need_fn_stub)
11077 {
11078 dirmips->need_fn_stub = TRUE;
11079 indmips->need_fn_stub = FALSE;
11080 }
11081 if (indmips->call_stub)
11082 {
11083 dirmips->call_stub = indmips->call_stub;
11084 indmips->call_stub = NULL;
11085 }
11086 if (indmips->call_fp_stub)
11087 {
11088 dirmips->call_fp_stub = indmips->call_fp_stub;
11089 indmips->call_fp_stub = NULL;
11090 }
11091 if (indmips->global_got_area < dirmips->global_got_area)
11092 dirmips->global_got_area = indmips->global_got_area;
11093 if (indmips->global_got_area < GGA_NONE)
11094 indmips->global_got_area = GGA_NONE;
11095 if (indmips->has_nonpic_branches)
11096 dirmips->has_nonpic_branches = TRUE;
11097
11098 if (dirmips->tls_type == 0)
11099 dirmips->tls_type = indmips->tls_type;
11100 }
11101 \f
11102 #define PDR_SIZE 32
11103
11104 bfd_boolean
11105 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11106 struct bfd_link_info *info)
11107 {
11108 asection *o;
11109 bfd_boolean ret = FALSE;
11110 unsigned char *tdata;
11111 size_t i, skip;
11112
11113 o = bfd_get_section_by_name (abfd, ".pdr");
11114 if (! o)
11115 return FALSE;
11116 if (o->size == 0)
11117 return FALSE;
11118 if (o->size % PDR_SIZE != 0)
11119 return FALSE;
11120 if (o->output_section != NULL
11121 && bfd_is_abs_section (o->output_section))
11122 return FALSE;
11123
11124 tdata = bfd_zmalloc (o->size / PDR_SIZE);
11125 if (! tdata)
11126 return FALSE;
11127
11128 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11129 info->keep_memory);
11130 if (!cookie->rels)
11131 {
11132 free (tdata);
11133 return FALSE;
11134 }
11135
11136 cookie->rel = cookie->rels;
11137 cookie->relend = cookie->rels + o->reloc_count;
11138
11139 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11140 {
11141 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11142 {
11143 tdata[i] = 1;
11144 skip ++;
11145 }
11146 }
11147
11148 if (skip != 0)
11149 {
11150 mips_elf_section_data (o)->u.tdata = tdata;
11151 o->size -= skip * PDR_SIZE;
11152 ret = TRUE;
11153 }
11154 else
11155 free (tdata);
11156
11157 if (! info->keep_memory)
11158 free (cookie->rels);
11159
11160 return ret;
11161 }
11162
11163 bfd_boolean
11164 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11165 {
11166 if (strcmp (sec->name, ".pdr") == 0)
11167 return TRUE;
11168 return FALSE;
11169 }
11170
11171 bfd_boolean
11172 _bfd_mips_elf_write_section (bfd *output_bfd,
11173 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11174 asection *sec, bfd_byte *contents)
11175 {
11176 bfd_byte *to, *from, *end;
11177 int i;
11178
11179 if (strcmp (sec->name, ".pdr") != 0)
11180 return FALSE;
11181
11182 if (mips_elf_section_data (sec)->u.tdata == NULL)
11183 return FALSE;
11184
11185 to = contents;
11186 end = contents + sec->size;
11187 for (from = contents, i = 0;
11188 from < end;
11189 from += PDR_SIZE, i++)
11190 {
11191 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11192 continue;
11193 if (to != from)
11194 memcpy (to, from, PDR_SIZE);
11195 to += PDR_SIZE;
11196 }
11197 bfd_set_section_contents (output_bfd, sec->output_section, contents,
11198 sec->output_offset, sec->size);
11199 return TRUE;
11200 }
11201 \f
11202 /* MIPS ELF uses a special find_nearest_line routine in order the
11203 handle the ECOFF debugging information. */
11204
11205 struct mips_elf_find_line
11206 {
11207 struct ecoff_debug_info d;
11208 struct ecoff_find_line i;
11209 };
11210
11211 bfd_boolean
11212 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11213 asymbol **symbols, bfd_vma offset,
11214 const char **filename_ptr,
11215 const char **functionname_ptr,
11216 unsigned int *line_ptr)
11217 {
11218 asection *msec;
11219
11220 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11221 filename_ptr, functionname_ptr,
11222 line_ptr))
11223 return TRUE;
11224
11225 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11226 filename_ptr, functionname_ptr,
11227 line_ptr, ABI_64_P (abfd) ? 8 : 0,
11228 &elf_tdata (abfd)->dwarf2_find_line_info))
11229 return TRUE;
11230
11231 msec = bfd_get_section_by_name (abfd, ".mdebug");
11232 if (msec != NULL)
11233 {
11234 flagword origflags;
11235 struct mips_elf_find_line *fi;
11236 const struct ecoff_debug_swap * const swap =
11237 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11238
11239 /* If we are called during a link, mips_elf_final_link may have
11240 cleared the SEC_HAS_CONTENTS field. We force it back on here
11241 if appropriate (which it normally will be). */
11242 origflags = msec->flags;
11243 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11244 msec->flags |= SEC_HAS_CONTENTS;
11245
11246 fi = elf_tdata (abfd)->find_line_info;
11247 if (fi == NULL)
11248 {
11249 bfd_size_type external_fdr_size;
11250 char *fraw_src;
11251 char *fraw_end;
11252 struct fdr *fdr_ptr;
11253 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11254
11255 fi = bfd_zalloc (abfd, amt);
11256 if (fi == NULL)
11257 {
11258 msec->flags = origflags;
11259 return FALSE;
11260 }
11261
11262 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11263 {
11264 msec->flags = origflags;
11265 return FALSE;
11266 }
11267
11268 /* Swap in the FDR information. */
11269 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11270 fi->d.fdr = bfd_alloc (abfd, amt);
11271 if (fi->d.fdr == NULL)
11272 {
11273 msec->flags = origflags;
11274 return FALSE;
11275 }
11276 external_fdr_size = swap->external_fdr_size;
11277 fdr_ptr = fi->d.fdr;
11278 fraw_src = (char *) fi->d.external_fdr;
11279 fraw_end = (fraw_src
11280 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11281 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11282 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11283
11284 elf_tdata (abfd)->find_line_info = fi;
11285
11286 /* Note that we don't bother to ever free this information.
11287 find_nearest_line is either called all the time, as in
11288 objdump -l, so the information should be saved, or it is
11289 rarely called, as in ld error messages, so the memory
11290 wasted is unimportant. Still, it would probably be a
11291 good idea for free_cached_info to throw it away. */
11292 }
11293
11294 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11295 &fi->i, filename_ptr, functionname_ptr,
11296 line_ptr))
11297 {
11298 msec->flags = origflags;
11299 return TRUE;
11300 }
11301
11302 msec->flags = origflags;
11303 }
11304
11305 /* Fall back on the generic ELF find_nearest_line routine. */
11306
11307 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11308 filename_ptr, functionname_ptr,
11309 line_ptr);
11310 }
11311
11312 bfd_boolean
11313 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11314 const char **filename_ptr,
11315 const char **functionname_ptr,
11316 unsigned int *line_ptr)
11317 {
11318 bfd_boolean found;
11319 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11320 functionname_ptr, line_ptr,
11321 & elf_tdata (abfd)->dwarf2_find_line_info);
11322 return found;
11323 }
11324
11325 \f
11326 /* When are writing out the .options or .MIPS.options section,
11327 remember the bytes we are writing out, so that we can install the
11328 GP value in the section_processing routine. */
11329
11330 bfd_boolean
11331 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11332 const void *location,
11333 file_ptr offset, bfd_size_type count)
11334 {
11335 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11336 {
11337 bfd_byte *c;
11338
11339 if (elf_section_data (section) == NULL)
11340 {
11341 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11342 section->used_by_bfd = bfd_zalloc (abfd, amt);
11343 if (elf_section_data (section) == NULL)
11344 return FALSE;
11345 }
11346 c = mips_elf_section_data (section)->u.tdata;
11347 if (c == NULL)
11348 {
11349 c = bfd_zalloc (abfd, section->size);
11350 if (c == NULL)
11351 return FALSE;
11352 mips_elf_section_data (section)->u.tdata = c;
11353 }
11354
11355 memcpy (c + offset, location, count);
11356 }
11357
11358 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11359 count);
11360 }
11361
11362 /* This is almost identical to bfd_generic_get_... except that some
11363 MIPS relocations need to be handled specially. Sigh. */
11364
11365 bfd_byte *
11366 _bfd_elf_mips_get_relocated_section_contents
11367 (bfd *abfd,
11368 struct bfd_link_info *link_info,
11369 struct bfd_link_order *link_order,
11370 bfd_byte *data,
11371 bfd_boolean relocatable,
11372 asymbol **symbols)
11373 {
11374 /* Get enough memory to hold the stuff */
11375 bfd *input_bfd = link_order->u.indirect.section->owner;
11376 asection *input_section = link_order->u.indirect.section;
11377 bfd_size_type sz;
11378
11379 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11380 arelent **reloc_vector = NULL;
11381 long reloc_count;
11382
11383 if (reloc_size < 0)
11384 goto error_return;
11385
11386 reloc_vector = bfd_malloc (reloc_size);
11387 if (reloc_vector == NULL && reloc_size != 0)
11388 goto error_return;
11389
11390 /* read in the section */
11391 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11392 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11393 goto error_return;
11394
11395 reloc_count = bfd_canonicalize_reloc (input_bfd,
11396 input_section,
11397 reloc_vector,
11398 symbols);
11399 if (reloc_count < 0)
11400 goto error_return;
11401
11402 if (reloc_count > 0)
11403 {
11404 arelent **parent;
11405 /* for mips */
11406 int gp_found;
11407 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11408
11409 {
11410 struct bfd_hash_entry *h;
11411 struct bfd_link_hash_entry *lh;
11412 /* Skip all this stuff if we aren't mixing formats. */
11413 if (abfd && input_bfd
11414 && abfd->xvec == input_bfd->xvec)
11415 lh = 0;
11416 else
11417 {
11418 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11419 lh = (struct bfd_link_hash_entry *) h;
11420 }
11421 lookup:
11422 if (lh)
11423 {
11424 switch (lh->type)
11425 {
11426 case bfd_link_hash_undefined:
11427 case bfd_link_hash_undefweak:
11428 case bfd_link_hash_common:
11429 gp_found = 0;
11430 break;
11431 case bfd_link_hash_defined:
11432 case bfd_link_hash_defweak:
11433 gp_found = 1;
11434 gp = lh->u.def.value;
11435 break;
11436 case bfd_link_hash_indirect:
11437 case bfd_link_hash_warning:
11438 lh = lh->u.i.link;
11439 /* @@FIXME ignoring warning for now */
11440 goto lookup;
11441 case bfd_link_hash_new:
11442 default:
11443 abort ();
11444 }
11445 }
11446 else
11447 gp_found = 0;
11448 }
11449 /* end mips */
11450 for (parent = reloc_vector; *parent != NULL; parent++)
11451 {
11452 char *error_message = NULL;
11453 bfd_reloc_status_type r;
11454
11455 /* Specific to MIPS: Deal with relocation types that require
11456 knowing the gp of the output bfd. */
11457 asymbol *sym = *(*parent)->sym_ptr_ptr;
11458
11459 /* If we've managed to find the gp and have a special
11460 function for the relocation then go ahead, else default
11461 to the generic handling. */
11462 if (gp_found
11463 && (*parent)->howto->special_function
11464 == _bfd_mips_elf32_gprel16_reloc)
11465 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11466 input_section, relocatable,
11467 data, gp);
11468 else
11469 r = bfd_perform_relocation (input_bfd, *parent, data,
11470 input_section,
11471 relocatable ? abfd : NULL,
11472 &error_message);
11473
11474 if (relocatable)
11475 {
11476 asection *os = input_section->output_section;
11477
11478 /* A partial link, so keep the relocs */
11479 os->orelocation[os->reloc_count] = *parent;
11480 os->reloc_count++;
11481 }
11482
11483 if (r != bfd_reloc_ok)
11484 {
11485 switch (r)
11486 {
11487 case bfd_reloc_undefined:
11488 if (!((*link_info->callbacks->undefined_symbol)
11489 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11490 input_bfd, input_section, (*parent)->address, TRUE)))
11491 goto error_return;
11492 break;
11493 case bfd_reloc_dangerous:
11494 BFD_ASSERT (error_message != NULL);
11495 if (!((*link_info->callbacks->reloc_dangerous)
11496 (link_info, error_message, input_bfd, input_section,
11497 (*parent)->address)))
11498 goto error_return;
11499 break;
11500 case bfd_reloc_overflow:
11501 if (!((*link_info->callbacks->reloc_overflow)
11502 (link_info, NULL,
11503 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11504 (*parent)->howto->name, (*parent)->addend,
11505 input_bfd, input_section, (*parent)->address)))
11506 goto error_return;
11507 break;
11508 case bfd_reloc_outofrange:
11509 default:
11510 abort ();
11511 break;
11512 }
11513
11514 }
11515 }
11516 }
11517 if (reloc_vector != NULL)
11518 free (reloc_vector);
11519 return data;
11520
11521 error_return:
11522 if (reloc_vector != NULL)
11523 free (reloc_vector);
11524 return NULL;
11525 }
11526 \f
11527 /* Create a MIPS ELF linker hash table. */
11528
11529 struct bfd_link_hash_table *
11530 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
11531 {
11532 struct mips_elf_link_hash_table *ret;
11533 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11534
11535 ret = bfd_malloc (amt);
11536 if (ret == NULL)
11537 return NULL;
11538
11539 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11540 mips_elf_link_hash_newfunc,
11541 sizeof (struct mips_elf_link_hash_entry),
11542 MIPS_ELF_DATA))
11543 {
11544 free (ret);
11545 return NULL;
11546 }
11547
11548 #if 0
11549 /* We no longer use this. */
11550 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11551 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11552 #endif
11553 ret->procedure_count = 0;
11554 ret->compact_rel_size = 0;
11555 ret->use_rld_obj_head = FALSE;
11556 ret->rld_value = 0;
11557 ret->mips16_stubs_seen = FALSE;
11558 ret->use_plts_and_copy_relocs = FALSE;
11559 ret->is_vxworks = FALSE;
11560 ret->small_data_overflow_reported = FALSE;
11561 ret->srelbss = NULL;
11562 ret->sdynbss = NULL;
11563 ret->srelplt = NULL;
11564 ret->srelplt2 = NULL;
11565 ret->sgotplt = NULL;
11566 ret->splt = NULL;
11567 ret->sstubs = NULL;
11568 ret->sgot = NULL;
11569 ret->got_info = NULL;
11570 ret->plt_header_size = 0;
11571 ret->plt_entry_size = 0;
11572 ret->lazy_stub_count = 0;
11573 ret->function_stub_size = 0;
11574 ret->strampoline = NULL;
11575 ret->la25_stubs = NULL;
11576 ret->add_stub_section = NULL;
11577
11578 return &ret->root.root;
11579 }
11580
11581 /* Likewise, but indicate that the target is VxWorks. */
11582
11583 struct bfd_link_hash_table *
11584 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11585 {
11586 struct bfd_link_hash_table *ret;
11587
11588 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11589 if (ret)
11590 {
11591 struct mips_elf_link_hash_table *htab;
11592
11593 htab = (struct mips_elf_link_hash_table *) ret;
11594 htab->use_plts_and_copy_relocs = TRUE;
11595 htab->is_vxworks = TRUE;
11596 }
11597 return ret;
11598 }
11599
11600 /* A function that the linker calls if we are allowed to use PLTs
11601 and copy relocs. */
11602
11603 void
11604 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11605 {
11606 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11607 }
11608 \f
11609 /* We need to use a special link routine to handle the .reginfo and
11610 the .mdebug sections. We need to merge all instances of these
11611 sections together, not write them all out sequentially. */
11612
11613 bfd_boolean
11614 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11615 {
11616 asection *o;
11617 struct bfd_link_order *p;
11618 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11619 asection *rtproc_sec;
11620 Elf32_RegInfo reginfo;
11621 struct ecoff_debug_info debug;
11622 struct mips_htab_traverse_info hti;
11623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11624 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
11625 HDRR *symhdr = &debug.symbolic_header;
11626 void *mdebug_handle = NULL;
11627 asection *s;
11628 EXTR esym;
11629 unsigned int i;
11630 bfd_size_type amt;
11631 struct mips_elf_link_hash_table *htab;
11632
11633 static const char * const secname[] =
11634 {
11635 ".text", ".init", ".fini", ".data",
11636 ".rodata", ".sdata", ".sbss", ".bss"
11637 };
11638 static const int sc[] =
11639 {
11640 scText, scInit, scFini, scData,
11641 scRData, scSData, scSBss, scBss
11642 };
11643
11644 /* Sort the dynamic symbols so that those with GOT entries come after
11645 those without. */
11646 htab = mips_elf_hash_table (info);
11647 BFD_ASSERT (htab != NULL);
11648
11649 if (!mips_elf_sort_hash_table (abfd, info))
11650 return FALSE;
11651
11652 /* Create any scheduled LA25 stubs. */
11653 hti.info = info;
11654 hti.output_bfd = abfd;
11655 hti.error = FALSE;
11656 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11657 if (hti.error)
11658 return FALSE;
11659
11660 /* Get a value for the GP register. */
11661 if (elf_gp (abfd) == 0)
11662 {
11663 struct bfd_link_hash_entry *h;
11664
11665 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
11666 if (h != NULL && h->type == bfd_link_hash_defined)
11667 elf_gp (abfd) = (h->u.def.value
11668 + h->u.def.section->output_section->vma
11669 + h->u.def.section->output_offset);
11670 else if (htab->is_vxworks
11671 && (h = bfd_link_hash_lookup (info->hash,
11672 "_GLOBAL_OFFSET_TABLE_",
11673 FALSE, FALSE, TRUE))
11674 && h->type == bfd_link_hash_defined)
11675 elf_gp (abfd) = (h->u.def.section->output_section->vma
11676 + h->u.def.section->output_offset
11677 + h->u.def.value);
11678 else if (info->relocatable)
11679 {
11680 bfd_vma lo = MINUS_ONE;
11681
11682 /* Find the GP-relative section with the lowest offset. */
11683 for (o = abfd->sections; o != NULL; o = o->next)
11684 if (o->vma < lo
11685 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11686 lo = o->vma;
11687
11688 /* And calculate GP relative to that. */
11689 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
11690 }
11691 else
11692 {
11693 /* If the relocate_section function needs to do a reloc
11694 involving the GP value, it should make a reloc_dangerous
11695 callback to warn that GP is not defined. */
11696 }
11697 }
11698
11699 /* Go through the sections and collect the .reginfo and .mdebug
11700 information. */
11701 reginfo_sec = NULL;
11702 mdebug_sec = NULL;
11703 gptab_data_sec = NULL;
11704 gptab_bss_sec = NULL;
11705 for (o = abfd->sections; o != NULL; o = o->next)
11706 {
11707 if (strcmp (o->name, ".reginfo") == 0)
11708 {
11709 memset (&reginfo, 0, sizeof reginfo);
11710
11711 /* We have found the .reginfo section in the output file.
11712 Look through all the link_orders comprising it and merge
11713 the information together. */
11714 for (p = o->map_head.link_order; p != NULL; p = p->next)
11715 {
11716 asection *input_section;
11717 bfd *input_bfd;
11718 Elf32_External_RegInfo ext;
11719 Elf32_RegInfo sub;
11720
11721 if (p->type != bfd_indirect_link_order)
11722 {
11723 if (p->type == bfd_data_link_order)
11724 continue;
11725 abort ();
11726 }
11727
11728 input_section = p->u.indirect.section;
11729 input_bfd = input_section->owner;
11730
11731 if (! bfd_get_section_contents (input_bfd, input_section,
11732 &ext, 0, sizeof ext))
11733 return FALSE;
11734
11735 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11736
11737 reginfo.ri_gprmask |= sub.ri_gprmask;
11738 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11739 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11740 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11741 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11742
11743 /* ri_gp_value is set by the function
11744 mips_elf32_section_processing when the section is
11745 finally written out. */
11746
11747 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11748 elf_link_input_bfd ignores this section. */
11749 input_section->flags &= ~SEC_HAS_CONTENTS;
11750 }
11751
11752 /* Size has been set in _bfd_mips_elf_always_size_sections. */
11753 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
11754
11755 /* Skip this section later on (I don't think this currently
11756 matters, but someday it might). */
11757 o->map_head.link_order = NULL;
11758
11759 reginfo_sec = o;
11760 }
11761
11762 if (strcmp (o->name, ".mdebug") == 0)
11763 {
11764 struct extsym_info einfo;
11765 bfd_vma last;
11766
11767 /* We have found the .mdebug section in the output file.
11768 Look through all the link_orders comprising it and merge
11769 the information together. */
11770 symhdr->magic = swap->sym_magic;
11771 /* FIXME: What should the version stamp be? */
11772 symhdr->vstamp = 0;
11773 symhdr->ilineMax = 0;
11774 symhdr->cbLine = 0;
11775 symhdr->idnMax = 0;
11776 symhdr->ipdMax = 0;
11777 symhdr->isymMax = 0;
11778 symhdr->ioptMax = 0;
11779 symhdr->iauxMax = 0;
11780 symhdr->issMax = 0;
11781 symhdr->issExtMax = 0;
11782 symhdr->ifdMax = 0;
11783 symhdr->crfd = 0;
11784 symhdr->iextMax = 0;
11785
11786 /* We accumulate the debugging information itself in the
11787 debug_info structure. */
11788 debug.line = NULL;
11789 debug.external_dnr = NULL;
11790 debug.external_pdr = NULL;
11791 debug.external_sym = NULL;
11792 debug.external_opt = NULL;
11793 debug.external_aux = NULL;
11794 debug.ss = NULL;
11795 debug.ssext = debug.ssext_end = NULL;
11796 debug.external_fdr = NULL;
11797 debug.external_rfd = NULL;
11798 debug.external_ext = debug.external_ext_end = NULL;
11799
11800 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11801 if (mdebug_handle == NULL)
11802 return FALSE;
11803
11804 esym.jmptbl = 0;
11805 esym.cobol_main = 0;
11806 esym.weakext = 0;
11807 esym.reserved = 0;
11808 esym.ifd = ifdNil;
11809 esym.asym.iss = issNil;
11810 esym.asym.st = stLocal;
11811 esym.asym.reserved = 0;
11812 esym.asym.index = indexNil;
11813 last = 0;
11814 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11815 {
11816 esym.asym.sc = sc[i];
11817 s = bfd_get_section_by_name (abfd, secname[i]);
11818 if (s != NULL)
11819 {
11820 esym.asym.value = s->vma;
11821 last = s->vma + s->size;
11822 }
11823 else
11824 esym.asym.value = last;
11825 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11826 secname[i], &esym))
11827 return FALSE;
11828 }
11829
11830 for (p = o->map_head.link_order; p != NULL; p = p->next)
11831 {
11832 asection *input_section;
11833 bfd *input_bfd;
11834 const struct ecoff_debug_swap *input_swap;
11835 struct ecoff_debug_info input_debug;
11836 char *eraw_src;
11837 char *eraw_end;
11838
11839 if (p->type != bfd_indirect_link_order)
11840 {
11841 if (p->type == bfd_data_link_order)
11842 continue;
11843 abort ();
11844 }
11845
11846 input_section = p->u.indirect.section;
11847 input_bfd = input_section->owner;
11848
11849 if (!is_mips_elf (input_bfd))
11850 {
11851 /* I don't know what a non MIPS ELF bfd would be
11852 doing with a .mdebug section, but I don't really
11853 want to deal with it. */
11854 continue;
11855 }
11856
11857 input_swap = (get_elf_backend_data (input_bfd)
11858 ->elf_backend_ecoff_debug_swap);
11859
11860 BFD_ASSERT (p->size == input_section->size);
11861
11862 /* The ECOFF linking code expects that we have already
11863 read in the debugging information and set up an
11864 ecoff_debug_info structure, so we do that now. */
11865 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11866 &input_debug))
11867 return FALSE;
11868
11869 if (! (bfd_ecoff_debug_accumulate
11870 (mdebug_handle, abfd, &debug, swap, input_bfd,
11871 &input_debug, input_swap, info)))
11872 return FALSE;
11873
11874 /* Loop through the external symbols. For each one with
11875 interesting information, try to find the symbol in
11876 the linker global hash table and save the information
11877 for the output external symbols. */
11878 eraw_src = input_debug.external_ext;
11879 eraw_end = (eraw_src
11880 + (input_debug.symbolic_header.iextMax
11881 * input_swap->external_ext_size));
11882 for (;
11883 eraw_src < eraw_end;
11884 eraw_src += input_swap->external_ext_size)
11885 {
11886 EXTR ext;
11887 const char *name;
11888 struct mips_elf_link_hash_entry *h;
11889
11890 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11891 if (ext.asym.sc == scNil
11892 || ext.asym.sc == scUndefined
11893 || ext.asym.sc == scSUndefined)
11894 continue;
11895
11896 name = input_debug.ssext + ext.asym.iss;
11897 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11898 name, FALSE, FALSE, TRUE);
11899 if (h == NULL || h->esym.ifd != -2)
11900 continue;
11901
11902 if (ext.ifd != -1)
11903 {
11904 BFD_ASSERT (ext.ifd
11905 < input_debug.symbolic_header.ifdMax);
11906 ext.ifd = input_debug.ifdmap[ext.ifd];
11907 }
11908
11909 h->esym = ext;
11910 }
11911
11912 /* Free up the information we just read. */
11913 free (input_debug.line);
11914 free (input_debug.external_dnr);
11915 free (input_debug.external_pdr);
11916 free (input_debug.external_sym);
11917 free (input_debug.external_opt);
11918 free (input_debug.external_aux);
11919 free (input_debug.ss);
11920 free (input_debug.ssext);
11921 free (input_debug.external_fdr);
11922 free (input_debug.external_rfd);
11923 free (input_debug.external_ext);
11924
11925 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11926 elf_link_input_bfd ignores this section. */
11927 input_section->flags &= ~SEC_HAS_CONTENTS;
11928 }
11929
11930 if (SGI_COMPAT (abfd) && info->shared)
11931 {
11932 /* Create .rtproc section. */
11933 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11934 if (rtproc_sec == NULL)
11935 {
11936 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11937 | SEC_LINKER_CREATED | SEC_READONLY);
11938
11939 rtproc_sec = bfd_make_section_with_flags (abfd,
11940 ".rtproc",
11941 flags);
11942 if (rtproc_sec == NULL
11943 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11944 return FALSE;
11945 }
11946
11947 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11948 info, rtproc_sec,
11949 &debug))
11950 return FALSE;
11951 }
11952
11953 /* Build the external symbol information. */
11954 einfo.abfd = abfd;
11955 einfo.info = info;
11956 einfo.debug = &debug;
11957 einfo.swap = swap;
11958 einfo.failed = FALSE;
11959 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11960 mips_elf_output_extsym, &einfo);
11961 if (einfo.failed)
11962 return FALSE;
11963
11964 /* Set the size of the .mdebug section. */
11965 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11966
11967 /* Skip this section later on (I don't think this currently
11968 matters, but someday it might). */
11969 o->map_head.link_order = NULL;
11970
11971 mdebug_sec = o;
11972 }
11973
11974 if (CONST_STRNEQ (o->name, ".gptab."))
11975 {
11976 const char *subname;
11977 unsigned int c;
11978 Elf32_gptab *tab;
11979 Elf32_External_gptab *ext_tab;
11980 unsigned int j;
11981
11982 /* The .gptab.sdata and .gptab.sbss sections hold
11983 information describing how the small data area would
11984 change depending upon the -G switch. These sections
11985 not used in executables files. */
11986 if (! info->relocatable)
11987 {
11988 for (p = o->map_head.link_order; p != NULL; p = p->next)
11989 {
11990 asection *input_section;
11991
11992 if (p->type != bfd_indirect_link_order)
11993 {
11994 if (p->type == bfd_data_link_order)
11995 continue;
11996 abort ();
11997 }
11998
11999 input_section = p->u.indirect.section;
12000
12001 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12002 elf_link_input_bfd ignores this section. */
12003 input_section->flags &= ~SEC_HAS_CONTENTS;
12004 }
12005
12006 /* Skip this section later on (I don't think this
12007 currently matters, but someday it might). */
12008 o->map_head.link_order = NULL;
12009
12010 /* Really remove the section. */
12011 bfd_section_list_remove (abfd, o);
12012 --abfd->section_count;
12013
12014 continue;
12015 }
12016
12017 /* There is one gptab for initialized data, and one for
12018 uninitialized data. */
12019 if (strcmp (o->name, ".gptab.sdata") == 0)
12020 gptab_data_sec = o;
12021 else if (strcmp (o->name, ".gptab.sbss") == 0)
12022 gptab_bss_sec = o;
12023 else
12024 {
12025 (*_bfd_error_handler)
12026 (_("%s: illegal section name `%s'"),
12027 bfd_get_filename (abfd), o->name);
12028 bfd_set_error (bfd_error_nonrepresentable_section);
12029 return FALSE;
12030 }
12031
12032 /* The linker script always combines .gptab.data and
12033 .gptab.sdata into .gptab.sdata, and likewise for
12034 .gptab.bss and .gptab.sbss. It is possible that there is
12035 no .sdata or .sbss section in the output file, in which
12036 case we must change the name of the output section. */
12037 subname = o->name + sizeof ".gptab" - 1;
12038 if (bfd_get_section_by_name (abfd, subname) == NULL)
12039 {
12040 if (o == gptab_data_sec)
12041 o->name = ".gptab.data";
12042 else
12043 o->name = ".gptab.bss";
12044 subname = o->name + sizeof ".gptab" - 1;
12045 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
12046 }
12047
12048 /* Set up the first entry. */
12049 c = 1;
12050 amt = c * sizeof (Elf32_gptab);
12051 tab = bfd_malloc (amt);
12052 if (tab == NULL)
12053 return FALSE;
12054 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
12055 tab[0].gt_header.gt_unused = 0;
12056
12057 /* Combine the input sections. */
12058 for (p = o->map_head.link_order; p != NULL; p = p->next)
12059 {
12060 asection *input_section;
12061 bfd *input_bfd;
12062 bfd_size_type size;
12063 unsigned long last;
12064 bfd_size_type gpentry;
12065
12066 if (p->type != bfd_indirect_link_order)
12067 {
12068 if (p->type == bfd_data_link_order)
12069 continue;
12070 abort ();
12071 }
12072
12073 input_section = p->u.indirect.section;
12074 input_bfd = input_section->owner;
12075
12076 /* Combine the gptab entries for this input section one
12077 by one. We know that the input gptab entries are
12078 sorted by ascending -G value. */
12079 size = input_section->size;
12080 last = 0;
12081 for (gpentry = sizeof (Elf32_External_gptab);
12082 gpentry < size;
12083 gpentry += sizeof (Elf32_External_gptab))
12084 {
12085 Elf32_External_gptab ext_gptab;
12086 Elf32_gptab int_gptab;
12087 unsigned long val;
12088 unsigned long add;
12089 bfd_boolean exact;
12090 unsigned int look;
12091
12092 if (! (bfd_get_section_contents
12093 (input_bfd, input_section, &ext_gptab, gpentry,
12094 sizeof (Elf32_External_gptab))))
12095 {
12096 free (tab);
12097 return FALSE;
12098 }
12099
12100 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12101 &int_gptab);
12102 val = int_gptab.gt_entry.gt_g_value;
12103 add = int_gptab.gt_entry.gt_bytes - last;
12104
12105 exact = FALSE;
12106 for (look = 1; look < c; look++)
12107 {
12108 if (tab[look].gt_entry.gt_g_value >= val)
12109 tab[look].gt_entry.gt_bytes += add;
12110
12111 if (tab[look].gt_entry.gt_g_value == val)
12112 exact = TRUE;
12113 }
12114
12115 if (! exact)
12116 {
12117 Elf32_gptab *new_tab;
12118 unsigned int max;
12119
12120 /* We need a new table entry. */
12121 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
12122 new_tab = bfd_realloc (tab, amt);
12123 if (new_tab == NULL)
12124 {
12125 free (tab);
12126 return FALSE;
12127 }
12128 tab = new_tab;
12129 tab[c].gt_entry.gt_g_value = val;
12130 tab[c].gt_entry.gt_bytes = add;
12131
12132 /* Merge in the size for the next smallest -G
12133 value, since that will be implied by this new
12134 value. */
12135 max = 0;
12136 for (look = 1; look < c; look++)
12137 {
12138 if (tab[look].gt_entry.gt_g_value < val
12139 && (max == 0
12140 || (tab[look].gt_entry.gt_g_value
12141 > tab[max].gt_entry.gt_g_value)))
12142 max = look;
12143 }
12144 if (max != 0)
12145 tab[c].gt_entry.gt_bytes +=
12146 tab[max].gt_entry.gt_bytes;
12147
12148 ++c;
12149 }
12150
12151 last = int_gptab.gt_entry.gt_bytes;
12152 }
12153
12154 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12155 elf_link_input_bfd ignores this section. */
12156 input_section->flags &= ~SEC_HAS_CONTENTS;
12157 }
12158
12159 /* The table must be sorted by -G value. */
12160 if (c > 2)
12161 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12162
12163 /* Swap out the table. */
12164 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
12165 ext_tab = bfd_alloc (abfd, amt);
12166 if (ext_tab == NULL)
12167 {
12168 free (tab);
12169 return FALSE;
12170 }
12171
12172 for (j = 0; j < c; j++)
12173 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12174 free (tab);
12175
12176 o->size = c * sizeof (Elf32_External_gptab);
12177 o->contents = (bfd_byte *) ext_tab;
12178
12179 /* Skip this section later on (I don't think this currently
12180 matters, but someday it might). */
12181 o->map_head.link_order = NULL;
12182 }
12183 }
12184
12185 /* Invoke the regular ELF backend linker to do all the work. */
12186 if (!bfd_elf_final_link (abfd, info))
12187 return FALSE;
12188
12189 /* Now write out the computed sections. */
12190
12191 if (reginfo_sec != NULL)
12192 {
12193 Elf32_External_RegInfo ext;
12194
12195 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
12196 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
12197 return FALSE;
12198 }
12199
12200 if (mdebug_sec != NULL)
12201 {
12202 BFD_ASSERT (abfd->output_has_begun);
12203 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12204 swap, info,
12205 mdebug_sec->filepos))
12206 return FALSE;
12207
12208 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12209 }
12210
12211 if (gptab_data_sec != NULL)
12212 {
12213 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12214 gptab_data_sec->contents,
12215 0, gptab_data_sec->size))
12216 return FALSE;
12217 }
12218
12219 if (gptab_bss_sec != NULL)
12220 {
12221 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12222 gptab_bss_sec->contents,
12223 0, gptab_bss_sec->size))
12224 return FALSE;
12225 }
12226
12227 if (SGI_COMPAT (abfd))
12228 {
12229 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12230 if (rtproc_sec != NULL)
12231 {
12232 if (! bfd_set_section_contents (abfd, rtproc_sec,
12233 rtproc_sec->contents,
12234 0, rtproc_sec->size))
12235 return FALSE;
12236 }
12237 }
12238
12239 return TRUE;
12240 }
12241 \f
12242 /* Structure for saying that BFD machine EXTENSION extends BASE. */
12243
12244 struct mips_mach_extension {
12245 unsigned long extension, base;
12246 };
12247
12248
12249 /* An array describing how BFD machines relate to one another. The entries
12250 are ordered topologically with MIPS I extensions listed last. */
12251
12252 static const struct mips_mach_extension mips_mach_extensions[] = {
12253 /* MIPS64r2 extensions. */
12254 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12255
12256 /* MIPS64 extensions. */
12257 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
12258 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
12259 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
12260
12261 /* MIPS V extensions. */
12262 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12263
12264 /* R10000 extensions. */
12265 { bfd_mach_mips12000, bfd_mach_mips10000 },
12266 { bfd_mach_mips14000, bfd_mach_mips10000 },
12267 { bfd_mach_mips16000, bfd_mach_mips10000 },
12268
12269 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12270 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12271 better to allow vr5400 and vr5500 code to be merged anyway, since
12272 many libraries will just use the core ISA. Perhaps we could add
12273 some sort of ASE flag if this ever proves a problem. */
12274 { bfd_mach_mips5500, bfd_mach_mips5400 },
12275 { bfd_mach_mips5400, bfd_mach_mips5000 },
12276
12277 /* MIPS IV extensions. */
12278 { bfd_mach_mips5, bfd_mach_mips8000 },
12279 { bfd_mach_mips10000, bfd_mach_mips8000 },
12280 { bfd_mach_mips5000, bfd_mach_mips8000 },
12281 { bfd_mach_mips7000, bfd_mach_mips8000 },
12282 { bfd_mach_mips9000, bfd_mach_mips8000 },
12283
12284 /* VR4100 extensions. */
12285 { bfd_mach_mips4120, bfd_mach_mips4100 },
12286 { bfd_mach_mips4111, bfd_mach_mips4100 },
12287
12288 /* MIPS III extensions. */
12289 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12290 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
12291 { bfd_mach_mips8000, bfd_mach_mips4000 },
12292 { bfd_mach_mips4650, bfd_mach_mips4000 },
12293 { bfd_mach_mips4600, bfd_mach_mips4000 },
12294 { bfd_mach_mips4400, bfd_mach_mips4000 },
12295 { bfd_mach_mips4300, bfd_mach_mips4000 },
12296 { bfd_mach_mips4100, bfd_mach_mips4000 },
12297 { bfd_mach_mips4010, bfd_mach_mips4000 },
12298
12299 /* MIPS32 extensions. */
12300 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12301
12302 /* MIPS II extensions. */
12303 { bfd_mach_mips4000, bfd_mach_mips6000 },
12304 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12305
12306 /* MIPS I extensions. */
12307 { bfd_mach_mips6000, bfd_mach_mips3000 },
12308 { bfd_mach_mips3900, bfd_mach_mips3000 }
12309 };
12310
12311
12312 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12313
12314 static bfd_boolean
12315 mips_mach_extends_p (unsigned long base, unsigned long extension)
12316 {
12317 size_t i;
12318
12319 if (extension == base)
12320 return TRUE;
12321
12322 if (base == bfd_mach_mipsisa32
12323 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12324 return TRUE;
12325
12326 if (base == bfd_mach_mipsisa32r2
12327 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12328 return TRUE;
12329
12330 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
12331 if (extension == mips_mach_extensions[i].extension)
12332 {
12333 extension = mips_mach_extensions[i].base;
12334 if (extension == base)
12335 return TRUE;
12336 }
12337
12338 return FALSE;
12339 }
12340
12341
12342 /* Return true if the given ELF header flags describe a 32-bit binary. */
12343
12344 static bfd_boolean
12345 mips_32bit_flags_p (flagword flags)
12346 {
12347 return ((flags & EF_MIPS_32BITMODE) != 0
12348 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12349 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12350 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12351 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12352 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12353 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
12354 }
12355
12356
12357 /* Merge object attributes from IBFD into OBFD. Raise an error if
12358 there are conflicting attributes. */
12359 static bfd_boolean
12360 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12361 {
12362 obj_attribute *in_attr;
12363 obj_attribute *out_attr;
12364
12365 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12366 {
12367 /* This is the first object. Copy the attributes. */
12368 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12369
12370 /* Use the Tag_null value to indicate the attributes have been
12371 initialized. */
12372 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12373
12374 return TRUE;
12375 }
12376
12377 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12378 non-conflicting ones. */
12379 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12380 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12381 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12382 {
12383 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12384 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12385 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12386 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12387 ;
12388 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12389 _bfd_error_handler
12390 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12391 in_attr[Tag_GNU_MIPS_ABI_FP].i);
12392 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12393 _bfd_error_handler
12394 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12395 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12396 else
12397 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12398 {
12399 case 1:
12400 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12401 {
12402 case 2:
12403 _bfd_error_handler
12404 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12405 obfd, ibfd);
12406 break;
12407
12408 case 3:
12409 _bfd_error_handler
12410 (_("Warning: %B uses hard float, %B uses soft float"),
12411 obfd, ibfd);
12412 break;
12413
12414 case 4:
12415 _bfd_error_handler
12416 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12417 obfd, ibfd);
12418 break;
12419
12420 default:
12421 abort ();
12422 }
12423 break;
12424
12425 case 2:
12426 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12427 {
12428 case 1:
12429 _bfd_error_handler
12430 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12431 ibfd, obfd);
12432 break;
12433
12434 case 3:
12435 _bfd_error_handler
12436 (_("Warning: %B uses hard float, %B uses soft float"),
12437 obfd, ibfd);
12438 break;
12439
12440 case 4:
12441 _bfd_error_handler
12442 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12443 obfd, ibfd);
12444 break;
12445
12446 default:
12447 abort ();
12448 }
12449 break;
12450
12451 case 3:
12452 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12453 {
12454 case 1:
12455 case 2:
12456 case 4:
12457 _bfd_error_handler
12458 (_("Warning: %B uses hard float, %B uses soft float"),
12459 ibfd, obfd);
12460 break;
12461
12462 default:
12463 abort ();
12464 }
12465 break;
12466
12467 case 4:
12468 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12469 {
12470 case 1:
12471 _bfd_error_handler
12472 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12473 ibfd, obfd);
12474 break;
12475
12476 case 2:
12477 _bfd_error_handler
12478 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12479 ibfd, obfd);
12480 break;
12481
12482 case 3:
12483 _bfd_error_handler
12484 (_("Warning: %B uses hard float, %B uses soft float"),
12485 obfd, ibfd);
12486 break;
12487
12488 default:
12489 abort ();
12490 }
12491 break;
12492
12493 default:
12494 abort ();
12495 }
12496 }
12497
12498 /* Merge Tag_compatibility attributes and any common GNU ones. */
12499 _bfd_elf_merge_object_attributes (ibfd, obfd);
12500
12501 return TRUE;
12502 }
12503
12504 /* Merge backend specific data from an object file to the output
12505 object file when linking. */
12506
12507 bfd_boolean
12508 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
12509 {
12510 flagword old_flags;
12511 flagword new_flags;
12512 bfd_boolean ok;
12513 bfd_boolean null_input_bfd = TRUE;
12514 asection *sec;
12515
12516 /* Check if we have the same endianess */
12517 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
12518 {
12519 (*_bfd_error_handler)
12520 (_("%B: endianness incompatible with that of the selected emulation"),
12521 ibfd);
12522 return FALSE;
12523 }
12524
12525 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
12526 return TRUE;
12527
12528 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12529 {
12530 (*_bfd_error_handler)
12531 (_("%B: ABI is incompatible with that of the selected emulation"),
12532 ibfd);
12533 return FALSE;
12534 }
12535
12536 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12537 return FALSE;
12538
12539 new_flags = elf_elfheader (ibfd)->e_flags;
12540 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12541 old_flags = elf_elfheader (obfd)->e_flags;
12542
12543 if (! elf_flags_init (obfd))
12544 {
12545 elf_flags_init (obfd) = TRUE;
12546 elf_elfheader (obfd)->e_flags = new_flags;
12547 elf_elfheader (obfd)->e_ident[EI_CLASS]
12548 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12549
12550 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
12551 && (bfd_get_arch_info (obfd)->the_default
12552 || mips_mach_extends_p (bfd_get_mach (obfd),
12553 bfd_get_mach (ibfd))))
12554 {
12555 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12556 bfd_get_mach (ibfd)))
12557 return FALSE;
12558 }
12559
12560 return TRUE;
12561 }
12562
12563 /* Check flag compatibility. */
12564
12565 new_flags &= ~EF_MIPS_NOREORDER;
12566 old_flags &= ~EF_MIPS_NOREORDER;
12567
12568 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12569 doesn't seem to matter. */
12570 new_flags &= ~EF_MIPS_XGOT;
12571 old_flags &= ~EF_MIPS_XGOT;
12572
12573 /* MIPSpro generates ucode info in n64 objects. Again, we should
12574 just be able to ignore this. */
12575 new_flags &= ~EF_MIPS_UCODE;
12576 old_flags &= ~EF_MIPS_UCODE;
12577
12578 /* DSOs should only be linked with CPIC code. */
12579 if ((ibfd->flags & DYNAMIC) != 0)
12580 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
12581
12582 if (new_flags == old_flags)
12583 return TRUE;
12584
12585 /* Check to see if the input BFD actually contains any sections.
12586 If not, its flags may not have been initialised either, but it cannot
12587 actually cause any incompatibility. */
12588 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12589 {
12590 /* Ignore synthetic sections and empty .text, .data and .bss sections
12591 which are automatically generated by gas. */
12592 if (strcmp (sec->name, ".reginfo")
12593 && strcmp (sec->name, ".mdebug")
12594 && (sec->size != 0
12595 || (strcmp (sec->name, ".text")
12596 && strcmp (sec->name, ".data")
12597 && strcmp (sec->name, ".bss"))))
12598 {
12599 null_input_bfd = FALSE;
12600 break;
12601 }
12602 }
12603 if (null_input_bfd)
12604 return TRUE;
12605
12606 ok = TRUE;
12607
12608 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12609 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
12610 {
12611 (*_bfd_error_handler)
12612 (_("%B: warning: linking abicalls files with non-abicalls files"),
12613 ibfd);
12614 ok = TRUE;
12615 }
12616
12617 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12618 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12619 if (! (new_flags & EF_MIPS_PIC))
12620 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12621
12622 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12623 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12624
12625 /* Compare the ISAs. */
12626 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
12627 {
12628 (*_bfd_error_handler)
12629 (_("%B: linking 32-bit code with 64-bit code"),
12630 ibfd);
12631 ok = FALSE;
12632 }
12633 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12634 {
12635 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12636 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
12637 {
12638 /* Copy the architecture info from IBFD to OBFD. Also copy
12639 the 32-bit flag (if set) so that we continue to recognise
12640 OBFD as a 32-bit binary. */
12641 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12642 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12643 elf_elfheader (obfd)->e_flags
12644 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12645
12646 /* Copy across the ABI flags if OBFD doesn't use them
12647 and if that was what caused us to treat IBFD as 32-bit. */
12648 if ((old_flags & EF_MIPS_ABI) == 0
12649 && mips_32bit_flags_p (new_flags)
12650 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12651 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
12652 }
12653 else
12654 {
12655 /* The ISAs aren't compatible. */
12656 (*_bfd_error_handler)
12657 (_("%B: linking %s module with previous %s modules"),
12658 ibfd,
12659 bfd_printable_name (ibfd),
12660 bfd_printable_name (obfd));
12661 ok = FALSE;
12662 }
12663 }
12664
12665 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12666 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12667
12668 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
12669 does set EI_CLASS differently from any 32-bit ABI. */
12670 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12671 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12672 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12673 {
12674 /* Only error if both are set (to different values). */
12675 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12676 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12677 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12678 {
12679 (*_bfd_error_handler)
12680 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12681 ibfd,
12682 elf_mips_abi_name (ibfd),
12683 elf_mips_abi_name (obfd));
12684 ok = FALSE;
12685 }
12686 new_flags &= ~EF_MIPS_ABI;
12687 old_flags &= ~EF_MIPS_ABI;
12688 }
12689
12690 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12691 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12692 {
12693 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12694
12695 new_flags &= ~ EF_MIPS_ARCH_ASE;
12696 old_flags &= ~ EF_MIPS_ARCH_ASE;
12697 }
12698
12699 /* Warn about any other mismatches */
12700 if (new_flags != old_flags)
12701 {
12702 (*_bfd_error_handler)
12703 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12704 ibfd, (unsigned long) new_flags,
12705 (unsigned long) old_flags);
12706 ok = FALSE;
12707 }
12708
12709 if (! ok)
12710 {
12711 bfd_set_error (bfd_error_bad_value);
12712 return FALSE;
12713 }
12714
12715 return TRUE;
12716 }
12717
12718 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12719
12720 bfd_boolean
12721 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
12722 {
12723 BFD_ASSERT (!elf_flags_init (abfd)
12724 || elf_elfheader (abfd)->e_flags == flags);
12725
12726 elf_elfheader (abfd)->e_flags = flags;
12727 elf_flags_init (abfd) = TRUE;
12728 return TRUE;
12729 }
12730
12731 char *
12732 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12733 {
12734 switch (dtag)
12735 {
12736 default: return "";
12737 case DT_MIPS_RLD_VERSION:
12738 return "MIPS_RLD_VERSION";
12739 case DT_MIPS_TIME_STAMP:
12740 return "MIPS_TIME_STAMP";
12741 case DT_MIPS_ICHECKSUM:
12742 return "MIPS_ICHECKSUM";
12743 case DT_MIPS_IVERSION:
12744 return "MIPS_IVERSION";
12745 case DT_MIPS_FLAGS:
12746 return "MIPS_FLAGS";
12747 case DT_MIPS_BASE_ADDRESS:
12748 return "MIPS_BASE_ADDRESS";
12749 case DT_MIPS_MSYM:
12750 return "MIPS_MSYM";
12751 case DT_MIPS_CONFLICT:
12752 return "MIPS_CONFLICT";
12753 case DT_MIPS_LIBLIST:
12754 return "MIPS_LIBLIST";
12755 case DT_MIPS_LOCAL_GOTNO:
12756 return "MIPS_LOCAL_GOTNO";
12757 case DT_MIPS_CONFLICTNO:
12758 return "MIPS_CONFLICTNO";
12759 case DT_MIPS_LIBLISTNO:
12760 return "MIPS_LIBLISTNO";
12761 case DT_MIPS_SYMTABNO:
12762 return "MIPS_SYMTABNO";
12763 case DT_MIPS_UNREFEXTNO:
12764 return "MIPS_UNREFEXTNO";
12765 case DT_MIPS_GOTSYM:
12766 return "MIPS_GOTSYM";
12767 case DT_MIPS_HIPAGENO:
12768 return "MIPS_HIPAGENO";
12769 case DT_MIPS_RLD_MAP:
12770 return "MIPS_RLD_MAP";
12771 case DT_MIPS_DELTA_CLASS:
12772 return "MIPS_DELTA_CLASS";
12773 case DT_MIPS_DELTA_CLASS_NO:
12774 return "MIPS_DELTA_CLASS_NO";
12775 case DT_MIPS_DELTA_INSTANCE:
12776 return "MIPS_DELTA_INSTANCE";
12777 case DT_MIPS_DELTA_INSTANCE_NO:
12778 return "MIPS_DELTA_INSTANCE_NO";
12779 case DT_MIPS_DELTA_RELOC:
12780 return "MIPS_DELTA_RELOC";
12781 case DT_MIPS_DELTA_RELOC_NO:
12782 return "MIPS_DELTA_RELOC_NO";
12783 case DT_MIPS_DELTA_SYM:
12784 return "MIPS_DELTA_SYM";
12785 case DT_MIPS_DELTA_SYM_NO:
12786 return "MIPS_DELTA_SYM_NO";
12787 case DT_MIPS_DELTA_CLASSSYM:
12788 return "MIPS_DELTA_CLASSSYM";
12789 case DT_MIPS_DELTA_CLASSSYM_NO:
12790 return "MIPS_DELTA_CLASSSYM_NO";
12791 case DT_MIPS_CXX_FLAGS:
12792 return "MIPS_CXX_FLAGS";
12793 case DT_MIPS_PIXIE_INIT:
12794 return "MIPS_PIXIE_INIT";
12795 case DT_MIPS_SYMBOL_LIB:
12796 return "MIPS_SYMBOL_LIB";
12797 case DT_MIPS_LOCALPAGE_GOTIDX:
12798 return "MIPS_LOCALPAGE_GOTIDX";
12799 case DT_MIPS_LOCAL_GOTIDX:
12800 return "MIPS_LOCAL_GOTIDX";
12801 case DT_MIPS_HIDDEN_GOTIDX:
12802 return "MIPS_HIDDEN_GOTIDX";
12803 case DT_MIPS_PROTECTED_GOTIDX:
12804 return "MIPS_PROTECTED_GOT_IDX";
12805 case DT_MIPS_OPTIONS:
12806 return "MIPS_OPTIONS";
12807 case DT_MIPS_INTERFACE:
12808 return "MIPS_INTERFACE";
12809 case DT_MIPS_DYNSTR_ALIGN:
12810 return "DT_MIPS_DYNSTR_ALIGN";
12811 case DT_MIPS_INTERFACE_SIZE:
12812 return "DT_MIPS_INTERFACE_SIZE";
12813 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12814 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12815 case DT_MIPS_PERF_SUFFIX:
12816 return "DT_MIPS_PERF_SUFFIX";
12817 case DT_MIPS_COMPACT_SIZE:
12818 return "DT_MIPS_COMPACT_SIZE";
12819 case DT_MIPS_GP_VALUE:
12820 return "DT_MIPS_GP_VALUE";
12821 case DT_MIPS_AUX_DYNAMIC:
12822 return "DT_MIPS_AUX_DYNAMIC";
12823 case DT_MIPS_PLTGOT:
12824 return "DT_MIPS_PLTGOT";
12825 case DT_MIPS_RWPLT:
12826 return "DT_MIPS_RWPLT";
12827 }
12828 }
12829
12830 bfd_boolean
12831 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12832 {
12833 FILE *file = ptr;
12834
12835 BFD_ASSERT (abfd != NULL && ptr != NULL);
12836
12837 /* Print normal ELF private data. */
12838 _bfd_elf_print_private_bfd_data (abfd, ptr);
12839
12840 /* xgettext:c-format */
12841 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12842
12843 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12844 fprintf (file, _(" [abi=O32]"));
12845 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12846 fprintf (file, _(" [abi=O64]"));
12847 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12848 fprintf (file, _(" [abi=EABI32]"));
12849 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12850 fprintf (file, _(" [abi=EABI64]"));
12851 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12852 fprintf (file, _(" [abi unknown]"));
12853 else if (ABI_N32_P (abfd))
12854 fprintf (file, _(" [abi=N32]"));
12855 else if (ABI_64_P (abfd))
12856 fprintf (file, _(" [abi=64]"));
12857 else
12858 fprintf (file, _(" [no abi set]"));
12859
12860 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12861 fprintf (file, " [mips1]");
12862 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12863 fprintf (file, " [mips2]");
12864 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12865 fprintf (file, " [mips3]");
12866 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12867 fprintf (file, " [mips4]");
12868 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12869 fprintf (file, " [mips5]");
12870 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12871 fprintf (file, " [mips32]");
12872 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12873 fprintf (file, " [mips64]");
12874 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12875 fprintf (file, " [mips32r2]");
12876 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12877 fprintf (file, " [mips64r2]");
12878 else
12879 fprintf (file, _(" [unknown ISA]"));
12880
12881 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12882 fprintf (file, " [mdmx]");
12883
12884 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12885 fprintf (file, " [mips16]");
12886
12887 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12888 fprintf (file, " [32bitmode]");
12889 else
12890 fprintf (file, _(" [not 32bitmode]"));
12891
12892 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12893 fprintf (file, " [noreorder]");
12894
12895 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12896 fprintf (file, " [PIC]");
12897
12898 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12899 fprintf (file, " [CPIC]");
12900
12901 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12902 fprintf (file, " [XGOT]");
12903
12904 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12905 fprintf (file, " [UCODE]");
12906
12907 fputc ('\n', file);
12908
12909 return TRUE;
12910 }
12911
12912 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12913 {
12914 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12915 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12916 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12917 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12918 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12919 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12920 { NULL, 0, 0, 0, 0 }
12921 };
12922
12923 /* Merge non visibility st_other attributes. Ensure that the
12924 STO_OPTIONAL flag is copied into h->other, even if this is not a
12925 definiton of the symbol. */
12926 void
12927 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12928 const Elf_Internal_Sym *isym,
12929 bfd_boolean definition,
12930 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12931 {
12932 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12933 {
12934 unsigned char other;
12935
12936 other = (definition ? isym->st_other : h->other);
12937 other &= ~ELF_ST_VISIBILITY (-1);
12938 h->other = other | ELF_ST_VISIBILITY (h->other);
12939 }
12940
12941 if (!definition
12942 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12943 h->other |= STO_OPTIONAL;
12944 }
12945
12946 /* Decide whether an undefined symbol is special and can be ignored.
12947 This is the case for OPTIONAL symbols on IRIX. */
12948 bfd_boolean
12949 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12950 {
12951 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12952 }
12953
12954 bfd_boolean
12955 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12956 {
12957 return (sym->st_shndx == SHN_COMMON
12958 || sym->st_shndx == SHN_MIPS_ACOMMON
12959 || sym->st_shndx == SHN_MIPS_SCOMMON);
12960 }
12961
12962 /* Return address for Ith PLT stub in section PLT, for relocation REL
12963 or (bfd_vma) -1 if it should not be included. */
12964
12965 bfd_vma
12966 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12967 const arelent *rel ATTRIBUTE_UNUSED)
12968 {
12969 return (plt->vma
12970 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12971 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12972 }
12973
12974 void
12975 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12976 {
12977 struct mips_elf_link_hash_table *htab;
12978 Elf_Internal_Ehdr *i_ehdrp;
12979
12980 i_ehdrp = elf_elfheader (abfd);
12981 if (link_info)
12982 {
12983 htab = mips_elf_hash_table (link_info);
12984 BFD_ASSERT (htab != NULL);
12985
12986 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12987 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12988 }
12989 }