merge from gcc
[binutils.git] / bfd / elfxx-mips.c
bloba4baea95138002b5ba71f9ef1100a930bf33e6ff
1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
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>
12 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
17 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 /* This file handles functionality common to the different MIPS ABI's. */
30 #include "bfd.h"
31 #include "sysdep.h"
32 #include "libbfd.h"
33 #include "libiberty.h"
34 #include "elf-bfd.h"
35 #include "elfxx-mips.h"
36 #include "elf/mips.h"
38 /* Get the ECOFF swapping routines. */
39 #include "coff/sym.h"
40 #include "coff/symconst.h"
41 #include "coff/ecoff.h"
42 #include "coff/mips.h"
44 #include "hashtab.h"
46 /* This structure is used to hold .got entries while estimating got
47 sizes. */
48 struct mips_got_entry
50 /* The input bfd in which the symbol is defined. */
51 bfd *abfd;
52 /* The index of the symbol, as stored in the relocation r_info, if
53 we have a local symbol; -1 otherwise. */
54 long symndx;
55 union
57 /* If abfd == NULL, an address that must be stored in the got. */
58 bfd_vma address;
59 /* If abfd != NULL && symndx != -1, the addend of the relocation
60 that should be added to the symbol value. */
61 bfd_vma addend;
62 /* If abfd != NULL && symndx == -1, the hash table entry
63 corresponding to a global symbol in the got (or, local, if
64 h->forced_local). */
65 struct mips_elf_link_hash_entry *h;
66 } d;
68 /* The TLS types included in this GOT entry (specifically, GD and
69 IE). The GD and IE flags can be added as we encounter new
70 relocations. LDM can also be set; it will always be alone, not
71 combined with any GD or IE flags. An LDM GOT entry will be
72 a local symbol entry with r_symndx == 0. */
73 unsigned char tls_type;
75 /* The offset from the beginning of the .got section to the entry
76 corresponding to this symbol+addend. If it's a global symbol
77 whose offset is yet to be decided, it's going to be -1. */
78 long gotidx;
81 /* This structure is used to hold .got information when linking. */
83 struct mips_got_info
85 /* The global symbol in the GOT with the lowest index in the dynamic
86 symbol table. */
87 struct elf_link_hash_entry *global_gotsym;
88 /* The number of global .got entries. */
89 unsigned int global_gotno;
90 /* The number of .got slots used for TLS. */
91 unsigned int tls_gotno;
92 /* The first unused TLS .got entry. Used only during
93 mips_elf_initialize_tls_index. */
94 unsigned int tls_assigned_gotno;
95 /* The number of local .got entries. */
96 unsigned int local_gotno;
97 /* The number of local .got entries we have used. */
98 unsigned int assigned_gotno;
99 /* A hash table holding members of the got. */
100 struct htab *got_entries;
101 /* A hash table mapping input bfds to other mips_got_info. NULL
102 unless multi-got was necessary. */
103 struct htab *bfd2got;
104 /* In multi-got links, a pointer to the next got (err, rather, most
105 of the time, it points to the previous got). */
106 struct mips_got_info *next;
107 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
108 for none, or MINUS_TWO for not yet assigned. This is needed
109 because a single-GOT link may have multiple hash table entries
110 for the LDM. It does not get initialized in multi-GOT mode. */
111 bfd_vma tls_ldm_offset;
114 /* Map an input bfd to a got in a multi-got link. */
116 struct mips_elf_bfd2got_hash {
117 bfd *bfd;
118 struct mips_got_info *g;
121 /* Structure passed when traversing the bfd2got hash table, used to
122 create and merge bfd's gots. */
124 struct mips_elf_got_per_bfd_arg
126 /* A hashtable that maps bfds to gots. */
127 htab_t bfd2got;
128 /* The output bfd. */
129 bfd *obfd;
130 /* The link information. */
131 struct bfd_link_info *info;
132 /* A pointer to the primary got, i.e., the one that's going to get
133 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
134 DT_MIPS_GOTSYM. */
135 struct mips_got_info *primary;
136 /* A non-primary got we're trying to merge with other input bfd's
137 gots. */
138 struct mips_got_info *current;
139 /* The maximum number of got entries that can be addressed with a
140 16-bit offset. */
141 unsigned int max_count;
142 /* The number of local and global entries in the primary got. */
143 unsigned int primary_count;
144 /* The number of local and global entries in the current got. */
145 unsigned int current_count;
146 /* The total number of global entries which will live in the
147 primary got and be automatically relocated. This includes
148 those not referenced by the primary GOT but included in
149 the "master" GOT. */
150 unsigned int global_count;
153 /* Another structure used to pass arguments for got entries traversal. */
155 struct mips_elf_set_global_got_offset_arg
157 struct mips_got_info *g;
158 int value;
159 unsigned int needed_relocs;
160 struct bfd_link_info *info;
163 /* A structure used to count TLS relocations or GOT entries, for GOT
164 entry or ELF symbol table traversal. */
166 struct mips_elf_count_tls_arg
168 struct bfd_link_info *info;
169 unsigned int needed;
172 struct _mips_elf_section_data
174 struct bfd_elf_section_data elf;
175 union
177 struct mips_got_info *got_info;
178 bfd_byte *tdata;
179 } u;
182 #define mips_elf_section_data(sec) \
183 ((struct _mips_elf_section_data *) elf_section_data (sec))
185 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
186 the dynamic symbols. */
188 struct mips_elf_hash_sort_data
190 /* The symbol in the global GOT with the lowest dynamic symbol table
191 index. */
192 struct elf_link_hash_entry *low;
193 /* The least dynamic symbol table index corresponding to a non-TLS
194 symbol with a GOT entry. */
195 long min_got_dynindx;
196 /* The greatest dynamic symbol table index corresponding to a symbol
197 with a GOT entry that is not referenced (e.g., a dynamic symbol
198 with dynamic relocations pointing to it from non-primary GOTs). */
199 long max_unref_got_dynindx;
200 /* The greatest dynamic symbol table index not corresponding to a
201 symbol without a GOT entry. */
202 long max_non_got_dynindx;
205 /* The MIPS ELF linker needs additional information for each symbol in
206 the global hash table. */
208 struct mips_elf_link_hash_entry
210 struct elf_link_hash_entry root;
212 /* External symbol information. */
213 EXTR esym;
215 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
216 this symbol. */
217 unsigned int possibly_dynamic_relocs;
219 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
220 a readonly section. */
221 bfd_boolean readonly_reloc;
223 /* We must not create a stub for a symbol that has relocations
224 related to taking the function's address, i.e. any but
225 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
226 p. 4-20. */
227 bfd_boolean no_fn_stub;
229 /* If there is a stub that 32 bit functions should use to call this
230 16 bit function, this points to the section containing the stub. */
231 asection *fn_stub;
233 /* Whether we need the fn_stub; this is set if this symbol appears
234 in any relocs other than a 16 bit call. */
235 bfd_boolean need_fn_stub;
237 /* If there is a stub that 16 bit functions should use to call this
238 32 bit function, this points to the section containing the stub. */
239 asection *call_stub;
241 /* This is like the call_stub field, but it is used if the function
242 being called returns a floating point value. */
243 asection *call_fp_stub;
245 /* Are we forced local? .*/
246 bfd_boolean forced_local;
248 #define GOT_NORMAL 0
249 #define GOT_TLS_GD 1
250 #define GOT_TLS_LDM 2
251 #define GOT_TLS_IE 4
252 #define GOT_TLS_OFFSET_DONE 0x40
253 #define GOT_TLS_DONE 0x80
254 unsigned char tls_type;
255 /* This is only used in single-GOT mode; in multi-GOT mode there
256 is one mips_got_entry per GOT entry, so the offset is stored
257 there. In single-GOT mode there may be many mips_got_entry
258 structures all referring to the same GOT slot. It might be
259 possible to use root.got.offset instead, but that field is
260 overloaded already. */
261 bfd_vma tls_got_offset;
264 /* MIPS ELF linker hash table. */
266 struct mips_elf_link_hash_table
268 struct elf_link_hash_table root;
269 #if 0
270 /* We no longer use this. */
271 /* String section indices for the dynamic section symbols. */
272 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
273 #endif
274 /* The number of .rtproc entries. */
275 bfd_size_type procedure_count;
276 /* The size of the .compact_rel section (if SGI_COMPAT). */
277 bfd_size_type compact_rel_size;
278 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
279 entry is set to the address of __rld_obj_head as in IRIX5. */
280 bfd_boolean use_rld_obj_head;
281 /* This is the value of the __rld_map or __rld_obj_head symbol. */
282 bfd_vma rld_value;
283 /* This is set if we see any mips16 stub sections. */
284 bfd_boolean mips16_stubs_seen;
287 #define TLS_RELOC_P(r_type) \
288 (r_type == R_MIPS_TLS_DTPMOD32 \
289 || r_type == R_MIPS_TLS_DTPMOD64 \
290 || r_type == R_MIPS_TLS_DTPREL32 \
291 || r_type == R_MIPS_TLS_DTPREL64 \
292 || r_type == R_MIPS_TLS_GD \
293 || r_type == R_MIPS_TLS_LDM \
294 || r_type == R_MIPS_TLS_DTPREL_HI16 \
295 || r_type == R_MIPS_TLS_DTPREL_LO16 \
296 || r_type == R_MIPS_TLS_GOTTPREL \
297 || r_type == R_MIPS_TLS_TPREL32 \
298 || r_type == R_MIPS_TLS_TPREL64 \
299 || r_type == R_MIPS_TLS_TPREL_HI16 \
300 || r_type == R_MIPS_TLS_TPREL_LO16)
302 /* Structure used to pass information to mips_elf_output_extsym. */
304 struct extsym_info
306 bfd *abfd;
307 struct bfd_link_info *info;
308 struct ecoff_debug_info *debug;
309 const struct ecoff_debug_swap *swap;
310 bfd_boolean failed;
313 /* The names of the runtime procedure table symbols used on IRIX5. */
315 static const char * const mips_elf_dynsym_rtproc_names[] =
317 "_procedure_table",
318 "_procedure_string_table",
319 "_procedure_table_size",
320 NULL
323 /* These structures are used to generate the .compact_rel section on
324 IRIX5. */
326 typedef struct
328 unsigned long id1; /* Always one? */
329 unsigned long num; /* Number of compact relocation entries. */
330 unsigned long id2; /* Always two? */
331 unsigned long offset; /* The file offset of the first relocation. */
332 unsigned long reserved0; /* Zero? */
333 unsigned long reserved1; /* Zero? */
334 } Elf32_compact_rel;
336 typedef struct
338 bfd_byte id1[4];
339 bfd_byte num[4];
340 bfd_byte id2[4];
341 bfd_byte offset[4];
342 bfd_byte reserved0[4];
343 bfd_byte reserved1[4];
344 } Elf32_External_compact_rel;
346 typedef struct
348 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
349 unsigned int rtype : 4; /* Relocation types. See below. */
350 unsigned int dist2to : 8;
351 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
352 unsigned long konst; /* KONST field. See below. */
353 unsigned long vaddr; /* VADDR to be relocated. */
354 } Elf32_crinfo;
356 typedef struct
358 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
359 unsigned int rtype : 4; /* Relocation types. See below. */
360 unsigned int dist2to : 8;
361 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
362 unsigned long konst; /* KONST field. See below. */
363 } Elf32_crinfo2;
365 typedef struct
367 bfd_byte info[4];
368 bfd_byte konst[4];
369 bfd_byte vaddr[4];
370 } Elf32_External_crinfo;
372 typedef struct
374 bfd_byte info[4];
375 bfd_byte konst[4];
376 } Elf32_External_crinfo2;
378 /* These are the constants used to swap the bitfields in a crinfo. */
380 #define CRINFO_CTYPE (0x1)
381 #define CRINFO_CTYPE_SH (31)
382 #define CRINFO_RTYPE (0xf)
383 #define CRINFO_RTYPE_SH (27)
384 #define CRINFO_DIST2TO (0xff)
385 #define CRINFO_DIST2TO_SH (19)
386 #define CRINFO_RELVADDR (0x7ffff)
387 #define CRINFO_RELVADDR_SH (0)
389 /* A compact relocation info has long (3 words) or short (2 words)
390 formats. A short format doesn't have VADDR field and relvaddr
391 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
392 #define CRF_MIPS_LONG 1
393 #define CRF_MIPS_SHORT 0
395 /* There are 4 types of compact relocation at least. The value KONST
396 has different meaning for each type:
398 (type) (konst)
399 CT_MIPS_REL32 Address in data
400 CT_MIPS_WORD Address in word (XXX)
401 CT_MIPS_GPHI_LO GP - vaddr
402 CT_MIPS_JMPAD Address to jump
405 #define CRT_MIPS_REL32 0xa
406 #define CRT_MIPS_WORD 0xb
407 #define CRT_MIPS_GPHI_LO 0xc
408 #define CRT_MIPS_JMPAD 0xd
410 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
411 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
412 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
413 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
415 /* The structure of the runtime procedure descriptor created by the
416 loader for use by the static exception system. */
418 typedef struct runtime_pdr {
419 bfd_vma adr; /* Memory address of start of procedure. */
420 long regmask; /* Save register mask. */
421 long regoffset; /* Save register offset. */
422 long fregmask; /* Save floating point register mask. */
423 long fregoffset; /* Save floating point register offset. */
424 long frameoffset; /* Frame size. */
425 short framereg; /* Frame pointer register. */
426 short pcreg; /* Offset or reg of return pc. */
427 long irpss; /* Index into the runtime string table. */
428 long reserved;
429 struct exception_info *exception_info;/* Pointer to exception array. */
430 } RPDR, *pRPDR;
431 #define cbRPDR sizeof (RPDR)
432 #define rpdNil ((pRPDR) 0)
434 static struct mips_got_entry *mips_elf_create_local_got_entry
435 (bfd *, bfd *, struct mips_got_info *, asection *, bfd_vma, unsigned long,
436 struct mips_elf_link_hash_entry *, int);
437 static bfd_boolean mips_elf_sort_hash_table_f
438 (struct mips_elf_link_hash_entry *, void *);
439 static bfd_vma mips_elf_high
440 (bfd_vma);
441 static bfd_boolean mips_elf_stub_section_p
442 (bfd *, asection *);
443 static bfd_boolean mips_elf_create_dynamic_relocation
444 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
445 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
446 bfd_vma *, asection *);
447 static hashval_t mips_elf_got_entry_hash
448 (const void *);
449 static bfd_vma mips_elf_adjust_gp
450 (bfd *, struct mips_got_info *, bfd *);
451 static struct mips_got_info *mips_elf_got_for_ibfd
452 (struct mips_got_info *, bfd *);
454 /* This will be used when we sort the dynamic relocation records. */
455 static bfd *reldyn_sorting_bfd;
457 /* Nonzero if ABFD is using the N32 ABI. */
459 #define ABI_N32_P(abfd) \
460 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
462 /* Nonzero if ABFD is using the N64 ABI. */
463 #define ABI_64_P(abfd) \
464 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
466 /* Nonzero if ABFD is using NewABI conventions. */
467 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
469 /* The IRIX compatibility level we are striving for. */
470 #define IRIX_COMPAT(abfd) \
471 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
473 /* Whether we are trying to be compatible with IRIX at all. */
474 #define SGI_COMPAT(abfd) \
475 (IRIX_COMPAT (abfd) != ict_none)
477 /* The name of the options section. */
478 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
479 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
481 /* The name of the stub section. */
482 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
484 /* The size of an external REL relocation. */
485 #define MIPS_ELF_REL_SIZE(abfd) \
486 (get_elf_backend_data (abfd)->s->sizeof_rel)
488 /* The size of an external dynamic table entry. */
489 #define MIPS_ELF_DYN_SIZE(abfd) \
490 (get_elf_backend_data (abfd)->s->sizeof_dyn)
492 /* The size of a GOT entry. */
493 #define MIPS_ELF_GOT_SIZE(abfd) \
494 (get_elf_backend_data (abfd)->s->arch_size / 8)
496 /* The size of a symbol-table entry. */
497 #define MIPS_ELF_SYM_SIZE(abfd) \
498 (get_elf_backend_data (abfd)->s->sizeof_sym)
500 /* The default alignment for sections, as a power of two. */
501 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
502 (get_elf_backend_data (abfd)->s->log_file_align)
504 /* Get word-sized data. */
505 #define MIPS_ELF_GET_WORD(abfd, ptr) \
506 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
508 /* Put out word-sized data. */
509 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
510 (ABI_64_P (abfd) \
511 ? bfd_put_64 (abfd, val, ptr) \
512 : bfd_put_32 (abfd, val, ptr))
514 /* Add a dynamic symbol table-entry. */
515 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
516 _bfd_elf_add_dynamic_entry (info, tag, val)
518 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
519 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
521 /* Determine whether the internal relocation of index REL_IDX is REL
522 (zero) or RELA (non-zero). The assumption is that, if there are
523 two relocation sections for this section, one of them is REL and
524 the other is RELA. If the index of the relocation we're testing is
525 in range for the first relocation section, check that the external
526 relocation size is that for RELA. It is also assumed that, if
527 rel_idx is not in range for the first section, and this first
528 section contains REL relocs, then the relocation is in the second
529 section, that is RELA. */
530 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
531 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
532 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
533 > (bfd_vma)(rel_idx)) \
534 == (elf_section_data (sec)->rel_hdr.sh_entsize \
535 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
536 : sizeof (Elf32_External_Rela))))
538 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
539 from smaller values. Start with zero, widen, *then* decrement. */
540 #define MINUS_ONE (((bfd_vma)0) - 1)
541 #define MINUS_TWO (((bfd_vma)0) - 2)
543 /* The number of local .got entries we reserve. */
544 #define MIPS_RESERVED_GOTNO (2)
546 /* The offset of $gp from the beginning of the .got section. */
547 #define ELF_MIPS_GP_OFFSET(abfd) (0x7ff0)
549 /* The maximum size of the GOT for it to be addressable using 16-bit
550 offsets from $gp. */
551 #define MIPS_ELF_GOT_MAX_SIZE(abfd) (ELF_MIPS_GP_OFFSET(abfd) + 0x7fff)
553 /* Instructions which appear in a stub. */
554 #define STUB_LW(abfd) \
555 ((ABI_64_P (abfd) \
556 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
557 : 0x8f998010)) /* lw t9,0x8010(gp) */
558 #define STUB_MOVE(abfd) \
559 ((ABI_64_P (abfd) \
560 ? 0x03e0782d /* daddu t7,ra */ \
561 : 0x03e07821)) /* addu t7,ra */
562 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
563 #define STUB_LI16(abfd) \
564 ((ABI_64_P (abfd) \
565 ? 0x64180000 /* daddiu t8,zero,0 */ \
566 : 0x24180000)) /* addiu t8,zero,0 */
567 #define MIPS_FUNCTION_STUB_SIZE (16)
569 /* The name of the dynamic interpreter. This is put in the .interp
570 section. */
572 #define ELF_DYNAMIC_INTERPRETER(abfd) \
573 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
574 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
575 : "/usr/lib/libc.so.1")
577 #ifdef BFD64
578 #define MNAME(bfd,pre,pos) \
579 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
580 #define ELF_R_SYM(bfd, i) \
581 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
582 #define ELF_R_TYPE(bfd, i) \
583 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
584 #define ELF_R_INFO(bfd, s, t) \
585 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
586 #else
587 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
588 #define ELF_R_SYM(bfd, i) \
589 (ELF32_R_SYM (i))
590 #define ELF_R_TYPE(bfd, i) \
591 (ELF32_R_TYPE (i))
592 #define ELF_R_INFO(bfd, s, t) \
593 (ELF32_R_INFO (s, t))
594 #endif
596 /* The mips16 compiler uses a couple of special sections to handle
597 floating point arguments.
599 Section names that look like .mips16.fn.FNNAME contain stubs that
600 copy floating point arguments from the fp regs to the gp regs and
601 then jump to FNNAME. If any 32 bit function calls FNNAME, the
602 call should be redirected to the stub instead. If no 32 bit
603 function calls FNNAME, the stub should be discarded. We need to
604 consider any reference to the function, not just a call, because
605 if the address of the function is taken we will need the stub,
606 since the address might be passed to a 32 bit function.
608 Section names that look like .mips16.call.FNNAME contain stubs
609 that copy floating point arguments from the gp regs to the fp
610 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
611 then any 16 bit function that calls FNNAME should be redirected
612 to the stub instead. If FNNAME is not a 32 bit function, the
613 stub should be discarded.
615 .mips16.call.fp.FNNAME sections are similar, but contain stubs
616 which call FNNAME and then copy the return value from the fp regs
617 to the gp regs. These stubs store the return value in $18 while
618 calling FNNAME; any function which might call one of these stubs
619 must arrange to save $18 around the call. (This case is not
620 needed for 32 bit functions that call 16 bit functions, because
621 16 bit functions always return floating point values in both
622 $f0/$f1 and $2/$3.)
624 Note that in all cases FNNAME might be defined statically.
625 Therefore, FNNAME is not used literally. Instead, the relocation
626 information will indicate which symbol the section is for.
628 We record any stubs that we find in the symbol table. */
630 #define FN_STUB ".mips16.fn."
631 #define CALL_STUB ".mips16.call."
632 #define CALL_FP_STUB ".mips16.call.fp."
634 /* Look up an entry in a MIPS ELF linker hash table. */
636 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
637 ((struct mips_elf_link_hash_entry *) \
638 elf_link_hash_lookup (&(table)->root, (string), (create), \
639 (copy), (follow)))
641 /* Traverse a MIPS ELF linker hash table. */
643 #define mips_elf_link_hash_traverse(table, func, info) \
644 (elf_link_hash_traverse \
645 (&(table)->root, \
646 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
647 (info)))
649 /* Get the MIPS ELF linker hash table from a link_info structure. */
651 #define mips_elf_hash_table(p) \
652 ((struct mips_elf_link_hash_table *) ((p)->hash))
654 /* Find the base offsets for thread-local storage in this object,
655 for GD/LD and IE/LE respectively. */
657 #define TP_OFFSET 0x7000
658 #define DTP_OFFSET 0x8000
660 static bfd_vma
661 dtprel_base (struct bfd_link_info *info)
663 /* If tls_sec is NULL, we should have signalled an error already. */
664 if (elf_hash_table (info)->tls_sec == NULL)
665 return 0;
666 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
669 static bfd_vma
670 tprel_base (struct bfd_link_info *info)
672 /* If tls_sec is NULL, we should have signalled an error already. */
673 if (elf_hash_table (info)->tls_sec == NULL)
674 return 0;
675 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
678 /* Create an entry in a MIPS ELF linker hash table. */
680 static struct bfd_hash_entry *
681 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
682 struct bfd_hash_table *table, const char *string)
684 struct mips_elf_link_hash_entry *ret =
685 (struct mips_elf_link_hash_entry *) entry;
687 /* Allocate the structure if it has not already been allocated by a
688 subclass. */
689 if (ret == NULL)
690 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
691 if (ret == NULL)
692 return (struct bfd_hash_entry *) ret;
694 /* Call the allocation method of the superclass. */
695 ret = ((struct mips_elf_link_hash_entry *)
696 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
697 table, string));
698 if (ret != NULL)
700 /* Set local fields. */
701 memset (&ret->esym, 0, sizeof (EXTR));
702 /* We use -2 as a marker to indicate that the information has
703 not been set. -1 means there is no associated ifd. */
704 ret->esym.ifd = -2;
705 ret->possibly_dynamic_relocs = 0;
706 ret->readonly_reloc = FALSE;
707 ret->no_fn_stub = FALSE;
708 ret->fn_stub = NULL;
709 ret->need_fn_stub = FALSE;
710 ret->call_stub = NULL;
711 ret->call_fp_stub = NULL;
712 ret->forced_local = FALSE;
713 ret->tls_type = GOT_NORMAL;
716 return (struct bfd_hash_entry *) ret;
719 bfd_boolean
720 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
722 struct _mips_elf_section_data *sdata;
723 bfd_size_type amt = sizeof (*sdata);
725 sdata = bfd_zalloc (abfd, amt);
726 if (sdata == NULL)
727 return FALSE;
728 sec->used_by_bfd = sdata;
730 return _bfd_elf_new_section_hook (abfd, sec);
733 /* Read ECOFF debugging information from a .mdebug section into a
734 ecoff_debug_info structure. */
736 bfd_boolean
737 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
738 struct ecoff_debug_info *debug)
740 HDRR *symhdr;
741 const struct ecoff_debug_swap *swap;
742 char *ext_hdr;
744 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
745 memset (debug, 0, sizeof (*debug));
747 ext_hdr = bfd_malloc (swap->external_hdr_size);
748 if (ext_hdr == NULL && swap->external_hdr_size != 0)
749 goto error_return;
751 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
752 swap->external_hdr_size))
753 goto error_return;
755 symhdr = &debug->symbolic_header;
756 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
758 /* The symbolic header contains absolute file offsets and sizes to
759 read. */
760 #define READ(ptr, offset, count, size, type) \
761 if (symhdr->count == 0) \
762 debug->ptr = NULL; \
763 else \
765 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
766 debug->ptr = bfd_malloc (amt); \
767 if (debug->ptr == NULL) \
768 goto error_return; \
769 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
770 || bfd_bread (debug->ptr, amt, abfd) != amt) \
771 goto error_return; \
774 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
775 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
776 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
777 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
778 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
779 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
780 union aux_ext *);
781 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
782 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
783 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
784 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
785 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
786 #undef READ
788 debug->fdr = NULL;
790 return TRUE;
792 error_return:
793 if (ext_hdr != NULL)
794 free (ext_hdr);
795 if (debug->line != NULL)
796 free (debug->line);
797 if (debug->external_dnr != NULL)
798 free (debug->external_dnr);
799 if (debug->external_pdr != NULL)
800 free (debug->external_pdr);
801 if (debug->external_sym != NULL)
802 free (debug->external_sym);
803 if (debug->external_opt != NULL)
804 free (debug->external_opt);
805 if (debug->external_aux != NULL)
806 free (debug->external_aux);
807 if (debug->ss != NULL)
808 free (debug->ss);
809 if (debug->ssext != NULL)
810 free (debug->ssext);
811 if (debug->external_fdr != NULL)
812 free (debug->external_fdr);
813 if (debug->external_rfd != NULL)
814 free (debug->external_rfd);
815 if (debug->external_ext != NULL)
816 free (debug->external_ext);
817 return FALSE;
820 /* Swap RPDR (runtime procedure table entry) for output. */
822 static void
823 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
825 H_PUT_S32 (abfd, in->adr, ex->p_adr);
826 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
827 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
828 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
829 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
830 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
832 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
833 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
835 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
838 /* Create a runtime procedure table from the .mdebug section. */
840 static bfd_boolean
841 mips_elf_create_procedure_table (void *handle, bfd *abfd,
842 struct bfd_link_info *info, asection *s,
843 struct ecoff_debug_info *debug)
845 const struct ecoff_debug_swap *swap;
846 HDRR *hdr = &debug->symbolic_header;
847 RPDR *rpdr, *rp;
848 struct rpdr_ext *erp;
849 void *rtproc;
850 struct pdr_ext *epdr;
851 struct sym_ext *esym;
852 char *ss, **sv;
853 char *str;
854 bfd_size_type size;
855 bfd_size_type count;
856 unsigned long sindex;
857 unsigned long i;
858 PDR pdr;
859 SYMR sym;
860 const char *no_name_func = _("static procedure (no name)");
862 epdr = NULL;
863 rpdr = NULL;
864 esym = NULL;
865 ss = NULL;
866 sv = NULL;
868 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
870 sindex = strlen (no_name_func) + 1;
871 count = hdr->ipdMax;
872 if (count > 0)
874 size = swap->external_pdr_size;
876 epdr = bfd_malloc (size * count);
877 if (epdr == NULL)
878 goto error_return;
880 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
881 goto error_return;
883 size = sizeof (RPDR);
884 rp = rpdr = bfd_malloc (size * count);
885 if (rpdr == NULL)
886 goto error_return;
888 size = sizeof (char *);
889 sv = bfd_malloc (size * count);
890 if (sv == NULL)
891 goto error_return;
893 count = hdr->isymMax;
894 size = swap->external_sym_size;
895 esym = bfd_malloc (size * count);
896 if (esym == NULL)
897 goto error_return;
899 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
900 goto error_return;
902 count = hdr->issMax;
903 ss = bfd_malloc (count);
904 if (ss == NULL)
905 goto error_return;
906 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
907 goto error_return;
909 count = hdr->ipdMax;
910 for (i = 0; i < (unsigned long) count; i++, rp++)
912 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
913 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
914 rp->adr = sym.value;
915 rp->regmask = pdr.regmask;
916 rp->regoffset = pdr.regoffset;
917 rp->fregmask = pdr.fregmask;
918 rp->fregoffset = pdr.fregoffset;
919 rp->frameoffset = pdr.frameoffset;
920 rp->framereg = pdr.framereg;
921 rp->pcreg = pdr.pcreg;
922 rp->irpss = sindex;
923 sv[i] = ss + sym.iss;
924 sindex += strlen (sv[i]) + 1;
928 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
929 size = BFD_ALIGN (size, 16);
930 rtproc = bfd_alloc (abfd, size);
931 if (rtproc == NULL)
933 mips_elf_hash_table (info)->procedure_count = 0;
934 goto error_return;
937 mips_elf_hash_table (info)->procedure_count = count + 2;
939 erp = rtproc;
940 memset (erp, 0, sizeof (struct rpdr_ext));
941 erp++;
942 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
943 strcpy (str, no_name_func);
944 str += strlen (no_name_func) + 1;
945 for (i = 0; i < count; i++)
947 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
948 strcpy (str, sv[i]);
949 str += strlen (sv[i]) + 1;
951 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
953 /* Set the size and contents of .rtproc section. */
954 s->size = size;
955 s->contents = rtproc;
957 /* Skip this section later on (I don't think this currently
958 matters, but someday it might). */
959 s->link_order_head = NULL;
961 if (epdr != NULL)
962 free (epdr);
963 if (rpdr != NULL)
964 free (rpdr);
965 if (esym != NULL)
966 free (esym);
967 if (ss != NULL)
968 free (ss);
969 if (sv != NULL)
970 free (sv);
972 return TRUE;
974 error_return:
975 if (epdr != NULL)
976 free (epdr);
977 if (rpdr != NULL)
978 free (rpdr);
979 if (esym != NULL)
980 free (esym);
981 if (ss != NULL)
982 free (ss);
983 if (sv != NULL)
984 free (sv);
985 return FALSE;
988 /* Check the mips16 stubs for a particular symbol, and see if we can
989 discard them. */
991 static bfd_boolean
992 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h,
993 void *data ATTRIBUTE_UNUSED)
995 if (h->root.root.type == bfd_link_hash_warning)
996 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
998 if (h->fn_stub != NULL
999 && ! h->need_fn_stub)
1001 /* We don't need the fn_stub; the only references to this symbol
1002 are 16 bit calls. Clobber the size to 0 to prevent it from
1003 being included in the link. */
1004 h->fn_stub->size = 0;
1005 h->fn_stub->flags &= ~SEC_RELOC;
1006 h->fn_stub->reloc_count = 0;
1007 h->fn_stub->flags |= SEC_EXCLUDE;
1010 if (h->call_stub != NULL
1011 && h->root.other == STO_MIPS16)
1013 /* We don't need the call_stub; this is a 16 bit function, so
1014 calls from other 16 bit functions are OK. Clobber the size
1015 to 0 to prevent it from being included in the link. */
1016 h->call_stub->size = 0;
1017 h->call_stub->flags &= ~SEC_RELOC;
1018 h->call_stub->reloc_count = 0;
1019 h->call_stub->flags |= SEC_EXCLUDE;
1022 if (h->call_fp_stub != NULL
1023 && h->root.other == STO_MIPS16)
1025 /* We don't need the call_stub; this is a 16 bit function, so
1026 calls from other 16 bit functions are OK. Clobber the size
1027 to 0 to prevent it from being included in the link. */
1028 h->call_fp_stub->size = 0;
1029 h->call_fp_stub->flags &= ~SEC_RELOC;
1030 h->call_fp_stub->reloc_count = 0;
1031 h->call_fp_stub->flags |= SEC_EXCLUDE;
1034 return TRUE;
1037 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1038 Most mips16 instructions are 16 bits, but these instructions
1039 are 32 bits.
1041 The format of these instructions is:
1043 +--------------+--------------------------------+
1044 | JALX | X| Imm 20:16 | Imm 25:21 |
1045 +--------------+--------------------------------+
1046 | Immediate 15:0 |
1047 +-----------------------------------------------+
1049 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1050 Note that the immediate value in the first word is swapped.
1052 When producing a relocatable object file, R_MIPS16_26 is
1053 handled mostly like R_MIPS_26. In particular, the addend is
1054 stored as a straight 26-bit value in a 32-bit instruction.
1055 (gas makes life simpler for itself by never adjusting a
1056 R_MIPS16_26 reloc to be against a section, so the addend is
1057 always zero). However, the 32 bit instruction is stored as 2
1058 16-bit values, rather than a single 32-bit value. In a
1059 big-endian file, the result is the same; in a little-endian
1060 file, the two 16-bit halves of the 32 bit value are swapped.
1061 This is so that a disassembler can recognize the jal
1062 instruction.
1064 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1065 instruction stored as two 16-bit values. The addend A is the
1066 contents of the targ26 field. The calculation is the same as
1067 R_MIPS_26. When storing the calculated value, reorder the
1068 immediate value as shown above, and don't forget to store the
1069 value as two 16-bit values.
1071 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1072 defined as
1074 big-endian:
1075 +--------+----------------------+
1076 | | |
1077 | | targ26-16 |
1078 |31 26|25 0|
1079 +--------+----------------------+
1081 little-endian:
1082 +----------+------+-------------+
1083 | | | |
1084 | sub1 | | sub2 |
1085 |0 9|10 15|16 31|
1086 +----------+--------------------+
1087 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1088 ((sub1 << 16) | sub2)).
1090 When producing a relocatable object file, the calculation is
1091 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1092 When producing a fully linked file, the calculation is
1093 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1094 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1096 R_MIPS16_GPREL is used for GP-relative addressing in mips16
1097 mode. A typical instruction will have a format like this:
1099 +--------------+--------------------------------+
1100 | EXTEND | Imm 10:5 | Imm 15:11 |
1101 +--------------+--------------------------------+
1102 | Major | rx | ry | Imm 4:0 |
1103 +--------------+--------------------------------+
1105 EXTEND is the five bit value 11110. Major is the instruction
1106 opcode.
1108 This is handled exactly like R_MIPS_GPREL16, except that the
1109 addend is retrieved and stored as shown in this diagram; that
1110 is, the Imm fields above replace the V-rel16 field.
1112 All we need to do here is shuffle the bits appropriately. As
1113 above, the two 16-bit halves must be swapped on a
1114 little-endian system.
1116 R_MIPS16_HI16 and R_MIPS16_LO16 are used in mips16 mode to
1117 access data when neither GP-relative nor PC-relative addressing
1118 can be used. They are handled like R_MIPS_HI16 and R_MIPS_LO16,
1119 except that the addend is retrieved and stored as shown above
1120 for R_MIPS16_GPREL.
1122 void
1123 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1124 bfd_boolean jal_shuffle, bfd_byte *data)
1126 bfd_vma extend, insn, val;
1128 if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1129 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1130 return;
1132 /* Pick up the mips16 extend instruction and the real instruction. */
1133 extend = bfd_get_16 (abfd, data);
1134 insn = bfd_get_16 (abfd, data + 2);
1135 if (r_type == R_MIPS16_26)
1137 if (jal_shuffle)
1138 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1139 | ((extend & 0x1f) << 21) | insn;
1140 else
1141 val = extend << 16 | insn;
1143 else
1144 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1145 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1146 bfd_put_32 (abfd, val, data);
1149 void
1150 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1151 bfd_boolean jal_shuffle, bfd_byte *data)
1153 bfd_vma extend, insn, val;
1155 if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1156 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1157 return;
1159 val = bfd_get_32 (abfd, data);
1160 if (r_type == R_MIPS16_26)
1162 if (jal_shuffle)
1164 insn = val & 0xffff;
1165 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1166 | ((val >> 21) & 0x1f);
1168 else
1170 insn = val & 0xffff;
1171 extend = val >> 16;
1174 else
1176 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1177 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1179 bfd_put_16 (abfd, insn, data + 2);
1180 bfd_put_16 (abfd, extend, data);
1183 bfd_reloc_status_type
1184 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1185 arelent *reloc_entry, asection *input_section,
1186 bfd_boolean relocatable, void *data, bfd_vma gp)
1188 bfd_vma relocation;
1189 bfd_signed_vma val;
1190 bfd_reloc_status_type status;
1192 if (bfd_is_com_section (symbol->section))
1193 relocation = 0;
1194 else
1195 relocation = symbol->value;
1197 relocation += symbol->section->output_section->vma;
1198 relocation += symbol->section->output_offset;
1200 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1201 return bfd_reloc_outofrange;
1203 /* Set val to the offset into the section or symbol. */
1204 val = reloc_entry->addend;
1206 _bfd_mips_elf_sign_extend (val, 16);
1208 /* Adjust val for the final section location and GP value. If we
1209 are producing relocatable output, we don't want to do this for
1210 an external symbol. */
1211 if (! relocatable
1212 || (symbol->flags & BSF_SECTION_SYM) != 0)
1213 val += relocation - gp;
1215 if (reloc_entry->howto->partial_inplace)
1217 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1218 (bfd_byte *) data
1219 + reloc_entry->address);
1220 if (status != bfd_reloc_ok)
1221 return status;
1223 else
1224 reloc_entry->addend = val;
1226 if (relocatable)
1227 reloc_entry->address += input_section->output_offset;
1229 return bfd_reloc_ok;
1232 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1233 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1234 that contains the relocation field and DATA points to the start of
1235 INPUT_SECTION. */
1237 struct mips_hi16
1239 struct mips_hi16 *next;
1240 bfd_byte *data;
1241 asection *input_section;
1242 arelent rel;
1245 /* FIXME: This should not be a static variable. */
1247 static struct mips_hi16 *mips_hi16_list;
1249 /* A howto special_function for REL *HI16 relocations. We can only
1250 calculate the correct value once we've seen the partnering
1251 *LO16 relocation, so just save the information for later.
1253 The ABI requires that the *LO16 immediately follow the *HI16.
1254 However, as a GNU extension, we permit an arbitrary number of
1255 *HI16s to be associated with a single *LO16. This significantly
1256 simplies the relocation handling in gcc. */
1258 bfd_reloc_status_type
1259 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1260 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1261 asection *input_section, bfd *output_bfd,
1262 char **error_message ATTRIBUTE_UNUSED)
1264 struct mips_hi16 *n;
1266 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1267 return bfd_reloc_outofrange;
1269 n = bfd_malloc (sizeof *n);
1270 if (n == NULL)
1271 return bfd_reloc_outofrange;
1273 n->next = mips_hi16_list;
1274 n->data = data;
1275 n->input_section = input_section;
1276 n->rel = *reloc_entry;
1277 mips_hi16_list = n;
1279 if (output_bfd != NULL)
1280 reloc_entry->address += input_section->output_offset;
1282 return bfd_reloc_ok;
1285 /* A howto special_function for REL R_MIPS_GOT16 relocations. This is just
1286 like any other 16-bit relocation when applied to global symbols, but is
1287 treated in the same as R_MIPS_HI16 when applied to local symbols. */
1289 bfd_reloc_status_type
1290 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1291 void *data, asection *input_section,
1292 bfd *output_bfd, char **error_message)
1294 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1295 || bfd_is_und_section (bfd_get_section (symbol))
1296 || bfd_is_com_section (bfd_get_section (symbol)))
1297 /* The relocation is against a global symbol. */
1298 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1299 input_section, output_bfd,
1300 error_message);
1302 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1303 input_section, output_bfd, error_message);
1306 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
1307 is a straightforward 16 bit inplace relocation, but we must deal with
1308 any partnering high-part relocations as well. */
1310 bfd_reloc_status_type
1311 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1312 void *data, asection *input_section,
1313 bfd *output_bfd, char **error_message)
1315 bfd_vma vallo;
1316 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1318 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1319 return bfd_reloc_outofrange;
1321 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1322 location);
1323 vallo = bfd_get_32 (abfd, location);
1324 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1325 location);
1327 while (mips_hi16_list != NULL)
1329 bfd_reloc_status_type ret;
1330 struct mips_hi16 *hi;
1332 hi = mips_hi16_list;
1334 /* R_MIPS_GOT16 relocations are something of a special case. We
1335 want to install the addend in the same way as for a R_MIPS_HI16
1336 relocation (with a rightshift of 16). However, since GOT16
1337 relocations can also be used with global symbols, their howto
1338 has a rightshift of 0. */
1339 if (hi->rel.howto->type == R_MIPS_GOT16)
1340 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1342 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
1343 carry or borrow will induce a change of +1 or -1 in the high part. */
1344 hi->rel.addend += (vallo + 0x8000) & 0xffff;
1346 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1347 hi->input_section, output_bfd,
1348 error_message);
1349 if (ret != bfd_reloc_ok)
1350 return ret;
1352 mips_hi16_list = hi->next;
1353 free (hi);
1356 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1357 input_section, output_bfd,
1358 error_message);
1361 /* A generic howto special_function. This calculates and installs the
1362 relocation itself, thus avoiding the oft-discussed problems in
1363 bfd_perform_relocation and bfd_install_relocation. */
1365 bfd_reloc_status_type
1366 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1367 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1368 asection *input_section, bfd *output_bfd,
1369 char **error_message ATTRIBUTE_UNUSED)
1371 bfd_signed_vma val;
1372 bfd_reloc_status_type status;
1373 bfd_boolean relocatable;
1375 relocatable = (output_bfd != NULL);
1377 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1378 return bfd_reloc_outofrange;
1380 /* Build up the field adjustment in VAL. */
1381 val = 0;
1382 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1384 /* Either we're calculating the final field value or we have a
1385 relocation against a section symbol. Add in the section's
1386 offset or address. */
1387 val += symbol->section->output_section->vma;
1388 val += symbol->section->output_offset;
1391 if (!relocatable)
1393 /* We're calculating the final field value. Add in the symbol's value
1394 and, if pc-relative, subtract the address of the field itself. */
1395 val += symbol->value;
1396 if (reloc_entry->howto->pc_relative)
1398 val -= input_section->output_section->vma;
1399 val -= input_section->output_offset;
1400 val -= reloc_entry->address;
1404 /* VAL is now the final adjustment. If we're keeping this relocation
1405 in the output file, and if the relocation uses a separate addend,
1406 we just need to add VAL to that addend. Otherwise we need to add
1407 VAL to the relocation field itself. */
1408 if (relocatable && !reloc_entry->howto->partial_inplace)
1409 reloc_entry->addend += val;
1410 else
1412 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1414 /* Add in the separate addend, if any. */
1415 val += reloc_entry->addend;
1417 /* Add VAL to the relocation field. */
1418 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1419 location);
1420 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1421 location);
1422 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1423 location);
1425 if (status != bfd_reloc_ok)
1426 return status;
1429 if (relocatable)
1430 reloc_entry->address += input_section->output_offset;
1432 return bfd_reloc_ok;
1435 /* Swap an entry in a .gptab section. Note that these routines rely
1436 on the equivalence of the two elements of the union. */
1438 static void
1439 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1440 Elf32_gptab *in)
1442 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1443 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1446 static void
1447 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1448 Elf32_External_gptab *ex)
1450 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1451 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1454 static void
1455 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1456 Elf32_External_compact_rel *ex)
1458 H_PUT_32 (abfd, in->id1, ex->id1);
1459 H_PUT_32 (abfd, in->num, ex->num);
1460 H_PUT_32 (abfd, in->id2, ex->id2);
1461 H_PUT_32 (abfd, in->offset, ex->offset);
1462 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1463 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1466 static void
1467 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1468 Elf32_External_crinfo *ex)
1470 unsigned long l;
1472 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1473 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1474 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1475 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1476 H_PUT_32 (abfd, l, ex->info);
1477 H_PUT_32 (abfd, in->konst, ex->konst);
1478 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1481 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1482 routines swap this structure in and out. They are used outside of
1483 BFD, so they are globally visible. */
1485 void
1486 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1487 Elf32_RegInfo *in)
1489 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1490 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1491 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1492 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1493 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1494 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1497 void
1498 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1499 Elf32_External_RegInfo *ex)
1501 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1502 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1503 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1504 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1505 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1506 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1509 /* In the 64 bit ABI, the .MIPS.options section holds register
1510 information in an Elf64_Reginfo structure. These routines swap
1511 them in and out. They are globally visible because they are used
1512 outside of BFD. These routines are here so that gas can call them
1513 without worrying about whether the 64 bit ABI has been included. */
1515 void
1516 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1517 Elf64_Internal_RegInfo *in)
1519 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1520 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1521 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1522 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1523 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1524 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1525 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1528 void
1529 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1530 Elf64_External_RegInfo *ex)
1532 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1533 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1534 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1535 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1536 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1537 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1538 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1541 /* Swap in an options header. */
1543 void
1544 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1545 Elf_Internal_Options *in)
1547 in->kind = H_GET_8 (abfd, ex->kind);
1548 in->size = H_GET_8 (abfd, ex->size);
1549 in->section = H_GET_16 (abfd, ex->section);
1550 in->info = H_GET_32 (abfd, ex->info);
1553 /* Swap out an options header. */
1555 void
1556 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1557 Elf_External_Options *ex)
1559 H_PUT_8 (abfd, in->kind, ex->kind);
1560 H_PUT_8 (abfd, in->size, ex->size);
1561 H_PUT_16 (abfd, in->section, ex->section);
1562 H_PUT_32 (abfd, in->info, ex->info);
1565 /* This function is called via qsort() to sort the dynamic relocation
1566 entries by increasing r_symndx value. */
1568 static int
1569 sort_dynamic_relocs (const void *arg1, const void *arg2)
1571 Elf_Internal_Rela int_reloc1;
1572 Elf_Internal_Rela int_reloc2;
1574 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1575 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1577 return ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1580 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
1582 static int
1583 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1584 const void *arg2 ATTRIBUTE_UNUSED)
1586 #ifdef BFD64
1587 Elf_Internal_Rela int_reloc1[3];
1588 Elf_Internal_Rela int_reloc2[3];
1590 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1591 (reldyn_sorting_bfd, arg1, int_reloc1);
1592 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1593 (reldyn_sorting_bfd, arg2, int_reloc2);
1595 return (ELF64_R_SYM (int_reloc1[0].r_info)
1596 - ELF64_R_SYM (int_reloc2[0].r_info));
1597 #else
1598 abort ();
1599 #endif
1603 /* This routine is used to write out ECOFF debugging external symbol
1604 information. It is called via mips_elf_link_hash_traverse. The
1605 ECOFF external symbol information must match the ELF external
1606 symbol information. Unfortunately, at this point we don't know
1607 whether a symbol is required by reloc information, so the two
1608 tables may wind up being different. We must sort out the external
1609 symbol information before we can set the final size of the .mdebug
1610 section, and we must set the size of the .mdebug section before we
1611 can relocate any sections, and we can't know which symbols are
1612 required by relocation until we relocate the sections.
1613 Fortunately, it is relatively unlikely that any symbol will be
1614 stripped but required by a reloc. In particular, it can not happen
1615 when generating a final executable. */
1617 static bfd_boolean
1618 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1620 struct extsym_info *einfo = data;
1621 bfd_boolean strip;
1622 asection *sec, *output_section;
1624 if (h->root.root.type == bfd_link_hash_warning)
1625 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1627 if (h->root.indx == -2)
1628 strip = FALSE;
1629 else if ((h->root.def_dynamic
1630 || h->root.ref_dynamic
1631 || h->root.type == bfd_link_hash_new)
1632 && !h->root.def_regular
1633 && !h->root.ref_regular)
1634 strip = TRUE;
1635 else if (einfo->info->strip == strip_all
1636 || (einfo->info->strip == strip_some
1637 && bfd_hash_lookup (einfo->info->keep_hash,
1638 h->root.root.root.string,
1639 FALSE, FALSE) == NULL))
1640 strip = TRUE;
1641 else
1642 strip = FALSE;
1644 if (strip)
1645 return TRUE;
1647 if (h->esym.ifd == -2)
1649 h->esym.jmptbl = 0;
1650 h->esym.cobol_main = 0;
1651 h->esym.weakext = 0;
1652 h->esym.reserved = 0;
1653 h->esym.ifd = ifdNil;
1654 h->esym.asym.value = 0;
1655 h->esym.asym.st = stGlobal;
1657 if (h->root.root.type == bfd_link_hash_undefined
1658 || h->root.root.type == bfd_link_hash_undefweak)
1660 const char *name;
1662 /* Use undefined class. Also, set class and type for some
1663 special symbols. */
1664 name = h->root.root.root.string;
1665 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1666 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1668 h->esym.asym.sc = scData;
1669 h->esym.asym.st = stLabel;
1670 h->esym.asym.value = 0;
1672 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1674 h->esym.asym.sc = scAbs;
1675 h->esym.asym.st = stLabel;
1676 h->esym.asym.value =
1677 mips_elf_hash_table (einfo->info)->procedure_count;
1679 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1681 h->esym.asym.sc = scAbs;
1682 h->esym.asym.st = stLabel;
1683 h->esym.asym.value = elf_gp (einfo->abfd);
1685 else
1686 h->esym.asym.sc = scUndefined;
1688 else if (h->root.root.type != bfd_link_hash_defined
1689 && h->root.root.type != bfd_link_hash_defweak)
1690 h->esym.asym.sc = scAbs;
1691 else
1693 const char *name;
1695 sec = h->root.root.u.def.section;
1696 output_section = sec->output_section;
1698 /* When making a shared library and symbol h is the one from
1699 the another shared library, OUTPUT_SECTION may be null. */
1700 if (output_section == NULL)
1701 h->esym.asym.sc = scUndefined;
1702 else
1704 name = bfd_section_name (output_section->owner, output_section);
1706 if (strcmp (name, ".text") == 0)
1707 h->esym.asym.sc = scText;
1708 else if (strcmp (name, ".data") == 0)
1709 h->esym.asym.sc = scData;
1710 else if (strcmp (name, ".sdata") == 0)
1711 h->esym.asym.sc = scSData;
1712 else if (strcmp (name, ".rodata") == 0
1713 || strcmp (name, ".rdata") == 0)
1714 h->esym.asym.sc = scRData;
1715 else if (strcmp (name, ".bss") == 0)
1716 h->esym.asym.sc = scBss;
1717 else if (strcmp (name, ".sbss") == 0)
1718 h->esym.asym.sc = scSBss;
1719 else if (strcmp (name, ".init") == 0)
1720 h->esym.asym.sc = scInit;
1721 else if (strcmp (name, ".fini") == 0)
1722 h->esym.asym.sc = scFini;
1723 else
1724 h->esym.asym.sc = scAbs;
1728 h->esym.asym.reserved = 0;
1729 h->esym.asym.index = indexNil;
1732 if (h->root.root.type == bfd_link_hash_common)
1733 h->esym.asym.value = h->root.root.u.c.size;
1734 else if (h->root.root.type == bfd_link_hash_defined
1735 || h->root.root.type == bfd_link_hash_defweak)
1737 if (h->esym.asym.sc == scCommon)
1738 h->esym.asym.sc = scBss;
1739 else if (h->esym.asym.sc == scSCommon)
1740 h->esym.asym.sc = scSBss;
1742 sec = h->root.root.u.def.section;
1743 output_section = sec->output_section;
1744 if (output_section != NULL)
1745 h->esym.asym.value = (h->root.root.u.def.value
1746 + sec->output_offset
1747 + output_section->vma);
1748 else
1749 h->esym.asym.value = 0;
1751 else if (h->root.needs_plt)
1753 struct mips_elf_link_hash_entry *hd = h;
1754 bfd_boolean no_fn_stub = h->no_fn_stub;
1756 while (hd->root.root.type == bfd_link_hash_indirect)
1758 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
1759 no_fn_stub = no_fn_stub || hd->no_fn_stub;
1762 if (!no_fn_stub)
1764 /* Set type and value for a symbol with a function stub. */
1765 h->esym.asym.st = stProc;
1766 sec = hd->root.root.u.def.section;
1767 if (sec == NULL)
1768 h->esym.asym.value = 0;
1769 else
1771 output_section = sec->output_section;
1772 if (output_section != NULL)
1773 h->esym.asym.value = (hd->root.plt.offset
1774 + sec->output_offset
1775 + output_section->vma);
1776 else
1777 h->esym.asym.value = 0;
1782 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1783 h->root.root.root.string,
1784 &h->esym))
1786 einfo->failed = TRUE;
1787 return FALSE;
1790 return TRUE;
1793 /* A comparison routine used to sort .gptab entries. */
1795 static int
1796 gptab_compare (const void *p1, const void *p2)
1798 const Elf32_gptab *a1 = p1;
1799 const Elf32_gptab *a2 = p2;
1801 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1804 /* Functions to manage the got entry hash table. */
1806 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
1807 hash number. */
1809 static INLINE hashval_t
1810 mips_elf_hash_bfd_vma (bfd_vma addr)
1812 #ifdef BFD64
1813 return addr + (addr >> 32);
1814 #else
1815 return addr;
1816 #endif
1819 /* got_entries only match if they're identical, except for gotidx, so
1820 use all fields to compute the hash, and compare the appropriate
1821 union members. */
1823 static hashval_t
1824 mips_elf_got_entry_hash (const void *entry_)
1826 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1828 return entry->symndx
1829 + ((entry->tls_type & GOT_TLS_LDM) << 17)
1830 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
1831 : entry->abfd->id
1832 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
1833 : entry->d.h->root.root.root.hash));
1836 static int
1837 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
1839 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1840 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1842 /* An LDM entry can only match another LDM entry. */
1843 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1844 return 0;
1846 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
1847 && (! e1->abfd ? e1->d.address == e2->d.address
1848 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
1849 : e1->d.h == e2->d.h);
1852 /* multi_got_entries are still a match in the case of global objects,
1853 even if the input bfd in which they're referenced differs, so the
1854 hash computation and compare functions are adjusted
1855 accordingly. */
1857 static hashval_t
1858 mips_elf_multi_got_entry_hash (const void *entry_)
1860 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1862 return entry->symndx
1863 + (! entry->abfd
1864 ? mips_elf_hash_bfd_vma (entry->d.address)
1865 : entry->symndx >= 0
1866 ? ((entry->tls_type & GOT_TLS_LDM)
1867 ? (GOT_TLS_LDM << 17)
1868 : (entry->abfd->id
1869 + mips_elf_hash_bfd_vma (entry->d.addend)))
1870 : entry->d.h->root.root.root.hash);
1873 static int
1874 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
1876 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1877 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1879 /* Any two LDM entries match. */
1880 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
1881 return 1;
1883 /* Nothing else matches an LDM entry. */
1884 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1885 return 0;
1887 return e1->symndx == e2->symndx
1888 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
1889 : e1->abfd == NULL || e2->abfd == NULL
1890 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
1891 : e1->d.h == e2->d.h);
1894 /* Returns the dynamic relocation section for DYNOBJ. */
1896 static asection *
1897 mips_elf_rel_dyn_section (bfd *dynobj, bfd_boolean create_p)
1899 static const char dname[] = ".rel.dyn";
1900 asection *sreloc;
1902 sreloc = bfd_get_section_by_name (dynobj, dname);
1903 if (sreloc == NULL && create_p)
1905 sreloc = bfd_make_section (dynobj, dname);
1906 if (sreloc == NULL
1907 || ! bfd_set_section_flags (dynobj, sreloc,
1908 (SEC_ALLOC
1909 | SEC_LOAD
1910 | SEC_HAS_CONTENTS
1911 | SEC_IN_MEMORY
1912 | SEC_LINKER_CREATED
1913 | SEC_READONLY))
1914 || ! bfd_set_section_alignment (dynobj, sreloc,
1915 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
1916 return NULL;
1918 return sreloc;
1921 /* Returns the GOT section for ABFD. */
1923 static asection *
1924 mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
1926 asection *sgot = bfd_get_section_by_name (abfd, ".got");
1927 if (sgot == NULL
1928 || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
1929 return NULL;
1930 return sgot;
1933 /* Returns the GOT information associated with the link indicated by
1934 INFO. If SGOTP is non-NULL, it is filled in with the GOT
1935 section. */
1937 static struct mips_got_info *
1938 mips_elf_got_info (bfd *abfd, asection **sgotp)
1940 asection *sgot;
1941 struct mips_got_info *g;
1943 sgot = mips_elf_got_section (abfd, TRUE);
1944 BFD_ASSERT (sgot != NULL);
1945 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
1946 g = mips_elf_section_data (sgot)->u.got_info;
1947 BFD_ASSERT (g != NULL);
1949 if (sgotp)
1950 *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
1952 return g;
1955 /* Count the number of relocations needed for a TLS GOT entry, with
1956 access types from TLS_TYPE, and symbol H (or a local symbol if H
1957 is NULL). */
1959 static int
1960 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
1961 struct elf_link_hash_entry *h)
1963 int indx = 0;
1964 int ret = 0;
1965 bfd_boolean need_relocs = FALSE;
1966 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
1968 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1969 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
1970 indx = h->dynindx;
1972 if ((info->shared || indx != 0)
1973 && (h == NULL
1974 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1975 || h->root.type != bfd_link_hash_undefweak))
1976 need_relocs = TRUE;
1978 if (!need_relocs)
1979 return FALSE;
1981 if (tls_type & GOT_TLS_GD)
1983 ret++;
1984 if (indx != 0)
1985 ret++;
1988 if (tls_type & GOT_TLS_IE)
1989 ret++;
1991 if ((tls_type & GOT_TLS_LDM) && info->shared)
1992 ret++;
1994 return ret;
1997 /* Count the number of TLS relocations required for the GOT entry in
1998 ARG1, if it describes a local symbol. */
2000 static int
2001 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2003 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2004 struct mips_elf_count_tls_arg *arg = arg2;
2006 if (entry->abfd != NULL && entry->symndx != -1)
2007 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2009 return 1;
2012 /* Count the number of TLS GOT entries required for the global (or
2013 forced-local) symbol in ARG1. */
2015 static int
2016 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2018 struct mips_elf_link_hash_entry *hm
2019 = (struct mips_elf_link_hash_entry *) arg1;
2020 struct mips_elf_count_tls_arg *arg = arg2;
2022 if (hm->tls_type & GOT_TLS_GD)
2023 arg->needed += 2;
2024 if (hm->tls_type & GOT_TLS_IE)
2025 arg->needed += 1;
2027 return 1;
2030 /* Count the number of TLS relocations required for the global (or
2031 forced-local) symbol in ARG1. */
2033 static int
2034 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2036 struct mips_elf_link_hash_entry *hm
2037 = (struct mips_elf_link_hash_entry *) arg1;
2038 struct mips_elf_count_tls_arg *arg = arg2;
2040 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2042 return 1;
2045 /* Output a simple dynamic relocation into SRELOC. */
2047 static void
2048 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2049 asection *sreloc,
2050 unsigned long indx,
2051 int r_type,
2052 bfd_vma offset)
2054 Elf_Internal_Rela rel[3];
2056 memset (rel, 0, sizeof (rel));
2058 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2059 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2061 if (ABI_64_P (output_bfd))
2063 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2064 (output_bfd, &rel[0],
2065 (sreloc->contents
2066 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2068 else
2069 bfd_elf32_swap_reloc_out
2070 (output_bfd, &rel[0],
2071 (sreloc->contents
2072 + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2073 ++sreloc->reloc_count;
2076 /* Initialize a set of TLS GOT entries for one symbol. */
2078 static void
2079 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2080 unsigned char *tls_type_p,
2081 struct bfd_link_info *info,
2082 struct mips_elf_link_hash_entry *h,
2083 bfd_vma value)
2085 int indx;
2086 asection *sreloc, *sgot;
2087 bfd_vma offset, offset2;
2088 bfd *dynobj;
2089 bfd_boolean need_relocs = FALSE;
2091 dynobj = elf_hash_table (info)->dynobj;
2092 sgot = mips_elf_got_section (dynobj, FALSE);
2094 indx = 0;
2095 if (h != NULL)
2097 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2099 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2100 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2101 indx = h->root.dynindx;
2104 if (*tls_type_p & GOT_TLS_DONE)
2105 return;
2107 if ((info->shared || indx != 0)
2108 && (h == NULL
2109 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2110 || h->root.type != bfd_link_hash_undefweak))
2111 need_relocs = TRUE;
2113 /* MINUS_ONE means the symbol is not defined in this object. It may not
2114 be defined at all; assume that the value doesn't matter in that
2115 case. Otherwise complain if we would use the value. */
2116 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2117 || h->root.root.type == bfd_link_hash_undefweak);
2119 /* Emit necessary relocations. */
2120 sreloc = mips_elf_rel_dyn_section (dynobj, FALSE);
2122 /* General Dynamic. */
2123 if (*tls_type_p & GOT_TLS_GD)
2125 offset = got_offset;
2126 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2128 if (need_relocs)
2130 mips_elf_output_dynamic_relocation
2131 (abfd, sreloc, indx,
2132 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2133 sgot->output_offset + sgot->output_section->vma + offset);
2135 if (indx)
2136 mips_elf_output_dynamic_relocation
2137 (abfd, sreloc, indx,
2138 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2139 sgot->output_offset + sgot->output_section->vma + offset2);
2140 else
2141 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2142 sgot->contents + offset2);
2144 else
2146 MIPS_ELF_PUT_WORD (abfd, 1,
2147 sgot->contents + offset);
2148 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2149 sgot->contents + offset2);
2152 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2155 /* Initial Exec model. */
2156 if (*tls_type_p & GOT_TLS_IE)
2158 offset = got_offset;
2160 if (need_relocs)
2162 if (indx == 0)
2163 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2164 sgot->contents + offset);
2165 else
2166 MIPS_ELF_PUT_WORD (abfd, 0,
2167 sgot->contents + offset);
2169 mips_elf_output_dynamic_relocation
2170 (abfd, sreloc, indx,
2171 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2172 sgot->output_offset + sgot->output_section->vma + offset);
2174 else
2175 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2176 sgot->contents + offset);
2179 if (*tls_type_p & GOT_TLS_LDM)
2181 /* The initial offset is zero, and the LD offsets will include the
2182 bias by DTP_OFFSET. */
2183 MIPS_ELF_PUT_WORD (abfd, 0,
2184 sgot->contents + got_offset
2185 + MIPS_ELF_GOT_SIZE (abfd));
2187 if (!info->shared)
2188 MIPS_ELF_PUT_WORD (abfd, 1,
2189 sgot->contents + got_offset);
2190 else
2191 mips_elf_output_dynamic_relocation
2192 (abfd, sreloc, indx,
2193 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2194 sgot->output_offset + sgot->output_section->vma + got_offset);
2197 *tls_type_p |= GOT_TLS_DONE;
2200 /* Return the GOT index to use for a relocation of type R_TYPE against
2201 a symbol accessed using TLS_TYPE models. The GOT entries for this
2202 symbol in this GOT start at GOT_INDEX. This function initializes the
2203 GOT entries and corresponding relocations. */
2205 static bfd_vma
2206 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2207 int r_type, struct bfd_link_info *info,
2208 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2210 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2211 || r_type == R_MIPS_TLS_LDM);
2213 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2215 if (r_type == R_MIPS_TLS_GOTTPREL)
2217 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2218 if (*tls_type & GOT_TLS_GD)
2219 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2220 else
2221 return got_index;
2224 if (r_type == R_MIPS_TLS_GD)
2226 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2227 return got_index;
2230 if (r_type == R_MIPS_TLS_LDM)
2232 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2233 return got_index;
2236 return got_index;
2239 /* Returns the GOT offset at which the indicated address can be found.
2240 If there is not yet a GOT entry for this value, create one. If
2241 R_SYMNDX refers to a TLS symbol, create a TLS GOT entry instead.
2242 Returns -1 if no satisfactory GOT offset can be found. */
2244 static bfd_vma
2245 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2246 bfd_vma value, unsigned long r_symndx,
2247 struct mips_elf_link_hash_entry *h, int r_type)
2249 asection *sgot;
2250 struct mips_got_info *g;
2251 struct mips_got_entry *entry;
2253 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2255 entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot, value,
2256 r_symndx, h, r_type);
2257 if (!entry)
2258 return MINUS_ONE;
2260 if (TLS_RELOC_P (r_type))
2261 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type, r_type,
2262 info, h, value);
2263 else
2264 return entry->gotidx;
2267 /* Returns the GOT index for the global symbol indicated by H. */
2269 static bfd_vma
2270 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2271 int r_type, struct bfd_link_info *info)
2273 bfd_vma index;
2274 asection *sgot;
2275 struct mips_got_info *g, *gg;
2276 long global_got_dynindx = 0;
2278 gg = g = mips_elf_got_info (abfd, &sgot);
2279 if (g->bfd2got && ibfd)
2281 struct mips_got_entry e, *p;
2283 BFD_ASSERT (h->dynindx >= 0);
2285 g = mips_elf_got_for_ibfd (g, ibfd);
2286 if (g->next != gg || TLS_RELOC_P (r_type))
2288 e.abfd = ibfd;
2289 e.symndx = -1;
2290 e.d.h = (struct mips_elf_link_hash_entry *)h;
2291 e.tls_type = 0;
2293 p = htab_find (g->got_entries, &e);
2295 BFD_ASSERT (p->gotidx > 0);
2297 if (TLS_RELOC_P (r_type))
2299 bfd_vma value = MINUS_ONE;
2300 if ((h->root.type == bfd_link_hash_defined
2301 || h->root.type == bfd_link_hash_defweak)
2302 && h->root.u.def.section->output_section)
2303 value = (h->root.u.def.value
2304 + h->root.u.def.section->output_offset
2305 + h->root.u.def.section->output_section->vma);
2307 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2308 info, e.d.h, value);
2310 else
2311 return p->gotidx;
2315 if (gg->global_gotsym != NULL)
2316 global_got_dynindx = gg->global_gotsym->dynindx;
2318 if (TLS_RELOC_P (r_type))
2320 struct mips_elf_link_hash_entry *hm
2321 = (struct mips_elf_link_hash_entry *) h;
2322 bfd_vma value = MINUS_ONE;
2324 if ((h->root.type == bfd_link_hash_defined
2325 || h->root.type == bfd_link_hash_defweak)
2326 && h->root.u.def.section->output_section)
2327 value = (h->root.u.def.value
2328 + h->root.u.def.section->output_offset
2329 + h->root.u.def.section->output_section->vma);
2331 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2332 r_type, info, hm, value);
2334 else
2336 /* Once we determine the global GOT entry with the lowest dynamic
2337 symbol table index, we must put all dynamic symbols with greater
2338 indices into the GOT. That makes it easy to calculate the GOT
2339 offset. */
2340 BFD_ASSERT (h->dynindx >= global_got_dynindx);
2341 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2342 * MIPS_ELF_GOT_SIZE (abfd));
2344 BFD_ASSERT (index < sgot->size);
2346 return index;
2349 /* Find a GOT entry that is within 32KB of the VALUE. These entries
2350 are supposed to be placed at small offsets in the GOT, i.e.,
2351 within 32KB of GP. Return the index into the GOT for this page,
2352 and store the offset from this entry to the desired address in
2353 OFFSETP, if it is non-NULL. */
2355 static bfd_vma
2356 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2357 bfd_vma value, bfd_vma *offsetp)
2359 asection *sgot;
2360 struct mips_got_info *g;
2361 bfd_vma index;
2362 struct mips_got_entry *entry;
2364 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2366 entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot,
2367 (value + 0x8000)
2368 & (~(bfd_vma)0xffff), 0,
2369 NULL, R_MIPS_GOT_PAGE);
2371 if (!entry)
2372 return MINUS_ONE;
2374 index = entry->gotidx;
2376 if (offsetp)
2377 *offsetp = value - entry->d.address;
2379 return index;
2382 /* Find a GOT entry whose higher-order 16 bits are the same as those
2383 for value. Return the index into the GOT for this entry. */
2385 static bfd_vma
2386 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2387 bfd_vma value, bfd_boolean external)
2389 asection *sgot;
2390 struct mips_got_info *g;
2391 struct mips_got_entry *entry;
2393 if (! external)
2395 /* Although the ABI says that it is "the high-order 16 bits" that we
2396 want, it is really the %high value. The complete value is
2397 calculated with a `addiu' of a LO16 relocation, just as with a
2398 HI16/LO16 pair. */
2399 value = mips_elf_high (value) << 16;
2402 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2404 entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot, value, 0, NULL,
2405 R_MIPS_GOT16);
2406 if (entry)
2407 return entry->gotidx;
2408 else
2409 return MINUS_ONE;
2412 /* Returns the offset for the entry at the INDEXth position
2413 in the GOT. */
2415 static bfd_vma
2416 mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2417 bfd *input_bfd, bfd_vma index)
2419 asection *sgot;
2420 bfd_vma gp;
2421 struct mips_got_info *g;
2423 g = mips_elf_got_info (dynobj, &sgot);
2424 gp = _bfd_get_gp_value (output_bfd)
2425 + mips_elf_adjust_gp (output_bfd, g, input_bfd);
2427 return sgot->output_section->vma + sgot->output_offset + index - gp;
2430 /* Create a local GOT entry for VALUE. Return the index of the entry,
2431 or -1 if it could not be created. If R_SYMNDX refers to a TLS symbol,
2432 create a TLS entry instead. */
2434 static struct mips_got_entry *
2435 mips_elf_create_local_got_entry (bfd *abfd, bfd *ibfd,
2436 struct mips_got_info *gg,
2437 asection *sgot, bfd_vma value,
2438 unsigned long r_symndx,
2439 struct mips_elf_link_hash_entry *h,
2440 int r_type)
2442 struct mips_got_entry entry, **loc;
2443 struct mips_got_info *g;
2445 entry.abfd = NULL;
2446 entry.symndx = -1;
2447 entry.d.address = value;
2448 entry.tls_type = 0;
2450 g = mips_elf_got_for_ibfd (gg, ibfd);
2451 if (g == NULL)
2453 g = mips_elf_got_for_ibfd (gg, abfd);
2454 BFD_ASSERT (g != NULL);
2457 /* We might have a symbol, H, if it has been forced local. Use the
2458 global entry then. It doesn't matter whether an entry is local
2459 or global for TLS, since the dynamic linker does not
2460 automatically relocate TLS GOT entries. */
2461 BFD_ASSERT (h == NULL || h->forced_local);
2462 if (TLS_RELOC_P (r_type))
2464 struct mips_got_entry *p;
2466 entry.abfd = ibfd;
2467 if (r_type == R_MIPS_TLS_LDM)
2469 entry.tls_type = GOT_TLS_LDM;
2470 entry.symndx = 0;
2471 entry.d.addend = 0;
2473 else if (h == NULL)
2475 entry.symndx = r_symndx;
2476 entry.d.addend = 0;
2478 else
2479 entry.d.h = h;
2481 p = (struct mips_got_entry *)
2482 htab_find (g->got_entries, &entry);
2484 BFD_ASSERT (p);
2485 return p;
2488 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2489 INSERT);
2490 if (*loc)
2491 return *loc;
2493 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2494 entry.tls_type = 0;
2496 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2498 if (! *loc)
2499 return NULL;
2501 memcpy (*loc, &entry, sizeof entry);
2503 if (g->assigned_gotno >= g->local_gotno)
2505 (*loc)->gotidx = -1;
2506 /* We didn't allocate enough space in the GOT. */
2507 (*_bfd_error_handler)
2508 (_("not enough GOT space for local GOT entries"));
2509 bfd_set_error (bfd_error_bad_value);
2510 return NULL;
2513 MIPS_ELF_PUT_WORD (abfd, value,
2514 (sgot->contents + entry.gotidx));
2516 return *loc;
2519 /* Sort the dynamic symbol table so that symbols that need GOT entries
2520 appear towards the end. This reduces the amount of GOT space
2521 required. MAX_LOCAL is used to set the number of local symbols
2522 known to be in the dynamic symbol table. During
2523 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
2524 section symbols are added and the count is higher. */
2526 static bfd_boolean
2527 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2529 struct mips_elf_hash_sort_data hsd;
2530 struct mips_got_info *g;
2531 bfd *dynobj;
2533 dynobj = elf_hash_table (info)->dynobj;
2535 g = mips_elf_got_info (dynobj, NULL);
2537 hsd.low = NULL;
2538 hsd.max_unref_got_dynindx =
2539 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2540 /* In the multi-got case, assigned_gotno of the master got_info
2541 indicate the number of entries that aren't referenced in the
2542 primary GOT, but that must have entries because there are
2543 dynamic relocations that reference it. Since they aren't
2544 referenced, we move them to the end of the GOT, so that they
2545 don't prevent other entries that are referenced from getting
2546 too large offsets. */
2547 - (g->next ? g->assigned_gotno : 0);
2548 hsd.max_non_got_dynindx = max_local;
2549 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2550 elf_hash_table (info)),
2551 mips_elf_sort_hash_table_f,
2552 &hsd);
2554 /* There should have been enough room in the symbol table to
2555 accommodate both the GOT and non-GOT symbols. */
2556 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2557 BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2558 <= elf_hash_table (info)->dynsymcount);
2560 /* Now we know which dynamic symbol has the lowest dynamic symbol
2561 table index in the GOT. */
2562 g->global_gotsym = hsd.low;
2564 return TRUE;
2567 /* If H needs a GOT entry, assign it the highest available dynamic
2568 index. Otherwise, assign it the lowest available dynamic
2569 index. */
2571 static bfd_boolean
2572 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2574 struct mips_elf_hash_sort_data *hsd = data;
2576 if (h->root.root.type == bfd_link_hash_warning)
2577 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2579 /* Symbols without dynamic symbol table entries aren't interesting
2580 at all. */
2581 if (h->root.dynindx == -1)
2582 return TRUE;
2584 /* Global symbols that need GOT entries that are not explicitly
2585 referenced are marked with got offset 2. Those that are
2586 referenced get a 1, and those that don't need GOT entries get
2587 -1. */
2588 if (h->root.got.offset == 2)
2590 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2592 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2593 hsd->low = (struct elf_link_hash_entry *) h;
2594 h->root.dynindx = hsd->max_unref_got_dynindx++;
2596 else if (h->root.got.offset != 1)
2597 h->root.dynindx = hsd->max_non_got_dynindx++;
2598 else
2600 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2602 h->root.dynindx = --hsd->min_got_dynindx;
2603 hsd->low = (struct elf_link_hash_entry *) h;
2606 return TRUE;
2609 /* If H is a symbol that needs a global GOT entry, but has a dynamic
2610 symbol table index lower than any we've seen to date, record it for
2611 posterity. */
2613 static bfd_boolean
2614 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
2615 bfd *abfd, struct bfd_link_info *info,
2616 struct mips_got_info *g,
2617 unsigned char tls_flag)
2619 struct mips_got_entry entry, **loc;
2621 /* A global symbol in the GOT must also be in the dynamic symbol
2622 table. */
2623 if (h->dynindx == -1)
2625 switch (ELF_ST_VISIBILITY (h->other))
2627 case STV_INTERNAL:
2628 case STV_HIDDEN:
2629 _bfd_mips_elf_hide_symbol (info, h, TRUE);
2630 break;
2632 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2633 return FALSE;
2636 entry.abfd = abfd;
2637 entry.symndx = -1;
2638 entry.d.h = (struct mips_elf_link_hash_entry *) h;
2639 entry.tls_type = 0;
2641 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2642 INSERT);
2644 /* If we've already marked this entry as needing GOT space, we don't
2645 need to do it again. */
2646 if (*loc)
2648 (*loc)->tls_type |= tls_flag;
2649 return TRUE;
2652 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2654 if (! *loc)
2655 return FALSE;
2657 entry.gotidx = -1;
2658 entry.tls_type = tls_flag;
2660 memcpy (*loc, &entry, sizeof entry);
2662 if (h->got.offset != MINUS_ONE)
2663 return TRUE;
2665 /* By setting this to a value other than -1, we are indicating that
2666 there needs to be a GOT entry for H. Avoid using zero, as the
2667 generic ELF copy_indirect_symbol tests for <= 0. */
2668 if (tls_flag == 0)
2669 h->got.offset = 1;
2671 return TRUE;
2674 /* Reserve space in G for a GOT entry containing the value of symbol
2675 SYMNDX in input bfd ABDF, plus ADDEND. */
2677 static bfd_boolean
2678 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
2679 struct mips_got_info *g,
2680 unsigned char tls_flag)
2682 struct mips_got_entry entry, **loc;
2684 entry.abfd = abfd;
2685 entry.symndx = symndx;
2686 entry.d.addend = addend;
2687 entry.tls_type = tls_flag;
2688 loc = (struct mips_got_entry **)
2689 htab_find_slot (g->got_entries, &entry, INSERT);
2691 if (*loc)
2693 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
2695 g->tls_gotno += 2;
2696 (*loc)->tls_type |= tls_flag;
2698 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
2700 g->tls_gotno += 1;
2701 (*loc)->tls_type |= tls_flag;
2703 return TRUE;
2706 if (tls_flag != 0)
2708 entry.gotidx = -1;
2709 entry.tls_type = tls_flag;
2710 if (tls_flag == GOT_TLS_IE)
2711 g->tls_gotno += 1;
2712 else if (tls_flag == GOT_TLS_GD)
2713 g->tls_gotno += 2;
2714 else if (g->tls_ldm_offset == MINUS_ONE)
2716 g->tls_ldm_offset = MINUS_TWO;
2717 g->tls_gotno += 2;
2720 else
2722 entry.gotidx = g->local_gotno++;
2723 entry.tls_type = 0;
2726 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2728 if (! *loc)
2729 return FALSE;
2731 memcpy (*loc, &entry, sizeof entry);
2733 return TRUE;
2736 /* Compute the hash value of the bfd in a bfd2got hash entry. */
2738 static hashval_t
2739 mips_elf_bfd2got_entry_hash (const void *entry_)
2741 const struct mips_elf_bfd2got_hash *entry
2742 = (struct mips_elf_bfd2got_hash *)entry_;
2744 return entry->bfd->id;
2747 /* Check whether two hash entries have the same bfd. */
2749 static int
2750 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
2752 const struct mips_elf_bfd2got_hash *e1
2753 = (const struct mips_elf_bfd2got_hash *)entry1;
2754 const struct mips_elf_bfd2got_hash *e2
2755 = (const struct mips_elf_bfd2got_hash *)entry2;
2757 return e1->bfd == e2->bfd;
2760 /* In a multi-got link, determine the GOT to be used for IBDF. G must
2761 be the master GOT data. */
2763 static struct mips_got_info *
2764 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
2766 struct mips_elf_bfd2got_hash e, *p;
2768 if (! g->bfd2got)
2769 return g;
2771 e.bfd = ibfd;
2772 p = htab_find (g->bfd2got, &e);
2773 return p ? p->g : NULL;
2776 /* Create one separate got for each bfd that has entries in the global
2777 got, such that we can tell how many local and global entries each
2778 bfd requires. */
2780 static int
2781 mips_elf_make_got_per_bfd (void **entryp, void *p)
2783 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2784 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2785 htab_t bfd2got = arg->bfd2got;
2786 struct mips_got_info *g;
2787 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
2788 void **bfdgotp;
2790 /* Find the got_info for this GOT entry's input bfd. Create one if
2791 none exists. */
2792 bfdgot_entry.bfd = entry->abfd;
2793 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
2794 bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
2796 if (bfdgot != NULL)
2797 g = bfdgot->g;
2798 else
2800 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
2801 (arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
2803 if (bfdgot == NULL)
2805 arg->obfd = 0;
2806 return 0;
2809 *bfdgotp = bfdgot;
2811 bfdgot->bfd = entry->abfd;
2812 bfdgot->g = g = (struct mips_got_info *)
2813 bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
2814 if (g == NULL)
2816 arg->obfd = 0;
2817 return 0;
2820 g->global_gotsym = NULL;
2821 g->global_gotno = 0;
2822 g->local_gotno = 0;
2823 g->assigned_gotno = -1;
2824 g->tls_gotno = 0;
2825 g->tls_assigned_gotno = 0;
2826 g->tls_ldm_offset = MINUS_ONE;
2827 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
2828 mips_elf_multi_got_entry_eq, NULL);
2829 if (g->got_entries == NULL)
2831 arg->obfd = 0;
2832 return 0;
2835 g->bfd2got = NULL;
2836 g->next = NULL;
2839 /* Insert the GOT entry in the bfd's got entry hash table. */
2840 entryp = htab_find_slot (g->got_entries, entry, INSERT);
2841 if (*entryp != NULL)
2842 return 1;
2844 *entryp = entry;
2846 if (entry->tls_type)
2848 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
2849 g->tls_gotno += 2;
2850 if (entry->tls_type & GOT_TLS_IE)
2851 g->tls_gotno += 1;
2853 else if (entry->symndx >= 0 || entry->d.h->forced_local)
2854 ++g->local_gotno;
2855 else
2856 ++g->global_gotno;
2858 return 1;
2861 /* Attempt to merge gots of different input bfds. Try to use as much
2862 as possible of the primary got, since it doesn't require explicit
2863 dynamic relocations, but don't use bfds that would reference global
2864 symbols out of the addressable range. Failing the primary got,
2865 attempt to merge with the current got, or finish the current got
2866 and then make make the new got current. */
2868 static int
2869 mips_elf_merge_gots (void **bfd2got_, void *p)
2871 struct mips_elf_bfd2got_hash *bfd2got
2872 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
2873 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2874 unsigned int lcount = bfd2got->g->local_gotno;
2875 unsigned int gcount = bfd2got->g->global_gotno;
2876 unsigned int tcount = bfd2got->g->tls_gotno;
2877 unsigned int maxcnt = arg->max_count;
2878 bfd_boolean too_many_for_tls = FALSE;
2880 /* We place TLS GOT entries after both locals and globals. The globals
2881 for the primary GOT may overflow the normal GOT size limit, so be
2882 sure not to merge a GOT which requires TLS with the primary GOT in that
2883 case. This doesn't affect non-primary GOTs. */
2884 if (tcount > 0)
2886 unsigned int primary_total = lcount + tcount + arg->global_count;
2887 if (primary_total * MIPS_ELF_GOT_SIZE (bfd2got->bfd)
2888 >= MIPS_ELF_GOT_MAX_SIZE (bfd2got->bfd))
2889 too_many_for_tls = TRUE;
2892 /* If we don't have a primary GOT and this is not too big, use it as
2893 a starting point for the primary GOT. */
2894 if (! arg->primary && lcount + gcount + tcount <= maxcnt
2895 && ! too_many_for_tls)
2897 arg->primary = bfd2got->g;
2898 arg->primary_count = lcount + gcount;
2900 /* If it looks like we can merge this bfd's entries with those of
2901 the primary, merge them. The heuristics is conservative, but we
2902 don't have to squeeze it too hard. */
2903 else if (arg->primary && ! too_many_for_tls
2904 && (arg->primary_count + lcount + gcount + tcount) <= maxcnt)
2906 struct mips_got_info *g = bfd2got->g;
2907 int old_lcount = arg->primary->local_gotno;
2908 int old_gcount = arg->primary->global_gotno;
2909 int old_tcount = arg->primary->tls_gotno;
2911 bfd2got->g = arg->primary;
2913 htab_traverse (g->got_entries,
2914 mips_elf_make_got_per_bfd,
2915 arg);
2916 if (arg->obfd == NULL)
2917 return 0;
2919 htab_delete (g->got_entries);
2920 /* We don't have to worry about releasing memory of the actual
2921 got entries, since they're all in the master got_entries hash
2922 table anyway. */
2924 BFD_ASSERT (old_lcount + lcount >= arg->primary->local_gotno);
2925 BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
2926 BFD_ASSERT (old_tcount + tcount >= arg->primary->tls_gotno);
2928 arg->primary_count = arg->primary->local_gotno
2929 + arg->primary->global_gotno + arg->primary->tls_gotno;
2931 /* If we can merge with the last-created got, do it. */
2932 else if (arg->current
2933 && arg->current_count + lcount + gcount + tcount <= maxcnt)
2935 struct mips_got_info *g = bfd2got->g;
2936 int old_lcount = arg->current->local_gotno;
2937 int old_gcount = arg->current->global_gotno;
2938 int old_tcount = arg->current->tls_gotno;
2940 bfd2got->g = arg->current;
2942 htab_traverse (g->got_entries,
2943 mips_elf_make_got_per_bfd,
2944 arg);
2945 if (arg->obfd == NULL)
2946 return 0;
2948 htab_delete (g->got_entries);
2950 BFD_ASSERT (old_lcount + lcount >= arg->current->local_gotno);
2951 BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
2952 BFD_ASSERT (old_tcount + tcount >= arg->current->tls_gotno);
2954 arg->current_count = arg->current->local_gotno
2955 + arg->current->global_gotno + arg->current->tls_gotno;
2957 /* Well, we couldn't merge, so create a new GOT. Don't check if it
2958 fits; if it turns out that it doesn't, we'll get relocation
2959 overflows anyway. */
2960 else
2962 bfd2got->g->next = arg->current;
2963 arg->current = bfd2got->g;
2965 arg->current_count = lcount + gcount + 2 * tcount;
2968 return 1;
2971 /* Set the TLS GOT index for the GOT entry in ENTRYP. */
2973 static int
2974 mips_elf_initialize_tls_index (void **entryp, void *p)
2976 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2977 struct mips_got_info *g = p;
2979 /* We're only interested in TLS symbols. */
2980 if (entry->tls_type == 0)
2981 return 1;
2983 if (entry->symndx == -1)
2985 /* There may be multiple mips_got_entry structs for a global variable
2986 if there is just one GOT. Just do this once. */
2987 if (g->next == NULL)
2989 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
2990 return 1;
2991 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
2994 else if (entry->tls_type & GOT_TLS_LDM)
2996 /* Similarly, there may be multiple structs for the LDM entry. */
2997 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
2999 entry->gotidx = g->tls_ldm_offset;
3000 return 1;
3004 /* Initialize the GOT offset. */
3005 entry->gotidx = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3006 if (g->next == NULL && entry->symndx == -1)
3007 entry->d.h->tls_got_offset = entry->gotidx;
3009 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3010 g->tls_assigned_gotno += 2;
3011 if (entry->tls_type & GOT_TLS_IE)
3012 g->tls_assigned_gotno += 1;
3014 if (entry->tls_type & GOT_TLS_LDM)
3015 g->tls_ldm_offset = entry->gotidx;
3017 return 1;
3020 /* If passed a NULL mips_got_info in the argument, set the marker used
3021 to tell whether a global symbol needs a got entry (in the primary
3022 got) to the given VALUE.
3024 If passed a pointer G to a mips_got_info in the argument (it must
3025 not be the primary GOT), compute the offset from the beginning of
3026 the (primary) GOT section to the entry in G corresponding to the
3027 global symbol. G's assigned_gotno must contain the index of the
3028 first available global GOT entry in G. VALUE must contain the size
3029 of a GOT entry in bytes. For each global GOT entry that requires a
3030 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3031 marked as not eligible for lazy resolution through a function
3032 stub. */
3033 static int
3034 mips_elf_set_global_got_offset (void **entryp, void *p)
3036 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3037 struct mips_elf_set_global_got_offset_arg *arg
3038 = (struct mips_elf_set_global_got_offset_arg *)p;
3039 struct mips_got_info *g = arg->g;
3041 if (g && entry->tls_type != GOT_NORMAL)
3042 arg->needed_relocs +=
3043 mips_tls_got_relocs (arg->info, entry->tls_type,
3044 entry->symndx == -1 ? &entry->d.h->root : NULL);
3046 if (entry->abfd != NULL && entry->symndx == -1
3047 && entry->d.h->root.dynindx != -1
3048 && entry->d.h->tls_type == GOT_NORMAL)
3050 if (g)
3052 BFD_ASSERT (g->global_gotsym == NULL);
3054 entry->gotidx = arg->value * (long) g->assigned_gotno++;
3055 if (arg->info->shared
3056 || (elf_hash_table (arg->info)->dynamic_sections_created
3057 && entry->d.h->root.def_dynamic
3058 && !entry->d.h->root.def_regular))
3059 ++arg->needed_relocs;
3061 else
3062 entry->d.h->root.got.offset = arg->value;
3065 return 1;
3068 /* Mark any global symbols referenced in the GOT we are iterating over
3069 as inelligible for lazy resolution stubs. */
3070 static int
3071 mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
3073 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3075 if (entry->abfd != NULL
3076 && entry->symndx == -1
3077 && entry->d.h->root.dynindx != -1)
3078 entry->d.h->no_fn_stub = TRUE;
3080 return 1;
3083 /* Follow indirect and warning hash entries so that each got entry
3084 points to the final symbol definition. P must point to a pointer
3085 to the hash table we're traversing. Since this traversal may
3086 modify the hash table, we set this pointer to NULL to indicate
3087 we've made a potentially-destructive change to the hash table, so
3088 the traversal must be restarted. */
3089 static int
3090 mips_elf_resolve_final_got_entry (void **entryp, void *p)
3092 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3093 htab_t got_entries = *(htab_t *)p;
3095 if (entry->abfd != NULL && entry->symndx == -1)
3097 struct mips_elf_link_hash_entry *h = entry->d.h;
3099 while (h->root.root.type == bfd_link_hash_indirect
3100 || h->root.root.type == bfd_link_hash_warning)
3101 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3103 if (entry->d.h == h)
3104 return 1;
3106 entry->d.h = h;
3108 /* If we can't find this entry with the new bfd hash, re-insert
3109 it, and get the traversal restarted. */
3110 if (! htab_find (got_entries, entry))
3112 htab_clear_slot (got_entries, entryp);
3113 entryp = htab_find_slot (got_entries, entry, INSERT);
3114 if (! *entryp)
3115 *entryp = entry;
3116 /* Abort the traversal, since the whole table may have
3117 moved, and leave it up to the parent to restart the
3118 process. */
3119 *(htab_t *)p = NULL;
3120 return 0;
3122 /* We might want to decrement the global_gotno count, but it's
3123 either too early or too late for that at this point. */
3126 return 1;
3129 /* Turn indirect got entries in a got_entries table into their final
3130 locations. */
3131 static void
3132 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3134 htab_t got_entries;
3138 got_entries = g->got_entries;
3140 htab_traverse (got_entries,
3141 mips_elf_resolve_final_got_entry,
3142 &got_entries);
3144 while (got_entries == NULL);
3147 /* Return the offset of an input bfd IBFD's GOT from the beginning of
3148 the primary GOT. */
3149 static bfd_vma
3150 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3152 if (g->bfd2got == NULL)
3153 return 0;
3155 g = mips_elf_got_for_ibfd (g, ibfd);
3156 if (! g)
3157 return 0;
3159 BFD_ASSERT (g->next);
3161 g = g->next;
3163 return (g->local_gotno + g->global_gotno + g->tls_gotno)
3164 * MIPS_ELF_GOT_SIZE (abfd);
3167 /* Turn a single GOT that is too big for 16-bit addressing into
3168 a sequence of GOTs, each one 16-bit addressable. */
3170 static bfd_boolean
3171 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3172 struct mips_got_info *g, asection *got,
3173 bfd_size_type pages)
3175 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3176 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3177 struct mips_got_info *gg;
3178 unsigned int assign;
3180 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3181 mips_elf_bfd2got_entry_eq, NULL);
3182 if (g->bfd2got == NULL)
3183 return FALSE;
3185 got_per_bfd_arg.bfd2got = g->bfd2got;
3186 got_per_bfd_arg.obfd = abfd;
3187 got_per_bfd_arg.info = info;
3189 /* Count how many GOT entries each input bfd requires, creating a
3190 map from bfd to got info while at that. */
3191 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3192 if (got_per_bfd_arg.obfd == NULL)
3193 return FALSE;
3195 got_per_bfd_arg.current = NULL;
3196 got_per_bfd_arg.primary = NULL;
3197 /* Taking out PAGES entries is a worst-case estimate. We could
3198 compute the maximum number of pages that each separate input bfd
3199 uses, but it's probably not worth it. */
3200 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (abfd)
3201 / MIPS_ELF_GOT_SIZE (abfd))
3202 - MIPS_RESERVED_GOTNO - pages);
3203 /* The number of globals that will be included in the primary GOT.
3204 See the calls to mips_elf_set_global_got_offset below for more
3205 information. */
3206 got_per_bfd_arg.global_count = g->global_gotno;
3208 /* Try to merge the GOTs of input bfds together, as long as they
3209 don't seem to exceed the maximum GOT size, choosing one of them
3210 to be the primary GOT. */
3211 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3212 if (got_per_bfd_arg.obfd == NULL)
3213 return FALSE;
3215 /* If we do not find any suitable primary GOT, create an empty one. */
3216 if (got_per_bfd_arg.primary == NULL)
3218 g->next = (struct mips_got_info *)
3219 bfd_alloc (abfd, sizeof (struct mips_got_info));
3220 if (g->next == NULL)
3221 return FALSE;
3223 g->next->global_gotsym = NULL;
3224 g->next->global_gotno = 0;
3225 g->next->local_gotno = 0;
3226 g->next->tls_gotno = 0;
3227 g->next->assigned_gotno = 0;
3228 g->next->tls_assigned_gotno = 0;
3229 g->next->tls_ldm_offset = MINUS_ONE;
3230 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3231 mips_elf_multi_got_entry_eq,
3232 NULL);
3233 if (g->next->got_entries == NULL)
3234 return FALSE;
3235 g->next->bfd2got = NULL;
3237 else
3238 g->next = got_per_bfd_arg.primary;
3239 g->next->next = got_per_bfd_arg.current;
3241 /* GG is now the master GOT, and G is the primary GOT. */
3242 gg = g;
3243 g = g->next;
3245 /* Map the output bfd to the primary got. That's what we're going
3246 to use for bfds that use GOT16 or GOT_PAGE relocations that we
3247 didn't mark in check_relocs, and we want a quick way to find it.
3248 We can't just use gg->next because we're going to reverse the
3249 list. */
3251 struct mips_elf_bfd2got_hash *bfdgot;
3252 void **bfdgotp;
3254 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3255 (abfd, sizeof (struct mips_elf_bfd2got_hash));
3257 if (bfdgot == NULL)
3258 return FALSE;
3260 bfdgot->bfd = abfd;
3261 bfdgot->g = g;
3262 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3264 BFD_ASSERT (*bfdgotp == NULL);
3265 *bfdgotp = bfdgot;
3268 /* The IRIX dynamic linker requires every symbol that is referenced
3269 in a dynamic relocation to be present in the primary GOT, so
3270 arrange for them to appear after those that are actually
3271 referenced.
3273 GNU/Linux could very well do without it, but it would slow down
3274 the dynamic linker, since it would have to resolve every dynamic
3275 symbol referenced in other GOTs more than once, without help from
3276 the cache. Also, knowing that every external symbol has a GOT
3277 helps speed up the resolution of local symbols too, so GNU/Linux
3278 follows IRIX's practice.
3280 The number 2 is used by mips_elf_sort_hash_table_f to count
3281 global GOT symbols that are unreferenced in the primary GOT, with
3282 an initial dynamic index computed from gg->assigned_gotno, where
3283 the number of unreferenced global entries in the primary GOT is
3284 preserved. */
3285 if (1)
3287 gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3288 g->global_gotno = gg->global_gotno;
3289 set_got_offset_arg.value = 2;
3291 else
3293 /* This could be used for dynamic linkers that don't optimize
3294 symbol resolution while applying relocations so as to use
3295 primary GOT entries or assuming the symbol is locally-defined.
3296 With this code, we assign lower dynamic indices to global
3297 symbols that are not referenced in the primary GOT, so that
3298 their entries can be omitted. */
3299 gg->assigned_gotno = 0;
3300 set_got_offset_arg.value = -1;
3303 /* Reorder dynamic symbols as described above (which behavior
3304 depends on the setting of VALUE). */
3305 set_got_offset_arg.g = NULL;
3306 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3307 &set_got_offset_arg);
3308 set_got_offset_arg.value = 1;
3309 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3310 &set_got_offset_arg);
3311 if (! mips_elf_sort_hash_table (info, 1))
3312 return FALSE;
3314 /* Now go through the GOTs assigning them offset ranges.
3315 [assigned_gotno, local_gotno[ will be set to the range of local
3316 entries in each GOT. We can then compute the end of a GOT by
3317 adding local_gotno to global_gotno. We reverse the list and make
3318 it circular since then we'll be able to quickly compute the
3319 beginning of a GOT, by computing the end of its predecessor. To
3320 avoid special cases for the primary GOT, while still preserving
3321 assertions that are valid for both single- and multi-got links,
3322 we arrange for the main got struct to have the right number of
3323 global entries, but set its local_gotno such that the initial
3324 offset of the primary GOT is zero. Remember that the primary GOT
3325 will become the last item in the circular linked list, so it
3326 points back to the master GOT. */
3327 gg->local_gotno = -g->global_gotno;
3328 gg->global_gotno = g->global_gotno;
3329 gg->tls_gotno = 0;
3330 assign = 0;
3331 gg->next = gg;
3335 struct mips_got_info *gn;
3337 assign += MIPS_RESERVED_GOTNO;
3338 g->assigned_gotno = assign;
3339 g->local_gotno += assign + pages;
3340 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3342 /* Set up any TLS entries. We always place the TLS entries after
3343 all non-TLS entries. */
3344 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3345 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3347 /* Take g out of the direct list, and push it onto the reversed
3348 list that gg points to. */
3349 gn = g->next;
3350 g->next = gg->next;
3351 gg->next = g;
3352 g = gn;
3354 /* Mark global symbols in every non-primary GOT as ineligible for
3355 stubs. */
3356 if (g)
3357 htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
3359 while (g);
3361 got->size = (gg->next->local_gotno
3362 + gg->next->global_gotno
3363 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
3365 return TRUE;
3369 /* Returns the first relocation of type r_type found, beginning with
3370 RELOCATION. RELEND is one-past-the-end of the relocation table. */
3372 static const Elf_Internal_Rela *
3373 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3374 const Elf_Internal_Rela *relocation,
3375 const Elf_Internal_Rela *relend)
3377 while (relocation < relend)
3379 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type)
3380 return relocation;
3382 ++relocation;
3385 /* We didn't find it. */
3386 bfd_set_error (bfd_error_bad_value);
3387 return NULL;
3390 /* Return whether a relocation is against a local symbol. */
3392 static bfd_boolean
3393 mips_elf_local_relocation_p (bfd *input_bfd,
3394 const Elf_Internal_Rela *relocation,
3395 asection **local_sections,
3396 bfd_boolean check_forced)
3398 unsigned long r_symndx;
3399 Elf_Internal_Shdr *symtab_hdr;
3400 struct mips_elf_link_hash_entry *h;
3401 size_t extsymoff;
3403 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3404 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3405 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3407 if (r_symndx < extsymoff)
3408 return TRUE;
3409 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3410 return TRUE;
3412 if (check_forced)
3414 /* Look up the hash table to check whether the symbol
3415 was forced local. */
3416 h = (struct mips_elf_link_hash_entry *)
3417 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3418 /* Find the real hash-table entry for this symbol. */
3419 while (h->root.root.type == bfd_link_hash_indirect
3420 || h->root.root.type == bfd_link_hash_warning)
3421 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3422 if (h->root.forced_local)
3423 return TRUE;
3426 return FALSE;
3429 /* Sign-extend VALUE, which has the indicated number of BITS. */
3431 bfd_vma
3432 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
3434 if (value & ((bfd_vma) 1 << (bits - 1)))
3435 /* VALUE is negative. */
3436 value |= ((bfd_vma) - 1) << bits;
3438 return value;
3441 /* Return non-zero if the indicated VALUE has overflowed the maximum
3442 range expressible by a signed number with the indicated number of
3443 BITS. */
3445 static bfd_boolean
3446 mips_elf_overflow_p (bfd_vma value, int bits)
3448 bfd_signed_vma svalue = (bfd_signed_vma) value;
3450 if (svalue > (1 << (bits - 1)) - 1)
3451 /* The value is too big. */
3452 return TRUE;
3453 else if (svalue < -(1 << (bits - 1)))
3454 /* The value is too small. */
3455 return TRUE;
3457 /* All is well. */
3458 return FALSE;
3461 /* Calculate the %high function. */
3463 static bfd_vma
3464 mips_elf_high (bfd_vma value)
3466 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
3469 /* Calculate the %higher function. */
3471 static bfd_vma
3472 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
3474 #ifdef BFD64
3475 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
3476 #else
3477 abort ();
3478 return MINUS_ONE;
3479 #endif
3482 /* Calculate the %highest function. */
3484 static bfd_vma
3485 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
3487 #ifdef BFD64
3488 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
3489 #else
3490 abort ();
3491 return MINUS_ONE;
3492 #endif
3495 /* Create the .compact_rel section. */
3497 static bfd_boolean
3498 mips_elf_create_compact_rel_section
3499 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
3501 flagword flags;
3502 register asection *s;
3504 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
3506 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
3507 | SEC_READONLY);
3509 s = bfd_make_section (abfd, ".compact_rel");
3510 if (s == NULL
3511 || ! bfd_set_section_flags (abfd, s, flags)
3512 || ! bfd_set_section_alignment (abfd, s,
3513 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3514 return FALSE;
3516 s->size = sizeof (Elf32_External_compact_rel);
3519 return TRUE;
3522 /* Create the .got section to hold the global offset table. */
3524 static bfd_boolean
3525 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
3526 bfd_boolean maybe_exclude)
3528 flagword flags;
3529 register asection *s;
3530 struct elf_link_hash_entry *h;
3531 struct bfd_link_hash_entry *bh;
3532 struct mips_got_info *g;
3533 bfd_size_type amt;
3535 /* This function may be called more than once. */
3536 s = mips_elf_got_section (abfd, TRUE);
3537 if (s)
3539 if (! maybe_exclude)
3540 s->flags &= ~SEC_EXCLUDE;
3541 return TRUE;
3544 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3545 | SEC_LINKER_CREATED);
3547 if (maybe_exclude)
3548 flags |= SEC_EXCLUDE;
3550 /* We have to use an alignment of 2**4 here because this is hardcoded
3551 in the function stub generation and in the linker script. */
3552 s = bfd_make_section (abfd, ".got");
3553 if (s == NULL
3554 || ! bfd_set_section_flags (abfd, s, flags)
3555 || ! bfd_set_section_alignment (abfd, s, 4))
3556 return FALSE;
3558 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
3559 linker script because we don't want to define the symbol if we
3560 are not creating a global offset table. */
3561 bh = NULL;
3562 if (! (_bfd_generic_link_add_one_symbol
3563 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
3564 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
3565 return FALSE;
3567 h = (struct elf_link_hash_entry *) bh;
3568 h->non_elf = 0;
3569 h->def_regular = 1;
3570 h->type = STT_OBJECT;
3572 if (info->shared
3573 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3574 return FALSE;
3576 amt = sizeof (struct mips_got_info);
3577 g = bfd_alloc (abfd, amt);
3578 if (g == NULL)
3579 return FALSE;
3580 g->global_gotsym = NULL;
3581 g->global_gotno = 0;
3582 g->tls_gotno = 0;
3583 g->local_gotno = MIPS_RESERVED_GOTNO;
3584 g->assigned_gotno = MIPS_RESERVED_GOTNO;
3585 g->bfd2got = NULL;
3586 g->next = NULL;
3587 g->tls_ldm_offset = MINUS_ONE;
3588 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3589 mips_elf_got_entry_eq, NULL);
3590 if (g->got_entries == NULL)
3591 return FALSE;
3592 mips_elf_section_data (s)->u.got_info = g;
3593 mips_elf_section_data (s)->elf.this_hdr.sh_flags
3594 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3596 return TRUE;
3599 /* Calculate the value produced by the RELOCATION (which comes from
3600 the INPUT_BFD). The ADDEND is the addend to use for this
3601 RELOCATION; RELOCATION->R_ADDEND is ignored.
3603 The result of the relocation calculation is stored in VALUEP.
3604 REQUIRE_JALXP indicates whether or not the opcode used with this
3605 relocation must be JALX.
3607 This function returns bfd_reloc_continue if the caller need take no
3608 further action regarding this relocation, bfd_reloc_notsupported if
3609 something goes dramatically wrong, bfd_reloc_overflow if an
3610 overflow occurs, and bfd_reloc_ok to indicate success. */
3612 static bfd_reloc_status_type
3613 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
3614 asection *input_section,
3615 struct bfd_link_info *info,
3616 const Elf_Internal_Rela *relocation,
3617 bfd_vma addend, reloc_howto_type *howto,
3618 Elf_Internal_Sym *local_syms,
3619 asection **local_sections, bfd_vma *valuep,
3620 const char **namep, bfd_boolean *require_jalxp,
3621 bfd_boolean save_addend)
3623 /* The eventual value we will return. */
3624 bfd_vma value;
3625 /* The address of the symbol against which the relocation is
3626 occurring. */
3627 bfd_vma symbol = 0;
3628 /* The final GP value to be used for the relocatable, executable, or
3629 shared object file being produced. */
3630 bfd_vma gp = MINUS_ONE;
3631 /* The place (section offset or address) of the storage unit being
3632 relocated. */
3633 bfd_vma p;
3634 /* The value of GP used to create the relocatable object. */
3635 bfd_vma gp0 = MINUS_ONE;
3636 /* The offset into the global offset table at which the address of
3637 the relocation entry symbol, adjusted by the addend, resides
3638 during execution. */
3639 bfd_vma g = MINUS_ONE;
3640 /* The section in which the symbol referenced by the relocation is
3641 located. */
3642 asection *sec = NULL;
3643 struct mips_elf_link_hash_entry *h = NULL;
3644 /* TRUE if the symbol referred to by this relocation is a local
3645 symbol. */
3646 bfd_boolean local_p, was_local_p;
3647 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
3648 bfd_boolean gp_disp_p = FALSE;
3649 /* TRUE if the symbol referred to by this relocation is
3650 "__gnu_local_gp". */
3651 bfd_boolean gnu_local_gp_p = FALSE;
3652 Elf_Internal_Shdr *symtab_hdr;
3653 size_t extsymoff;
3654 unsigned long r_symndx;
3655 int r_type;
3656 /* TRUE if overflow occurred during the calculation of the
3657 relocation value. */
3658 bfd_boolean overflowed_p;
3659 /* TRUE if this relocation refers to a MIPS16 function. */
3660 bfd_boolean target_is_16_bit_code_p = FALSE;
3662 /* Parse the relocation. */
3663 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3664 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
3665 p = (input_section->output_section->vma
3666 + input_section->output_offset
3667 + relocation->r_offset);
3669 /* Assume that there will be no overflow. */
3670 overflowed_p = FALSE;
3672 /* Figure out whether or not the symbol is local, and get the offset
3673 used in the array of hash table entries. */
3674 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3675 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3676 local_sections, FALSE);
3677 was_local_p = local_p;
3678 if (! elf_bad_symtab (input_bfd))
3679 extsymoff = symtab_hdr->sh_info;
3680 else
3682 /* The symbol table does not follow the rule that local symbols
3683 must come before globals. */
3684 extsymoff = 0;
3687 /* Figure out the value of the symbol. */
3688 if (local_p)
3690 Elf_Internal_Sym *sym;
3692 sym = local_syms + r_symndx;
3693 sec = local_sections[r_symndx];
3695 symbol = sec->output_section->vma + sec->output_offset;
3696 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
3697 || (sec->flags & SEC_MERGE))
3698 symbol += sym->st_value;
3699 if ((sec->flags & SEC_MERGE)
3700 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3702 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
3703 addend -= symbol;
3704 addend += sec->output_section->vma + sec->output_offset;
3707 /* MIPS16 text labels should be treated as odd. */
3708 if (sym->st_other == STO_MIPS16)
3709 ++symbol;
3711 /* Record the name of this symbol, for our caller. */
3712 *namep = bfd_elf_string_from_elf_section (input_bfd,
3713 symtab_hdr->sh_link,
3714 sym->st_name);
3715 if (*namep == '\0')
3716 *namep = bfd_section_name (input_bfd, sec);
3718 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
3720 else
3722 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
3724 /* For global symbols we look up the symbol in the hash-table. */
3725 h = ((struct mips_elf_link_hash_entry *)
3726 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
3727 /* Find the real hash-table entry for this symbol. */
3728 while (h->root.root.type == bfd_link_hash_indirect
3729 || h->root.root.type == bfd_link_hash_warning)
3730 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3732 /* Record the name of this symbol, for our caller. */
3733 *namep = h->root.root.root.string;
3735 /* See if this is the special _gp_disp symbol. Note that such a
3736 symbol must always be a global symbol. */
3737 if (strcmp (*namep, "_gp_disp") == 0
3738 && ! NEWABI_P (input_bfd))
3740 /* Relocations against _gp_disp are permitted only with
3741 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
3742 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16
3743 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
3744 return bfd_reloc_notsupported;
3746 gp_disp_p = TRUE;
3748 /* See if this is the special _gp symbol. Note that such a
3749 symbol must always be a global symbol. */
3750 else if (strcmp (*namep, "__gnu_local_gp") == 0)
3751 gnu_local_gp_p = TRUE;
3754 /* If this symbol is defined, calculate its address. Note that
3755 _gp_disp is a magic symbol, always implicitly defined by the
3756 linker, so it's inappropriate to check to see whether or not
3757 its defined. */
3758 else if ((h->root.root.type == bfd_link_hash_defined
3759 || h->root.root.type == bfd_link_hash_defweak)
3760 && h->root.root.u.def.section)
3762 sec = h->root.root.u.def.section;
3763 if (sec->output_section)
3764 symbol = (h->root.root.u.def.value
3765 + sec->output_section->vma
3766 + sec->output_offset);
3767 else
3768 symbol = h->root.root.u.def.value;
3770 else if (h->root.root.type == bfd_link_hash_undefweak)
3771 /* We allow relocations against undefined weak symbols, giving
3772 it the value zero, so that you can undefined weak functions
3773 and check to see if they exist by looking at their
3774 addresses. */
3775 symbol = 0;
3776 else if (info->unresolved_syms_in_objects == RM_IGNORE
3777 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3778 symbol = 0;
3779 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
3780 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
3782 /* If this is a dynamic link, we should have created a
3783 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
3784 in in _bfd_mips_elf_create_dynamic_sections.
3785 Otherwise, we should define the symbol with a value of 0.
3786 FIXME: It should probably get into the symbol table
3787 somehow as well. */
3788 BFD_ASSERT (! info->shared);
3789 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
3790 symbol = 0;
3792 else
3794 if (! ((*info->callbacks->undefined_symbol)
3795 (info, h->root.root.root.string, input_bfd,
3796 input_section, relocation->r_offset,
3797 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
3798 || ELF_ST_VISIBILITY (h->root.other))))
3799 return bfd_reloc_undefined;
3800 symbol = 0;
3803 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
3806 /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
3807 need to redirect the call to the stub, unless we're already *in*
3808 a stub. */
3809 if (r_type != R_MIPS16_26 && !info->relocatable
3810 && ((h != NULL && h->fn_stub != NULL)
3811 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
3812 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
3813 && !mips_elf_stub_section_p (input_bfd, input_section))
3815 /* This is a 32- or 64-bit call to a 16-bit function. We should
3816 have already noticed that we were going to need the
3817 stub. */
3818 if (local_p)
3819 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
3820 else
3822 BFD_ASSERT (h->need_fn_stub);
3823 sec = h->fn_stub;
3826 symbol = sec->output_section->vma + sec->output_offset;
3828 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
3829 need to redirect the call to the stub. */
3830 else if (r_type == R_MIPS16_26 && !info->relocatable
3831 && h != NULL
3832 && (h->call_stub != NULL || h->call_fp_stub != NULL)
3833 && !target_is_16_bit_code_p)
3835 /* If both call_stub and call_fp_stub are defined, we can figure
3836 out which one to use by seeing which one appears in the input
3837 file. */
3838 if (h->call_stub != NULL && h->call_fp_stub != NULL)
3840 asection *o;
3842 sec = NULL;
3843 for (o = input_bfd->sections; o != NULL; o = o->next)
3845 if (strncmp (bfd_get_section_name (input_bfd, o),
3846 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
3848 sec = h->call_fp_stub;
3849 break;
3852 if (sec == NULL)
3853 sec = h->call_stub;
3855 else if (h->call_stub != NULL)
3856 sec = h->call_stub;
3857 else
3858 sec = h->call_fp_stub;
3860 BFD_ASSERT (sec->size > 0);
3861 symbol = sec->output_section->vma + sec->output_offset;
3864 /* Calls from 16-bit code to 32-bit code and vice versa require the
3865 special jalx instruction. */
3866 *require_jalxp = (!info->relocatable
3867 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
3868 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
3870 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3871 local_sections, TRUE);
3873 /* If we haven't already determined the GOT offset, or the GP value,
3874 and we're going to need it, get it now. */
3875 switch (r_type)
3877 case R_MIPS_GOT_PAGE:
3878 case R_MIPS_GOT_OFST:
3879 /* We need to decay to GOT_DISP/addend if the symbol doesn't
3880 bind locally. */
3881 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
3882 if (local_p || r_type == R_MIPS_GOT_OFST)
3883 break;
3884 /* Fall through. */
3886 case R_MIPS_CALL16:
3887 case R_MIPS_GOT16:
3888 case R_MIPS_GOT_DISP:
3889 case R_MIPS_GOT_HI16:
3890 case R_MIPS_CALL_HI16:
3891 case R_MIPS_GOT_LO16:
3892 case R_MIPS_CALL_LO16:
3893 case R_MIPS_TLS_GD:
3894 case R_MIPS_TLS_GOTTPREL:
3895 case R_MIPS_TLS_LDM:
3896 /* Find the index into the GOT where this value is located. */
3897 if (r_type == R_MIPS_TLS_LDM)
3899 g = mips_elf_local_got_index (abfd, input_bfd, info, 0, 0, NULL,
3900 r_type);
3901 if (g == MINUS_ONE)
3902 return bfd_reloc_outofrange;
3904 else if (!local_p)
3906 /* GOT_PAGE may take a non-zero addend, that is ignored in a
3907 GOT_PAGE relocation that decays to GOT_DISP because the
3908 symbol turns out to be global. The addend is then added
3909 as GOT_OFST. */
3910 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
3911 g = mips_elf_global_got_index (elf_hash_table (info)->dynobj,
3912 input_bfd,
3913 (struct elf_link_hash_entry *) h,
3914 r_type, info);
3915 if (h->tls_type == GOT_NORMAL
3916 && (! elf_hash_table(info)->dynamic_sections_created
3917 || (info->shared
3918 && (info->symbolic || h->root.dynindx == -1)
3919 && h->root.def_regular)))
3921 /* This is a static link or a -Bsymbolic link. The
3922 symbol is defined locally, or was forced to be local.
3923 We must initialize this entry in the GOT. */
3924 bfd *tmpbfd = elf_hash_table (info)->dynobj;
3925 asection *sgot = mips_elf_got_section (tmpbfd, FALSE);
3926 MIPS_ELF_PUT_WORD (tmpbfd, symbol, sgot->contents + g);
3929 else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
3930 /* There's no need to create a local GOT entry here; the
3931 calculation for a local GOT16 entry does not involve G. */
3932 break;
3933 else
3935 g = mips_elf_local_got_index (abfd, input_bfd,
3936 info, symbol + addend, r_symndx, h,
3937 r_type);
3938 if (g == MINUS_ONE)
3939 return bfd_reloc_outofrange;
3942 /* Convert GOT indices to actual offsets. */
3943 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
3944 abfd, input_bfd, g);
3945 break;
3947 case R_MIPS_HI16:
3948 case R_MIPS_LO16:
3949 case R_MIPS_GPREL16:
3950 case R_MIPS_GPREL32:
3951 case R_MIPS_LITERAL:
3952 case R_MIPS16_HI16:
3953 case R_MIPS16_LO16:
3954 case R_MIPS16_GPREL:
3955 gp0 = _bfd_get_gp_value (input_bfd);
3956 gp = _bfd_get_gp_value (abfd);
3957 if (elf_hash_table (info)->dynobj)
3958 gp += mips_elf_adjust_gp (abfd,
3959 mips_elf_got_info
3960 (elf_hash_table (info)->dynobj, NULL),
3961 input_bfd);
3962 break;
3964 default:
3965 break;
3968 if (gnu_local_gp_p)
3969 symbol = gp;
3971 /* Figure out what kind of relocation is being performed. */
3972 switch (r_type)
3974 case R_MIPS_NONE:
3975 return bfd_reloc_continue;
3977 case R_MIPS_16:
3978 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
3979 overflowed_p = mips_elf_overflow_p (value, 16);
3980 break;
3982 case R_MIPS_32:
3983 case R_MIPS_REL32:
3984 case R_MIPS_64:
3985 if ((info->shared
3986 || (elf_hash_table (info)->dynamic_sections_created
3987 && h != NULL
3988 && h->root.def_dynamic
3989 && !h->root.def_regular))
3990 && r_symndx != 0
3991 && (input_section->flags & SEC_ALLOC) != 0)
3993 /* If we're creating a shared library, or this relocation is
3994 against a symbol in a shared library, then we can't know
3995 where the symbol will end up. So, we create a relocation
3996 record in the output, and leave the job up to the dynamic
3997 linker. */
3998 value = addend;
3999 if (!mips_elf_create_dynamic_relocation (abfd,
4000 info,
4001 relocation,
4003 sec,
4004 symbol,
4005 &value,
4006 input_section))
4007 return bfd_reloc_undefined;
4009 else
4011 if (r_type != R_MIPS_REL32)
4012 value = symbol + addend;
4013 else
4014 value = addend;
4016 value &= howto->dst_mask;
4017 break;
4019 case R_MIPS_PC32:
4020 value = symbol + addend - p;
4021 value &= howto->dst_mask;
4022 break;
4024 case R_MIPS_GNU_REL16_S2:
4025 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4026 overflowed_p = mips_elf_overflow_p (value, 18);
4027 value = (value >> 2) & howto->dst_mask;
4028 break;
4030 case R_MIPS16_26:
4031 /* The calculation for R_MIPS16_26 is just the same as for an
4032 R_MIPS_26. It's only the storage of the relocated field into
4033 the output file that's different. That's handled in
4034 mips_elf_perform_relocation. So, we just fall through to the
4035 R_MIPS_26 case here. */
4036 case R_MIPS_26:
4037 if (local_p)
4038 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4039 else
4041 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4042 if (h->root.root.type != bfd_link_hash_undefweak)
4043 overflowed_p = (value >> 26) != ((p + 4) >> 28);
4045 value &= howto->dst_mask;
4046 break;
4048 case R_MIPS_TLS_DTPREL_HI16:
4049 value = (mips_elf_high (addend + symbol - dtprel_base (info))
4050 & howto->dst_mask);
4051 break;
4053 case R_MIPS_TLS_DTPREL_LO16:
4054 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4055 break;
4057 case R_MIPS_TLS_TPREL_HI16:
4058 value = (mips_elf_high (addend + symbol - tprel_base (info))
4059 & howto->dst_mask);
4060 break;
4062 case R_MIPS_TLS_TPREL_LO16:
4063 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4064 break;
4066 case R_MIPS_HI16:
4067 case R_MIPS16_HI16:
4068 if (!gp_disp_p)
4070 value = mips_elf_high (addend + symbol);
4071 value &= howto->dst_mask;
4073 else
4075 /* For MIPS16 ABI code we generate this sequence
4076 0: li $v0,%hi(_gp_disp)
4077 4: addiupc $v1,%lo(_gp_disp)
4078 8: sll $v0,16
4079 12: addu $v0,$v1
4080 14: move $gp,$v0
4081 So the offsets of hi and lo relocs are the same, but the
4082 $pc is four higher than $t9 would be, so reduce
4083 both reloc addends by 4. */
4084 if (r_type == R_MIPS16_HI16)
4085 value = mips_elf_high (addend + gp - p - 4);
4086 else
4087 value = mips_elf_high (addend + gp - p);
4088 overflowed_p = mips_elf_overflow_p (value, 16);
4090 break;
4092 case R_MIPS_LO16:
4093 case R_MIPS16_LO16:
4094 if (!gp_disp_p)
4095 value = (symbol + addend) & howto->dst_mask;
4096 else
4098 /* See the comment for R_MIPS16_HI16 above for the reason
4099 for this conditional. */
4100 if (r_type == R_MIPS16_LO16)
4101 value = addend + gp - p;
4102 else
4103 value = addend + gp - p + 4;
4104 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4105 for overflow. But, on, say, IRIX5, relocations against
4106 _gp_disp are normally generated from the .cpload
4107 pseudo-op. It generates code that normally looks like
4108 this:
4110 lui $gp,%hi(_gp_disp)
4111 addiu $gp,$gp,%lo(_gp_disp)
4112 addu $gp,$gp,$t9
4114 Here $t9 holds the address of the function being called,
4115 as required by the MIPS ELF ABI. The R_MIPS_LO16
4116 relocation can easily overflow in this situation, but the
4117 R_MIPS_HI16 relocation will handle the overflow.
4118 Therefore, we consider this a bug in the MIPS ABI, and do
4119 not check for overflow here. */
4121 break;
4123 case R_MIPS_LITERAL:
4124 /* Because we don't merge literal sections, we can handle this
4125 just like R_MIPS_GPREL16. In the long run, we should merge
4126 shared literals, and then we will need to additional work
4127 here. */
4129 /* Fall through. */
4131 case R_MIPS16_GPREL:
4132 /* The R_MIPS16_GPREL performs the same calculation as
4133 R_MIPS_GPREL16, but stores the relocated bits in a different
4134 order. We don't need to do anything special here; the
4135 differences are handled in mips_elf_perform_relocation. */
4136 case R_MIPS_GPREL16:
4137 /* Only sign-extend the addend if it was extracted from the
4138 instruction. If the addend was separate, leave it alone,
4139 otherwise we may lose significant bits. */
4140 if (howto->partial_inplace)
4141 addend = _bfd_mips_elf_sign_extend (addend, 16);
4142 value = symbol + addend - gp;
4143 /* If the symbol was local, any earlier relocatable links will
4144 have adjusted its addend with the gp offset, so compensate
4145 for that now. Don't do it for symbols forced local in this
4146 link, though, since they won't have had the gp offset applied
4147 to them before. */
4148 if (was_local_p)
4149 value += gp0;
4150 overflowed_p = mips_elf_overflow_p (value, 16);
4151 break;
4153 case R_MIPS_GOT16:
4154 case R_MIPS_CALL16:
4155 if (local_p)
4157 bfd_boolean forced;
4159 /* The special case is when the symbol is forced to be local. We
4160 need the full address in the GOT since no R_MIPS_LO16 relocation
4161 follows. */
4162 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4163 local_sections, FALSE);
4164 value = mips_elf_got16_entry (abfd, input_bfd, info,
4165 symbol + addend, forced);
4166 if (value == MINUS_ONE)
4167 return bfd_reloc_outofrange;
4168 value
4169 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
4170 abfd, input_bfd, value);
4171 overflowed_p = mips_elf_overflow_p (value, 16);
4172 break;
4175 /* Fall through. */
4177 case R_MIPS_TLS_GD:
4178 case R_MIPS_TLS_GOTTPREL:
4179 case R_MIPS_TLS_LDM:
4180 case R_MIPS_GOT_DISP:
4181 got_disp:
4182 value = g;
4183 overflowed_p = mips_elf_overflow_p (value, 16);
4184 break;
4186 case R_MIPS_GPREL32:
4187 value = (addend + symbol + gp0 - gp);
4188 if (!save_addend)
4189 value &= howto->dst_mask;
4190 break;
4192 case R_MIPS_PC16:
4193 value = _bfd_mips_elf_sign_extend (addend, 16) + symbol - p;
4194 overflowed_p = mips_elf_overflow_p (value, 16);
4195 break;
4197 case R_MIPS_GOT_HI16:
4198 case R_MIPS_CALL_HI16:
4199 /* We're allowed to handle these two relocations identically.
4200 The dynamic linker is allowed to handle the CALL relocations
4201 differently by creating a lazy evaluation stub. */
4202 value = g;
4203 value = mips_elf_high (value);
4204 value &= howto->dst_mask;
4205 break;
4207 case R_MIPS_GOT_LO16:
4208 case R_MIPS_CALL_LO16:
4209 value = g & howto->dst_mask;
4210 break;
4212 case R_MIPS_GOT_PAGE:
4213 /* GOT_PAGE relocations that reference non-local symbols decay
4214 to GOT_DISP. The corresponding GOT_OFST relocation decays to
4215 0. */
4216 if (! local_p)
4217 goto got_disp;
4218 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4219 if (value == MINUS_ONE)
4220 return bfd_reloc_outofrange;
4221 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
4222 abfd, input_bfd, value);
4223 overflowed_p = mips_elf_overflow_p (value, 16);
4224 break;
4226 case R_MIPS_GOT_OFST:
4227 if (local_p)
4228 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4229 else
4230 value = addend;
4231 overflowed_p = mips_elf_overflow_p (value, 16);
4232 break;
4234 case R_MIPS_SUB:
4235 value = symbol - addend;
4236 value &= howto->dst_mask;
4237 break;
4239 case R_MIPS_HIGHER:
4240 value = mips_elf_higher (addend + symbol);
4241 value &= howto->dst_mask;
4242 break;
4244 case R_MIPS_HIGHEST:
4245 value = mips_elf_highest (addend + symbol);
4246 value &= howto->dst_mask;
4247 break;
4249 case R_MIPS_SCN_DISP:
4250 value = symbol + addend - sec->output_offset;
4251 value &= howto->dst_mask;
4252 break;
4254 case R_MIPS_JALR:
4255 /* This relocation is only a hint. In some cases, we optimize
4256 it into a bal instruction. But we don't try to optimize
4257 branches to the PLT; that will wind up wasting time. */
4258 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4259 return bfd_reloc_continue;
4260 value = symbol + addend;
4261 break;
4263 case R_MIPS_PJUMP:
4264 case R_MIPS_GNU_VTINHERIT:
4265 case R_MIPS_GNU_VTENTRY:
4266 /* We don't do anything with these at present. */
4267 return bfd_reloc_continue;
4269 default:
4270 /* An unrecognized relocation type. */
4271 return bfd_reloc_notsupported;
4274 /* Store the VALUE for our caller. */
4275 *valuep = value;
4276 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4279 /* Obtain the field relocated by RELOCATION. */
4281 static bfd_vma
4282 mips_elf_obtain_contents (reloc_howto_type *howto,
4283 const Elf_Internal_Rela *relocation,
4284 bfd *input_bfd, bfd_byte *contents)
4286 bfd_vma x;
4287 bfd_byte *location = contents + relocation->r_offset;
4289 /* Obtain the bytes. */
4290 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4292 return x;
4295 /* It has been determined that the result of the RELOCATION is the
4296 VALUE. Use HOWTO to place VALUE into the output file at the
4297 appropriate position. The SECTION is the section to which the
4298 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
4299 for the relocation must be either JAL or JALX, and it is
4300 unconditionally converted to JALX.
4302 Returns FALSE if anything goes wrong. */
4304 static bfd_boolean
4305 mips_elf_perform_relocation (struct bfd_link_info *info,
4306 reloc_howto_type *howto,
4307 const Elf_Internal_Rela *relocation,
4308 bfd_vma value, bfd *input_bfd,
4309 asection *input_section, bfd_byte *contents,
4310 bfd_boolean require_jalx)
4312 bfd_vma x;
4313 bfd_byte *location;
4314 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4316 /* Figure out where the relocation is occurring. */
4317 location = contents + relocation->r_offset;
4319 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4321 /* Obtain the current value. */
4322 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4324 /* Clear the field we are setting. */
4325 x &= ~howto->dst_mask;
4327 /* Set the field. */
4328 x |= (value & howto->dst_mask);
4330 /* If required, turn JAL into JALX. */
4331 if (require_jalx)
4333 bfd_boolean ok;
4334 bfd_vma opcode = x >> 26;
4335 bfd_vma jalx_opcode;
4337 /* Check to see if the opcode is already JAL or JALX. */
4338 if (r_type == R_MIPS16_26)
4340 ok = ((opcode == 0x6) || (opcode == 0x7));
4341 jalx_opcode = 0x7;
4343 else
4345 ok = ((opcode == 0x3) || (opcode == 0x1d));
4346 jalx_opcode = 0x1d;
4349 /* If the opcode is not JAL or JALX, there's a problem. */
4350 if (!ok)
4352 (*_bfd_error_handler)
4353 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
4354 input_bfd,
4355 input_section,
4356 (unsigned long) relocation->r_offset);
4357 bfd_set_error (bfd_error_bad_value);
4358 return FALSE;
4361 /* Make this the JALX opcode. */
4362 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
4365 /* On the RM9000, bal is faster than jal, because bal uses branch
4366 prediction hardware. If we are linking for the RM9000, and we
4367 see jal, and bal fits, use it instead. Note that this
4368 transformation should be safe for all architectures. */
4369 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
4370 && !info->relocatable
4371 && !require_jalx
4372 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
4373 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
4375 bfd_vma addr;
4376 bfd_vma dest;
4377 bfd_signed_vma off;
4379 addr = (input_section->output_section->vma
4380 + input_section->output_offset
4381 + relocation->r_offset
4382 + 4);
4383 if (r_type == R_MIPS_26)
4384 dest = (value << 2) | ((addr >> 28) << 28);
4385 else
4386 dest = value;
4387 off = dest - addr;
4388 if (off <= 0x1ffff && off >= -0x20000)
4389 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
4392 /* Put the value into the output. */
4393 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
4395 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
4396 location);
4398 return TRUE;
4401 /* Returns TRUE if SECTION is a MIPS16 stub section. */
4403 static bfd_boolean
4404 mips_elf_stub_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
4406 const char *name = bfd_get_section_name (abfd, section);
4408 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
4409 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4410 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
4413 /* Add room for N relocations to the .rel.dyn section in ABFD. */
4415 static void
4416 mips_elf_allocate_dynamic_relocations (bfd *abfd, unsigned int n)
4418 asection *s;
4420 s = mips_elf_rel_dyn_section (abfd, FALSE);
4421 BFD_ASSERT (s != NULL);
4423 if (s->size == 0)
4425 /* Make room for a null element. */
4426 s->size += MIPS_ELF_REL_SIZE (abfd);
4427 ++s->reloc_count;
4429 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4432 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
4433 is the original relocation, which is now being transformed into a
4434 dynamic relocation. The ADDENDP is adjusted if necessary; the
4435 caller should store the result in place of the original addend. */
4437 static bfd_boolean
4438 mips_elf_create_dynamic_relocation (bfd *output_bfd,
4439 struct bfd_link_info *info,
4440 const Elf_Internal_Rela *rel,
4441 struct mips_elf_link_hash_entry *h,
4442 asection *sec, bfd_vma symbol,
4443 bfd_vma *addendp, asection *input_section)
4445 Elf_Internal_Rela outrel[3];
4446 asection *sreloc;
4447 bfd *dynobj;
4448 int r_type;
4449 long indx;
4450 bfd_boolean defined_p;
4452 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
4453 dynobj = elf_hash_table (info)->dynobj;
4454 sreloc = mips_elf_rel_dyn_section (dynobj, FALSE);
4455 BFD_ASSERT (sreloc != NULL);
4456 BFD_ASSERT (sreloc->contents != NULL);
4457 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
4458 < sreloc->size);
4460 outrel[0].r_offset =
4461 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
4462 outrel[1].r_offset =
4463 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
4464 outrel[2].r_offset =
4465 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
4467 if (outrel[0].r_offset == MINUS_ONE)
4468 /* The relocation field has been deleted. */
4469 return TRUE;
4471 if (outrel[0].r_offset == MINUS_TWO)
4473 /* The relocation field has been converted into a relative value of
4474 some sort. Functions like _bfd_elf_write_section_eh_frame expect
4475 the field to be fully relocated, so add in the symbol's value. */
4476 *addendp += symbol;
4477 return TRUE;
4480 /* We must now calculate the dynamic symbol table index to use
4481 in the relocation. */
4482 if (h != NULL
4483 && (! info->symbolic || !h->root.def_regular)
4484 /* h->root.dynindx may be -1 if this symbol was marked to
4485 become local. */
4486 && h->root.dynindx != -1)
4488 indx = h->root.dynindx;
4489 if (SGI_COMPAT (output_bfd))
4490 defined_p = h->root.def_regular;
4491 else
4492 /* ??? glibc's ld.so just adds the final GOT entry to the
4493 relocation field. It therefore treats relocs against
4494 defined symbols in the same way as relocs against
4495 undefined symbols. */
4496 defined_p = FALSE;
4498 else
4500 if (sec != NULL && bfd_is_abs_section (sec))
4501 indx = 0;
4502 else if (sec == NULL || sec->owner == NULL)
4504 bfd_set_error (bfd_error_bad_value);
4505 return FALSE;
4507 else
4509 indx = elf_section_data (sec->output_section)->dynindx;
4510 if (indx == 0)
4511 abort ();
4514 /* Instead of generating a relocation using the section
4515 symbol, we may as well make it a fully relative
4516 relocation. We want to avoid generating relocations to
4517 local symbols because we used to generate them
4518 incorrectly, without adding the original symbol value,
4519 which is mandated by the ABI for section symbols. In
4520 order to give dynamic loaders and applications time to
4521 phase out the incorrect use, we refrain from emitting
4522 section-relative relocations. It's not like they're
4523 useful, after all. This should be a bit more efficient
4524 as well. */
4525 /* ??? Although this behavior is compatible with glibc's ld.so,
4526 the ABI says that relocations against STN_UNDEF should have
4527 a symbol value of 0. Irix rld honors this, so relocations
4528 against STN_UNDEF have no effect. */
4529 if (!SGI_COMPAT (output_bfd))
4530 indx = 0;
4531 defined_p = TRUE;
4534 /* If the relocation was previously an absolute relocation and
4535 this symbol will not be referred to by the relocation, we must
4536 adjust it by the value we give it in the dynamic symbol table.
4537 Otherwise leave the job up to the dynamic linker. */
4538 if (defined_p && r_type != R_MIPS_REL32)
4539 *addendp += symbol;
4541 /* The relocation is always an REL32 relocation because we don't
4542 know where the shared library will wind up at load-time. */
4543 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
4544 R_MIPS_REL32);
4545 /* For strict adherence to the ABI specification, we should
4546 generate a R_MIPS_64 relocation record by itself before the
4547 _REL32/_64 record as well, such that the addend is read in as
4548 a 64-bit value (REL32 is a 32-bit relocation, after all).
4549 However, since none of the existing ELF64 MIPS dynamic
4550 loaders seems to care, we don't waste space with these
4551 artificial relocations. If this turns out to not be true,
4552 mips_elf_allocate_dynamic_relocation() should be tweaked so
4553 as to make room for a pair of dynamic relocations per
4554 invocation if ABI_64_P, and here we should generate an
4555 additional relocation record with R_MIPS_64 by itself for a
4556 NULL symbol before this relocation record. */
4557 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
4558 ABI_64_P (output_bfd)
4559 ? R_MIPS_64
4560 : R_MIPS_NONE);
4561 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
4563 /* Adjust the output offset of the relocation to reference the
4564 correct location in the output file. */
4565 outrel[0].r_offset += (input_section->output_section->vma
4566 + input_section->output_offset);
4567 outrel[1].r_offset += (input_section->output_section->vma
4568 + input_section->output_offset);
4569 outrel[2].r_offset += (input_section->output_section->vma
4570 + input_section->output_offset);
4572 /* Put the relocation back out. We have to use the special
4573 relocation outputter in the 64-bit case since the 64-bit
4574 relocation format is non-standard. */
4575 if (ABI_64_P (output_bfd))
4577 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
4578 (output_bfd, &outrel[0],
4579 (sreloc->contents
4580 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
4582 else
4583 bfd_elf32_swap_reloc_out
4584 (output_bfd, &outrel[0],
4585 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
4587 /* We've now added another relocation. */
4588 ++sreloc->reloc_count;
4590 /* Make sure the output section is writable. The dynamic linker
4591 will be writing to it. */
4592 elf_section_data (input_section->output_section)->this_hdr.sh_flags
4593 |= SHF_WRITE;
4595 /* On IRIX5, make an entry of compact relocation info. */
4596 if (IRIX_COMPAT (output_bfd) == ict_irix5)
4598 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4599 bfd_byte *cr;
4601 if (scpt)
4603 Elf32_crinfo cptrel;
4605 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4606 cptrel.vaddr = (rel->r_offset
4607 + input_section->output_section->vma
4608 + input_section->output_offset);
4609 if (r_type == R_MIPS_REL32)
4610 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4611 else
4612 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4613 mips_elf_set_cr_dist2to (cptrel, 0);
4614 cptrel.konst = *addendp;
4616 cr = (scpt->contents
4617 + sizeof (Elf32_External_compact_rel));
4618 mips_elf_set_cr_relvaddr (cptrel, 0);
4619 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4620 ((Elf32_External_crinfo *) cr
4621 + scpt->reloc_count));
4622 ++scpt->reloc_count;
4626 return TRUE;
4629 /* Return the MACH for a MIPS e_flags value. */
4631 unsigned long
4632 _bfd_elf_mips_mach (flagword flags)
4634 switch (flags & EF_MIPS_MACH)
4636 case E_MIPS_MACH_3900:
4637 return bfd_mach_mips3900;
4639 case E_MIPS_MACH_4010:
4640 return bfd_mach_mips4010;
4642 case E_MIPS_MACH_4100:
4643 return bfd_mach_mips4100;
4645 case E_MIPS_MACH_4111:
4646 return bfd_mach_mips4111;
4648 case E_MIPS_MACH_4120:
4649 return bfd_mach_mips4120;
4651 case E_MIPS_MACH_4650:
4652 return bfd_mach_mips4650;
4654 case E_MIPS_MACH_5400:
4655 return bfd_mach_mips5400;
4657 case E_MIPS_MACH_5500:
4658 return bfd_mach_mips5500;
4660 case E_MIPS_MACH_9000:
4661 return bfd_mach_mips9000;
4663 case E_MIPS_MACH_SB1:
4664 return bfd_mach_mips_sb1;
4666 default:
4667 switch (flags & EF_MIPS_ARCH)
4669 default:
4670 case E_MIPS_ARCH_1:
4671 return bfd_mach_mips3000;
4672 break;
4674 case E_MIPS_ARCH_2:
4675 return bfd_mach_mips6000;
4676 break;
4678 case E_MIPS_ARCH_3:
4679 return bfd_mach_mips4000;
4680 break;
4682 case E_MIPS_ARCH_4:
4683 return bfd_mach_mips8000;
4684 break;
4686 case E_MIPS_ARCH_5:
4687 return bfd_mach_mips5;
4688 break;
4690 case E_MIPS_ARCH_32:
4691 return bfd_mach_mipsisa32;
4692 break;
4694 case E_MIPS_ARCH_64:
4695 return bfd_mach_mipsisa64;
4696 break;
4698 case E_MIPS_ARCH_32R2:
4699 return bfd_mach_mipsisa32r2;
4700 break;
4702 case E_MIPS_ARCH_64R2:
4703 return bfd_mach_mipsisa64r2;
4704 break;
4708 return 0;
4711 /* Return printable name for ABI. */
4713 static INLINE char *
4714 elf_mips_abi_name (bfd *abfd)
4716 flagword flags;
4718 flags = elf_elfheader (abfd)->e_flags;
4719 switch (flags & EF_MIPS_ABI)
4721 case 0:
4722 if (ABI_N32_P (abfd))
4723 return "N32";
4724 else if (ABI_64_P (abfd))
4725 return "64";
4726 else
4727 return "none";
4728 case E_MIPS_ABI_O32:
4729 return "O32";
4730 case E_MIPS_ABI_O64:
4731 return "O64";
4732 case E_MIPS_ABI_EABI32:
4733 return "EABI32";
4734 case E_MIPS_ABI_EABI64:
4735 return "EABI64";
4736 default:
4737 return "unknown abi";
4741 /* MIPS ELF uses two common sections. One is the usual one, and the
4742 other is for small objects. All the small objects are kept
4743 together, and then referenced via the gp pointer, which yields
4744 faster assembler code. This is what we use for the small common
4745 section. This approach is copied from ecoff.c. */
4746 static asection mips_elf_scom_section;
4747 static asymbol mips_elf_scom_symbol;
4748 static asymbol *mips_elf_scom_symbol_ptr;
4750 /* MIPS ELF also uses an acommon section, which represents an
4751 allocated common symbol which may be overridden by a
4752 definition in a shared library. */
4753 static asection mips_elf_acom_section;
4754 static asymbol mips_elf_acom_symbol;
4755 static asymbol *mips_elf_acom_symbol_ptr;
4757 /* Handle the special MIPS section numbers that a symbol may use.
4758 This is used for both the 32-bit and the 64-bit ABI. */
4760 void
4761 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
4763 elf_symbol_type *elfsym;
4765 elfsym = (elf_symbol_type *) asym;
4766 switch (elfsym->internal_elf_sym.st_shndx)
4768 case SHN_MIPS_ACOMMON:
4769 /* This section is used in a dynamically linked executable file.
4770 It is an allocated common section. The dynamic linker can
4771 either resolve these symbols to something in a shared
4772 library, or it can just leave them here. For our purposes,
4773 we can consider these symbols to be in a new section. */
4774 if (mips_elf_acom_section.name == NULL)
4776 /* Initialize the acommon section. */
4777 mips_elf_acom_section.name = ".acommon";
4778 mips_elf_acom_section.flags = SEC_ALLOC;
4779 mips_elf_acom_section.output_section = &mips_elf_acom_section;
4780 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
4781 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
4782 mips_elf_acom_symbol.name = ".acommon";
4783 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
4784 mips_elf_acom_symbol.section = &mips_elf_acom_section;
4785 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
4787 asym->section = &mips_elf_acom_section;
4788 break;
4790 case SHN_COMMON:
4791 /* Common symbols less than the GP size are automatically
4792 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
4793 if (asym->value > elf_gp_size (abfd)
4794 || IRIX_COMPAT (abfd) == ict_irix6)
4795 break;
4796 /* Fall through. */
4797 case SHN_MIPS_SCOMMON:
4798 if (mips_elf_scom_section.name == NULL)
4800 /* Initialize the small common section. */
4801 mips_elf_scom_section.name = ".scommon";
4802 mips_elf_scom_section.flags = SEC_IS_COMMON;
4803 mips_elf_scom_section.output_section = &mips_elf_scom_section;
4804 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
4805 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
4806 mips_elf_scom_symbol.name = ".scommon";
4807 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
4808 mips_elf_scom_symbol.section = &mips_elf_scom_section;
4809 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
4811 asym->section = &mips_elf_scom_section;
4812 asym->value = elfsym->internal_elf_sym.st_size;
4813 break;
4815 case SHN_MIPS_SUNDEFINED:
4816 asym->section = bfd_und_section_ptr;
4817 break;
4819 case SHN_MIPS_TEXT:
4821 asection *section = bfd_get_section_by_name (abfd, ".text");
4823 BFD_ASSERT (SGI_COMPAT (abfd));
4824 if (section != NULL)
4826 asym->section = section;
4827 /* MIPS_TEXT is a bit special, the address is not an offset
4828 to the base of the .text section. So substract the section
4829 base address to make it an offset. */
4830 asym->value -= section->vma;
4833 break;
4835 case SHN_MIPS_DATA:
4837 asection *section = bfd_get_section_by_name (abfd, ".data");
4839 BFD_ASSERT (SGI_COMPAT (abfd));
4840 if (section != NULL)
4842 asym->section = section;
4843 /* MIPS_DATA is a bit special, the address is not an offset
4844 to the base of the .data section. So substract the section
4845 base address to make it an offset. */
4846 asym->value -= section->vma;
4849 break;
4853 /* Implement elf_backend_eh_frame_address_size. This differs from
4854 the default in the way it handles EABI64.
4856 EABI64 was originally specified as an LP64 ABI, and that is what
4857 -mabi=eabi normally gives on a 64-bit target. However, gcc has
4858 historically accepted the combination of -mabi=eabi and -mlong32,
4859 and this ILP32 variation has become semi-official over time.
4860 Both forms use elf32 and have pointer-sized FDE addresses.
4862 If an EABI object was generated by GCC 4.0 or above, it will have
4863 an empty .gcc_compiled_longXX section, where XX is the size of longs
4864 in bits. Unfortunately, ILP32 objects generated by earlier compilers
4865 have no special marking to distinguish them from LP64 objects.
4867 We don't want users of the official LP64 ABI to be punished for the
4868 existence of the ILP32 variant, but at the same time, we don't want
4869 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
4870 We therefore take the following approach:
4872 - If ABFD contains a .gcc_compiled_longXX section, use it to
4873 determine the pointer size.
4875 - Otherwise check the type of the first relocation. Assume that
4876 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
4878 - Otherwise punt.
4880 The second check is enough to detect LP64 objects generated by pre-4.0
4881 compilers because, in the kind of output generated by those compilers,
4882 the first relocation will be associated with either a CIE personality
4883 routine or an FDE start address. Furthermore, the compilers never
4884 used a special (non-pointer) encoding for this ABI.
4886 Checking the relocation type should also be safe because there is no
4887 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
4888 did so. */
4890 unsigned int
4891 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
4893 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
4894 return 8;
4895 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
4897 bfd_boolean long32_p, long64_p;
4899 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
4900 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
4901 if (long32_p && long64_p)
4902 return 0;
4903 if (long32_p)
4904 return 4;
4905 if (long64_p)
4906 return 8;
4908 if (sec->reloc_count > 0
4909 && elf_section_data (sec)->relocs != NULL
4910 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
4911 == R_MIPS_64))
4912 return 8;
4914 return 0;
4916 return 4;
4919 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
4920 relocations against two unnamed section symbols to resolve to the
4921 same address. For example, if we have code like:
4923 lw $4,%got_disp(.data)($gp)
4924 lw $25,%got_disp(.text)($gp)
4925 jalr $25
4927 then the linker will resolve both relocations to .data and the program
4928 will jump there rather than to .text.
4930 We can work around this problem by giving names to local section symbols.
4931 This is also what the MIPSpro tools do. */
4933 bfd_boolean
4934 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
4936 return SGI_COMPAT (abfd);
4939 /* Work over a section just before writing it out. This routine is
4940 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
4941 sections that need the SHF_MIPS_GPREL flag by name; there has to be
4942 a better way. */
4944 bfd_boolean
4945 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
4947 if (hdr->sh_type == SHT_MIPS_REGINFO
4948 && hdr->sh_size > 0)
4950 bfd_byte buf[4];
4952 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
4953 BFD_ASSERT (hdr->contents == NULL);
4955 if (bfd_seek (abfd,
4956 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
4957 SEEK_SET) != 0)
4958 return FALSE;
4959 H_PUT_32 (abfd, elf_gp (abfd), buf);
4960 if (bfd_bwrite (buf, 4, abfd) != 4)
4961 return FALSE;
4964 if (hdr->sh_type == SHT_MIPS_OPTIONS
4965 && hdr->bfd_section != NULL
4966 && mips_elf_section_data (hdr->bfd_section) != NULL
4967 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
4969 bfd_byte *contents, *l, *lend;
4971 /* We stored the section contents in the tdata field in the
4972 set_section_contents routine. We save the section contents
4973 so that we don't have to read them again.
4974 At this point we know that elf_gp is set, so we can look
4975 through the section contents to see if there is an
4976 ODK_REGINFO structure. */
4978 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
4979 l = contents;
4980 lend = contents + hdr->sh_size;
4981 while (l + sizeof (Elf_External_Options) <= lend)
4983 Elf_Internal_Options intopt;
4985 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
4986 &intopt);
4987 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
4989 bfd_byte buf[8];
4991 if (bfd_seek (abfd,
4992 (hdr->sh_offset
4993 + (l - contents)
4994 + sizeof (Elf_External_Options)
4995 + (sizeof (Elf64_External_RegInfo) - 8)),
4996 SEEK_SET) != 0)
4997 return FALSE;
4998 H_PUT_64 (abfd, elf_gp (abfd), buf);
4999 if (bfd_bwrite (buf, 8, abfd) != 8)
5000 return FALSE;
5002 else if (intopt.kind == ODK_REGINFO)
5004 bfd_byte buf[4];
5006 if (bfd_seek (abfd,
5007 (hdr->sh_offset
5008 + (l - contents)
5009 + sizeof (Elf_External_Options)
5010 + (sizeof (Elf32_External_RegInfo) - 4)),
5011 SEEK_SET) != 0)
5012 return FALSE;
5013 H_PUT_32 (abfd, elf_gp (abfd), buf);
5014 if (bfd_bwrite (buf, 4, abfd) != 4)
5015 return FALSE;
5017 l += intopt.size;
5021 if (hdr->bfd_section != NULL)
5023 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5025 if (strcmp (name, ".sdata") == 0
5026 || strcmp (name, ".lit8") == 0
5027 || strcmp (name, ".lit4") == 0)
5029 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5030 hdr->sh_type = SHT_PROGBITS;
5032 else if (strcmp (name, ".sbss") == 0)
5034 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5035 hdr->sh_type = SHT_NOBITS;
5037 else if (strcmp (name, ".srdata") == 0)
5039 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5040 hdr->sh_type = SHT_PROGBITS;
5042 else if (strcmp (name, ".compact_rel") == 0)
5044 hdr->sh_flags = 0;
5045 hdr->sh_type = SHT_PROGBITS;
5047 else if (strcmp (name, ".rtproc") == 0)
5049 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5051 unsigned int adjust;
5053 adjust = hdr->sh_size % hdr->sh_addralign;
5054 if (adjust != 0)
5055 hdr->sh_size += hdr->sh_addralign - adjust;
5060 return TRUE;
5063 /* Handle a MIPS specific section when reading an object file. This
5064 is called when elfcode.h finds a section with an unknown type.
5065 This routine supports both the 32-bit and 64-bit ELF ABI.
5067 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5068 how to. */
5070 bfd_boolean
5071 _bfd_mips_elf_section_from_shdr (bfd *abfd,
5072 Elf_Internal_Shdr *hdr,
5073 const char *name,
5074 int shindex)
5076 flagword flags = 0;
5078 /* There ought to be a place to keep ELF backend specific flags, but
5079 at the moment there isn't one. We just keep track of the
5080 sections by their name, instead. Fortunately, the ABI gives
5081 suggested names for all the MIPS specific sections, so we will
5082 probably get away with this. */
5083 switch (hdr->sh_type)
5085 case SHT_MIPS_LIBLIST:
5086 if (strcmp (name, ".liblist") != 0)
5087 return FALSE;
5088 break;
5089 case SHT_MIPS_MSYM:
5090 if (strcmp (name, ".msym") != 0)
5091 return FALSE;
5092 break;
5093 case SHT_MIPS_CONFLICT:
5094 if (strcmp (name, ".conflict") != 0)
5095 return FALSE;
5096 break;
5097 case SHT_MIPS_GPTAB:
5098 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
5099 return FALSE;
5100 break;
5101 case SHT_MIPS_UCODE:
5102 if (strcmp (name, ".ucode") != 0)
5103 return FALSE;
5104 break;
5105 case SHT_MIPS_DEBUG:
5106 if (strcmp (name, ".mdebug") != 0)
5107 return FALSE;
5108 flags = SEC_DEBUGGING;
5109 break;
5110 case SHT_MIPS_REGINFO:
5111 if (strcmp (name, ".reginfo") != 0
5112 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5113 return FALSE;
5114 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5115 break;
5116 case SHT_MIPS_IFACE:
5117 if (strcmp (name, ".MIPS.interfaces") != 0)
5118 return FALSE;
5119 break;
5120 case SHT_MIPS_CONTENT:
5121 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
5122 return FALSE;
5123 break;
5124 case SHT_MIPS_OPTIONS:
5125 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
5126 return FALSE;
5127 break;
5128 case SHT_MIPS_DWARF:
5129 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
5130 return FALSE;
5131 break;
5132 case SHT_MIPS_SYMBOL_LIB:
5133 if (strcmp (name, ".MIPS.symlib") != 0)
5134 return FALSE;
5135 break;
5136 case SHT_MIPS_EVENTS:
5137 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
5138 && strncmp (name, ".MIPS.post_rel",
5139 sizeof ".MIPS.post_rel" - 1) != 0)
5140 return FALSE;
5141 break;
5142 default:
5143 return FALSE;
5146 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5147 return FALSE;
5149 if (flags)
5151 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5152 (bfd_get_section_flags (abfd,
5153 hdr->bfd_section)
5154 | flags)))
5155 return FALSE;
5158 /* FIXME: We should record sh_info for a .gptab section. */
5160 /* For a .reginfo section, set the gp value in the tdata information
5161 from the contents of this section. We need the gp value while
5162 processing relocs, so we just get it now. The .reginfo section
5163 is not used in the 64-bit MIPS ELF ABI. */
5164 if (hdr->sh_type == SHT_MIPS_REGINFO)
5166 Elf32_External_RegInfo ext;
5167 Elf32_RegInfo s;
5169 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5170 &ext, 0, sizeof ext))
5171 return FALSE;
5172 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5173 elf_gp (abfd) = s.ri_gp_value;
5176 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5177 set the gp value based on what we find. We may see both
5178 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5179 they should agree. */
5180 if (hdr->sh_type == SHT_MIPS_OPTIONS)
5182 bfd_byte *contents, *l, *lend;
5184 contents = bfd_malloc (hdr->sh_size);
5185 if (contents == NULL)
5186 return FALSE;
5187 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5188 0, hdr->sh_size))
5190 free (contents);
5191 return FALSE;
5193 l = contents;
5194 lend = contents + hdr->sh_size;
5195 while (l + sizeof (Elf_External_Options) <= lend)
5197 Elf_Internal_Options intopt;
5199 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5200 &intopt);
5201 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5203 Elf64_Internal_RegInfo intreg;
5205 bfd_mips_elf64_swap_reginfo_in
5206 (abfd,
5207 ((Elf64_External_RegInfo *)
5208 (l + sizeof (Elf_External_Options))),
5209 &intreg);
5210 elf_gp (abfd) = intreg.ri_gp_value;
5212 else if (intopt.kind == ODK_REGINFO)
5214 Elf32_RegInfo intreg;
5216 bfd_mips_elf32_swap_reginfo_in
5217 (abfd,
5218 ((Elf32_External_RegInfo *)
5219 (l + sizeof (Elf_External_Options))),
5220 &intreg);
5221 elf_gp (abfd) = intreg.ri_gp_value;
5223 l += intopt.size;
5225 free (contents);
5228 return TRUE;
5231 /* Set the correct type for a MIPS ELF section. We do this by the
5232 section name, which is a hack, but ought to work. This routine is
5233 used by both the 32-bit and the 64-bit ABI. */
5235 bfd_boolean
5236 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
5238 register const char *name;
5240 name = bfd_get_section_name (abfd, sec);
5242 if (strcmp (name, ".liblist") == 0)
5244 hdr->sh_type = SHT_MIPS_LIBLIST;
5245 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
5246 /* The sh_link field is set in final_write_processing. */
5248 else if (strcmp (name, ".conflict") == 0)
5249 hdr->sh_type = SHT_MIPS_CONFLICT;
5250 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
5252 hdr->sh_type = SHT_MIPS_GPTAB;
5253 hdr->sh_entsize = sizeof (Elf32_External_gptab);
5254 /* The sh_info field is set in final_write_processing. */
5256 else if (strcmp (name, ".ucode") == 0)
5257 hdr->sh_type = SHT_MIPS_UCODE;
5258 else if (strcmp (name, ".mdebug") == 0)
5260 hdr->sh_type = SHT_MIPS_DEBUG;
5261 /* In a shared object on IRIX 5.3, the .mdebug section has an
5262 entsize of 0. FIXME: Does this matter? */
5263 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5264 hdr->sh_entsize = 0;
5265 else
5266 hdr->sh_entsize = 1;
5268 else if (strcmp (name, ".reginfo") == 0)
5270 hdr->sh_type = SHT_MIPS_REGINFO;
5271 /* In a shared object on IRIX 5.3, the .reginfo section has an
5272 entsize of 0x18. FIXME: Does this matter? */
5273 if (SGI_COMPAT (abfd))
5275 if ((abfd->flags & DYNAMIC) != 0)
5276 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5277 else
5278 hdr->sh_entsize = 1;
5280 else
5281 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5283 else if (SGI_COMPAT (abfd)
5284 && (strcmp (name, ".hash") == 0
5285 || strcmp (name, ".dynamic") == 0
5286 || strcmp (name, ".dynstr") == 0))
5288 if (SGI_COMPAT (abfd))
5289 hdr->sh_entsize = 0;
5290 #if 0
5291 /* This isn't how the IRIX6 linker behaves. */
5292 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
5293 #endif
5295 else if (strcmp (name, ".got") == 0
5296 || strcmp (name, ".srdata") == 0
5297 || strcmp (name, ".sdata") == 0
5298 || strcmp (name, ".sbss") == 0
5299 || strcmp (name, ".lit4") == 0
5300 || strcmp (name, ".lit8") == 0)
5301 hdr->sh_flags |= SHF_MIPS_GPREL;
5302 else if (strcmp (name, ".MIPS.interfaces") == 0)
5304 hdr->sh_type = SHT_MIPS_IFACE;
5305 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5307 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
5309 hdr->sh_type = SHT_MIPS_CONTENT;
5310 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5311 /* The sh_info field is set in final_write_processing. */
5313 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
5315 hdr->sh_type = SHT_MIPS_OPTIONS;
5316 hdr->sh_entsize = 1;
5317 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5319 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
5320 hdr->sh_type = SHT_MIPS_DWARF;
5321 else if (strcmp (name, ".MIPS.symlib") == 0)
5323 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
5324 /* The sh_link and sh_info fields are set in
5325 final_write_processing. */
5327 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
5328 || strncmp (name, ".MIPS.post_rel",
5329 sizeof ".MIPS.post_rel" - 1) == 0)
5331 hdr->sh_type = SHT_MIPS_EVENTS;
5332 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5333 /* The sh_link field is set in final_write_processing. */
5335 else if (strcmp (name, ".msym") == 0)
5337 hdr->sh_type = SHT_MIPS_MSYM;
5338 hdr->sh_flags |= SHF_ALLOC;
5339 hdr->sh_entsize = 8;
5342 /* The generic elf_fake_sections will set up REL_HDR using the default
5343 kind of relocations. We used to set up a second header for the
5344 non-default kind of relocations here, but only NewABI would use
5345 these, and the IRIX ld doesn't like resulting empty RELA sections.
5346 Thus we create those header only on demand now. */
5348 return TRUE;
5351 /* Given a BFD section, try to locate the corresponding ELF section
5352 index. This is used by both the 32-bit and the 64-bit ABI.
5353 Actually, it's not clear to me that the 64-bit ABI supports these,
5354 but for non-PIC objects we will certainly want support for at least
5355 the .scommon section. */
5357 bfd_boolean
5358 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5359 asection *sec, int *retval)
5361 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
5363 *retval = SHN_MIPS_SCOMMON;
5364 return TRUE;
5366 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
5368 *retval = SHN_MIPS_ACOMMON;
5369 return TRUE;
5371 return FALSE;
5374 /* Hook called by the linker routine which adds symbols from an object
5375 file. We must handle the special MIPS section numbers here. */
5377 bfd_boolean
5378 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
5379 Elf_Internal_Sym *sym, const char **namep,
5380 flagword *flagsp ATTRIBUTE_UNUSED,
5381 asection **secp, bfd_vma *valp)
5383 if (SGI_COMPAT (abfd)
5384 && (abfd->flags & DYNAMIC) != 0
5385 && strcmp (*namep, "_rld_new_interface") == 0)
5387 /* Skip IRIX5 rld entry name. */
5388 *namep = NULL;
5389 return TRUE;
5392 switch (sym->st_shndx)
5394 case SHN_COMMON:
5395 /* Common symbols less than the GP size are automatically
5396 treated as SHN_MIPS_SCOMMON symbols. */
5397 if (sym->st_size > elf_gp_size (abfd)
5398 || IRIX_COMPAT (abfd) == ict_irix6)
5399 break;
5400 /* Fall through. */
5401 case SHN_MIPS_SCOMMON:
5402 *secp = bfd_make_section_old_way (abfd, ".scommon");
5403 (*secp)->flags |= SEC_IS_COMMON;
5404 *valp = sym->st_size;
5405 break;
5407 case SHN_MIPS_TEXT:
5408 /* This section is used in a shared object. */
5409 if (elf_tdata (abfd)->elf_text_section == NULL)
5411 asymbol *elf_text_symbol;
5412 asection *elf_text_section;
5413 bfd_size_type amt = sizeof (asection);
5415 elf_text_section = bfd_zalloc (abfd, amt);
5416 if (elf_text_section == NULL)
5417 return FALSE;
5419 amt = sizeof (asymbol);
5420 elf_text_symbol = bfd_zalloc (abfd, amt);
5421 if (elf_text_symbol == NULL)
5422 return FALSE;
5424 /* Initialize the section. */
5426 elf_tdata (abfd)->elf_text_section = elf_text_section;
5427 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
5429 elf_text_section->symbol = elf_text_symbol;
5430 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
5432 elf_text_section->name = ".text";
5433 elf_text_section->flags = SEC_NO_FLAGS;
5434 elf_text_section->output_section = NULL;
5435 elf_text_section->owner = abfd;
5436 elf_text_symbol->name = ".text";
5437 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5438 elf_text_symbol->section = elf_text_section;
5440 /* This code used to do *secp = bfd_und_section_ptr if
5441 info->shared. I don't know why, and that doesn't make sense,
5442 so I took it out. */
5443 *secp = elf_tdata (abfd)->elf_text_section;
5444 break;
5446 case SHN_MIPS_ACOMMON:
5447 /* Fall through. XXX Can we treat this as allocated data? */
5448 case SHN_MIPS_DATA:
5449 /* This section is used in a shared object. */
5450 if (elf_tdata (abfd)->elf_data_section == NULL)
5452 asymbol *elf_data_symbol;
5453 asection *elf_data_section;
5454 bfd_size_type amt = sizeof (asection);
5456 elf_data_section = bfd_zalloc (abfd, amt);
5457 if (elf_data_section == NULL)
5458 return FALSE;
5460 amt = sizeof (asymbol);
5461 elf_data_symbol = bfd_zalloc (abfd, amt);
5462 if (elf_data_symbol == NULL)
5463 return FALSE;
5465 /* Initialize the section. */
5467 elf_tdata (abfd)->elf_data_section = elf_data_section;
5468 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
5470 elf_data_section->symbol = elf_data_symbol;
5471 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
5473 elf_data_section->name = ".data";
5474 elf_data_section->flags = SEC_NO_FLAGS;
5475 elf_data_section->output_section = NULL;
5476 elf_data_section->owner = abfd;
5477 elf_data_symbol->name = ".data";
5478 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5479 elf_data_symbol->section = elf_data_section;
5481 /* This code used to do *secp = bfd_und_section_ptr if
5482 info->shared. I don't know why, and that doesn't make sense,
5483 so I took it out. */
5484 *secp = elf_tdata (abfd)->elf_data_section;
5485 break;
5487 case SHN_MIPS_SUNDEFINED:
5488 *secp = bfd_und_section_ptr;
5489 break;
5492 if (SGI_COMPAT (abfd)
5493 && ! info->shared
5494 && info->hash->creator == abfd->xvec
5495 && strcmp (*namep, "__rld_obj_head") == 0)
5497 struct elf_link_hash_entry *h;
5498 struct bfd_link_hash_entry *bh;
5500 /* Mark __rld_obj_head as dynamic. */
5501 bh = NULL;
5502 if (! (_bfd_generic_link_add_one_symbol
5503 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
5504 get_elf_backend_data (abfd)->collect, &bh)))
5505 return FALSE;
5507 h = (struct elf_link_hash_entry *) bh;
5508 h->non_elf = 0;
5509 h->def_regular = 1;
5510 h->type = STT_OBJECT;
5512 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5513 return FALSE;
5515 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
5518 /* If this is a mips16 text symbol, add 1 to the value to make it
5519 odd. This will cause something like .word SYM to come up with
5520 the right value when it is loaded into the PC. */
5521 if (sym->st_other == STO_MIPS16)
5522 ++*valp;
5524 return TRUE;
5527 /* This hook function is called before the linker writes out a global
5528 symbol. We mark symbols as small common if appropriate. This is
5529 also where we undo the increment of the value for a mips16 symbol. */
5531 bfd_boolean
5532 _bfd_mips_elf_link_output_symbol_hook
5533 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5534 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
5535 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5537 /* If we see a common symbol, which implies a relocatable link, then
5538 if a symbol was small common in an input file, mark it as small
5539 common in the output file. */
5540 if (sym->st_shndx == SHN_COMMON
5541 && strcmp (input_sec->name, ".scommon") == 0)
5542 sym->st_shndx = SHN_MIPS_SCOMMON;
5544 if (sym->st_other == STO_MIPS16)
5545 sym->st_value &= ~1;
5547 return TRUE;
5550 /* Functions for the dynamic linker. */
5552 /* Create dynamic sections when linking against a dynamic object. */
5554 bfd_boolean
5555 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
5557 struct elf_link_hash_entry *h;
5558 struct bfd_link_hash_entry *bh;
5559 flagword flags;
5560 register asection *s;
5561 const char * const *namep;
5563 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5564 | SEC_LINKER_CREATED | SEC_READONLY);
5566 /* Mips ABI requests the .dynamic section to be read only. */
5567 s = bfd_get_section_by_name (abfd, ".dynamic");
5568 if (s != NULL)
5570 if (! bfd_set_section_flags (abfd, s, flags))
5571 return FALSE;
5574 /* We need to create .got section. */
5575 if (! mips_elf_create_got_section (abfd, info, FALSE))
5576 return FALSE;
5578 if (! mips_elf_rel_dyn_section (elf_hash_table (info)->dynobj, TRUE))
5579 return FALSE;
5581 /* Create .stub section. */
5582 if (bfd_get_section_by_name (abfd,
5583 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
5585 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
5586 if (s == NULL
5587 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
5588 || ! bfd_set_section_alignment (abfd, s,
5589 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5590 return FALSE;
5593 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
5594 && !info->shared
5595 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5597 s = bfd_make_section (abfd, ".rld_map");
5598 if (s == NULL
5599 || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
5600 || ! bfd_set_section_alignment (abfd, s,
5601 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5602 return FALSE;
5605 /* On IRIX5, we adjust add some additional symbols and change the
5606 alignments of several sections. There is no ABI documentation
5607 indicating that this is necessary on IRIX6, nor any evidence that
5608 the linker takes such action. */
5609 if (IRIX_COMPAT (abfd) == ict_irix5)
5611 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5613 bh = NULL;
5614 if (! (_bfd_generic_link_add_one_symbol
5615 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
5616 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5617 return FALSE;
5619 h = (struct elf_link_hash_entry *) bh;
5620 h->non_elf = 0;
5621 h->def_regular = 1;
5622 h->type = STT_SECTION;
5624 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5625 return FALSE;
5628 /* We need to create a .compact_rel section. */
5629 if (SGI_COMPAT (abfd))
5631 if (!mips_elf_create_compact_rel_section (abfd, info))
5632 return FALSE;
5635 /* Change alignments of some sections. */
5636 s = bfd_get_section_by_name (abfd, ".hash");
5637 if (s != NULL)
5638 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5639 s = bfd_get_section_by_name (abfd, ".dynsym");
5640 if (s != NULL)
5641 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5642 s = bfd_get_section_by_name (abfd, ".dynstr");
5643 if (s != NULL)
5644 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5645 s = bfd_get_section_by_name (abfd, ".reginfo");
5646 if (s != NULL)
5647 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5648 s = bfd_get_section_by_name (abfd, ".dynamic");
5649 if (s != NULL)
5650 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5653 if (!info->shared)
5655 const char *name;
5657 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
5658 bh = NULL;
5659 if (!(_bfd_generic_link_add_one_symbol
5660 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
5661 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5662 return FALSE;
5664 h = (struct elf_link_hash_entry *) bh;
5665 h->non_elf = 0;
5666 h->def_regular = 1;
5667 h->type = STT_SECTION;
5669 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5670 return FALSE;
5672 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5674 /* __rld_map is a four byte word located in the .data section
5675 and is filled in by the rtld to contain a pointer to
5676 the _r_debug structure. Its symbol value will be set in
5677 _bfd_mips_elf_finish_dynamic_symbol. */
5678 s = bfd_get_section_by_name (abfd, ".rld_map");
5679 BFD_ASSERT (s != NULL);
5681 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
5682 bh = NULL;
5683 if (!(_bfd_generic_link_add_one_symbol
5684 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
5685 get_elf_backend_data (abfd)->collect, &bh)))
5686 return FALSE;
5688 h = (struct elf_link_hash_entry *) bh;
5689 h->non_elf = 0;
5690 h->def_regular = 1;
5691 h->type = STT_OBJECT;
5693 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5694 return FALSE;
5698 return TRUE;
5701 /* Look through the relocs for a section during the first phase, and
5702 allocate space in the global offset table. */
5704 bfd_boolean
5705 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5706 asection *sec, const Elf_Internal_Rela *relocs)
5708 const char *name;
5709 bfd *dynobj;
5710 Elf_Internal_Shdr *symtab_hdr;
5711 struct elf_link_hash_entry **sym_hashes;
5712 struct mips_got_info *g;
5713 size_t extsymoff;
5714 const Elf_Internal_Rela *rel;
5715 const Elf_Internal_Rela *rel_end;
5716 asection *sgot;
5717 asection *sreloc;
5718 const struct elf_backend_data *bed;
5720 if (info->relocatable)
5721 return TRUE;
5723 dynobj = elf_hash_table (info)->dynobj;
5724 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5725 sym_hashes = elf_sym_hashes (abfd);
5726 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5728 /* Check for the mips16 stub sections. */
5730 name = bfd_get_section_name (abfd, sec);
5731 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5733 unsigned long r_symndx;
5735 /* Look at the relocation information to figure out which symbol
5736 this is for. */
5738 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
5740 if (r_symndx < extsymoff
5741 || sym_hashes[r_symndx - extsymoff] == NULL)
5743 asection *o;
5745 /* This stub is for a local symbol. This stub will only be
5746 needed if there is some relocation in this BFD, other
5747 than a 16 bit function call, which refers to this symbol. */
5748 for (o = abfd->sections; o != NULL; o = o->next)
5750 Elf_Internal_Rela *sec_relocs;
5751 const Elf_Internal_Rela *r, *rend;
5753 /* We can ignore stub sections when looking for relocs. */
5754 if ((o->flags & SEC_RELOC) == 0
5755 || o->reloc_count == 0
5756 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5757 sizeof FN_STUB - 1) == 0
5758 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5759 sizeof CALL_STUB - 1) == 0
5760 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5761 sizeof CALL_FP_STUB - 1) == 0)
5762 continue;
5764 sec_relocs
5765 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
5766 info->keep_memory);
5767 if (sec_relocs == NULL)
5768 return FALSE;
5770 rend = sec_relocs + o->reloc_count;
5771 for (r = sec_relocs; r < rend; r++)
5772 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
5773 && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
5774 break;
5776 if (elf_section_data (o)->relocs != sec_relocs)
5777 free (sec_relocs);
5779 if (r < rend)
5780 break;
5783 if (o == NULL)
5785 /* There is no non-call reloc for this stub, so we do
5786 not need it. Since this function is called before
5787 the linker maps input sections to output sections, we
5788 can easily discard it by setting the SEC_EXCLUDE
5789 flag. */
5790 sec->flags |= SEC_EXCLUDE;
5791 return TRUE;
5794 /* Record this stub in an array of local symbol stubs for
5795 this BFD. */
5796 if (elf_tdata (abfd)->local_stubs == NULL)
5798 unsigned long symcount;
5799 asection **n;
5800 bfd_size_type amt;
5802 if (elf_bad_symtab (abfd))
5803 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
5804 else
5805 symcount = symtab_hdr->sh_info;
5806 amt = symcount * sizeof (asection *);
5807 n = bfd_zalloc (abfd, amt);
5808 if (n == NULL)
5809 return FALSE;
5810 elf_tdata (abfd)->local_stubs = n;
5813 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5815 /* We don't need to set mips16_stubs_seen in this case.
5816 That flag is used to see whether we need to look through
5817 the global symbol table for stubs. We don't need to set
5818 it here, because we just have a local stub. */
5820 else
5822 struct mips_elf_link_hash_entry *h;
5824 h = ((struct mips_elf_link_hash_entry *)
5825 sym_hashes[r_symndx - extsymoff]);
5827 /* H is the symbol this stub is for. */
5829 h->fn_stub = sec;
5830 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
5833 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5834 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5836 unsigned long r_symndx;
5837 struct mips_elf_link_hash_entry *h;
5838 asection **loc;
5840 /* Look at the relocation information to figure out which symbol
5841 this is for. */
5843 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
5845 if (r_symndx < extsymoff
5846 || sym_hashes[r_symndx - extsymoff] == NULL)
5848 /* This stub was actually built for a static symbol defined
5849 in the same file. We assume that all static symbols in
5850 mips16 code are themselves mips16, so we can simply
5851 discard this stub. Since this function is called before
5852 the linker maps input sections to output sections, we can
5853 easily discard it by setting the SEC_EXCLUDE flag. */
5854 sec->flags |= SEC_EXCLUDE;
5855 return TRUE;
5858 h = ((struct mips_elf_link_hash_entry *)
5859 sym_hashes[r_symndx - extsymoff]);
5861 /* H is the symbol this stub is for. */
5863 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5864 loc = &h->call_fp_stub;
5865 else
5866 loc = &h->call_stub;
5868 /* If we already have an appropriate stub for this function, we
5869 don't need another one, so we can discard this one. Since
5870 this function is called before the linker maps input sections
5871 to output sections, we can easily discard it by setting the
5872 SEC_EXCLUDE flag. We can also discard this section if we
5873 happen to already know that this is a mips16 function; it is
5874 not necessary to check this here, as it is checked later, but
5875 it is slightly faster to check now. */
5876 if (*loc != NULL || h->root.other == STO_MIPS16)
5878 sec->flags |= SEC_EXCLUDE;
5879 return TRUE;
5882 *loc = sec;
5883 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
5886 if (dynobj == NULL)
5888 sgot = NULL;
5889 g = NULL;
5891 else
5893 sgot = mips_elf_got_section (dynobj, FALSE);
5894 if (sgot == NULL)
5895 g = NULL;
5896 else
5898 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
5899 g = mips_elf_section_data (sgot)->u.got_info;
5900 BFD_ASSERT (g != NULL);
5904 sreloc = NULL;
5905 bed = get_elf_backend_data (abfd);
5906 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5907 for (rel = relocs; rel < rel_end; ++rel)
5909 unsigned long r_symndx;
5910 unsigned int r_type;
5911 struct elf_link_hash_entry *h;
5913 r_symndx = ELF_R_SYM (abfd, rel->r_info);
5914 r_type = ELF_R_TYPE (abfd, rel->r_info);
5916 if (r_symndx < extsymoff)
5917 h = NULL;
5918 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
5920 (*_bfd_error_handler)
5921 (_("%B: Malformed reloc detected for section %s"),
5922 abfd, name);
5923 bfd_set_error (bfd_error_bad_value);
5924 return FALSE;
5926 else
5928 h = sym_hashes[r_symndx - extsymoff];
5930 /* This may be an indirect symbol created because of a version. */
5931 if (h != NULL)
5933 while (h->root.type == bfd_link_hash_indirect)
5934 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5938 /* Some relocs require a global offset table. */
5939 if (dynobj == NULL || sgot == NULL)
5941 switch (r_type)
5943 case R_MIPS_GOT16:
5944 case R_MIPS_CALL16:
5945 case R_MIPS_CALL_HI16:
5946 case R_MIPS_CALL_LO16:
5947 case R_MIPS_GOT_HI16:
5948 case R_MIPS_GOT_LO16:
5949 case R_MIPS_GOT_PAGE:
5950 case R_MIPS_GOT_OFST:
5951 case R_MIPS_GOT_DISP:
5952 case R_MIPS_TLS_GD:
5953 case R_MIPS_TLS_LDM:
5954 if (dynobj == NULL)
5955 elf_hash_table (info)->dynobj = dynobj = abfd;
5956 if (! mips_elf_create_got_section (dynobj, info, FALSE))
5957 return FALSE;
5958 g = mips_elf_got_info (dynobj, &sgot);
5959 break;
5961 case R_MIPS_32:
5962 case R_MIPS_REL32:
5963 case R_MIPS_64:
5964 if (dynobj == NULL
5965 && (info->shared || h != NULL)
5966 && (sec->flags & SEC_ALLOC) != 0)
5967 elf_hash_table (info)->dynobj = dynobj = abfd;
5968 break;
5970 default:
5971 break;
5975 if (!h && (r_type == R_MIPS_CALL_LO16
5976 || r_type == R_MIPS_GOT_LO16
5977 || r_type == R_MIPS_GOT_DISP))
5979 /* We may need a local GOT entry for this relocation. We
5980 don't count R_MIPS_GOT_PAGE because we can estimate the
5981 maximum number of pages needed by looking at the size of
5982 the segment. Similar comments apply to R_MIPS_GOT16 and
5983 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
5984 R_MIPS_CALL_HI16 because these are always followed by an
5985 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
5986 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
5987 rel->r_addend, g, 0))
5988 return FALSE;
5991 switch (r_type)
5993 case R_MIPS_CALL16:
5994 if (h == NULL)
5996 (*_bfd_error_handler)
5997 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
5998 abfd, (unsigned long) rel->r_offset);
5999 bfd_set_error (bfd_error_bad_value);
6000 return FALSE;
6002 /* Fall through. */
6004 case R_MIPS_CALL_HI16:
6005 case R_MIPS_CALL_LO16:
6006 if (h != NULL)
6008 /* This symbol requires a global offset table entry. */
6009 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6010 return FALSE;
6012 /* We need a stub, not a plt entry for the undefined
6013 function. But we record it as if it needs plt. See
6014 _bfd_elf_adjust_dynamic_symbol. */
6015 h->needs_plt = 1;
6016 h->type = STT_FUNC;
6018 break;
6020 case R_MIPS_GOT_PAGE:
6021 /* If this is a global, overridable symbol, GOT_PAGE will
6022 decay to GOT_DISP, so we'll need a GOT entry for it. */
6023 if (h == NULL)
6024 break;
6025 else
6027 struct mips_elf_link_hash_entry *hmips =
6028 (struct mips_elf_link_hash_entry *) h;
6030 while (hmips->root.root.type == bfd_link_hash_indirect
6031 || hmips->root.root.type == bfd_link_hash_warning)
6032 hmips = (struct mips_elf_link_hash_entry *)
6033 hmips->root.root.u.i.link;
6035 if (hmips->root.def_regular
6036 && ! (info->shared && ! info->symbolic
6037 && ! hmips->root.forced_local))
6038 break;
6040 /* Fall through. */
6042 case R_MIPS_GOT16:
6043 case R_MIPS_GOT_HI16:
6044 case R_MIPS_GOT_LO16:
6045 case R_MIPS_GOT_DISP:
6046 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6047 return FALSE;
6048 break;
6050 case R_MIPS_TLS_GOTTPREL:
6051 if (info->shared)
6052 info->flags |= DF_STATIC_TLS;
6053 /* Fall through */
6055 case R_MIPS_TLS_LDM:
6056 if (r_type == R_MIPS_TLS_LDM)
6058 r_symndx = 0;
6059 h = NULL;
6061 /* Fall through */
6063 case R_MIPS_TLS_GD:
6064 /* This symbol requires a global offset table entry, or two
6065 for TLS GD relocations. */
6067 unsigned char flag = (r_type == R_MIPS_TLS_GD
6068 ? GOT_TLS_GD
6069 : r_type == R_MIPS_TLS_LDM
6070 ? GOT_TLS_LDM
6071 : GOT_TLS_IE);
6072 if (h != NULL)
6074 struct mips_elf_link_hash_entry *hmips =
6075 (struct mips_elf_link_hash_entry *) h;
6076 hmips->tls_type |= flag;
6078 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
6079 return FALSE;
6081 else
6083 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
6085 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
6086 rel->r_addend, g, flag))
6087 return FALSE;
6090 break;
6092 case R_MIPS_32:
6093 case R_MIPS_REL32:
6094 case R_MIPS_64:
6095 if ((info->shared || h != NULL)
6096 && (sec->flags & SEC_ALLOC) != 0)
6098 if (sreloc == NULL)
6100 sreloc = mips_elf_rel_dyn_section (dynobj, TRUE);
6101 if (sreloc == NULL)
6102 return FALSE;
6104 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
6105 if (info->shared)
6107 /* When creating a shared object, we must copy these
6108 reloc types into the output file as R_MIPS_REL32
6109 relocs. We make room for this reloc in the
6110 .rel.dyn reloc section. */
6111 mips_elf_allocate_dynamic_relocations (dynobj, 1);
6112 if ((sec->flags & MIPS_READONLY_SECTION)
6113 == MIPS_READONLY_SECTION)
6114 /* We tell the dynamic linker that there are
6115 relocations against the text segment. */
6116 info->flags |= DF_TEXTREL;
6118 else
6120 struct mips_elf_link_hash_entry *hmips;
6122 /* We only need to copy this reloc if the symbol is
6123 defined in a dynamic object. */
6124 hmips = (struct mips_elf_link_hash_entry *) h;
6125 ++hmips->possibly_dynamic_relocs;
6126 if ((sec->flags & MIPS_READONLY_SECTION)
6127 == MIPS_READONLY_SECTION)
6128 /* We need it to tell the dynamic linker if there
6129 are relocations against the text segment. */
6130 hmips->readonly_reloc = TRUE;
6133 /* Even though we don't directly need a GOT entry for
6134 this symbol, a symbol must have a dynamic symbol
6135 table index greater that DT_MIPS_GOTSYM if there are
6136 dynamic relocations against it. */
6137 if (h != NULL)
6139 if (dynobj == NULL)
6140 elf_hash_table (info)->dynobj = dynobj = abfd;
6141 if (! mips_elf_create_got_section (dynobj, info, TRUE))
6142 return FALSE;
6143 g = mips_elf_got_info (dynobj, &sgot);
6144 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6145 return FALSE;
6149 if (SGI_COMPAT (abfd))
6150 mips_elf_hash_table (info)->compact_rel_size +=
6151 sizeof (Elf32_External_crinfo);
6152 break;
6154 case R_MIPS_26:
6155 case R_MIPS_GPREL16:
6156 case R_MIPS_LITERAL:
6157 case R_MIPS_GPREL32:
6158 if (SGI_COMPAT (abfd))
6159 mips_elf_hash_table (info)->compact_rel_size +=
6160 sizeof (Elf32_External_crinfo);
6161 break;
6163 /* This relocation describes the C++ object vtable hierarchy.
6164 Reconstruct it for later use during GC. */
6165 case R_MIPS_GNU_VTINHERIT:
6166 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6167 return FALSE;
6168 break;
6170 /* This relocation describes which C++ vtable entries are actually
6171 used. Record for later use during GC. */
6172 case R_MIPS_GNU_VTENTRY:
6173 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6174 return FALSE;
6175 break;
6177 default:
6178 break;
6181 /* We must not create a stub for a symbol that has relocations
6182 related to taking the function's address. */
6183 switch (r_type)
6185 default:
6186 if (h != NULL)
6188 struct mips_elf_link_hash_entry *mh;
6190 mh = (struct mips_elf_link_hash_entry *) h;
6191 mh->no_fn_stub = TRUE;
6193 break;
6194 case R_MIPS_CALL16:
6195 case R_MIPS_CALL_HI16:
6196 case R_MIPS_CALL_LO16:
6197 case R_MIPS_JALR:
6198 break;
6201 /* If this reloc is not a 16 bit call, and it has a global
6202 symbol, then we will need the fn_stub if there is one.
6203 References from a stub section do not count. */
6204 if (h != NULL
6205 && r_type != R_MIPS16_26
6206 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6207 sizeof FN_STUB - 1) != 0
6208 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6209 sizeof CALL_STUB - 1) != 0
6210 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6211 sizeof CALL_FP_STUB - 1) != 0)
6213 struct mips_elf_link_hash_entry *mh;
6215 mh = (struct mips_elf_link_hash_entry *) h;
6216 mh->need_fn_stub = TRUE;
6220 return TRUE;
6223 bfd_boolean
6224 _bfd_mips_relax_section (bfd *abfd, asection *sec,
6225 struct bfd_link_info *link_info,
6226 bfd_boolean *again)
6228 Elf_Internal_Rela *internal_relocs;
6229 Elf_Internal_Rela *irel, *irelend;
6230 Elf_Internal_Shdr *symtab_hdr;
6231 bfd_byte *contents = NULL;
6232 size_t extsymoff;
6233 bfd_boolean changed_contents = FALSE;
6234 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
6235 Elf_Internal_Sym *isymbuf = NULL;
6237 /* We are not currently changing any sizes, so only one pass. */
6238 *again = FALSE;
6240 if (link_info->relocatable)
6241 return TRUE;
6243 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
6244 link_info->keep_memory);
6245 if (internal_relocs == NULL)
6246 return TRUE;
6248 irelend = internal_relocs + sec->reloc_count
6249 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
6250 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6251 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6253 for (irel = internal_relocs; irel < irelend; irel++)
6255 bfd_vma symval;
6256 bfd_signed_vma sym_offset;
6257 unsigned int r_type;
6258 unsigned long r_symndx;
6259 asection *sym_sec;
6260 unsigned long instruction;
6262 /* Turn jalr into bgezal, and jr into beq, if they're marked
6263 with a JALR relocation, that indicate where they jump to.
6264 This saves some pipeline bubbles. */
6265 r_type = ELF_R_TYPE (abfd, irel->r_info);
6266 if (r_type != R_MIPS_JALR)
6267 continue;
6269 r_symndx = ELF_R_SYM (abfd, irel->r_info);
6270 /* Compute the address of the jump target. */
6271 if (r_symndx >= extsymoff)
6273 struct mips_elf_link_hash_entry *h
6274 = ((struct mips_elf_link_hash_entry *)
6275 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
6277 while (h->root.root.type == bfd_link_hash_indirect
6278 || h->root.root.type == bfd_link_hash_warning)
6279 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6281 /* If a symbol is undefined, or if it may be overridden,
6282 skip it. */
6283 if (! ((h->root.root.type == bfd_link_hash_defined
6284 || h->root.root.type == bfd_link_hash_defweak)
6285 && h->root.root.u.def.section)
6286 || (link_info->shared && ! link_info->symbolic
6287 && !h->root.forced_local))
6288 continue;
6290 sym_sec = h->root.root.u.def.section;
6291 if (sym_sec->output_section)
6292 symval = (h->root.root.u.def.value
6293 + sym_sec->output_section->vma
6294 + sym_sec->output_offset);
6295 else
6296 symval = h->root.root.u.def.value;
6298 else
6300 Elf_Internal_Sym *isym;
6302 /* Read this BFD's symbols if we haven't done so already. */
6303 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
6305 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6306 if (isymbuf == NULL)
6307 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6308 symtab_hdr->sh_info, 0,
6309 NULL, NULL, NULL);
6310 if (isymbuf == NULL)
6311 goto relax_return;
6314 isym = isymbuf + r_symndx;
6315 if (isym->st_shndx == SHN_UNDEF)
6316 continue;
6317 else if (isym->st_shndx == SHN_ABS)
6318 sym_sec = bfd_abs_section_ptr;
6319 else if (isym->st_shndx == SHN_COMMON)
6320 sym_sec = bfd_com_section_ptr;
6321 else
6322 sym_sec
6323 = bfd_section_from_elf_index (abfd, isym->st_shndx);
6324 symval = isym->st_value
6325 + sym_sec->output_section->vma
6326 + sym_sec->output_offset;
6329 /* Compute branch offset, from delay slot of the jump to the
6330 branch target. */
6331 sym_offset = (symval + irel->r_addend)
6332 - (sec_start + irel->r_offset + 4);
6334 /* Branch offset must be properly aligned. */
6335 if ((sym_offset & 3) != 0)
6336 continue;
6338 sym_offset >>= 2;
6340 /* Check that it's in range. */
6341 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
6342 continue;
6344 /* Get the section contents if we haven't done so already. */
6345 if (contents == NULL)
6347 /* Get cached copy if it exists. */
6348 if (elf_section_data (sec)->this_hdr.contents != NULL)
6349 contents = elf_section_data (sec)->this_hdr.contents;
6350 else
6352 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6353 goto relax_return;
6357 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
6359 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
6360 if ((instruction & 0xfc1fffff) == 0x0000f809)
6361 instruction = 0x04110000;
6362 /* If it was jr <reg>, turn it into b <target>. */
6363 else if ((instruction & 0xfc1fffff) == 0x00000008)
6364 instruction = 0x10000000;
6365 else
6366 continue;
6368 instruction |= (sym_offset & 0xffff);
6369 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
6370 changed_contents = TRUE;
6373 if (contents != NULL
6374 && elf_section_data (sec)->this_hdr.contents != contents)
6376 if (!changed_contents && !link_info->keep_memory)
6377 free (contents);
6378 else
6380 /* Cache the section contents for elf_link_input_bfd. */
6381 elf_section_data (sec)->this_hdr.contents = contents;
6384 return TRUE;
6386 relax_return:
6387 if (contents != NULL
6388 && elf_section_data (sec)->this_hdr.contents != contents)
6389 free (contents);
6390 return FALSE;
6393 /* Adjust a symbol defined by a dynamic object and referenced by a
6394 regular object. The current definition is in some section of the
6395 dynamic object, but we're not including those sections. We have to
6396 change the definition to something the rest of the link can
6397 understand. */
6399 bfd_boolean
6400 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6401 struct elf_link_hash_entry *h)
6403 bfd *dynobj;
6404 struct mips_elf_link_hash_entry *hmips;
6405 asection *s;
6407 dynobj = elf_hash_table (info)->dynobj;
6409 /* Make sure we know what is going on here. */
6410 BFD_ASSERT (dynobj != NULL
6411 && (h->needs_plt
6412 || h->u.weakdef != NULL
6413 || (h->def_dynamic
6414 && h->ref_regular
6415 && !h->def_regular)));
6417 /* If this symbol is defined in a dynamic object, we need to copy
6418 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6419 file. */
6420 hmips = (struct mips_elf_link_hash_entry *) h;
6421 if (! info->relocatable
6422 && hmips->possibly_dynamic_relocs != 0
6423 && (h->root.type == bfd_link_hash_defweak
6424 || !h->def_regular))
6426 mips_elf_allocate_dynamic_relocations (dynobj,
6427 hmips->possibly_dynamic_relocs);
6428 if (hmips->readonly_reloc)
6429 /* We tell the dynamic linker that there are relocations
6430 against the text segment. */
6431 info->flags |= DF_TEXTREL;
6434 /* For a function, create a stub, if allowed. */
6435 if (! hmips->no_fn_stub
6436 && h->needs_plt)
6438 if (! elf_hash_table (info)->dynamic_sections_created)
6439 return TRUE;
6441 /* If this symbol is not defined in a regular file, then set
6442 the symbol to the stub location. This is required to make
6443 function pointers compare as equal between the normal
6444 executable and the shared library. */
6445 if (!h->def_regular)
6447 /* We need .stub section. */
6448 s = bfd_get_section_by_name (dynobj,
6449 MIPS_ELF_STUB_SECTION_NAME (dynobj));
6450 BFD_ASSERT (s != NULL);
6452 h->root.u.def.section = s;
6453 h->root.u.def.value = s->size;
6455 /* XXX Write this stub address somewhere. */
6456 h->plt.offset = s->size;
6458 /* Make room for this stub code. */
6459 s->size += MIPS_FUNCTION_STUB_SIZE;
6461 /* The last half word of the stub will be filled with the index
6462 of this symbol in .dynsym section. */
6463 return TRUE;
6466 else if ((h->type == STT_FUNC)
6467 && !h->needs_plt)
6469 /* This will set the entry for this symbol in the GOT to 0, and
6470 the dynamic linker will take care of this. */
6471 h->root.u.def.value = 0;
6472 return TRUE;
6475 /* If this is a weak symbol, and there is a real definition, the
6476 processor independent code will have arranged for us to see the
6477 real definition first, and we can just use the same value. */
6478 if (h->u.weakdef != NULL)
6480 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6481 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6482 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6483 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6484 return TRUE;
6487 /* This is a reference to a symbol defined by a dynamic object which
6488 is not a function. */
6490 return TRUE;
6493 /* This function is called after all the input files have been read,
6494 and the input sections have been assigned to output sections. We
6495 check for any mips16 stub sections that we can discard. */
6497 bfd_boolean
6498 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
6499 struct bfd_link_info *info)
6501 asection *ri;
6503 bfd *dynobj;
6504 asection *s;
6505 struct mips_got_info *g;
6506 int i;
6507 bfd_size_type loadable_size = 0;
6508 bfd_size_type local_gotno;
6509 bfd *sub;
6510 struct mips_elf_count_tls_arg count_tls_arg;
6512 /* The .reginfo section has a fixed size. */
6513 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6514 if (ri != NULL)
6515 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6517 if (! (info->relocatable
6518 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
6519 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6520 mips_elf_check_mips16_stubs, NULL);
6522 dynobj = elf_hash_table (info)->dynobj;
6523 if (dynobj == NULL)
6524 /* Relocatable links don't have it. */
6525 return TRUE;
6527 g = mips_elf_got_info (dynobj, &s);
6528 if (s == NULL)
6529 return TRUE;
6531 /* Calculate the total loadable size of the output. That
6532 will give us the maximum number of GOT_PAGE entries
6533 required. */
6534 for (sub = info->input_bfds; sub; sub = sub->link_next)
6536 asection *subsection;
6538 for (subsection = sub->sections;
6539 subsection;
6540 subsection = subsection->next)
6542 if ((subsection->flags & SEC_ALLOC) == 0)
6543 continue;
6544 loadable_size += ((subsection->size + 0xf)
6545 &~ (bfd_size_type) 0xf);
6549 /* There has to be a global GOT entry for every symbol with
6550 a dynamic symbol table index of DT_MIPS_GOTSYM or
6551 higher. Therefore, it make sense to put those symbols
6552 that need GOT entries at the end of the symbol table. We
6553 do that here. */
6554 if (! mips_elf_sort_hash_table (info, 1))
6555 return FALSE;
6557 if (g->global_gotsym != NULL)
6558 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
6559 else
6560 /* If there are no global symbols, or none requiring
6561 relocations, then GLOBAL_GOTSYM will be NULL. */
6562 i = 0;
6564 /* In the worst case, we'll get one stub per dynamic symbol, plus
6565 one to account for the dummy entry at the end required by IRIX
6566 rld. */
6567 loadable_size += MIPS_FUNCTION_STUB_SIZE * (i + 1);
6569 /* Assume there are two loadable segments consisting of
6570 contiguous sections. Is 5 enough? */
6571 local_gotno = (loadable_size >> 16) + 5;
6573 g->local_gotno += local_gotno;
6574 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
6576 g->global_gotno = i;
6577 s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
6579 /* We need to calculate tls_gotno for global symbols at this point
6580 instead of building it up earlier, to avoid doublecounting
6581 entries for one global symbol from multiple input files. */
6582 count_tls_arg.info = info;
6583 count_tls_arg.needed = 0;
6584 elf_link_hash_traverse (elf_hash_table (info),
6585 mips_elf_count_global_tls_entries,
6586 &count_tls_arg);
6587 g->tls_gotno += count_tls_arg.needed;
6588 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
6590 mips_elf_resolve_final_got_entries (g);
6592 if (s->size > MIPS_ELF_GOT_MAX_SIZE (output_bfd))
6594 if (! mips_elf_multi_got (output_bfd, info, g, s, local_gotno))
6595 return FALSE;
6597 else
6599 /* Set up TLS entries for the first GOT. */
6600 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
6601 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
6604 return TRUE;
6607 /* Set the sizes of the dynamic sections. */
6609 bfd_boolean
6610 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
6611 struct bfd_link_info *info)
6613 bfd *dynobj;
6614 asection *s;
6615 bfd_boolean reltext;
6617 dynobj = elf_hash_table (info)->dynobj;
6618 BFD_ASSERT (dynobj != NULL);
6620 if (elf_hash_table (info)->dynamic_sections_created)
6622 /* Set the contents of the .interp section to the interpreter. */
6623 if (info->executable)
6625 s = bfd_get_section_by_name (dynobj, ".interp");
6626 BFD_ASSERT (s != NULL);
6627 s->size
6628 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
6629 s->contents
6630 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
6634 /* The check_relocs and adjust_dynamic_symbol entry points have
6635 determined the sizes of the various dynamic sections. Allocate
6636 memory for them. */
6637 reltext = FALSE;
6638 for (s = dynobj->sections; s != NULL; s = s->next)
6640 const char *name;
6641 bfd_boolean strip;
6643 /* It's OK to base decisions on the section name, because none
6644 of the dynobj section names depend upon the input files. */
6645 name = bfd_get_section_name (dynobj, s);
6647 if ((s->flags & SEC_LINKER_CREATED) == 0)
6648 continue;
6650 strip = FALSE;
6652 if (strncmp (name, ".rel", 4) == 0)
6654 if (s->size == 0)
6656 /* We only strip the section if the output section name
6657 has the same name. Otherwise, there might be several
6658 input sections for this output section. FIXME: This
6659 code is probably not needed these days anyhow, since
6660 the linker now does not create empty output sections. */
6661 if (s->output_section != NULL
6662 && strcmp (name,
6663 bfd_get_section_name (s->output_section->owner,
6664 s->output_section)) == 0)
6665 strip = TRUE;
6667 else
6669 const char *outname;
6670 asection *target;
6672 /* If this relocation section applies to a read only
6673 section, then we probably need a DT_TEXTREL entry.
6674 If the relocation section is .rel.dyn, we always
6675 assert a DT_TEXTREL entry rather than testing whether
6676 there exists a relocation to a read only section or
6677 not. */
6678 outname = bfd_get_section_name (output_bfd,
6679 s->output_section);
6680 target = bfd_get_section_by_name (output_bfd, outname + 4);
6681 if ((target != NULL
6682 && (target->flags & SEC_READONLY) != 0
6683 && (target->flags & SEC_ALLOC) != 0)
6684 || strcmp (outname, ".rel.dyn") == 0)
6685 reltext = TRUE;
6687 /* We use the reloc_count field as a counter if we need
6688 to copy relocs into the output file. */
6689 if (strcmp (name, ".rel.dyn") != 0)
6690 s->reloc_count = 0;
6692 /* If combreloc is enabled, elf_link_sort_relocs() will
6693 sort relocations, but in a different way than we do,
6694 and before we're done creating relocations. Also, it
6695 will move them around between input sections'
6696 relocation's contents, so our sorting would be
6697 broken, so don't let it run. */
6698 info->combreloc = 0;
6701 else if (strncmp (name, ".got", 4) == 0)
6703 /* _bfd_mips_elf_always_size_sections() has already done
6704 most of the work, but some symbols may have been mapped
6705 to versions that we must now resolve in the got_entries
6706 hash tables. */
6707 struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
6708 struct mips_got_info *g = gg;
6709 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
6710 unsigned int needed_relocs = 0;
6712 if (gg->next)
6714 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
6715 set_got_offset_arg.info = info;
6717 /* NOTE 2005-02-03: How can this call, or the next, ever
6718 find any indirect entries to resolve? They were all
6719 resolved in mips_elf_multi_got. */
6720 mips_elf_resolve_final_got_entries (gg);
6721 for (g = gg->next; g && g->next != gg; g = g->next)
6723 unsigned int save_assign;
6725 mips_elf_resolve_final_got_entries (g);
6727 /* Assign offsets to global GOT entries. */
6728 save_assign = g->assigned_gotno;
6729 g->assigned_gotno = g->local_gotno;
6730 set_got_offset_arg.g = g;
6731 set_got_offset_arg.needed_relocs = 0;
6732 htab_traverse (g->got_entries,
6733 mips_elf_set_global_got_offset,
6734 &set_got_offset_arg);
6735 needed_relocs += set_got_offset_arg.needed_relocs;
6736 BFD_ASSERT (g->assigned_gotno - g->local_gotno
6737 <= g->global_gotno);
6739 g->assigned_gotno = save_assign;
6740 if (info->shared)
6742 needed_relocs += g->local_gotno - g->assigned_gotno;
6743 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
6744 + g->next->global_gotno
6745 + g->next->tls_gotno
6746 + MIPS_RESERVED_GOTNO);
6750 else
6752 struct mips_elf_count_tls_arg arg;
6753 arg.info = info;
6754 arg.needed = 0;
6756 htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
6757 &arg);
6758 elf_link_hash_traverse (elf_hash_table (info),
6759 mips_elf_count_global_tls_relocs,
6760 &arg);
6762 needed_relocs += arg.needed;
6765 if (needed_relocs)
6766 mips_elf_allocate_dynamic_relocations (dynobj, needed_relocs);
6768 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
6770 /* IRIX rld assumes that the function stub isn't at the end
6771 of .text section. So put a dummy. XXX */
6772 s->size += MIPS_FUNCTION_STUB_SIZE;
6774 else if (! info->shared
6775 && ! mips_elf_hash_table (info)->use_rld_obj_head
6776 && strncmp (name, ".rld_map", 8) == 0)
6778 /* We add a room for __rld_map. It will be filled in by the
6779 rtld to contain a pointer to the _r_debug structure. */
6780 s->size += 4;
6782 else if (SGI_COMPAT (output_bfd)
6783 && strncmp (name, ".compact_rel", 12) == 0)
6784 s->size += mips_elf_hash_table (info)->compact_rel_size;
6785 else if (strncmp (name, ".init", 5) != 0)
6787 /* It's not one of our sections, so don't allocate space. */
6788 continue;
6791 if (strip)
6793 _bfd_strip_section_from_output (info, s);
6794 continue;
6797 /* Allocate memory for the section contents. */
6798 s->contents = bfd_zalloc (dynobj, s->size);
6799 if (s->contents == NULL && s->size != 0)
6801 bfd_set_error (bfd_error_no_memory);
6802 return FALSE;
6806 if (elf_hash_table (info)->dynamic_sections_created)
6808 /* Add some entries to the .dynamic section. We fill in the
6809 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
6810 must add the entries now so that we get the correct size for
6811 the .dynamic section. The DT_DEBUG entry is filled in by the
6812 dynamic linker and used by the debugger. */
6813 if (! info->shared)
6815 /* SGI object has the equivalence of DT_DEBUG in the
6816 DT_MIPS_RLD_MAP entry. */
6817 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
6818 return FALSE;
6819 if (!SGI_COMPAT (output_bfd))
6821 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
6822 return FALSE;
6825 else
6827 /* Shared libraries on traditional mips have DT_DEBUG. */
6828 if (!SGI_COMPAT (output_bfd))
6830 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
6831 return FALSE;
6835 if (reltext && SGI_COMPAT (output_bfd))
6836 info->flags |= DF_TEXTREL;
6838 if ((info->flags & DF_TEXTREL) != 0)
6840 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
6841 return FALSE;
6844 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
6845 return FALSE;
6847 if (mips_elf_rel_dyn_section (dynobj, FALSE))
6849 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
6850 return FALSE;
6852 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
6853 return FALSE;
6855 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
6856 return FALSE;
6859 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
6860 return FALSE;
6862 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
6863 return FALSE;
6865 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
6866 return FALSE;
6868 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
6869 return FALSE;
6871 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
6872 return FALSE;
6874 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
6875 return FALSE;
6877 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
6878 return FALSE;
6880 if (IRIX_COMPAT (dynobj) == ict_irix5
6881 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
6882 return FALSE;
6884 if (IRIX_COMPAT (dynobj) == ict_irix6
6885 && (bfd_get_section_by_name
6886 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
6887 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
6888 return FALSE;
6891 return TRUE;
6894 /* Relocate a MIPS ELF section. */
6896 bfd_boolean
6897 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
6898 bfd *input_bfd, asection *input_section,
6899 bfd_byte *contents, Elf_Internal_Rela *relocs,
6900 Elf_Internal_Sym *local_syms,
6901 asection **local_sections)
6903 Elf_Internal_Rela *rel;
6904 const Elf_Internal_Rela *relend;
6905 bfd_vma addend = 0;
6906 bfd_boolean use_saved_addend_p = FALSE;
6907 const struct elf_backend_data *bed;
6909 bed = get_elf_backend_data (output_bfd);
6910 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6911 for (rel = relocs; rel < relend; ++rel)
6913 const char *name;
6914 bfd_vma value;
6915 reloc_howto_type *howto;
6916 bfd_boolean require_jalx;
6917 /* TRUE if the relocation is a RELA relocation, rather than a
6918 REL relocation. */
6919 bfd_boolean rela_relocation_p = TRUE;
6920 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6921 const char *msg;
6923 /* Find the relocation howto for this relocation. */
6924 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
6926 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6927 64-bit code, but make sure all their addresses are in the
6928 lowermost or uppermost 32-bit section of the 64-bit address
6929 space. Thus, when they use an R_MIPS_64 they mean what is
6930 usually meant by R_MIPS_32, with the exception that the
6931 stored value is sign-extended to 64 bits. */
6932 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
6934 /* On big-endian systems, we need to lie about the position
6935 of the reloc. */
6936 if (bfd_big_endian (input_bfd))
6937 rel->r_offset += 4;
6939 else
6940 /* NewABI defaults to RELA relocations. */
6941 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
6942 NEWABI_P (input_bfd)
6943 && (MIPS_RELOC_RELA_P
6944 (input_bfd, input_section,
6945 rel - relocs)));
6947 if (!use_saved_addend_p)
6949 Elf_Internal_Shdr *rel_hdr;
6951 /* If these relocations were originally of the REL variety,
6952 we must pull the addend out of the field that will be
6953 relocated. Otherwise, we simply use the contents of the
6954 RELA relocation. To determine which flavor or relocation
6955 this is, we depend on the fact that the INPUT_SECTION's
6956 REL_HDR is read before its REL_HDR2. */
6957 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6958 if ((size_t) (rel - relocs)
6959 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6960 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6961 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6963 bfd_byte *location = contents + rel->r_offset;
6965 /* Note that this is a REL relocation. */
6966 rela_relocation_p = FALSE;
6968 /* Get the addend, which is stored in the input file. */
6969 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE,
6970 location);
6971 addend = mips_elf_obtain_contents (howto, rel, input_bfd,
6972 contents);
6973 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, FALSE,
6974 location);
6976 addend &= howto->src_mask;
6978 /* For some kinds of relocations, the ADDEND is a
6979 combination of the addend stored in two different
6980 relocations. */
6981 if (r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16
6982 || (r_type == R_MIPS_GOT16
6983 && mips_elf_local_relocation_p (input_bfd, rel,
6984 local_sections, FALSE)))
6986 bfd_vma l;
6987 const Elf_Internal_Rela *lo16_relocation;
6988 reloc_howto_type *lo16_howto;
6989 bfd_byte *lo16_location;
6990 int lo16_type;
6992 if (r_type == R_MIPS16_HI16)
6993 lo16_type = R_MIPS16_LO16;
6994 else
6995 lo16_type = R_MIPS_LO16;
6997 /* The combined value is the sum of the HI16 addend,
6998 left-shifted by sixteen bits, and the LO16
6999 addend, sign extended. (Usually, the code does
7000 a `lui' of the HI16 value, and then an `addiu' of
7001 the LO16 value.)
7003 Scan ahead to find a matching LO16 relocation.
7005 According to the MIPS ELF ABI, the R_MIPS_LO16
7006 relocation must be immediately following.
7007 However, for the IRIX6 ABI, the next relocation
7008 may be a composed relocation consisting of
7009 several relocations for the same address. In
7010 that case, the R_MIPS_LO16 relocation may occur
7011 as one of these. We permit a similar extension
7012 in general, as that is useful for GCC. */
7013 lo16_relocation = mips_elf_next_relocation (input_bfd,
7014 lo16_type,
7015 rel, relend);
7016 if (lo16_relocation == NULL)
7017 return FALSE;
7019 lo16_location = contents + lo16_relocation->r_offset;
7021 /* Obtain the addend kept there. */
7022 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd,
7023 lo16_type, FALSE);
7024 _bfd_mips16_elf_reloc_unshuffle (input_bfd, lo16_type, FALSE,
7025 lo16_location);
7026 l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
7027 input_bfd, contents);
7028 _bfd_mips16_elf_reloc_shuffle (input_bfd, lo16_type, FALSE,
7029 lo16_location);
7030 l &= lo16_howto->src_mask;
7031 l <<= lo16_howto->rightshift;
7032 l = _bfd_mips_elf_sign_extend (l, 16);
7034 addend <<= 16;
7036 /* Compute the combined addend. */
7037 addend += l;
7039 else
7040 addend <<= howto->rightshift;
7042 else
7043 addend = rel->r_addend;
7046 if (info->relocatable)
7048 Elf_Internal_Sym *sym;
7049 unsigned long r_symndx;
7051 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
7052 && bfd_big_endian (input_bfd))
7053 rel->r_offset -= 4;
7055 /* Since we're just relocating, all we need to do is copy
7056 the relocations back out to the object file, unless
7057 they're against a section symbol, in which case we need
7058 to adjust by the section offset, or unless they're GP
7059 relative in which case we need to adjust by the amount
7060 that we're adjusting GP in this relocatable object. */
7062 if (! mips_elf_local_relocation_p (input_bfd, rel, local_sections,
7063 FALSE))
7064 /* There's nothing to do for non-local relocations. */
7065 continue;
7067 if (r_type == R_MIPS16_GPREL
7068 || r_type == R_MIPS_GPREL16
7069 || r_type == R_MIPS_GPREL32
7070 || r_type == R_MIPS_LITERAL)
7071 addend -= (_bfd_get_gp_value (output_bfd)
7072 - _bfd_get_gp_value (input_bfd));
7074 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
7075 sym = local_syms + r_symndx;
7076 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7077 /* Adjust the addend appropriately. */
7078 addend += local_sections[r_symndx]->output_offset;
7080 if (rela_relocation_p)
7081 /* If this is a RELA relocation, just update the addend. */
7082 rel->r_addend = addend;
7083 else
7085 if (r_type == R_MIPS_HI16
7086 || r_type == R_MIPS_GOT16)
7087 addend = mips_elf_high (addend);
7088 else if (r_type == R_MIPS_HIGHER)
7089 addend = mips_elf_higher (addend);
7090 else if (r_type == R_MIPS_HIGHEST)
7091 addend = mips_elf_highest (addend);
7092 else
7093 addend >>= howto->rightshift;
7095 /* We use the source mask, rather than the destination
7096 mask because the place to which we are writing will be
7097 source of the addend in the final link. */
7098 addend &= howto->src_mask;
7100 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
7101 /* See the comment above about using R_MIPS_64 in the 32-bit
7102 ABI. Here, we need to update the addend. It would be
7103 possible to get away with just using the R_MIPS_32 reloc
7104 but for endianness. */
7106 bfd_vma sign_bits;
7107 bfd_vma low_bits;
7108 bfd_vma high_bits;
7110 if (addend & ((bfd_vma) 1 << 31))
7111 #ifdef BFD64
7112 sign_bits = ((bfd_vma) 1 << 32) - 1;
7113 #else
7114 sign_bits = -1;
7115 #endif
7116 else
7117 sign_bits = 0;
7119 /* If we don't know that we have a 64-bit type,
7120 do two separate stores. */
7121 if (bfd_big_endian (input_bfd))
7123 /* Store the sign-bits (which are most significant)
7124 first. */
7125 low_bits = sign_bits;
7126 high_bits = addend;
7128 else
7130 low_bits = addend;
7131 high_bits = sign_bits;
7133 bfd_put_32 (input_bfd, low_bits,
7134 contents + rel->r_offset);
7135 bfd_put_32 (input_bfd, high_bits,
7136 contents + rel->r_offset + 4);
7137 continue;
7140 if (! mips_elf_perform_relocation (info, howto, rel, addend,
7141 input_bfd, input_section,
7142 contents, FALSE))
7143 return FALSE;
7146 /* Go on to the next relocation. */
7147 continue;
7150 /* In the N32 and 64-bit ABIs there may be multiple consecutive
7151 relocations for the same offset. In that case we are
7152 supposed to treat the output of each relocation as the addend
7153 for the next. */
7154 if (rel + 1 < relend
7155 && rel->r_offset == rel[1].r_offset
7156 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
7157 use_saved_addend_p = TRUE;
7158 else
7159 use_saved_addend_p = FALSE;
7161 /* Figure out what value we are supposed to relocate. */
7162 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
7163 input_section, info, rel,
7164 addend, howto, local_syms,
7165 local_sections, &value,
7166 &name, &require_jalx,
7167 use_saved_addend_p))
7169 case bfd_reloc_continue:
7170 /* There's nothing to do. */
7171 continue;
7173 case bfd_reloc_undefined:
7174 /* mips_elf_calculate_relocation already called the
7175 undefined_symbol callback. There's no real point in
7176 trying to perform the relocation at this point, so we
7177 just skip ahead to the next relocation. */
7178 continue;
7180 case bfd_reloc_notsupported:
7181 msg = _("internal error: unsupported relocation error");
7182 info->callbacks->warning
7183 (info, msg, name, input_bfd, input_section, rel->r_offset);
7184 return FALSE;
7186 case bfd_reloc_overflow:
7187 if (use_saved_addend_p)
7188 /* Ignore overflow until we reach the last relocation for
7189 a given location. */
7191 else
7193 BFD_ASSERT (name != NULL);
7194 if (! ((*info->callbacks->reloc_overflow)
7195 (info, NULL, name, howto->name, (bfd_vma) 0,
7196 input_bfd, input_section, rel->r_offset)))
7197 return FALSE;
7199 break;
7201 case bfd_reloc_ok:
7202 break;
7204 default:
7205 abort ();
7206 break;
7209 /* If we've got another relocation for the address, keep going
7210 until we reach the last one. */
7211 if (use_saved_addend_p)
7213 addend = value;
7214 continue;
7217 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
7218 /* See the comment above about using R_MIPS_64 in the 32-bit
7219 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
7220 that calculated the right value. Now, however, we
7221 sign-extend the 32-bit result to 64-bits, and store it as a
7222 64-bit value. We are especially generous here in that we
7223 go to extreme lengths to support this usage on systems with
7224 only a 32-bit VMA. */
7226 bfd_vma sign_bits;
7227 bfd_vma low_bits;
7228 bfd_vma high_bits;
7230 if (value & ((bfd_vma) 1 << 31))
7231 #ifdef BFD64
7232 sign_bits = ((bfd_vma) 1 << 32) - 1;
7233 #else
7234 sign_bits = -1;
7235 #endif
7236 else
7237 sign_bits = 0;
7239 /* If we don't know that we have a 64-bit type,
7240 do two separate stores. */
7241 if (bfd_big_endian (input_bfd))
7243 /* Undo what we did above. */
7244 rel->r_offset -= 4;
7245 /* Store the sign-bits (which are most significant)
7246 first. */
7247 low_bits = sign_bits;
7248 high_bits = value;
7250 else
7252 low_bits = value;
7253 high_bits = sign_bits;
7255 bfd_put_32 (input_bfd, low_bits,
7256 contents + rel->r_offset);
7257 bfd_put_32 (input_bfd, high_bits,
7258 contents + rel->r_offset + 4);
7259 continue;
7262 /* Actually perform the relocation. */
7263 if (! mips_elf_perform_relocation (info, howto, rel, value,
7264 input_bfd, input_section,
7265 contents, require_jalx))
7266 return FALSE;
7269 return TRUE;
7272 /* If NAME is one of the special IRIX6 symbols defined by the linker,
7273 adjust it appropriately now. */
7275 static void
7276 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7277 const char *name, Elf_Internal_Sym *sym)
7279 /* The linker script takes care of providing names and values for
7280 these, but we must place them into the right sections. */
7281 static const char* const text_section_symbols[] = {
7282 "_ftext",
7283 "_etext",
7284 "__dso_displacement",
7285 "__elf_header",
7286 "__program_header_table",
7287 NULL
7290 static const char* const data_section_symbols[] = {
7291 "_fdata",
7292 "_edata",
7293 "_end",
7294 "_fbss",
7295 NULL
7298 const char* const *p;
7299 int i;
7301 for (i = 0; i < 2; ++i)
7302 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7304 ++p)
7305 if (strcmp (*p, name) == 0)
7307 /* All of these symbols are given type STT_SECTION by the
7308 IRIX6 linker. */
7309 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7310 sym->st_other = STO_PROTECTED;
7312 /* The IRIX linker puts these symbols in special sections. */
7313 if (i == 0)
7314 sym->st_shndx = SHN_MIPS_TEXT;
7315 else
7316 sym->st_shndx = SHN_MIPS_DATA;
7318 break;
7322 /* Finish up dynamic symbol handling. We set the contents of various
7323 dynamic sections here. */
7325 bfd_boolean
7326 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
7327 struct bfd_link_info *info,
7328 struct elf_link_hash_entry *h,
7329 Elf_Internal_Sym *sym)
7331 bfd *dynobj;
7332 asection *sgot;
7333 struct mips_got_info *g, *gg;
7334 const char *name;
7336 dynobj = elf_hash_table (info)->dynobj;
7338 if (h->plt.offset != MINUS_ONE)
7340 asection *s;
7341 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7343 /* This symbol has a stub. Set it up. */
7345 BFD_ASSERT (h->dynindx != -1);
7347 s = bfd_get_section_by_name (dynobj,
7348 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7349 BFD_ASSERT (s != NULL);
7351 /* FIXME: Can h->dynindex be more than 64K? */
7352 if (h->dynindx & 0xffff0000)
7353 return FALSE;
7355 /* Fill the stub. */
7356 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
7357 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
7358 bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
7359 bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
7361 BFD_ASSERT (h->plt.offset <= s->size);
7362 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7364 /* Mark the symbol as undefined. plt.offset != -1 occurs
7365 only for the referenced symbol. */
7366 sym->st_shndx = SHN_UNDEF;
7368 /* The run-time linker uses the st_value field of the symbol
7369 to reset the global offset table entry for this external
7370 to its stub address when unlinking a shared object. */
7371 sym->st_value = (s->output_section->vma + s->output_offset
7372 + h->plt.offset);
7375 BFD_ASSERT (h->dynindx != -1
7376 || h->forced_local);
7378 sgot = mips_elf_got_section (dynobj, FALSE);
7379 BFD_ASSERT (sgot != NULL);
7380 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
7381 g = mips_elf_section_data (sgot)->u.got_info;
7382 BFD_ASSERT (g != NULL);
7384 /* Run through the global symbol table, creating GOT entries for all
7385 the symbols that need them. */
7386 if (g->global_gotsym != NULL
7387 && h->dynindx >= g->global_gotsym->dynindx)
7389 bfd_vma offset;
7390 bfd_vma value;
7392 value = sym->st_value;
7393 offset = mips_elf_global_got_index (dynobj, output_bfd, h, R_MIPS_GOT16, info);
7394 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
7397 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
7399 struct mips_got_entry e, *p;
7400 bfd_vma entry;
7401 bfd_vma offset;
7403 gg = g;
7405 e.abfd = output_bfd;
7406 e.symndx = -1;
7407 e.d.h = (struct mips_elf_link_hash_entry *)h;
7408 e.tls_type = 0;
7410 for (g = g->next; g->next != gg; g = g->next)
7412 if (g->got_entries
7413 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
7414 &e)))
7416 offset = p->gotidx;
7417 if (info->shared
7418 || (elf_hash_table (info)->dynamic_sections_created
7419 && p->d.h != NULL
7420 && p->d.h->root.def_dynamic
7421 && !p->d.h->root.def_regular))
7423 /* Create an R_MIPS_REL32 relocation for this entry. Due to
7424 the various compatibility problems, it's easier to mock
7425 up an R_MIPS_32 or R_MIPS_64 relocation and leave
7426 mips_elf_create_dynamic_relocation to calculate the
7427 appropriate addend. */
7428 Elf_Internal_Rela rel[3];
7430 memset (rel, 0, sizeof (rel));
7431 if (ABI_64_P (output_bfd))
7432 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
7433 else
7434 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
7435 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
7437 entry = 0;
7438 if (! (mips_elf_create_dynamic_relocation
7439 (output_bfd, info, rel,
7440 e.d.h, NULL, sym->st_value, &entry, sgot)))
7441 return FALSE;
7443 else
7444 entry = sym->st_value;
7445 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
7450 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7451 name = h->root.root.string;
7452 if (strcmp (name, "_DYNAMIC") == 0
7453 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7454 sym->st_shndx = SHN_ABS;
7455 else if (strcmp (name, "_DYNAMIC_LINK") == 0
7456 || strcmp (name, "_DYNAMIC_LINKING") == 0)
7458 sym->st_shndx = SHN_ABS;
7459 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7460 sym->st_value = 1;
7462 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
7464 sym->st_shndx = SHN_ABS;
7465 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7466 sym->st_value = elf_gp (output_bfd);
7468 else if (SGI_COMPAT (output_bfd))
7470 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7471 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7473 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7474 sym->st_other = STO_PROTECTED;
7475 sym->st_value = 0;
7476 sym->st_shndx = SHN_MIPS_DATA;
7478 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7480 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7481 sym->st_other = STO_PROTECTED;
7482 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7483 sym->st_shndx = SHN_ABS;
7485 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7487 if (h->type == STT_FUNC)
7488 sym->st_shndx = SHN_MIPS_TEXT;
7489 else if (h->type == STT_OBJECT)
7490 sym->st_shndx = SHN_MIPS_DATA;
7494 /* Handle the IRIX6-specific symbols. */
7495 if (IRIX_COMPAT (output_bfd) == ict_irix6)
7496 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
7498 if (! info->shared)
7500 if (! mips_elf_hash_table (info)->use_rld_obj_head
7501 && (strcmp (name, "__rld_map") == 0
7502 || strcmp (name, "__RLD_MAP") == 0))
7504 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7505 BFD_ASSERT (s != NULL);
7506 sym->st_value = s->output_section->vma + s->output_offset;
7507 bfd_put_32 (output_bfd, 0, s->contents);
7508 if (mips_elf_hash_table (info)->rld_value == 0)
7509 mips_elf_hash_table (info)->rld_value = sym->st_value;
7511 else if (mips_elf_hash_table (info)->use_rld_obj_head
7512 && strcmp (name, "__rld_obj_head") == 0)
7514 /* IRIX6 does not use a .rld_map section. */
7515 if (IRIX_COMPAT (output_bfd) == ict_irix5
7516 || IRIX_COMPAT (output_bfd) == ict_none)
7517 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
7518 != NULL);
7519 mips_elf_hash_table (info)->rld_value = sym->st_value;
7523 /* If this is a mips16 symbol, force the value to be even. */
7524 if (sym->st_other == STO_MIPS16)
7525 sym->st_value &= ~1;
7527 return TRUE;
7530 /* Finish up the dynamic sections. */
7532 bfd_boolean
7533 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
7534 struct bfd_link_info *info)
7536 bfd *dynobj;
7537 asection *sdyn;
7538 asection *sgot;
7539 struct mips_got_info *gg, *g;
7541 dynobj = elf_hash_table (info)->dynobj;
7543 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7545 sgot = mips_elf_got_section (dynobj, FALSE);
7546 if (sgot == NULL)
7547 gg = g = NULL;
7548 else
7550 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
7551 gg = mips_elf_section_data (sgot)->u.got_info;
7552 BFD_ASSERT (gg != NULL);
7553 g = mips_elf_got_for_ibfd (gg, output_bfd);
7554 BFD_ASSERT (g != NULL);
7557 if (elf_hash_table (info)->dynamic_sections_created)
7559 bfd_byte *b;
7561 BFD_ASSERT (sdyn != NULL);
7562 BFD_ASSERT (g != NULL);
7564 for (b = sdyn->contents;
7565 b < sdyn->contents + sdyn->size;
7566 b += MIPS_ELF_DYN_SIZE (dynobj))
7568 Elf_Internal_Dyn dyn;
7569 const char *name;
7570 size_t elemsize;
7571 asection *s;
7572 bfd_boolean swap_out_p;
7574 /* Read in the current dynamic entry. */
7575 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
7577 /* Assume that we're going to modify it and write it out. */
7578 swap_out_p = TRUE;
7580 switch (dyn.d_tag)
7582 case DT_RELENT:
7583 s = mips_elf_rel_dyn_section (dynobj, FALSE);
7584 BFD_ASSERT (s != NULL);
7585 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
7586 break;
7588 case DT_STRSZ:
7589 /* Rewrite DT_STRSZ. */
7590 dyn.d_un.d_val =
7591 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7592 break;
7594 case DT_PLTGOT:
7595 name = ".got";
7596 s = bfd_get_section_by_name (output_bfd, name);
7597 BFD_ASSERT (s != NULL);
7598 dyn.d_un.d_ptr = s->vma;
7599 break;
7601 case DT_MIPS_RLD_VERSION:
7602 dyn.d_un.d_val = 1; /* XXX */
7603 break;
7605 case DT_MIPS_FLAGS:
7606 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7607 break;
7609 case DT_MIPS_TIME_STAMP:
7610 time ((time_t *) &dyn.d_un.d_val);
7611 break;
7613 case DT_MIPS_ICHECKSUM:
7614 /* XXX FIXME: */
7615 swap_out_p = FALSE;
7616 break;
7618 case DT_MIPS_IVERSION:
7619 /* XXX FIXME: */
7620 swap_out_p = FALSE;
7621 break;
7623 case DT_MIPS_BASE_ADDRESS:
7624 s = output_bfd->sections;
7625 BFD_ASSERT (s != NULL);
7626 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
7627 break;
7629 case DT_MIPS_LOCAL_GOTNO:
7630 dyn.d_un.d_val = g->local_gotno;
7631 break;
7633 case DT_MIPS_UNREFEXTNO:
7634 /* The index into the dynamic symbol table which is the
7635 entry of the first external symbol that is not
7636 referenced within the same object. */
7637 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
7638 break;
7640 case DT_MIPS_GOTSYM:
7641 if (gg->global_gotsym)
7643 dyn.d_un.d_val = gg->global_gotsym->dynindx;
7644 break;
7646 /* In case if we don't have global got symbols we default
7647 to setting DT_MIPS_GOTSYM to the same value as
7648 DT_MIPS_SYMTABNO, so we just fall through. */
7650 case DT_MIPS_SYMTABNO:
7651 name = ".dynsym";
7652 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
7653 s = bfd_get_section_by_name (output_bfd, name);
7654 BFD_ASSERT (s != NULL);
7656 dyn.d_un.d_val = s->size / elemsize;
7657 break;
7659 case DT_MIPS_HIPAGENO:
7660 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7661 break;
7663 case DT_MIPS_RLD_MAP:
7664 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7665 break;
7667 case DT_MIPS_OPTIONS:
7668 s = (bfd_get_section_by_name
7669 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
7670 dyn.d_un.d_ptr = s->vma;
7671 break;
7673 case DT_RELSZ:
7674 /* Reduce DT_RELSZ to account for any relocations we
7675 decided not to make. This is for the n64 irix rld,
7676 which doesn't seem to apply any relocations if there
7677 are trailing null entries. */
7678 s = mips_elf_rel_dyn_section (dynobj, FALSE);
7679 dyn.d_un.d_val = (s->reloc_count
7680 * (ABI_64_P (output_bfd)
7681 ? sizeof (Elf64_Mips_External_Rel)
7682 : sizeof (Elf32_External_Rel)));
7683 break;
7685 default:
7686 swap_out_p = FALSE;
7687 break;
7690 if (swap_out_p)
7691 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
7692 (dynobj, &dyn, b);
7696 /* The first entry of the global offset table will be filled at
7697 runtime. The second entry will be used by some runtime loaders.
7698 This isn't the case of IRIX rld. */
7699 if (sgot != NULL && sgot->size > 0)
7701 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents);
7702 MIPS_ELF_PUT_WORD (output_bfd, 0x80000000,
7703 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
7706 if (sgot != NULL)
7707 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
7708 = MIPS_ELF_GOT_SIZE (output_bfd);
7710 /* Generate dynamic relocations for the non-primary gots. */
7711 if (gg != NULL && gg->next)
7713 Elf_Internal_Rela rel[3];
7714 bfd_vma addend = 0;
7716 memset (rel, 0, sizeof (rel));
7717 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
7719 for (g = gg->next; g->next != gg; g = g->next)
7721 bfd_vma index = g->next->local_gotno + g->next->global_gotno
7722 + g->next->tls_gotno;
7724 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
7725 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
7726 MIPS_ELF_PUT_WORD (output_bfd, 0x80000000, sgot->contents
7727 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
7729 if (! info->shared)
7730 continue;
7732 while (index < g->assigned_gotno)
7734 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
7735 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
7736 if (!(mips_elf_create_dynamic_relocation
7737 (output_bfd, info, rel, NULL,
7738 bfd_abs_section_ptr,
7739 0, &addend, sgot)))
7740 return FALSE;
7741 BFD_ASSERT (addend == 0);
7747 asection *s;
7748 Elf32_compact_rel cpt;
7750 if (SGI_COMPAT (output_bfd))
7752 /* Write .compact_rel section out. */
7753 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7754 if (s != NULL)
7756 cpt.id1 = 1;
7757 cpt.num = s->reloc_count;
7758 cpt.id2 = 2;
7759 cpt.offset = (s->output_section->filepos
7760 + sizeof (Elf32_External_compact_rel));
7761 cpt.reserved0 = 0;
7762 cpt.reserved1 = 0;
7763 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7764 ((Elf32_External_compact_rel *)
7765 s->contents));
7767 /* Clean up a dummy stub function entry in .text. */
7768 s = bfd_get_section_by_name (dynobj,
7769 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7770 if (s != NULL)
7772 file_ptr dummy_offset;
7774 BFD_ASSERT (s->size >= MIPS_FUNCTION_STUB_SIZE);
7775 dummy_offset = s->size - MIPS_FUNCTION_STUB_SIZE;
7776 memset (s->contents + dummy_offset, 0,
7777 MIPS_FUNCTION_STUB_SIZE);
7782 /* We need to sort the entries of the dynamic relocation section. */
7784 s = mips_elf_rel_dyn_section (dynobj, FALSE);
7786 if (s != NULL
7787 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
7789 reldyn_sorting_bfd = output_bfd;
7791 if (ABI_64_P (output_bfd))
7792 qsort ((Elf64_External_Rel *) s->contents + 1, s->reloc_count - 1,
7793 sizeof (Elf64_Mips_External_Rel), sort_dynamic_relocs_64);
7794 else
7795 qsort ((Elf32_External_Rel *) s->contents + 1, s->reloc_count - 1,
7796 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
7800 return TRUE;
7804 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
7806 static void
7807 mips_set_isa_flags (bfd *abfd)
7809 flagword val;
7811 switch (bfd_get_mach (abfd))
7813 default:
7814 case bfd_mach_mips3000:
7815 val = E_MIPS_ARCH_1;
7816 break;
7818 case bfd_mach_mips3900:
7819 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
7820 break;
7822 case bfd_mach_mips6000:
7823 val = E_MIPS_ARCH_2;
7824 break;
7826 case bfd_mach_mips4000:
7827 case bfd_mach_mips4300:
7828 case bfd_mach_mips4400:
7829 case bfd_mach_mips4600:
7830 val = E_MIPS_ARCH_3;
7831 break;
7833 case bfd_mach_mips4010:
7834 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
7835 break;
7837 case bfd_mach_mips4100:
7838 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
7839 break;
7841 case bfd_mach_mips4111:
7842 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
7843 break;
7845 case bfd_mach_mips4120:
7846 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
7847 break;
7849 case bfd_mach_mips4650:
7850 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
7851 break;
7853 case bfd_mach_mips5400:
7854 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
7855 break;
7857 case bfd_mach_mips5500:
7858 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
7859 break;
7861 case bfd_mach_mips9000:
7862 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
7863 break;
7865 case bfd_mach_mips5000:
7866 case bfd_mach_mips7000:
7867 case bfd_mach_mips8000:
7868 case bfd_mach_mips10000:
7869 case bfd_mach_mips12000:
7870 val = E_MIPS_ARCH_4;
7871 break;
7873 case bfd_mach_mips5:
7874 val = E_MIPS_ARCH_5;
7875 break;
7877 case bfd_mach_mips_sb1:
7878 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
7879 break;
7881 case bfd_mach_mipsisa32:
7882 val = E_MIPS_ARCH_32;
7883 break;
7885 case bfd_mach_mipsisa64:
7886 val = E_MIPS_ARCH_64;
7887 break;
7889 case bfd_mach_mipsisa32r2:
7890 val = E_MIPS_ARCH_32R2;
7891 break;
7893 case bfd_mach_mipsisa64r2:
7894 val = E_MIPS_ARCH_64R2;
7895 break;
7897 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7898 elf_elfheader (abfd)->e_flags |= val;
7903 /* The final processing done just before writing out a MIPS ELF object
7904 file. This gets the MIPS architecture right based on the machine
7905 number. This is used by both the 32-bit and the 64-bit ABI. */
7907 void
7908 _bfd_mips_elf_final_write_processing (bfd *abfd,
7909 bfd_boolean linker ATTRIBUTE_UNUSED)
7911 unsigned int i;
7912 Elf_Internal_Shdr **hdrpp;
7913 const char *name;
7914 asection *sec;
7916 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
7917 is nonzero. This is for compatibility with old objects, which used
7918 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
7919 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
7920 mips_set_isa_flags (abfd);
7922 /* Set the sh_info field for .gptab sections and other appropriate
7923 info for each special section. */
7924 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
7925 i < elf_numsections (abfd);
7926 i++, hdrpp++)
7928 switch ((*hdrpp)->sh_type)
7930 case SHT_MIPS_MSYM:
7931 case SHT_MIPS_LIBLIST:
7932 sec = bfd_get_section_by_name (abfd, ".dynstr");
7933 if (sec != NULL)
7934 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7935 break;
7937 case SHT_MIPS_GPTAB:
7938 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
7939 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
7940 BFD_ASSERT (name != NULL
7941 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
7942 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
7943 BFD_ASSERT (sec != NULL);
7944 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
7945 break;
7947 case SHT_MIPS_CONTENT:
7948 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
7949 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
7950 BFD_ASSERT (name != NULL
7951 && strncmp (name, ".MIPS.content",
7952 sizeof ".MIPS.content" - 1) == 0);
7953 sec = bfd_get_section_by_name (abfd,
7954 name + sizeof ".MIPS.content" - 1);
7955 BFD_ASSERT (sec != NULL);
7956 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7957 break;
7959 case SHT_MIPS_SYMBOL_LIB:
7960 sec = bfd_get_section_by_name (abfd, ".dynsym");
7961 if (sec != NULL)
7962 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7963 sec = bfd_get_section_by_name (abfd, ".liblist");
7964 if (sec != NULL)
7965 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
7966 break;
7968 case SHT_MIPS_EVENTS:
7969 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
7970 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
7971 BFD_ASSERT (name != NULL);
7972 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
7973 sec = bfd_get_section_by_name (abfd,
7974 name + sizeof ".MIPS.events" - 1);
7975 else
7977 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
7978 sizeof ".MIPS.post_rel" - 1) == 0);
7979 sec = bfd_get_section_by_name (abfd,
7980 (name
7981 + sizeof ".MIPS.post_rel" - 1));
7983 BFD_ASSERT (sec != NULL);
7984 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
7985 break;
7991 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
7992 segments. */
7995 _bfd_mips_elf_additional_program_headers (bfd *abfd)
7997 asection *s;
7998 int ret = 0;
8000 /* See if we need a PT_MIPS_REGINFO segment. */
8001 s = bfd_get_section_by_name (abfd, ".reginfo");
8002 if (s && (s->flags & SEC_LOAD))
8003 ++ret;
8005 /* See if we need a PT_MIPS_OPTIONS segment. */
8006 if (IRIX_COMPAT (abfd) == ict_irix6
8007 && bfd_get_section_by_name (abfd,
8008 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
8009 ++ret;
8011 /* See if we need a PT_MIPS_RTPROC segment. */
8012 if (IRIX_COMPAT (abfd) == ict_irix5
8013 && bfd_get_section_by_name (abfd, ".dynamic")
8014 && bfd_get_section_by_name (abfd, ".mdebug"))
8015 ++ret;
8017 return ret;
8020 /* Modify the segment map for an IRIX5 executable. */
8022 bfd_boolean
8023 _bfd_mips_elf_modify_segment_map (bfd *abfd,
8024 struct bfd_link_info *info ATTRIBUTE_UNUSED)
8026 asection *s;
8027 struct elf_segment_map *m, **pm;
8028 bfd_size_type amt;
8030 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
8031 segment. */
8032 s = bfd_get_section_by_name (abfd, ".reginfo");
8033 if (s != NULL && (s->flags & SEC_LOAD) != 0)
8035 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8036 if (m->p_type == PT_MIPS_REGINFO)
8037 break;
8038 if (m == NULL)
8040 amt = sizeof *m;
8041 m = bfd_zalloc (abfd, amt);
8042 if (m == NULL)
8043 return FALSE;
8045 m->p_type = PT_MIPS_REGINFO;
8046 m->count = 1;
8047 m->sections[0] = s;
8049 /* We want to put it after the PHDR and INTERP segments. */
8050 pm = &elf_tdata (abfd)->segment_map;
8051 while (*pm != NULL
8052 && ((*pm)->p_type == PT_PHDR
8053 || (*pm)->p_type == PT_INTERP))
8054 pm = &(*pm)->next;
8056 m->next = *pm;
8057 *pm = m;
8061 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
8062 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
8063 PT_MIPS_OPTIONS segment immediately following the program header
8064 table. */
8065 if (NEWABI_P (abfd)
8066 /* On non-IRIX6 new abi, we'll have already created a segment
8067 for this section, so don't create another. I'm not sure this
8068 is not also the case for IRIX 6, but I can't test it right
8069 now. */
8070 && IRIX_COMPAT (abfd) == ict_irix6)
8072 for (s = abfd->sections; s; s = s->next)
8073 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
8074 break;
8076 if (s)
8078 struct elf_segment_map *options_segment;
8080 pm = &elf_tdata (abfd)->segment_map;
8081 while (*pm != NULL
8082 && ((*pm)->p_type == PT_PHDR
8083 || (*pm)->p_type == PT_INTERP))
8084 pm = &(*pm)->next;
8086 amt = sizeof (struct elf_segment_map);
8087 options_segment = bfd_zalloc (abfd, amt);
8088 options_segment->next = *pm;
8089 options_segment->p_type = PT_MIPS_OPTIONS;
8090 options_segment->p_flags = PF_R;
8091 options_segment->p_flags_valid = TRUE;
8092 options_segment->count = 1;
8093 options_segment->sections[0] = s;
8094 *pm = options_segment;
8097 else
8099 if (IRIX_COMPAT (abfd) == ict_irix5)
8101 /* If there are .dynamic and .mdebug sections, we make a room
8102 for the RTPROC header. FIXME: Rewrite without section names. */
8103 if (bfd_get_section_by_name (abfd, ".interp") == NULL
8104 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
8105 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
8107 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8108 if (m->p_type == PT_MIPS_RTPROC)
8109 break;
8110 if (m == NULL)
8112 amt = sizeof *m;
8113 m = bfd_zalloc (abfd, amt);
8114 if (m == NULL)
8115 return FALSE;
8117 m->p_type = PT_MIPS_RTPROC;
8119 s = bfd_get_section_by_name (abfd, ".rtproc");
8120 if (s == NULL)
8122 m->count = 0;
8123 m->p_flags = 0;
8124 m->p_flags_valid = 1;
8126 else
8128 m->count = 1;
8129 m->sections[0] = s;
8132 /* We want to put it after the DYNAMIC segment. */
8133 pm = &elf_tdata (abfd)->segment_map;
8134 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
8135 pm = &(*pm)->next;
8136 if (*pm != NULL)
8137 pm = &(*pm)->next;
8139 m->next = *pm;
8140 *pm = m;
8144 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
8145 .dynstr, .dynsym, and .hash sections, and everything in
8146 between. */
8147 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
8148 pm = &(*pm)->next)
8149 if ((*pm)->p_type == PT_DYNAMIC)
8150 break;
8151 m = *pm;
8152 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
8154 /* For a normal mips executable the permissions for the PT_DYNAMIC
8155 segment are read, write and execute. We do that here since
8156 the code in elf.c sets only the read permission. This matters
8157 sometimes for the dynamic linker. */
8158 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
8160 m->p_flags = PF_R | PF_W | PF_X;
8161 m->p_flags_valid = 1;
8164 if (m != NULL
8165 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
8167 static const char *sec_names[] =
8169 ".dynamic", ".dynstr", ".dynsym", ".hash"
8171 bfd_vma low, high;
8172 unsigned int i, c;
8173 struct elf_segment_map *n;
8175 low = ~(bfd_vma) 0;
8176 high = 0;
8177 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
8179 s = bfd_get_section_by_name (abfd, sec_names[i]);
8180 if (s != NULL && (s->flags & SEC_LOAD) != 0)
8182 bfd_size_type sz;
8184 if (low > s->vma)
8185 low = s->vma;
8186 sz = s->size;
8187 if (high < s->vma + sz)
8188 high = s->vma + sz;
8192 c = 0;
8193 for (s = abfd->sections; s != NULL; s = s->next)
8194 if ((s->flags & SEC_LOAD) != 0
8195 && s->vma >= low
8196 && s->vma + s->size <= high)
8197 ++c;
8199 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
8200 n = bfd_zalloc (abfd, amt);
8201 if (n == NULL)
8202 return FALSE;
8203 *n = *m;
8204 n->count = c;
8206 i = 0;
8207 for (s = abfd->sections; s != NULL; s = s->next)
8209 if ((s->flags & SEC_LOAD) != 0
8210 && s->vma >= low
8211 && s->vma + s->size <= high)
8213 n->sections[i] = s;
8214 ++i;
8218 *pm = n;
8222 return TRUE;
8225 /* Return the section that should be marked against GC for a given
8226 relocation. */
8228 asection *
8229 _bfd_mips_elf_gc_mark_hook (asection *sec,
8230 struct bfd_link_info *info ATTRIBUTE_UNUSED,
8231 Elf_Internal_Rela *rel,
8232 struct elf_link_hash_entry *h,
8233 Elf_Internal_Sym *sym)
8235 /* ??? Do mips16 stub sections need to be handled special? */
8237 if (h != NULL)
8239 switch (ELF_R_TYPE (sec->owner, rel->r_info))
8241 case R_MIPS_GNU_VTINHERIT:
8242 case R_MIPS_GNU_VTENTRY:
8243 break;
8245 default:
8246 switch (h->root.type)
8248 case bfd_link_hash_defined:
8249 case bfd_link_hash_defweak:
8250 return h->root.u.def.section;
8252 case bfd_link_hash_common:
8253 return h->root.u.c.p->section;
8255 default:
8256 break;
8260 else
8261 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
8263 return NULL;
8266 /* Update the got entry reference counts for the section being removed. */
8268 bfd_boolean
8269 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
8270 struct bfd_link_info *info ATTRIBUTE_UNUSED,
8271 asection *sec ATTRIBUTE_UNUSED,
8272 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
8274 #if 0
8275 Elf_Internal_Shdr *symtab_hdr;
8276 struct elf_link_hash_entry **sym_hashes;
8277 bfd_signed_vma *local_got_refcounts;
8278 const Elf_Internal_Rela *rel, *relend;
8279 unsigned long r_symndx;
8280 struct elf_link_hash_entry *h;
8282 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8283 sym_hashes = elf_sym_hashes (abfd);
8284 local_got_refcounts = elf_local_got_refcounts (abfd);
8286 relend = relocs + sec->reloc_count;
8287 for (rel = relocs; rel < relend; rel++)
8288 switch (ELF_R_TYPE (abfd, rel->r_info))
8290 case R_MIPS_GOT16:
8291 case R_MIPS_CALL16:
8292 case R_MIPS_CALL_HI16:
8293 case R_MIPS_CALL_LO16:
8294 case R_MIPS_GOT_HI16:
8295 case R_MIPS_GOT_LO16:
8296 case R_MIPS_GOT_DISP:
8297 case R_MIPS_GOT_PAGE:
8298 case R_MIPS_GOT_OFST:
8299 /* ??? It would seem that the existing MIPS code does no sort
8300 of reference counting or whatnot on its GOT and PLT entries,
8301 so it is not possible to garbage collect them at this time. */
8302 break;
8304 default:
8305 break;
8307 #endif
8309 return TRUE;
8312 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
8313 hiding the old indirect symbol. Process additional relocation
8314 information. Also called for weakdefs, in which case we just let
8315 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
8317 void
8318 _bfd_mips_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
8319 struct elf_link_hash_entry *dir,
8320 struct elf_link_hash_entry *ind)
8322 struct mips_elf_link_hash_entry *dirmips, *indmips;
8324 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
8326 if (ind->root.type != bfd_link_hash_indirect)
8327 return;
8329 dirmips = (struct mips_elf_link_hash_entry *) dir;
8330 indmips = (struct mips_elf_link_hash_entry *) ind;
8331 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
8332 if (indmips->readonly_reloc)
8333 dirmips->readonly_reloc = TRUE;
8334 if (indmips->no_fn_stub)
8335 dirmips->no_fn_stub = TRUE;
8337 if (dirmips->tls_type == 0)
8338 dirmips->tls_type = indmips->tls_type;
8339 else
8340 BFD_ASSERT (indmips->tls_type == 0);
8343 void
8344 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
8345 struct elf_link_hash_entry *entry,
8346 bfd_boolean force_local)
8348 bfd *dynobj;
8349 asection *got;
8350 struct mips_got_info *g;
8351 struct mips_elf_link_hash_entry *h;
8353 h = (struct mips_elf_link_hash_entry *) entry;
8354 if (h->forced_local)
8355 return;
8356 h->forced_local = force_local;
8358 dynobj = elf_hash_table (info)->dynobj;
8359 if (dynobj != NULL && force_local && h->root.type != STT_TLS)
8361 got = mips_elf_got_section (dynobj, FALSE);
8362 g = mips_elf_section_data (got)->u.got_info;
8364 if (g->next)
8366 struct mips_got_entry e;
8367 struct mips_got_info *gg = g;
8369 /* Since we're turning what used to be a global symbol into a
8370 local one, bump up the number of local entries of each GOT
8371 that had an entry for it. This will automatically decrease
8372 the number of global entries, since global_gotno is actually
8373 the upper limit of global entries. */
8374 e.abfd = dynobj;
8375 e.symndx = -1;
8376 e.d.h = h;
8377 e.tls_type = 0;
8379 for (g = g->next; g != gg; g = g->next)
8380 if (htab_find (g->got_entries, &e))
8382 BFD_ASSERT (g->global_gotno > 0);
8383 g->local_gotno++;
8384 g->global_gotno--;
8387 /* If this was a global symbol forced into the primary GOT, we
8388 no longer need an entry for it. We can't release the entry
8389 at this point, but we must at least stop counting it as one
8390 of the symbols that required a forced got entry. */
8391 if (h->root.got.offset == 2)
8393 BFD_ASSERT (gg->assigned_gotno > 0);
8394 gg->assigned_gotno--;
8397 else if (g->global_gotno == 0 && g->global_gotsym == NULL)
8398 /* If we haven't got through GOT allocation yet, just bump up the
8399 number of local entries, as this symbol won't be counted as
8400 global. */
8401 g->local_gotno++;
8402 else if (h->root.got.offset == 1)
8404 /* If we're past non-multi-GOT allocation and this symbol had
8405 been marked for a global got entry, give it a local entry
8406 instead. */
8407 BFD_ASSERT (g->global_gotno > 0);
8408 g->local_gotno++;
8409 g->global_gotno--;
8413 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
8416 #define PDR_SIZE 32
8418 bfd_boolean
8419 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
8420 struct bfd_link_info *info)
8422 asection *o;
8423 bfd_boolean ret = FALSE;
8424 unsigned char *tdata;
8425 size_t i, skip;
8427 o = bfd_get_section_by_name (abfd, ".pdr");
8428 if (! o)
8429 return FALSE;
8430 if (o->size == 0)
8431 return FALSE;
8432 if (o->size % PDR_SIZE != 0)
8433 return FALSE;
8434 if (o->output_section != NULL
8435 && bfd_is_abs_section (o->output_section))
8436 return FALSE;
8438 tdata = bfd_zmalloc (o->size / PDR_SIZE);
8439 if (! tdata)
8440 return FALSE;
8442 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8443 info->keep_memory);
8444 if (!cookie->rels)
8446 free (tdata);
8447 return FALSE;
8450 cookie->rel = cookie->rels;
8451 cookie->relend = cookie->rels + o->reloc_count;
8453 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
8455 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
8457 tdata[i] = 1;
8458 skip ++;
8462 if (skip != 0)
8464 mips_elf_section_data (o)->u.tdata = tdata;
8465 o->size -= skip * PDR_SIZE;
8466 ret = TRUE;
8468 else
8469 free (tdata);
8471 if (! info->keep_memory)
8472 free (cookie->rels);
8474 return ret;
8477 bfd_boolean
8478 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
8480 if (strcmp (sec->name, ".pdr") == 0)
8481 return TRUE;
8482 return FALSE;
8485 bfd_boolean
8486 _bfd_mips_elf_write_section (bfd *output_bfd, asection *sec,
8487 bfd_byte *contents)
8489 bfd_byte *to, *from, *end;
8490 int i;
8492 if (strcmp (sec->name, ".pdr") != 0)
8493 return FALSE;
8495 if (mips_elf_section_data (sec)->u.tdata == NULL)
8496 return FALSE;
8498 to = contents;
8499 end = contents + sec->size;
8500 for (from = contents, i = 0;
8501 from < end;
8502 from += PDR_SIZE, i++)
8504 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
8505 continue;
8506 if (to != from)
8507 memcpy (to, from, PDR_SIZE);
8508 to += PDR_SIZE;
8510 bfd_set_section_contents (output_bfd, sec->output_section, contents,
8511 sec->output_offset, sec->size);
8512 return TRUE;
8515 /* MIPS ELF uses a special find_nearest_line routine in order the
8516 handle the ECOFF debugging information. */
8518 struct mips_elf_find_line
8520 struct ecoff_debug_info d;
8521 struct ecoff_find_line i;
8524 bfd_boolean
8525 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
8526 asymbol **symbols, bfd_vma offset,
8527 const char **filename_ptr,
8528 const char **functionname_ptr,
8529 unsigned int *line_ptr)
8531 asection *msec;
8533 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
8534 filename_ptr, functionname_ptr,
8535 line_ptr))
8536 return TRUE;
8538 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
8539 filename_ptr, functionname_ptr,
8540 line_ptr, ABI_64_P (abfd) ? 8 : 0,
8541 &elf_tdata (abfd)->dwarf2_find_line_info))
8542 return TRUE;
8544 msec = bfd_get_section_by_name (abfd, ".mdebug");
8545 if (msec != NULL)
8547 flagword origflags;
8548 struct mips_elf_find_line *fi;
8549 const struct ecoff_debug_swap * const swap =
8550 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
8552 /* If we are called during a link, mips_elf_final_link may have
8553 cleared the SEC_HAS_CONTENTS field. We force it back on here
8554 if appropriate (which it normally will be). */
8555 origflags = msec->flags;
8556 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
8557 msec->flags |= SEC_HAS_CONTENTS;
8559 fi = elf_tdata (abfd)->find_line_info;
8560 if (fi == NULL)
8562 bfd_size_type external_fdr_size;
8563 char *fraw_src;
8564 char *fraw_end;
8565 struct fdr *fdr_ptr;
8566 bfd_size_type amt = sizeof (struct mips_elf_find_line);
8568 fi = bfd_zalloc (abfd, amt);
8569 if (fi == NULL)
8571 msec->flags = origflags;
8572 return FALSE;
8575 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
8577 msec->flags = origflags;
8578 return FALSE;
8581 /* Swap in the FDR information. */
8582 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
8583 fi->d.fdr = bfd_alloc (abfd, amt);
8584 if (fi->d.fdr == NULL)
8586 msec->flags = origflags;
8587 return FALSE;
8589 external_fdr_size = swap->external_fdr_size;
8590 fdr_ptr = fi->d.fdr;
8591 fraw_src = (char *) fi->d.external_fdr;
8592 fraw_end = (fraw_src
8593 + fi->d.symbolic_header.ifdMax * external_fdr_size);
8594 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
8595 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
8597 elf_tdata (abfd)->find_line_info = fi;
8599 /* Note that we don't bother to ever free this information.
8600 find_nearest_line is either called all the time, as in
8601 objdump -l, so the information should be saved, or it is
8602 rarely called, as in ld error messages, so the memory
8603 wasted is unimportant. Still, it would probably be a
8604 good idea for free_cached_info to throw it away. */
8607 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
8608 &fi->i, filename_ptr, functionname_ptr,
8609 line_ptr))
8611 msec->flags = origflags;
8612 return TRUE;
8615 msec->flags = origflags;
8618 /* Fall back on the generic ELF find_nearest_line routine. */
8620 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
8621 filename_ptr, functionname_ptr,
8622 line_ptr);
8625 /* When are writing out the .options or .MIPS.options section,
8626 remember the bytes we are writing out, so that we can install the
8627 GP value in the section_processing routine. */
8629 bfd_boolean
8630 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
8631 const void *location,
8632 file_ptr offset, bfd_size_type count)
8634 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
8636 bfd_byte *c;
8638 if (elf_section_data (section) == NULL)
8640 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
8641 section->used_by_bfd = bfd_zalloc (abfd, amt);
8642 if (elf_section_data (section) == NULL)
8643 return FALSE;
8645 c = mips_elf_section_data (section)->u.tdata;
8646 if (c == NULL)
8648 c = bfd_zalloc (abfd, section->size);
8649 if (c == NULL)
8650 return FALSE;
8651 mips_elf_section_data (section)->u.tdata = c;
8654 memcpy (c + offset, location, count);
8657 return _bfd_elf_set_section_contents (abfd, section, location, offset,
8658 count);
8661 /* This is almost identical to bfd_generic_get_... except that some
8662 MIPS relocations need to be handled specially. Sigh. */
8664 bfd_byte *
8665 _bfd_elf_mips_get_relocated_section_contents
8666 (bfd *abfd,
8667 struct bfd_link_info *link_info,
8668 struct bfd_link_order *link_order,
8669 bfd_byte *data,
8670 bfd_boolean relocatable,
8671 asymbol **symbols)
8673 /* Get enough memory to hold the stuff */
8674 bfd *input_bfd = link_order->u.indirect.section->owner;
8675 asection *input_section = link_order->u.indirect.section;
8676 bfd_size_type sz;
8678 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8679 arelent **reloc_vector = NULL;
8680 long reloc_count;
8682 if (reloc_size < 0)
8683 goto error_return;
8685 reloc_vector = bfd_malloc (reloc_size);
8686 if (reloc_vector == NULL && reloc_size != 0)
8687 goto error_return;
8689 /* read in the section */
8690 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
8691 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
8692 goto error_return;
8694 reloc_count = bfd_canonicalize_reloc (input_bfd,
8695 input_section,
8696 reloc_vector,
8697 symbols);
8698 if (reloc_count < 0)
8699 goto error_return;
8701 if (reloc_count > 0)
8703 arelent **parent;
8704 /* for mips */
8705 int gp_found;
8706 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8709 struct bfd_hash_entry *h;
8710 struct bfd_link_hash_entry *lh;
8711 /* Skip all this stuff if we aren't mixing formats. */
8712 if (abfd && input_bfd
8713 && abfd->xvec == input_bfd->xvec)
8714 lh = 0;
8715 else
8717 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
8718 lh = (struct bfd_link_hash_entry *) h;
8720 lookup:
8721 if (lh)
8723 switch (lh->type)
8725 case bfd_link_hash_undefined:
8726 case bfd_link_hash_undefweak:
8727 case bfd_link_hash_common:
8728 gp_found = 0;
8729 break;
8730 case bfd_link_hash_defined:
8731 case bfd_link_hash_defweak:
8732 gp_found = 1;
8733 gp = lh->u.def.value;
8734 break;
8735 case bfd_link_hash_indirect:
8736 case bfd_link_hash_warning:
8737 lh = lh->u.i.link;
8738 /* @@FIXME ignoring warning for now */
8739 goto lookup;
8740 case bfd_link_hash_new:
8741 default:
8742 abort ();
8745 else
8746 gp_found = 0;
8748 /* end mips */
8749 for (parent = reloc_vector; *parent != NULL; parent++)
8751 char *error_message = NULL;
8752 bfd_reloc_status_type r;
8754 /* Specific to MIPS: Deal with relocation types that require
8755 knowing the gp of the output bfd. */
8756 asymbol *sym = *(*parent)->sym_ptr_ptr;
8757 if (bfd_is_abs_section (sym->section) && abfd)
8759 /* The special_function wouldn't get called anyway. */
8761 else if (!gp_found)
8763 /* The gp isn't there; let the special function code
8764 fall over on its own. */
8766 else if ((*parent)->howto->special_function
8767 == _bfd_mips_elf32_gprel16_reloc)
8769 /* bypass special_function call */
8770 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
8771 input_section, relocatable,
8772 data, gp);
8773 goto skip_bfd_perform_relocation;
8775 /* end mips specific stuff */
8777 r = bfd_perform_relocation (input_bfd, *parent, data, input_section,
8778 relocatable ? abfd : NULL,
8779 &error_message);
8780 skip_bfd_perform_relocation:
8782 if (relocatable)
8784 asection *os = input_section->output_section;
8786 /* A partial link, so keep the relocs */
8787 os->orelocation[os->reloc_count] = *parent;
8788 os->reloc_count++;
8791 if (r != bfd_reloc_ok)
8793 switch (r)
8795 case bfd_reloc_undefined:
8796 if (!((*link_info->callbacks->undefined_symbol)
8797 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8798 input_bfd, input_section, (*parent)->address,
8799 TRUE)))
8800 goto error_return;
8801 break;
8802 case bfd_reloc_dangerous:
8803 BFD_ASSERT (error_message != NULL);
8804 if (!((*link_info->callbacks->reloc_dangerous)
8805 (link_info, error_message, input_bfd, input_section,
8806 (*parent)->address)))
8807 goto error_return;
8808 break;
8809 case bfd_reloc_overflow:
8810 if (!((*link_info->callbacks->reloc_overflow)
8811 (link_info, NULL,
8812 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8813 (*parent)->howto->name, (*parent)->addend,
8814 input_bfd, input_section, (*parent)->address)))
8815 goto error_return;
8816 break;
8817 case bfd_reloc_outofrange:
8818 default:
8819 abort ();
8820 break;
8826 if (reloc_vector != NULL)
8827 free (reloc_vector);
8828 return data;
8830 error_return:
8831 if (reloc_vector != NULL)
8832 free (reloc_vector);
8833 return NULL;
8836 /* Create a MIPS ELF linker hash table. */
8838 struct bfd_link_hash_table *
8839 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
8841 struct mips_elf_link_hash_table *ret;
8842 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
8844 ret = bfd_malloc (amt);
8845 if (ret == NULL)
8846 return NULL;
8848 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
8849 mips_elf_link_hash_newfunc))
8851 free (ret);
8852 return NULL;
8855 #if 0
8856 /* We no longer use this. */
8857 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
8858 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
8859 #endif
8860 ret->procedure_count = 0;
8861 ret->compact_rel_size = 0;
8862 ret->use_rld_obj_head = FALSE;
8863 ret->rld_value = 0;
8864 ret->mips16_stubs_seen = FALSE;
8866 return &ret->root.root;
8869 /* We need to use a special link routine to handle the .reginfo and
8870 the .mdebug sections. We need to merge all instances of these
8871 sections together, not write them all out sequentially. */
8873 bfd_boolean
8874 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
8876 asection **secpp;
8877 asection *o;
8878 struct bfd_link_order *p;
8879 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
8880 asection *rtproc_sec;
8881 Elf32_RegInfo reginfo;
8882 struct ecoff_debug_info debug;
8883 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8884 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
8885 HDRR *symhdr = &debug.symbolic_header;
8886 void *mdebug_handle = NULL;
8887 asection *s;
8888 EXTR esym;
8889 unsigned int i;
8890 bfd_size_type amt;
8892 static const char * const secname[] =
8894 ".text", ".init", ".fini", ".data",
8895 ".rodata", ".sdata", ".sbss", ".bss"
8897 static const int sc[] =
8899 scText, scInit, scFini, scData,
8900 scRData, scSData, scSBss, scBss
8903 /* We'd carefully arranged the dynamic symbol indices, and then the
8904 generic size_dynamic_sections renumbered them out from under us.
8905 Rather than trying somehow to prevent the renumbering, just do
8906 the sort again. */
8907 if (elf_hash_table (info)->dynamic_sections_created)
8909 bfd *dynobj;
8910 asection *got;
8911 struct mips_got_info *g;
8912 bfd_size_type dynsecsymcount;
8914 /* When we resort, we must tell mips_elf_sort_hash_table what
8915 the lowest index it may use is. That's the number of section
8916 symbols we're going to add. The generic ELF linker only
8917 adds these symbols when building a shared object. Note that
8918 we count the sections after (possibly) removing the .options
8919 section above. */
8921 dynsecsymcount = 0;
8922 if (info->shared)
8924 asection * p;
8926 for (p = abfd->sections; p ; p = p->next)
8927 if ((p->flags & SEC_EXCLUDE) == 0
8928 && (p->flags & SEC_ALLOC) != 0
8929 && !(*bed->elf_backend_omit_section_dynsym) (abfd, info, p))
8930 ++ dynsecsymcount;
8933 if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
8934 return FALSE;
8936 /* Make sure we didn't grow the global .got region. */
8937 dynobj = elf_hash_table (info)->dynobj;
8938 got = mips_elf_got_section (dynobj, FALSE);
8939 g = mips_elf_section_data (got)->u.got_info;
8941 if (g->global_gotsym != NULL)
8942 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
8943 - g->global_gotsym->dynindx)
8944 <= g->global_gotno);
8947 /* Get a value for the GP register. */
8948 if (elf_gp (abfd) == 0)
8950 struct bfd_link_hash_entry *h;
8952 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
8953 if (h != NULL && h->type == bfd_link_hash_defined)
8954 elf_gp (abfd) = (h->u.def.value
8955 + h->u.def.section->output_section->vma
8956 + h->u.def.section->output_offset);
8957 else if (info->relocatable)
8959 bfd_vma lo = MINUS_ONE;
8961 /* Find the GP-relative section with the lowest offset. */
8962 for (o = abfd->sections; o != NULL; o = o->next)
8963 if (o->vma < lo
8964 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
8965 lo = o->vma;
8967 /* And calculate GP relative to that. */
8968 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
8970 else
8972 /* If the relocate_section function needs to do a reloc
8973 involving the GP value, it should make a reloc_dangerous
8974 callback to warn that GP is not defined. */
8978 /* Go through the sections and collect the .reginfo and .mdebug
8979 information. */
8980 reginfo_sec = NULL;
8981 mdebug_sec = NULL;
8982 gptab_data_sec = NULL;
8983 gptab_bss_sec = NULL;
8984 for (o = abfd->sections; o != NULL; o = o->next)
8986 if (strcmp (o->name, ".reginfo") == 0)
8988 memset (&reginfo, 0, sizeof reginfo);
8990 /* We have found the .reginfo section in the output file.
8991 Look through all the link_orders comprising it and merge
8992 the information together. */
8993 for (p = o->link_order_head; p != NULL; p = p->next)
8995 asection *input_section;
8996 bfd *input_bfd;
8997 Elf32_External_RegInfo ext;
8998 Elf32_RegInfo sub;
9000 if (p->type != bfd_indirect_link_order)
9002 if (p->type == bfd_data_link_order)
9003 continue;
9004 abort ();
9007 input_section = p->u.indirect.section;
9008 input_bfd = input_section->owner;
9010 if (! bfd_get_section_contents (input_bfd, input_section,
9011 &ext, 0, sizeof ext))
9012 return FALSE;
9014 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
9016 reginfo.ri_gprmask |= sub.ri_gprmask;
9017 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
9018 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
9019 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
9020 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
9022 /* ri_gp_value is set by the function
9023 mips_elf32_section_processing when the section is
9024 finally written out. */
9026 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9027 elf_link_input_bfd ignores this section. */
9028 input_section->flags &= ~SEC_HAS_CONTENTS;
9031 /* Size has been set in _bfd_mips_elf_always_size_sections. */
9032 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
9034 /* Skip this section later on (I don't think this currently
9035 matters, but someday it might). */
9036 o->link_order_head = NULL;
9038 reginfo_sec = o;
9041 if (strcmp (o->name, ".mdebug") == 0)
9043 struct extsym_info einfo;
9044 bfd_vma last;
9046 /* We have found the .mdebug section in the output file.
9047 Look through all the link_orders comprising it and merge
9048 the information together. */
9049 symhdr->magic = swap->sym_magic;
9050 /* FIXME: What should the version stamp be? */
9051 symhdr->vstamp = 0;
9052 symhdr->ilineMax = 0;
9053 symhdr->cbLine = 0;
9054 symhdr->idnMax = 0;
9055 symhdr->ipdMax = 0;
9056 symhdr->isymMax = 0;
9057 symhdr->ioptMax = 0;
9058 symhdr->iauxMax = 0;
9059 symhdr->issMax = 0;
9060 symhdr->issExtMax = 0;
9061 symhdr->ifdMax = 0;
9062 symhdr->crfd = 0;
9063 symhdr->iextMax = 0;
9065 /* We accumulate the debugging information itself in the
9066 debug_info structure. */
9067 debug.line = NULL;
9068 debug.external_dnr = NULL;
9069 debug.external_pdr = NULL;
9070 debug.external_sym = NULL;
9071 debug.external_opt = NULL;
9072 debug.external_aux = NULL;
9073 debug.ss = NULL;
9074 debug.ssext = debug.ssext_end = NULL;
9075 debug.external_fdr = NULL;
9076 debug.external_rfd = NULL;
9077 debug.external_ext = debug.external_ext_end = NULL;
9079 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9080 if (mdebug_handle == NULL)
9081 return FALSE;
9083 esym.jmptbl = 0;
9084 esym.cobol_main = 0;
9085 esym.weakext = 0;
9086 esym.reserved = 0;
9087 esym.ifd = ifdNil;
9088 esym.asym.iss = issNil;
9089 esym.asym.st = stLocal;
9090 esym.asym.reserved = 0;
9091 esym.asym.index = indexNil;
9092 last = 0;
9093 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
9095 esym.asym.sc = sc[i];
9096 s = bfd_get_section_by_name (abfd, secname[i]);
9097 if (s != NULL)
9099 esym.asym.value = s->vma;
9100 last = s->vma + s->size;
9102 else
9103 esym.asym.value = last;
9104 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
9105 secname[i], &esym))
9106 return FALSE;
9109 for (p = o->link_order_head; p != NULL; p = p->next)
9111 asection *input_section;
9112 bfd *input_bfd;
9113 const struct ecoff_debug_swap *input_swap;
9114 struct ecoff_debug_info input_debug;
9115 char *eraw_src;
9116 char *eraw_end;
9118 if (p->type != bfd_indirect_link_order)
9120 if (p->type == bfd_data_link_order)
9121 continue;
9122 abort ();
9125 input_section = p->u.indirect.section;
9126 input_bfd = input_section->owner;
9128 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
9129 || (get_elf_backend_data (input_bfd)
9130 ->elf_backend_ecoff_debug_swap) == NULL)
9132 /* I don't know what a non MIPS ELF bfd would be
9133 doing with a .mdebug section, but I don't really
9134 want to deal with it. */
9135 continue;
9138 input_swap = (get_elf_backend_data (input_bfd)
9139 ->elf_backend_ecoff_debug_swap);
9141 BFD_ASSERT (p->size == input_section->size);
9143 /* The ECOFF linking code expects that we have already
9144 read in the debugging information and set up an
9145 ecoff_debug_info structure, so we do that now. */
9146 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
9147 &input_debug))
9148 return FALSE;
9150 if (! (bfd_ecoff_debug_accumulate
9151 (mdebug_handle, abfd, &debug, swap, input_bfd,
9152 &input_debug, input_swap, info)))
9153 return FALSE;
9155 /* Loop through the external symbols. For each one with
9156 interesting information, try to find the symbol in
9157 the linker global hash table and save the information
9158 for the output external symbols. */
9159 eraw_src = input_debug.external_ext;
9160 eraw_end = (eraw_src
9161 + (input_debug.symbolic_header.iextMax
9162 * input_swap->external_ext_size));
9163 for (;
9164 eraw_src < eraw_end;
9165 eraw_src += input_swap->external_ext_size)
9167 EXTR ext;
9168 const char *name;
9169 struct mips_elf_link_hash_entry *h;
9171 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
9172 if (ext.asym.sc == scNil
9173 || ext.asym.sc == scUndefined
9174 || ext.asym.sc == scSUndefined)
9175 continue;
9177 name = input_debug.ssext + ext.asym.iss;
9178 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
9179 name, FALSE, FALSE, TRUE);
9180 if (h == NULL || h->esym.ifd != -2)
9181 continue;
9183 if (ext.ifd != -1)
9185 BFD_ASSERT (ext.ifd
9186 < input_debug.symbolic_header.ifdMax);
9187 ext.ifd = input_debug.ifdmap[ext.ifd];
9190 h->esym = ext;
9193 /* Free up the information we just read. */
9194 free (input_debug.line);
9195 free (input_debug.external_dnr);
9196 free (input_debug.external_pdr);
9197 free (input_debug.external_sym);
9198 free (input_debug.external_opt);
9199 free (input_debug.external_aux);
9200 free (input_debug.ss);
9201 free (input_debug.ssext);
9202 free (input_debug.external_fdr);
9203 free (input_debug.external_rfd);
9204 free (input_debug.external_ext);
9206 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9207 elf_link_input_bfd ignores this section. */
9208 input_section->flags &= ~SEC_HAS_CONTENTS;
9211 if (SGI_COMPAT (abfd) && info->shared)
9213 /* Create .rtproc section. */
9214 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
9215 if (rtproc_sec == NULL)
9217 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
9218 | SEC_LINKER_CREATED | SEC_READONLY);
9220 rtproc_sec = bfd_make_section (abfd, ".rtproc");
9221 if (rtproc_sec == NULL
9222 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
9223 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
9224 return FALSE;
9227 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
9228 info, rtproc_sec,
9229 &debug))
9230 return FALSE;
9233 /* Build the external symbol information. */
9234 einfo.abfd = abfd;
9235 einfo.info = info;
9236 einfo.debug = &debug;
9237 einfo.swap = swap;
9238 einfo.failed = FALSE;
9239 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9240 mips_elf_output_extsym, &einfo);
9241 if (einfo.failed)
9242 return FALSE;
9244 /* Set the size of the .mdebug section. */
9245 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
9247 /* Skip this section later on (I don't think this currently
9248 matters, but someday it might). */
9249 o->link_order_head = NULL;
9251 mdebug_sec = o;
9254 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
9256 const char *subname;
9257 unsigned int c;
9258 Elf32_gptab *tab;
9259 Elf32_External_gptab *ext_tab;
9260 unsigned int j;
9262 /* The .gptab.sdata and .gptab.sbss sections hold
9263 information describing how the small data area would
9264 change depending upon the -G switch. These sections
9265 not used in executables files. */
9266 if (! info->relocatable)
9268 for (p = o->link_order_head; p != NULL; p = p->next)
9270 asection *input_section;
9272 if (p->type != bfd_indirect_link_order)
9274 if (p->type == bfd_data_link_order)
9275 continue;
9276 abort ();
9279 input_section = p->u.indirect.section;
9281 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9282 elf_link_input_bfd ignores this section. */
9283 input_section->flags &= ~SEC_HAS_CONTENTS;
9286 /* Skip this section later on (I don't think this
9287 currently matters, but someday it might). */
9288 o->link_order_head = NULL;
9290 /* Really remove the section. */
9291 for (secpp = &abfd->sections;
9292 *secpp != o;
9293 secpp = &(*secpp)->next)
9295 bfd_section_list_remove (abfd, secpp);
9296 --abfd->section_count;
9298 continue;
9301 /* There is one gptab for initialized data, and one for
9302 uninitialized data. */
9303 if (strcmp (o->name, ".gptab.sdata") == 0)
9304 gptab_data_sec = o;
9305 else if (strcmp (o->name, ".gptab.sbss") == 0)
9306 gptab_bss_sec = o;
9307 else
9309 (*_bfd_error_handler)
9310 (_("%s: illegal section name `%s'"),
9311 bfd_get_filename (abfd), o->name);
9312 bfd_set_error (bfd_error_nonrepresentable_section);
9313 return FALSE;
9316 /* The linker script always combines .gptab.data and
9317 .gptab.sdata into .gptab.sdata, and likewise for
9318 .gptab.bss and .gptab.sbss. It is possible that there is
9319 no .sdata or .sbss section in the output file, in which
9320 case we must change the name of the output section. */
9321 subname = o->name + sizeof ".gptab" - 1;
9322 if (bfd_get_section_by_name (abfd, subname) == NULL)
9324 if (o == gptab_data_sec)
9325 o->name = ".gptab.data";
9326 else
9327 o->name = ".gptab.bss";
9328 subname = o->name + sizeof ".gptab" - 1;
9329 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
9332 /* Set up the first entry. */
9333 c = 1;
9334 amt = c * sizeof (Elf32_gptab);
9335 tab = bfd_malloc (amt);
9336 if (tab == NULL)
9337 return FALSE;
9338 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
9339 tab[0].gt_header.gt_unused = 0;
9341 /* Combine the input sections. */
9342 for (p = o->link_order_head; p != NULL; p = p->next)
9344 asection *input_section;
9345 bfd *input_bfd;
9346 bfd_size_type size;
9347 unsigned long last;
9348 bfd_size_type gpentry;
9350 if (p->type != bfd_indirect_link_order)
9352 if (p->type == bfd_data_link_order)
9353 continue;
9354 abort ();
9357 input_section = p->u.indirect.section;
9358 input_bfd = input_section->owner;
9360 /* Combine the gptab entries for this input section one
9361 by one. We know that the input gptab entries are
9362 sorted by ascending -G value. */
9363 size = input_section->size;
9364 last = 0;
9365 for (gpentry = sizeof (Elf32_External_gptab);
9366 gpentry < size;
9367 gpentry += sizeof (Elf32_External_gptab))
9369 Elf32_External_gptab ext_gptab;
9370 Elf32_gptab int_gptab;
9371 unsigned long val;
9372 unsigned long add;
9373 bfd_boolean exact;
9374 unsigned int look;
9376 if (! (bfd_get_section_contents
9377 (input_bfd, input_section, &ext_gptab, gpentry,
9378 sizeof (Elf32_External_gptab))))
9380 free (tab);
9381 return FALSE;
9384 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
9385 &int_gptab);
9386 val = int_gptab.gt_entry.gt_g_value;
9387 add = int_gptab.gt_entry.gt_bytes - last;
9389 exact = FALSE;
9390 for (look = 1; look < c; look++)
9392 if (tab[look].gt_entry.gt_g_value >= val)
9393 tab[look].gt_entry.gt_bytes += add;
9395 if (tab[look].gt_entry.gt_g_value == val)
9396 exact = TRUE;
9399 if (! exact)
9401 Elf32_gptab *new_tab;
9402 unsigned int max;
9404 /* We need a new table entry. */
9405 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9406 new_tab = bfd_realloc (tab, amt);
9407 if (new_tab == NULL)
9409 free (tab);
9410 return FALSE;
9412 tab = new_tab;
9413 tab[c].gt_entry.gt_g_value = val;
9414 tab[c].gt_entry.gt_bytes = add;
9416 /* Merge in the size for the next smallest -G
9417 value, since that will be implied by this new
9418 value. */
9419 max = 0;
9420 for (look = 1; look < c; look++)
9422 if (tab[look].gt_entry.gt_g_value < val
9423 && (max == 0
9424 || (tab[look].gt_entry.gt_g_value
9425 > tab[max].gt_entry.gt_g_value)))
9426 max = look;
9428 if (max != 0)
9429 tab[c].gt_entry.gt_bytes +=
9430 tab[max].gt_entry.gt_bytes;
9432 ++c;
9435 last = int_gptab.gt_entry.gt_bytes;
9438 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9439 elf_link_input_bfd ignores this section. */
9440 input_section->flags &= ~SEC_HAS_CONTENTS;
9443 /* The table must be sorted by -G value. */
9444 if (c > 2)
9445 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
9447 /* Swap out the table. */
9448 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9449 ext_tab = bfd_alloc (abfd, amt);
9450 if (ext_tab == NULL)
9452 free (tab);
9453 return FALSE;
9456 for (j = 0; j < c; j++)
9457 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
9458 free (tab);
9460 o->size = c * sizeof (Elf32_External_gptab);
9461 o->contents = (bfd_byte *) ext_tab;
9463 /* Skip this section later on (I don't think this currently
9464 matters, but someday it might). */
9465 o->link_order_head = NULL;
9469 /* Invoke the regular ELF backend linker to do all the work. */
9470 if (!bfd_elf_final_link (abfd, info))
9471 return FALSE;
9473 /* Now write out the computed sections. */
9475 if (reginfo_sec != NULL)
9477 Elf32_External_RegInfo ext;
9479 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9480 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
9481 return FALSE;
9484 if (mdebug_sec != NULL)
9486 BFD_ASSERT (abfd->output_has_begun);
9487 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
9488 swap, info,
9489 mdebug_sec->filepos))
9490 return FALSE;
9492 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
9495 if (gptab_data_sec != NULL)
9497 if (! bfd_set_section_contents (abfd, gptab_data_sec,
9498 gptab_data_sec->contents,
9499 0, gptab_data_sec->size))
9500 return FALSE;
9503 if (gptab_bss_sec != NULL)
9505 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
9506 gptab_bss_sec->contents,
9507 0, gptab_bss_sec->size))
9508 return FALSE;
9511 if (SGI_COMPAT (abfd))
9513 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
9514 if (rtproc_sec != NULL)
9516 if (! bfd_set_section_contents (abfd, rtproc_sec,
9517 rtproc_sec->contents,
9518 0, rtproc_sec->size))
9519 return FALSE;
9523 return TRUE;
9526 /* Structure for saying that BFD machine EXTENSION extends BASE. */
9528 struct mips_mach_extension {
9529 unsigned long extension, base;
9533 /* An array describing how BFD machines relate to one another. The entries
9534 are ordered topologically with MIPS I extensions listed last. */
9536 static const struct mips_mach_extension mips_mach_extensions[] = {
9537 /* MIPS64 extensions. */
9538 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
9539 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
9541 /* MIPS V extensions. */
9542 { bfd_mach_mipsisa64, bfd_mach_mips5 },
9544 /* R10000 extensions. */
9545 { bfd_mach_mips12000, bfd_mach_mips10000 },
9547 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
9548 vr5400 ISA, but doesn't include the multimedia stuff. It seems
9549 better to allow vr5400 and vr5500 code to be merged anyway, since
9550 many libraries will just use the core ISA. Perhaps we could add
9551 some sort of ASE flag if this ever proves a problem. */
9552 { bfd_mach_mips5500, bfd_mach_mips5400 },
9553 { bfd_mach_mips5400, bfd_mach_mips5000 },
9555 /* MIPS IV extensions. */
9556 { bfd_mach_mips5, bfd_mach_mips8000 },
9557 { bfd_mach_mips10000, bfd_mach_mips8000 },
9558 { bfd_mach_mips5000, bfd_mach_mips8000 },
9559 { bfd_mach_mips7000, bfd_mach_mips8000 },
9560 { bfd_mach_mips9000, bfd_mach_mips8000 },
9562 /* VR4100 extensions. */
9563 { bfd_mach_mips4120, bfd_mach_mips4100 },
9564 { bfd_mach_mips4111, bfd_mach_mips4100 },
9566 /* MIPS III extensions. */
9567 { bfd_mach_mips8000, bfd_mach_mips4000 },
9568 { bfd_mach_mips4650, bfd_mach_mips4000 },
9569 { bfd_mach_mips4600, bfd_mach_mips4000 },
9570 { bfd_mach_mips4400, bfd_mach_mips4000 },
9571 { bfd_mach_mips4300, bfd_mach_mips4000 },
9572 { bfd_mach_mips4100, bfd_mach_mips4000 },
9573 { bfd_mach_mips4010, bfd_mach_mips4000 },
9575 /* MIPS32 extensions. */
9576 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
9578 /* MIPS II extensions. */
9579 { bfd_mach_mips4000, bfd_mach_mips6000 },
9580 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
9582 /* MIPS I extensions. */
9583 { bfd_mach_mips6000, bfd_mach_mips3000 },
9584 { bfd_mach_mips3900, bfd_mach_mips3000 }
9588 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
9590 static bfd_boolean
9591 mips_mach_extends_p (unsigned long base, unsigned long extension)
9593 size_t i;
9595 for (i = 0; extension != base && i < ARRAY_SIZE (mips_mach_extensions); i++)
9596 if (extension == mips_mach_extensions[i].extension)
9597 extension = mips_mach_extensions[i].base;
9599 return extension == base;
9603 /* Return true if the given ELF header flags describe a 32-bit binary. */
9605 static bfd_boolean
9606 mips_32bit_flags_p (flagword flags)
9608 return ((flags & EF_MIPS_32BITMODE) != 0
9609 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
9610 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
9611 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
9612 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
9613 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
9614 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
9618 /* Merge backend specific data from an object file to the output
9619 object file when linking. */
9621 bfd_boolean
9622 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
9624 flagword old_flags;
9625 flagword new_flags;
9626 bfd_boolean ok;
9627 bfd_boolean null_input_bfd = TRUE;
9628 asection *sec;
9630 /* Check if we have the same endianess */
9631 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
9633 (*_bfd_error_handler)
9634 (_("%B: endianness incompatible with that of the selected emulation"),
9635 ibfd);
9636 return FALSE;
9639 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
9640 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
9641 return TRUE;
9643 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
9645 (*_bfd_error_handler)
9646 (_("%B: ABI is incompatible with that of the selected emulation"),
9647 ibfd);
9648 return FALSE;
9651 new_flags = elf_elfheader (ibfd)->e_flags;
9652 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
9653 old_flags = elf_elfheader (obfd)->e_flags;
9655 if (! elf_flags_init (obfd))
9657 elf_flags_init (obfd) = TRUE;
9658 elf_elfheader (obfd)->e_flags = new_flags;
9659 elf_elfheader (obfd)->e_ident[EI_CLASS]
9660 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
9662 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
9663 && bfd_get_arch_info (obfd)->the_default)
9665 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
9666 bfd_get_mach (ibfd)))
9667 return FALSE;
9670 return TRUE;
9673 /* Check flag compatibility. */
9675 new_flags &= ~EF_MIPS_NOREORDER;
9676 old_flags &= ~EF_MIPS_NOREORDER;
9678 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
9679 doesn't seem to matter. */
9680 new_flags &= ~EF_MIPS_XGOT;
9681 old_flags &= ~EF_MIPS_XGOT;
9683 /* MIPSpro generates ucode info in n64 objects. Again, we should
9684 just be able to ignore this. */
9685 new_flags &= ~EF_MIPS_UCODE;
9686 old_flags &= ~EF_MIPS_UCODE;
9688 if (new_flags == old_flags)
9689 return TRUE;
9691 /* Check to see if the input BFD actually contains any sections.
9692 If not, its flags may not have been initialised either, but it cannot
9693 actually cause any incompatibility. */
9694 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9696 /* Ignore synthetic sections and empty .text, .data and .bss sections
9697 which are automatically generated by gas. */
9698 if (strcmp (sec->name, ".reginfo")
9699 && strcmp (sec->name, ".mdebug")
9700 && (sec->size != 0
9701 || (strcmp (sec->name, ".text")
9702 && strcmp (sec->name, ".data")
9703 && strcmp (sec->name, ".bss"))))
9705 null_input_bfd = FALSE;
9706 break;
9709 if (null_input_bfd)
9710 return TRUE;
9712 ok = TRUE;
9714 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
9715 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
9717 (*_bfd_error_handler)
9718 (_("%B: warning: linking PIC files with non-PIC files"),
9719 ibfd);
9720 ok = TRUE;
9723 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
9724 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
9725 if (! (new_flags & EF_MIPS_PIC))
9726 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
9728 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
9729 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
9731 /* Compare the ISAs. */
9732 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
9734 (*_bfd_error_handler)
9735 (_("%B: linking 32-bit code with 64-bit code"),
9736 ibfd);
9737 ok = FALSE;
9739 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
9741 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
9742 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
9744 /* Copy the architecture info from IBFD to OBFD. Also copy
9745 the 32-bit flag (if set) so that we continue to recognise
9746 OBFD as a 32-bit binary. */
9747 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
9748 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9749 elf_elfheader (obfd)->e_flags
9750 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9752 /* Copy across the ABI flags if OBFD doesn't use them
9753 and if that was what caused us to treat IBFD as 32-bit. */
9754 if ((old_flags & EF_MIPS_ABI) == 0
9755 && mips_32bit_flags_p (new_flags)
9756 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
9757 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
9759 else
9761 /* The ISAs aren't compatible. */
9762 (*_bfd_error_handler)
9763 (_("%B: linking %s module with previous %s modules"),
9764 ibfd,
9765 bfd_printable_name (ibfd),
9766 bfd_printable_name (obfd));
9767 ok = FALSE;
9771 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9772 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9774 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
9775 does set EI_CLASS differently from any 32-bit ABI. */
9776 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
9777 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
9778 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
9780 /* Only error if both are set (to different values). */
9781 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
9782 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
9783 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
9785 (*_bfd_error_handler)
9786 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
9787 ibfd,
9788 elf_mips_abi_name (ibfd),
9789 elf_mips_abi_name (obfd));
9790 ok = FALSE;
9792 new_flags &= ~EF_MIPS_ABI;
9793 old_flags &= ~EF_MIPS_ABI;
9796 /* For now, allow arbitrary mixing of ASEs (retain the union). */
9797 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
9799 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
9801 new_flags &= ~ EF_MIPS_ARCH_ASE;
9802 old_flags &= ~ EF_MIPS_ARCH_ASE;
9805 /* Warn about any other mismatches */
9806 if (new_flags != old_flags)
9808 (*_bfd_error_handler)
9809 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
9810 ibfd, (unsigned long) new_flags,
9811 (unsigned long) old_flags);
9812 ok = FALSE;
9815 if (! ok)
9817 bfd_set_error (bfd_error_bad_value);
9818 return FALSE;
9821 return TRUE;
9824 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
9826 bfd_boolean
9827 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
9829 BFD_ASSERT (!elf_flags_init (abfd)
9830 || elf_elfheader (abfd)->e_flags == flags);
9832 elf_elfheader (abfd)->e_flags = flags;
9833 elf_flags_init (abfd) = TRUE;
9834 return TRUE;
9837 bfd_boolean
9838 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
9840 FILE *file = ptr;
9842 BFD_ASSERT (abfd != NULL && ptr != NULL);
9844 /* Print normal ELF private data. */
9845 _bfd_elf_print_private_bfd_data (abfd, ptr);
9847 /* xgettext:c-format */
9848 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
9850 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
9851 fprintf (file, _(" [abi=O32]"));
9852 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
9853 fprintf (file, _(" [abi=O64]"));
9854 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
9855 fprintf (file, _(" [abi=EABI32]"));
9856 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
9857 fprintf (file, _(" [abi=EABI64]"));
9858 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
9859 fprintf (file, _(" [abi unknown]"));
9860 else if (ABI_N32_P (abfd))
9861 fprintf (file, _(" [abi=N32]"));
9862 else if (ABI_64_P (abfd))
9863 fprintf (file, _(" [abi=64]"));
9864 else
9865 fprintf (file, _(" [no abi set]"));
9867 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
9868 fprintf (file, _(" [mips1]"));
9869 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
9870 fprintf (file, _(" [mips2]"));
9871 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
9872 fprintf (file, _(" [mips3]"));
9873 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
9874 fprintf (file, _(" [mips4]"));
9875 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
9876 fprintf (file, _(" [mips5]"));
9877 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
9878 fprintf (file, _(" [mips32]"));
9879 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
9880 fprintf (file, _(" [mips64]"));
9881 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
9882 fprintf (file, _(" [mips32r2]"));
9883 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
9884 fprintf (file, _(" [mips64r2]"));
9885 else
9886 fprintf (file, _(" [unknown ISA]"));
9888 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
9889 fprintf (file, _(" [mdmx]"));
9891 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
9892 fprintf (file, _(" [mips16]"));
9894 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
9895 fprintf (file, _(" [32bitmode]"));
9896 else
9897 fprintf (file, _(" [not 32bitmode]"));
9899 fputc ('\n', file);
9901 return TRUE;
9904 struct bfd_elf_special_section const _bfd_mips_elf_special_sections[]=
9906 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9907 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9908 { ".lit4", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9909 { ".lit8", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9910 { ".ucode", 6, 0, SHT_MIPS_UCODE, 0 },
9911 { ".mdebug", 7, 0, SHT_MIPS_DEBUG, 0 },
9912 { NULL, 0, 0, 0, 0 }