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