daily update
[binutils.git] / bfd / elfxx-mips.c
blob389c2affa3b6c44f6cdc79ba037456c42c012ef1
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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? This will only be set if we have converted
246 the initial global GOT entry to a local GOT entry. */
247 bfd_boolean forced_local;
249 #define GOT_NORMAL 0
250 #define GOT_TLS_GD 1
251 #define GOT_TLS_LDM 2
252 #define GOT_TLS_IE 4
253 #define GOT_TLS_OFFSET_DONE 0x40
254 #define GOT_TLS_DONE 0x80
255 unsigned char tls_type;
256 /* This is only used in single-GOT mode; in multi-GOT mode there
257 is one mips_got_entry per GOT entry, so the offset is stored
258 there. In single-GOT mode there may be many mips_got_entry
259 structures all referring to the same GOT slot. It might be
260 possible to use root.got.offset instead, but that field is
261 overloaded already. */
262 bfd_vma tls_got_offset;
265 /* MIPS ELF linker hash table. */
267 struct mips_elf_link_hash_table
269 struct elf_link_hash_table root;
270 #if 0
271 /* We no longer use this. */
272 /* String section indices for the dynamic section symbols. */
273 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
274 #endif
275 /* The number of .rtproc entries. */
276 bfd_size_type procedure_count;
277 /* The size of the .compact_rel section (if SGI_COMPAT). */
278 bfd_size_type compact_rel_size;
279 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
280 entry is set to the address of __rld_obj_head as in IRIX5. */
281 bfd_boolean use_rld_obj_head;
282 /* This is the value of the __rld_map or __rld_obj_head symbol. */
283 bfd_vma rld_value;
284 /* This is set if we see any mips16 stub sections. */
285 bfd_boolean mips16_stubs_seen;
288 #define TLS_RELOC_P(r_type) \
289 (r_type == R_MIPS_TLS_DTPMOD32 \
290 || r_type == R_MIPS_TLS_DTPMOD64 \
291 || r_type == R_MIPS_TLS_DTPREL32 \
292 || r_type == R_MIPS_TLS_DTPREL64 \
293 || r_type == R_MIPS_TLS_GD \
294 || r_type == R_MIPS_TLS_LDM \
295 || r_type == R_MIPS_TLS_DTPREL_HI16 \
296 || r_type == R_MIPS_TLS_DTPREL_LO16 \
297 || r_type == R_MIPS_TLS_GOTTPREL \
298 || r_type == R_MIPS_TLS_TPREL32 \
299 || r_type == R_MIPS_TLS_TPREL64 \
300 || r_type == R_MIPS_TLS_TPREL_HI16 \
301 || r_type == R_MIPS_TLS_TPREL_LO16)
303 /* Structure used to pass information to mips_elf_output_extsym. */
305 struct extsym_info
307 bfd *abfd;
308 struct bfd_link_info *info;
309 struct ecoff_debug_info *debug;
310 const struct ecoff_debug_swap *swap;
311 bfd_boolean failed;
314 /* The names of the runtime procedure table symbols used on IRIX5. */
316 static const char * const mips_elf_dynsym_rtproc_names[] =
318 "_procedure_table",
319 "_procedure_string_table",
320 "_procedure_table_size",
321 NULL
324 /* These structures are used to generate the .compact_rel section on
325 IRIX5. */
327 typedef struct
329 unsigned long id1; /* Always one? */
330 unsigned long num; /* Number of compact relocation entries. */
331 unsigned long id2; /* Always two? */
332 unsigned long offset; /* The file offset of the first relocation. */
333 unsigned long reserved0; /* Zero? */
334 unsigned long reserved1; /* Zero? */
335 } Elf32_compact_rel;
337 typedef struct
339 bfd_byte id1[4];
340 bfd_byte num[4];
341 bfd_byte id2[4];
342 bfd_byte offset[4];
343 bfd_byte reserved0[4];
344 bfd_byte reserved1[4];
345 } Elf32_External_compact_rel;
347 typedef struct
349 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
350 unsigned int rtype : 4; /* Relocation types. See below. */
351 unsigned int dist2to : 8;
352 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
353 unsigned long konst; /* KONST field. See below. */
354 unsigned long vaddr; /* VADDR to be relocated. */
355 } Elf32_crinfo;
357 typedef struct
359 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
360 unsigned int rtype : 4; /* Relocation types. See below. */
361 unsigned int dist2to : 8;
362 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
363 unsigned long konst; /* KONST field. See below. */
364 } Elf32_crinfo2;
366 typedef struct
368 bfd_byte info[4];
369 bfd_byte konst[4];
370 bfd_byte vaddr[4];
371 } Elf32_External_crinfo;
373 typedef struct
375 bfd_byte info[4];
376 bfd_byte konst[4];
377 } Elf32_External_crinfo2;
379 /* These are the constants used to swap the bitfields in a crinfo. */
381 #define CRINFO_CTYPE (0x1)
382 #define CRINFO_CTYPE_SH (31)
383 #define CRINFO_RTYPE (0xf)
384 #define CRINFO_RTYPE_SH (27)
385 #define CRINFO_DIST2TO (0xff)
386 #define CRINFO_DIST2TO_SH (19)
387 #define CRINFO_RELVADDR (0x7ffff)
388 #define CRINFO_RELVADDR_SH (0)
390 /* A compact relocation info has long (3 words) or short (2 words)
391 formats. A short format doesn't have VADDR field and relvaddr
392 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
393 #define CRF_MIPS_LONG 1
394 #define CRF_MIPS_SHORT 0
396 /* There are 4 types of compact relocation at least. The value KONST
397 has different meaning for each type:
399 (type) (konst)
400 CT_MIPS_REL32 Address in data
401 CT_MIPS_WORD Address in word (XXX)
402 CT_MIPS_GPHI_LO GP - vaddr
403 CT_MIPS_JMPAD Address to jump
406 #define CRT_MIPS_REL32 0xa
407 #define CRT_MIPS_WORD 0xb
408 #define CRT_MIPS_GPHI_LO 0xc
409 #define CRT_MIPS_JMPAD 0xd
411 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
412 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
413 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
414 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
416 /* The structure of the runtime procedure descriptor created by the
417 loader for use by the static exception system. */
419 typedef struct runtime_pdr {
420 bfd_vma adr; /* Memory address of start of procedure. */
421 long regmask; /* Save register mask. */
422 long regoffset; /* Save register offset. */
423 long fregmask; /* Save floating point register mask. */
424 long fregoffset; /* Save floating point register offset. */
425 long frameoffset; /* Frame size. */
426 short framereg; /* Frame pointer register. */
427 short pcreg; /* Offset or reg of return pc. */
428 long irpss; /* Index into the runtime string table. */
429 long reserved;
430 struct exception_info *exception_info;/* Pointer to exception array. */
431 } RPDR, *pRPDR;
432 #define cbRPDR sizeof (RPDR)
433 #define rpdNil ((pRPDR) 0)
435 static struct mips_got_entry *mips_elf_create_local_got_entry
436 (bfd *, bfd *, struct mips_got_info *, asection *, bfd_vma, unsigned long,
437 struct mips_elf_link_hash_entry *, int);
438 static bfd_boolean mips_elf_sort_hash_table_f
439 (struct mips_elf_link_hash_entry *, void *);
440 static bfd_vma mips_elf_high
441 (bfd_vma);
442 static bfd_boolean mips_elf_stub_section_p
443 (bfd *, asection *);
444 static bfd_boolean mips_elf_create_dynamic_relocation
445 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
446 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
447 bfd_vma *, asection *);
448 static hashval_t mips_elf_got_entry_hash
449 (const void *);
450 static bfd_vma mips_elf_adjust_gp
451 (bfd *, struct mips_got_info *, bfd *);
452 static struct mips_got_info *mips_elf_got_for_ibfd
453 (struct mips_got_info *, bfd *);
455 /* This will be used when we sort the dynamic relocation records. */
456 static bfd *reldyn_sorting_bfd;
458 /* Nonzero if ABFD is using the N32 ABI. */
460 #define ABI_N32_P(abfd) \
461 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
463 /* Nonzero if ABFD is using the N64 ABI. */
464 #define ABI_64_P(abfd) \
465 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
467 /* Nonzero if ABFD is using NewABI conventions. */
468 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
470 /* The IRIX compatibility level we are striving for. */
471 #define IRIX_COMPAT(abfd) \
472 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
474 /* Whether we are trying to be compatible with IRIX at all. */
475 #define SGI_COMPAT(abfd) \
476 (IRIX_COMPAT (abfd) != ict_none)
478 /* The name of the options section. */
479 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
480 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
482 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
483 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
484 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
485 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
487 /* The name of the stub section. */
488 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
490 /* The size of an external REL relocation. */
491 #define MIPS_ELF_REL_SIZE(abfd) \
492 (get_elf_backend_data (abfd)->s->sizeof_rel)
494 /* The size of an external dynamic table entry. */
495 #define MIPS_ELF_DYN_SIZE(abfd) \
496 (get_elf_backend_data (abfd)->s->sizeof_dyn)
498 /* The size of a GOT entry. */
499 #define MIPS_ELF_GOT_SIZE(abfd) \
500 (get_elf_backend_data (abfd)->s->arch_size / 8)
502 /* The size of a symbol-table entry. */
503 #define MIPS_ELF_SYM_SIZE(abfd) \
504 (get_elf_backend_data (abfd)->s->sizeof_sym)
506 /* The default alignment for sections, as a power of two. */
507 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
508 (get_elf_backend_data (abfd)->s->log_file_align)
510 /* Get word-sized data. */
511 #define MIPS_ELF_GET_WORD(abfd, ptr) \
512 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
514 /* Put out word-sized data. */
515 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
516 (ABI_64_P (abfd) \
517 ? bfd_put_64 (abfd, val, ptr) \
518 : bfd_put_32 (abfd, val, ptr))
520 /* Add a dynamic symbol table-entry. */
521 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
522 _bfd_elf_add_dynamic_entry (info, tag, val)
524 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
525 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
527 /* Determine whether the internal relocation of index REL_IDX is REL
528 (zero) or RELA (non-zero). The assumption is that, if there are
529 two relocation sections for this section, one of them is REL and
530 the other is RELA. If the index of the relocation we're testing is
531 in range for the first relocation section, check that the external
532 relocation size is that for RELA. It is also assumed that, if
533 rel_idx is not in range for the first section, and this first
534 section contains REL relocs, then the relocation is in the second
535 section, that is RELA. */
536 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
537 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
538 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
539 > (bfd_vma)(rel_idx)) \
540 == (elf_section_data (sec)->rel_hdr.sh_entsize \
541 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
542 : sizeof (Elf32_External_Rela))))
544 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
545 from smaller values. Start with zero, widen, *then* decrement. */
546 #define MINUS_ONE (((bfd_vma)0) - 1)
547 #define MINUS_TWO (((bfd_vma)0) - 2)
549 /* The number of local .got entries we reserve. */
550 #define MIPS_RESERVED_GOTNO (2)
552 /* The offset of $gp from the beginning of the .got section. */
553 #define ELF_MIPS_GP_OFFSET(abfd) (0x7ff0)
555 /* The maximum size of the GOT for it to be addressable using 16-bit
556 offsets from $gp. */
557 #define MIPS_ELF_GOT_MAX_SIZE(abfd) (ELF_MIPS_GP_OFFSET(abfd) + 0x7fff)
559 /* Instructions which appear in a stub. */
560 #define STUB_LW(abfd) \
561 ((ABI_64_P (abfd) \
562 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
563 : 0x8f998010)) /* lw t9,0x8010(gp) */
564 #define STUB_MOVE(abfd) \
565 ((ABI_64_P (abfd) \
566 ? 0x03e0782d /* daddu t7,ra */ \
567 : 0x03e07821)) /* addu t7,ra */
568 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
569 #define STUB_LI16(abfd) \
570 ((ABI_64_P (abfd) \
571 ? 0x64180000 /* daddiu t8,zero,0 */ \
572 : 0x24180000)) /* addiu t8,zero,0 */
573 #define MIPS_FUNCTION_STUB_SIZE (16)
575 /* The name of the dynamic interpreter. This is put in the .interp
576 section. */
578 #define ELF_DYNAMIC_INTERPRETER(abfd) \
579 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
580 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
581 : "/usr/lib/libc.so.1")
583 #ifdef BFD64
584 #define MNAME(bfd,pre,pos) \
585 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
586 #define ELF_R_SYM(bfd, i) \
587 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
588 #define ELF_R_TYPE(bfd, i) \
589 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
590 #define ELF_R_INFO(bfd, s, t) \
591 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
592 #else
593 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
594 #define ELF_R_SYM(bfd, i) \
595 (ELF32_R_SYM (i))
596 #define ELF_R_TYPE(bfd, i) \
597 (ELF32_R_TYPE (i))
598 #define ELF_R_INFO(bfd, s, t) \
599 (ELF32_R_INFO (s, t))
600 #endif
602 /* The mips16 compiler uses a couple of special sections to handle
603 floating point arguments.
605 Section names that look like .mips16.fn.FNNAME contain stubs that
606 copy floating point arguments from the fp regs to the gp regs and
607 then jump to FNNAME. If any 32 bit function calls FNNAME, the
608 call should be redirected to the stub instead. If no 32 bit
609 function calls FNNAME, the stub should be discarded. We need to
610 consider any reference to the function, not just a call, because
611 if the address of the function is taken we will need the stub,
612 since the address might be passed to a 32 bit function.
614 Section names that look like .mips16.call.FNNAME contain stubs
615 that copy floating point arguments from the gp regs to the fp
616 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
617 then any 16 bit function that calls FNNAME should be redirected
618 to the stub instead. If FNNAME is not a 32 bit function, the
619 stub should be discarded.
621 .mips16.call.fp.FNNAME sections are similar, but contain stubs
622 which call FNNAME and then copy the return value from the fp regs
623 to the gp regs. These stubs store the return value in $18 while
624 calling FNNAME; any function which might call one of these stubs
625 must arrange to save $18 around the call. (This case is not
626 needed for 32 bit functions that call 16 bit functions, because
627 16 bit functions always return floating point values in both
628 $f0/$f1 and $2/$3.)
630 Note that in all cases FNNAME might be defined statically.
631 Therefore, FNNAME is not used literally. Instead, the relocation
632 information will indicate which symbol the section is for.
634 We record any stubs that we find in the symbol table. */
636 #define FN_STUB ".mips16.fn."
637 #define CALL_STUB ".mips16.call."
638 #define CALL_FP_STUB ".mips16.call.fp."
640 /* Look up an entry in a MIPS ELF linker hash table. */
642 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
643 ((struct mips_elf_link_hash_entry *) \
644 elf_link_hash_lookup (&(table)->root, (string), (create), \
645 (copy), (follow)))
647 /* Traverse a MIPS ELF linker hash table. */
649 #define mips_elf_link_hash_traverse(table, func, info) \
650 (elf_link_hash_traverse \
651 (&(table)->root, \
652 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
653 (info)))
655 /* Get the MIPS ELF linker hash table from a link_info structure. */
657 #define mips_elf_hash_table(p) \
658 ((struct mips_elf_link_hash_table *) ((p)->hash))
660 /* Find the base offsets for thread-local storage in this object,
661 for GD/LD and IE/LE respectively. */
663 #define TP_OFFSET 0x7000
664 #define DTP_OFFSET 0x8000
666 static bfd_vma
667 dtprel_base (struct bfd_link_info *info)
669 /* If tls_sec is NULL, we should have signalled an error already. */
670 if (elf_hash_table (info)->tls_sec == NULL)
671 return 0;
672 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
675 static bfd_vma
676 tprel_base (struct bfd_link_info *info)
678 /* If tls_sec is NULL, we should have signalled an error already. */
679 if (elf_hash_table (info)->tls_sec == NULL)
680 return 0;
681 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
684 /* Create an entry in a MIPS ELF linker hash table. */
686 static struct bfd_hash_entry *
687 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
688 struct bfd_hash_table *table, const char *string)
690 struct mips_elf_link_hash_entry *ret =
691 (struct mips_elf_link_hash_entry *) entry;
693 /* Allocate the structure if it has not already been allocated by a
694 subclass. */
695 if (ret == NULL)
696 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
697 if (ret == NULL)
698 return (struct bfd_hash_entry *) ret;
700 /* Call the allocation method of the superclass. */
701 ret = ((struct mips_elf_link_hash_entry *)
702 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
703 table, string));
704 if (ret != NULL)
706 /* Set local fields. */
707 memset (&ret->esym, 0, sizeof (EXTR));
708 /* We use -2 as a marker to indicate that the information has
709 not been set. -1 means there is no associated ifd. */
710 ret->esym.ifd = -2;
711 ret->possibly_dynamic_relocs = 0;
712 ret->readonly_reloc = FALSE;
713 ret->no_fn_stub = FALSE;
714 ret->fn_stub = NULL;
715 ret->need_fn_stub = FALSE;
716 ret->call_stub = NULL;
717 ret->call_fp_stub = NULL;
718 ret->forced_local = FALSE;
719 ret->tls_type = GOT_NORMAL;
722 return (struct bfd_hash_entry *) ret;
725 bfd_boolean
726 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
728 struct _mips_elf_section_data *sdata;
729 bfd_size_type amt = sizeof (*sdata);
731 sdata = bfd_zalloc (abfd, amt);
732 if (sdata == NULL)
733 return FALSE;
734 sec->used_by_bfd = sdata;
736 return _bfd_elf_new_section_hook (abfd, sec);
739 /* Read ECOFF debugging information from a .mdebug section into a
740 ecoff_debug_info structure. */
742 bfd_boolean
743 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
744 struct ecoff_debug_info *debug)
746 HDRR *symhdr;
747 const struct ecoff_debug_swap *swap;
748 char *ext_hdr;
750 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
751 memset (debug, 0, sizeof (*debug));
753 ext_hdr = bfd_malloc (swap->external_hdr_size);
754 if (ext_hdr == NULL && swap->external_hdr_size != 0)
755 goto error_return;
757 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
758 swap->external_hdr_size))
759 goto error_return;
761 symhdr = &debug->symbolic_header;
762 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
764 /* The symbolic header contains absolute file offsets and sizes to
765 read. */
766 #define READ(ptr, offset, count, size, type) \
767 if (symhdr->count == 0) \
768 debug->ptr = NULL; \
769 else \
771 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
772 debug->ptr = bfd_malloc (amt); \
773 if (debug->ptr == NULL) \
774 goto error_return; \
775 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
776 || bfd_bread (debug->ptr, amt, abfd) != amt) \
777 goto error_return; \
780 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
781 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
782 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
783 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
784 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
785 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
786 union aux_ext *);
787 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
788 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
789 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
790 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
791 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
792 #undef READ
794 debug->fdr = NULL;
796 return TRUE;
798 error_return:
799 if (ext_hdr != NULL)
800 free (ext_hdr);
801 if (debug->line != NULL)
802 free (debug->line);
803 if (debug->external_dnr != NULL)
804 free (debug->external_dnr);
805 if (debug->external_pdr != NULL)
806 free (debug->external_pdr);
807 if (debug->external_sym != NULL)
808 free (debug->external_sym);
809 if (debug->external_opt != NULL)
810 free (debug->external_opt);
811 if (debug->external_aux != NULL)
812 free (debug->external_aux);
813 if (debug->ss != NULL)
814 free (debug->ss);
815 if (debug->ssext != NULL)
816 free (debug->ssext);
817 if (debug->external_fdr != NULL)
818 free (debug->external_fdr);
819 if (debug->external_rfd != NULL)
820 free (debug->external_rfd);
821 if (debug->external_ext != NULL)
822 free (debug->external_ext);
823 return FALSE;
826 /* Swap RPDR (runtime procedure table entry) for output. */
828 static void
829 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
831 H_PUT_S32 (abfd, in->adr, ex->p_adr);
832 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
833 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
834 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
835 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
836 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
838 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
839 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
841 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
844 /* Create a runtime procedure table from the .mdebug section. */
846 static bfd_boolean
847 mips_elf_create_procedure_table (void *handle, bfd *abfd,
848 struct bfd_link_info *info, asection *s,
849 struct ecoff_debug_info *debug)
851 const struct ecoff_debug_swap *swap;
852 HDRR *hdr = &debug->symbolic_header;
853 RPDR *rpdr, *rp;
854 struct rpdr_ext *erp;
855 void *rtproc;
856 struct pdr_ext *epdr;
857 struct sym_ext *esym;
858 char *ss, **sv;
859 char *str;
860 bfd_size_type size;
861 bfd_size_type count;
862 unsigned long sindex;
863 unsigned long i;
864 PDR pdr;
865 SYMR sym;
866 const char *no_name_func = _("static procedure (no name)");
868 epdr = NULL;
869 rpdr = NULL;
870 esym = NULL;
871 ss = NULL;
872 sv = NULL;
874 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
876 sindex = strlen (no_name_func) + 1;
877 count = hdr->ipdMax;
878 if (count > 0)
880 size = swap->external_pdr_size;
882 epdr = bfd_malloc (size * count);
883 if (epdr == NULL)
884 goto error_return;
886 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
887 goto error_return;
889 size = sizeof (RPDR);
890 rp = rpdr = bfd_malloc (size * count);
891 if (rpdr == NULL)
892 goto error_return;
894 size = sizeof (char *);
895 sv = bfd_malloc (size * count);
896 if (sv == NULL)
897 goto error_return;
899 count = hdr->isymMax;
900 size = swap->external_sym_size;
901 esym = bfd_malloc (size * count);
902 if (esym == NULL)
903 goto error_return;
905 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
906 goto error_return;
908 count = hdr->issMax;
909 ss = bfd_malloc (count);
910 if (ss == NULL)
911 goto error_return;
912 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
913 goto error_return;
915 count = hdr->ipdMax;
916 for (i = 0; i < (unsigned long) count; i++, rp++)
918 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
919 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
920 rp->adr = sym.value;
921 rp->regmask = pdr.regmask;
922 rp->regoffset = pdr.regoffset;
923 rp->fregmask = pdr.fregmask;
924 rp->fregoffset = pdr.fregoffset;
925 rp->frameoffset = pdr.frameoffset;
926 rp->framereg = pdr.framereg;
927 rp->pcreg = pdr.pcreg;
928 rp->irpss = sindex;
929 sv[i] = ss + sym.iss;
930 sindex += strlen (sv[i]) + 1;
934 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
935 size = BFD_ALIGN (size, 16);
936 rtproc = bfd_alloc (abfd, size);
937 if (rtproc == NULL)
939 mips_elf_hash_table (info)->procedure_count = 0;
940 goto error_return;
943 mips_elf_hash_table (info)->procedure_count = count + 2;
945 erp = rtproc;
946 memset (erp, 0, sizeof (struct rpdr_ext));
947 erp++;
948 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
949 strcpy (str, no_name_func);
950 str += strlen (no_name_func) + 1;
951 for (i = 0; i < count; i++)
953 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
954 strcpy (str, sv[i]);
955 str += strlen (sv[i]) + 1;
957 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
959 /* Set the size and contents of .rtproc section. */
960 s->size = size;
961 s->contents = rtproc;
963 /* Skip this section later on (I don't think this currently
964 matters, but someday it might). */
965 s->map_head.link_order = NULL;
967 if (epdr != NULL)
968 free (epdr);
969 if (rpdr != NULL)
970 free (rpdr);
971 if (esym != NULL)
972 free (esym);
973 if (ss != NULL)
974 free (ss);
975 if (sv != NULL)
976 free (sv);
978 return TRUE;
980 error_return:
981 if (epdr != NULL)
982 free (epdr);
983 if (rpdr != NULL)
984 free (rpdr);
985 if (esym != NULL)
986 free (esym);
987 if (ss != NULL)
988 free (ss);
989 if (sv != NULL)
990 free (sv);
991 return FALSE;
994 /* Check the mips16 stubs for a particular symbol, and see if we can
995 discard them. */
997 static bfd_boolean
998 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h,
999 void *data ATTRIBUTE_UNUSED)
1001 if (h->root.root.type == bfd_link_hash_warning)
1002 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1004 if (h->fn_stub != NULL
1005 && ! h->need_fn_stub)
1007 /* We don't need the fn_stub; the only references to this symbol
1008 are 16 bit calls. Clobber the size to 0 to prevent it from
1009 being included in the link. */
1010 h->fn_stub->size = 0;
1011 h->fn_stub->flags &= ~SEC_RELOC;
1012 h->fn_stub->reloc_count = 0;
1013 h->fn_stub->flags |= SEC_EXCLUDE;
1016 if (h->call_stub != NULL
1017 && h->root.other == STO_MIPS16)
1019 /* We don't need the call_stub; this is a 16 bit function, so
1020 calls from other 16 bit functions are OK. Clobber the size
1021 to 0 to prevent it from being included in the link. */
1022 h->call_stub->size = 0;
1023 h->call_stub->flags &= ~SEC_RELOC;
1024 h->call_stub->reloc_count = 0;
1025 h->call_stub->flags |= SEC_EXCLUDE;
1028 if (h->call_fp_stub != NULL
1029 && h->root.other == STO_MIPS16)
1031 /* We don't need the call_stub; this is a 16 bit function, so
1032 calls from other 16 bit functions are OK. Clobber the size
1033 to 0 to prevent it from being included in the link. */
1034 h->call_fp_stub->size = 0;
1035 h->call_fp_stub->flags &= ~SEC_RELOC;
1036 h->call_fp_stub->reloc_count = 0;
1037 h->call_fp_stub->flags |= SEC_EXCLUDE;
1040 return TRUE;
1043 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1044 Most mips16 instructions are 16 bits, but these instructions
1045 are 32 bits.
1047 The format of these instructions is:
1049 +--------------+--------------------------------+
1050 | JALX | X| Imm 20:16 | Imm 25:21 |
1051 +--------------+--------------------------------+
1052 | Immediate 15:0 |
1053 +-----------------------------------------------+
1055 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1056 Note that the immediate value in the first word is swapped.
1058 When producing a relocatable object file, R_MIPS16_26 is
1059 handled mostly like R_MIPS_26. In particular, the addend is
1060 stored as a straight 26-bit value in a 32-bit instruction.
1061 (gas makes life simpler for itself by never adjusting a
1062 R_MIPS16_26 reloc to be against a section, so the addend is
1063 always zero). However, the 32 bit instruction is stored as 2
1064 16-bit values, rather than a single 32-bit value. In a
1065 big-endian file, the result is the same; in a little-endian
1066 file, the two 16-bit halves of the 32 bit value are swapped.
1067 This is so that a disassembler can recognize the jal
1068 instruction.
1070 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1071 instruction stored as two 16-bit values. The addend A is the
1072 contents of the targ26 field. The calculation is the same as
1073 R_MIPS_26. When storing the calculated value, reorder the
1074 immediate value as shown above, and don't forget to store the
1075 value as two 16-bit values.
1077 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1078 defined as
1080 big-endian:
1081 +--------+----------------------+
1082 | | |
1083 | | targ26-16 |
1084 |31 26|25 0|
1085 +--------+----------------------+
1087 little-endian:
1088 +----------+------+-------------+
1089 | | | |
1090 | sub1 | | sub2 |
1091 |0 9|10 15|16 31|
1092 +----------+--------------------+
1093 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1094 ((sub1 << 16) | sub2)).
1096 When producing a relocatable object file, the calculation is
1097 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1098 When producing a fully linked file, the calculation is
1099 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1100 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1102 R_MIPS16_GPREL is used for GP-relative addressing in mips16
1103 mode. A typical instruction will have a format like this:
1105 +--------------+--------------------------------+
1106 | EXTEND | Imm 10:5 | Imm 15:11 |
1107 +--------------+--------------------------------+
1108 | Major | rx | ry | Imm 4:0 |
1109 +--------------+--------------------------------+
1111 EXTEND is the five bit value 11110. Major is the instruction
1112 opcode.
1114 This is handled exactly like R_MIPS_GPREL16, except that the
1115 addend is retrieved and stored as shown in this diagram; that
1116 is, the Imm fields above replace the V-rel16 field.
1118 All we need to do here is shuffle the bits appropriately. As
1119 above, the two 16-bit halves must be swapped on a
1120 little-endian system.
1122 R_MIPS16_HI16 and R_MIPS16_LO16 are used in mips16 mode to
1123 access data when neither GP-relative nor PC-relative addressing
1124 can be used. They are handled like R_MIPS_HI16 and R_MIPS_LO16,
1125 except that the addend is retrieved and stored as shown above
1126 for R_MIPS16_GPREL.
1128 void
1129 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1130 bfd_boolean jal_shuffle, bfd_byte *data)
1132 bfd_vma extend, insn, val;
1134 if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1135 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1136 return;
1138 /* Pick up the mips16 extend instruction and the real instruction. */
1139 extend = bfd_get_16 (abfd, data);
1140 insn = bfd_get_16 (abfd, data + 2);
1141 if (r_type == R_MIPS16_26)
1143 if (jal_shuffle)
1144 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1145 | ((extend & 0x1f) << 21) | insn;
1146 else
1147 val = extend << 16 | insn;
1149 else
1150 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1151 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1152 bfd_put_32 (abfd, val, data);
1155 void
1156 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1157 bfd_boolean jal_shuffle, bfd_byte *data)
1159 bfd_vma extend, insn, val;
1161 if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1162 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1163 return;
1165 val = bfd_get_32 (abfd, data);
1166 if (r_type == R_MIPS16_26)
1168 if (jal_shuffle)
1170 insn = val & 0xffff;
1171 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1172 | ((val >> 21) & 0x1f);
1174 else
1176 insn = val & 0xffff;
1177 extend = val >> 16;
1180 else
1182 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1183 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1185 bfd_put_16 (abfd, insn, data + 2);
1186 bfd_put_16 (abfd, extend, data);
1189 bfd_reloc_status_type
1190 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1191 arelent *reloc_entry, asection *input_section,
1192 bfd_boolean relocatable, void *data, bfd_vma gp)
1194 bfd_vma relocation;
1195 bfd_signed_vma val;
1196 bfd_reloc_status_type status;
1198 if (bfd_is_com_section (symbol->section))
1199 relocation = 0;
1200 else
1201 relocation = symbol->value;
1203 relocation += symbol->section->output_section->vma;
1204 relocation += symbol->section->output_offset;
1206 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1207 return bfd_reloc_outofrange;
1209 /* Set val to the offset into the section or symbol. */
1210 val = reloc_entry->addend;
1212 _bfd_mips_elf_sign_extend (val, 16);
1214 /* Adjust val for the final section location and GP value. If we
1215 are producing relocatable output, we don't want to do this for
1216 an external symbol. */
1217 if (! relocatable
1218 || (symbol->flags & BSF_SECTION_SYM) != 0)
1219 val += relocation - gp;
1221 if (reloc_entry->howto->partial_inplace)
1223 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1224 (bfd_byte *) data
1225 + reloc_entry->address);
1226 if (status != bfd_reloc_ok)
1227 return status;
1229 else
1230 reloc_entry->addend = val;
1232 if (relocatable)
1233 reloc_entry->address += input_section->output_offset;
1235 return bfd_reloc_ok;
1238 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1239 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1240 that contains the relocation field and DATA points to the start of
1241 INPUT_SECTION. */
1243 struct mips_hi16
1245 struct mips_hi16 *next;
1246 bfd_byte *data;
1247 asection *input_section;
1248 arelent rel;
1251 /* FIXME: This should not be a static variable. */
1253 static struct mips_hi16 *mips_hi16_list;
1255 /* A howto special_function for REL *HI16 relocations. We can only
1256 calculate the correct value once we've seen the partnering
1257 *LO16 relocation, so just save the information for later.
1259 The ABI requires that the *LO16 immediately follow the *HI16.
1260 However, as a GNU extension, we permit an arbitrary number of
1261 *HI16s to be associated with a single *LO16. This significantly
1262 simplies the relocation handling in gcc. */
1264 bfd_reloc_status_type
1265 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1266 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1267 asection *input_section, bfd *output_bfd,
1268 char **error_message ATTRIBUTE_UNUSED)
1270 struct mips_hi16 *n;
1272 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1273 return bfd_reloc_outofrange;
1275 n = bfd_malloc (sizeof *n);
1276 if (n == NULL)
1277 return bfd_reloc_outofrange;
1279 n->next = mips_hi16_list;
1280 n->data = data;
1281 n->input_section = input_section;
1282 n->rel = *reloc_entry;
1283 mips_hi16_list = n;
1285 if (output_bfd != NULL)
1286 reloc_entry->address += input_section->output_offset;
1288 return bfd_reloc_ok;
1291 /* A howto special_function for REL R_MIPS_GOT16 relocations. This is just
1292 like any other 16-bit relocation when applied to global symbols, but is
1293 treated in the same as R_MIPS_HI16 when applied to local symbols. */
1295 bfd_reloc_status_type
1296 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1297 void *data, asection *input_section,
1298 bfd *output_bfd, char **error_message)
1300 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1301 || bfd_is_und_section (bfd_get_section (symbol))
1302 || bfd_is_com_section (bfd_get_section (symbol)))
1303 /* The relocation is against a global symbol. */
1304 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1305 input_section, output_bfd,
1306 error_message);
1308 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1309 input_section, output_bfd, error_message);
1312 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
1313 is a straightforward 16 bit inplace relocation, but we must deal with
1314 any partnering high-part relocations as well. */
1316 bfd_reloc_status_type
1317 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1318 void *data, asection *input_section,
1319 bfd *output_bfd, char **error_message)
1321 bfd_vma vallo;
1322 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1324 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1325 return bfd_reloc_outofrange;
1327 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1328 location);
1329 vallo = bfd_get_32 (abfd, location);
1330 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1331 location);
1333 while (mips_hi16_list != NULL)
1335 bfd_reloc_status_type ret;
1336 struct mips_hi16 *hi;
1338 hi = mips_hi16_list;
1340 /* R_MIPS_GOT16 relocations are something of a special case. We
1341 want to install the addend in the same way as for a R_MIPS_HI16
1342 relocation (with a rightshift of 16). However, since GOT16
1343 relocations can also be used with global symbols, their howto
1344 has a rightshift of 0. */
1345 if (hi->rel.howto->type == R_MIPS_GOT16)
1346 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1348 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
1349 carry or borrow will induce a change of +1 or -1 in the high part. */
1350 hi->rel.addend += (vallo + 0x8000) & 0xffff;
1352 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1353 hi->input_section, output_bfd,
1354 error_message);
1355 if (ret != bfd_reloc_ok)
1356 return ret;
1358 mips_hi16_list = hi->next;
1359 free (hi);
1362 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1363 input_section, output_bfd,
1364 error_message);
1367 /* A generic howto special_function. This calculates and installs the
1368 relocation itself, thus avoiding the oft-discussed problems in
1369 bfd_perform_relocation and bfd_install_relocation. */
1371 bfd_reloc_status_type
1372 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1373 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1374 asection *input_section, bfd *output_bfd,
1375 char **error_message ATTRIBUTE_UNUSED)
1377 bfd_signed_vma val;
1378 bfd_reloc_status_type status;
1379 bfd_boolean relocatable;
1381 relocatable = (output_bfd != NULL);
1383 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1384 return bfd_reloc_outofrange;
1386 /* Build up the field adjustment in VAL. */
1387 val = 0;
1388 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1390 /* Either we're calculating the final field value or we have a
1391 relocation against a section symbol. Add in the section's
1392 offset or address. */
1393 val += symbol->section->output_section->vma;
1394 val += symbol->section->output_offset;
1397 if (!relocatable)
1399 /* We're calculating the final field value. Add in the symbol's value
1400 and, if pc-relative, subtract the address of the field itself. */
1401 val += symbol->value;
1402 if (reloc_entry->howto->pc_relative)
1404 val -= input_section->output_section->vma;
1405 val -= input_section->output_offset;
1406 val -= reloc_entry->address;
1410 /* VAL is now the final adjustment. If we're keeping this relocation
1411 in the output file, and if the relocation uses a separate addend,
1412 we just need to add VAL to that addend. Otherwise we need to add
1413 VAL to the relocation field itself. */
1414 if (relocatable && !reloc_entry->howto->partial_inplace)
1415 reloc_entry->addend += val;
1416 else
1418 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1420 /* Add in the separate addend, if any. */
1421 val += reloc_entry->addend;
1423 /* Add VAL to the relocation field. */
1424 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1425 location);
1426 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1427 location);
1428 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1429 location);
1431 if (status != bfd_reloc_ok)
1432 return status;
1435 if (relocatable)
1436 reloc_entry->address += input_section->output_offset;
1438 return bfd_reloc_ok;
1441 /* Swap an entry in a .gptab section. Note that these routines rely
1442 on the equivalence of the two elements of the union. */
1444 static void
1445 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1446 Elf32_gptab *in)
1448 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1449 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1452 static void
1453 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1454 Elf32_External_gptab *ex)
1456 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1457 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1460 static void
1461 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1462 Elf32_External_compact_rel *ex)
1464 H_PUT_32 (abfd, in->id1, ex->id1);
1465 H_PUT_32 (abfd, in->num, ex->num);
1466 H_PUT_32 (abfd, in->id2, ex->id2);
1467 H_PUT_32 (abfd, in->offset, ex->offset);
1468 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1469 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1472 static void
1473 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1474 Elf32_External_crinfo *ex)
1476 unsigned long l;
1478 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1479 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1480 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1481 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1482 H_PUT_32 (abfd, l, ex->info);
1483 H_PUT_32 (abfd, in->konst, ex->konst);
1484 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1487 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1488 routines swap this structure in and out. They are used outside of
1489 BFD, so they are globally visible. */
1491 void
1492 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1493 Elf32_RegInfo *in)
1495 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1496 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1497 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1498 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1499 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1500 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1503 void
1504 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1505 Elf32_External_RegInfo *ex)
1507 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1508 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1509 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1510 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1511 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1512 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1515 /* In the 64 bit ABI, the .MIPS.options section holds register
1516 information in an Elf64_Reginfo structure. These routines swap
1517 them in and out. They are globally visible because they are used
1518 outside of BFD. These routines are here so that gas can call them
1519 without worrying about whether the 64 bit ABI has been included. */
1521 void
1522 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1523 Elf64_Internal_RegInfo *in)
1525 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1526 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1527 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1528 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1529 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1530 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1531 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1534 void
1535 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1536 Elf64_External_RegInfo *ex)
1538 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1539 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1540 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1541 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1542 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1543 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1544 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1547 /* Swap in an options header. */
1549 void
1550 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1551 Elf_Internal_Options *in)
1553 in->kind = H_GET_8 (abfd, ex->kind);
1554 in->size = H_GET_8 (abfd, ex->size);
1555 in->section = H_GET_16 (abfd, ex->section);
1556 in->info = H_GET_32 (abfd, ex->info);
1559 /* Swap out an options header. */
1561 void
1562 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1563 Elf_External_Options *ex)
1565 H_PUT_8 (abfd, in->kind, ex->kind);
1566 H_PUT_8 (abfd, in->size, ex->size);
1567 H_PUT_16 (abfd, in->section, ex->section);
1568 H_PUT_32 (abfd, in->info, ex->info);
1571 /* This function is called via qsort() to sort the dynamic relocation
1572 entries by increasing r_symndx value. */
1574 static int
1575 sort_dynamic_relocs (const void *arg1, const void *arg2)
1577 Elf_Internal_Rela int_reloc1;
1578 Elf_Internal_Rela int_reloc2;
1580 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1581 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1583 return ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1586 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
1588 static int
1589 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1590 const void *arg2 ATTRIBUTE_UNUSED)
1592 #ifdef BFD64
1593 Elf_Internal_Rela int_reloc1[3];
1594 Elf_Internal_Rela int_reloc2[3];
1596 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1597 (reldyn_sorting_bfd, arg1, int_reloc1);
1598 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1599 (reldyn_sorting_bfd, arg2, int_reloc2);
1601 return (ELF64_R_SYM (int_reloc1[0].r_info)
1602 - ELF64_R_SYM (int_reloc2[0].r_info));
1603 #else
1604 abort ();
1605 #endif
1609 /* This routine is used to write out ECOFF debugging external symbol
1610 information. It is called via mips_elf_link_hash_traverse. The
1611 ECOFF external symbol information must match the ELF external
1612 symbol information. Unfortunately, at this point we don't know
1613 whether a symbol is required by reloc information, so the two
1614 tables may wind up being different. We must sort out the external
1615 symbol information before we can set the final size of the .mdebug
1616 section, and we must set the size of the .mdebug section before we
1617 can relocate any sections, and we can't know which symbols are
1618 required by relocation until we relocate the sections.
1619 Fortunately, it is relatively unlikely that any symbol will be
1620 stripped but required by a reloc. In particular, it can not happen
1621 when generating a final executable. */
1623 static bfd_boolean
1624 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1626 struct extsym_info *einfo = data;
1627 bfd_boolean strip;
1628 asection *sec, *output_section;
1630 if (h->root.root.type == bfd_link_hash_warning)
1631 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1633 if (h->root.indx == -2)
1634 strip = FALSE;
1635 else if ((h->root.def_dynamic
1636 || h->root.ref_dynamic
1637 || h->root.type == bfd_link_hash_new)
1638 && !h->root.def_regular
1639 && !h->root.ref_regular)
1640 strip = TRUE;
1641 else if (einfo->info->strip == strip_all
1642 || (einfo->info->strip == strip_some
1643 && bfd_hash_lookup (einfo->info->keep_hash,
1644 h->root.root.root.string,
1645 FALSE, FALSE) == NULL))
1646 strip = TRUE;
1647 else
1648 strip = FALSE;
1650 if (strip)
1651 return TRUE;
1653 if (h->esym.ifd == -2)
1655 h->esym.jmptbl = 0;
1656 h->esym.cobol_main = 0;
1657 h->esym.weakext = 0;
1658 h->esym.reserved = 0;
1659 h->esym.ifd = ifdNil;
1660 h->esym.asym.value = 0;
1661 h->esym.asym.st = stGlobal;
1663 if (h->root.root.type == bfd_link_hash_undefined
1664 || h->root.root.type == bfd_link_hash_undefweak)
1666 const char *name;
1668 /* Use undefined class. Also, set class and type for some
1669 special symbols. */
1670 name = h->root.root.root.string;
1671 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1672 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1674 h->esym.asym.sc = scData;
1675 h->esym.asym.st = stLabel;
1676 h->esym.asym.value = 0;
1678 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1680 h->esym.asym.sc = scAbs;
1681 h->esym.asym.st = stLabel;
1682 h->esym.asym.value =
1683 mips_elf_hash_table (einfo->info)->procedure_count;
1685 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1687 h->esym.asym.sc = scAbs;
1688 h->esym.asym.st = stLabel;
1689 h->esym.asym.value = elf_gp (einfo->abfd);
1691 else
1692 h->esym.asym.sc = scUndefined;
1694 else if (h->root.root.type != bfd_link_hash_defined
1695 && h->root.root.type != bfd_link_hash_defweak)
1696 h->esym.asym.sc = scAbs;
1697 else
1699 const char *name;
1701 sec = h->root.root.u.def.section;
1702 output_section = sec->output_section;
1704 /* When making a shared library and symbol h is the one from
1705 the another shared library, OUTPUT_SECTION may be null. */
1706 if (output_section == NULL)
1707 h->esym.asym.sc = scUndefined;
1708 else
1710 name = bfd_section_name (output_section->owner, output_section);
1712 if (strcmp (name, ".text") == 0)
1713 h->esym.asym.sc = scText;
1714 else if (strcmp (name, ".data") == 0)
1715 h->esym.asym.sc = scData;
1716 else if (strcmp (name, ".sdata") == 0)
1717 h->esym.asym.sc = scSData;
1718 else if (strcmp (name, ".rodata") == 0
1719 || strcmp (name, ".rdata") == 0)
1720 h->esym.asym.sc = scRData;
1721 else if (strcmp (name, ".bss") == 0)
1722 h->esym.asym.sc = scBss;
1723 else if (strcmp (name, ".sbss") == 0)
1724 h->esym.asym.sc = scSBss;
1725 else if (strcmp (name, ".init") == 0)
1726 h->esym.asym.sc = scInit;
1727 else if (strcmp (name, ".fini") == 0)
1728 h->esym.asym.sc = scFini;
1729 else
1730 h->esym.asym.sc = scAbs;
1734 h->esym.asym.reserved = 0;
1735 h->esym.asym.index = indexNil;
1738 if (h->root.root.type == bfd_link_hash_common)
1739 h->esym.asym.value = h->root.root.u.c.size;
1740 else if (h->root.root.type == bfd_link_hash_defined
1741 || h->root.root.type == bfd_link_hash_defweak)
1743 if (h->esym.asym.sc == scCommon)
1744 h->esym.asym.sc = scBss;
1745 else if (h->esym.asym.sc == scSCommon)
1746 h->esym.asym.sc = scSBss;
1748 sec = h->root.root.u.def.section;
1749 output_section = sec->output_section;
1750 if (output_section != NULL)
1751 h->esym.asym.value = (h->root.root.u.def.value
1752 + sec->output_offset
1753 + output_section->vma);
1754 else
1755 h->esym.asym.value = 0;
1757 else if (h->root.needs_plt)
1759 struct mips_elf_link_hash_entry *hd = h;
1760 bfd_boolean no_fn_stub = h->no_fn_stub;
1762 while (hd->root.root.type == bfd_link_hash_indirect)
1764 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
1765 no_fn_stub = no_fn_stub || hd->no_fn_stub;
1768 if (!no_fn_stub)
1770 /* Set type and value for a symbol with a function stub. */
1771 h->esym.asym.st = stProc;
1772 sec = hd->root.root.u.def.section;
1773 if (sec == NULL)
1774 h->esym.asym.value = 0;
1775 else
1777 output_section = sec->output_section;
1778 if (output_section != NULL)
1779 h->esym.asym.value = (hd->root.plt.offset
1780 + sec->output_offset
1781 + output_section->vma);
1782 else
1783 h->esym.asym.value = 0;
1788 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1789 h->root.root.root.string,
1790 &h->esym))
1792 einfo->failed = TRUE;
1793 return FALSE;
1796 return TRUE;
1799 /* A comparison routine used to sort .gptab entries. */
1801 static int
1802 gptab_compare (const void *p1, const void *p2)
1804 const Elf32_gptab *a1 = p1;
1805 const Elf32_gptab *a2 = p2;
1807 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1810 /* Functions to manage the got entry hash table. */
1812 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
1813 hash number. */
1815 static INLINE hashval_t
1816 mips_elf_hash_bfd_vma (bfd_vma addr)
1818 #ifdef BFD64
1819 return addr + (addr >> 32);
1820 #else
1821 return addr;
1822 #endif
1825 /* got_entries only match if they're identical, except for gotidx, so
1826 use all fields to compute the hash, and compare the appropriate
1827 union members. */
1829 static hashval_t
1830 mips_elf_got_entry_hash (const void *entry_)
1832 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1834 return entry->symndx
1835 + ((entry->tls_type & GOT_TLS_LDM) << 17)
1836 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
1837 : entry->abfd->id
1838 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
1839 : entry->d.h->root.root.root.hash));
1842 static int
1843 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
1845 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1846 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1848 /* An LDM entry can only match another LDM entry. */
1849 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1850 return 0;
1852 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
1853 && (! e1->abfd ? e1->d.address == e2->d.address
1854 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
1855 : e1->d.h == e2->d.h);
1858 /* multi_got_entries are still a match in the case of global objects,
1859 even if the input bfd in which they're referenced differs, so the
1860 hash computation and compare functions are adjusted
1861 accordingly. */
1863 static hashval_t
1864 mips_elf_multi_got_entry_hash (const void *entry_)
1866 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1868 return entry->symndx
1869 + (! entry->abfd
1870 ? mips_elf_hash_bfd_vma (entry->d.address)
1871 : entry->symndx >= 0
1872 ? ((entry->tls_type & GOT_TLS_LDM)
1873 ? (GOT_TLS_LDM << 17)
1874 : (entry->abfd->id
1875 + mips_elf_hash_bfd_vma (entry->d.addend)))
1876 : entry->d.h->root.root.root.hash);
1879 static int
1880 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
1882 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1883 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1885 /* Any two LDM entries match. */
1886 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
1887 return 1;
1889 /* Nothing else matches an LDM entry. */
1890 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1891 return 0;
1893 return e1->symndx == e2->symndx
1894 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
1895 : e1->abfd == NULL || e2->abfd == NULL
1896 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
1897 : e1->d.h == e2->d.h);
1900 /* Returns the dynamic relocation section for DYNOBJ. */
1902 static asection *
1903 mips_elf_rel_dyn_section (bfd *dynobj, bfd_boolean create_p)
1905 static const char dname[] = ".rel.dyn";
1906 asection *sreloc;
1908 sreloc = bfd_get_section_by_name (dynobj, dname);
1909 if (sreloc == NULL && create_p)
1911 sreloc = bfd_make_section_with_flags (dynobj, dname,
1912 (SEC_ALLOC
1913 | SEC_LOAD
1914 | SEC_HAS_CONTENTS
1915 | SEC_IN_MEMORY
1916 | SEC_LINKER_CREATED
1917 | SEC_READONLY));
1918 if (sreloc == NULL
1919 || ! bfd_set_section_alignment (dynobj, sreloc,
1920 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
1921 return NULL;
1923 return sreloc;
1926 /* Returns the GOT section for ABFD. */
1928 static asection *
1929 mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
1931 asection *sgot = bfd_get_section_by_name (abfd, ".got");
1932 if (sgot == NULL
1933 || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
1934 return NULL;
1935 return sgot;
1938 /* Returns the GOT information associated with the link indicated by
1939 INFO. If SGOTP is non-NULL, it is filled in with the GOT
1940 section. */
1942 static struct mips_got_info *
1943 mips_elf_got_info (bfd *abfd, asection **sgotp)
1945 asection *sgot;
1946 struct mips_got_info *g;
1948 sgot = mips_elf_got_section (abfd, TRUE);
1949 BFD_ASSERT (sgot != NULL);
1950 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
1951 g = mips_elf_section_data (sgot)->u.got_info;
1952 BFD_ASSERT (g != NULL);
1954 if (sgotp)
1955 *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
1957 return g;
1960 /* Count the number of relocations needed for a TLS GOT entry, with
1961 access types from TLS_TYPE, and symbol H (or a local symbol if H
1962 is NULL). */
1964 static int
1965 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
1966 struct elf_link_hash_entry *h)
1968 int indx = 0;
1969 int ret = 0;
1970 bfd_boolean need_relocs = FALSE;
1971 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
1973 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1974 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
1975 indx = h->dynindx;
1977 if ((info->shared || indx != 0)
1978 && (h == NULL
1979 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1980 || h->root.type != bfd_link_hash_undefweak))
1981 need_relocs = TRUE;
1983 if (!need_relocs)
1984 return FALSE;
1986 if (tls_type & GOT_TLS_GD)
1988 ret++;
1989 if (indx != 0)
1990 ret++;
1993 if (tls_type & GOT_TLS_IE)
1994 ret++;
1996 if ((tls_type & GOT_TLS_LDM) && info->shared)
1997 ret++;
1999 return ret;
2002 /* Count the number of TLS relocations required for the GOT entry in
2003 ARG1, if it describes a local symbol. */
2005 static int
2006 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2008 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2009 struct mips_elf_count_tls_arg *arg = arg2;
2011 if (entry->abfd != NULL && entry->symndx != -1)
2012 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2014 return 1;
2017 /* Count the number of TLS GOT entries required for the global (or
2018 forced-local) symbol in ARG1. */
2020 static int
2021 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2023 struct mips_elf_link_hash_entry *hm
2024 = (struct mips_elf_link_hash_entry *) arg1;
2025 struct mips_elf_count_tls_arg *arg = arg2;
2027 if (hm->tls_type & GOT_TLS_GD)
2028 arg->needed += 2;
2029 if (hm->tls_type & GOT_TLS_IE)
2030 arg->needed += 1;
2032 return 1;
2035 /* Count the number of TLS relocations required for the global (or
2036 forced-local) symbol in ARG1. */
2038 static int
2039 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2041 struct mips_elf_link_hash_entry *hm
2042 = (struct mips_elf_link_hash_entry *) arg1;
2043 struct mips_elf_count_tls_arg *arg = arg2;
2045 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2047 return 1;
2050 /* Output a simple dynamic relocation into SRELOC. */
2052 static void
2053 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2054 asection *sreloc,
2055 unsigned long indx,
2056 int r_type,
2057 bfd_vma offset)
2059 Elf_Internal_Rela rel[3];
2061 memset (rel, 0, sizeof (rel));
2063 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2064 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2066 if (ABI_64_P (output_bfd))
2068 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2069 (output_bfd, &rel[0],
2070 (sreloc->contents
2071 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2073 else
2074 bfd_elf32_swap_reloc_out
2075 (output_bfd, &rel[0],
2076 (sreloc->contents
2077 + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2078 ++sreloc->reloc_count;
2081 /* Initialize a set of TLS GOT entries for one symbol. */
2083 static void
2084 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2085 unsigned char *tls_type_p,
2086 struct bfd_link_info *info,
2087 struct mips_elf_link_hash_entry *h,
2088 bfd_vma value)
2090 int indx;
2091 asection *sreloc, *sgot;
2092 bfd_vma offset, offset2;
2093 bfd *dynobj;
2094 bfd_boolean need_relocs = FALSE;
2096 dynobj = elf_hash_table (info)->dynobj;
2097 sgot = mips_elf_got_section (dynobj, FALSE);
2099 indx = 0;
2100 if (h != NULL)
2102 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2104 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2105 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2106 indx = h->root.dynindx;
2109 if (*tls_type_p & GOT_TLS_DONE)
2110 return;
2112 if ((info->shared || indx != 0)
2113 && (h == NULL
2114 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2115 || h->root.type != bfd_link_hash_undefweak))
2116 need_relocs = TRUE;
2118 /* MINUS_ONE means the symbol is not defined in this object. It may not
2119 be defined at all; assume that the value doesn't matter in that
2120 case. Otherwise complain if we would use the value. */
2121 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2122 || h->root.root.type == bfd_link_hash_undefweak);
2124 /* Emit necessary relocations. */
2125 sreloc = mips_elf_rel_dyn_section (dynobj, FALSE);
2127 /* General Dynamic. */
2128 if (*tls_type_p & GOT_TLS_GD)
2130 offset = got_offset;
2131 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2133 if (need_relocs)
2135 mips_elf_output_dynamic_relocation
2136 (abfd, sreloc, indx,
2137 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2138 sgot->output_offset + sgot->output_section->vma + offset);
2140 if (indx)
2141 mips_elf_output_dynamic_relocation
2142 (abfd, sreloc, indx,
2143 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2144 sgot->output_offset + sgot->output_section->vma + offset2);
2145 else
2146 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2147 sgot->contents + offset2);
2149 else
2151 MIPS_ELF_PUT_WORD (abfd, 1,
2152 sgot->contents + offset);
2153 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2154 sgot->contents + offset2);
2157 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2160 /* Initial Exec model. */
2161 if (*tls_type_p & GOT_TLS_IE)
2163 offset = got_offset;
2165 if (need_relocs)
2167 if (indx == 0)
2168 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2169 sgot->contents + offset);
2170 else
2171 MIPS_ELF_PUT_WORD (abfd, 0,
2172 sgot->contents + offset);
2174 mips_elf_output_dynamic_relocation
2175 (abfd, sreloc, indx,
2176 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2177 sgot->output_offset + sgot->output_section->vma + offset);
2179 else
2180 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2181 sgot->contents + offset);
2184 if (*tls_type_p & GOT_TLS_LDM)
2186 /* The initial offset is zero, and the LD offsets will include the
2187 bias by DTP_OFFSET. */
2188 MIPS_ELF_PUT_WORD (abfd, 0,
2189 sgot->contents + got_offset
2190 + MIPS_ELF_GOT_SIZE (abfd));
2192 if (!info->shared)
2193 MIPS_ELF_PUT_WORD (abfd, 1,
2194 sgot->contents + got_offset);
2195 else
2196 mips_elf_output_dynamic_relocation
2197 (abfd, sreloc, indx,
2198 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2199 sgot->output_offset + sgot->output_section->vma + got_offset);
2202 *tls_type_p |= GOT_TLS_DONE;
2205 /* Return the GOT index to use for a relocation of type R_TYPE against
2206 a symbol accessed using TLS_TYPE models. The GOT entries for this
2207 symbol in this GOT start at GOT_INDEX. This function initializes the
2208 GOT entries and corresponding relocations. */
2210 static bfd_vma
2211 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2212 int r_type, struct bfd_link_info *info,
2213 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2215 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2216 || r_type == R_MIPS_TLS_LDM);
2218 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2220 if (r_type == R_MIPS_TLS_GOTTPREL)
2222 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2223 if (*tls_type & GOT_TLS_GD)
2224 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2225 else
2226 return got_index;
2229 if (r_type == R_MIPS_TLS_GD)
2231 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2232 return got_index;
2235 if (r_type == R_MIPS_TLS_LDM)
2237 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2238 return got_index;
2241 return got_index;
2244 /* Returns the GOT offset at which the indicated address can be found.
2245 If there is not yet a GOT entry for this value, create one. If
2246 R_SYMNDX refers to a TLS symbol, create a TLS GOT entry instead.
2247 Returns -1 if no satisfactory GOT offset can be found. */
2249 static bfd_vma
2250 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2251 bfd_vma value, unsigned long r_symndx,
2252 struct mips_elf_link_hash_entry *h, int r_type)
2254 asection *sgot;
2255 struct mips_got_info *g;
2256 struct mips_got_entry *entry;
2258 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2260 entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot, value,
2261 r_symndx, h, r_type);
2262 if (!entry)
2263 return MINUS_ONE;
2265 if (TLS_RELOC_P (r_type))
2266 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type, r_type,
2267 info, h, value);
2268 else
2269 return entry->gotidx;
2272 /* Returns the GOT index for the global symbol indicated by H. */
2274 static bfd_vma
2275 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2276 int r_type, struct bfd_link_info *info)
2278 bfd_vma index;
2279 asection *sgot;
2280 struct mips_got_info *g, *gg;
2281 long global_got_dynindx = 0;
2283 gg = g = mips_elf_got_info (abfd, &sgot);
2284 if (g->bfd2got && ibfd)
2286 struct mips_got_entry e, *p;
2288 BFD_ASSERT (h->dynindx >= 0);
2290 g = mips_elf_got_for_ibfd (g, ibfd);
2291 if (g->next != gg || TLS_RELOC_P (r_type))
2293 e.abfd = ibfd;
2294 e.symndx = -1;
2295 e.d.h = (struct mips_elf_link_hash_entry *)h;
2296 e.tls_type = 0;
2298 p = htab_find (g->got_entries, &e);
2300 BFD_ASSERT (p->gotidx > 0);
2302 if (TLS_RELOC_P (r_type))
2304 bfd_vma value = MINUS_ONE;
2305 if ((h->root.type == bfd_link_hash_defined
2306 || h->root.type == bfd_link_hash_defweak)
2307 && h->root.u.def.section->output_section)
2308 value = (h->root.u.def.value
2309 + h->root.u.def.section->output_offset
2310 + h->root.u.def.section->output_section->vma);
2312 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2313 info, e.d.h, value);
2315 else
2316 return p->gotidx;
2320 if (gg->global_gotsym != NULL)
2321 global_got_dynindx = gg->global_gotsym->dynindx;
2323 if (TLS_RELOC_P (r_type))
2325 struct mips_elf_link_hash_entry *hm
2326 = (struct mips_elf_link_hash_entry *) h;
2327 bfd_vma value = MINUS_ONE;
2329 if ((h->root.type == bfd_link_hash_defined
2330 || h->root.type == bfd_link_hash_defweak)
2331 && h->root.u.def.section->output_section)
2332 value = (h->root.u.def.value
2333 + h->root.u.def.section->output_offset
2334 + h->root.u.def.section->output_section->vma);
2336 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2337 r_type, info, hm, value);
2339 else
2341 /* Once we determine the global GOT entry with the lowest dynamic
2342 symbol table index, we must put all dynamic symbols with greater
2343 indices into the GOT. That makes it easy to calculate the GOT
2344 offset. */
2345 BFD_ASSERT (h->dynindx >= global_got_dynindx);
2346 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2347 * MIPS_ELF_GOT_SIZE (abfd));
2349 BFD_ASSERT (index < sgot->size);
2351 return index;
2354 /* Find a GOT entry that is within 32KB of the VALUE. These entries
2355 are supposed to be placed at small offsets in the GOT, i.e.,
2356 within 32KB of GP. Return the index into the GOT for this page,
2357 and store the offset from this entry to the desired address in
2358 OFFSETP, if it is non-NULL. */
2360 static bfd_vma
2361 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2362 bfd_vma value, bfd_vma *offsetp)
2364 asection *sgot;
2365 struct mips_got_info *g;
2366 bfd_vma index;
2367 struct mips_got_entry *entry;
2369 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2371 entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot,
2372 (value + 0x8000)
2373 & (~(bfd_vma)0xffff), 0,
2374 NULL, R_MIPS_GOT_PAGE);
2376 if (!entry)
2377 return MINUS_ONE;
2379 index = entry->gotidx;
2381 if (offsetp)
2382 *offsetp = value - entry->d.address;
2384 return index;
2387 /* Find a GOT entry whose higher-order 16 bits are the same as those
2388 for value. Return the index into the GOT for this entry. */
2390 static bfd_vma
2391 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2392 bfd_vma value, bfd_boolean external)
2394 asection *sgot;
2395 struct mips_got_info *g;
2396 struct mips_got_entry *entry;
2398 if (! external)
2400 /* Although the ABI says that it is "the high-order 16 bits" that we
2401 want, it is really the %high value. The complete value is
2402 calculated with a `addiu' of a LO16 relocation, just as with a
2403 HI16/LO16 pair. */
2404 value = mips_elf_high (value) << 16;
2407 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2409 entry = mips_elf_create_local_got_entry (abfd, ibfd, g, sgot, value, 0, NULL,
2410 R_MIPS_GOT16);
2411 if (entry)
2412 return entry->gotidx;
2413 else
2414 return MINUS_ONE;
2417 /* Returns the offset for the entry at the INDEXth position
2418 in the GOT. */
2420 static bfd_vma
2421 mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2422 bfd *input_bfd, bfd_vma index)
2424 asection *sgot;
2425 bfd_vma gp;
2426 struct mips_got_info *g;
2428 g = mips_elf_got_info (dynobj, &sgot);
2429 gp = _bfd_get_gp_value (output_bfd)
2430 + mips_elf_adjust_gp (output_bfd, g, input_bfd);
2432 return sgot->output_section->vma + sgot->output_offset + index - gp;
2435 /* Create a local GOT entry for VALUE. Return the index of the entry,
2436 or -1 if it could not be created. If R_SYMNDX refers to a TLS symbol,
2437 create a TLS entry instead. */
2439 static struct mips_got_entry *
2440 mips_elf_create_local_got_entry (bfd *abfd, bfd *ibfd,
2441 struct mips_got_info *gg,
2442 asection *sgot, bfd_vma value,
2443 unsigned long r_symndx,
2444 struct mips_elf_link_hash_entry *h,
2445 int r_type)
2447 struct mips_got_entry entry, **loc;
2448 struct mips_got_info *g;
2450 entry.abfd = NULL;
2451 entry.symndx = -1;
2452 entry.d.address = value;
2453 entry.tls_type = 0;
2455 g = mips_elf_got_for_ibfd (gg, ibfd);
2456 if (g == NULL)
2458 g = mips_elf_got_for_ibfd (gg, abfd);
2459 BFD_ASSERT (g != NULL);
2462 /* We might have a symbol, H, if it has been forced local. Use the
2463 global entry then. It doesn't matter whether an entry is local
2464 or global for TLS, since the dynamic linker does not
2465 automatically relocate TLS GOT entries. */
2466 BFD_ASSERT (h == NULL || h->root.forced_local);
2467 if (TLS_RELOC_P (r_type))
2469 struct mips_got_entry *p;
2471 entry.abfd = ibfd;
2472 if (r_type == R_MIPS_TLS_LDM)
2474 entry.tls_type = GOT_TLS_LDM;
2475 entry.symndx = 0;
2476 entry.d.addend = 0;
2478 else if (h == NULL)
2480 entry.symndx = r_symndx;
2481 entry.d.addend = 0;
2483 else
2484 entry.d.h = h;
2486 p = (struct mips_got_entry *)
2487 htab_find (g->got_entries, &entry);
2489 BFD_ASSERT (p);
2490 return p;
2493 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2494 INSERT);
2495 if (*loc)
2496 return *loc;
2498 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2499 entry.tls_type = 0;
2501 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2503 if (! *loc)
2504 return NULL;
2506 memcpy (*loc, &entry, sizeof entry);
2508 if (g->assigned_gotno >= g->local_gotno)
2510 (*loc)->gotidx = -1;
2511 /* We didn't allocate enough space in the GOT. */
2512 (*_bfd_error_handler)
2513 (_("not enough GOT space for local GOT entries"));
2514 bfd_set_error (bfd_error_bad_value);
2515 return NULL;
2518 MIPS_ELF_PUT_WORD (abfd, value,
2519 (sgot->contents + entry.gotidx));
2521 return *loc;
2524 /* Sort the dynamic symbol table so that symbols that need GOT entries
2525 appear towards the end. This reduces the amount of GOT space
2526 required. MAX_LOCAL is used to set the number of local symbols
2527 known to be in the dynamic symbol table. During
2528 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
2529 section symbols are added and the count is higher. */
2531 static bfd_boolean
2532 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2534 struct mips_elf_hash_sort_data hsd;
2535 struct mips_got_info *g;
2536 bfd *dynobj;
2538 dynobj = elf_hash_table (info)->dynobj;
2540 g = mips_elf_got_info (dynobj, NULL);
2542 hsd.low = NULL;
2543 hsd.max_unref_got_dynindx =
2544 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2545 /* In the multi-got case, assigned_gotno of the master got_info
2546 indicate the number of entries that aren't referenced in the
2547 primary GOT, but that must have entries because there are
2548 dynamic relocations that reference it. Since they aren't
2549 referenced, we move them to the end of the GOT, so that they
2550 don't prevent other entries that are referenced from getting
2551 too large offsets. */
2552 - (g->next ? g->assigned_gotno : 0);
2553 hsd.max_non_got_dynindx = max_local;
2554 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2555 elf_hash_table (info)),
2556 mips_elf_sort_hash_table_f,
2557 &hsd);
2559 /* There should have been enough room in the symbol table to
2560 accommodate both the GOT and non-GOT symbols. */
2561 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2562 BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2563 <= elf_hash_table (info)->dynsymcount);
2565 /* Now we know which dynamic symbol has the lowest dynamic symbol
2566 table index in the GOT. */
2567 g->global_gotsym = hsd.low;
2569 return TRUE;
2572 /* If H needs a GOT entry, assign it the highest available dynamic
2573 index. Otherwise, assign it the lowest available dynamic
2574 index. */
2576 static bfd_boolean
2577 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2579 struct mips_elf_hash_sort_data *hsd = data;
2581 if (h->root.root.type == bfd_link_hash_warning)
2582 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2584 /* Symbols without dynamic symbol table entries aren't interesting
2585 at all. */
2586 if (h->root.dynindx == -1)
2587 return TRUE;
2589 /* Global symbols that need GOT entries that are not explicitly
2590 referenced are marked with got offset 2. Those that are
2591 referenced get a 1, and those that don't need GOT entries get
2592 -1. */
2593 if (h->root.got.offset == 2)
2595 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2597 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2598 hsd->low = (struct elf_link_hash_entry *) h;
2599 h->root.dynindx = hsd->max_unref_got_dynindx++;
2601 else if (h->root.got.offset != 1)
2602 h->root.dynindx = hsd->max_non_got_dynindx++;
2603 else
2605 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2607 h->root.dynindx = --hsd->min_got_dynindx;
2608 hsd->low = (struct elf_link_hash_entry *) h;
2611 return TRUE;
2614 /* If H is a symbol that needs a global GOT entry, but has a dynamic
2615 symbol table index lower than any we've seen to date, record it for
2616 posterity. */
2618 static bfd_boolean
2619 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
2620 bfd *abfd, struct bfd_link_info *info,
2621 struct mips_got_info *g,
2622 unsigned char tls_flag)
2624 struct mips_got_entry entry, **loc;
2626 /* A global symbol in the GOT must also be in the dynamic symbol
2627 table. */
2628 if (h->dynindx == -1)
2630 switch (ELF_ST_VISIBILITY (h->other))
2632 case STV_INTERNAL:
2633 case STV_HIDDEN:
2634 _bfd_mips_elf_hide_symbol (info, h, TRUE);
2635 break;
2637 if (!bfd_elf_link_record_dynamic_symbol (info, h))
2638 return FALSE;
2641 entry.abfd = abfd;
2642 entry.symndx = -1;
2643 entry.d.h = (struct mips_elf_link_hash_entry *) h;
2644 entry.tls_type = 0;
2646 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2647 INSERT);
2649 /* If we've already marked this entry as needing GOT space, we don't
2650 need to do it again. */
2651 if (*loc)
2653 (*loc)->tls_type |= tls_flag;
2654 return TRUE;
2657 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2659 if (! *loc)
2660 return FALSE;
2662 entry.gotidx = -1;
2663 entry.tls_type = tls_flag;
2665 memcpy (*loc, &entry, sizeof entry);
2667 if (h->got.offset != MINUS_ONE)
2668 return TRUE;
2670 /* By setting this to a value other than -1, we are indicating that
2671 there needs to be a GOT entry for H. Avoid using zero, as the
2672 generic ELF copy_indirect_symbol tests for <= 0. */
2673 if (tls_flag == 0)
2674 h->got.offset = 1;
2676 return TRUE;
2679 /* Reserve space in G for a GOT entry containing the value of symbol
2680 SYMNDX in input bfd ABDF, plus ADDEND. */
2682 static bfd_boolean
2683 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
2684 struct mips_got_info *g,
2685 unsigned char tls_flag)
2687 struct mips_got_entry entry, **loc;
2689 entry.abfd = abfd;
2690 entry.symndx = symndx;
2691 entry.d.addend = addend;
2692 entry.tls_type = tls_flag;
2693 loc = (struct mips_got_entry **)
2694 htab_find_slot (g->got_entries, &entry, INSERT);
2696 if (*loc)
2698 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
2700 g->tls_gotno += 2;
2701 (*loc)->tls_type |= tls_flag;
2703 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
2705 g->tls_gotno += 1;
2706 (*loc)->tls_type |= tls_flag;
2708 return TRUE;
2711 if (tls_flag != 0)
2713 entry.gotidx = -1;
2714 entry.tls_type = tls_flag;
2715 if (tls_flag == GOT_TLS_IE)
2716 g->tls_gotno += 1;
2717 else if (tls_flag == GOT_TLS_GD)
2718 g->tls_gotno += 2;
2719 else if (g->tls_ldm_offset == MINUS_ONE)
2721 g->tls_ldm_offset = MINUS_TWO;
2722 g->tls_gotno += 2;
2725 else
2727 entry.gotidx = g->local_gotno++;
2728 entry.tls_type = 0;
2731 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2733 if (! *loc)
2734 return FALSE;
2736 memcpy (*loc, &entry, sizeof entry);
2738 return TRUE;
2741 /* Compute the hash value of the bfd in a bfd2got hash entry. */
2743 static hashval_t
2744 mips_elf_bfd2got_entry_hash (const void *entry_)
2746 const struct mips_elf_bfd2got_hash *entry
2747 = (struct mips_elf_bfd2got_hash *)entry_;
2749 return entry->bfd->id;
2752 /* Check whether two hash entries have the same bfd. */
2754 static int
2755 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
2757 const struct mips_elf_bfd2got_hash *e1
2758 = (const struct mips_elf_bfd2got_hash *)entry1;
2759 const struct mips_elf_bfd2got_hash *e2
2760 = (const struct mips_elf_bfd2got_hash *)entry2;
2762 return e1->bfd == e2->bfd;
2765 /* In a multi-got link, determine the GOT to be used for IBDF. G must
2766 be the master GOT data. */
2768 static struct mips_got_info *
2769 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
2771 struct mips_elf_bfd2got_hash e, *p;
2773 if (! g->bfd2got)
2774 return g;
2776 e.bfd = ibfd;
2777 p = htab_find (g->bfd2got, &e);
2778 return p ? p->g : NULL;
2781 /* Create one separate got for each bfd that has entries in the global
2782 got, such that we can tell how many local and global entries each
2783 bfd requires. */
2785 static int
2786 mips_elf_make_got_per_bfd (void **entryp, void *p)
2788 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2789 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2790 htab_t bfd2got = arg->bfd2got;
2791 struct mips_got_info *g;
2792 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
2793 void **bfdgotp;
2795 /* Find the got_info for this GOT entry's input bfd. Create one if
2796 none exists. */
2797 bfdgot_entry.bfd = entry->abfd;
2798 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
2799 bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
2801 if (bfdgot != NULL)
2802 g = bfdgot->g;
2803 else
2805 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
2806 (arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
2808 if (bfdgot == NULL)
2810 arg->obfd = 0;
2811 return 0;
2814 *bfdgotp = bfdgot;
2816 bfdgot->bfd = entry->abfd;
2817 bfdgot->g = g = (struct mips_got_info *)
2818 bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
2819 if (g == NULL)
2821 arg->obfd = 0;
2822 return 0;
2825 g->global_gotsym = NULL;
2826 g->global_gotno = 0;
2827 g->local_gotno = 0;
2828 g->assigned_gotno = -1;
2829 g->tls_gotno = 0;
2830 g->tls_assigned_gotno = 0;
2831 g->tls_ldm_offset = MINUS_ONE;
2832 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
2833 mips_elf_multi_got_entry_eq, NULL);
2834 if (g->got_entries == NULL)
2836 arg->obfd = 0;
2837 return 0;
2840 g->bfd2got = NULL;
2841 g->next = NULL;
2844 /* Insert the GOT entry in the bfd's got entry hash table. */
2845 entryp = htab_find_slot (g->got_entries, entry, INSERT);
2846 if (*entryp != NULL)
2847 return 1;
2849 *entryp = entry;
2851 if (entry->tls_type)
2853 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
2854 g->tls_gotno += 2;
2855 if (entry->tls_type & GOT_TLS_IE)
2856 g->tls_gotno += 1;
2858 else if (entry->symndx >= 0 || entry->d.h->forced_local)
2859 ++g->local_gotno;
2860 else
2861 ++g->global_gotno;
2863 return 1;
2866 /* Attempt to merge gots of different input bfds. Try to use as much
2867 as possible of the primary got, since it doesn't require explicit
2868 dynamic relocations, but don't use bfds that would reference global
2869 symbols out of the addressable range. Failing the primary got,
2870 attempt to merge with the current got, or finish the current got
2871 and then make make the new got current. */
2873 static int
2874 mips_elf_merge_gots (void **bfd2got_, void *p)
2876 struct mips_elf_bfd2got_hash *bfd2got
2877 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
2878 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2879 unsigned int lcount = bfd2got->g->local_gotno;
2880 unsigned int gcount = bfd2got->g->global_gotno;
2881 unsigned int tcount = bfd2got->g->tls_gotno;
2882 unsigned int maxcnt = arg->max_count;
2883 bfd_boolean too_many_for_tls = FALSE;
2885 /* We place TLS GOT entries after both locals and globals. The globals
2886 for the primary GOT may overflow the normal GOT size limit, so be
2887 sure not to merge a GOT which requires TLS with the primary GOT in that
2888 case. This doesn't affect non-primary GOTs. */
2889 if (tcount > 0)
2891 unsigned int primary_total = lcount + tcount + arg->global_count;
2892 if (primary_total * MIPS_ELF_GOT_SIZE (bfd2got->bfd)
2893 >= MIPS_ELF_GOT_MAX_SIZE (bfd2got->bfd))
2894 too_many_for_tls = TRUE;
2897 /* If we don't have a primary GOT and this is not too big, use it as
2898 a starting point for the primary GOT. */
2899 if (! arg->primary && lcount + gcount + tcount <= maxcnt
2900 && ! too_many_for_tls)
2902 arg->primary = bfd2got->g;
2903 arg->primary_count = lcount + gcount;
2905 /* If it looks like we can merge this bfd's entries with those of
2906 the primary, merge them. The heuristics is conservative, but we
2907 don't have to squeeze it too hard. */
2908 else if (arg->primary && ! too_many_for_tls
2909 && (arg->primary_count + lcount + gcount + tcount) <= maxcnt)
2911 struct mips_got_info *g = bfd2got->g;
2912 int old_lcount = arg->primary->local_gotno;
2913 int old_gcount = arg->primary->global_gotno;
2914 int old_tcount = arg->primary->tls_gotno;
2916 bfd2got->g = arg->primary;
2918 htab_traverse (g->got_entries,
2919 mips_elf_make_got_per_bfd,
2920 arg);
2921 if (arg->obfd == NULL)
2922 return 0;
2924 htab_delete (g->got_entries);
2925 /* We don't have to worry about releasing memory of the actual
2926 got entries, since they're all in the master got_entries hash
2927 table anyway. */
2929 BFD_ASSERT (old_lcount + lcount >= arg->primary->local_gotno);
2930 BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
2931 BFD_ASSERT (old_tcount + tcount >= arg->primary->tls_gotno);
2933 arg->primary_count = arg->primary->local_gotno
2934 + arg->primary->global_gotno + arg->primary->tls_gotno;
2936 /* If we can merge with the last-created got, do it. */
2937 else if (arg->current
2938 && arg->current_count + lcount + gcount + tcount <= maxcnt)
2940 struct mips_got_info *g = bfd2got->g;
2941 int old_lcount = arg->current->local_gotno;
2942 int old_gcount = arg->current->global_gotno;
2943 int old_tcount = arg->current->tls_gotno;
2945 bfd2got->g = arg->current;
2947 htab_traverse (g->got_entries,
2948 mips_elf_make_got_per_bfd,
2949 arg);
2950 if (arg->obfd == NULL)
2951 return 0;
2953 htab_delete (g->got_entries);
2955 BFD_ASSERT (old_lcount + lcount >= arg->current->local_gotno);
2956 BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
2957 BFD_ASSERT (old_tcount + tcount >= arg->current->tls_gotno);
2959 arg->current_count = arg->current->local_gotno
2960 + arg->current->global_gotno + arg->current->tls_gotno;
2962 /* Well, we couldn't merge, so create a new GOT. Don't check if it
2963 fits; if it turns out that it doesn't, we'll get relocation
2964 overflows anyway. */
2965 else
2967 bfd2got->g->next = arg->current;
2968 arg->current = bfd2got->g;
2970 arg->current_count = lcount + gcount + 2 * tcount;
2973 return 1;
2976 /* Set the TLS GOT index for the GOT entry in ENTRYP. */
2978 static int
2979 mips_elf_initialize_tls_index (void **entryp, void *p)
2981 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2982 struct mips_got_info *g = p;
2984 /* We're only interested in TLS symbols. */
2985 if (entry->tls_type == 0)
2986 return 1;
2988 if (entry->symndx == -1)
2990 /* There may be multiple mips_got_entry structs for a global variable
2991 if there is just one GOT. Just do this once. */
2992 if (g->next == NULL)
2994 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
2995 return 1;
2996 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
2999 else if (entry->tls_type & GOT_TLS_LDM)
3001 /* Similarly, there may be multiple structs for the LDM entry. */
3002 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
3004 entry->gotidx = g->tls_ldm_offset;
3005 return 1;
3009 /* Initialize the GOT offset. */
3010 entry->gotidx = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3011 if (g->next == NULL && entry->symndx == -1)
3012 entry->d.h->tls_got_offset = entry->gotidx;
3014 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3015 g->tls_assigned_gotno += 2;
3016 if (entry->tls_type & GOT_TLS_IE)
3017 g->tls_assigned_gotno += 1;
3019 if (entry->tls_type & GOT_TLS_LDM)
3020 g->tls_ldm_offset = entry->gotidx;
3022 return 1;
3025 /* If passed a NULL mips_got_info in the argument, set the marker used
3026 to tell whether a global symbol needs a got entry (in the primary
3027 got) to the given VALUE.
3029 If passed a pointer G to a mips_got_info in the argument (it must
3030 not be the primary GOT), compute the offset from the beginning of
3031 the (primary) GOT section to the entry in G corresponding to the
3032 global symbol. G's assigned_gotno must contain the index of the
3033 first available global GOT entry in G. VALUE must contain the size
3034 of a GOT entry in bytes. For each global GOT entry that requires a
3035 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3036 marked as not eligible for lazy resolution through a function
3037 stub. */
3038 static int
3039 mips_elf_set_global_got_offset (void **entryp, void *p)
3041 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3042 struct mips_elf_set_global_got_offset_arg *arg
3043 = (struct mips_elf_set_global_got_offset_arg *)p;
3044 struct mips_got_info *g = arg->g;
3046 if (g && entry->tls_type != GOT_NORMAL)
3047 arg->needed_relocs +=
3048 mips_tls_got_relocs (arg->info, entry->tls_type,
3049 entry->symndx == -1 ? &entry->d.h->root : NULL);
3051 if (entry->abfd != NULL && entry->symndx == -1
3052 && entry->d.h->root.dynindx != -1
3053 && entry->d.h->tls_type == GOT_NORMAL)
3055 if (g)
3057 BFD_ASSERT (g->global_gotsym == NULL);
3059 entry->gotidx = arg->value * (long) g->assigned_gotno++;
3060 if (arg->info->shared
3061 || (elf_hash_table (arg->info)->dynamic_sections_created
3062 && entry->d.h->root.def_dynamic
3063 && !entry->d.h->root.def_regular))
3064 ++arg->needed_relocs;
3066 else
3067 entry->d.h->root.got.offset = arg->value;
3070 return 1;
3073 /* Mark any global symbols referenced in the GOT we are iterating over
3074 as inelligible for lazy resolution stubs. */
3075 static int
3076 mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
3078 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3080 if (entry->abfd != NULL
3081 && entry->symndx == -1
3082 && entry->d.h->root.dynindx != -1)
3083 entry->d.h->no_fn_stub = TRUE;
3085 return 1;
3088 /* Follow indirect and warning hash entries so that each got entry
3089 points to the final symbol definition. P must point to a pointer
3090 to the hash table we're traversing. Since this traversal may
3091 modify the hash table, we set this pointer to NULL to indicate
3092 we've made a potentially-destructive change to the hash table, so
3093 the traversal must be restarted. */
3094 static int
3095 mips_elf_resolve_final_got_entry (void **entryp, void *p)
3097 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3098 htab_t got_entries = *(htab_t *)p;
3100 if (entry->abfd != NULL && entry->symndx == -1)
3102 struct mips_elf_link_hash_entry *h = entry->d.h;
3104 while (h->root.root.type == bfd_link_hash_indirect
3105 || h->root.root.type == bfd_link_hash_warning)
3106 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3108 if (entry->d.h == h)
3109 return 1;
3111 entry->d.h = h;
3113 /* If we can't find this entry with the new bfd hash, re-insert
3114 it, and get the traversal restarted. */
3115 if (! htab_find (got_entries, entry))
3117 htab_clear_slot (got_entries, entryp);
3118 entryp = htab_find_slot (got_entries, entry, INSERT);
3119 if (! *entryp)
3120 *entryp = entry;
3121 /* Abort the traversal, since the whole table may have
3122 moved, and leave it up to the parent to restart the
3123 process. */
3124 *(htab_t *)p = NULL;
3125 return 0;
3127 /* We might want to decrement the global_gotno count, but it's
3128 either too early or too late for that at this point. */
3131 return 1;
3134 /* Turn indirect got entries in a got_entries table into their final
3135 locations. */
3136 static void
3137 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3139 htab_t got_entries;
3143 got_entries = g->got_entries;
3145 htab_traverse (got_entries,
3146 mips_elf_resolve_final_got_entry,
3147 &got_entries);
3149 while (got_entries == NULL);
3152 /* Return the offset of an input bfd IBFD's GOT from the beginning of
3153 the primary GOT. */
3154 static bfd_vma
3155 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3157 if (g->bfd2got == NULL)
3158 return 0;
3160 g = mips_elf_got_for_ibfd (g, ibfd);
3161 if (! g)
3162 return 0;
3164 BFD_ASSERT (g->next);
3166 g = g->next;
3168 return (g->local_gotno + g->global_gotno + g->tls_gotno)
3169 * MIPS_ELF_GOT_SIZE (abfd);
3172 /* Turn a single GOT that is too big for 16-bit addressing into
3173 a sequence of GOTs, each one 16-bit addressable. */
3175 static bfd_boolean
3176 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3177 struct mips_got_info *g, asection *got,
3178 bfd_size_type pages)
3180 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3181 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3182 struct mips_got_info *gg;
3183 unsigned int assign;
3185 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3186 mips_elf_bfd2got_entry_eq, NULL);
3187 if (g->bfd2got == NULL)
3188 return FALSE;
3190 got_per_bfd_arg.bfd2got = g->bfd2got;
3191 got_per_bfd_arg.obfd = abfd;
3192 got_per_bfd_arg.info = info;
3194 /* Count how many GOT entries each input bfd requires, creating a
3195 map from bfd to got info while at that. */
3196 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3197 if (got_per_bfd_arg.obfd == NULL)
3198 return FALSE;
3200 got_per_bfd_arg.current = NULL;
3201 got_per_bfd_arg.primary = NULL;
3202 /* Taking out PAGES entries is a worst-case estimate. We could
3203 compute the maximum number of pages that each separate input bfd
3204 uses, but it's probably not worth it. */
3205 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (abfd)
3206 / MIPS_ELF_GOT_SIZE (abfd))
3207 - MIPS_RESERVED_GOTNO - pages);
3208 /* The number of globals that will be included in the primary GOT.
3209 See the calls to mips_elf_set_global_got_offset below for more
3210 information. */
3211 got_per_bfd_arg.global_count = g->global_gotno;
3213 /* Try to merge the GOTs of input bfds together, as long as they
3214 don't seem to exceed the maximum GOT size, choosing one of them
3215 to be the primary GOT. */
3216 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3217 if (got_per_bfd_arg.obfd == NULL)
3218 return FALSE;
3220 /* If we do not find any suitable primary GOT, create an empty one. */
3221 if (got_per_bfd_arg.primary == NULL)
3223 g->next = (struct mips_got_info *)
3224 bfd_alloc (abfd, sizeof (struct mips_got_info));
3225 if (g->next == NULL)
3226 return FALSE;
3228 g->next->global_gotsym = NULL;
3229 g->next->global_gotno = 0;
3230 g->next->local_gotno = 0;
3231 g->next->tls_gotno = 0;
3232 g->next->assigned_gotno = 0;
3233 g->next->tls_assigned_gotno = 0;
3234 g->next->tls_ldm_offset = MINUS_ONE;
3235 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3236 mips_elf_multi_got_entry_eq,
3237 NULL);
3238 if (g->next->got_entries == NULL)
3239 return FALSE;
3240 g->next->bfd2got = NULL;
3242 else
3243 g->next = got_per_bfd_arg.primary;
3244 g->next->next = got_per_bfd_arg.current;
3246 /* GG is now the master GOT, and G is the primary GOT. */
3247 gg = g;
3248 g = g->next;
3250 /* Map the output bfd to the primary got. That's what we're going
3251 to use for bfds that use GOT16 or GOT_PAGE relocations that we
3252 didn't mark in check_relocs, and we want a quick way to find it.
3253 We can't just use gg->next because we're going to reverse the
3254 list. */
3256 struct mips_elf_bfd2got_hash *bfdgot;
3257 void **bfdgotp;
3259 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3260 (abfd, sizeof (struct mips_elf_bfd2got_hash));
3262 if (bfdgot == NULL)
3263 return FALSE;
3265 bfdgot->bfd = abfd;
3266 bfdgot->g = g;
3267 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3269 BFD_ASSERT (*bfdgotp == NULL);
3270 *bfdgotp = bfdgot;
3273 /* The IRIX dynamic linker requires every symbol that is referenced
3274 in a dynamic relocation to be present in the primary GOT, so
3275 arrange for them to appear after those that are actually
3276 referenced.
3278 GNU/Linux could very well do without it, but it would slow down
3279 the dynamic linker, since it would have to resolve every dynamic
3280 symbol referenced in other GOTs more than once, without help from
3281 the cache. Also, knowing that every external symbol has a GOT
3282 helps speed up the resolution of local symbols too, so GNU/Linux
3283 follows IRIX's practice.
3285 The number 2 is used by mips_elf_sort_hash_table_f to count
3286 global GOT symbols that are unreferenced in the primary GOT, with
3287 an initial dynamic index computed from gg->assigned_gotno, where
3288 the number of unreferenced global entries in the primary GOT is
3289 preserved. */
3290 if (1)
3292 gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3293 g->global_gotno = gg->global_gotno;
3294 set_got_offset_arg.value = 2;
3296 else
3298 /* This could be used for dynamic linkers that don't optimize
3299 symbol resolution while applying relocations so as to use
3300 primary GOT entries or assuming the symbol is locally-defined.
3301 With this code, we assign lower dynamic indices to global
3302 symbols that are not referenced in the primary GOT, so that
3303 their entries can be omitted. */
3304 gg->assigned_gotno = 0;
3305 set_got_offset_arg.value = -1;
3308 /* Reorder dynamic symbols as described above (which behavior
3309 depends on the setting of VALUE). */
3310 set_got_offset_arg.g = NULL;
3311 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3312 &set_got_offset_arg);
3313 set_got_offset_arg.value = 1;
3314 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3315 &set_got_offset_arg);
3316 if (! mips_elf_sort_hash_table (info, 1))
3317 return FALSE;
3319 /* Now go through the GOTs assigning them offset ranges.
3320 [assigned_gotno, local_gotno[ will be set to the range of local
3321 entries in each GOT. We can then compute the end of a GOT by
3322 adding local_gotno to global_gotno. We reverse the list and make
3323 it circular since then we'll be able to quickly compute the
3324 beginning of a GOT, by computing the end of its predecessor. To
3325 avoid special cases for the primary GOT, while still preserving
3326 assertions that are valid for both single- and multi-got links,
3327 we arrange for the main got struct to have the right number of
3328 global entries, but set its local_gotno such that the initial
3329 offset of the primary GOT is zero. Remember that the primary GOT
3330 will become the last item in the circular linked list, so it
3331 points back to the master GOT. */
3332 gg->local_gotno = -g->global_gotno;
3333 gg->global_gotno = g->global_gotno;
3334 gg->tls_gotno = 0;
3335 assign = 0;
3336 gg->next = gg;
3340 struct mips_got_info *gn;
3342 assign += MIPS_RESERVED_GOTNO;
3343 g->assigned_gotno = assign;
3344 g->local_gotno += assign + pages;
3345 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3347 /* Set up any TLS entries. We always place the TLS entries after
3348 all non-TLS entries. */
3349 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3350 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3352 /* Take g out of the direct list, and push it onto the reversed
3353 list that gg points to. */
3354 gn = g->next;
3355 g->next = gg->next;
3356 gg->next = g;
3357 g = gn;
3359 /* Mark global symbols in every non-primary GOT as ineligible for
3360 stubs. */
3361 if (g)
3362 htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
3364 while (g);
3366 got->size = (gg->next->local_gotno
3367 + gg->next->global_gotno
3368 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
3370 return TRUE;
3374 /* Returns the first relocation of type r_type found, beginning with
3375 RELOCATION. RELEND is one-past-the-end of the relocation table. */
3377 static const Elf_Internal_Rela *
3378 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3379 const Elf_Internal_Rela *relocation,
3380 const Elf_Internal_Rela *relend)
3382 while (relocation < relend)
3384 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type)
3385 return relocation;
3387 ++relocation;
3390 /* We didn't find it. */
3391 bfd_set_error (bfd_error_bad_value);
3392 return NULL;
3395 /* Return whether a relocation is against a local symbol. */
3397 static bfd_boolean
3398 mips_elf_local_relocation_p (bfd *input_bfd,
3399 const Elf_Internal_Rela *relocation,
3400 asection **local_sections,
3401 bfd_boolean check_forced)
3403 unsigned long r_symndx;
3404 Elf_Internal_Shdr *symtab_hdr;
3405 struct mips_elf_link_hash_entry *h;
3406 size_t extsymoff;
3408 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3409 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3410 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3412 if (r_symndx < extsymoff)
3413 return TRUE;
3414 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3415 return TRUE;
3417 if (check_forced)
3419 /* Look up the hash table to check whether the symbol
3420 was forced local. */
3421 h = (struct mips_elf_link_hash_entry *)
3422 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3423 /* Find the real hash-table entry for this symbol. */
3424 while (h->root.root.type == bfd_link_hash_indirect
3425 || h->root.root.type == bfd_link_hash_warning)
3426 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3427 if (h->root.forced_local)
3428 return TRUE;
3431 return FALSE;
3434 /* Sign-extend VALUE, which has the indicated number of BITS. */
3436 bfd_vma
3437 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
3439 if (value & ((bfd_vma) 1 << (bits - 1)))
3440 /* VALUE is negative. */
3441 value |= ((bfd_vma) - 1) << bits;
3443 return value;
3446 /* Return non-zero if the indicated VALUE has overflowed the maximum
3447 range expressible by a signed number with the indicated number of
3448 BITS. */
3450 static bfd_boolean
3451 mips_elf_overflow_p (bfd_vma value, int bits)
3453 bfd_signed_vma svalue = (bfd_signed_vma) value;
3455 if (svalue > (1 << (bits - 1)) - 1)
3456 /* The value is too big. */
3457 return TRUE;
3458 else if (svalue < -(1 << (bits - 1)))
3459 /* The value is too small. */
3460 return TRUE;
3462 /* All is well. */
3463 return FALSE;
3466 /* Calculate the %high function. */
3468 static bfd_vma
3469 mips_elf_high (bfd_vma value)
3471 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
3474 /* Calculate the %higher function. */
3476 static bfd_vma
3477 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
3479 #ifdef BFD64
3480 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
3481 #else
3482 abort ();
3483 return MINUS_ONE;
3484 #endif
3487 /* Calculate the %highest function. */
3489 static bfd_vma
3490 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
3492 #ifdef BFD64
3493 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
3494 #else
3495 abort ();
3496 return MINUS_ONE;
3497 #endif
3500 /* Create the .compact_rel section. */
3502 static bfd_boolean
3503 mips_elf_create_compact_rel_section
3504 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
3506 flagword flags;
3507 register asection *s;
3509 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
3511 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
3512 | SEC_READONLY);
3514 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
3515 if (s == NULL
3516 || ! bfd_set_section_alignment (abfd, s,
3517 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3518 return FALSE;
3520 s->size = sizeof (Elf32_External_compact_rel);
3523 return TRUE;
3526 /* Create the .got section to hold the global offset table. */
3528 static bfd_boolean
3529 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
3530 bfd_boolean maybe_exclude)
3532 flagword flags;
3533 register asection *s;
3534 struct elf_link_hash_entry *h;
3535 struct bfd_link_hash_entry *bh;
3536 struct mips_got_info *g;
3537 bfd_size_type amt;
3539 /* This function may be called more than once. */
3540 s = mips_elf_got_section (abfd, TRUE);
3541 if (s)
3543 if (! maybe_exclude)
3544 s->flags &= ~SEC_EXCLUDE;
3545 return TRUE;
3548 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3549 | SEC_LINKER_CREATED);
3551 if (maybe_exclude)
3552 flags |= SEC_EXCLUDE;
3554 /* We have to use an alignment of 2**4 here because this is hardcoded
3555 in the function stub generation and in the linker script. */
3556 s = bfd_make_section_with_flags (abfd, ".got", flags);
3557 if (s == NULL
3558 || ! bfd_set_section_alignment (abfd, s, 4))
3559 return FALSE;
3561 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
3562 linker script because we don't want to define the symbol if we
3563 are not creating a global offset table. */
3564 bh = NULL;
3565 if (! (_bfd_generic_link_add_one_symbol
3566 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
3567 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
3568 return FALSE;
3570 h = (struct elf_link_hash_entry *) bh;
3571 h->non_elf = 0;
3572 h->def_regular = 1;
3573 h->type = STT_OBJECT;
3575 if (info->shared
3576 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3577 return FALSE;
3579 amt = sizeof (struct mips_got_info);
3580 g = bfd_alloc (abfd, amt);
3581 if (g == NULL)
3582 return FALSE;
3583 g->global_gotsym = NULL;
3584 g->global_gotno = 0;
3585 g->tls_gotno = 0;
3586 g->local_gotno = MIPS_RESERVED_GOTNO;
3587 g->assigned_gotno = MIPS_RESERVED_GOTNO;
3588 g->bfd2got = NULL;
3589 g->next = NULL;
3590 g->tls_ldm_offset = MINUS_ONE;
3591 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3592 mips_elf_got_entry_eq, NULL);
3593 if (g->got_entries == NULL)
3594 return FALSE;
3595 mips_elf_section_data (s)->u.got_info = g;
3596 mips_elf_section_data (s)->elf.this_hdr.sh_flags
3597 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3599 return TRUE;
3602 /* Calculate the value produced by the RELOCATION (which comes from
3603 the INPUT_BFD). The ADDEND is the addend to use for this
3604 RELOCATION; RELOCATION->R_ADDEND is ignored.
3606 The result of the relocation calculation is stored in VALUEP.
3607 REQUIRE_JALXP indicates whether or not the opcode used with this
3608 relocation must be JALX.
3610 This function returns bfd_reloc_continue if the caller need take no
3611 further action regarding this relocation, bfd_reloc_notsupported if
3612 something goes dramatically wrong, bfd_reloc_overflow if an
3613 overflow occurs, and bfd_reloc_ok to indicate success. */
3615 static bfd_reloc_status_type
3616 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
3617 asection *input_section,
3618 struct bfd_link_info *info,
3619 const Elf_Internal_Rela *relocation,
3620 bfd_vma addend, reloc_howto_type *howto,
3621 Elf_Internal_Sym *local_syms,
3622 asection **local_sections, bfd_vma *valuep,
3623 const char **namep, bfd_boolean *require_jalxp,
3624 bfd_boolean save_addend)
3626 /* The eventual value we will return. */
3627 bfd_vma value;
3628 /* The address of the symbol against which the relocation is
3629 occurring. */
3630 bfd_vma symbol = 0;
3631 /* The final GP value to be used for the relocatable, executable, or
3632 shared object file being produced. */
3633 bfd_vma gp = MINUS_ONE;
3634 /* The place (section offset or address) of the storage unit being
3635 relocated. */
3636 bfd_vma p;
3637 /* The value of GP used to create the relocatable object. */
3638 bfd_vma gp0 = MINUS_ONE;
3639 /* The offset into the global offset table at which the address of
3640 the relocation entry symbol, adjusted by the addend, resides
3641 during execution. */
3642 bfd_vma g = MINUS_ONE;
3643 /* The section in which the symbol referenced by the relocation is
3644 located. */
3645 asection *sec = NULL;
3646 struct mips_elf_link_hash_entry *h = NULL;
3647 /* TRUE if the symbol referred to by this relocation is a local
3648 symbol. */
3649 bfd_boolean local_p, was_local_p;
3650 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
3651 bfd_boolean gp_disp_p = FALSE;
3652 /* TRUE if the symbol referred to by this relocation is
3653 "__gnu_local_gp". */
3654 bfd_boolean gnu_local_gp_p = FALSE;
3655 Elf_Internal_Shdr *symtab_hdr;
3656 size_t extsymoff;
3657 unsigned long r_symndx;
3658 int r_type;
3659 /* TRUE if overflow occurred during the calculation of the
3660 relocation value. */
3661 bfd_boolean overflowed_p;
3662 /* TRUE if this relocation refers to a MIPS16 function. */
3663 bfd_boolean target_is_16_bit_code_p = FALSE;
3665 /* Parse the relocation. */
3666 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3667 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
3668 p = (input_section->output_section->vma
3669 + input_section->output_offset
3670 + relocation->r_offset);
3672 /* Assume that there will be no overflow. */
3673 overflowed_p = FALSE;
3675 /* Figure out whether or not the symbol is local, and get the offset
3676 used in the array of hash table entries. */
3677 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3678 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3679 local_sections, FALSE);
3680 was_local_p = local_p;
3681 if (! elf_bad_symtab (input_bfd))
3682 extsymoff = symtab_hdr->sh_info;
3683 else
3685 /* The symbol table does not follow the rule that local symbols
3686 must come before globals. */
3687 extsymoff = 0;
3690 /* Figure out the value of the symbol. */
3691 if (local_p)
3693 Elf_Internal_Sym *sym;
3695 sym = local_syms + r_symndx;
3696 sec = local_sections[r_symndx];
3698 symbol = sec->output_section->vma + sec->output_offset;
3699 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
3700 || (sec->flags & SEC_MERGE))
3701 symbol += sym->st_value;
3702 if ((sec->flags & SEC_MERGE)
3703 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3705 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
3706 addend -= symbol;
3707 addend += sec->output_section->vma + sec->output_offset;
3710 /* MIPS16 text labels should be treated as odd. */
3711 if (sym->st_other == STO_MIPS16)
3712 ++symbol;
3714 /* Record the name of this symbol, for our caller. */
3715 *namep = bfd_elf_string_from_elf_section (input_bfd,
3716 symtab_hdr->sh_link,
3717 sym->st_name);
3718 if (*namep == '\0')
3719 *namep = bfd_section_name (input_bfd, sec);
3721 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
3723 else
3725 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
3727 /* For global symbols we look up the symbol in the hash-table. */
3728 h = ((struct mips_elf_link_hash_entry *)
3729 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
3730 /* Find the real hash-table entry for this symbol. */
3731 while (h->root.root.type == bfd_link_hash_indirect
3732 || h->root.root.type == bfd_link_hash_warning)
3733 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3735 /* Record the name of this symbol, for our caller. */
3736 *namep = h->root.root.root.string;
3738 /* See if this is the special _gp_disp symbol. Note that such a
3739 symbol must always be a global symbol. */
3740 if (strcmp (*namep, "_gp_disp") == 0
3741 && ! NEWABI_P (input_bfd))
3743 /* Relocations against _gp_disp are permitted only with
3744 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
3745 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16
3746 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
3747 return bfd_reloc_notsupported;
3749 gp_disp_p = TRUE;
3751 /* See if this is the special _gp symbol. Note that such a
3752 symbol must always be a global symbol. */
3753 else if (strcmp (*namep, "__gnu_local_gp") == 0)
3754 gnu_local_gp_p = TRUE;
3757 /* If this symbol is defined, calculate its address. Note that
3758 _gp_disp is a magic symbol, always implicitly defined by the
3759 linker, so it's inappropriate to check to see whether or not
3760 its defined. */
3761 else if ((h->root.root.type == bfd_link_hash_defined
3762 || h->root.root.type == bfd_link_hash_defweak)
3763 && h->root.root.u.def.section)
3765 sec = h->root.root.u.def.section;
3766 if (sec->output_section)
3767 symbol = (h->root.root.u.def.value
3768 + sec->output_section->vma
3769 + sec->output_offset);
3770 else
3771 symbol = h->root.root.u.def.value;
3773 else if (h->root.root.type == bfd_link_hash_undefweak)
3774 /* We allow relocations against undefined weak symbols, giving
3775 it the value zero, so that you can undefined weak functions
3776 and check to see if they exist by looking at their
3777 addresses. */
3778 symbol = 0;
3779 else if (info->unresolved_syms_in_objects == RM_IGNORE
3780 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3781 symbol = 0;
3782 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
3783 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
3785 /* If this is a dynamic link, we should have created a
3786 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
3787 in in _bfd_mips_elf_create_dynamic_sections.
3788 Otherwise, we should define the symbol with a value of 0.
3789 FIXME: It should probably get into the symbol table
3790 somehow as well. */
3791 BFD_ASSERT (! info->shared);
3792 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
3793 symbol = 0;
3795 else
3797 if (! ((*info->callbacks->undefined_symbol)
3798 (info, h->root.root.root.string, input_bfd,
3799 input_section, relocation->r_offset,
3800 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
3801 || ELF_ST_VISIBILITY (h->root.other))))
3802 return bfd_reloc_undefined;
3803 symbol = 0;
3806 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
3809 /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
3810 need to redirect the call to the stub, unless we're already *in*
3811 a stub. */
3812 if (r_type != R_MIPS16_26 && !info->relocatable
3813 && ((h != NULL && h->fn_stub != NULL)
3814 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
3815 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
3816 && !mips_elf_stub_section_p (input_bfd, input_section))
3818 /* This is a 32- or 64-bit call to a 16-bit function. We should
3819 have already noticed that we were going to need the
3820 stub. */
3821 if (local_p)
3822 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
3823 else
3825 BFD_ASSERT (h->need_fn_stub);
3826 sec = h->fn_stub;
3829 symbol = sec->output_section->vma + sec->output_offset;
3831 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
3832 need to redirect the call to the stub. */
3833 else if (r_type == R_MIPS16_26 && !info->relocatable
3834 && h != NULL
3835 && (h->call_stub != NULL || h->call_fp_stub != NULL)
3836 && !target_is_16_bit_code_p)
3838 /* If both call_stub and call_fp_stub are defined, we can figure
3839 out which one to use by seeing which one appears in the input
3840 file. */
3841 if (h->call_stub != NULL && h->call_fp_stub != NULL)
3843 asection *o;
3845 sec = NULL;
3846 for (o = input_bfd->sections; o != NULL; o = o->next)
3848 if (strncmp (bfd_get_section_name (input_bfd, o),
3849 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
3851 sec = h->call_fp_stub;
3852 break;
3855 if (sec == NULL)
3856 sec = h->call_stub;
3858 else if (h->call_stub != NULL)
3859 sec = h->call_stub;
3860 else
3861 sec = h->call_fp_stub;
3863 BFD_ASSERT (sec->size > 0);
3864 symbol = sec->output_section->vma + sec->output_offset;
3867 /* Calls from 16-bit code to 32-bit code and vice versa require the
3868 special jalx instruction. */
3869 *require_jalxp = (!info->relocatable
3870 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
3871 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
3873 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
3874 local_sections, TRUE);
3876 /* If we haven't already determined the GOT offset, or the GP value,
3877 and we're going to need it, get it now. */
3878 switch (r_type)
3880 case R_MIPS_GOT_PAGE:
3881 case R_MIPS_GOT_OFST:
3882 /* We need to decay to GOT_DISP/addend if the symbol doesn't
3883 bind locally. */
3884 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
3885 if (local_p || r_type == R_MIPS_GOT_OFST)
3886 break;
3887 /* Fall through. */
3889 case R_MIPS_CALL16:
3890 case R_MIPS_GOT16:
3891 case R_MIPS_GOT_DISP:
3892 case R_MIPS_GOT_HI16:
3893 case R_MIPS_CALL_HI16:
3894 case R_MIPS_GOT_LO16:
3895 case R_MIPS_CALL_LO16:
3896 case R_MIPS_TLS_GD:
3897 case R_MIPS_TLS_GOTTPREL:
3898 case R_MIPS_TLS_LDM:
3899 /* Find the index into the GOT where this value is located. */
3900 if (r_type == R_MIPS_TLS_LDM)
3902 g = mips_elf_local_got_index (abfd, input_bfd, info, 0, 0, NULL,
3903 r_type);
3904 if (g == MINUS_ONE)
3905 return bfd_reloc_outofrange;
3907 else if (!local_p)
3909 /* GOT_PAGE may take a non-zero addend, that is ignored in a
3910 GOT_PAGE relocation that decays to GOT_DISP because the
3911 symbol turns out to be global. The addend is then added
3912 as GOT_OFST. */
3913 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
3914 g = mips_elf_global_got_index (elf_hash_table (info)->dynobj,
3915 input_bfd,
3916 (struct elf_link_hash_entry *) h,
3917 r_type, info);
3918 if (h->tls_type == GOT_NORMAL
3919 && (! elf_hash_table(info)->dynamic_sections_created
3920 || (info->shared
3921 && (info->symbolic || h->root.dynindx == -1)
3922 && h->root.def_regular)))
3924 /* This is a static link or a -Bsymbolic link. The
3925 symbol is defined locally, or was forced to be local.
3926 We must initialize this entry in the GOT. */
3927 bfd *tmpbfd = elf_hash_table (info)->dynobj;
3928 asection *sgot = mips_elf_got_section (tmpbfd, FALSE);
3929 MIPS_ELF_PUT_WORD (tmpbfd, symbol, sgot->contents + g);
3932 else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
3933 /* There's no need to create a local GOT entry here; the
3934 calculation for a local GOT16 entry does not involve G. */
3935 break;
3936 else
3938 g = mips_elf_local_got_index (abfd, input_bfd,
3939 info, symbol + addend, r_symndx, h,
3940 r_type);
3941 if (g == MINUS_ONE)
3942 return bfd_reloc_outofrange;
3945 /* Convert GOT indices to actual offsets. */
3946 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
3947 abfd, input_bfd, g);
3948 break;
3950 case R_MIPS_HI16:
3951 case R_MIPS_LO16:
3952 case R_MIPS_GPREL16:
3953 case R_MIPS_GPREL32:
3954 case R_MIPS_LITERAL:
3955 case R_MIPS16_HI16:
3956 case R_MIPS16_LO16:
3957 case R_MIPS16_GPREL:
3958 gp0 = _bfd_get_gp_value (input_bfd);
3959 gp = _bfd_get_gp_value (abfd);
3960 if (elf_hash_table (info)->dynobj)
3961 gp += mips_elf_adjust_gp (abfd,
3962 mips_elf_got_info
3963 (elf_hash_table (info)->dynobj, NULL),
3964 input_bfd);
3965 break;
3967 default:
3968 break;
3971 if (gnu_local_gp_p)
3972 symbol = gp;
3974 /* Figure out what kind of relocation is being performed. */
3975 switch (r_type)
3977 case R_MIPS_NONE:
3978 return bfd_reloc_continue;
3980 case R_MIPS_16:
3981 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
3982 overflowed_p = mips_elf_overflow_p (value, 16);
3983 break;
3985 case R_MIPS_32:
3986 case R_MIPS_REL32:
3987 case R_MIPS_64:
3988 if ((info->shared
3989 || (elf_hash_table (info)->dynamic_sections_created
3990 && h != NULL
3991 && h->root.def_dynamic
3992 && !h->root.def_regular))
3993 && r_symndx != 0
3994 && (input_section->flags & SEC_ALLOC) != 0)
3996 /* If we're creating a shared library, or this relocation is
3997 against a symbol in a shared library, then we can't know
3998 where the symbol will end up. So, we create a relocation
3999 record in the output, and leave the job up to the dynamic
4000 linker. */
4001 value = addend;
4002 if (!mips_elf_create_dynamic_relocation (abfd,
4003 info,
4004 relocation,
4006 sec,
4007 symbol,
4008 &value,
4009 input_section))
4010 return bfd_reloc_undefined;
4012 else
4014 if (r_type != R_MIPS_REL32)
4015 value = symbol + addend;
4016 else
4017 value = addend;
4019 value &= howto->dst_mask;
4020 break;
4022 case R_MIPS_PC32:
4023 value = symbol + addend - p;
4024 value &= howto->dst_mask;
4025 break;
4027 case R_MIPS_GNU_REL16_S2:
4028 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4029 overflowed_p = mips_elf_overflow_p (value, 18);
4030 value = (value >> 2) & howto->dst_mask;
4031 break;
4033 case R_MIPS16_26:
4034 /* The calculation for R_MIPS16_26 is just the same as for an
4035 R_MIPS_26. It's only the storage of the relocated field into
4036 the output file that's different. That's handled in
4037 mips_elf_perform_relocation. So, we just fall through to the
4038 R_MIPS_26 case here. */
4039 case R_MIPS_26:
4040 if (local_p)
4041 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4042 else
4044 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4045 if (h->root.root.type != bfd_link_hash_undefweak)
4046 overflowed_p = (value >> 26) != ((p + 4) >> 28);
4048 value &= howto->dst_mask;
4049 break;
4051 case R_MIPS_TLS_DTPREL_HI16:
4052 value = (mips_elf_high (addend + symbol - dtprel_base (info))
4053 & howto->dst_mask);
4054 break;
4056 case R_MIPS_TLS_DTPREL_LO16:
4057 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4058 break;
4060 case R_MIPS_TLS_TPREL_HI16:
4061 value = (mips_elf_high (addend + symbol - tprel_base (info))
4062 & howto->dst_mask);
4063 break;
4065 case R_MIPS_TLS_TPREL_LO16:
4066 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4067 break;
4069 case R_MIPS_HI16:
4070 case R_MIPS16_HI16:
4071 if (!gp_disp_p)
4073 value = mips_elf_high (addend + symbol);
4074 value &= howto->dst_mask;
4076 else
4078 /* For MIPS16 ABI code we generate this sequence
4079 0: li $v0,%hi(_gp_disp)
4080 4: addiupc $v1,%lo(_gp_disp)
4081 8: sll $v0,16
4082 12: addu $v0,$v1
4083 14: move $gp,$v0
4084 So the offsets of hi and lo relocs are the same, but the
4085 $pc is four higher than $t9 would be, so reduce
4086 both reloc addends by 4. */
4087 if (r_type == R_MIPS16_HI16)
4088 value = mips_elf_high (addend + gp - p - 4);
4089 else
4090 value = mips_elf_high (addend + gp - p);
4091 overflowed_p = mips_elf_overflow_p (value, 16);
4093 break;
4095 case R_MIPS_LO16:
4096 case R_MIPS16_LO16:
4097 if (!gp_disp_p)
4098 value = (symbol + addend) & howto->dst_mask;
4099 else
4101 /* See the comment for R_MIPS16_HI16 above for the reason
4102 for this conditional. */
4103 if (r_type == R_MIPS16_LO16)
4104 value = addend + gp - p;
4105 else
4106 value = addend + gp - p + 4;
4107 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4108 for overflow. But, on, say, IRIX5, relocations against
4109 _gp_disp are normally generated from the .cpload
4110 pseudo-op. It generates code that normally looks like
4111 this:
4113 lui $gp,%hi(_gp_disp)
4114 addiu $gp,$gp,%lo(_gp_disp)
4115 addu $gp,$gp,$t9
4117 Here $t9 holds the address of the function being called,
4118 as required by the MIPS ELF ABI. The R_MIPS_LO16
4119 relocation can easily overflow in this situation, but the
4120 R_MIPS_HI16 relocation will handle the overflow.
4121 Therefore, we consider this a bug in the MIPS ABI, and do
4122 not check for overflow here. */
4124 break;
4126 case R_MIPS_LITERAL:
4127 /* Because we don't merge literal sections, we can handle this
4128 just like R_MIPS_GPREL16. In the long run, we should merge
4129 shared literals, and then we will need to additional work
4130 here. */
4132 /* Fall through. */
4134 case R_MIPS16_GPREL:
4135 /* The R_MIPS16_GPREL performs the same calculation as
4136 R_MIPS_GPREL16, but stores the relocated bits in a different
4137 order. We don't need to do anything special here; the
4138 differences are handled in mips_elf_perform_relocation. */
4139 case R_MIPS_GPREL16:
4140 /* Only sign-extend the addend if it was extracted from the
4141 instruction. If the addend was separate, leave it alone,
4142 otherwise we may lose significant bits. */
4143 if (howto->partial_inplace)
4144 addend = _bfd_mips_elf_sign_extend (addend, 16);
4145 value = symbol + addend - gp;
4146 /* If the symbol was local, any earlier relocatable links will
4147 have adjusted its addend with the gp offset, so compensate
4148 for that now. Don't do it for symbols forced local in this
4149 link, though, since they won't have had the gp offset applied
4150 to them before. */
4151 if (was_local_p)
4152 value += gp0;
4153 overflowed_p = mips_elf_overflow_p (value, 16);
4154 break;
4156 case R_MIPS_GOT16:
4157 case R_MIPS_CALL16:
4158 if (local_p)
4160 bfd_boolean forced;
4162 /* The special case is when the symbol is forced to be local. We
4163 need the full address in the GOT since no R_MIPS_LO16 relocation
4164 follows. */
4165 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4166 local_sections, FALSE);
4167 value = mips_elf_got16_entry (abfd, input_bfd, info,
4168 symbol + addend, forced);
4169 if (value == MINUS_ONE)
4170 return bfd_reloc_outofrange;
4171 value
4172 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
4173 abfd, input_bfd, value);
4174 overflowed_p = mips_elf_overflow_p (value, 16);
4175 break;
4178 /* Fall through. */
4180 case R_MIPS_TLS_GD:
4181 case R_MIPS_TLS_GOTTPREL:
4182 case R_MIPS_TLS_LDM:
4183 case R_MIPS_GOT_DISP:
4184 got_disp:
4185 value = g;
4186 overflowed_p = mips_elf_overflow_p (value, 16);
4187 break;
4189 case R_MIPS_GPREL32:
4190 value = (addend + symbol + gp0 - gp);
4191 if (!save_addend)
4192 value &= howto->dst_mask;
4193 break;
4195 case R_MIPS_PC16:
4196 value = _bfd_mips_elf_sign_extend (addend, 16) + symbol - p;
4197 overflowed_p = mips_elf_overflow_p (value, 16);
4198 break;
4200 case R_MIPS_GOT_HI16:
4201 case R_MIPS_CALL_HI16:
4202 /* We're allowed to handle these two relocations identically.
4203 The dynamic linker is allowed to handle the CALL relocations
4204 differently by creating a lazy evaluation stub. */
4205 value = g;
4206 value = mips_elf_high (value);
4207 value &= howto->dst_mask;
4208 break;
4210 case R_MIPS_GOT_LO16:
4211 case R_MIPS_CALL_LO16:
4212 value = g & howto->dst_mask;
4213 break;
4215 case R_MIPS_GOT_PAGE:
4216 /* GOT_PAGE relocations that reference non-local symbols decay
4217 to GOT_DISP. The corresponding GOT_OFST relocation decays to
4218 0. */
4219 if (! local_p)
4220 goto got_disp;
4221 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4222 if (value == MINUS_ONE)
4223 return bfd_reloc_outofrange;
4224 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
4225 abfd, input_bfd, value);
4226 overflowed_p = mips_elf_overflow_p (value, 16);
4227 break;
4229 case R_MIPS_GOT_OFST:
4230 if (local_p)
4231 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4232 else
4233 value = addend;
4234 overflowed_p = mips_elf_overflow_p (value, 16);
4235 break;
4237 case R_MIPS_SUB:
4238 value = symbol - addend;
4239 value &= howto->dst_mask;
4240 break;
4242 case R_MIPS_HIGHER:
4243 value = mips_elf_higher (addend + symbol);
4244 value &= howto->dst_mask;
4245 break;
4247 case R_MIPS_HIGHEST:
4248 value = mips_elf_highest (addend + symbol);
4249 value &= howto->dst_mask;
4250 break;
4252 case R_MIPS_SCN_DISP:
4253 value = symbol + addend - sec->output_offset;
4254 value &= howto->dst_mask;
4255 break;
4257 case R_MIPS_JALR:
4258 /* This relocation is only a hint. In some cases, we optimize
4259 it into a bal instruction. But we don't try to optimize
4260 branches to the PLT; that will wind up wasting time. */
4261 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4262 return bfd_reloc_continue;
4263 value = symbol + addend;
4264 break;
4266 case R_MIPS_PJUMP:
4267 case R_MIPS_GNU_VTINHERIT:
4268 case R_MIPS_GNU_VTENTRY:
4269 /* We don't do anything with these at present. */
4270 return bfd_reloc_continue;
4272 default:
4273 /* An unrecognized relocation type. */
4274 return bfd_reloc_notsupported;
4277 /* Store the VALUE for our caller. */
4278 *valuep = value;
4279 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4282 /* Obtain the field relocated by RELOCATION. */
4284 static bfd_vma
4285 mips_elf_obtain_contents (reloc_howto_type *howto,
4286 const Elf_Internal_Rela *relocation,
4287 bfd *input_bfd, bfd_byte *contents)
4289 bfd_vma x;
4290 bfd_byte *location = contents + relocation->r_offset;
4292 /* Obtain the bytes. */
4293 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4295 return x;
4298 /* It has been determined that the result of the RELOCATION is the
4299 VALUE. Use HOWTO to place VALUE into the output file at the
4300 appropriate position. The SECTION is the section to which the
4301 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
4302 for the relocation must be either JAL or JALX, and it is
4303 unconditionally converted to JALX.
4305 Returns FALSE if anything goes wrong. */
4307 static bfd_boolean
4308 mips_elf_perform_relocation (struct bfd_link_info *info,
4309 reloc_howto_type *howto,
4310 const Elf_Internal_Rela *relocation,
4311 bfd_vma value, bfd *input_bfd,
4312 asection *input_section, bfd_byte *contents,
4313 bfd_boolean require_jalx)
4315 bfd_vma x;
4316 bfd_byte *location;
4317 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4319 /* Figure out where the relocation is occurring. */
4320 location = contents + relocation->r_offset;
4322 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4324 /* Obtain the current value. */
4325 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4327 /* Clear the field we are setting. */
4328 x &= ~howto->dst_mask;
4330 /* Set the field. */
4331 x |= (value & howto->dst_mask);
4333 /* If required, turn JAL into JALX. */
4334 if (require_jalx)
4336 bfd_boolean ok;
4337 bfd_vma opcode = x >> 26;
4338 bfd_vma jalx_opcode;
4340 /* Check to see if the opcode is already JAL or JALX. */
4341 if (r_type == R_MIPS16_26)
4343 ok = ((opcode == 0x6) || (opcode == 0x7));
4344 jalx_opcode = 0x7;
4346 else
4348 ok = ((opcode == 0x3) || (opcode == 0x1d));
4349 jalx_opcode = 0x1d;
4352 /* If the opcode is not JAL or JALX, there's a problem. */
4353 if (!ok)
4355 (*_bfd_error_handler)
4356 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
4357 input_bfd,
4358 input_section,
4359 (unsigned long) relocation->r_offset);
4360 bfd_set_error (bfd_error_bad_value);
4361 return FALSE;
4364 /* Make this the JALX opcode. */
4365 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
4368 /* On the RM9000, bal is faster than jal, because bal uses branch
4369 prediction hardware. If we are linking for the RM9000, and we
4370 see jal, and bal fits, use it instead. Note that this
4371 transformation should be safe for all architectures. */
4372 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
4373 && !info->relocatable
4374 && !require_jalx
4375 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
4376 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
4378 bfd_vma addr;
4379 bfd_vma dest;
4380 bfd_signed_vma off;
4382 addr = (input_section->output_section->vma
4383 + input_section->output_offset
4384 + relocation->r_offset
4385 + 4);
4386 if (r_type == R_MIPS_26)
4387 dest = (value << 2) | ((addr >> 28) << 28);
4388 else
4389 dest = value;
4390 off = dest - addr;
4391 if (off <= 0x1ffff && off >= -0x20000)
4392 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
4395 /* Put the value into the output. */
4396 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
4398 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
4399 location);
4401 return TRUE;
4404 /* Returns TRUE if SECTION is a MIPS16 stub section. */
4406 static bfd_boolean
4407 mips_elf_stub_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
4409 const char *name = bfd_get_section_name (abfd, section);
4411 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
4412 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4413 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
4416 /* Add room for N relocations to the .rel.dyn section in ABFD. */
4418 static void
4419 mips_elf_allocate_dynamic_relocations (bfd *abfd, unsigned int n)
4421 asection *s;
4423 s = mips_elf_rel_dyn_section (abfd, FALSE);
4424 BFD_ASSERT (s != NULL);
4426 if (s->size == 0)
4428 /* Make room for a null element. */
4429 s->size += MIPS_ELF_REL_SIZE (abfd);
4430 ++s->reloc_count;
4432 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4435 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
4436 is the original relocation, which is now being transformed into a
4437 dynamic relocation. The ADDENDP is adjusted if necessary; the
4438 caller should store the result in place of the original addend. */
4440 static bfd_boolean
4441 mips_elf_create_dynamic_relocation (bfd *output_bfd,
4442 struct bfd_link_info *info,
4443 const Elf_Internal_Rela *rel,
4444 struct mips_elf_link_hash_entry *h,
4445 asection *sec, bfd_vma symbol,
4446 bfd_vma *addendp, asection *input_section)
4448 Elf_Internal_Rela outrel[3];
4449 asection *sreloc;
4450 bfd *dynobj;
4451 int r_type;
4452 long indx;
4453 bfd_boolean defined_p;
4455 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
4456 dynobj = elf_hash_table (info)->dynobj;
4457 sreloc = mips_elf_rel_dyn_section (dynobj, FALSE);
4458 BFD_ASSERT (sreloc != NULL);
4459 BFD_ASSERT (sreloc->contents != NULL);
4460 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
4461 < sreloc->size);
4463 outrel[0].r_offset =
4464 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
4465 outrel[1].r_offset =
4466 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
4467 outrel[2].r_offset =
4468 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
4470 if (outrel[0].r_offset == MINUS_ONE)
4471 /* The relocation field has been deleted. */
4472 return TRUE;
4474 if (outrel[0].r_offset == MINUS_TWO)
4476 /* The relocation field has been converted into a relative value of
4477 some sort. Functions like _bfd_elf_write_section_eh_frame expect
4478 the field to be fully relocated, so add in the symbol's value. */
4479 *addendp += symbol;
4480 return TRUE;
4483 /* We must now calculate the dynamic symbol table index to use
4484 in the relocation. */
4485 if (h != NULL
4486 && (! info->symbolic || !h->root.def_regular)
4487 /* h->root.dynindx may be -1 if this symbol was marked to
4488 become local. */
4489 && h->root.dynindx != -1)
4491 indx = h->root.dynindx;
4492 if (SGI_COMPAT (output_bfd))
4493 defined_p = h->root.def_regular;
4494 else
4495 /* ??? glibc's ld.so just adds the final GOT entry to the
4496 relocation field. It therefore treats relocs against
4497 defined symbols in the same way as relocs against
4498 undefined symbols. */
4499 defined_p = FALSE;
4501 else
4503 if (sec != NULL && bfd_is_abs_section (sec))
4504 indx = 0;
4505 else if (sec == NULL || sec->owner == NULL)
4507 bfd_set_error (bfd_error_bad_value);
4508 return FALSE;
4510 else
4512 indx = elf_section_data (sec->output_section)->dynindx;
4513 if (indx == 0)
4514 abort ();
4517 /* Instead of generating a relocation using the section
4518 symbol, we may as well make it a fully relative
4519 relocation. We want to avoid generating relocations to
4520 local symbols because we used to generate them
4521 incorrectly, without adding the original symbol value,
4522 which is mandated by the ABI for section symbols. In
4523 order to give dynamic loaders and applications time to
4524 phase out the incorrect use, we refrain from emitting
4525 section-relative relocations. It's not like they're
4526 useful, after all. This should be a bit more efficient
4527 as well. */
4528 /* ??? Although this behavior is compatible with glibc's ld.so,
4529 the ABI says that relocations against STN_UNDEF should have
4530 a symbol value of 0. Irix rld honors this, so relocations
4531 against STN_UNDEF have no effect. */
4532 if (!SGI_COMPAT (output_bfd))
4533 indx = 0;
4534 defined_p = TRUE;
4537 /* If the relocation was previously an absolute relocation and
4538 this symbol will not be referred to by the relocation, we must
4539 adjust it by the value we give it in the dynamic symbol table.
4540 Otherwise leave the job up to the dynamic linker. */
4541 if (defined_p && r_type != R_MIPS_REL32)
4542 *addendp += symbol;
4544 /* The relocation is always an REL32 relocation because we don't
4545 know where the shared library will wind up at load-time. */
4546 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
4547 R_MIPS_REL32);
4548 /* For strict adherence to the ABI specification, we should
4549 generate a R_MIPS_64 relocation record by itself before the
4550 _REL32/_64 record as well, such that the addend is read in as
4551 a 64-bit value (REL32 is a 32-bit relocation, after all).
4552 However, since none of the existing ELF64 MIPS dynamic
4553 loaders seems to care, we don't waste space with these
4554 artificial relocations. If this turns out to not be true,
4555 mips_elf_allocate_dynamic_relocation() should be tweaked so
4556 as to make room for a pair of dynamic relocations per
4557 invocation if ABI_64_P, and here we should generate an
4558 additional relocation record with R_MIPS_64 by itself for a
4559 NULL symbol before this relocation record. */
4560 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
4561 ABI_64_P (output_bfd)
4562 ? R_MIPS_64
4563 : R_MIPS_NONE);
4564 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
4566 /* Adjust the output offset of the relocation to reference the
4567 correct location in the output file. */
4568 outrel[0].r_offset += (input_section->output_section->vma
4569 + input_section->output_offset);
4570 outrel[1].r_offset += (input_section->output_section->vma
4571 + input_section->output_offset);
4572 outrel[2].r_offset += (input_section->output_section->vma
4573 + input_section->output_offset);
4575 /* Put the relocation back out. We have to use the special
4576 relocation outputter in the 64-bit case since the 64-bit
4577 relocation format is non-standard. */
4578 if (ABI_64_P (output_bfd))
4580 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
4581 (output_bfd, &outrel[0],
4582 (sreloc->contents
4583 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
4585 else
4586 bfd_elf32_swap_reloc_out
4587 (output_bfd, &outrel[0],
4588 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
4590 /* We've now added another relocation. */
4591 ++sreloc->reloc_count;
4593 /* Make sure the output section is writable. The dynamic linker
4594 will be writing to it. */
4595 elf_section_data (input_section->output_section)->this_hdr.sh_flags
4596 |= SHF_WRITE;
4598 /* On IRIX5, make an entry of compact relocation info. */
4599 if (IRIX_COMPAT (output_bfd) == ict_irix5)
4601 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4602 bfd_byte *cr;
4604 if (scpt)
4606 Elf32_crinfo cptrel;
4608 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4609 cptrel.vaddr = (rel->r_offset
4610 + input_section->output_section->vma
4611 + input_section->output_offset);
4612 if (r_type == R_MIPS_REL32)
4613 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4614 else
4615 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4616 mips_elf_set_cr_dist2to (cptrel, 0);
4617 cptrel.konst = *addendp;
4619 cr = (scpt->contents
4620 + sizeof (Elf32_External_compact_rel));
4621 mips_elf_set_cr_relvaddr (cptrel, 0);
4622 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4623 ((Elf32_External_crinfo *) cr
4624 + scpt->reloc_count));
4625 ++scpt->reloc_count;
4629 return TRUE;
4632 /* Return the MACH for a MIPS e_flags value. */
4634 unsigned long
4635 _bfd_elf_mips_mach (flagword flags)
4637 switch (flags & EF_MIPS_MACH)
4639 case E_MIPS_MACH_3900:
4640 return bfd_mach_mips3900;
4642 case E_MIPS_MACH_4010:
4643 return bfd_mach_mips4010;
4645 case E_MIPS_MACH_4100:
4646 return bfd_mach_mips4100;
4648 case E_MIPS_MACH_4111:
4649 return bfd_mach_mips4111;
4651 case E_MIPS_MACH_4120:
4652 return bfd_mach_mips4120;
4654 case E_MIPS_MACH_4650:
4655 return bfd_mach_mips4650;
4657 case E_MIPS_MACH_5400:
4658 return bfd_mach_mips5400;
4660 case E_MIPS_MACH_5500:
4661 return bfd_mach_mips5500;
4663 case E_MIPS_MACH_9000:
4664 return bfd_mach_mips9000;
4666 case E_MIPS_MACH_SB1:
4667 return bfd_mach_mips_sb1;
4669 default:
4670 switch (flags & EF_MIPS_ARCH)
4672 default:
4673 case E_MIPS_ARCH_1:
4674 return bfd_mach_mips3000;
4675 break;
4677 case E_MIPS_ARCH_2:
4678 return bfd_mach_mips6000;
4679 break;
4681 case E_MIPS_ARCH_3:
4682 return bfd_mach_mips4000;
4683 break;
4685 case E_MIPS_ARCH_4:
4686 return bfd_mach_mips8000;
4687 break;
4689 case E_MIPS_ARCH_5:
4690 return bfd_mach_mips5;
4691 break;
4693 case E_MIPS_ARCH_32:
4694 return bfd_mach_mipsisa32;
4695 break;
4697 case E_MIPS_ARCH_64:
4698 return bfd_mach_mipsisa64;
4699 break;
4701 case E_MIPS_ARCH_32R2:
4702 return bfd_mach_mipsisa32r2;
4703 break;
4705 case E_MIPS_ARCH_64R2:
4706 return bfd_mach_mipsisa64r2;
4707 break;
4711 return 0;
4714 /* Return printable name for ABI. */
4716 static INLINE char *
4717 elf_mips_abi_name (bfd *abfd)
4719 flagword flags;
4721 flags = elf_elfheader (abfd)->e_flags;
4722 switch (flags & EF_MIPS_ABI)
4724 case 0:
4725 if (ABI_N32_P (abfd))
4726 return "N32";
4727 else if (ABI_64_P (abfd))
4728 return "64";
4729 else
4730 return "none";
4731 case E_MIPS_ABI_O32:
4732 return "O32";
4733 case E_MIPS_ABI_O64:
4734 return "O64";
4735 case E_MIPS_ABI_EABI32:
4736 return "EABI32";
4737 case E_MIPS_ABI_EABI64:
4738 return "EABI64";
4739 default:
4740 return "unknown abi";
4744 /* MIPS ELF uses two common sections. One is the usual one, and the
4745 other is for small objects. All the small objects are kept
4746 together, and then referenced via the gp pointer, which yields
4747 faster assembler code. This is what we use for the small common
4748 section. This approach is copied from ecoff.c. */
4749 static asection mips_elf_scom_section;
4750 static asymbol mips_elf_scom_symbol;
4751 static asymbol *mips_elf_scom_symbol_ptr;
4753 /* MIPS ELF also uses an acommon section, which represents an
4754 allocated common symbol which may be overridden by a
4755 definition in a shared library. */
4756 static asection mips_elf_acom_section;
4757 static asymbol mips_elf_acom_symbol;
4758 static asymbol *mips_elf_acom_symbol_ptr;
4760 /* Handle the special MIPS section numbers that a symbol may use.
4761 This is used for both the 32-bit and the 64-bit ABI. */
4763 void
4764 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
4766 elf_symbol_type *elfsym;
4768 elfsym = (elf_symbol_type *) asym;
4769 switch (elfsym->internal_elf_sym.st_shndx)
4771 case SHN_MIPS_ACOMMON:
4772 /* This section is used in a dynamically linked executable file.
4773 It is an allocated common section. The dynamic linker can
4774 either resolve these symbols to something in a shared
4775 library, or it can just leave them here. For our purposes,
4776 we can consider these symbols to be in a new section. */
4777 if (mips_elf_acom_section.name == NULL)
4779 /* Initialize the acommon section. */
4780 mips_elf_acom_section.name = ".acommon";
4781 mips_elf_acom_section.flags = SEC_ALLOC;
4782 mips_elf_acom_section.output_section = &mips_elf_acom_section;
4783 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
4784 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
4785 mips_elf_acom_symbol.name = ".acommon";
4786 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
4787 mips_elf_acom_symbol.section = &mips_elf_acom_section;
4788 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
4790 asym->section = &mips_elf_acom_section;
4791 break;
4793 case SHN_COMMON:
4794 /* Common symbols less than the GP size are automatically
4795 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
4796 if (asym->value > elf_gp_size (abfd)
4797 || IRIX_COMPAT (abfd) == ict_irix6)
4798 break;
4799 /* Fall through. */
4800 case SHN_MIPS_SCOMMON:
4801 if (mips_elf_scom_section.name == NULL)
4803 /* Initialize the small common section. */
4804 mips_elf_scom_section.name = ".scommon";
4805 mips_elf_scom_section.flags = SEC_IS_COMMON;
4806 mips_elf_scom_section.output_section = &mips_elf_scom_section;
4807 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
4808 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
4809 mips_elf_scom_symbol.name = ".scommon";
4810 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
4811 mips_elf_scom_symbol.section = &mips_elf_scom_section;
4812 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
4814 asym->section = &mips_elf_scom_section;
4815 asym->value = elfsym->internal_elf_sym.st_size;
4816 break;
4818 case SHN_MIPS_SUNDEFINED:
4819 asym->section = bfd_und_section_ptr;
4820 break;
4822 case SHN_MIPS_TEXT:
4824 asection *section = bfd_get_section_by_name (abfd, ".text");
4826 BFD_ASSERT (SGI_COMPAT (abfd));
4827 if (section != NULL)
4829 asym->section = section;
4830 /* MIPS_TEXT is a bit special, the address is not an offset
4831 to the base of the .text section. So substract the section
4832 base address to make it an offset. */
4833 asym->value -= section->vma;
4836 break;
4838 case SHN_MIPS_DATA:
4840 asection *section = bfd_get_section_by_name (abfd, ".data");
4842 BFD_ASSERT (SGI_COMPAT (abfd));
4843 if (section != NULL)
4845 asym->section = section;
4846 /* MIPS_DATA is a bit special, the address is not an offset
4847 to the base of the .data section. So substract the section
4848 base address to make it an offset. */
4849 asym->value -= section->vma;
4852 break;
4856 /* Implement elf_backend_eh_frame_address_size. This differs from
4857 the default in the way it handles EABI64.
4859 EABI64 was originally specified as an LP64 ABI, and that is what
4860 -mabi=eabi normally gives on a 64-bit target. However, gcc has
4861 historically accepted the combination of -mabi=eabi and -mlong32,
4862 and this ILP32 variation has become semi-official over time.
4863 Both forms use elf32 and have pointer-sized FDE addresses.
4865 If an EABI object was generated by GCC 4.0 or above, it will have
4866 an empty .gcc_compiled_longXX section, where XX is the size of longs
4867 in bits. Unfortunately, ILP32 objects generated by earlier compilers
4868 have no special marking to distinguish them from LP64 objects.
4870 We don't want users of the official LP64 ABI to be punished for the
4871 existence of the ILP32 variant, but at the same time, we don't want
4872 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
4873 We therefore take the following approach:
4875 - If ABFD contains a .gcc_compiled_longXX section, use it to
4876 determine the pointer size.
4878 - Otherwise check the type of the first relocation. Assume that
4879 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
4881 - Otherwise punt.
4883 The second check is enough to detect LP64 objects generated by pre-4.0
4884 compilers because, in the kind of output generated by those compilers,
4885 the first relocation will be associated with either a CIE personality
4886 routine or an FDE start address. Furthermore, the compilers never
4887 used a special (non-pointer) encoding for this ABI.
4889 Checking the relocation type should also be safe because there is no
4890 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
4891 did so. */
4893 unsigned int
4894 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
4896 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
4897 return 8;
4898 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
4900 bfd_boolean long32_p, long64_p;
4902 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
4903 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
4904 if (long32_p && long64_p)
4905 return 0;
4906 if (long32_p)
4907 return 4;
4908 if (long64_p)
4909 return 8;
4911 if (sec->reloc_count > 0
4912 && elf_section_data (sec)->relocs != NULL
4913 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
4914 == R_MIPS_64))
4915 return 8;
4917 return 0;
4919 return 4;
4922 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
4923 relocations against two unnamed section symbols to resolve to the
4924 same address. For example, if we have code like:
4926 lw $4,%got_disp(.data)($gp)
4927 lw $25,%got_disp(.text)($gp)
4928 jalr $25
4930 then the linker will resolve both relocations to .data and the program
4931 will jump there rather than to .text.
4933 We can work around this problem by giving names to local section symbols.
4934 This is also what the MIPSpro tools do. */
4936 bfd_boolean
4937 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
4939 return SGI_COMPAT (abfd);
4942 /* Work over a section just before writing it out. This routine is
4943 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
4944 sections that need the SHF_MIPS_GPREL flag by name; there has to be
4945 a better way. */
4947 bfd_boolean
4948 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
4950 if (hdr->sh_type == SHT_MIPS_REGINFO
4951 && hdr->sh_size > 0)
4953 bfd_byte buf[4];
4955 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
4956 BFD_ASSERT (hdr->contents == NULL);
4958 if (bfd_seek (abfd,
4959 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
4960 SEEK_SET) != 0)
4961 return FALSE;
4962 H_PUT_32 (abfd, elf_gp (abfd), buf);
4963 if (bfd_bwrite (buf, 4, abfd) != 4)
4964 return FALSE;
4967 if (hdr->sh_type == SHT_MIPS_OPTIONS
4968 && hdr->bfd_section != NULL
4969 && mips_elf_section_data (hdr->bfd_section) != NULL
4970 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
4972 bfd_byte *contents, *l, *lend;
4974 /* We stored the section contents in the tdata field in the
4975 set_section_contents routine. We save the section contents
4976 so that we don't have to read them again.
4977 At this point we know that elf_gp is set, so we can look
4978 through the section contents to see if there is an
4979 ODK_REGINFO structure. */
4981 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
4982 l = contents;
4983 lend = contents + hdr->sh_size;
4984 while (l + sizeof (Elf_External_Options) <= lend)
4986 Elf_Internal_Options intopt;
4988 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
4989 &intopt);
4990 if (intopt.size < sizeof (Elf_External_Options))
4992 (*_bfd_error_handler)
4993 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
4994 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
4995 break;
4997 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
4999 bfd_byte buf[8];
5001 if (bfd_seek (abfd,
5002 (hdr->sh_offset
5003 + (l - contents)
5004 + sizeof (Elf_External_Options)
5005 + (sizeof (Elf64_External_RegInfo) - 8)),
5006 SEEK_SET) != 0)
5007 return FALSE;
5008 H_PUT_64 (abfd, elf_gp (abfd), buf);
5009 if (bfd_bwrite (buf, 8, abfd) != 8)
5010 return FALSE;
5012 else if (intopt.kind == ODK_REGINFO)
5014 bfd_byte buf[4];
5016 if (bfd_seek (abfd,
5017 (hdr->sh_offset
5018 + (l - contents)
5019 + sizeof (Elf_External_Options)
5020 + (sizeof (Elf32_External_RegInfo) - 4)),
5021 SEEK_SET) != 0)
5022 return FALSE;
5023 H_PUT_32 (abfd, elf_gp (abfd), buf);
5024 if (bfd_bwrite (buf, 4, abfd) != 4)
5025 return FALSE;
5027 l += intopt.size;
5031 if (hdr->bfd_section != NULL)
5033 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5035 if (strcmp (name, ".sdata") == 0
5036 || strcmp (name, ".lit8") == 0
5037 || strcmp (name, ".lit4") == 0)
5039 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5040 hdr->sh_type = SHT_PROGBITS;
5042 else if (strcmp (name, ".sbss") == 0)
5044 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5045 hdr->sh_type = SHT_NOBITS;
5047 else if (strcmp (name, ".srdata") == 0)
5049 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5050 hdr->sh_type = SHT_PROGBITS;
5052 else if (strcmp (name, ".compact_rel") == 0)
5054 hdr->sh_flags = 0;
5055 hdr->sh_type = SHT_PROGBITS;
5057 else if (strcmp (name, ".rtproc") == 0)
5059 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5061 unsigned int adjust;
5063 adjust = hdr->sh_size % hdr->sh_addralign;
5064 if (adjust != 0)
5065 hdr->sh_size += hdr->sh_addralign - adjust;
5070 return TRUE;
5073 /* Handle a MIPS specific section when reading an object file. This
5074 is called when elfcode.h finds a section with an unknown type.
5075 This routine supports both the 32-bit and 64-bit ELF ABI.
5077 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5078 how to. */
5080 bfd_boolean
5081 _bfd_mips_elf_section_from_shdr (bfd *abfd,
5082 Elf_Internal_Shdr *hdr,
5083 const char *name,
5084 int shindex)
5086 flagword flags = 0;
5088 /* There ought to be a place to keep ELF backend specific flags, but
5089 at the moment there isn't one. We just keep track of the
5090 sections by their name, instead. Fortunately, the ABI gives
5091 suggested names for all the MIPS specific sections, so we will
5092 probably get away with this. */
5093 switch (hdr->sh_type)
5095 case SHT_MIPS_LIBLIST:
5096 if (strcmp (name, ".liblist") != 0)
5097 return FALSE;
5098 break;
5099 case SHT_MIPS_MSYM:
5100 if (strcmp (name, ".msym") != 0)
5101 return FALSE;
5102 break;
5103 case SHT_MIPS_CONFLICT:
5104 if (strcmp (name, ".conflict") != 0)
5105 return FALSE;
5106 break;
5107 case SHT_MIPS_GPTAB:
5108 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
5109 return FALSE;
5110 break;
5111 case SHT_MIPS_UCODE:
5112 if (strcmp (name, ".ucode") != 0)
5113 return FALSE;
5114 break;
5115 case SHT_MIPS_DEBUG:
5116 if (strcmp (name, ".mdebug") != 0)
5117 return FALSE;
5118 flags = SEC_DEBUGGING;
5119 break;
5120 case SHT_MIPS_REGINFO:
5121 if (strcmp (name, ".reginfo") != 0
5122 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5123 return FALSE;
5124 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5125 break;
5126 case SHT_MIPS_IFACE:
5127 if (strcmp (name, ".MIPS.interfaces") != 0)
5128 return FALSE;
5129 break;
5130 case SHT_MIPS_CONTENT:
5131 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
5132 return FALSE;
5133 break;
5134 case SHT_MIPS_OPTIONS:
5135 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5136 return FALSE;
5137 break;
5138 case SHT_MIPS_DWARF:
5139 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
5140 return FALSE;
5141 break;
5142 case SHT_MIPS_SYMBOL_LIB:
5143 if (strcmp (name, ".MIPS.symlib") != 0)
5144 return FALSE;
5145 break;
5146 case SHT_MIPS_EVENTS:
5147 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
5148 && strncmp (name, ".MIPS.post_rel",
5149 sizeof ".MIPS.post_rel" - 1) != 0)
5150 return FALSE;
5151 break;
5152 default:
5153 break;
5156 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5157 return FALSE;
5159 if (flags)
5161 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5162 (bfd_get_section_flags (abfd,
5163 hdr->bfd_section)
5164 | flags)))
5165 return FALSE;
5168 /* FIXME: We should record sh_info for a .gptab section. */
5170 /* For a .reginfo section, set the gp value in the tdata information
5171 from the contents of this section. We need the gp value while
5172 processing relocs, so we just get it now. The .reginfo section
5173 is not used in the 64-bit MIPS ELF ABI. */
5174 if (hdr->sh_type == SHT_MIPS_REGINFO)
5176 Elf32_External_RegInfo ext;
5177 Elf32_RegInfo s;
5179 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5180 &ext, 0, sizeof ext))
5181 return FALSE;
5182 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5183 elf_gp (abfd) = s.ri_gp_value;
5186 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5187 set the gp value based on what we find. We may see both
5188 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5189 they should agree. */
5190 if (hdr->sh_type == SHT_MIPS_OPTIONS)
5192 bfd_byte *contents, *l, *lend;
5194 contents = bfd_malloc (hdr->sh_size);
5195 if (contents == NULL)
5196 return FALSE;
5197 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5198 0, hdr->sh_size))
5200 free (contents);
5201 return FALSE;
5203 l = contents;
5204 lend = contents + hdr->sh_size;
5205 while (l + sizeof (Elf_External_Options) <= lend)
5207 Elf_Internal_Options intopt;
5209 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5210 &intopt);
5211 if (intopt.size < sizeof (Elf_External_Options))
5213 (*_bfd_error_handler)
5214 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5215 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5216 break;
5218 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5220 Elf64_Internal_RegInfo intreg;
5222 bfd_mips_elf64_swap_reginfo_in
5223 (abfd,
5224 ((Elf64_External_RegInfo *)
5225 (l + sizeof (Elf_External_Options))),
5226 &intreg);
5227 elf_gp (abfd) = intreg.ri_gp_value;
5229 else if (intopt.kind == ODK_REGINFO)
5231 Elf32_RegInfo intreg;
5233 bfd_mips_elf32_swap_reginfo_in
5234 (abfd,
5235 ((Elf32_External_RegInfo *)
5236 (l + sizeof (Elf_External_Options))),
5237 &intreg);
5238 elf_gp (abfd) = intreg.ri_gp_value;
5240 l += intopt.size;
5242 free (contents);
5245 return TRUE;
5248 /* Set the correct type for a MIPS ELF section. We do this by the
5249 section name, which is a hack, but ought to work. This routine is
5250 used by both the 32-bit and the 64-bit ABI. */
5252 bfd_boolean
5253 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
5255 register const char *name;
5256 unsigned int sh_type;
5258 name = bfd_get_section_name (abfd, sec);
5259 sh_type = hdr->sh_type;
5261 if (strcmp (name, ".liblist") == 0)
5263 hdr->sh_type = SHT_MIPS_LIBLIST;
5264 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
5265 /* The sh_link field is set in final_write_processing. */
5267 else if (strcmp (name, ".conflict") == 0)
5268 hdr->sh_type = SHT_MIPS_CONFLICT;
5269 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
5271 hdr->sh_type = SHT_MIPS_GPTAB;
5272 hdr->sh_entsize = sizeof (Elf32_External_gptab);
5273 /* The sh_info field is set in final_write_processing. */
5275 else if (strcmp (name, ".ucode") == 0)
5276 hdr->sh_type = SHT_MIPS_UCODE;
5277 else if (strcmp (name, ".mdebug") == 0)
5279 hdr->sh_type = SHT_MIPS_DEBUG;
5280 /* In a shared object on IRIX 5.3, the .mdebug section has an
5281 entsize of 0. FIXME: Does this matter? */
5282 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5283 hdr->sh_entsize = 0;
5284 else
5285 hdr->sh_entsize = 1;
5287 else if (strcmp (name, ".reginfo") == 0)
5289 hdr->sh_type = SHT_MIPS_REGINFO;
5290 /* In a shared object on IRIX 5.3, the .reginfo section has an
5291 entsize of 0x18. FIXME: Does this matter? */
5292 if (SGI_COMPAT (abfd))
5294 if ((abfd->flags & DYNAMIC) != 0)
5295 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5296 else
5297 hdr->sh_entsize = 1;
5299 else
5300 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5302 else if (SGI_COMPAT (abfd)
5303 && (strcmp (name, ".hash") == 0
5304 || strcmp (name, ".dynamic") == 0
5305 || strcmp (name, ".dynstr") == 0))
5307 if (SGI_COMPAT (abfd))
5308 hdr->sh_entsize = 0;
5309 #if 0
5310 /* This isn't how the IRIX6 linker behaves. */
5311 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
5312 #endif
5314 else if (strcmp (name, ".got") == 0
5315 || strcmp (name, ".srdata") == 0
5316 || strcmp (name, ".sdata") == 0
5317 || strcmp (name, ".sbss") == 0
5318 || strcmp (name, ".lit4") == 0
5319 || strcmp (name, ".lit8") == 0)
5320 hdr->sh_flags |= SHF_MIPS_GPREL;
5321 else if (strcmp (name, ".MIPS.interfaces") == 0)
5323 hdr->sh_type = SHT_MIPS_IFACE;
5324 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5326 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
5328 hdr->sh_type = SHT_MIPS_CONTENT;
5329 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5330 /* The sh_info field is set in final_write_processing. */
5332 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5334 hdr->sh_type = SHT_MIPS_OPTIONS;
5335 hdr->sh_entsize = 1;
5336 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5338 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
5339 hdr->sh_type = SHT_MIPS_DWARF;
5340 else if (strcmp (name, ".MIPS.symlib") == 0)
5342 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
5343 /* The sh_link and sh_info fields are set in
5344 final_write_processing. */
5346 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
5347 || strncmp (name, ".MIPS.post_rel",
5348 sizeof ".MIPS.post_rel" - 1) == 0)
5350 hdr->sh_type = SHT_MIPS_EVENTS;
5351 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5352 /* The sh_link field is set in final_write_processing. */
5354 else if (strcmp (name, ".msym") == 0)
5356 hdr->sh_type = SHT_MIPS_MSYM;
5357 hdr->sh_flags |= SHF_ALLOC;
5358 hdr->sh_entsize = 8;
5361 /* In the unlikely event a special section is empty it has to lose its
5362 special meaning. This may happen e.g. when using `strip' with the
5363 "--only-keep-debug" option. */
5364 if (sec->size > 0 && !(sec->flags & SEC_HAS_CONTENTS))
5365 hdr->sh_type = sh_type;
5367 /* The generic elf_fake_sections will set up REL_HDR using the default
5368 kind of relocations. We used to set up a second header for the
5369 non-default kind of relocations here, but only NewABI would use
5370 these, and the IRIX ld doesn't like resulting empty RELA sections.
5371 Thus we create those header only on demand now. */
5373 return TRUE;
5376 /* Given a BFD section, try to locate the corresponding ELF section
5377 index. This is used by both the 32-bit and the 64-bit ABI.
5378 Actually, it's not clear to me that the 64-bit ABI supports these,
5379 but for non-PIC objects we will certainly want support for at least
5380 the .scommon section. */
5382 bfd_boolean
5383 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5384 asection *sec, int *retval)
5386 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
5388 *retval = SHN_MIPS_SCOMMON;
5389 return TRUE;
5391 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
5393 *retval = SHN_MIPS_ACOMMON;
5394 return TRUE;
5396 return FALSE;
5399 /* Hook called by the linker routine which adds symbols from an object
5400 file. We must handle the special MIPS section numbers here. */
5402 bfd_boolean
5403 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
5404 Elf_Internal_Sym *sym, const char **namep,
5405 flagword *flagsp ATTRIBUTE_UNUSED,
5406 asection **secp, bfd_vma *valp)
5408 if (SGI_COMPAT (abfd)
5409 && (abfd->flags & DYNAMIC) != 0
5410 && strcmp (*namep, "_rld_new_interface") == 0)
5412 /* Skip IRIX5 rld entry name. */
5413 *namep = NULL;
5414 return TRUE;
5417 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
5418 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
5419 by setting a DT_NEEDED for the shared object. Since _gp_disp is
5420 a magic symbol resolved by the linker, we ignore this bogus definition
5421 of _gp_disp. New ABI objects do not suffer from this problem so this
5422 is not done for them. */
5423 if (!NEWABI_P(abfd)
5424 && (sym->st_shndx == SHN_ABS)
5425 && (strcmp (*namep, "_gp_disp") == 0))
5427 *namep = NULL;
5428 return TRUE;
5431 switch (sym->st_shndx)
5433 case SHN_COMMON:
5434 /* Common symbols less than the GP size are automatically
5435 treated as SHN_MIPS_SCOMMON symbols. */
5436 if (sym->st_size > elf_gp_size (abfd)
5437 || IRIX_COMPAT (abfd) == ict_irix6)
5438 break;
5439 /* Fall through. */
5440 case SHN_MIPS_SCOMMON:
5441 *secp = bfd_make_section_old_way (abfd, ".scommon");
5442 (*secp)->flags |= SEC_IS_COMMON;
5443 *valp = sym->st_size;
5444 break;
5446 case SHN_MIPS_TEXT:
5447 /* This section is used in a shared object. */
5448 if (elf_tdata (abfd)->elf_text_section == NULL)
5450 asymbol *elf_text_symbol;
5451 asection *elf_text_section;
5452 bfd_size_type amt = sizeof (asection);
5454 elf_text_section = bfd_zalloc (abfd, amt);
5455 if (elf_text_section == NULL)
5456 return FALSE;
5458 amt = sizeof (asymbol);
5459 elf_text_symbol = bfd_zalloc (abfd, amt);
5460 if (elf_text_symbol == NULL)
5461 return FALSE;
5463 /* Initialize the section. */
5465 elf_tdata (abfd)->elf_text_section = elf_text_section;
5466 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
5468 elf_text_section->symbol = elf_text_symbol;
5469 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
5471 elf_text_section->name = ".text";
5472 elf_text_section->flags = SEC_NO_FLAGS;
5473 elf_text_section->output_section = NULL;
5474 elf_text_section->owner = abfd;
5475 elf_text_symbol->name = ".text";
5476 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5477 elf_text_symbol->section = elf_text_section;
5479 /* This code used to do *secp = bfd_und_section_ptr if
5480 info->shared. I don't know why, and that doesn't make sense,
5481 so I took it out. */
5482 *secp = elf_tdata (abfd)->elf_text_section;
5483 break;
5485 case SHN_MIPS_ACOMMON:
5486 /* Fall through. XXX Can we treat this as allocated data? */
5487 case SHN_MIPS_DATA:
5488 /* This section is used in a shared object. */
5489 if (elf_tdata (abfd)->elf_data_section == NULL)
5491 asymbol *elf_data_symbol;
5492 asection *elf_data_section;
5493 bfd_size_type amt = sizeof (asection);
5495 elf_data_section = bfd_zalloc (abfd, amt);
5496 if (elf_data_section == NULL)
5497 return FALSE;
5499 amt = sizeof (asymbol);
5500 elf_data_symbol = bfd_zalloc (abfd, amt);
5501 if (elf_data_symbol == NULL)
5502 return FALSE;
5504 /* Initialize the section. */
5506 elf_tdata (abfd)->elf_data_section = elf_data_section;
5507 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
5509 elf_data_section->symbol = elf_data_symbol;
5510 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
5512 elf_data_section->name = ".data";
5513 elf_data_section->flags = SEC_NO_FLAGS;
5514 elf_data_section->output_section = NULL;
5515 elf_data_section->owner = abfd;
5516 elf_data_symbol->name = ".data";
5517 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5518 elf_data_symbol->section = elf_data_section;
5520 /* This code used to do *secp = bfd_und_section_ptr if
5521 info->shared. I don't know why, and that doesn't make sense,
5522 so I took it out. */
5523 *secp = elf_tdata (abfd)->elf_data_section;
5524 break;
5526 case SHN_MIPS_SUNDEFINED:
5527 *secp = bfd_und_section_ptr;
5528 break;
5531 if (SGI_COMPAT (abfd)
5532 && ! info->shared
5533 && info->hash->creator == abfd->xvec
5534 && strcmp (*namep, "__rld_obj_head") == 0)
5536 struct elf_link_hash_entry *h;
5537 struct bfd_link_hash_entry *bh;
5539 /* Mark __rld_obj_head as dynamic. */
5540 bh = NULL;
5541 if (! (_bfd_generic_link_add_one_symbol
5542 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
5543 get_elf_backend_data (abfd)->collect, &bh)))
5544 return FALSE;
5546 h = (struct elf_link_hash_entry *) bh;
5547 h->non_elf = 0;
5548 h->def_regular = 1;
5549 h->type = STT_OBJECT;
5551 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5552 return FALSE;
5554 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
5557 /* If this is a mips16 text symbol, add 1 to the value to make it
5558 odd. This will cause something like .word SYM to come up with
5559 the right value when it is loaded into the PC. */
5560 if (sym->st_other == STO_MIPS16)
5561 ++*valp;
5563 return TRUE;
5566 /* This hook function is called before the linker writes out a global
5567 symbol. We mark symbols as small common if appropriate. This is
5568 also where we undo the increment of the value for a mips16 symbol. */
5570 bfd_boolean
5571 _bfd_mips_elf_link_output_symbol_hook
5572 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5573 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
5574 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5576 /* If we see a common symbol, which implies a relocatable link, then
5577 if a symbol was small common in an input file, mark it as small
5578 common in the output file. */
5579 if (sym->st_shndx == SHN_COMMON
5580 && strcmp (input_sec->name, ".scommon") == 0)
5581 sym->st_shndx = SHN_MIPS_SCOMMON;
5583 if (sym->st_other == STO_MIPS16)
5584 sym->st_value &= ~1;
5586 return TRUE;
5589 /* Functions for the dynamic linker. */
5591 /* Create dynamic sections when linking against a dynamic object. */
5593 bfd_boolean
5594 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
5596 struct elf_link_hash_entry *h;
5597 struct bfd_link_hash_entry *bh;
5598 flagword flags;
5599 register asection *s;
5600 const char * const *namep;
5602 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5603 | SEC_LINKER_CREATED | SEC_READONLY);
5605 /* Mips ABI requests the .dynamic section to be read only. */
5606 s = bfd_get_section_by_name (abfd, ".dynamic");
5607 if (s != NULL)
5609 if (! bfd_set_section_flags (abfd, s, flags))
5610 return FALSE;
5613 /* We need to create .got section. */
5614 if (! mips_elf_create_got_section (abfd, info, FALSE))
5615 return FALSE;
5617 if (! mips_elf_rel_dyn_section (elf_hash_table (info)->dynobj, TRUE))
5618 return FALSE;
5620 /* Create .stub section. */
5621 if (bfd_get_section_by_name (abfd,
5622 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
5624 s = bfd_make_section_with_flags (abfd,
5625 MIPS_ELF_STUB_SECTION_NAME (abfd),
5626 flags | SEC_CODE);
5627 if (s == NULL
5628 || ! bfd_set_section_alignment (abfd, s,
5629 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5630 return FALSE;
5633 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
5634 && !info->shared
5635 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5637 s = bfd_make_section_with_flags (abfd, ".rld_map",
5638 flags &~ (flagword) SEC_READONLY);
5639 if (s == NULL
5640 || ! bfd_set_section_alignment (abfd, s,
5641 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5642 return FALSE;
5645 /* On IRIX5, we adjust add some additional symbols and change the
5646 alignments of several sections. There is no ABI documentation
5647 indicating that this is necessary on IRIX6, nor any evidence that
5648 the linker takes such action. */
5649 if (IRIX_COMPAT (abfd) == ict_irix5)
5651 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5653 bh = NULL;
5654 if (! (_bfd_generic_link_add_one_symbol
5655 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
5656 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5657 return FALSE;
5659 h = (struct elf_link_hash_entry *) bh;
5660 h->non_elf = 0;
5661 h->def_regular = 1;
5662 h->type = STT_SECTION;
5664 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5665 return FALSE;
5668 /* We need to create a .compact_rel section. */
5669 if (SGI_COMPAT (abfd))
5671 if (!mips_elf_create_compact_rel_section (abfd, info))
5672 return FALSE;
5675 /* Change alignments of some sections. */
5676 s = bfd_get_section_by_name (abfd, ".hash");
5677 if (s != NULL)
5678 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5679 s = bfd_get_section_by_name (abfd, ".dynsym");
5680 if (s != NULL)
5681 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5682 s = bfd_get_section_by_name (abfd, ".dynstr");
5683 if (s != NULL)
5684 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5685 s = bfd_get_section_by_name (abfd, ".reginfo");
5686 if (s != NULL)
5687 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5688 s = bfd_get_section_by_name (abfd, ".dynamic");
5689 if (s != NULL)
5690 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
5693 if (!info->shared)
5695 const char *name;
5697 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
5698 bh = NULL;
5699 if (!(_bfd_generic_link_add_one_symbol
5700 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
5701 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5702 return FALSE;
5704 h = (struct elf_link_hash_entry *) bh;
5705 h->non_elf = 0;
5706 h->def_regular = 1;
5707 h->type = STT_SECTION;
5709 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5710 return FALSE;
5712 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5714 /* __rld_map is a four byte word located in the .data section
5715 and is filled in by the rtld to contain a pointer to
5716 the _r_debug structure. Its symbol value will be set in
5717 _bfd_mips_elf_finish_dynamic_symbol. */
5718 s = bfd_get_section_by_name (abfd, ".rld_map");
5719 BFD_ASSERT (s != NULL);
5721 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
5722 bh = NULL;
5723 if (!(_bfd_generic_link_add_one_symbol
5724 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
5725 get_elf_backend_data (abfd)->collect, &bh)))
5726 return FALSE;
5728 h = (struct elf_link_hash_entry *) bh;
5729 h->non_elf = 0;
5730 h->def_regular = 1;
5731 h->type = STT_OBJECT;
5733 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5734 return FALSE;
5738 return TRUE;
5741 /* Look through the relocs for a section during the first phase, and
5742 allocate space in the global offset table. */
5744 bfd_boolean
5745 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5746 asection *sec, const Elf_Internal_Rela *relocs)
5748 const char *name;
5749 bfd *dynobj;
5750 Elf_Internal_Shdr *symtab_hdr;
5751 struct elf_link_hash_entry **sym_hashes;
5752 struct mips_got_info *g;
5753 size_t extsymoff;
5754 const Elf_Internal_Rela *rel;
5755 const Elf_Internal_Rela *rel_end;
5756 asection *sgot;
5757 asection *sreloc;
5758 const struct elf_backend_data *bed;
5760 if (info->relocatable)
5761 return TRUE;
5763 dynobj = elf_hash_table (info)->dynobj;
5764 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5765 sym_hashes = elf_sym_hashes (abfd);
5766 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5768 /* Check for the mips16 stub sections. */
5770 name = bfd_get_section_name (abfd, sec);
5771 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5773 unsigned long r_symndx;
5775 /* Look at the relocation information to figure out which symbol
5776 this is for. */
5778 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
5780 if (r_symndx < extsymoff
5781 || sym_hashes[r_symndx - extsymoff] == NULL)
5783 asection *o;
5785 /* This stub is for a local symbol. This stub will only be
5786 needed if there is some relocation in this BFD, other
5787 than a 16 bit function call, which refers to this symbol. */
5788 for (o = abfd->sections; o != NULL; o = o->next)
5790 Elf_Internal_Rela *sec_relocs;
5791 const Elf_Internal_Rela *r, *rend;
5793 /* We can ignore stub sections when looking for relocs. */
5794 if ((o->flags & SEC_RELOC) == 0
5795 || o->reloc_count == 0
5796 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5797 sizeof FN_STUB - 1) == 0
5798 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5799 sizeof CALL_STUB - 1) == 0
5800 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5801 sizeof CALL_FP_STUB - 1) == 0)
5802 continue;
5804 sec_relocs
5805 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
5806 info->keep_memory);
5807 if (sec_relocs == NULL)
5808 return FALSE;
5810 rend = sec_relocs + o->reloc_count;
5811 for (r = sec_relocs; r < rend; r++)
5812 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
5813 && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
5814 break;
5816 if (elf_section_data (o)->relocs != sec_relocs)
5817 free (sec_relocs);
5819 if (r < rend)
5820 break;
5823 if (o == NULL)
5825 /* There is no non-call reloc for this stub, so we do
5826 not need it. Since this function is called before
5827 the linker maps input sections to output sections, we
5828 can easily discard it by setting the SEC_EXCLUDE
5829 flag. */
5830 sec->flags |= SEC_EXCLUDE;
5831 return TRUE;
5834 /* Record this stub in an array of local symbol stubs for
5835 this BFD. */
5836 if (elf_tdata (abfd)->local_stubs == NULL)
5838 unsigned long symcount;
5839 asection **n;
5840 bfd_size_type amt;
5842 if (elf_bad_symtab (abfd))
5843 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
5844 else
5845 symcount = symtab_hdr->sh_info;
5846 amt = symcount * sizeof (asection *);
5847 n = bfd_zalloc (abfd, amt);
5848 if (n == NULL)
5849 return FALSE;
5850 elf_tdata (abfd)->local_stubs = n;
5853 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5855 /* We don't need to set mips16_stubs_seen in this case.
5856 That flag is used to see whether we need to look through
5857 the global symbol table for stubs. We don't need to set
5858 it here, because we just have a local stub. */
5860 else
5862 struct mips_elf_link_hash_entry *h;
5864 h = ((struct mips_elf_link_hash_entry *)
5865 sym_hashes[r_symndx - extsymoff]);
5867 while (h->root.root.type == bfd_link_hash_indirect
5868 || h->root.root.type == bfd_link_hash_warning)
5869 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5871 /* H is the symbol this stub is for. */
5873 h->fn_stub = sec;
5874 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
5877 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5878 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5880 unsigned long r_symndx;
5881 struct mips_elf_link_hash_entry *h;
5882 asection **loc;
5884 /* Look at the relocation information to figure out which symbol
5885 this is for. */
5887 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
5889 if (r_symndx < extsymoff
5890 || sym_hashes[r_symndx - extsymoff] == NULL)
5892 /* This stub was actually built for a static symbol defined
5893 in the same file. We assume that all static symbols in
5894 mips16 code are themselves mips16, so we can simply
5895 discard this stub. Since this function is called before
5896 the linker maps input sections to output sections, we can
5897 easily discard it by setting the SEC_EXCLUDE flag. */
5898 sec->flags |= SEC_EXCLUDE;
5899 return TRUE;
5902 h = ((struct mips_elf_link_hash_entry *)
5903 sym_hashes[r_symndx - extsymoff]);
5905 /* H is the symbol this stub is for. */
5907 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5908 loc = &h->call_fp_stub;
5909 else
5910 loc = &h->call_stub;
5912 /* If we already have an appropriate stub for this function, we
5913 don't need another one, so we can discard this one. Since
5914 this function is called before the linker maps input sections
5915 to output sections, we can easily discard it by setting the
5916 SEC_EXCLUDE flag. We can also discard this section if we
5917 happen to already know that this is a mips16 function; it is
5918 not necessary to check this here, as it is checked later, but
5919 it is slightly faster to check now. */
5920 if (*loc != NULL || h->root.other == STO_MIPS16)
5922 sec->flags |= SEC_EXCLUDE;
5923 return TRUE;
5926 *loc = sec;
5927 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
5930 if (dynobj == NULL)
5932 sgot = NULL;
5933 g = NULL;
5935 else
5937 sgot = mips_elf_got_section (dynobj, FALSE);
5938 if (sgot == NULL)
5939 g = NULL;
5940 else
5942 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
5943 g = mips_elf_section_data (sgot)->u.got_info;
5944 BFD_ASSERT (g != NULL);
5948 sreloc = NULL;
5949 bed = get_elf_backend_data (abfd);
5950 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5951 for (rel = relocs; rel < rel_end; ++rel)
5953 unsigned long r_symndx;
5954 unsigned int r_type;
5955 struct elf_link_hash_entry *h;
5957 r_symndx = ELF_R_SYM (abfd, rel->r_info);
5958 r_type = ELF_R_TYPE (abfd, rel->r_info);
5960 if (r_symndx < extsymoff)
5961 h = NULL;
5962 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
5964 (*_bfd_error_handler)
5965 (_("%B: Malformed reloc detected for section %s"),
5966 abfd, name);
5967 bfd_set_error (bfd_error_bad_value);
5968 return FALSE;
5970 else
5972 h = sym_hashes[r_symndx - extsymoff];
5974 /* This may be an indirect symbol created because of a version. */
5975 if (h != NULL)
5977 while (h->root.type == bfd_link_hash_indirect)
5978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5982 /* Some relocs require a global offset table. */
5983 if (dynobj == NULL || sgot == NULL)
5985 switch (r_type)
5987 case R_MIPS_GOT16:
5988 case R_MIPS_CALL16:
5989 case R_MIPS_CALL_HI16:
5990 case R_MIPS_CALL_LO16:
5991 case R_MIPS_GOT_HI16:
5992 case R_MIPS_GOT_LO16:
5993 case R_MIPS_GOT_PAGE:
5994 case R_MIPS_GOT_OFST:
5995 case R_MIPS_GOT_DISP:
5996 case R_MIPS_TLS_GD:
5997 case R_MIPS_TLS_LDM:
5998 if (dynobj == NULL)
5999 elf_hash_table (info)->dynobj = dynobj = abfd;
6000 if (! mips_elf_create_got_section (dynobj, info, FALSE))
6001 return FALSE;
6002 g = mips_elf_got_info (dynobj, &sgot);
6003 break;
6005 case R_MIPS_32:
6006 case R_MIPS_REL32:
6007 case R_MIPS_64:
6008 if (dynobj == NULL
6009 && (info->shared || h != NULL)
6010 && (sec->flags & SEC_ALLOC) != 0)
6011 elf_hash_table (info)->dynobj = dynobj = abfd;
6012 break;
6014 default:
6015 break;
6019 if (!h && (r_type == R_MIPS_CALL_LO16
6020 || r_type == R_MIPS_GOT_LO16
6021 || r_type == R_MIPS_GOT_DISP))
6023 /* We may need a local GOT entry for this relocation. We
6024 don't count R_MIPS_GOT_PAGE because we can estimate the
6025 maximum number of pages needed by looking at the size of
6026 the segment. Similar comments apply to R_MIPS_GOT16 and
6027 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
6028 R_MIPS_CALL_HI16 because these are always followed by an
6029 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
6030 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
6031 rel->r_addend, g, 0))
6032 return FALSE;
6035 switch (r_type)
6037 case R_MIPS_CALL16:
6038 if (h == NULL)
6040 (*_bfd_error_handler)
6041 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
6042 abfd, (unsigned long) rel->r_offset);
6043 bfd_set_error (bfd_error_bad_value);
6044 return FALSE;
6046 /* Fall through. */
6048 case R_MIPS_CALL_HI16:
6049 case R_MIPS_CALL_LO16:
6050 if (h != NULL)
6052 /* This symbol requires a global offset table entry. */
6053 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6054 return FALSE;
6056 /* We need a stub, not a plt entry for the undefined
6057 function. But we record it as if it needs plt. See
6058 _bfd_elf_adjust_dynamic_symbol. */
6059 h->needs_plt = 1;
6060 h->type = STT_FUNC;
6062 break;
6064 case R_MIPS_GOT_PAGE:
6065 /* If this is a global, overridable symbol, GOT_PAGE will
6066 decay to GOT_DISP, so we'll need a GOT entry for it. */
6067 if (h == NULL)
6068 break;
6069 else
6071 struct mips_elf_link_hash_entry *hmips =
6072 (struct mips_elf_link_hash_entry *) h;
6074 while (hmips->root.root.type == bfd_link_hash_indirect
6075 || hmips->root.root.type == bfd_link_hash_warning)
6076 hmips = (struct mips_elf_link_hash_entry *)
6077 hmips->root.root.u.i.link;
6079 if (hmips->root.def_regular
6080 && ! (info->shared && ! info->symbolic
6081 && ! hmips->root.forced_local))
6082 break;
6084 /* Fall through. */
6086 case R_MIPS_GOT16:
6087 case R_MIPS_GOT_HI16:
6088 case R_MIPS_GOT_LO16:
6089 case R_MIPS_GOT_DISP:
6090 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6091 return FALSE;
6092 break;
6094 case R_MIPS_TLS_GOTTPREL:
6095 if (info->shared)
6096 info->flags |= DF_STATIC_TLS;
6097 /* Fall through */
6099 case R_MIPS_TLS_LDM:
6100 if (r_type == R_MIPS_TLS_LDM)
6102 r_symndx = 0;
6103 h = NULL;
6105 /* Fall through */
6107 case R_MIPS_TLS_GD:
6108 /* This symbol requires a global offset table entry, or two
6109 for TLS GD relocations. */
6111 unsigned char flag = (r_type == R_MIPS_TLS_GD
6112 ? GOT_TLS_GD
6113 : r_type == R_MIPS_TLS_LDM
6114 ? GOT_TLS_LDM
6115 : GOT_TLS_IE);
6116 if (h != NULL)
6118 struct mips_elf_link_hash_entry *hmips =
6119 (struct mips_elf_link_hash_entry *) h;
6120 hmips->tls_type |= flag;
6122 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
6123 return FALSE;
6125 else
6127 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
6129 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
6130 rel->r_addend, g, flag))
6131 return FALSE;
6134 break;
6136 case R_MIPS_32:
6137 case R_MIPS_REL32:
6138 case R_MIPS_64:
6139 if ((info->shared || h != NULL)
6140 && (sec->flags & SEC_ALLOC) != 0)
6142 if (sreloc == NULL)
6144 sreloc = mips_elf_rel_dyn_section (dynobj, TRUE);
6145 if (sreloc == NULL)
6146 return FALSE;
6148 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
6149 if (info->shared)
6151 /* When creating a shared object, we must copy these
6152 reloc types into the output file as R_MIPS_REL32
6153 relocs. We make room for this reloc in the
6154 .rel.dyn reloc section. */
6155 mips_elf_allocate_dynamic_relocations (dynobj, 1);
6156 if ((sec->flags & MIPS_READONLY_SECTION)
6157 == MIPS_READONLY_SECTION)
6158 /* We tell the dynamic linker that there are
6159 relocations against the text segment. */
6160 info->flags |= DF_TEXTREL;
6162 else
6164 struct mips_elf_link_hash_entry *hmips;
6166 /* We only need to copy this reloc if the symbol is
6167 defined in a dynamic object. */
6168 hmips = (struct mips_elf_link_hash_entry *) h;
6169 ++hmips->possibly_dynamic_relocs;
6170 if ((sec->flags & MIPS_READONLY_SECTION)
6171 == MIPS_READONLY_SECTION)
6172 /* We need it to tell the dynamic linker if there
6173 are relocations against the text segment. */
6174 hmips->readonly_reloc = TRUE;
6177 /* Even though we don't directly need a GOT entry for
6178 this symbol, a symbol must have a dynamic symbol
6179 table index greater that DT_MIPS_GOTSYM if there are
6180 dynamic relocations against it. */
6181 if (h != NULL)
6183 if (dynobj == NULL)
6184 elf_hash_table (info)->dynobj = dynobj = abfd;
6185 if (! mips_elf_create_got_section (dynobj, info, TRUE))
6186 return FALSE;
6187 g = mips_elf_got_info (dynobj, &sgot);
6188 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
6189 return FALSE;
6193 if (SGI_COMPAT (abfd))
6194 mips_elf_hash_table (info)->compact_rel_size +=
6195 sizeof (Elf32_External_crinfo);
6196 break;
6198 case R_MIPS_26:
6199 case R_MIPS_GPREL16:
6200 case R_MIPS_LITERAL:
6201 case R_MIPS_GPREL32:
6202 if (SGI_COMPAT (abfd))
6203 mips_elf_hash_table (info)->compact_rel_size +=
6204 sizeof (Elf32_External_crinfo);
6205 break;
6207 /* This relocation describes the C++ object vtable hierarchy.
6208 Reconstruct it for later use during GC. */
6209 case R_MIPS_GNU_VTINHERIT:
6210 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6211 return FALSE;
6212 break;
6214 /* This relocation describes which C++ vtable entries are actually
6215 used. Record for later use during GC. */
6216 case R_MIPS_GNU_VTENTRY:
6217 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6218 return FALSE;
6219 break;
6221 default:
6222 break;
6225 /* We must not create a stub for a symbol that has relocations
6226 related to taking the function's address. */
6227 switch (r_type)
6229 default:
6230 if (h != NULL)
6232 struct mips_elf_link_hash_entry *mh;
6234 mh = (struct mips_elf_link_hash_entry *) h;
6235 mh->no_fn_stub = TRUE;
6237 break;
6238 case R_MIPS_CALL16:
6239 case R_MIPS_CALL_HI16:
6240 case R_MIPS_CALL_LO16:
6241 case R_MIPS_JALR:
6242 break;
6245 /* If this reloc is not a 16 bit call, and it has a global
6246 symbol, then we will need the fn_stub if there is one.
6247 References from a stub section do not count. */
6248 if (h != NULL
6249 && r_type != R_MIPS16_26
6250 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6251 sizeof FN_STUB - 1) != 0
6252 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6253 sizeof CALL_STUB - 1) != 0
6254 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6255 sizeof CALL_FP_STUB - 1) != 0)
6257 struct mips_elf_link_hash_entry *mh;
6259 mh = (struct mips_elf_link_hash_entry *) h;
6260 mh->need_fn_stub = TRUE;
6264 return TRUE;
6267 bfd_boolean
6268 _bfd_mips_relax_section (bfd *abfd, asection *sec,
6269 struct bfd_link_info *link_info,
6270 bfd_boolean *again)
6272 Elf_Internal_Rela *internal_relocs;
6273 Elf_Internal_Rela *irel, *irelend;
6274 Elf_Internal_Shdr *symtab_hdr;
6275 bfd_byte *contents = NULL;
6276 size_t extsymoff;
6277 bfd_boolean changed_contents = FALSE;
6278 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
6279 Elf_Internal_Sym *isymbuf = NULL;
6281 /* We are not currently changing any sizes, so only one pass. */
6282 *again = FALSE;
6284 if (link_info->relocatable)
6285 return TRUE;
6287 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
6288 link_info->keep_memory);
6289 if (internal_relocs == NULL)
6290 return TRUE;
6292 irelend = internal_relocs + sec->reloc_count
6293 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
6294 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6295 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6297 for (irel = internal_relocs; irel < irelend; irel++)
6299 bfd_vma symval;
6300 bfd_signed_vma sym_offset;
6301 unsigned int r_type;
6302 unsigned long r_symndx;
6303 asection *sym_sec;
6304 unsigned long instruction;
6306 /* Turn jalr into bgezal, and jr into beq, if they're marked
6307 with a JALR relocation, that indicate where they jump to.
6308 This saves some pipeline bubbles. */
6309 r_type = ELF_R_TYPE (abfd, irel->r_info);
6310 if (r_type != R_MIPS_JALR)
6311 continue;
6313 r_symndx = ELF_R_SYM (abfd, irel->r_info);
6314 /* Compute the address of the jump target. */
6315 if (r_symndx >= extsymoff)
6317 struct mips_elf_link_hash_entry *h
6318 = ((struct mips_elf_link_hash_entry *)
6319 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
6321 while (h->root.root.type == bfd_link_hash_indirect
6322 || h->root.root.type == bfd_link_hash_warning)
6323 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6325 /* If a symbol is undefined, or if it may be overridden,
6326 skip it. */
6327 if (! ((h->root.root.type == bfd_link_hash_defined
6328 || h->root.root.type == bfd_link_hash_defweak)
6329 && h->root.root.u.def.section)
6330 || (link_info->shared && ! link_info->symbolic
6331 && !h->root.forced_local))
6332 continue;
6334 sym_sec = h->root.root.u.def.section;
6335 if (sym_sec->output_section)
6336 symval = (h->root.root.u.def.value
6337 + sym_sec->output_section->vma
6338 + sym_sec->output_offset);
6339 else
6340 symval = h->root.root.u.def.value;
6342 else
6344 Elf_Internal_Sym *isym;
6346 /* Read this BFD's symbols if we haven't done so already. */
6347 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
6349 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6350 if (isymbuf == NULL)
6351 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6352 symtab_hdr->sh_info, 0,
6353 NULL, NULL, NULL);
6354 if (isymbuf == NULL)
6355 goto relax_return;
6358 isym = isymbuf + r_symndx;
6359 if (isym->st_shndx == SHN_UNDEF)
6360 continue;
6361 else if (isym->st_shndx == SHN_ABS)
6362 sym_sec = bfd_abs_section_ptr;
6363 else if (isym->st_shndx == SHN_COMMON)
6364 sym_sec = bfd_com_section_ptr;
6365 else
6366 sym_sec
6367 = bfd_section_from_elf_index (abfd, isym->st_shndx);
6368 symval = isym->st_value
6369 + sym_sec->output_section->vma
6370 + sym_sec->output_offset;
6373 /* Compute branch offset, from delay slot of the jump to the
6374 branch target. */
6375 sym_offset = (symval + irel->r_addend)
6376 - (sec_start + irel->r_offset + 4);
6378 /* Branch offset must be properly aligned. */
6379 if ((sym_offset & 3) != 0)
6380 continue;
6382 sym_offset >>= 2;
6384 /* Check that it's in range. */
6385 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
6386 continue;
6388 /* Get the section contents if we haven't done so already. */
6389 if (contents == NULL)
6391 /* Get cached copy if it exists. */
6392 if (elf_section_data (sec)->this_hdr.contents != NULL)
6393 contents = elf_section_data (sec)->this_hdr.contents;
6394 else
6396 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6397 goto relax_return;
6401 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
6403 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
6404 if ((instruction & 0xfc1fffff) == 0x0000f809)
6405 instruction = 0x04110000;
6406 /* If it was jr <reg>, turn it into b <target>. */
6407 else if ((instruction & 0xfc1fffff) == 0x00000008)
6408 instruction = 0x10000000;
6409 else
6410 continue;
6412 instruction |= (sym_offset & 0xffff);
6413 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
6414 changed_contents = TRUE;
6417 if (contents != NULL
6418 && elf_section_data (sec)->this_hdr.contents != contents)
6420 if (!changed_contents && !link_info->keep_memory)
6421 free (contents);
6422 else
6424 /* Cache the section contents for elf_link_input_bfd. */
6425 elf_section_data (sec)->this_hdr.contents = contents;
6428 return TRUE;
6430 relax_return:
6431 if (contents != NULL
6432 && elf_section_data (sec)->this_hdr.contents != contents)
6433 free (contents);
6434 return FALSE;
6437 /* Adjust a symbol defined by a dynamic object and referenced by a
6438 regular object. The current definition is in some section of the
6439 dynamic object, but we're not including those sections. We have to
6440 change the definition to something the rest of the link can
6441 understand. */
6443 bfd_boolean
6444 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6445 struct elf_link_hash_entry *h)
6447 bfd *dynobj;
6448 struct mips_elf_link_hash_entry *hmips;
6449 asection *s;
6451 dynobj = elf_hash_table (info)->dynobj;
6453 /* Make sure we know what is going on here. */
6454 BFD_ASSERT (dynobj != NULL
6455 && (h->needs_plt
6456 || h->u.weakdef != NULL
6457 || (h->def_dynamic
6458 && h->ref_regular
6459 && !h->def_regular)));
6461 /* If this symbol is defined in a dynamic object, we need to copy
6462 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6463 file. */
6464 hmips = (struct mips_elf_link_hash_entry *) h;
6465 if (! info->relocatable
6466 && hmips->possibly_dynamic_relocs != 0
6467 && (h->root.type == bfd_link_hash_defweak
6468 || !h->def_regular))
6470 mips_elf_allocate_dynamic_relocations (dynobj,
6471 hmips->possibly_dynamic_relocs);
6472 if (hmips->readonly_reloc)
6473 /* We tell the dynamic linker that there are relocations
6474 against the text segment. */
6475 info->flags |= DF_TEXTREL;
6478 /* For a function, create a stub, if allowed. */
6479 if (! hmips->no_fn_stub
6480 && h->needs_plt)
6482 if (! elf_hash_table (info)->dynamic_sections_created)
6483 return TRUE;
6485 /* If this symbol is not defined in a regular file, then set
6486 the symbol to the stub location. This is required to make
6487 function pointers compare as equal between the normal
6488 executable and the shared library. */
6489 if (!h->def_regular)
6491 /* We need .stub section. */
6492 s = bfd_get_section_by_name (dynobj,
6493 MIPS_ELF_STUB_SECTION_NAME (dynobj));
6494 BFD_ASSERT (s != NULL);
6496 h->root.u.def.section = s;
6497 h->root.u.def.value = s->size;
6499 /* XXX Write this stub address somewhere. */
6500 h->plt.offset = s->size;
6502 /* Make room for this stub code. */
6503 s->size += MIPS_FUNCTION_STUB_SIZE;
6505 /* The last half word of the stub will be filled with the index
6506 of this symbol in .dynsym section. */
6507 return TRUE;
6510 else if ((h->type == STT_FUNC)
6511 && !h->needs_plt)
6513 /* This will set the entry for this symbol in the GOT to 0, and
6514 the dynamic linker will take care of this. */
6515 h->root.u.def.value = 0;
6516 return TRUE;
6519 /* If this is a weak symbol, and there is a real definition, the
6520 processor independent code will have arranged for us to see the
6521 real definition first, and we can just use the same value. */
6522 if (h->u.weakdef != NULL)
6524 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6525 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6526 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6527 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6528 return TRUE;
6531 /* This is a reference to a symbol defined by a dynamic object which
6532 is not a function. */
6534 return TRUE;
6537 /* This function is called after all the input files have been read,
6538 and the input sections have been assigned to output sections. We
6539 check for any mips16 stub sections that we can discard. */
6541 bfd_boolean
6542 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
6543 struct bfd_link_info *info)
6545 asection *ri;
6547 bfd *dynobj;
6548 asection *s;
6549 struct mips_got_info *g;
6550 int i;
6551 bfd_size_type loadable_size = 0;
6552 bfd_size_type local_gotno;
6553 bfd *sub;
6554 struct mips_elf_count_tls_arg count_tls_arg;
6556 /* The .reginfo section has a fixed size. */
6557 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6558 if (ri != NULL)
6559 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6561 if (! (info->relocatable
6562 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
6563 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6564 mips_elf_check_mips16_stubs, NULL);
6566 dynobj = elf_hash_table (info)->dynobj;
6567 if (dynobj == NULL)
6568 /* Relocatable links don't have it. */
6569 return TRUE;
6571 g = mips_elf_got_info (dynobj, &s);
6572 if (s == NULL)
6573 return TRUE;
6575 /* Calculate the total loadable size of the output. That
6576 will give us the maximum number of GOT_PAGE entries
6577 required. */
6578 for (sub = info->input_bfds; sub; sub = sub->link_next)
6580 asection *subsection;
6582 for (subsection = sub->sections;
6583 subsection;
6584 subsection = subsection->next)
6586 if ((subsection->flags & SEC_ALLOC) == 0)
6587 continue;
6588 loadable_size += ((subsection->size + 0xf)
6589 &~ (bfd_size_type) 0xf);
6593 /* There has to be a global GOT entry for every symbol with
6594 a dynamic symbol table index of DT_MIPS_GOTSYM or
6595 higher. Therefore, it make sense to put those symbols
6596 that need GOT entries at the end of the symbol table. We
6597 do that here. */
6598 if (! mips_elf_sort_hash_table (info, 1))
6599 return FALSE;
6601 if (g->global_gotsym != NULL)
6602 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
6603 else
6604 /* If there are no global symbols, or none requiring
6605 relocations, then GLOBAL_GOTSYM will be NULL. */
6606 i = 0;
6608 /* In the worst case, we'll get one stub per dynamic symbol, plus
6609 one to account for the dummy entry at the end required by IRIX
6610 rld. */
6611 loadable_size += MIPS_FUNCTION_STUB_SIZE * (i + 1);
6613 /* Assume there are two loadable segments consisting of
6614 contiguous sections. Is 5 enough? */
6615 local_gotno = (loadable_size >> 16) + 5;
6617 g->local_gotno += local_gotno;
6618 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
6620 g->global_gotno = i;
6621 s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
6623 /* We need to calculate tls_gotno for global symbols at this point
6624 instead of building it up earlier, to avoid doublecounting
6625 entries for one global symbol from multiple input files. */
6626 count_tls_arg.info = info;
6627 count_tls_arg.needed = 0;
6628 elf_link_hash_traverse (elf_hash_table (info),
6629 mips_elf_count_global_tls_entries,
6630 &count_tls_arg);
6631 g->tls_gotno += count_tls_arg.needed;
6632 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
6634 mips_elf_resolve_final_got_entries (g);
6636 if (s->size > MIPS_ELF_GOT_MAX_SIZE (output_bfd))
6638 if (! mips_elf_multi_got (output_bfd, info, g, s, local_gotno))
6639 return FALSE;
6641 else
6643 /* Set up TLS entries for the first GOT. */
6644 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
6645 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
6648 return TRUE;
6651 /* Set the sizes of the dynamic sections. */
6653 bfd_boolean
6654 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
6655 struct bfd_link_info *info)
6657 bfd *dynobj;
6658 asection *s;
6659 bfd_boolean reltext;
6661 dynobj = elf_hash_table (info)->dynobj;
6662 BFD_ASSERT (dynobj != NULL);
6664 if (elf_hash_table (info)->dynamic_sections_created)
6666 /* Set the contents of the .interp section to the interpreter. */
6667 if (info->executable)
6669 s = bfd_get_section_by_name (dynobj, ".interp");
6670 BFD_ASSERT (s != NULL);
6671 s->size
6672 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
6673 s->contents
6674 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
6678 /* The check_relocs and adjust_dynamic_symbol entry points have
6679 determined the sizes of the various dynamic sections. Allocate
6680 memory for them. */
6681 reltext = FALSE;
6682 for (s = dynobj->sections; s != NULL; s = s->next)
6684 const char *name;
6686 /* It's OK to base decisions on the section name, because none
6687 of the dynobj section names depend upon the input files. */
6688 name = bfd_get_section_name (dynobj, s);
6690 if ((s->flags & SEC_LINKER_CREATED) == 0)
6691 continue;
6693 if (strncmp (name, ".rel", 4) == 0)
6695 if (s->size != 0)
6697 const char *outname;
6698 asection *target;
6700 /* If this relocation section applies to a read only
6701 section, then we probably need a DT_TEXTREL entry.
6702 If the relocation section is .rel.dyn, we always
6703 assert a DT_TEXTREL entry rather than testing whether
6704 there exists a relocation to a read only section or
6705 not. */
6706 outname = bfd_get_section_name (output_bfd,
6707 s->output_section);
6708 target = bfd_get_section_by_name (output_bfd, outname + 4);
6709 if ((target != NULL
6710 && (target->flags & SEC_READONLY) != 0
6711 && (target->flags & SEC_ALLOC) != 0)
6712 || strcmp (outname, ".rel.dyn") == 0)
6713 reltext = TRUE;
6715 /* We use the reloc_count field as a counter if we need
6716 to copy relocs into the output file. */
6717 if (strcmp (name, ".rel.dyn") != 0)
6718 s->reloc_count = 0;
6720 /* If combreloc is enabled, elf_link_sort_relocs() will
6721 sort relocations, but in a different way than we do,
6722 and before we're done creating relocations. Also, it
6723 will move them around between input sections'
6724 relocation's contents, so our sorting would be
6725 broken, so don't let it run. */
6726 info->combreloc = 0;
6729 else if (strncmp (name, ".got", 4) == 0)
6731 /* _bfd_mips_elf_always_size_sections() has already done
6732 most of the work, but some symbols may have been mapped
6733 to versions that we must now resolve in the got_entries
6734 hash tables. */
6735 struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
6736 struct mips_got_info *g = gg;
6737 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
6738 unsigned int needed_relocs = 0;
6740 if (gg->next)
6742 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
6743 set_got_offset_arg.info = info;
6745 /* NOTE 2005-02-03: How can this call, or the next, ever
6746 find any indirect entries to resolve? They were all
6747 resolved in mips_elf_multi_got. */
6748 mips_elf_resolve_final_got_entries (gg);
6749 for (g = gg->next; g && g->next != gg; g = g->next)
6751 unsigned int save_assign;
6753 mips_elf_resolve_final_got_entries (g);
6755 /* Assign offsets to global GOT entries. */
6756 save_assign = g->assigned_gotno;
6757 g->assigned_gotno = g->local_gotno;
6758 set_got_offset_arg.g = g;
6759 set_got_offset_arg.needed_relocs = 0;
6760 htab_traverse (g->got_entries,
6761 mips_elf_set_global_got_offset,
6762 &set_got_offset_arg);
6763 needed_relocs += set_got_offset_arg.needed_relocs;
6764 BFD_ASSERT (g->assigned_gotno - g->local_gotno
6765 <= g->global_gotno);
6767 g->assigned_gotno = save_assign;
6768 if (info->shared)
6770 needed_relocs += g->local_gotno - g->assigned_gotno;
6771 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
6772 + g->next->global_gotno
6773 + g->next->tls_gotno
6774 + MIPS_RESERVED_GOTNO);
6778 else
6780 struct mips_elf_count_tls_arg arg;
6781 arg.info = info;
6782 arg.needed = 0;
6784 htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
6785 &arg);
6786 elf_link_hash_traverse (elf_hash_table (info),
6787 mips_elf_count_global_tls_relocs,
6788 &arg);
6790 needed_relocs += arg.needed;
6793 if (needed_relocs)
6794 mips_elf_allocate_dynamic_relocations (dynobj, needed_relocs);
6796 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
6798 /* IRIX rld assumes that the function stub isn't at the end
6799 of .text section. So put a dummy. XXX */
6800 s->size += MIPS_FUNCTION_STUB_SIZE;
6802 else if (! info->shared
6803 && ! mips_elf_hash_table (info)->use_rld_obj_head
6804 && strncmp (name, ".rld_map", 8) == 0)
6806 /* We add a room for __rld_map. It will be filled in by the
6807 rtld to contain a pointer to the _r_debug structure. */
6808 s->size += 4;
6810 else if (SGI_COMPAT (output_bfd)
6811 && strncmp (name, ".compact_rel", 12) == 0)
6812 s->size += mips_elf_hash_table (info)->compact_rel_size;
6813 else if (strncmp (name, ".init", 5) != 0)
6815 /* It's not one of our sections, so don't allocate space. */
6816 continue;
6819 if (s->size == 0)
6821 s->flags |= SEC_EXCLUDE;
6822 continue;
6825 if ((s->flags & SEC_HAS_CONTENTS) == 0)
6826 continue;
6828 /* Allocate memory for the section contents. */
6829 s->contents = bfd_zalloc (dynobj, s->size);
6830 if (s->contents == NULL)
6832 bfd_set_error (bfd_error_no_memory);
6833 return FALSE;
6837 if (elf_hash_table (info)->dynamic_sections_created)
6839 /* Add some entries to the .dynamic section. We fill in the
6840 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
6841 must add the entries now so that we get the correct size for
6842 the .dynamic section. The DT_DEBUG entry is filled in by the
6843 dynamic linker and used by the debugger. */
6844 if (! info->shared)
6846 /* SGI object has the equivalence of DT_DEBUG in the
6847 DT_MIPS_RLD_MAP entry. */
6848 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
6849 return FALSE;
6850 if (!SGI_COMPAT (output_bfd))
6852 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
6853 return FALSE;
6856 else
6858 /* Shared libraries on traditional mips have DT_DEBUG. */
6859 if (!SGI_COMPAT (output_bfd))
6861 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
6862 return FALSE;
6866 if (reltext && SGI_COMPAT (output_bfd))
6867 info->flags |= DF_TEXTREL;
6869 if ((info->flags & DF_TEXTREL) != 0)
6871 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
6872 return FALSE;
6875 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
6876 return FALSE;
6878 if (mips_elf_rel_dyn_section (dynobj, FALSE))
6880 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
6881 return FALSE;
6883 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
6884 return FALSE;
6886 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
6887 return FALSE;
6890 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
6891 return FALSE;
6893 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
6894 return FALSE;
6896 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
6897 return FALSE;
6899 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
6900 return FALSE;
6902 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
6903 return FALSE;
6905 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
6906 return FALSE;
6908 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
6909 return FALSE;
6911 if (IRIX_COMPAT (dynobj) == ict_irix5
6912 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
6913 return FALSE;
6915 if (IRIX_COMPAT (dynobj) == ict_irix6
6916 && (bfd_get_section_by_name
6917 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
6918 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
6919 return FALSE;
6922 return TRUE;
6925 /* Relocate a MIPS ELF section. */
6927 bfd_boolean
6928 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
6929 bfd *input_bfd, asection *input_section,
6930 bfd_byte *contents, Elf_Internal_Rela *relocs,
6931 Elf_Internal_Sym *local_syms,
6932 asection **local_sections)
6934 Elf_Internal_Rela *rel;
6935 const Elf_Internal_Rela *relend;
6936 bfd_vma addend = 0;
6937 bfd_boolean use_saved_addend_p = FALSE;
6938 const struct elf_backend_data *bed;
6940 bed = get_elf_backend_data (output_bfd);
6941 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6942 for (rel = relocs; rel < relend; ++rel)
6944 const char *name;
6945 bfd_vma value;
6946 reloc_howto_type *howto;
6947 bfd_boolean require_jalx;
6948 /* TRUE if the relocation is a RELA relocation, rather than a
6949 REL relocation. */
6950 bfd_boolean rela_relocation_p = TRUE;
6951 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6952 const char *msg;
6954 /* Find the relocation howto for this relocation. */
6955 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
6957 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6958 64-bit code, but make sure all their addresses are in the
6959 lowermost or uppermost 32-bit section of the 64-bit address
6960 space. Thus, when they use an R_MIPS_64 they mean what is
6961 usually meant by R_MIPS_32, with the exception that the
6962 stored value is sign-extended to 64 bits. */
6963 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
6965 /* On big-endian systems, we need to lie about the position
6966 of the reloc. */
6967 if (bfd_big_endian (input_bfd))
6968 rel->r_offset += 4;
6970 else
6971 /* NewABI defaults to RELA relocations. */
6972 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
6973 NEWABI_P (input_bfd)
6974 && (MIPS_RELOC_RELA_P
6975 (input_bfd, input_section,
6976 rel - relocs)));
6978 if (!use_saved_addend_p)
6980 Elf_Internal_Shdr *rel_hdr;
6982 /* If these relocations were originally of the REL variety,
6983 we must pull the addend out of the field that will be
6984 relocated. Otherwise, we simply use the contents of the
6985 RELA relocation. To determine which flavor or relocation
6986 this is, we depend on the fact that the INPUT_SECTION's
6987 REL_HDR is read before its REL_HDR2. */
6988 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6989 if ((size_t) (rel - relocs)
6990 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6991 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6992 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6994 bfd_byte *location = contents + rel->r_offset;
6996 /* Note that this is a REL relocation. */
6997 rela_relocation_p = FALSE;
6999 /* Get the addend, which is stored in the input file. */
7000 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE,
7001 location);
7002 addend = mips_elf_obtain_contents (howto, rel, input_bfd,
7003 contents);
7004 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, FALSE,
7005 location);
7007 addend &= howto->src_mask;
7009 /* For some kinds of relocations, the ADDEND is a
7010 combination of the addend stored in two different
7011 relocations. */
7012 if (r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16
7013 || (r_type == R_MIPS_GOT16
7014 && mips_elf_local_relocation_p (input_bfd, rel,
7015 local_sections, FALSE)))
7017 bfd_vma l;
7018 const Elf_Internal_Rela *lo16_relocation;
7019 reloc_howto_type *lo16_howto;
7020 bfd_byte *lo16_location;
7021 int lo16_type;
7023 if (r_type == R_MIPS16_HI16)
7024 lo16_type = R_MIPS16_LO16;
7025 else
7026 lo16_type = R_MIPS_LO16;
7028 /* The combined value is the sum of the HI16 addend,
7029 left-shifted by sixteen bits, and the LO16
7030 addend, sign extended. (Usually, the code does
7031 a `lui' of the HI16 value, and then an `addiu' of
7032 the LO16 value.)
7034 Scan ahead to find a matching LO16 relocation.
7036 According to the MIPS ELF ABI, the R_MIPS_LO16
7037 relocation must be immediately following.
7038 However, for the IRIX6 ABI, the next relocation
7039 may be a composed relocation consisting of
7040 several relocations for the same address. In
7041 that case, the R_MIPS_LO16 relocation may occur
7042 as one of these. We permit a similar extension
7043 in general, as that is useful for GCC. */
7044 lo16_relocation = mips_elf_next_relocation (input_bfd,
7045 lo16_type,
7046 rel, relend);
7047 if (lo16_relocation == NULL)
7048 return FALSE;
7050 lo16_location = contents + lo16_relocation->r_offset;
7052 /* Obtain the addend kept there. */
7053 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd,
7054 lo16_type, FALSE);
7055 _bfd_mips16_elf_reloc_unshuffle (input_bfd, lo16_type, FALSE,
7056 lo16_location);
7057 l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
7058 input_bfd, contents);
7059 _bfd_mips16_elf_reloc_shuffle (input_bfd, lo16_type, FALSE,
7060 lo16_location);
7061 l &= lo16_howto->src_mask;
7062 l <<= lo16_howto->rightshift;
7063 l = _bfd_mips_elf_sign_extend (l, 16);
7065 addend <<= 16;
7067 /* Compute the combined addend. */
7068 addend += l;
7070 else
7071 addend <<= howto->rightshift;
7073 else
7074 addend = rel->r_addend;
7077 if (info->relocatable)
7079 Elf_Internal_Sym *sym;
7080 unsigned long r_symndx;
7082 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
7083 && bfd_big_endian (input_bfd))
7084 rel->r_offset -= 4;
7086 /* Since we're just relocating, all we need to do is copy
7087 the relocations back out to the object file, unless
7088 they're against a section symbol, in which case we need
7089 to adjust by the section offset, or unless they're GP
7090 relative in which case we need to adjust by the amount
7091 that we're adjusting GP in this relocatable object. */
7093 if (! mips_elf_local_relocation_p (input_bfd, rel, local_sections,
7094 FALSE))
7095 /* There's nothing to do for non-local relocations. */
7096 continue;
7098 if (r_type == R_MIPS16_GPREL
7099 || r_type == R_MIPS_GPREL16
7100 || r_type == R_MIPS_GPREL32
7101 || r_type == R_MIPS_LITERAL)
7102 addend -= (_bfd_get_gp_value (output_bfd)
7103 - _bfd_get_gp_value (input_bfd));
7105 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
7106 sym = local_syms + r_symndx;
7107 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7108 /* Adjust the addend appropriately. */
7109 addend += local_sections[r_symndx]->output_offset;
7111 if (rela_relocation_p)
7112 /* If this is a RELA relocation, just update the addend. */
7113 rel->r_addend = addend;
7114 else
7116 if (r_type == R_MIPS_HI16
7117 || r_type == R_MIPS_GOT16)
7118 addend = mips_elf_high (addend);
7119 else if (r_type == R_MIPS_HIGHER)
7120 addend = mips_elf_higher (addend);
7121 else if (r_type == R_MIPS_HIGHEST)
7122 addend = mips_elf_highest (addend);
7123 else
7124 addend >>= howto->rightshift;
7126 /* We use the source mask, rather than the destination
7127 mask because the place to which we are writing will be
7128 source of the addend in the final link. */
7129 addend &= howto->src_mask;
7131 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
7132 /* See the comment above about using R_MIPS_64 in the 32-bit
7133 ABI. Here, we need to update the addend. It would be
7134 possible to get away with just using the R_MIPS_32 reloc
7135 but for endianness. */
7137 bfd_vma sign_bits;
7138 bfd_vma low_bits;
7139 bfd_vma high_bits;
7141 if (addend & ((bfd_vma) 1 << 31))
7142 #ifdef BFD64
7143 sign_bits = ((bfd_vma) 1 << 32) - 1;
7144 #else
7145 sign_bits = -1;
7146 #endif
7147 else
7148 sign_bits = 0;
7150 /* If we don't know that we have a 64-bit type,
7151 do two separate stores. */
7152 if (bfd_big_endian (input_bfd))
7154 /* Store the sign-bits (which are most significant)
7155 first. */
7156 low_bits = sign_bits;
7157 high_bits = addend;
7159 else
7161 low_bits = addend;
7162 high_bits = sign_bits;
7164 bfd_put_32 (input_bfd, low_bits,
7165 contents + rel->r_offset);
7166 bfd_put_32 (input_bfd, high_bits,
7167 contents + rel->r_offset + 4);
7168 continue;
7171 if (! mips_elf_perform_relocation (info, howto, rel, addend,
7172 input_bfd, input_section,
7173 contents, FALSE))
7174 return FALSE;
7177 /* Go on to the next relocation. */
7178 continue;
7181 /* In the N32 and 64-bit ABIs there may be multiple consecutive
7182 relocations for the same offset. In that case we are
7183 supposed to treat the output of each relocation as the addend
7184 for the next. */
7185 if (rel + 1 < relend
7186 && rel->r_offset == rel[1].r_offset
7187 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
7188 use_saved_addend_p = TRUE;
7189 else
7190 use_saved_addend_p = FALSE;
7192 /* Figure out what value we are supposed to relocate. */
7193 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
7194 input_section, info, rel,
7195 addend, howto, local_syms,
7196 local_sections, &value,
7197 &name, &require_jalx,
7198 use_saved_addend_p))
7200 case bfd_reloc_continue:
7201 /* There's nothing to do. */
7202 continue;
7204 case bfd_reloc_undefined:
7205 /* mips_elf_calculate_relocation already called the
7206 undefined_symbol callback. There's no real point in
7207 trying to perform the relocation at this point, so we
7208 just skip ahead to the next relocation. */
7209 continue;
7211 case bfd_reloc_notsupported:
7212 msg = _("internal error: unsupported relocation error");
7213 info->callbacks->warning
7214 (info, msg, name, input_bfd, input_section, rel->r_offset);
7215 return FALSE;
7217 case bfd_reloc_overflow:
7218 if (use_saved_addend_p)
7219 /* Ignore overflow until we reach the last relocation for
7220 a given location. */
7222 else
7224 BFD_ASSERT (name != NULL);
7225 if (! ((*info->callbacks->reloc_overflow)
7226 (info, NULL, name, howto->name, (bfd_vma) 0,
7227 input_bfd, input_section, rel->r_offset)))
7228 return FALSE;
7230 break;
7232 case bfd_reloc_ok:
7233 break;
7235 default:
7236 abort ();
7237 break;
7240 /* If we've got another relocation for the address, keep going
7241 until we reach the last one. */
7242 if (use_saved_addend_p)
7244 addend = value;
7245 continue;
7248 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
7249 /* See the comment above about using R_MIPS_64 in the 32-bit
7250 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
7251 that calculated the right value. Now, however, we
7252 sign-extend the 32-bit result to 64-bits, and store it as a
7253 64-bit value. We are especially generous here in that we
7254 go to extreme lengths to support this usage on systems with
7255 only a 32-bit VMA. */
7257 bfd_vma sign_bits;
7258 bfd_vma low_bits;
7259 bfd_vma high_bits;
7261 if (value & ((bfd_vma) 1 << 31))
7262 #ifdef BFD64
7263 sign_bits = ((bfd_vma) 1 << 32) - 1;
7264 #else
7265 sign_bits = -1;
7266 #endif
7267 else
7268 sign_bits = 0;
7270 /* If we don't know that we have a 64-bit type,
7271 do two separate stores. */
7272 if (bfd_big_endian (input_bfd))
7274 /* Undo what we did above. */
7275 rel->r_offset -= 4;
7276 /* Store the sign-bits (which are most significant)
7277 first. */
7278 low_bits = sign_bits;
7279 high_bits = value;
7281 else
7283 low_bits = value;
7284 high_bits = sign_bits;
7286 bfd_put_32 (input_bfd, low_bits,
7287 contents + rel->r_offset);
7288 bfd_put_32 (input_bfd, high_bits,
7289 contents + rel->r_offset + 4);
7290 continue;
7293 /* Actually perform the relocation. */
7294 if (! mips_elf_perform_relocation (info, howto, rel, value,
7295 input_bfd, input_section,
7296 contents, require_jalx))
7297 return FALSE;
7300 return TRUE;
7303 /* If NAME is one of the special IRIX6 symbols defined by the linker,
7304 adjust it appropriately now. */
7306 static void
7307 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7308 const char *name, Elf_Internal_Sym *sym)
7310 /* The linker script takes care of providing names and values for
7311 these, but we must place them into the right sections. */
7312 static const char* const text_section_symbols[] = {
7313 "_ftext",
7314 "_etext",
7315 "__dso_displacement",
7316 "__elf_header",
7317 "__program_header_table",
7318 NULL
7321 static const char* const data_section_symbols[] = {
7322 "_fdata",
7323 "_edata",
7324 "_end",
7325 "_fbss",
7326 NULL
7329 const char* const *p;
7330 int i;
7332 for (i = 0; i < 2; ++i)
7333 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7335 ++p)
7336 if (strcmp (*p, name) == 0)
7338 /* All of these symbols are given type STT_SECTION by the
7339 IRIX6 linker. */
7340 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7341 sym->st_other = STO_PROTECTED;
7343 /* The IRIX linker puts these symbols in special sections. */
7344 if (i == 0)
7345 sym->st_shndx = SHN_MIPS_TEXT;
7346 else
7347 sym->st_shndx = SHN_MIPS_DATA;
7349 break;
7353 /* Finish up dynamic symbol handling. We set the contents of various
7354 dynamic sections here. */
7356 bfd_boolean
7357 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
7358 struct bfd_link_info *info,
7359 struct elf_link_hash_entry *h,
7360 Elf_Internal_Sym *sym)
7362 bfd *dynobj;
7363 asection *sgot;
7364 struct mips_got_info *g, *gg;
7365 const char *name;
7367 dynobj = elf_hash_table (info)->dynobj;
7369 if (h->plt.offset != MINUS_ONE)
7371 asection *s;
7372 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7374 /* This symbol has a stub. Set it up. */
7376 BFD_ASSERT (h->dynindx != -1);
7378 s = bfd_get_section_by_name (dynobj,
7379 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7380 BFD_ASSERT (s != NULL);
7382 /* FIXME: Can h->dynindex be more than 64K? */
7383 if (h->dynindx & 0xffff0000)
7384 return FALSE;
7386 /* Fill the stub. */
7387 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
7388 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
7389 bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
7390 bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
7392 BFD_ASSERT (h->plt.offset <= s->size);
7393 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7395 /* Mark the symbol as undefined. plt.offset != -1 occurs
7396 only for the referenced symbol. */
7397 sym->st_shndx = SHN_UNDEF;
7399 /* The run-time linker uses the st_value field of the symbol
7400 to reset the global offset table entry for this external
7401 to its stub address when unlinking a shared object. */
7402 sym->st_value = (s->output_section->vma + s->output_offset
7403 + h->plt.offset);
7406 BFD_ASSERT (h->dynindx != -1
7407 || h->forced_local);
7409 sgot = mips_elf_got_section (dynobj, FALSE);
7410 BFD_ASSERT (sgot != NULL);
7411 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
7412 g = mips_elf_section_data (sgot)->u.got_info;
7413 BFD_ASSERT (g != NULL);
7415 /* Run through the global symbol table, creating GOT entries for all
7416 the symbols that need them. */
7417 if (g->global_gotsym != NULL
7418 && h->dynindx >= g->global_gotsym->dynindx)
7420 bfd_vma offset;
7421 bfd_vma value;
7423 value = sym->st_value;
7424 offset = mips_elf_global_got_index (dynobj, output_bfd, h, R_MIPS_GOT16, info);
7425 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
7428 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
7430 struct mips_got_entry e, *p;
7431 bfd_vma entry;
7432 bfd_vma offset;
7434 gg = g;
7436 e.abfd = output_bfd;
7437 e.symndx = -1;
7438 e.d.h = (struct mips_elf_link_hash_entry *)h;
7439 e.tls_type = 0;
7441 for (g = g->next; g->next != gg; g = g->next)
7443 if (g->got_entries
7444 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
7445 &e)))
7447 offset = p->gotidx;
7448 if (info->shared
7449 || (elf_hash_table (info)->dynamic_sections_created
7450 && p->d.h != NULL
7451 && p->d.h->root.def_dynamic
7452 && !p->d.h->root.def_regular))
7454 /* Create an R_MIPS_REL32 relocation for this entry. Due to
7455 the various compatibility problems, it's easier to mock
7456 up an R_MIPS_32 or R_MIPS_64 relocation and leave
7457 mips_elf_create_dynamic_relocation to calculate the
7458 appropriate addend. */
7459 Elf_Internal_Rela rel[3];
7461 memset (rel, 0, sizeof (rel));
7462 if (ABI_64_P (output_bfd))
7463 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
7464 else
7465 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
7466 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
7468 entry = 0;
7469 if (! (mips_elf_create_dynamic_relocation
7470 (output_bfd, info, rel,
7471 e.d.h, NULL, sym->st_value, &entry, sgot)))
7472 return FALSE;
7474 else
7475 entry = sym->st_value;
7476 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
7481 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7482 name = h->root.root.string;
7483 if (strcmp (name, "_DYNAMIC") == 0
7484 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7485 sym->st_shndx = SHN_ABS;
7486 else if (strcmp (name, "_DYNAMIC_LINK") == 0
7487 || strcmp (name, "_DYNAMIC_LINKING") == 0)
7489 sym->st_shndx = SHN_ABS;
7490 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7491 sym->st_value = 1;
7493 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
7495 sym->st_shndx = SHN_ABS;
7496 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7497 sym->st_value = elf_gp (output_bfd);
7499 else if (SGI_COMPAT (output_bfd))
7501 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7502 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7504 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7505 sym->st_other = STO_PROTECTED;
7506 sym->st_value = 0;
7507 sym->st_shndx = SHN_MIPS_DATA;
7509 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7511 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7512 sym->st_other = STO_PROTECTED;
7513 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7514 sym->st_shndx = SHN_ABS;
7516 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7518 if (h->type == STT_FUNC)
7519 sym->st_shndx = SHN_MIPS_TEXT;
7520 else if (h->type == STT_OBJECT)
7521 sym->st_shndx = SHN_MIPS_DATA;
7525 /* Handle the IRIX6-specific symbols. */
7526 if (IRIX_COMPAT (output_bfd) == ict_irix6)
7527 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
7529 if (! info->shared)
7531 if (! mips_elf_hash_table (info)->use_rld_obj_head
7532 && (strcmp (name, "__rld_map") == 0
7533 || strcmp (name, "__RLD_MAP") == 0))
7535 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7536 BFD_ASSERT (s != NULL);
7537 sym->st_value = s->output_section->vma + s->output_offset;
7538 bfd_put_32 (output_bfd, 0, s->contents);
7539 if (mips_elf_hash_table (info)->rld_value == 0)
7540 mips_elf_hash_table (info)->rld_value = sym->st_value;
7542 else if (mips_elf_hash_table (info)->use_rld_obj_head
7543 && strcmp (name, "__rld_obj_head") == 0)
7545 /* IRIX6 does not use a .rld_map section. */
7546 if (IRIX_COMPAT (output_bfd) == ict_irix5
7547 || IRIX_COMPAT (output_bfd) == ict_none)
7548 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
7549 != NULL);
7550 mips_elf_hash_table (info)->rld_value = sym->st_value;
7554 /* If this is a mips16 symbol, force the value to be even. */
7555 if (sym->st_other == STO_MIPS16)
7556 sym->st_value &= ~1;
7558 return TRUE;
7561 /* Finish up the dynamic sections. */
7563 bfd_boolean
7564 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
7565 struct bfd_link_info *info)
7567 bfd *dynobj;
7568 asection *sdyn;
7569 asection *sgot;
7570 struct mips_got_info *gg, *g;
7572 dynobj = elf_hash_table (info)->dynobj;
7574 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7576 sgot = mips_elf_got_section (dynobj, FALSE);
7577 if (sgot == NULL)
7578 gg = g = NULL;
7579 else
7581 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
7582 gg = mips_elf_section_data (sgot)->u.got_info;
7583 BFD_ASSERT (gg != NULL);
7584 g = mips_elf_got_for_ibfd (gg, output_bfd);
7585 BFD_ASSERT (g != NULL);
7588 if (elf_hash_table (info)->dynamic_sections_created)
7590 bfd_byte *b;
7592 BFD_ASSERT (sdyn != NULL);
7593 BFD_ASSERT (g != NULL);
7595 for (b = sdyn->contents;
7596 b < sdyn->contents + sdyn->size;
7597 b += MIPS_ELF_DYN_SIZE (dynobj))
7599 Elf_Internal_Dyn dyn;
7600 const char *name;
7601 size_t elemsize;
7602 asection *s;
7603 bfd_boolean swap_out_p;
7605 /* Read in the current dynamic entry. */
7606 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
7608 /* Assume that we're going to modify it and write it out. */
7609 swap_out_p = TRUE;
7611 switch (dyn.d_tag)
7613 case DT_RELENT:
7614 s = mips_elf_rel_dyn_section (dynobj, FALSE);
7615 BFD_ASSERT (s != NULL);
7616 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
7617 break;
7619 case DT_STRSZ:
7620 /* Rewrite DT_STRSZ. */
7621 dyn.d_un.d_val =
7622 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7623 break;
7625 case DT_PLTGOT:
7626 name = ".got";
7627 s = bfd_get_section_by_name (output_bfd, name);
7628 BFD_ASSERT (s != NULL);
7629 dyn.d_un.d_ptr = s->vma;
7630 break;
7632 case DT_MIPS_RLD_VERSION:
7633 dyn.d_un.d_val = 1; /* XXX */
7634 break;
7636 case DT_MIPS_FLAGS:
7637 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7638 break;
7640 case DT_MIPS_TIME_STAMP:
7641 time ((time_t *) &dyn.d_un.d_val);
7642 break;
7644 case DT_MIPS_ICHECKSUM:
7645 /* XXX FIXME: */
7646 swap_out_p = FALSE;
7647 break;
7649 case DT_MIPS_IVERSION:
7650 /* XXX FIXME: */
7651 swap_out_p = FALSE;
7652 break;
7654 case DT_MIPS_BASE_ADDRESS:
7655 s = output_bfd->sections;
7656 BFD_ASSERT (s != NULL);
7657 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
7658 break;
7660 case DT_MIPS_LOCAL_GOTNO:
7661 dyn.d_un.d_val = g->local_gotno;
7662 break;
7664 case DT_MIPS_UNREFEXTNO:
7665 /* The index into the dynamic symbol table which is the
7666 entry of the first external symbol that is not
7667 referenced within the same object. */
7668 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
7669 break;
7671 case DT_MIPS_GOTSYM:
7672 if (gg->global_gotsym)
7674 dyn.d_un.d_val = gg->global_gotsym->dynindx;
7675 break;
7677 /* In case if we don't have global got symbols we default
7678 to setting DT_MIPS_GOTSYM to the same value as
7679 DT_MIPS_SYMTABNO, so we just fall through. */
7681 case DT_MIPS_SYMTABNO:
7682 name = ".dynsym";
7683 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
7684 s = bfd_get_section_by_name (output_bfd, name);
7685 BFD_ASSERT (s != NULL);
7687 dyn.d_un.d_val = s->size / elemsize;
7688 break;
7690 case DT_MIPS_HIPAGENO:
7691 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7692 break;
7694 case DT_MIPS_RLD_MAP:
7695 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7696 break;
7698 case DT_MIPS_OPTIONS:
7699 s = (bfd_get_section_by_name
7700 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
7701 dyn.d_un.d_ptr = s->vma;
7702 break;
7704 default:
7705 swap_out_p = FALSE;
7706 break;
7709 if (swap_out_p)
7710 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
7711 (dynobj, &dyn, b);
7715 /* The first entry of the global offset table will be filled at
7716 runtime. The second entry will be used by some runtime loaders.
7717 This isn't the case of IRIX rld. */
7718 if (sgot != NULL && sgot->size > 0)
7720 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents);
7721 MIPS_ELF_PUT_WORD (output_bfd, 0x80000000,
7722 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
7725 if (sgot != NULL)
7726 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
7727 = MIPS_ELF_GOT_SIZE (output_bfd);
7729 /* Generate dynamic relocations for the non-primary gots. */
7730 if (gg != NULL && gg->next)
7732 Elf_Internal_Rela rel[3];
7733 bfd_vma addend = 0;
7735 memset (rel, 0, sizeof (rel));
7736 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
7738 for (g = gg->next; g->next != gg; g = g->next)
7740 bfd_vma index = g->next->local_gotno + g->next->global_gotno
7741 + g->next->tls_gotno;
7743 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
7744 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
7745 MIPS_ELF_PUT_WORD (output_bfd, 0x80000000, sgot->contents
7746 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
7748 if (! info->shared)
7749 continue;
7751 while (index < g->assigned_gotno)
7753 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
7754 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
7755 if (!(mips_elf_create_dynamic_relocation
7756 (output_bfd, info, rel, NULL,
7757 bfd_abs_section_ptr,
7758 0, &addend, sgot)))
7759 return FALSE;
7760 BFD_ASSERT (addend == 0);
7765 /* The generation of dynamic relocations for the non-primary gots
7766 adds more dynamic relocations. We cannot count them until
7767 here. */
7769 if (elf_hash_table (info)->dynamic_sections_created)
7771 bfd_byte *b;
7772 bfd_boolean swap_out_p;
7774 BFD_ASSERT (sdyn != NULL);
7776 for (b = sdyn->contents;
7777 b < sdyn->contents + sdyn->size;
7778 b += MIPS_ELF_DYN_SIZE (dynobj))
7780 Elf_Internal_Dyn dyn;
7781 asection *s;
7783 /* Read in the current dynamic entry. */
7784 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
7786 /* Assume that we're going to modify it and write it out. */
7787 swap_out_p = TRUE;
7789 switch (dyn.d_tag)
7791 case DT_RELSZ:
7792 /* Reduce DT_RELSZ to account for any relocations we
7793 decided not to make. This is for the n64 irix rld,
7794 which doesn't seem to apply any relocations if there
7795 are trailing null entries. */
7796 s = mips_elf_rel_dyn_section (dynobj, FALSE);
7797 dyn.d_un.d_val = (s->reloc_count
7798 * (ABI_64_P (output_bfd)
7799 ? sizeof (Elf64_Mips_External_Rel)
7800 : sizeof (Elf32_External_Rel)));
7801 break;
7803 default:
7804 swap_out_p = FALSE;
7805 break;
7808 if (swap_out_p)
7809 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
7810 (dynobj, &dyn, b);
7815 asection *s;
7816 Elf32_compact_rel cpt;
7818 if (SGI_COMPAT (output_bfd))
7820 /* Write .compact_rel section out. */
7821 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7822 if (s != NULL)
7824 cpt.id1 = 1;
7825 cpt.num = s->reloc_count;
7826 cpt.id2 = 2;
7827 cpt.offset = (s->output_section->filepos
7828 + sizeof (Elf32_External_compact_rel));
7829 cpt.reserved0 = 0;
7830 cpt.reserved1 = 0;
7831 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7832 ((Elf32_External_compact_rel *)
7833 s->contents));
7835 /* Clean up a dummy stub function entry in .text. */
7836 s = bfd_get_section_by_name (dynobj,
7837 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7838 if (s != NULL)
7840 file_ptr dummy_offset;
7842 BFD_ASSERT (s->size >= MIPS_FUNCTION_STUB_SIZE);
7843 dummy_offset = s->size - MIPS_FUNCTION_STUB_SIZE;
7844 memset (s->contents + dummy_offset, 0,
7845 MIPS_FUNCTION_STUB_SIZE);
7850 /* We need to sort the entries of the dynamic relocation section. */
7852 s = mips_elf_rel_dyn_section (dynobj, FALSE);
7854 if (s != NULL
7855 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
7857 reldyn_sorting_bfd = output_bfd;
7859 if (ABI_64_P (output_bfd))
7860 qsort ((Elf64_External_Rel *) s->contents + 1, s->reloc_count - 1,
7861 sizeof (Elf64_Mips_External_Rel), sort_dynamic_relocs_64);
7862 else
7863 qsort ((Elf32_External_Rel *) s->contents + 1, s->reloc_count - 1,
7864 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
7868 return TRUE;
7872 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
7874 static void
7875 mips_set_isa_flags (bfd *abfd)
7877 flagword val;
7879 switch (bfd_get_mach (abfd))
7881 default:
7882 case bfd_mach_mips3000:
7883 val = E_MIPS_ARCH_1;
7884 break;
7886 case bfd_mach_mips3900:
7887 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
7888 break;
7890 case bfd_mach_mips6000:
7891 val = E_MIPS_ARCH_2;
7892 break;
7894 case bfd_mach_mips4000:
7895 case bfd_mach_mips4300:
7896 case bfd_mach_mips4400:
7897 case bfd_mach_mips4600:
7898 val = E_MIPS_ARCH_3;
7899 break;
7901 case bfd_mach_mips4010:
7902 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
7903 break;
7905 case bfd_mach_mips4100:
7906 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
7907 break;
7909 case bfd_mach_mips4111:
7910 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
7911 break;
7913 case bfd_mach_mips4120:
7914 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
7915 break;
7917 case bfd_mach_mips4650:
7918 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
7919 break;
7921 case bfd_mach_mips5400:
7922 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
7923 break;
7925 case bfd_mach_mips5500:
7926 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
7927 break;
7929 case bfd_mach_mips9000:
7930 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
7931 break;
7933 case bfd_mach_mips5000:
7934 case bfd_mach_mips7000:
7935 case bfd_mach_mips8000:
7936 case bfd_mach_mips10000:
7937 case bfd_mach_mips12000:
7938 val = E_MIPS_ARCH_4;
7939 break;
7941 case bfd_mach_mips5:
7942 val = E_MIPS_ARCH_5;
7943 break;
7945 case bfd_mach_mips_sb1:
7946 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
7947 break;
7949 case bfd_mach_mipsisa32:
7950 val = E_MIPS_ARCH_32;
7951 break;
7953 case bfd_mach_mipsisa64:
7954 val = E_MIPS_ARCH_64;
7955 break;
7957 case bfd_mach_mipsisa32r2:
7958 val = E_MIPS_ARCH_32R2;
7959 break;
7961 case bfd_mach_mipsisa64r2:
7962 val = E_MIPS_ARCH_64R2;
7963 break;
7965 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7966 elf_elfheader (abfd)->e_flags |= val;
7971 /* The final processing done just before writing out a MIPS ELF object
7972 file. This gets the MIPS architecture right based on the machine
7973 number. This is used by both the 32-bit and the 64-bit ABI. */
7975 void
7976 _bfd_mips_elf_final_write_processing (bfd *abfd,
7977 bfd_boolean linker ATTRIBUTE_UNUSED)
7979 unsigned int i;
7980 Elf_Internal_Shdr **hdrpp;
7981 const char *name;
7982 asection *sec;
7984 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
7985 is nonzero. This is for compatibility with old objects, which used
7986 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
7987 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
7988 mips_set_isa_flags (abfd);
7990 /* Set the sh_info field for .gptab sections and other appropriate
7991 info for each special section. */
7992 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
7993 i < elf_numsections (abfd);
7994 i++, hdrpp++)
7996 switch ((*hdrpp)->sh_type)
7998 case SHT_MIPS_MSYM:
7999 case SHT_MIPS_LIBLIST:
8000 sec = bfd_get_section_by_name (abfd, ".dynstr");
8001 if (sec != NULL)
8002 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8003 break;
8005 case SHT_MIPS_GPTAB:
8006 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
8007 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
8008 BFD_ASSERT (name != NULL
8009 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
8010 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
8011 BFD_ASSERT (sec != NULL);
8012 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
8013 break;
8015 case SHT_MIPS_CONTENT:
8016 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
8017 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
8018 BFD_ASSERT (name != NULL
8019 && strncmp (name, ".MIPS.content",
8020 sizeof ".MIPS.content" - 1) == 0);
8021 sec = bfd_get_section_by_name (abfd,
8022 name + sizeof ".MIPS.content" - 1);
8023 BFD_ASSERT (sec != NULL);
8024 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8025 break;
8027 case SHT_MIPS_SYMBOL_LIB:
8028 sec = bfd_get_section_by_name (abfd, ".dynsym");
8029 if (sec != NULL)
8030 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8031 sec = bfd_get_section_by_name (abfd, ".liblist");
8032 if (sec != NULL)
8033 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
8034 break;
8036 case SHT_MIPS_EVENTS:
8037 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
8038 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
8039 BFD_ASSERT (name != NULL);
8040 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
8041 sec = bfd_get_section_by_name (abfd,
8042 name + sizeof ".MIPS.events" - 1);
8043 else
8045 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
8046 sizeof ".MIPS.post_rel" - 1) == 0);
8047 sec = bfd_get_section_by_name (abfd,
8048 (name
8049 + sizeof ".MIPS.post_rel" - 1));
8051 BFD_ASSERT (sec != NULL);
8052 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8053 break;
8059 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
8060 segments. */
8063 _bfd_mips_elf_additional_program_headers (bfd *abfd)
8065 asection *s;
8066 int ret = 0;
8068 /* See if we need a PT_MIPS_REGINFO segment. */
8069 s = bfd_get_section_by_name (abfd, ".reginfo");
8070 if (s && (s->flags & SEC_LOAD))
8071 ++ret;
8073 /* See if we need a PT_MIPS_OPTIONS segment. */
8074 if (IRIX_COMPAT (abfd) == ict_irix6
8075 && bfd_get_section_by_name (abfd,
8076 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
8077 ++ret;
8079 /* See if we need a PT_MIPS_RTPROC segment. */
8080 if (IRIX_COMPAT (abfd) == ict_irix5
8081 && bfd_get_section_by_name (abfd, ".dynamic")
8082 && bfd_get_section_by_name (abfd, ".mdebug"))
8083 ++ret;
8085 return ret;
8088 /* Modify the segment map for an IRIX5 executable. */
8090 bfd_boolean
8091 _bfd_mips_elf_modify_segment_map (bfd *abfd,
8092 struct bfd_link_info *info ATTRIBUTE_UNUSED)
8094 asection *s;
8095 struct elf_segment_map *m, **pm;
8096 bfd_size_type amt;
8098 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
8099 segment. */
8100 s = bfd_get_section_by_name (abfd, ".reginfo");
8101 if (s != NULL && (s->flags & SEC_LOAD) != 0)
8103 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8104 if (m->p_type == PT_MIPS_REGINFO)
8105 break;
8106 if (m == NULL)
8108 amt = sizeof *m;
8109 m = bfd_zalloc (abfd, amt);
8110 if (m == NULL)
8111 return FALSE;
8113 m->p_type = PT_MIPS_REGINFO;
8114 m->count = 1;
8115 m->sections[0] = s;
8117 /* We want to put it after the PHDR and INTERP segments. */
8118 pm = &elf_tdata (abfd)->segment_map;
8119 while (*pm != NULL
8120 && ((*pm)->p_type == PT_PHDR
8121 || (*pm)->p_type == PT_INTERP))
8122 pm = &(*pm)->next;
8124 m->next = *pm;
8125 *pm = m;
8129 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
8130 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
8131 PT_MIPS_OPTIONS segment immediately following the program header
8132 table. */
8133 if (NEWABI_P (abfd)
8134 /* On non-IRIX6 new abi, we'll have already created a segment
8135 for this section, so don't create another. I'm not sure this
8136 is not also the case for IRIX 6, but I can't test it right
8137 now. */
8138 && IRIX_COMPAT (abfd) == ict_irix6)
8140 for (s = abfd->sections; s; s = s->next)
8141 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
8142 break;
8144 if (s)
8146 struct elf_segment_map *options_segment;
8148 pm = &elf_tdata (abfd)->segment_map;
8149 while (*pm != NULL
8150 && ((*pm)->p_type == PT_PHDR
8151 || (*pm)->p_type == PT_INTERP))
8152 pm = &(*pm)->next;
8154 amt = sizeof (struct elf_segment_map);
8155 options_segment = bfd_zalloc (abfd, amt);
8156 options_segment->next = *pm;
8157 options_segment->p_type = PT_MIPS_OPTIONS;
8158 options_segment->p_flags = PF_R;
8159 options_segment->p_flags_valid = TRUE;
8160 options_segment->count = 1;
8161 options_segment->sections[0] = s;
8162 *pm = options_segment;
8165 else
8167 if (IRIX_COMPAT (abfd) == ict_irix5)
8169 /* If there are .dynamic and .mdebug sections, we make a room
8170 for the RTPROC header. FIXME: Rewrite without section names. */
8171 if (bfd_get_section_by_name (abfd, ".interp") == NULL
8172 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
8173 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
8175 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8176 if (m->p_type == PT_MIPS_RTPROC)
8177 break;
8178 if (m == NULL)
8180 amt = sizeof *m;
8181 m = bfd_zalloc (abfd, amt);
8182 if (m == NULL)
8183 return FALSE;
8185 m->p_type = PT_MIPS_RTPROC;
8187 s = bfd_get_section_by_name (abfd, ".rtproc");
8188 if (s == NULL)
8190 m->count = 0;
8191 m->p_flags = 0;
8192 m->p_flags_valid = 1;
8194 else
8196 m->count = 1;
8197 m->sections[0] = s;
8200 /* We want to put it after the DYNAMIC segment. */
8201 pm = &elf_tdata (abfd)->segment_map;
8202 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
8203 pm = &(*pm)->next;
8204 if (*pm != NULL)
8205 pm = &(*pm)->next;
8207 m->next = *pm;
8208 *pm = m;
8212 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
8213 .dynstr, .dynsym, and .hash sections, and everything in
8214 between. */
8215 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
8216 pm = &(*pm)->next)
8217 if ((*pm)->p_type == PT_DYNAMIC)
8218 break;
8219 m = *pm;
8220 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
8222 /* For a normal mips executable the permissions for the PT_DYNAMIC
8223 segment are read, write and execute. We do that here since
8224 the code in elf.c sets only the read permission. This matters
8225 sometimes for the dynamic linker. */
8226 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
8228 m->p_flags = PF_R | PF_W | PF_X;
8229 m->p_flags_valid = 1;
8232 if (m != NULL
8233 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
8235 static const char *sec_names[] =
8237 ".dynamic", ".dynstr", ".dynsym", ".hash"
8239 bfd_vma low, high;
8240 unsigned int i, c;
8241 struct elf_segment_map *n;
8243 low = ~(bfd_vma) 0;
8244 high = 0;
8245 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
8247 s = bfd_get_section_by_name (abfd, sec_names[i]);
8248 if (s != NULL && (s->flags & SEC_LOAD) != 0)
8250 bfd_size_type sz;
8252 if (low > s->vma)
8253 low = s->vma;
8254 sz = s->size;
8255 if (high < s->vma + sz)
8256 high = s->vma + sz;
8260 c = 0;
8261 for (s = abfd->sections; s != NULL; s = s->next)
8262 if ((s->flags & SEC_LOAD) != 0
8263 && s->vma >= low
8264 && s->vma + s->size <= high)
8265 ++c;
8267 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
8268 n = bfd_zalloc (abfd, amt);
8269 if (n == NULL)
8270 return FALSE;
8271 *n = *m;
8272 n->count = c;
8274 i = 0;
8275 for (s = abfd->sections; s != NULL; s = s->next)
8277 if ((s->flags & SEC_LOAD) != 0
8278 && s->vma >= low
8279 && s->vma + s->size <= high)
8281 n->sections[i] = s;
8282 ++i;
8286 *pm = n;
8290 return TRUE;
8293 /* Return the section that should be marked against GC for a given
8294 relocation. */
8296 asection *
8297 _bfd_mips_elf_gc_mark_hook (asection *sec,
8298 struct bfd_link_info *info ATTRIBUTE_UNUSED,
8299 Elf_Internal_Rela *rel,
8300 struct elf_link_hash_entry *h,
8301 Elf_Internal_Sym *sym)
8303 /* ??? Do mips16 stub sections need to be handled special? */
8305 if (h != NULL)
8307 switch (ELF_R_TYPE (sec->owner, rel->r_info))
8309 case R_MIPS_GNU_VTINHERIT:
8310 case R_MIPS_GNU_VTENTRY:
8311 break;
8313 default:
8314 switch (h->root.type)
8316 case bfd_link_hash_defined:
8317 case bfd_link_hash_defweak:
8318 return h->root.u.def.section;
8320 case bfd_link_hash_common:
8321 return h->root.u.c.p->section;
8323 default:
8324 break;
8328 else
8329 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
8331 return NULL;
8334 /* Update the got entry reference counts for the section being removed. */
8336 bfd_boolean
8337 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
8338 struct bfd_link_info *info ATTRIBUTE_UNUSED,
8339 asection *sec ATTRIBUTE_UNUSED,
8340 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
8342 #if 0
8343 Elf_Internal_Shdr *symtab_hdr;
8344 struct elf_link_hash_entry **sym_hashes;
8345 bfd_signed_vma *local_got_refcounts;
8346 const Elf_Internal_Rela *rel, *relend;
8347 unsigned long r_symndx;
8348 struct elf_link_hash_entry *h;
8350 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8351 sym_hashes = elf_sym_hashes (abfd);
8352 local_got_refcounts = elf_local_got_refcounts (abfd);
8354 relend = relocs + sec->reloc_count;
8355 for (rel = relocs; rel < relend; rel++)
8356 switch (ELF_R_TYPE (abfd, rel->r_info))
8358 case R_MIPS_GOT16:
8359 case R_MIPS_CALL16:
8360 case R_MIPS_CALL_HI16:
8361 case R_MIPS_CALL_LO16:
8362 case R_MIPS_GOT_HI16:
8363 case R_MIPS_GOT_LO16:
8364 case R_MIPS_GOT_DISP:
8365 case R_MIPS_GOT_PAGE:
8366 case R_MIPS_GOT_OFST:
8367 /* ??? It would seem that the existing MIPS code does no sort
8368 of reference counting or whatnot on its GOT and PLT entries,
8369 so it is not possible to garbage collect them at this time. */
8370 break;
8372 default:
8373 break;
8375 #endif
8377 return TRUE;
8380 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
8381 hiding the old indirect symbol. Process additional relocation
8382 information. Also called for weakdefs, in which case we just let
8383 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
8385 void
8386 _bfd_mips_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
8387 struct elf_link_hash_entry *dir,
8388 struct elf_link_hash_entry *ind)
8390 struct mips_elf_link_hash_entry *dirmips, *indmips;
8392 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
8394 if (ind->root.type != bfd_link_hash_indirect)
8395 return;
8397 dirmips = (struct mips_elf_link_hash_entry *) dir;
8398 indmips = (struct mips_elf_link_hash_entry *) ind;
8399 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
8400 if (indmips->readonly_reloc)
8401 dirmips->readonly_reloc = TRUE;
8402 if (indmips->no_fn_stub)
8403 dirmips->no_fn_stub = TRUE;
8405 if (dirmips->tls_type == 0)
8406 dirmips->tls_type = indmips->tls_type;
8407 else
8408 BFD_ASSERT (indmips->tls_type == 0);
8411 void
8412 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
8413 struct elf_link_hash_entry *entry,
8414 bfd_boolean force_local)
8416 bfd *dynobj;
8417 asection *got;
8418 struct mips_got_info *g;
8419 struct mips_elf_link_hash_entry *h;
8421 h = (struct mips_elf_link_hash_entry *) entry;
8422 if (h->forced_local)
8423 return;
8424 h->forced_local = force_local;
8426 dynobj = elf_hash_table (info)->dynobj;
8427 if (dynobj != NULL && force_local && h->root.type != STT_TLS
8428 && (got = mips_elf_got_section (dynobj, FALSE)) != NULL
8429 && (g = mips_elf_section_data (got)->u.got_info) != NULL)
8431 if (g->next)
8433 struct mips_got_entry e;
8434 struct mips_got_info *gg = g;
8436 /* Since we're turning what used to be a global symbol into a
8437 local one, bump up the number of local entries of each GOT
8438 that had an entry for it. This will automatically decrease
8439 the number of global entries, since global_gotno is actually
8440 the upper limit of global entries. */
8441 e.abfd = dynobj;
8442 e.symndx = -1;
8443 e.d.h = h;
8444 e.tls_type = 0;
8446 for (g = g->next; g != gg; g = g->next)
8447 if (htab_find (g->got_entries, &e))
8449 BFD_ASSERT (g->global_gotno > 0);
8450 g->local_gotno++;
8451 g->global_gotno--;
8454 /* If this was a global symbol forced into the primary GOT, we
8455 no longer need an entry for it. We can't release the entry
8456 at this point, but we must at least stop counting it as one
8457 of the symbols that required a forced got entry. */
8458 if (h->root.got.offset == 2)
8460 BFD_ASSERT (gg->assigned_gotno > 0);
8461 gg->assigned_gotno--;
8464 else if (g->global_gotno == 0 && g->global_gotsym == NULL)
8465 /* If we haven't got through GOT allocation yet, just bump up the
8466 number of local entries, as this symbol won't be counted as
8467 global. */
8468 g->local_gotno++;
8469 else if (h->root.got.offset == 1)
8471 /* If we're past non-multi-GOT allocation and this symbol had
8472 been marked for a global got entry, give it a local entry
8473 instead. */
8474 BFD_ASSERT (g->global_gotno > 0);
8475 g->local_gotno++;
8476 g->global_gotno--;
8480 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
8483 #define PDR_SIZE 32
8485 bfd_boolean
8486 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
8487 struct bfd_link_info *info)
8489 asection *o;
8490 bfd_boolean ret = FALSE;
8491 unsigned char *tdata;
8492 size_t i, skip;
8494 o = bfd_get_section_by_name (abfd, ".pdr");
8495 if (! o)
8496 return FALSE;
8497 if (o->size == 0)
8498 return FALSE;
8499 if (o->size % PDR_SIZE != 0)
8500 return FALSE;
8501 if (o->output_section != NULL
8502 && bfd_is_abs_section (o->output_section))
8503 return FALSE;
8505 tdata = bfd_zmalloc (o->size / PDR_SIZE);
8506 if (! tdata)
8507 return FALSE;
8509 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8510 info->keep_memory);
8511 if (!cookie->rels)
8513 free (tdata);
8514 return FALSE;
8517 cookie->rel = cookie->rels;
8518 cookie->relend = cookie->rels + o->reloc_count;
8520 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
8522 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
8524 tdata[i] = 1;
8525 skip ++;
8529 if (skip != 0)
8531 mips_elf_section_data (o)->u.tdata = tdata;
8532 o->size -= skip * PDR_SIZE;
8533 ret = TRUE;
8535 else
8536 free (tdata);
8538 if (! info->keep_memory)
8539 free (cookie->rels);
8541 return ret;
8544 bfd_boolean
8545 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
8547 if (strcmp (sec->name, ".pdr") == 0)
8548 return TRUE;
8549 return FALSE;
8552 bfd_boolean
8553 _bfd_mips_elf_write_section (bfd *output_bfd, asection *sec,
8554 bfd_byte *contents)
8556 bfd_byte *to, *from, *end;
8557 int i;
8559 if (strcmp (sec->name, ".pdr") != 0)
8560 return FALSE;
8562 if (mips_elf_section_data (sec)->u.tdata == NULL)
8563 return FALSE;
8565 to = contents;
8566 end = contents + sec->size;
8567 for (from = contents, i = 0;
8568 from < end;
8569 from += PDR_SIZE, i++)
8571 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
8572 continue;
8573 if (to != from)
8574 memcpy (to, from, PDR_SIZE);
8575 to += PDR_SIZE;
8577 bfd_set_section_contents (output_bfd, sec->output_section, contents,
8578 sec->output_offset, sec->size);
8579 return TRUE;
8582 /* MIPS ELF uses a special find_nearest_line routine in order the
8583 handle the ECOFF debugging information. */
8585 struct mips_elf_find_line
8587 struct ecoff_debug_info d;
8588 struct ecoff_find_line i;
8591 bfd_boolean
8592 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
8593 asymbol **symbols, bfd_vma offset,
8594 const char **filename_ptr,
8595 const char **functionname_ptr,
8596 unsigned int *line_ptr)
8598 asection *msec;
8600 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
8601 filename_ptr, functionname_ptr,
8602 line_ptr))
8603 return TRUE;
8605 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
8606 filename_ptr, functionname_ptr,
8607 line_ptr, ABI_64_P (abfd) ? 8 : 0,
8608 &elf_tdata (abfd)->dwarf2_find_line_info))
8609 return TRUE;
8611 msec = bfd_get_section_by_name (abfd, ".mdebug");
8612 if (msec != NULL)
8614 flagword origflags;
8615 struct mips_elf_find_line *fi;
8616 const struct ecoff_debug_swap * const swap =
8617 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
8619 /* If we are called during a link, mips_elf_final_link may have
8620 cleared the SEC_HAS_CONTENTS field. We force it back on here
8621 if appropriate (which it normally will be). */
8622 origflags = msec->flags;
8623 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
8624 msec->flags |= SEC_HAS_CONTENTS;
8626 fi = elf_tdata (abfd)->find_line_info;
8627 if (fi == NULL)
8629 bfd_size_type external_fdr_size;
8630 char *fraw_src;
8631 char *fraw_end;
8632 struct fdr *fdr_ptr;
8633 bfd_size_type amt = sizeof (struct mips_elf_find_line);
8635 fi = bfd_zalloc (abfd, amt);
8636 if (fi == NULL)
8638 msec->flags = origflags;
8639 return FALSE;
8642 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
8644 msec->flags = origflags;
8645 return FALSE;
8648 /* Swap in the FDR information. */
8649 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
8650 fi->d.fdr = bfd_alloc (abfd, amt);
8651 if (fi->d.fdr == NULL)
8653 msec->flags = origflags;
8654 return FALSE;
8656 external_fdr_size = swap->external_fdr_size;
8657 fdr_ptr = fi->d.fdr;
8658 fraw_src = (char *) fi->d.external_fdr;
8659 fraw_end = (fraw_src
8660 + fi->d.symbolic_header.ifdMax * external_fdr_size);
8661 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
8662 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
8664 elf_tdata (abfd)->find_line_info = fi;
8666 /* Note that we don't bother to ever free this information.
8667 find_nearest_line is either called all the time, as in
8668 objdump -l, so the information should be saved, or it is
8669 rarely called, as in ld error messages, so the memory
8670 wasted is unimportant. Still, it would probably be a
8671 good idea for free_cached_info to throw it away. */
8674 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
8675 &fi->i, filename_ptr, functionname_ptr,
8676 line_ptr))
8678 msec->flags = origflags;
8679 return TRUE;
8682 msec->flags = origflags;
8685 /* Fall back on the generic ELF find_nearest_line routine. */
8687 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
8688 filename_ptr, functionname_ptr,
8689 line_ptr);
8692 bfd_boolean
8693 _bfd_mips_elf_find_inliner_info (bfd *abfd,
8694 const char **filename_ptr,
8695 const char **functionname_ptr,
8696 unsigned int *line_ptr)
8698 bfd_boolean found;
8699 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8700 functionname_ptr, line_ptr,
8701 & elf_tdata (abfd)->dwarf2_find_line_info);
8702 return found;
8706 /* When are writing out the .options or .MIPS.options section,
8707 remember the bytes we are writing out, so that we can install the
8708 GP value in the section_processing routine. */
8710 bfd_boolean
8711 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
8712 const void *location,
8713 file_ptr offset, bfd_size_type count)
8715 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
8717 bfd_byte *c;
8719 if (elf_section_data (section) == NULL)
8721 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
8722 section->used_by_bfd = bfd_zalloc (abfd, amt);
8723 if (elf_section_data (section) == NULL)
8724 return FALSE;
8726 c = mips_elf_section_data (section)->u.tdata;
8727 if (c == NULL)
8729 c = bfd_zalloc (abfd, section->size);
8730 if (c == NULL)
8731 return FALSE;
8732 mips_elf_section_data (section)->u.tdata = c;
8735 memcpy (c + offset, location, count);
8738 return _bfd_elf_set_section_contents (abfd, section, location, offset,
8739 count);
8742 /* This is almost identical to bfd_generic_get_... except that some
8743 MIPS relocations need to be handled specially. Sigh. */
8745 bfd_byte *
8746 _bfd_elf_mips_get_relocated_section_contents
8747 (bfd *abfd,
8748 struct bfd_link_info *link_info,
8749 struct bfd_link_order *link_order,
8750 bfd_byte *data,
8751 bfd_boolean relocatable,
8752 asymbol **symbols)
8754 /* Get enough memory to hold the stuff */
8755 bfd *input_bfd = link_order->u.indirect.section->owner;
8756 asection *input_section = link_order->u.indirect.section;
8757 bfd_size_type sz;
8759 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8760 arelent **reloc_vector = NULL;
8761 long reloc_count;
8763 if (reloc_size < 0)
8764 goto error_return;
8766 reloc_vector = bfd_malloc (reloc_size);
8767 if (reloc_vector == NULL && reloc_size != 0)
8768 goto error_return;
8770 /* read in the section */
8771 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
8772 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
8773 goto error_return;
8775 reloc_count = bfd_canonicalize_reloc (input_bfd,
8776 input_section,
8777 reloc_vector,
8778 symbols);
8779 if (reloc_count < 0)
8780 goto error_return;
8782 if (reloc_count > 0)
8784 arelent **parent;
8785 /* for mips */
8786 int gp_found;
8787 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8790 struct bfd_hash_entry *h;
8791 struct bfd_link_hash_entry *lh;
8792 /* Skip all this stuff if we aren't mixing formats. */
8793 if (abfd && input_bfd
8794 && abfd->xvec == input_bfd->xvec)
8795 lh = 0;
8796 else
8798 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
8799 lh = (struct bfd_link_hash_entry *) h;
8801 lookup:
8802 if (lh)
8804 switch (lh->type)
8806 case bfd_link_hash_undefined:
8807 case bfd_link_hash_undefweak:
8808 case bfd_link_hash_common:
8809 gp_found = 0;
8810 break;
8811 case bfd_link_hash_defined:
8812 case bfd_link_hash_defweak:
8813 gp_found = 1;
8814 gp = lh->u.def.value;
8815 break;
8816 case bfd_link_hash_indirect:
8817 case bfd_link_hash_warning:
8818 lh = lh->u.i.link;
8819 /* @@FIXME ignoring warning for now */
8820 goto lookup;
8821 case bfd_link_hash_new:
8822 default:
8823 abort ();
8826 else
8827 gp_found = 0;
8829 /* end mips */
8830 for (parent = reloc_vector; *parent != NULL; parent++)
8832 char *error_message = NULL;
8833 bfd_reloc_status_type r;
8835 /* Specific to MIPS: Deal with relocation types that require
8836 knowing the gp of the output bfd. */
8837 asymbol *sym = *(*parent)->sym_ptr_ptr;
8839 /* If we've managed to find the gp and have a special
8840 function for the relocation then go ahead, else default
8841 to the generic handling. */
8842 if (gp_found
8843 && (*parent)->howto->special_function
8844 == _bfd_mips_elf32_gprel16_reloc)
8845 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
8846 input_section, relocatable,
8847 data, gp);
8848 else
8849 r = bfd_perform_relocation (input_bfd, *parent, data,
8850 input_section,
8851 relocatable ? abfd : NULL,
8852 &error_message);
8854 if (relocatable)
8856 asection *os = input_section->output_section;
8858 /* A partial link, so keep the relocs */
8859 os->orelocation[os->reloc_count] = *parent;
8860 os->reloc_count++;
8863 if (r != bfd_reloc_ok)
8865 switch (r)
8867 case bfd_reloc_undefined:
8868 if (!((*link_info->callbacks->undefined_symbol)
8869 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8870 input_bfd, input_section, (*parent)->address,
8871 TRUE)))
8872 goto error_return;
8873 break;
8874 case bfd_reloc_dangerous:
8875 BFD_ASSERT (error_message != NULL);
8876 if (!((*link_info->callbacks->reloc_dangerous)
8877 (link_info, error_message, input_bfd, input_section,
8878 (*parent)->address)))
8879 goto error_return;
8880 break;
8881 case bfd_reloc_overflow:
8882 if (!((*link_info->callbacks->reloc_overflow)
8883 (link_info, NULL,
8884 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8885 (*parent)->howto->name, (*parent)->addend,
8886 input_bfd, input_section, (*parent)->address)))
8887 goto error_return;
8888 break;
8889 case bfd_reloc_outofrange:
8890 default:
8891 abort ();
8892 break;
8898 if (reloc_vector != NULL)
8899 free (reloc_vector);
8900 return data;
8902 error_return:
8903 if (reloc_vector != NULL)
8904 free (reloc_vector);
8905 return NULL;
8908 /* Create a MIPS ELF linker hash table. */
8910 struct bfd_link_hash_table *
8911 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
8913 struct mips_elf_link_hash_table *ret;
8914 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
8916 ret = bfd_malloc (amt);
8917 if (ret == NULL)
8918 return NULL;
8920 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
8921 mips_elf_link_hash_newfunc))
8923 free (ret);
8924 return NULL;
8927 #if 0
8928 /* We no longer use this. */
8929 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
8930 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
8931 #endif
8932 ret->procedure_count = 0;
8933 ret->compact_rel_size = 0;
8934 ret->use_rld_obj_head = FALSE;
8935 ret->rld_value = 0;
8936 ret->mips16_stubs_seen = FALSE;
8938 return &ret->root.root;
8941 /* We need to use a special link routine to handle the .reginfo and
8942 the .mdebug sections. We need to merge all instances of these
8943 sections together, not write them all out sequentially. */
8945 bfd_boolean
8946 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
8948 asection *o;
8949 struct bfd_link_order *p;
8950 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
8951 asection *rtproc_sec;
8952 Elf32_RegInfo reginfo;
8953 struct ecoff_debug_info debug;
8954 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8955 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
8956 HDRR *symhdr = &debug.symbolic_header;
8957 void *mdebug_handle = NULL;
8958 asection *s;
8959 EXTR esym;
8960 unsigned int i;
8961 bfd_size_type amt;
8963 static const char * const secname[] =
8965 ".text", ".init", ".fini", ".data",
8966 ".rodata", ".sdata", ".sbss", ".bss"
8968 static const int sc[] =
8970 scText, scInit, scFini, scData,
8971 scRData, scSData, scSBss, scBss
8974 /* We'd carefully arranged the dynamic symbol indices, and then the
8975 generic size_dynamic_sections renumbered them out from under us.
8976 Rather than trying somehow to prevent the renumbering, just do
8977 the sort again. */
8978 if (elf_hash_table (info)->dynamic_sections_created)
8980 bfd *dynobj;
8981 asection *got;
8982 struct mips_got_info *g;
8983 bfd_size_type dynsecsymcount;
8985 /* When we resort, we must tell mips_elf_sort_hash_table what
8986 the lowest index it may use is. That's the number of section
8987 symbols we're going to add. The generic ELF linker only
8988 adds these symbols when building a shared object. Note that
8989 we count the sections after (possibly) removing the .options
8990 section above. */
8992 dynsecsymcount = 0;
8993 if (info->shared)
8995 asection * p;
8997 for (p = abfd->sections; p ; p = p->next)
8998 if ((p->flags & SEC_EXCLUDE) == 0
8999 && (p->flags & SEC_ALLOC) != 0
9000 && !(*bed->elf_backend_omit_section_dynsym) (abfd, info, p))
9001 ++ dynsecsymcount;
9004 if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
9005 return FALSE;
9007 /* Make sure we didn't grow the global .got region. */
9008 dynobj = elf_hash_table (info)->dynobj;
9009 got = mips_elf_got_section (dynobj, FALSE);
9010 g = mips_elf_section_data (got)->u.got_info;
9012 if (g->global_gotsym != NULL)
9013 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
9014 - g->global_gotsym->dynindx)
9015 <= g->global_gotno);
9018 /* Get a value for the GP register. */
9019 if (elf_gp (abfd) == 0)
9021 struct bfd_link_hash_entry *h;
9023 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9024 if (h != NULL && h->type == bfd_link_hash_defined)
9025 elf_gp (abfd) = (h->u.def.value
9026 + h->u.def.section->output_section->vma
9027 + h->u.def.section->output_offset);
9028 else if (info->relocatable)
9030 bfd_vma lo = MINUS_ONE;
9032 /* Find the GP-relative section with the lowest offset. */
9033 for (o = abfd->sections; o != NULL; o = o->next)
9034 if (o->vma < lo
9035 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
9036 lo = o->vma;
9038 /* And calculate GP relative to that. */
9039 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
9041 else
9043 /* If the relocate_section function needs to do a reloc
9044 involving the GP value, it should make a reloc_dangerous
9045 callback to warn that GP is not defined. */
9049 /* Go through the sections and collect the .reginfo and .mdebug
9050 information. */
9051 reginfo_sec = NULL;
9052 mdebug_sec = NULL;
9053 gptab_data_sec = NULL;
9054 gptab_bss_sec = NULL;
9055 for (o = abfd->sections; o != NULL; o = o->next)
9057 if (strcmp (o->name, ".reginfo") == 0)
9059 memset (&reginfo, 0, sizeof reginfo);
9061 /* We have found the .reginfo section in the output file.
9062 Look through all the link_orders comprising it and merge
9063 the information together. */
9064 for (p = o->map_head.link_order; p != NULL; p = p->next)
9066 asection *input_section;
9067 bfd *input_bfd;
9068 Elf32_External_RegInfo ext;
9069 Elf32_RegInfo sub;
9071 if (p->type != bfd_indirect_link_order)
9073 if (p->type == bfd_data_link_order)
9074 continue;
9075 abort ();
9078 input_section = p->u.indirect.section;
9079 input_bfd = input_section->owner;
9081 if (! bfd_get_section_contents (input_bfd, input_section,
9082 &ext, 0, sizeof ext))
9083 return FALSE;
9085 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
9087 reginfo.ri_gprmask |= sub.ri_gprmask;
9088 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
9089 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
9090 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
9091 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
9093 /* ri_gp_value is set by the function
9094 mips_elf32_section_processing when the section is
9095 finally written out. */
9097 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9098 elf_link_input_bfd ignores this section. */
9099 input_section->flags &= ~SEC_HAS_CONTENTS;
9102 /* Size has been set in _bfd_mips_elf_always_size_sections. */
9103 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
9105 /* Skip this section later on (I don't think this currently
9106 matters, but someday it might). */
9107 o->map_head.link_order = NULL;
9109 reginfo_sec = o;
9112 if (strcmp (o->name, ".mdebug") == 0)
9114 struct extsym_info einfo;
9115 bfd_vma last;
9117 /* We have found the .mdebug section in the output file.
9118 Look through all the link_orders comprising it and merge
9119 the information together. */
9120 symhdr->magic = swap->sym_magic;
9121 /* FIXME: What should the version stamp be? */
9122 symhdr->vstamp = 0;
9123 symhdr->ilineMax = 0;
9124 symhdr->cbLine = 0;
9125 symhdr->idnMax = 0;
9126 symhdr->ipdMax = 0;
9127 symhdr->isymMax = 0;
9128 symhdr->ioptMax = 0;
9129 symhdr->iauxMax = 0;
9130 symhdr->issMax = 0;
9131 symhdr->issExtMax = 0;
9132 symhdr->ifdMax = 0;
9133 symhdr->crfd = 0;
9134 symhdr->iextMax = 0;
9136 /* We accumulate the debugging information itself in the
9137 debug_info structure. */
9138 debug.line = NULL;
9139 debug.external_dnr = NULL;
9140 debug.external_pdr = NULL;
9141 debug.external_sym = NULL;
9142 debug.external_opt = NULL;
9143 debug.external_aux = NULL;
9144 debug.ss = NULL;
9145 debug.ssext = debug.ssext_end = NULL;
9146 debug.external_fdr = NULL;
9147 debug.external_rfd = NULL;
9148 debug.external_ext = debug.external_ext_end = NULL;
9150 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9151 if (mdebug_handle == NULL)
9152 return FALSE;
9154 esym.jmptbl = 0;
9155 esym.cobol_main = 0;
9156 esym.weakext = 0;
9157 esym.reserved = 0;
9158 esym.ifd = ifdNil;
9159 esym.asym.iss = issNil;
9160 esym.asym.st = stLocal;
9161 esym.asym.reserved = 0;
9162 esym.asym.index = indexNil;
9163 last = 0;
9164 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
9166 esym.asym.sc = sc[i];
9167 s = bfd_get_section_by_name (abfd, secname[i]);
9168 if (s != NULL)
9170 esym.asym.value = s->vma;
9171 last = s->vma + s->size;
9173 else
9174 esym.asym.value = last;
9175 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
9176 secname[i], &esym))
9177 return FALSE;
9180 for (p = o->map_head.link_order; p != NULL; p = p->next)
9182 asection *input_section;
9183 bfd *input_bfd;
9184 const struct ecoff_debug_swap *input_swap;
9185 struct ecoff_debug_info input_debug;
9186 char *eraw_src;
9187 char *eraw_end;
9189 if (p->type != bfd_indirect_link_order)
9191 if (p->type == bfd_data_link_order)
9192 continue;
9193 abort ();
9196 input_section = p->u.indirect.section;
9197 input_bfd = input_section->owner;
9199 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
9200 || (get_elf_backend_data (input_bfd)
9201 ->elf_backend_ecoff_debug_swap) == NULL)
9203 /* I don't know what a non MIPS ELF bfd would be
9204 doing with a .mdebug section, but I don't really
9205 want to deal with it. */
9206 continue;
9209 input_swap = (get_elf_backend_data (input_bfd)
9210 ->elf_backend_ecoff_debug_swap);
9212 BFD_ASSERT (p->size == input_section->size);
9214 /* The ECOFF linking code expects that we have already
9215 read in the debugging information and set up an
9216 ecoff_debug_info structure, so we do that now. */
9217 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
9218 &input_debug))
9219 return FALSE;
9221 if (! (bfd_ecoff_debug_accumulate
9222 (mdebug_handle, abfd, &debug, swap, input_bfd,
9223 &input_debug, input_swap, info)))
9224 return FALSE;
9226 /* Loop through the external symbols. For each one with
9227 interesting information, try to find the symbol in
9228 the linker global hash table and save the information
9229 for the output external symbols. */
9230 eraw_src = input_debug.external_ext;
9231 eraw_end = (eraw_src
9232 + (input_debug.symbolic_header.iextMax
9233 * input_swap->external_ext_size));
9234 for (;
9235 eraw_src < eraw_end;
9236 eraw_src += input_swap->external_ext_size)
9238 EXTR ext;
9239 const char *name;
9240 struct mips_elf_link_hash_entry *h;
9242 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
9243 if (ext.asym.sc == scNil
9244 || ext.asym.sc == scUndefined
9245 || ext.asym.sc == scSUndefined)
9246 continue;
9248 name = input_debug.ssext + ext.asym.iss;
9249 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
9250 name, FALSE, FALSE, TRUE);
9251 if (h == NULL || h->esym.ifd != -2)
9252 continue;
9254 if (ext.ifd != -1)
9256 BFD_ASSERT (ext.ifd
9257 < input_debug.symbolic_header.ifdMax);
9258 ext.ifd = input_debug.ifdmap[ext.ifd];
9261 h->esym = ext;
9264 /* Free up the information we just read. */
9265 free (input_debug.line);
9266 free (input_debug.external_dnr);
9267 free (input_debug.external_pdr);
9268 free (input_debug.external_sym);
9269 free (input_debug.external_opt);
9270 free (input_debug.external_aux);
9271 free (input_debug.ss);
9272 free (input_debug.ssext);
9273 free (input_debug.external_fdr);
9274 free (input_debug.external_rfd);
9275 free (input_debug.external_ext);
9277 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9278 elf_link_input_bfd ignores this section. */
9279 input_section->flags &= ~SEC_HAS_CONTENTS;
9282 if (SGI_COMPAT (abfd) && info->shared)
9284 /* Create .rtproc section. */
9285 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
9286 if (rtproc_sec == NULL)
9288 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
9289 | SEC_LINKER_CREATED | SEC_READONLY);
9291 rtproc_sec = bfd_make_section_with_flags (abfd,
9292 ".rtproc",
9293 flags);
9294 if (rtproc_sec == NULL
9295 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
9296 return FALSE;
9299 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
9300 info, rtproc_sec,
9301 &debug))
9302 return FALSE;
9305 /* Build the external symbol information. */
9306 einfo.abfd = abfd;
9307 einfo.info = info;
9308 einfo.debug = &debug;
9309 einfo.swap = swap;
9310 einfo.failed = FALSE;
9311 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9312 mips_elf_output_extsym, &einfo);
9313 if (einfo.failed)
9314 return FALSE;
9316 /* Set the size of the .mdebug section. */
9317 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
9319 /* Skip this section later on (I don't think this currently
9320 matters, but someday it might). */
9321 o->map_head.link_order = NULL;
9323 mdebug_sec = o;
9326 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
9328 const char *subname;
9329 unsigned int c;
9330 Elf32_gptab *tab;
9331 Elf32_External_gptab *ext_tab;
9332 unsigned int j;
9334 /* The .gptab.sdata and .gptab.sbss sections hold
9335 information describing how the small data area would
9336 change depending upon the -G switch. These sections
9337 not used in executables files. */
9338 if (! info->relocatable)
9340 for (p = o->map_head.link_order; p != NULL; p = p->next)
9342 asection *input_section;
9344 if (p->type != bfd_indirect_link_order)
9346 if (p->type == bfd_data_link_order)
9347 continue;
9348 abort ();
9351 input_section = p->u.indirect.section;
9353 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9354 elf_link_input_bfd ignores this section. */
9355 input_section->flags &= ~SEC_HAS_CONTENTS;
9358 /* Skip this section later on (I don't think this
9359 currently matters, but someday it might). */
9360 o->map_head.link_order = NULL;
9362 /* Really remove the section. */
9363 bfd_section_list_remove (abfd, o);
9364 --abfd->section_count;
9366 continue;
9369 /* There is one gptab for initialized data, and one for
9370 uninitialized data. */
9371 if (strcmp (o->name, ".gptab.sdata") == 0)
9372 gptab_data_sec = o;
9373 else if (strcmp (o->name, ".gptab.sbss") == 0)
9374 gptab_bss_sec = o;
9375 else
9377 (*_bfd_error_handler)
9378 (_("%s: illegal section name `%s'"),
9379 bfd_get_filename (abfd), o->name);
9380 bfd_set_error (bfd_error_nonrepresentable_section);
9381 return FALSE;
9384 /* The linker script always combines .gptab.data and
9385 .gptab.sdata into .gptab.sdata, and likewise for
9386 .gptab.bss and .gptab.sbss. It is possible that there is
9387 no .sdata or .sbss section in the output file, in which
9388 case we must change the name of the output section. */
9389 subname = o->name + sizeof ".gptab" - 1;
9390 if (bfd_get_section_by_name (abfd, subname) == NULL)
9392 if (o == gptab_data_sec)
9393 o->name = ".gptab.data";
9394 else
9395 o->name = ".gptab.bss";
9396 subname = o->name + sizeof ".gptab" - 1;
9397 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
9400 /* Set up the first entry. */
9401 c = 1;
9402 amt = c * sizeof (Elf32_gptab);
9403 tab = bfd_malloc (amt);
9404 if (tab == NULL)
9405 return FALSE;
9406 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
9407 tab[0].gt_header.gt_unused = 0;
9409 /* Combine the input sections. */
9410 for (p = o->map_head.link_order; p != NULL; p = p->next)
9412 asection *input_section;
9413 bfd *input_bfd;
9414 bfd_size_type size;
9415 unsigned long last;
9416 bfd_size_type gpentry;
9418 if (p->type != bfd_indirect_link_order)
9420 if (p->type == bfd_data_link_order)
9421 continue;
9422 abort ();
9425 input_section = p->u.indirect.section;
9426 input_bfd = input_section->owner;
9428 /* Combine the gptab entries for this input section one
9429 by one. We know that the input gptab entries are
9430 sorted by ascending -G value. */
9431 size = input_section->size;
9432 last = 0;
9433 for (gpentry = sizeof (Elf32_External_gptab);
9434 gpentry < size;
9435 gpentry += sizeof (Elf32_External_gptab))
9437 Elf32_External_gptab ext_gptab;
9438 Elf32_gptab int_gptab;
9439 unsigned long val;
9440 unsigned long add;
9441 bfd_boolean exact;
9442 unsigned int look;
9444 if (! (bfd_get_section_contents
9445 (input_bfd, input_section, &ext_gptab, gpentry,
9446 sizeof (Elf32_External_gptab))))
9448 free (tab);
9449 return FALSE;
9452 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
9453 &int_gptab);
9454 val = int_gptab.gt_entry.gt_g_value;
9455 add = int_gptab.gt_entry.gt_bytes - last;
9457 exact = FALSE;
9458 for (look = 1; look < c; look++)
9460 if (tab[look].gt_entry.gt_g_value >= val)
9461 tab[look].gt_entry.gt_bytes += add;
9463 if (tab[look].gt_entry.gt_g_value == val)
9464 exact = TRUE;
9467 if (! exact)
9469 Elf32_gptab *new_tab;
9470 unsigned int max;
9472 /* We need a new table entry. */
9473 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9474 new_tab = bfd_realloc (tab, amt);
9475 if (new_tab == NULL)
9477 free (tab);
9478 return FALSE;
9480 tab = new_tab;
9481 tab[c].gt_entry.gt_g_value = val;
9482 tab[c].gt_entry.gt_bytes = add;
9484 /* Merge in the size for the next smallest -G
9485 value, since that will be implied by this new
9486 value. */
9487 max = 0;
9488 for (look = 1; look < c; look++)
9490 if (tab[look].gt_entry.gt_g_value < val
9491 && (max == 0
9492 || (tab[look].gt_entry.gt_g_value
9493 > tab[max].gt_entry.gt_g_value)))
9494 max = look;
9496 if (max != 0)
9497 tab[c].gt_entry.gt_bytes +=
9498 tab[max].gt_entry.gt_bytes;
9500 ++c;
9503 last = int_gptab.gt_entry.gt_bytes;
9506 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9507 elf_link_input_bfd ignores this section. */
9508 input_section->flags &= ~SEC_HAS_CONTENTS;
9511 /* The table must be sorted by -G value. */
9512 if (c > 2)
9513 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
9515 /* Swap out the table. */
9516 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9517 ext_tab = bfd_alloc (abfd, amt);
9518 if (ext_tab == NULL)
9520 free (tab);
9521 return FALSE;
9524 for (j = 0; j < c; j++)
9525 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
9526 free (tab);
9528 o->size = c * sizeof (Elf32_External_gptab);
9529 o->contents = (bfd_byte *) ext_tab;
9531 /* Skip this section later on (I don't think this currently
9532 matters, but someday it might). */
9533 o->map_head.link_order = NULL;
9537 /* Invoke the regular ELF backend linker to do all the work. */
9538 if (!bfd_elf_final_link (abfd, info))
9539 return FALSE;
9541 /* Now write out the computed sections. */
9543 if (reginfo_sec != NULL)
9545 Elf32_External_RegInfo ext;
9547 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9548 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
9549 return FALSE;
9552 if (mdebug_sec != NULL)
9554 BFD_ASSERT (abfd->output_has_begun);
9555 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
9556 swap, info,
9557 mdebug_sec->filepos))
9558 return FALSE;
9560 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
9563 if (gptab_data_sec != NULL)
9565 if (! bfd_set_section_contents (abfd, gptab_data_sec,
9566 gptab_data_sec->contents,
9567 0, gptab_data_sec->size))
9568 return FALSE;
9571 if (gptab_bss_sec != NULL)
9573 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
9574 gptab_bss_sec->contents,
9575 0, gptab_bss_sec->size))
9576 return FALSE;
9579 if (SGI_COMPAT (abfd))
9581 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
9582 if (rtproc_sec != NULL)
9584 if (! bfd_set_section_contents (abfd, rtproc_sec,
9585 rtproc_sec->contents,
9586 0, rtproc_sec->size))
9587 return FALSE;
9591 return TRUE;
9594 /* Structure for saying that BFD machine EXTENSION extends BASE. */
9596 struct mips_mach_extension {
9597 unsigned long extension, base;
9601 /* An array describing how BFD machines relate to one another. The entries
9602 are ordered topologically with MIPS I extensions listed last. */
9604 static const struct mips_mach_extension mips_mach_extensions[] = {
9605 /* MIPS64 extensions. */
9606 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
9607 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
9609 /* MIPS V extensions. */
9610 { bfd_mach_mipsisa64, bfd_mach_mips5 },
9612 /* R10000 extensions. */
9613 { bfd_mach_mips12000, bfd_mach_mips10000 },
9615 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
9616 vr5400 ISA, but doesn't include the multimedia stuff. It seems
9617 better to allow vr5400 and vr5500 code to be merged anyway, since
9618 many libraries will just use the core ISA. Perhaps we could add
9619 some sort of ASE flag if this ever proves a problem. */
9620 { bfd_mach_mips5500, bfd_mach_mips5400 },
9621 { bfd_mach_mips5400, bfd_mach_mips5000 },
9623 /* MIPS IV extensions. */
9624 { bfd_mach_mips5, bfd_mach_mips8000 },
9625 { bfd_mach_mips10000, bfd_mach_mips8000 },
9626 { bfd_mach_mips5000, bfd_mach_mips8000 },
9627 { bfd_mach_mips7000, bfd_mach_mips8000 },
9628 { bfd_mach_mips9000, bfd_mach_mips8000 },
9630 /* VR4100 extensions. */
9631 { bfd_mach_mips4120, bfd_mach_mips4100 },
9632 { bfd_mach_mips4111, bfd_mach_mips4100 },
9634 /* MIPS III extensions. */
9635 { bfd_mach_mips8000, bfd_mach_mips4000 },
9636 { bfd_mach_mips4650, bfd_mach_mips4000 },
9637 { bfd_mach_mips4600, bfd_mach_mips4000 },
9638 { bfd_mach_mips4400, bfd_mach_mips4000 },
9639 { bfd_mach_mips4300, bfd_mach_mips4000 },
9640 { bfd_mach_mips4100, bfd_mach_mips4000 },
9641 { bfd_mach_mips4010, bfd_mach_mips4000 },
9643 /* MIPS32 extensions. */
9644 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
9646 /* MIPS II extensions. */
9647 { bfd_mach_mips4000, bfd_mach_mips6000 },
9648 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
9650 /* MIPS I extensions. */
9651 { bfd_mach_mips6000, bfd_mach_mips3000 },
9652 { bfd_mach_mips3900, bfd_mach_mips3000 }
9656 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
9658 static bfd_boolean
9659 mips_mach_extends_p (unsigned long base, unsigned long extension)
9661 size_t i;
9663 if (extension == base)
9664 return TRUE;
9666 if (base == bfd_mach_mipsisa32
9667 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
9668 return TRUE;
9670 if (base == bfd_mach_mipsisa32r2
9671 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
9672 return TRUE;
9674 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
9675 if (extension == mips_mach_extensions[i].extension)
9677 extension = mips_mach_extensions[i].base;
9678 if (extension == base)
9679 return TRUE;
9682 return FALSE;
9686 /* Return true if the given ELF header flags describe a 32-bit binary. */
9688 static bfd_boolean
9689 mips_32bit_flags_p (flagword flags)
9691 return ((flags & EF_MIPS_32BITMODE) != 0
9692 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
9693 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
9694 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
9695 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
9696 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
9697 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
9701 /* Merge backend specific data from an object file to the output
9702 object file when linking. */
9704 bfd_boolean
9705 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
9707 flagword old_flags;
9708 flagword new_flags;
9709 bfd_boolean ok;
9710 bfd_boolean null_input_bfd = TRUE;
9711 asection *sec;
9713 /* Check if we have the same endianess */
9714 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
9716 (*_bfd_error_handler)
9717 (_("%B: endianness incompatible with that of the selected emulation"),
9718 ibfd);
9719 return FALSE;
9722 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
9723 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
9724 return TRUE;
9726 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
9728 (*_bfd_error_handler)
9729 (_("%B: ABI is incompatible with that of the selected emulation"),
9730 ibfd);
9731 return FALSE;
9734 new_flags = elf_elfheader (ibfd)->e_flags;
9735 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
9736 old_flags = elf_elfheader (obfd)->e_flags;
9738 if (! elf_flags_init (obfd))
9740 elf_flags_init (obfd) = TRUE;
9741 elf_elfheader (obfd)->e_flags = new_flags;
9742 elf_elfheader (obfd)->e_ident[EI_CLASS]
9743 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
9745 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
9746 && bfd_get_arch_info (obfd)->the_default)
9748 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
9749 bfd_get_mach (ibfd)))
9750 return FALSE;
9753 return TRUE;
9756 /* Check flag compatibility. */
9758 new_flags &= ~EF_MIPS_NOREORDER;
9759 old_flags &= ~EF_MIPS_NOREORDER;
9761 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
9762 doesn't seem to matter. */
9763 new_flags &= ~EF_MIPS_XGOT;
9764 old_flags &= ~EF_MIPS_XGOT;
9766 /* MIPSpro generates ucode info in n64 objects. Again, we should
9767 just be able to ignore this. */
9768 new_flags &= ~EF_MIPS_UCODE;
9769 old_flags &= ~EF_MIPS_UCODE;
9771 if (new_flags == old_flags)
9772 return TRUE;
9774 /* Check to see if the input BFD actually contains any sections.
9775 If not, its flags may not have been initialised either, but it cannot
9776 actually cause any incompatibility. */
9777 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9779 /* Ignore synthetic sections and empty .text, .data and .bss sections
9780 which are automatically generated by gas. */
9781 if (strcmp (sec->name, ".reginfo")
9782 && strcmp (sec->name, ".mdebug")
9783 && (sec->size != 0
9784 || (strcmp (sec->name, ".text")
9785 && strcmp (sec->name, ".data")
9786 && strcmp (sec->name, ".bss"))))
9788 null_input_bfd = FALSE;
9789 break;
9792 if (null_input_bfd)
9793 return TRUE;
9795 ok = TRUE;
9797 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
9798 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
9800 (*_bfd_error_handler)
9801 (_("%B: warning: linking PIC files with non-PIC files"),
9802 ibfd);
9803 ok = TRUE;
9806 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
9807 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
9808 if (! (new_flags & EF_MIPS_PIC))
9809 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
9811 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
9812 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
9814 /* Compare the ISAs. */
9815 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
9817 (*_bfd_error_handler)
9818 (_("%B: linking 32-bit code with 64-bit code"),
9819 ibfd);
9820 ok = FALSE;
9822 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
9824 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
9825 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
9827 /* Copy the architecture info from IBFD to OBFD. Also copy
9828 the 32-bit flag (if set) so that we continue to recognise
9829 OBFD as a 32-bit binary. */
9830 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
9831 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9832 elf_elfheader (obfd)->e_flags
9833 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9835 /* Copy across the ABI flags if OBFD doesn't use them
9836 and if that was what caused us to treat IBFD as 32-bit. */
9837 if ((old_flags & EF_MIPS_ABI) == 0
9838 && mips_32bit_flags_p (new_flags)
9839 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
9840 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
9842 else
9844 /* The ISAs aren't compatible. */
9845 (*_bfd_error_handler)
9846 (_("%B: linking %s module with previous %s modules"),
9847 ibfd,
9848 bfd_printable_name (ibfd),
9849 bfd_printable_name (obfd));
9850 ok = FALSE;
9854 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9855 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
9857 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
9858 does set EI_CLASS differently from any 32-bit ABI. */
9859 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
9860 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
9861 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
9863 /* Only error if both are set (to different values). */
9864 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
9865 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
9866 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
9868 (*_bfd_error_handler)
9869 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
9870 ibfd,
9871 elf_mips_abi_name (ibfd),
9872 elf_mips_abi_name (obfd));
9873 ok = FALSE;
9875 new_flags &= ~EF_MIPS_ABI;
9876 old_flags &= ~EF_MIPS_ABI;
9879 /* For now, allow arbitrary mixing of ASEs (retain the union). */
9880 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
9882 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
9884 new_flags &= ~ EF_MIPS_ARCH_ASE;
9885 old_flags &= ~ EF_MIPS_ARCH_ASE;
9888 /* Warn about any other mismatches */
9889 if (new_flags != old_flags)
9891 (*_bfd_error_handler)
9892 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
9893 ibfd, (unsigned long) new_flags,
9894 (unsigned long) old_flags);
9895 ok = FALSE;
9898 if (! ok)
9900 bfd_set_error (bfd_error_bad_value);
9901 return FALSE;
9904 return TRUE;
9907 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
9909 bfd_boolean
9910 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
9912 BFD_ASSERT (!elf_flags_init (abfd)
9913 || elf_elfheader (abfd)->e_flags == flags);
9915 elf_elfheader (abfd)->e_flags = flags;
9916 elf_flags_init (abfd) = TRUE;
9917 return TRUE;
9920 bfd_boolean
9921 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
9923 FILE *file = ptr;
9925 BFD_ASSERT (abfd != NULL && ptr != NULL);
9927 /* Print normal ELF private data. */
9928 _bfd_elf_print_private_bfd_data (abfd, ptr);
9930 /* xgettext:c-format */
9931 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
9933 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
9934 fprintf (file, _(" [abi=O32]"));
9935 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
9936 fprintf (file, _(" [abi=O64]"));
9937 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
9938 fprintf (file, _(" [abi=EABI32]"));
9939 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
9940 fprintf (file, _(" [abi=EABI64]"));
9941 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
9942 fprintf (file, _(" [abi unknown]"));
9943 else if (ABI_N32_P (abfd))
9944 fprintf (file, _(" [abi=N32]"));
9945 else if (ABI_64_P (abfd))
9946 fprintf (file, _(" [abi=64]"));
9947 else
9948 fprintf (file, _(" [no abi set]"));
9950 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
9951 fprintf (file, _(" [mips1]"));
9952 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
9953 fprintf (file, _(" [mips2]"));
9954 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
9955 fprintf (file, _(" [mips3]"));
9956 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
9957 fprintf (file, _(" [mips4]"));
9958 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
9959 fprintf (file, _(" [mips5]"));
9960 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
9961 fprintf (file, _(" [mips32]"));
9962 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
9963 fprintf (file, _(" [mips64]"));
9964 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
9965 fprintf (file, _(" [mips32r2]"));
9966 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
9967 fprintf (file, _(" [mips64r2]"));
9968 else
9969 fprintf (file, _(" [unknown ISA]"));
9971 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
9972 fprintf (file, _(" [mdmx]"));
9974 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
9975 fprintf (file, _(" [mips16]"));
9977 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
9978 fprintf (file, _(" [32bitmode]"));
9979 else
9980 fprintf (file, _(" [not 32bitmode]"));
9982 fputc ('\n', file);
9984 return TRUE;
9987 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
9989 { ".lit4", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9990 { ".lit8", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9991 { ".mdebug", 7, 0, SHT_MIPS_DEBUG, 0 },
9992 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9993 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
9994 { ".ucode", 6, 0, SHT_MIPS_UCODE, 0 },
9995 { NULL, 0, 0, 0, 0 }