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