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