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