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