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