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