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