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