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