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