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