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