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