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