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