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