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