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