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