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