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