* elf64-alpha.c (elf64_alpha_check_relocs): Don't set reltext
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
CommitLineData
efcbd82c 1/* MIPS-specific support for 32-bit ELF
3f0a7f7f 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
be3ccd9c 3 Free Software Foundation, Inc.
252b5132
RH
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
103186c6
MM
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
f7cb7d68
UC
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
252b5132
RH
11
12This file is part of BFD, the Binary File Descriptor library.
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program; if not, write to the Free Software
26Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27
28/* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
29 different MIPS ELF from other targets. This matters when linking.
30 This file supports both, switching at runtime. */
31
32#include "bfd.h"
33#include "sysdep.h"
34#include "libbfd.h"
35#include "bfdlink.h"
36#include "genlink.h"
37#include "elf-bfd.h"
38#include "elf/mips.h"
39
40/* Get the ECOFF swapping routines. */
41#include "coff/sym.h"
42#include "coff/symconst.h"
43#include "coff/internal.h"
44#include "coff/ecoff.h"
45#include "coff/mips.h"
23e2c83b 46#define ECOFF_SIGNED_32
252b5132
RH
47#include "ecoffswap.h"
48
7403cb63
MM
49/* This structure is used to hold .got information when linking. It
50 is stored in the tdata field of the bfd_elf_section_data structure. */
51
38b1a46c
NC
52struct mips_got_info
53{
7403cb63
MM
54 /* The global symbol in the GOT with the lowest index in the dynamic
55 symbol table. */
56 struct elf_link_hash_entry *global_gotsym;
b3be9b46
RH
57 /* The number of global .got entries. */
58 unsigned int global_gotno;
7403cb63
MM
59 /* The number of local .got entries. */
60 unsigned int local_gotno;
61 /* The number of local .got entries we have used. */
62 unsigned int assigned_gotno;
63};
64
65/* The MIPS ELF linker needs additional information for each symbol in
66 the global hash table. */
67
38b1a46c
NC
68struct mips_elf_link_hash_entry
69{
7403cb63
MM
70 struct elf_link_hash_entry root;
71
72 /* External symbol information. */
73 EXTR esym;
74
a3c7651d 75 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
be3ccd9c 76 this symbol. */
a3c7651d 77 unsigned int possibly_dynamic_relocs;
7403cb63 78
43917054
L
79 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
80 a readonly section. */
81 boolean readonly_reloc;
82
7403cb63
MM
83 /* The index of the first dynamic relocation (in the .rel.dyn
84 section) against this symbol. */
85 unsigned int min_dyn_reloc_index;
86
9117d219
NC
87 /* We must not create a stub for a symbol that has relocations
88 related to taking the function's address, i.e. any but
89 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
90 p. 4-20. */
91 boolean no_fn_stub;
92
7403cb63
MM
93 /* If there is a stub that 32 bit functions should use to call this
94 16 bit function, this points to the section containing the stub. */
95 asection *fn_stub;
96
97 /* Whether we need the fn_stub; this is set if this symbol appears
98 in any relocs other than a 16 bit call. */
99 boolean need_fn_stub;
100
101 /* If there is a stub that 16 bit functions should use to call this
102 32 bit function, this points to the section containing the stub. */
103 asection *call_stub;
104
105 /* This is like the call_stub field, but it is used if the function
106 being called returns a floating point value. */
107 asection *call_fp_stub;
108};
109
252b5132
RH
110static bfd_reloc_status_type mips32_64bit_reloc
111 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
112static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
113 PARAMS ((bfd *, bfd_reloc_code_real_type));
c9b3cbf3
RH
114static reloc_howto_type *mips_rtype_to_howto
115 PARAMS ((unsigned int));
252b5132
RH
116static void mips_info_to_howto_rel
117 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
3f830999
MM
118static void mips_info_to_howto_rela
119 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
252b5132
RH
120static void bfd_mips_elf32_swap_gptab_in
121 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
122static void bfd_mips_elf32_swap_gptab_out
123 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
86033394 124#if 0
be3ccd9c 125static void bfd_mips_elf_swap_msym_in
c6142e5d 126 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
86033394 127#endif
c6142e5d
MM
128static void bfd_mips_elf_swap_msym_out
129 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
252b5132 130static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
252b5132
RH
131static boolean mips_elf_create_procedure_table
132 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
133 struct ecoff_debug_info *));
252b5132 134static INLINE int elf_mips_isa PARAMS ((flagword));
dc810e39 135static INLINE unsigned long elf_mips_mach PARAMS ((flagword));
103186c6 136static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
252b5132
RH
137static boolean mips_elf_is_local_label_name
138 PARAMS ((bfd *, const char *));
139static struct bfd_hash_entry *mips_elf_link_hash_newfunc
140 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
252b5132 141static int gptab_compare PARAMS ((const void *, const void *));
252b5132
RH
142static bfd_reloc_status_type mips16_jump_reloc
143 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
144static bfd_reloc_status_type mips16_gprel_reloc
145 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
252b5132
RH
146static boolean mips_elf_create_compact_rel_section
147 PARAMS ((bfd *, struct bfd_link_info *));
148static boolean mips_elf_create_got_section
149 PARAMS ((bfd *, struct bfd_link_info *));
252b5132
RH
150static bfd_reloc_status_type mips_elf_final_gp
151 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
152static bfd_byte *elf32_mips_get_relocated_section_contents
153 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
154 bfd_byte *, boolean, asymbol **));
be3ccd9c 155static asection *mips_elf_create_msym_section
c6142e5d 156 PARAMS ((bfd *));
be3ccd9c 157static void mips_elf_irix6_finish_dynamic_symbol
7403cb63
MM
158 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
159static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
160static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
161static bfd_vma mips_elf_high PARAMS ((bfd_vma));
162static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
163static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
164static bfd_vma mips_elf_global_got_index
165 PARAMS ((bfd *, struct elf_link_hash_entry *));
166static bfd_vma mips_elf_local_got_index
167 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
168static bfd_vma mips_elf_got_offset_from_index
169 PARAMS ((bfd *, bfd *, bfd_vma));
be3ccd9c 170static boolean mips_elf_record_global_got_symbol
7403cb63
MM
171 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
172 struct mips_got_info *));
173static bfd_vma mips_elf_got_page
174 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
bb2d6cd7 175static const Elf_Internal_Rela *mips_elf_next_relocation
be3ccd9c 176 PARAMS ((unsigned int, const Elf_Internal_Rela *,
bb2d6cd7 177 const Elf_Internal_Rela *));
7403cb63
MM
178static bfd_reloc_status_type mips_elf_calculate_relocation
179 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
103186c6 180 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
197b9ca0
MM
181 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
182 boolean *));
7403cb63 183static bfd_vma mips_elf_obtain_contents
103186c6 184 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
197b9ca0 185static boolean mips_elf_perform_relocation
be3ccd9c 186 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
e53bd91b 187 const Elf_Internal_Rela *, bfd_vma,
197b9ca0 188 bfd *, asection *, bfd_byte *, boolean));
7403cb63 189static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
be3ccd9c 190static boolean mips_elf_sort_hash_table_f
7403cb63 191 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
be3ccd9c 192static boolean mips_elf_sort_hash_table
b3be9b46 193 PARAMS ((struct bfd_link_info *, unsigned long));
7403cb63 194static asection * mips_elf_got_section PARAMS ((bfd *));
be3ccd9c 195static struct mips_got_info *mips_elf_got_info
7403cb63 196 PARAMS ((bfd *, asection **));
6387d602 197static boolean mips_elf_local_relocation_p
b305ef96 198 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **, boolean));
be3ccd9c 199static bfd_vma mips_elf_create_local_got_entry
7403cb63 200 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
be3ccd9c 201static bfd_vma mips_elf_got16_entry
b305ef96 202 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, boolean));
be3ccd9c 203static boolean mips_elf_create_dynamic_relocation
103186c6 204 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
7b1f1231 205 struct mips_elf_link_hash_entry *, asection *,
9117d219 206 bfd_vma, bfd_vma *, asection *));
be3ccd9c 207static void mips_elf_allocate_dynamic_relocations
103186c6 208 PARAMS ((bfd *, unsigned int));
be3ccd9c 209static boolean mips_elf_stub_section_p
197b9ca0 210 PARAMS ((bfd *, asection *));
adb76a3e
UC
211static int sort_dynamic_relocs
212 PARAMS ((const void *, const void *));
9e80ff3a 213static void _bfd_mips_elf_hide_symbol
e5094212 214 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, boolean));
9e80ff3a
L
215static void _bfd_mips_elf_copy_indirect_symbol
216 PARAMS ((struct elf_link_hash_entry *,
217 struct elf_link_hash_entry *));
218static boolean _bfd_elf32_mips_grok_prstatus
219 PARAMS ((bfd *, Elf_Internal_Note *));
220static boolean _bfd_elf32_mips_grok_psinfo
221 PARAMS ((bfd *, Elf_Internal_Note *));
73d074b4
DJ
222static boolean _bfd_elf32_mips_discard_info
223 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *));
224static boolean _bfd_elf32_mips_ignore_discarded_relocs
225 PARAMS ((asection *));
226static boolean _bfd_elf32_mips_write_section
227 PARAMS ((bfd *, asection *, bfd_byte *));
252b5132 228
f7cb7d68 229extern const bfd_target bfd_elf32_tradbigmips_vec;
fdbafa10 230extern const bfd_target bfd_elf32_tradlittlemips_vec;
b3baf5d0 231#ifdef BFD64
fdbafa10
L
232extern const bfd_target bfd_elf64_tradbigmips_vec;
233extern const bfd_target bfd_elf64_tradlittlemips_vec;
b3baf5d0 234#endif
f7cb7d68 235
a94a7c1c 236/* The level of IRIX compatibility we're striving for. */
252b5132 237
a94a7c1c
MM
238typedef enum {
239 ict_none,
240 ict_irix5,
241 ict_irix6
242} irix_compat_t;
243
adb76a3e
UC
244/* This will be used when we sort the dynamic relocation records. */
245static bfd *reldyn_sorting_bfd;
246
a94a7c1c
MM
247/* Nonzero if ABFD is using the N32 ABI. */
248
249#define ABI_N32_P(abfd) \
250 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
251
fdbafa10 252/* Nonzero if ABFD is using the 64-bit ABI. */
5e38c3b8
MM
253#define ABI_64_P(abfd) \
254 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
255
f7cb7d68 256/* Depending on the target vector we generate some version of Irix
be3ccd9c 257 executables or "normal" MIPS ELF ABI executables. */
b3baf5d0 258#ifdef BFD64
a94a7c1c 259#define IRIX_COMPAT(abfd) \
fdbafa10
L
260 (((abfd->xvec == &bfd_elf64_tradbigmips_vec) || \
261 (abfd->xvec == &bfd_elf64_tradlittlemips_vec) || \
262 (abfd->xvec == &bfd_elf32_tradbigmips_vec) || \
263 (abfd->xvec == &bfd_elf32_tradlittlemips_vec)) ? ict_none : \
f7cb7d68 264 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5))
b3baf5d0
NC
265#else
266#define IRIX_COMPAT(abfd) \
267 (((abfd->xvec == &bfd_elf32_tradbigmips_vec) || \
268 (abfd->xvec == &bfd_elf32_tradlittlemips_vec)) ? ict_none : \
269 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5))
270#endif
a94a7c1c 271
d75bc93d
TS
272#define NEWABI_P(abfd) (ABI_N32_P(abfd) || ABI_64_P(abfd))
273
4e8a9624 274/* Whether we are trying to be compatible with IRIX at all. */
a94a7c1c
MM
275#define SGI_COMPAT(abfd) \
276 (IRIX_COMPAT (abfd) != ict_none)
252b5132 277
c6142e5d
MM
278/* The name of the msym section. */
279#define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
280
303f629d
MM
281/* The name of the srdata section. */
282#define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
283
284/* The name of the options section. */
285#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
286 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
287
288/* The name of the stub section. */
289#define MIPS_ELF_STUB_SECTION_NAME(abfd) \
290 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
291
103186c6
MM
292/* The name of the dynamic relocation section. */
293#define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
294
295/* The size of an external REL relocation. */
296#define MIPS_ELF_REL_SIZE(abfd) \
297 (get_elf_backend_data (abfd)->s->sizeof_rel)
298
299/* The size of an external dynamic table entry. */
300#define MIPS_ELF_DYN_SIZE(abfd) \
301 (get_elf_backend_data (abfd)->s->sizeof_dyn)
302
303/* The size of a GOT entry. */
304#define MIPS_ELF_GOT_SIZE(abfd) \
305 (get_elf_backend_data (abfd)->s->arch_size / 8)
306
307/* The size of a symbol-table entry. */
308#define MIPS_ELF_SYM_SIZE(abfd) \
309 (get_elf_backend_data (abfd)->s->sizeof_sym)
310
311/* The default alignment for sections, as a power of two. */
312#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
313 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
314
315/* Get word-sized data. */
316#define MIPS_ELF_GET_WORD(abfd, ptr) \
317 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
318
319/* Put out word-sized data. */
320#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
321 (ABI_64_P (abfd) \
322 ? bfd_put_64 (abfd, val, ptr) \
323 : bfd_put_32 (abfd, val, ptr))
324
325/* Add a dynamic symbol table-entry. */
9ebbd33e 326#ifdef BFD64
dc810e39
AM
327#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
328 (ABI_64_P (elf_hash_table (info)->dynobj) \
329 ? bfd_elf64_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val) \
330 : bfd_elf32_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
9ebbd33e 331#else
dc810e39
AM
332#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
333 (ABI_64_P (elf_hash_table (info)->dynobj) \
1e738b87 334 ? (boolean) (abort (), false) \
dc810e39 335 : bfd_elf32_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
9ebbd33e 336#endif
103186c6 337
252b5132
RH
338/* The number of local .got entries we reserve. */
339#define MIPS_RESERVED_GOTNO (2)
340
341/* Instructions which appear in a stub. For some reason the stub is
342 slightly different on an SGI system. */
343#define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
103186c6
MM
344#define STUB_LW(abfd) \
345 (SGI_COMPAT (abfd) \
346 ? (ABI_64_P (abfd) \
347 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
348 : 0x8f998010) /* lw t9,0x8010(gp) */ \
f7cb7d68
UC
349 : 0x8f998010) /* lw t9,0x8000(gp) */
350#define STUB_MOVE(abfd) \
351 (SGI_COMPAT (abfd) ? 0x03e07825 : 0x03e07821) /* move t7,ra */
352#define STUB_JALR 0x0320f809 /* jal t9 */
353#define STUB_LI16(abfd) \
354 (SGI_COMPAT (abfd) ? 0x34180000 : 0x24180000) /* ori t8,zero,0 */
252b5132
RH
355#define MIPS_FUNCTION_STUB_SIZE (16)
356
357#if 0
358/* We no longer try to identify particular sections for the .dynsym
359 section. When we do, we wind up crashing if there are other random
360 sections with relocations. */
361
362/* Names of sections which appear in the .dynsym section in an Irix 5
363 executable. */
364
38b1a46c
NC
365static const char * const mips_elf_dynsym_sec_names[] =
366{
252b5132
RH
367 ".text",
368 ".init",
369 ".fini",
370 ".data",
371 ".rodata",
372 ".sdata",
373 ".sbss",
374 ".bss",
375 NULL
376};
377
378#define SIZEOF_MIPS_DYNSYM_SECNAMES \
379 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
380
381/* The number of entries in mips_elf_dynsym_sec_names which go in the
382 text segment. */
383
384#define MIPS_TEXT_DYNSYM_SECNO (3)
385
386#endif /* 0 */
387
388/* The names of the runtime procedure table symbols used on Irix 5. */
389
38b1a46c
NC
390static const char * const mips_elf_dynsym_rtproc_names[] =
391{
252b5132
RH
392 "_procedure_table",
393 "_procedure_string_table",
394 "_procedure_table_size",
395 NULL
396};
397
398/* These structures are used to generate the .compact_rel section on
399 Irix 5. */
400
38b1a46c
NC
401typedef struct
402{
252b5132
RH
403 unsigned long id1; /* Always one? */
404 unsigned long num; /* Number of compact relocation entries. */
405 unsigned long id2; /* Always two? */
406 unsigned long offset; /* The file offset of the first relocation. */
407 unsigned long reserved0; /* Zero? */
408 unsigned long reserved1; /* Zero? */
409} Elf32_compact_rel;
410
38b1a46c
NC
411typedef struct
412{
252b5132
RH
413 bfd_byte id1[4];
414 bfd_byte num[4];
415 bfd_byte id2[4];
416 bfd_byte offset[4];
417 bfd_byte reserved0[4];
418 bfd_byte reserved1[4];
419} Elf32_External_compact_rel;
420
38b1a46c
NC
421typedef struct
422{
252b5132 423 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
be3ccd9c 424 unsigned int rtype : 4; /* Relocation types. See below. */
252b5132
RH
425 unsigned int dist2to : 8;
426 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
427 unsigned long konst; /* KONST field. See below. */
428 unsigned long vaddr; /* VADDR to be relocated. */
429} Elf32_crinfo;
430
38b1a46c
NC
431typedef struct
432{
252b5132 433 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
be3ccd9c 434 unsigned int rtype : 4; /* Relocation types. See below. */
252b5132
RH
435 unsigned int dist2to : 8;
436 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
437 unsigned long konst; /* KONST field. See below. */
438} Elf32_crinfo2;
439
38b1a46c
NC
440typedef struct
441{
252b5132
RH
442 bfd_byte info[4];
443 bfd_byte konst[4];
444 bfd_byte vaddr[4];
445} Elf32_External_crinfo;
446
38b1a46c
NC
447typedef struct
448{
252b5132
RH
449 bfd_byte info[4];
450 bfd_byte konst[4];
451} Elf32_External_crinfo2;
452
453/* These are the constants used to swap the bitfields in a crinfo. */
454
455#define CRINFO_CTYPE (0x1)
456#define CRINFO_CTYPE_SH (31)
457#define CRINFO_RTYPE (0xf)
458#define CRINFO_RTYPE_SH (27)
459#define CRINFO_DIST2TO (0xff)
460#define CRINFO_DIST2TO_SH (19)
461#define CRINFO_RELVADDR (0x7ffff)
462#define CRINFO_RELVADDR_SH (0)
463
464/* A compact relocation info has long (3 words) or short (2 words)
465 formats. A short format doesn't have VADDR field and relvaddr
466 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
467#define CRF_MIPS_LONG 1
468#define CRF_MIPS_SHORT 0
469
470/* There are 4 types of compact relocation at least. The value KONST
471 has different meaning for each type:
472
473 (type) (konst)
474 CT_MIPS_REL32 Address in data
475 CT_MIPS_WORD Address in word (XXX)
476 CT_MIPS_GPHI_LO GP - vaddr
477 CT_MIPS_JMPAD Address to jump
478 */
479
480#define CRT_MIPS_REL32 0xa
481#define CRT_MIPS_WORD 0xb
482#define CRT_MIPS_GPHI_LO 0xc
483#define CRT_MIPS_JMPAD 0xd
484
485#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
486#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
487#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
488#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
489
490static void bfd_elf32_swap_compact_rel_out
491 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
492static void bfd_elf32_swap_crinfo_out
493 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
494
3f830999
MM
495/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
496 from smaller values. Start with zero, widen, *then* decrement. */
497#define MINUS_ONE (((bfd_vma)0) - 1)
498
d75bc93d
TS
499/* The relocation table used for SHT_REL sections. */
500
501static reloc_howto_type elf_mips_howto_table_rel[] =
502{
503 /* No relocation. */
504 HOWTO (R_MIPS_NONE, /* type */
505 0, /* rightshift */
506 0, /* size (0 = byte, 1 = short, 2 = long) */
507 0, /* bitsize */
508 false, /* pc_relative */
509 0, /* bitpos */
510 complain_overflow_dont, /* complain_on_overflow */
511 bfd_elf_generic_reloc, /* special_function */
512 "R_MIPS_NONE", /* name */
513 false, /* partial_inplace */
514 0, /* src_mask */
515 0, /* dst_mask */
516 false), /* pcrel_offset */
517
518 /* 16 bit relocation. */
519 HOWTO (R_MIPS_16, /* type */
520 0, /* rightshift */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
522 16, /* bitsize */
523 false, /* pc_relative */
524 0, /* bitpos */
525 complain_overflow_signed, /* complain_on_overflow */
526 bfd_elf_generic_reloc, /* special_function */
527 "R_MIPS_16", /* name */
528 true, /* partial_inplace */
529 0x0000ffff, /* src_mask */
530 0x0000ffff, /* dst_mask */
531 false), /* pcrel_offset */
532
533 /* 32 bit relocation. */
534 HOWTO (R_MIPS_32, /* type */
535 0, /* rightshift */
536 2, /* size (0 = byte, 1 = short, 2 = long) */
537 32, /* bitsize */
538 false, /* pc_relative */
539 0, /* bitpos */
540 complain_overflow_dont, /* complain_on_overflow */
541 bfd_elf_generic_reloc, /* special_function */
542 "R_MIPS_32", /* name */
543 true, /* partial_inplace */
544 0xffffffff, /* src_mask */
545 0xffffffff, /* dst_mask */
546 false), /* pcrel_offset */
547
548 /* 32 bit symbol relative relocation. */
549 HOWTO (R_MIPS_REL32, /* type */
550 0, /* rightshift */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
552 32, /* bitsize */
553 false, /* pc_relative */
554 0, /* bitpos */
555 complain_overflow_dont, /* complain_on_overflow */
556 bfd_elf_generic_reloc, /* special_function */
557 "R_MIPS_REL32", /* name */
558 true, /* partial_inplace */
559 0xffffffff, /* src_mask */
560 0xffffffff, /* dst_mask */
561 false), /* pcrel_offset */
562
563 /* 26 bit jump address. */
564 HOWTO (R_MIPS_26, /* type */
565 2, /* rightshift */
566 2, /* size (0 = byte, 1 = short, 2 = long) */
567 26, /* bitsize */
568 false, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_dont, /* complain_on_overflow */
571 /* This needs complex overflow
572 detection, because the upper four
573 bits must match the PC + 4. */
574 bfd_elf_generic_reloc, /* special_function */
575 "R_MIPS_26", /* name */
576 true, /* partial_inplace */
577 0x03ffffff, /* src_mask */
578 0x03ffffff, /* dst_mask */
579 false), /* pcrel_offset */
580
581 /* High 16 bits of symbol value. */
582 HOWTO (R_MIPS_HI16, /* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 16, /* bitsize */
586 false, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_dont, /* complain_on_overflow */
589 _bfd_mips_elf_hi16_reloc, /* special_function */
590 "R_MIPS_HI16", /* name */
591 true, /* partial_inplace */
592 0x0000ffff, /* src_mask */
593 0x0000ffff, /* dst_mask */
594 false), /* pcrel_offset */
595
596 /* Low 16 bits of symbol value. */
597 HOWTO (R_MIPS_LO16, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 16, /* bitsize */
601 false, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_dont, /* complain_on_overflow */
604 _bfd_mips_elf_lo16_reloc, /* special_function */
605 "R_MIPS_LO16", /* name */
606 true, /* partial_inplace */
607 0x0000ffff, /* src_mask */
608 0x0000ffff, /* dst_mask */
609 false), /* pcrel_offset */
610
611 /* GP relative reference. */
612 HOWTO (R_MIPS_GPREL16, /* type */
613 0, /* rightshift */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
615 16, /* bitsize */
616 false, /* pc_relative */
617 0, /* bitpos */
618 complain_overflow_signed, /* complain_on_overflow */
619 _bfd_mips_elf_gprel16_reloc, /* special_function */
620 "R_MIPS_GPREL16", /* name */
621 true, /* partial_inplace */
622 0x0000ffff, /* src_mask */
623 0x0000ffff, /* dst_mask */
624 false), /* pcrel_offset */
625
626 /* Reference to literal section. */
627 HOWTO (R_MIPS_LITERAL, /* type */
628 0, /* rightshift */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
630 16, /* bitsize */
631 false, /* pc_relative */
632 0, /* bitpos */
633 complain_overflow_signed, /* complain_on_overflow */
634 _bfd_mips_elf_gprel16_reloc, /* special_function */
635 "R_MIPS_LITERAL", /* name */
636 true, /* partial_inplace */
637 0x0000ffff, /* src_mask */
638 0x0000ffff, /* dst_mask */
639 false), /* pcrel_offset */
640
641 /* Reference to global offset table. */
642 HOWTO (R_MIPS_GOT16, /* type */
643 0, /* rightshift */
644 2, /* size (0 = byte, 1 = short, 2 = long) */
645 16, /* bitsize */
646 false, /* pc_relative */
647 0, /* bitpos */
648 complain_overflow_signed, /* complain_on_overflow */
649 _bfd_mips_elf_got16_reloc, /* special_function */
650 "R_MIPS_GOT16", /* name */
651 true, /* partial_inplace */
652 0x0000ffff, /* src_mask */
653 0x0000ffff, /* dst_mask */
654 false), /* pcrel_offset */
655
656 /* 16 bit PC relative reference. */
657 HOWTO (R_MIPS_PC16, /* type */
658 0, /* rightshift */
659 2, /* size (0 = byte, 1 = short, 2 = long) */
660 16, /* bitsize */
661 true, /* pc_relative */
662 0, /* bitpos */
663 complain_overflow_signed, /* complain_on_overflow */
664 bfd_elf_generic_reloc, /* special_function */
665 "R_MIPS_PC16", /* name */
666 true, /* partial_inplace */
667 0x0000ffff, /* src_mask */
668 0x0000ffff, /* dst_mask */
669 true), /* pcrel_offset */
670
671 /* 16 bit call through global offset table. */
672 HOWTO (R_MIPS_CALL16, /* type */
673 0, /* rightshift */
674 2, /* size (0 = byte, 1 = short, 2 = long) */
675 16, /* bitsize */
676 false, /* pc_relative */
677 0, /* bitpos */
678 complain_overflow_signed, /* complain_on_overflow */
679 bfd_elf_generic_reloc, /* special_function */
680 "R_MIPS_CALL16", /* name */
681 true, /* partial_inplace */
682 0x0000ffff, /* src_mask */
683 0x0000ffff, /* dst_mask */
684 false), /* pcrel_offset */
685
686 /* 32 bit GP relative reference. */
687 HOWTO (R_MIPS_GPREL32, /* type */
688 0, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 32, /* bitsize */
691 false, /* pc_relative */
692 0, /* bitpos */
693 complain_overflow_dont, /* complain_on_overflow */
694 _bfd_mips_elf_gprel32_reloc, /* special_function */
695 "R_MIPS_GPREL32", /* name */
696 true, /* partial_inplace */
697 0xffffffff, /* src_mask */
698 0xffffffff, /* dst_mask */
699 false), /* pcrel_offset */
700
701 /* The remaining relocs are defined on Irix 5, although they are
702 not defined by the ABI. */
703 EMPTY_HOWTO (13),
704 EMPTY_HOWTO (14),
705 EMPTY_HOWTO (15),
706
707 /* A 5 bit shift field. */
708 HOWTO (R_MIPS_SHIFT5, /* type */
709 0, /* rightshift */
710 2, /* size (0 = byte, 1 = short, 2 = long) */
711 5, /* bitsize */
712 false, /* pc_relative */
713 6, /* bitpos */
714 complain_overflow_bitfield, /* complain_on_overflow */
715 bfd_elf_generic_reloc, /* special_function */
716 "R_MIPS_SHIFT5", /* name */
717 true, /* partial_inplace */
718 0x000007c0, /* src_mask */
719 0x000007c0, /* dst_mask */
720 false), /* pcrel_offset */
721
722 /* A 6 bit shift field. */
723 /* FIXME: This is not handled correctly; a special function is
724 needed to put the most significant bit in the right place. */
725 HOWTO (R_MIPS_SHIFT6, /* type */
726 0, /* rightshift */
727 2, /* size (0 = byte, 1 = short, 2 = long) */
728 6, /* bitsize */
729 false, /* pc_relative */
730 6, /* bitpos */
731 complain_overflow_bitfield, /* complain_on_overflow */
732 bfd_elf_generic_reloc, /* special_function */
733 "R_MIPS_SHIFT6", /* name */
734 true, /* partial_inplace */
735 0x000007c4, /* src_mask */
736 0x000007c4, /* dst_mask */
737 false), /* pcrel_offset */
738
739 /* A 64 bit relocation. */
740 HOWTO (R_MIPS_64, /* type */
741 0, /* rightshift */
742 4, /* size (0 = byte, 1 = short, 2 = long) */
743 64, /* bitsize */
744 false, /* pc_relative */
745 0, /* bitpos */
746 complain_overflow_dont, /* complain_on_overflow */
747 mips32_64bit_reloc, /* special_function */
748 "R_MIPS_64", /* name */
749 true, /* partial_inplace */
750 MINUS_ONE, /* src_mask */
751 MINUS_ONE, /* dst_mask */
752 false), /* pcrel_offset */
753
754 /* Displacement in the global offset table. */
755 HOWTO (R_MIPS_GOT_DISP, /* type */
756 0, /* rightshift */
757 2, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
759 false, /* pc_relative */
760 0, /* bitpos */
761 complain_overflow_signed, /* complain_on_overflow */
762 bfd_elf_generic_reloc, /* special_function */
763 "R_MIPS_GOT_DISP", /* name */
764 true, /* partial_inplace */
765 0x0000ffff, /* src_mask */
766 0x0000ffff, /* dst_mask */
767 false), /* pcrel_offset */
768
769 /* Displacement to page pointer in the global offset table. */
770 HOWTO (R_MIPS_GOT_PAGE, /* type */
771 0, /* rightshift */
772 2, /* size (0 = byte, 1 = short, 2 = long) */
773 16, /* bitsize */
774 false, /* pc_relative */
775 0, /* bitpos */
776 complain_overflow_signed, /* complain_on_overflow */
777 bfd_elf_generic_reloc, /* special_function */
778 "R_MIPS_GOT_PAGE", /* name */
779 true, /* partial_inplace */
780 0x0000ffff, /* src_mask */
781 0x0000ffff, /* dst_mask */
782 false), /* pcrel_offset */
783
784 /* Offset from page pointer in the global offset table. */
785 HOWTO (R_MIPS_GOT_OFST, /* type */
786 0, /* rightshift */
787 2, /* size (0 = byte, 1 = short, 2 = long) */
788 16, /* bitsize */
789 false, /* pc_relative */
790 0, /* bitpos */
791 complain_overflow_signed, /* complain_on_overflow */
792 bfd_elf_generic_reloc, /* special_function */
793 "R_MIPS_GOT_OFST", /* name */
794 true, /* partial_inplace */
795 0x0000ffff, /* src_mask */
796 0x0000ffff, /* dst_mask */
797 false), /* pcrel_offset */
798
799 /* High 16 bits of displacement in global offset table. */
800 HOWTO (R_MIPS_GOT_HI16, /* type */
801 0, /* rightshift */
802 2, /* size (0 = byte, 1 = short, 2 = long) */
803 16, /* bitsize */
804 false, /* pc_relative */
805 0, /* bitpos */
806 complain_overflow_dont, /* complain_on_overflow */
807 bfd_elf_generic_reloc, /* special_function */
808 "R_MIPS_GOT_HI16", /* name */
809 true, /* partial_inplace */
810 0x0000ffff, /* src_mask */
811 0x0000ffff, /* dst_mask */
812 false), /* pcrel_offset */
813
814 /* Low 16 bits of displacement in global offset table. */
815 HOWTO (R_MIPS_GOT_LO16, /* type */
816 0, /* rightshift */
817 2, /* size (0 = byte, 1 = short, 2 = long) */
818 16, /* bitsize */
819 false, /* pc_relative */
820 0, /* bitpos */
821 complain_overflow_dont, /* complain_on_overflow */
822 bfd_elf_generic_reloc, /* special_function */
823 "R_MIPS_GOT_LO16", /* name */
824 true, /* partial_inplace */
825 0x0000ffff, /* src_mask */
826 0x0000ffff, /* dst_mask */
827 false), /* pcrel_offset */
828
829 /* 64 bit subtraction. Used in the N32 ABI. */
830 HOWTO (R_MIPS_SUB, /* type */
831 0, /* rightshift */
832 4, /* size (0 = byte, 1 = short, 2 = long) */
833 64, /* bitsize */
834 false, /* pc_relative */
835 0, /* bitpos */
836 complain_overflow_dont, /* complain_on_overflow */
837 bfd_elf_generic_reloc, /* special_function */
838 "R_MIPS_SUB", /* name */
839 true, /* partial_inplace */
840 MINUS_ONE, /* src_mask */
841 MINUS_ONE, /* dst_mask */
842 false), /* pcrel_offset */
843
844 /* Used to cause the linker to insert and delete instructions? */
845 EMPTY_HOWTO (R_MIPS_INSERT_A),
846 EMPTY_HOWTO (R_MIPS_INSERT_B),
847 EMPTY_HOWTO (R_MIPS_DELETE),
848
849 /* Get the higher value of a 64 bit addend. */
850 HOWTO (R_MIPS_HIGHER, /* type */
851 0, /* rightshift */
852 2, /* size (0 = byte, 1 = short, 2 = long) */
853 16, /* bitsize */
854 false, /* pc_relative */
855 0, /* bitpos */
856 complain_overflow_dont, /* complain_on_overflow */
857 bfd_elf_generic_reloc, /* special_function */
858 "R_MIPS_HIGHER", /* name */
859 true, /* partial_inplace */
860 0x0000ffff, /* src_mask */
861 0x0000ffff, /* dst_mask */
862 false), /* pcrel_offset */
863
864 /* Get the highest value of a 64 bit addend. */
865 HOWTO (R_MIPS_HIGHEST, /* type */
866 0, /* rightshift */
867 2, /* size (0 = byte, 1 = short, 2 = long) */
868 16, /* bitsize */
869 false, /* pc_relative */
870 0, /* bitpos */
871 complain_overflow_dont, /* complain_on_overflow */
872 bfd_elf_generic_reloc, /* special_function */
873 "R_MIPS_HIGHEST", /* name */
874 true, /* partial_inplace */
875 0x0000ffff, /* src_mask */
876 0x0000ffff, /* dst_mask */
877 false), /* pcrel_offset */
878
879 /* High 16 bits of displacement in global offset table. */
880 HOWTO (R_MIPS_CALL_HI16, /* type */
881 0, /* rightshift */
882 2, /* size (0 = byte, 1 = short, 2 = long) */
883 16, /* bitsize */
884 false, /* pc_relative */
885 0, /* bitpos */
886 complain_overflow_dont, /* complain_on_overflow */
887 bfd_elf_generic_reloc, /* special_function */
888 "R_MIPS_CALL_HI16", /* name */
889 true, /* partial_inplace */
890 0x0000ffff, /* src_mask */
891 0x0000ffff, /* dst_mask */
892 false), /* pcrel_offset */
893
894 /* Low 16 bits of displacement in global offset table. */
895 HOWTO (R_MIPS_CALL_LO16, /* type */
896 0, /* rightshift */
897 2, /* size (0 = byte, 1 = short, 2 = long) */
898 16, /* bitsize */
899 false, /* pc_relative */
900 0, /* bitpos */
901 complain_overflow_dont, /* complain_on_overflow */
902 bfd_elf_generic_reloc, /* special_function */
903 "R_MIPS_CALL_LO16", /* name */
904 true, /* partial_inplace */
905 0x0000ffff, /* src_mask */
906 0x0000ffff, /* dst_mask */
907 false), /* pcrel_offset */
908
909 /* Section displacement. */
910 HOWTO (R_MIPS_SCN_DISP, /* type */
911 0, /* rightshift */
912 2, /* size (0 = byte, 1 = short, 2 = long) */
913 32, /* bitsize */
914 false, /* pc_relative */
915 0, /* bitpos */
916 complain_overflow_dont, /* complain_on_overflow */
917 bfd_elf_generic_reloc, /* special_function */
918 "R_MIPS_SCN_DISP", /* name */
919 true, /* partial_inplace */
920 0xffffffff, /* src_mask */
921 0xffffffff, /* dst_mask */
922 false), /* pcrel_offset */
923
924 EMPTY_HOWTO (R_MIPS_REL16),
925 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
926 EMPTY_HOWTO (R_MIPS_PJUMP),
927 EMPTY_HOWTO (R_MIPS_RELGOT),
928
929 /* Protected jump conversion. This is an optimization hint. No
930 relocation is required for correctness. */
931 HOWTO (R_MIPS_JALR, /* type */
932 0, /* rightshift */
933 2, /* size (0 = byte, 1 = short, 2 = long) */
934 32, /* bitsize */
935 false, /* pc_relative */
936 0, /* bitpos */
937 complain_overflow_dont, /* complain_on_overflow */
938 bfd_elf_generic_reloc, /* special_function */
939 "R_MIPS_JALR", /* name */
940 false, /* partial_inplace */
941 0x00000000, /* src_mask */
942 0x00000000, /* dst_mask */
943 false), /* pcrel_offset */
944};
945
946/* The relocation table used for SHT_RELA sections. */
947
948static reloc_howto_type elf_mips_howto_table_rela[] =
38b1a46c 949{
252b5132
RH
950 /* No relocation. */
951 HOWTO (R_MIPS_NONE, /* type */
952 0, /* rightshift */
953 0, /* size (0 = byte, 1 = short, 2 = long) */
954 0, /* bitsize */
955 false, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_MIPS_NONE", /* name */
960 false, /* partial_inplace */
961 0, /* src_mask */
962 0, /* dst_mask */
963 false), /* pcrel_offset */
964
965 /* 16 bit relocation. */
966 HOWTO (R_MIPS_16, /* type */
967 0, /* rightshift */
d75bc93d 968 2, /* size (0 = byte, 1 = short, 2 = long) */
252b5132
RH
969 16, /* bitsize */
970 false, /* pc_relative */
971 0, /* bitpos */
d75bc93d 972 complain_overflow_signed, /* complain_on_overflow */
252b5132
RH
973 bfd_elf_generic_reloc, /* special_function */
974 "R_MIPS_16", /* name */
d75bc93d
TS
975 false, /* partial_inplace */
976 0, /* src_mask */
977 0x0000, /* dst_mask */
252b5132
RH
978 false), /* pcrel_offset */
979
980 /* 32 bit relocation. */
981 HOWTO (R_MIPS_32, /* type */
982 0, /* rightshift */
983 2, /* size (0 = byte, 1 = short, 2 = long) */
984 32, /* bitsize */
985 false, /* pc_relative */
986 0, /* bitpos */
7e766c3b 987 complain_overflow_dont, /* complain_on_overflow */
252b5132
RH
988 bfd_elf_generic_reloc, /* special_function */
989 "R_MIPS_32", /* name */
d75bc93d
TS
990 false, /* partial_inplace */
991 0, /* src_mask */
252b5132
RH
992 0xffffffff, /* dst_mask */
993 false), /* pcrel_offset */
994
995 /* 32 bit symbol relative relocation. */
996 HOWTO (R_MIPS_REL32, /* type */
997 0, /* rightshift */
998 2, /* size (0 = byte, 1 = short, 2 = long) */
999 32, /* bitsize */
1000 false, /* pc_relative */
1001 0, /* bitpos */
7e766c3b 1002 complain_overflow_dont, /* complain_on_overflow */
252b5132
RH
1003 bfd_elf_generic_reloc, /* special_function */
1004 "R_MIPS_REL32", /* name */
d75bc93d
TS
1005 false, /* partial_inplace */
1006 0, /* src_mask */
252b5132
RH
1007 0xffffffff, /* dst_mask */
1008 false), /* pcrel_offset */
1009
062e2358 1010 /* 26 bit jump address. */
252b5132
RH
1011 HOWTO (R_MIPS_26, /* type */
1012 2, /* rightshift */
1013 2, /* size (0 = byte, 1 = short, 2 = long) */
1014 26, /* bitsize */
1015 false, /* pc_relative */
1016 0, /* bitpos */
1017 complain_overflow_dont, /* complain_on_overflow */
d75bc93d
TS
1018 /* This needs complex overflow
1019 detection, because the upper 36
9117d219 1020 bits must match the PC + 4. */
252b5132
RH
1021 bfd_elf_generic_reloc, /* special_function */
1022 "R_MIPS_26", /* name */
d75bc93d
TS
1023 false, /* partial_inplace */
1024 0, /* src_mask */
1025 0x03ffffff, /* dst_mask */
252b5132
RH
1026 false), /* pcrel_offset */
1027
d75bc93d 1028 /* R_MIPS_HI16 and R_MIPS_LO16 are unsupported for 64 bit REL. */
252b5132
RH
1029 /* High 16 bits of symbol value. */
1030 HOWTO (R_MIPS_HI16, /* type */
1031 0, /* rightshift */
1032 2, /* size (0 = byte, 1 = short, 2 = long) */
1033 16, /* bitsize */
1034 false, /* pc_relative */
1035 0, /* bitpos */
1036 complain_overflow_dont, /* complain_on_overflow */
d75bc93d 1037 bfd_elf_generic_reloc, /* special_function */
252b5132 1038 "R_MIPS_HI16", /* name */
d75bc93d
TS
1039 false, /* partial_inplace */
1040 0, /* src_mask */
1041 0x0000ffff, /* dst_mask */
252b5132
RH
1042 false), /* pcrel_offset */
1043
1044 /* Low 16 bits of symbol value. */
1045 HOWTO (R_MIPS_LO16, /* type */
1046 0, /* rightshift */
1047 2, /* size (0 = byte, 1 = short, 2 = long) */
1048 16, /* bitsize */
1049 false, /* pc_relative */
1050 0, /* bitpos */
1051 complain_overflow_dont, /* complain_on_overflow */
d75bc93d 1052 bfd_elf_generic_reloc, /* special_function */
252b5132 1053 "R_MIPS_LO16", /* name */
d75bc93d
TS
1054 false, /* partial_inplace */
1055 0, /* src_mask */
1056 0x0000ffff, /* dst_mask */
252b5132
RH
1057 false), /* pcrel_offset */
1058
1059 /* GP relative reference. */
1060 HOWTO (R_MIPS_GPREL16, /* type */
1061 0, /* rightshift */
1062 2, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 false, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_signed, /* complain_on_overflow */
1067 _bfd_mips_elf_gprel16_reloc, /* special_function */
1068 "R_MIPS_GPREL16", /* name */
d75bc93d
TS
1069 false, /* partial_inplace */
1070 0, /* src_mask */
1071 0x0000ffff, /* dst_mask */
252b5132
RH
1072 false), /* pcrel_offset */
1073
1074 /* Reference to literal section. */
1075 HOWTO (R_MIPS_LITERAL, /* type */
1076 0, /* rightshift */
1077 2, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 false, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_signed, /* complain_on_overflow */
1082 _bfd_mips_elf_gprel16_reloc, /* special_function */
1083 "R_MIPS_LITERAL", /* name */
d75bc93d
TS
1084 false, /* partial_inplace */
1085 0, /* src_mask */
1086 0x0000ffff, /* dst_mask */
252b5132
RH
1087 false), /* pcrel_offset */
1088
1089 /* Reference to global offset table. */
d75bc93d 1090 /* FIXME: This is not handled correctly. */
252b5132
RH
1091 HOWTO (R_MIPS_GOT16, /* type */
1092 0, /* rightshift */
1093 2, /* size (0 = byte, 1 = short, 2 = long) */
1094 16, /* bitsize */
1095 false, /* pc_relative */
1096 0, /* bitpos */
1097 complain_overflow_signed, /* complain_on_overflow */
d75bc93d 1098 bfd_elf_generic_reloc, /* special_function */
252b5132
RH
1099 "R_MIPS_GOT16", /* name */
1100 false, /* partial_inplace */
d75bc93d
TS
1101 0, /* src_mask */
1102 0x0000ffff, /* dst_mask */
252b5132
RH
1103 false), /* pcrel_offset */
1104
1105 /* 16 bit PC relative reference. */
1106 HOWTO (R_MIPS_PC16, /* type */
1107 0, /* rightshift */
1108 2, /* size (0 = byte, 1 = short, 2 = long) */
1109 16, /* bitsize */
1110 true, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_signed, /* complain_on_overflow */
1113 bfd_elf_generic_reloc, /* special_function */
1114 "R_MIPS_PC16", /* name */
d75bc93d
TS
1115 false, /* partial_inplace */
1116 0, /* src_mask */
1117 0x0000ffff, /* dst_mask */
bb2d6cd7 1118 true), /* pcrel_offset */
252b5132
RH
1119
1120 /* 16 bit call through global offset table. */
d75bc93d 1121 /* FIXME: This is not handled correctly. */
252b5132
RH
1122 HOWTO (R_MIPS_CALL16, /* type */
1123 0, /* rightshift */
1124 2, /* size (0 = byte, 1 = short, 2 = long) */
1125 16, /* bitsize */
1126 false, /* pc_relative */
1127 0, /* bitpos */
1128 complain_overflow_signed, /* complain_on_overflow */
1129 bfd_elf_generic_reloc, /* special_function */
1130 "R_MIPS_CALL16", /* name */
1131 false, /* partial_inplace */
d75bc93d
TS
1132 0, /* src_mask */
1133 0x0000ffff, /* dst_mask */
252b5132
RH
1134 false), /* pcrel_offset */
1135
1136 /* 32 bit GP relative reference. */
1137 HOWTO (R_MIPS_GPREL32, /* type */
1138 0, /* rightshift */
1139 2, /* size (0 = byte, 1 = short, 2 = long) */
1140 32, /* bitsize */
1141 false, /* pc_relative */
1142 0, /* bitpos */
7e766c3b 1143 complain_overflow_dont, /* complain_on_overflow */
252b5132
RH
1144 _bfd_mips_elf_gprel32_reloc, /* special_function */
1145 "R_MIPS_GPREL32", /* name */
d75bc93d
TS
1146 false, /* partial_inplace */
1147 0, /* src_mask */
252b5132
RH
1148 0xffffffff, /* dst_mask */
1149 false), /* pcrel_offset */
1150
d75bc93d
TS
1151 EMPTY_HOWTO (13),
1152 EMPTY_HOWTO (14),
1153 EMPTY_HOWTO (15),
252b5132
RH
1154
1155 /* A 5 bit shift field. */
1156 HOWTO (R_MIPS_SHIFT5, /* type */
1157 0, /* rightshift */
1158 2, /* size (0 = byte, 1 = short, 2 = long) */
1159 5, /* bitsize */
1160 false, /* pc_relative */
1161 6, /* bitpos */
1162 complain_overflow_bitfield, /* complain_on_overflow */
1163 bfd_elf_generic_reloc, /* special_function */
1164 "R_MIPS_SHIFT5", /* name */
d75bc93d
TS
1165 false, /* partial_inplace */
1166 0, /* src_mask */
252b5132
RH
1167 0x000007c0, /* dst_mask */
1168 false), /* pcrel_offset */
1169
1170 /* A 6 bit shift field. */
d75bc93d 1171 /* FIXME: Not handled correctly. */
252b5132
RH
1172 HOWTO (R_MIPS_SHIFT6, /* type */
1173 0, /* rightshift */
1174 2, /* size (0 = byte, 1 = short, 2 = long) */
1175 6, /* bitsize */
1176 false, /* pc_relative */
1177 6, /* bitpos */
1178 complain_overflow_bitfield, /* complain_on_overflow */
d75bc93d 1179 bfd_elf_generic_reloc, /* special_function */
252b5132 1180 "R_MIPS_SHIFT6", /* name */
d75bc93d
TS
1181 false, /* partial_inplace */
1182 0, /* src_mask */
252b5132
RH
1183 0x000007c4, /* dst_mask */
1184 false), /* pcrel_offset */
1185
d75bc93d 1186 /* 64 bit relocation. */
252b5132
RH
1187 HOWTO (R_MIPS_64, /* type */
1188 0, /* rightshift */
a3c7651d
MM
1189 4, /* size (0 = byte, 1 = short, 2 = long) */
1190 64, /* bitsize */
252b5132
RH
1191 false, /* pc_relative */
1192 0, /* bitpos */
7e766c3b 1193 complain_overflow_dont, /* complain_on_overflow */
d75bc93d 1194 bfd_elf_generic_reloc, /* special_function */
252b5132 1195 "R_MIPS_64", /* name */
d75bc93d
TS
1196 false, /* partial_inplace */
1197 0, /* src_mask */
a3c7651d 1198 MINUS_ONE, /* dst_mask */
252b5132
RH
1199 false), /* pcrel_offset */
1200
1201 /* Displacement in the global offset table. */
d75bc93d 1202 /* FIXME: Not handled correctly. */
252b5132
RH
1203 HOWTO (R_MIPS_GOT_DISP, /* type */
1204 0, /* rightshift */
1205 2, /* size (0 = byte, 1 = short, 2 = long) */
1206 16, /* bitsize */
1207 false, /* pc_relative */
1208 0, /* bitpos */
7e766c3b 1209 complain_overflow_signed, /* complain_on_overflow */
252b5132
RH
1210 bfd_elf_generic_reloc, /* special_function */
1211 "R_MIPS_GOT_DISP", /* name */
d75bc93d
TS
1212 false, /* partial_inplace */
1213 0, /* src_mask */
252b5132
RH
1214 0x0000ffff, /* dst_mask */
1215 false), /* pcrel_offset */
1216
1217 /* Displacement to page pointer in the global offset table. */
d75bc93d 1218 /* FIXME: Not handled correctly. */
252b5132
RH
1219 HOWTO (R_MIPS_GOT_PAGE, /* type */
1220 0, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 16, /* bitsize */
1223 false, /* pc_relative */
1224 0, /* bitpos */
7e766c3b 1225 complain_overflow_signed, /* complain_on_overflow */
252b5132
RH
1226 bfd_elf_generic_reloc, /* special_function */
1227 "R_MIPS_GOT_PAGE", /* name */
d75bc93d
TS
1228 false, /* partial_inplace */
1229 0, /* src_mask */
252b5132
RH
1230 0x0000ffff, /* dst_mask */
1231 false), /* pcrel_offset */
1232
1233 /* Offset from page pointer in the global offset table. */
d75bc93d 1234 /* FIXME: Not handled correctly. */
252b5132
RH
1235 HOWTO (R_MIPS_GOT_OFST, /* type */
1236 0, /* rightshift */
1237 2, /* size (0 = byte, 1 = short, 2 = long) */
1238 16, /* bitsize */
1239 false, /* pc_relative */
1240 0, /* bitpos */
7e766c3b 1241 complain_overflow_signed, /* complain_on_overflow */
252b5132
RH
1242 bfd_elf_generic_reloc, /* special_function */
1243 "R_MIPS_GOT_OFST", /* name */
d75bc93d
TS
1244 false, /* partial_inplace */
1245 0, /* src_mask */
252b5132
RH
1246 0x0000ffff, /* dst_mask */
1247 false), /* pcrel_offset */
1248
1249 /* High 16 bits of displacement in global offset table. */
d75bc93d 1250 /* FIXME: Not handled correctly. */
252b5132
RH
1251 HOWTO (R_MIPS_GOT_HI16, /* type */
1252 0, /* rightshift */
1253 2, /* size (0 = byte, 1 = short, 2 = long) */
1254 16, /* bitsize */
1255 false, /* pc_relative */
1256 0, /* bitpos */
1257 complain_overflow_dont, /* complain_on_overflow */
1258 bfd_elf_generic_reloc, /* special_function */
1259 "R_MIPS_GOT_HI16", /* name */
d75bc93d
TS
1260 false, /* partial_inplace */
1261 0, /* src_mask */
252b5132
RH
1262 0x0000ffff, /* dst_mask */
1263 false), /* pcrel_offset */
1264
1265 /* Low 16 bits of displacement in global offset table. */
d75bc93d 1266 /* FIXME: Not handled correctly. */
252b5132
RH
1267 HOWTO (R_MIPS_GOT_LO16, /* type */
1268 0, /* rightshift */
1269 2, /* size (0 = byte, 1 = short, 2 = long) */
1270 16, /* bitsize */
1271 false, /* pc_relative */
1272 0, /* bitpos */
1273 complain_overflow_dont, /* complain_on_overflow */
1274 bfd_elf_generic_reloc, /* special_function */
1275 "R_MIPS_GOT_LO16", /* name */
d75bc93d
TS
1276 false, /* partial_inplace */
1277 0, /* src_mask */
252b5132
RH
1278 0x0000ffff, /* dst_mask */
1279 false), /* pcrel_offset */
1280
d75bc93d
TS
1281 /* 64 bit substraction. */
1282 /* FIXME: Not handled correctly. */
3f830999
MM
1283 HOWTO (R_MIPS_SUB, /* type */
1284 0, /* rightshift */
1285 4, /* size (0 = byte, 1 = short, 2 = long) */
1286 64, /* bitsize */
1287 false, /* pc_relative */
1288 0, /* bitpos */
7e766c3b 1289 complain_overflow_dont, /* complain_on_overflow */
3f830999
MM
1290 bfd_elf_generic_reloc, /* special_function */
1291 "R_MIPS_SUB", /* name */
d75bc93d
TS
1292 false, /* partial_inplace */
1293 0, /* src_mask */
3f830999
MM
1294 MINUS_ONE, /* dst_mask */
1295 false), /* pcrel_offset */
252b5132 1296
d75bc93d
TS
1297 /* Insert the addend as an instruction. */
1298 /* FIXME: Not handled correctly. */
1299 HOWTO (R_MIPS_INSERT_A, /* type */
1300 0, /* rightshift */
1301 2, /* size (0 = byte, 1 = short, 2 = long) */
1302 32, /* bitsize */
1303 false, /* pc_relative */
1304 0, /* bitpos */
1305 complain_overflow_dont, /* complain_on_overflow */
1306 bfd_elf_generic_reloc, /* special_function */
1307 "R_MIPS_INSERT_A", /* name */
1308 false, /* partial_inplace */
1309 0, /* src_mask */
1310 0xffffffff, /* dst_mask */
1311 false), /* pcrel_offset */
1312
1313 /* Insert the addend as an instruction, and change all relocations
1314 to refer to the old instruction at the address. */
1315 /* FIXME: Not handled correctly. */
1316 HOWTO (R_MIPS_INSERT_B, /* type */
1317 0, /* rightshift */
1318 2, /* size (0 = byte, 1 = short, 2 = long) */
1319 32, /* bitsize */
1320 false, /* pc_relative */
1321 0, /* bitpos */
1322 complain_overflow_dont, /* complain_on_overflow */
1323 bfd_elf_generic_reloc, /* special_function */
1324 "R_MIPS_INSERT_B", /* name */
1325 false, /* partial_inplace */
1326 0, /* src_mask */
1327 0xffffffff, /* dst_mask */
1328 false), /* pcrel_offset */
1329
1330 /* Delete a 32 bit instruction. */
1331 /* FIXME: Not handled correctly. */
1332 HOWTO (R_MIPS_DELETE, /* type */
1333 0, /* rightshift */
1334 2, /* size (0 = byte, 1 = short, 2 = long) */
1335 32, /* bitsize */
1336 false, /* pc_relative */
1337 0, /* bitpos */
1338 complain_overflow_dont, /* complain_on_overflow */
1339 bfd_elf_generic_reloc, /* special_function */
1340 "R_MIPS_DELETE", /* name */
1341 false, /* partial_inplace */
1342 0, /* src_mask */
1343 0xffffffff, /* dst_mask */
1344 false), /* pcrel_offset */
252b5132 1345
103186c6
MM
1346 /* Get the higher value of a 64 bit addend. */
1347 HOWTO (R_MIPS_HIGHER, /* type */
1348 0, /* rightshift */
1349 2, /* size (0 = byte, 1 = short, 2 = long) */
1350 16, /* bitsize */
1351 false, /* pc_relative */
1352 0, /* bitpos */
1353 complain_overflow_dont, /* complain_on_overflow */
d75bc93d 1354 bfd_elf_generic_reloc, /* special_function */
103186c6 1355 "R_MIPS_HIGHER", /* name */
d75bc93d
TS
1356 false, /* partial_inplace */
1357 0, /* src_mask */
1358 0x0000ffff, /* dst_mask */
103186c6
MM
1359 false), /* pcrel_offset */
1360
1361 /* Get the highest value of a 64 bit addend. */
1362 HOWTO (R_MIPS_HIGHEST, /* type */
1363 0, /* rightshift */
1364 2, /* size (0 = byte, 1 = short, 2 = long) */
1365 16, /* bitsize */
1366 false, /* pc_relative */
1367 0, /* bitpos */
1368 complain_overflow_dont, /* complain_on_overflow */
d75bc93d 1369 bfd_elf_generic_reloc, /* special_function */
103186c6 1370 "R_MIPS_HIGHEST", /* name */
d75bc93d
TS
1371 false, /* partial_inplace */
1372 0, /* src_mask */
1373 0x0000ffff, /* dst_mask */
103186c6 1374 false), /* pcrel_offset */
252b5132
RH
1375
1376 /* High 16 bits of displacement in global offset table. */
d75bc93d 1377 /* FIXME: Not handled correctly. */
252b5132
RH
1378 HOWTO (R_MIPS_CALL_HI16, /* type */
1379 0, /* rightshift */
1380 2, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 false, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 bfd_elf_generic_reloc, /* special_function */
1386 "R_MIPS_CALL_HI16", /* name */
d75bc93d
TS
1387 false, /* partial_inplace */
1388 0, /* src_mask */
252b5132
RH
1389 0x0000ffff, /* dst_mask */
1390 false), /* pcrel_offset */
1391
1392 /* Low 16 bits of displacement in global offset table. */
d75bc93d 1393 /* FIXME: Not handled correctly. */
252b5132
RH
1394 HOWTO (R_MIPS_CALL_LO16, /* type */
1395 0, /* rightshift */
1396 2, /* size (0 = byte, 1 = short, 2 = long) */
1397 16, /* bitsize */
1398 false, /* pc_relative */
1399 0, /* bitpos */
1400 complain_overflow_dont, /* complain_on_overflow */
1401 bfd_elf_generic_reloc, /* special_function */
1402 "R_MIPS_CALL_LO16", /* name */
d75bc93d
TS
1403 false, /* partial_inplace */
1404 0, /* src_mask */
252b5132
RH
1405 0x0000ffff, /* dst_mask */
1406 false), /* pcrel_offset */
1407
d75bc93d
TS
1408 /* Section displacement, used by an associated event location section. */
1409 /* FIXME: Not handled correctly. */
1410 HOWTO (R_MIPS_SCN_DISP, /* type */
7403cb63
MM
1411 0, /* rightshift */
1412 2, /* size (0 = byte, 1 = short, 2 = long) */
1413 32, /* bitsize */
1414 false, /* pc_relative */
1415 0, /* bitpos */
1416 complain_overflow_dont, /* complain_on_overflow */
1417 bfd_elf_generic_reloc, /* special_function */
d75bc93d
TS
1418 "R_MIPS_SCN_DISP", /* name */
1419 false, /* partial_inplace */
1420 0, /* src_mask */
7403cb63
MM
1421 0xffffffff, /* dst_mask */
1422 false), /* pcrel_offset */
1423
d75bc93d
TS
1424 HOWTO (R_MIPS_REL16, /* type */
1425 0, /* rightshift */
1426 1, /* size (0 = byte, 1 = short, 2 = long) */
1427 16, /* bitsize */
1428 false, /* pc_relative */
1429 0, /* bitpos */
1430 complain_overflow_signed, /* complain_on_overflow */
1431 bfd_elf_generic_reloc, /* special_function */
1432 "R_MIPS_REL16", /* name */
1433 false, /* partial_inplace */
1434 0, /* src_mask */
1435 0xffff, /* dst_mask */
1436 false), /* pcrel_offset */
1437
1438 /* These two are obsolete. */
5f771d47
ILT
1439 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
1440 EMPTY_HOWTO (R_MIPS_PJUMP),
d75bc93d
TS
1441
1442 /* Similiar to R_MIPS_REL32, but used for relocations in a GOT section.
1443 It must be used for multigot GOT's (and only there). */
1444 HOWTO (R_MIPS_RELGOT, /* type */
1445 0, /* rightshift */
1446 2, /* size (0 = byte, 1 = short, 2 = long) */
1447 32, /* bitsize */
1448 false, /* pc_relative */
1449 0, /* bitpos */
1450 complain_overflow_dont, /* complain_on_overflow */
1451 bfd_elf_generic_reloc, /* special_function */
1452 "R_MIPS_RELGOT", /* name */
1453 false, /* partial_inplace */
1454 0, /* src_mask */
1455 0xffffffff, /* dst_mask */
1456 false), /* pcrel_offset */
d2905643 1457
be3ccd9c 1458 /* Protected jump conversion. This is an optimization hint. No
d2905643
MM
1459 relocation is required for correctness. */
1460 HOWTO (R_MIPS_JALR, /* type */
1461 0, /* rightshift */
d75bc93d
TS
1462 2, /* size (0 = byte, 1 = short, 2 = long) */
1463 32, /* bitsize */
d2905643
MM
1464 false, /* pc_relative */
1465 0, /* bitpos */
1466 complain_overflow_dont, /* complain_on_overflow */
1467 bfd_elf_generic_reloc, /* special_function */
1468 "R_MIPS_JALR", /* name */
1469 false, /* partial_inplace */
d75bc93d
TS
1470 0, /* src_mask */
1471 0xffffffff, /* dst_mask */
d2905643 1472 false), /* pcrel_offset */
252b5132
RH
1473};
1474
1475/* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
1476 is a hack to make the linker think that we need 64 bit values. */
1477static reloc_howto_type elf_mips_ctor64_howto =
1478 HOWTO (R_MIPS_64, /* type */
1479 0, /* rightshift */
1480 4, /* size (0 = byte, 1 = short, 2 = long) */
1481 32, /* bitsize */
1482 false, /* pc_relative */
1483 0, /* bitpos */
1484 complain_overflow_signed, /* complain_on_overflow */
1485 mips32_64bit_reloc, /* special_function */
1486 "R_MIPS_64", /* name */
1487 true, /* partial_inplace */
1488 0xffffffff, /* src_mask */
1489 0xffffffff, /* dst_mask */
1490 false); /* pcrel_offset */
1491
1492/* The reloc used for the mips16 jump instruction. */
1493static reloc_howto_type elf_mips16_jump_howto =
1494 HOWTO (R_MIPS16_26, /* type */
1495 2, /* rightshift */
1496 2, /* size (0 = byte, 1 = short, 2 = long) */
1497 26, /* bitsize */
1498 false, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_dont, /* complain_on_overflow */
1501 /* This needs complex overflow
1502 detection, because the upper four
1503 bits must match the PC. */
1504 mips16_jump_reloc, /* special_function */
1505 "R_MIPS16_26", /* name */
1506 true, /* partial_inplace */
1507 0x3ffffff, /* src_mask */
1508 0x3ffffff, /* dst_mask */
1509 false); /* pcrel_offset */
1510
b7233c24 1511/* The reloc used for the mips16 gprel instruction. */
252b5132
RH
1512static reloc_howto_type elf_mips16_gprel_howto =
1513 HOWTO (R_MIPS16_GPREL, /* type */
1514 0, /* rightshift */
1515 2, /* size (0 = byte, 1 = short, 2 = long) */
1516 16, /* bitsize */
1517 false, /* pc_relative */
1518 0, /* bitpos */
1519 complain_overflow_signed, /* complain_on_overflow */
1520 mips16_gprel_reloc, /* special_function */
1521 "R_MIPS16_GPREL", /* name */
1522 true, /* partial_inplace */
b7233c24
MM
1523 0x07ff001f, /* src_mask */
1524 0x07ff001f, /* dst_mask */
252b5132
RH
1525 false); /* pcrel_offset */
1526
bb2d6cd7
GK
1527/* GNU extensions for embedded-pic. */
1528/* High 16 bits of symbol value, pc-relative. */
1529static reloc_howto_type elf_mips_gnu_rel_hi16 =
1530 HOWTO (R_MIPS_GNU_REL_HI16, /* type */
1531 0, /* rightshift */
1532 2, /* size (0 = byte, 1 = short, 2 = long) */
1533 16, /* bitsize */
1534 true, /* pc_relative */
1535 0, /* bitpos */
1536 complain_overflow_dont, /* complain_on_overflow */
1537 _bfd_mips_elf_hi16_reloc, /* special_function */
1538 "R_MIPS_GNU_REL_HI16", /* name */
1539 true, /* partial_inplace */
1540 0xffff, /* src_mask */
1541 0xffff, /* dst_mask */
1542 true); /* pcrel_offset */
1543
1544/* Low 16 bits of symbol value, pc-relative. */
1545static reloc_howto_type elf_mips_gnu_rel_lo16 =
1546 HOWTO (R_MIPS_GNU_REL_LO16, /* type */
1547 0, /* rightshift */
1548 2, /* size (0 = byte, 1 = short, 2 = long) */
1549 16, /* bitsize */
1550 true, /* pc_relative */
1551 0, /* bitpos */
1552 complain_overflow_dont, /* complain_on_overflow */
1553 _bfd_mips_elf_lo16_reloc, /* special_function */
1554 "R_MIPS_GNU_REL_LO16", /* name */
1555 true, /* partial_inplace */
1556 0xffff, /* src_mask */
1557 0xffff, /* dst_mask */
1558 true); /* pcrel_offset */
1559
1560/* 16 bit offset for pc-relative branches. */
1561static reloc_howto_type elf_mips_gnu_rel16_s2 =
1562 HOWTO (R_MIPS_GNU_REL16_S2, /* type */
1563 2, /* rightshift */
1564 2, /* size (0 = byte, 1 = short, 2 = long) */
1565 16, /* bitsize */
1566 true, /* pc_relative */
1567 0, /* bitpos */
1568 complain_overflow_signed, /* complain_on_overflow */
1569 bfd_elf_generic_reloc, /* special_function */
1570 "R_MIPS_GNU_REL16_S2", /* name */
1571 true, /* partial_inplace */
1572 0xffff, /* src_mask */
1573 0xffff, /* dst_mask */
1574 true); /* pcrel_offset */
1575
1576/* 64 bit pc-relative. */
1577static reloc_howto_type elf_mips_gnu_pcrel64 =
1578 HOWTO (R_MIPS_PC64, /* type */
1579 0, /* rightshift */
1580 4, /* size (0 = byte, 1 = short, 2 = long) */
1581 64, /* bitsize */
1582 true, /* pc_relative */
1583 0, /* bitpos */
1584 complain_overflow_signed, /* complain_on_overflow */
1585 bfd_elf_generic_reloc, /* special_function */
1586 "R_MIPS_PC64", /* name */
1587 true, /* partial_inplace */
1588 MINUS_ONE, /* src_mask */
1589 MINUS_ONE, /* dst_mask */
1590 true); /* pcrel_offset */
1591
1592/* 32 bit pc-relative. */
1593static reloc_howto_type elf_mips_gnu_pcrel32 =
1594 HOWTO (R_MIPS_PC32, /* type */
1595 0, /* rightshift */
1596 2, /* size (0 = byte, 1 = short, 2 = long) */
1597 32, /* bitsize */
1598 true, /* pc_relative */
1599 0, /* bitpos */
1600 complain_overflow_signed, /* complain_on_overflow */
1601 bfd_elf_generic_reloc, /* special_function */
1602 "R_MIPS_PC32", /* name */
1603 true, /* partial_inplace */
1604 0xffffffff, /* src_mask */
1605 0xffffffff, /* dst_mask */
1606 true); /* pcrel_offset */
1607
252b5132
RH
1608/* GNU extension to record C++ vtable hierarchy */
1609static reloc_howto_type elf_mips_gnu_vtinherit_howto =
1610 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
1611 0, /* rightshift */
1612 2, /* size (0 = byte, 1 = short, 2 = long) */
1613 0, /* bitsize */
1614 false, /* pc_relative */
1615 0, /* bitpos */
1616 complain_overflow_dont, /* complain_on_overflow */
1617 NULL, /* special_function */
1618 "R_MIPS_GNU_VTINHERIT", /* name */
1619 false, /* partial_inplace */
1620 0, /* src_mask */
1621 0, /* dst_mask */
1622 false); /* pcrel_offset */
1623
1624/* GNU extension to record C++ vtable member usage */
1625static reloc_howto_type elf_mips_gnu_vtentry_howto =
1626 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
1627 0, /* rightshift */
1628 2, /* size (0 = byte, 1 = short, 2 = long) */
1629 0, /* bitsize */
1630 false, /* pc_relative */
1631 0, /* bitpos */
1632 complain_overflow_dont, /* complain_on_overflow */
1633 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
1634 "R_MIPS_GNU_VTENTRY", /* name */
1635 false, /* partial_inplace */
1636 0, /* src_mask */
1637 0, /* dst_mask */
1638 false); /* pcrel_offset */
1639
1640/* Do a R_MIPS_HI16 relocation. This has to be done in combination
1641 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
1642 the HI16. Here we just save the information we need; we do the
1643 actual relocation when we see the LO16. MIPS ELF requires that the
1644 LO16 immediately follow the HI16. As a GNU extension, we permit an
1645 arbitrary number of HI16 relocs to be associated with a single LO16
1646 reloc. This extension permits gcc to output the HI and LO relocs
1647 itself. */
1648
38b1a46c
NC
1649struct mips_hi16
1650{
252b5132
RH
1651 struct mips_hi16 *next;
1652 bfd_byte *addr;
1653 bfd_vma addend;
1654};
1655
1656/* FIXME: This should not be a static variable. */
1657
1658static struct mips_hi16 *mips_hi16_list;
1659
1660bfd_reloc_status_type
1661_bfd_mips_elf_hi16_reloc (abfd,
1662 reloc_entry,
1663 symbol,
1664 data,
1665 input_section,
1666 output_bfd,
1667 error_message)
5f771d47 1668 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1669 arelent *reloc_entry;
1670 asymbol *symbol;
1671 PTR data;
1672 asection *input_section;
1673 bfd *output_bfd;
1674 char **error_message;
1675{
1676 bfd_reloc_status_type ret;
1677 bfd_vma relocation;
1678 struct mips_hi16 *n;
1679
1680 /* If we're relocating, and this an external symbol, we don't want
1681 to change anything. */
1682 if (output_bfd != (bfd *) NULL
1683 && (symbol->flags & BSF_SECTION_SYM) == 0
1684 && reloc_entry->addend == 0)
1685 {
1686 reloc_entry->address += input_section->output_offset;
1687 return bfd_reloc_ok;
1688 }
1689
1690 ret = bfd_reloc_ok;
1691
1692 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1693 {
1694 boolean relocateable;
1695 bfd_vma gp;
1696
1697 if (ret == bfd_reloc_undefined)
1698 abort ();
1699
1700 if (output_bfd != NULL)
1701 relocateable = true;
1702 else
1703 {
1704 relocateable = false;
1705 output_bfd = symbol->section->output_section->owner;
1706 }
1707
1708 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1709 error_message, &gp);
1710 if (ret != bfd_reloc_ok)
1711 return ret;
1712
1713 relocation = gp - reloc_entry->address;
1714 }
1715 else
1716 {
1717 if (bfd_is_und_section (symbol->section)
1718 && output_bfd == (bfd *) NULL)
1719 ret = bfd_reloc_undefined;
1720
1721 if (bfd_is_com_section (symbol->section))
1722 relocation = 0;
1723 else
1724 relocation = symbol->value;
1725 }
1726
1727 relocation += symbol->section->output_section->vma;
1728 relocation += symbol->section->output_offset;
1729 relocation += reloc_entry->addend;
afdbd6d0
CD
1730 if (reloc_entry->howto->pc_relative)
1731 relocation -= reloc_entry->address;
252b5132
RH
1732
1733 if (reloc_entry->address > input_section->_cooked_size)
1734 return bfd_reloc_outofrange;
1735
1736 /* Save the information, and let LO16 do the actual relocation. */
dc810e39 1737 n = (struct mips_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
252b5132
RH
1738 if (n == NULL)
1739 return bfd_reloc_outofrange;
1740 n->addr = (bfd_byte *) data + reloc_entry->address;
1741 n->addend = relocation;
1742 n->next = mips_hi16_list;
1743 mips_hi16_list = n;
1744
1745 if (output_bfd != (bfd *) NULL)
1746 reloc_entry->address += input_section->output_offset;
1747
1748 return ret;
1749}
1750
1751/* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1752 inplace relocation; this function exists in order to do the
1753 R_MIPS_HI16 relocation described above. */
1754
1755bfd_reloc_status_type
1756_bfd_mips_elf_lo16_reloc (abfd,
1757 reloc_entry,
1758 symbol,
1759 data,
1760 input_section,
1761 output_bfd,
1762 error_message)
1763 bfd *abfd;
1764 arelent *reloc_entry;
1765 asymbol *symbol;
1766 PTR data;
1767 asection *input_section;
1768 bfd *output_bfd;
1769 char **error_message;
1770{
1771 arelent gp_disp_relent;
1772
1773 if (mips_hi16_list != NULL)
1774 {
1775 struct mips_hi16 *l;
1776
1777 l = mips_hi16_list;
1778 while (l != NULL)
1779 {
1780 unsigned long insn;
1781 unsigned long val;
1782 unsigned long vallo;
1783 struct mips_hi16 *next;
1784
1785 /* Do the HI16 relocation. Note that we actually don't need
1786 to know anything about the LO16 itself, except where to
1787 find the low 16 bits of the addend needed by the LO16. */
1788 insn = bfd_get_32 (abfd, l->addr);
3f0a7f7f
AM
1789 vallo = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1790
1791 /* The low order 16 bits are always treated as a signed
1792 value. */
1793 vallo = ((vallo & 0xffff) ^ 0x8000) - 0x8000;
252b5132
RH
1794 val = ((insn & 0xffff) << 16) + vallo;
1795 val += l->addend;
1796
3f0a7f7f
AM
1797 /* At this point, "val" has the value of the combined HI/LO
1798 pair. If the low order 16 bits (which will be used for
1799 the LO16 insn) are negative, then we will need an
1800 adjustment for the high order 16 bits. */
1801 val += 0x8000;
1802 val = (val >> 16) & 0xffff;
1803
1804 insn &= ~ (bfd_vma) 0xffff;
1805 insn |= val;
dc810e39 1806 bfd_put_32 (abfd, (bfd_vma) insn, l->addr);
252b5132
RH
1807
1808 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1809 {
1810 gp_disp_relent = *reloc_entry;
1811 reloc_entry = &gp_disp_relent;
1812 reloc_entry->addend = l->addend;
1813 }
1814
1815 next = l->next;
1816 free (l);
1817 l = next;
1818 }
1819
1820 mips_hi16_list = NULL;
1821 }
1822 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1823 {
1824 bfd_reloc_status_type ret;
1825 bfd_vma gp, relocation;
1826
1827 /* FIXME: Does this case ever occur? */
1828
1829 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1830 if (ret != bfd_reloc_ok)
1831 return ret;
1832
1833 relocation = gp - reloc_entry->address;
1834 relocation += symbol->section->output_section->vma;
1835 relocation += symbol->section->output_offset;
1836 relocation += reloc_entry->addend;
1837
1838 if (reloc_entry->address > input_section->_cooked_size)
1839 return bfd_reloc_outofrange;
1840
1841 gp_disp_relent = *reloc_entry;
1842 reloc_entry = &gp_disp_relent;
1843 reloc_entry->addend = relocation - 4;
1844 }
1845
1846 /* Now do the LO16 reloc in the usual way. */
1847 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1848 input_section, output_bfd, error_message);
1849}
1850
1851/* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1852 table used for PIC code. If the symbol is an external symbol, the
1853 instruction is modified to contain the offset of the appropriate
1854 entry in the global offset table. If the symbol is a section
1855 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1856 addends are combined to form the real addend against the section
1857 symbol; the GOT16 is modified to contain the offset of an entry in
1858 the global offset table, and the LO16 is modified to offset it
1859 appropriately. Thus an offset larger than 16 bits requires a
1860 modified value in the global offset table.
1861
1862 This implementation suffices for the assembler, but the linker does
1863 not yet know how to create global offset tables. */
1864
1865bfd_reloc_status_type
1866_bfd_mips_elf_got16_reloc (abfd,
1867 reloc_entry,
1868 symbol,
1869 data,
1870 input_section,
1871 output_bfd,
1872 error_message)
1873 bfd *abfd;
1874 arelent *reloc_entry;
1875 asymbol *symbol;
1876 PTR data;
1877 asection *input_section;
1878 bfd *output_bfd;
1879 char **error_message;
1880{
1881 /* If we're relocating, and this an external symbol, we don't want
1882 to change anything. */
1883 if (output_bfd != (bfd *) NULL
1884 && (symbol->flags & BSF_SECTION_SYM) == 0
1885 && reloc_entry->addend == 0)
1886 {
1887 reloc_entry->address += input_section->output_offset;
1888 return bfd_reloc_ok;
1889 }
1890
1891 /* If we're relocating, and this is a local symbol, we can handle it
1892 just like HI16. */
1893 if (output_bfd != (bfd *) NULL
1894 && (symbol->flags & BSF_SECTION_SYM) != 0)
1895 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1896 input_section, output_bfd, error_message);
1897
1898 abort ();
1899}
1900
7403cb63
MM
1901/* Set the GP value for OUTPUT_BFD. Returns false if this is a
1902 dangerous relocation. */
1903
1904static boolean
1905mips_elf_assign_gp (output_bfd, pgp)
1906 bfd *output_bfd;
1907 bfd_vma *pgp;
1908{
1909 unsigned int count;
1910 asymbol **sym;
1911 unsigned int i;
1912
1913 /* If we've already figured out what GP will be, just return it. */
1914 *pgp = _bfd_get_gp_value (output_bfd);
1915 if (*pgp)
1916 return true;
1917
1918 count = bfd_get_symcount (output_bfd);
1919 sym = bfd_get_outsymbols (output_bfd);
1920
1921 /* The linker script will have created a symbol named `_gp' with the
1922 appropriate value. */
1923 if (sym == (asymbol **) NULL)
1924 i = count;
1925 else
1926 {
1927 for (i = 0; i < count; i++, sym++)
1928 {
dc810e39 1929 register const char *name;
7403cb63
MM
1930
1931 name = bfd_asymbol_name (*sym);
1932 if (*name == '_' && strcmp (name, "_gp") == 0)
1933 {
1934 *pgp = bfd_asymbol_value (*sym);
1935 _bfd_set_gp_value (output_bfd, *pgp);
1936 break;
1937 }
1938 }
1939 }
1940
1941 if (i >= count)
1942 {
1943 /* Only get the error once. */
1944 *pgp = 4;
1945 _bfd_set_gp_value (output_bfd, *pgp);
1946 return false;
1947 }
1948
1949 return true;
1950}
1951
252b5132
RH
1952/* We have to figure out the gp value, so that we can adjust the
1953 symbol value correctly. We look up the symbol _gp in the output
1954 BFD. If we can't find it, we're stuck. We cache it in the ELF
1955 target data. We don't need to adjust the symbol value for an
1956 external symbol if we are producing relocateable output. */
1957
1958static bfd_reloc_status_type
1959mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1960 bfd *output_bfd;
1961 asymbol *symbol;
1962 boolean relocateable;
1963 char **error_message;
1964 bfd_vma *pgp;
1965{
1966 if (bfd_is_und_section (symbol->section)
1967 && ! relocateable)
1968 {
1969 *pgp = 0;
1970 return bfd_reloc_undefined;
1971 }
1972
1973 *pgp = _bfd_get_gp_value (output_bfd);
1974 if (*pgp == 0
1975 && (! relocateable
1976 || (symbol->flags & BSF_SECTION_SYM) != 0))
1977 {
1978 if (relocateable)
1979 {
1980 /* Make up a value. */
1981 *pgp = symbol->section->output_section->vma + 0x4000;
1982 _bfd_set_gp_value (output_bfd, *pgp);
1983 }
7403cb63 1984 else if (!mips_elf_assign_gp (output_bfd, pgp))
252b5132 1985 {
7403cb63
MM
1986 *error_message =
1987 (char *) _("GP relative relocation when _gp not defined");
1988 return bfd_reloc_dangerous;
252b5132
RH
1989 }
1990 }
1991
1992 return bfd_reloc_ok;
1993}
1994
1995/* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1996 become the offset from the gp register. This function also handles
1997 R_MIPS_LITERAL relocations, although those can be handled more
1998 cleverly because the entries in the .lit8 and .lit4 sections can be
1999 merged. */
2000
2001static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
2002 arelent *, asection *,
2003 boolean, PTR, bfd_vma));
2004
2005bfd_reloc_status_type
2006_bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
2007 output_bfd, error_message)
2008 bfd *abfd;
2009 arelent *reloc_entry;
2010 asymbol *symbol;
2011 PTR data;
2012 asection *input_section;
2013 bfd *output_bfd;
2014 char **error_message;
2015{
2016 boolean relocateable;
2017 bfd_reloc_status_type ret;
2018 bfd_vma gp;
2019
2020 /* If we're relocating, and this is an external symbol with no
2021 addend, we don't want to change anything. We will only have an
2022 addend if this is a newly created reloc, not read from an ELF
2023 file. */
2024 if (output_bfd != (bfd *) NULL
2025 && (symbol->flags & BSF_SECTION_SYM) == 0
2026 && reloc_entry->addend == 0)
2027 {
2028 reloc_entry->address += input_section->output_offset;
2029 return bfd_reloc_ok;
2030 }
2031
2032 if (output_bfd != (bfd *) NULL)
2033 relocateable = true;
2034 else
2035 {
2036 relocateable = false;
2037 output_bfd = symbol->section->output_section->owner;
2038 }
2039
2040 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
2041 &gp);
2042 if (ret != bfd_reloc_ok)
2043 return ret;
2044
2045 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
2046 relocateable, data, gp);
2047}
2048
2049static bfd_reloc_status_type
2050gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
2051 gp)
2052 bfd *abfd;
2053 asymbol *symbol;
2054 arelent *reloc_entry;
2055 asection *input_section;
2056 boolean relocateable;
2057 PTR data;
2058 bfd_vma gp;
2059{
2060 bfd_vma relocation;
2061 unsigned long insn;
2062 unsigned long val;
2063
2064 if (bfd_is_com_section (symbol->section))
2065 relocation = 0;
2066 else
2067 relocation = symbol->value;
2068
2069 relocation += symbol->section->output_section->vma;
2070 relocation += symbol->section->output_offset;
2071
2072 if (reloc_entry->address > input_section->_cooked_size)
2073 return bfd_reloc_outofrange;
2074
2075 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2076
2077 /* Set val to the offset into the section or symbol. */
2078 if (reloc_entry->howto->src_mask == 0)
2079 {
2080 /* This case occurs with the 64-bit MIPS ELF ABI. */
2081 val = reloc_entry->addend;
2082 }
2083 else
2084 {
2085 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
2086 if (val & 0x8000)
2087 val -= 0x10000;
2088 }
2089
2090 /* Adjust val for the final section location and GP value. If we
2091 are producing relocateable output, we don't want to do this for
2092 an external symbol. */
2093 if (! relocateable
2094 || (symbol->flags & BSF_SECTION_SYM) != 0)
2095 val += relocation - gp;
2096
956cd1d6
TS
2097 insn = (insn & ~0xffff) | (val & 0xffff);
2098 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
252b5132
RH
2099
2100 if (relocateable)
2101 reloc_entry->address += input_section->output_offset;
2102
2103 /* Make sure it fit in 16 bits. */
43cbcf28 2104 if ((long) val >= 0x8000 || (long) val < -0x8000)
252b5132
RH
2105 return bfd_reloc_overflow;
2106
2107 return bfd_reloc_ok;
2108}
2109
2110/* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
2111 from the gp register? XXX */
2112
2113static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
2114 arelent *, asection *,
2115 boolean, PTR, bfd_vma));
2116
2117bfd_reloc_status_type
2118_bfd_mips_elf_gprel32_reloc (abfd,
2119 reloc_entry,
2120 symbol,
2121 data,
2122 input_section,
2123 output_bfd,
2124 error_message)
2125 bfd *abfd;
2126 arelent *reloc_entry;
2127 asymbol *symbol;
2128 PTR data;
2129 asection *input_section;
2130 bfd *output_bfd;
2131 char **error_message;
2132{
2133 boolean relocateable;
2134 bfd_reloc_status_type ret;
2135 bfd_vma gp;
2136
2137 /* If we're relocating, and this is an external symbol with no
2138 addend, we don't want to change anything. We will only have an
2139 addend if this is a newly created reloc, not read from an ELF
2140 file. */
2141 if (output_bfd != (bfd *) NULL
2142 && (symbol->flags & BSF_SECTION_SYM) == 0
2143 && reloc_entry->addend == 0)
2144 {
2145 *error_message = (char *)
2146 _("32bits gp relative relocation occurs for an external symbol");
2147 return bfd_reloc_outofrange;
2148 }
2149
2150 if (output_bfd != (bfd *) NULL)
2151 {
2152 relocateable = true;
2153 gp = _bfd_get_gp_value (output_bfd);
2154 }
2155 else
2156 {
2157 relocateable = false;
2158 output_bfd = symbol->section->output_section->owner;
2159
2160 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
2161 error_message, &gp);
2162 if (ret != bfd_reloc_ok)
2163 return ret;
2164 }
2165
2166 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
2167 relocateable, data, gp);
2168}
2169
2170static bfd_reloc_status_type
2171gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
2172 gp)
2173 bfd *abfd;
2174 asymbol *symbol;
2175 arelent *reloc_entry;
2176 asection *input_section;
2177 boolean relocateable;
2178 PTR data;
2179 bfd_vma gp;
2180{
2181 bfd_vma relocation;
2182 unsigned long val;
2183
2184 if (bfd_is_com_section (symbol->section))
2185 relocation = 0;
2186 else
2187 relocation = symbol->value;
2188
2189 relocation += symbol->section->output_section->vma;
2190 relocation += symbol->section->output_offset;
2191
2192 if (reloc_entry->address > input_section->_cooked_size)
2193 return bfd_reloc_outofrange;
2194
2195 if (reloc_entry->howto->src_mask == 0)
2196 {
2197 /* This case arises with the 64-bit MIPS ELF ABI. */
2198 val = 0;
2199 }
2200 else
2201 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2202
2203 /* Set val to the offset into the section or symbol. */
2204 val += reloc_entry->addend;
2205
2206 /* Adjust val for the final section location and GP value. If we
2207 are producing relocateable output, we don't want to do this for
2208 an external symbol. */
2209 if (! relocateable
2210 || (symbol->flags & BSF_SECTION_SYM) != 0)
2211 val += relocation - gp;
2212
dc810e39 2213 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + reloc_entry->address);
252b5132
RH
2214
2215 if (relocateable)
2216 reloc_entry->address += input_section->output_offset;
2217
2218 return bfd_reloc_ok;
2219}
2220
2221/* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
062e2358 2222 generated when addresses are 64 bits. The upper 32 bits are a simple
252b5132
RH
2223 sign extension. */
2224
2225static bfd_reloc_status_type
2226mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
2227 output_bfd, error_message)
2228 bfd *abfd;
2229 arelent *reloc_entry;
2230 asymbol *symbol;
2231 PTR data;
2232 asection *input_section;
2233 bfd *output_bfd;
2234 char **error_message;
2235{
2236 bfd_reloc_status_type r;
2237 arelent reloc32;
2238 unsigned long val;
2239 bfd_size_type addr;
2240
2241 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2242 input_section, output_bfd, error_message);
2243 if (r != bfd_reloc_continue)
2244 return r;
2245
2246 /* Do a normal 32 bit relocation on the lower 32 bits. */
2247 reloc32 = *reloc_entry;
2248 if (bfd_big_endian (abfd))
2249 reloc32.address += 4;
d75bc93d 2250 reloc32.howto = &elf_mips_howto_table_rel[R_MIPS_32];
252b5132
RH
2251 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
2252 output_bfd, error_message);
2253
2254 /* Sign extend into the upper 32 bits. */
2255 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
2256 if ((val & 0x80000000) != 0)
2257 val = 0xffffffff;
2258 else
2259 val = 0;
2260 addr = reloc_entry->address;
2261 if (bfd_little_endian (abfd))
2262 addr += 4;
dc810e39 2263 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + addr);
252b5132
RH
2264
2265 return r;
2266}
2267
2268/* Handle a mips16 jump. */
2269
2270static bfd_reloc_status_type
2271mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
2272 output_bfd, error_message)
5f771d47 2273 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
2274 arelent *reloc_entry;
2275 asymbol *symbol;
5f771d47 2276 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
2277 asection *input_section;
2278 bfd *output_bfd;
5f771d47 2279 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
2280{
2281 if (output_bfd != (bfd *) NULL
2282 && (symbol->flags & BSF_SECTION_SYM) == 0
2283 && reloc_entry->addend == 0)
2284 {
2285 reloc_entry->address += input_section->output_offset;
2286 return bfd_reloc_ok;
2287 }
2288
2289 /* FIXME. */
2290 {
2291 static boolean warned;
2292
2293 if (! warned)
2294 (*_bfd_error_handler)
2295 (_("Linking mips16 objects into %s format is not supported"),
2296 bfd_get_target (input_section->output_section->owner));
2297 warned = true;
2298 }
2299
2300 return bfd_reloc_undefined;
2301}
2302
2303/* Handle a mips16 GP relative reloc. */
2304
2305static bfd_reloc_status_type
2306mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
2307 output_bfd, error_message)
2308 bfd *abfd;
2309 arelent *reloc_entry;
2310 asymbol *symbol;
2311 PTR data;
2312 asection *input_section;
2313 bfd *output_bfd;
2314 char **error_message;
2315{
2316 boolean relocateable;
2317 bfd_reloc_status_type ret;
2318 bfd_vma gp;
2319 unsigned short extend, insn;
2320 unsigned long final;
2321
2322 /* If we're relocating, and this is an external symbol with no
2323 addend, we don't want to change anything. We will only have an
2324 addend if this is a newly created reloc, not read from an ELF
2325 file. */
2326 if (output_bfd != NULL
2327 && (symbol->flags & BSF_SECTION_SYM) == 0
2328 && reloc_entry->addend == 0)
2329 {
2330 reloc_entry->address += input_section->output_offset;
2331 return bfd_reloc_ok;
2332 }
2333
2334 if (output_bfd != NULL)
2335 relocateable = true;
2336 else
2337 {
2338 relocateable = false;
2339 output_bfd = symbol->section->output_section->owner;
2340 }
2341
2342 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
2343 &gp);
2344 if (ret != bfd_reloc_ok)
2345 return ret;
2346
2347 if (reloc_entry->address > input_section->_cooked_size)
2348 return bfd_reloc_outofrange;
2349
2350 /* Pick up the mips16 extend instruction and the real instruction. */
2351 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
2352 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
2353
2354 /* Stuff the current addend back as a 32 bit value, do the usual
2355 relocation, and then clean up. */
2356 bfd_put_32 (abfd,
dc810e39
AM
2357 (bfd_vma) (((extend & 0x1f) << 11)
2358 | (extend & 0x7e0)
2359 | (insn & 0x1f)),
252b5132
RH
2360 (bfd_byte *) data + reloc_entry->address);
2361
2362 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
2363 relocateable, data, gp);
2364
2365 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2366 bfd_put_16 (abfd,
dc810e39
AM
2367 (bfd_vma) ((extend & 0xf800)
2368 | ((final >> 11) & 0x1f)
2369 | (final & 0x7e0)),
252b5132
RH
2370 (bfd_byte *) data + reloc_entry->address);
2371 bfd_put_16 (abfd,
dc810e39
AM
2372 (bfd_vma) ((insn & 0xffe0)
2373 | (final & 0x1f)),
252b5132
RH
2374 (bfd_byte *) data + reloc_entry->address + 2);
2375
2376 return ret;
2377}
2378
2379/* Return the ISA for a MIPS e_flags value. */
2380
2381static INLINE int
2382elf_mips_isa (flags)
2383 flagword flags;
2384{
2385 switch (flags & EF_MIPS_ARCH)
2386 {
2387 case E_MIPS_ARCH_1:
2388 return 1;
2389 case E_MIPS_ARCH_2:
2390 return 2;
2391 case E_MIPS_ARCH_3:
2392 return 3;
2393 case E_MIPS_ARCH_4:
2394 return 4;
84ea6cf2
NC
2395 case E_MIPS_ARCH_5:
2396 return 5;
e7af610e
NC
2397 case E_MIPS_ARCH_32:
2398 return 32;
84ea6cf2
NC
2399 case E_MIPS_ARCH_64:
2400 return 64;
252b5132
RH
2401 }
2402 return 4;
2403}
2404
2405/* Return the MACH for a MIPS e_flags value. */
2406
dc810e39 2407static INLINE unsigned long
252b5132
RH
2408elf_mips_mach (flags)
2409 flagword flags;
2410{
2411 switch (flags & EF_MIPS_MACH)
2412 {
2413 case E_MIPS_MACH_3900:
2414 return bfd_mach_mips3900;
2415
2416 case E_MIPS_MACH_4010:
2417 return bfd_mach_mips4010;
2418
2419 case E_MIPS_MACH_4100:
2420 return bfd_mach_mips4100;
2421
2422 case E_MIPS_MACH_4111:
2423 return bfd_mach_mips4111;
2424
2425 case E_MIPS_MACH_4650:
2426 return bfd_mach_mips4650;
2427
c6c98b38
NC
2428 case E_MIPS_MACH_SB1:
2429 return bfd_mach_mips_sb1;
2430
252b5132
RH
2431 default:
2432 switch (flags & EF_MIPS_ARCH)
2433 {
2434 default:
2435 case E_MIPS_ARCH_1:
2436 return bfd_mach_mips3000;
2437 break;
2438
2439 case E_MIPS_ARCH_2:
2440 return bfd_mach_mips6000;
2441 break;
2442
2443 case E_MIPS_ARCH_3:
2444 return bfd_mach_mips4000;
2445 break;
2446
2447 case E_MIPS_ARCH_4:
2448 return bfd_mach_mips8000;
2449 break;
e7af610e 2450
84ea6cf2
NC
2451 case E_MIPS_ARCH_5:
2452 return bfd_mach_mips5;
2453 break;
2454
e7af610e 2455 case E_MIPS_ARCH_32:
a1cd6a8f 2456 return bfd_mach_mipsisa32;
e7af610e 2457 break;
84ea6cf2
NC
2458
2459 case E_MIPS_ARCH_64:
a1cd6a8f 2460 return bfd_mach_mipsisa64;
84ea6cf2 2461 break;
252b5132
RH
2462 }
2463 }
2464
2465 return 0;
2466}
2467
be3ccd9c 2468/* Return printable name for ABI. */
252b5132 2469
be3ccd9c 2470static INLINE char *
103186c6
MM
2471elf_mips_abi_name (abfd)
2472 bfd *abfd;
252b5132 2473{
103186c6
MM
2474 flagword flags;
2475
103186c6 2476 flags = elf_elfheader (abfd)->e_flags;
252b5132
RH
2477 switch (flags & EF_MIPS_ABI)
2478 {
2479 case 0:
aee61b13
TS
2480 if (ABI_N32_P (abfd))
2481 return "N32";
2482 else if (ABI_64_P (abfd))
2483 return "64";
2484 else
2485 return "none";
252b5132
RH
2486 case E_MIPS_ABI_O32:
2487 return "O32";
2488 case E_MIPS_ABI_O64:
2489 return "O64";
2490 case E_MIPS_ABI_EABI32:
2491 return "EABI32";
2492 case E_MIPS_ABI_EABI64:
2493 return "EABI64";
2494 default:
2495 return "unknown abi";
2496 }
2497}
2498
2499/* A mapping from BFD reloc types to MIPS ELF reloc types. */
2500
2501struct elf_reloc_map {
2502 bfd_reloc_code_real_type bfd_reloc_val;
2503 enum elf_mips_reloc_type elf_reloc_val;
2504};
2505
dc810e39 2506static const struct elf_reloc_map mips_reloc_map[] =
38b1a46c 2507{
252b5132
RH
2508 { BFD_RELOC_NONE, R_MIPS_NONE, },
2509 { BFD_RELOC_16, R_MIPS_16 },
2510 { BFD_RELOC_32, R_MIPS_32 },
2511 { BFD_RELOC_64, R_MIPS_64 },
2512 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
2513 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
2514 { BFD_RELOC_LO16, R_MIPS_LO16 },
cdf6fd85 2515 { BFD_RELOC_GPREL16, R_MIPS_GPREL16 },
252b5132
RH
2516 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
2517 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
2518 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
2519 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
cdf6fd85 2520 { BFD_RELOC_GPREL32, R_MIPS_GPREL32 },
252b5132
RH
2521 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
2522 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
2523 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
3f830999
MM
2524 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
2525 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
2526 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
2527 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
2528 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
252b5132
RH
2529};
2530
2531/* Given a BFD reloc type, return a howto structure. */
2532
2533static reloc_howto_type *
2534bfd_elf32_bfd_reloc_type_lookup (abfd, code)
2535 bfd *abfd;
2536 bfd_reloc_code_real_type code;
2537{
2538 unsigned int i;
2539
2540 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
2541 {
2542 if (mips_reloc_map[i].bfd_reloc_val == code)
d75bc93d 2543 return &elf_mips_howto_table_rel[(int) mips_reloc_map[i].elf_reloc_val];
252b5132
RH
2544 }
2545
2546 switch (code)
2547 {
2548 default:
2549 bfd_set_error (bfd_error_bad_value);
2550 return NULL;
2551
2552 case BFD_RELOC_CTOR:
2553 /* We need to handle BFD_RELOC_CTOR specially.
2554 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
2555 size of addresses on this architecture. */
2556 if (bfd_arch_bits_per_address (abfd) == 32)
d75bc93d 2557 return &elf_mips_howto_table_rel[(int) R_MIPS_32];
252b5132
RH
2558 else
2559 return &elf_mips_ctor64_howto;
2560
2561 case BFD_RELOC_MIPS16_JMP:
2562 return &elf_mips16_jump_howto;
2563 case BFD_RELOC_MIPS16_GPREL:
2564 return &elf_mips16_gprel_howto;
2565 case BFD_RELOC_VTABLE_INHERIT:
2566 return &elf_mips_gnu_vtinherit_howto;
2567 case BFD_RELOC_VTABLE_ENTRY:
2568 return &elf_mips_gnu_vtentry_howto;
bb2d6cd7
GK
2569 case BFD_RELOC_PCREL_HI16_S:
2570 return &elf_mips_gnu_rel_hi16;
2571 case BFD_RELOC_PCREL_LO16:
2572 return &elf_mips_gnu_rel_lo16;
2573 case BFD_RELOC_16_PCREL_S2:
2574 return &elf_mips_gnu_rel16_s2;
2575 case BFD_RELOC_64_PCREL:
2576 return &elf_mips_gnu_pcrel64;
2577 case BFD_RELOC_32_PCREL:
2578 return &elf_mips_gnu_pcrel32;
252b5132
RH
2579 }
2580}
2581
3f830999 2582/* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
252b5132 2583
c9b3cbf3
RH
2584static reloc_howto_type *
2585mips_rtype_to_howto (r_type)
2586 unsigned int r_type;
252b5132 2587{
252b5132
RH
2588 switch (r_type)
2589 {
2590 case R_MIPS16_26:
c9b3cbf3 2591 return &elf_mips16_jump_howto;
252b5132
RH
2592 break;
2593 case R_MIPS16_GPREL:
c9b3cbf3 2594 return &elf_mips16_gprel_howto;
252b5132
RH
2595 break;
2596 case R_MIPS_GNU_VTINHERIT:
c9b3cbf3 2597 return &elf_mips_gnu_vtinherit_howto;
252b5132
RH
2598 break;
2599 case R_MIPS_GNU_VTENTRY:
c9b3cbf3 2600 return &elf_mips_gnu_vtentry_howto;
252b5132 2601 break;
bb2d6cd7
GK
2602 case R_MIPS_GNU_REL_HI16:
2603 return &elf_mips_gnu_rel_hi16;
2604 break;
2605 case R_MIPS_GNU_REL_LO16:
2606 return &elf_mips_gnu_rel_lo16;
2607 break;
2608 case R_MIPS_GNU_REL16_S2:
2609 return &elf_mips_gnu_rel16_s2;
2610 break;
2611 case R_MIPS_PC64:
2612 return &elf_mips_gnu_pcrel64;
2613 break;
2614 case R_MIPS_PC32:
2615 return &elf_mips_gnu_pcrel32;
2616 break;
252b5132
RH
2617
2618 default:
2619 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
d75bc93d 2620 return &elf_mips_howto_table_rel[r_type];
252b5132
RH
2621 break;
2622 }
c9b3cbf3
RH
2623}
2624
2625/* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
2626
2627static void
2628mips_info_to_howto_rel (abfd, cache_ptr, dst)
2629 bfd *abfd;
2630 arelent *cache_ptr;
2631 Elf32_Internal_Rel *dst;
2632{
2633 unsigned int r_type;
2634
2635 r_type = ELF32_R_TYPE (dst->r_info);
2636 cache_ptr->howto = mips_rtype_to_howto (r_type);
252b5132
RH
2637
2638 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
2639 value for the object file. We get the addend now, rather than
2640 when we do the relocation, because the symbol manipulations done
2641 by the linker may cause us to lose track of the input BFD. */
2642 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
2643 && (r_type == (unsigned int) R_MIPS_GPREL16
2644 || r_type == (unsigned int) R_MIPS_LITERAL))
2645 cache_ptr->addend = elf_gp (abfd);
2646}
3f830999
MM
2647
2648/* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
2649
2650static void
2651mips_info_to_howto_rela (abfd, cache_ptr, dst)
2652 bfd *abfd;
2653 arelent *cache_ptr;
2654 Elf32_Internal_Rela *dst;
2655{
2656 /* Since an Elf32_Internal_Rel is an initial prefix of an
2657 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
2658 above. */
2659 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
2660
2661 /* If we ever need to do any extra processing with dst->r_addend
2662 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
2663}
252b5132
RH
2664\f
2665/* A .reginfo section holds a single Elf32_RegInfo structure. These
2666 routines swap this structure in and out. They are used outside of
2667 BFD, so they are globally visible. */
2668
2669void
2670bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
2671 bfd *abfd;
2672 const Elf32_External_RegInfo *ex;
2673 Elf32_RegInfo *in;
2674{
dc810e39
AM
2675 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2676 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2677 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2678 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2679 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2680 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
252b5132
RH
2681}
2682
2683void
2684bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
2685 bfd *abfd;
2686 const Elf32_RegInfo *in;
2687 Elf32_External_RegInfo *ex;
2688{
dc810e39
AM
2689 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2690 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2691 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2692 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2693 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2694 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
252b5132
RH
2695}
2696
2697/* In the 64 bit ABI, the .MIPS.options section holds register
2698 information in an Elf64_Reginfo structure. These routines swap
2699 them in and out. They are globally visible because they are used
2700 outside of BFD. These routines are here so that gas can call them
2701 without worrying about whether the 64 bit ABI has been included. */
2702
2703void
2704bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
2705 bfd *abfd;
2706 const Elf64_External_RegInfo *ex;
2707 Elf64_Internal_RegInfo *in;
2708{
dc810e39
AM
2709 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2710 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2711 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2712 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2713 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2714 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2715 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
252b5132
RH
2716}
2717
2718void
2719bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
2720 bfd *abfd;
2721 const Elf64_Internal_RegInfo *in;
2722 Elf64_External_RegInfo *ex;
2723{
dc810e39
AM
2724 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2725 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2726 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2727 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2728 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2729 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2730 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
252b5132
RH
2731}
2732
2733/* Swap an entry in a .gptab section. Note that these routines rely
2734 on the equivalence of the two elements of the union. */
2735
2736static void
2737bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
2738 bfd *abfd;
2739 const Elf32_External_gptab *ex;
2740 Elf32_gptab *in;
2741{
dc810e39
AM
2742 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2743 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
252b5132
RH
2744}
2745
2746static void
2747bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
2748 bfd *abfd;
2749 const Elf32_gptab *in;
2750 Elf32_External_gptab *ex;
2751{
dc810e39
AM
2752 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2753 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
252b5132
RH
2754}
2755
2756static void
2757bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2758 bfd *abfd;
2759 const Elf32_compact_rel *in;
2760 Elf32_External_compact_rel *ex;
2761{
dc810e39
AM
2762 H_PUT_32 (abfd, in->id1, ex->id1);
2763 H_PUT_32 (abfd, in->num, ex->num);
2764 H_PUT_32 (abfd, in->id2, ex->id2);
2765 H_PUT_32 (abfd, in->offset, ex->offset);
2766 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2767 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
252b5132
RH
2768}
2769
2770static void
2771bfd_elf32_swap_crinfo_out (abfd, in, ex)
2772 bfd *abfd;
2773 const Elf32_crinfo *in;
2774 Elf32_External_crinfo *ex;
2775{
2776 unsigned long l;
2777
2778 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2779 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2780 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2781 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
dc810e39
AM
2782 H_PUT_32 (abfd, l, ex->info);
2783 H_PUT_32 (abfd, in->konst, ex->konst);
2784 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
252b5132
RH
2785}
2786
2787/* Swap in an options header. */
2788
2789void
2790bfd_mips_elf_swap_options_in (abfd, ex, in)
2791 bfd *abfd;
2792 const Elf_External_Options *ex;
2793 Elf_Internal_Options *in;
2794{
dc810e39
AM
2795 in->kind = H_GET_8 (abfd, ex->kind);
2796 in->size = H_GET_8 (abfd, ex->size);
2797 in->section = H_GET_16 (abfd, ex->section);
2798 in->info = H_GET_32 (abfd, ex->info);
252b5132
RH
2799}
2800
2801/* Swap out an options header. */
2802
2803void
2804bfd_mips_elf_swap_options_out (abfd, in, ex)
2805 bfd *abfd;
2806 const Elf_Internal_Options *in;
2807 Elf_External_Options *ex;
2808{
dc810e39
AM
2809 H_PUT_8 (abfd, in->kind, ex->kind);
2810 H_PUT_8 (abfd, in->size, ex->size);
2811 H_PUT_16 (abfd, in->section, ex->section);
2812 H_PUT_32 (abfd, in->info, ex->info);
252b5132 2813}
86033394 2814#if 0
c6142e5d
MM
2815/* Swap in an MSYM entry. */
2816
2817static void
2818bfd_mips_elf_swap_msym_in (abfd, ex, in)
2819 bfd *abfd;
2820 const Elf32_External_Msym *ex;
2821 Elf32_Internal_Msym *in;
2822{
dc810e39
AM
2823 in->ms_hash_value = H_GET_32 (abfd, ex->ms_hash_value);
2824 in->ms_info = H_GET_32 (abfd, ex->ms_info);
c6142e5d 2825}
86033394 2826#endif
c6142e5d
MM
2827/* Swap out an MSYM entry. */
2828
2829static void
2830bfd_mips_elf_swap_msym_out (abfd, in, ex)
2831 bfd *abfd;
2832 const Elf32_Internal_Msym *in;
2833 Elf32_External_Msym *ex;
2834{
dc810e39
AM
2835 H_PUT_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2836 H_PUT_32 (abfd, in->ms_info, ex->ms_info);
c6142e5d 2837}
252b5132
RH
2838\f
2839/* Determine whether a symbol is global for the purposes of splitting
2840 the symbol table into global symbols and local symbols. At least
2841 on Irix 5, this split must be between section symbols and all other
2842 symbols. On most ELF targets the split is between static symbols
2843 and externally visible symbols. */
2844
252b5132
RH
2845static boolean
2846mips_elf_sym_is_global (abfd, sym)
5f771d47 2847 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
2848 asymbol *sym;
2849{
936e320b
AM
2850 if (SGI_COMPAT (abfd))
2851 return (sym->flags & BSF_SECTION_SYM) == 0;
fdbafa10
L
2852 else
2853 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
936e320b
AM
2854 || bfd_is_und_section (bfd_get_section (sym))
2855 || bfd_is_com_section (bfd_get_section (sym)));
252b5132
RH
2856}
2857\f
2858/* Set the right machine number for a MIPS ELF file. This is used for
2859 both the 32-bit and the 64-bit ABI. */
2860
2861boolean
2862_bfd_mips_elf_object_p (abfd)
2863 bfd *abfd;
2864{
d75bc93d 2865 /* Irix 5 and 6 are broken. Object file symbol tables are not always
252b5132
RH
2866 sorted correctly such that local symbols precede global symbols,
2867 and the sh_info field in the symbol table is not always right. */
c36e006f
L
2868 if (SGI_COMPAT(abfd))
2869 elf_bad_symtab (abfd) = true;
252b5132 2870
103186c6
MM
2871 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2872 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2873 return true;
252b5132
RH
2874}
2875
2876/* The final processing done just before writing out a MIPS ELF object
2877 file. This gets the MIPS architecture right based on the machine
2878 number. This is used by both the 32-bit and the 64-bit ABI. */
2879
252b5132
RH
2880void
2881_bfd_mips_elf_final_write_processing (abfd, linker)
2882 bfd *abfd;
5f771d47 2883 boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
2884{
2885 unsigned long val;
2886 unsigned int i;
2887 Elf_Internal_Shdr **hdrpp;
2888 const char *name;
2889 asection *sec;
2890
2891 switch (bfd_get_mach (abfd))
2892 {
2893 default:
2894 case bfd_mach_mips3000:
2895 val = E_MIPS_ARCH_1;
2896 break;
2897
2898 case bfd_mach_mips3900:
2899 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2900 break;
2901
2902 case bfd_mach_mips6000:
2903 val = E_MIPS_ARCH_2;
2904 break;
2905
2906 case bfd_mach_mips4000:
2907 case bfd_mach_mips4300:
5e7079af
L
2908 case bfd_mach_mips4400:
2909 case bfd_mach_mips4600:
252b5132
RH
2910 val = E_MIPS_ARCH_3;
2911 break;
2912
2913 case bfd_mach_mips4010:
2914 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2915 break;
2916
2917 case bfd_mach_mips4100:
2918 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2919 break;
2920
2921 case bfd_mach_mips4111:
2922 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2923 break;
2924
2925 case bfd_mach_mips4650:
2926 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2927 break;
2928
5e7079af 2929 case bfd_mach_mips5000:
252b5132 2930 case bfd_mach_mips8000:
d1cf510e
NC
2931 case bfd_mach_mips10000:
2932 case bfd_mach_mips12000:
252b5132
RH
2933 val = E_MIPS_ARCH_4;
2934 break;
156c2f8b 2935
84ea6cf2
NC
2936 case bfd_mach_mips5:
2937 val = E_MIPS_ARCH_5;
2938 break;
2939
c6c98b38
NC
2940 case bfd_mach_mips_sb1:
2941 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
2942 break;
a1cd6a8f
EC
2943
2944 case bfd_mach_mipsisa32:
2945 val = E_MIPS_ARCH_32;
2946 break;
2947
2948 case bfd_mach_mipsisa64:
2949 val = E_MIPS_ARCH_64;
252b5132
RH
2950 }
2951
be3ccd9c 2952 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
252b5132
RH
2953 elf_elfheader (abfd)->e_flags |= val;
2954
2955 /* Set the sh_info field for .gptab sections and other appropriate
2956 info for each special section. */
2957 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
9ad5cbcf 2958 i < elf_numsections (abfd);
252b5132
RH
2959 i++, hdrpp++)
2960 {
2961 switch ((*hdrpp)->sh_type)
2962 {
c6142e5d 2963 case SHT_MIPS_MSYM:
252b5132
RH
2964 case SHT_MIPS_LIBLIST:
2965 sec = bfd_get_section_by_name (abfd, ".dynstr");
2966 if (sec != NULL)
2967 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2968 break;
2969
2970 case SHT_MIPS_GPTAB:
2971 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2972 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2973 BFD_ASSERT (name != NULL
2974 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2975 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2976 BFD_ASSERT (sec != NULL);
2977 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2978 break;
2979
2980 case SHT_MIPS_CONTENT:
2981 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2982 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2983 BFD_ASSERT (name != NULL
2984 && strncmp (name, ".MIPS.content",
2985 sizeof ".MIPS.content" - 1) == 0);
2986 sec = bfd_get_section_by_name (abfd,
2987 name + sizeof ".MIPS.content" - 1);
2988 BFD_ASSERT (sec != NULL);
3f830999 2989 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
252b5132
RH
2990 break;
2991
2992 case SHT_MIPS_SYMBOL_LIB:
2993 sec = bfd_get_section_by_name (abfd, ".dynsym");
2994 if (sec != NULL)
2995 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2996 sec = bfd_get_section_by_name (abfd, ".liblist");
2997 if (sec != NULL)
2998 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2999 break;
3000
3001 case SHT_MIPS_EVENTS:
3002 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
3003 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
3004 BFD_ASSERT (name != NULL);
3005 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
3006 sec = bfd_get_section_by_name (abfd,
3007 name + sizeof ".MIPS.events" - 1);
3008 else
3009 {
3010 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
3011 sizeof ".MIPS.post_rel" - 1) == 0);
3012 sec = bfd_get_section_by_name (abfd,
3013 (name
3014 + sizeof ".MIPS.post_rel" - 1));
3015 }
3016 BFD_ASSERT (sec != NULL);
3017 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
3018 break;
3019
3020 }
3021 }
3022}
3023\f
be3ccd9c 3024/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
252b5132
RH
3025
3026boolean
3027_bfd_mips_elf_set_private_flags (abfd, flags)
3028 bfd *abfd;
3029 flagword flags;
3030{
3031 BFD_ASSERT (!elf_flags_init (abfd)
3032 || elf_elfheader (abfd)->e_flags == flags);
3033
3034 elf_elfheader (abfd)->e_flags = flags;
3035 elf_flags_init (abfd) = true;
3036 return true;
3037}
3038
252b5132
RH
3039/* Merge backend specific data from an object file to the output
3040 object file when linking. */
3041
3042boolean
3043_bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
3044 bfd *ibfd;
3045 bfd *obfd;
3046{
3047 flagword old_flags;
3048 flagword new_flags;
3049 boolean ok;
a9922e52
UC
3050 boolean null_input_bfd = true;
3051 asection *sec;
252b5132
RH
3052
3053 /* Check if we have the same endianess */
1fe494a5
NC
3054 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
3055 return false;
252b5132
RH
3056
3057 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3058 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3059 return true;
3060
3061 new_flags = elf_elfheader (ibfd)->e_flags;
3062 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
3063 old_flags = elf_elfheader (obfd)->e_flags;
3064
3065 if (! elf_flags_init (obfd))
3066 {
3067 elf_flags_init (obfd) = true;
3068 elf_elfheader (obfd)->e_flags = new_flags;
be3ccd9c 3069 elf_elfheader (obfd)->e_ident[EI_CLASS]
103186c6 3070 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
252b5132
RH
3071
3072 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3073 && bfd_get_arch_info (obfd)->the_default)
3074 {
3075 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3076 bfd_get_mach (ibfd)))
3077 return false;
3078 }
3079
3080 return true;
3081 }
3082
3083 /* Check flag compatibility. */
3084
3085 new_flags &= ~EF_MIPS_NOREORDER;
3086 old_flags &= ~EF_MIPS_NOREORDER;
3087
3088 if (new_flags == old_flags)
3089 return true;
3090
a9922e52
UC
3091 /* Check to see if the input BFD actually contains any sections.
3092 If not, its flags may not have been initialised either, but it cannot
3093 actually cause any incompatibility. */
3094 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3095 {
3096 /* Ignore synthetic sections and empty .text, .data and .bss sections
3097 which are automatically generated by gas. */
3098 if (strcmp (sec->name, ".reginfo")
3099 && strcmp (sec->name, ".mdebug")
3100 && ((!strcmp (sec->name, ".text")
3101 || !strcmp (sec->name, ".data")
3102 || !strcmp (sec->name, ".bss"))
3103 && sec->_raw_size != 0))
3104 {
3105 null_input_bfd = false;
3106 break;
3107 }
3108 }
3109 if (null_input_bfd)
3110 return true;
3111
252b5132
RH
3112 ok = true;
3113
3114 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
3115 {
3116 new_flags &= ~EF_MIPS_PIC;
3117 old_flags &= ~EF_MIPS_PIC;
3118 (*_bfd_error_handler)
3119 (_("%s: linking PIC files with non-PIC files"),
8f615d07 3120 bfd_archive_filename (ibfd));
252b5132
RH
3121 ok = false;
3122 }
3123
3124 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
3125 {
3126 new_flags &= ~EF_MIPS_CPIC;
3127 old_flags &= ~EF_MIPS_CPIC;
3128 (*_bfd_error_handler)
3129 (_("%s: linking abicalls files with non-abicalls files"),
8f615d07 3130 bfd_archive_filename (ibfd));
252b5132
RH
3131 ok = false;
3132 }
3133
be3ccd9c 3134 /* Compare the ISA's. */
252b5132
RH
3135 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
3136 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
3137 {
3138 int new_mach = new_flags & EF_MIPS_MACH;
3139 int old_mach = old_flags & EF_MIPS_MACH;
3140 int new_isa = elf_mips_isa (new_flags);
3141 int old_isa = elf_mips_isa (old_flags);
3142
3143 /* If either has no machine specified, just compare the general isa's.
be3ccd9c
KH
3144 Some combinations of machines are ok, if the isa's match. */
3145 if (! new_mach
252b5132
RH
3146 || ! old_mach
3147 || new_mach == old_mach
3148 )
3149 {
e7af610e
NC
3150 /* Don't warn about mixing code using 32-bit ISAs, or mixing code
3151 using 64-bit ISAs. They will normally use the same data sizes
3152 and calling conventions. */
252b5132 3153
e7af610e
NC
3154 if (( (new_isa == 1 || new_isa == 2 || new_isa == 32)
3155 ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0)
252b5132
RH
3156 {
3157 (*_bfd_error_handler)
3158 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
8f615d07 3159 bfd_archive_filename (ibfd), new_isa, old_isa);
252b5132
RH
3160 ok = false;
3161 }
3162 }
3163
3164 else
3165 {
3166 (*_bfd_error_handler)
3167 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
8f615d07 3168 bfd_archive_filename (ibfd),
252b5132
RH
3169 elf_mips_mach (new_flags),
3170 elf_mips_mach (old_flags));
3171 ok = false;
3172 }
3173
be3ccd9c
KH
3174 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
3175 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
252b5132
RH
3176 }
3177
103186c6
MM
3178 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
3179 does set EI_CLASS differently from any 32-bit ABI. */
3180 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
be3ccd9c 3181 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
103186c6 3182 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
252b5132 3183 {
be3ccd9c 3184 /* Only error if both are set (to different values). */
103186c6 3185 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
be3ccd9c 3186 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
103186c6 3187 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
252b5132
RH
3188 {
3189 (*_bfd_error_handler)
3190 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
8f615d07 3191 bfd_archive_filename (ibfd),
103186c6
MM
3192 elf_mips_abi_name (ibfd),
3193 elf_mips_abi_name (obfd));
252b5132
RH
3194 ok = false;
3195 }
3196 new_flags &= ~EF_MIPS_ABI;
3197 old_flags &= ~EF_MIPS_ABI;
3198 }
3199
3200 /* Warn about any other mismatches */
3201 if (new_flags != old_flags)
3202 {
3203 (*_bfd_error_handler)
3204 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
8f615d07 3205 bfd_archive_filename (ibfd), (unsigned long) new_flags,
252b5132
RH
3206 (unsigned long) old_flags);
3207 ok = false;
3208 }
3209
3210 if (! ok)
3211 {
3212 bfd_set_error (bfd_error_bad_value);
3213 return false;
3214 }
3215
3216 return true;
3217}
3218\f
103186c6 3219boolean
252b5132
RH
3220_bfd_mips_elf_print_private_bfd_data (abfd, ptr)
3221 bfd *abfd;
3222 PTR ptr;
3223{
3224 FILE *file = (FILE *) ptr;
3225
3226 BFD_ASSERT (abfd != NULL && ptr != NULL);
3227
3228 /* Print normal ELF private data. */
3229 _bfd_elf_print_private_bfd_data (abfd, ptr);
3230
3231 /* xgettext:c-format */
be3ccd9c 3232 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
252b5132
RH
3233
3234 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
be3ccd9c 3235 fprintf (file, _(" [abi=O32]"));
252b5132 3236 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
be3ccd9c 3237 fprintf (file, _(" [abi=O64]"));
252b5132 3238 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
be3ccd9c 3239 fprintf (file, _(" [abi=EABI32]"));
252b5132 3240 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
be3ccd9c 3241 fprintf (file, _(" [abi=EABI64]"));
252b5132 3242 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
be3ccd9c 3243 fprintf (file, _(" [abi unknown]"));
103186c6 3244 else if (ABI_N32_P (abfd))
be3ccd9c 3245 fprintf (file, _(" [abi=N32]"));
103186c6 3246 else if (ABI_64_P (abfd))
be3ccd9c 3247 fprintf (file, _(" [abi=64]"));
252b5132 3248 else
be3ccd9c 3249 fprintf (file, _(" [no abi set]"));
252b5132
RH
3250
3251 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
be3ccd9c 3252 fprintf (file, _(" [mips1]"));
252b5132 3253 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
be3ccd9c 3254 fprintf (file, _(" [mips2]"));
252b5132 3255 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
be3ccd9c 3256 fprintf (file, _(" [mips3]"));
252b5132 3257 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
be3ccd9c 3258 fprintf (file, _(" [mips4]"));
84ea6cf2
NC
3259 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
3260 fprintf (file, _ (" [mips5]"));
e7af610e
NC
3261 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
3262 fprintf (file, _ (" [mips32]"));
84ea6cf2
NC
3263 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
3264 fprintf (file, _ (" [mips64]"));
252b5132 3265 else
be3ccd9c 3266 fprintf (file, _(" [unknown ISA]"));
252b5132
RH
3267
3268 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
be3ccd9c 3269 fprintf (file, _(" [32bitmode]"));
252b5132 3270 else
be3ccd9c 3271 fprintf (file, _(" [not 32bitmode]"));
252b5132
RH
3272
3273 fputc ('\n', file);
3274
3275 return true;
3276}
3277\f
3278/* Handle a MIPS specific section when reading an object file. This
3279 is called when elfcode.h finds a section with an unknown type.
3280 This routine supports both the 32-bit and 64-bit ELF ABI.
3281
3282 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
3283 how to. */
3284
3285boolean
3286_bfd_mips_elf_section_from_shdr (abfd, hdr, name)
3287 bfd *abfd;
3288 Elf_Internal_Shdr *hdr;
103186c6 3289 char *name;
252b5132
RH
3290{
3291 flagword flags = 0;
3292
3293 /* There ought to be a place to keep ELF backend specific flags, but
3294 at the moment there isn't one. We just keep track of the
3295 sections by their name, instead. Fortunately, the ABI gives
3296 suggested names for all the MIPS specific sections, so we will
3297 probably get away with this. */
3298 switch (hdr->sh_type)
3299 {
3300 case SHT_MIPS_LIBLIST:
3301 if (strcmp (name, ".liblist") != 0)
3302 return false;
3303 break;
3304 case SHT_MIPS_MSYM:
c6142e5d 3305 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
252b5132
RH
3306 return false;
3307 break;
3308 case SHT_MIPS_CONFLICT:
3309 if (strcmp (name, ".conflict") != 0)
3310 return false;
3311 break;
3312 case SHT_MIPS_GPTAB:
3313 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
3314 return false;
3315 break;
3316 case SHT_MIPS_UCODE:
3317 if (strcmp (name, ".ucode") != 0)
3318 return false;
3319 break;
3320 case SHT_MIPS_DEBUG:
3321 if (strcmp (name, ".mdebug") != 0)
3322 return false;
3323 flags = SEC_DEBUGGING;
3324 break;
3325 case SHT_MIPS_REGINFO:
3326 if (strcmp (name, ".reginfo") != 0
3327 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
3328 return false;
3329 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
3330 break;
3331 case SHT_MIPS_IFACE:
3332 if (strcmp (name, ".MIPS.interfaces") != 0)
3333 return false;
3334 break;
3335 case SHT_MIPS_CONTENT:
3336 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
3337 return false;
3338 break;
3339 case SHT_MIPS_OPTIONS:
303f629d 3340 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
252b5132
RH
3341 return false;
3342 break;
3343 case SHT_MIPS_DWARF:
3344 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
3345 return false;
3346 break;
3347 case SHT_MIPS_SYMBOL_LIB:
3348 if (strcmp (name, ".MIPS.symlib") != 0)
3349 return false;
3350 break;
3351 case SHT_MIPS_EVENTS:
3352 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
3353 && strncmp (name, ".MIPS.post_rel",
3354 sizeof ".MIPS.post_rel" - 1) != 0)
3355 return false;
3356 break;
3357 default:
3358 return false;
3359 }
3360
3361 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
3362 return false;
3363
3364 if (flags)
3365 {
3366 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
3367 (bfd_get_section_flags (abfd,
3368 hdr->bfd_section)
3369 | flags)))
3370 return false;
3371 }
3372
252b5132
RH
3373 /* FIXME: We should record sh_info for a .gptab section. */
3374
3375 /* For a .reginfo section, set the gp value in the tdata information
3376 from the contents of this section. We need the gp value while
3377 processing relocs, so we just get it now. The .reginfo section
3378 is not used in the 64-bit MIPS ELF ABI. */
3379 if (hdr->sh_type == SHT_MIPS_REGINFO)
3380 {
3381 Elf32_External_RegInfo ext;
3382 Elf32_RegInfo s;
3383
3384 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
dc810e39
AM
3385 (file_ptr) 0,
3386 (bfd_size_type) sizeof ext))
252b5132
RH
3387 return false;
3388 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
3389 elf_gp (abfd) = s.ri_gp_value;
3390 }
3391
3392 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
3393 set the gp value based on what we find. We may see both
3394 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
3395 they should agree. */
3396 if (hdr->sh_type == SHT_MIPS_OPTIONS)
3397 {
3398 bfd_byte *contents, *l, *lend;
3399
3400 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3401 if (contents == NULL)
3402 return false;
3403 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
3404 (file_ptr) 0, hdr->sh_size))
3405 {
3406 free (contents);
3407 return false;
3408 }
3409 l = contents;
3410 lend = contents + hdr->sh_size;
3411 while (l + sizeof (Elf_External_Options) <= lend)
3412 {
3413 Elf_Internal_Options intopt;
3414
3415 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3416 &intopt);
103186c6
MM
3417 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3418 {
3419 Elf64_Internal_RegInfo intreg;
3420
3421 bfd_mips_elf64_swap_reginfo_in
3422 (abfd,
3423 ((Elf64_External_RegInfo *)
3424 (l + sizeof (Elf_External_Options))),
3425 &intreg);
3426 elf_gp (abfd) = intreg.ri_gp_value;
3427 }
3428 else if (intopt.kind == ODK_REGINFO)
252b5132
RH
3429 {
3430 Elf32_RegInfo intreg;
3431
3432 bfd_mips_elf32_swap_reginfo_in
3433 (abfd,
3434 ((Elf32_External_RegInfo *)
3435 (l + sizeof (Elf_External_Options))),
3436 &intreg);
3437 elf_gp (abfd) = intreg.ri_gp_value;
3438 }
3439 l += intopt.size;
3440 }
3441 free (contents);
3442 }
3443
3444 return true;
3445}
3446
3447/* Set the correct type for a MIPS ELF section. We do this by the
3448 section name, which is a hack, but ought to work. This routine is
3449 used by both the 32-bit and the 64-bit ABI. */
3450
3451boolean
3452_bfd_mips_elf_fake_sections (abfd, hdr, sec)
3453 bfd *abfd;
3454 Elf32_Internal_Shdr *hdr;
3455 asection *sec;
3456{
3457 register const char *name;
3458
3459 name = bfd_get_section_name (abfd, sec);
3460
3461 if (strcmp (name, ".liblist") == 0)
3462 {
3463 hdr->sh_type = SHT_MIPS_LIBLIST;
3464 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
3465 /* The sh_link field is set in final_write_processing. */
3466 }
252b5132
RH
3467 else if (strcmp (name, ".conflict") == 0)
3468 hdr->sh_type = SHT_MIPS_CONFLICT;
3469 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
3470 {
3471 hdr->sh_type = SHT_MIPS_GPTAB;
3472 hdr->sh_entsize = sizeof (Elf32_External_gptab);
3473 /* The sh_info field is set in final_write_processing. */
3474 }
3475 else if (strcmp (name, ".ucode") == 0)
3476 hdr->sh_type = SHT_MIPS_UCODE;
3477 else if (strcmp (name, ".mdebug") == 0)
3478 {
3479 hdr->sh_type = SHT_MIPS_DEBUG;
3480 /* In a shared object on Irix 5.3, the .mdebug section has an
3481 entsize of 0. FIXME: Does this matter? */
3482 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
3483 hdr->sh_entsize = 0;
3484 else
3485 hdr->sh_entsize = 1;
3486 }
3487 else if (strcmp (name, ".reginfo") == 0)
3488 {
3489 hdr->sh_type = SHT_MIPS_REGINFO;
3490 /* In a shared object on Irix 5.3, the .reginfo section has an
3491 entsize of 0x18. FIXME: Does this matter? */
f7cb7d68 3492 if (SGI_COMPAT (abfd))
be3ccd9c
KH
3493 {
3494 if ((abfd->flags & DYNAMIC) != 0)
3495 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3496 else
3497 hdr->sh_entsize = 1;
3498 }
252b5132 3499 else
be3ccd9c 3500 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
252b5132
RH
3501 }
3502 else if (SGI_COMPAT (abfd)
3503 && (strcmp (name, ".hash") == 0
3504 || strcmp (name, ".dynamic") == 0
3505 || strcmp (name, ".dynstr") == 0))
3506 {
be3ccd9c
KH
3507 if (SGI_COMPAT (abfd))
3508 hdr->sh_entsize = 0;
252b5132
RH
3509#if 0
3510 /* This isn't how the Irix 6 linker behaves. */
3511 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
3512#endif
3513 }
3514 else if (strcmp (name, ".got") == 0
303f629d 3515 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
252b5132
RH
3516 || strcmp (name, ".sdata") == 0
3517 || strcmp (name, ".sbss") == 0
3518 || strcmp (name, ".lit4") == 0
3519 || strcmp (name, ".lit8") == 0)
3520 hdr->sh_flags |= SHF_MIPS_GPREL;
3521 else if (strcmp (name, ".MIPS.interfaces") == 0)
3522 {
3523 hdr->sh_type = SHT_MIPS_IFACE;
3524 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3525 }
3f830999 3526 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
252b5132
RH
3527 {
3528 hdr->sh_type = SHT_MIPS_CONTENT;
3f830999 3529 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
252b5132
RH
3530 /* The sh_info field is set in final_write_processing. */
3531 }
303f629d 3532 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
252b5132
RH
3533 {
3534 hdr->sh_type = SHT_MIPS_OPTIONS;
3535 hdr->sh_entsize = 1;
3536 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3537 }
3538 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
3539 hdr->sh_type = SHT_MIPS_DWARF;
3540 else if (strcmp (name, ".MIPS.symlib") == 0)
3541 {
3542 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
3543 /* The sh_link and sh_info fields are set in
3544 final_write_processing. */
3545 }
3546 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
3547 || strncmp (name, ".MIPS.post_rel",
3548 sizeof ".MIPS.post_rel" - 1) == 0)
3549 {
3550 hdr->sh_type = SHT_MIPS_EVENTS;
3551 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3552 /* The sh_link field is set in final_write_processing. */
3553 }
c6142e5d
MM
3554 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
3555 {
3556 hdr->sh_type = SHT_MIPS_MSYM;
3557 hdr->sh_flags |= SHF_ALLOC;
3558 hdr->sh_entsize = 8;
3559 }
252b5132 3560
23bc299b
MM
3561 /* The generic elf_fake_sections will set up REL_HDR using the
3562 default kind of relocations. But, we may actually need both
3563 kinds of relocations, so we set up the second header here. */
3564 if ((sec->flags & SEC_RELOC) != 0)
3565 {
3566 struct bfd_elf_section_data *esd;
dc810e39 3567 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
23bc299b
MM
3568
3569 esd = elf_section_data (sec);
3570 BFD_ASSERT (esd->rel_hdr2 == NULL);
dc810e39 3571 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
23bc299b
MM
3572 if (!esd->rel_hdr2)
3573 return false;
3574 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
3575 !elf_section_data (sec)->use_rela_p);
3576 }
3577
252b5132
RH
3578 return true;
3579}
3580
3581/* Given a BFD section, try to locate the corresponding ELF section
3582 index. This is used by both the 32-bit and the 64-bit ABI.
3583 Actually, it's not clear to me that the 64-bit ABI supports these,
3584 but for non-PIC objects we will certainly want support for at least
3585 the .scommon section. */
3586
3587boolean
af746e92 3588_bfd_mips_elf_section_from_bfd_section (abfd, sec, retval)
d9bc7a44 3589 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
3590 asection *sec;
3591 int *retval;
3592{
3593 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
3594 {
3595 *retval = SHN_MIPS_SCOMMON;
3596 return true;
3597 }
3598 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
3599 {
3600 *retval = SHN_MIPS_ACOMMON;
3601 return true;
3602 }
3603 return false;
3604}
3605
3606/* When are writing out the .options or .MIPS.options section,
3607 remember the bytes we are writing out, so that we can install the
3608 GP value in the section_processing routine. */
3609
3610boolean
3611_bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
3612 bfd *abfd;
3613 sec_ptr section;
3614 PTR location;
3615 file_ptr offset;
3616 bfd_size_type count;
3617{
303f629d 3618 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
252b5132
RH
3619 {
3620 bfd_byte *c;
3621
3622 if (elf_section_data (section) == NULL)
3623 {
dc810e39
AM
3624 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
3625 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
252b5132
RH
3626 if (elf_section_data (section) == NULL)
3627 return false;
3628 }
3629 c = (bfd_byte *) elf_section_data (section)->tdata;
3630 if (c == NULL)
3631 {
3632 bfd_size_type size;
3633
3634 if (section->_cooked_size != 0)
3635 size = section->_cooked_size;
3636 else
3637 size = section->_raw_size;
3638 c = (bfd_byte *) bfd_zalloc (abfd, size);
3639 if (c == NULL)
3640 return false;
3641 elf_section_data (section)->tdata = (PTR) c;
3642 }
3643
dc810e39 3644 memcpy (c + offset, location, (size_t) count);
252b5132
RH
3645 }
3646
3647 return _bfd_elf_set_section_contents (abfd, section, location, offset,
3648 count);
3649}
3650
3651/* Work over a section just before writing it out. This routine is
3652 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
3653 sections that need the SHF_MIPS_GPREL flag by name; there has to be
3654 a better way. */
3655
3656boolean
3657_bfd_mips_elf_section_processing (abfd, hdr)
3658 bfd *abfd;
3659 Elf_Internal_Shdr *hdr;
252b5132 3660{
cc3bfcee
ILT
3661 if (hdr->sh_type == SHT_MIPS_REGINFO
3662 && hdr->sh_size > 0)
252b5132
RH
3663 {
3664 bfd_byte buf[4];
3665
3666 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
3667 BFD_ASSERT (hdr->contents == NULL);
3668
3669 if (bfd_seek (abfd,
3670 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
dc810e39 3671 SEEK_SET) != 0)
252b5132 3672 return false;
dc810e39
AM
3673 H_PUT_32 (abfd, elf_gp (abfd), buf);
3674 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
252b5132
RH
3675 return false;
3676 }
3677
3678 if (hdr->sh_type == SHT_MIPS_OPTIONS
3679 && hdr->bfd_section != NULL
3680 && elf_section_data (hdr->bfd_section) != NULL
3681 && elf_section_data (hdr->bfd_section)->tdata != NULL)
3682 {
3683 bfd_byte *contents, *l, *lend;
3684
3685 /* We stored the section contents in the elf_section_data tdata
3686 field in the set_section_contents routine. We save the
3687 section contents so that we don't have to read them again.
3688 At this point we know that elf_gp is set, so we can look
3689 through the section contents to see if there is an
3690 ODK_REGINFO structure. */
3691
3692 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
3693 l = contents;
3694 lend = contents + hdr->sh_size;
3695 while (l + sizeof (Elf_External_Options) <= lend)
3696 {
3697 Elf_Internal_Options intopt;
3698
3699 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3700 &intopt);
103186c6
MM
3701 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3702 {
3703 bfd_byte buf[8];
3704
3705 if (bfd_seek (abfd,
3706 (hdr->sh_offset
3707 + (l - contents)
3708 + sizeof (Elf_External_Options)
3709 + (sizeof (Elf64_External_RegInfo) - 8)),
dc810e39 3710 SEEK_SET) != 0)
103186c6 3711 return false;
dc810e39
AM
3712 H_PUT_64 (abfd, elf_gp (abfd), buf);
3713 if (bfd_bwrite (buf, (bfd_size_type) 8, abfd) != 8)
103186c6
MM
3714 return false;
3715 }
3716 else if (intopt.kind == ODK_REGINFO)
252b5132
RH
3717 {
3718 bfd_byte buf[4];
3719
3720 if (bfd_seek (abfd,
3721 (hdr->sh_offset
3722 + (l - contents)
3723 + sizeof (Elf_External_Options)
3724 + (sizeof (Elf32_External_RegInfo) - 4)),
dc810e39 3725 SEEK_SET) != 0)
252b5132 3726 return false;
dc810e39
AM
3727 H_PUT_32 (abfd, elf_gp (abfd), buf);
3728 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
252b5132
RH
3729 return false;
3730 }
3731 l += intopt.size;
3732 }
3733 }
3734
103186c6
MM
3735 if (hdr->bfd_section != NULL)
3736 {
3737 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3738
3739 if (strcmp (name, ".sdata") == 0
3740 || strcmp (name, ".lit8") == 0
3741 || strcmp (name, ".lit4") == 0)
3742 {
3743 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3744 hdr->sh_type = SHT_PROGBITS;
3745 }
3746 else if (strcmp (name, ".sbss") == 0)
3747 {
3748 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3749 hdr->sh_type = SHT_NOBITS;
3750 }
3751 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3752 {
3753 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3754 hdr->sh_type = SHT_PROGBITS;
3755 }
3756 else if (strcmp (name, ".compact_rel") == 0)
3757 {
3758 hdr->sh_flags = 0;
3759 hdr->sh_type = SHT_PROGBITS;
3760 }
3761 else if (strcmp (name, ".rtproc") == 0)
3762 {
3763 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3764 {
3765 unsigned int adjust;
3766
3767 adjust = hdr->sh_size % hdr->sh_addralign;
3768 if (adjust != 0)
3769 hdr->sh_size += hdr->sh_addralign - adjust;
3770 }
3771 }
3772 }
3773
3774 return true;
252b5132
RH
3775}
3776\f
3777/* MIPS ELF uses two common sections. One is the usual one, and the
3778 other is for small objects. All the small objects are kept
3779 together, and then referenced via the gp pointer, which yields
3780 faster assembler code. This is what we use for the small common
3781 section. This approach is copied from ecoff.c. */
3782static asection mips_elf_scom_section;
3783static asymbol mips_elf_scom_symbol;
3784static asymbol *mips_elf_scom_symbol_ptr;
3785
3786/* MIPS ELF also uses an acommon section, which represents an
3787 allocated common symbol which may be overridden by a
3788 definition in a shared library. */
3789static asection mips_elf_acom_section;
3790static asymbol mips_elf_acom_symbol;
3791static asymbol *mips_elf_acom_symbol_ptr;
3792
252b5132
RH
3793/* Handle the special MIPS section numbers that a symbol may use.
3794 This is used for both the 32-bit and the 64-bit ABI. */
3795
3796void
3797_bfd_mips_elf_symbol_processing (abfd, asym)
3798 bfd *abfd;
3799 asymbol *asym;
3800{
3801 elf_symbol_type *elfsym;
3802
3803 elfsym = (elf_symbol_type *) asym;
3804 switch (elfsym->internal_elf_sym.st_shndx)
3805 {
3806 case SHN_MIPS_ACOMMON:
3807 /* This section is used in a dynamically linked executable file.
3808 It is an allocated common section. The dynamic linker can
3809 either resolve these symbols to something in a shared
3810 library, or it can just leave them here. For our purposes,
3811 we can consider these symbols to be in a new section. */
3812 if (mips_elf_acom_section.name == NULL)
3813 {
3814 /* Initialize the acommon section. */
3815 mips_elf_acom_section.name = ".acommon";
3816 mips_elf_acom_section.flags = SEC_ALLOC;
3817 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3818 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3819 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3820 mips_elf_acom_symbol.name = ".acommon";
3821 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3822 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3823 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3824 }
3825 asym->section = &mips_elf_acom_section;
3826 break;
3827
3828 case SHN_COMMON:
3829 /* Common symbols less than the GP size are automatically
7403cb63
MM
3830 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3831 if (asym->value > elf_gp_size (abfd)
3832 || IRIX_COMPAT (abfd) == ict_irix6)
252b5132
RH
3833 break;
3834 /* Fall through. */
3835 case SHN_MIPS_SCOMMON:
3836 if (mips_elf_scom_section.name == NULL)
3837 {
3838 /* Initialize the small common section. */
3839 mips_elf_scom_section.name = ".scommon";
3840 mips_elf_scom_section.flags = SEC_IS_COMMON;
3841 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3842 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3843 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3844 mips_elf_scom_symbol.name = ".scommon";
3845 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3846 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3847 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3848 }
3849 asym->section = &mips_elf_scom_section;
3850 asym->value = elfsym->internal_elf_sym.st_size;
3851 break;
3852
3853 case SHN_MIPS_SUNDEFINED:
3854 asym->section = bfd_und_section_ptr;
3855 break;
3856
3857#if 0 /* for SGI_COMPAT */
3858 case SHN_MIPS_TEXT:
3859 asym->section = mips_elf_text_section_ptr;
3860 break;
3861
3862 case SHN_MIPS_DATA:
3863 asym->section = mips_elf_data_section_ptr;
3864 break;
3865#endif
3866 }
3867}
3868\f
3869/* When creating an Irix 5 executable, we need REGINFO and RTPROC
3870 segments. */
3871
103186c6
MM
3872int
3873_bfd_mips_elf_additional_program_headers (abfd)
252b5132
RH
3874 bfd *abfd;
3875{
3876 asection *s;
303f629d 3877 int ret = 0;
252b5132 3878
303f629d 3879 /* See if we need a PT_MIPS_REGINFO segment. */
252b5132 3880 s = bfd_get_section_by_name (abfd, ".reginfo");
303f629d
MM
3881 if (s && (s->flags & SEC_LOAD))
3882 ++ret;
252b5132 3883
303f629d
MM
3884 /* See if we need a PT_MIPS_OPTIONS segment. */
3885 if (IRIX_COMPAT (abfd) == ict_irix6
be3ccd9c 3886 && bfd_get_section_by_name (abfd,
303f629d
MM
3887 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3888 ++ret;
3889
3890 /* See if we need a PT_MIPS_RTPROC segment. */
3891 if (IRIX_COMPAT (abfd) == ict_irix5
3892 && bfd_get_section_by_name (abfd, ".dynamic")
3893 && bfd_get_section_by_name (abfd, ".mdebug"))
3894 ++ret;
252b5132
RH
3895
3896 return ret;
3897}
3898
3899/* Modify the segment map for an Irix 5 executable. */
3900
103186c6
MM
3901boolean
3902_bfd_mips_elf_modify_segment_map (abfd)
252b5132
RH
3903 bfd *abfd;
3904{
3905 asection *s;
3906 struct elf_segment_map *m, **pm;
dc810e39 3907 bfd_size_type amt;
252b5132 3908
252b5132
RH
3909 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3910 segment. */
3911 s = bfd_get_section_by_name (abfd, ".reginfo");
3912 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3913 {
3914 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3915 if (m->p_type == PT_MIPS_REGINFO)
3916 break;
3917 if (m == NULL)
3918 {
dc810e39
AM
3919 amt = sizeof *m;
3920 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3921 if (m == NULL)
3922 return false;
3923
3924 m->p_type = PT_MIPS_REGINFO;
3925 m->count = 1;
3926 m->sections[0] = s;
3927
3928 /* We want to put it after the PHDR and INTERP segments. */
3929 pm = &elf_tdata (abfd)->segment_map;
3930 while (*pm != NULL
3931 && ((*pm)->p_type == PT_PHDR
3932 || (*pm)->p_type == PT_INTERP))
3933 pm = &(*pm)->next;
3934
3935 m->next = *pm;
3936 *pm = m;
3937 }
3938 }
3939
303f629d
MM
3940 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3941 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3942 PT_OPTIONS segement immediately following the program header
3943 table. */
3944 if (IRIX_COMPAT (abfd) == ict_irix6)
252b5132 3945 {
303f629d
MM
3946 for (s = abfd->sections; s; s = s->next)
3947 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
252b5132 3948 break;
303f629d
MM
3949
3950 if (s)
252b5132 3951 {
303f629d
MM
3952 struct elf_segment_map *options_segment;
3953
435394bf
MM
3954 /* Usually, there's a program header table. But, sometimes
3955 there's not (like when running the `ld' testsuite). So,
3956 if there's no program header table, we just put the
3957 options segement at the end. */
be3ccd9c 3958 for (pm = &elf_tdata (abfd)->segment_map;
435394bf
MM
3959 *pm != NULL;
3960 pm = &(*pm)->next)
3961 if ((*pm)->p_type == PT_PHDR)
303f629d
MM
3962 break;
3963
dc810e39
AM
3964 amt = sizeof (struct elf_segment_map);
3965 options_segment = bfd_zalloc (abfd, amt);
435394bf 3966 options_segment->next = *pm;
303f629d
MM
3967 options_segment->p_type = PT_MIPS_OPTIONS;
3968 options_segment->p_flags = PF_R;
3969 options_segment->p_flags_valid = true;
3970 options_segment->count = 1;
3971 options_segment->sections[0] = s;
435394bf 3972 *pm = options_segment;
303f629d
MM
3973 }
3974 }
3975 else
3976 {
f7cb7d68 3977 if (IRIX_COMPAT (abfd) == ict_irix5)
303f629d 3978 {
f7cb7d68
UC
3979 /* If there are .dynamic and .mdebug sections, we make a room
3980 for the RTPROC header. FIXME: Rewrite without section names. */
3981 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3982 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3983 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
252b5132 3984 {
f7cb7d68
UC
3985 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3986 if (m->p_type == PT_MIPS_RTPROC)
3987 break;
303f629d 3988 if (m == NULL)
f7cb7d68 3989 {
dc810e39
AM
3990 amt = sizeof *m;
3991 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f7cb7d68
UC
3992 if (m == NULL)
3993 return false;
252b5132 3994
f7cb7d68 3995 m->p_type = PT_MIPS_RTPROC;
252b5132 3996
f7cb7d68
UC
3997 s = bfd_get_section_by_name (abfd, ".rtproc");
3998 if (s == NULL)
3999 {
4000 m->count = 0;
4001 m->p_flags = 0;
4002 m->p_flags_valid = 1;
4003 }
4004 else
4005 {
4006 m->count = 1;
4007 m->sections[0] = s;
4008 }
303f629d 4009
f7cb7d68
UC
4010 /* We want to put it after the DYNAMIC segment. */
4011 pm = &elf_tdata (abfd)->segment_map;
4012 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
4013 pm = &(*pm)->next;
4014 if (*pm != NULL)
4015 pm = &(*pm)->next;
303f629d 4016
f7cb7d68
UC
4017 m->next = *pm;
4018 *pm = m;
4019 }
303f629d 4020 }
252b5132 4021 }
303f629d
MM
4022 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
4023 .dynstr, .dynsym, and .hash sections, and everything in
4024 between. */
f7cb7d68
UC
4025 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
4026 pm = &(*pm)->next)
303f629d
MM
4027 if ((*pm)->p_type == PT_DYNAMIC)
4028 break;
4029 m = *pm;
1b58be7e 4030 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
f7cb7d68
UC
4031 {
4032 /* For a normal mips executable the permissions for the PT_DYNAMIC
4033 segment are read, write and execute. We do that here since
4034 the code in elf.c sets only the read permission. This matters
be3ccd9c 4035 sometimes for the dynamic linker. */
f7cb7d68
UC
4036 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4037 {
4038 m->p_flags = PF_R | PF_W | PF_X;
4039 m->p_flags_valid = 1;
4040 }
4041 }
303f629d 4042 if (m != NULL
f7cb7d68 4043 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
252b5132 4044 {
38b1a46c
NC
4045 static const char *sec_names[] =
4046 {
be3ccd9c
KH
4047 ".dynamic", ".dynstr", ".dynsym", ".hash"
4048 };
303f629d
MM
4049 bfd_vma low, high;
4050 unsigned int i, c;
4051 struct elf_segment_map *n;
4052
4053 low = 0xffffffff;
4054 high = 0;
4055 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
252b5132 4056 {
303f629d
MM
4057 s = bfd_get_section_by_name (abfd, sec_names[i]);
4058 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4059 {
4060 bfd_size_type sz;
4061
4062 if (low > s->vma)
4063 low = s->vma;
4064 sz = s->_cooked_size;
4065 if (sz == 0)
4066 sz = s->_raw_size;
4067 if (high < s->vma + sz)
4068 high = s->vma + sz;
4069 }
252b5132 4070 }
252b5132 4071
303f629d
MM
4072 c = 0;
4073 for (s = abfd->sections; s != NULL; s = s->next)
4074 if ((s->flags & SEC_LOAD) != 0
4075 && s->vma >= low
4076 && ((s->vma
f7cb7d68
UC
4077 + (s->_cooked_size !=
4078 0 ? s->_cooked_size : s->_raw_size)) <= high))
303f629d
MM
4079 ++c;
4080
dc810e39
AM
4081 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
4082 n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
303f629d
MM
4083 if (n == NULL)
4084 return false;
4085 *n = *m;
4086 n->count = c;
252b5132 4087
303f629d
MM
4088 i = 0;
4089 for (s = abfd->sections; s != NULL; s = s->next)
252b5132 4090 {
303f629d
MM
4091 if ((s->flags & SEC_LOAD) != 0
4092 && s->vma >= low
4093 && ((s->vma
4094 + (s->_cooked_size != 0 ?
f7cb7d68 4095 s->_cooked_size : s->_raw_size)) <= high))
303f629d
MM
4096 {
4097 n->sections[i] = s;
4098 ++i;
4099 }
252b5132 4100 }
252b5132 4101
303f629d
MM
4102 *pm = n;
4103 }
252b5132
RH
4104 }
4105
4106 return true;
4107}
4108\f
4109/* The structure of the runtime procedure descriptor created by the
4110 loader for use by the static exception system. */
4111
4112typedef struct runtime_pdr {
4113 bfd_vma adr; /* memory address of start of procedure */
4114 long regmask; /* save register mask */
4115 long regoffset; /* save register offset */
4116 long fregmask; /* save floating point register mask */
4117 long fregoffset; /* save floating point register offset */
4118 long frameoffset; /* frame size */
4119 short framereg; /* frame pointer register */
4120 short pcreg; /* offset or reg of return pc */
4121 long irpss; /* index into the runtime string table */
4122 long reserved;
4123 struct exception_info *exception_info;/* pointer to exception array */
4124} RPDR, *pRPDR;
be3ccd9c 4125#define cbRPDR sizeof (RPDR)
252b5132
RH
4126#define rpdNil ((pRPDR) 0)
4127
4128/* Swap RPDR (runtime procedure table entry) for output. */
4129
4130static void ecoff_swap_rpdr_out
4131 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
4132
4133static void
4134ecoff_swap_rpdr_out (abfd, in, ex)
4135 bfd *abfd;
4136 const RPDR *in;
4137 struct rpdr_ext *ex;
4138{
dc810e39
AM
4139 /* ECOFF_PUT_OFF was defined in ecoffswap.h. */
4140 ECOFF_PUT_OFF (abfd, in->adr, ex->p_adr);
4141 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
4142 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
4143 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
4144 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
4145 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
4146
4147 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
4148 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
4149
4150 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
252b5132 4151#if 0 /* FIXME */
dc810e39 4152 ECOFF_PUT_OFF (abfd, in->exception_info, ex->p_exception_info);
252b5132
RH
4153#endif
4154}
4155\f
4156/* Read ECOFF debugging information from a .mdebug section into a
4157 ecoff_debug_info structure. */
4158
4159boolean
4160_bfd_mips_elf_read_ecoff_info (abfd, section, debug)
4161 bfd *abfd;
4162 asection *section;
4163 struct ecoff_debug_info *debug;
4164{
4165 HDRR *symhdr;
4166 const struct ecoff_debug_swap *swap;
4167 char *ext_hdr = NULL;
4168
4169 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
be3ccd9c 4170 memset (debug, 0, sizeof (*debug));
252b5132 4171
dc810e39 4172 ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
252b5132
RH
4173 if (ext_hdr == NULL && swap->external_hdr_size != 0)
4174 goto error_return;
4175
4176 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
4177 swap->external_hdr_size)
4178 == false)
4179 goto error_return;
4180
4181 symhdr = &debug->symbolic_header;
4182 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
4183
4184 /* The symbolic header contains absolute file offsets and sizes to
4185 read. */
4186#define READ(ptr, offset, count, size, type) \
4187 if (symhdr->count == 0) \
4188 debug->ptr = NULL; \
4189 else \
4190 { \
dc810e39
AM
4191 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
4192 debug->ptr = (type) bfd_malloc (amt); \
252b5132
RH
4193 if (debug->ptr == NULL) \
4194 goto error_return; \
4195 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
dc810e39 4196 || bfd_bread (debug->ptr, amt, abfd) != amt) \
252b5132
RH
4197 goto error_return; \
4198 }
4199
4200 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
4201 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4202 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4203 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4204 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4205 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4206 union aux_ext *);
4207 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4208 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
4209 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4210 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4211 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
4212#undef READ
4213
4214 debug->fdr = NULL;
4215 debug->adjust = NULL;
4216
4217 return true;
4218
4219 error_return:
4220 if (ext_hdr != NULL)
4221 free (ext_hdr);
4222 if (debug->line != NULL)
4223 free (debug->line);
4224 if (debug->external_dnr != NULL)
4225 free (debug->external_dnr);
4226 if (debug->external_pdr != NULL)
4227 free (debug->external_pdr);
4228 if (debug->external_sym != NULL)
4229 free (debug->external_sym);
4230 if (debug->external_opt != NULL)
4231 free (debug->external_opt);
4232 if (debug->external_aux != NULL)
4233 free (debug->external_aux);
4234 if (debug->ss != NULL)
4235 free (debug->ss);
4236 if (debug->ssext != NULL)
4237 free (debug->ssext);
4238 if (debug->external_fdr != NULL)
4239 free (debug->external_fdr);
4240 if (debug->external_rfd != NULL)
4241 free (debug->external_rfd);
4242 if (debug->external_ext != NULL)
4243 free (debug->external_ext);
4244 return false;
4245}
4246\f
4247/* MIPS ELF local labels start with '$', not 'L'. */
4248
252b5132
RH
4249static boolean
4250mips_elf_is_local_label_name (abfd, name)
4251 bfd *abfd;
4252 const char *name;
4253{
4254 if (name[0] == '$')
4255 return true;
4256
4257 /* On Irix 6, the labels go back to starting with '.', so we accept
4258 the generic ELF local label syntax as well. */
4259 return _bfd_elf_is_local_label_name (abfd, name);
4260}
4261
4262/* MIPS ELF uses a special find_nearest_line routine in order the
4263 handle the ECOFF debugging information. */
4264
38b1a46c
NC
4265struct mips_elf_find_line
4266{
252b5132
RH
4267 struct ecoff_debug_info d;
4268 struct ecoff_find_line i;
4269};
4270
4271boolean
4272_bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
4273 functionname_ptr, line_ptr)
4274 bfd *abfd;
4275 asection *section;
4276 asymbol **symbols;
4277 bfd_vma offset;
4278 const char **filename_ptr;
4279 const char **functionname_ptr;
4280 unsigned int *line_ptr;
4281{
4282 asection *msec;
4283
4284 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4285 filename_ptr, functionname_ptr,
4286 line_ptr))
4287 return true;
4288
4289 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4290 filename_ptr, functionname_ptr,
be3ccd9c 4291 line_ptr,
dc810e39 4292 (unsigned) (ABI_64_P (abfd) ? 8 : 0),
857ec808 4293 &elf_tdata (abfd)->dwarf2_find_line_info))
252b5132
RH
4294 return true;
4295
4296 msec = bfd_get_section_by_name (abfd, ".mdebug");
4297 if (msec != NULL)
4298 {
4299 flagword origflags;
4300 struct mips_elf_find_line *fi;
4301 const struct ecoff_debug_swap * const swap =
4302 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4303
4304 /* If we are called during a link, mips_elf_final_link may have
4305 cleared the SEC_HAS_CONTENTS field. We force it back on here
4306 if appropriate (which it normally will be). */
4307 origflags = msec->flags;
4308 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
4309 msec->flags |= SEC_HAS_CONTENTS;
4310
4311 fi = elf_tdata (abfd)->find_line_info;
4312 if (fi == NULL)
4313 {
4314 bfd_size_type external_fdr_size;
4315 char *fraw_src;
4316 char *fraw_end;
4317 struct fdr *fdr_ptr;
dc810e39 4318 bfd_size_type amt = sizeof (struct mips_elf_find_line);
252b5132 4319
dc810e39 4320 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
252b5132
RH
4321 if (fi == NULL)
4322 {
4323 msec->flags = origflags;
4324 return false;
4325 }
4326
4327 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
4328 {
4329 msec->flags = origflags;
4330 return false;
4331 }
4332
4333 /* Swap in the FDR information. */
dc810e39
AM
4334 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
4335 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
252b5132
RH
4336 if (fi->d.fdr == NULL)
4337 {
4338 msec->flags = origflags;
4339 return false;
4340 }
4341 external_fdr_size = swap->external_fdr_size;
4342 fdr_ptr = fi->d.fdr;
4343 fraw_src = (char *) fi->d.external_fdr;
4344 fraw_end = (fraw_src
4345 + fi->d.symbolic_header.ifdMax * external_fdr_size);
4346 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
4347 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
4348
4349 elf_tdata (abfd)->find_line_info = fi;
4350
4351 /* Note that we don't bother to ever free this information.
4352 find_nearest_line is either called all the time, as in
4353 objdump -l, so the information should be saved, or it is
4354 rarely called, as in ld error messages, so the memory
4355 wasted is unimportant. Still, it would probably be a
4356 good idea for free_cached_info to throw it away. */
4357 }
4358
4359 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
4360 &fi->i, filename_ptr, functionname_ptr,
4361 line_ptr))
4362 {
4363 msec->flags = origflags;
4364 return true;
4365 }
4366
4367 msec->flags = origflags;
4368 }
4369
4370 /* Fall back on the generic ELF find_nearest_line routine. */
4371
4372 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
4373 filename_ptr, functionname_ptr,
4374 line_ptr);
4375}
4376\f
4377 /* The mips16 compiler uses a couple of special sections to handle
4378 floating point arguments.
4379
4380 Section names that look like .mips16.fn.FNNAME contain stubs that
4381 copy floating point arguments from the fp regs to the gp regs and
4382 then jump to FNNAME. If any 32 bit function calls FNNAME, the
4383 call should be redirected to the stub instead. If no 32 bit
4384 function calls FNNAME, the stub should be discarded. We need to
4385 consider any reference to the function, not just a call, because
4386 if the address of the function is taken we will need the stub,
4387 since the address might be passed to a 32 bit function.
4388
4389 Section names that look like .mips16.call.FNNAME contain stubs
4390 that copy floating point arguments from the gp regs to the fp
4391 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
4392 then any 16 bit function that calls FNNAME should be redirected
4393 to the stub instead. If FNNAME is not a 32 bit function, the
4394 stub should be discarded.
4395
4396 .mips16.call.fp.FNNAME sections are similar, but contain stubs
4397 which call FNNAME and then copy the return value from the fp regs
4398 to the gp regs. These stubs store the return value in $18 while
4399 calling FNNAME; any function which might call one of these stubs
4400 must arrange to save $18 around the call. (This case is not
4401 needed for 32 bit functions that call 16 bit functions, because
4402 16 bit functions always return floating point values in both
4403 $f0/$f1 and $2/$3.)
4404
4405 Note that in all cases FNNAME might be defined statically.
4406 Therefore, FNNAME is not used literally. Instead, the relocation
4407 information will indicate which symbol the section is for.
4408
4409 We record any stubs that we find in the symbol table. */
4410
4411#define FN_STUB ".mips16.fn."
4412#define CALL_STUB ".mips16.call."
4413#define CALL_FP_STUB ".mips16.call.fp."
4414
252b5132
RH
4415/* MIPS ELF linker hash table. */
4416
38b1a46c
NC
4417struct mips_elf_link_hash_table
4418{
252b5132
RH
4419 struct elf_link_hash_table root;
4420#if 0
4421 /* We no longer use this. */
4422 /* String section indices for the dynamic section symbols. */
4423 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
4424#endif
4425 /* The number of .rtproc entries. */
4426 bfd_size_type procedure_count;
4427 /* The size of the .compact_rel section (if SGI_COMPAT). */
4428 bfd_size_type compact_rel_size;
4429 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
be3ccd9c 4430 entry is set to the address of __rld_obj_head as in Irix 5. */
252b5132
RH
4431 boolean use_rld_obj_head;
4432 /* This is the value of the __rld_map or __rld_obj_head symbol. */
4433 bfd_vma rld_value;
be3ccd9c 4434 /* This is set if we see any mips16 stub sections. */
252b5132
RH
4435 boolean mips16_stubs_seen;
4436};
4437
4438/* Look up an entry in a MIPS ELF linker hash table. */
4439
4440#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
4441 ((struct mips_elf_link_hash_entry *) \
4442 elf_link_hash_lookup (&(table)->root, (string), (create), \
4443 (copy), (follow)))
4444
4445/* Traverse a MIPS ELF linker hash table. */
4446
4447#define mips_elf_link_hash_traverse(table, func, info) \
4448 (elf_link_hash_traverse \
4449 (&(table)->root, \
4450 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
4451 (info)))
4452
4453/* Get the MIPS ELF linker hash table from a link_info structure. */
4454
4455#define mips_elf_hash_table(p) \
4456 ((struct mips_elf_link_hash_table *) ((p)->hash))
4457
4458static boolean mips_elf_output_extsym
4459 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
4460
4461/* Create an entry in a MIPS ELF linker hash table. */
4462
4463static struct bfd_hash_entry *
4464mips_elf_link_hash_newfunc (entry, table, string)
4465 struct bfd_hash_entry *entry;
4466 struct bfd_hash_table *table;
4467 const char *string;
4468{
4469 struct mips_elf_link_hash_entry *ret =
4470 (struct mips_elf_link_hash_entry *) entry;
4471
4472 /* Allocate the structure if it has not already been allocated by a
4473 subclass. */
4474 if (ret == (struct mips_elf_link_hash_entry *) NULL)
4475 ret = ((struct mips_elf_link_hash_entry *)
4476 bfd_hash_allocate (table,
4477 sizeof (struct mips_elf_link_hash_entry)));
4478 if (ret == (struct mips_elf_link_hash_entry *) NULL)
4479 return (struct bfd_hash_entry *) ret;
4480
4481 /* Call the allocation method of the superclass. */
4482 ret = ((struct mips_elf_link_hash_entry *)
4483 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
4484 table, string));
4485 if (ret != (struct mips_elf_link_hash_entry *) NULL)
4486 {
4487 /* Set local fields. */
4488 memset (&ret->esym, 0, sizeof (EXTR));
4489 /* We use -2 as a marker to indicate that the information has
4490 not been set. -1 means there is no associated ifd. */
4491 ret->esym.ifd = -2;
a3c7651d 4492 ret->possibly_dynamic_relocs = 0;
43917054 4493 ret->readonly_reloc = false;
c6142e5d 4494 ret->min_dyn_reloc_index = 0;
9117d219 4495 ret->no_fn_stub = false;
252b5132
RH
4496 ret->fn_stub = NULL;
4497 ret->need_fn_stub = false;
4498 ret->call_stub = NULL;
4499 ret->call_fp_stub = NULL;
4500 }
4501
4502 return (struct bfd_hash_entry *) ret;
4503}
4504
9e80ff3a 4505static void
e5094212 4506_bfd_mips_elf_hide_symbol (info, entry, force_local)
b305ef96 4507 struct bfd_link_info *info;
9e80ff3a 4508 struct elf_link_hash_entry *entry;
e5094212 4509 boolean force_local;
b305ef96
UC
4510{
4511 bfd *dynobj;
4512 asection *got;
4513 struct mips_got_info *g;
9e80ff3a
L
4514 struct mips_elf_link_hash_entry *h;
4515 h = (struct mips_elf_link_hash_entry *) entry;
b305ef96
UC
4516 dynobj = elf_hash_table (info)->dynobj;
4517 got = bfd_get_section_by_name (dynobj, ".got");
4518 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4519
e5094212 4520 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
b305ef96
UC
4521
4522 /* FIXME: Do we allocate too much GOT space here? */
4523 g->local_gotno++;
4524 got->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
4525}
4526
252b5132
RH
4527/* Create a MIPS ELF linker hash table. */
4528
103186c6
MM
4529struct bfd_link_hash_table *
4530_bfd_mips_elf_link_hash_table_create (abfd)
252b5132
RH
4531 bfd *abfd;
4532{
4533 struct mips_elf_link_hash_table *ret;
dc810e39 4534 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
252b5132 4535
dc810e39 4536 ret = (struct mips_elf_link_hash_table *) bfd_alloc (abfd, amt);
252b5132
RH
4537 if (ret == (struct mips_elf_link_hash_table *) NULL)
4538 return NULL;
4539
4540 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
4541 mips_elf_link_hash_newfunc))
4542 {
4543 bfd_release (abfd, ret);
4544 return NULL;
4545 }
4546
4547#if 0
4548 /* We no longer use this. */
4549 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
4550 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
4551#endif
4552 ret->procedure_count = 0;
4553 ret->compact_rel_size = 0;
4554 ret->use_rld_obj_head = false;
4555 ret->rld_value = 0;
4556 ret->mips16_stubs_seen = false;
4557
4558 return &ret->root.root;
4559}
4560
4561/* Hook called by the linker routine which adds symbols from an object
4562 file. We must handle the special MIPS section numbers here. */
4563
103186c6
MM
4564boolean
4565_bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
252b5132
RH
4566 bfd *abfd;
4567 struct bfd_link_info *info;
4568 const Elf_Internal_Sym *sym;
4569 const char **namep;
5f771d47 4570 flagword *flagsp ATTRIBUTE_UNUSED;
252b5132
RH
4571 asection **secp;
4572 bfd_vma *valp;
4573{
4574 if (SGI_COMPAT (abfd)
4575 && (abfd->flags & DYNAMIC) != 0
4576 && strcmp (*namep, "_rld_new_interface") == 0)
4577 {
4578 /* Skip Irix 5 rld entry name. */
4579 *namep = NULL;
4580 return true;
4581 }
4582
4583 switch (sym->st_shndx)
4584 {
4585 case SHN_COMMON:
4586 /* Common symbols less than the GP size are automatically
4587 treated as SHN_MIPS_SCOMMON symbols. */
7403cb63
MM
4588 if (sym->st_size > elf_gp_size (abfd)
4589 || IRIX_COMPAT (abfd) == ict_irix6)
252b5132
RH
4590 break;
4591 /* Fall through. */
4592 case SHN_MIPS_SCOMMON:
4593 *secp = bfd_make_section_old_way (abfd, ".scommon");
4594 (*secp)->flags |= SEC_IS_COMMON;
4595 *valp = sym->st_size;
4596 break;
4597
4598 case SHN_MIPS_TEXT:
4599 /* This section is used in a shared object. */
b305ef96 4600 if (elf_tdata (abfd)->elf_text_section == NULL)
252b5132 4601 {
b305ef96
UC
4602 asymbol *elf_text_symbol;
4603 asection *elf_text_section;
dc810e39 4604 bfd_size_type amt = sizeof (asection);
b305ef96 4605
dc810e39 4606 elf_text_section = bfd_zalloc (abfd, amt);
b305ef96
UC
4607 if (elf_text_section == NULL)
4608 return false;
4609
dc810e39
AM
4610 amt = sizeof (asymbol);
4611 elf_text_symbol = bfd_zalloc (abfd, amt);
b305ef96
UC
4612 if (elf_text_symbol == NULL)
4613 return false;
4614
252b5132 4615 /* Initialize the section. */
b305ef96
UC
4616
4617 elf_tdata (abfd)->elf_text_section = elf_text_section;
4618 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
4619
4620 elf_text_section->symbol = elf_text_symbol;
4621 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
4622
4623 elf_text_section->name = ".text";
4624 elf_text_section->flags = SEC_NO_FLAGS;
4625 elf_text_section->output_section = NULL;
4626 elf_text_section->owner = abfd;
4627 elf_text_symbol->name = ".text";
4628 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
4629 elf_text_symbol->section = elf_text_section;
252b5132
RH
4630 }
4631 /* This code used to do *secp = bfd_und_section_ptr if
4632 info->shared. I don't know why, and that doesn't make sense,
4633 so I took it out. */
b305ef96 4634 *secp = elf_tdata (abfd)->elf_text_section;
252b5132
RH
4635 break;
4636
4637 case SHN_MIPS_ACOMMON:
4638 /* Fall through. XXX Can we treat this as allocated data? */
4639 case SHN_MIPS_DATA:
4640 /* This section is used in a shared object. */
b305ef96 4641 if (elf_tdata (abfd)->elf_data_section == NULL)
252b5132 4642 {
b305ef96
UC
4643 asymbol *elf_data_symbol;
4644 asection *elf_data_section;
dc810e39 4645 bfd_size_type amt = sizeof (asection);
b305ef96 4646
dc810e39 4647 elf_data_section = bfd_zalloc (abfd, amt);
b305ef96
UC
4648 if (elf_data_section == NULL)
4649 return false;
4650
dc810e39
AM
4651 amt = sizeof (asymbol);
4652 elf_data_symbol = bfd_zalloc (abfd, amt);
b305ef96
UC
4653 if (elf_data_symbol == NULL)
4654 return false;
4655
252b5132 4656 /* Initialize the section. */
b305ef96
UC
4657
4658 elf_tdata (abfd)->elf_data_section = elf_data_section;
4659 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
4660
4661 elf_data_section->symbol = elf_data_symbol;
4662 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
4663
4664 elf_data_section->name = ".data";
4665 elf_data_section->flags = SEC_NO_FLAGS;
4666 elf_data_section->output_section = NULL;
4667 elf_data_section->owner = abfd;
4668 elf_data_symbol->name = ".data";
4669 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
4670 elf_data_symbol->section = elf_data_section;
252b5132
RH
4671 }
4672 /* This code used to do *secp = bfd_und_section_ptr if
4673 info->shared. I don't know why, and that doesn't make sense,
4674 so I took it out. */
b305ef96 4675 *secp = elf_tdata (abfd)->elf_data_section;
252b5132
RH
4676 break;
4677
4678 case SHN_MIPS_SUNDEFINED:
4679 *secp = bfd_und_section_ptr;
4680 break;
4681 }
4682
4683 if (SGI_COMPAT (abfd)
4684 && ! info->shared
4685 && info->hash->creator == abfd->xvec
4686 && strcmp (*namep, "__rld_obj_head") == 0)
4687 {
4688 struct elf_link_hash_entry *h;
4689
4690 /* Mark __rld_obj_head as dynamic. */
4691 h = NULL;
4692 if (! (_bfd_generic_link_add_one_symbol
4693 (info, abfd, *namep, BSF_GLOBAL, *secp,
4694 (bfd_vma) *valp, (const char *) NULL, false,
4695 get_elf_backend_data (abfd)->collect,
4696 (struct bfd_link_hash_entry **) &h)))
4697 return false;
be3ccd9c 4698 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
4699 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4700 h->type = STT_OBJECT;
4701
4702 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4703 return false;
4704
4705 mips_elf_hash_table (info)->use_rld_obj_head = true;
4706 }
4707
4708 /* If this is a mips16 text symbol, add 1 to the value to make it
4709 odd. This will cause something like .word SYM to come up with
4710 the right value when it is loaded into the PC. */
4711 if (sym->st_other == STO_MIPS16)
4712 ++*valp;
4713
4714 return true;
4715}
4716
4717/* Structure used to pass information to mips_elf_output_extsym. */
4718
38b1a46c
NC
4719struct extsym_info
4720{
252b5132
RH
4721 bfd *abfd;
4722 struct bfd_link_info *info;
4723 struct ecoff_debug_info *debug;
4724 const struct ecoff_debug_swap *swap;
4725 boolean failed;
4726};
4727
4728/* This routine is used to write out ECOFF debugging external symbol
4729 information. It is called via mips_elf_link_hash_traverse. The
4730 ECOFF external symbol information must match the ELF external
4731 symbol information. Unfortunately, at this point we don't know
4732 whether a symbol is required by reloc information, so the two
4733 tables may wind up being different. We must sort out the external
4734 symbol information before we can set the final size of the .mdebug
4735 section, and we must set the size of the .mdebug section before we
4736 can relocate any sections, and we can't know which symbols are
4737 required by relocation until we relocate the sections.
4738 Fortunately, it is relatively unlikely that any symbol will be
4739 stripped but required by a reloc. In particular, it can not happen
4740 when generating a final executable. */
4741
4742static boolean
4743mips_elf_output_extsym (h, data)
4744 struct mips_elf_link_hash_entry *h;
4745 PTR data;
4746{
4747 struct extsym_info *einfo = (struct extsym_info *) data;
4748 boolean strip;
4749 asection *sec, *output_section;
4750
4751 if (h->root.indx == -2)
4752 strip = false;
4753 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4754 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4755 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4756 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4757 strip = true;
4758 else if (einfo->info->strip == strip_all
4759 || (einfo->info->strip == strip_some
4760 && bfd_hash_lookup (einfo->info->keep_hash,
4761 h->root.root.root.string,
4762 false, false) == NULL))
4763 strip = true;
4764 else
4765 strip = false;
4766
4767 if (strip)
4768 return true;
4769
4770 if (h->esym.ifd == -2)
4771 {
4772 h->esym.jmptbl = 0;
4773 h->esym.cobol_main = 0;
4774 h->esym.weakext = 0;
4775 h->esym.reserved = 0;
4776 h->esym.ifd = ifdNil;
4777 h->esym.asym.value = 0;
4778 h->esym.asym.st = stGlobal;
4779
f7cb7d68 4780 if (h->root.root.type == bfd_link_hash_undefined
be3ccd9c 4781 || h->root.root.type == bfd_link_hash_undefweak)
252b5132
RH
4782 {
4783 const char *name;
4784
4785 /* Use undefined class. Also, set class and type for some
4786 special symbols. */
4787 name = h->root.root.root.string;
4788 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4789 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4790 {
4791 h->esym.asym.sc = scData;
4792 h->esym.asym.st = stLabel;
4793 h->esym.asym.value = 0;
4794 }
4795 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4796 {
4797 h->esym.asym.sc = scAbs;
4798 h->esym.asym.st = stLabel;
4799 h->esym.asym.value =
4800 mips_elf_hash_table (einfo->info)->procedure_count;
4801 }
4802 else if (strcmp (name, "_gp_disp") == 0)
4803 {
4804 h->esym.asym.sc = scAbs;
4805 h->esym.asym.st = stLabel;
4806 h->esym.asym.value = elf_gp (einfo->abfd);
4807 }
4808 else
4809 h->esym.asym.sc = scUndefined;
4810 }
4811 else if (h->root.root.type != bfd_link_hash_defined
4812 && h->root.root.type != bfd_link_hash_defweak)
4813 h->esym.asym.sc = scAbs;
4814 else
4815 {
4816 const char *name;
4817
4818 sec = h->root.root.u.def.section;
4819 output_section = sec->output_section;
4820
4821 /* When making a shared library and symbol h is the one from
4822 the another shared library, OUTPUT_SECTION may be null. */
4823 if (output_section == NULL)
4824 h->esym.asym.sc = scUndefined;
4825 else
4826 {
4827 name = bfd_section_name (output_section->owner, output_section);
4828
4829 if (strcmp (name, ".text") == 0)
4830 h->esym.asym.sc = scText;
4831 else if (strcmp (name, ".data") == 0)
4832 h->esym.asym.sc = scData;
4833 else if (strcmp (name, ".sdata") == 0)
4834 h->esym.asym.sc = scSData;
4835 else if (strcmp (name, ".rodata") == 0
4836 || strcmp (name, ".rdata") == 0)
4837 h->esym.asym.sc = scRData;
4838 else if (strcmp (name, ".bss") == 0)
4839 h->esym.asym.sc = scBss;
4840 else if (strcmp (name, ".sbss") == 0)
4841 h->esym.asym.sc = scSBss;
4842 else if (strcmp (name, ".init") == 0)
4843 h->esym.asym.sc = scInit;
4844 else if (strcmp (name, ".fini") == 0)
4845 h->esym.asym.sc = scFini;
4846 else
4847 h->esym.asym.sc = scAbs;
4848 }
4849 }
4850
4851 h->esym.asym.reserved = 0;
4852 h->esym.asym.index = indexNil;
4853 }
4854
4855 if (h->root.root.type == bfd_link_hash_common)
4856 h->esym.asym.value = h->root.root.u.c.size;
4857 else if (h->root.root.type == bfd_link_hash_defined
4858 || h->root.root.type == bfd_link_hash_defweak)
4859 {
4860 if (h->esym.asym.sc == scCommon)
4861 h->esym.asym.sc = scBss;
4862 else if (h->esym.asym.sc == scSCommon)
4863 h->esym.asym.sc = scSBss;
4864
4865 sec = h->root.root.u.def.section;
4866 output_section = sec->output_section;
4867 if (output_section != NULL)
4868 h->esym.asym.value = (h->root.root.u.def.value
4869 + sec->output_offset
4870 + output_section->vma);
4871 else
4872 h->esym.asym.value = 0;
4873 }
4874 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4875 {
9117d219
NC
4876 struct mips_elf_link_hash_entry *hd = h;
4877 boolean no_fn_stub = h->no_fn_stub;
4878
4879 while (hd->root.root.type == bfd_link_hash_indirect)
252b5132 4880 {
9117d219
NC
4881 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
4882 no_fn_stub = no_fn_stub || hd->no_fn_stub;
252b5132 4883 }
9117d219
NC
4884
4885 if (!no_fn_stub)
4886 {
4887 /* Set type and value for a symbol with a function stub. */
4888 h->esym.asym.st = stProc;
4889 sec = hd->root.root.u.def.section;
4890 if (sec == NULL)
4891 h->esym.asym.value = 0;
4892 else
4893 {
4894 output_section = sec->output_section;
4895 if (output_section != NULL)
4896 h->esym.asym.value = (hd->root.plt.offset
4897 + sec->output_offset
4898 + output_section->vma);
4899 else
4900 h->esym.asym.value = 0;
4901 }
252b5132 4902#if 0 /* FIXME? */
9117d219 4903 h->esym.ifd = 0;
252b5132 4904#endif
9117d219 4905 }
252b5132
RH
4906 }
4907
4908 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4909 h->root.root.root.string,
4910 &h->esym))
4911 {
4912 einfo->failed = true;
4913 return false;
4914 }
4915
4916 return true;
4917}
4918
4919/* Create a runtime procedure table from the .mdebug section. */
4920
4921static boolean
4922mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4923 PTR handle;
4924 bfd *abfd;
4925 struct bfd_link_info *info;
4926 asection *s;
4927 struct ecoff_debug_info *debug;
4928{
4929 const struct ecoff_debug_swap *swap;
4930 HDRR *hdr = &debug->symbolic_header;
4931 RPDR *rpdr, *rp;
4932 struct rpdr_ext *erp;
4933 PTR rtproc;
4934 struct pdr_ext *epdr;
4935 struct sym_ext *esym;
4936 char *ss, **sv;
4937 char *str;
dc810e39
AM
4938 bfd_size_type size;
4939 bfd_size_type count;
252b5132
RH
4940 unsigned long sindex;
4941 unsigned long i;
4942 PDR pdr;
4943 SYMR sym;
4944 const char *no_name_func = _("static procedure (no name)");
4945
4946 epdr = NULL;
4947 rpdr = NULL;
4948 esym = NULL;
4949 ss = NULL;
4950 sv = NULL;
4951
4952 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4953
4954 sindex = strlen (no_name_func) + 1;
4955 count = hdr->ipdMax;
4956 if (count > 0)
4957 {
4958 size = swap->external_pdr_size;
4959
4960 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4961 if (epdr == NULL)
4962 goto error_return;
4963
4964 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4965 goto error_return;
4966
4967 size = sizeof (RPDR);
4968 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4969 if (rpdr == NULL)
4970 goto error_return;
4971
dc810e39
AM
4972 size = sizeof (char *);
4973 sv = (char **) bfd_malloc (size * count);
252b5132
RH
4974 if (sv == NULL)
4975 goto error_return;
4976
4977 count = hdr->isymMax;
4978 size = swap->external_sym_size;
4979 esym = (struct sym_ext *) bfd_malloc (size * count);
4980 if (esym == NULL)
4981 goto error_return;
4982
4983 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4984 goto error_return;
4985
4986 count = hdr->issMax;
4987 ss = (char *) bfd_malloc (count);
4988 if (ss == NULL)
4989 goto error_return;
4990 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4991 goto error_return;
4992
4993 count = hdr->ipdMax;
dc810e39 4994 for (i = 0; i < (unsigned long) count; i++, rp++)
252b5132
RH
4995 {
4996 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4997 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4998 rp->adr = sym.value;
4999 rp->regmask = pdr.regmask;
5000 rp->regoffset = pdr.regoffset;
5001 rp->fregmask = pdr.fregmask;
5002 rp->fregoffset = pdr.fregoffset;
5003 rp->frameoffset = pdr.frameoffset;
5004 rp->framereg = pdr.framereg;
5005 rp->pcreg = pdr.pcreg;
5006 rp->irpss = sindex;
5007 sv[i] = ss + sym.iss;
5008 sindex += strlen (sv[i]) + 1;
5009 }
5010 }
5011
5012 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
5013 size = BFD_ALIGN (size, 16);
5014 rtproc = (PTR) bfd_alloc (abfd, size);
5015 if (rtproc == NULL)
5016 {
5017 mips_elf_hash_table (info)->procedure_count = 0;
5018 goto error_return;
5019 }
5020
5021 mips_elf_hash_table (info)->procedure_count = count + 2;
5022
5023 erp = (struct rpdr_ext *) rtproc;
5024 memset (erp, 0, sizeof (struct rpdr_ext));
5025 erp++;
5026 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
5027 strcpy (str, no_name_func);
5028 str += strlen (no_name_func) + 1;
5029 for (i = 0; i < count; i++)
5030 {
5031 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
5032 strcpy (str, sv[i]);
5033 str += strlen (sv[i]) + 1;
5034 }
dc810e39 5035 ECOFF_PUT_OFF (abfd, -1, (erp + count)->p_adr);
252b5132
RH
5036
5037 /* Set the size and contents of .rtproc section. */
5038 s->_raw_size = size;
5039 s->contents = (bfd_byte *) rtproc;
5040
5041 /* Skip this section later on (I don't think this currently
5042 matters, but someday it might). */
5043 s->link_order_head = (struct bfd_link_order *) NULL;
5044
5045 if (epdr != NULL)
5046 free (epdr);
5047 if (rpdr != NULL)
5048 free (rpdr);
5049 if (esym != NULL)
5050 free (esym);
5051 if (ss != NULL)
5052 free (ss);
5053 if (sv != NULL)
5054 free (sv);
5055
5056 return true;
5057
5058 error_return:
5059 if (epdr != NULL)
5060 free (epdr);
5061 if (rpdr != NULL)
5062 free (rpdr);
5063 if (esym != NULL)
5064 free (esym);
5065 if (ss != NULL)
5066 free (ss);
5067 if (sv != NULL)
5068 free (sv);
5069 return false;
5070}
5071
5072/* A comparison routine used to sort .gptab entries. */
5073
5074static int
5075gptab_compare (p1, p2)
5076 const PTR p1;
5077 const PTR p2;
5078{
5079 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
5080 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
5081
5082 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
5083}
5084
5085/* We need to use a special link routine to handle the .reginfo and
5086 the .mdebug sections. We need to merge all instances of these
5087 sections together, not write them all out sequentially. */
5088
103186c6
MM
5089boolean
5090_bfd_mips_elf_final_link (abfd, info)
252b5132
RH
5091 bfd *abfd;
5092 struct bfd_link_info *info;
5093{
5094 asection **secpp;
5095 asection *o;
5096 struct bfd_link_order *p;
5097 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
5098 asection *rtproc_sec;
5099 Elf32_RegInfo reginfo;
5100 struct ecoff_debug_info debug;
5101 const struct ecoff_debug_swap *swap
5102 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
5103 HDRR *symhdr = &debug.symbolic_header;
5104 PTR mdebug_handle = NULL;
f7cb7d68
UC
5105 asection *s;
5106 EXTR esym;
f7cb7d68 5107 unsigned int i;
dc810e39
AM
5108 bfd_size_type amt;
5109
5110 static const char * const secname[] =
38b1a46c 5111 {
be3ccd9c
KH
5112 ".text", ".init", ".fini", ".data",
5113 ".rodata", ".sdata", ".sbss", ".bss"
5114 };
38b1a46c
NC
5115 static const int sc[] =
5116 {
be3ccd9c
KH
5117 scText, scInit, scFini, scData,
5118 scRData, scSData, scSBss, scBss
5119 };
252b5132 5120
303f629d
MM
5121 /* If all the things we linked together were PIC, but we're
5122 producing an executable (rather than a shared object), then the
5123 resulting file is CPIC (i.e., it calls PIC code.) */
0dda5f7a
ILT
5124 if (!info->shared
5125 && !info->relocateable
5126 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
252b5132 5127 {
303f629d
MM
5128 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
5129 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
252b5132
RH
5130 }
5131
b3be9b46
RH
5132 /* We'd carefully arranged the dynamic symbol indices, and then the
5133 generic size_dynamic_sections renumbered them out from under us.
5134 Rather than trying somehow to prevent the renumbering, just do
5135 the sort again. */
441d6d79 5136 if (elf_hash_table (info)->dynamic_sections_created)
b3be9b46
RH
5137 {
5138 bfd *dynobj;
5139 asection *got;
5140 struct mips_got_info *g;
5141
435394bf
MM
5142 /* When we resort, we must tell mips_elf_sort_hash_table what
5143 the lowest index it may use is. That's the number of section
5144 symbols we're going to add. The generic ELF linker only
5145 adds these symbols when building a shared object. Note that
5146 we count the sections after (possibly) removing the .options
5147 section above. */
be3ccd9c 5148 if (!mips_elf_sort_hash_table (info, (info->shared
435394bf
MM
5149 ? bfd_count_sections (abfd) + 1
5150 : 1)))
be3ccd9c 5151 return false;
b3be9b46
RH
5152
5153 /* Make sure we didn't grow the global .got region. */
5154 dynobj = elf_hash_table (info)->dynobj;
5155 got = bfd_get_section_by_name (dynobj, ".got");
5156 g = (struct mips_got_info *) elf_section_data (got)->tdata;
5157
8b237a89
MM
5158 if (g->global_gotsym != NULL)
5159 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
5160 - g->global_gotsym->dynindx)
5161 <= g->global_gotno);
b3be9b46
RH
5162 }
5163
303f629d
MM
5164 /* On IRIX5, we omit the .options section. On IRIX6, however, we
5165 include it, even though we don't process it quite right. (Some
5166 entries are supposed to be merged.) Empirically, we seem to be
5167 better off including it then not. */
f7cb7d68 5168 if (IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
303f629d
MM
5169 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
5170 {
5171 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
5172 {
5173 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
5174 if (p->type == bfd_indirect_link_order)
be3ccd9c 5175 p->u.indirect.section->flags &= ~SEC_HAS_CONTENTS;
303f629d 5176 (*secpp)->link_order_head = NULL;
9e7b37b3 5177 bfd_section_list_remove (abfd, secpp);
303f629d 5178 --abfd->section_count;
be3ccd9c 5179
303f629d
MM
5180 break;
5181 }
5182 }
5183
252b5132
RH
5184 /* Get a value for the GP register. */
5185 if (elf_gp (abfd) == 0)
5186 {
5187 struct bfd_link_hash_entry *h;
5188
5189 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
5190 if (h != (struct bfd_link_hash_entry *) NULL
5191 && h->type == bfd_link_hash_defined)
5192 elf_gp (abfd) = (h->u.def.value
5193 + h->u.def.section->output_section->vma
5194 + h->u.def.section->output_offset);
0db63c18
MM
5195 else if (info->relocateable)
5196 {
5197 bfd_vma lo;
5198
5199 /* Find the GP-relative section with the lowest offset. */
5200 lo = (bfd_vma) -1;
5201 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
be3ccd9c 5202 if (o->vma < lo
0db63c18
MM
5203 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
5204 lo = o->vma;
5205
5206 /* And calculate GP relative to that. */
5207 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
5208 }
252b5132
RH
5209 else
5210 {
5211 /* If the relocate_section function needs to do a reloc
5212 involving the GP value, it should make a reloc_dangerous
5213 callback to warn that GP is not defined. */
5214 }
5215 }
5216
5217 /* Go through the sections and collect the .reginfo and .mdebug
5218 information. */
5219 reginfo_sec = NULL;
5220 mdebug_sec = NULL;
5221 gptab_data_sec = NULL;
5222 gptab_bss_sec = NULL;
5223 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5224 {
5225 if (strcmp (o->name, ".reginfo") == 0)
5226 {
5227 memset (&reginfo, 0, sizeof reginfo);
5228
5229 /* We have found the .reginfo section in the output file.
5230 Look through all the link_orders comprising it and merge
5231 the information together. */
5232 for (p = o->link_order_head;
5233 p != (struct bfd_link_order *) NULL;
5234 p = p->next)
5235 {
5236 asection *input_section;
5237 bfd *input_bfd;
5238 Elf32_External_RegInfo ext;
5239 Elf32_RegInfo sub;
5240
5241 if (p->type != bfd_indirect_link_order)
5242 {
5243 if (p->type == bfd_fill_link_order)
5244 continue;
5245 abort ();
5246 }
5247
5248 input_section = p->u.indirect.section;
5249 input_bfd = input_section->owner;
5250
5251 /* The linker emulation code has probably clobbered the
5252 size to be zero bytes. */
5253 if (input_section->_raw_size == 0)
5254 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
5255
5256 if (! bfd_get_section_contents (input_bfd, input_section,
5257 (PTR) &ext,
5258 (file_ptr) 0,
dc810e39 5259 (bfd_size_type) sizeof ext))
252b5132
RH
5260 return false;
5261
5262 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
5263
5264 reginfo.ri_gprmask |= sub.ri_gprmask;
5265 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
5266 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
5267 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
5268 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
5269
5270 /* ri_gp_value is set by the function
5271 mips_elf32_section_processing when the section is
5272 finally written out. */
5273
5274 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5275 elf_link_input_bfd ignores this section. */
be3ccd9c 5276 input_section->flags &= ~SEC_HAS_CONTENTS;
252b5132
RH
5277 }
5278
5279 /* Size has been set in mips_elf_always_size_sections */
5280 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
5281
5282 /* Skip this section later on (I don't think this currently
5283 matters, but someday it might). */
5284 o->link_order_head = (struct bfd_link_order *) NULL;
5285
5286 reginfo_sec = o;
5287 }
5288
5289 if (strcmp (o->name, ".mdebug") == 0)
5290 {
5291 struct extsym_info einfo;
dc810e39 5292 bfd_vma last;
252b5132
RH
5293
5294 /* We have found the .mdebug section in the output file.
5295 Look through all the link_orders comprising it and merge
5296 the information together. */
5297 symhdr->magic = swap->sym_magic;
5298 /* FIXME: What should the version stamp be? */
5299 symhdr->vstamp = 0;
5300 symhdr->ilineMax = 0;
5301 symhdr->cbLine = 0;
5302 symhdr->idnMax = 0;
5303 symhdr->ipdMax = 0;
5304 symhdr->isymMax = 0;
5305 symhdr->ioptMax = 0;
5306 symhdr->iauxMax = 0;
5307 symhdr->issMax = 0;
5308 symhdr->issExtMax = 0;
5309 symhdr->ifdMax = 0;
5310 symhdr->crfd = 0;
5311 symhdr->iextMax = 0;
5312
5313 /* We accumulate the debugging information itself in the
5314 debug_info structure. */
5315 debug.line = NULL;
5316 debug.external_dnr = NULL;
5317 debug.external_pdr = NULL;
5318 debug.external_sym = NULL;
5319 debug.external_opt = NULL;
5320 debug.external_aux = NULL;
5321 debug.ss = NULL;
5322 debug.ssext = debug.ssext_end = NULL;
5323 debug.external_fdr = NULL;
5324 debug.external_rfd = NULL;
5325 debug.external_ext = debug.external_ext_end = NULL;
5326
5327 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
5328 if (mdebug_handle == (PTR) NULL)
5329 return false;
5330
be3ccd9c
KH
5331 esym.jmptbl = 0;
5332 esym.cobol_main = 0;
5333 esym.weakext = 0;
5334 esym.reserved = 0;
5335 esym.ifd = ifdNil;
5336 esym.asym.iss = issNil;
5337 esym.asym.st = stLocal;
5338 esym.asym.reserved = 0;
5339 esym.asym.index = indexNil;
5340 last = 0;
dc810e39 5341 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
be3ccd9c
KH
5342 {
5343 esym.asym.sc = sc[i];
dc810e39 5344 s = bfd_get_section_by_name (abfd, secname[i]);
be3ccd9c
KH
5345 if (s != NULL)
5346 {
5347 esym.asym.value = s->vma;
5348 last = s->vma + s->_raw_size;
5349 }
5350 else
5351 esym.asym.value = last;
5352 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
dc810e39 5353 secname[i], &esym))
be3ccd9c
KH
5354 return false;
5355 }
252b5132
RH
5356
5357 for (p = o->link_order_head;
5358 p != (struct bfd_link_order *) NULL;
5359 p = p->next)
5360 {
5361 asection *input_section;
5362 bfd *input_bfd;
5363 const struct ecoff_debug_swap *input_swap;
5364 struct ecoff_debug_info input_debug;
5365 char *eraw_src;
5366 char *eraw_end;
5367
5368 if (p->type != bfd_indirect_link_order)
5369 {
5370 if (p->type == bfd_fill_link_order)
5371 continue;
5372 abort ();
5373 }
5374
5375 input_section = p->u.indirect.section;
5376 input_bfd = input_section->owner;
5377
5378 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
5379 || (get_elf_backend_data (input_bfd)
5380 ->elf_backend_ecoff_debug_swap) == NULL)
5381 {
5382 /* I don't know what a non MIPS ELF bfd would be
5383 doing with a .mdebug section, but I don't really
5384 want to deal with it. */
5385 continue;
5386 }
5387
5388 input_swap = (get_elf_backend_data (input_bfd)
5389 ->elf_backend_ecoff_debug_swap);
5390
5391 BFD_ASSERT (p->size == input_section->_raw_size);
5392
5393 /* The ECOFF linking code expects that we have already
5394 read in the debugging information and set up an
5395 ecoff_debug_info structure, so we do that now. */
5396 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
5397 &input_debug))
5398 return false;
5399
5400 if (! (bfd_ecoff_debug_accumulate
5401 (mdebug_handle, abfd, &debug, swap, input_bfd,
5402 &input_debug, input_swap, info)))
5403 return false;
5404
5405 /* Loop through the external symbols. For each one with
5406 interesting information, try to find the symbol in
5407 the linker global hash table and save the information
5408 for the output external symbols. */
5409 eraw_src = input_debug.external_ext;
5410 eraw_end = (eraw_src
5411 + (input_debug.symbolic_header.iextMax
5412 * input_swap->external_ext_size));
5413 for (;
5414 eraw_src < eraw_end;
5415 eraw_src += input_swap->external_ext_size)
5416 {
5417 EXTR ext;
5418 const char *name;
5419 struct mips_elf_link_hash_entry *h;
5420
5421 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
5422 if (ext.asym.sc == scNil
5423 || ext.asym.sc == scUndefined
5424 || ext.asym.sc == scSUndefined)
5425 continue;
5426
5427 name = input_debug.ssext + ext.asym.iss;
5428 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
5429 name, false, false, true);
5430 if (h == NULL || h->esym.ifd != -2)
5431 continue;
5432
5433 if (ext.ifd != -1)
5434 {
5435 BFD_ASSERT (ext.ifd
5436 < input_debug.symbolic_header.ifdMax);
5437 ext.ifd = input_debug.ifdmap[ext.ifd];
5438 }
5439
5440 h->esym = ext;
5441 }
5442
5443 /* Free up the information we just read. */
5444 free (input_debug.line);
5445 free (input_debug.external_dnr);
5446 free (input_debug.external_pdr);
5447 free (input_debug.external_sym);
5448 free (input_debug.external_opt);
5449 free (input_debug.external_aux);
5450 free (input_debug.ss);
5451 free (input_debug.ssext);
5452 free (input_debug.external_fdr);
5453 free (input_debug.external_rfd);
5454 free (input_debug.external_ext);
5455
5456 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5457 elf_link_input_bfd ignores this section. */
be3ccd9c 5458 input_section->flags &= ~SEC_HAS_CONTENTS;
252b5132
RH
5459 }
5460
5461 if (SGI_COMPAT (abfd) && info->shared)
5462 {
5463 /* Create .rtproc section. */
5464 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
5465 if (rtproc_sec == NULL)
5466 {
5467 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
5468 | SEC_LINKER_CREATED | SEC_READONLY);
5469
5470 rtproc_sec = bfd_make_section (abfd, ".rtproc");
5471 if (rtproc_sec == NULL
5472 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
5473 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
5474 return false;
5475 }
5476
5477 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
5478 info, rtproc_sec, &debug))
5479 return false;
5480 }
5481
5482 /* Build the external symbol information. */
5483 einfo.abfd = abfd;
5484 einfo.info = info;
5485 einfo.debug = &debug;
5486 einfo.swap = swap;
5487 einfo.failed = false;
5488 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
5489 mips_elf_output_extsym,
5490 (PTR) &einfo);
5491 if (einfo.failed)
5492 return false;
5493
5494 /* Set the size of the .mdebug section. */
5495 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
5496
5497 /* Skip this section later on (I don't think this currently
5498 matters, but someday it might). */
5499 o->link_order_head = (struct bfd_link_order *) NULL;
5500
5501 mdebug_sec = o;
5502 }
5503
5504 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
5505 {
5506 const char *subname;
5507 unsigned int c;
5508 Elf32_gptab *tab;
5509 Elf32_External_gptab *ext_tab;
dc810e39 5510 unsigned int j;
252b5132
RH
5511
5512 /* The .gptab.sdata and .gptab.sbss sections hold
5513 information describing how the small data area would
5514 change depending upon the -G switch. These sections
5515 not used in executables files. */
5516 if (! info->relocateable)
5517 {
252b5132
RH
5518 for (p = o->link_order_head;
5519 p != (struct bfd_link_order *) NULL;
5520 p = p->next)
5521 {
5522 asection *input_section;
5523
5524 if (p->type != bfd_indirect_link_order)
5525 {
5526 if (p->type == bfd_fill_link_order)
5527 continue;
5528 abort ();
5529 }
5530
5531 input_section = p->u.indirect.section;
5532
5533 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5534 elf_link_input_bfd ignores this section. */
be3ccd9c 5535 input_section->flags &= ~SEC_HAS_CONTENTS;
252b5132
RH
5536 }
5537
5538 /* Skip this section later on (I don't think this
5539 currently matters, but someday it might). */
5540 o->link_order_head = (struct bfd_link_order *) NULL;
5541
5542 /* Really remove the section. */
5543 for (secpp = &abfd->sections;
5544 *secpp != o;
5545 secpp = &(*secpp)->next)
5546 ;
9e7b37b3 5547 bfd_section_list_remove (abfd, secpp);
252b5132
RH
5548 --abfd->section_count;
5549
5550 continue;
5551 }
5552
5553 /* There is one gptab for initialized data, and one for
5554 uninitialized data. */
5555 if (strcmp (o->name, ".gptab.sdata") == 0)
5556 gptab_data_sec = o;
5557 else if (strcmp (o->name, ".gptab.sbss") == 0)
5558 gptab_bss_sec = o;
5559 else
5560 {
5561 (*_bfd_error_handler)
5562 (_("%s: illegal section name `%s'"),
5563 bfd_get_filename (abfd), o->name);
5564 bfd_set_error (bfd_error_nonrepresentable_section);
5565 return false;
5566 }
5567
5568 /* The linker script always combines .gptab.data and
5569 .gptab.sdata into .gptab.sdata, and likewise for
5570 .gptab.bss and .gptab.sbss. It is possible that there is
5571 no .sdata or .sbss section in the output file, in which
5572 case we must change the name of the output section. */
5573 subname = o->name + sizeof ".gptab" - 1;
5574 if (bfd_get_section_by_name (abfd, subname) == NULL)
5575 {
5576 if (o == gptab_data_sec)
5577 o->name = ".gptab.data";
5578 else
5579 o->name = ".gptab.bss";
5580 subname = o->name + sizeof ".gptab" - 1;
5581 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
5582 }
5583
5584 /* Set up the first entry. */
5585 c = 1;
dc810e39
AM
5586 amt = c * sizeof (Elf32_gptab);
5587 tab = (Elf32_gptab *) bfd_malloc (amt);
252b5132
RH
5588 if (tab == NULL)
5589 return false;
5590 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
5591 tab[0].gt_header.gt_unused = 0;
5592
5593 /* Combine the input sections. */
5594 for (p = o->link_order_head;
5595 p != (struct bfd_link_order *) NULL;
5596 p = p->next)
5597 {
5598 asection *input_section;
5599 bfd *input_bfd;
5600 bfd_size_type size;
5601 unsigned long last;
5602 bfd_size_type gpentry;
5603
5604 if (p->type != bfd_indirect_link_order)
5605 {
5606 if (p->type == bfd_fill_link_order)
5607 continue;
5608 abort ();
5609 }
5610
5611 input_section = p->u.indirect.section;
5612 input_bfd = input_section->owner;
5613
5614 /* Combine the gptab entries for this input section one
5615 by one. We know that the input gptab entries are
5616 sorted by ascending -G value. */
5617 size = bfd_section_size (input_bfd, input_section);
5618 last = 0;
5619 for (gpentry = sizeof (Elf32_External_gptab);
5620 gpentry < size;
5621 gpentry += sizeof (Elf32_External_gptab))
5622 {
5623 Elf32_External_gptab ext_gptab;
5624 Elf32_gptab int_gptab;
5625 unsigned long val;
5626 unsigned long add;
5627 boolean exact;
5628 unsigned int look;
5629
5630 if (! (bfd_get_section_contents
5631 (input_bfd, input_section, (PTR) &ext_gptab,
dc810e39
AM
5632 (file_ptr) gpentry,
5633 (bfd_size_type) sizeof (Elf32_External_gptab))))
252b5132
RH
5634 {
5635 free (tab);
5636 return false;
5637 }
5638
5639 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
5640 &int_gptab);
5641 val = int_gptab.gt_entry.gt_g_value;
5642 add = int_gptab.gt_entry.gt_bytes - last;
5643
5644 exact = false;
5645 for (look = 1; look < c; look++)
5646 {
5647 if (tab[look].gt_entry.gt_g_value >= val)
5648 tab[look].gt_entry.gt_bytes += add;
5649
5650 if (tab[look].gt_entry.gt_g_value == val)
5651 exact = true;
5652 }
5653
5654 if (! exact)
5655 {
5656 Elf32_gptab *new_tab;
5657 unsigned int max;
5658
5659 /* We need a new table entry. */
dc810e39
AM
5660 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
5661 new_tab = (Elf32_gptab *) bfd_realloc ((PTR) tab, amt);
252b5132
RH
5662 if (new_tab == NULL)
5663 {
5664 free (tab);
5665 return false;
5666 }
5667 tab = new_tab;
5668 tab[c].gt_entry.gt_g_value = val;
5669 tab[c].gt_entry.gt_bytes = add;
5670
5671 /* Merge in the size for the next smallest -G
5672 value, since that will be implied by this new
5673 value. */
5674 max = 0;
5675 for (look = 1; look < c; look++)
5676 {
5677 if (tab[look].gt_entry.gt_g_value < val
5678 && (max == 0
5679 || (tab[look].gt_entry.gt_g_value
5680 > tab[max].gt_entry.gt_g_value)))
5681 max = look;
5682 }
5683 if (max != 0)
5684 tab[c].gt_entry.gt_bytes +=
5685 tab[max].gt_entry.gt_bytes;
5686
5687 ++c;
5688 }
5689
5690 last = int_gptab.gt_entry.gt_bytes;
5691 }
5692
5693 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5694 elf_link_input_bfd ignores this section. */
be3ccd9c 5695 input_section->flags &= ~SEC_HAS_CONTENTS;
252b5132
RH
5696 }
5697
5698 /* The table must be sorted by -G value. */
5699 if (c > 2)
5700 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
5701
5702 /* Swap out the table. */
dc810e39
AM
5703 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
5704 ext_tab = (Elf32_External_gptab *) bfd_alloc (abfd, amt);
252b5132
RH
5705 if (ext_tab == NULL)
5706 {
5707 free (tab);
5708 return false;
5709 }
5710
dc810e39
AM
5711 for (j = 0; j < c; j++)
5712 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
252b5132
RH
5713 free (tab);
5714
5715 o->_raw_size = c * sizeof (Elf32_External_gptab);
5716 o->contents = (bfd_byte *) ext_tab;
5717
5718 /* Skip this section later on (I don't think this currently
5719 matters, but someday it might). */
5720 o->link_order_head = (struct bfd_link_order *) NULL;
5721 }
5722 }
5723
5724 /* Invoke the regular ELF backend linker to do all the work. */
9ebbd33e
MM
5725 if (ABI_64_P (abfd))
5726 {
5727#ifdef BFD64
5728 if (!bfd_elf64_bfd_final_link (abfd, info))
5729 return false;
5730#else
5731 abort ();
103186c6 5732 return false;
9ebbd33e
MM
5733#endif /* BFD64 */
5734 }
5735 else if (!bfd_elf32_bfd_final_link (abfd, info))
5736 return false;
252b5132
RH
5737
5738 /* Now write out the computed sections. */
5739
5740 if (reginfo_sec != (asection *) NULL)
5741 {
5742 Elf32_External_RegInfo ext;
5743
5744 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
5745 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
dc810e39 5746 (file_ptr) 0, (bfd_size_type) sizeof ext))
252b5132
RH
5747 return false;
5748 }
5749
5750 if (mdebug_sec != (asection *) NULL)
5751 {
5752 BFD_ASSERT (abfd->output_has_begun);
5753 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5754 swap, info,
5755 mdebug_sec->filepos))
5756 return false;
5757
5758 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5759 }
5760
5761 if (gptab_data_sec != (asection *) NULL)
5762 {
5763 if (! bfd_set_section_contents (abfd, gptab_data_sec,
5764 gptab_data_sec->contents,
5765 (file_ptr) 0,
5766 gptab_data_sec->_raw_size))
5767 return false;
5768 }
5769
5770 if (gptab_bss_sec != (asection *) NULL)
5771 {
5772 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
5773 gptab_bss_sec->contents,
5774 (file_ptr) 0,
5775 gptab_bss_sec->_raw_size))
5776 return false;
5777 }
5778
5779 if (SGI_COMPAT (abfd))
5780 {
5781 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
5782 if (rtproc_sec != NULL)
5783 {
5784 if (! bfd_set_section_contents (abfd, rtproc_sec,
5785 rtproc_sec->contents,
5786 (file_ptr) 0,
5787 rtproc_sec->_raw_size))
5788 return false;
5789 }
5790 }
5791
5792 return true;
5793}
5794
adb76a3e
UC
5795/* This function is called via qsort() to sort the dynamic relocation
5796 entries by increasing r_symndx value. */
5797
5798static int
be3ccd9c
KH
5799sort_dynamic_relocs (arg1, arg2)
5800 const PTR arg1;
5801 const PTR arg2;
adb76a3e
UC
5802{
5803 const Elf32_External_Rel *ext_reloc1 = (const Elf32_External_Rel *) arg1;
5804 const Elf32_External_Rel *ext_reloc2 = (const Elf32_External_Rel *) arg2;
5805
5806 Elf_Internal_Rel int_reloc1;
5807 Elf_Internal_Rel int_reloc2;
5808
be3ccd9c
KH
5809 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc1, &int_reloc1);
5810 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc2, &int_reloc2);
adb76a3e 5811
be3ccd9c 5812 return (ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info));
adb76a3e
UC
5813}
5814
7403cb63 5815/* Returns the GOT section for ABFD. */
252b5132 5816
7403cb63
MM
5817static asection *
5818mips_elf_got_section (abfd)
5819 bfd *abfd;
252b5132 5820{
7403cb63
MM
5821 return bfd_get_section_by_name (abfd, ".got");
5822}
5823
5824/* Returns the GOT information associated with the link indicated by
be3ccd9c 5825 INFO. If SGOTP is non-NULL, it is filled in with the GOT
7403cb63
MM
5826 section. */
5827
5828static struct mips_got_info *
5829mips_elf_got_info (abfd, sgotp)
5830 bfd *abfd;
5831 asection **sgotp;
5832{
5833 asection *sgot;
252b5132
RH
5834 struct mips_got_info *g;
5835
7403cb63
MM
5836 sgot = mips_elf_got_section (abfd);
5837 BFD_ASSERT (sgot != NULL);
5838 BFD_ASSERT (elf_section_data (sgot) != NULL);
5839 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5840 BFD_ASSERT (g != NULL);
252b5132 5841
7403cb63
MM
5842 if (sgotp)
5843 *sgotp = sgot;
5844 return g;
5845}
252b5132 5846
6387d602
ILT
5847/* Return whether a relocation is against a local symbol. */
5848
5849static boolean
b305ef96
UC
5850mips_elf_local_relocation_p (input_bfd, relocation, local_sections,
5851 check_forced)
6387d602
ILT
5852 bfd *input_bfd;
5853 const Elf_Internal_Rela *relocation;
5854 asection **local_sections;
b305ef96 5855 boolean check_forced;
6387d602
ILT
5856{
5857 unsigned long r_symndx;
5858 Elf_Internal_Shdr *symtab_hdr;
be3ccd9c 5859 struct mips_elf_link_hash_entry *h;
b305ef96 5860 size_t extsymoff;
6387d602
ILT
5861
5862 r_symndx = ELF32_R_SYM (relocation->r_info);
5863 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
b305ef96
UC
5864 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5865
5866 if (r_symndx < extsymoff)
5867 return true;
5868 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5869 return true;
5870
5871 if (check_forced)
6387d602 5872 {
be3ccd9c 5873 /* Look up the hash table to check whether the symbol
b305ef96 5874 was forced local. */
be3ccd9c
KH
5875 h = (struct mips_elf_link_hash_entry *)
5876 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
5877 /* Find the real hash-table entry for this symbol. */
5878 while (h->root.root.type == bfd_link_hash_indirect
b305ef96 5879 || h->root.root.type == bfd_link_hash_warning)
be3ccd9c
KH
5880 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5881 if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5882 return true;
6387d602 5883 }
b305ef96
UC
5884
5885 return false;
6387d602
ILT
5886}
5887
7403cb63 5888/* Sign-extend VALUE, which has the indicated number of BITS. */
252b5132 5889
7403cb63
MM
5890static bfd_vma
5891mips_elf_sign_extend (value, bits)
5892 bfd_vma value;
5893 int bits;
5894{
be3ccd9c 5895 if (value & ((bfd_vma) 1 << (bits - 1)))
7403cb63 5896 /* VALUE is negative. */
be3ccd9c
KH
5897 value |= ((bfd_vma) - 1) << bits;
5898
7403cb63
MM
5899 return value;
5900}
252b5132 5901
7403cb63
MM
5902/* Return non-zero if the indicated VALUE has overflowed the maximum
5903 range expressable by a signed number with the indicated number of
5904 BITS. */
252b5132 5905
7403cb63
MM
5906static boolean
5907mips_elf_overflow_p (value, bits)
5908 bfd_vma value;
5909 int bits;
5910{
5911 bfd_signed_vma svalue = (bfd_signed_vma) value;
252b5132 5912
7403cb63
MM
5913 if (svalue > (1 << (bits - 1)) - 1)
5914 /* The value is too big. */
5915 return true;
5916 else if (svalue < -(1 << (bits - 1)))
5917 /* The value is too small. */
5918 return true;
be3ccd9c 5919
7403cb63
MM
5920 /* All is well. */
5921 return false;
5922}
252b5132 5923
7403cb63 5924/* Calculate the %high function. */
252b5132 5925
7403cb63
MM
5926static bfd_vma
5927mips_elf_high (value)
5928 bfd_vma value;
5929{
5930 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5931}
252b5132 5932
7403cb63
MM
5933/* Calculate the %higher function. */
5934
5935static bfd_vma
5936mips_elf_higher (value)
5f771d47 5937 bfd_vma value ATTRIBUTE_UNUSED;
7403cb63
MM
5938{
5939#ifdef BFD64
5940 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5941#else
5942 abort ();
5943 return (bfd_vma) -1;
5944#endif
5945}
5946
5947/* Calculate the %highest function. */
5948
be3ccd9c 5949static bfd_vma
7403cb63 5950mips_elf_highest (value)
5f771d47 5951 bfd_vma value ATTRIBUTE_UNUSED;
7403cb63
MM
5952{
5953#ifdef BFD64
0af99795 5954 return ((value + (bfd_vma) 0x800080008000) >> 48) & 0xffff;
7403cb63
MM
5955#else
5956 abort ();
5957 return (bfd_vma) -1;
5958#endif
5959}
5960
5961/* Returns the GOT index for the global symbol indicated by H. */
5962
be3ccd9c 5963static bfd_vma
7403cb63
MM
5964mips_elf_global_got_index (abfd, h)
5965 bfd *abfd;
5966 struct elf_link_hash_entry *h;
5967{
5968 bfd_vma index;
5969 asection *sgot;
5970 struct mips_got_info *g;
5971
5972 g = mips_elf_got_info (abfd, &sgot);
5973
5974 /* Once we determine the global GOT entry with the lowest dynamic
5975 symbol table index, we must put all dynamic symbols with greater
5976 indices into the GOT. That makes it easy to calculate the GOT
5977 offset. */
5978 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
be3ccd9c 5979 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
103186c6 5980 * MIPS_ELF_GOT_SIZE (abfd));
7403cb63
MM
5981 BFD_ASSERT (index < sgot->_raw_size);
5982
5983 return index;
5984}
5985
5986/* Returns the offset for the entry at the INDEXth position
5987 in the GOT. */
5988
5989static bfd_vma
5990mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5991 bfd *dynobj;
5992 bfd *output_bfd;
5993 bfd_vma index;
5994{
5995 asection *sgot;
5996 bfd_vma gp;
7403cb63 5997
103186c6 5998 sgot = mips_elf_got_section (dynobj);
7403cb63 5999 gp = _bfd_get_gp_value (output_bfd);
be3ccd9c 6000 return (sgot->output_section->vma + sgot->output_offset + index -
7403cb63
MM
6001 gp);
6002}
6003
6004/* If H is a symbol that needs a global GOT entry, but has a dynamic
6005 symbol table index lower than any we've seen to date, record it for
6006 posterity. */
6007
6008static boolean
6009mips_elf_record_global_got_symbol (h, info, g)
6010 struct elf_link_hash_entry *h;
6011 struct bfd_link_info *info;
5f771d47 6012 struct mips_got_info *g ATTRIBUTE_UNUSED;
7403cb63
MM
6013{
6014 /* A global symbol in the GOT must also be in the dynamic symbol
6015 table. */
6016 if (h->dynindx == -1
6017 && !bfd_elf32_link_record_dynamic_symbol (info, h))
6018 return false;
be3ccd9c 6019
3a6b8597 6020 /* If we've already marked this entry as needing GOT space, we don't
7403cb63 6021 need to do it again. */
3a6b8597 6022 if (h->got.offset != (bfd_vma) -1)
7403cb63
MM
6023 return true;
6024
6025 /* By setting this to a value other than -1, we are indicating that
3a6b8597
AM
6026 there needs to be a GOT entry for H. Avoid using zero, as the
6027 generic ELF copy_indirect_symbol tests for <= 0. */
6028 h->got.offset = 1;
7403cb63
MM
6029
6030 return true;
6031}
6032
6033/* This structure is passed to mips_elf_sort_hash_table_f when sorting
6034 the dynamic symbols. */
be3ccd9c 6035
38b1a46c
NC
6036struct mips_elf_hash_sort_data
6037{
7403cb63
MM
6038 /* The symbol in the global GOT with the lowest dynamic symbol table
6039 index. */
6040 struct elf_link_hash_entry *low;
6041 /* The least dynamic symbol table index corresponding to a symbol
6042 with a GOT entry. */
6043 long min_got_dynindx;
6044 /* The greatest dynamic symbol table index not corresponding to a
6045 symbol without a GOT entry. */
6046 long max_non_got_dynindx;
6047};
6048
6049/* If H needs a GOT entry, assign it the highest available dynamic
be3ccd9c 6050 index. Otherwise, assign it the lowest available dynamic
7403cb63
MM
6051 index. */
6052
6053static boolean
6054mips_elf_sort_hash_table_f (h, data)
6055 struct mips_elf_link_hash_entry *h;
6056 PTR data;
6057{
be3ccd9c 6058 struct mips_elf_hash_sort_data *hsd
7403cb63
MM
6059 = (struct mips_elf_hash_sort_data *) data;
6060
6061 /* Symbols without dynamic symbol table entries aren't interesting
6062 at all. */
6063 if (h->root.dynindx == -1)
6064 return true;
6065
3a6b8597 6066 if (h->root.got.offset != 1)
7403cb63
MM
6067 h->root.dynindx = hsd->max_non_got_dynindx++;
6068 else
6069 {
6070 h->root.dynindx = --hsd->min_got_dynindx;
6071 hsd->low = (struct elf_link_hash_entry *) h;
6072 }
6073
6074 return true;
6075}
6076
6077/* Sort the dynamic symbol table so that symbols that need GOT entries
6078 appear towards the end. This reduces the amount of GOT space
b3be9b46
RH
6079 required. MAX_LOCAL is used to set the number of local symbols
6080 known to be in the dynamic symbol table. During
6081 mips_elf_size_dynamic_sections, this value is 1. Afterward, the
6082 section symbols are added and the count is higher. */
7403cb63
MM
6083
6084static boolean
b3be9b46 6085mips_elf_sort_hash_table (info, max_local)
7403cb63 6086 struct bfd_link_info *info;
b3be9b46 6087 unsigned long max_local;
7403cb63
MM
6088{
6089 struct mips_elf_hash_sort_data hsd;
6090 struct mips_got_info *g;
6091 bfd *dynobj;
6092
6093 dynobj = elf_hash_table (info)->dynobj;
6094
6095 hsd.low = NULL;
6096 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
b3be9b46 6097 hsd.max_non_got_dynindx = max_local;
be3ccd9c
KH
6098 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
6099 elf_hash_table (info)),
6100 mips_elf_sort_hash_table_f,
7403cb63
MM
6101 &hsd);
6102
7e766c3b 6103 /* There should have been enough room in the symbol table to
7403cb63 6104 accomodate both the GOT and non-GOT symbols. */
b305ef96 6105 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
7403cb63
MM
6106
6107 /* Now we know which dynamic symbol has the lowest dynamic symbol
6108 table index in the GOT. */
6109 g = mips_elf_got_info (dynobj, NULL);
6110 g->global_gotsym = hsd.low;
6111
6112 return true;
6113}
6114
6115/* Create a local GOT entry for VALUE. Return the index of the entry,
6116 or -1 if it could not be created. */
6117
6118static bfd_vma
6119mips_elf_create_local_got_entry (abfd, g, sgot, value)
6120 bfd *abfd;
6121 struct mips_got_info *g;
6122 asection *sgot;
6123 bfd_vma value;
6124{
6125 if (g->assigned_gotno >= g->local_gotno)
6126 {
6127 /* We didn't allocate enough space in the GOT. */
6128 (*_bfd_error_handler)
6129 (_("not enough GOT space for local GOT entries"));
6130 bfd_set_error (bfd_error_bad_value);
6131 return (bfd_vma) -1;
6132 }
6133
103186c6 6134 MIPS_ELF_PUT_WORD (abfd, value,
be3ccd9c 6135 (sgot->contents
103186c6
MM
6136 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
6137 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
7403cb63
MM
6138}
6139
6140/* Returns the GOT offset at which the indicated address can be found.
6141 If there is not yet a GOT entry for this value, create one. Returns
6142 -1 if no satisfactory GOT offset can be found. */
6143
6144static bfd_vma
6145mips_elf_local_got_index (abfd, info, value)
6146 bfd *abfd;
6147 struct bfd_link_info *info;
6148 bfd_vma value;
6149{
6150 asection *sgot;
6151 struct mips_got_info *g;
6152 bfd_byte *entry;
6153
6154 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
6155
6156 /* Look to see if we already have an appropriate entry. */
be3ccd9c
KH
6157 for (entry = (sgot->contents
6158 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
103186c6
MM
6159 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
6160 entry += MIPS_ELF_GOT_SIZE (abfd))
7403cb63 6161 {
103186c6 6162 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
7403cb63
MM
6163 if (address == value)
6164 return entry - sgot->contents;
6165 }
6166
6167 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
6168}
6169
6170/* Find a GOT entry that is within 32KB of the VALUE. These entries
6171 are supposed to be placed at small offsets in the GOT, i.e.,
6172 within 32KB of GP. Return the index into the GOT for this page,
6173 and store the offset from this entry to the desired address in
6174 OFFSETP, if it is non-NULL. */
6175
6176static bfd_vma
6177mips_elf_got_page (abfd, info, value, offsetp)
6178 bfd *abfd;
6179 struct bfd_link_info *info;
6180 bfd_vma value;
6181 bfd_vma *offsetp;
6182{
6183 asection *sgot;
6184 struct mips_got_info *g;
6185 bfd_byte *entry;
6186 bfd_byte *last_entry;
86033394 6187 bfd_vma index = 0;
7403cb63
MM
6188 bfd_vma address;
6189
6190 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
6191
6192 /* Look to see if we aleady have an appropriate entry. */
103186c6 6193 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
be3ccd9c 6194 for (entry = (sgot->contents
103186c6 6195 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
7403cb63 6196 entry != last_entry;
103186c6 6197 entry += MIPS_ELF_GOT_SIZE (abfd))
7403cb63 6198 {
103186c6
MM
6199 address = MIPS_ELF_GET_WORD (abfd, entry);
6200
7403cb63
MM
6201 if (!mips_elf_overflow_p (value - address, 16))
6202 {
6203 /* This entry will serve as the page pointer. We can add a
6204 16-bit number to it to get the actual address. */
6205 index = entry - sgot->contents;
6206 break;
252b5132 6207 }
7403cb63
MM
6208 }
6209
6210 /* If we didn't have an appropriate entry, we create one now. */
6211 if (entry == last_entry)
6212 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
6213
6214 if (offsetp)
6215 {
103186c6 6216 address = MIPS_ELF_GET_WORD (abfd, entry);
7403cb63
MM
6217 *offsetp = value - address;
6218 }
6219
6220 return index;
6221}
6222
6223/* Find a GOT entry whose higher-order 16 bits are the same as those
6224 for value. Return the index into the GOT for this entry. */
6225
6226static bfd_vma
b305ef96 6227mips_elf_got16_entry (abfd, info, value, external)
7403cb63
MM
6228 bfd *abfd;
6229 struct bfd_link_info *info;
6230 bfd_vma value;
b305ef96 6231 boolean external;
7403cb63
MM
6232{
6233 asection *sgot;
6234 struct mips_got_info *g;
6235 bfd_byte *entry;
6236 bfd_byte *last_entry;
86033394 6237 bfd_vma index = 0;
7403cb63
MM
6238 bfd_vma address;
6239
b305ef96
UC
6240 if (! external)
6241 {
6242 /* Although the ABI says that it is "the high-order 16 bits" that we
6243 want, it is really the %high value. The complete value is
6244 calculated with a `addiu' of a LO16 relocation, just as with a
6245 HI16/LO16 pair. */
6246 value = mips_elf_high (value) << 16;
6247 }
6248
7403cb63
MM
6249 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
6250
6251 /* Look to see if we already have an appropriate entry. */
103186c6 6252 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
be3ccd9c 6253 for (entry = (sgot->contents
103186c6 6254 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
7403cb63 6255 entry != last_entry;
103186c6 6256 entry += MIPS_ELF_GOT_SIZE (abfd))
7403cb63 6257 {
103186c6 6258 address = MIPS_ELF_GET_WORD (abfd, entry);
b305ef96 6259 if (address == value)
252b5132 6260 {
b305ef96
UC
6261 /* This entry has the right high-order 16 bits, and the low-order
6262 16 bits are set to zero. */
4f2860ca 6263 index = entry - sgot->contents;
7403cb63
MM
6264 break;
6265 }
6266 }
6267
6268 /* If we didn't have an appropriate entry, we create one now. */
6269 if (entry == last_entry)
6270 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
6271
6272 return index;
6273}
6274
bb2d6cd7 6275/* Returns the first relocation of type r_type found, beginning with
23b255aa 6276 RELOCATION. RELEND is one-past-the-end of the relocation table. */
7403cb63 6277
23b255aa 6278static const Elf_Internal_Rela *
bb2d6cd7
GK
6279mips_elf_next_relocation (r_type, relocation, relend)
6280 unsigned int r_type;
103186c6
MM
6281 const Elf_Internal_Rela *relocation;
6282 const Elf_Internal_Rela *relend;
7403cb63
MM
6283{
6284 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
6285 immediately following. However, for the IRIX6 ABI, the next
6286 relocation may be a composed relocation consisting of several
6287 relocations for the same address. In that case, the R_MIPS_LO16
435394bf 6288 relocation may occur as one of these. We permit a similar
7403cb63
MM
6289 extension in general, as that is useful for GCC. */
6290 while (relocation < relend)
6291 {
bb2d6cd7 6292 if (ELF32_R_TYPE (relocation->r_info) == r_type)
23b255aa 6293 return relocation;
7403cb63
MM
6294
6295 ++relocation;
6296 }
6297
6298 /* We didn't find it. */
6387d602 6299 bfd_set_error (bfd_error_bad_value);
23b255aa 6300 return NULL;
7403cb63
MM
6301}
6302
7b1f1231
MM
6303/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6304 is the original relocation, which is now being transformed into a
b305ef96 6305 dynamic relocation. The ADDENDP is adjusted if necessary; the
7b1f1231 6306 caller should store the result in place of the original addend. */
7403cb63 6307
7b1f1231
MM
6308static boolean
6309mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
9117d219 6310 symbol, addendp, input_section)
7403cb63
MM
6311 bfd *output_bfd;
6312 struct bfd_link_info *info;
103186c6 6313 const Elf_Internal_Rela *rel;
7b1f1231
MM
6314 struct mips_elf_link_hash_entry *h;
6315 asection *sec;
6316 bfd_vma symbol;
6317 bfd_vma *addendp;
7403cb63
MM
6318 asection *input_section;
6319{
6320 Elf_Internal_Rel outrel;
6321 boolean skip;
6322 asection *sreloc;
6323 bfd *dynobj;
6324 int r_type;
6325
6326 r_type = ELF32_R_TYPE (rel->r_info);
6327 dynobj = elf_hash_table (info)->dynobj;
be3ccd9c 6328 sreloc
103186c6
MM
6329 = bfd_get_section_by_name (dynobj,
6330 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
7403cb63 6331 BFD_ASSERT (sreloc != NULL);
b305ef96 6332 BFD_ASSERT (sreloc->contents != NULL);
14d5043a
L
6333 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6334 < sreloc->_raw_size);
7403cb63
MM
6335
6336 skip = false;
c629eae0
JJ
6337 outrel.r_offset =
6338 _bfd_elf_section_offset (output_bfd, info, input_section, rel->r_offset);
6339 if (outrel.r_offset == (bfd_vma) -1)
6340 skip = true;
7403cb63 6341
b305ef96 6342 /* If we've decided to skip this relocation, just output an empty
7b1f1231
MM
6343 record. Note that R_MIPS_NONE == 0, so that this call to memset
6344 is a way of setting R_TYPE to R_MIPS_NONE. */
7403cb63
MM
6345 if (skip)
6346 memset (&outrel, 0, sizeof (outrel));
7b1f1231
MM
6347 else
6348 {
6349 long indx;
6350 bfd_vma section_offset;
6351
6352 /* We must now calculate the dynamic symbol table index to use
6353 in the relocation. */
6354 if (h != NULL
6355 && (! info->symbolic || (h->root.elf_link_hash_flags
6356 & ELF_LINK_HASH_DEF_REGULAR) == 0))
6357 {
6358 indx = h->root.dynindx;
b305ef96
UC
6359 /* h->root.dynindx may be -1 if this symbol was marked to
6360 become local. */
6361 if (indx == -1)
be3ccd9c 6362 indx = 0;
7b1f1231
MM
6363 }
6364 else
6365 {
6366 if (sec != NULL && bfd_is_abs_section (sec))
6367 indx = 0;
6368 else if (sec == NULL || sec->owner == NULL)
6369 {
6370 bfd_set_error (bfd_error_bad_value);
6371 return false;
6372 }
6373 else
6374 {
6375 indx = elf_section_data (sec->output_section)->dynindx;
6376 if (indx == 0)
6377 abort ();
6378 }
6379
6380 /* Figure out how far the target of the relocation is from
6381 the beginning of its section. */
6382 section_offset = symbol - sec->output_section->vma;
6383 /* The relocation we're building is section-relative.
6384 Therefore, the original addend must be adjusted by the
6385 section offset. */
9117d219 6386 *addendp += section_offset;
7b1f1231
MM
6387 /* Now, the relocation is just against the section. */
6388 symbol = sec->output_section->vma;
6389 }
be3ccd9c 6390
9117d219
NC
6391 /* If the relocation was previously an absolute relocation and
6392 this symbol will not be referred to by the relocation, we must
6393 adjust it by the value we give it in the dynamic symbol table.
6394 Otherwise leave the job up to the dynamic linker. */
6395 if (!indx && r_type != R_MIPS_REL32)
7b1f1231
MM
6396 *addendp += symbol;
6397
6398 /* The relocation is always an REL32 relocation because we don't
6399 know where the shared library will wind up at load-time. */
6400 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
6401
6402 /* Adjust the output offset of the relocation to reference the
6403 correct location in the output file. */
6404 outrel.r_offset += (input_section->output_section->vma
6405 + input_section->output_offset);
6406 }
7403cb63 6407
7b1f1231
MM
6408 /* Put the relocation back out. We have to use the special
6409 relocation outputter in the 64-bit case since the 64-bit
6410 relocation format is non-standard. */
103186c6
MM
6411 if (ABI_64_P (output_bfd))
6412 {
6413 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6414 (output_bfd, &outrel,
be3ccd9c 6415 (sreloc->contents
103186c6
MM
6416 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6417 }
6418 else
6419 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
6420 (((Elf32_External_Rel *)
6421 sreloc->contents)
6422 + sreloc->reloc_count));
7b1f1231
MM
6423
6424 /* Record the index of the first relocation referencing H. This
6425 information is later emitted in the .msym section. */
6426 if (h != NULL
be3ccd9c 6427 && (h->min_dyn_reloc_index == 0
7b1f1231
MM
6428 || sreloc->reloc_count < h->min_dyn_reloc_index))
6429 h->min_dyn_reloc_index = sreloc->reloc_count;
6430
6431 /* We've now added another relocation. */
7403cb63
MM
6432 ++sreloc->reloc_count;
6433
6434 /* Make sure the output section is writable. The dynamic linker
6435 will be writing to it. */
6436 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6437 |= SHF_WRITE;
6438
6439 /* On IRIX5, make an entry of compact relocation info. */
6440 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
6441 {
be3ccd9c 6442 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
7403cb63
MM
6443 bfd_byte *cr;
6444
6445 if (scpt)
6446 {
6447 Elf32_crinfo cptrel;
6448
6449 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6450 cptrel.vaddr = (rel->r_offset
6451 + input_section->output_section->vma
6452 + input_section->output_offset);
6453 if (r_type == R_MIPS_REL32)
6454 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
252b5132 6455 else
7403cb63
MM
6456 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6457 mips_elf_set_cr_dist2to (cptrel, 0);
7b1f1231 6458 cptrel.konst = *addendp;
7403cb63
MM
6459
6460 cr = (scpt->contents
6461 + sizeof (Elf32_External_compact_rel));
6462 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6463 ((Elf32_External_crinfo *) cr
6464 + scpt->reloc_count));
6465 ++scpt->reloc_count;
6466 }
6467 }
252b5132 6468
7b1f1231 6469 return true;
7403cb63 6470}
252b5132 6471
7403cb63
MM
6472/* Calculate the value produced by the RELOCATION (which comes from
6473 the INPUT_BFD). The ADDEND is the addend to use for this
6474 RELOCATION; RELOCATION->R_ADDEND is ignored.
6475
6476 The result of the relocation calculation is stored in VALUEP.
197b9ca0
MM
6477 REQUIRE_JALXP indicates whether or not the opcode used with this
6478 relocation must be JALX.
7403cb63
MM
6479
6480 This function returns bfd_reloc_continue if the caller need take no
6481 further action regarding this relocation, bfd_reloc_notsupported if
6482 something goes dramatically wrong, bfd_reloc_overflow if an
6483 overflow occurs, and bfd_reloc_ok to indicate success. */
6484
6485static bfd_reloc_status_type
be3ccd9c 6486mips_elf_calculate_relocation (abfd,
7403cb63
MM
6487 input_bfd,
6488 input_section,
6489 info,
6490 relocation,
6491 addend,
6492 howto,
7403cb63
MM
6493 local_syms,
6494 local_sections,
6495 valuep,
197b9ca0 6496 namep,
be3ccd9c 6497 require_jalxp)
7403cb63
MM
6498 bfd *abfd;
6499 bfd *input_bfd;
6500 asection *input_section;
6501 struct bfd_link_info *info;
103186c6 6502 const Elf_Internal_Rela *relocation;
7403cb63
MM
6503 bfd_vma addend;
6504 reloc_howto_type *howto;
7403cb63
MM
6505 Elf_Internal_Sym *local_syms;
6506 asection **local_sections;
6507 bfd_vma *valuep;
6508 const char **namep;
197b9ca0 6509 boolean *require_jalxp;
7403cb63
MM
6510{
6511 /* The eventual value we will return. */
6512 bfd_vma value;
6513 /* The address of the symbol against which the relocation is
6514 occurring. */
6515 bfd_vma symbol = 0;
6516 /* The final GP value to be used for the relocatable, executable, or
6517 shared object file being produced. */
6518 bfd_vma gp = (bfd_vma) - 1;
6519 /* The place (section offset or address) of the storage unit being
6520 relocated. */
6521 bfd_vma p;
6522 /* The value of GP used to create the relocatable object. */
6523 bfd_vma gp0 = (bfd_vma) - 1;
6524 /* The offset into the global offset table at which the address of
6525 the relocation entry symbol, adjusted by the addend, resides
6526 during execution. */
6527 bfd_vma g = (bfd_vma) - 1;
6528 /* The section in which the symbol referenced by the relocation is
6529 located. */
6530 asection *sec = NULL;
be3ccd9c 6531 struct mips_elf_link_hash_entry *h = NULL;
103186c6
MM
6532 /* True if the symbol referred to by this relocation is a local
6533 symbol. */
7403cb63 6534 boolean local_p;
103186c6 6535 /* True if the symbol referred to by this relocation is "_gp_disp". */
7403cb63
MM
6536 boolean gp_disp_p = false;
6537 Elf_Internal_Shdr *symtab_hdr;
6538 size_t extsymoff;
103186c6 6539 unsigned long r_symndx;
7403cb63 6540 int r_type;
103186c6
MM
6541 /* True if overflow occurred during the calculation of the
6542 relocation value. */
7403cb63 6543 boolean overflowed_p;
197b9ca0
MM
6544 /* True if this relocation refers to a MIPS16 function. */
6545 boolean target_is_16_bit_code_p = false;
7403cb63
MM
6546
6547 /* Parse the relocation. */
6548 r_symndx = ELF32_R_SYM (relocation->r_info);
6549 r_type = ELF32_R_TYPE (relocation->r_info);
be3ccd9c 6550 p = (input_section->output_section->vma
7403cb63
MM
6551 + input_section->output_offset
6552 + relocation->r_offset);
6553
6554 /* Assume that there will be no overflow. */
6555 overflowed_p = false;
6556
6387d602
ILT
6557 /* Figure out whether or not the symbol is local, and get the offset
6558 used in the array of hash table entries. */
7403cb63 6559 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6387d602 6560 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b305ef96 6561 local_sections, false);
6387d602
ILT
6562 if (! elf_bad_symtab (input_bfd))
6563 extsymoff = symtab_hdr->sh_info;
6564 else
7403cb63
MM
6565 {
6566 /* The symbol table does not follow the rule that local symbols
6567 must come before globals. */
6568 extsymoff = 0;
7403cb63 6569 }
be3ccd9c 6570
7403cb63
MM
6571 /* Figure out the value of the symbol. */
6572 if (local_p)
6573 {
6574 Elf_Internal_Sym *sym;
6575
6576 sym = local_syms + r_symndx;
6577 sec = local_sections[r_symndx];
6578
6579 symbol = sec->output_section->vma + sec->output_offset;
6580 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6581 symbol += sym->st_value;
6582
6583 /* MIPS16 text labels should be treated as odd. */
6584 if (sym->st_other == STO_MIPS16)
6585 ++symbol;
6586
6587 /* Record the name of this symbol, for our caller. */
6588 *namep = bfd_elf_string_from_elf_section (input_bfd,
6589 symtab_hdr->sh_link,
6590 sym->st_name);
e049a0de 6591 if (*namep == '\0')
7403cb63 6592 *namep = bfd_section_name (input_bfd, sec);
197b9ca0
MM
6593
6594 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
7403cb63
MM
6595 }
6596 else
6597 {
6598 /* For global symbols we look up the symbol in the hash-table. */
be3ccd9c 6599 h = ((struct mips_elf_link_hash_entry *)
7403cb63
MM
6600 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
6601 /* Find the real hash-table entry for this symbol. */
b305ef96
UC
6602 while (h->root.root.type == bfd_link_hash_indirect
6603 || h->root.root.type == bfd_link_hash_warning)
7403cb63 6604 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
be3ccd9c 6605
7403cb63
MM
6606 /* Record the name of this symbol, for our caller. */
6607 *namep = h->root.root.root.string;
6608
6609 /* See if this is the special _gp_disp symbol. Note that such a
6610 symbol must always be a global symbol. */
6611 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
6612 {
6613 /* Relocations against _gp_disp are permitted only with
6614 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
6615 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
6616 return bfd_reloc_notsupported;
6617
6618 gp_disp_p = true;
6619 }
97a4bb05
MM
6620 /* If this symbol is defined, calculate its address. Note that
6621 _gp_disp is a magic symbol, always implicitly defined by the
6622 linker, so it's inappropriate to check to see whether or not
6623 its defined. */
6624 else if ((h->root.root.type == bfd_link_hash_defined
6625 || h->root.root.type == bfd_link_hash_defweak)
6626 && h->root.root.u.def.section)
7403cb63
MM
6627 {
6628 sec = h->root.root.u.def.section;
6629 if (sec->output_section)
be3ccd9c 6630 symbol = (h->root.root.u.def.value
7403cb63
MM
6631 + sec->output_section->vma
6632 + sec->output_offset);
252b5132 6633 else
7403cb63
MM
6634 symbol = h->root.root.u.def.value;
6635 }
97287574
MM
6636 else if (h->root.root.type == bfd_link_hash_undefweak)
6637 /* We allow relocations against undefined weak symbols, giving
6638 it the value zero, so that you can undefined weak functions
6639 and check to see if they exist by looking at their
6640 addresses. */
6641 symbol = 0;
671bae9c
NC
6642 else if (info->shared
6643 && (!info->symbolic || info->allow_shlib_undefined)
6644 && !info->no_undefined
ba09750c 6645 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
8535d39c 6646 symbol = 0;
f7cb7d68
UC
6647 else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0 ||
6648 strcmp (h->root.root.root.string, "_DYNAMIC_LINKING") == 0)
3811169e
MM
6649 {
6650 /* If this is a dynamic link, we should have created a
be3ccd9c 6651 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
f7cb7d68 6652 in in mips_elf_create_dynamic_sections.
3811169e
MM
6653 Otherwise, we should define the symbol with a value of 0.
6654 FIXME: It should probably get into the symbol table
6655 somehow as well. */
6656 BFD_ASSERT (! info->shared);
6657 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
8535d39c 6658 symbol = 0;
3811169e 6659 }
7403cb63
MM
6660 else
6661 {
5cc7c785
L
6662 if (! ((*info->callbacks->undefined_symbol)
6663 (info, h->root.root.root.string, input_bfd,
6664 input_section, relocation->r_offset,
3a27a730 6665 (!info->shared || info->no_undefined
ba09750c 6666 || ELF_ST_VISIBILITY (h->root.other)))))
5cc7c785
L
6667 return bfd_reloc_undefined;
6668 symbol = 0;
7403cb63 6669 }
197b9ca0
MM
6670
6671 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
6672 }
be3ccd9c 6673
197b9ca0
MM
6674 /* If this is a 32-bit call to a 16-bit function with a stub, we
6675 need to redirect the call to the stub, unless we're already *in*
6676 a stub. */
6677 if (r_type != R_MIPS16_26 && !info->relocateable
6678 && ((h != NULL && h->fn_stub != NULL)
6679 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
6680 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
6681 && !mips_elf_stub_section_p (input_bfd, input_section))
6682 {
6683 /* This is a 32-bit call to a 16-bit function. We should
6684 have already noticed that we were going to need the
6685 stub. */
6686 if (local_p)
6687 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
6688 else
6689 {
6690 BFD_ASSERT (h->need_fn_stub);
6691 sec = h->fn_stub;
6692 }
6693
6694 symbol = sec->output_section->vma + sec->output_offset;
7403cb63 6695 }
197b9ca0
MM
6696 /* If this is a 16-bit call to a 32-bit function with a stub, we
6697 need to redirect the call to the stub. */
6698 else if (r_type == R_MIPS16_26 && !info->relocateable
be3ccd9c 6699 && h != NULL
197b9ca0
MM
6700 && (h->call_stub != NULL || h->call_fp_stub != NULL)
6701 && !target_is_16_bit_code_p)
6702 {
6703 /* If both call_stub and call_fp_stub are defined, we can figure
6704 out which one to use by seeing which one appears in the input
6705 file. */
6706 if (h->call_stub != NULL && h->call_fp_stub != NULL)
6707 {
6708 asection *o;
6709
6710 sec = NULL;
6711 for (o = input_bfd->sections; o != NULL; o = o->next)
6712 {
6713 if (strncmp (bfd_get_section_name (input_bfd, o),
6714 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6715 {
6716 sec = h->call_fp_stub;
6717 break;
6718 }
6719 }
6720 if (sec == NULL)
6721 sec = h->call_stub;
6722 }
6723 else if (h->call_stub != NULL)
6724 sec = h->call_stub;
6725 else
6726 sec = h->call_fp_stub;
6727
6728 BFD_ASSERT (sec->_raw_size > 0);
6729 symbol = sec->output_section->vma + sec->output_offset;
6730 }
6731
6732 /* Calls from 16-bit code to 32-bit code and vice versa require the
6733 special jalx instruction. */
6387d602 6734 *require_jalxp = (!info->relocateable
55baa95b
EC
6735 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
6736 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
252b5132 6737
b305ef96
UC
6738 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
6739 local_sections, true);
6740
7403cb63
MM
6741 /* If we haven't already determined the GOT offset, or the GP value,
6742 and we're going to need it, get it now. */
6743 switch (r_type)
6744 {
6745 case R_MIPS_CALL16:
2841ecd0 6746 case R_MIPS_GOT16:
7403cb63
MM
6747 case R_MIPS_GOT_DISP:
6748 case R_MIPS_GOT_HI16:
6749 case R_MIPS_CALL_HI16:
6750 case R_MIPS_GOT_LO16:
6751 case R_MIPS_CALL_LO16:
6752 /* Find the index into the GOT where this value is located. */
4f2860ca 6753 if (!local_p)
7403cb63
MM
6754 {
6755 BFD_ASSERT (addend == 0);
be3ccd9c 6756 g = mips_elf_global_got_index
7403cb63 6757 (elf_hash_table (info)->dynobj,
be3ccd9c 6758 (struct elf_link_hash_entry *) h);
9a8f3bb7
UC
6759 if (! elf_hash_table(info)->dynamic_sections_created
6760 || (info->shared
6761 && (info->symbolic || h->root.dynindx == -1)
6762 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6763 {
6764 /* This is a static link or a -Bsymbolic link. The
6765 symbol is defined locally, or was forced to be local.
6766 We must initialize this entry in the GOT. */
6767 asection *sgot = mips_elf_got_section(elf_hash_table
6768 (info)->dynobj);
6769 MIPS_ELF_PUT_WORD (elf_hash_table (info)->dynobj,
6770 symbol + addend, sgot->contents + g);
6771 }
7403cb63 6772 }
9117d219 6773 else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
4f2860ca
MM
6774 /* There's no need to create a local GOT entry here; the
6775 calculation for a local GOT16 entry does not involve G. */
6776 break;
7403cb63
MM
6777 else
6778 {
6779 g = mips_elf_local_got_index (abfd, info, symbol + addend);
6780 if (g == (bfd_vma) -1)
6781 return false;
6782 }
252b5132 6783
7403cb63
MM
6784 /* Convert GOT indices to actual offsets. */
6785 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6786 abfd, g);
6787 break;
be3ccd9c 6788
7403cb63
MM
6789 case R_MIPS_HI16:
6790 case R_MIPS_LO16:
175e9962 6791 case R_MIPS16_GPREL:
7403cb63
MM
6792 case R_MIPS_GPREL16:
6793 case R_MIPS_GPREL32:
0af99795 6794 case R_MIPS_LITERAL:
7403cb63
MM
6795 gp0 = _bfd_get_gp_value (input_bfd);
6796 gp = _bfd_get_gp_value (abfd);
6797 break;
252b5132 6798
7403cb63
MM
6799 default:
6800 break;
6801 }
252b5132 6802
7403cb63
MM
6803 /* Figure out what kind of relocation is being performed. */
6804 switch (r_type)
6805 {
6806 case R_MIPS_NONE:
6807 return bfd_reloc_continue;
252b5132 6808
7403cb63
MM
6809 case R_MIPS_16:
6810 value = symbol + mips_elf_sign_extend (addend, 16);
6811 overflowed_p = mips_elf_overflow_p (value, 16);
6812 break;
252b5132 6813
7403cb63
MM
6814 case R_MIPS_32:
6815 case R_MIPS_REL32:
a3c7651d 6816 case R_MIPS_64:
7b1f1231
MM
6817 if ((info->shared
6818 || (elf_hash_table (info)->dynamic_sections_created
6819 && h != NULL
d918dd35
L
6820 && ((h->root.elf_link_hash_flags
6821 & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
6822 && ((h->root.elf_link_hash_flags
6823 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
ec338859 6824 && r_symndx != 0
7b1f1231 6825 && (input_section->flags & SEC_ALLOC) != 0)
7403cb63 6826 {
7b1f1231
MM
6827 /* If we're creating a shared library, or this relocation is
6828 against a symbol in a shared library, then we can't know
6829 where the symbol will end up. So, we create a relocation
6830 record in the output, and leave the job up to the dynamic
6831 linker. */
6832 value = addend;
be3ccd9c
KH
6833 if (!mips_elf_create_dynamic_relocation (abfd,
6834 info,
7b1f1231
MM
6835 relocation,
6836 h,
6837 sec,
6838 symbol,
6839 &value,
9117d219 6840 input_section))
7b1f1231 6841 return false;
7403cb63
MM
6842 }
6843 else
6844 {
a3c7651d 6845 if (r_type != R_MIPS_REL32)
7403cb63
MM
6846 value = symbol + addend;
6847 else
6848 value = addend;
6849 }
6850 value &= howto->dst_mask;
6851 break;
6852
bb2d6cd7
GK
6853 case R_MIPS_PC32:
6854 case R_MIPS_PC64:
6855 case R_MIPS_GNU_REL_LO16:
6856 value = symbol + addend - p;
6857 value &= howto->dst_mask;
6858 break;
6859
6860 case R_MIPS_GNU_REL16_S2:
6861 value = symbol + mips_elf_sign_extend (addend << 2, 18) - p;
6862 overflowed_p = mips_elf_overflow_p (value, 18);
6863 value = (value >> 2) & howto->dst_mask;
6864 break;
6865
6866 case R_MIPS_GNU_REL_HI16:
6867 value = mips_elf_high (addend + symbol - p);
6868 value &= howto->dst_mask;
6869 break;
6870
e53bd91b 6871 case R_MIPS16_26:
9117d219 6872 /* The calculation for R_MIPS16_26 is just the same as for an
e53bd91b 6873 R_MIPS_26. It's only the storage of the relocated field into
1e52e2ee 6874 the output file that's different. That's handled in
e53bd91b
MM
6875 mips_elf_perform_relocation. So, we just fall through to the
6876 R_MIPS_26 case here. */
7403cb63
MM
6877 case R_MIPS_26:
6878 if (local_p)
9117d219 6879 value = (((addend << 2) | ((p + 4) & 0xf0000000)) + symbol) >> 2;
7403cb63
MM
6880 else
6881 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
6882 value &= howto->dst_mask;
6883 break;
6884
6885 case R_MIPS_HI16:
6886 if (!gp_disp_p)
6887 {
6888 value = mips_elf_high (addend + symbol);
6889 value &= howto->dst_mask;
6890 }
6891 else
6892 {
6893 value = mips_elf_high (addend + gp - p);
6894 overflowed_p = mips_elf_overflow_p (value, 16);
6895 }
6896 break;
6897
6898 case R_MIPS_LO16:
6899 if (!gp_disp_p)
6900 value = (symbol + addend) & howto->dst_mask;
6901 else
6902 {
6903 value = addend + gp - p + 4;
97a4bb05
MM
6904 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6905 for overflow. But, on, say, Irix 5, relocations against
6906 _gp_disp are normally generated from the .cpload
6907 pseudo-op. It generates code that normally looks like
6908 this:
6909
6910 lui $gp,%hi(_gp_disp)
6911 addiu $gp,$gp,%lo(_gp_disp)
6912 addu $gp,$gp,$t9
6913
6914 Here $t9 holds the address of the function being called,
6915 as required by the MIPS ELF ABI. The R_MIPS_LO16
e53bd91b 6916 relocation can easily overflow in this situation, but the
97a4bb05
MM
6917 R_MIPS_HI16 relocation will handle the overflow.
6918 Therefore, we consider this a bug in the MIPS ABI, and do
6919 not check for overflow here. */
7403cb63
MM
6920 }
6921 break;
6922
6923 case R_MIPS_LITERAL:
6924 /* Because we don't merge literal sections, we can handle this
6925 just like R_MIPS_GPREL16. In the long run, we should merge
6926 shared literals, and then we will need to additional work
6927 here. */
6928
6929 /* Fall through. */
6930
b7233c24
MM
6931 case R_MIPS16_GPREL:
6932 /* The R_MIPS16_GPREL performs the same calculation as
6933 R_MIPS_GPREL16, but stores the relocated bits in a different
6934 order. We don't need to do anything special here; the
6935 differences are handled in mips_elf_perform_relocation. */
7403cb63
MM
6936 case R_MIPS_GPREL16:
6937 if (local_p)
6938 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
6939 else
6940 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
6941 overflowed_p = mips_elf_overflow_p (value, 16);
6942 break;
be3ccd9c 6943
7403cb63 6944 case R_MIPS_GOT16:
9117d219 6945 case R_MIPS_CALL16:
7403cb63
MM
6946 if (local_p)
6947 {
b305ef96 6948 boolean forced;
be3ccd9c 6949
b305ef96
UC
6950 /* The special case is when the symbol is forced to be local. We
6951 need the full address in the GOT since no R_MIPS_LO16 relocation
6952 follows. */
6953 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
6954 local_sections, false);
6955 value = mips_elf_got16_entry (abfd, info, symbol + addend, forced);
7403cb63
MM
6956 if (value == (bfd_vma) -1)
6957 return false;
be3ccd9c 6958 value
7403cb63
MM
6959 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6960 abfd,
6961 value);
6962 overflowed_p = mips_elf_overflow_p (value, 16);
6963 break;
6964 }
6965
6966 /* Fall through. */
6967
7403cb63
MM
6968 case R_MIPS_GOT_DISP:
6969 value = g;
6970 overflowed_p = mips_elf_overflow_p (value, 16);
6971 break;
6972
6973 case R_MIPS_GPREL32:
6974 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6975 break;
6976
6977 case R_MIPS_PC16:
6978 value = mips_elf_sign_extend (addend, 16) + symbol - p;
6979 overflowed_p = mips_elf_overflow_p (value, 16);
cb56d3d3 6980 value = (bfd_vma) ((bfd_signed_vma) value / 4);
7403cb63
MM
6981 break;
6982
6983 case R_MIPS_GOT_HI16:
6984 case R_MIPS_CALL_HI16:
6985 /* We're allowed to handle these two relocations identically.
6986 The dynamic linker is allowed to handle the CALL relocations
6987 differently by creating a lazy evaluation stub. */
6988 value = g;
6989 value = mips_elf_high (value);
6990 value &= howto->dst_mask;
6991 break;
6992
6993 case R_MIPS_GOT_LO16:
6994 case R_MIPS_CALL_LO16:
6995 value = g & howto->dst_mask;
6996 break;
6997
7403cb63
MM
6998 case R_MIPS_GOT_PAGE:
6999 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
7000 if (value == (bfd_vma) -1)
7001 return false;
7002 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
7003 abfd,
7004 value);
7005 overflowed_p = mips_elf_overflow_p (value, 16);
7006 break;
be3ccd9c 7007
7403cb63
MM
7008 case R_MIPS_GOT_OFST:
7009 mips_elf_got_page (abfd, info, symbol + addend, &value);
7010 overflowed_p = mips_elf_overflow_p (value, 16);
7011 break;
7012
7013 case R_MIPS_SUB:
7014 value = symbol - addend;
7015 value &= howto->dst_mask;
7016 break;
7017
7018 case R_MIPS_HIGHER:
7019 value = mips_elf_higher (addend + symbol);
7020 value &= howto->dst_mask;
7021 break;
7022
7023 case R_MIPS_HIGHEST:
7024 value = mips_elf_highest (addend + symbol);
7025 value &= howto->dst_mask;
7026 break;
be3ccd9c 7027
7403cb63
MM
7028 case R_MIPS_SCN_DISP:
7029 value = symbol + addend - sec->output_offset;
7030 value &= howto->dst_mask;
7031 break;
7032
7033 case R_MIPS_PJUMP:
7034 case R_MIPS_JALR:
7035 /* Both of these may be ignored. R_MIPS_JALR is an optimization
7036 hint; we could improve performance by honoring that hint. */
7037 return bfd_reloc_continue;
7038
7039 case R_MIPS_GNU_VTINHERIT:
7040 case R_MIPS_GNU_VTENTRY:
7041 /* We don't do anything with these at present. */
7042 return bfd_reloc_continue;
7043
7403cb63
MM
7044 default:
7045 /* An unrecognized relocation type. */
7046 return bfd_reloc_notsupported;
7047 }
7048
7049 /* Store the VALUE for our caller. */
7050 *valuep = value;
7051 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
7052}
7053
7054/* Obtain the field relocated by RELOCATION. */
7055
7056static bfd_vma
7057mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
7058 reloc_howto_type *howto;
103186c6 7059 const Elf_Internal_Rela *relocation;
7403cb63
MM
7060 bfd *input_bfd;
7061 bfd_byte *contents;
7062{
7063 bfd_vma x;
7064 bfd_byte *location = contents + relocation->r_offset;
7065
b7233c24 7066 /* Obtain the bytes. */
1e738b87 7067 x = bfd_get (((bfd_vma)(8 * bfd_get_reloc_size (howto))), input_bfd, location);
7403cb63 7068
6296902e
MM
7069 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
7070 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
1e52e2ee
MM
7071 && bfd_little_endian (input_bfd))
7072 /* The two 16-bit words will be reversed on a little-endian
7073 system. See mips_elf_perform_relocation for more details. */
7074 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
7075
7403cb63
MM
7076 return x;
7077}
7078
7079/* It has been determined that the result of the RELOCATION is the
7080 VALUE. Use HOWTO to place VALUE into the output file at the
7081 appropriate position. The SECTION is the section to which the
197b9ca0
MM
7082 relocation applies. If REQUIRE_JALX is true, then the opcode used
7083 for the relocation must be either JAL or JALX, and it is
7084 unconditionally converted to JALX.
7403cb63
MM
7085
7086 Returns false if anything goes wrong. */
252b5132 7087
197b9ca0 7088static boolean
e53bd91b 7089mips_elf_perform_relocation (info, howto, relocation, value,
be3ccd9c 7090 input_bfd, input_section,
197b9ca0 7091 contents, require_jalx)
e53bd91b 7092 struct bfd_link_info *info;
7403cb63 7093 reloc_howto_type *howto;
103186c6 7094 const Elf_Internal_Rela *relocation;
7403cb63
MM
7095 bfd_vma value;
7096 bfd *input_bfd;
197b9ca0 7097 asection *input_section;
7403cb63 7098 bfd_byte *contents;
197b9ca0 7099 boolean require_jalx;
7403cb63
MM
7100{
7101 bfd_vma x;
e53bd91b 7102 bfd_byte *location;
197b9ca0 7103 int r_type = ELF32_R_TYPE (relocation->r_info);
e53bd91b
MM
7104
7105 /* Figure out where the relocation is occurring. */
7106 location = contents + relocation->r_offset;
252b5132 7107
7403cb63
MM
7108 /* Obtain the current value. */
7109 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
252b5132 7110
7403cb63
MM
7111 /* Clear the field we are setting. */
7112 x &= ~howto->dst_mask;
252b5132 7113
e53bd91b
MM
7114 /* If this is the R_MIPS16_26 relocation, we must store the
7115 value in a funny way. */
197b9ca0 7116 if (r_type == R_MIPS16_26)
7403cb63 7117 {
e53bd91b
MM
7118 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
7119 Most mips16 instructions are 16 bits, but these instructions
7120 are 32 bits.
7121
7122 The format of these instructions is:
7123
7124 +--------------+--------------------------------+
7125 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
7126 +--------------+--------------------------------+
7127 ! Immediate 15:0 !
7128 +-----------------------------------------------+
be3ccd9c 7129
e53bd91b
MM
7130 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
7131 Note that the immediate value in the first word is swapped.
7132
7133 When producing a relocateable object file, R_MIPS16_26 is
7134 handled mostly like R_MIPS_26. In particular, the addend is
7135 stored as a straight 26-bit value in a 32-bit instruction.
7136 (gas makes life simpler for itself by never adjusting a
7137 R_MIPS16_26 reloc to be against a section, so the addend is
7138 always zero). However, the 32 bit instruction is stored as 2
7139 16-bit values, rather than a single 32-bit value. In a
7140 big-endian file, the result is the same; in a little-endian
7141 file, the two 16-bit halves of the 32 bit value are swapped.
7142 This is so that a disassembler can recognize the jal
7143 instruction.
7144
7145 When doing a final link, R_MIPS16_26 is treated as a 32 bit
7146 instruction stored as two 16-bit values. The addend A is the
7147 contents of the targ26 field. The calculation is the same as
7148 R_MIPS_26. When storing the calculated value, reorder the
7149 immediate value as shown above, and don't forget to store the
7150 value as two 16-bit values.
7151
7152 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
7153 defined as
be3ccd9c 7154
e53bd91b
MM
7155 big-endian:
7156 +--------+----------------------+
7157 | | |
7158 | | targ26-16 |
7159 |31 26|25 0|
7160 +--------+----------------------+
be3ccd9c 7161
e53bd91b
MM
7162 little-endian:
7163 +----------+------+-------------+
7164 | | | |
7165 | sub1 | | sub2 |
7166 |0 9|10 15|16 31|
7167 +----------+--------------------+
7168 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
7169 ((sub1 << 16) | sub2)).
be3ccd9c 7170
e53bd91b 7171 When producing a relocateable object file, the calculation is
9117d219 7172 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
e53bd91b 7173 When producing a fully linked file, the calculation is
9117d219 7174 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
e53bd91b
MM
7175 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
7176
7177 if (!info->relocateable)
7178 /* Shuffle the bits according to the formula above. */
be3ccd9c
KH
7179 value = (((value & 0x1f0000) << 5)
7180 | ((value & 0x3e00000) >> 5)
e53bd91b 7181 | (value & 0xffff));
e53bd91b 7182 }
197b9ca0 7183 else if (r_type == R_MIPS16_GPREL)
b7233c24
MM
7184 {
7185 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
7186 mode. A typical instruction will have a format like this:
7187
7188 +--------------+--------------------------------+
7189 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
7190 +--------------+--------------------------------+
7191 ! Major ! rx ! ry ! Imm 4:0 !
7192 +--------------+--------------------------------+
be3ccd9c 7193
b7233c24
MM
7194 EXTEND is the five bit value 11110. Major is the instruction
7195 opcode.
be3ccd9c 7196
b7233c24
MM
7197 This is handled exactly like R_MIPS_GPREL16, except that the
7198 addend is retrieved and stored as shown in this diagram; that
be3ccd9c 7199 is, the Imm fields above replace the V-rel16 field.
b7233c24 7200
6296902e
MM
7201 All we need to do here is shuffle the bits appropriately. As
7202 above, the two 16-bit halves must be swapped on a
7203 little-endian system. */
b7233c24
MM
7204 value = (((value & 0x7e0) << 16)
7205 | ((value & 0xf800) << 5)
7206 | (value & 0x1f));
7207 }
252b5132 7208
e53bd91b
MM
7209 /* Set the field. */
7210 x |= (value & howto->dst_mask);
252b5132 7211
197b9ca0
MM
7212 /* If required, turn JAL into JALX. */
7213 if (require_jalx)
7214 {
7215 boolean ok;
7216 bfd_vma opcode = x >> 26;
7217 bfd_vma jalx_opcode;
7218
7219 /* Check to see if the opcode is already JAL or JALX. */
7220 if (r_type == R_MIPS16_26)
7221 {
7222 ok = ((opcode == 0x6) || (opcode == 0x7));
7223 jalx_opcode = 0x7;
7224 }
7225 else
7226 {
7227 ok = ((opcode == 0x3) || (opcode == 0x1d));
7228 jalx_opcode = 0x1d;
7229 }
7230
7231 /* If the opcode is not JAL or JALX, there's a problem. */
7232 if (!ok)
7233 {
7234 (*_bfd_error_handler)
7235 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
8f615d07 7236 bfd_archive_filename (input_bfd),
197b9ca0
MM
7237 input_section->name,
7238 (unsigned long) relocation->r_offset);
7239 bfd_set_error (bfd_error_bad_value);
7240 return false;
7241 }
7242
7243 /* Make this the JALX opcode. */
7244 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
7245 }
7246
6296902e
MM
7247 /* Swap the high- and low-order 16 bits on little-endian systems
7248 when doing a MIPS16 relocation. */
197b9ca0 7249 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
6296902e
MM
7250 && bfd_little_endian (input_bfd))
7251 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
be3ccd9c 7252
e53bd91b
MM
7253 /* Put the value into the output. */
7254 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
197b9ca0
MM
7255 return true;
7256}
7257
7258/* Returns true if SECTION is a MIPS16 stub section. */
7259
7260static boolean
7261mips_elf_stub_section_p (abfd, section)
6387d602 7262 bfd *abfd ATTRIBUTE_UNUSED;
197b9ca0
MM
7263 asection *section;
7264{
7265 const char *name = bfd_get_section_name (abfd, section);
7266
7267 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
7268 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
7269 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
7403cb63 7270}
252b5132 7271
7403cb63 7272/* Relocate a MIPS ELF section. */
252b5132 7273
103186c6
MM
7274boolean
7275_bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
7276 contents, relocs, local_syms, local_sections)
7403cb63
MM
7277 bfd *output_bfd;
7278 struct bfd_link_info *info;
7279 bfd *input_bfd;
7280 asection *input_section;
7281 bfd_byte *contents;
7282 Elf_Internal_Rela *relocs;
7283 Elf_Internal_Sym *local_syms;
7284 asection **local_sections;
7285{
31367b81 7286 Elf_Internal_Rela *rel;
103186c6 7287 const Elf_Internal_Rela *relend;
86033394 7288 bfd_vma addend = 0;
7403cb63 7289 boolean use_saved_addend_p = false;
103186c6 7290 struct elf_backend_data *bed;
252b5132 7291
103186c6
MM
7292 bed = get_elf_backend_data (output_bfd);
7293 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
7403cb63
MM
7294 for (rel = relocs; rel < relend; ++rel)
7295 {
7296 const char *name;
7297 bfd_vma value;
7403cb63 7298 reloc_howto_type *howto;
197b9ca0 7299 boolean require_jalx;
31367b81
MM
7300 /* True if the relocation is a RELA relocation, rather than a
7301 REL relocation. */
7302 boolean rela_relocation_p = true;
dc810e39 7303 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
7a65545d 7304 const char * msg = (const char *) NULL;
252b5132 7305
7403cb63 7306 /* Find the relocation howto for this relocation. */
31367b81 7307 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
0af99795
GK
7308 {
7309 /* Some 32-bit code uses R_MIPS_64. In particular, people use
be3ccd9c 7310 64-bit code, but make sure all their addresses are in the
0af99795
GK
7311 lowermost or uppermost 32-bit section of the 64-bit address
7312 space. Thus, when they use an R_MIPS_64 they mean what is
7313 usually meant by R_MIPS_32, with the exception that the
7314 stored value is sign-extended to 64 bits. */
d75bc93d 7315 howto = elf_mips_howto_table_rel + R_MIPS_32;
0af99795
GK
7316
7317 /* On big-endian systems, we need to lie about the position
7318 of the reloc. */
7319 if (bfd_big_endian (input_bfd))
be3ccd9c 7320 rel->r_offset += 4;
0af99795 7321 }
a3c7651d 7322 else
c9b3cbf3 7323 howto = mips_rtype_to_howto (r_type);
252b5132 7324
7403cb63
MM
7325 if (!use_saved_addend_p)
7326 {
7327 Elf_Internal_Shdr *rel_hdr;
7328
7329 /* If these relocations were originally of the REL variety,
7330 we must pull the addend out of the field that will be
7331 relocated. Otherwise, we simply use the contents of the
7332 RELA relocation. To determine which flavor or relocation
7333 this is, we depend on the fact that the INPUT_SECTION's
7334 REL_HDR is read before its REL_HDR2. */
7335 rel_hdr = &elf_section_data (input_section)->rel_hdr;
5f771d47 7336 if ((size_t) (rel - relocs)
d9bc7a44 7337 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7403cb63 7338 rel_hdr = elf_section_data (input_section)->rel_hdr2;
103186c6 7339 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
7403cb63 7340 {
31367b81
MM
7341 /* Note that this is a REL relocation. */
7342 rela_relocation_p = false;
7403cb63 7343
31367b81 7344 /* Get the addend, which is stored in the input file. */
be3ccd9c 7345 addend = mips_elf_obtain_contents (howto,
7403cb63
MM
7346 rel,
7347 input_bfd,
7348 contents);
7349 addend &= howto->src_mask;
7350
7351 /* For some kinds of relocations, the ADDEND is a
7352 combination of the addend stored in two different
7353 relocations. */
6387d602 7354 if (r_type == R_MIPS_HI16
bb2d6cd7 7355 || r_type == R_MIPS_GNU_REL_HI16
6387d602
ILT
7356 || (r_type == R_MIPS_GOT16
7357 && mips_elf_local_relocation_p (input_bfd, rel,
b305ef96 7358 local_sections, false)))
252b5132 7359 {
23b255aa
MM
7360 bfd_vma l;
7361 const Elf_Internal_Rela *lo16_relocation;
7362 reloc_howto_type *lo16_howto;
dc810e39 7363 unsigned int lo;
23b255aa 7364
e7c44218
MM
7365 /* The combined value is the sum of the HI16 addend,
7366 left-shifted by sixteen bits, and the LO16
7367 addend, sign extended. (Usually, the code does
7368 a `lui' of the HI16 value, and then an `addiu' of
be3ccd9c 7369 the LO16 value.)
e7c44218 7370
bb2d6cd7
GK
7371 Scan ahead to find a matching LO16 relocation. */
7372 if (r_type == R_MIPS_GNU_REL_HI16)
7373 lo = R_MIPS_GNU_REL_LO16;
7374 else
7375 lo = R_MIPS_LO16;
be3ccd9c
KH
7376 lo16_relocation
7377 = mips_elf_next_relocation (lo, rel, relend);
23b255aa 7378 if (lo16_relocation == NULL)
7403cb63 7379 return false;
252b5132 7380
23b255aa 7381 /* Obtain the addend kept there. */
bb2d6cd7 7382 lo16_howto = mips_rtype_to_howto (lo);
23b255aa
MM
7383 l = mips_elf_obtain_contents (lo16_howto,
7384 lo16_relocation,
7385 input_bfd, contents);
7386 l &= lo16_howto->src_mask;
e7c44218 7387 l = mips_elf_sign_extend (l, 16);
23b255aa 7388
7403cb63 7389 addend <<= 16;
252b5132 7390
7403cb63 7391 /* Compute the combined addend. */
e7c44218 7392 addend += l;
252b5132 7393 }
b7233c24
MM
7394 else if (r_type == R_MIPS16_GPREL)
7395 {
7396 /* The addend is scrambled in the object file. See
7397 mips_elf_perform_relocation for details on the
7398 format. */
7399 addend = (((addend & 0x1f0000) >> 5)
7400 | ((addend & 0x7e00000) >> 16)
7401 | (addend & 0x1f));
7402 }
252b5132
RH
7403 }
7404 else
7403cb63
MM
7405 addend = rel->r_addend;
7406 }
252b5132 7407
31367b81
MM
7408 if (info->relocateable)
7409 {
7410 Elf_Internal_Sym *sym;
7411 unsigned long r_symndx;
7412
7893e6a2
GK
7413 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd)
7414 && bfd_big_endian (input_bfd))
7415 rel->r_offset -= 4;
7416
31367b81 7417 /* Since we're just relocating, all we need to do is copy
0db63c18
MM
7418 the relocations back out to the object file, unless
7419 they're against a section symbol, in which case we need
7420 to adjust by the section offset, or unless they're GP
7421 relative in which case we need to adjust by the amount
7422 that we're adjusting GP in this relocateable object. */
31367b81 7423
b305ef96
UC
7424 if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections,
7425 false))
f1a5f37e 7426 /* There's nothing to do for non-local relocations. */
31367b81
MM
7427 continue;
7428
be3ccd9c 7429 if (r_type == R_MIPS16_GPREL
0db63c18 7430 || r_type == R_MIPS_GPREL16
0af99795
GK
7431 || r_type == R_MIPS_GPREL32
7432 || r_type == R_MIPS_LITERAL)
0db63c18
MM
7433 addend -= (_bfd_get_gp_value (output_bfd)
7434 - _bfd_get_gp_value (input_bfd));
bb2d6cd7
GK
7435 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26
7436 || r_type == R_MIPS_GNU_REL16_S2)
e7c44218
MM
7437 /* The addend is stored without its two least
7438 significant bits (which are always zero.) In a
7439 non-relocateable link, calculate_relocation will do
7440 this shift; here, we must do it ourselves. */
7441 addend <<= 2;
31367b81 7442
4f2860ca
MM
7443 r_symndx = ELF32_R_SYM (rel->r_info);
7444 sym = local_syms + r_symndx;
7445 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7446 /* Adjust the addend appropriately. */
7447 addend += local_sections[r_symndx]->output_offset;
be3ccd9c 7448
f1a5f37e
MM
7449 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
7450 then we only want to write out the high-order 16 bits.
7451 The subsequent R_MIPS_LO16 will handle the low-order bits. */
bb2d6cd7
GK
7452 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
7453 || r_type == R_MIPS_GNU_REL_HI16)
23b255aa 7454 addend = mips_elf_high (addend);
5a44662b
MM
7455 /* If the relocation is for an R_MIPS_26 relocation, then
7456 the two low-order bits are not stored in the object file;
7457 they are implicitly zero. */
bb2d6cd7
GK
7458 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26
7459 || r_type == R_MIPS_GNU_REL16_S2)
5a44662b 7460 addend >>= 2;
f1a5f37e 7461
31367b81
MM
7462 if (rela_relocation_p)
7463 /* If this is a RELA relocation, just update the addend.
bb2d6cd7 7464 We have to cast away constness for REL. */
31367b81
MM
7465 rel->r_addend = addend;
7466 else
7467 {
7468 /* Otherwise, we have to write the value back out. Note
7469 that we use the source mask, rather than the
7470 destination mask because the place to which we are
7471 writing will be source of the addend in the final
7472 link. */
7473 addend &= howto->src_mask;
7893e6a2
GK
7474
7475 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
7476 /* See the comment above about using R_MIPS_64 in the 32-bit
7477 ABI. Here, we need to update the addend. It would be
7478 possible to get away with just using the R_MIPS_32 reloc
7479 but for endianness. */
7480 {
7481 bfd_vma sign_bits;
7482 bfd_vma low_bits;
7483 bfd_vma high_bits;
be3ccd9c 7484
fc633e5b 7485 if (addend & ((bfd_vma) 1 << 31))
1e738b87 7486#ifdef BFD64
fc633e5b 7487 sign_bits = ((bfd_vma) 1 << 32) - 1;
1e738b87
NC
7488#else
7489 sign_bits = -1;
7490#endif
7893e6a2
GK
7491 else
7492 sign_bits = 0;
be3ccd9c 7493
7893e6a2
GK
7494 /* If we don't know that we have a 64-bit type,
7495 do two separate stores. */
7496 if (bfd_big_endian (input_bfd))
7497 {
7498 /* Store the sign-bits (which are most significant)
7499 first. */
7500 low_bits = sign_bits;
7501 high_bits = addend;
7502 }
7503 else
7504 {
7505 low_bits = addend;
7506 high_bits = sign_bits;
7507 }
be3ccd9c 7508 bfd_put_32 (input_bfd, low_bits,
7893e6a2 7509 contents + rel->r_offset);
be3ccd9c 7510 bfd_put_32 (input_bfd, high_bits,
7893e6a2
GK
7511 contents + rel->r_offset + 4);
7512 continue;
7513 }
7514
31367b81 7515 if (!mips_elf_perform_relocation (info, howto, rel, addend,
a17aee54 7516 input_bfd, input_section,
31367b81
MM
7517 contents, false))
7518 return false;
7519 }
7520
7521 /* Go on to the next relocation. */
7522 continue;
7523 }
7524
7403cb63
MM
7525 /* In the N32 and 64-bit ABIs there may be multiple consecutive
7526 relocations for the same offset. In that case we are
7527 supposed to treat the output of each relocation as the addend
7528 for the next. */
be3ccd9c 7529 if (rel + 1 < relend
103186c6 7530 && rel->r_offset == rel[1].r_offset
b89db8f2 7531 && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
7403cb63
MM
7532 use_saved_addend_p = true;
7533 else
7534 use_saved_addend_p = false;
7535
7536 /* Figure out what value we are supposed to relocate. */
be3ccd9c 7537 switch (mips_elf_calculate_relocation (output_bfd,
7403cb63
MM
7538 input_bfd,
7539 input_section,
7540 info,
7541 rel,
7542 addend,
7543 howto,
7403cb63
MM
7544 local_syms,
7545 local_sections,
7546 &value,
197b9ca0
MM
7547 &name,
7548 &require_jalx))
7403cb63
MM
7549 {
7550 case bfd_reloc_continue:
7551 /* There's nothing to do. */
7552 continue;
252b5132 7553
7403cb63 7554 case bfd_reloc_undefined:
6387d602 7555 /* mips_elf_calculate_relocation already called the
bb2d6cd7 7556 undefined_symbol callback. There's no real point in
97287574
MM
7557 trying to perform the relocation at this point, so we
7558 just skip ahead to the next relocation. */
7559 continue;
252b5132 7560
7403cb63 7561 case bfd_reloc_notsupported:
7a65545d
DN
7562 msg = _("internal error: unsupported relocation error");
7563 info->callbacks->warning
7564 (info, msg, name, input_bfd, input_section, rel->r_offset);
7565 return false;
252b5132 7566
7403cb63
MM
7567 case bfd_reloc_overflow:
7568 if (use_saved_addend_p)
7569 /* Ignore overflow until we reach the last relocation for
7570 a given location. */
7571 ;
6387d602
ILT
7572 else
7573 {
7574 BFD_ASSERT (name != NULL);
7575 if (! ((*info->callbacks->reloc_overflow)
7576 (info, name, howto->name, (bfd_vma) 0,
7577 input_bfd, input_section, rel->r_offset)))
7578 return false;
7579 }
7403cb63 7580 break;
252b5132 7581
7403cb63
MM
7582 case bfd_reloc_ok:
7583 break;
7584
7585 default:
7586 abort ();
7587 break;
252b5132
RH
7588 }
7589
7403cb63
MM
7590 /* If we've got another relocation for the address, keep going
7591 until we reach the last one. */
7592 if (use_saved_addend_p)
252b5132 7593 {
7403cb63
MM
7594 addend = value;
7595 continue;
252b5132 7596 }
7403cb63 7597
31367b81 7598 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
a3c7651d
MM
7599 /* See the comment above about using R_MIPS_64 in the 32-bit
7600 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
7601 that calculated the right value. Now, however, we
7602 sign-extend the 32-bit result to 64-bits, and store it as a
7603 64-bit value. We are especially generous here in that we
7604 go to extreme lengths to support this usage on systems with
7605 only a 32-bit VMA. */
7606 {
a3c7651d
MM
7607 bfd_vma sign_bits;
7608 bfd_vma low_bits;
7609 bfd_vma high_bits;
7610
fc633e5b 7611 if (value & ((bfd_vma) 1 << 31))
1e738b87 7612#ifdef BFD64
fc633e5b 7613 sign_bits = ((bfd_vma) 1 << 32) - 1;
1e738b87
NC
7614#else
7615 sign_bits = -1;
7616#endif
a3c7651d
MM
7617 else
7618 sign_bits = 0;
7619
7893e6a2
GK
7620 /* If we don't know that we have a 64-bit type,
7621 do two separate stores. */
a3c7651d
MM
7622 if (bfd_big_endian (input_bfd))
7623 {
0af99795
GK
7624 /* Undo what we did above. */
7625 rel->r_offset -= 4;
a3c7651d
MM
7626 /* Store the sign-bits (which are most significant)
7627 first. */
7628 low_bits = sign_bits;
7629 high_bits = value;
7630 }
7631 else
7632 {
7633 low_bits = value;
7634 high_bits = sign_bits;
7635 }
be3ccd9c 7636 bfd_put_32 (input_bfd, low_bits,
a3c7651d 7637 contents + rel->r_offset);
be3ccd9c 7638 bfd_put_32 (input_bfd, high_bits,
a3c7651d
MM
7639 contents + rel->r_offset + 4);
7640 continue;
a3c7651d
MM
7641 }
7642
7403cb63 7643 /* Actually perform the relocation. */
be3ccd9c 7644 if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
197b9ca0
MM
7645 input_section, contents,
7646 require_jalx))
7647 return false;
252b5132
RH
7648 }
7649
7650 return true;
7651}
7652
7653/* This hook function is called before the linker writes out a global
7654 symbol. We mark symbols as small common if appropriate. This is
7655 also where we undo the increment of the value for a mips16 symbol. */
7656
103186c6
MM
7657boolean
7658_bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5f771d47
ILT
7659 bfd *abfd ATTRIBUTE_UNUSED;
7660 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7661 const char *name ATTRIBUTE_UNUSED;
252b5132
RH
7662 Elf_Internal_Sym *sym;
7663 asection *input_sec;
7664{
7665 /* If we see a common symbol, which implies a relocatable link, then
7666 if a symbol was small common in an input file, mark it as small
7667 common in the output file. */
7668 if (sym->st_shndx == SHN_COMMON
7669 && strcmp (input_sec->name, ".scommon") == 0)
7670 sym->st_shndx = SHN_MIPS_SCOMMON;
7671
7672 if (sym->st_other == STO_MIPS16
7673 && (sym->st_value & 1) != 0)
7674 --sym->st_value;
7675
7676 return true;
7677}
7678\f
7679/* Functions for the dynamic linker. */
7680
7681/* The name of the dynamic interpreter. This is put in the .interp
7682 section. */
7683
103186c6
MM
7684#define ELF_DYNAMIC_INTERPRETER(abfd) \
7685 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
7686 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
7687 : "/usr/lib/libc.so.1")
252b5132
RH
7688
7689/* Create dynamic sections when linking against a dynamic object. */
7690
103186c6
MM
7691boolean
7692_bfd_mips_elf_create_dynamic_sections (abfd, info)
252b5132
RH
7693 bfd *abfd;
7694 struct bfd_link_info *info;
7695{
7696 struct elf_link_hash_entry *h;
7697 flagword flags;
7698 register asection *s;
7699 const char * const *namep;
7700
7701 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7702 | SEC_LINKER_CREATED | SEC_READONLY);
7703
7704 /* Mips ABI requests the .dynamic section to be read only. */
7705 s = bfd_get_section_by_name (abfd, ".dynamic");
7706 if (s != NULL)
7707 {
7708 if (! bfd_set_section_flags (abfd, s, flags))
7709 return false;
7710 }
7711
7712 /* We need to create .got section. */
7713 if (! mips_elf_create_got_section (abfd, info))
7714 return false;
7715
c6142e5d
MM
7716 /* Create the .msym section on IRIX6. It is used by the dynamic
7717 linker to speed up dynamic relocations, and to avoid computing
7718 the ELF hash for symbols. */
7719 if (IRIX_COMPAT (abfd) == ict_irix6
7720 && !mips_elf_create_msym_section (abfd))
7721 return false;
be3ccd9c 7722
252b5132 7723 /* Create .stub section. */
be3ccd9c 7724 if (bfd_get_section_by_name (abfd,
7403cb63 7725 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
252b5132 7726 {
7403cb63 7727 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
252b5132 7728 if (s == NULL
7403cb63 7729 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
103186c6
MM
7730 || ! bfd_set_section_alignment (abfd, s,
7731 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
252b5132
RH
7732 return false;
7733 }
7734
31a9bdd9 7735 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
252b5132
RH
7736 && !info->shared
7737 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
7738 {
7739 s = bfd_make_section (abfd, ".rld_map");
7740 if (s == NULL
dc810e39 7741 || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
103186c6
MM
7742 || ! bfd_set_section_alignment (abfd, s,
7743 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
252b5132
RH
7744 return false;
7745 }
7746
303f629d
MM
7747 /* On IRIX5, we adjust add some additional symbols and change the
7748 alignments of several sections. There is no ABI documentation
7749 indicating that this is necessary on IRIX6, nor any evidence that
7750 the linker takes such action. */
7751 if (IRIX_COMPAT (abfd) == ict_irix5)
252b5132
RH
7752 {
7753 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7754 {
7755 h = NULL;
7756 if (! (_bfd_generic_link_add_one_symbol
7757 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
7758 (bfd_vma) 0, (const char *) NULL, false,
7759 get_elf_backend_data (abfd)->collect,
7760 (struct bfd_link_hash_entry **) &h)))
7761 return false;
be3ccd9c 7762 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
7763 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7764 h->type = STT_SECTION;
7765
7766 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7767 return false;
7768 }
7769
7770 /* We need to create a .compact_rel section. */
f7cb7d68 7771 if (SGI_COMPAT (abfd))
be3ccd9c
KH
7772 {
7773 if (!mips_elf_create_compact_rel_section (abfd, info))
f7cb7d68 7774 return false;
be3ccd9c 7775 }
252b5132
RH
7776
7777 /* Change aligments of some sections. */
7778 s = bfd_get_section_by_name (abfd, ".hash");
7779 if (s != NULL)
7780 bfd_set_section_alignment (abfd, s, 4);
7781 s = bfd_get_section_by_name (abfd, ".dynsym");
7782 if (s != NULL)
7783 bfd_set_section_alignment (abfd, s, 4);
7784 s = bfd_get_section_by_name (abfd, ".dynstr");
7785 if (s != NULL)
7786 bfd_set_section_alignment (abfd, s, 4);
7787 s = bfd_get_section_by_name (abfd, ".reginfo");
7788 if (s != NULL)
7789 bfd_set_section_alignment (abfd, s, 4);
7790 s = bfd_get_section_by_name (abfd, ".dynamic");
7791 if (s != NULL)
7792 bfd_set_section_alignment (abfd, s, 4);
7793 }
7794
7795 if (!info->shared)
7796 {
7797 h = NULL;
f7cb7d68 7798 if (SGI_COMPAT (abfd))
be3ccd9c
KH
7799 {
7800 if (!(_bfd_generic_link_add_one_symbol
7801 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
7802 (bfd_vma) 0, (const char *) NULL, false,
7803 get_elf_backend_data (abfd)->collect,
7804 (struct bfd_link_hash_entry **) &h)))
f7cb7d68 7805 return false;
be3ccd9c 7806 }
f7cb7d68 7807 else
be3ccd9c
KH
7808 {
7809 /* For normal mips it is _DYNAMIC_LINKING. */
7810 if (!(_bfd_generic_link_add_one_symbol
7811 (info, abfd, "_DYNAMIC_LINKING", BSF_GLOBAL,
7812 bfd_abs_section_ptr, (bfd_vma) 0, (const char *) NULL, false,
7813 get_elf_backend_data (abfd)->collect,
7814 (struct bfd_link_hash_entry **) &h)))
7815 return false;
7816 }
7817 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
7818 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7819 h->type = STT_SECTION;
7820
7821 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7822 return false;
7823
7824 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7825 {
7826 /* __rld_map is a four byte word located in the .data section
7827 and is filled in by the rtld to contain a pointer to
7828 the _r_debug structure. Its symbol value will be set in
7829 mips_elf_finish_dynamic_symbol. */
7830 s = bfd_get_section_by_name (abfd, ".rld_map");
7831 BFD_ASSERT (s != NULL);
7832
7833 h = NULL;
be3ccd9c
KH
7834 if (SGI_COMPAT (abfd))
7835 {
7836 if (!(_bfd_generic_link_add_one_symbol
7837 (info, abfd, "__rld_map", BSF_GLOBAL, s,
7838 (bfd_vma) 0, (const char *) NULL, false,
7839 get_elf_backend_data (abfd)->collect,
7840 (struct bfd_link_hash_entry **) &h)))
7841 return false;
7842 }
7843 else
7844 {
7845 /* For normal mips the symbol is __RLD_MAP. */
7846 if (!(_bfd_generic_link_add_one_symbol
7847 (info, abfd, "__RLD_MAP", BSF_GLOBAL, s,
7848 (bfd_vma) 0, (const char *) NULL, false,
7849 get_elf_backend_data (abfd)->collect,
7850 (struct bfd_link_hash_entry **) &h)))
7851 return false;
7852 }
7853 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
7854 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7855 h->type = STT_OBJECT;
7856
7857 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7858 return false;
7859 }
7860 }
7861
7862 return true;
7863}
7864
7865/* Create the .compact_rel section. */
7866
7867static boolean
7868mips_elf_create_compact_rel_section (abfd, info)
7869 bfd *abfd;
5f771d47 7870 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
7871{
7872 flagword flags;
7873 register asection *s;
7874
7875 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
7876 {
7877 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
7878 | SEC_READONLY);
7879
7880 s = bfd_make_section (abfd, ".compact_rel");
7881 if (s == NULL
7882 || ! bfd_set_section_flags (abfd, s, flags)
103186c6
MM
7883 || ! bfd_set_section_alignment (abfd, s,
7884 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
252b5132
RH
7885 return false;
7886
7887 s->_raw_size = sizeof (Elf32_External_compact_rel);
7888 }
7889
7890 return true;
7891}
7892
be3ccd9c 7893/* Create the .got section to hold the global offset table. */
252b5132
RH
7894
7895static boolean
7896mips_elf_create_got_section (abfd, info)
7897 bfd *abfd;
7898 struct bfd_link_info *info;
7899{
7900 flagword flags;
7901 register asection *s;
7902 struct elf_link_hash_entry *h;
7903 struct mips_got_info *g;
dc810e39 7904 bfd_size_type amt;
252b5132
RH
7905
7906 /* This function may be called more than once. */
103186c6 7907 if (mips_elf_got_section (abfd))
252b5132
RH
7908 return true;
7909
7910 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7911 | SEC_LINKER_CREATED);
7912
7913 s = bfd_make_section (abfd, ".got");
7914 if (s == NULL
7915 || ! bfd_set_section_flags (abfd, s, flags)
7916 || ! bfd_set_section_alignment (abfd, s, 4))
7917 return false;
7918
7919 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
7920 linker script because we don't want to define the symbol if we
7921 are not creating a global offset table. */
7922 h = NULL;
7923 if (! (_bfd_generic_link_add_one_symbol
7924 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
7925 (bfd_vma) 0, (const char *) NULL, false,
7926 get_elf_backend_data (abfd)->collect,
7927 (struct bfd_link_hash_entry **) &h)))
7928 return false;
be3ccd9c 7929 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
7930 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7931 h->type = STT_OBJECT;
7932
7933 if (info->shared
7934 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
7935 return false;
7936
7937 /* The first several global offset table entries are reserved. */
103186c6 7938 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
252b5132 7939
dc810e39
AM
7940 amt = sizeof (struct mips_got_info);
7941 g = (struct mips_got_info *) bfd_alloc (abfd, amt);
252b5132
RH
7942 if (g == NULL)
7943 return false;
7403cb63 7944 g->global_gotsym = NULL;
252b5132
RH
7945 g->local_gotno = MIPS_RESERVED_GOTNO;
7946 g->assigned_gotno = MIPS_RESERVED_GOTNO;
7947 if (elf_section_data (s) == NULL)
7948 {
dc810e39
AM
7949 amt = sizeof (struct bfd_elf_section_data);
7950 s->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
252b5132
RH
7951 if (elf_section_data (s) == NULL)
7952 return false;
7953 }
7954 elf_section_data (s)->tdata = (PTR) g;
be3ccd9c 7955 elf_section_data (s)->this_hdr.sh_flags
7403cb63 7956 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
252b5132
RH
7957
7958 return true;
7959}
7960
c6142e5d
MM
7961/* Returns the .msym section for ABFD, creating it if it does not
7962 already exist. Returns NULL to indicate error. */
7963
7964static asection *
7965mips_elf_create_msym_section (abfd)
7966 bfd *abfd;
7967{
7968 asection *s;
7969
7970 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
be3ccd9c 7971 if (!s)
c6142e5d
MM
7972 {
7973 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7974 if (!s
be3ccd9c 7975 || !bfd_set_section_flags (abfd, s,
c6142e5d
MM
7976 SEC_ALLOC
7977 | SEC_LOAD
7978 | SEC_HAS_CONTENTS
be3ccd9c 7979 | SEC_LINKER_CREATED
c6142e5d 7980 | SEC_READONLY)
103186c6
MM
7981 || !bfd_set_section_alignment (abfd, s,
7982 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
c6142e5d
MM
7983 return NULL;
7984 }
7985
7986 return s;
7987}
7988
103186c6
MM
7989/* Add room for N relocations to the .rel.dyn section in ABFD. */
7990
7991static void
7992mips_elf_allocate_dynamic_relocations (abfd, n)
7993 bfd *abfd;
7994 unsigned int n;
7995{
7996 asection *s;
7997
7998 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
7999 BFD_ASSERT (s != NULL);
be3ccd9c 8000
103186c6
MM
8001 if (s->_raw_size == 0)
8002 {
be3ccd9c 8003 /* Make room for a null element. */
103186c6
MM
8004 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
8005 ++s->reloc_count;
8006 }
8007 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
8008}
8009
252b5132
RH
8010/* Look through the relocs for a section during the first phase, and
8011 allocate space in the global offset table. */
8012
103186c6
MM
8013boolean
8014_bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
252b5132
RH
8015 bfd *abfd;
8016 struct bfd_link_info *info;
8017 asection *sec;
8018 const Elf_Internal_Rela *relocs;
8019{
8020 const char *name;
8021 bfd *dynobj;
8022 Elf_Internal_Shdr *symtab_hdr;
8023 struct elf_link_hash_entry **sym_hashes;
8024 struct mips_got_info *g;
8025 size_t extsymoff;
8026 const Elf_Internal_Rela *rel;
8027 const Elf_Internal_Rela *rel_end;
8028 asection *sgot;
8029 asection *sreloc;
103186c6 8030 struct elf_backend_data *bed;
252b5132
RH
8031
8032 if (info->relocateable)
8033 return true;
8034
8035 dynobj = elf_hash_table (info)->dynobj;
8036 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8037 sym_hashes = elf_sym_hashes (abfd);
8038 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8039
8040 /* Check for the mips16 stub sections. */
8041
8042 name = bfd_get_section_name (abfd, sec);
8043 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
8044 {
8045 unsigned long r_symndx;
8046
8047 /* Look at the relocation information to figure out which symbol
8048 this is for. */
8049
8050 r_symndx = ELF32_R_SYM (relocs->r_info);
8051
8052 if (r_symndx < extsymoff
8053 || sym_hashes[r_symndx - extsymoff] == NULL)
8054 {
8055 asection *o;
8056
8057 /* This stub is for a local symbol. This stub will only be
8058 needed if there is some relocation in this BFD, other
8059 than a 16 bit function call, which refers to this symbol. */
8060 for (o = abfd->sections; o != NULL; o = o->next)
8061 {
8062 Elf_Internal_Rela *sec_relocs;
8063 const Elf_Internal_Rela *r, *rend;
8064
8065 /* We can ignore stub sections when looking for relocs. */
8066 if ((o->flags & SEC_RELOC) == 0
8067 || o->reloc_count == 0
8068 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
8069 sizeof FN_STUB - 1) == 0
8070 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
8071 sizeof CALL_STUB - 1) == 0
8072 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
8073 sizeof CALL_FP_STUB - 1) == 0)
8074 continue;
8075
8076 sec_relocs = (_bfd_elf32_link_read_relocs
8077 (abfd, o, (PTR) NULL,
8078 (Elf_Internal_Rela *) NULL,
8079 info->keep_memory));
8080 if (sec_relocs == NULL)
8081 return false;
8082
8083 rend = sec_relocs + o->reloc_count;
8084 for (r = sec_relocs; r < rend; r++)
8085 if (ELF32_R_SYM (r->r_info) == r_symndx
8086 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
8087 break;
8088
8089 if (! info->keep_memory)
8090 free (sec_relocs);
8091
8092 if (r < rend)
8093 break;
8094 }
8095
8096 if (o == NULL)
8097 {
8098 /* There is no non-call reloc for this stub, so we do
8099 not need it. Since this function is called before
8100 the linker maps input sections to output sections, we
8101 can easily discard it by setting the SEC_EXCLUDE
8102 flag. */
8103 sec->flags |= SEC_EXCLUDE;
8104 return true;
8105 }
8106
8107 /* Record this stub in an array of local symbol stubs for
be3ccd9c 8108 this BFD. */
252b5132
RH
8109 if (elf_tdata (abfd)->local_stubs == NULL)
8110 {
8111 unsigned long symcount;
8112 asection **n;
dc810e39 8113 bfd_size_type amt;
252b5132
RH
8114
8115 if (elf_bad_symtab (abfd))
d9bc7a44 8116 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
252b5132
RH
8117 else
8118 symcount = symtab_hdr->sh_info;
dc810e39
AM
8119 amt = symcount * sizeof (asection *);
8120 n = (asection **) bfd_zalloc (abfd, amt);
252b5132
RH
8121 if (n == NULL)
8122 return false;
8123 elf_tdata (abfd)->local_stubs = n;
8124 }
8125
8126 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8127
8128 /* We don't need to set mips16_stubs_seen in this case.
8129 That flag is used to see whether we need to look through
8130 the global symbol table for stubs. We don't need to set
8131 it here, because we just have a local stub. */
8132 }
8133 else
8134 {
8135 struct mips_elf_link_hash_entry *h;
8136
8137 h = ((struct mips_elf_link_hash_entry *)
8138 sym_hashes[r_symndx - extsymoff]);
8139
8140 /* H is the symbol this stub is for. */
8141
8142 h->fn_stub = sec;
8143 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8144 }
8145 }
8146 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
8147 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
8148 {
8149 unsigned long r_symndx;
8150 struct mips_elf_link_hash_entry *h;
8151 asection **loc;
8152
8153 /* Look at the relocation information to figure out which symbol
8154 this is for. */
8155
8156 r_symndx = ELF32_R_SYM (relocs->r_info);
8157
8158 if (r_symndx < extsymoff
8159 || sym_hashes[r_symndx - extsymoff] == NULL)
8160 {
8161 /* This stub was actually built for a static symbol defined
8162 in the same file. We assume that all static symbols in
8163 mips16 code are themselves mips16, so we can simply
8164 discard this stub. Since this function is called before
8165 the linker maps input sections to output sections, we can
8166 easily discard it by setting the SEC_EXCLUDE flag. */
8167 sec->flags |= SEC_EXCLUDE;
8168 return true;
8169 }
8170
8171 h = ((struct mips_elf_link_hash_entry *)
8172 sym_hashes[r_symndx - extsymoff]);
8173
8174 /* H is the symbol this stub is for. */
8175
8176 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
8177 loc = &h->call_fp_stub;
8178 else
8179 loc = &h->call_stub;
8180
8181 /* If we already have an appropriate stub for this function, we
8182 don't need another one, so we can discard this one. Since
8183 this function is called before the linker maps input sections
8184 to output sections, we can easily discard it by setting the
8185 SEC_EXCLUDE flag. We can also discard this section if we
8186 happen to already know that this is a mips16 function; it is
8187 not necessary to check this here, as it is checked later, but
8188 it is slightly faster to check now. */
8189 if (*loc != NULL || h->root.other == STO_MIPS16)
8190 {
8191 sec->flags |= SEC_EXCLUDE;
8192 return true;
8193 }
8194
8195 *loc = sec;
8196 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8197 }
8198
8199 if (dynobj == NULL)
8200 {
8201 sgot = NULL;
8202 g = NULL;
8203 }
8204 else
8205 {
103186c6 8206 sgot = mips_elf_got_section (dynobj);
252b5132
RH
8207 if (sgot == NULL)
8208 g = NULL;
8209 else
8210 {
8211 BFD_ASSERT (elf_section_data (sgot) != NULL);
8212 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8213 BFD_ASSERT (g != NULL);
8214 }
8215 }
8216
8217 sreloc = NULL;
103186c6
MM
8218 bed = get_elf_backend_data (abfd);
8219 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8220 for (rel = relocs; rel < rel_end; ++rel)
252b5132
RH
8221 {
8222 unsigned long r_symndx;
dc810e39 8223 unsigned int r_type;
252b5132
RH
8224 struct elf_link_hash_entry *h;
8225
8226 r_symndx = ELF32_R_SYM (rel->r_info);
7403cb63 8227 r_type = ELF32_R_TYPE (rel->r_info);
252b5132
RH
8228
8229 if (r_symndx < extsymoff)
8230 h = NULL;
d9bc7a44 8231 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7a3120d9 8232 {
8f615d07
AM
8233 (*_bfd_error_handler)
8234 (_("%s: Malformed reloc detected for section %s"),
8235 bfd_archive_filename (abfd), name);
7a3120d9
NC
8236 bfd_set_error (bfd_error_bad_value);
8237 return false;
8238 }
252b5132
RH
8239 else
8240 {
8241 h = sym_hashes[r_symndx - extsymoff];
8242
8243 /* This may be an indirect symbol created because of a version. */
8244 if (h != NULL)
8245 {
8246 while (h->root.type == bfd_link_hash_indirect)
8247 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8248 }
8249 }
8250
8251 /* Some relocs require a global offset table. */
8252 if (dynobj == NULL || sgot == NULL)
8253 {
7403cb63 8254 switch (r_type)
252b5132
RH
8255 {
8256 case R_MIPS_GOT16:
8257 case R_MIPS_CALL16:
8258 case R_MIPS_CALL_HI16:
8259 case R_MIPS_CALL_LO16:
8260 case R_MIPS_GOT_HI16:
8261 case R_MIPS_GOT_LO16:
435394bf
MM
8262 case R_MIPS_GOT_PAGE:
8263 case R_MIPS_GOT_OFST:
8264 case R_MIPS_GOT_DISP:
252b5132
RH
8265 if (dynobj == NULL)
8266 elf_hash_table (info)->dynobj = dynobj = abfd;
8267 if (! mips_elf_create_got_section (dynobj, info))
8268 return false;
7403cb63 8269 g = mips_elf_got_info (dynobj, &sgot);
252b5132
RH
8270 break;
8271
8272 case R_MIPS_32:
8273 case R_MIPS_REL32:
a3c7651d 8274 case R_MIPS_64:
252b5132
RH
8275 if (dynobj == NULL
8276 && (info->shared || h != NULL)
8277 && (sec->flags & SEC_ALLOC) != 0)
8278 elf_hash_table (info)->dynobj = dynobj = abfd;
8279 break;
8280
8281 default:
8282 break;
8283 }
8284 }
8285
7403cb63
MM
8286 if (!h && (r_type == R_MIPS_CALL_LO16
8287 || r_type == R_MIPS_GOT_LO16
9458945f 8288 || r_type == R_MIPS_GOT_DISP))
252b5132 8289 {
7403cb63 8290 /* We may need a local GOT entry for this relocation. We
97287574
MM
8291 don't count R_MIPS_GOT_PAGE because we can estimate the
8292 maximum number of pages needed by looking at the size of
9117d219
NC
8293 the segment. Similar comments apply to R_MIPS_GOT16 and
8294 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
8295 R_MIPS_CALL_HI16 because these are always followed by an
8296 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
7403cb63
MM
8297
8298 This estimation is very conservative since we can merge
8299 duplicate entries in the GOT. In order to be less
8300 conservative, we could actually build the GOT here,
8301 rather than in relocate_section. */
8302 g->local_gotno++;
a3c7651d 8303 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7403cb63 8304 }
252b5132 8305
7403cb63
MM
8306 switch (r_type)
8307 {
8308 case R_MIPS_CALL16:
252b5132
RH
8309 if (h == NULL)
8310 {
8311 (*_bfd_error_handler)
8312 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
8f615d07 8313 bfd_archive_filename (abfd), (unsigned long) rel->r_offset);
252b5132
RH
8314 bfd_set_error (bfd_error_bad_value);
8315 return false;
8316 }
7403cb63 8317 /* Fall through. */
252b5132 8318
7403cb63
MM
8319 case R_MIPS_CALL_HI16:
8320 case R_MIPS_CALL_LO16:
5a44662b
MM
8321 if (h != NULL)
8322 {
8323 /* This symbol requires a global offset table entry. */
8324 if (!mips_elf_record_global_got_symbol (h, info, g))
8325 return false;
252b5132 8326
5a44662b
MM
8327 /* We need a stub, not a plt entry for the undefined
8328 function. But we record it as if it needs plt. See
8329 elf_adjust_dynamic_symbol in elflink.h. */
8330 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
8331 h->type = STT_FUNC;
8332 }
252b5132
RH
8333 break;
8334
8335 case R_MIPS_GOT16:
8336 case R_MIPS_GOT_HI16:
8337 case R_MIPS_GOT_LO16:
7403cb63 8338 case R_MIPS_GOT_DISP:
252b5132 8339 /* This symbol requires a global offset table entry. */
7403cb63
MM
8340 if (h && !mips_elf_record_global_got_symbol (h, info, g))
8341 return false;
252b5132
RH
8342 break;
8343
8344 case R_MIPS_32:
8345 case R_MIPS_REL32:
a3c7651d 8346 case R_MIPS_64:
252b5132
RH
8347 if ((info->shared || h != NULL)
8348 && (sec->flags & SEC_ALLOC) != 0)
8349 {
8350 if (sreloc == NULL)
8351 {
dc810e39 8352 const char *dname = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
252b5132 8353
dc810e39 8354 sreloc = bfd_get_section_by_name (dynobj, dname);
252b5132
RH
8355 if (sreloc == NULL)
8356 {
dc810e39 8357 sreloc = bfd_make_section (dynobj, dname);
252b5132
RH
8358 if (sreloc == NULL
8359 || ! bfd_set_section_flags (dynobj, sreloc,
8360 (SEC_ALLOC
8361 | SEC_LOAD
8362 | SEC_HAS_CONTENTS
8363 | SEC_IN_MEMORY
8364 | SEC_LINKER_CREATED
8365 | SEC_READONLY))
8366 || ! bfd_set_section_alignment (dynobj, sreloc,
8367 4))
8368 return false;
8369 }
8370 }
43917054 8371#define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
252b5132 8372 if (info->shared)
43917054
L
8373 {
8374 /* When creating a shared object, we must copy these
8375 reloc types into the output file as R_MIPS_REL32
8376 relocs. We make room for this reloc in the
8377 .rel.dyn reloc section. */
8378 mips_elf_allocate_dynamic_relocations (dynobj, 1);
8379 if ((sec->flags & MIPS_READONLY_SECTION)
8380 == MIPS_READONLY_SECTION)
8381 /* We tell the dynamic linker that there are
8382 relocations against the text segment. */
8383 info->flags |= DF_TEXTREL;
8384 }
252b5132
RH
8385 else
8386 {
8387 struct mips_elf_link_hash_entry *hmips;
8388
8389 /* We only need to copy this reloc if the symbol is
8390 defined in a dynamic object. */
8391 hmips = (struct mips_elf_link_hash_entry *) h;
a3c7651d 8392 ++hmips->possibly_dynamic_relocs;
43917054
L
8393 if ((sec->flags & MIPS_READONLY_SECTION)
8394 == MIPS_READONLY_SECTION)
8395 /* We need it to tell the dynamic linker if there
8396 are relocations against the text segment. */
8397 hmips->readonly_reloc = true;
252b5132 8398 }
be3ccd9c 8399
7403cb63
MM
8400 /* Even though we don't directly need a GOT entry for
8401 this symbol, a symbol must have a dynamic symbol
5499724a 8402 table index greater that DT_MIPS_GOTSYM if there are
7403cb63 8403 dynamic relocations against it. */
7b1f1231
MM
8404 if (h != NULL
8405 && !mips_elf_record_global_got_symbol (h, info, g))
7403cb63 8406 return false;
252b5132
RH
8407 }
8408
313ba8d6 8409 if (SGI_COMPAT (abfd))
252b5132
RH
8410 mips_elf_hash_table (info)->compact_rel_size +=
8411 sizeof (Elf32_External_crinfo);
252b5132
RH
8412 break;
8413
8414 case R_MIPS_26:
8415 case R_MIPS_GPREL16:
8416 case R_MIPS_LITERAL:
8417 case R_MIPS_GPREL32:
313ba8d6 8418 if (SGI_COMPAT (abfd))
252b5132
RH
8419 mips_elf_hash_table (info)->compact_rel_size +=
8420 sizeof (Elf32_External_crinfo);
8421 break;
8422
8423 /* This relocation describes the C++ object vtable hierarchy.
8424 Reconstruct it for later use during GC. */
8425 case R_MIPS_GNU_VTINHERIT:
8426 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8427 return false;
8428 break;
8429
8430 /* This relocation describes which C++ vtable entries are actually
8431 used. Record for later use during GC. */
8432 case R_MIPS_GNU_VTENTRY:
8433 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8434 return false;
8435 break;
8436
8437 default:
8438 break;
8439 }
8440
9117d219
NC
8441 /* We must not create a stub for a symbol that has relocations
8442 related to taking the function's address. */
8443 switch (r_type)
8444 {
8445 default:
8446 if (h != NULL)
8447 {
8448 struct mips_elf_link_hash_entry *mh;
8449
8450 mh = (struct mips_elf_link_hash_entry *) h;
8451 mh->no_fn_stub = true;
8452 }
8453 break;
8454 case R_MIPS_CALL16:
8455 case R_MIPS_CALL_HI16:
8456 case R_MIPS_CALL_LO16:
8457 break;
8458 }
8459
252b5132
RH
8460 /* If this reloc is not a 16 bit call, and it has a global
8461 symbol, then we will need the fn_stub if there is one.
be3ccd9c 8462 References from a stub section do not count. */
252b5132 8463 if (h != NULL
7403cb63 8464 && r_type != R_MIPS16_26
252b5132
RH
8465 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
8466 sizeof FN_STUB - 1) != 0
8467 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
8468 sizeof CALL_STUB - 1) != 0
8469 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
8470 sizeof CALL_FP_STUB - 1) != 0)
8471 {
8472 struct mips_elf_link_hash_entry *mh;
8473
8474 mh = (struct mips_elf_link_hash_entry *) h;
8475 mh->need_fn_stub = true;
8476 }
8477 }
8478
8479 return true;
8480}
8481
8482/* Return the section that should be marked against GC for a given
8483 relocation. */
8484
103186c6
MM
8485asection *
8486_bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
252b5132 8487 bfd *abfd;
5f771d47 8488 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
8489 Elf_Internal_Rela *rel;
8490 struct elf_link_hash_entry *h;
8491 Elf_Internal_Sym *sym;
8492{
8493 /* ??? Do mips16 stub sections need to be handled special? */
8494
8495 if (h != NULL)
8496 {
8497 switch (ELF32_R_TYPE (rel->r_info))
8498 {
8499 case R_MIPS_GNU_VTINHERIT:
8500 case R_MIPS_GNU_VTENTRY:
8501 break;
8502
8503 default:
8504 switch (h->root.type)
8505 {
8506 case bfd_link_hash_defined:
8507 case bfd_link_hash_defweak:
8508 return h->root.u.def.section;
8509
8510 case bfd_link_hash_common:
8511 return h->root.u.c.p->section;
8512
8513 default:
8514 break;
8515 }
8516 }
8517 }
8518 else
8519 {
9ad5cbcf 8520 return bfd_section_from_elf_index (abfd, sym->st_shndx);
252b5132
RH
8521 }
8522
8523 return NULL;
8524}
8525
8526/* Update the got entry reference counts for the section being removed. */
8527
103186c6
MM
8528boolean
8529_bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
5f771d47
ILT
8530 bfd *abfd ATTRIBUTE_UNUSED;
8531 struct bfd_link_info *info ATTRIBUTE_UNUSED;
8532 asection *sec ATTRIBUTE_UNUSED;
8533 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
252b5132
RH
8534{
8535#if 0
8536 Elf_Internal_Shdr *symtab_hdr;
8537 struct elf_link_hash_entry **sym_hashes;
8538 bfd_signed_vma *local_got_refcounts;
8539 const Elf_Internal_Rela *rel, *relend;
8540 unsigned long r_symndx;
8541 struct elf_link_hash_entry *h;
8542
8543 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8544 sym_hashes = elf_sym_hashes (abfd);
8545 local_got_refcounts = elf_local_got_refcounts (abfd);
8546
8547 relend = relocs + sec->reloc_count;
8548 for (rel = relocs; rel < relend; rel++)
8549 switch (ELF32_R_TYPE (rel->r_info))
8550 {
8551 case R_MIPS_GOT16:
8552 case R_MIPS_CALL16:
8553 case R_MIPS_CALL_HI16:
8554 case R_MIPS_CALL_LO16:
8555 case R_MIPS_GOT_HI16:
8556 case R_MIPS_GOT_LO16:
8557 /* ??? It would seem that the existing MIPS code does no sort
8558 of reference counting or whatnot on its GOT and PLT entries,
8559 so it is not possible to garbage collect them at this time. */
be3ccd9c 8560 break;
252b5132
RH
8561
8562 default:
8563 break;
8564 }
8565#endif
8566
8567 return true;
8568}
8569
8a20f077
UC
8570/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
8571 hiding the old indirect symbol. Process additional relocation
58e3d09d 8572 information. Also called for weakdefs, in which case we just let
8ea52284 8573 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
8a20f077 8574
9e80ff3a 8575static void
8a20f077
UC
8576_bfd_mips_elf_copy_indirect_symbol (dir, ind)
8577 struct elf_link_hash_entry *dir, *ind;
8578{
8579 struct mips_elf_link_hash_entry *dirmips, *indmips;
8580
8581 _bfd_elf_link_hash_copy_indirect (dir, ind);
8582
1e370bd2 8583 if (ind->root.type != bfd_link_hash_indirect)
58e3d09d
AM
8584 return;
8585
8a20f077
UC
8586 dirmips = (struct mips_elf_link_hash_entry *) dir;
8587 indmips = (struct mips_elf_link_hash_entry *) ind;
8588 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
43917054
L
8589 if (indmips->readonly_reloc)
8590 dirmips->readonly_reloc = true;
8a20f077
UC
8591 if (dirmips->min_dyn_reloc_index == 0
8592 || (indmips->min_dyn_reloc_index != 0
be3ccd9c 8593 && indmips->min_dyn_reloc_index < dirmips->min_dyn_reloc_index))
8a20f077 8594 dirmips->min_dyn_reloc_index = indmips->min_dyn_reloc_index;
9117d219
NC
8595 if (indmips->no_fn_stub)
8596 dirmips->no_fn_stub = true;
8a20f077
UC
8597}
8598
252b5132
RH
8599/* Adjust a symbol defined by a dynamic object and referenced by a
8600 regular object. The current definition is in some section of the
8601 dynamic object, but we're not including those sections. We have to
8602 change the definition to something the rest of the link can
8603 understand. */
8604
103186c6
MM
8605boolean
8606_bfd_mips_elf_adjust_dynamic_symbol (info, h)
252b5132
RH
8607 struct bfd_link_info *info;
8608 struct elf_link_hash_entry *h;
8609{
8610 bfd *dynobj;
8611 struct mips_elf_link_hash_entry *hmips;
8612 asection *s;
8613
8614 dynobj = elf_hash_table (info)->dynobj;
8615
8616 /* Make sure we know what is going on here. */
8617 BFD_ASSERT (dynobj != NULL
8618 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
8619 || h->weakdef != NULL
8620 || ((h->elf_link_hash_flags
8621 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
8622 && (h->elf_link_hash_flags
8623 & ELF_LINK_HASH_REF_REGULAR) != 0
8624 && (h->elf_link_hash_flags
8625 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
8626
8627 /* If this symbol is defined in a dynamic object, we need to copy
8628 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
8629 file. */
8630 hmips = (struct mips_elf_link_hash_entry *) h;
8631 if (! info->relocateable
a3c7651d 8632 && hmips->possibly_dynamic_relocs != 0
2bab9785 8633 && (h->root.type == bfd_link_hash_defweak
dc810e39 8634 || (h->elf_link_hash_flags
2bab9785 8635 & ELF_LINK_HASH_DEF_REGULAR) == 0))
43917054
L
8636 {
8637 mips_elf_allocate_dynamic_relocations (dynobj,
8638 hmips->possibly_dynamic_relocs);
8639 if (hmips->readonly_reloc)
8640 /* We tell the dynamic linker that there are relocations
8641 against the text segment. */
8642 info->flags |= DF_TEXTREL;
8643 }
252b5132 8644
9117d219
NC
8645 /* For a function, create a stub, if allowed. */
8646 if (! hmips->no_fn_stub
8647 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
252b5132
RH
8648 {
8649 if (! elf_hash_table (info)->dynamic_sections_created)
8650 return true;
8651
8652 /* If this symbol is not defined in a regular file, then set
8653 the symbol to the stub location. This is required to make
8654 function pointers compare as equal between the normal
8655 executable and the shared library. */
8656 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
8657 {
8658 /* We need .stub section. */
be3ccd9c 8659 s = bfd_get_section_by_name (dynobj,
303f629d 8660 MIPS_ELF_STUB_SECTION_NAME (dynobj));
252b5132
RH
8661 BFD_ASSERT (s != NULL);
8662
8663 h->root.u.def.section = s;
8664 h->root.u.def.value = s->_raw_size;
8665
8666 /* XXX Write this stub address somewhere. */
8667 h->plt.offset = s->_raw_size;
8668
8669 /* Make room for this stub code. */
8670 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8671
8672 /* The last half word of the stub will be filled with the index
8673 of this symbol in .dynsym section. */
8674 return true;
8675 }
8676 }
f7cb7d68 8677 else if ((h->type == STT_FUNC)
be3ccd9c 8678 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
f7cb7d68
UC
8679 {
8680 /* This will set the entry for this symbol in the GOT to 0, and
be3ccd9c 8681 the dynamic linker will take care of this. */
f7cb7d68
UC
8682 h->root.u.def.value = 0;
8683 return true;
8684 }
252b5132
RH
8685
8686 /* If this is a weak symbol, and there is a real definition, the
8687 processor independent code will have arranged for us to see the
8688 real definition first, and we can just use the same value. */
8689 if (h->weakdef != NULL)
8690 {
8691 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
8692 || h->weakdef->root.type == bfd_link_hash_defweak);
8693 h->root.u.def.section = h->weakdef->root.u.def.section;
8694 h->root.u.def.value = h->weakdef->root.u.def.value;
8695 return true;
8696 }
8697
8698 /* This is a reference to a symbol defined by a dynamic object which
8699 is not a function. */
8700
8701 return true;
8702}
8703
8704/* This function is called after all the input files have been read,
8705 and the input sections have been assigned to output sections. We
8706 check for any mips16 stub sections that we can discard. */
8707
8708static boolean mips_elf_check_mips16_stubs
8709 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
8710
103186c6
MM
8711boolean
8712_bfd_mips_elf_always_size_sections (output_bfd, info)
252b5132
RH
8713 bfd *output_bfd;
8714 struct bfd_link_info *info;
8715{
8716 asection *ri;
8717
8718 /* The .reginfo section has a fixed size. */
8719 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8720 if (ri != NULL)
dc810e39
AM
8721 bfd_set_section_size (output_bfd, ri,
8722 (bfd_size_type) sizeof (Elf32_External_RegInfo));
252b5132
RH
8723
8724 if (info->relocateable
8725 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
8726 return true;
8727
8728 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8729 mips_elf_check_mips16_stubs,
8730 (PTR) NULL);
8731
8732 return true;
8733}
8734
8735/* Check the mips16 stubs for a particular symbol, and see if we can
8736 discard them. */
8737
252b5132
RH
8738static boolean
8739mips_elf_check_mips16_stubs (h, data)
8740 struct mips_elf_link_hash_entry *h;
5f771d47 8741 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
8742{
8743 if (h->fn_stub != NULL
8744 && ! h->need_fn_stub)
8745 {
8746 /* We don't need the fn_stub; the only references to this symbol
8747 are 16 bit calls. Clobber the size to 0 to prevent it from
8748 being included in the link. */
8749 h->fn_stub->_raw_size = 0;
8750 h->fn_stub->_cooked_size = 0;
be3ccd9c 8751 h->fn_stub->flags &= ~SEC_RELOC;
252b5132
RH
8752 h->fn_stub->reloc_count = 0;
8753 h->fn_stub->flags |= SEC_EXCLUDE;
8754 }
8755
8756 if (h->call_stub != NULL
8757 && h->root.other == STO_MIPS16)
8758 {
8759 /* We don't need the call_stub; this is a 16 bit function, so
8760 calls from other 16 bit functions are OK. Clobber the size
8761 to 0 to prevent it from being included in the link. */
8762 h->call_stub->_raw_size = 0;
8763 h->call_stub->_cooked_size = 0;
be3ccd9c 8764 h->call_stub->flags &= ~SEC_RELOC;
252b5132
RH
8765 h->call_stub->reloc_count = 0;
8766 h->call_stub->flags |= SEC_EXCLUDE;
8767 }
8768
8769 if (h->call_fp_stub != NULL
8770 && h->root.other == STO_MIPS16)
8771 {
8772 /* We don't need the call_stub; this is a 16 bit function, so
8773 calls from other 16 bit functions are OK. Clobber the size
8774 to 0 to prevent it from being included in the link. */
8775 h->call_fp_stub->_raw_size = 0;
8776 h->call_fp_stub->_cooked_size = 0;
be3ccd9c 8777 h->call_fp_stub->flags &= ~SEC_RELOC;
252b5132
RH
8778 h->call_fp_stub->reloc_count = 0;
8779 h->call_fp_stub->flags |= SEC_EXCLUDE;
8780 }
8781
8782 return true;
8783}
8784
8785/* Set the sizes of the dynamic sections. */
8786
103186c6
MM
8787boolean
8788_bfd_mips_elf_size_dynamic_sections (output_bfd, info)
252b5132
RH
8789 bfd *output_bfd;
8790 struct bfd_link_info *info;
8791{
8792 bfd *dynobj;
8793 asection *s;
8794 boolean reltext;
7a12753d 8795 struct mips_got_info *g = NULL;
252b5132
RH
8796
8797 dynobj = elf_hash_table (info)->dynobj;
8798 BFD_ASSERT (dynobj != NULL);
8799
8800 if (elf_hash_table (info)->dynamic_sections_created)
8801 {
8802 /* Set the contents of the .interp section to the interpreter. */
8803 if (! info->shared)
8804 {
8805 s = bfd_get_section_by_name (dynobj, ".interp");
8806 BFD_ASSERT (s != NULL);
be3ccd9c 8807 s->_raw_size
303f629d 8808 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
be3ccd9c 8809 s->contents
7403cb63 8810 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
252b5132
RH
8811 }
8812 }
8813
252b5132
RH
8814 /* The check_relocs and adjust_dynamic_symbol entry points have
8815 determined the sizes of the various dynamic sections. Allocate
8816 memory for them. */
8817 reltext = false;
8818 for (s = dynobj->sections; s != NULL; s = s->next)
8819 {
8820 const char *name;
8821 boolean strip;
8822
8823 /* It's OK to base decisions on the section name, because none
8824 of the dynobj section names depend upon the input files. */
8825 name = bfd_get_section_name (dynobj, s);
8826
8827 if ((s->flags & SEC_LINKER_CREATED) == 0)
8828 continue;
8829
8830 strip = false;
8831
8832 if (strncmp (name, ".rel", 4) == 0)
8833 {
8834 if (s->_raw_size == 0)
8835 {
8836 /* We only strip the section if the output section name
8837 has the same name. Otherwise, there might be several
8838 input sections for this output section. FIXME: This
8839 code is probably not needed these days anyhow, since
8840 the linker now does not create empty output sections. */
8841 if (s->output_section != NULL
8842 && strcmp (name,
8843 bfd_get_section_name (s->output_section->owner,
8844 s->output_section)) == 0)
8845 strip = true;
8846 }
8847 else
8848 {
8849 const char *outname;
8850 asection *target;
8851
8852 /* If this relocation section applies to a read only
8853 section, then we probably need a DT_TEXTREL entry.
8854 If the relocation section is .rel.dyn, we always
8855 assert a DT_TEXTREL entry rather than testing whether
8856 there exists a relocation to a read only section or
8857 not. */
8858 outname = bfd_get_section_name (output_bfd,
8859 s->output_section);
8860 target = bfd_get_section_by_name (output_bfd, outname + 4);
8861 if ((target != NULL
8862 && (target->flags & SEC_READONLY) != 0
8863 && (target->flags & SEC_ALLOC) != 0)
be3ccd9c 8864 || strcmp (outname,
103186c6 8865 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
252b5132
RH
8866 reltext = true;
8867
8868 /* We use the reloc_count field as a counter if we need
8869 to copy relocs into the output file. */
be3ccd9c 8870 if (strcmp (name,
103186c6 8871 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
252b5132
RH
8872 s->reloc_count = 0;
8873 }
8874 }
8875 else if (strncmp (name, ".got", 4) == 0)
8876 {
8877 int i;
be3ccd9c
KH
8878 bfd_size_type loadable_size = 0;
8879 bfd_size_type local_gotno;
d1cf510e 8880 bfd *sub;
252b5132 8881
be3ccd9c 8882 BFD_ASSERT (elf_section_data (s) != NULL);
252b5132 8883 g = (struct mips_got_info *) elf_section_data (s)->tdata;
be3ccd9c
KH
8884 BFD_ASSERT (g != NULL);
8885
8886 /* Calculate the total loadable size of the output. That
8887 will give us the maximum number of GOT_PAGE entries
8888 required. */
8889 for (sub = info->input_bfds; sub; sub = sub->link_next)
8890 {
8891 asection *subsection;
8892
8893 for (subsection = sub->sections;
8894 subsection;
8895 subsection = subsection->next)
8896 {
8897 if ((subsection->flags & SEC_ALLOC) == 0)
8898 continue;
dc810e39
AM
8899 loadable_size += ((subsection->_raw_size + 0xf)
8900 &~ (bfd_size_type) 0xf);
be3ccd9c
KH
8901 }
8902 }
8903 loadable_size += MIPS_FUNCTION_STUB_SIZE;
8904
8905 /* Assume there are two loadable segments consisting of
8906 contiguous sections. Is 5 enough? */
8907 local_gotno = (loadable_size >> 16) + 5;
9458945f
MM
8908 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8909 /* It's possible we will need GOT_PAGE entries as well as
8910 GOT16 entries. Often, these will be able to share GOT
8911 entries, but not always. */
8912 local_gotno *= 2;
8913
be3ccd9c
KH
8914 g->local_gotno += local_gotno;
8915 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
7403cb63 8916
be3ccd9c
KH
8917 /* There has to be a global GOT entry for every symbol with
8918 a dynamic symbol table index of DT_MIPS_GOTSYM or
8919 higher. Therefore, it make sense to put those symbols
8920 that need GOT entries at the end of the symbol table. We
8921 do that here. */
b3be9b46 8922 if (!mips_elf_sort_hash_table (info, 1))
7403cb63
MM
8923 return false;
8924
8b237a89
MM
8925 if (g->global_gotsym != NULL)
8926 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
8927 else
8928 /* If there are no global symbols, or none requiring
8929 relocations, then GLOBAL_GOTSYM will be NULL. */
8930 i = 0;
b3be9b46 8931 g->global_gotno = i;
103186c6 8932 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
252b5132 8933 }
303f629d 8934 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
252b5132
RH
8935 {
8936 /* Irix rld assumes that the function stub isn't at the end
8937 of .text section. So put a dummy. XXX */
8938 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8939 }
8940 else if (! info->shared
8941 && ! mips_elf_hash_table (info)->use_rld_obj_head
8942 && strncmp (name, ".rld_map", 8) == 0)
8943 {
8944 /* We add a room for __rld_map. It will be filled in by the
8945 rtld to contain a pointer to the _r_debug structure. */
8946 s->_raw_size += 4;
8947 }
8948 else if (SGI_COMPAT (output_bfd)
8949 && strncmp (name, ".compact_rel", 12) == 0)
8950 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
c6142e5d
MM
8951 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
8952 == 0)
be3ccd9c 8953 s->_raw_size = (sizeof (Elf32_External_Msym)
c6142e5d
MM
8954 * (elf_hash_table (info)->dynsymcount
8955 + bfd_count_sections (output_bfd)));
252b5132
RH
8956 else if (strncmp (name, ".init", 5) != 0)
8957 {
8958 /* It's not one of our sections, so don't allocate space. */
8959 continue;
8960 }
8961
8962 if (strip)
8963 {
7f8d5fc9 8964 _bfd_strip_section_from_output (info, s);
252b5132
RH
8965 continue;
8966 }
8967
8968 /* Allocate memory for the section contents. */
303f629d 8969 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
252b5132
RH
8970 if (s->contents == NULL && s->_raw_size != 0)
8971 {
8972 bfd_set_error (bfd_error_no_memory);
8973 return false;
8974 }
252b5132
RH
8975 }
8976
8977 if (elf_hash_table (info)->dynamic_sections_created)
8978 {
8979 /* Add some entries to the .dynamic section. We fill in the
8980 values later, in elf_mips_finish_dynamic_sections, but we
8981 must add the entries now so that we get the correct size for
8982 the .dynamic section. The DT_DEBUG entry is filled in by the
8983 dynamic linker and used by the debugger. */
8984 if (! info->shared)
8985 {
be3ccd9c
KH
8986 /* SGI object has the equivalence of DT_DEBUG in the
8987 DT_MIPS_RLD_MAP entry. */
8988 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8989 return false;
8990 if (!SGI_COMPAT (output_bfd))
8991 {
8992 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8993 return false;
8994 }
8995 }
f7cb7d68 8996 else
be3ccd9c
KH
8997 {
8998 /* Shared libraries on traditional mips have DT_DEBUG. */
8999 if (!SGI_COMPAT (output_bfd))
9000 {
9001 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9002 return false;
9003 }
9004 }
43917054 9005
be3ccd9c 9006 if (reltext && SGI_COMPAT (output_bfd))
43917054
L
9007 info->flags |= DF_TEXTREL;
9008
9009 if ((info->flags & DF_TEXTREL) != 0)
252b5132 9010 {
103186c6 9011 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
252b5132
RH
9012 return false;
9013 }
9014
103186c6 9015 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
252b5132
RH
9016 return false;
9017
103186c6
MM
9018 if (bfd_get_section_by_name (dynobj,
9019 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
252b5132 9020 {
103186c6 9021 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
252b5132
RH
9022 return false;
9023
103186c6 9024 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
252b5132
RH
9025 return false;
9026
103186c6 9027 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
252b5132
RH
9028 return false;
9029 }
9030
f7cb7d68 9031 if (SGI_COMPAT (output_bfd))
be3ccd9c
KH
9032 {
9033 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
f7cb7d68 9034 return false;
be3ccd9c 9035 }
252b5132 9036
f7cb7d68 9037 if (SGI_COMPAT (output_bfd))
be3ccd9c
KH
9038 {
9039 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
f7cb7d68 9040 return false;
be3ccd9c 9041 }
252b5132
RH
9042
9043 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
9044 {
103186c6 9045 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
252b5132
RH
9046 return false;
9047
9048 s = bfd_get_section_by_name (dynobj, ".liblist");
9049 BFD_ASSERT (s != NULL);
9050
103186c6 9051 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
252b5132
RH
9052 return false;
9053 }
9054
103186c6 9055 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
252b5132
RH
9056 return false;
9057
103186c6 9058 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
252b5132
RH
9059 return false;
9060
9061#if 0
9062 /* Time stamps in executable files are a bad idea. */
103186c6 9063 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
252b5132
RH
9064 return false;
9065#endif
9066
9067#if 0 /* FIXME */
103186c6 9068 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
252b5132
RH
9069 return false;
9070#endif
9071
9072#if 0 /* FIXME */
103186c6 9073 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
252b5132
RH
9074 return false;
9075#endif
9076
103186c6 9077 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
252b5132
RH
9078 return false;
9079
103186c6 9080 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
252b5132
RH
9081 return false;
9082
103186c6 9083 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
252b5132
RH
9084 return false;
9085
103186c6 9086 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
252b5132
RH
9087 return false;
9088
5499724a 9089 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
252b5132
RH
9090 return false;
9091
7403cb63 9092 if (IRIX_COMPAT (dynobj) == ict_irix5
103186c6 9093 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
252b5132
RH
9094 return false;
9095
7403cb63 9096 if (IRIX_COMPAT (dynobj) == ict_irix6
be3ccd9c 9097 && (bfd_get_section_by_name
7403cb63 9098 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
103186c6 9099 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
7403cb63 9100 return false;
c6142e5d 9101
be3ccd9c 9102 if (bfd_get_section_by_name (dynobj,
c6142e5d 9103 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
103186c6 9104 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
c6142e5d 9105 return false;
252b5132
RH
9106 }
9107
252b5132
RH
9108 return true;
9109}
9110
7403cb63
MM
9111/* If NAME is one of the special IRIX6 symbols defined by the linker,
9112 adjust it appropriately now. */
9113
9114static void
9115mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
5f771d47 9116 bfd *abfd ATTRIBUTE_UNUSED;
7403cb63
MM
9117 const char *name;
9118 Elf_Internal_Sym *sym;
9119{
9120 /* The linker script takes care of providing names and values for
9121 these, but we must place them into the right sections. */
9122 static const char* const text_section_symbols[] = {
9123 "_ftext",
9124 "_etext",
9125 "__dso_displacement",
9126 "__elf_header",
9127 "__program_header_table",
9128 NULL
9129 };
9130
9131 static const char* const data_section_symbols[] = {
9132 "_fdata",
9133 "_edata",
9134 "_end",
9135 "_fbss",
9136 NULL
9137 };
9138
9139 const char* const *p;
9140 int i;
9141
9142 for (i = 0; i < 2; ++i)
be3ccd9c 9143 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7403cb63
MM
9144 *p;
9145 ++p)
9146 if (strcmp (*p, name) == 0)
9147 {
9148 /* All of these symbols are given type STT_SECTION by the
9149 IRIX6 linker. */
9150 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
be3ccd9c 9151
7403cb63
MM
9152 /* The IRIX linker puts these symbols in special sections. */
9153 if (i == 0)
9154 sym->st_shndx = SHN_MIPS_TEXT;
9155 else
9156 sym->st_shndx = SHN_MIPS_DATA;
be3ccd9c 9157
7403cb63
MM
9158 break;
9159 }
9160}
9161
252b5132
RH
9162/* Finish up dynamic symbol handling. We set the contents of various
9163 dynamic sections here. */
9164
103186c6
MM
9165boolean
9166_bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
252b5132
RH
9167 bfd *output_bfd;
9168 struct bfd_link_info *info;
9169 struct elf_link_hash_entry *h;
9170 Elf_Internal_Sym *sym;
9171{
9172 bfd *dynobj;
9173 bfd_vma gval;
9174 asection *sgot;
c6142e5d 9175 asection *smsym;
252b5132
RH
9176 struct mips_got_info *g;
9177 const char *name;
c6142e5d 9178 struct mips_elf_link_hash_entry *mh;
252b5132
RH
9179
9180 dynobj = elf_hash_table (info)->dynobj;
9181 gval = sym->st_value;
c6142e5d 9182 mh = (struct mips_elf_link_hash_entry *) h;
252b5132
RH
9183
9184 if (h->plt.offset != (bfd_vma) -1)
9185 {
9186 asection *s;
9187 bfd_byte *p;
9188 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
9189
9190 /* This symbol has a stub. Set it up. */
9191
9192 BFD_ASSERT (h->dynindx != -1);
9193
be3ccd9c 9194 s = bfd_get_section_by_name (dynobj,
303f629d 9195 MIPS_ELF_STUB_SECTION_NAME (dynobj));
252b5132
RH
9196 BFD_ASSERT (s != NULL);
9197
9198 /* Fill the stub. */
9199 p = stub;
dc810e39 9200 bfd_put_32 (output_bfd, (bfd_vma) STUB_LW (output_bfd), p);
252b5132 9201 p += 4;
dc810e39 9202 bfd_put_32 (output_bfd, (bfd_vma) STUB_MOVE (output_bfd), p);
252b5132
RH
9203 p += 4;
9204
9205 /* FIXME: Can h->dynindex be more than 64K? */
9206 if (h->dynindx & 0xffff0000)
9207 return false;
9208
dc810e39 9209 bfd_put_32 (output_bfd, (bfd_vma) STUB_JALR, p);
252b5132 9210 p += 4;
dc810e39 9211 bfd_put_32 (output_bfd, (bfd_vma) STUB_LI16 (output_bfd) + h->dynindx, p);
252b5132
RH
9212
9213 BFD_ASSERT (h->plt.offset <= s->_raw_size);
9214 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
9215
9216 /* Mark the symbol as undefined. plt.offset != -1 occurs
9217 only for the referenced symbol. */
9218 sym->st_shndx = SHN_UNDEF;
9219
9220 /* The run-time linker uses the st_value field of the symbol
9221 to reset the global offset table entry for this external
9222 to its stub address when unlinking a shared object. */
9223 gval = s->output_section->vma + s->output_offset + h->plt.offset;
9224 sym->st_value = gval;
9225 }
9226
b305ef96
UC
9227 BFD_ASSERT (h->dynindx != -1
9228 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0);
252b5132 9229
103186c6 9230 sgot = mips_elf_got_section (dynobj);
252b5132
RH
9231 BFD_ASSERT (sgot != NULL);
9232 BFD_ASSERT (elf_section_data (sgot) != NULL);
9233 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
9234 BFD_ASSERT (g != NULL);
9235
7403cb63
MM
9236 /* Run through the global symbol table, creating GOT entries for all
9237 the symbols that need them. */
8b237a89
MM
9238 if (g->global_gotsym != NULL
9239 && h->dynindx >= g->global_gotsym->dynindx)
252b5132 9240 {
7403cb63
MM
9241 bfd_vma offset;
9242 bfd_vma value;
252b5132 9243
7403cb63
MM
9244 if (sym->st_value)
9245 value = sym->st_value;
9246 else
be3ccd9c
KH
9247 {
9248 /* For an entity defined in a shared object, this will be
9249 NULL. (For functions in shared objects for
9250 which we have created stubs, ST_VALUE will be non-NULL.
9251 That's because such the functions are now no longer defined
9252 in a shared object.) */
9253
9254 if (info->shared && h->root.type == bfd_link_hash_undefined)
9255 value = 0;
9256 else
9257 value = h->root.u.def.value;
9258 }
7403cb63 9259 offset = mips_elf_global_got_index (dynobj, h);
103186c6 9260 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
252b5132
RH
9261 }
9262
c6142e5d 9263 /* Create a .msym entry, if appropriate. */
be3ccd9c 9264 smsym = bfd_get_section_by_name (dynobj,
c6142e5d
MM
9265 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
9266 if (smsym)
9267 {
9268 Elf32_Internal_Msym msym;
9269
9270 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
9271 /* It is undocumented what the `1' indicates, but IRIX6 uses
9272 this value. */
9273 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
be3ccd9c 9274 bfd_mips_elf_swap_msym_out
c6142e5d
MM
9275 (dynobj, &msym,
9276 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
9277 }
9278
252b5132
RH
9279 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9280 name = h->root.root.string;
9281 if (strcmp (name, "_DYNAMIC") == 0
9282 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
9283 sym->st_shndx = SHN_ABS;
f7cb7d68 9284 else if (strcmp (name, "_DYNAMIC_LINK") == 0
be3ccd9c 9285 || strcmp (name, "_DYNAMIC_LINKING") == 0)
252b5132
RH
9286 {
9287 sym->st_shndx = SHN_ABS;
9288 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9289 sym->st_value = 1;
9290 }
f7cb7d68
UC
9291 else if (strcmp (name, "_gp_disp") == 0)
9292 {
9293 sym->st_shndx = SHN_ABS;
9294 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9295 sym->st_value = elf_gp (output_bfd);
9296 }
252b5132
RH
9297 else if (SGI_COMPAT (output_bfd))
9298 {
f7cb7d68 9299 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
be3ccd9c 9300 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
252b5132
RH
9301 {
9302 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9303 sym->st_other = STO_PROTECTED;
9304 sym->st_value = 0;
9305 sym->st_shndx = SHN_MIPS_DATA;
9306 }
9307 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9308 {
9309 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9310 sym->st_other = STO_PROTECTED;
9311 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9312 sym->st_shndx = SHN_ABS;
9313 }
9314 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9315 {
9316 if (h->type == STT_FUNC)
9317 sym->st_shndx = SHN_MIPS_TEXT;
9318 else if (h->type == STT_OBJECT)
9319 sym->st_shndx = SHN_MIPS_DATA;
9320 }
9321 }
9322
7403cb63
MM
9323 /* Handle the IRIX6-specific symbols. */
9324 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9325 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9326
f7cb7d68 9327 if (! info->shared)
252b5132
RH
9328 {
9329 if (! mips_elf_hash_table (info)->use_rld_obj_head
31a9bdd9
UC
9330 && (strcmp (name, "__rld_map") == 0
9331 || strcmp (name, "__RLD_MAP") == 0))
252b5132
RH
9332 {
9333 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9334 BFD_ASSERT (s != NULL);
9335 sym->st_value = s->output_section->vma + s->output_offset;
9336 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
9337 if (mips_elf_hash_table (info)->rld_value == 0)
9338 mips_elf_hash_table (info)->rld_value = sym->st_value;
9339 }
9340 else if (mips_elf_hash_table (info)->use_rld_obj_head
9341 && strcmp (name, "__rld_obj_head") == 0)
9342 {
303f629d 9343 /* IRIX6 does not use a .rld_map section. */
f7cb7d68
UC
9344 if (IRIX_COMPAT (output_bfd) == ict_irix5
9345 || IRIX_COMPAT (output_bfd) == ict_none)
be3ccd9c 9346 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
303f629d 9347 != NULL);
252b5132
RH
9348 mips_elf_hash_table (info)->rld_value = sym->st_value;
9349 }
9350 }
9351
9352 /* If this is a mips16 symbol, force the value to be even. */
9353 if (sym->st_other == STO_MIPS16
9354 && (sym->st_value & 1) != 0)
9355 --sym->st_value;
9356
9357 return true;
9358}
9359
9360/* Finish up the dynamic sections. */
9361
103186c6
MM
9362boolean
9363_bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
252b5132
RH
9364 bfd *output_bfd;
9365 struct bfd_link_info *info;
9366{
9367 bfd *dynobj;
9368 asection *sdyn;
9369 asection *sgot;
9370 struct mips_got_info *g;
9371
9372 dynobj = elf_hash_table (info)->dynobj;
9373
9374 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9375
103186c6 9376 sgot = mips_elf_got_section (dynobj);
252b5132
RH
9377 if (sgot == NULL)
9378 g = NULL;
9379 else
9380 {
9381 BFD_ASSERT (elf_section_data (sgot) != NULL);
9382 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
9383 BFD_ASSERT (g != NULL);
9384 }
9385
9386 if (elf_hash_table (info)->dynamic_sections_created)
9387 {
103186c6 9388 bfd_byte *b;
252b5132
RH
9389
9390 BFD_ASSERT (sdyn != NULL);
9391 BFD_ASSERT (g != NULL);
9392
103186c6
MM
9393 for (b = sdyn->contents;
9394 b < sdyn->contents + sdyn->_raw_size;
9395 b += MIPS_ELF_DYN_SIZE (dynobj))
252b5132
RH
9396 {
9397 Elf_Internal_Dyn dyn;
9398 const char *name;
9399 size_t elemsize;
9400 asection *s;
103186c6 9401 boolean swap_out_p;
252b5132 9402
103186c6
MM
9403 /* Read in the current dynamic entry. */
9404 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
be3ccd9c 9405
103186c6
MM
9406 /* Assume that we're going to modify it and write it out. */
9407 swap_out_p = true;
252b5132
RH
9408
9409 switch (dyn.d_tag)
9410 {
252b5132 9411 case DT_RELENT:
be3ccd9c 9412 s = (bfd_get_section_by_name
103186c6
MM
9413 (dynobj,
9414 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
252b5132 9415 BFD_ASSERT (s != NULL);
103186c6 9416 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
252b5132
RH
9417 break;
9418
9419 case DT_STRSZ:
9420 /* Rewrite DT_STRSZ. */
9421 dyn.d_un.d_val =
350836e3 9422 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
252b5132
RH
9423 break;
9424
9425 case DT_PLTGOT:
9426 name = ".got";
9427 goto get_vma;
9428 case DT_MIPS_CONFLICT:
9429 name = ".conflict";
9430 goto get_vma;
9431 case DT_MIPS_LIBLIST:
9432 name = ".liblist";
9433 get_vma:
9434 s = bfd_get_section_by_name (output_bfd, name);
9435 BFD_ASSERT (s != NULL);
9436 dyn.d_un.d_ptr = s->vma;
252b5132
RH
9437 break;
9438
9439 case DT_MIPS_RLD_VERSION:
9440 dyn.d_un.d_val = 1; /* XXX */
252b5132
RH
9441 break;
9442
9443 case DT_MIPS_FLAGS:
9444 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
252b5132
RH
9445 break;
9446
9447 case DT_MIPS_CONFLICTNO:
9448 name = ".conflict";
9449 elemsize = sizeof (Elf32_Conflict);
9450 goto set_elemno;
9451
9452 case DT_MIPS_LIBLISTNO:
9453 name = ".liblist";
9454 elemsize = sizeof (Elf32_Lib);
9455 set_elemno:
9456 s = bfd_get_section_by_name (output_bfd, name);
9457 if (s != NULL)
9458 {
9459 if (s->_cooked_size != 0)
9460 dyn.d_un.d_val = s->_cooked_size / elemsize;
9461 else
9462 dyn.d_un.d_val = s->_raw_size / elemsize;
9463 }
9464 else
be3ccd9c 9465 dyn.d_un.d_val = 0;
252b5132
RH
9466 break;
9467
9468 case DT_MIPS_TIME_STAMP:
9469 time ((time_t *) &dyn.d_un.d_val);
252b5132
RH
9470 break;
9471
9472 case DT_MIPS_ICHECKSUM:
9473 /* XXX FIXME: */
103186c6 9474 swap_out_p = false;
252b5132
RH
9475 break;
9476
9477 case DT_MIPS_IVERSION:
9478 /* XXX FIXME: */
103186c6 9479 swap_out_p = false;
252b5132
RH
9480 break;
9481
9482 case DT_MIPS_BASE_ADDRESS:
9483 s = output_bfd->sections;
9484 BFD_ASSERT (s != NULL);
dc810e39 9485 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
252b5132
RH
9486 break;
9487
9488 case DT_MIPS_LOCAL_GOTNO:
9489 dyn.d_un.d_val = g->local_gotno;
252b5132
RH
9490 break;
9491
5499724a
MM
9492 case DT_MIPS_UNREFEXTNO:
9493 /* The index into the dynamic symbol table which is the
9494 entry of the first external symbol that is not
9495 referenced within the same object. */
9496 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
9497 break;
9498
9499 case DT_MIPS_GOTSYM:
9500 if (g->global_gotsym)
9501 {
9502 dyn.d_un.d_val = g->global_gotsym->dynindx;
9503 break;
9504 }
9505 /* In case if we don't have global got symbols we default
9506 to setting DT_MIPS_GOTSYM to the same value as
9507 DT_MIPS_SYMTABNO, so we just fall through. */
9508
252b5132
RH
9509 case DT_MIPS_SYMTABNO:
9510 name = ".dynsym";
103186c6 9511 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
252b5132
RH
9512 s = bfd_get_section_by_name (output_bfd, name);
9513 BFD_ASSERT (s != NULL);
9514
9515 if (s->_cooked_size != 0)
9516 dyn.d_un.d_val = s->_cooked_size / elemsize;
9517 else
9518 dyn.d_un.d_val = s->_raw_size / elemsize;
252b5132
RH
9519 break;
9520
252b5132
RH
9521 case DT_MIPS_HIPAGENO:
9522 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
252b5132
RH
9523 break;
9524
9525 case DT_MIPS_RLD_MAP:
9526 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
252b5132
RH
9527 break;
9528
7403cb63 9529 case DT_MIPS_OPTIONS:
be3ccd9c 9530 s = (bfd_get_section_by_name
7403cb63
MM
9531 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
9532 dyn.d_un.d_ptr = s->vma;
7403cb63
MM
9533 break;
9534
c6142e5d 9535 case DT_MIPS_MSYM:
be3ccd9c 9536 s = (bfd_get_section_by_name
c6142e5d
MM
9537 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
9538 dyn.d_un.d_ptr = s->vma;
103186c6
MM
9539 break;
9540
9541 default:
9542 swap_out_p = false;
c6142e5d 9543 break;
252b5132 9544 }
103186c6
MM
9545
9546 if (swap_out_p)
be3ccd9c 9547 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
103186c6 9548 (dynobj, &dyn, b);
252b5132
RH
9549 }
9550 }
9551
9552 /* The first entry of the global offset table will be filled at
9553 runtime. The second entry will be used by some runtime loaders.
be3ccd9c 9554 This isn't the case of Irix rld. */
252b5132
RH
9555 if (sgot != NULL && sgot->_raw_size > 0)
9556 {
103186c6 9557 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
be3ccd9c 9558 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
103186c6 9559 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
252b5132
RH
9560 }
9561
9562 if (sgot != NULL)
103186c6
MM
9563 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
9564 = MIPS_ELF_GOT_SIZE (output_bfd);
252b5132
RH
9565
9566 {
c6142e5d 9567 asection *smsym;
252b5132 9568 asection *s;
252b5132
RH
9569 Elf32_compact_rel cpt;
9570
30b30c21
RH
9571 /* ??? The section symbols for the output sections were set up in
9572 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
9573 symbols. Should we do so? */
252b5132 9574
be3ccd9c 9575 smsym = bfd_get_section_by_name (dynobj,
c6142e5d 9576 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
30b30c21 9577 if (smsym != NULL)
252b5132 9578 {
103186c6 9579 Elf32_Internal_Msym msym;
c6142e5d 9580
103186c6
MM
9581 msym.ms_hash_value = 0;
9582 msym.ms_info = ELF32_MS_INFO (0, 1);
c6142e5d 9583
103186c6
MM
9584 for (s = output_bfd->sections; s != NULL; s = s->next)
9585 {
30b30c21 9586 long dynindx = elf_section_data (s)->dynindx;
252b5132 9587
be3ccd9c 9588 bfd_mips_elf_swap_msym_out
30b30c21
RH
9589 (output_bfd, &msym,
9590 (((Elf32_External_Msym *) smsym->contents)
9591 + dynindx));
9592 }
252b5132
RH
9593 }
9594
9595 if (SGI_COMPAT (output_bfd))
9596 {
9597 /* Write .compact_rel section out. */
9598 s = bfd_get_section_by_name (dynobj, ".compact_rel");
9599 if (s != NULL)
9600 {
9601 cpt.id1 = 1;
9602 cpt.num = s->reloc_count;
9603 cpt.id2 = 2;
9604 cpt.offset = (s->output_section->filepos
9605 + sizeof (Elf32_External_compact_rel));
9606 cpt.reserved0 = 0;
9607 cpt.reserved1 = 0;
9608 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
9609 ((Elf32_External_compact_rel *)
9610 s->contents));
9611
9612 /* Clean up a dummy stub function entry in .text. */
be3ccd9c 9613 s = bfd_get_section_by_name (dynobj,
303f629d 9614 MIPS_ELF_STUB_SECTION_NAME (dynobj));
252b5132
RH
9615 if (s != NULL)
9616 {
9617 file_ptr dummy_offset;
9618
9619 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
9620 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
9621 memset (s->contents + dummy_offset, 0,
9622 MIPS_FUNCTION_STUB_SIZE);
9623 }
9624 }
9625 }
9626
adb76a3e
UC
9627 /* We need to sort the entries of the dynamic relocation section. */
9628
9629 if (!ABI_64_P (output_bfd))
9630 {
be3ccd9c
KH
9631 asection *reldyn;
9632
9633 reldyn = bfd_get_section_by_name (dynobj,
9634 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
9635 if (reldyn != NULL && reldyn->reloc_count > 2)
9636 {
9637 reldyn_sorting_bfd = output_bfd;
9638 qsort ((Elf32_External_Rel *) reldyn->contents + 1,
9639 (size_t) reldyn->reloc_count - 1,
9640 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
9641 }
adb76a3e
UC
9642 }
9643
252b5132 9644 /* Clean up a first relocation in .rel.dyn. */
be3ccd9c 9645 s = bfd_get_section_by_name (dynobj,
103186c6 9646 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
252b5132 9647 if (s != NULL && s->_raw_size > 0)
103186c6 9648 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
252b5132
RH
9649 }
9650
9651 return true;
9652}
9653\f
bb0082d6
AM
9654/* Support for core dump NOTE sections */
9655static boolean
9656_bfd_elf32_mips_grok_prstatus (abfd, note)
9657 bfd *abfd;
9658 Elf_Internal_Note *note;
9659{
9660 int offset;
dc810e39 9661 unsigned int raw_size;
bb0082d6
AM
9662
9663 switch (note->descsz)
9664 {
9665 default:
9666 return false;
9667
9668 case 256: /* Linux/MIPS */
9669 /* pr_cursig */
9670 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
9671
9672 /* pr_pid */
9673 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
9674
9675 /* pr_reg */
9676 offset = 72;
9677 raw_size = 180;
9678
9679 break;
9680 }
9681
9682 /* Make a ".reg/999" section. */
936e320b
AM
9683 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9684 raw_size, note->descpos + offset);
bb0082d6
AM
9685}
9686
9e80ff3a
L
9687static boolean
9688_bfd_elf32_mips_grok_psinfo (abfd, note)
bb0082d6
AM
9689 bfd *abfd;
9690 Elf_Internal_Note *note;
9691{
9692 switch (note->descsz)
9693 {
9694 default:
9695 return false;
9696
9697 case 128: /* Linux/MIPS elf_prpsinfo */
9698 elf_tdata (abfd)->core_program
9699 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
9700 elf_tdata (abfd)->core_command
9701 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
9702 }
9703
9704 /* Note that for some reason, a spurious space is tacked
9705 onto the end of the args in some (at least one anyway)
9706 implementations, so strip it off if it exists. */
9707
9708 {
9709 char *command = elf_tdata (abfd)->core_command;
9710 int n = strlen (command);
9711
9712 if (0 < n && command[n - 1] == ' ')
9713 command[n - 1] = '\0';
9714 }
9715
9716 return true;
9717}
9718\f
73d074b4
DJ
9719#define PDR_SIZE 32
9720
9721static boolean
9722_bfd_elf32_mips_discard_info (abfd, cookie, info)
9723 bfd *abfd;
9724 struct elf_reloc_cookie *cookie;
9725 struct bfd_link_info *info;
9726{
9727 asection *o;
9728 struct elf_backend_data *bed = get_elf_backend_data (abfd);
9729 boolean ret = false;
9730 unsigned char *tdata;
9731 size_t i, skip;
9732
9733 o = bfd_get_section_by_name (abfd, ".pdr");
9734 if (! o)
9735 return false;
9736 if (o->_raw_size == 0)
9737 return false;
9738 if (o->_raw_size % PDR_SIZE != 0)
9739 return false;
9740 if (o->output_section != NULL
9741 && bfd_is_abs_section (o->output_section))
9742 return false;
9743
9744 tdata = bfd_zmalloc (o->_raw_size / PDR_SIZE);
9745 if (! tdata)
9746 return false;
9747
9748 cookie->rels = _bfd_elf32_link_read_relocs (abfd, o, (PTR) NULL,
9749 (Elf_Internal_Rela *) NULL,
9750 info->keep_memory);
9751 if (!cookie->rels)
9752 {
9753 free (tdata);
9754 return false;
9755 }
9756
9757 cookie->rel = cookie->rels;
9758 cookie->relend =
9759 cookie->rels + o->reloc_count * bed->s->int_rels_per_ext_rel;
175e9962 9760
73d074b4
DJ
9761 for (i = 0, skip = 0; i < o->_raw_size; i ++)
9762 {
9763 if (_bfd_elf32_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
9764 {
9765 tdata[i] = 1;
9766 skip ++;
9767 }
9768 }
9769
9770 if (skip != 0)
9771 {
9772 elf_section_data (o)->tdata = tdata;
9773 o->_cooked_size = o->_raw_size - skip * PDR_SIZE;
9774 ret = true;
9775 }
9776 else
9777 free (tdata);
9778
9779 if (! info->keep_memory)
9780 free (cookie->rels);
9781
9782 return ret;
9783}
9784
9785static boolean
9786_bfd_elf32_mips_ignore_discarded_relocs (sec)
9787 asection *sec;
9788{
9789 if (strcmp (sec->name, ".pdr") == 0)
9790 return true;
9791 return false;
9792}
9793
9794static boolean
9795_bfd_elf32_mips_write_section (output_bfd, sec, contents)
9796 bfd *output_bfd;
9797 asection *sec;
9798 bfd_byte *contents;
9799{
9800 bfd_byte *to, *from, *end;
9801 int i;
9802
9803 if (strcmp (sec->name, ".pdr") != 0)
9804 return false;
9805
9806 if (elf_section_data (sec)->tdata == NULL)
9807 return false;
9808
9809 to = contents;
9810 end = contents + sec->_raw_size;
9811 for (from = contents, i = 0;
9812 from < end;
9813 from += PDR_SIZE, i++)
9814 {
9815 if (((unsigned char *)elf_section_data (sec)->tdata)[i] == 1)
9816 continue;
9817 if (to != from)
9818 memcpy (to, from, PDR_SIZE);
9819 to += PDR_SIZE;
9820 }
9821 bfd_set_section_contents (output_bfd, sec->output_section, contents,
9822 (file_ptr) sec->output_offset,
9823 sec->_cooked_size);
9824 return true;
9825}
9826\f
252b5132
RH
9827/* This is almost identical to bfd_generic_get_... except that some
9828 MIPS relocations need to be handled specially. Sigh. */
9829
9830static bfd_byte *
9831elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
9832 relocateable, symbols)
9833 bfd *abfd;
9834 struct bfd_link_info *link_info;
9835 struct bfd_link_order *link_order;
9836 bfd_byte *data;
9837 boolean relocateable;
9838 asymbol **symbols;
9839{
9840 /* Get enough memory to hold the stuff */
9841 bfd *input_bfd = link_order->u.indirect.section->owner;
9842 asection *input_section = link_order->u.indirect.section;
9843
9844 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
9845 arelent **reloc_vector = NULL;
9846 long reloc_count;
9847
9848 if (reloc_size < 0)
9849 goto error_return;
9850
dc810e39 9851 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
252b5132
RH
9852 if (reloc_vector == NULL && reloc_size != 0)
9853 goto error_return;
9854
9855 /* read in the section */
9856 if (!bfd_get_section_contents (input_bfd,
9857 input_section,
9858 (PTR) data,
dc810e39 9859 (file_ptr) 0,
252b5132
RH
9860 input_section->_raw_size))
9861 goto error_return;
9862
9863 /* We're not relaxing the section, so just copy the size info */
9864 input_section->_cooked_size = input_section->_raw_size;
9865 input_section->reloc_done = true;
9866
9867 reloc_count = bfd_canonicalize_reloc (input_bfd,
9868 input_section,
9869 reloc_vector,
9870 symbols);
9871 if (reloc_count < 0)
9872 goto error_return;
9873
9874 if (reloc_count > 0)
9875 {
9876 arelent **parent;
9877 /* for mips */
9878 int gp_found;
9879 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
9880
9881 {
9882 struct bfd_hash_entry *h;
9883 struct bfd_link_hash_entry *lh;
9884 /* Skip all this stuff if we aren't mixing formats. */
9885 if (abfd && input_bfd
9886 && abfd->xvec == input_bfd->xvec)
9887 lh = 0;
9888 else
9889 {
9890 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
9891 lh = (struct bfd_link_hash_entry *) h;
9892 }
9893 lookup:
9894 if (lh)
9895 {
9896 switch (lh->type)
9897 {
9898 case bfd_link_hash_undefined:
9899 case bfd_link_hash_undefweak:
9900 case bfd_link_hash_common:
9901 gp_found = 0;
9902 break;
9903 case bfd_link_hash_defined:
9904 case bfd_link_hash_defweak:
9905 gp_found = 1;
9906 gp = lh->u.def.value;
9907 break;
9908 case bfd_link_hash_indirect:
9909 case bfd_link_hash_warning:
9910 lh = lh->u.i.link;
9911 /* @@FIXME ignoring warning for now */
9912 goto lookup;
9913 case bfd_link_hash_new:
9914 default:
9915 abort ();
9916 }
9917 }
9918 else
9919 gp_found = 0;
9920 }
9921 /* end mips */
9922 for (parent = reloc_vector; *parent != (arelent *) NULL;
9923 parent++)
9924 {
9925 char *error_message = (char *) NULL;
9926 bfd_reloc_status_type r;
9927
9928 /* Specific to MIPS: Deal with relocation types that require
9929 knowing the gp of the output bfd. */
9930 asymbol *sym = *(*parent)->sym_ptr_ptr;
9931 if (bfd_is_abs_section (sym->section) && abfd)
9932 {
9933 /* The special_function wouldn't get called anyways. */
9934 }
9935 else if (!gp_found)
9936 {
9937 /* The gp isn't there; let the special function code
9938 fall over on its own. */
9939 }
9940 else if ((*parent)->howto->special_function
9941 == _bfd_mips_elf_gprel16_reloc)
9942 {
9943 /* bypass special_function call */
9944 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
9945 relocateable, (PTR) data, gp);
9946 goto skip_bfd_perform_relocation;
9947 }
9948 /* end mips specific stuff */
9949
9950 r = bfd_perform_relocation (input_bfd,
9951 *parent,
9952 (PTR) data,
9953 input_section,
9954 relocateable ? abfd : (bfd *) NULL,
9955 &error_message);
9956 skip_bfd_perform_relocation:
9957
9958 if (relocateable)
9959 {
9960 asection *os = input_section->output_section;
9961
9962 /* A partial link, so keep the relocs */
9963 os->orelocation[os->reloc_count] = *parent;
9964 os->reloc_count++;
9965 }
9966
9967 if (r != bfd_reloc_ok)
9968 {
9969 switch (r)
9970 {
9971 case bfd_reloc_undefined:
9972 if (!((*link_info->callbacks->undefined_symbol)
9973 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5cc7c785
L
9974 input_bfd, input_section, (*parent)->address,
9975 true)))
252b5132
RH
9976 goto error_return;
9977 break;
9978 case bfd_reloc_dangerous:
9979 BFD_ASSERT (error_message != (char *) NULL);
9980 if (!((*link_info->callbacks->reloc_dangerous)
9981 (link_info, error_message, input_bfd, input_section,
9982 (*parent)->address)))
9983 goto error_return;
9984 break;
9985 case bfd_reloc_overflow:
9986 if (!((*link_info->callbacks->reloc_overflow)
9987 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
9988 (*parent)->howto->name, (*parent)->addend,
9989 input_bfd, input_section, (*parent)->address)))
9990 goto error_return;
9991 break;
9992 case bfd_reloc_outofrange:
9993 default:
9994 abort ();
9995 break;
9996 }
9997
9998 }
9999 }
10000 }
10001 if (reloc_vector != NULL)
10002 free (reloc_vector);
10003 return data;
10004
10005error_return:
10006 if (reloc_vector != NULL)
10007 free (reloc_vector);
10008 return NULL;
10009}
be3ccd9c 10010
252b5132
RH
10011#define bfd_elf32_bfd_get_relocated_section_contents \
10012 elf32_mips_get_relocated_section_contents
10013\f
10014/* ECOFF swapping routines. These are used when dealing with the
10015 .mdebug section, which is in the ECOFF debugging format. */
be3ccd9c 10016static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
252b5132
RH
10017 /* Symbol table magic number. */
10018 magicSym,
10019 /* Alignment of debugging information. E.g., 4. */
10020 4,
10021 /* Sizes of external symbolic information. */
10022 sizeof (struct hdr_ext),
10023 sizeof (struct dnr_ext),
10024 sizeof (struct pdr_ext),
10025 sizeof (struct sym_ext),
10026 sizeof (struct opt_ext),
10027 sizeof (struct fdr_ext),
10028 sizeof (struct rfd_ext),
10029 sizeof (struct ext_ext),
10030 /* Functions to swap in external symbolic data. */
10031 ecoff_swap_hdr_in,
10032 ecoff_swap_dnr_in,
10033 ecoff_swap_pdr_in,
10034 ecoff_swap_sym_in,
10035 ecoff_swap_opt_in,
10036 ecoff_swap_fdr_in,
10037 ecoff_swap_rfd_in,
10038 ecoff_swap_ext_in,
10039 _bfd_ecoff_swap_tir_in,
10040 _bfd_ecoff_swap_rndx_in,
10041 /* Functions to swap out external symbolic data. */
10042 ecoff_swap_hdr_out,
10043 ecoff_swap_dnr_out,
10044 ecoff_swap_pdr_out,
10045 ecoff_swap_sym_out,
10046 ecoff_swap_opt_out,
10047 ecoff_swap_fdr_out,
10048 ecoff_swap_rfd_out,
10049 ecoff_swap_ext_out,
10050 _bfd_ecoff_swap_tir_out,
10051 _bfd_ecoff_swap_rndx_out,
10052 /* Function to read in symbolic data. */
10053 _bfd_mips_elf_read_ecoff_info
10054};
10055\f
252b5132
RH
10056#define ELF_ARCH bfd_arch_mips
10057#define ELF_MACHINE_CODE EM_MIPS
10058
10059/* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
10060 a value of 0x1000, and we are compatible. */
10061#define ELF_MAXPAGESIZE 0x1000
10062
10063#define elf_backend_collect true
10064#define elf_backend_type_change_ok true
10065#define elf_backend_can_gc_sections true
3f830999 10066#define elf_info_to_howto mips_info_to_howto_rela
252b5132
RH
10067#define elf_info_to_howto_rel mips_info_to_howto_rel
10068#define elf_backend_sym_is_global mips_elf_sym_is_global
103186c6 10069#define elf_backend_object_p _bfd_mips_elf_object_p
d75bc93d
TS
10070#define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
10071#define elf_backend_section_processing _bfd_mips_elf_section_processing
103186c6 10072#define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
252b5132
RH
10073#define elf_backend_fake_sections _bfd_mips_elf_fake_sections
10074#define elf_backend_section_from_bfd_section \
10075 _bfd_mips_elf_section_from_bfd_section
103186c6 10076#define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
d75bc93d
TS
10077#define elf_backend_link_output_symbol_hook \
10078 _bfd_mips_elf_link_output_symbol_hook
103186c6
MM
10079#define elf_backend_create_dynamic_sections \
10080 _bfd_mips_elf_create_dynamic_sections
10081#define elf_backend_check_relocs _bfd_mips_elf_check_relocs
10082#define elf_backend_adjust_dynamic_symbol \
10083 _bfd_mips_elf_adjust_dynamic_symbol
10084#define elf_backend_always_size_sections \
10085 _bfd_mips_elf_always_size_sections
10086#define elf_backend_size_dynamic_sections \
10087 _bfd_mips_elf_size_dynamic_sections
10088#define elf_backend_relocate_section _bfd_mips_elf_relocate_section
103186c6
MM
10089#define elf_backend_finish_dynamic_symbol \
10090 _bfd_mips_elf_finish_dynamic_symbol
10091#define elf_backend_finish_dynamic_sections \
10092 _bfd_mips_elf_finish_dynamic_sections
d75bc93d
TS
10093#define elf_backend_final_write_processing \
10094 _bfd_mips_elf_final_write_processing
10095#define elf_backend_additional_program_headers \
10096 _bfd_mips_elf_additional_program_headers
10097#define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
103186c6
MM
10098#define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
10099#define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8a20f077
UC
10100#define elf_backend_copy_indirect_symbol \
10101 _bfd_mips_elf_copy_indirect_symbol
b305ef96 10102#define elf_backend_hide_symbol _bfd_mips_elf_hide_symbol
bb0082d6
AM
10103#define elf_backend_grok_prstatus _bfd_elf32_mips_grok_prstatus
10104#define elf_backend_grok_psinfo _bfd_elf32_mips_grok_psinfo
d75bc93d
TS
10105#define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
10106
10107#define elf_backend_got_header_size (4 * MIPS_RESERVED_GOTNO)
10108#define elf_backend_plt_header_size 0
10109#define elf_backend_may_use_rel_p 1
10110#define elf_backend_may_use_rela_p 0
10111#define elf_backend_default_use_rela_p 0
10112#define elf_backend_sign_extend_vma true
b305ef96 10113
73d074b4
DJ
10114#define elf_backend_discard_info _bfd_elf32_mips_discard_info
10115#define elf_backend_ignore_discarded_relocs \
10116 _bfd_elf32_mips_ignore_discarded_relocs
10117#define elf_backend_write_section _bfd_elf32_mips_write_section
10118
252b5132
RH
10119#define bfd_elf32_bfd_is_local_label_name \
10120 mips_elf_is_local_label_name
10121#define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
10122#define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
10123#define bfd_elf32_bfd_link_hash_table_create \
103186c6
MM
10124 _bfd_mips_elf_link_hash_table_create
10125#define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
252b5132
RH
10126#define bfd_elf32_bfd_merge_private_bfd_data \
10127 _bfd_mips_elf_merge_private_bfd_data
10128#define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
10129#define bfd_elf32_bfd_print_private_bfd_data \
10130 _bfd_mips_elf_print_private_bfd_data
e364195d 10131
d75bc93d
TS
10132/* Support for SGI-ish mips targets. */
10133#define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
10134#define TARGET_LITTLE_NAME "elf32-littlemips"
10135#define TARGET_BIG_SYM bfd_elf32_bigmips_vec
10136#define TARGET_BIG_NAME "elf32-bigmips"
10137
10138#include "elf32-target.h"
e364195d 10139
d75bc93d
TS
10140/* Support for traditional mips targets. */
10141#define INCLUDED_TARGET_FILE /* More a type of flag. */
e364195d
UC
10142
10143#undef TARGET_LITTLE_SYM
10144#undef TARGET_LITTLE_NAME
10145#undef TARGET_BIG_SYM
10146#undef TARGET_BIG_NAME
10147
10148#define TARGET_LITTLE_SYM bfd_elf32_tradlittlemips_vec
10149#define TARGET_LITTLE_NAME "elf32-tradlittlemips"
10150#define TARGET_BIG_SYM bfd_elf32_tradbigmips_vec
10151#define TARGET_BIG_NAME "elf32-tradbigmips"
10152
10153/* Include the target file again for this target */
10154#include "elf32-target.h"
This page took 0.670651 seconds and 4 git commands to generate.