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